summaryrefslogtreecommitdiff
path: root/src/arch/dalvik/instruction.c
blob: 051bbe27859921c9de487cd834bf73d4f5dccd6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

/* OpenIDA - Outil d'analyse de fichiers binaires
 * instruction.c - gestion des instructions de la VM Dalvik
 *
 * Copyright (C) 2010 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "instruction.h"


#include "translate.h"
#include "../instruction-int.h"



/* Définition générique d'une instruction d'architecture Dalvik (instance) */
struct _GDalvikInstruction
{
    GArchInstruction parent;                /* A laisser en premier        */

    DalvikOpcodes type;                     /* Position dans la liste      */

};

/* Définition générique d'une instruction d'architecture Dalvik (classe) */
struct _GDalvikInstructionClass
{
    GArchInstructionClass parent;           /* A laisser en premier        */

};


/* Initialise la classe des instructions pour Dalvik. */
static void g_dalvik_instruction_class_init(GDalvikInstructionClass *);

/* Initialise une instance d'opérande d'architecture Dalvik. */
static void g_dalvik_instruction_init(GDalvikInstruction *);



/* --------------------- AIDE A LA MISE EN PLACE D'INSTRUCTIONS --------------------- */


/* Répertoire de toutes les instructions Dalvik */
typedef struct _dalvik_instruction
{
    bin_t opcode;                           /* Opcode de l'instruction     */

    const char *keyword;                    /* Mot clef de la commande     */

    decomp_instr_fc decomp;                 /* Procédure de décompilation  */

} dalvik_instruction;


