summaryrefslogtreecommitdiff
path: root/src/arch/x86/operands/modrm.c
blob: f48fe28e6daff0f4229b372b531f940a1fbf90e8 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * modrm.c - opérandes de type mod/rm
 *
 * Copyright (C) 2012-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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.
 *
 *  Chrysalide 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 "modrm.h"


#include <math.h>


#include "register.h"
#include "../../operand-int.h"



/* Définition d'un opérande x86 de type ModRM (instance) */
struct _GX86ModRMOperand
{
    GArchOperand parent;                    /* Instance parente            */

    uint8_t scale;                          /* Puissance de deux           */
    GX86Register *index;                    /* Registre servant d'indice   */
    GX86Register *base;                     /* Registre de base            */
    GImmOperand *displacement;              /* Décallage supplémentaire    */

};


/* Définition d'un opérande x86 de type ModRM (classe) */
struct _GX86ModRMOperandClass
{
    GArchOperandClass parent;               /* Classe parente              */

};


/* Initialise la classe des opérandes x86 de type ModRM. */
static void g_x86_mod_rm_operand_class_init(GX86ModRMOperandClass *);

/* Initialise une instance d'opérande x86 de type ModRM. */
static void g_x86_mod_rm_operand_init(GX86ModRMOperand *);



/* Indique le type défini par la GLib pour un opérande x86 de type ModRM. */
G_DEFINE_TYPE(GX86ModRMOperand, g_x86_mod_rm_operand, G_TYPE_ARCH_OPERAND);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des opérandes x86 de type ModRM.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_x86_mod_rm_operand_class_init(GX86ModRMOperandClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = instance à initialiser.                            *
*                                                                             *
*  Description : Initialise une instance d'opérande x86 de type ModRM.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_x86_mod_rm_operand_init(GX86ModRMOperand *operand)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : data = flux de données à analyser.                           *
*                pos  = position courante dans ce flux. [OUT]                 *
*                len  = taille totale des données à analyser.                 *
*                size = taille de l'opérande, et donc du registre.            *
*                                                                             *
*  Description : Crée un opérande x86 de type ModRM.                          *
*                                                                             *
*  Retour      : Opérande mis en place.                                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArchOperand *g_x86_mod_rm_operand_new(const bin_t *data, off_t *pos, off_t len, MemoryDataSize size)
{
    GX86ModRMOperand *result;               /* Structure à retourner       */
    uint8_t mod;                            /* Modificateur présent        */
    GX86Register *reg;                      /* Registre lu                 */

    mod = (data[*pos] & 0xc0);

    if (mod == 0xc0)
        return g_x86_register_operand_new_from_mod_rm(data, pos, len, size, true);

    reg = g_x86_register_new(size, data[*pos] & 0x07);
    if (reg == NULL) return NULL;

    (*pos)++;

    /* Vieille astuce de l'emplacement mémoire fixe ? */
    if (g_x86_register_is_base_pointer(reg) && mod == 0x00)
    {
        /* FIXME *///free_x86_register(reg);
        assert(0);
        return NULL;
        //return g_imm_operand_new_from_data_old(MDS_32_BITS/* FIXME */, data, pos, len, SRE_LITTLE /*FIXME*/);
    }

    result = g_object_new(G_TYPE_X86_MOD_RM_OPERAND, NULL);

    /* A la recherche d'un SIB */
    if (g_x86_register_is_stack_pointer(reg))
    {
        /* FIXME *///free_x86_register(reg);

        result->base = g_x86_register_new(size, data[*pos] & 0x07);
        if (result->base == NULL) goto gxmron_error;

        result->index = g_x86_register_new(size, (data[*pos] & 0x38) >> 3);
        if (result->index == NULL) goto gxmron_error;

        result->scale = ((data[*pos] & 0xc0) >> 6);

        if (g_x86_register_is_stack_pointer(result->index))
        {
            /* FIXME *///free_x86_register(result->index);
            result->index = result->base;
            result->base = NULL;
        }

        (*pos)++;

    }

    else result->index = reg;

    /* Décallage supplémentaire ? */
    switch (mod)
    {
        case 0x00:
            if (result->base != NULL && g_x86_register_is_base_pointer(result->base))
            {
                /* FIXME *///free_x86_register(result->base);
                result->base = NULL;

                assert(0);
                //result->displacement = g_imm_operand_new_from_data_old(size/* FIXME : !convert mds/aos */, data, pos, len, SRE_LITTLE /* FIXME */);
                if (result->displacement == NULL) goto gxmron_error;

            }
            break;

        case 0x40:
            assert(0);
            //result->displacement = g_imm_operand_new_from_data_old(MDS_8_BITS_SIGNED, data, pos, len, SRE_LITTLE /* FIXME */);
            if (result->displacement == NULL) goto gxmron_error;
            break;

        case 0x80:
            assert(0);
            //result->displacement = g_imm_operand_new_from_data_old(MDS_32_BITS_SIGNED/* FIXME ! 16/32 */, data, pos, len, SRE_LITTLE /* FIXME */);
            if (result->displacement == NULL) goto gxmron_error;
            break;

    }

    return G_ARCH_OPERAND(result);

 gxmron_error:

    /* FIXME free(result);*/
    return NULL;

}

#if 0
/******************************************************************************
*                                                                             *
*  Paramètres  : operand   = opérande à transcrire.                           *
*                options   = options de rendu.                                *
*                rendering = support effectif final des lignes de code.       *
*                stream    = flux ouvert en écriture.                         *
*                                                                             *
*  Description : Ajoute du texte simple à un fichier ouvert en écriture.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_x86_mod_rm_operand_add_text(const GX86ModRMOperand *operand, GRenderingOptions *options, MainRendering rendering, FILE *stream)
{
    GContentExporter *exporter;             /* Autre vision de l'opérande  */
    char tmp[2];                            /* Echelle en puissance de 2   */

    exporter = G_CONTENT_EXPORTER(operand);

    switch (g_rendering_options_get_syntax(options))
    {
        case ASX_INTEL:

            g_content_exporter_insert_text(exporter, stream, "[", 1, RTT_HOOK);

            if (operand->scale > 0)
            {
                snprintf(tmp, 2, "%d", (int)pow(2, operand->scale)); 

                g_content_exporter_insert_text(exporter, stream, tmp, 1, RTT_IMMEDIATE);

                g_content_exporter_insert_text(exporter, stream, "*", 1, RTT_SIGNS);

            }

            g_content_exporter_add_text(G_CONTENT_EXPORTER(operand->index),
                                        options, rendering, stream);

            if (operand->base != NULL)
            {
                g_content_exporter_insert_text(exporter, stream, "+", 1, RTT_SIGNS);

                g_content_exporter_add_text(G_CONTENT_EXPORTER(operand->base),
                                            options, rendering, stream);

            }

            if (operand->displacement != NULL)
            {
                if (g_imm_operand_is_negative(operand->displacement))
                    g_content_exporter_insert_text(exporter, stream, "-", 1, RTT_SIGNS);
                else
                    g_content_exporter_insert_text(exporter, stream, "+", 1, RTT_SIGNS);

                g_content_exporter_add_text(G_CONTENT_EXPORTER(operand->displacement),
                                            options, rendering, stream);

            }

            g_content_exporter_insert_text(exporter, stream, "]", 1, RTT_HOOK);

            break;

        case ASX_ATT:

            /* TODO */
            g_content_exporter_insert_text(exporter, stream, "[ModRM]", 7, RTT_HOOK);

            break;

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = opérande à transcrire.                             *
*                buffer  = espace où placer ledit contenu.                    *
*                options = options de rendu.                                  *
*                                                                             *
*  Description : Ajoute à un tampon GLib le contenu de l'instance spécifiée.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_x86_mod_rm_operand_to_buffer(const GX86ModRMOperand *operand, GBufferLine *buffer, GRenderingOptions *options)
{
    GContentExporter *exporter;             /* Autre vision de l'opérande  */
    char tmp[2];                            /* Echelle en puissance de 2   */

    exporter = G_CONTENT_EXPORTER(operand);

    switch (g_rendering_options_get_syntax(options))
    {
        case ASX_INTEL:

            g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                  "[", 1, RTT_HOOK);

            if (operand->scale > 0)
            {
                snprintf(tmp, 2, "%d", (int)pow(2, operand->scale)); 

                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      tmp, 1, RTT_IMMEDIATE);

                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      "*", 1, RTT_SIGNS);

            }

            g_content_exporter_to_buffer(G_CONTENT_EXPORTER(operand->index), buffer, options);

            if (operand->base != NULL)
            {
                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      "+", 1, RTT_SIGNS);

                g_content_exporter_to_buffer(G_CONTENT_EXPORTER(operand->base), buffer, options);

            }

            if (operand->displacement != NULL)
            {
                if (g_imm_operand_is_negative(operand->displacement))
                    g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                          "-", 1, RTT_SIGNS);
                else
                    g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                          "+", 1, RTT_SIGNS);

                g_content_exporter_to_buffer(G_CONTENT_EXPORTER(operand->displacement), buffer, options);

            }

            g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                  "]", 1, RTT_HOOK);

            break;

        case ASX_ATT:

            /* TODO */
            g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                  "[ModRM]", 7, RTT_HOOK);

            break;

    }

}
#endif


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = opérande à consulter.                              *
*                scale   = facteur sous forme de puissance de deux. [OUT      *
*                index   = register principal de l'opérande. [OUT]            *
*                                                                             *
*  Description : Fournit l'indice et l'échelle d'un opérande x86 ModRM.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_x86_mod_rm_operand_get_scale_and_index(const GX86ModRMOperand *operand, uint8_t *scale, const GX86Register **index)
{
    *scale = operand->scale;
    *index = operand->index;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = opérande à consulter.                              *
*                                                                             *
*  Description : Fournit le registre de base d'un opérande x86 ModRM.         *
*                                                                             *
*  Retour      : Registre de base de l'opérande.                              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const GX86Register *g_x86_mod_rm_operand_get_base(const GX86ModRMOperand *operand)
{
    return operand->base;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = opérande à consulter.                              *
*                                                                             *
*  Description : Fournit le décallage supplémentaire d'un opérande x86 ModRM. *
*                                                                             *
*  Retour      : Décallage numérique pour l'opérande.                         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const GImmOperand *g_x86_mod_rm_operand_get_displacement(const GX86ModRMOperand *operand)
{
    return operand->displacement;

}