-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1542 lines (1322 loc) · 55.9 KB
/
main.c
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 <string.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <endian.h>
#include <getopt.h>
#include "orc.h"
#define USAGE "%s [ -S section_headers_csv ] [ -s symbols_csv ] elf-file\n"
#define SHT_MIPS_ABIFLAGS 0x7000002a /* This is not included in elf.h */
/* Nr,Name,Type,Addr,Offset,Size,EntSize,Flags,Link,Info,Alignment */
#define CSV_FORMAT_STR "%m[^,],%u,0x%08x,0x%08x,%u,%u,%u,%u,%u,%u\n"
#define FUNC_CSV_FORMAT_STR "\"%m[^\"]\",\"%x\",\"%i\"\n"
#define NUM_DYNSYM_SECTION_LABELS 8
#ifndef STO_MIPS16
#define STO_MIPS16 0xf0
#define ELF_ST_IS_MIPS16(other) (((other) & STO_MIPS16) == STO_MIPS16)
#endif
/*
https://github.com/bminor/binutils-gdb/blob/master/binutils/readelf.c#L18968
https://github.com/m-labs/uclibc-lm32/blob/master/ldso/ldso/mips/elfinterp.c#L35
*/
#define GP_DISP 0x7ff0
/*
Program Headers
.MIPS.stubs
dynamic section entries
DT_MIPS_RLD_MAP .rld_map
For nonPIC (with PLT):
musl-gcc -fno-PIC -mips16 hello_world.c -mno-abicalls -o hello_world16e_nopic
For nonPIC (with PLT) and partial RELRO (both .got and .got.plt are writable):
musl-gcc -z relro -fno-PIC -mips16 hello_world.c -mno-abicalls -o hello_world16e_nopic
For nonPIC (with PLT) and "full" RELRO (only .got is writable):
musl-gcc -z relro -z now -fno-PIC -mips16 hello_world.c -mno-abicalls -o hello_world16e_nopic
*/
struct csv_section_header {
char *name;
Elf32_Shdr header;
struct csv_section_header *prev;
struct csv_section_header *next;
const char *info;
const char *link;
};
struct dynsym_section_label {
const char *name;
Elf32_Sym symbol;
struct dynsym_section_label *prev;
struct dynsym_section_label *next;
};
struct section_info {
Elf32_Shdr *headers;
Elf32_Half num_headers;
uint8_t *shstrtab;
size_t shstrtab_len;
struct csv_section_header *csv_headers;
uint8_t *strtab;
size_t strtab_len;
Elf32_Sym *symtab;
Elf32_Word num_symbols;
};
enum ORCError add_section_header(struct section_info *s_info, const char *name, Elf32_Shdr *sh, const char *link, const char *info);
enum ORCError parse_dynamic_segment(FILE *handle, Elf32_Phdr *dyn_seg, Elf32_Phdr *loadable_segs, Elf32_Half num_loadable_segs, struct section_info *s_info, Elf32_Ehdr *elf_hdr);
enum ORCError parse_mips_nonpic(
FILE *handle,
Elf32_Off dyn_seg_offset,
Elf32_Word dyn_seg_size,
Elf32_Phdr *loadable_segs,
Elf32_Half num_loadable_segs,
struct section_info *s_info
);
enum ORCError parse_dynamic_relocation_section(
FILE *handle,
Elf32_Off dyn_seg_offset,
Elf32_Word dyn_seg_size,
Elf32_Phdr *loadable_segs,
Elf32_Half num_loadable_segs,
struct section_info *s_info
);
enum ORCError zero_elfhdr_sh(FILE *handle, Elf32_Ehdr *elf_hdr);
enum ORCError parse_section_header_csv(const char *csv_filepath, struct section_info *s_info);
enum ORCError parse_gnu_version_requirements_section(
FILE *handle,
Elf32_Off dyn_seg_offset,
Elf32_Word dyn_seg_size,
Elf32_Phdr *loadable_segs,
Elf32_Half num_loadable_segs,
struct section_info *s_info
);
enum ORCError build_section_headers(struct section_info *s_info);
enum ORCError parse_sh_from_dynsym(FILE *handle, Elf32_Phdr *loadable_segs, Elf32_Half num_loadable_segs, struct section_info *s_info);
enum ORCError parse_symtab_from_ghidra_csv(const char *sym_csv_filepath, struct section_info *s_info);
static struct dynsym_section_label dynsym_section_labels[NUM_DYNSYM_SECTION_LABELS] = {
{
"_init",
{
0, STT_FUNC, 0, 0, 0, 0
},
NULL,
NULL
},
{
"_ftext",
{
0, STT_NOTYPE, 0, 0, 0, 0
},
NULL,
NULL
},
{
"_fini",
{
0, STT_FUNC, 0, 0, 0, 0
},
NULL,
NULL
},
{
"_fdata",
{
0, STT_NOTYPE, 0, 0, 0, 0
},
NULL,
NULL
},
{
"_edata",
{
0, STT_NOTYPE, 0, 0, 0, 0
},
NULL,
NULL
},
{
"__bss_start",
{
0, STT_NOTYPE, 0, 0, 0, 0
},
NULL,
NULL
},
{
"_fbss",
{
0, STT_NOTYPE, 0, 0, 0, 0
},
NULL,
NULL
},
{
"_end",
{
0, STT_NOTYPE, 0, 0, 0, 0
},
NULL,
NULL
}
};
int main(int argc, char *argv[])
{
FILE *handle;
Elf32_Ehdr elf_header;
Elf32_Phdr *loadable_segments = NULL, *seg = NULL;
Elf32_Shdr null_section = { 0 }, interp = { 0 }, mips_abiflags = { 0 }, reginfo = { 0 }, sh = { 0 };
Elf32_Half num_loadable_segments, phdr_count;
long offset;
int ret, opt;
enum ORCError err;
struct section_info s_info = { 0 };
char *csv_file = NULL, *ghidra_symbols_csv = NULL, *ghidra_functions_csv = NULL;
struct csv_section_header *hdr_ptr;
opterr = 0;
while ((opt = getopt(argc, argv, "S:s:")) != -1)
{
switch (opt)
{
case 'S':
csv_file = optarg;
break;
case 's':
ghidra_symbols_csv = optarg;
break;
case '?':
default:
fprintf(stderr, USAGE, argv[0]);
return 1;
}
}
if (argc - optind != 1)
{
fprintf(stderr, USAGE, argv[0]);
return 1;
}
if (!(handle = fopen(argv[optind], "r+")))
{
fprintf(stderr, "Failed to open %s: %s\n", argv[optind], strerror(errno));
return 1;
}
if (fread(&elf_header, sizeof(Elf32_Ehdr), 1, handle) != 1)
{
if (ferror(handle))
fprintf(stderr, "Failed to read ELF header from %s\n", argv[optind]);
else
fprintf(stderr, "No ELF header found in %s\n", argv[optind]);
fclose(handle);
return 1;
}
if (!IS_SUPPORTED_ARCH((&elf_header))) {
fprintf(stderr, "Currently only 32 bit big-endian MIPS binaries are supported\n");
goto err_exit;
}
if (elf_header.e_shoff || elf_header.e_shnum) {
fprintf(stderr, "%s already contains %hu section headers at offset 0x%x\n", argv[optind], be16toh(elf_header.e_shnum), be32toh(elf_header.e_shoff));
goto err_exit;
}
if (add_section_header(&s_info, "", &null_section, NULL, NULL) != ORC_SUCCESS)
goto err_exit;
/* parse program header info */
Elf32_Half ph_num = be16toh(elf_header.e_phnum);
Elf32_Off ph_off = be32toh(elf_header.e_phoff);
fprintf(stderr, "Found %hu program headers at offset %u\n", ph_num, ph_off);
switch (find_program_headers(handle, ph_off, ph_num, PT_INTERP, &seg, &phdr_count)) {
case ORC_SUCCESS:
interp.sh_addr = seg->p_vaddr;
interp.sh_addralign = seg->p_align;
if (seg->p_flags & htobe32(PF_R))
interp.sh_flags |= htobe32(SHF_ALLOC);
if (seg->p_flags & htobe32(PF_W))
interp.sh_flags |= htobe32(SHF_WRITE);
if (seg->p_flags & htobe32(PF_X))
interp.sh_flags |= htobe32(SHF_EXECINSTR);
interp.sh_offset = seg->p_offset;
interp.sh_size = seg->p_filesz;
interp.sh_type = htobe32(SHT_PROGBITS);
if (add_section_header(&s_info, ".interp", &interp, NULL, NULL) != ORC_SUCCESS)
goto err_exit;
case ORC_PHDR_NOT_FOUND:
break;
default:
goto err_exit;
}
switch (find_program_headers(handle, ph_off, ph_num, PT_MIPS_ABIFLAGS, &seg, &phdr_count)) {
case ORC_SUCCESS:
mips_abiflags.sh_addr = seg->p_vaddr;
mips_abiflags.sh_addralign = seg->p_align;
if (seg->p_flags & htobe32(PF_R))
mips_abiflags.sh_flags |= htobe32(SHF_ALLOC);
if (seg->p_flags & htobe32(PF_W))
mips_abiflags.sh_flags |= htobe32(SHF_WRITE);
if (seg->p_flags & htobe32(PF_X))
mips_abiflags.sh_flags |= htobe32(SHF_EXECINSTR);
mips_abiflags.sh_offset = seg->p_offset;
mips_abiflags.sh_size = seg->p_filesz;
mips_abiflags.sh_type = htobe32(SHT_MIPS_ABIFLAGS);
if (add_section_header(&s_info, ".MIPS.abiflags", &mips_abiflags, NULL, NULL) != ORC_SUCCESS)
goto err_exit;
case ORC_PHDR_NOT_FOUND:
break;
default:
goto err_exit;
}
switch (find_program_headers(handle, ph_off, ph_num, PT_MIPS_REGINFO, &seg, &phdr_count)) {
case ORC_SUCCESS:
reginfo.sh_addr = seg->p_vaddr;
reginfo.sh_addralign = seg->p_align;
if (seg->p_flags & htobe32(PF_R))
reginfo.sh_flags |= htobe32(SHF_ALLOC);
if (seg->p_flags & htobe32(PF_W))
reginfo.sh_flags |= htobe32(SHF_WRITE);
if (seg->p_flags & htobe32(PF_X))
reginfo.sh_flags |= htobe32(SHF_EXECINSTR);
reginfo.sh_offset = seg->p_offset;
reginfo.sh_size = seg->p_filesz;
reginfo.sh_type = htobe32(SHT_MIPS_REGINFO);
if (add_section_header(&s_info, ".reginfo", ®info, NULL, NULL) != ORC_SUCCESS)
goto err_exit;
case ORC_PHDR_NOT_FOUND:
break;
default:
goto err_exit;
}
switch (find_program_headers(handle, ph_off, ph_num, PT_GNU_EH_FRAME, &seg, &phdr_count)) {
case ORC_SUCCESS:
sh.sh_addr = seg->p_vaddr;
sh.sh_addralign = seg->p_align;
if (seg->p_flags & htobe32(PF_R))
sh.sh_flags |= htobe32(SHF_ALLOC);
if (seg->p_flags & htobe32(PF_W))
sh.sh_flags |= htobe32(SHF_WRITE);
if (seg->p_flags & htobe32(PF_X))
sh.sh_flags |= htobe32(SHF_EXECINSTR);
sh.sh_offset = seg->p_offset;
sh.sh_size = seg->p_filesz;
sh.sh_type = htobe32(SHT_PROGBITS);
if (add_section_header(&s_info, ".eh_frame_hdr", &sh, NULL, NULL) != ORC_SUCCESS)
goto err_exit;
case ORC_PHDR_NOT_FOUND:
break;
default:
goto err_exit;
}
switch (find_program_headers(handle, ph_off, ph_num, PT_LOAD, &loadable_segments, &num_loadable_segments)) {
case ORC_SUCCESS:
case ORC_PHDR_NOT_FOUND:
break;
default:
goto err_exit;
}
switch (find_program_headers(handle, ph_off, ph_num, PT_DYNAMIC, &seg, &phdr_count)) {
case ORC_SUCCESS:
if ((err = parse_dynamic_segment(handle, seg, loadable_segments, num_loadable_segments, &s_info, &elf_header)) == ORC_CRITICIAL)
goto err_exit;
case ORC_PHDR_NOT_FOUND:
break;
default:
goto err_exit;
}
if (csv_file)
if (parse_section_header_csv(csv_file, &s_info) != ORC_SUCCESS)
goto err_exit;
if ((err = parse_sh_from_dynsym(handle, loadable_segments, num_loadable_segments, &s_info)) != ORC_SUCCESS && err != ORC_SYM_NOT_FOUND)
goto err_exit;
for (hdr_ptr = s_info.csv_headers; hdr_ptr->next; hdr_ptr = hdr_ptr->next) {
fprintf(stderr, "%s: 0x%x : 0x%x\n", hdr_ptr->name, be32toh(hdr_ptr->header.sh_addr), be32toh(hdr_ptr->header.sh_size));
}
fprintf(stderr, "%s: 0x%x : 0x%x\n", hdr_ptr->name, be32toh(hdr_ptr->header.sh_addr), be32toh(hdr_ptr->header.sh_size));
if (fseek(handle, 0L, SEEK_END) == -1 || (offset = ftell(handle)) == -1)
{
fprintf(stderr, "Failed to obtain file size of %s: %s\n", argv[optind], strerror(errno));
fclose(handle);
return 1;
}
fprintf(stderr, "File %s size: 0x%lx bytes\n", argv[optind], offset);
Elf32_Word section_end = be32toh(hdr_ptr->header.sh_type) == SHT_NOBITS ? be32toh(hdr_ptr->header.sh_offset): be32toh(hdr_ptr->header.sh_offset) + be32toh(hdr_ptr->header.sh_size);
if (section_end > offset) {
if (fseek(handle, section_end-offset, SEEK_CUR) == -1) {
fprintf(stderr, "Failed to seek to section header end offset at 0x%x in %s: %s\n", section_end, argv[optind], strerror(errno));
goto err_exit;
}
offset = section_end;
}
if (ghidra_symbols_csv) {
if ((err = parse_symtab_from_ghidra_csv(ghidra_symbols_csv, &s_info)) != ORC_SUCCESS)
goto err_exit;
if (offset % 4)
{
offset += (4 - (offset % 4));
if (fseek(handle, offset, SEEK_SET) == -1)
{
fprintf(stderr, "Failed to seek to .symtab offset at %li in %s: %s\n", offset, argv[optind], strerror(errno));
goto err_exit;
}
}
if (fwrite(s_info.symtab, sizeof(Elf32_Sym), s_info.num_symbols, handle) != s_info.num_symbols) {
fprintf(stderr, "Failed to write %hu symbols to %s at offset 0x%lx\n", s_info.num_symbols, argv[optind], offset);
goto err_exit;
}
sh.sh_addr = sh.sh_flags = 0;
sh.sh_addralign = htobe32(4);
sh.sh_entsize = htobe32(sizeof(Elf32_Sym));
/*
One greater than the symbol table index of the last local symbol.
https://docs.oracle.com/cd/E19455-01/806-3773/6jct9o0bs/index.html#elf-15226
since we are only parsing Global symbols from Ghidra, this will always be 1
*/
sh.sh_info = htobe32(1);
sh.sh_offset = htobe32(offset);
sh.sh_size = htobe32(s_info.num_symbols * sizeof(Elf32_Sym));
sh.sh_type = htobe32(SHT_SYMTAB);
if (add_section_header(&s_info, ".symtab", &sh, ".strtab", NULL) != ORC_SUCCESS)
goto err_exit;
offset += be32toh(sh.sh_size);
if (fwrite(s_info.strtab, s_info.strtab_len, 1, handle) != 1)
{
fprintf(stderr, "Failed to write %lu byte .strtab to %s at offset 0x%lx\n", s_info.shstrtab_len, argv[optind], offset);
goto err_exit;
}
sh.sh_addr = sh.sh_flags = sh.sh_info = sh.sh_link = sh.sh_entsize = 0;
sh.sh_addralign = htobe32(1);
sh.sh_offset = htobe32(offset);
sh.sh_size = htobe32(s_info.strtab_len);
sh.sh_type = htobe32(SHT_STRTAB);
if (add_section_header(&s_info, ".strtab", &sh, NULL, NULL) != ORC_SUCCESS)
goto err_exit;
offset += be32toh(sh.sh_size);
}
if (offset % 32)
{
offset += (32 - (offset % 32));
if (fseek(handle, offset, SEEK_SET) == -1)
{
fprintf(stderr, "Failed to seek to .shstrtab offset at %li in %s: %s\n", offset, argv[optind], strerror(errno));
goto err_exit;
}
}
fprintf(stderr, ".shstrtab offset: 0x%lx\n", offset);
fprintf(stderr, "End of last section: 0x%x\n", section_end);
/*
.shstrtab
*/
Elf32_Shdr shstrtab_header = {0};
shstrtab_header.sh_name = htobe32(s_info.num_headers - 1);
shstrtab_header.sh_type = htobe32(SHT_STRTAB);
shstrtab_header.sh_offset = htobe32(offset);
shstrtab_header.sh_size = htobe32(s_info.shstrtab_len + strlen(".shstrtab") + 1); /* plus terminating \0 */
shstrtab_header.sh_addralign = htobe32(1);
if (add_section_header(&s_info, ".shstrtab", &shstrtab_header, NULL, NULL) != ORC_SUCCESS)
goto err_exit;
if (build_section_headers(&s_info) != ORC_SUCCESS)
goto err_exit;
if (fwrite(s_info.shstrtab, s_info.shstrtab_len, 1, handle) != 1)
{
fprintf(stderr, "Failed to write %lu byte .shstrtab to %s at offset 0x%lx\n", s_info.shstrtab_len, argv[optind], offset);
fclose(handle);
return 1;
}
fprintf(stderr, "Wrote %lu byte .shstrtab to %s at offset 0x%lx\n", s_info.shstrtab_len, argv[optind], offset);
offset += s_info.shstrtab_len;
if (offset % 4)
{
offset += 4 - (offset % 4);
if (fseek(handle, offset, SEEK_SET) == -1)
{
fprintf(
stderr,
"Failed to seek to section header offset at 0x%lx in %s: %s\n",
offset,
argv[optind],
strerror(errno)
);
goto err_exit;
}
}
fprintf(stderr, "section header offset: 0x%lx\n", offset);
fprintf(stderr, "%li\n", ftell(handle));
if (fwrite(s_info.headers, sizeof(Elf32_Shdr), s_info.num_headers, handle) != s_info.num_headers) {
fprintf(stderr, "Failed to write %hu section headers to %s\n", s_info.num_headers, argv[optind]);
fclose(handle);
return 1;
}
elf_header.e_shentsize = htobe16(sizeof(Elf32_Shdr));
elf_header.e_shnum = htobe16(s_info.num_headers);
elf_header.e_shoff = htobe32(offset);
elf_header.e_shstrndx = htobe16(s_info.num_headers - 1);
if (fseek(handle, 0, SEEK_SET) == -1) {
fprintf(stderr, "Failed to seek to beginning of %s: %s\n", argv[optind], strerror(errno));
fclose(handle);
return 1;
}
if (fwrite(&elf_header, sizeof(Elf32_Ehdr), 1, handle) != 1) {
fprintf(stderr, "Failed to write updated ELF header to %s\n", argv[optind]);
fclose(handle);
return 1;
}
ret = 0;
goto cleanup;
err_exit:
ret = 1;
cleanup:
free(seg);
free(loadable_segments);
free(s_info.headers);
free(s_info.shstrtab);
fclose(handle);
return ret;
}
enum ORCError add_shstrtab_entry(struct section_info *s_info, const char *name, Elf32_Shdr *sh) {
size_t name_len;
name_len = strlen(name) + 1; /* plus terminating \0 */
if (!(s_info->shstrtab = reallocarray(s_info->shstrtab, s_info->shstrtab_len + name_len, sizeof(uint8_t)))) {
fprintf(stderr, "Failed to allocate %lu bytes of additional space to add %s to .shstrtab: %s\n", name_len, name, strerror(errno));
return ORC_CRITICIAL;
}
strcpy(s_info->shstrtab + s_info->shstrtab_len, name);
sh->sh_name = htobe32(s_info->shstrtab_len);
s_info->shstrtab_len += name_len;
return ORC_SUCCESS;
}
enum ORCError add_section_header(struct section_info *s_info, const char *name, Elf32_Shdr *sh, const char *link, const char *info) {
enum ORCError err;
struct csv_section_header *node, *temp;
if ((err = add_shstrtab_entry(s_info, name, sh)) != ORC_SUCCESS)
return err;
if (!(node = malloc(sizeof(struct csv_section_header)))) {
fprintf(stderr, "Failed to allocate section header node for %s: %s\n", name, strerror(errno));
return ORC_CRITICIAL;
}
memcpy(&node->header, sh, sizeof(Elf32_Shdr));
node->name = strdup(name);
node->link = link;
node->info = info;
node->prev = NULL;
node->next = s_info->csv_headers;
if (s_info->csv_headers)
s_info->csv_headers->prev = node;
while (node->next && be32toh(node->header.sh_offset) >= be32toh(node->next->header.sh_offset))
{
if (node->prev != NULL)
node->prev->next = node->next;
temp = node->prev;
node->prev = node->next;
node->next->prev = temp;
temp = node->next->next;
node->next->next = node;
node->next = temp;
if (node->next)
node->next->prev = node;
}
while (node->prev)
node = node->prev;
s_info->csv_headers = node;
return ORC_SUCCESS;
}
int find_referenced_section(struct section_info *s_info, const char *name) {
int idx = 0;
for (struct csv_section_header *node = s_info->csv_headers; node != NULL; node = node->next) {
if (!strcmp(name, node->name))
return idx;
idx++;
}
return -1;
}
enum ORCError build_section_headers(struct section_info *s_info) {
Elf32_Shdr *ptr;
int idx;
for (struct csv_section_header *node = s_info->csv_headers; node != NULL; node = node->next) {
if (node->info) {
if ((idx = find_referenced_section(s_info, node->info)) == -1)
fprintf(stderr, "Failed to find info section %s for section %s\n", node->info, node->name);
else
node->header.sh_info = htobe32(idx);
}
if (node->link) {
if ((idx = find_referenced_section(s_info, node->link)) == -1)
fprintf(stderr, "Failed to find link section %s for section %s\n", node->info, node->name);
else
node->header.sh_link = htobe32(idx);
}
s_info->num_headers++;
}
if (!(ptr = s_info->headers = calloc(s_info->num_headers, sizeof(Elf32_Shdr)))) {
fprintf(stderr, "Failed to allocate space for section headers: %s\n", strerror(errno));
return ORC_CRITICIAL;
}
for (struct csv_section_header *node = s_info->csv_headers; node != NULL; node = node->next)
memcpy(ptr++, &node->header, sizeof(Elf32_Shdr));
return ORC_SUCCESS;
}
enum ORCError parse_dynamic_segment(FILE *handle, Elf32_Phdr *dyn_seg, Elf32_Phdr *loadable_segs, Elf32_Half num_loadable_segs, struct section_info *s_info, Elf32_Ehdr *elf_hdr) {
/*
TODO: and subroutines and better error handling to account
for various architectures and dynamic tag combinations
*/
/*
TODO: fill in missing sht_addralign
*/
enum ORCError err;
Elf32_Shdr dynamic = { 0 }, dynstr = { 0 }, dynsym = { 0 }, got = { 0 }, rld_map = { 0 }, mips_stubs = { 0 }, hash = { 0 }, gnu_version = { 0 };
Elf32_Dyn dynamic_tag;
Elf32_Addr base_addr = 0, got_entry;
Elf32_Off dyn_seg_offset = be32toh(dyn_seg->p_offset), got_off, dynsym_off;
Elf32_Word dyn_seg_size = be32toh(dyn_seg->p_filesz), syment, symtabno, mips_local_gotno, mips_gotsym, mips_external_gotno, mips_stub_count;
Elf32_Sym sym;
/*
TODO
only for MIPS
x64 binaries don't have a base address
*/
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_MIPS_BASE_ADDRESS, &dynamic_tag))) {
case ORC_SUCCESS:
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find MIPS_BASE_ADDRESS dynamic tag\n");
default:
return err;
}
base_addr = be32toh(dynamic_tag.d_un.d_ptr);
fprintf(stderr, "Found MIPS base address at 0x%x\n", base_addr);
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_STRTAB, &dynamic_tag))) {
case ORC_SUCCESS:
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_STRTAB dynamic tag\n");
default:
return err;
}
dynstr.sh_addr = dynamic_tag.d_un.d_ptr;
fprintf(stderr, "Found DT_STRTAB at 0x%x\n", be32toh(dynstr.sh_addr));
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_STRSZ, &dynamic_tag))) {
case ORC_SUCCESS:
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_STRSZ dynamic tag\n");
default:
return err;
}
dynstr.sh_size = dynamic_tag.d_un.d_val;
fprintf(stderr, "Found DT_STRSZ at %u\n", be32toh(dynstr.sh_size));
if ((err = calculate_file_offset(loadable_segs, num_loadable_segs, be32toh(dynstr.sh_addr), &dynstr.sh_offset)) != ORC_SUCCESS)
return err;
dynstr.sh_addralign = htobe32(1);
dynstr.sh_type = htobe32(SHT_STRTAB);
dynstr.sh_flags = htobe32(SHF_ALLOC);
if ((err = add_section_header(s_info, ".dynstr", &dynstr, NULL, NULL)) != ORC_SUCCESS)
return err;
dynamic.sh_addr = dyn_seg->p_vaddr;
dynamic.sh_addralign = dyn_seg->p_align;
dynamic.sh_entsize = htobe32(sizeof(Elf32_Dyn));
if (dyn_seg->p_flags & htobe32(PF_R))
dynamic.sh_flags |= htobe32(SHF_ALLOC);
if (dyn_seg->p_flags & htobe32(PF_W))
dynamic.sh_flags |= htobe32(SHF_WRITE);
if (dyn_seg->p_flags & htobe32(PF_X))
dynamic.sh_flags |= htobe32(SHF_EXECINSTR);
dynamic.sh_offset = dyn_seg->p_offset;
dynamic.sh_size = dyn_seg->p_filesz; /* Practical Binary Analysis, 2.4.3 */
dynamic.sh_type = htobe32(SHT_DYNAMIC);
if ((err = add_section_header(s_info, ".dynamic", &dynamic, ".dynstr", NULL)) != ORC_SUCCESS)
return err;
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_SYMTAB, &dynamic_tag))) {
case ORC_SUCCESS:
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_SYMTAB dynamic tag\n");
default:
return err;
}
dynsym.sh_addr = dynamic_tag.d_un.d_ptr;
fprintf(stderr, "Found DT_SYMTAB at 0x%x\n", be32toh(dynsym.sh_addr));
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_SYMENT, &dynamic_tag))) {
case ORC_SUCCESS:
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_SYMENT dynamic tag\n");
default:
return err;
}
syment = be32toh(dynamic_tag.d_un.d_val);
fprintf(stderr, "Found DT_SYMENT at 0x%x\n", syment);
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_MIPS_SYMTABNO, &dynamic_tag))) {
case ORC_SUCCESS:
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_MIPS_SYMTABNO dynamic tag\n");
default:
return err;
}
symtabno = be32toh(dynamic_tag.d_un.d_val);
fprintf(stderr, "Found DT_MIPS_SYMTABNO at 0x%x\n", symtabno);
if ((err = calculate_file_offset(loadable_segs, num_loadable_segs, be32toh(dynsym.sh_addr), &dynsym.sh_offset)) != ORC_SUCCESS)
return err;
dynsym.sh_type = htobe32(SHT_DYNSYM);
dynsym.sh_flags = htobe32(SHF_ALLOC);
dynsym.sh_size = htobe32(syment * symtabno);
dynsym.sh_entsize = htobe32(syment);
/*
One greater than the symbol table index of the last local symbol.
https://docs.oracle.com/cd/E19455-01/806-3773/6jct9o0bs/index.html#elf-15226
since this is the dynamic string table, this will always be 1
*/
dynsym.sh_info = htobe32(1);
if ((err = add_section_header(s_info, ".dynsym", &dynsym, ".dynstr", NULL)) != ORC_SUCCESS)
return err;
if ((err = parse_dynamic_relocation_section(
handle,
dyn_seg_offset,
dyn_seg_size,
loadable_segs,
num_loadable_segs,
s_info)) != ORC_SUCCESS) {
fprintf(stderr, "Failed to parse dynamic relocation section\n");
return err;
}
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_VERSYM, &dynamic_tag))) {
case ORC_SUCCESS: /* https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA.junk/symversion.html */
gnu_version.sh_addr = dynamic_tag.d_un.d_ptr;
gnu_version.sh_addralign = gnu_version.sh_entsize = htobe32(sizeof(Elf32_Half));
gnu_version.sh_flags = htobe32(SHF_ALLOC);
gnu_version.sh_size = htobe32(symtabno * sizeof(Elf32_Half));
gnu_version.sh_type = htobe32(SHT_GNU_versym);
if ((err = calculate_file_offset(loadable_segs, num_loadable_segs, be32toh(gnu_version.sh_addr), &gnu_version.sh_offset)) != ORC_SUCCESS)
return err;
if ((err = add_section_header(s_info, ".gnu.version", &gnu_version, ".dynsym", NULL)) != ORC_SUCCESS)
return err;
if ((err = parse_gnu_version_requirements_section(handle, dyn_seg_offset, dyn_seg_size, loadable_segs, num_loadable_segs, s_info)) != ORC_SUCCESS)
return err;
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_VERSYM dynamic tag\n");
break;
default:
return err;
}
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_HASH, &dynamic_tag))) {
case ORC_SUCCESS:
fprintf(stderr, "Found HASH at 0x%x\n", be32toh(dynamic_tag.d_un.d_ptr));
// elf_header->e_ident[EI_CLASS] & ELFCLASS64 ? htobe64(8) : htobe32(4)
hash.sh_addr = dynamic_tag.d_un.d_ptr;
hash.sh_addralign = hash.sh_entsize = htobe32(sizeof(Elf32_Addr));
hash.sh_flags = htobe32(SHF_ALLOC);
hash.sh_offset = dynamic_tag.d_un.d_ptr - htobe32(base_addr);
if ((err = calculate_hash_size(handle, &hash)) != ORC_SUCCESS)
return err;
hash.sh_type = htobe32(SHT_HASH);
if ((err = add_section_header(s_info, ".hash", &hash, ".dynsym", NULL)) != ORC_SUCCESS)
return err;
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_HASH dynamic tag\n");
break;
default:
return err;
}
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_PLTGOT, &dynamic_tag))) {
case ORC_SUCCESS:
got.sh_addr = dynamic_tag.d_un.d_ptr;
fprintf(stderr, "Found DT_PLTGOT: 0x%x\n", be32toh(got.sh_addr));
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_PLTGOT dynamic tag\n");
break;
default:
return err;
}
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_MIPS_LOCAL_GOTNO, &dynamic_tag))) {
case ORC_SUCCESS:
mips_local_gotno = be32toh(dynamic_tag.d_un.d_val);
fprintf(stderr, "Found DT_MIPS_LOCAL_GOTNO: 0x%x\n", mips_local_gotno);
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_MIPS_LOCAL_GOTNO dynamic tag\n");
break;
default:
return err;
}
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_MIPS_GOTSYM, &dynamic_tag))) {
case ORC_SUCCESS:
mips_gotsym = be32toh(dynamic_tag.d_un.d_val);
fprintf(stderr, "Found DT_MIPS_GOTSYM: 0x%x\n", mips_gotsym);
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_MIPS_GOTSYM dynamic tag\n");
break;
default:
return err;
}
got.sh_addralign = htobe32(16);
got.sh_entsize = htobe32(4);
got.sh_flags = htobe32(SHF_ALLOC) | htobe32(SHF_WRITE) | htobe32(SHF_MIPS_GPREL);
if ((err = calculate_file_offset(loadable_segs, num_loadable_segs, be32toh(got.sh_addr), &got.sh_offset)) != ORC_SUCCESS)
return err;
got.sh_size = htobe32(((symtabno - mips_gotsym) + mips_local_gotno) * be32toh(got.sh_entsize));
got.sh_type = htobe32(SHT_PROGBITS);
if ((err = add_section_header(s_info, ".got", &got, NULL, NULL)) != ORC_SUCCESS)
return err;
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_MIPS_RLD_MAP, &dynamic_tag))) {
case ORC_SUCCESS:
rld_map.sh_addr = dynamic_tag.d_un.d_ptr;
fprintf(stderr, "Found DT_MIPS_RLD_MAP: 0x%x\n", be32toh(rld_map.sh_addr));
rld_map.sh_addralign = htobe32(4); /* size of instruction */
rld_map.sh_flags = htobe32(SHF_ALLOC) | htobe32(SHF_WRITE);
if ((err = calculate_file_offset(loadable_segs, num_loadable_segs, be32toh(rld_map.sh_addr), &rld_map.sh_offset)) != ORC_SUCCESS)
return err;
rld_map.sh_size = htobe32(4); /* size of instruction */
rld_map.sh_type = htobe32(SHT_PROGBITS);
if ((err = add_section_header(s_info, ".rld_map", &rld_map, NULL, NULL)) != ORC_SUCCESS)
return err;
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_MIPS_RLD_MAP dynamic tag\n");
break;
default:
return err;
}
if (IS_MIPS_NONPIC(elf_hdr)) {
err = parse_mips_nonpic(
handle,
dyn_seg_offset,
dyn_seg_size,
loadable_segs,
num_loadable_segs,
s_info
);
if (err != ORC_SUCCESS)
return err;
}
//fseek dynsym + (SYMENT * MIPS_GOTSYM)
/*
parse MIPS stubs
*/
mips_external_gotno = symtabno - mips_gotsym;
got_off = be32toh(got.sh_offset) + (be32toh(got.sh_entsize) * mips_local_gotno);
dynsym_off = be32toh(dynsym.sh_offset) + (be32toh(dynsym.sh_entsize) * mips_gotsym);
fprintf(stderr, "GOT offset: 0x%x\ndynsym offset: 0x%x\nnum external gotno: %u\n", got_off, dynsym_off, mips_external_gotno);
err = get_mips_stub_info(
handle,
mips_external_gotno,
got_off,
dynsym_off,
be32toh(got.sh_entsize),
be32toh(dynsym.sh_entsize),
&mips_stub_count,
&mips_stubs.sh_addr
);
if (err != ORC_SUCCESS) {
fprintf(stderr, "Failed to build .MIPS.stubs section\n");
return err;
}
if (!mips_stub_count)
fprintf(stderr, "No .MIPS.stubs detected\n");
else {
mips_stubs.sh_addralign = htobe32(sizeof(Elf32_Addr));
mips_stubs.sh_flags = htobe32(SHF_ALLOC) | htobe32(SHF_EXECINSTR);
if ((err = calculate_file_offset(loadable_segs, num_loadable_segs, be32toh(mips_stubs.sh_addr), &mips_stubs.sh_offset)) != ORC_SUCCESS)
return err;
mips_stubs.sh_size = htobe32(sizeof(Elf32_Addr) * 4 * (mips_stub_count + 1)); /* are stubs always 4 instructions? are they always terminated with a null stub? */
mips_stubs.sh_type = htobe32(SHT_PROGBITS);
if ((err = add_section_header(s_info, ".MIPS.stubs", &mips_stubs, NULL, NULL)) != ORC_SUCCESS)
return err;
}
return ORC_SUCCESS;
}
enum ORCError parse_mips_nonpic(
FILE *handle,
Elf32_Off dyn_seg_offset,
Elf32_Word dyn_seg_size,
Elf32_Phdr *loadable_segs,
Elf32_Half num_loadable_segs,
struct section_info *s_info
) {
Elf32_Shdr rel_plt = { 0 }, got_plt = { 0 }, plt = { 0 };
Elf32_Dyn dynamic_tag;
enum ORCError err;
/*
This will add attempt to add the following sections,
present in MIPS non-PIC ABI objects:
* .got.plt
* .rel.plt
* .plt
*/
if ((err = parse_rel_plt_from_dyn_seg(handle, dyn_seg_offset, dyn_seg_size, &rel_plt)) != ORC_SUCCESS)
return err;
switch ((err = find_dynamic_tag(handle, dyn_seg_offset, dyn_seg_size, DT_MIPS_PLTGOT, &dynamic_tag))) {
case ORC_SUCCESS:
got_plt.sh_addr = dynamic_tag.d_un.d_ptr;
fprintf(stderr, "Found DT_MIPS_PLTGOT: 0x%x\n", be32toh(got_plt.sh_addr));
break;
case ORC_DYN_TAG_NOT_FOUND:
fprintf(stderr, "Failed to find DT_MIPS_PLTGOT dynamic tag\n");
default:
return err;
}
if ((err = calculate_file_offset(loadable_segs, num_loadable_segs, be32toh(rel_plt.sh_addr), &rel_plt.sh_offset)) != ORC_SUCCESS)
return err;
/*
This section headers sh_info field holds a section header table index.
https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-94076.html
*/
rel_plt.sh_flags = htobe32(SHF_ALLOC) | htobe32(SHF_INFO_LINK);
rel_plt.sh_type = htobe32(SHT_REL);
Elf32_Word num_jump_slot_relocs;
if ((err = count_mips_jump_slot_relocs(handle, be32toh(rel_plt.sh_offset), be32toh(rel_plt.sh_size), &num_jump_slot_relocs)) != ORC_SUCCESS)
return err;
/*
number of R_MIPS_JUMP_SLOT in .rel.plt + pltgot[0] (dynamic linker's PLT resolver) + pltgot[1] (object link map)
multiplied by the size of a MIPS32 address (4 bytes)
*/
got_plt.sh_size = htobe32((num_jump_slot_relocs + 2) * 4);
if ((err = calculate_file_offset(loadable_segs, num_loadable_segs, be32toh(got_plt.sh_addr), &got_plt.sh_offset)) != ORC_SUCCESS)