-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathelf.c
1497 lines (1257 loc) · 43.7 KB
/
elf.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 elf.c V0.15e (23.03.17)
*
* This file is part of vlink, a portable linker for multiple
* object formats.
* Copyright (c) 1997-2017 Frank Wille
*/
#include "config.h"
#if defined(ELF32) || defined(ELF64)
#define ELF_C
#include "vlink.h"
#include "elfcommon.h"
#include "elf32std.h"
#include "elf64std.h"
/* global data for all ELF targets */
struct StrTabList elfstringlist = { {NULL,NULL,NULL},NULL,STRHTABSIZE,0 };
struct StrTabList elfdstrlist = { {NULL,NULL,NULL},NULL,DYNSTRHTABSIZE,0 };
struct StrTabList elfshstrlist = { {NULL,NULL,NULL},NULL,SHSTRHTABSIZE,0 };
struct SymTabList elfsymlist;
struct SymTabList elfdsymlist;
struct list shdrlist;
struct list elfdynsymlist;
struct Section *elfdynrelocs;
struct Section *elfpltrelocs;
uint32_t elfshdridx,elfsymtabidx,elfshstrtabidx,elfstrtabidx;
uint32_t elfoffset; /* current ELF file offset */
unsigned long elf_file_hdr_gap; /* gap between hdr and first segment */
int8_t elf_endianness = -1; /* used while creating output-file */
/* ELF section names */
const char note_name[] = ".note";
const char dyn_name[] = ".dynamic";
const char hash_name[] = ".hash";
const char dynsym_name[] = ".dynsym";
const char dynstr_name[] = ".dynstr";
const char *dynrel_name[2] = { ".rel.dyn",".rela.dyn" };
const char *pltrel_name[2] = { ".rel.plt",".rela.plt" };
/* local static data */
static struct list phdrlist;
static struct Section *gotsec;
static struct Section *pltsec;
static int secsyms; /* offset to find section symbols by shndx */
static char ELFid[4] = { /* identification for all ELF files */
0x7f,'E','L','F'
};
/* table to determine number of buckets in .hash */
static const size_t elf_buckets[] =
{
1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
16411, 32771, 0
};
/* common linker symbols */
static const char *elf_symnames[] = {
sdabase_name,sda2base_name,
"__CTOR_LIST__","__DTOR_LIST__","__CTOR_LIST_END","__DTOR_LIST_END",
gotbase_name+1,pltbase_name+1,dynamic_name+1
};
/*****************************************************************/
/* Read ELF */
/*****************************************************************/
int elf_identify(struct FFFuncs *ff,char *name,void *p,
lword plen,unsigned char class,
unsigned char endian,uint16_t machine,uint32_t ver)
/* check a possible ELF file against the requirements, then */
/* return its type (object, library, shared object) */
{
bool arflag = FALSE;
bool be = (endian == ELFDATA2MSB);
struct Elf_CommonHdr *hdr;
struct ar_info ai;
if (plen < sizeof(struct Elf_CommonHdr))
return ID_UNKNOWN;
if (ar_init(&ai,(char *)p,plen,name)) {
/* library archive detected, extract 1st archive member */
arflag = TRUE;
if (!(ar_extract(&ai))) {
error(38,name); /* Empty archive ignored */
return ID_IGNORE;
}
hdr = (struct Elf_CommonHdr *)ai.data;
}
else
hdr = (struct Elf_CommonHdr *)p;
if ((class==ELFCLASS32 && plen < sizeof(struct Elf32_Ehdr)) ||
(class==ELFCLASS64 && plen < sizeof(struct Elf64_Ehdr)))
return ID_UNKNOWN;
if (!strncmp((char *)hdr->e_ident,ELFid,4)) {
/* ELF identification found */
if (hdr->e_ident[EI_CLASS]==class && hdr->e_ident[EI_DATA]==endian &&
hdr->e_ident[EI_VERSION]==(unsigned char)ver &&
read32(be,hdr->e_version)==ver && read16(be,hdr->e_machine)==machine) {
switch (read16(be,hdr->e_type)) {
case ET_REL:
return arflag ? ID_LIBARCH : ID_OBJECT;
case ET_EXEC:
if (arflag) { /* no executables in library archives */
error(40,name,ff->tname);
return ID_UNKNOWN;
}
return ID_EXECUTABLE;
case ET_DYN:
if (arflag) { /* no shared objects in library archives */
error(39,name,ff->tname);
return ID_UNKNOWN;
}
return ID_SHAREDOBJ;
default:
error(41,name,ff->tname); /* illegal fmt. / file corrupted */
break;
}
}
}
return ID_UNKNOWN;
}
bool elf_check_ar_type(struct FFFuncs *ff,const char *name,void *p,
unsigned char class,unsigned char endian,
uint32_t ver,int nmach,...)
/* check all library archive members before conversion */
{
struct Elf_CommonHdr *ehdr = (struct Elf_CommonHdr *)p;
bool be = (endian == ELFDATA2MSB);
bool valid = TRUE;
uint16_t m = read16(be,ehdr->e_machine);
va_list vl;
va_start(vl,nmach);
if (!strncmp((char *)ehdr->e_ident,ELFid,4)) {
/* ELF identification found */
if (ehdr->e_ident[EI_CLASS]==class && ehdr->e_ident[EI_DATA]==endian &&
ehdr->e_ident[EI_VERSION]==(unsigned char)ver &&
read32(be,ehdr->e_version)==ver && nmach>0) {
while (nmach--) {
if (va_arg(vl,int) == m)
m = 0;
}
if (m == 0) {
switch (read16(be,ehdr->e_type)) {
case ET_REL:
goto check_ar_exit;
case ET_EXEC: /* no executables in library archives */
error(40,name,ff->tname);
break;
case ET_DYN: /* no shared objects in library archives */
error(39,name,ff->tname);
break;
default: /* illegal fmt. / file corrupted */
break;
}
}
}
}
valid = FALSE;
check_ar_exit:
va_end(vl);
return valid;
}
void elf_check_offset(struct LinkFile *lf,char *tabname,void *data,lword size)
{
uint8_t *p = (uint8_t *)data;
if ((p < lf->data) || (p + size) > (lf->data + lf->length))
error(51,lf->pathname,tabname,lf->objname);
}
struct Section *elf_add_section(struct GlobalVars *gv,struct ObjectUnit *ou,
char *name,uint8_t *data,lword size,
uint32_t shtype,uint64_t shflags,uint8_t align)
{
struct LinkFile *lf = ou->lnkfile;
uint8_t type=ST_DATA,flags=0,prot=SP_READ;
if (gv->strip_symbols>=STRIP_DEBUG &&
(!strncmp(name,".debug",6) || !strncmp(name,".line",5) ||
!strncmp(name,".stab",5)))
return NULL; /* ignore debugging sections when -S or -s is given */
if (shtype == SHT_NOBITS) {
data = NULL;
type = ST_UDATA;
flags |= SF_UNINITIALIZED;
}
else {
if (data+size > lf->data+lf->length) /* illegal section offset */
error(49,lf->pathname,name,lf->objname);
}
if ((shflags & SHF_EXECINSTR) && data!=NULL) {
type = ST_CODE;
prot |= SP_EXEC;
}
if (shflags & SHF_WRITE)
prot |= SP_WRITE;
if (shflags & SHF_ALLOC)
flags |= SF_ALLOC;
if (!strncmp(name,".gnu.linkonce",13))
flags |= SF_LINKONCE;
return add_section(ou,name,data,size,type,flags,prot,align,FALSE);
}
void elf_add_symbol(struct GlobalVars *gv,struct ObjectUnit *ou,
char *symname,uint8_t flags,int shndx,uint32_t shtype,
uint8_t sttype,uint8_t stbind,lword value,uint32_t size)
{
struct LinkFile *lf = ou->lnkfile;
struct Section *sec;
uint8_t type = 0;
if (flags & SYMF_SHLIB) {
/* don't import linker symbols from shared objects - we make our own */
int i;
for (i=0; i<(sizeof(elf_symnames)/sizeof(elf_symnames[0])); i++) {
if (!strcmp(elf_symnames[i],symname))
return;
}
}
switch (shndx) {
case SHN_UNDEF:
sec = NULL; /* ignore xrefs for now */
break;
/* assign a section for ABS and COMMON symbols */
case SHN_ABS:
sec = abs_section(ou);
type = SYM_ABS;
break;
case SHN_COMMON:
sec = common_section(gv,ou);
type = SYM_COMMON;
break;
/* reloc symbols have a definite section to which they belong */
default:
if (shtype == SHT_DYNSYM) {
/* just put dynamic symbols into the first section -
the section index might be wrong in .dynsym */
sec = abs_section(ou);
}
else if (!(sec = find_sect_id(ou,shndx))) {
/* a section with this index doesn't exist! */
if (sttype != STT_SECTION)
error(53,lf->pathname,symname,lf->objname,shndx);
}
type = SYM_RELOC;
break;
}
if (sttype == STT_SECTION)
sec = NULL; /* ignore section defines - will be reproduced */
if (sec) {
if (sttype > STT_FILE) {
/* illegal symbol type */
error(54,lf->pathname,(int)sttype,symname,lf->objname);
sttype = STT_NOTYPE;
}
switch (stbind) {
case STB_LOCAL:
stbind = SYMB_LOCAL;
break;
case STB_GLOBAL:
stbind = SYMB_GLOBAL;
break;
case STB_WEAK:
stbind = SYMB_WEAK;
break;
default: /* illegal binding type */
error(55,lf->pathname,symname,stbind,lf->objname);
stbind = SYMB_LOCAL;
break;
}
/* add a new symbol definition */
if (stbind == SYMB_LOCAL)
addlocsymbol(gv,sec,symname,NULL,value,type,flags,sttype,size);
else
addsymbol(gv,sec,symname,NULL,value,type,flags,sttype,stbind,size,TRUE);
}
}
/*****************************************************************/
/* Link ELF */
/*****************************************************************/
int elf_targetlink(struct GlobalVars *gv,struct LinkedSection *ls,
struct Section *s)
/* returns 1, if target requires the combination of the two sections, */
/* returns -1, if target doesn't want to combine them, */
/* returns 0, if target doesn't care - standard linking rules are used. */
{
return 0;
}
struct Symbol *elf_makelnksym(struct GlobalVars *gv,int idx)
{
struct Symbol *sym = addlnksymbol(gv,elf_symnames[idx],0,SYM_ABS,
SYMF_LNKSYM,SYMI_OBJECT,SYMB_GLOBAL,0);
sym->extra = idx; /* for easy ident. in elf_setlnksym */
switch (idx) {
case SDABASE:
case SDA2BASE:
sym->type = SYM_RELOC;
sym->value = (lword)fff[gv->dest_format]->baseoff;
break;
case GLOBOFFSTAB:
sym->value = fff[gv->dest_format]->gotoff;
gv->got_base_name = elf_symnames[idx];
break;
case PROCLINKTAB:
gv->plt_base_name = elf_symnames[idx];
break;
}
return sym;
}
struct Symbol *elf_lnksym(struct GlobalVars *gv,struct Section *sec,
struct Reloc *xref)
/* Check for common ELF linker symbols. */
{
int i;
if (!gv->dest_object) {
for (i=0; i<(sizeof(elf_symnames)/sizeof(elf_symnames[0])); i++) {
if (!strcmp(elf_symnames[i],xref->xrefname))
return elf_makelnksym(gv,i); /* new linker symbol created */
}
}
return NULL;
}
void elf_setlnksym(struct GlobalVars *gv,struct Symbol *xdef)
/* Initialize common ELF linker symbol structure during resolve_xref() */
{
if (xdef->flags & SYMF_LNKSYM) {
struct LinkedSection *ls;
switch (xdef->extra) {
case SDABASE:
if (!(ls = find_lnksec(gv,sdata_name,ST_DATA,0,0,0)))
if (!(ls = find_lnksec(gv,sbss_name,ST_UDATA,0,0,0)))
ls = smalldata_section(gv);
xdef->relsect = (struct Section *)ls->sections.first;
break;
case SDA2BASE:
if (!(ls = find_lnksec(gv,sdata2_name,ST_DATA,0,0,0)))
if (!(ls = find_lnksec(gv,sbss2_name,ST_UDATA,0,0,0)))
ls = smalldata_section(gv);
xdef->relsect = (struct Section *)ls->sections.first;
break;
case CTORS:
if (ls = find_lnksec(gv,ctors_name,0,0,0,0)) {
xdef->type = SYM_RELOC;
xdef->relsect = (struct Section *)ls->sections.first;
}
break;
case DTORS:
if (ls = find_lnksec(gv,dtors_name,0,0,0,0)) {
xdef->type = SYM_RELOC;
xdef->relsect = (struct Section *)ls->sections.first;
}
break;
case CTOREND:
if (ls = find_lnksec(gv,ctors_name,0,0,0,0)) {
xdef->type = SYM_RELOC;
xdef->relsect = (struct Section *)ls->sections.last;
if (xdef->relsect->size >= 4)
xdef->value = xdef->relsect->size - 4;
}
break;
case DTOREND:
if (ls = find_lnksec(gv,dtors_name,0,0,0,0)) {
xdef->type = SYM_RELOC;
xdef->relsect = (struct Section *)ls->sections.last;
if (xdef->relsect->size >= 4)
xdef->value = xdef->relsect->size - 4;
}
break;
case GLOBOFFSTAB:
if (ls = find_lnksec(gv,got_name,0,0,0,0)) {
xdef->type = SYM_RELOC;
xdef->relsect = (struct Section *)ls->sections.first;
}
break;
case PROCLINKTAB:
if (ls = find_lnksec(gv,plt_name,0,0,0,0)) {
xdef->type = SYM_RELOC;
xdef->relsect = (struct Section *)ls->sections.first;
}
break;
case DYNAMICSYM:
if (ls = find_lnksec(gv,dyn_name,0,0,0,0)) {
xdef->value = ls->base;
}
/* @@@ when .dynamic was not created _DYNAMIC stays NULL - ok? */
break;
}
xdef->flags &= ~SYMF_LNKSYM; /* do not init again */
}
}
struct Section *elf_dyntable(struct GlobalVars *gv,
unsigned long initial_size,
unsigned long initial_offset,
uint8_t sectype,uint8_t secflags,
uint8_t secprot,int type)
/* return got/plt section, create new when missing */
{
static const char fn[] = "elf_dyntable():";
static const char *secname[] = { NULL, got_name, plt_name };
struct Section *sec,**secp;
int symidx = -1;
switch (type) {
case GOT_ENTRY:
secp = &gotsec;
symidx = GLOBOFFSTAB;
break;
case PLT_ENTRY:
secp = &pltsec;
symidx = PROCLINKTAB;
break;
default:
ierror("%s wrong type: %d",fn,type);
break;
}
if (sec = *secp)
return sec;
if (gv->dynobj == NULL)
ierror("%s no dynobj",fn);
/* Section does not exist - create it.
The offset field is used for the next table entry offset. */
sec = add_section(gv->dynobj,(char *)secname[type],NULL,initial_size,
sectype,secflags,secprot,gv->ptr_alignment,TRUE);
sec->offset = initial_offset;
*secp = sec;
/* create _GLOBAL_OFFSET_TABLE_ or _PROCEDURE_LINKAGE_TABLE_ linker symbol */
if (symidx >= 0) {
if (!findlnksymbol(gv,elf_symnames[symidx]))
elf_makelnksym(gv,symidx);
}
return sec;
}
void elf_adddynsym(struct Symbol *sym)
{
if (sym->flags & SYMF_DYNIMPORT) {
elf_addsym(&elfdsymlist,sym->name,0,0,STB_GLOBAL,STT_NOTYPE,SHN_UNDEF);
}
else if (sym->flags & SYMF_DYNEXPORT) {
struct DynSymNode *dsn = alloc(sizeof(struct DynSymNode));
/* section index and value need to be fixed later for these entries! */
dsn->idx = elf_addsym(&elfdsymlist,sym->name,0,sym->size,
elf_getbind(sym),elf_getinfo(sym),0);
dsn->sym = sym;
addtail(&elfdynsymlist,&dsn->n);
}
else
ierror("elf_adddynsym(): <%s> was not flagged as dynamic",sym->name);
}
void elf_dynreloc(struct GlobalVars *gv,struct ObjectUnit *ou,
struct Reloc *r,int relafmt,size_t elfrelsize)
{
const char *secname;
struct Section **secp;
uint8_t dynflag = SYMF_DYNIMPORT;
switch (r->rtype) {
case R_COPY:
dynflag = SYMF_DYNEXPORT;
/* fall through */
case R_GLOBDAT:
case R_LOADREL:
secp = &elfdynrelocs;
secname = dynrel_name[relafmt?1:0];
r->flags |= RELF_DYN;
break;
case R_JMPSLOT:
secp = &elfpltrelocs;
secname = pltrel_name[relafmt?1:0];
r->flags |= RELF_PLT;
break;
case R_ABS:
return;
default:
ierror("elf_dynreloc(): wrong rtype %s (%d)",
reloc_name[r->rtype],(int)r->rtype);
break;
}
/* make sure that dynamic relocation section exists */
if (*secp == NULL)
*secp = add_section(ou,secname,NULL,0,ST_DATA,SF_ALLOC,SP_READ,
gv->ptr_alignment,TRUE);
/* increase size for new entry */
(*secp)->size += elfrelsize;
/* allocate referenced symbol in .dynsym and .dynstr */
if (r->xrefname) {
struct Symbol *sym = r->relocsect.symbol;
if (!(sym->flags & SYMF_DYNLINK)) {
/* add symbol to .dynsym symbol list, if not already present */
sym->flags |= dynflag;
elf_adddynsym(sym);
}
}
}
struct Section *elf_initdynlink(struct GlobalVars *gv)
{
struct ObjectUnit *ou = gv->dynobj;
struct Symbol *sym;
struct Section *dynsec;
/* set endianness for output file */
elf_endianness = fff[gv->dest_format]->endianness;
/* init dynamic symbol list */
initlist(&elfdynsymlist);
/* allocate .interp section for dynamically linked executables only */
if (!gv->dest_sharedobj)
add_section(ou,".interp",(uint8_t *)gv->interp_path,
strlen(gv->interp_path)+1,ST_DATA,SF_ALLOC,SP_READ,0,TRUE);
/* .hash, .dynsym, .dynstr and .dynamic are always present.
Set them to an initial size. They will grow with dynamic symbols added. */
add_section(ou,hash_name,NULL,0,ST_DATA,SF_ALLOC,SP_READ,
gv->ptr_alignment,TRUE);
add_section(ou,dynsym_name,NULL,0,ST_DATA,SF_ALLOC,SP_READ,
gv->ptr_alignment,TRUE);
add_section(ou,dynstr_name,NULL,0,ST_DATA,SF_ALLOC,SP_READ,0,TRUE);
dynsec = add_section(ou,dyn_name,NULL,0,ST_DATA,SF_ALLOC,
SP_READ|SP_WRITE,gv->ptr_alignment,TRUE);
/* assign symbol _DYNAMIC the address of the .dynamic section */
sym = elf_makelnksym(gv,DYNAMICSYM);
sym->flags |= SYMF_DYNEXPORT;
elf_adddynsym(sym);
return dynsec;
}
struct Symbol *elf_pltgotentry(struct GlobalVars *gv,struct Section *sec,
DynArg a,uint8_t entrysymtype,
unsigned long offsadd,unsigned long sizeadd,
int etype,bool relaflag,
size_t relocsize,size_t addrsize)
/* 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. */
{
const char *fn = "elf_pltgotentry():";
char entryname[MAXLEN];
struct Symbol *tabsym;
struct Section *refsec;
unsigned long refoffs;
/* determine reference section and offset of ext. symbol or local reloc */
if (etype == GOT_LOCAL) {
refsec = a.rel->relocsect.ptr;
refoffs = a.rel->offset;
}
else {
refsec = a.sym->relsect;
refoffs = (unsigned long)a.sym->value;
}
/* generate internal symbol name for this reference */
snprintf(entryname,MAXLEN," %s@%lx@%lx",
sec->name,(unsigned long)refsec,refoffs);
/* create internal symbol, or return old one, when already present */
tabsym = addsymbol(gv,sec,allocstring(entryname),NULL,(lword)sec->offset,
SYM_RELOC,0,entrysymtype,SYMB_LOCAL,offsadd,FALSE);
/* tabsym is NULL when it was just created, otherwise we already got it */
if (tabsym == NULL) {
static uint8_t dyn_reloc_types[] = { R_NONE,R_GLOBDAT,R_JMPSLOT,R_COPY };
struct Reloc *r;
tabsym = findlocsymbol(gv,sec->obj,entryname);
if (tabsym == NULL) {
ierror("%s %s-symbol referring to %s+%lx disappeared",
fn,sec->name,refsec->name,refoffs);
}
/* create a relocation for the new entry */
if (etype == GOT_LOCAL) {
/* local symbol: GOT relocation can be resolved now */
r = newreloc(gv,sec,NULL,refsec,0,sec->offset,R_ABS,(lword)refoffs);
}
else {
/* we need a dynamic linker relocation at the entry's offset */
r = newreloc(gv,sec,a.sym->name,NULL,0,sec->offset,
dyn_reloc_types[etype],0);
r->relocsect.symbol = a.sym; /* resolve with external symbol */
/* Possible enhancement: Find out whether referenced symbol resides
in an uninitialized section, without a relocation, then we don't
need an R_COPY relocation either! */
}
addreloc(sec,r,0,addrsize,-1); /* size,mask only important for R_ABS */
elf_dynreloc(gv,gv->dynobj,r,relaflag,relocsize);
/* increase offset and size counters of table-section */
sec->offset += offsadd;
sec->size += sizeadd;
}
return tabsym;
}
struct Symbol *elf_bssentry(struct GlobalVars *gv,const char *secname,
struct Symbol *xdef,bool relaflag,
size_t relocsize,size_t addrsize)
/* 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. */
{
struct Symbol *newxdef;
if (gv->dynobj == NULL)
ierror("elf_bssentry(): no dynobj");
newxdef = bss_entry(gv,gv->dynobj,secname,xdef);
if (newxdef) {
/* entry in BSS was done, so we need a R_COPY relocation */
struct Reloc *r;
xdef = newxdef;
r = newreloc(gv,xdef->relsect,xdef->name,NULL,0,0,R_COPY,0);
r->relocsect.symbol = xdef;
addreloc(xdef->relsect,r,0,addrsize,-1); /* mask/size irrel. for R_COPY */
elf_dynreloc(gv,xdef->relsect->obj,r,relaflag,relocsize);
}
return xdef;
}
size_t elf_num_buckets(size_t symcount)
/* determine optimal number of buckets in dynamic symbol hash table */
{
int i;
size_t best_num;
for (i=0; elf_buckets[i]; i++) {
best_num = elf_buckets[i];
if (symcount < elf_buckets[i+1])
break;
}
return best_num;
}
void elf_putsymtab(uint8_t *p,struct SymTabList *sl)
/* write all SymTabList nodes sequentially into memory */
{
size_t sz = sl->elfsymsize;
struct SymbolNode *sym;
while (sym = (struct SymbolNode *)remhead(&sl->l)) {
memcpy(p,sym->elfsym,sz);
p += sz;
}
}
/*****************************************************************/
/* Write ELF */
/*****************************************************************/
unsigned long elf_numsegments(struct GlobalVars *gv)
{
int segcnt = 0;
struct Phdr *p = gv->phdrlist;
while (p) {
if (p->flags & PHDR_USED)
segcnt++;
p = p->next;
}
return segcnt;
}
uint8_t elf_getinfo(struct Symbol *sym)
{
uint8_t type = STT_NOTYPE;
switch (sym->info) {
case SYMI_NOTYPE:
break; /* @@@ Is this allowed? */
case SYMI_OBJECT:
type = STT_OBJECT;
break;
case SYMI_FUNC:
type = STT_FUNC;
break;
case SYMI_SECTION:
ierror("elf_getinfo(): STT_SECTION symbol detected");
type = STT_SECTION;
break;
case SYMI_FILE:
type = STT_FILE;
break;
default:
ierror("elf_getinfo(): Illegal symbol info: %d",(int)sym->info);
break;
}
return type;
}
uint8_t elf_getbind(struct Symbol *sym)
{
uint8_t bind = STB_GLOBAL;
switch (sym->bind) {
case SYMB_LOCAL:
bind = STB_LOCAL;
break;
case SYMB_GLOBAL:
bind = STB_GLOBAL;
break;
case SYMB_WEAK:
bind = STB_WEAK;
break;
default:
ierror("elf_getbind(): Illegal symbol binding: %d",(int)sym->bind);
break;
}
return bind;
}
uint16_t elf_getshndx(struct GlobalVars *gv,
struct Symbol *sym,uint8_t symtype)
{
uint16_t shndx = SHN_UNDEF;
switch (sym->type) {
case SYM_INDIR:
/* can't reproduce unsupported symbol type */
error(33,fff[gv->dest_format]->tname,sym->name,sym_bind[sym->bind],
sym_type[sym->type],sym_info[sym->info]);
case SYM_UNDEF:
shndx = SHN_UNDEF;
break;
case SYM_ABS:
shndx = SHN_ABS;
break;
case SYM_RELOC:
if (symtype > STT_FUNC)
ierror("elf_getshndx(): %s is relocatable, but not a "
"function or object (type %d)",sym->name,(int)symtype);
shndx = (uint16_t)sym->relsect->lnksec->index;
break;
case SYM_COMMON:
shndx = SHN_COMMON;
break;
default:
ierror("elf_getshndx(): Illegal symbol type: %d",(int)sym->type);
break;
}
return shndx;
}
void elf_putstrtab(uint8_t *p,struct StrTabList *sl)
/* write all StrTabList nodes sequentially into memory */
{
struct StrTabNode *stn;
while (stn = (struct StrTabNode *)remhead(&sl->l)) {
const char *s;
char c;
s = stn->str;
do {
c = *s++;
*p++ = c;
} while (c);
}
}
uint32_t elf_addstrlist(struct StrTabList *sl,const char *s)
/* add a new string to an ELF string table and return its index */
{
struct StrTabNode **chain,*sn;
if (sl->hashtab == NULL) {
/* initialize an unused string list */
initlist(&sl->l);
sl->hashtab = alloczero(sl->htabsize * sizeof(struct StrTabNode *));
elf_addstrlist(sl,noname);
}
chain = &sl->hashtab[elf_hash(s) % sl->htabsize];
/* search string in hash table */
while (sn = *chain) {
if (!strcmp(s,sn->str))
return sn->index; /* it's already in, return index */
chain = &sn->hashchain;
}
/* new string table entry */
*chain = sn = alloc(sizeof(struct StrTabNode));
sn->hashchain = NULL;
sn->str = s;
sn->index = sl->nextindex;
addtail(&sl->l,&sn->n);
sl->nextindex += (uint32_t)strlen(s) + 1;
return sn->index;
}
uint32_t elf_addshdrstr(const char *s)
{
return elf_addstrlist(&elfshstrlist,s);
}
uint32_t elf_addstr(const char *s)
{
return elf_addstrlist(&elfstringlist,s);
}
uint32_t elf_adddynstr(const char *s)
{
return elf_addstrlist(&elfdstrlist,s);
}
uint32_t elf_addsym(struct SymTabList *sl,const char *name,uint64_t value,
uint64_t size,uint8_t bind,uint8_t type,uint16_t shndx)
{
struct SymbolNode **chain,*sn;
void *sym;
if (name == NULL) /* do nothing, return first index */
return 0;
chain = &sl->hashtab[elf_hash(name) % sl->htabsize];
while (sn = *chain)
chain = &sn->hashchain;
/* new symbol table entry */
*chain = sn = alloc(sizeof(struct SymbolNode));
sn->hashchain = NULL;
sn->name = name;
sn->index = sl->nextindex++;
sn->shndx = shndx;
sn->elfsym = sym = alloc(sl->elfsymsize);
addtail(&sl->l,&sn->n);
/* initialize ELF symbol */
sl->initsym(sym,elf_addstrlist(sl->strlist,name),value,size,
bind,type,shndx,elf_endianness==_BIG_ENDIAN_);
return sn->index;
}
void elf_initsymlist(struct SymTabList *sl,struct StrTabList *strl,
size_t tabsize,size_t symsize,
void (*init)(void *,uint32_t,uint64_t,uint64_t,
uint8_t,uint8_t,uint16_t,bool))
{
initlist(&sl->l);
sl->htabsize = tabsize;
sl->hashtab = alloczero(tabsize * sizeof(struct SymbolNode *));
sl->strlist = strl;
sl->elfsymsize = symsize;
sl->initsym = init;
sl->nextindex = sl->globalindex = 0;
elf_addsym(sl,noname,0,0,0,0,SHN_UNDEF);
}
struct SymbolNode *elf_findSymNode(struct SymTabList *sl,const char *name)
/* Find an ELF symbol node by its name.
Return pointer to it, or NULL when not found. */
{
struct SymbolNode **chain = &sl->hashtab[elf_hash(name) % sl->htabsize];
struct SymbolNode *sym;
while (sym = *chain) {
if (!strcmp(name,sym->name))
return sym;
chain = &sym->hashchain;
}
return NULL;
}
static uint32_t elf_findsymidx(struct SymTabList *sl,const char *name,
uint16_t shndx)
/* find an ELF symbol by its name and shndx */
/* return its symbol table index, index=0 means 'not found' */
{
struct SymbolNode **chain = &sl->hashtab[elf_hash(name) % sl->htabsize];
struct SymbolNode *sym;
while (sym = *chain) {
if (!strcmp(name,sym->name) && sym->shndx==shndx)
return sym->index;
chain = &sym->hashchain;
}
return 0;
}
uint32_t elf_extsymidx(struct SymTabList *sl,const char *name)
/* return index of external symbol with name from SymTabList */
{
uint32_t symidx;
if (!(symidx = elf_findsymidx(sl,name,SHN_COMMON)))
if (!(symidx = elf_findsymidx(sl,name,SHN_UNDEF)))
symidx = elf_addsym(sl,name,0,0,STB_GLOBAL,STT_NOTYPE,SHN_UNDEF);
return symidx;
}
void elf_ident(void *p,bool be,uint8_t class,uint16_t type,uint16_t mach)
{
struct Elf_CommonHdr *hdr = (struct Elf_CommonHdr *)p;
strncpy((char *)hdr->e_ident,ELFid,4);
hdr->e_ident[EI_CLASS] = class;
hdr->e_ident[EI_DATA] = be ? ELFDATA2MSB : ELFDATA2LSB;
hdr->e_ident[EI_VERSION] = ELF_VER;
memset(&hdr->e_ident[EI_PAD],0,EI_NIDENT-EI_PAD);
write16(be,hdr->e_type,type);
write16(be,hdr->e_machine,mach);
write32(be,hdr->e_version,ELF_VER);
}
void elf_addsymlist(struct GlobalVars *gv,struct SymTabList *sl,
uint8_t bind,uint8_t type)
/* add all symbols with specified bind and type to the ELF symbol list */
{
struct LinkedSection *ls = (struct LinkedSection *)gv->lnksec.first;
struct LinkedSection *nextls;
struct Symbol *nextsym,*sym;