1
0
mirror of https://git.FreeBSD.org/src.git synced 2025-01-28 16:43:09 +00:00

Clean up the code some more. Unify the text/data (progbits) and bss

(nobits) tables to simplify some code.  Try and shorten some of the very
wide lines.  Somewhere along the way, I think I fixed the memory
corruption that caused panics after going multiuser.
This commit is contained in:
Peter Wemm 2004-05-17 21:20:23 +00:00
parent 910bb7dbe9
commit 2094780104
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=129362

View File

@ -60,40 +60,30 @@ __FBSDID("$FreeBSD$");
typedef struct { typedef struct {
void *addr; void *addr;
Elf_Off fileoff; Elf_Off size;
Elf_Off filesz;
int align; int align;
int flags; int flags;
int sec; /* Original section */ int sec; /* Original section */
char *name; char *name;
} Elf_progent; } Elf_progent;
typedef struct {
void *addr;
Elf_Off memsz;
int align;
int flags;
int sec; /* Original section */
char *name;
} Elf_nobitent;
typedef struct { typedef struct {
Elf_Rel *rel; Elf_Rel *rel;
Elf_Off fileoff; int nrel;
Elf_Off filesz;
int sec; int sec;
} Elf_relent; } Elf_relent;
typedef struct { typedef struct {
Elf_Rela *rela; Elf_Rela *rela;
Elf_Off fileoff; int nrela;
Elf_Off filesz;
int sec; int sec;
} Elf_relaent; } Elf_relaent;
typedef struct elf_file { typedef struct elf_file {
struct linker_file lf; /* Common fields */ struct linker_file lf; /* Common fields */
char pad0[80];
caddr_t address; /* Relocation address */ caddr_t address; /* Relocation address */
vm_object_t object; /* VM object to hold file pages */ vm_object_t object; /* VM object to hold file pages */
Elf_Shdr *e_shdr; Elf_Shdr *e_shdr;
@ -101,9 +91,6 @@ typedef struct elf_file {
Elf_progent *progtab; Elf_progent *progtab;
int nprogtab; int nprogtab;
Elf_nobitent *nobittab;
int nnobittab;
Elf_relaent *relatab; Elf_relaent *relatab;
int nrela; int nrela;
@ -117,6 +104,7 @@ typedef struct elf_file {
caddr_t shstrtab; /* Section name string table */ caddr_t shstrtab; /* Section name string table */
long shstrcnt; /* number of bytes in string table */ long shstrcnt; /* number of bytes in string table */
char pad2[80];
} *elf_file_t; } *elf_file_t;
@ -203,7 +191,7 @@ link_elf_load_file(linker_class_t cls, const char *filename,
Elf_Ehdr *hdr; Elf_Ehdr *hdr;
Elf_Shdr *shdr; Elf_Shdr *shdr;
int nbytes, i; int nbytes, i;
caddr_t mapbase; vm_offset_t mapbase;
size_t mapsize; size_t mapsize;
int error = 0; int error = 0;
int resid, flags; int resid, flags;
@ -213,7 +201,7 @@ link_elf_load_file(linker_class_t cls, const char *filename,
int symstrindex; int symstrindex;
int shstrindex; int shstrindex;
int nsym; int nsym;
int pb, nb, rl, ra; int pb, rl, ra;
int alignmask; int alignmask;
GIANT_REQUIRED; GIANT_REQUIRED;
@ -287,7 +275,6 @@ link_elf_load_file(linker_class_t cls, const char *filename,
} }
ef = (elf_file_t) lf; ef = (elf_file_t) lf;
ef->nprogtab = 0; ef->nprogtab = 0;
ef->nnobittab = 0;
ef->e_shdr = 0; ef->e_shdr = 0;
ef->nrel = 0; ef->nrel = 0;
ef->nrela = 0; ef->nrela = 0;
@ -299,7 +286,7 @@ link_elf_load_file(linker_class_t cls, const char *filename,
error = ENOEXEC; error = ENOEXEC;
goto out; goto out;
} }
shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO); shdr = malloc(nbytes, M_LINKER, M_WAITOK);
if (shdr == NULL) { if (shdr == NULL) {
error = ENOMEM; error = ENOMEM;
goto out; goto out;
@ -321,10 +308,8 @@ link_elf_load_file(linker_class_t cls, const char *filename,
for (i = 0; i < hdr->e_shnum; i++) { for (i = 0; i < hdr->e_shnum; i++) {
switch (shdr[i].sh_type) { switch (shdr[i].sh_type) {
case SHT_PROGBITS: case SHT_PROGBITS:
ef->nprogtab++;
break;
case SHT_NOBITS: case SHT_NOBITS:
ef->nnobittab++; ef->nprogtab++;
break; break;
case SHT_SYMTAB: case SHT_SYMTAB:
nsym++; nsym++;
@ -341,7 +326,7 @@ link_elf_load_file(linker_class_t cls, const char *filename,
break; break;
} }
} }
if (ef->nprogtab == 0 && ef->nnobittab == 0) { if (ef->nprogtab == 0) {
link_elf_error("file has no contents"); link_elf_error("file has no contents");
error = ENOEXEC; error = ENOEXEC;
goto out; goto out;
@ -360,52 +345,87 @@ link_elf_load_file(linker_class_t cls, const char *filename,
} }
/* Allocate space for tracking the load chunks */ /* Allocate space for tracking the load chunks */
/* XXX - maybe unneeded. might be able to use the shdr directly */
if (ef->nprogtab != 0) if (ef->nprogtab != 0)
ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab), M_LINKER, M_WAITOK); ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
if (ef->nnobittab != 0) M_LINKER, M_WAITOK | M_ZERO);
ef->nobittab = malloc(ef->nnobittab * sizeof(*ef->nobittab), M_LINKER, M_WAITOK);
if (ef->nrel != 0) if (ef->nrel != 0)
ef->reltab = malloc(ef->nrel * sizeof(*ef->reltab), M_LINKER, M_WAITOK); ef->reltab = malloc(ef->nrel * sizeof(*ef->reltab), M_LINKER,
M_WAITOK | M_ZERO);
if (ef->nrela != 0) if (ef->nrela != 0)
ef->relatab = malloc(ef->nrela * sizeof(*ef->relatab), M_LINKER, M_WAITOK); ef->relatab = malloc(ef->nrela * sizeof(*ef->relatab), M_LINKER,
/* XXX check for failures */ M_WAITOK | M_ZERO);
if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
(ef->nrel != 0 && ef->reltab == NULL) ||
(ef->nrela != 0 && ef->relatab == NULL)) {
error = ENOMEM;
goto out;
}
/* Space for symbol table */ if (symtabindex == -1)
panic("lost symbol table index");
/* Allocate space for and load the symbol table */
ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK); ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
if (ef->ddbsymtab == NULL) {
error = ENOMEM;
goto out;
}
error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
&resid, td);
if (error)
goto out;
if (resid != 0){
error = EINVAL;
goto out;
}
if (symstrindex == -1)
panic("lost symbol string index");
/* Allocate space for and load the symbol strings */
ef->ddbstrcnt = shdr[symstrindex].sh_size; ef->ddbstrcnt = shdr[symstrindex].sh_size;
ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK); ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
if (ef->ddbstrtab == NULL) {
if (ef->ddbsymtab == NULL || ef->ddbstrtab == NULL) {
error = ENOMEM; error = ENOMEM;
goto out; goto out;
} }
error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
&resid, td);
if (error)
goto out;
if (resid != 0){
error = EINVAL;
goto out;
}
/* Do we have a string table for the section names? */ /* Do we have a string table for the section names? */
shstrindex = -1; shstrindex = -1;
if (hdr->e_shstrndx != 0 && shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) { if (hdr->e_shstrndx != 0 &&
shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
shstrindex = hdr->e_shstrndx; shstrindex = hdr->e_shstrndx;
ef->shstrcnt = shdr[shstrindex].sh_size; ef->shstrcnt = shdr[shstrindex].sh_size;
ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER, M_WAITOK); ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
M_WAITOK);
if (ef->shstrtab == NULL) { if (ef->shstrtab == NULL) {
error = ENOMEM; error = ENOMEM;
goto out; goto out;
} }
error = vn_rdwr(UIO_READ, nd.ni_vp, error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
ef->shstrtab, shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset, shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
&resid, td); &resid, td);
if (error) if (error)
goto out; goto out;
if (resid != 0){
error = EINVAL;
goto out;
}
} }
/* Size code/data(progbits) and bss(nobits). allocate space for relocs */ /* Size up code/data(progbits) and bss(nobits). */
pb = 0;
nb = 0;
rl = 0;
ra = 0;
alignmask = 0; alignmask = 0;
for (i = 0; i < hdr->e_shnum; i++) { for (i = 0; i < hdr->e_shnum; i++) {
switch (shdr[i].sh_type) { switch (shdr[i].sh_type) {
@ -414,147 +434,130 @@ link_elf_load_file(linker_class_t cls, const char *filename,
alignmask = shdr[i].sh_addralign - 1; alignmask = shdr[i].sh_addralign - 1;
mapsize += alignmask; mapsize += alignmask;
mapsize &= ~alignmask; mapsize &= ~alignmask;
break;
}
switch (shdr[i].sh_type) {
case SHT_PROGBITS:
ef->progtab[pb].addr = (void *)(uintptr_t)mapsize;
ef->progtab[pb].fileoff = shdr[i].sh_offset;
ef->progtab[pb].filesz = shdr[i].sh_size;
ef->progtab[pb].align = shdr[i].sh_addralign;
ef->progtab[pb].sec = i;
if (ef->shstrtab && shdr[i].sh_name != 0)
ef->progtab[pb].name = ef->shstrtab + shdr[i].sh_name;
else
ef->progtab[pb].name = "<<PROGBITS>>";
mapsize += shdr[i].sh_size; mapsize += shdr[i].sh_size;
pb++;
break;
case SHT_NOBITS:
ef->nobittab[nb].addr = (void *)(uintptr_t)mapsize;
ef->nobittab[nb].memsz = shdr[i].sh_size;
ef->nobittab[nb].align = shdr[i].sh_addralign;
ef->nobittab[nb].sec = i;
if (ef->shstrtab && shdr[i].sh_name != 0)
ef->nobittab[nb].name = ef->shstrtab + shdr[i].sh_name;
else
ef->nobittab[nb].name = "<<NOBITS>>";
mapsize += shdr[i].sh_size;
nb++;
break;
case SHT_REL:
ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
ef->reltab[rl].fileoff = shdr[i].sh_offset;
ef->reltab[rl].filesz = shdr[i].sh_size;
ef->reltab[rl].sec = shdr[i].sh_info;
rl++;
break;
case SHT_RELA:
ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
ef->relatab[ra].fileoff = shdr[i].sh_offset;
ef->relatab[ra].filesz = shdr[i].sh_size;
ef->relatab[ra].sec = shdr[i].sh_info;
ra++;
break; break;
} }
} }
if (pb != ef->nprogtab)
panic("lots progbits");
if (nb != ef->nnobittab)
panic("lots nobits");
if (rl != ef->nrel)
panic("lots rel");
if (ra != ef->nrela)
panic("lots rela");
/* /*
* We know how much space we need for the text/data/bss/etc. * We know how much space we need for the text/data/bss/etc.
* This stuff needs to be in a single chunk so that profiling etc * This stuff needs to be in a single chunk so that profiling etc
* can get the bounds and gdb can associate offsets with modules * can get the bounds and gdb can associate offsets with modules
*/ */
ef->object = vm_object_allocate(OBJT_DEFAULT, round_page(mapsize) >> PAGE_SHIFT); ef->object = vm_object_allocate(OBJT_DEFAULT,
round_page(mapsize) >> PAGE_SHIFT);
if (ef->object == NULL) { if (ef->object == NULL) {
error = ENOMEM; error = ENOMEM;
goto out; goto out;
} }
vm_object_reference(ef->object); vm_object_reference(ef->object);
ef->address = (caddr_t) vm_map_min(kernel_map); ef->address = (caddr_t) vm_map_min(kernel_map);
error = vm_map_find(kernel_map, ef->object, 0, error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
(vm_offset_t *) &ef->address, mapsize, TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE); round_page(mapsize), TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE);
if (error) { if (error) {
vm_object_deallocate(ef->object); vm_object_deallocate(ef->object);
ef->object = 0; ef->object = 0;
goto out; goto out;
} }
mapbase = ef->address;
/* Wire the pages */ /* Wire the pages */
vm_map_wire(kernel_map, (vm_offset_t)mapbase, vm_map_wire(kernel_map, mapbase,
(vm_offset_t)mapbase + round_page(mapsize), mapbase + round_page(mapsize),
VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES); VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
/* Add the base address to the previously calculated/aligned offsets */ /* Inform the kld system about the situation */
for (i = 0; i < ef->nprogtab; i++) lf->address = ef->address = (caddr_t)mapbase;
ef->progtab[i].addr = mapbase + (uintptr_t)ef->progtab[i].addr; lf->size = mapsize;
for (i = 0; i < ef->nnobittab; i++)
ef->nobittab[i].addr = mapbase + (uintptr_t)ef->nobittab[i].addr;
/* Load the symbol table. */
error = vn_rdwr(UIO_READ, nd.ni_vp,
(void *)ef->ddbsymtab, shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
&resid, td);
if (error)
goto out;
error = vn_rdwr(UIO_READ, nd.ni_vp,
ef->ddbstrtab, shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
&resid, td);
if (error)
goto out;
/* Read in the text/data/set/etc sections */
for (i = 0; i < ef->nprogtab; i++) {
error = vn_rdwr(UIO_READ, nd.ni_vp,
ef->progtab[i].addr,
ef->progtab[i].filesz,
ef->progtab[i].fileoff,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
&resid, td);
if (error)
goto out;
}
/* /*
* Read in relocation tables. Platforms use rel or rela, but * Now load code/data(progbits), zero bss(nobits), allocate space for
* usually not both. * and load relocs
*/ */
for (i = 0; i < ef->nrel; i++) { pb = 0;
error = vn_rdwr(UIO_READ, nd.ni_vp, rl = 0;
(void *)ef->reltab[i].rel, ra = 0;
ef->reltab[i].filesz, alignmask = 0;
ef->reltab[i].fileoff, for (i = 0; i < hdr->e_shnum; i++) {
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, switch (shdr[i].sh_type) {
&resid, td); case SHT_PROGBITS:
if (error) case SHT_NOBITS:
goto out; alignmask = shdr[i].sh_addralign - 1;
mapbase += alignmask;
mapbase &= ~alignmask;
ef->progtab[pb].addr = (void *)(uintptr_t)mapbase;
if (shdr[i].sh_type == SHT_PROGBITS) {
ef->progtab[pb].name = "<<PROGBITS>>";
error = vn_rdwr(UIO_READ, nd.ni_vp,
ef->progtab[pb].addr,
shdr[i].sh_size, shdr[i].sh_offset,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
NOCRED, &resid, td);
if (error)
goto out;
if (resid != 0){
error = EINVAL;
goto out;
}
} else {
ef->progtab[pb].name = "<<NOBITS>>";
bzero(ef->progtab[pb].addr, shdr[i].sh_size);
}
ef->progtab[pb].size = shdr[i].sh_size;
ef->progtab[pb].align = shdr[i].sh_addralign;
ef->progtab[pb].sec = i;
if (ef->shstrtab && shdr[i].sh_name != 0)
ef->progtab[pb].name =
ef->shstrtab + shdr[i].sh_name;
mapbase += shdr[i].sh_size;
pb++;
break;
case SHT_REL:
ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
M_WAITOK);
ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
ef->reltab[rl].sec = shdr[i].sh_info;
error = vn_rdwr(UIO_READ, nd.ni_vp,
(void *)ef->reltab[rl].rel,
shdr[i].sh_size, shdr[i].sh_offset,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
&resid, td);
if (error)
goto out;
if (resid != 0){
error = EINVAL;
goto out;
}
rl++;
break;
case SHT_RELA:
ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
M_WAITOK);
ef->relatab[ra].nrela =
shdr[i].sh_size / sizeof(Elf_Rela);
ef->relatab[ra].sec = shdr[i].sh_info;
error = vn_rdwr(UIO_READ, nd.ni_vp,
(void *)ef->relatab[ra].rela,
shdr[i].sh_size, shdr[i].sh_offset,
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
&resid, td);
if (error)
goto out;
if (resid != 0){
error = EINVAL;
goto out;
}
ra++;
break;
}
} }
for (i = 0; i < ef->nrela; i++) { if (pb != ef->nprogtab)
error = vn_rdwr(UIO_READ, nd.ni_vp, panic("lost progbits");
(void *)ef->relatab[i].rela, if (rl != ef->nrel)
ef->relatab[i].filesz, panic("lost rel");
ef->relatab[i].fileoff, if (ra != ef->nrela)
UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, panic("lost rela");
&resid, td); if (mapbase != (vm_offset_t)ef->address + mapsize)
if (error) panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n", mapbase, ef->address, mapsize, (vm_offset_t)ef->address + mapsize);
goto out;
}
/* Inform the kld system about the situation */
lf->address = ef->address = mapbase;
lf->size = mapsize;
/* Local intra-module relocations */ /* Local intra-module relocations */
link_elf_reloc_local(lf); link_elf_reloc_local(lf);
@ -591,13 +594,28 @@ static void
link_elf_unload_file(linker_file_t file) link_elf_unload_file(linker_file_t file)
{ {
elf_file_t ef = (elf_file_t) file; elf_file_t ef = (elf_file_t) file;
int i;
/* Notify MD code that a module is being unloaded. */ /* Notify MD code that a module is being unloaded. */
elf_cpu_unload_file(file); elf_cpu_unload_file(file);
for (i = 0; i < ef->nrel; i++)
if (ef->reltab[i].rel)
free(ef->reltab[i].rel, M_LINKER);
for (i = 0; i < ef->nrela; i++)
if (ef->relatab[i].rela)
free(ef->relatab[i].rela, M_LINKER);
if (ef->reltab)
free(ef->reltab, M_LINKER);
if (ef->relatab)
free(ef->relatab, M_LINKER);
if (ef->progtab)
free(ef->progtab, M_LINKER);
if (ef->object) { if (ef->object) {
vm_map_remove(kernel_map, (vm_offset_t) ef->address, vm_map_remove(kernel_map, (vm_offset_t) ef->address,
(vm_offset_t) ef->address + (ef->object->size << PAGE_SHIFT)); (vm_offset_t) ef->address +
(ef->object->size << PAGE_SHIFT));
vm_object_deallocate(ef->object); vm_object_deallocate(ef->object);
} }
if (ef->e_shdr) if (ef->e_shdr)
@ -606,6 +624,8 @@ link_elf_unload_file(linker_file_t file)
free(ef->ddbsymtab, M_LINKER); free(ef->ddbsymtab, M_LINKER);
if (ef->ddbstrtab) if (ef->ddbstrtab)
free(ef->ddbstrtab, M_LINKER); free(ef->ddbstrtab, M_LINKER);
if (ef->shstrtab)
free(ef->shstrtab, M_LINKER);
} }
static const char * static const char *
@ -630,12 +650,6 @@ findbase(elf_file_t ef, int sec)
if (sec == ef->progtab[i].sec) if (sec == ef->progtab[i].sec)
base = (Elf_Addr)ef->progtab[i].addr; base = (Elf_Addr)ef->progtab[i].addr;
} }
if (base == 0) {
for (i = 0; i < ef->nnobittab; i++) {
if (sec == ef->nobittab[i].sec)
base = (Elf_Addr)ef->nobittab[i].addr;
}
}
if (base == 0) if (base == 0)
base = (Elf_Addr)ef->address; base = (Elf_Addr)ef->address;
return base; return base;
@ -658,22 +672,24 @@ relocate_file(elf_file_t ef)
/* Perform relocations without addend if there are any: */ /* Perform relocations without addend if there are any: */
for (i = 0; i < ef->nrel; i++) { for (i = 0; i < ef->nrel; i++) {
rel = ef->reltab[i].rel; rel = ef->reltab[i].rel;
if (rel) { if (rel == NULL)
rellim = (const Elf_Rel *)((const char *)rel + ef->reltab[i].filesz); continue;
base = findbase(ef, ef->reltab[i].sec); rellim = rel + ef->reltab[i].nrel;
while (rel < rellim) { base = findbase(ef, ef->reltab[i].sec);
symidx = ELF_R_SYM(rel->r_info); for ( ; rel < rellim; rel++) {
if (symidx < ef->ddbsymcnt) { symidx = ELF_R_SYM(rel->r_info);
sym = ef->ddbsymtab + symidx; if (symidx >= ef->ddbsymcnt)
if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) { continue;
if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL, elf_obj_lookup)) { sym = ef->ddbsymtab + symidx;
symname = symbol_name(ef, rel->r_info); /* Local relocs are already done */
printf("link_elf: symbol %s undefined\n", symname); if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
return ENOENT; continue;
} if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
} elf_obj_lookup)) {
} symname = symbol_name(ef, rel->r_info);
rel++; printf("link_elf_obj: symbol %s undefined\n",
symname);
return ENOENT;
} }
} }
} }
@ -681,22 +697,24 @@ relocate_file(elf_file_t ef)
/* Perform relocations with addend if there are any: */ /* Perform relocations with addend if there are any: */
for (i = 0; i < ef->nrela; i++) { for (i = 0; i < ef->nrela; i++) {
rela = ef->relatab[i].rela; rela = ef->relatab[i].rela;
if (rela) { if (rela == NULL)
relalim = (const Elf_Rela *)((const char *)rela + ef->relatab[i].filesz); continue;
base = findbase(ef, ef->relatab[i].sec); relalim = rela + ef->relatab[i].nrela;
while (rela < relalim) { base = findbase(ef, ef->relatab[i].sec);
symidx = ELF_R_SYM(rela->r_info); for ( ; rela < relalim; rela++) {
if (symidx < ef->ddbsymcnt) { symidx = ELF_R_SYM(rela->r_info);
sym = ef->ddbsymtab + symidx; if (symidx >= ef->ddbsymcnt)
if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) { continue;
if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA, elf_obj_lookup)) { sym = ef->ddbsymtab + symidx;
symname = symbol_name(ef, rela->r_info); /* Local relocs are already done */
printf("link_elf: symbol %s undefined\n", symname); if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
return ENOENT; continue;
} if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
} elf_obj_lookup)) {
} symname = symbol_name(ef, rela->r_info);
rela++; printf("link_elf_obj: symbol %s undefined\n",
symname);
return ENOENT;
} }
} }
} }
@ -713,13 +731,12 @@ link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
int i; int i;
/* XXX search for globals first */ /* XXX search for globals first */
/* Exhaustive search */
for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
strp = ef->ddbstrtab + symp->st_name; strp = ef->ddbstrtab + symp->st_name;
if (strcmp(name, strp) == 0) { if (strcmp(name, strp) == 0) {
if (symp->st_shndx != SHN_UNDEF || if (symp->st_shndx != SHN_UNDEF ||
(symp->st_value != 0 && (symp->st_value != 0 &&
ELF_ST_TYPE(symp->st_info) == STT_FUNC)) { ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
*sym = (c_linker_sym_t) symp; *sym = (c_linker_sym_t) symp;
return 0; return 0;
} else } else
@ -731,7 +748,8 @@ link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
} }
static int static int
link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t *symval) link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
linker_symval_t *symval)
{ {
elf_file_t ef = (elf_file_t) lf; elf_file_t ef = (elf_file_t) lf;
const Elf_Sym *es = (const Elf_Sym*) sym; const Elf_Sym *es = (const Elf_Sym*) sym;
@ -797,7 +815,8 @@ link_elf_lookup_set(linker_file_t lf, const char *name,
if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) && if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
strcmp(ef->progtab[i].name + 4, name) == 0) { strcmp(ef->progtab[i].name + 4, name) == 0) {
start = (void **)ef->progtab[i].addr; start = (void **)ef->progtab[i].addr;
stop = (void **)((char *)ef->progtab[i].addr + ef->progtab[i].filesz); stop = (void **)((char *)ef->progtab[i].addr +
ef->progtab[i].size);
count = stop - start; count = stop - start;
if (startp) if (startp)
*startp = start; *startp = start;
@ -867,14 +886,6 @@ elf_obj_lookup(linker_file_t lf, Elf_Word symidx, int deps)
break; break;
} }
} }
if (ret == 0) {
for (i = 0; i < ef->nnobittab; i++) {
if (sym->st_shndx == ef->nobittab[i].sec) {
ret = (Elf_Addr)ef->nobittab[i].addr;
break;
}
}
}
return ret + sym->st_value; return ret + sym->st_value;
case STB_GLOBAL: case STB_GLOBAL:
@ -888,7 +899,7 @@ elf_obj_lookup(linker_file_t lf, Elf_Word symidx, int deps)
return ret; return ret;
case STB_WEAK: case STB_WEAK:
printf("Weak symbols not supported\n"); printf("link_elf_obj: Weak symbols not supported\n");
return (0); return (0);
default: default:
@ -896,8 +907,6 @@ elf_obj_lookup(linker_file_t lf, Elf_Word symidx, int deps)
} }
} }
static void static void
link_elf_reloc_local(linker_file_t lf) link_elf_reloc_local(linker_file_t lf)
{ {
@ -915,36 +924,40 @@ link_elf_reloc_local(linker_file_t lf)
/* Perform relocations without addend if there are any: */ /* Perform relocations without addend if there are any: */
for (i = 0; i < ef->nrel; i++) { for (i = 0; i < ef->nrel; i++) {
rel = ef->reltab[i].rel; rel = ef->reltab[i].rel;
if (rel) { if (rel == NULL)
rellim = (const Elf_Rel *)((const char *)rel + ef->reltab[i].filesz); continue;
base = findbase(ef, ef->reltab[i].sec); rellim = rel + ef->reltab[i].nrel;
while (rel < rellim) { base = findbase(ef, ef->reltab[i].sec);
symidx = ELF_R_SYM(rel->r_info); for ( ; rel < rellim; rel++) {
if (symidx < ef->ddbsymcnt) { symidx = ELF_R_SYM(rel->r_info);
sym = ef->ddbsymtab + symidx; if (symidx >= ef->ddbsymcnt)
if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) continue;
elf_reloc_local(lf, base, rel, ELF_RELOC_REL, elf_obj_lookup); sym = ef->ddbsymtab + symidx;
} /* Only do local relocs */
rel++; if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
} continue;
elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
elf_obj_lookup);
} }
} }
/* Perform relocations with addend if there are any: */ /* Perform relocations with addend if there are any: */
for (i = 0; i < ef->nrela; i++) { for (i = 0; i < ef->nrela; i++) {
rela = ef->relatab[i].rela; rela = ef->relatab[i].rela;
if (rela) { if (rela == NULL)
relalim = (const Elf_Rela *)((const char *)rela + ef->relatab[i].filesz); continue;
base = findbase(ef, ef->relatab[i].sec); relalim = rela + ef->relatab[i].nrela;
while (rela < relalim) { base = findbase(ef, ef->relatab[i].sec);
symidx = ELF_R_SYM(rela->r_info); for ( ; rela < relalim; rela++) {
if (symidx < ef->ddbsymcnt) { symidx = ELF_R_SYM(rela->r_info);
sym = ef->ddbsymtab + symidx; if (symidx >= ef->ddbsymcnt)
if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) continue;
elf_reloc_local(lf, base, rela, ELF_RELOC_RELA, elf_obj_lookup); sym = ef->ddbsymtab + symidx;
} /* Only do local relocs */
rela++; if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
} continue;
elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
elf_obj_lookup);
} }
} }
} }