summaryrefslogtreecommitdiff
path: root/src/arch/arm/v7/arm.c
blob: ea9819c7f117b68ffbccf5489b08e3ac8b47138a (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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717

/* Chrysalide - Outil d'analyse de fichiers binaires
 * arm.c - désassemblage des instructions ARMv7
 *
 * Copyright (C) 2014 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 "arm.h"


#include <stdint.h>


#include "opcodes/opcodes.h"
#include "../../../common/bconst.h"




/* Désassemble une instruction ARMv7 de données ou autre. */
static GArchInstruction *process_armv7_data_processing_and_miscellaneous_instructions(uint32_t);

/* Désassemble une instruction ARMv7 de données de registre. */
static GArchInstruction *process_armv7_data_processing_register(uint32_t);

/* Désassemble une instruction ARMv7 de données de immédiate. */
static GArchInstruction *process_armv7_data_processing_immediate(uint32_t);

/* Désassemble une instruction ARMv7 liées aux multiplications. */
static GArchInstruction *process_armv7_multiply_and_multiply_accumulate(uint32_t);

/* Désassemble une instruction ARMv7 de données ou autre. */
static GArchInstruction *process_armv7_branch_branch_with_link_and_block_data_transfer(uint32_t);



// process_armv7_instruction_set_encoding

#define process_armv7_load_store_word_and_unsigned_byte(raw) NULL
#define process_armv7_load_store_word_and_unsigned_byte(raw) NULL
#define process_armv7_media_instructions(raw) NULL
//#define process_armv7_branch_branch_with_link_and_block_data_transfer(raw) NULL
#define process_armv7_coprocessor_instructions_and_Supervisor_call(raw) NULL
#define process_armv7_unconditional_instructions(raw) NULL


// process_armv7_data_processing_and_miscellaneous_instructions

#define process_armv7_data_processing_register_shifted_register(raw) NULL
#define process_armv7_extra_load_store_instructions(raw) NULL
#define process_armv7_miscellaneous_instructions(raw) NULL
#define process_armv7_halfword_multiply_and_multiply_accumulate(raw) NULL
//#define process_armv7_multiply_and_multiply_accumulate(raw) NULL
#define process_armv7_synchronization_primitives(raw) NULL
#define process_armv7_extra_load_store_instructions_unprivileged(raw) NULL
#define process_armv7_extra_load_store_instructions(raw) NULL
//#define process_armv7_data_processing_immediate(raw) NULL
#define armv7_read_instr_mov_immediate(raw) NULL
#define armv7_read_instr_movt(raw) NULL
#define process_armv7_msr_immediate_and_hints(raw) NULL


// process_armv7_data_processing_register

/*
#define armv7_read_instr_and_register(raw) NULL
#define armv7_read_instr_eor_register(raw) NULL
#define armv7_read_instr_sub_register(raw) NULL
#define armv7_read_instr_rsb_register(raw) NULL
#define armv7_read_instr_add_register_arm(raw) NULL
#define armv7_read_instr_adc_register(raw) NULL
#define armv7_read_instr_sbc_register(raw) NULL
#define armv7_read_instr_rsc_register(raw) NULL
#define armv7_read_instr_tst_register(raw) NULL
#define armv7_read_instr_teq_register(raw) NULL
#define armv7_read_instr_cmp_register(raw) NULL
#define armv7_read_instr_cmn_register(raw) NULL
#define armv7_read_instr_orr_register(raw) NULL
*/
//#define armv7_read_instr_mov_register_arm(raw) NULL
#define armv7_read_instr_lsl_immediate(raw) NULL
#define armv7_read_instr_lsr_immediate(raw) NULL
#define armv7_read_instr_asr_immediate(raw) NULL
#define armv7_read_instr_rrx(raw) NULL
#define armv7_read_instr_ror_immediate(raw) NULL
//#define armv7_read_instr_bic_register(raw) NULL
//#define armv7_read_instr_mvn_register(raw) NULL


// process_armv7_branch_branch_with_link_and_block_data_transfer

#define armv7_read_instr_stmda_stmed(raw) NULL
#define armv7_read_instr_ldmda_ldmfa(raw) NULL
#define armv7_read_instr_stm_stmia_stmea(raw) NULL
#define armv7_read_instr_ldm_ldmia_ldmfd_arm(raw) NULL
#define armv7_read_instr_ldm_ldmia_ldmfd_arm(raw) NULL
#define armv7_read_instr_pop_arm(raw) NULL
#define armv7_read_instr_stmdb_stmfd(raw) NULL
#define armv7_read_instr_stmdb_stmfd(raw) NULL
#define armv7_read_instr_push(raw) NULL
#define armv7_read_instr_ldmdb_ldmea(raw) NULL
#define armv7_read_instr_stmib_stmfa(raw) NULL
#define armv7_read_instr_ldmib_ldmed(raw) NULL
#define armv7_read_instr_stm_user_registers(raw) NULL
#define armv7_read_instr_ldm_user_registers(raw) NULL
#define armv7_read_instr_ldm_exception_return(raw) NULL
#define armv7_read_instr_b(raw) NULL
//#define armv7_read_instr_bl_blx_immediate(raw) NULL



#define armv7_read_instr_adr(raw) NULL



// ???
#define armv7_read_instr_yield(raw) NULL
#define armv7_read_instr_bx(raw) NULL





/******************************************************************************
*                                                                             *
*  Paramètres  : raw = donnée brute de 32 bits à désassembler.                *
*                                                                             *
*  Description : Désassemble une instruction ARMv7 classique.                 *
*                                                                             *
*  Retour      : Instruction mise en place ou NULL en cas d'échec.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArchInstruction *process_armv7_instruction_set_encoding(uint32_t raw)
{
    GArchInstruction *result;               /* Instruction à renvoyer      */
    uint32_t cond;                          /* Champ 'cond' à retrouver    */
    uint32_t op1;                           /* Champ 'op1' à retrouver     */
    uint32_t op;                            /* Champ 'op' à retrouver      */

    /**
     * Suit les directives de :
     * § A5.1 ARM instruction set encoding
     */

    result = NULL;

    cond = (raw >> 28) & 0xf;
    op1 = (raw >> 25) & 0x7;
    op = (raw >> 4) & 0x1;

    if (cond != b1111)
    {
        if ((op1 & b110) == b000)
            result = process_armv7_data_processing_and_miscellaneous_instructions(raw);

        else if (op1 == b010)
            result = process_armv7_load_store_word_and_unsigned_byte(raw);

        else if (op1 == b011)
        {
            if (op == b0)
                result = process_armv7_load_store_word_and_unsigned_byte(raw);

            else
                result = process_armv7_media_instructions(raw);

        }

        else if ((op1 & b110) == b100)
            result = process_armv7_branch_branch_with_link_and_block_data_transfer(raw);

        else if ((op1 & b110) == b110)
            result = process_armv7_coprocessor_instructions_and_Supervisor_call(raw);

    }

    else /* if (cond == b1111) */
        result = process_armv7_unconditional_instructions(raw);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : raw = donnée brute de 32 bits à désassembler.                *
*                                                                             *
*  Description : Désassemble une instruction ARMv7 de données ou autre.       *
*                                                                             *
*  Retour      : Instruction mise en place ou NULL en cas d'échec.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GArchInstruction *process_armv7_data_processing_and_miscellaneous_instructions(uint32_t raw)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    uint32_t op;                            /* Champ 'op' à retrouver      */
    uint32_t op1;                           /* Champ 'op1' à retrouver     */
    uint32_t op2;                           /* Champ 'op2' à retrouver     */

    /**
     * Suit les directives de :
     * § A5.2 Data-processing and miscellaneous instructions
     */

    result = NULL;

    op = (raw >> 25) & 0x1;
    op1 = (raw >> 20) & 0x1f;
    op2 = (raw >> 4) & 0xf;

    if (op == b0)
    {
        if ((op1 & b11001) != b10000)
        {
            if ((op2 & b0001) == b0000)
                result = process_armv7_data_processing_register(raw);

            else if ((op2 & b1001) == b0001)
                result = process_armv7_data_processing_register_shifted_register(raw);

            if (result != NULL) goto padpami_found;

        }

        else /* if ((op1 & b11001) == b10000) */
        {
            if ((op2 & b1000) == b0000)
                result = process_armv7_miscellaneous_instructions(raw);

            else if ((op2 & b1001) == b1000)
                result = process_armv7_halfword_multiply_and_multiply_accumulate(raw);

            if (result != NULL) goto padpami_found;

        }

        if ((op1 & b10000) == b00000)
        {
            if (op2 == b1001)
                result = process_armv7_multiply_and_multiply_accumulate(raw);

            if (result != NULL) goto padpami_found;

        }

        if ((op1 & b10000) == b10000)
        {
            if (op2 == b1001)
                result = process_armv7_synchronization_primitives(raw);

            if (result != NULL) goto padpami_found;

        }

        if ((op1 & b10010) != b00010)
        {
            if (op2 == b1011)
                result = process_armv7_extra_load_store_instructions(raw);

            else if ((op2 & b1101) == b1101)
                result = process_armv7_extra_load_store_instructions(raw);

            if (result != NULL) goto padpami_found;

        }

        else /* if ((op1 & b10010) != b00010) */
        {
            if (op2 == b1011)
                result = process_armv7_extra_load_store_instructions_unprivileged(raw);

            else if ((op2 & b1101) == b1101)
                result = process_armv7_extra_load_store_instructions(raw);

            if (result != NULL) goto padpami_found;

        }

    }

    else
    {
        if ((op1 & b11001) != b10000)
            result = process_armv7_data_processing_immediate(raw);

        else if (op1 == b10000)
            result = armv7_read_instr_mov_immediate(raw);

        else if (op1 == b10100)
            result = armv7_read_instr_movt(raw);

        else if ((op1 & b11011) == b10010)
            result = process_armv7_msr_immediate_and_hints(raw);

    }

 padpami_found:

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : raw = donnée brute de 32 bits à désassembler.                *
*                                                                             *
*  Description : Désassemble une instruction ARMv7 de données de registre.    *
*                                                                             *
*  Retour      : Instruction mise en place ou NULL en cas d'échec.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GArchInstruction *process_armv7_data_processing_register(uint32_t raw)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    uint32_t op;                            /* Champ 'op' à retrouver      */
    uint32_t imm5;                          /* Champ 'imm5' à retrouver    */
    uint32_t op2;                           /* Champ 'op2' à retrouver     */

    /**
     * Suit les directives de :
     * § A5.2.1 Data-processing (register)
     */

    result = NULL;

    op = (raw >> 20) & 0x1f;
    imm5 = (raw >> 7) & 0x1f;
    op2 = (raw >> 5) & 0x3;

    if ((op & b11110) == b00000)
        result = armv7_read_instr_and_register(raw);

    else if ((op & b11110) == b00010)
        result = armv7_read_instr_eor_register(raw);

    else if ((op & b11110) == b00100)
        result = armv7_read_instr_sub_register(raw);

    else if ((op & b11110) == b00110)
        result = armv7_read_instr_rsb_register(raw);

    else if ((op & b11110) == b01000)
        result = armv7_read_instr_add_register_arm(raw);

    else if ((op & b11110) == b01010)
        result = armv7_read_instr_adc_register(raw);

    else if ((op & b11110) == b01100)
        result = armv7_read_instr_sbc_register(raw);

    else if ((op & b11110) == b01110)
        result = armv7_read_instr_rsc_register(raw);

    /*
    else if ((op & b11001) == b10000)
        result = process_armv7_data_processing_and_miscellaneous_instructions(raw);
    */

    else if (op == b10001)
        result = armv7_read_instr_tst_register(raw);

    else if (op == b10011)
        result = armv7_read_instr_teq_register(raw);

    else if (op == b10101)
        result = armv7_read_instr_cmp_register(raw);

    else if (op == b10111)
        result = armv7_read_instr_cmn_register(raw);

    else if ((op & b11110) == b11000)
        result = armv7_read_instr_orr_register(raw);

    else if ((op & b11110) == b11010)
    {
        if (op2 == b00)
        {
            if (imm5 == b00000)
                result = armv7_read_instr_mov_register_arm(raw);

            else
                result = armv7_read_instr_lsl_immediate(raw);

        }

        else if (op2 == b01)
            result = armv7_read_instr_lsr_immediate(raw);

        else if (op2 == b10)
            result = armv7_read_instr_asr_immediate(raw);

        else if (op2 == b11)
        {
            if (imm5 == b00000)
                result = armv7_read_instr_rrx(raw);

            else
                result = armv7_read_instr_ror_immediate(raw);

        }

     }

    else if ((op & b11110) == b11100)
        result = armv7_read_instr_bic_register(raw);

    else if ((op & b11110) == b11110)
        result = armv7_read_instr_mvn_register(raw);

    return result;

}








