summaryrefslogtreecommitdiff
path: root/src/arch/raw.c
blob: 41f46c135f2f3d10765e4462e3a32404ea3a2875 (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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602

/* Chrysalide - Outil d'analyse de fichiers binaires
 * artificial.c - instructions pures vues de l'esprit
 *
 * Copyright (C) 2009-2012 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  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 "raw.h"


#include <assert.h>
#include <ctype.h>
#include <string.h>


#include "immediate.h"
#include "instruction-int.h"
#include "target.h"



/* ------------------------- INSTRUCTION INCONNUE / DONNEES ------------------------- */


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

    bool is_padding;                        /* Bourrage à représenter ?    */
    bool is_string;                         /* Chaîne de caractères ?      */

};

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

};


/* Initialise la classe générique des opérandes. */
static void g_raw_instruction_class_init(GRawInstructionClass *);

/* Initialise une instance d'opérande d'architecture. */
static void g_raw_instruction_init(GRawInstruction *);

/* Supprime toutes les références externes. */
static void g_raw_instruction_dispose(GRawInstruction *);

/* Procède à la libération totale de la mémoire. */
static void g_raw_instruction_finalize(GRawInstruction *);

/* Ajoute à un tampon GLib le contenu de l'instance spécifiée. */
static GBufferLine *g_raw_instruction_print(const GRawInstruction *, GCodeBuffer *, MemoryDataSize, const GBinContent *, AsmSyntax);

/* Reconstruit le cache complet d'une désignation d'instruction. */
static void g_raw_instruction_build_keyword(const GRawInstruction *, AsmSyntax);



/* ---------------------------------------------------------------------------------- */
/*                           INSTRUCTION INCONNUE / DONNEES                           */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour une instruction inconnue d'architecture. */
G_DEFINE_TYPE(GRawInstruction, g_raw_instruction, G_TYPE_ARCH_INSTRUCTION);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe générique des opérandes.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_raw_instruction_class_init(GRawInstructionClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GArchInstructionClass *instr;           /* Encore une autre vision...  */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_raw_instruction_dispose;
    object->finalize = (GObjectFinalizeFunc)g_raw_instruction_finalize;

    instr = G_ARCH_INSTRUCTION_CLASS(klass);

    instr->print = (print_instruction_fc)g_raw_instruction_print;
    instr->build_key = (build_instruction_keyword_fc)g_raw_instruction_build_keyword;

}


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

