summaryrefslogtreecommitdiff
path: root/src/format/dex/method.c
blob: 233ecb1049fe7acb0c6cb738294b23ab941d5167 (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

/* OpenIDA - Outil d'analyse de fichiers binaires
 * method.c - manipulation des methodes du format DEX
 *
 * Copyright (C) 2010-2012 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 "method.h"


#include "dex-int.h"
#include "pool.h"





/* Methode issue du code source (instance) */
struct _GDexMethod
{
    GObject parent;                         /* A laisser en premier        */

    GBinRoutine *routine;                   /* Représentation interne      */

    encoded_method info;                    /* Propriétés de la méthode    */
    code_item body;                         /* Corps de la méthode         */
    off_t offset;                           /* Position du code            */

};

/* Methode issue du code source (classe) */
struct _GDexMethodClass
{
    GObjectClass parent;                    /* A laisser en premier        */

};


/* Procède à l'initialisation d'une methode issue du code. */
static void g_dex_method_class_init(GDexMethodClass *);

/* Procède à l'initialisation d'une methode issue du code. */
static void g_dex_method_init(GDexMethod *);










/* Détermine le type d'une methode issue du code source. */
G_DEFINE_TYPE(GDexMethod, g_dex_method, G_TYPE_OBJECT);



/******************************************************************************
*                                                                             *
*  Paramètres  : class = classe de composant GLib à initialiser.              *
*                                                                             *
*  Description : Procède à l'initialisation d'une methode issue du code.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dex_method_class_init(GDexMethodClass *class)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : method = composant GLib à initialiser.                       *
*                                                                             *
*  Description : Procède à l'initialisation d'une methode issue du code.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dex_method_init(GDexMethod *method)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = représentation interne du format DEX à consulter.   *
*                seed   = graine des informations à extraire.                 *
*                last   = dernier indice utilisé (à mettre à jour). [OUT]     *
*                                                                             *
*  Description : Crée une nouvelle représentation de methode issue de code.   *
*                                                                             *
*  Retour      : Composant GLib créé.                                         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDexMethod *g_dex_method_new(const GDexFormat *format, const encoded_method *seed, uleb128_t *last)
{
    GDexMethod *result;                     /* Composant à retourner       */
    off_t offset;                           /* Tête de lecture             */
    code_item item;                         /* Corps de la méthode         */


    GBinRoutine *routine;

    offset = seed->code_off;

    if (!read_dex_code_item(format, &offset, &item))
        return NULL;

    *last += seed->method_idx_diff;
    routine = get_routine_from_dex_pool(format, *last);

    if (routine == NULL) return NULL;


    result = g_object_new(G_TYPE_DEX_METHOD, NULL);

    result->info = *seed;
    result->body = item;

    printf(" ==== %s ====\n", g_binary_routine_get_name(routine));

    //printf(" method idx :: %d\n", seed->method_idx_diff);
    //printf(" code size  :: %d\n", item.insns_size);

    printf(" code regs  :: %d\n", item.registers_size);
    printf(" code ins   :: %d\n", item.ins_size);
    printf(" code outs  :: %d\n", item.outs_size);

    //printf(" method idx :: %lld\n", *last);


    result->offset = seed->code_off + 4 * sizeof(uint16_t) + 2 *sizeof(uint32_t);/* TODO : faire plus propre ! */


    //printf(" method off :: 0x%08x\n", result->offset);


    g_binary_routine_set_address(routine, result->offset);
    g_binary_routine_set_size(routine, item.insns_size * sizeof(uint16_t));

    result->routine = routine;


    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : method = représentation interne du format DEX à consulter.   *
*                                                                             *
*  Description : Fournit la routine OpenIDA correspondant à la méthode.       *
*                                                                             *
*  Retour      : Instance de routine mise en place.                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GBinRoutine *g_dex_method_get_routine(const GDexMethod *method)
{
    return method->routine;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : method = représentation interne du format DEX à consulter.   *
*                                                                             *
*  Description : Fournit la zone binaire correspondant à la méthode.          *
*                                                                             *
*  Retour      : Zone binaire à analyser.                                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GBinPart *g_dex_method_as_part(const GDexMethod *method)
{
    GBinPart *result;                       /* Instance à retourner        */
    off_t size;                             /* Taille du code associé      */

    result = g_binary_part_new();

    g_binary_part_set_name(result, "name");

    if (method->info.access_flags & ACC_NATIVE) size = 0;
    else size = method->body.insns_size * sizeof(uint16_t);

    g_binary_part_set_values(result, method->offset, size, method->offset);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : method = informations chargées à consulter.                  *
*                lang   = langage à utiliser pour la sortie humaine.          *
*                buffer = tampon mis à disposition pour la sortie.            *
*                                                                             *
*  Description : Procède à la décompilation complète d'une routine donnée.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_dex_method_decompile(const GDexMethod *method, GLangOutput *lang, GCodeBuffer *buffer)
{
    g_binary_routine_print_code(method->routine, lang, buffer, true);

}