static dalvik_instruction _instructions[DOP_COUNT] = {

    [DOP_NOP]                   = { 0x00, "nop" },
    [DOP_MOVE]                  = { 0x01, "move" },

    [DOP_MOVE_OBJECT]           = { 0x07, "move-object" },

    [DOP_MOVE_RESULT]           = { 0x0a, "move-result" },
    [DOP_MOVE_RESULT_WIDE]      = { 0x0b, "move-result-wide" },
    [DOP_MOVE_RESULT_OBJECT]    = { 0x0c, "move-result-object" },
    [DOP_MOVE_EXCEPTION]        = { 0x0d, "move-exception" },
    [DOP_RETURN_VOID]           = { 0x0e, "return-void",        dalvik_decomp_instr_return_void },
    [DOP_RETURN]                = { 0x0f, "return",             dalvik_decomp_instr_return },
    [DOP_RETURN_WIDE]           = { 0x10, "return-wide",        dalvik_decomp_instr_return },
    [DOP_RETURN_OBJECT]         = { 0x11, "return-object",      dalvik_decomp_instr_return },
    [DOP_CONST_4]               = { 0x12, "const/4",            dalvik_decomp_instr_const },
    [DOP_CONST_16]              = { 0x13, "const/16",           dalvik_decomp_instr_const },
    [DOP_CONST]                 = { 0x14, "const" },
    [DOP_CONST_HIGH16]          = { 0x15, "const/high16" },
    [DOP_CONST_WIDE_16]         = { 0x16, "const-wide/16" },

    [DOP_CONST_WIDE]            = { 0x18, "const-wide" },

    [DOP_CONST_STRING]          = { 0x1a, "const-string" },

    [DOP_CHECK_CAST]            = { 0x1f, "check-cast" },


    [DOP_ARRAY_LENGTH]          = { 0x21, "array-length",       dalvik_decomp_instr_array_length },
    [DOP_NEW_INSTANCE]          = { 0x22, "new-instance" },
    [DOP_NEW_ARRAY]             = { 0x23, "new-array" },

    [DOP_GOTO]                  = { 0x28, "goto" },
    [DOP_GOTO_16]               = { 0x29, "goto/16" },
    [DOP_GOTO_32]               = { 0x2a, "goto/32" },

    [DOP_CMPL_FLOAT]            = { 0x2d, "cmp-long" },
    [DOP_CMPG_FLOAT]            = { 0x2e, "cmpg-float" },
    [DOP_CMPL_DOUBLE]           = { 0x2f, "cmpl-double" },
    [DOP_CMPG_DOUBLE]           = { 0x30, "cmpg-double" },
    [DOP_CMP_LONG]              = { 0x31, "cmp-long" },
    [DOP_IF_EQ]                 = { 0x32, "if-eq",              dalvik_decomp_instr_if },
    [DOP_IF_NE]                 = { 0x33, "if-ne",              dalvik_decomp_instr_if },
    [DOP_IF_LT]                 = { 0x34, "if-lt",              dalvik_decomp_instr_if },
    [DOP_IF_GE]                 = { 0x35, "if-ge",              dalvik_decomp_instr_if },
    [DOP_IF_GT]                 = { 0x36, "if-gt",              dalvik_decomp_instr_if },
    [DOP_IF_LE]                 = { 0x37, "if-le",              dalvik_decomp_instr_if },
    [DOP_IF_EQZ]                = { 0x38, "if-eqz" },
    [DOP_IF_NEZ]                = { 0x39, "if-nez" },
    [DOP_IF_LTZ]                = { 0x3a, "if-ltz" },
    [DOP_IF_GEZ]                = { 0x3b, "if-gez" },
    [DOP_IF_GTZ]                = { 0x3c, "if-gtz" },
    [DOP_IF_LEZ]                = { 0x3d, "if-lez" },



    [DOP_AGET]                  = { 0x44, "aget" },
    [DOP_AGET_WIDE]             = { 0x45, "aget-wide" },
    [DOP_AGET_OBJECT]           = { 0x46, "aget-object" },
    [DOP_AGET_BOOLEAN]          = { 0x47, "aget-boolean" },
    [DOP_AGET_BYTE]             = { 0x48, "aget-byte",          dalvik_decomp_instr_aget },
    [DOP_AGET_CHAR]             = { 0x49, "aget-char" },
    [DOP_AGET_SHORT]            = { 0x4a, "aget-short" },
    [DOP_APUT]                  = { 0x4b, "aput" },
    [DOP_APUT_WIDE]             = { 0x4c, "aput-wide" },
    [DOP_APUT_OBJECT]           = { 0x4d, "aput-object" },
    [DOP_APUT_BOOLEAN]          = { 0x4e, "aput-boolean" },
    [DOP_APUT_BYTE]             = { 0x4f, "aput-byte",          dalvik_decomp_instr_aput },
    [DOP_APUT_CHAR]             = { 0x50, "aput-char" },
    [DOP_APUT_SHORT]            = { 0x51, "aput-short" },
    [DOP_IGET]                  = { 0x52, "iget" },
    [DOP_IGET_WIDE]             = { 0x53, "iget-wide" },
    [DOP_IGET_OBJECT]           = { 0x54, "iget-object" },
    [DOP_IGET_BOOLEAN]          = { 0x55, "iget-boolean" },
    [DOP_IGET_BYTE]             = { 0x56, "iget-byte" },
    [DOP_IGET_CHAR]             = { 0x57, "iget-char" },
    [DOP_IGET_SHORT]            = { 0x58, "iget-short" },
    [DOP_IPUT]                  = { 0x59, "iput" },
    [DOP_IPUT_WIDE]             = { 0x5a, "iput-wide" },
    [DOP_IPUT_OBJECT]           = { 0x5b, "iput-object" },
    [DOP_IPUT_BOOLEAN]          = { 0x5c, "iput-boolean" },
    [DOP_IPUT_BYTE]             = { 0x5d, "iput-byte" },
    [DOP_IPUT_CHAR]             = { 0x5e, "iput-char" },
    [DOP_IPUT_SHORT]            = { 0x5f, "iput-short" },
    [DOP_SGET]                  = { 0x60, "sget" },
    [DOP_SGET_WIDE]             = { 0x61, "sget-wide" },
    [DOP_SGET_OBJECT]           = { 0x62, "sget-object" },
    [DOP_SGET_BOOLEAN]          = { 0x63, "sget-boolean" },
    [DOP_SGET_BYTE]             = { 0x64, "sget-byte" },
    [DOP_SGET_CHAR]             = { 0x65, "sget-char" },
    [DOP_SGET_SHORT]            = { 0x66, "sget-short" },
    [DOP_SPUT]                  = { 0x67, "sput" },
    [DOP_SPUT_WIDE]             = { 0x68, "sput-wide" },
    [DOP_SPUT_OBJECT]           = { 0x69, "sput-object" },
    [DOP_SPUT_BOOLEAN]          = { 0x6a, "sput-boolean" },
    [DOP_SPUT_BYTE]             = { 0x6b, "sput-byte" },
    [DOP_SPUT_CHAR]             = { 0x6c, "sput-char" },
    [DOP_SPUT_SHORT]            = { 0x6d, "sput-short" },
    [DOP_INVOKE_VIRTUAL]        = { 0x6e, "invoke-virtual",     dalvik_decomp_instr_invoke_virtual },
    [DOP_INVOKE_SUPER]          = { 0x6f, "invoke-static" },
    [DOP_INVOKE_DIRECT]         = { 0x70, "invoke-direct" },
    [DOP_INVOKE_STATIC]         = { 0x71, "invoke-static" },
    [DOP_INVOKE_INTERFACE]      = { 0x72, "invoke-interface" },

    [DOP_TO_INT_LONG]           = { 0x81, "int-to-long" },
    [DOP_TO_INT_FLOAT]          = { 0x82, "int-to-float" },
    [DOP_TO_INT_DOUBLE]         = { 0x83, "int-to-double" },
    [DOP_TO_LONG_INT]           = { 0x84, "long-to-int" },
    [DOP_TO_LONG_FLOAT]         = { 0x85, "long-to-float" },
    [DOP_TO_LONG_DOUBLE]        = { 0x86, "long-to-double" },
    [DOP_TO_FLOAT_INT]          = { 0x87, "float-to-int" },
    [DOP_TO_FLOAT_LONG]         = { 0x88, "float-to-long" },
    [DOP_TO_FLOAT_DOUBLE]       = { 0x89, "float-to-double" },
    [DOP_TO_DOUBLE_INT]         = { 0x8a, "double-to-int" },
    [DOP_TO_DOUBLE_LONG]        = { 0x8b, "double-to-long" },
    [DOP_TO_DOUBLE_FLOAT]       = { 0x8c, "double-to-float" },
    [DOP_TO_INT_BYTE]           = { 0x8d, "int-to-byte" },
    [DOP_TO_INT_CHAR]           = { 0x8e, "int-to-char" },
    [DOP_TO_INT_SHORT]          = { 0x8f, "int-to-short" },
    [DOP_ADD_INT]               = { 0x90, "add-int",            dalvik_decomp_instr_arithm },
    [DOP_SUB_INT]               = { 0x91, "sub-int",            dalvik_decomp_instr_arithm },
    [DOP_MUL_INT]               = { 0x92, "mul-int",            dalvik_decomp_instr_arithm },
    [DOP_DIV_INT]               = { 0x93, "div-int",            dalvik_decomp_instr_arithm },
    [DOP_REM_INT]               = { 0x94, "rem-int",            dalvik_decomp_instr_arithm },
    [DOP_AND_INT]               = { 0x95, "and-int",            dalvik_decomp_instr_arithm },
    [DOP_OR_INT]                = { 0x96, "or-int",             dalvik_decomp_instr_arithm },
    [DOP_XOR_INT]               = { 0x97, "xor-int",            dalvik_decomp_instr_arithm },


    [DOP_ADD_INT_2ADDR]         = { 0xb0, "add-int/2addr",      dalvik_decomp_instr_arithm_2addr },


    [DOP_MUL_INT_2ADDR]         = { 0xb2, "mul-int/2addr",      dalvik_decomp_instr_arithm_2addr },
    [DOP_DIV_INT_2ADDR]         = { 0xb3, "div-int/2addr",      dalvik_decomp_instr_arithm_2addr },
    [DOP_REM_INT_2ADDR]         = { 0xb4, "rem-int/2addr",      dalvik_decomp_instr_arithm_2addr },
    [DOP_AND_INT_2ADDR]         = { 0xb5, "and-int/2addr",      dalvik_decomp_instr_arithm_2addr },
    [DOP_OR_INT_2ADDR]          = { 0xb6, "or-int/2addr",       dalvik_decomp_instr_arithm_2addr },
    [DOP_XOR_INT_2ADDR]         = { 0xb7, "xor-int/2addr",      dalvik_decomp_instr_arithm_2addr },

    [DOP_MUL_DOUBLE_2ADDR]      = { 0xcd, "mul-double/2addr",   dalvik_decomp_instr_arithm_2addr },

    [DOP_ADD_INT_LIT16]         = { 0xd0, "add-int/lit16",      dalvik_decomp_instr_arithm_lit },
    [DOP_RSUB_INT]              = { 0xd1, "rsub-int" },
    [DOP_MUL_INT_LIT16]         = { 0xd2, "mul-int/lit16",      dalvik_decomp_instr_arithm_lit },
    [DOP_DIV_INT_LIT16]         = { 0xd3, "div-int/lit16",      dalvik_decomp_instr_arithm_lit },
    [DOP_REM_INT_LIT16]         = { 0xd4, "rem-int/lit16",      dalvik_decomp_instr_arithm_lit },
    [DOP_AND_INT_LIT16]         = { 0xd5, "and-int/lit16",      dalvik_decomp_instr_arithm_lit },
    [DOP_OR_INT_LIT16]          = { 0xd6, "or-int/lit16",       dalvik_decomp_instr_arithm_lit },
    [DOP_XOR_INT_LIT16]         = { 0xd7, "xor-int/lit16",      dalvik_decomp_instr_arithm_lit },
    [DOP_ADD_INT_LIT8]          = { 0xd8, "add-int/lit8",       dalvik_decomp_instr_arithm_lit },
    [DOP_RSUB_INT_LIT8]         = { 0xd9, "rsub-int/lit8" },
    [DOP_MUL_INT_LIT8]          = { 0xda, "mul-int/lit8",       dalvik_decomp_instr_arithm_lit },
    [DOP_DIV_INT_LIT8]          = { 0xdb, "div-int/lit8",       dalvik_decomp_instr_arithm_lit },
    [DOP_REM_INT_LIT8]          = { 0xdc, "rem-int/lit8",       dalvik_decomp_instr_arithm_lit },
    [DOP_AND_INT_LIT8]          = { 0xdd, "and-int/lit8",       dalvik_decomp_instr_arithm_lit },
    [DOP_OR_INT_LIT8]           = { 0xde, "or-int/lit8",        dalvik_decomp_instr_arithm_lit },
    [DOP_XOR_INT_LIT8]          = { 0xdf, "xor-int/lit8",       dalvik_decomp_instr_arithm_lit }


};


