-
Notifications
You must be signed in to change notification settings - Fork 9
/
t_elf32.c
1478 lines (1244 loc) · 47.5 KB
/
t_elf32.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
/* $VER: vlink t_elf32.c V0.17a (30.03.22)
*
* This file is part of vlink, a portable linker for multiple
* object formats.
* Copyright (c) 1997-2022
*/
#include "config.h"
#ifdef ELF32
#define T_ELF32_C
#include "vlink.h"
#include "elf32.h"
#include "stabdefs.h"
/* static data required for output file generation */
static struct RelocList *reloclist;
static struct Section *dynamic;
/* stabs */
static struct ShdrNode *stabshdr;
static struct list stabcompunits;
static uint32_t stabdebugidx;
/*****************************************************************/
/* Read ELF */
/*****************************************************************/
static struct Elf32_Shdr *elf32_shdr(struct LinkFile *lf,
struct Elf32_Ehdr *ehdr,uint16_t idx)
/* return pointer to section header #idx */
{
bool be = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB);
struct Elf32_Shdr *shdr;
if (idx < read16(be,ehdr->e_shnum)) {
shdr = (struct Elf32_Shdr *)(((char *)ehdr) + ((read32(be,ehdr->e_shoff) +
(uint32_t)read16(be,ehdr->e_shentsize) * (uint32_t)idx)));
if (((uint8_t *)shdr < lf->data) ||
(((uint8_t *)shdr)+read16(be,ehdr->e_shentsize) > lf->data+lf->length))
/* section header #x has illegal offset */
error(44,lf->pathname,(int)idx,lf->objname);
return shdr;
}
else /* Invalid ELF section header index */
error(43,lf->pathname,(int)idx,lf->objname);
return NULL; /* not reached, for compiler's sake */
}
static char *elf32_shstrtab(struct LinkFile *lf,struct Elf32_Ehdr *ehdr)
/* returns a pointer to the section header string table, if present, */
/* or NULL, otherwise */
{
bool be = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB);
uint16_t i;
struct Elf32_Shdr *shdr;
char *stab;
if (i = read16(be,ehdr->e_shstrndx)) {
shdr = elf32_shdr(lf,ehdr,i);
if (read32(be,shdr->sh_type) != SHT_STRTAB)
error(45,lf->pathname,lf->objname); /* illegal type */
stab = ((char *)ehdr) + read32(be,shdr->sh_offset);
if (((uint8_t *)stab < lf->data) ||
((uint8_t *)stab + read32(be,shdr->sh_size) > lf->data + lf->length))
error(46,lf->pathname,lf->objname); /* illegal offset */
else
return stab;
}
return NULL;
}
static char *elf32_strtab(struct LinkFile *lf,struct Elf32_Ehdr *ehdr,int idx)
/* returns a pointer to the string table */
{
static char *tabname = "string";
bool be = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB);
struct Elf32_Shdr *shdr;
char *stab;
shdr = elf32_shdr(lf,ehdr,idx);
if (read32(be,shdr->sh_type) != SHT_STRTAB)
error(50,lf->pathname,tabname,lf->objname); /* illegal type */
stab = ((char *)ehdr) + read32(be,shdr->sh_offset);
elf_check_offset(lf,tabname,stab,read32(be,shdr->sh_size));
return stab;
}
static struct Elf32_Sym *elf32_symtab(struct LinkFile *lf,
struct Elf32_Ehdr *ehdr,int idx)
/* returns a pointer to the symbol table */
{
static char *tabname = "symbol";
bool be = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB);
struct Elf32_Shdr *shdr;
struct Elf32_Sym *symtab;
uint32_t shtype;
shdr = elf32_shdr(lf,ehdr,idx);
shtype = read32(be,shdr->sh_type);
if (shtype!=SHT_SYMTAB && shtype!=SHT_DYNSYM)
error(50,lf->pathname,tabname,lf->objname); /* illegal type */
symtab = (struct Elf32_Sym *)((uint8_t *)ehdr + read32(be,shdr->sh_offset));
elf_check_offset(lf,tabname,symtab,read32(be,shdr->sh_size));
return symtab;
}
static void elf32_section(struct GlobalVars *gv,struct Elf32_Ehdr *ehdr,
struct ObjectUnit *ou,struct Elf32_Shdr *shdr,
int shndx,char *shstrtab)
/* create a new section */
{
bool be = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB);
struct Section *s;
if (s = elf_add_section(gv,ou,
shstrtab+read32(be,shdr->sh_name),
(uint8_t *)ehdr+read32(be,shdr->sh_offset),
read32(be,shdr->sh_size),
read32(be,shdr->sh_type),
read32(be,shdr->sh_flags),
shiftcnt(read32(be,shdr->sh_addralign)))) {
s->link = read32(be,shdr->sh_link); /* save link for later use */
s->id = shndx; /* use section header index for identification */
}
}
static void elf32_symbols(struct GlobalVars *gv,struct Elf32_Ehdr *ehdr,
struct ObjectUnit *ou,struct Elf32_Shdr *shdr)
/* convert ELF symbol definitions into internal format */
{
bool be = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB);
struct LinkFile *lf = ou->lnkfile;
uint8_t *data = (uint8_t *)ehdr + read32(be,shdr->sh_offset);
unsigned long entsize = read32(be,shdr->sh_entsize);
int nsyms = (int)(read32(be,shdr->sh_size) / (uint32_t)entsize);
char *strtab = elf32_strtab(lf,ehdr,read32(be,shdr->sh_link));
elf_check_offset(lf,"symbol",data,read32(be,shdr->sh_size));
/* read ELF xdef symbols and convert to internal format */
while (--nsyms > 0) {
struct Elf32_Sym *elfsym;
char *symname;
elfsym = (struct Elf32_Sym *)(data += entsize);
symname = strtab + read32(be,elfsym->st_name);
if (symname<(char *)lf->data || symname>(char *)lf->data+lf->length)
error(127,lf->pathname,read32(be,elfsym->st_name),lf->objname);
elf_add_symbol(gv,ou,symname,
(read32(be,shdr->sh_type)==SHT_DYNSYM) ? SYMF_SHLIB : 0,
read16(be,elfsym->st_shndx),
read32(be,shdr->sh_type),
ELF32_ST_TYPE(*elfsym->st_info),
ELF32_ST_BIND(*elfsym->st_info),
(int32_t)read32(be,elfsym->st_value),
read32(be,elfsym->st_size));
}
}
static void elf32_dynrefs(struct GlobalVars *gv,struct Elf32_Ehdr *ehdr,
struct ObjectUnit *ou,struct Elf32_Shdr *shdr,
bool be,
uint8_t (*reloc_elf2vlink)(uint8_t,struct RelocInsert *))
/* Find all relocs in a shared object which refer to an undefined symbol. */
{
uint8_t *data = (uint8_t *)ehdr + read32(be,shdr->sh_offset);
unsigned long entsize = read32(be,shdr->sh_entsize);
uint32_t symndx = read32(be,shdr->sh_link);
int nrelocs = (int)(read32(be,shdr->sh_size) / (uint32_t)entsize);
struct LinkFile *lf = ou->lnkfile;
struct Section *sec;
struct Reloc *r;
elf_check_offset(lf,"reloc",data,read32(be,shdr->sh_size));
/* just put all xrefs into the first section of the shared object */
sec = abs_section(ou);
for (; nrelocs; nrelocs--,data+=entsize) {
struct Elf32_Rela *elfrel = (struct Elf32_Rela *)data;
struct Elf32_Shdr *symhdr = elf32_shdr(lf,ehdr,symndx);
struct Elf32_Sym *sym = elf32_symtab(lf,ehdr,symndx) +
ELF32_R_SYM(read32(be,elfrel->r_info));
uint32_t shndx = (uint32_t)read16(be,sym->st_shndx);
struct RelocInsert ri;
uint8_t rtype;
if (shndx == SHN_UNDEF || shndx == SHN_COMMON) {
memset(&ri,0,sizeof(struct RelocInsert));
rtype = reloc_elf2vlink(ELF32_R_TYPE(read32(be,elfrel->r_info)),&ri);
r = newreloc(gv,sec,elf32_strtab(lf,ehdr,read32(be,symhdr->sh_link))
+ read32(be,sym->st_name),
NULL,0,read32(be,elfrel->r_offset),rtype,0);
addreloc_ri(sec,r,&ri);
/* referenced symbol is weak? */
if (ELF32_ST_BIND(*sym->st_info)==STB_WEAK)
r->flags |= RELF_WEAK;
}
}
}
static void elf32_reloc(struct GlobalVars *gv,struct Elf32_Ehdr *ehdr,
struct ObjectUnit *ou,struct Elf32_Shdr *shdr,
char *shstrtab,bool be,
uint8_t (*reloc_elf2vlink)(uint8_t,struct RelocInsert *))
/* Read ELF32 relocations, which are relative to a defined symbol, into
the section's reloc-list. If the symbol is undefined, create an
external reference on it, with the supplied relocation type. */
{
uint8_t *data = (uint8_t *)ehdr + read32(be,shdr->sh_offset);
bool is_rela = read32(be,shdr->sh_type) == SHT_RELA;
unsigned long entsize = read32(be,shdr->sh_entsize);
uint32_t symndx = read32(be,shdr->sh_link);
int nrelocs = (int)(read32(be,shdr->sh_size) / (uint32_t)entsize);
char *sec_name = shstrtab + read32(be,shdr->sh_name);
struct LinkFile *lf = ou->lnkfile;
struct Section *sec;
if (gv->strip_symbols>=STRIP_DEBUG &&
(!strncmp(sec_name,".rel.debug",10) ||
!strncmp(sec_name,".rel.stab",9) ||
!strncmp(sec_name,".rela.debug",11) ||
!strncmp(sec_name,".rela.stab",10)))
return; /* ignore debugging sections when -S or -s is given */
elf_check_offset(lf,"reloc",data,read32(be,shdr->sh_size));
if (!(sec = find_sect_id(ou,read32(be,shdr->sh_info)))) {
/* a section with this index doesn't exist! */
error(52,lf->pathname,shstrtab + read32(be,shdr->sh_name),
getobjname(ou),(int)read32(be,shdr->sh_info));
}
for (; nrelocs; nrelocs--,data+=entsize) {
struct Elf32_Rela *elfrel = (struct Elf32_Rela *)data;
struct Elf32_Shdr *symhdr = elf32_shdr(lf,ehdr,symndx);
struct Elf32_Sym *sym = elf32_symtab(lf,ehdr,symndx) +
ELF32_R_SYM(read32(be,elfrel->r_info));
uint32_t offs = read32(be,elfrel->r_offset);
uint32_t shndx = (uint32_t)read16(be,sym->st_shndx);
char *xrefname = NULL;
struct Section *relsec=NULL;
struct Reloc *r;
struct RelocInsert ri;
lword a;
uint8_t rtype;
memset(&ri,0,sizeof(struct RelocInsert));
rtype = reloc_elf2vlink(ELF32_R_TYPE(read32(be,elfrel->r_info)),&ri);
/* if addend is not defined in Reloc, read it directly from the section */
if (is_rela)
a = (int32_t)read32(be,elfrel->r_addend);
else
a = (int32_t)readsection(gv,rtype,sec->data,offs,&ri);
if (shndx == SHN_UNDEF || shndx == SHN_COMMON ||
ELF32_ST_BIND(*sym->st_info) == STB_WEAK) {
/* undefined, common or weak symbol - create external reference */
xrefname = elf32_strtab(lf,ehdr,read32(be,symhdr->sh_link)) +
read32(be,sym->st_name);
relsec = NULL;
}
else if (ELF32_ST_TYPE(*sym->st_info) == STT_SECTION) {
/* a normal relocation, with an offset relative to a section base */
relsec = find_sect_id(ou,shndx);
}
else if (ELF32_ST_TYPE(*sym->st_info)<STT_SECTION && shndx<SHN_ABS) {
/* relocations, which are relative to a known symbol */
relsec = find_sect_id(ou,shndx);
a += (lword)read32(be,sym->st_value);
}
else
ierror("elf32_reloc(): %s (%s): Only relocations which are relative "
"to a section, function or object are supported "
"(sym=%s, ST_TYPE=%d)",lf->pathname,lf->objname,
elf32_strtab(lf,ehdr,read32(be,symhdr->sh_link)) +
read32(be,sym->st_name),
ELF32_ST_TYPE(*sym->st_info));
r = newreloc(gv,sec,xrefname,relsec,0,(unsigned long)offs,rtype,a);
addreloc_ri(sec,r,&ri);
if (xrefname!=NULL && ELF32_ST_BIND(*sym->st_info)==STB_WEAK)
r->flags |= RELF_WEAK; /* referenced symbol is weak */
/* make sure that section data reflects this addend for other formats */
if (is_rela)
writesection(gv,sec->data,offs,r,a);
}
}
static void elf32_stabs(struct GlobalVars *gv,struct LinkFile *lf,
struct Elf32_Ehdr *ehdr,struct ObjectUnit *ou)
/* find .stabstr belonging to .stab, convert everything (including
.rela.stab) into internal format and delete *.stab* afterwards. */
{
static const char *fn = "elf32_stabs";
static const char *stabname = "stab";
bool be = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB);
struct Section *stabsec;
if (stabsec = find_sect_name(ou,".stab")) {
char *strtab = elf32_strtab(lf,ehdr,stabsec->link);
struct nlist32 *nlst = (struct nlist32 *)stabsec->data;
long nlstlen = (long)stabsec->size;
while (nlstlen >= sizeof(struct nlist32)) {
/* next compilation unit: */
/* read number of nlist records and size of string table */
int cnt = (int)read16(be,&nlst->n_desc);
uint32_t strtabsize = read32(be,&nlst->n_value);
uint32_t funstart = 0; /* start address of last function definition */
struct Section *funsec = NULL;
nlst++;
nlstlen -= sizeof(struct nlist32);
while (cnt--) {
struct Reloc *r;
struct Section *relsec;
char *name;
uint32_t val;
if (nlstlen < sizeof(struct nlist32))
error(118,lf->pathname,stabname,lf->objname); /* malformatted */
if (nlst->n_strx)
name = strtab + read32(be,&nlst->n_strx);
else
name = NULL;
switch (nlst->n_type & N_TYPE) {
case N_TEXT:
case N_DATA:
case N_BSS:
if (r = findreloc(stabsec,
(uint8_t *)&nlst->n_value - stabsec->data)) {
if (r->rtype!=R_ABS || r->insert->bsiz!=32)
ierror("%s: Bad .stab relocation",fn);
relsec = r->relocsect.ptr;
val = (uint32_t)r->addend;
break;
}
default:
relsec = NULL;
val = read32(be,&nlst->n_value);
break;
}
switch (nlst->n_type) {
case N_FUN:
if (nlst->n_strx) {
if (relsec) { /* function start is always relocatable */
funsec = relsec;
funstart = val;
}
#if 0 /* @@@ N_FUN without a label in n_value seems legal? */
else
ierror("%s: N_FUN without relocatable address",fn);
#endif
}
else { /* no name marks function end, still relative */
relsec = funsec;
val += funstart;
}
break;
case N_SLINE:
if (relsec == NULL) {
relsec = funsec;
val += funstart;
}
break;
}
addstabs(ou,relsec,name,nlst->n_type,nlst->n_other,
(int16_t)read16(be,&nlst->n_desc),val);
nlst++;
nlstlen -= sizeof(struct nlist32);
}
strtab += strtabsize;
}
if (nlstlen)
error(118,lf->pathname,stabname,lf->objname); /* ignoring junk */
/* remove .stab from this object unit - will be recreated later */
remnode(&stabsec->n);
}
}
void elf32_parse(struct GlobalVars *gv,struct LinkFile *lf,
struct Elf32_Ehdr *ehdr,
uint8_t (*reloc_elf2vlink)(uint8_t,struct RelocInsert *))
/* parses a complete ELF file and converts into vlink-internal format */
{
static const char *fn = "elf32_parse(): ";
bool be = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB);
struct ObjectUnit *u;
struct Elf32_Shdr *shdr;
uint16_t i,num_shdr,dynstr_idx,dynsym_idx;
char *shstrtab;
struct Elf32_Dyn *dyn;
shstrtab = elf32_shstrtab(lf,ehdr);
u = create_objunit(gv,lf,lf->objname);
switch (read16(be,ehdr->e_type)) {
case ET_REL: /* relocatable object file */
if (read16(be,ehdr->e_phnum) > 0)
error(47,lf->pathname,lf->objname); /* ignoring program hdr. tab */
num_shdr = read16(be,ehdr->e_shnum);
/* create vlink sections */
for (i=1; i<num_shdr; i++) {
shdr = elf32_shdr(lf,ehdr,i);
switch (read32(be,shdr->sh_type)) {
case SHT_PROGBITS:
case SHT_NOBITS:
case SHT_NOTE:
/* create a new section */
elf32_section(gv,ehdr,u,shdr,i,shstrtab);
default:
break;
}
}
/* parse the other section headers */
for (i=1; i<num_shdr; i++) {
shdr = elf32_shdr(lf,ehdr,i);
switch (read32(be,shdr->sh_type)) {
case SHT_NULL:
case SHT_STRTAB:
case SHT_NOTE:
case SHT_PROGBITS:
case SHT_NOBITS:
break;
case SHT_SYMTAB:
elf32_symbols(gv,ehdr,u,shdr); /* symbol definitions */
break;
case SHT_REL:
case SHT_RELA:
elf32_reloc(gv,ehdr,u,shdr,shstrtab,be,reloc_elf2vlink);
break;
default:
/* section header type not needed in relocatable objects */
error(48,lf->pathname,read32(be,shdr->sh_type),lf->objname);
break;
}
}
elf32_stabs(gv,lf,ehdr,u); /* convert .stab into internal format */
break;
case ET_DYN: /* shared object file */
dyn = NULL;
dynstr_idx = dynsym_idx = 0;
num_shdr = read16(be,ehdr->e_shnum);
/* create vlink sections */
for (i=1; i<num_shdr; i++) {
shdr = elf32_shdr(lf,ehdr,i);
switch (read32(be,shdr->sh_type)) {
case SHT_DYNAMIC:
/* remember pointer to .dynamic section contents */
dyn = (struct Elf32_Dyn *)((uint8_t *)ehdr
+ read32(be,shdr->sh_offset));
break;
case SHT_DYNSYM:
dynsym_idx = i;
break;
case SHT_PROGBITS:
case SHT_NOBITS:
/* create a new section */
elf32_section(gv,ehdr,u,shdr,i,shstrtab);
default:
break;
}
}
/* parse the other section headers */
for (i=1; i<num_shdr; i++) {
shdr = elf32_shdr(lf,ehdr,i);
switch (read32(be,shdr->sh_type)) {
case SHT_NULL:
case SHT_STRTAB:
case SHT_NOTE:
case SHT_PROGBITS:
case SHT_NOBITS:
case SHT_HASH:
case SHT_DYNAMIC:
case SHT_SYMTAB:
break;
case SHT_DYNSYM:
dynstr_idx = read32(be,shdr->sh_link);
elf32_symbols(gv,ehdr,u,shdr); /* symbol definitions */
break;
case SHT_REL:
case SHT_RELA:
if (fff[gv->dest_format]->flags & FFF_DYN_RESOLVE_ALL) {
/* The dynamic link editor is limited, so we even have to
resolve references from the shared object at link time.
But only those which are linked to .dynsym. */
if (read32(be,shdr->sh_link) == dynsym_idx)
elf32_dynrefs(gv,ehdr,u,shdr,be,reloc_elf2vlink);
}
break;
default:
/* section header type not needed in shared objects */
error(60,lf->pathname,read32(be,shdr->sh_type),lf->objname);
break;
}
}
if (dynstr_idx!=0 && dyn!=NULL) {
/* set ObjectUnit's objname to the SONAME of the shared object */
uint32_t tag;
shdr = elf32_shdr(lf,ehdr,dynstr_idx); /* .dynstr */
while (tag = read32(be,dyn->d_tag)) {
if (tag == DT_SONAME) {
u->objname = (char *)ehdr + read32(be,shdr->sh_offset)
+ read32(be,dyn->d_val);
break;
}
dyn++;
}
}
break;
case ET_EXEC: /* executable file */
/* @@@ */
ierror("%s%s: Executables are currently not supported",fn,lf->pathname);
break;
default:
error(41,lf->pathname,lf->objname); /* illegal fmt./file corrupted */
break;
}
/* add new object unit to the appropriate list */
add_objunit(gv,u,FALSE);
}
/*****************************************************************/
/* Link ELF */
/*****************************************************************/
static void elf32_initsym(void *p,uint32_t nameoff,uint64_t value,
uint64_t size,uint8_t bind,uint8_t type,
uint16_t shndx,bool be)
{
struct Elf32_Sym *s = (struct Elf32_Sym *)p;
write32(be,s->st_name,nameoff);
write32(be,s->st_value,(uint32_t)value);
write32(be,s->st_size,(uint32_t)size);
s->st_info[0] = ELF32_ST_INFO(bind,type);
s->st_other[0] = 0;
write16(be,s->st_shndx,shndx);
}
static void elf32_initreloc(void *p,uint64_t offset,uint64_t addend,
uint32_t symidx,uint32_t type,bool be)
{
struct Elf32_Rela *r = (struct Elf32_Rela *)p;
write32(be,r->r_offset,(uint32_t)offset);
write32(be,r->r_addend,(uint32_t)addend);
write32(be,r->r_info,ELF32_R_INFO(symidx,type));
}
void elf32_initdynlink(struct GlobalVars *gv)
{
elf_initsymtabs(sizeof(struct Elf32_Sym),elf32_initsym);
dynamic = elf_initdynlink(gv);
}
struct Symbol *elf32_pltgotentry(struct GlobalVars *gv,struct Section *sec,
DynArg a,uint8_t entrysymtype,
unsigned long offsadd,unsigned long sizeadd,
int etype)
/* Make a table entry for indirectly accessing a location from an external
symbol definition (GOT_ENTRY/PLT_ENTRY) or a local relocation (GOT_LOCAL).
The entry has a size of offsadd bytes, while the table section sec will
become sizeadd bytes larger per entry. */
{
bool relaflag = gv->reloctab_format == RTAB_ADDEND;
return elf_pltgotentry(gv,sec,a,entrysymtype,offsadd,sizeadd,etype,relaflag,
relaflag ?
sizeof(struct Elf32_Rela) : sizeof(struct Elf32_Rel),
32);
}
struct Symbol *elf32_bssentry(struct GlobalVars *gv,const char *secname,
struct Symbol *xdef)
/* Allocate space for a copy-object in a .bss or .sbss section and create
a R_COPY relocation to the original symbol in the shared object. */
{
bool relaflag = gv->reloctab_format == RTAB_ADDEND;
return elf_bssentry(gv,secname,xdef,relaflag,relaflag ?
sizeof(struct Elf32_Rela) : sizeof(struct Elf32_Rel),
32);
}
void elf32_dynamicentry(struct GlobalVars *gv,uint32_t tag,uint32_t val,
struct Section *relsec)
/* store another entry into the .dynamic section, make new relocation with
relsec in value-field, when nonzero */
{
if (dynamic) {
bool be = elf_endianness == _BIG_ENDIAN_;
struct Elf32_Dyn dyn;
unsigned long offs = dynamic->size;
write32(be,dyn.d_tag,tag);
write32(be,dyn.d_val,val);
dynamic->data = re_alloc(dynamic->data,
dynamic->size+sizeof(struct Elf32_Dyn));
memcpy(dynamic->data+offs,&dyn,sizeof(struct Elf32_Dyn));
dynamic->size += sizeof(struct Elf32_Dyn);
if (relsec) {
/* we need a 32-bit R_ABS relocation for d_val */
struct Reloc *r;
int o = offsetof(struct Elf32_Dyn,d_val);
r = newreloc(gv,dynamic,NULL,relsec,0,offs+o,R_ABS,(lword)val);
r->flags |= RELF_INTERNAL; /* just for calculating the address */
addreloc(dynamic,r,0,32,-1);
}
}
else
ierror("elf32_dynamicentry(): .dynamic was never created");
}
static void elf32_makehash(struct GlobalVars *gv)
/* Allocate and populate .hash section. */
{
bool be = elf_endianness == _BIG_ENDIAN_;
size_t nsyms = elfdsymlist.nextindex;
size_t nbuckets = elf_num_buckets(nsyms);
struct Section *hashsec = find_sect_name(gv->dynobj,hash_name);
if (hashsec) {
struct SymbolNode *sn;
uint32_t *hdata;
/* .hash layout: nbuckets, nsyms, [buckets], [sym-indexes] */
hashsec->size = (2 + nbuckets + nsyms) * sizeof(uint32_t);
hashsec->data = alloczero(hashsec->size);
hdata = (uint32_t *)hashsec->data;
write32(be,&hdata[0],nbuckets);
write32(be,&hdata[1],nsyms);
for (sn=(struct SymbolNode *)elfdsymlist.l.first;
sn->n.next!=NULL; sn=(struct SymbolNode *)sn->n.next) {
uint32_t *i = &hdata[2 + elf_hash(sn->name) % nbuckets];
uint32_t j;
while (j = read32(be,i))
i = &hdata[2 + nbuckets + j];
write32(be,i,sn->index);
}
}
else
ierror("elf32_makehash(): no %s",hash_name);
}
void elf32_dyncreate(struct GlobalVars *gv,const char *pltgot_name)
/* generate .hash, populate .dynstr and .dynamic, allocate .dynsym,
so that all sections have a valid size for the address calculation */
{
const char *fn = "elf32_dyncreate():";
struct Section *dynstr,*dynsym,*pltgot;
struct LibPath *lpn;
if (gv->dynobj == NULL)
ierror("%s no dynobj",fn);
/* write SONAME and RPATHs */
if (gv->soname && gv->dest_sharedobj)
elf32_dynamicentry(gv,DT_SONAME,elf_adddynstr(gv->soname),NULL);
for (lpn=(struct LibPath *)gv->rpaths.first; lpn->n.next!=NULL;
lpn=(struct LibPath *)lpn->n.next) {
elf32_dynamicentry(gv,DT_RPATH,elf_adddynstr(lpn->path),NULL);
}
/* generate .hash section */
elf32_makehash(gv);
/* allocate and populate .dynstr section */
if (dynstr = find_sect_name(gv->dynobj,dynstr_name)) {
dynstr->size = elfdstrlist.nextindex;
dynstr->data = alloc(dynstr->size);
elf_putstrtab(dynstr->data,&elfdstrlist);
}
else
ierror("%s %s missing",fn,dynstr_name);
/* allocate .dynsym section - populate it later, when addresses are fixed */
if (dynsym = find_sect_name(gv->dynobj,dynsym_name)) {
dynsym->size = elfdsymlist.nextindex * sizeof(struct Elf32_Sym);
dynsym->data = alloc(dynsym->size);
}
else
ierror("%s %s missing",fn,dynsym_name);
/* finish .dynamic section */
elf32_dynamicentry(gv,DT_HASH,0,find_sect_name(gv->dynobj,hash_name));
elf32_dynamicentry(gv,DT_STRTAB,0,dynstr);
elf32_dynamicentry(gv,DT_SYMTAB,0,dynsym);
elf32_dynamicentry(gv,DT_STRSZ,dynstr->size,NULL);
elf32_dynamicentry(gv,DT_SYMENT,sizeof(struct Elf32_Sym),NULL);
elf32_dynamicentry(gv,DT_DEBUG,0,NULL); /* needed? */
/* do we have a .plt or .got section (target dependent) */
if (pltgot = find_sect_name(gv->dynobj,pltgot_name)) {
elf32_dynamicentry(gv,DT_PLTGOT,0,pltgot);
}
/* do we have .plt relocations? */
if (elfpltrelocs) {
elf32_dynamicentry(gv,DT_PLTRELSZ,elfpltrelocs->size,NULL);
elf32_dynamicentry(gv,DT_PLTREL,
gv->reloctab_format==RTAB_ADDEND?DT_RELA:DT_REL,
NULL);
elf32_dynamicentry(gv,DT_JMPREL,0,elfpltrelocs);
}
/* do we have any other dynamic relocations? */
if (elfdynrelocs) {
if (gv->reloctab_format == RTAB_ADDEND) {
elf32_dynamicentry(gv,DT_RELA,0,elfdynrelocs);
elf32_dynamicentry(gv,DT_RELASZ,elfdynrelocs->size,NULL);
elf32_dynamicentry(gv,DT_RELAENT,sizeof(struct Elf32_Rela),NULL);
}
else {
elf32_dynamicentry(gv,DT_REL,0,elfdynrelocs);
elf32_dynamicentry(gv,DT_RELSZ,elfdynrelocs->size,NULL);
elf32_dynamicentry(gv,DT_RELENT,sizeof(struct Elf32_Rel),NULL);
}
}
/* end tag */
elf32_dynamicentry(gv,DT_NULL,0,NULL);
}
/*****************************************************************/
/* Write ELF */
/*****************************************************************/
unsigned long elf32_headersize(struct GlobalVars *gv)
{
return sizeof(struct Elf32_Ehdr) +
elf_numsegments(gv) * sizeof(struct Elf32_Phdr);
}
static void elf32_header(FILE *f,uint16_t type,uint16_t mach,uint32_t entry,
uint32_t phoff,uint32_t shoff,uint32_t flags,
uint16_t phnum,uint16_t shnum,uint16_t shstrndx,
bool be)
/* write 32-bit ELF header */
{
struct Elf32_Ehdr eh;
memset(&eh,0,sizeof(struct Elf32_Ehdr));
elf_ident(&eh,be,ELFCLASS32,type,mach);
write32(be,eh.e_entry,entry);
write32(be,eh.e_phoff,phoff);
write32(be,eh.e_shoff,shoff);
write32(be,eh.e_flags,flags);
write16(be,eh.e_ehsize,sizeof(struct Elf32_Ehdr));
write16(be,eh.e_phentsize,phnum ? sizeof(struct Elf32_Phdr):0);
write16(be,eh.e_phnum,phnum);
write16(be,eh.e_shentsize,shnum ? sizeof(struct Elf32_Shdr):0);
write16(be,eh.e_shnum,shnum);
write16(be,eh.e_shstrndx,shstrndx);
fwritex(f,&eh,sizeof(struct Elf32_Ehdr));
}
static struct ShdrNode *elf32_newshdr(void)
{
struct ShdrNode *s = alloczero(sizeof(struct ShdrNode));
addtail(&shdrlist,&s->n);
++elfshdridx;
return s;
}
static struct ShdrNode *elf32_addshdr(uint32_t name,uint32_t type,
uint32_t flags,uint32_t addr,
uint32_t offset,uint32_t size,
uint32_t link,uint32_t info,
uint32_t align,uint32_t entsize,bool be)
{
struct ShdrNode *shn = elf32_newshdr();
write32(be,shn->s.sh_name,name);
write32(be,shn->s.sh_type,type);
write32(be,shn->s.sh_flags,flags);
write32(be,shn->s.sh_addr,addr);
write32(be,shn->s.sh_offset,offset);
write32(be,shn->s.sh_size,size);
write32(be,shn->s.sh_link,link);
write32(be,shn->s.sh_info,info);
write32(be,shn->s.sh_addralign,align);
write32(be,shn->s.sh_entsize,entsize);
return shn;
}
static void elf32_writephdrs(struct GlobalVars *gv,FILE *f)
/* write 32-bit ELF Program Header (PHDR) */
{
bool be = elf_endianness == _BIG_ENDIAN_;
long gapsize = elf_file_hdr_gap;
struct Elf32_Phdr phdr;
struct Phdr *p;
for (p=gv->phdrlist; p; p=p->next) {
if (p->flags & PHDR_USED) {
if (p->start!=ADDR_NONE && p->start_vma!=ADDR_NONE) {
write32(be,phdr.p_type,p->type);
write32(be,phdr.p_offset,p->offset);
write32(be,phdr.p_vaddr,p->start_vma);
write32(be,phdr.p_paddr,p->start);
write32(be,phdr.p_filesz,p->file_end - p->start);
write32(be,phdr.p_memsz,p->mem_end - p->start);
write32(be,phdr.p_flags,p->flags & PHDR_PFMASK);
write32(be,phdr.p_align,1L<<p->alignment);
fwritex(f,&phdr,sizeof(struct Elf32_Phdr));
}
else
gapsize += sizeof(struct Elf32_Phdr);
}
}
fwritegap(gv,f,gapsize); /* gap at the end, for unused PHDRs */
}
static void elf32_writeshdrs(struct GlobalVars *gv,FILE *f,
uint32_t reloffset,uint32_t stabndx)
/* write all section headers */
{
const char *fn = "elf32_writeshdrs():";
bool be = elf_endianness == _BIG_ENDIAN_;
struct LinkedSection *ls;
struct ShdrNode *shn;
uint32_t type;
while (shn = (struct ShdrNode *)remhead(&shdrlist)) {
type = read32(be,shn->s.sh_type);
/* handle REL and RELA sections */
if (type == SHT_RELA || type == SHT_REL) {
if (read32(be,shn->s.sh_flags) & SHF_ALLOC) {
/* allocated, so probably dynamic relocs: link to .dynsym */
if (ls = find_lnksec(gv,dynsym_name,0,0,0,0))
write32(be,shn->s.sh_link,(uint32_t)ls->index);
else
ierror("%s %s",fn,dynsym_name);
if (read32(be,shn->s.sh_info) != 0) {
/* link to .plt requested in info field */
if (ls = find_lnksec(gv,plt_name,0,0,0,0))
write32(be,shn->s.sh_info,(uint32_t)ls->index);
else
ierror("%s %s",fn,plt_name);
}
}
else {
/* patch correct sh_offset and sh_link for reloc header */
write32(be,shn->s.sh_offset,read32(be,shn->s.sh_offset)+reloffset);
write32(be,shn->s.sh_link,stabndx);
}
}
/* handle HASH sections, which need a .dynsym link */
else if (type == SHT_HASH) {
if (ls = find_lnksec(gv,dynsym_name,0,0,0,0))
write32(be,shn->s.sh_link,(uint32_t)ls->index);
else
ierror("%s %s",fn,dynsym_name);
}
/* handle DYNAMIC and DYNSYM sections */
else if (type==SHT_DYNAMIC || type==SHT_DYNSYM) {
/* write .dynstr link */
if (ls = find_lnksec(gv,dynstr_name,0,0,0,0))
write32(be,shn->s.sh_link,(uint32_t)ls->index);
else
ierror("%s %s",fn,dynstr_name);
if (type == SHT_DYNSYM) {
write32(be,shn->s.sh_info,1); /* @@ FIXME! number of local symbols */
}
}
fwritex(f,&shn->s,sizeof(struct Elf32_Shdr));
}
}
static void elf32_sec2shdr(struct LinkedSection *ls,bool bss,uint64_t f)
{
struct ShdrNode *shn;
uint32_t type = bss ? SHT_NOBITS : SHT_PROGBITS;
uint32_t info = 0;
uint32_t entsize = 0;
if (!strncmp(ls->name,note_name,strlen(note_name)))
type = SHT_NOTE;
else if (!strncmp(ls->name,".rela",5)) {
type = SHT_RELA;
entsize = sizeof(struct Elf32_Rela);
}
else if (!strncmp(ls->name,".rel",4)) {
type = SHT_REL;
entsize = sizeof(struct Elf32_Rel);
}
else if (!strcmp(ls->name,hash_name)) {
type = SHT_HASH;
entsize = sizeof(uint32_t);
}
else if (!strcmp(ls->name,dynsym_name)) {
type = SHT_DYNSYM;
entsize = sizeof(struct Elf32_Sym);
}
else if (!strcmp(ls->name,dyn_name)) {
type = SHT_DYNAMIC;
entsize = sizeof(struct Elf32_Dyn);
}
else if (!strcmp(ls->name,dynstr_name))
type = SHT_STRTAB;
else if (!strncmp(ls->name,got_name,strlen(got_name)))
entsize = sizeof(uint32_t);
if (!strcmp(ls->name,pltrel_name[0]) || !strcmp(ls->name,pltrel_name[1]))
info = ~0; /* request .plt index in info field for .rel(a).plt */
shn = elf32_addshdr(elf_addshdrstr(ls->name),type,(uint32_t)f,ls->base,
elfoffset,ls->size,0,info,1<<(uint32_t)ls->alignment,
entsize,elf_endianness);
if (stabdebugidx && !strcmp(ls->name,".stab"))
stabshdr = shn; /* patch sh_link field for .stabstr later */
}
static void elf32_addrelocs(struct GlobalVars *gv,
uint8_t (*reloc_vlink2elf)(struct Reloc *))
/* creates relocations for all sections */
{
bool be = elf_endianness == _BIG_ENDIAN_;
struct LinkedSection *ls;
struct Reloc *rel;
uint32_t sroffs=0,roffs=0;
for (ls=(struct LinkedSection *)gv->lnksec.first;
ls->n.next!=NULL; ls=(struct LinkedSection *)ls->n.next) {
sroffs = roffs;