/******************************************************************************
*                                                                             *
*  Paramètres  : raw = donnée brute de 32 bits à désassembler.                *
*                                                                             *
*  Description : Désassemble une instruction ARMv7 de données de immédiate.   *
*                                                                             *
*  Retour      : Instruction mise en place ou NULL en cas d'échec.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GArchInstruction *process_armv7_data_processing_immediate(uint32_t raw)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    uint32_t op;                            /* Champ 'op' à retrouver      */
    uint32_t rn;                            /* Champ 'rn' à retrouver      */

    /**
     * Suit les directives de :
     * § A5.2.3 Data-processing (immediate)
     */

    result = NULL;

    op = (raw >> 20) & 0x1f;
    rn = (raw >> 16) & 0xf;

    if ((op & b11110) == b00000)
        result = armv7_read_instr_and_immediate(raw);

    else if ((op & b11110) == b00010)
        result = armv7_read_instr_eor_immediate(raw);

    else if ((op & b11110) == b00100)
    {
        if (rn == b1111)
            result = armv7_read_instr_adr(raw);

        else
            result = armv7_read_instr_sub_immediate_arm(raw);

    }

    else if ((op & b11110) == b00110)
        result = armv7_read_instr_rsb_immediate(raw);

    else if ((op & b11110) == b01000)
    {
        if (rn == b1111)
            result = armv7_read_instr_adr(raw);

        else
            result = armv7_read_instr_add_immediate_arm(raw);

    }

    else if ((op & b11110) == b01010)
        result = armv7_read_instr_adc_immediate(raw);

    else if ((op & b11110) == b01100)
        result = armv7_read_instr_sbc_immediate(raw);

    else if ((op & b11110) == b01110)
        result = armv7_read_instr_rsc_immediate(raw);

    /*
    else if ((op & b11110) == b10000)
        result = process_armv7_data_processing_and_miscellaneous_instructions(raw);
    */

    else if (op == b10001)
        result = armv7_read_instr_tst_immediate(raw);

    else if (op == b10011)
        result = armv7_read_instr_teq_immediate(raw);

    else if (op == b10101)
        result = armv7_read_instr_cmp_immediate(raw);

    else if (op == b10111)
        result = armv7_read_instr_cmn_immediate(raw);

    else if ((op & b11110) == b11000)
        result = armv7_read_instr_orr_immediate(raw);

    else if ((op & b11110) == b11010)
        result = armv7_read_instr_mov_immediate(raw);

    else if ((op & b11110) == b11100)
        result = armv7_read_instr_bic_immediate(raw);

    else if ((op & b11110) == b11110)
        result = armv7_read_instr_mvn_immediate(raw);

    return result;

}












