3539
3540
* Generic ELF functions
3543
findElfSection ( void* objImage, Elf_Word sh_type )
3545
char* ehdrC = (char*)objImage;
3546
Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3547
Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
3548
char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3552
for (i = 0; i < ehdr->e_shnum; i++) {
3553
if (shdr[i].sh_type == sh_type
3554
/* Ignore the section header's string table. */
3555
&& i != ehdr->e_shstrndx
3556
/* Ignore string tables named .stabstr, as they contain
3558
&& 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3560
ptr = ehdrC + shdr[i].sh_offset;
3568
3544
ocVerifyImage_ELF ( ObjectCode* oc )
3653
3628
ehdrC + shdr[i].sh_offset,
3654
3629
ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
3656
if (shdr[i].sh_type == SHT_REL) {
3657
IF_DEBUG(linker,debugBelch("Rel " ));
3658
} else if (shdr[i].sh_type == SHT_RELA) {
3659
IF_DEBUG(linker,debugBelch("RelA " ));
3661
IF_DEBUG(linker,debugBelch(" "));
3631
#define SECTION_INDEX_VALID(ndx) (ndx > SHN_UNDEF && ndx < ehdr->e_shnum)
3633
switch (shdr[i].sh_type) {
3637
IF_DEBUG(linker,debugBelch( shdr[i].sh_type == SHT_REL ? "Rel " : "RelA "));
3639
if (!SECTION_INDEX_VALID(shdr[i].sh_link)) {
3640
if (shdr[i].sh_link == SHN_UNDEF)
3641
errorBelch("\n%s: relocation section #%d has no symbol table\n"
3642
"This object file has probably been fully striped. "
3643
"Such files cannot be linked.\n",
3644
oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
3646
errorBelch("\n%s: relocation section #%d has an invalid link field (%d)\n",
3647
oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
3648
i, shdr[i].sh_link);
3651
if (shdr[shdr[i].sh_link].sh_type != SHT_SYMTAB) {
3652
errorBelch("\n%s: relocation section #%d does not link to a symbol table\n",
3653
oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
3656
if (!SECTION_INDEX_VALID(shdr[i].sh_info)) {
3657
errorBelch("\n%s: relocation section #%d has an invalid info field (%d)\n",
3658
oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
3659
i, shdr[i].sh_info);
3665
IF_DEBUG(linker,debugBelch("Sym "));
3667
if (!SECTION_INDEX_VALID(shdr[i].sh_link)) {
3668
errorBelch("\n%s: symbol table section #%d has an invalid link field (%d)\n",
3669
oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
3670
i, shdr[i].sh_link);
3673
if (shdr[shdr[i].sh_link].sh_type != SHT_STRTAB) {
3674
errorBelch("\n%s: symbol table section #%d does not link to a string table\n",
3675
oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
3680
case SHT_STRTAB: IF_DEBUG(linker,debugBelch("Str ")); break;
3681
default: IF_DEBUG(linker,debugBelch(" ")); break;
3663
3683
if (sh_strtab) {
3664
3684
IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
3668
IF_DEBUG(linker,debugBelch( "\nString tables" ));
3688
IF_DEBUG(linker,debugBelch( "\nString tables\n" ));
3671
3690
for (i = 0; i < ehdr->e_shnum; i++) {
3672
3691
if (shdr[i].sh_type == SHT_STRTAB
3676
3695
debugging info. */
3677
3696
&& 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3679
IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
3680
strtab = ehdrC + shdr[i].sh_offset;
3698
IF_DEBUG(linker,debugBelch(" section %d is a normal string table\n", i ));
3685
errorBelch("%s: no string tables, or too many", oc->fileName);
3703
IF_DEBUG(linker,debugBelch(" no normal string tables (potentially, but not necessarily a problem)\n"));
3690
IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
3707
IF_DEBUG(linker,debugBelch( "Symbol tables\n" ));
3691
3708
for (i = 0; i < ehdr->e_shnum; i++) {
3692
3709
if (shdr[i].sh_type != SHT_SYMTAB) continue;
3693
3710
IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
3730
3747
IF_DEBUG(linker,debugBelch(" " ));
3732
IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
3749
IF_DEBUG(linker,debugBelch("name=%s\n",
3750
ehdrC + shdr[shdr[i].sh_link].sh_offset
3751
+ stab[j].st_name ));
3736
3755
if (nsymtabs == 0) {
3737
errorBelch("%s: didn't find any symbol tables", oc->fileName);
3756
// Not having a symbol table is not in principle a problem.
3757
// When an object file has no symbols then the 'strip' program
3758
// typically will remove the symbol table entirely.
3759
IF_DEBUG(linker,debugBelch(" no symbol tables (potentially, but not necessarily a problem)\n"));
3783
3804
char* ehdrC = (char*)(oc->image);
3784
3805
Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3785
char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
3786
3807
Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3788
3809
ASSERT(symhash != NULL);
3791
errorBelch("%s: no strtab", oc->fileName);
3796
3812
for (i = 0; i < ehdr->e_shnum; i++) {
3797
3813
/* Figure out what kind of section it is. Logic derived from
3927
3944
relocations appear to be of this form. */
3929
3946
do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3930
Elf_Shdr* shdr, int shnum,
3931
Elf_Sym* stab, char* strtab )
3947
Elf_Shdr* shdr, int shnum )
3935
3951
Elf_Word* targ;
3936
3952
Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3937
3955
int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3938
3956
int target_shndx = shdr[shnum].sh_info;
3939
3957
int symtab_shndx = shdr[shnum].sh_link;
3958
int strtab_shndx = shdr[symtab_shndx].sh_link;
3941
3960
stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3961
strtab= (char*) (ehdrC + shdr[ strtab_shndx ].sh_offset);
3942
3962
targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3943
IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3944
target_shndx, symtab_shndx ));
3963
IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d and strtab %d\n",
3964
target_shndx, symtab_shndx, strtab_shndx ));
3946
3966
/* Skip sections that we're not interested in. */
4027
4047
sparc-solaris relocations appear to be of this form. */
4029
4049
do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
4030
Elf_Shdr* shdr, int shnum,
4031
Elf_Sym* stab, char* strtab )
4050
Elf_Shdr* shdr, int shnum )
4034
4053
char *symbol = NULL;
4036
4055
Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
4037
4058
int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
4038
4059
int target_shndx = shdr[shnum].sh_info;
4039
4060
int symtab_shndx = shdr[shnum].sh_link;
4061
int strtab_shndx = shdr[symtab_shndx].sh_link;
4041
4063
stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
4064
strtab= (char*) (ehdrC + shdr[ strtab_shndx ].sh_offset);
4042
4065
targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
4043
4066
IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
4044
4067
target_shndx, symtab_shndx ));
4308
4331
ocResolve_ELF ( ObjectCode* oc )
4312
Elf_Sym* stab = NULL;
4313
4334
char* ehdrC = (char*)(oc->image);
4314
4335
Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
4315
4336
Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
4317
/* first find "the" symbol table */
4318
stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
4320
/* also go find the string table */
4321
strtab = findElfSection ( ehdrC, SHT_STRTAB );
4323
if (stab == NULL || strtab == NULL) {
4324
errorBelch("%s: can't find string or symbol table", oc->fileName);
4328
4338
/* Process the relocation sections. */
4329
4339
for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
4330
4340
if (shdr[shnum].sh_type == SHT_REL) {
4331
ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
4332
shnum, stab, strtab );
4341
ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr, shnum );
4333
4342
if (!ok) return ok;
4336
4345
if (shdr[shnum].sh_type == SHT_RELA) {
4337
ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
4338
shnum, stab, strtab );
4346
ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr, shnum );
4339
4347
if (!ok) return ok;
4369
4377
if( i == ehdr->e_shnum )
4371
errorBelch( "This ELF file contains no symtab" );
4379
// Not having a symbol table is not in principle a problem.
4380
// When an object file has no symbols then the 'strip' program
4381
// typically will remove the symbol table entirely.
4382
IF_DEBUG(linker, debugBelch( "The ELF file %s contains no symtab\n",
4383
oc->archiveMemberName ? oc->archiveMemberName : oc->fileName ));
4375
4387
if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )