-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
elf.cpp
2920 lines (2628 loc) · 83.3 KB
/
elf.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../NLS.h"
#include "../common/Port.h"
#include "GBA.h"
#include "elf.h"
#define elfReadMemory(addr) \
READ32LE((&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask]))
#define DW_TAG_array_type 0x01
#define DW_TAG_enumeration_type 0x04
#define DW_TAG_formal_parameter 0x05
#define DW_TAG_label 0x0a
#define DW_TAG_lexical_block 0x0b
#define DW_TAG_member 0x0d
#define DW_TAG_pointer_type 0x0f
#define DW_TAG_reference_type 0x10
#define DW_TAG_compile_unit 0x11
#define DW_TAG_structure_type 0x13
#define DW_TAG_subroutine_type 0x15
#define DW_TAG_typedef 0x16
#define DW_TAG_union_type 0x17
#define DW_TAG_unspecified_parameters 0x18
#define DW_TAG_inheritance 0x1c
#define DW_TAG_inlined_subroutine 0x1d
#define DW_TAG_subrange_type 0x21
#define DW_TAG_base_type 0x24
#define DW_TAG_const_type 0x26
#define DW_TAG_enumerator 0x28
#define DW_TAG_subprogram 0x2e
#define DW_TAG_variable 0x34
#define DW_TAG_volatile_type 0x35
#define DW_AT_sibling 0x01
#define DW_AT_location 0x02
#define DW_AT_name 0x03
#define DW_AT_byte_size 0x0b
#define DW_AT_bit_offset 0x0c
#define DW_AT_bit_size 0x0d
#define DW_AT_stmt_list 0x10
#define DW_AT_low_pc 0x11
#define DW_AT_high_pc 0x12
#define DW_AT_language 0x13
#define DW_AT_compdir 0x1b
#define DW_AT_const_value 0x1c
#define DW_AT_containing_type 0x1d
#define DW_AT_inline 0x20
#define DW_AT_producer 0x25
#define DW_AT_prototyped 0x27
#define DW_AT_upper_bound 0x2f
#define DW_AT_abstract_origin 0x31
#define DW_AT_accessibility 0x32
#define DW_AT_artificial 0x34
#define DW_AT_data_member_location 0x38
#define DW_AT_decl_file 0x3a
#define DW_AT_decl_line 0x3b
#define DW_AT_declaration 0x3c
#define DW_AT_encoding 0x3e
#define DW_AT_external 0x3f
#define DW_AT_frame_base 0x40
#define DW_AT_macro_info 0x43
#define DW_AT_specification 0x47
#define DW_AT_type 0x49
#define DW_AT_virtuality 0x4c
#define DW_AT_vtable_elem_location 0x4d
// DWARF 2.1/3.0 extensions
#define DW_AT_entry_pc 0x52
#define DW_AT_ranges 0x55
// ARM Compiler extensions
#define DW_AT_proc_body 0x2000
#define DW_AT_save_offset 0x2001
#define DW_AT_user_2002 0x2002
// MIPS extensions
#define DW_AT_MIPS_linkage_name 0x2007
#define DW_FORM_addr 0x01
#define DW_FORM_data2 0x05
#define DW_FORM_data4 0x06
#define DW_FORM_string 0x08
#define DW_FORM_block 0x09
#define DW_FORM_block1 0x0a
#define DW_FORM_data1 0x0b
#define DW_FORM_flag 0x0c
#define DW_FORM_sdata 0x0d
#define DW_FORM_strp 0x0e
#define DW_FORM_udata 0x0f
#define DW_FORM_ref_addr 0x10
#define DW_FORM_ref4 0x13
#define DW_FORM_ref_udata 0x15
#define DW_FORM_indirect 0x16
#define DW_OP_addr 0x03
#define DW_OP_plus_uconst 0x23
#define DW_OP_reg0 0x50
#define DW_OP_reg1 0x51
#define DW_OP_reg2 0x52
#define DW_OP_reg3 0x53
#define DW_OP_reg4 0x54
#define DW_OP_reg5 0x55
#define DW_OP_reg6 0x56
#define DW_OP_reg7 0x57
#define DW_OP_reg8 0x58
#define DW_OP_reg9 0x59
#define DW_OP_reg10 0x5a
#define DW_OP_reg11 0x5b
#define DW_OP_reg12 0x5c
#define DW_OP_reg13 0x5d
#define DW_OP_reg14 0x5e
#define DW_OP_reg15 0x5f
#define DW_OP_fbreg 0x91
#define DW_LNS_extended_op 0x00
#define DW_LNS_copy 0x01
#define DW_LNS_advance_pc 0x02
#define DW_LNS_advance_line 0x03
#define DW_LNS_set_file 0x04
#define DW_LNS_set_column 0x05
#define DW_LNS_negate_stmt 0x06
#define DW_LNS_set_basic_block 0x07
#define DW_LNS_const_add_pc 0x08
#define DW_LNS_fixed_advance_pc 0x09
#define DW_LNE_end_sequence 0x01
#define DW_LNE_set_address 0x02
#define DW_LNE_define_file 0x03
#define DW_CFA_advance_loc 0x01
#define DW_CFA_offset 0x02
#define DW_CFA_restore 0x03
#define DW_CFA_set_loc 0x01
#define DW_CFA_advance_loc1 0x02
#define DW_CFA_advance_loc2 0x03
#define DW_CFA_advance_loc4 0x04
#define DW_CFA_offset_extended 0x05
#define DW_CFA_restore_extended 0x06
#define DW_CFA_undefined 0x07
#define DW_CFA_same_value 0x08
#define DW_CFA_register 0x09
#define DW_CFA_remember_state 0x0a
#define DW_CFA_restore_state 0x0b
#define DW_CFA_def_cfa 0x0c
#define DW_CFA_def_cfa_register 0x0d
#define DW_CFA_def_cfa_offset 0x0e
#define DW_CFA_nop 0x00
#define CASE_TYPE_TAG \
case DW_TAG_const_type: \
case DW_TAG_volatile_type: \
case DW_TAG_pointer_type: \
case DW_TAG_base_type: \
case DW_TAG_array_type: \
case DW_TAG_structure_type: \
case DW_TAG_union_type: \
case DW_TAG_typedef: \
case DW_TAG_subroutine_type: \
case DW_TAG_enumeration_type: \
case DW_TAG_enumerator: \
case DW_TAG_reference_type
struct ELFcie {
ELFcie* next;
uint32_t offset;
uint8_t* augmentation;
uint32_t codeAlign;
int32_t dataAlign;
int returnAddress;
uint8_t* data;
uint32_t dataLen;
};
struct ELFfde {
ELFcie* cie;
uint32_t address;
uint32_t end;
uint8_t* data;
uint32_t dataLen;
};
enum ELFRegMode {
REG_NOT_SET,
REG_OFFSET,
REG_REGISTER
};
struct ELFFrameStateRegister {
ELFRegMode mode;
int reg;
int32_t offset;
};
struct ELFFrameStateRegisters {
ELFFrameStateRegister regs[16];
ELFFrameStateRegisters* previous;
};
enum ELFCfaMode {
CFA_NOT_SET,
CFA_REG_OFFSET
};
struct ELFFrameState {
ELFFrameStateRegisters registers;
ELFCfaMode cfaMode;
int cfaRegister;
int32_t cfaOffset;
uint32_t pc;
int dataAlign;
int codeAlign;
int returnAddress;
};
extern bool cpuIsMultiBoot;
Symbol* elfSymbols = NULL;
char* elfSymbolsStrTab = NULL;
int elfSymbolsCount = 0;
ELFSectionHeader** elfSectionHeaders = NULL;
char* elfSectionHeadersStringTable = NULL;
int elfSectionHeadersCount = 0;
uint8_t* elfFileData = NULL;
CompileUnit* elfCompileUnits = NULL;
DebugInfo* elfDebugInfo = NULL;
char* elfDebugStrings = NULL;
ELFcie* elfCies = NULL;
ELFfde** elfFdes = NULL;
int elfFdeCount = 0;
CompileUnit* elfCurrentUnit = NULL;
uint32_t elfRead4Bytes(uint8_t*);
uint16_t elfRead2Bytes(uint8_t*);
CompileUnit* elfGetCompileUnit(uint32_t addr)
{
if (elfCompileUnits) {
CompileUnit* unit = elfCompileUnits;
while (unit) {
if (unit->lowPC) {
if (addr >= unit->lowPC && addr < unit->highPC)
return unit;
} else {
ARanges* r = unit->ranges;
if (r) {
int count = r->count;
for (int j = 0; j < count; j++) {
if (addr >= r->ranges[j].lowPC && addr < r->ranges[j].highPC)
return unit;
}
}
}
unit = unit->next;
}
}
return NULL;
}
const char* elfGetAddressSymbol(uint32_t addr)
{
static char buffer[256]; //defining globalscope here just feels so wrong
CompileUnit* unit = elfGetCompileUnit(addr);
// found unit, need to find function
if (unit) {
Function* func = unit->functions;
while (func) {
if (addr >= func->lowPC && addr < func->highPC) {
int offset = addr - func->lowPC;
const char* name = func->name;
if (!name)
name = "";
if (offset)
snprintf(buffer, 256, "%s+%d", name, offset);
else {
strncpy(buffer, name, 255); //strncpy does not allways append a '\0'
buffer[255] = '\0';
}
return buffer;
}
func = func->next;
}
}
if (elfSymbolsCount) {
for (int i = 0; i < elfSymbolsCount; i++) {
Symbol* s = &elfSymbols[i];
if ((addr >= s->value) && addr < (s->value + s->size)) {
int offset = addr - s->value;
const char* name = s->name;
if (name == NULL)
name = "";
if (offset)
snprintf(buffer, 256,"%s+%d", name, addr - s->value);
else {
strncpy(buffer, name, 255);
buffer[255] = '\0';
}
return buffer;
} else if (addr == s->value) {
if (s->name) {
strncpy(buffer, s->name, 255);
buffer[255] = '\0';
} else
strcpy(buffer, "");
return buffer;
}
}
}
return "";
}
bool elfFindLineInModule(uint32_t* addr, const char* name, int line)
{
CompileUnit* unit = elfCompileUnits;
while (unit) {
if (unit->lineInfoTable) {
int i;
int count = unit->lineInfoTable->fileCount;
char* found = NULL;
for (i = 0; i < count; i++) {
if (strcmp(name, unit->lineInfoTable->files[i]) == 0) {
found = unit->lineInfoTable->files[i];
break;
}
}
// found a matching filename... try to find line now
if (found) {
LineInfoItem* table = unit->lineInfoTable->lines;
count = unit->lineInfoTable->number;
for (i = 0; i < count; i++) {
if (table[i].file == found && table[i].line == line) {
*addr = table[i].address;
return true;
}
}
// we can only find a single match
return false;
}
}
unit = unit->next;
}
return false;
}
int elfFindLine(CompileUnit* unit, Function* /* func */, uint32_t addr, const char** f)
{
int currentLine = -1;
if (unit->hasLineInfo) {
int count = unit->lineInfoTable->number;
LineInfoItem* table = unit->lineInfoTable->lines;
int i;
for (i = 0; i < count; i++) {
if (addr <= table[i].address)
break;
}
if (i == count)
i--;
*f = table[i].file;
currentLine = table[i].line;
}
return currentLine;
}
bool elfFindLineInUnit(uint32_t* addr, CompileUnit* unit, int line)
{
if (unit->hasLineInfo) {
int count = unit->lineInfoTable->number;
LineInfoItem* table = unit->lineInfoTable->lines;
int i;
for (i = 0; i < count; i++) {
if (line == table[i].line) {
*addr = table[i].address;
return true;
}
}
}
return false;
}
bool elfGetCurrentFunction(uint32_t addr, Function** f, CompileUnit** u)
{
CompileUnit* unit = elfGetCompileUnit(addr);
// found unit, need to find function
if (unit) {
Function* func = unit->functions;
while (func) {
if (addr >= func->lowPC && addr < func->highPC) {
*f = func;
*u = unit;
return true;
}
func = func->next;
}
}
return false;
}
bool elfGetObject(const char* name, Function* f, CompileUnit* u, Object** o)
{
if (f && u) {
Object* v = f->variables;
while (v) {
if (strcmp(name, v->name) == 0) {
*o = v;
return true;
}
v = v->next;
}
v = f->parameters;
while (v) {
if (strcmp(name, v->name) == 0) {
*o = v;
return true;
}
v = v->next;
}
v = u->variables;
while (v) {
if (strcmp(name, v->name) == 0) {
*o = v;
return true;
}
v = v->next;
}
}
CompileUnit* c = elfCompileUnits;
while (c) {
if (c != u) {
Object* v = c->variables;
while (v) {
if (strcmp(name, v->name) == 0) {
*o = v;
return true;
}
v = v->next;
}
}
c = c->next;
}
return false;
}
const char* elfGetSymbol(int i, uint32_t* value, uint32_t* size, int* type)
{
if (i < elfSymbolsCount) {
Symbol* s = &elfSymbols[i];
*value = s->value;
*size = s->size;
*type = s->type;
return s->name;
}
return NULL;
}
bool elfGetSymbolAddress(const char* sym, uint32_t* addr, uint32_t* size, int* type)
{
if (elfSymbolsCount) {
for (int i = 0; i < elfSymbolsCount; i++) {
Symbol* s = &elfSymbols[i];
if (strcmp(sym, s->name) == 0) {
*addr = s->value;
*size = s->size;
*type = s->type;
return true;
}
}
}
return false;
}
ELFfde* elfGetFde(uint32_t address)
{
if (elfFdes) {
int i;
for (i = 0; i < elfFdeCount; i++) {
if (address >= elfFdes[i]->address && address < elfFdes[i]->end) {
return elfFdes[i];
}
}
}
return NULL;
}
void elfExecuteCFAInstructions(ELFFrameState* state, uint8_t* data, uint32_t len,
uint32_t pc)
{
uint8_t* end = data + len;
int bytes;
int reg;
ELFFrameStateRegisters* fs;
while (data < end && state->pc < pc) {
uint8_t op = *data++;
switch (op >> 6) {
case DW_CFA_advance_loc:
state->pc += (op & 0x3f) * state->codeAlign;
break;
case DW_CFA_offset:
reg = op & 0x3f;
state->registers.regs[reg].mode = REG_OFFSET;
state->registers.regs[reg].offset = state->dataAlign * (int32_t)elfReadLEB128(data, &bytes);
data += bytes;
break;
case DW_CFA_restore:
// we don't care much about the other possible settings,
// so just setting to unset is enough for now
state->registers.regs[op & 0x3f].mode = REG_NOT_SET;
break;
case 0:
switch (op & 0x3f) {
case DW_CFA_nop:
break;
case DW_CFA_advance_loc1:
state->pc += state->codeAlign * (*data++);
break;
case DW_CFA_advance_loc2:
state->pc += state->codeAlign * elfRead2Bytes(data);
data += 2;
break;
case DW_CFA_advance_loc4:
state->pc += state->codeAlign * elfRead4Bytes(data);
data += 4;
break;
case DW_CFA_offset_extended:
reg = elfReadLEB128(data, &bytes);
data += bytes;
state->registers.regs[reg].mode = REG_OFFSET;
state->registers.regs[reg].offset = state->dataAlign * (int32_t)elfReadLEB128(data, &bytes);
data += bytes;
break;
case DW_CFA_restore_extended:
case DW_CFA_undefined:
case DW_CFA_same_value:
reg = elfReadLEB128(data, &bytes);
data += bytes;
state->registers.regs[reg].mode = REG_NOT_SET;
break;
case DW_CFA_register:
reg = elfReadLEB128(data, &bytes);
data += bytes;
state->registers.regs[reg].mode = REG_REGISTER;
state->registers.regs[reg].reg = elfReadLEB128(data, &bytes);
data += bytes;
break;
case DW_CFA_remember_state:
fs = (ELFFrameStateRegisters*)calloc(1,
sizeof(ELFFrameStateRegisters));
memcpy(fs, &state->registers, sizeof(ELFFrameStateRegisters));
state->registers.previous = fs;
break;
case DW_CFA_restore_state:
if (state->registers.previous == NULL) {
printf("Error: previous frame state is NULL.\n");
return;
}
fs = state->registers.previous;
memcpy(&state->registers, fs, sizeof(ELFFrameStateRegisters));
free(fs);
break;
case DW_CFA_def_cfa:
state->cfaRegister = elfReadLEB128(data, &bytes);
data += bytes;
state->cfaOffset = (int32_t)elfReadLEB128(data, &bytes);
data += bytes;
state->cfaMode = CFA_REG_OFFSET;
break;
case DW_CFA_def_cfa_register:
state->cfaRegister = elfReadLEB128(data, &bytes);
data += bytes;
state->cfaMode = CFA_REG_OFFSET;
break;
case DW_CFA_def_cfa_offset:
state->cfaOffset = (int32_t)elfReadLEB128(data, &bytes);
data += bytes;
state->cfaMode = CFA_REG_OFFSET;
break;
default:
printf("Unknown CFA opcode %08x\n", op);
return;
}
break;
default:
printf("Unknown CFA opcode %08x\n", op);
return;
}
}
}
ELFFrameState* elfGetFrameState(ELFfde* fde, uint32_t address)
{
ELFFrameState* state = (ELFFrameState*)calloc(1, sizeof(ELFFrameState));
state->pc = fde->address;
state->dataAlign = fde->cie->dataAlign;
state->codeAlign = fde->cie->codeAlign;
state->returnAddress = fde->cie->returnAddress;
elfExecuteCFAInstructions(state,
fde->cie->data,
fde->cie->dataLen,
0xffffffff);
elfExecuteCFAInstructions(state,
fde->data,
fde->dataLen,
address);
return state;
}
void elfPrintCallChain(uint32_t address)
{
int count = 1;
reg_pair regs[15];
reg_pair newRegs[15];
memcpy(®s[0], ®[0], sizeof(reg_pair) * 15);
while (count < 20) {
const char* addr = elfGetAddressSymbol(address);
if (*addr == 0)
addr = "???";
printf("%08x %s\n", address, addr);
ELFfde* fde = elfGetFde(address);
if (fde == NULL) {
break;
}
ELFFrameState* state = elfGetFrameState(fde, address);
if (!state) {
break;
}
if (state->cfaMode == CFA_REG_OFFSET) {
memcpy(&newRegs[0], ®s[0], sizeof(reg_pair) * 15);
uint32_t addr = 0;
for (int i = 0; i < 15; i++) {
ELFFrameStateRegister* r = &state->registers.regs[i];
switch (r->mode) {
case REG_NOT_SET:
newRegs[i].I = regs[i].I;
break;
case REG_OFFSET:
newRegs[i].I = elfReadMemory(regs[state->cfaRegister].I + state->cfaOffset + r->offset);
break;
case REG_REGISTER:
newRegs[i].I = regs[r->reg].I;
break;
default:
printf("Unknown register mode: %d\n", r->mode);
break;
}
}
memcpy(regs, newRegs, sizeof(reg_pair) * 15);
addr = newRegs[14].I;
addr &= 0xfffffffe;
address = addr;
count++;
} else {
printf("CFA not set\n");
break;
}
if (state->registers.previous) {
ELFFrameStateRegisters* prev = state->registers.previous;
while (prev) {
ELFFrameStateRegisters* p = prev->previous;
free(prev);
prev = p;
}
}
free(state);
}
}
uint32_t elfDecodeLocation(Function* f, ELFBlock* o, LocationType* type, uint32_t base)
{
uint32_t framebase = 0;
if (f && f->frameBase) {
ELFBlock* b = f->frameBase;
switch (*b->data) {
case DW_OP_reg0:
case DW_OP_reg1:
case DW_OP_reg2:
case DW_OP_reg3:
case DW_OP_reg4:
case DW_OP_reg5:
case DW_OP_reg6:
case DW_OP_reg7:
case DW_OP_reg8:
case DW_OP_reg9:
case DW_OP_reg10:
case DW_OP_reg11:
case DW_OP_reg12:
case DW_OP_reg13:
case DW_OP_reg14:
case DW_OP_reg15:
framebase = reg[*b->data - 0x50].I;
break;
default:
fprintf(stderr, "Unknown frameBase %02x\n", *b->data);
break;
}
}
ELFBlock* loc = o;
uint32_t location = 0;
int bytes = 0;
if (loc) {
switch (*loc->data) {
case DW_OP_addr:
location = elfRead4Bytes(loc->data + 1);
*type = LOCATION_memory;
break;
case DW_OP_plus_uconst:
location = base + elfReadLEB128(loc->data + 1, &bytes);
*type = LOCATION_memory;
break;
case DW_OP_reg0:
case DW_OP_reg1:
case DW_OP_reg2:
case DW_OP_reg3:
case DW_OP_reg4:
case DW_OP_reg5:
case DW_OP_reg6:
case DW_OP_reg7:
case DW_OP_reg8:
case DW_OP_reg9:
case DW_OP_reg10:
case DW_OP_reg11:
case DW_OP_reg12:
case DW_OP_reg13:
case DW_OP_reg14:
case DW_OP_reg15:
location = *loc->data - 0x50;
*type = LOCATION_register;
break;
case DW_OP_fbreg: {
int bytes;
int32_t off = elfReadSignedLEB128(loc->data + 1, &bytes);
location = framebase + off;
*type = LOCATION_memory;
} break;
default:
fprintf(stderr, "Unknown location %02x\n", *loc->data);
break;
}
}
return location;
}
uint32_t elfDecodeLocation(Function* f, ELFBlock* o, LocationType* type)
{
return elfDecodeLocation(f, o, type, 0);
}
// reading function
uint32_t elfRead4Bytes(uint8_t* data)
{
uint32_t value = *data++;
value |= (*data++ << 8);
value |= (*data++ << 16);
value |= (*data << 24);
return value;
}
uint16_t elfRead2Bytes(uint8_t* data)
{
uint16_t value = *data++;
value |= (*data << 8);
return value;
}
char* elfReadString(uint8_t* data, int* bytesRead)
{
if (*data == 0) {
*bytesRead = 1;
return NULL;
}
*bytesRead = (int)strlen((char*)data) + 1;
return (char*)data;
}
int32_t elfReadSignedLEB128(uint8_t* data, int* bytesRead)
{
int32_t result = 0;
int shift = 0;
int count = 0;
uint8_t byte;
do {
byte = *data++;
count++;
result |= (byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
if ((shift < 32) && (byte & 0x40))
result |= -(1 << shift);
*bytesRead = count;
return result;
}
uint32_t elfReadLEB128(uint8_t* data, int* bytesRead)
{
uint32_t result = 0;
int shift = 0;
int count = 0;
uint8_t byte;
do {
byte = *data++;
count++;
result |= (byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
*bytesRead = count;
return result;
}
uint8_t* elfReadSection(uint8_t* data, ELFSectionHeader* sh)
{
return data + READ32LE(&sh->offset);
}
ELFSectionHeader* elfGetSectionByName(const char* name)
{
if (elfSectionHeadersStringTable == NULL)
return NULL;
for (int i = 0; i < elfSectionHeadersCount; i++) {
if (strcmp(name,
&elfSectionHeadersStringTable[READ32LE(&elfSectionHeaders[i]->name)])
== 0) {
return elfSectionHeaders[i];
}
}
return NULL;
}
ELFSectionHeader* elfGetSectionByNumber(int number)
{
if (number < elfSectionHeadersCount) {
return elfSectionHeaders[number];
}
return NULL;
}
CompileUnit* elfGetCompileUnitForData(uint8_t* data)
{
uint8_t* end = elfCurrentUnit->top + 4 + elfCurrentUnit->length;
if (data >= elfCurrentUnit->top && data < end)
return elfCurrentUnit;
CompileUnit* unit = elfCompileUnits;
while (unit) {
end = unit->top + 4 + unit->length;
if (data >= unit->top && data < end)
return unit;
unit = unit->next;
}
printf("Error: cannot find reference to compile unit at offset %08x\n",
(int)(data - elfDebugInfo->infodata));
exit(-1);
}
uint8_t* elfReadAttribute(uint8_t* data, ELFAttr* attr)
{
int bytes;
int form = attr->form;
start:
switch (form) {
case DW_FORM_addr:
attr->value = elfRead4Bytes(data);
data += 4;
break;
case DW_FORM_data2:
attr->value = elfRead2Bytes(data);
data += 2;
break;
case DW_FORM_data4:
attr->value = elfRead4Bytes(data);
data += 4;
break;
case DW_FORM_string:
attr->string = (char*)data;
data += strlen(attr->string) + 1;
break;
case DW_FORM_strp:
attr->string = elfDebugStrings + elfRead4Bytes(data);
data += 4;
break;
case DW_FORM_block:
attr->block = (ELFBlock*)malloc(sizeof(ELFBlock));
attr->block->length = elfReadLEB128(data, &bytes);
data += bytes;
attr->block->data = data;
data += attr->block->length;
break;
case DW_FORM_block1:
attr->block = (ELFBlock*)malloc(sizeof(ELFBlock));
attr->block->length = *data++;
attr->block->data = data;
data += attr->block->length;
break;
case DW_FORM_data1:
attr->value = *data++;
break;
case DW_FORM_flag:
attr->flag = (*data++) ? true : false;
break;
case DW_FORM_sdata:
attr->value = elfReadSignedLEB128(data, &bytes);
data += bytes;
break;
case DW_FORM_udata:
attr->value = elfReadLEB128(data, &bytes);
data += bytes;
break;
case DW_FORM_ref_addr:
attr->value = (uint32_t)((elfDebugInfo->infodata + elfRead4Bytes(data)) - elfGetCompileUnitForData(data)->top);
data += 4;
break;
case DW_FORM_ref4:
attr->value = elfRead4Bytes(data);
data += 4;
break;
case DW_FORM_ref_udata:
attr->value = (uint32_t)((elfDebugInfo->infodata + (elfGetCompileUnitForData(data)->top - elfDebugInfo->infodata) + elfReadLEB128(data, &bytes)) - elfCurrentUnit->top);
data += bytes;
break;
case DW_FORM_indirect:
form = elfReadLEB128(data, &bytes);
data += bytes;
goto start;
default:
fprintf(stderr, "Unsupported FORM %02x\n", form);
exit(-1);
}
return data;
}
ELFAbbrev* elfGetAbbrev(ELFAbbrev** table, uint32_t number)
{
int hash = number % 121;
ELFAbbrev* abbrev = table[hash];
while (abbrev) {
if (abbrev->number == number)
return abbrev;
abbrev = abbrev->next;
}
return NULL;
}
ELFAbbrev** elfReadAbbrevs(uint8_t* data, uint32_t offset)
{
data += offset;
ELFAbbrev** abbrevs = (ELFAbbrev**)calloc(sizeof(ELFAbbrev*) * 121, 1);
int bytes = 0;
uint32_t number = elfReadLEB128(data, &bytes);
data += bytes;
while (number) {
ELFAbbrev* abbrev = (ELFAbbrev*)calloc(sizeof(ELFAbbrev), 1);
// read tag information
abbrev->number = number;
abbrev->tag = elfReadLEB128(data, &bytes);
data += bytes;
abbrev->hasChildren = *data++ ? true : false;
// read attributes
int name = elfReadLEB128(data, &bytes);
data += bytes;
int form = elfReadLEB128(data, &bytes);
data += bytes;