/* Traduit une instruction en version humainement lisible. */
static const char *dalvik_get_instruction_text(const GDalvikInstruction *, const GExeFormat *, AsmSyntax);

/* Informe sur une éventuelle référence à une autre instruction. */
static InstructionLinkType dalvik_get_instruction_link(const GDalvikInstruction *, vmpa_t *);

/* Indique si l'instruction correspond à un retour de fonction. */
static bool dalvik_instruction_is_return(const GDalvikInstruction *);

/* Décompile une instruction de la machine virtuelle Dalvik. */
GDecInstruction *dalvik_instruction_decompile(const GDalvikInstruction *, GDecContext *);



/* Indique le type défini pour une instruction d'architecture Dalvik. */
G_DEFINE_TYPE(GDalvikInstruction, g_dalvik_instruction, G_TYPE_ARCH_INSTRUCTION);



/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des instructions pour Dalvik.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_instruction_class_init(GDalvikInstructionClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr = instance à initialiser.                              *
*                                                                             *
*  Description : Initialise une instance d'instruction d'architecture Dalvik. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_instruction_init(GDalvikInstruction *instr)
{
    GArchInstruction *parent;               /* Instance parente            */

    parent = G_ARCH_INSTRUCTION(instr);

    parent->get_text = (get_instruction_text_fc)dalvik_get_instruction_text;
    parent->get_link = (get_instruction_link_fc)dalvik_get_instruction_link;
    parent->is_return = (is_instruction_return_fc)dalvik_instruction_is_return;
    parent->decomp = (decomp_instr_fc)dalvik_instruction_decompile;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type d'instruction à représenter.                     *
*                                                                             *
*  Description : Crée une instruction pour l'architecture Dalvik.             *
*                                                                             *
*  Retour      : Architecture mise en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArchInstruction *g_dalvik_instruction_new(DalvikOpcodes type)
{
    GArchInstruction *result;               /* Structure à retourner       */

    result = g_object_new(G_TYPE_DALVIK_INSTRUCTION, NULL);

    G_DALVIK_INSTRUCTION(result)->type = type;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr = instruction Dalvik à consulter.                      *
*                                                                             *
*  Description : Indique l'opcode associé à une instruction Dalvik.           *
*                                                                             *
*  Retour      : Identifiant de l'instruction en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

DalvikOpcodes g_dalvik_instruction_get_opcode(const GDalvikInstruction *instr)
{
    return instr->type;

}



/* ---------------------------------------------------------------------------------- */
/*                       AIDE A LA MISE EN PLACE D'INSTRUCTIONS                       */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : data = flux de données à analyser.                           *
*                pos  = position courante dans ce flux.                       *
*                len  = taille totale des données à analyser.                 *
*                                                                             *
*  Description : Recherche l'identifiant de la prochaine instruction.         *
*                                                                             *
*  Retour      : Identifiant de la prochaine instruction à tenter de charger. *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

DalvikOpcodes dalvik_guess_next_instruction(const bin_t *data, off_t pos, off_t len)
{
    DalvikOpcodes result;                   /* Identifiant à retourner     */
    bin_t opcode;                           /* Opcode à trouver            */

    opcode = data[pos];

    for (result = 0; result < DOP_COUNT; result++)
    {
        if (_instructions[result].opcode == opcode)
            break;

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr  = instruction à traiter.                              *
*                format = format du binaire manipulé.                         *
*                syntax = type de représentation demandée.                    *
*                                                                             *
*  Description : Traduit une instruction en version humainement lisible.      *
*                                                                             *
*  Retour      : Chaîne de caractères à libérer de la mémoire.                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static const char *dalvik_get_instruction_text(const GDalvikInstruction *instr, const GExeFormat *format, AsmSyntax syntax)
{
    return _instructions[instr->type].keyword;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr = instruction à consulter.                             *
*                addr  = eventuelle adresse associée à faire connaître. [OUT] *
*                                                                             *
*  Description : Informe sur une éventuelle référence à une autre instruction.*
*                                                                             *
*  Retour      : Type de lien trouvé ou ILT_NONE si aucun.                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static InstructionLinkType dalvik_get_instruction_link(const GDalvikInstruction *instr, vmpa_t *addr)
{
    return ILT_NONE/*instr->get_link(instr, addr)*/;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr = instruction à consulter.                             *
*                                                                             *
*  Description : Indique si l'instruction correspond à un retour de fonction. *
*                                                                             *
*  Retour      : true si l'instruction est un 'return' quelconque ou false.   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool dalvik_instruction_is_return(const GDalvikInstruction *instr)
{
    return (instr->type == DOP_RETURN_VOID);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr = instruction d'origine à convertir.                   *
*                ctx   = contexte de la phase de décompilation.               *
*                                                                             *
*  Description : Décompile une instruction de la machine virtuelle Dalvik.    *
*                                                                             *
*  Retour      : Instruction mise en place ou NULL.                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDecInstruction *dalvik_instruction_decompile(const GDalvikInstruction *instr, GDecContext *ctx)
{
    GDecInstruction *result;                /* Instruction à retourner     */

    if (_instructions[instr->type].decomp != NULL)
        result = _instructions[instr->type].decomp(G_ARCH_INSTRUCTION(instr), ctx);

    else
        result = NULL;

    return result;

}