/******************************************************************************
*                                                                             *
*  Paramètres  : raw = donnée brute de 32 bits à désassembler.                *
*                                                                             *
*  Description : Désassemble une instruction ARMv7 liées aux multiplications. *
*                                                                             *
*  Retour      : Instruction mise en place ou NULL en cas d'échec.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GArchInstruction *process_armv7_multiply_and_multiply_accumulate(uint32_t raw)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    uint32_t op;                            /* Champ 'op' à retrouver      */

    /**
     * Suit les directives de :
     * § A5.2.5 Multiply and multiply accumulate
     */

    result = NULL;

    if ((raw & 0x0f0000f0) == 0x00000090)
        return NULL;

    op = (raw >> 20) & 0x1f;

    if ((op & b1110) == b0000)
        result = armv7_read_instr_mul(raw);

    else if ((op & b1110) == b0010)
        result = armv7_read_instr_mla(raw);

    else if (op == b0100)
        result = armv7_read_instr_umaal(raw);

    else if (op == b0101)
        result = NULL;  /* Non défini */

    else if (op == b0110)
        result = armv7_read_instr_mls(raw);

    else if (op == b0111)
        result = NULL;  /* Non défini */

    else if ((op & b1110) == b1000)
        result = armv7_read_instr_umull(raw);

    else if ((op & b1110) == b1010)
        result = armv7_read_instr_umlal(raw);

    else if ((op & b1110) == b1100)
        result = armv7_read_instr_smull(raw);

    else if ((op & b1110) == b1110)
        result = armv7_read_instr_smlal(raw);

    return result;

}








