-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patharmdisasm.c
More file actions
3666 lines (3511 loc) · 115 KB
/
armdisasm.c
File metadata and controls
3666 lines (3511 loc) · 115 KB
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ARM instruction decoder (disassembler)
* Covers Thumb and Thumb2 (for Cortex M0 & Cortex M3), plus legacy ARM mode.
*
* Copyright 2022-2024, CompuPhase
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <ctype.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include "armdisasm.h"
#if defined _MSC_VER
# define strdup(s) _strdup(s)
#endif
typedef struct tagENCODEMASK16 {
uint16_t mask; /* bits to mask off for the test */
uint16_t match; /* masked bits must be equal to this */
bool (*func)(ARMSTATE *state, uint32_t instr);
} ENCODEMASK16;
typedef struct tagENCODEMASK32 {
uint32_t mask; /* bits to mask off for the test */
uint32_t match; /* masked bits must be equal to this */
bool (*func)(ARMSTATE *state, uint32_t instr);
} ENCODEMASK32;
enum {
POOL_CODE, /* either ARM or Thumb */
POOL_LITERAL, /* literal pool */
};
#define MASK(length) (~(~0u << (length)))
#define FIELD(word, offset, length) (((word) >> (offset)) & MASK(length))
#define BIT_SET(value, index) (((value) & (1 << (index))) != 0)
#define BIT_CLR(value, index) (((value) & (1 << (index))) == 0)
#define ROR32(word, bits) (((word) >> (bits)) | ((word) << (32 - (bits))))
#define SIGN_EXT(word, bits) if ((word) & (1 << (bits - 1))) word |= ~0uL << (bits)
#define ALIGN4(addr) ((addr) & ~0x03)
#define sizearray(a) (sizeof(a) / sizeof((a)[0]))
static int get_symbol(ARMSTATE *state, uint32_t address);
static char const *conditions[] = {
"eq", "ne", /* Z flag */
"cs", "cc", /* C flag */
"mi", "pl", /* N flag */
"vs", "vc", /* V flag (overflow) */
"hi", "ls",
"ge", "lt",
"gt", "le"
};
static const char *register_name(int reg)
{
static const char *registers[] = {
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
"r8", "r9", "r10", "fp", "ip", "sp", "lr", "pc"
};
return (reg >= 0 && reg < (int)sizearray(registers)) ? registers[reg] : NULL;
}
static const char *special_register(int reg, int mask)
{
static char field[16];
switch (reg) {
case 0x00:
strcpy(field, "APSR");
break;
case 0x01:
strcpy(field, "IAPSR");
break;
case 0x02:
strcpy(field, "EIAPSR");
break;
case 0x03:
strcpy(field, "XPSR");
break;
case 0x05:
strcpy(field, "IPSR");
break;
case 0x06:
strcpy(field, "EPSR");
break;
case 0x07:
strcpy(field, "IEPSR");
break;
case 0x08:
strcpy(field, "MSP");
break;
case 0x09:
strcpy(field, "PSP");
break;
case 0x10:
strcpy(field, "PRIMASK");
break;
case 0x11:
strcpy(field, "BASEPRI");
break;
case 0x12:
strcpy(field, "BASEPRI_MAX");
break;
case 0x13:
strcpy(field, "FAULTMASK");
break;
case 0x14:
strcpy(field, "CONTROL");
break;
default:
assert(0);
strcpy(field, "?");
}
if (reg < 5) {
switch (mask) {
case 0x4:
strcat(field, "_g");
break;
case 0x8:
strcat(field, "_nzcvq");
break;
case 0xc:
strcat(field, "_nzcvqg");
break;
}
}
return field;
}
static const char *shift_type(int type)
{
static const char *shifts[] = { "lsl", "lsr", "asr", "ror" };
assert(type >= 0 && type < (int)sizearray(shifts));
return shifts[type];
}
static char *tail(char *text)
{
assert(text);
return text + strlen(text);
}
static void padinstr(char *text)
{
assert(text);
int i = strlen(text);
assert(i > 0); /* there should already be some text in there */
if (i < 8) {
while (i < 8)
text[i++] = ' ';
} else {
text[i++] = ' '; /* length already >= 8, but add a space separator */
}
text[i] = '\0';
}
static void add_condition(ARMSTATE *state, int cond)
{
assert(cond >= 0);
if (cond < (int)sizearray(conditions))
strcat(state->text, conditions[cond]);
}
static void add_it_cond(ARMSTATE *state, int add_s)
{
if (state->it_mask != 0) {
uint16_t c = state->it_cond;
if (((state->it_mask >> 4) & 1) != (c & 1))
c ^= 1; /* invert condition */
assert(c < sizearray(conditions));
add_condition(state, c);
} else if (add_s) {
strcat(state->text, "s");
}
}
static int add_reglist(char *text, int mask)
{
strcat(text, "{");
int count = 0;
for (int i = 0; register_name(i); i++) {
if (BIT_SET(mask, i)) {
if (count++ > 0)
strcat(text, ", ");
strcat(text, register_name(i));
/* try to detect a range */
int j;
for (j = i + 1; register_name(j) && BIT_SET(mask, j); j++)
{}
j -= 1; /* reset for overrun */
if (j - i > 1) {
strcat(text, "-");
strcat(text, register_name(j));
count += j - i;
i = j;
}
}
}
strcat(text, "}");
return count;
}
static void add_insert_prefix(ARMSTATE *state, uint32_t instr)
{
assert(state);
char prefix[32] = "";
if (state->add_addr)
sprintf(prefix, "%08x ", state->address);
if (state->add_bin) {
if (state->arm_mode) {
sprintf(tail(prefix), "%08x ", instr);
} else {
if (state->size == 4)
sprintf(tail(prefix), "%04x %04x ", (instr >> 16) & 0xffff, instr & 0xffff);
else
sprintf(tail(prefix), "%04x ", instr & 0xffff);
}
}
int len = strlen(prefix);
assert(len == 0|| len == 12 || len == 24);
if (len > 0) {
assert(len + strlen(state->text) < sizearray(state->text));
memmove(state->text + len, state->text, strlen(state->text) + 1);
memmove(state->text, prefix, len);
}
}
static void append_comment(ARMSTATE *state, const char *text, const char *separator)
{
assert(state);
assert(state->add_cmt);
size_t len = strlen(state->text);
int padding = 24 - (int)len;
if (padding < 2)
padding = 2;
char prefix[40];
if (!separator) {
memset(prefix, ' ', padding);
strcpy(prefix + padding, "; ");
} else {
strcpy(prefix, separator);
}
size_t size = sizearray(state->text);
if (state->add_addr)
size -= 12;
if (state->add_bin)
size -= 12;
if (strlen(state->text) + strlen(prefix) + strlen(text) < size) {
strcat(state->text, prefix);
strcat(state->text, text);
} else if (strlen(state->text) + strlen(prefix) + 4 < size) {
/* text fits partially, but not completely: copy part and append "..." */
size_t count = size - (strlen(state->text) + strlen(prefix) + 4);
strcat(state->text, prefix);
len = strlen(state->text);
if (count > 0) {
strncpy(state->text + len, text, count);
state->text[len + count] = '\0';
}
strcat(state->text, " ...");
assert(strlen(state->text) < sizearray(state->text));
}
}
static void append_comment_hex(ARMSTATE *state, uint32_t value)
{
assert(state);
if (state->add_cmt && value >= 10) {
char hex[40];
sprintf(hex, "0x%08x", value);
append_comment(state, hex, NULL);
}
}
static void append_comment_symbol(ARMSTATE *state, uint32_t address)
{
assert(state);
if (state->add_cmt && state->symbolcount > 0) {
int i = get_symbol(state, address);
if (i >= 0 && state->symbols[i].name)
append_comment(state, state->symbols[i].name, NULL);
}
}
static void mark_address_type(ARMSTATE *state, uint32_t address, int type)
{
assert(state);
/* find the insertion point */
int pos;
for (pos = 0; pos < state->poolcount && state->codepool[pos].address < address; pos++)
{}
if (pos >= state->poolcount || state->codepool[pos].address != address) {
/* an entry must be added, first see whether there is space */
assert(state->poolcount <= state->poolsize);
if (state->poolcount == state->poolsize) {
int newsize = (state->poolsize == 0) ? 8 : 2 * state->poolsize;
ARMPOOL *list = malloc(newsize * sizeof(ARMPOOL));
if (list) {
if (state->codepool) {
memcpy(list, state->codepool, state->poolcount * sizeof(ARMPOOL));
free((void*)state->codepool);
}
state->codepool = list;
state->poolsize = newsize;
}
}
if (state->poolcount < state->poolsize) {
if (pos != state->poolcount)
memmove(&state->codepool[pos + 1], &state->codepool[pos],
(state->poolcount - pos) * sizeof(ARMPOOL));
state->poolcount += 1;
state->codepool[pos].address = address;
state->codepool[pos].type = type;
}
}
}
static int lookup_address_type(ARMSTATE *state, uint32_t address)
{
assert(state);
assert(state->poolcount == 0 || state->codepool != NULL);
assert(state->poolcount <= state->poolsize);
int type = POOL_CODE;
for (int idx = 0; idx < state->poolcount && state->codepool[idx].address <= address; idx++)
type = state->codepool[idx].type;
return type;
}
static bool thumb_shift(ARMSTATE *state, unsigned instr, const char *opcode)
{
/* helper function, for the common part of the Thumb shift instructions */
strcpy(state->text, opcode);
add_it_cond(state, 1);
padinstr(state->text);
sprintf(tail(state->text), "%s, %s, #%u", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)), FIELD(instr, 6, 5));
state->size = 2;
return true;
}
static bool thumb_lsl(ARMSTATE *state, uint32_t instr)
{
/* 0000 0xxx xxxx xxxx - shift by immediate, move register */
if (FIELD(instr, 6, 5) == 0) {
assert(state->it_mask == 0); /* this instruction is not valid inside an IT block*/
strcpy(state->text, "movs");
padinstr(state->text);
sprintf(tail(state->text), "%s, %s", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)));
state->size = 2;
return true;
}
return thumb_shift(state, instr, "lsl");
}
static bool thumb_lsr(ARMSTATE *state, uint32_t instr)
{
/* 0000 1xxx xxxx xxxx - shift by immediate, move register */
return thumb_shift(state, instr, "lsr");
}
static bool thumb_asr(ARMSTATE *state, uint32_t instr)
{
/* 0001 0xxx xxxx xxxx - shift by immediate, move register */
return thumb_shift(state, instr, "asr");
}
static bool thumb_addsub_reg(ARMSTATE *state, uint32_t instr)
{
/* 0001 10xx xxxx xxxx - add/subtract register */
if (BIT_SET(instr, 9))
strcpy(state->text, "sub");
else
strcpy(state->text, "add");
add_it_cond(state, 1);
padinstr(state->text);
sprintf(tail(state->text), "%s, %s, %s", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)), register_name(FIELD(instr, 6, 3)));
state->size = 2;
return true;
}
static bool thumb_addsub_imm(ARMSTATE *state, uint32_t instr)
{
/* 0001 11xx xxxx xxxx - add/subtract immediate */
if (BIT_SET(instr, 9))
strcpy(state->text, "sub");
else
strcpy(state->text, "add");
add_it_cond(state, 1);
padinstr(state->text);
uint32_t imm = FIELD(instr, 6, 3);
sprintf(tail(state->text), "%s, %s, #%u", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)), imm);
append_comment_hex(state, imm);
state->size = 2;
return true;
}
static bool thumb_immop(ARMSTATE *state, uint32_t instr)
{
/* 001x xxxx xxxx xxxx - add/subtract/compare/move immediate */
static const char *mnemonics[] = { "mov", "cmp", "add", "sub" };
unsigned opc = FIELD(instr, 11, 2);
assert(opc < sizearray(mnemonics));
strcpy(state->text, mnemonics[opc]);
if (opc != 1)
add_it_cond(state, 1);
padinstr(state->text);
uint32_t imm = FIELD(instr, 0, 8);
sprintf(tail(state->text), "%s, #%u", register_name(FIELD(instr, 8, 3)), imm);
append_comment_hex(state, imm);
state->size = 2;
return true;
}
static bool thumb_regop(ARMSTATE *state, uint32_t instr)
{
/* 0100 00xx xxxx xxxx - data processing register */
static const char *mnemonics[] = {
"and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
"tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn"
};
unsigned opc = FIELD(instr, 6, 4);
assert(opc < sizearray(mnemonics));
strcpy(state->text, mnemonics[opc]);
add_it_cond(state, (opc != 8 && opc != 10 && opc != 11));
padinstr(state->text);
sprintf(tail(state->text), "%s, %s", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)));
state->size = 2;
return true;
}
static bool thumb_regop_hi(ARMSTATE *state, uint32_t instr)
{
/* 0100 0100 xxxx xxxx - special data processing
0100 0101 xxxx xxxx - special data processing
0100 0110 xxxx xxxx - special data processing */
int opc = FIELD(instr, 8, 2);
switch (opc) {
case 0:
strcpy(state->text, "add");
break;
case 1:
strcpy(state->text, "cmp");
break;
case 2:
strcpy(state->text, "mov");
break;
case 3:
assert(0); /* this function should not have been called for this bit pattern */
break;
}
add_it_cond(state, 0);
padinstr(state->text);
int Rd = FIELD(instr, 0, 3);
if (BIT_SET(instr, 7))
Rd += 8;
int Rm = FIELD(instr, 3, 4);
if (opc == 0 && Rm == 13)
sprintf(tail(state->text), "%s, sp, %s", register_name(Rd), register_name(Rd));
else
sprintf(tail(state->text), "%s, %s", register_name(Rd), register_name(Rm));
state->size = 2;
return true;
}
static bool thumb_branch_exch(ARMSTATE *state, uint32_t instr)
{
/* 0100 0111 xxxx xxxx - branch exchange instruction set */
if (BIT_SET(instr, 7))
strcpy(state->text, "blx");
else
strcpy(state->text, "bx");
padinstr(state->text);
strcat(state->text, register_name(FIELD(instr, 3, 4)));
state->size = 2;
return true;
}
static bool thumb_load_lit(ARMSTATE *state, uint32_t instr)
{
/* 0100 1xxx xxxx xxxx - load from literal pool */
strcpy(state->text, "ldr");
add_it_cond(state, 0);
padinstr(state->text);
uint32_t offs = 4 * FIELD(instr, 0, 8);
sprintf(tail(state->text), "%s, [pc, #%u]", register_name(FIELD(instr, 8, 3)), offs);
state->ldr_addr = ALIGN4(state->address + 4) + offs;
append_comment_hex(state, state->ldr_addr);
mark_address_type(state, state->ldr_addr, POOL_LITERAL);
state->size = 2;
return true;
}
static bool thumb_loadstor_reg(ARMSTATE *state, uint32_t instr)
{
/* 0101 xxxx xxxx xxxx - load/store register offset */
static const char *mnemonics[] = {
"str", "strh", "strb", "ldrsb", "ldr", "ldrh", "ldrb", "ldrsh"
};
unsigned opc = FIELD(instr, 9, 3);
assert(opc < sizearray(mnemonics));
strcpy(state->text, mnemonics[opc]);
add_it_cond(state, 0);
padinstr(state->text);
sprintf(tail(state->text), "%s, [%s, %s]", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)), register_name(FIELD(instr, 6, 3)));
state->size = 2;
return true;
}
static bool thumb_loadstor_imm(ARMSTATE *state, uint32_t instr)
{
/* 011x xxxx xxxx xxxx - load/store word/byte immediate offset */
if (BIT_SET(instr, 11))
strcpy(state->text, "ldr");
else
strcpy(state->text, "str");
uint32_t offs = FIELD(instr, 6, 5);
if (BIT_SET(instr, 12))
strcat(state->text, "b");
else
offs *= 4;
add_it_cond(state, 0);
padinstr(state->text);
sprintf(tail(state->text), "%s, [%s, #%u]", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)), offs);
append_comment_hex(state, offs);
state->size = 2;
return true;
}
static bool thumb_loadstor_hw(ARMSTATE *state, uint32_t instr)
{
/* 1000 xxxx xxxx xxxx - load/store halfword immediate offset */
if (BIT_SET(instr, 11))
strcpy(state->text, "ldrh");
else
strcpy(state->text, "strh");
add_it_cond(state, 0);
padinstr(state->text);
uint32_t offs = 2 * FIELD(instr, 6, 5);
sprintf(tail(state->text), "%s, [%s, #%u]", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)), offs);
append_comment_hex(state, offs);
state->size = 2;
return true;
}
static bool thumb_loadstor_stk(ARMSTATE *state, uint32_t instr)
{
/* 1001 xxxx xxxx xxxx - load from or store to stack */
if (BIT_SET(instr, 11))
strcpy(state->text, "ldr");
else
strcpy(state->text, "str");
add_it_cond(state, 0);
padinstr(state->text);
uint32_t offs = 4 * FIELD(instr, 0, 7);
sprintf(tail(state->text), "%s, [sp, #%u]", register_name(FIELD(instr, 8, 3)), offs);
append_comment_hex(state, offs);
state->size = 2;
return true;
}
static bool thumb_add_sp_pc_imm(ARMSTATE *state, uint32_t instr)
{
/* 1010 xxxx xxxx xxxx - add to sp or pc */
if (BIT_SET(instr, 11))
strcpy(state->text, "add");
else
strcpy(state->text, "adr");
add_it_cond(state, 0);
padinstr(state->text);
uint32_t imm = FIELD(instr, 0, 7);
sprintf(tail(state->text), "%s, sp, #%u", register_name(FIELD(instr, 8, 3)), imm);
if (BIT_CLR(instr, 11))
imm += ALIGN4(state->add_addr + 4); /* as it might be a code address, we cannot mark it as a literal pool */
append_comment_hex(state, imm);
state->size = 2;
return true;
}
static bool thumb_adj_sp(ARMSTATE *state, uint32_t instr)
{
/* 1011 0000 xxxx xxxx - adjust stack pointer */
if (BIT_SET(instr, 7))
strcpy(state->text, "sub");
else
strcpy(state->text, "add");
add_it_cond(state, 0);
padinstr(state->text);
uint32_t imm = 4 * FIELD(instr, 0, 7);
sprintf(tail(state->text), "sp, #%u", imm);
append_comment_hex(state, imm);
state->size = 2;
return true;
}
static bool thumb_sign_ext(ARMSTATE *state, uint32_t instr)
{
/* 1011 0010 xxxx xxxx - sign/zero extend */
static const char *mnemonics[] = { "sxth", "sxtb", "uxth", "uxtb" };
unsigned opc = FIELD(instr, 6, 2);
assert(opc < sizearray(mnemonics));
strcpy(state->text, mnemonics[opc]);
add_it_cond(state, 0);
padinstr(state->text);
sprintf(tail(state->text), "%s, %s", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)));
state->size = 2;
return true;
}
static bool thumb_cmp_branch(ARMSTATE *state, uint32_t instr)
{
/* 1011 x0x1 xxxx xxxx - compare and branch on (non-)zero */
if (BIT_CLR(instr, 11))
strcpy(state->text, "cbz");
else
strcpy(state->text, "cbnz");
padinstr(state->text);
uint32_t address = FIELD(instr, 3, 5);
if (BIT_SET(instr, 9))
address += 32;
address = state->address + 4 + 2 * address;
sprintf(tail(state->text), "%s, 0x%07x", register_name(FIELD(instr, 0, 3)), address);
mark_address_type(state, address, POOL_CODE);
state->size = 2;
return true;
}
static bool thumb_push(ARMSTATE *state, uint32_t instr)
{
/* 1011 010x xxxx xxxx - push register list */
strcpy(state->text, "push");
padinstr(state->text);
int list = FIELD(instr, 0, 8);
if (BIT_SET(instr, 8))
list |= 1 << 14; /* lr */
if (list == 0)
return false;
add_reglist(state->text, list);
state->size = 2;
return true;
}
static bool thumb_pop(ARMSTATE *state, uint32_t instr)
{
/* 1011 110x xxxx xxxx - pop register list */
strcpy(state->text, "pop");
padinstr(state->text);
int list = FIELD(instr, 0, 8);
if (BIT_SET(instr, 8))
list |= 1 << 15; /* pc */
if (list == 0)
return false;
add_reglist(state->text, list);
state->size = 2;
return true;
}
static bool thumb_endian(ARMSTATE *state, uint32_t instr)
{
/* 1011 0110 0101 xxxx - set endianness */
strcpy(state->text, "setend");
padinstr(state->text);
if (BIT_SET(instr, 3))
strcat(state->text, "BE");
else
strcat(state->text, "LE");
state->size = 2;
return true;
}
static bool thumb_cpu_state(ARMSTATE *state, uint32_t instr)
{
/* 1011 0110 011x 0xxx - change processor state */
strcpy(state->text, "cps");
if (BIT_CLR(instr, 4))
strcat(state->text, "ie");
else
strcat(state->text, "id");
padinstr(state->text);
if (BIT_SET(instr, 2))
strcat(state->text, "a");
if (BIT_SET(instr, 1))
strcat(state->text, "i");
if (BIT_SET(instr, 0))
strcat(state->text, "f");
state->size = 2;
return true;
#if 0
/* code for 32-bit variant of CPS */
int imod = FIELD(instr, 9, 2);
strcpy(state->text, "cps");
if (imod == 2)
strcat(state->text, "ie");
else if (imod == 3)
strcat(state->text, "id");
padinstr(state->text);
if (imod >= 2) {
if (BIT_SET(instr, 7))
strcat(state->text, "a");
if (BIT_SET(instr, 6))
strcat(state->text, "i");
if (BIT_SET(instr, 6))
strcat(state->text, "f");
}
if (imod >= 2 && BIT_SET(instr, 8))
strcat(state->text, ", "); /* mode change follows */
if (BIT_SET(instr, 8))
sprintf(tail(state->text), "#%u", FIELD(instr, 0, 5));
#endif
}
static bool thumb_reverse(ARMSTATE *state, uint32_t instr)
{
/* 1011 1010 xxxx xxxx - reverse bytes */
switch (FIELD(instr, 6, 2)) {
case 0:
strcpy(state->text, "rev");
break;
case 1:
strcpy(state->text, "rev16");
break;
case 3:
strcpy(state->text, "revsh");
break;
default:
return false;
}
add_it_cond(state, 0);
padinstr(state->text);
sprintf(tail(state->text), "%s, %s", register_name(FIELD(instr, 0, 3)),
register_name(FIELD(instr, 3, 3)));
state->size = 2;
return true;
}
static bool thumb_break(ARMSTATE *state, uint32_t instr)
{
/* 1011 1110 xxxx xxxx - software breakpoint */
strcpy(state->text, "bkpt");
padinstr(state->text);
sprintf(tail(state->text), "#%u", FIELD(instr, 0, 8));
state->size = 2;
return true;
}
static bool thumb_if_then(ARMSTATE *state, uint32_t instr)
{
/* 1011 1111 xxxx xxxx - if-then instructions
1011 1111 xxxx 0000 - nop-compatible hints */
int mask = instr & 0x0f;
if (mask == 0) {
/* NOP compatible hints */
static const char *mnemonics[] = { "nop", "yield", "wfe", "wfi", "sev" };
unsigned opc = FIELD(instr, 4, 4);
if (opc >= sizearray(mnemonics))
return false;
strcpy(state->text, mnemonics[opc]);
add_it_cond(state, 0);
} else {
/* if-then */
unsigned cond = FIELD(instr, 4, 4);
if (cond >= sizearray(conditions))
return false;
/* "t" and "e" flags depend on the condition; rebuild the mask for the
"even" condition code (to get the same output as objdump) */
state->it_cond = cond;
state->it_mask = mask | ((cond & 1) << 4) | 0x20; /* bit 4 = implied first-condition flag, bit 5 = flag start of IT block */
int ccount = 3;
while ((mask & 1) == 0) {
ccount -= 1;
mask >>= 1;
}
assert(ccount >= 0); /* if -1, mask was 0, but that case was handled on top */
mask = state->it_mask & 0x0f;
strcpy(state->text, "it");
while (ccount-- > 0) {
if (((mask >> 3) & 1) == (cond & 1))
strcat(state->text, "t");
else
strcat(state->text, "e");
mask = (mask << 1) & 0x0f;
}
padinstr(state->text);
strcat(state->text, conditions[cond]);
}
state->size = 2;
return true;
}
static bool thumb_loadstor_mul(ARMSTATE *state, uint32_t instr)
{
/* 1100 xxxx xxx xxxx - load/store multiple */
if (BIT_SET(instr, 11))
strcpy(state->text, "ldmia");
else
strcpy(state->text, "stmia");
add_it_cond(state, 0);
padinstr(state->text);
int Rn = FIELD(instr, 8, 3);
int list = FIELD(instr, 0, 8);
if (list == 0)
return false;
strcat(state->text, register_name(Rn));
if (BIT_CLR(instr, 11) || (list & (1 << Rn)) == 0)
strcat(state->text, "!");
strcat(state->text, ", ");
add_reglist(state->text, list);
state->size = 2;
return true;
}
static bool thumb_condbranch(ARMSTATE *state, uint32_t instr)
{
/* 1101 000x xxxx xxxx - conditional branch
1101 001x xxxx xxxx - conditional branch
1101 010x xxxx xxxx - conditional branch
1101 011x xxxx xxxx - conditional branch
1101 100x xxxx xxxx - conditional branch
1101 101x xxxx xxxx - conditional branch
1101 110x xxxx xxxx - conditional branch
(this is split into 7 matching patterns, because 1011 111x must not match
conditional branch) */
strcpy(state->text, "b");
unsigned cond = FIELD(instr, 8, 4);
if (cond >= sizearray(conditions))
return false;
strcat(state->text, conditions[cond]);
padinstr(state->text);
int32_t address = FIELD(instr, 0, 8);
SIGN_EXT(address, 8);
address = state->address + 4 + 2 * address;
sprintf(tail(state->text), "0x%07x", address);
mark_address_type(state, address, POOL_CODE);
state->size = 2;
return true;
}
static bool thumb_service(ARMSTATE *state, uint32_t instr)
{
/* 1101 1111 xxxx xxxx */
strcpy(state->text, "swi");
add_it_cond(state, 0);
padinstr(state->text);
sprintf(tail(state->text), "#%u", FIELD(instr, 0, 8));
state->size = 2;
return true;
}
static bool thumb_branch(ARMSTATE *state, uint32_t instr)
{
/* 1110 0xxx xxxx xxxx - unconditional branch */
strcpy(state->text, "b");
add_it_cond(state, 0);
padinstr(state->text);
int32_t offset = FIELD(instr, 0, 11);
SIGN_EXT(offset, 11);
int32_t address = state->address + 4 + 2 * offset;
sprintf(tail(state->text), "0x%07x", address);
mark_address_type(state, address, POOL_CODE);
state->size = 2;
return true;
}
/* helper function, for special expansion rules for "modified immediate" encodings */
static int32_t expand_mod_imm(int imm1, int imm3, int imm8)
{
int32_t imm12 = ((int32_t)imm1 << 11) | ((int32_t)imm3 << 8) | (int32_t)imm8;
int32_t result;
if ((imm12 & 0x0c00) == 0) {
switch (FIELD(imm12, 8, 2)) {
case 0:
result = imm12;
break;
case 1:
imm12 &= 0xff;
result = (imm12 << 16) | imm12;
break;
case 2:
imm12 &= 0xff;
result = (imm12 << 24) | (imm12 << 8);
break;
case 3:
imm12 &= 0xff;
result = (imm12 << 24) | (imm12 << 16) | (imm12 << 8) | imm12;
break;
}
} else {
int value = FIELD(imm12, 0, 7) | 0x80;
int rot = FIELD(imm12, 7, 5);
result = ROR32(value, rot);
}
return result;
}
/* helper function, for expandion of immediate shift */
static const char *decode_imm_shift(int type, int count)
{
static char field[16];
switch (type) {
case 0:
if (count == 0)
field[0] = '\0'; /* LSL #0 is the default, so skip appending it */
else
sprintf(field, "%s #%d", shift_type(type), count);
break;
case 1:
case 2:
if (count == 0)
count = 32;
sprintf(field, "%s #%d", shift_type(type), count);
break;
case 3:
if (count == 0)
sprintf(field, "rrx #1");
else
sprintf(field, "%s #%d", shift_type(type), count);
break;
}
return field;
}
static bool thumb2_constshift(ARMSTATE *state, uint32_t instr)
{
/* 1110 101x xxxx xxxx - data processing, constant shift */
int Rm = FIELD(instr, 0, 4);
int Rd = FIELD(instr, 8, 4);
int Rn = FIELD(instr, 16, 4);
int opc = FIELD(instr, 21, 4);
int shifttype = FIELD(instr, 4, 2);
int imm = (FIELD(instr, 12, 3) << 2) | FIELD(instr, 6, 2);
int setflags = FIELD(instr, 20, 1);
switch (opc) {
case 0:
if (Rd == 15 && setflags) {
strcpy(state->text, "tst");
setflags = 0;
} else {
strcpy(state->text, "and");
}
break;
case 1:
strcpy(state->text, "bic");
break;
case 2:
if (Rn == 15) {
switch (shifttype) {
case 0:
if (imm == 0)
strcpy(state->text, "mov");
else
strcpy(state->text, "lsl");
break;
case 1:
strcpy(state->text, "lsr");
break;
case 2:
strcpy(state->text, "asr");
break;
case 3:
if (imm == 0)
strcpy(state->text, "rrx");
else
strcpy(state->text, "ror");
break;
}
} else {