-
Notifications
You must be signed in to change notification settings - Fork 11
/
recomp.cpp
executable file
·2935 lines (2778 loc) · 115 KB
/
recomp.cpp
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
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <capstone/capstone.h>
#include "elf.h"
#define INSPECT_FUNCTION_POINTERS 0 // set this to 1 when testing a new program, to verify that no false function pointers are found
#ifndef TRACE
#define TRACE 0
#endif
#ifndef ugen53
#define ugen53 0
#endif
#define LABELS_64_BIT 1
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define u32be(x) (uint32_t)(((x & 0xff) << 24) + ((x & 0xff00) << 8) + ((x & 0xff0000) >> 8) + ((uint32_t)(x) >> 24))
#define u16be(x) (uint16_t)(((x & 0xff) << 8) + ((x & 0xff00) >> 8))
#define read_u32_be(buf) (uint32_t)(((buf)[0] << 24) + ((buf)[1] << 16) + ((buf)[2] << 8) + ((buf)[3]))
using namespace std;
struct Edge {
uint32_t i;
uint8_t function_entry: 1;
uint8_t function_exit: 1;
uint8_t extern_function: 1;
uint8_t function_pointer: 1;
};
struct Insn {
uint32_t id;
uint8_t op_count;
string mnemonic;
string op_str;
cs_mips_op operands[8];
uint8_t is_jump: 1;
uint8_t is_global_got_memop: 1;
uint8_t no_following_successor: 1;
int linked_insn;
union {
uint32_t linked_value;
float linked_float;
};
uint32_t jtbl_addr;
uint32_t num_cases;
mips_reg index_reg;
vector<Edge> successors;
vector<Edge> predecessors;
uint64_t b_liveout;
uint64_t b_livein;
uint64_t f_livein;
uint64_t f_liveout;
};
struct Function {
vector<uint32_t> returns; //points to delay slots
uint32_t end_addr; //address after end
uint32_t nargs;
uint32_t nret;
bool v0_in;
bool referenced_by_function_pointer;
};
static csh handle;
static const uint8_t *text_section;
static uint32_t text_section_len;
static uint32_t text_vaddr;
static const uint8_t *rodata_section;
static uint32_t rodata_section_len;
static uint32_t rodata_vaddr;
static const uint8_t *data_section;
static uint32_t data_section_len;
static uint32_t data_vaddr;
static uint32_t bss_section_len;
static uint32_t bss_vaddr;
static vector<Insn> insns;
static set<uint32_t> label_addresses;
static vector<uint32_t> got_globals;
static vector<uint32_t> got_locals;
static uint32_t gp_value;
static uint32_t gp_value_adj;
static map<uint32_t, string> symbol_names;
static vector<pair<uint32_t, uint32_t>> data_function_pointers;
static set<uint32_t> li_function_pointers;
static map<uint32_t, Function> functions;
static uint32_t main_addr;
static uint32_t mcount_addr;
static uint32_t procedure_table_start;
static uint32_t procedure_table_len;
#define FLAG_NO_MEM 1
#define FLAG_VARARG 2
static const struct {
const char *name;
const char *params;
int flags;
} extern_functions[] = {
{"exit", "vi"}, // override exit from application
{"abort", "v"},
{"sbrk", "pi"},
{"malloc", "pu"},
{"calloc", "puu"},
{"realloc", "ppu"},
{"free", "vp"},
{"fscanf", "ipp", FLAG_VARARG},
{"printf", "ip", FLAG_VARARG},
{"sprintf", "ipp", FLAG_VARARG},
{"fprintf", "ipp", FLAG_VARARG},
{"_doprnt", "ippp"},
{"strlen", "up"},
{"open", "ipii"},
{"creat", "ipi"},
{"access", "ipi"},
{"rename", "ipp"},
{"utime", "ipp"},
{"flock", "iii"},
{"chmod", "ipu"},
{"umask", "ii", FLAG_NO_MEM},
{"ecvt", "pdipp"},
{"fcvt", "pdipp"},
{"sqrt", "dd", FLAG_NO_MEM},
{"sqrtf", "ff", FLAG_NO_MEM},
{"atoi", "ip"},
{"atol", "ip"},
{"atof", "dp"},
{"strtol", "ippi"},
{"strtoul", "uppi"},
{"strtod", "dpp"},
{"strchr", "ppi"},
{"strrchr", "ppi"},
{"strcspn", "upp"},
{"strpbrk", "ppp"},
{"fstat", "iip"},
{"stat", "ipp"},
{"ftruncate", "iii"},
{"bcopy", "vppu"},
{"memcpy", "pppu"},
{"memccpy", "pppiu"},
{"read", "iipu"},
{"write", "iipu"},
{"fopen", "ppp"},
{"freopen", "pppp"},
{"fclose", "ip"},
{"ftell", "ip"},
{"rewind", "ip"},
{"fseek", "ipii"},
{"lseek", "iiii"},
{"fflush", "ip"},
{"dup", "ii"},
{"dup2", "iii"},
{"pipe", "ip"},
{"perror", "vp"},
{"fdopen", "iip"},
{"memset", "ppiu"},
{"bcmp", "ippu"},
{"memcmp", "ippu"},
{"getpid", "i", FLAG_NO_MEM},
{"getpgrp", "i"},
{"remove", "ip"},
{"unlink", "ip"},
{"close", "ii"},
{"strcmp", "ipp"},
{"strncmp", "ippu"},
{"strcpy", "ppp"},
{"strncpy", "pppu"},
{"strcat", "ppp"},
{"strncat", "pppu"},
{"strtok", "ppp"},
{"strstr", "ppp"},
{"strdup", "pp"},
{"toupper", "ii", FLAG_NO_MEM},
{"tolower", "ii", FLAG_NO_MEM},
{"gethostname", "ipu"},
{"isatty", "ii"},
{"strftime", "upupp"},
{"times", "ip"},
{"clock", "i", FLAG_NO_MEM},
{"ctime", "pp"},
{"localtime", "pp"},
{"setvbuf", "ippiu"},
{"__semgetc", "ip"},
{"__semputc", "iip"},
{"fgetc", "ip"},
{"fgets", "ipip"},
{"__filbuf", "ip"},
{"__flsbuf", "iip"},
{"ungetc", "iip"},
{"gets", "pp"},
{"fread", "upuup"},
{"fwrite", "upuup"},
{"fputs", "ipp"},
{"puts", "ip"},
{"getcwd", "ppu"},
{"time", "ip"},
{"bzero", "vpu"},
{"fp_class_d", "id", FLAG_NO_MEM},
{"ldexp", "ddi", FLAG_NO_MEM},
{"__ll_mul", "lll", FLAG_NO_MEM},
{"__ll_div", "lll", FLAG_NO_MEM},
{"__ll_rem", "ljl", FLAG_NO_MEM},
{"__ll_lshift", "llj", FLAG_NO_MEM},
{"__ll_rshift", "llj", FLAG_NO_MEM},
{"__ull_div", "jjj", FLAG_NO_MEM},
{"__ull_rem", "jjj", FLAG_NO_MEM},
{"__ull_rshift", "jjj", FLAG_NO_MEM},
{"__d_to_ull", "jd", FLAG_NO_MEM},
{"__d_to_ll", "ld", FLAG_NO_MEM},
{"__f_to_ull", "jf", FLAG_NO_MEM},
{"__f_to_ll", "lf", FLAG_NO_MEM},
{"__ull_to_f", "fj", FLAG_NO_MEM},
{"__ll_to_f", "fl", FLAG_NO_MEM},
{"__ull_to_d", "dj", FLAG_NO_MEM},
{"__ll_to_d", "dl", FLAG_NO_MEM},
{"_exit", "vi"},
{"_cleanup", "v"},
{"_rld_new_interface", "pu", FLAG_VARARG},
{"_exithandle", "v"},
{"_prctl", "ii", FLAG_VARARG},
{"_atod", "dpii"},
{"pathconf", "ipi"},
{"getenv", "pp"},
{"gettxt", "ppp"},
{"setlocale", "pip"},
{"mmap", "ppuiiii"},
{"munmap", "ipu"},
{"mprotect", "ipui"},
{"sysconf", "ii"},
{"getpagesize", "i"},
{"strerror", "pi"},
{"ioctl", "iiu", FLAG_VARARG},
{"fcntl", "iii", FLAG_VARARG},
{"signal", "pit"},
{"sigset", "pit"},
{"get_fpc_csr", "i"},
{"set_fpc_csr", "ii"},
{"setjmp", "ip"},
{"longjmp", "vpi"},
{"tempnam", "ppp"},
{"tmpnam", "pp"},
{"mktemp", "pp"},
{"mkstemp", "ip"},
{"tmpfile", "p"},
{"wait", "ip"},
{"kill", "iii"},
{"execlp", "ip", FLAG_VARARG},
{"execv", "ipp"},
{"execvp", "ipp"},
{"fork", "i"},
{"system", "ip"},
{"tsearch", "pppp"},
{"tfind", "pppp"},
{"qsort", "vpuut"},
{"regcmp", "pp", FLAG_VARARG},
{"regex", "ppp", FLAG_VARARG},
{"__assert", "vppi"},
};
static void disassemble(void) {
csh handle;
cs_insn *disasm;
static size_t disasm_size;
assert(cs_open(CS_ARCH_MIPS, (cs_mode)(CS_MODE_MIPS64 | CS_MODE_BIG_ENDIAN), &handle) == CS_ERR_OK);
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
disasm_size = cs_disasm(handle, text_section, text_section_len, text_vaddr, 0, &disasm);
for (size_t i = 0; i < disasm_size; i++) {
insns.push_back(Insn());
Insn& insn = insns.back();
insn.id = disasm[i].id;
insn.mnemonic = disasm[i].mnemonic;
insn.op_str = disasm[i].op_str;
if (disasm[i].detail != nullptr && disasm[i].detail->mips.op_count > 0) {
insn.op_count = disasm[i].detail->mips.op_count;
memcpy(insn.operands, disasm[i].detail->mips.operands, sizeof(insn.operands));
}
insn.is_jump = cs_insn_group(handle, &disasm[i], MIPS_GRP_JUMP) || insn.id == MIPS_INS_JAL || insn.id == MIPS_INS_BAL || insn.id == MIPS_INS_JALR;
insn.linked_insn = -1;
}
cs_free(disasm, disasm_size);
cs_close(&handle);
{
// Add dummy instruction to avoid out of bounds
insns.push_back(Insn());
Insn& insn = insns.back();
insn.id = MIPS_INS_NOP;
insn.mnemonic = "nop";
insn.no_following_successor = true;
}
}
static void add_function(uint32_t addr) {
if (addr >= text_vaddr && addr < text_vaddr + text_section_len) {
functions[addr];
}
}
static map<uint32_t, Function>::iterator find_function(uint32_t addr) {
if (functions.size() == 0) {
return functions.end();
}
auto it = functions.upper_bound(addr);
if (it == functions.begin()) {
return functions.end();
}
--it;
return it;
}
// try to find a matching LUI for a given register
static void link_with_lui(int offset, uint32_t reg, int mem_imm)
{
#define MAX_LOOKBACK 128
// don't attempt to compute addresses for zero offset
// end search after some sane max number of instructions
int end_search = MAX(0, offset - MAX_LOOKBACK);
for (int search = offset - 1; search >= end_search; search--) {
// use an `if` instead of `case` block to allow breaking out of the `for` loop
if (insns[search].id == MIPS_INS_LUI) {
uint32_t rd = insns[search].operands[0].reg;
if (reg == rd) {
break;
}
} else if (insns[search].id == MIPS_INS_LW ||
insns[search].id == MIPS_INS_LD ||
insns[search].id == MIPS_INS_ADDIU ||
//insns[search].id == MIPS_INS_ADDU || used in jump tables for offset
insns[search].id == MIPS_INS_ADD ||
insns[search].id == MIPS_INS_SUB ||
insns[search].id == MIPS_INS_SUBU) {
uint32_t rd = insns[search].operands[0].reg;
if (reg == rd) {
if (insns[search].id == MIPS_INS_LW && insns[search].operands[1].mem.base == MIPS_REG_GP) {
int mem_imm0 = (int)insns[search].operands[1].mem.disp;
uint32_t got_entry = (mem_imm0 + gp_value_adj) / sizeof(uint32_t);
if (got_entry < got_locals.size()) {
// used for static functions
char buf[32];
uint32_t addr = got_locals[got_entry] + mem_imm;
insns[search].linked_insn = offset;
insns[search].linked_value = addr;
insns[offset].linked_insn = search;
insns[offset].linked_value = addr;
//vaddr_references[addr].insert(text_vaddr + offset * 4);
insns[search].id = MIPS_INS_LI;
insns[search].mnemonic = "li";
sprintf(buf, "$%s, 0x%x", cs_reg_name(handle, rd), addr);
insns[search].op_str = buf;
insns[search].operands[1].type = MIPS_OP_IMM;
insns[search].operands[1].imm = addr;
switch (insns[offset].id) {
case MIPS_INS_ADDIU:
insns[offset].id = MIPS_INS_MOVE;
insns[offset].operands[1].type = MIPS_OP_REG;
insns[offset].mnemonic = "move";
sprintf(buf, "$%s, $%s", cs_reg_name(handle, insns[offset].operands[0].reg), cs_reg_name(handle, rd));
insns[offset].op_str = buf;
if (addr >= text_vaddr && addr < text_vaddr + text_section_len) {
add_function(addr);
}
break;
case MIPS_INS_LB:
case MIPS_INS_LBU:
case MIPS_INS_SB:
case MIPS_INS_LH:
case MIPS_INS_LHU:
case MIPS_INS_SH:
case MIPS_INS_LW:
case MIPS_INS_SW:
case MIPS_INS_LDC1:
case MIPS_INS_LWC1:
case MIPS_INS_SWC1:
insns[offset].operands[1].mem.disp = 0;
sprintf(buf, "$%s, ($%s)", cs_reg_name(handle, insns[offset].operands[0].reg), cs_reg_name(handle, rd));
insns[offset].op_str = buf;
break;
default:
assert(0);
}
}
break;
} else {
// ignore: reg is pointer, offset is probably struct data member
break;
}
}
} else if (insns[search].id == MIPS_INS_JR &&
insns[search].operands[0].reg == MIPS_REG_RA && offset - search >= 2) {
// stop looking when previous `jr ra` is hit,
// but ignore if `offset` is branch delay slot for this `jr ra`
break;
}
}
}
// for a given `jalr t9`, find the matching t9 load
static void link_with_jalr(int offset)
{
// end search after some sane max number of instructions
int end_search = MAX(0, offset - MAX_LOOKBACK);
for (int search = offset - 1; search >= end_search; search--) {
if (insns[search].operands[0].reg == MIPS_REG_T9) {
if (insns[search].id == MIPS_INS_LW || insns[search].id == MIPS_INS_LI) {
if (insns[search].is_global_got_memop || insns[search].id == MIPS_INS_LI) {
char buf[32];
sprintf(buf, "0x%x", insns[search].linked_value);
insns[search].linked_insn = offset;
insns[offset].linked_insn = search;
insns[offset].linked_value = insns[search].linked_value;
//insns[offset].label = insns[search].label;
//function_entry_points.insert(insns[search].linked_value);
insns[offset].id = MIPS_INS_JAL;
insns[offset].mnemonic = "jal";
insns[offset].op_str = buf;
insns[offset].operands[0].type = MIPS_OP_IMM;
insns[offset].operands[0].imm = insns[search].linked_value;
insns[search].id = MIPS_INS_NOP;
insns[search].mnemonic = "nop";
insns[search].op_str = "";
insns[search].is_global_got_memop = false;
add_function(insns[search].linked_value);
}
break;
} else if (insns[search].id == MIPS_INS_ADDIU) {
if (insns[search].linked_insn != -1) {
//function_entry_points.insert(insns[search].linked_value);
uint32_t first = insns[search].linked_insn;
insns[search].linked_insn = offset;
insns[offset].linked_insn = first;
insns[offset].linked_value = insns[search].linked_value;
}
break;
} else if (insns[search].id == MIPS_INS_LI) {
if (insns[search].linked_insn != -1) {
//function_entry_points.insert(insns[search].linked_value);
uint32_t first = insns[search].linked_insn;
insns[search].linked_insn = offset;
insns[offset].linked_insn = first;
insns[offset].linked_value = insns[search].linked_value;
insns[search].id = MIPS_INS_NOP;
insns[search].mnemonic = "nop";
insns[search].op_str = "";
}
break;
} else if (insns[search].id == MIPS_INS_LD ||
insns[search].id == MIPS_INS_ADDU ||
insns[search].id == MIPS_INS_ADD ||
insns[search].id == MIPS_INS_SUB ||
insns[search].id == MIPS_INS_SUBU) {
break;
}
} else if (insns[search].id == MIPS_INS_JR &&
insns[search].operands[0].reg == MIPS_REG_RA)
{
// stop looking when previous `jr ra` is hit
break;
}
}
}
static void pass1(void) {
for (size_t i = 0; i < insns.size(); i++) {
Insn& insn = insns[i];
if (insn.id == MIPS_INS_BAL) {
insn.id = MIPS_INS_JAL;
insn.mnemonic = "jal";
}
if (insn.is_jump) {
if (insn.id == MIPS_INS_JAL || insn.id == MIPS_INS_J) {
uint32_t target = (uint32_t)insn.operands[0].imm;
label_addresses.insert(target);
add_function(target);
} else if (insn.id == MIPS_INS_JR) {
// sltiu $at, $ty, z
// sw $reg, offset($sp) (very seldom, one or more, usually in func entry)
// lw $gp, offset($sp) (if PIC, and very seldom)
// beqz $at, .L
// some other instruction (not always)
// lui $at, %hi(jtbl)
// sll $tx, $ty, 2
// addu $at, $at, $tx
// lw $tx, %lo(jtbl)($at)
// nop (code compiled with 5.3)
// addu $tx, $tx, $gp (if PIC)
// jr $tx
// IDO 7.1:
//lw at,offset(gp)
//andi t9,t8,0x3f
//sll t9,t9,0x2
//addu at,at,t9
//lw t9,offset(at)
//addu t9,t9,gp
//jr t9
// IDO 5.3:
//lw at,offset(gp)
//andi t3,t2,0x3f
//sll t3,t3,0x2
//addu at,at,t3
//something
//lw t3,offset(at)
//something
//addu t3,t3,gp
//jr t3
if (i >= 7 && rodata_section != NULL) {
bool is_pic = insns[i - 1].id == MIPS_INS_ADDU && insns[i - 1].operands[2].reg == MIPS_REG_GP;
bool has_nop = insns[i - is_pic - 1].id == MIPS_INS_NOP;
bool has_extra = insns[i - is_pic - has_nop - 5].id != MIPS_INS_BEQZ;
int lw = i - is_pic - has_nop - 1;
if (insns[lw].id != MIPS_INS_LW) {
--lw;
}
if (insns[lw].id == MIPS_INS_LW && insns[lw].linked_insn != -1) {
int sltiu_index = -1;
int andi_index = -1;
uint32_t addu_index = lw - 1;
uint32_t num_cases;
bool found = false;
bool and_variant = false;
int end = 14;
if (insns[addu_index].id != MIPS_INS_ADDU) {
--addu_index;
}
mips_reg index_reg = (mips_reg)insns[addu_index - 1].operands[1].reg;
if (insns[addu_index].id != MIPS_INS_ADDU) {
goto skip;
}
if (insns[addu_index - 1].id != MIPS_INS_SLL) {
goto skip;
}
if (insns[addu_index - 1].operands[0].reg != insn.operands[0].reg) {
goto skip;
}
for (int j = 3; j <= 4; j++) {
if (insns[lw - j].id == MIPS_INS_ANDI) {
andi_index = lw - j;
break;
}
}
if (i == 368393) {
// In copt
end = 18;
}
for (int j = 5; j <= end; j++) {
if (insns[lw - has_extra - j].id == MIPS_INS_SLTIU &&
insns[lw - has_extra - j].operands[0].reg == MIPS_REG_AT)
{
sltiu_index = j;
break;
}
if (insns[lw - has_extra - j].id == MIPS_INS_JR) {
// Prevent going into a previous switch
break;
}
}
if (sltiu_index != -1) {
andi_index = -1;
}
if (sltiu_index != -1 && insns[lw - has_extra - sltiu_index].id == MIPS_INS_SLTIU) {
num_cases = insns[lw - has_extra - sltiu_index].operands[2].imm;
found = true;
} else if (andi_index != -1) {
num_cases = insns[andi_index].operands[2].imm + 1;
found = true;
and_variant = true;
} else if (i == 219382) {
// Special hard case in copt where the initial sltiu is in another basic block
found = true;
num_cases = 13;
} else if (i == 370995) {
// Special hard case in copt where the initial sltiu is in another basic block
found = true;
num_cases = 12;
}
if (found) {
uint32_t jtbl_addr = insns[lw].linked_value;
if (is_pic) {
insns[i - 1].id = MIPS_INS_NOP;
}
//printf("jump table at %08x, size %u\n", jtbl_addr, num_cases);
insn.jtbl_addr = jtbl_addr;
insn.num_cases = num_cases;
insn.index_reg = index_reg;
insns[lw].id = MIPS_INS_NOP;
insns[addu_index].id = MIPS_INS_NOP;
insns[addu_index - 1].id = MIPS_INS_NOP;
if (!and_variant) {
insns[addu_index - 2].id = MIPS_INS_NOP;
}
if (jtbl_addr < rodata_vaddr || jtbl_addr + num_cases * sizeof(uint32_t) > rodata_vaddr + rodata_section_len) {
fprintf(stderr, "jump table outside rodata\n");
exit(EXIT_FAILURE);
}
for (uint32_t i = 0; i < num_cases; i++) {
uint32_t target_addr = read_u32_be(rodata_section + (jtbl_addr - rodata_vaddr) + i * sizeof(uint32_t));
target_addr += gp_value;
//printf("%08X\n", target_addr);
label_addresses.insert(target_addr);
}
}
skip:;
}
}
} else {
for (int j = 0; j < insn.op_count; j++) {
if (insn.operands[j].type == MIPS_OP_IMM) {
uint32_t target = (uint32_t)insn.operands[j].imm;
label_addresses.insert(target);
}
}
}
}
switch (insns[i].id) {
// find floating point LI
case MIPS_INS_MTC1:
{
unsigned int rt = insns[i].operands[0].reg;
for (int s = i - 1; s >= 0; s--) {
if (insns[s].id == MIPS_INS_LUI && insns[s].operands[0].reg == rt) {
float f;
uint32_t lui_imm = (uint32_t)(insns[s].operands[1].imm << 16);
memcpy(&f, &lui_imm, sizeof(f));
insns[s].operands[1].imm <<= 16;
// link up the LUI with this instruction and the float
insns[s].linked_insn = i;
insns[s].linked_float = f;
// rewrite LUI instruction to be LI
insns[s].id = MIPS_INS_LI;
insns[s].mnemonic = "li";
break;
} else if (insns[s].id == MIPS_INS_LW ||
insns[s].id == MIPS_INS_LD ||
insns[s].id == MIPS_INS_LH ||
insns[s].id == MIPS_INS_LHU ||
insns[s].id == MIPS_INS_LB ||
insns[s].id == MIPS_INS_LBU ||
insns[s].id == MIPS_INS_ADDIU ||
insns[s].id == MIPS_INS_ADD ||
insns[s].id == MIPS_INS_SUB ||
insns[s].id == MIPS_INS_SUBU) {
unsigned int rd = insns[s].operands[0].reg;
if (rt == rd) {
break;
}
} else if (insns[s].id == MIPS_INS_JR &&
insns[s].operands[0].reg == MIPS_REG_RA) {
// stop looking when previous `jr ra` is hit
break;
}
}
break;
}
case MIPS_INS_SD:
case MIPS_INS_SW:
case MIPS_INS_SH:
case MIPS_INS_SB:
case MIPS_INS_LB:
case MIPS_INS_LBU:
case MIPS_INS_LD:
case MIPS_INS_LDL:
case MIPS_INS_LDR:
case MIPS_INS_LH:
case MIPS_INS_LHU:
case MIPS_INS_LW:
case MIPS_INS_LWU:
case MIPS_INS_LDC1:
case MIPS_INS_LWC1:
case MIPS_INS_LWC2:
case MIPS_INS_LWC3:
case MIPS_INS_SWC1:
case MIPS_INS_SWC2:
case MIPS_INS_SWC3:
{
unsigned int mem_rs = insns[i].operands[1].mem.base;
int mem_imm = (int)insns[i].operands[1].mem.disp;
if (mem_rs == MIPS_REG_GP) {
unsigned int got_entry = (mem_imm + gp_value_adj) / sizeof(unsigned int);
if (got_entry >= got_locals.size()) {
got_entry -= got_locals.size();
if (got_entry < got_globals.size()) {
assert(insn.id == MIPS_INS_LW);
//printf("gp 0x%08x %s\n", mem_imm, got_globals[got_entry].name);
unsigned int dest_vaddr = got_globals[got_entry];
insns[i].is_global_got_memop = true;
insns[i].linked_value = dest_vaddr;
//insns[i].label = got_globals[got_entry].name;
//vaddr_references[dest_vaddr].insert(vaddr + i * 4);
//disasm_add_data_addr(state, dest_vaddr);
insns[i].id = MIPS_INS_LI;
insns[i].operands[1].imm = dest_vaddr;
char buf[32];
sprintf(buf, "$%s, 0x%x", cs_reg_name(handle, insn.operands[0].reg), dest_vaddr);
insns[i].op_str = buf;
}
}
} else {
link_with_lui(i, mem_rs, mem_imm);
}
break;
}
case MIPS_INS_ADDIU:
case MIPS_INS_ORI:
{
unsigned int rd = insns[i].operands[0].reg;
unsigned int rs = insns[i].operands[1].reg;
int64_t imm = insns[i].operands[2].imm;
if (rs == MIPS_REG_ZERO) { // becomes LI
char buf[32];
insns[i].id = MIPS_INS_LI;
insns[i].operands[1].imm = imm;
insns[i].mnemonic = "li";
sprintf(buf, "$%s, %" PRIi64, cs_reg_name(handle, rd), imm);
insns[i].op_str = buf;
} else if (/*rd == rs &&*/ rd != MIPS_REG_GP) { // only look for LUI if rd and rs are the same
link_with_lui(i, rs, (int)imm);
}
break;
}
case MIPS_INS_JALR:
{
unsigned int r = insn.operands[0].reg;
if (r == MIPS_REG_T9) {
link_with_jalr(i);
if (insn.linked_insn != -1) {
char buf[32];
sprintf(buf, "0x%x", insn.linked_value);
insn.id = MIPS_INS_JAL;
insn.mnemonic = "jal";
insn.op_str = buf;
insn.operands[0].type = MIPS_OP_IMM;
insn.operands[0].imm = insn.linked_value;
label_addresses.insert(insn.linked_value);
add_function(insn.linked_value);
}
}
break;
}
}
if (insn.id == MIPS_INS_ADDU && insn.operands[0].reg == MIPS_REG_GP && insn.operands[1].reg == MIPS_REG_GP && insn.operands[2].reg == MIPS_REG_T9 && i >= 2) {
//state->function_entry_points.insert(vaddr + (i - 2) * 4);
for (int j = i - 2; j <= i; j++) {
insns[j].id = MIPS_INS_NOP;
insns[j].mnemonic = "nop";
insns[j].op_str = "";
}
}
}
}
static uint32_t addr_to_i(uint32_t addr) {
return (addr - text_vaddr) / 4;
}
static void pass2(void) {
// Find returns in each function
for (size_t i = 0; i < insns.size(); i++) {
uint32_t addr = text_vaddr + i * 4;
Insn& insn = insns[i];
if (insn.id == MIPS_INS_JR && insn.operands[0].reg == MIPS_REG_RA) {
auto it = find_function(addr);
assert(it != functions.end());
it->second.returns.push_back(addr + 4);
}
if (insn.is_global_got_memop && text_vaddr <= insn.operands[1].imm && insn.operands[1].imm < text_vaddr + text_section_len) {
uint32_t faddr = insn.operands[1].imm;
li_function_pointers.insert(faddr);
functions[faddr].referenced_by_function_pointer = true;
#if INSPECT_FUNCTION_POINTERS
fprintf(stderr, "li function pointer: 0x%x at 0x%x\n", faddr, addr);
#endif
}
}
for (auto it = functions.begin(); it != functions.end(); ++it) {
if (it->second.returns.size() == 0) {
uint32_t i = addr_to_i(it->first);
auto str_it = symbol_names.find(it->first);
if (str_it != symbol_names.end() && str_it->second == "__start") {
} else if (str_it != symbol_names.end() && str_it->second == "xmalloc") {
// orig 5.3:
/*
496bf4: 3c1c0fb9 lui gp,0xfb9
496bf8: 279c366c addiu gp,gp,13932
496bfc: 0399e021 addu gp,gp,t9
496c00: 27bdffd8 addiu sp,sp,-40
496c04: 8f858de8 lw a1,-29208(gp)
496c08: 10000006 b 496c24 <alloc_new+0x14>
496c0c: afbf0020 sw ra,32(sp)
*/
// jal alloc_new
// lui $a1, malloc_scb
// jr $ra
// nop
uint32_t alloc_new_addr = text_vaddr + (i + 7) * 4;
insns[i].id = MIPS_INS_JAL;
insns[i].op_count = 1;
insns[i].mnemonic = "jal";
insns[i].op_str = "alloc_new";
insns[i].operands[0].imm = alloc_new_addr;
assert(symbol_names.count(alloc_new_addr) && symbol_names[alloc_new_addr] == "alloc_new");
i++;
if (insns[i + 5].id == MIPS_INS_LI) {
// 7.1
insns[i] = insns[i + 5];
} else {
// 5.3
insns[i] = insns[i + 3];
}
i++;
insns[i].id = MIPS_INS_JR;
insns[i].op_count = 1;
insns[i].mnemonic = "jr";
insns[i].op_str = "$ra";
insns[i].operands[0].reg = MIPS_REG_RA;
it->second.returns.push_back(text_vaddr + i * 4 + 4);
i++;
for (uint32_t j = 0; j < 4; j++) {
insns[i].id = MIPS_INS_NOP;
insns[i].op_count = 0;
insns[i].mnemonic = "nop";
i++;
}
} else if (str_it != symbol_names.end() && str_it->second == "xfree") {
// jal alloc_dispose
// lui $a1, malloc_scb
// jr $ra
// nop
uint32_t alloc_dispose_addr = text_vaddr + (i + 4) * 4;
if (symbol_names.count(alloc_dispose_addr + 4) && symbol_names[alloc_dispose_addr + 4] == "alloc_dispose") {
alloc_dispose_addr += 4;
}
insns[i].id = MIPS_INS_JAL;
insns[i].op_count = 1;
insns[i].mnemonic = "jal";
insns[i].op_str = "alloc_dispose";
insns[i].operands[0].imm = alloc_dispose_addr;
assert(symbol_names.count(alloc_dispose_addr) && symbol_names[alloc_dispose_addr] == "alloc_dispose");
i++;
insns[i] = insns[i + 2];
i++;
insns[i].id = MIPS_INS_JR;
insns[i].op_count = 1;
insns[i].mnemonic = "jr";
insns[i].op_str = "$ra";
insns[i].operands[0].reg = MIPS_REG_RA;
it->second.returns.push_back(text_vaddr + i * 4 + 4);
i++;
insns[i].id = MIPS_INS_NOP;
insns[i].op_count = 0;
insns[i].mnemonic = "nop";
} else if (insns[i].id == MIPS_INS_LW && insns[i + 1].id == MIPS_INS_MOVE && insns[i + 2].id == MIPS_INS_JALR) {
/*
408f50: 8f998010 lw t9,-32752(gp)
408f54: 03e07821 move t7,ra
408f58: 0320f809 jalr t9
*/
} else if (it->first > mcount_addr) {
fprintf(stderr, "no ret: 0x%x\n", it->first);
abort();
}
}
auto next = it;
++next;
if (next == functions.end()) {
it->second.end_addr = text_vaddr + text_section_len;
} else {
it->second.end_addr = next->first;
}
}
}
static void add_edge(uint32_t from, uint32_t to, bool function_entry = false, bool function_exit = false, bool extern_function = false, bool function_pointer = false) {
Edge fe = Edge(), be = Edge();
fe.i = to;
be.i = from;
fe.function_entry = function_entry;
be.function_entry = function_entry;
fe.function_exit = function_exit;
be.function_exit = function_exit;
fe.extern_function = extern_function;
be.extern_function = extern_function;
fe.function_pointer = function_pointer;
be.function_pointer = function_pointer;
insns[from].successors.push_back(fe);
insns[to].predecessors.push_back(be);
}
static void pass3(void) {
// Build graph
for (size_t i = 0; i < insns.size(); i++) {
uint32_t addr = text_vaddr + i * 4;
Insn& insn = insns[i];
if (insn.no_following_successor) {
continue;
}
switch (insn.id) {
case MIPS_INS_BEQ:
case MIPS_INS_BGEZ:
case MIPS_INS_BGTZ:
case MIPS_INS_BLEZ:
case MIPS_INS_BLTZ:
case MIPS_INS_BNE:
case MIPS_INS_BEQZ:
case MIPS_INS_BNEZ:
case MIPS_INS_BC1F:
case MIPS_INS_BC1T:
add_edge(i, i + 1);
add_edge(i + 1, addr_to_i((uint32_t)insn.operands[insn.op_count - 1].imm));
break;
case MIPS_INS_BEQL:
case MIPS_INS_BGEZL:
case MIPS_INS_BGTZL:
case MIPS_INS_BLEZL:
case MIPS_INS_BLTZL:
case MIPS_INS_BNEL:
case MIPS_INS_BC1FL:
case MIPS_INS_BC1TL:
add_edge(i, i + 1);
add_edge(i, i + 2);
add_edge(i + 1, addr_to_i((uint32_t)insn.operands[insn.op_count - 1].imm));
insns[i + 1].no_following_successor = true; // don't inspect delay slot
break;
case MIPS_INS_B:
case MIPS_INS_J:
add_edge(i, i + 1);
add_edge(i + 1, addr_to_i((uint32_t)insn.operands[0].imm));
insns[i + 1].no_following_successor = true; // don't inspect delay slot
break;
case MIPS_INS_JR: {
add_edge(i, i + 1);
if (insn.jtbl_addr != 0) {
uint32_t jtbl_pos = insn.jtbl_addr - rodata_vaddr;
assert(jtbl_pos < rodata_section_len && jtbl_pos + insn.num_cases * 4 <= rodata_section_len);
for (uint32_t j = 0; j < insn.num_cases; j++) {
uint32_t dest_addr = read_u32_be(rodata_section + jtbl_pos + j * 4) + gp_value;
add_edge(i + 1, addr_to_i(dest_addr));
}
} else {
assert(insn.operands[0].reg == MIPS_REG_RA && "jump to address in register not supported");
}
insns[i + 1].no_following_successor = true; // don't inspect delay slot
break;
}
case MIPS_INS_JAL: {
add_edge(i, i + 1);
uint32_t dest = (uint32_t)insn.operands[0].imm;
if (dest > mcount_addr && dest >= text_vaddr && dest < text_vaddr + text_section_len) {
add_edge(i + 1, addr_to_i(dest), true);
auto it = functions.find(dest);
assert(it != functions.end());
for (uint32_t ret_instr : it->second.returns) {
add_edge(addr_to_i(ret_instr), i + 2, false, true);
}
} else {
add_edge(i + 1, i + 2, false, false, true);
}
insns[i + 1].no_following_successor = true; // don't inspect delay slot
break;
}
case MIPS_INS_JALR:
// function pointer
add_edge(i, i + 1);
add_edge(i + 1, i + 2, false, false, false, true);
insns[i + 1].no_following_successor = true; // don't inspect delay slot
break;