static void g_raw_instruction_init(GRawInstruction *instr)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr = instance d'objet GLib à traiter.                     *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_raw_instruction_dispose(GRawInstruction *instr)
{
    G_OBJECT_CLASS(g_raw_instruction_parent_class)->dispose(G_OBJECT(instr));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr = instance d'objet GLib à traiter.                     *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_raw_instruction_finalize(GRawInstruction *instr)
{
    G_OBJECT_CLASS(g_raw_instruction_parent_class)->finalize(G_OBJECT(instr));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : addr  = position à associer à l'instruction.                 *
*                size  = taille de l'opérande souhaitée.                      *
*                value = valeur sur x bits à venir récupérer.                 *
*                                                                             *
*  Description : Crée une instruction de type 'db/dw/etc' simple.             *
*                                                                             *
*  Retour      : Instruction mise en place.                                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArchInstruction *g_raw_instruction_new_from_value(const vmpa2t *addr, MemoryDataSize size, uint64_t value)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    GArchOperand *operand;                  /* Octet non décodé à afficher */
    mrange_t range;                         /* Couverture de l'instruction */

    result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL);

    operand = g_imm_operand_new_from_value(size, value);
    if (operand == NULL) goto grinfv_error;

    g_imm_operand_pad(G_IMM_OPERAND(operand), true);

    g_arch_instruction_attach_extra_operand(result, operand);

    switch (size)
    {
        case MDS_8_BITS_UNSIGNED:
        case MDS_8_BITS_SIGNED:
            init_mrange(&range, addr, 1);
            break;

        case MDS_16_BITS_UNSIGNED:
        case MDS_16_BITS_SIGNED:
            init_mrange(&range, addr, 2);
            break;

        case MDS_32_BITS_UNSIGNED:
        case MDS_32_BITS_SIGNED:
            init_mrange(&range, addr, 4);
            break;

        case MDS_64_BITS_UNSIGNED:
        case MDS_64_BITS_SIGNED:
            init_mrange(&range, addr, 8);
            break;

        default:
            goto grinfv_error;
            break;

    }

    g_arch_instruction_set_range(result, &range);

    return result;

 grinfv_error:

    g_object_unref(G_OBJECT(result));

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : data   = flux de données à analyser.                         *
*                size   = taille de chacun des éléments à représenter.        *
*                count  = nombre de ces éléments.                             *
*                addr   = position courante dans ce flux. [OUT]               *
*                end    = limite des données à analyser.                      *
*                endian = ordre des bits dans la source.                      *
*                                                                             *
*  Description : Crée une instruction de type 'db/dw/etc' étendue.            *
*                                                                             *
*  Retour      : Instruction mise en place.                                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArchInstruction *g_raw_instruction_new_array_old(const bin_t *data, MemoryDataSize size, size_t count, vmpa2t *addr, off_t end, SourceEndian endian)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    vmpa2t old;                             /* Sauvegarde de la position   */
    size_t i;                               /* Boucle de parcours          */
    GArchOperand *operand;                  /* Octet non décodé à afficher */
    mrange_t range;                         /* Couverture de l'instruction */

    /* Par soucis de cohérence */
    if (count == 0) return NULL;

    result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL);

    copy_vmpa(&old, addr);

    for (i = 0; i < count; i++)
    {
        operand = g_imm_operand_new_from_data_old(size, data, addr, end, endian);
        if (operand == NULL) goto grina_error;

        g_imm_operand_pad(G_IMM_OPERAND(operand), true);

        g_arch_instruction_attach_extra_operand(result, operand);
    }

    init_mrange(&range, &old, compute_vmpa_diff(addr, &old));

    g_arch_instruction_set_range(result, &range);

    return result;

 grina_error:

    g_object_unref(G_OBJECT(result));

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : content = flux de données à analyser.                        *
*                size    = taille de chacun des éléments à représenter.       *
*                count   = nombre de ces éléments.                            *
*                addr    = position courante dans ce flux. [OUT]              *
*                endian  = ordre des bits dans la source.                     *
*                                                                             *
*  Description : Crée une instruction de type 'db/dw/etc' étendue.            *
*                                                                             *
*  Retour      : Instruction mise en place.                                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArchInstruction *g_raw_instruction_new_array(const GBinContent *content, MemoryDataSize size, size_t count, vmpa2t *addr, SourceEndian endian)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    vmpa2t old;                             /* Sauvegarde de la position   */
    size_t i;                               /* Boucle de parcours          */
    GArchOperand *operand;                  /* Octet non décodé à afficher */
    mrange_t range;                         /* Couverture de l'instruction */

    /* Par soucis de cohérence */
    if (count == 0) return NULL;

    result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL);

    copy_vmpa(&old, addr);

    for (i = 0; i < count; i++)
    {
        operand = g_imm_operand_new_from_data(size, content, addr, endian);
        if (operand == NULL) goto grina_error;

        g_imm_operand_pad(G_IMM_OPERAND(operand), true);

        g_arch_instruction_attach_extra_operand(result, operand);
    }

    init_mrange(&range, &old, compute_vmpa_diff(addr, &old));

    g_arch_instruction_set_range(result, &range);

    return result;

 grina_error:

    g_object_unref(G_OBJECT(result));

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr  = instruction d'assemblage à représenter.             *
*                buffer = espace où placer ledit contenu.                     *
*                msize   = taille idéale des positions et adresses;           *
*                content = contenu binaire global à venir lire.               *
*                syntax = type de représentation demandée.                    *
*                                                                             *
*  Description : Ajoute à un tampon GLib le contenu de l'instance spécifiée.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GBufferLine *g_raw_instruction_print(const GRawInstruction *instr, GCodeBuffer *buffer, MemoryDataSize msize, const GBinContent *content, AsmSyntax syntax)
{
    GBufferLine *result;                    /* Ligne de destination        */
    GArchInstruction *base;                 /* Autre version de l'instance */
    const char *key;                        /* Mot clef principal          */
    size_t klen;                            /* Taille de ce mot clef       */
    char *string;                           /* Chaîne reconstituée         */
    size_t iter;                            /* Tête d'écriture             */
    bool first;                             /* Mémorise une énumération    */
    size_t i;                               /* Boucle de parcours          */
    char byte;                              /* Octet à afficher (ou pas)   */
    bool status;                            /* Bilan d'une récupération    */

    base = G_ARCH_INSTRUCTION(instr);

    if (!instr->is_padding && !instr->is_string)
        result = G_ARCH_INSTRUCTION_CLASS(g_raw_instruction_parent_class)->print(base, buffer, msize, content, syntax);

    else
    {
        result = g_code_buffer_append_new_line(buffer, &base->range);

        g_buffer_line_add_flag(result, BLF_HAS_CODE);

        g_buffer_line_fill_mrange(result, msize/* TODO ! */, msize);

        /* Instruction proprement dite */

        key = g_arch_instruction_get_keyword(base, syntax);
        klen = strlen(key);

        g_buffer_line_insert_text(result, BLC_ASSEMBLY_HEAD, key, klen, RTT_INSTRUCTION);

        if (instr->is_padding)
            g_buffer_line_insert_text(result, BLC_ASSEMBLY, "...", 3, RTT_RAW);

        else /*if (instr->is_string)*/
        {
            string = (char *)calloc(base->operands_count + 3, sizeof(char));

            strcpy(string, "\"");
            iter = 1;

            first = true;

            for (i = 0; i < base->operands_count; i++)
            {
                status = g_imm_operand_get_value(G_IMM_OPERAND(base->operands[i]), MDS_8_BITS, &byte);
                assert(status);

                /* Si le caractère doit apparaître en hexadécimal... */
                if (!isprint(byte))
                {
                    /* Si une chaîne précède */
                    if (iter > 1)
                    {
                        if (!first)
                        {
                            g_buffer_line_insert_text(result, BLC_ASSEMBLY, ",", 1, RTT_PUNCT);
                            g_buffer_line_insert_text(result, BLC_ASSEMBLY, " ", 1, RTT_RAW);
                        }
                        else
                            first = false;

                        string[iter++] = '"';

                        g_buffer_line_insert_text(result, BLC_ASSEMBLY, string, iter, RTT_STRING);

                        iter = 1;

                    }

                    /* Impression de l'octet */

                    if (!first)
                    {
                        g_buffer_line_insert_text(result, BLC_ASSEMBLY, ",", 1, RTT_PUNCT);
                        g_buffer_line_insert_text(result, BLC_ASSEMBLY, " ", 1, RTT_RAW);
                    }
                    else
                        first = false;

                    g_arch_operand_print(base->operands[i], result, syntax);

                }

                else
                    string[iter++] = byte;

            }

            /* Si une chaîne reste encore */
            if (iter > 1)
            {
                if (!first)
                {
                    g_buffer_line_insert_text(result, BLC_ASSEMBLY, ",", 1, RTT_PUNCT);
                    g_buffer_line_insert_text(result, BLC_ASSEMBLY, " ", 1, RTT_RAW);
                }
                else
                    first = false;

                string[iter++] = '"';

                g_buffer_line_insert_text(result, BLC_ASSEMBLY, string, iter, RTT_STRING);

            }

            free(string);

        }

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr  = instruction à traiter.                              *
*                format = format du binaire manipulé.                         *
*                syntax = type de représentation demandée.                    *
*                                                                             *
*  Description : Reconstruit le cache complet d'une désignation d'instruction.*
*                                                                             *
*  Retour      : Mot clef de bas niveau.                                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_raw_instruction_build_keyword(const GRawInstruction *instr, AsmSyntax syntax)
{
    GArchOperand *operand;                  /* Octet décodé à afficher     */
    MemoryDataSize size;                    /* Taille de valeur associée   */

    static char *defines[] = { "dn", "db", "dw", "dd", "dq" };

    operand = g_arch_instruction_get_operand(G_ARCH_INSTRUCTION(instr), 0);

    if (G_IS_TARGET_OPERAND(operand))
        size = g_target_operand_get_size(G_TARGET_OPERAND(operand));
    else
        size = g_imm_operand_get_size(G_IMM_OPERAND(operand));

    G_ARCH_INSTRUCTION(instr)->cached_keyword = strdup(defines[MDS_RANGE(size)]);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr      = instruction à traiter.                          *
*                is_padding = nouveau statut à associer au contenu.           *
*                                                                             *
*  Description : Marque l'instruction comme ne contenant que du bourrage.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_raw_instruction_mark_as_padding(GRawInstruction *instr, bool is_padding)
{
    instr->is_padding = is_padding;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr      = instruction à traiter.                          *
*                is_padding = nouveau statut à associer au contenu.           *
*                                                                             *
*  Description : Indique si le contenu de l'instruction est du bourrage.      *
*                                                                             *
*  Retour      : Statut du contenu de l'instruction.                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_raw_instruction_is_padding(const GRawInstruction *instr)
{
    return instr->is_padding;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr     = instruction à traiter.                           *
*                is_string = nouveau statut à associer au contenu.            *
*                                                                             *
*  Description : Marque l'instruction comme contenant une chaîne de texte.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_raw_instruction_mark_as_string(GRawInstruction *instr, bool is_string)
{
    instr->is_string = is_string;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instr     = instruction à traiter.                           *
*                is_string = nouveau statut à associer au contenu.            *
*                                                                             *
*  Description : Indique si le contenu de l'instruction est un texte.         *
*                                                                             *
*  Retour      : Statut du contenu de l'instruction.                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_raw_instruction_is_string(const GRawInstruction *instr)
{
    return instr->is_string;

}