/******************************************************************************
*                                                                             *
*  Paramètres  : raw = donnée brute de 32 bits à désassembler.                *
*                                                                             *
*  Description : Désassemble une instruction ARMv7 de données ou autre.       *
*                                                                             *
*  Retour      : Instruction mise en place ou NULL en cas d'échec.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GArchInstruction *process_armv7_branch_branch_with_link_and_block_data_transfer(uint32_t raw)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    uint32_t op;                            /* Champ 'op' à retrouver      */
    uint32_t rn;                            /* Champ 'rn' à retrouver      */
    uint32_t r;                             /* Champ 'r' à retrouver       */

    /**
     * Suit les directives de :
     * § A5.5 Branch, branch with link, and block data transferr
     */

    result = NULL;

    op = (raw >> 20) & 0x3f;
    rn = (raw >> 16) & 0xf;
    r = (raw >> 15) & 0x1;

    if ((op & b111101) == b000000)
        result = armv7_read_instr_stmda_stmed(raw);

    else if ((op & b111101) == b000001) 
        result = armv7_read_instr_ldmda_ldmfa(raw);

    else if ((op & b111101) == b001000)
        result = armv7_read_instr_stm_stmia_stmea(raw);

    else if (op == b001001)
        result = armv7_read_instr_ldm_ldmia_ldmfd_arm(raw);

    else if (op == b001011)
    {
        if (rn != b1101)
            result = armv7_read_instr_ldm_ldmia_ldmfd_arm(raw);
        else /* if (rn == b1101) */
            result = armv7_read_instr_pop_arm(raw);
     }

    else if (op == b010000)
        result = armv7_read_instr_stmdb_stmfd(raw);

    else if (op == b010010)
    {
        if (rn != b1101)
            result = armv7_read_instr_stmdb_stmfd(raw);
        else /* if (rn == b1101) */
            result = armv7_read_instr_push(raw);
    }

    else if ((op & b111101) == b010001)
        result = armv7_read_instr_ldmdb_ldmea(raw);

    else if ((op & b111101) == b011000)
        result = armv7_read_instr_stmib_stmfa(raw);

    else if ((op & b111101) == b011001)
        result = armv7_read_instr_ldmib_ldmed(raw);

    else if ((op & b100101) == b000100)
        result = armv7_read_instr_stm_user_registers(raw);

    else if ((op & b100101) == b000101)
    {
        if (r == b0)
            result = armv7_read_instr_ldm_user_registers(raw);
        else /* if (r == b1) */
            result = armv7_read_instr_ldm_exception_return(raw);
    }

    else if ((op & b110000) == b100000)
        result = armv7_read_instr_b(raw);

    else if ((op & b110000) == b110000)
        result = armv7_read_instr_bl_blx_immediate(raw);

    return result;

}