2 * Copyright (c) 1998-2000 Doug Rabson
3 * Copyright (c) 2004 Peter Wemm
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <sys/namei.h>
43 #include <sys/fcntl.h>
44 #include <sys/vnode.h>
45 #include <sys/linker.h>
47 #include <machine/elf.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_extern.h>
55 #include <vm/vm_map.h>
57 #include <sys/link_elf.h>
59 #include "linker_if.h"
65 int sec; /* Original section */
82 typedef struct elf_file {
83 struct linker_file lf; /* Common fields */
86 caddr_t address; /* Relocation address */
87 vm_object_t object; /* VM object to hold file pages */
99 Elf_Sym *ddbsymtab; /* The symbol table we are using */
100 long ddbsymcnt; /* Number of symbols */
101 caddr_t ddbstrtab; /* String table */
102 long ddbstrcnt; /* number of bytes in string table */
104 caddr_t shstrtab; /* Section name string table */
105 long shstrcnt; /* number of bytes in string table */
109 static int link_elf_link_preload(linker_class_t cls,
110 const char *, linker_file_t *);
111 static int link_elf_link_preload_finish(linker_file_t);
112 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
113 static int link_elf_lookup_symbol(linker_file_t, const char *,
115 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
117 static int link_elf_search_symbol(linker_file_t, caddr_t value,
118 c_linker_sym_t *sym, long *diffp);
120 static void link_elf_unload_file(linker_file_t);
121 static int link_elf_lookup_set(linker_file_t, const char *,
122 void ***, void ***, int *);
123 static int link_elf_each_function_name(linker_file_t,
124 int (*)(const char *, void *), void *);
125 static void link_elf_reloc_local(linker_file_t);
127 static Elf_Addr elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps);
129 static kobj_method_t link_elf_methods[] = {
130 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
131 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
132 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
133 KOBJMETHOD(linker_unload, link_elf_unload_file),
134 KOBJMETHOD(linker_load_file, link_elf_load_file),
135 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
136 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
137 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
138 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
142 static struct linker_class link_elf_class = {
143 #if ELF_TARG_CLASS == ELFCLASS32
148 link_elf_methods, sizeof(struct elf_file)
151 static int relocate_file(elf_file_t ef);
154 link_elf_error(const char *s)
156 printf("kldload: %s\n", s);
160 link_elf_init(void *arg)
163 linker_add_class(&link_elf_class);
166 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
169 link_elf_link_preload(linker_class_t cls, const char *filename,
170 linker_file_t *result)
175 void *modptr, *baseptr, *sizeptr;
180 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
182 /* Look to see if we have the file preloaded */
183 modptr = preload_search_by_name(filename);
187 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
188 baseptr = preload_search_info(modptr, MODINFO_ADDR);
189 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
190 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
192 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
194 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
195 " obj module") != 0 &&
196 strcmp(type, "elf obj module") != 0)) {
199 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
203 lf = linker_make_file(filename, &link_elf_class);
209 ef->address = *(caddr_t *)baseptr;
210 lf->address = *(caddr_t *)baseptr;
211 lf->size = *(size_t *)sizeptr;
213 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
214 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
215 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
216 hdr->e_version != EV_CURRENT ||
217 hdr->e_type != ET_REL ||
218 hdr->e_machine != ELF_TARG_MACH) {
224 /* Scan the section header for information and table sizing. */
227 for (i = 0; i < hdr->e_shnum; i++) {
228 switch (shdr[i].sh_type) {
235 symstrindex = shdr[i].sh_link;
246 shstrindex = hdr->e_shstrndx;
247 if (ef->nprogtab == 0 || symstrindex < 0 ||
248 symstrindex >= hdr->e_shnum ||
249 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
250 shstrindex >= hdr->e_shnum ||
251 shdr[shstrindex].sh_type != SHT_STRTAB) {
252 printf("%s: bad/missing section headers\n", filename);
257 /* Allocate space for tracking the load chunks */
258 if (ef->nprogtab != 0)
259 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
260 M_LINKER, M_WAITOK | M_ZERO);
262 ef->reltab = malloc(ef->nrel * sizeof(*ef->reltab), M_LINKER,
265 ef->relatab = malloc(ef->nrela * sizeof(*ef->relatab), M_LINKER,
267 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
268 (ef->nrel != 0 && ef->reltab == NULL) ||
269 (ef->nrela != 0 && ef->relatab == NULL)) {
274 /* XXX, relocate the sh_addr fields saved by the loader. */
276 for (i = 0; i < hdr->e_shnum; i++) {
277 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
278 off = shdr[i].sh_addr;
280 for (i = 0; i < hdr->e_shnum; i++) {
281 if (shdr[i].sh_addr != 0)
282 shdr[i].sh_addr = shdr[i].sh_addr - off +
283 (Elf_Addr)ef->address;
286 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
287 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
288 ef->ddbstrcnt = shdr[symstrindex].sh_size;
289 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
290 ef->shstrcnt = shdr[shstrindex].sh_size;
291 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
293 /* Now fill out progtab and the relocation tables. */
297 for (i = 0; i < hdr->e_shnum; i++) {
298 switch (shdr[i].sh_type) {
301 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
302 if (shdr[i].sh_type == SHT_PROGBITS)
303 ef->progtab[pb].name = "<<PROGBITS>>";
305 ef->progtab[pb].name = "<<NOBITS>>";
306 ef->progtab[pb].size = shdr[i].sh_size;
307 ef->progtab[pb].sec = i;
308 if (ef->shstrtab && shdr[i].sh_name != 0)
309 ef->progtab[pb].name =
310 ef->shstrtab + shdr[i].sh_name;
312 /* Update all symbol values with the offset. */
313 for (j = 0; j < ef->ddbsymcnt; j++) {
314 es = &ef->ddbsymtab[j];
315 if (es->st_shndx != i)
317 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
322 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
323 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
324 ef->reltab[rl].sec = shdr[i].sh_info;
328 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
329 ef->relatab[ra].nrela =
330 shdr[i].sh_size / sizeof(Elf_Rela);
331 ef->relatab[ra].sec = shdr[i].sh_info;
336 if (pb != ef->nprogtab)
337 panic("lost progbits");
343 /* Local intra-module relocations */
344 link_elf_reloc_local(lf);
350 /* preload not done this way */
351 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
356 link_elf_link_preload_finish(linker_file_t lf)
362 error = relocate_file(ef);
366 /* Notify MD code that a module is being loaded. */
367 error = elf_cpu_load_file(lf);
375 link_elf_load_file(linker_class_t cls, const char *filename,
376 linker_file_t *result)
379 struct thread *td = curthread; /* XXX */
404 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
406 error = vn_open(&nd, &flags, 0, -1);
409 NDFREE(&nd, NDF_ONLY_PNBUF);
411 error = mac_check_kld_load(td->td_ucred, nd.ni_vp);
417 /* Read the elf header from the file. */
418 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
423 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0,
424 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
438 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
439 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
440 link_elf_error("Unsupported file layout");
444 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
445 || hdr->e_version != EV_CURRENT) {
446 link_elf_error("Unsupported file version");
450 if (hdr->e_type != ET_REL) {
451 link_elf_error("Unsupported file type");
455 if (hdr->e_machine != ELF_TARG_MACH) {
456 link_elf_error("Unsupported machine");
461 lf = linker_make_file(filename, &link_elf_class);
466 ef = (elf_file_t) lf;
472 /* Allocate and read in the section header */
473 nbytes = hdr->e_shnum * hdr->e_shentsize;
474 if (nbytes == 0 || hdr->e_shoff == 0 ||
475 hdr->e_shentsize != sizeof(Elf_Shdr)) {
479 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
485 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff,
486 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
494 /* Scan the section header for information and table sizing. */
498 for (i = 0; i < hdr->e_shnum; i++) {
499 switch (shdr[i].sh_type) {
507 symstrindex = shdr[i].sh_link;
519 if (ef->nprogtab == 0) {
520 link_elf_error("file has no contents");
525 /* Only allow one symbol table for now */
526 link_elf_error("file has no valid symbol table");
530 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
531 shdr[symstrindex].sh_type != SHT_STRTAB) {
532 link_elf_error("file has invalid symbol strings");
537 /* Allocate space for tracking the load chunks */
538 if (ef->nprogtab != 0)
539 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
540 M_LINKER, M_WAITOK | M_ZERO);
542 ef->reltab = malloc(ef->nrel * sizeof(*ef->reltab), M_LINKER,
545 ef->relatab = malloc(ef->nrela * sizeof(*ef->relatab), M_LINKER,
547 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
548 (ef->nrel != 0 && ef->reltab == NULL) ||
549 (ef->nrela != 0 && ef->relatab == NULL)) {
554 if (symtabindex == -1)
555 panic("lost symbol table index");
556 /* Allocate space for and load the symbol table */
557 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
558 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
559 if (ef->ddbsymtab == NULL) {
563 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
564 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
565 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
574 if (symstrindex == -1)
575 panic("lost symbol string index");
576 /* Allocate space for and load the symbol strings */
577 ef->ddbstrcnt = shdr[symstrindex].sh_size;
578 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
579 if (ef->ddbstrtab == NULL) {
583 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
584 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
585 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
594 /* Do we have a string table for the section names? */
596 if (hdr->e_shstrndx != 0 &&
597 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
598 shstrindex = hdr->e_shstrndx;
599 ef->shstrcnt = shdr[shstrindex].sh_size;
600 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
602 if (ef->shstrtab == NULL) {
606 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
607 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
608 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
618 /* Size up code/data(progbits) and bss(nobits). */
620 for (i = 0; i < hdr->e_shnum; i++) {
621 switch (shdr[i].sh_type) {
624 alignmask = shdr[i].sh_addralign - 1;
625 mapsize += alignmask;
626 mapsize &= ~alignmask;
627 mapsize += shdr[i].sh_size;
633 * We know how much space we need for the text/data/bss/etc.
634 * This stuff needs to be in a single chunk so that profiling etc
635 * can get the bounds and gdb can associate offsets with modules
637 ef->object = vm_object_allocate(OBJT_DEFAULT,
638 round_page(mapsize) >> PAGE_SHIFT);
639 if (ef->object == NULL) {
643 ef->address = (caddr_t) vm_map_min(kernel_map);
644 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
645 round_page(mapsize), TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE);
647 vm_object_deallocate(ef->object);
653 error = vm_map_wire(kernel_map, mapbase,
654 mapbase + round_page(mapsize),
655 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
656 if (error != KERN_SUCCESS) {
661 /* Inform the kld system about the situation */
662 lf->address = ef->address = (caddr_t)mapbase;
666 * Now load code/data(progbits), zero bss(nobits), allocate space for
673 for (i = 0; i < hdr->e_shnum; i++) {
674 switch (shdr[i].sh_type) {
677 alignmask = shdr[i].sh_addralign - 1;
678 mapbase += alignmask;
679 mapbase &= ~alignmask;
680 ef->progtab[pb].addr = (void *)(uintptr_t)mapbase;
681 if (shdr[i].sh_type == SHT_PROGBITS) {
682 ef->progtab[pb].name = "<<PROGBITS>>";
683 error = vn_rdwr(UIO_READ, nd.ni_vp,
684 ef->progtab[pb].addr,
685 shdr[i].sh_size, shdr[i].sh_offset,
686 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
695 ef->progtab[pb].name = "<<NOBITS>>";
696 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
698 ef->progtab[pb].size = shdr[i].sh_size;
699 ef->progtab[pb].sec = i;
700 if (ef->shstrtab && shdr[i].sh_name != 0)
701 ef->progtab[pb].name =
702 ef->shstrtab + shdr[i].sh_name;
704 /* Update all symbol values with the offset. */
705 for (j = 0; j < ef->ddbsymcnt; j++) {
706 es = &ef->ddbsymtab[j];
707 if (es->st_shndx != i)
709 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
711 mapbase += shdr[i].sh_size;
715 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
717 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
718 ef->reltab[rl].sec = shdr[i].sh_info;
719 error = vn_rdwr(UIO_READ, nd.ni_vp,
720 (void *)ef->reltab[rl].rel,
721 shdr[i].sh_size, shdr[i].sh_offset,
722 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
733 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
735 ef->relatab[ra].nrela =
736 shdr[i].sh_size / sizeof(Elf_Rela);
737 ef->relatab[ra].sec = shdr[i].sh_info;
738 error = vn_rdwr(UIO_READ, nd.ni_vp,
739 (void *)ef->relatab[ra].rela,
740 shdr[i].sh_size, shdr[i].sh_offset,
741 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
753 if (pb != ef->nprogtab)
754 panic("lost progbits");
759 if (mapbase != (vm_offset_t)ef->address + mapsize)
760 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
761 mapbase, ef->address, mapsize,
762 (vm_offset_t)ef->address + mapsize);
764 /* Local intra-module relocations */
765 link_elf_reloc_local(lf);
767 /* Pull in dependencies */
768 error = linker_load_dependencies(lf);
772 /* External relocations */
773 error = relocate_file(ef);
777 /* Notify MD code that a module is being loaded. */
778 error = elf_cpu_load_file(lf);
786 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
789 VOP_UNLOCK(nd.ni_vp, 0, td);
790 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
796 link_elf_unload_file(linker_file_t file)
798 elf_file_t ef = (elf_file_t) file;
801 /* Notify MD code that a module is being unloaded. */
802 elf_cpu_unload_file(file);
806 free(ef->reltab, M_LINKER);
808 free(ef->relatab, M_LINKER);
810 free(ef->progtab, M_LINKER);
811 if (file->filename != NULL)
812 preload_delete_name(file->filename);
813 /* XXX reclaim module memory? */
817 for (i = 0; i < ef->nrel; i++)
818 if (ef->reltab[i].rel)
819 free(ef->reltab[i].rel, M_LINKER);
820 for (i = 0; i < ef->nrela; i++)
821 if (ef->relatab[i].rela)
822 free(ef->relatab[i].rela, M_LINKER);
824 free(ef->reltab, M_LINKER);
826 free(ef->relatab, M_LINKER);
828 free(ef->progtab, M_LINKER);
831 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
832 (vm_offset_t) ef->address +
833 (ef->object->size << PAGE_SHIFT));
836 free(ef->e_shdr, M_LINKER);
838 free(ef->ddbsymtab, M_LINKER);
840 free(ef->ddbstrtab, M_LINKER);
842 free(ef->shstrtab, M_LINKER);
846 symbol_name(elf_file_t ef, Elf_Size r_info)
850 if (ELF_R_SYM(r_info)) {
851 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
852 return ef->ddbstrtab + ref->st_name;
858 findbase(elf_file_t ef, int sec)
863 for (i = 0; i < ef->nprogtab; i++) {
864 if (sec == ef->progtab[i].sec) {
865 base = (Elf_Addr)ef->progtab[i].addr;
873 relocate_file(elf_file_t ef)
875 const Elf_Rel *rellim;
877 const Elf_Rela *relalim;
878 const Elf_Rela *rela;
886 /* Perform relocations without addend if there are any: */
887 for (i = 0; i < ef->nrel; i++) {
888 rel = ef->reltab[i].rel;
890 panic("lost a reltab!");
891 rellim = rel + ef->reltab[i].nrel;
892 base = findbase(ef, ef->reltab[i].sec);
894 panic("lost base for reltab");
895 for ( ; rel < rellim; rel++) {
896 symidx = ELF_R_SYM(rel->r_info);
897 if (symidx >= ef->ddbsymcnt)
899 sym = ef->ddbsymtab + symidx;
900 /* Local relocs are already done */
901 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
903 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
905 symname = symbol_name(ef, rel->r_info);
906 printf("link_elf_obj: symbol %s undefined\n",
913 /* Perform relocations with addend if there are any: */
914 for (i = 0; i < ef->nrela; i++) {
915 rela = ef->relatab[i].rela;
917 panic("lost a relatab!");
918 relalim = rela + ef->relatab[i].nrela;
919 base = findbase(ef, ef->relatab[i].sec);
921 panic("lost base for relatab");
922 for ( ; rela < relalim; rela++) {
923 symidx = ELF_R_SYM(rela->r_info);
924 if (symidx >= ef->ddbsymcnt)
926 sym = ef->ddbsymtab + symidx;
927 /* Local relocs are already done */
928 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
930 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
932 symname = symbol_name(ef, rela->r_info);
933 printf("link_elf_obj: symbol %s undefined\n",
944 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
946 elf_file_t ef = (elf_file_t) lf;
951 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
952 strp = ef->ddbstrtab + symp->st_name;
953 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
954 *sym = (c_linker_sym_t) symp;
962 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
963 linker_symval_t *symval)
965 elf_file_t ef = (elf_file_t) lf;
966 const Elf_Sym *es = (const Elf_Sym*) sym;
968 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
969 symval->name = ef->ddbstrtab + es->st_name;
970 symval->value = (caddr_t)es->st_value;
971 symval->size = es->st_size;
978 link_elf_search_symbol(linker_file_t lf, caddr_t value,
979 c_linker_sym_t *sym, long *diffp)
981 elf_file_t ef = (elf_file_t) lf;
982 u_long off = (uintptr_t) (void *) value;
986 const Elf_Sym *best = 0;
989 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
990 if (es->st_name == 0)
992 st_value = es->st_value;
993 if (off >= st_value) {
994 if (off - st_value < diff) {
995 diff = off - st_value;
999 } else if (off - st_value == diff) {
1008 *sym = (c_linker_sym_t) best;
1014 * Look up a linker set on an ELF system.
1017 link_elf_lookup_set(linker_file_t lf, const char *name,
1018 void ***startp, void ***stopp, int *countp)
1020 elf_file_t ef = (elf_file_t)lf;
1021 void **start, **stop;
1024 /* Relative to section number */
1025 for (i = 0; i < ef->nprogtab; i++) {
1026 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1027 strcmp(ef->progtab[i].name + 4, name) == 0) {
1028 start = (void **)ef->progtab[i].addr;
1029 stop = (void **)((char *)ef->progtab[i].addr +
1030 ef->progtab[i].size);
1031 count = stop - start;
1045 link_elf_each_function_name(linker_file_t file,
1046 int (*callback)(const char *, void *), void *opaque)
1048 elf_file_t ef = (elf_file_t)file;
1049 const Elf_Sym *symp;
1052 /* Exhaustive search */
1053 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1054 if (symp->st_value != 0 &&
1055 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1056 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1065 * Symbol lookup function that can be used when the symbol index is known (ie
1066 * in relocations). It uses the symbol index instead of doing a fully fledged
1067 * hash table based lookup when such is valid. For example for local symbols.
1068 * This is not only more efficient, it's also more correct. It's not always
1069 * the case that the symbol can be found through the hash table.
1072 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps)
1074 elf_file_t ef = (elf_file_t)lf;
1079 /* Don't even try to lookup the symbol if the index is bogus. */
1080 if (symidx >= ef->ddbsymcnt)
1083 sym = ef->ddbsymtab + symidx;
1085 /* Quick answer if there is a definition included. */
1086 if (sym->st_shndx != SHN_UNDEF)
1087 return (sym->st_value);
1089 /* If we get here, then it is undefined and needs a lookup. */
1090 switch (ELF_ST_BIND(sym->st_info)) {
1092 /* Local, but undefined? huh? */
1096 /* Relative to Data or Function name */
1097 symbol = ef->ddbstrtab + sym->st_name;
1099 /* Force a lookup failure if the symbol name is bogus. */
1102 ret = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1106 printf("link_elf_obj: Weak symbols not supported\n");
1115 link_elf_reloc_local(linker_file_t lf)
1117 elf_file_t ef = (elf_file_t)lf;
1118 const Elf_Rel *rellim;
1120 const Elf_Rela *relalim;
1121 const Elf_Rela *rela;
1127 /* Perform relocations without addend if there are any: */
1128 for (i = 0; i < ef->nrel; i++) {
1129 rel = ef->reltab[i].rel;
1131 panic("lost a reltab!");
1132 rellim = rel + ef->reltab[i].nrel;
1133 base = findbase(ef, ef->reltab[i].sec);
1135 panic("lost base for reltab");
1136 for ( ; rel < rellim; rel++) {
1137 symidx = ELF_R_SYM(rel->r_info);
1138 if (symidx >= ef->ddbsymcnt)
1140 sym = ef->ddbsymtab + symidx;
1141 /* Only do local relocs */
1142 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1144 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1149 /* Perform relocations with addend if there are any: */
1150 for (i = 0; i < ef->nrela; i++) {
1151 rela = ef->relatab[i].rela;
1153 panic("lost a relatab!");
1154 relalim = rela + ef->relatab[i].nrela;
1155 base = findbase(ef, ef->relatab[i].sec);
1157 panic("lost base for relatab");
1158 for ( ; rela < relalim; rela++) {
1159 symidx = ELF_R_SYM(rela->r_info);
1160 if (symidx >= ef->ddbsymcnt)
1162 sym = ef->ddbsymtab + symidx;
1163 /* Only do local relocs */
1164 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1166 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,