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>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/mount.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>
49 #include <security/mac/mac_framework.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_extern.h>
57 #include <vm/vm_map.h>
59 #include <sys/link_elf.h>
65 #include "linker_if.h"
71 int sec; /* Original section */
88 typedef struct elf_file {
89 struct linker_file lf; /* Common fields */
92 caddr_t address; /* Relocation address */
93 vm_object_t object; /* VM object to hold file pages */
105 Elf_Sym *ddbsymtab; /* The symbol table we are using */
106 long ddbsymcnt; /* Number of symbols */
107 caddr_t ddbstrtab; /* String table */
108 long ddbstrcnt; /* number of bytes in string table */
110 caddr_t shstrtab; /* Section name string table */
111 long shstrcnt; /* number of bytes in string table */
113 caddr_t ctftab; /* CTF table */
114 long ctfcnt; /* number of bytes in CTF table */
115 caddr_t ctfoff; /* CTF offset table */
116 caddr_t typoff; /* Type offset table */
117 long typlen; /* Number of type entries. */
121 #include <kern/kern_ctf.c>
123 static int link_elf_link_preload(linker_class_t cls,
124 const char *, linker_file_t *);
125 static int link_elf_link_preload_finish(linker_file_t);
126 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
127 static int link_elf_lookup_symbol(linker_file_t, const char *,
129 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
131 static int link_elf_search_symbol(linker_file_t, caddr_t value,
132 c_linker_sym_t *sym, long *diffp);
134 static void link_elf_unload_file(linker_file_t);
135 static int link_elf_lookup_set(linker_file_t, const char *,
136 void ***, void ***, int *);
137 static int link_elf_each_function_name(linker_file_t,
138 int (*)(const char *, void *), void *);
139 static int link_elf_each_function_nameval(linker_file_t,
140 linker_function_nameval_callback_t,
142 static void link_elf_reloc_local(linker_file_t);
144 static Elf_Addr elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps);
146 static kobj_method_t link_elf_methods[] = {
147 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
148 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
149 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
150 KOBJMETHOD(linker_unload, link_elf_unload_file),
151 KOBJMETHOD(linker_load_file, link_elf_load_file),
152 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
153 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
154 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
155 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
156 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
157 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
161 static struct linker_class link_elf_class = {
162 #if ELF_TARG_CLASS == ELFCLASS32
167 link_elf_methods, sizeof(struct elf_file)
170 static int relocate_file(elf_file_t ef);
173 link_elf_error(const char *s)
175 printf("kldload: %s\n", s);
179 link_elf_init(void *arg)
182 linker_add_class(&link_elf_class);
185 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
188 link_elf_link_preload(linker_class_t cls, const char *filename,
189 linker_file_t *result)
194 void *modptr, *baseptr, *sizeptr;
199 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
201 /* Look to see if we have the file preloaded */
202 modptr = preload_search_by_name(filename);
206 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
207 baseptr = preload_search_info(modptr, MODINFO_ADDR);
208 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
209 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
211 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
213 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
214 " obj module") != 0 &&
215 strcmp(type, "elf obj module") != 0)) {
218 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
222 lf = linker_make_file(filename, &link_elf_class);
228 ef->address = *(caddr_t *)baseptr;
229 lf->address = *(caddr_t *)baseptr;
230 lf->size = *(size_t *)sizeptr;
232 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
233 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
234 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
235 hdr->e_version != EV_CURRENT ||
236 hdr->e_type != ET_REL ||
237 hdr->e_machine != ELF_TARG_MACH) {
243 /* Scan the section header for information and table sizing. */
246 for (i = 0; i < hdr->e_shnum; i++) {
247 switch (shdr[i].sh_type) {
254 symstrindex = shdr[i].sh_link;
265 shstrindex = hdr->e_shstrndx;
266 if (ef->nprogtab == 0 || symstrindex < 0 ||
267 symstrindex >= hdr->e_shnum ||
268 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
269 shstrindex >= hdr->e_shnum ||
270 shdr[shstrindex].sh_type != SHT_STRTAB) {
271 printf("%s: bad/missing section headers\n", filename);
276 /* Allocate space for tracking the load chunks */
277 if (ef->nprogtab != 0)
278 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
279 M_LINKER, M_WAITOK | M_ZERO);
281 ef->reltab = malloc(ef->nrel * sizeof(*ef->reltab), M_LINKER,
284 ef->relatab = malloc(ef->nrela * sizeof(*ef->relatab), M_LINKER,
286 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
287 (ef->nrel != 0 && ef->reltab == NULL) ||
288 (ef->nrela != 0 && ef->relatab == NULL)) {
293 /* XXX, relocate the sh_addr fields saved by the loader. */
295 for (i = 0; i < hdr->e_shnum; i++) {
296 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
297 off = shdr[i].sh_addr;
299 for (i = 0; i < hdr->e_shnum; i++) {
300 if (shdr[i].sh_addr != 0)
301 shdr[i].sh_addr = shdr[i].sh_addr - off +
302 (Elf_Addr)ef->address;
305 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
306 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
307 ef->ddbstrcnt = shdr[symstrindex].sh_size;
308 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
309 ef->shstrcnt = shdr[shstrindex].sh_size;
310 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
312 /* Now fill out progtab and the relocation tables. */
316 for (i = 0; i < hdr->e_shnum; i++) {
317 switch (shdr[i].sh_type) {
320 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
321 if (shdr[i].sh_type == SHT_PROGBITS)
322 ef->progtab[pb].name = "<<PROGBITS>>";
324 ef->progtab[pb].name = "<<NOBITS>>";
325 ef->progtab[pb].size = shdr[i].sh_size;
326 ef->progtab[pb].sec = i;
327 if (ef->shstrtab && shdr[i].sh_name != 0)
328 ef->progtab[pb].name =
329 ef->shstrtab + shdr[i].sh_name;
331 /* Update all symbol values with the offset. */
332 for (j = 0; j < ef->ddbsymcnt; j++) {
333 es = &ef->ddbsymtab[j];
334 if (es->st_shndx != i)
336 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
341 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
342 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
343 ef->reltab[rl].sec = shdr[i].sh_info;
347 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
348 ef->relatab[ra].nrela =
349 shdr[i].sh_size / sizeof(Elf_Rela);
350 ef->relatab[ra].sec = shdr[i].sh_info;
355 if (pb != ef->nprogtab)
356 panic("lost progbits");
362 /* Local intra-module relocations */
363 link_elf_reloc_local(lf);
369 /* preload not done this way */
370 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
375 link_elf_link_preload_finish(linker_file_t lf)
381 error = relocate_file(ef);
385 /* Notify MD code that a module is being loaded. */
386 error = elf_cpu_load_file(lf);
394 link_elf_load_file(linker_class_t cls, const char *filename,
395 linker_file_t *result)
398 struct thread *td = curthread; /* XXX */
422 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, filename, td);
424 error = vn_open(&nd, &flags, 0, NULL);
427 vfslocked = NDHASGIANT(&nd);
428 NDFREE(&nd, NDF_ONLY_PNBUF);
429 if (nd.ni_vp->v_type != VREG) {
434 error = mac_kld_check_load(td->td_ucred, nd.ni_vp);
440 /* Read the elf header from the file. */
441 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
446 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0,
447 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
461 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
462 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
463 link_elf_error("Unsupported file layout");
467 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
468 || hdr->e_version != EV_CURRENT) {
469 link_elf_error("Unsupported file version");
473 if (hdr->e_type != ET_REL) {
474 link_elf_error("Unsupported file type");
478 if (hdr->e_machine != ELF_TARG_MACH) {
479 link_elf_error("Unsupported machine");
484 lf = linker_make_file(filename, &link_elf_class);
489 ef = (elf_file_t) lf;
495 /* Allocate and read in the section header */
496 nbytes = hdr->e_shnum * hdr->e_shentsize;
497 if (nbytes == 0 || hdr->e_shoff == 0 ||
498 hdr->e_shentsize != sizeof(Elf_Shdr)) {
502 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
508 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff,
509 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
517 /* Scan the section header for information and table sizing. */
521 for (i = 0; i < hdr->e_shnum; i++) {
522 switch (shdr[i].sh_type) {
530 symstrindex = shdr[i].sh_link;
542 if (ef->nprogtab == 0) {
543 link_elf_error("file has no contents");
548 /* Only allow one symbol table for now */
549 link_elf_error("file has no valid symbol table");
553 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
554 shdr[symstrindex].sh_type != SHT_STRTAB) {
555 link_elf_error("file has invalid symbol strings");
560 /* Allocate space for tracking the load chunks */
561 if (ef->nprogtab != 0)
562 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
563 M_LINKER, M_WAITOK | M_ZERO);
565 ef->reltab = malloc(ef->nrel * sizeof(*ef->reltab), M_LINKER,
568 ef->relatab = malloc(ef->nrela * sizeof(*ef->relatab), M_LINKER,
570 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
571 (ef->nrel != 0 && ef->reltab == NULL) ||
572 (ef->nrela != 0 && ef->relatab == NULL)) {
577 if (symtabindex == -1)
578 panic("lost symbol table index");
579 /* Allocate space for and load the symbol table */
580 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
581 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
582 if (ef->ddbsymtab == NULL) {
586 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
587 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
588 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
597 if (symstrindex == -1)
598 panic("lost symbol string index");
599 /* Allocate space for and load the symbol strings */
600 ef->ddbstrcnt = shdr[symstrindex].sh_size;
601 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
602 if (ef->ddbstrtab == NULL) {
606 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
607 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
608 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
617 /* Do we have a string table for the section names? */
619 if (hdr->e_shstrndx != 0 &&
620 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
621 shstrindex = hdr->e_shstrndx;
622 ef->shstrcnt = shdr[shstrindex].sh_size;
623 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
625 if (ef->shstrtab == NULL) {
629 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
630 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
631 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
641 /* Size up code/data(progbits) and bss(nobits). */
643 for (i = 0; i < hdr->e_shnum; i++) {
644 switch (shdr[i].sh_type) {
647 alignmask = shdr[i].sh_addralign - 1;
648 mapsize += alignmask;
649 mapsize &= ~alignmask;
650 mapsize += shdr[i].sh_size;
656 * We know how much space we need for the text/data/bss/etc.
657 * This stuff needs to be in a single chunk so that profiling etc
658 * can get the bounds and gdb can associate offsets with modules
660 ef->object = vm_object_allocate(OBJT_DEFAULT,
661 round_page(mapsize) >> PAGE_SHIFT);
662 if (ef->object == NULL) {
666 ef->address = (caddr_t) vm_map_min(kernel_map);
669 * In order to satisfy amd64's architectural requirements on the
670 * location of code and data in the kernel's address space, request a
671 * mapping that is above the kernel.
674 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
675 round_page(mapsize), TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE);
677 vm_object_deallocate(ef->object);
683 error = vm_map_wire(kernel_map, mapbase,
684 mapbase + round_page(mapsize),
685 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
686 if (error != KERN_SUCCESS) {
691 /* Inform the kld system about the situation */
692 lf->address = ef->address = (caddr_t)mapbase;
696 * Now load code/data(progbits), zero bss(nobits), allocate space for
703 for (i = 0; i < hdr->e_shnum; i++) {
704 switch (shdr[i].sh_type) {
707 alignmask = shdr[i].sh_addralign - 1;
708 mapbase += alignmask;
709 mapbase &= ~alignmask;
710 ef->progtab[pb].addr = (void *)(uintptr_t)mapbase;
711 if (shdr[i].sh_type == SHT_PROGBITS) {
712 ef->progtab[pb].name = "<<PROGBITS>>";
713 error = vn_rdwr(UIO_READ, nd.ni_vp,
714 ef->progtab[pb].addr,
715 shdr[i].sh_size, shdr[i].sh_offset,
716 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
725 ef->progtab[pb].name = "<<NOBITS>>";
726 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
728 ef->progtab[pb].size = shdr[i].sh_size;
729 ef->progtab[pb].sec = i;
730 if (ef->shstrtab && shdr[i].sh_name != 0)
731 ef->progtab[pb].name =
732 ef->shstrtab + shdr[i].sh_name;
734 /* Update all symbol values with the offset. */
735 for (j = 0; j < ef->ddbsymcnt; j++) {
736 es = &ef->ddbsymtab[j];
737 if (es->st_shndx != i)
739 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
741 mapbase += shdr[i].sh_size;
745 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
747 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
748 ef->reltab[rl].sec = shdr[i].sh_info;
749 error = vn_rdwr(UIO_READ, nd.ni_vp,
750 (void *)ef->reltab[rl].rel,
751 shdr[i].sh_size, shdr[i].sh_offset,
752 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
763 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
765 ef->relatab[ra].nrela =
766 shdr[i].sh_size / sizeof(Elf_Rela);
767 ef->relatab[ra].sec = shdr[i].sh_info;
768 error = vn_rdwr(UIO_READ, nd.ni_vp,
769 (void *)ef->relatab[ra].rela,
770 shdr[i].sh_size, shdr[i].sh_offset,
771 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
783 if (pb != ef->nprogtab)
784 panic("lost progbits");
789 if (mapbase != (vm_offset_t)ef->address + mapsize)
790 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
791 mapbase, ef->address, mapsize,
792 (vm_offset_t)ef->address + mapsize);
794 /* Local intra-module relocations */
795 link_elf_reloc_local(lf);
797 /* Pull in dependencies */
798 error = linker_load_dependencies(lf);
802 /* External relocations */
803 error = relocate_file(ef);
807 /* Notify MD code that a module is being loaded. */
808 error = elf_cpu_load_file(lf);
816 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
819 VOP_UNLOCK(nd.ni_vp, 0);
820 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
821 VFS_UNLOCK_GIANT(vfslocked);
827 link_elf_unload_file(linker_file_t file)
829 elf_file_t ef = (elf_file_t) file;
832 /* Notify MD code that a module is being unloaded. */
833 elf_cpu_unload_file(file);
837 free(ef->reltab, M_LINKER);
839 free(ef->relatab, M_LINKER);
841 free(ef->progtab, M_LINKER);
843 free(ef->ctftab, M_LINKER);
845 free(ef->ctfoff, M_LINKER);
847 free(ef->typoff, M_LINKER);
848 if (file->filename != NULL)
849 preload_delete_name(file->filename);
850 /* XXX reclaim module memory? */
854 for (i = 0; i < ef->nrel; i++)
855 if (ef->reltab[i].rel)
856 free(ef->reltab[i].rel, M_LINKER);
857 for (i = 0; i < ef->nrela; i++)
858 if (ef->relatab[i].rela)
859 free(ef->relatab[i].rela, M_LINKER);
861 free(ef->reltab, M_LINKER);
863 free(ef->relatab, M_LINKER);
865 free(ef->progtab, M_LINKER);
868 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
869 (vm_offset_t) ef->address +
870 (ef->object->size << PAGE_SHIFT));
873 free(ef->e_shdr, M_LINKER);
875 free(ef->ddbsymtab, M_LINKER);
877 free(ef->ddbstrtab, M_LINKER);
879 free(ef->shstrtab, M_LINKER);
881 free(ef->ctftab, M_LINKER);
883 free(ef->ctfoff, M_LINKER);
885 free(ef->typoff, M_LINKER);
889 symbol_name(elf_file_t ef, Elf_Size r_info)
893 if (ELF_R_SYM(r_info)) {
894 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
895 return ef->ddbstrtab + ref->st_name;
901 findbase(elf_file_t ef, int sec)
906 for (i = 0; i < ef->nprogtab; i++) {
907 if (sec == ef->progtab[i].sec) {
908 base = (Elf_Addr)ef->progtab[i].addr;
916 relocate_file(elf_file_t ef)
918 const Elf_Rel *rellim;
920 const Elf_Rela *relalim;
921 const Elf_Rela *rela;
929 /* Perform relocations without addend if there are any: */
930 for (i = 0; i < ef->nrel; i++) {
931 rel = ef->reltab[i].rel;
933 panic("lost a reltab!");
934 rellim = rel + ef->reltab[i].nrel;
935 base = findbase(ef, ef->reltab[i].sec);
937 panic("lost base for reltab");
938 for ( ; rel < rellim; rel++) {
939 symidx = ELF_R_SYM(rel->r_info);
940 if (symidx >= ef->ddbsymcnt)
942 sym = ef->ddbsymtab + symidx;
943 /* Local relocs are already done */
944 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
946 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
948 symname = symbol_name(ef, rel->r_info);
949 printf("link_elf_obj: symbol %s undefined\n",
956 /* Perform relocations with addend if there are any: */
957 for (i = 0; i < ef->nrela; i++) {
958 rela = ef->relatab[i].rela;
960 panic("lost a relatab!");
961 relalim = rela + ef->relatab[i].nrela;
962 base = findbase(ef, ef->relatab[i].sec);
964 panic("lost base for relatab");
965 for ( ; rela < relalim; rela++) {
966 symidx = ELF_R_SYM(rela->r_info);
967 if (symidx >= ef->ddbsymcnt)
969 sym = ef->ddbsymtab + symidx;
970 /* Local relocs are already done */
971 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
973 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
975 symname = symbol_name(ef, rela->r_info);
976 printf("link_elf_obj: symbol %s undefined\n",
987 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
989 elf_file_t ef = (elf_file_t) lf;
994 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
995 strp = ef->ddbstrtab + symp->st_name;
996 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
997 *sym = (c_linker_sym_t) symp;
1005 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1006 linker_symval_t *symval)
1008 elf_file_t ef = (elf_file_t) lf;
1009 const Elf_Sym *es = (const Elf_Sym*) sym;
1011 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1012 symval->name = ef->ddbstrtab + es->st_name;
1013 symval->value = (caddr_t)es->st_value;
1014 symval->size = es->st_size;
1021 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1022 c_linker_sym_t *sym, long *diffp)
1024 elf_file_t ef = (elf_file_t) lf;
1025 u_long off = (uintptr_t) (void *) value;
1029 const Elf_Sym *best = 0;
1032 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1033 if (es->st_name == 0)
1035 st_value = es->st_value;
1036 if (off >= st_value) {
1037 if (off - st_value < diff) {
1038 diff = off - st_value;
1042 } else if (off - st_value == diff) {
1051 *sym = (c_linker_sym_t) best;
1057 * Look up a linker set on an ELF system.
1060 link_elf_lookup_set(linker_file_t lf, const char *name,
1061 void ***startp, void ***stopp, int *countp)
1063 elf_file_t ef = (elf_file_t)lf;
1064 void **start, **stop;
1067 /* Relative to section number */
1068 for (i = 0; i < ef->nprogtab; i++) {
1069 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1070 strcmp(ef->progtab[i].name + 4, name) == 0) {
1071 start = (void **)ef->progtab[i].addr;
1072 stop = (void **)((char *)ef->progtab[i].addr +
1073 ef->progtab[i].size);
1074 count = stop - start;
1088 link_elf_each_function_name(linker_file_t file,
1089 int (*callback)(const char *, void *), void *opaque)
1091 elf_file_t ef = (elf_file_t)file;
1092 const Elf_Sym *symp;
1095 /* Exhaustive search */
1096 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1097 if (symp->st_value != 0 &&
1098 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1099 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1108 link_elf_each_function_nameval(linker_file_t file,
1109 linker_function_nameval_callback_t callback, void *opaque)
1111 linker_symval_t symval;
1112 elf_file_t ef = (elf_file_t)file;
1113 const Elf_Sym* symp;
1116 /* Exhaustive search */
1117 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1118 if (symp->st_value != 0 &&
1119 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1120 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1123 error = callback(file, i, &symval, opaque);
1132 * Symbol lookup function that can be used when the symbol index is known (ie
1133 * in relocations). It uses the symbol index instead of doing a fully fledged
1134 * hash table based lookup when such is valid. For example for local symbols.
1135 * This is not only more efficient, it's also more correct. It's not always
1136 * the case that the symbol can be found through the hash table.
1139 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps)
1141 elf_file_t ef = (elf_file_t)lf;
1146 /* Don't even try to lookup the symbol if the index is bogus. */
1147 if (symidx >= ef->ddbsymcnt)
1150 sym = ef->ddbsymtab + symidx;
1152 /* Quick answer if there is a definition included. */
1153 if (sym->st_shndx != SHN_UNDEF)
1154 return (sym->st_value);
1156 /* If we get here, then it is undefined and needs a lookup. */
1157 switch (ELF_ST_BIND(sym->st_info)) {
1159 /* Local, but undefined? huh? */
1163 /* Relative to Data or Function name */
1164 symbol = ef->ddbstrtab + sym->st_name;
1166 /* Force a lookup failure if the symbol name is bogus. */
1169 ret = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1173 printf("link_elf_obj: Weak symbols not supported\n");
1182 link_elf_fix_link_set(elf_file_t ef)
1184 static const char startn[] = "__start_";
1185 static const char stopn[] = "__stop_";
1187 const char *sym_name, *linkset_name;
1188 Elf_Addr startp, stopp;
1193 for (symidx = 1 /* zero entry is special */;
1194 symidx < ef->ddbsymcnt; symidx++) {
1195 sym = ef->ddbsymtab + symidx;
1196 if (sym->st_shndx != SHN_UNDEF)
1199 sym_name = ef->ddbstrtab + sym->st_name;
1200 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1202 linkset_name = sym_name + sizeof(startn) - 1;
1204 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1206 linkset_name = sym_name + sizeof(stopn) - 1;
1211 for (i = 0; i < ef->nprogtab; i++) {
1212 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1213 startp = (Elf_Addr)ef->progtab[i].addr;
1214 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1218 if (i == ef->nprogtab)
1221 sym->st_value = start ? startp : stopp;
1227 link_elf_reloc_local(linker_file_t lf)
1229 elf_file_t ef = (elf_file_t)lf;
1230 const Elf_Rel *rellim;
1232 const Elf_Rela *relalim;
1233 const Elf_Rela *rela;
1239 link_elf_fix_link_set(ef);
1241 /* Perform relocations without addend if there are any: */
1242 for (i = 0; i < ef->nrel; i++) {
1243 rel = ef->reltab[i].rel;
1245 panic("lost a reltab!");
1246 rellim = rel + ef->reltab[i].nrel;
1247 base = findbase(ef, ef->reltab[i].sec);
1249 panic("lost base for reltab");
1250 for ( ; rel < rellim; rel++) {
1251 symidx = ELF_R_SYM(rel->r_info);
1252 if (symidx >= ef->ddbsymcnt)
1254 sym = ef->ddbsymtab + symidx;
1255 /* Only do local relocs */
1256 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1258 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1263 /* Perform relocations with addend if there are any: */
1264 for (i = 0; i < ef->nrela; i++) {
1265 rela = ef->relatab[i].rela;
1267 panic("lost a relatab!");
1268 relalim = rela + ef->relatab[i].nrela;
1269 base = findbase(ef, ef->relatab[i].sec);
1271 panic("lost base for relatab");
1272 for ( ; rela < relalim; rela++) {
1273 symidx = ELF_R_SYM(rela->r_info);
1274 if (symidx >= ef->ddbsymcnt)
1276 sym = ef->ddbsymtab + symidx;
1277 /* Only do local relocs */
1278 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1280 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,