]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/link_elf_obj.c
Merge lldb trunk r321017 to contrib/llvm/tools/lldb.
[FreeBSD/FreeBSD.git] / sys / kern / link_elf_obj.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998-2000 Doug Rabson
5  * Copyright (c) 2004 Peter Wemm
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_ddb.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/mount.h>
42 #include <sys/proc.h>
43 #include <sys/namei.h>
44 #include <sys/fcntl.h>
45 #include <sys/vnode.h>
46 #include <sys/linker.h>
47
48 #include <machine/elf.h>
49
50 #include <net/vnet.h>
51
52 #include <security/mac/mac_framework.h>
53
54 #include <vm/vm.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_extern.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_map.h>
61
62 #include <sys/link_elf.h>
63
64 #ifdef DDB_CTF
65 #include <sys/zlib.h>
66 #endif
67
68 #include "linker_if.h"
69
70 typedef struct {
71         void            *addr;
72         Elf_Off         size;
73         int             flags;
74         int             sec;    /* Original section */
75         char            *name;
76 } Elf_progent;
77
78 typedef struct {
79         Elf_Rel         *rel;
80         int             nrel;
81         int             sec;
82 } Elf_relent;
83
84 typedef struct {
85         Elf_Rela        *rela;
86         int             nrela;
87         int             sec;
88 } Elf_relaent;
89
90
91 typedef struct elf_file {
92         struct linker_file lf;          /* Common fields */
93
94         int             preloaded;
95         caddr_t         address;        /* Relocation address */
96         vm_object_t     object;         /* VM object to hold file pages */
97         Elf_Shdr        *e_shdr;
98
99         Elf_progent     *progtab;
100         int             nprogtab;
101
102         Elf_relaent     *relatab;
103         int             nrelatab;
104
105         Elf_relent      *reltab;
106         int             nreltab;
107
108         Elf_Sym         *ddbsymtab;     /* The symbol table we are using */
109         long            ddbsymcnt;      /* Number of symbols */
110         caddr_t         ddbstrtab;      /* String table */
111         long            ddbstrcnt;      /* number of bytes in string table */
112
113         caddr_t         shstrtab;       /* Section name string table */
114         long            shstrcnt;       /* number of bytes in string table */
115
116         caddr_t         ctftab;         /* CTF table */
117         long            ctfcnt;         /* number of bytes in CTF table */
118         caddr_t         ctfoff;         /* CTF offset table */
119         caddr_t         typoff;         /* Type offset table */
120         long            typlen;         /* Number of type entries. */
121
122 } *elf_file_t;
123
124 #include <kern/kern_ctf.c>
125
126 static int      link_elf_link_preload(linker_class_t cls,
127                     const char *, linker_file_t *);
128 static int      link_elf_link_preload_finish(linker_file_t);
129 static int      link_elf_load_file(linker_class_t, const char *, linker_file_t *);
130 static int      link_elf_lookup_symbol(linker_file_t, const char *,
131                     c_linker_sym_t *);
132 static int      link_elf_symbol_values(linker_file_t, c_linker_sym_t,
133                     linker_symval_t *);
134 static int      link_elf_search_symbol(linker_file_t, caddr_t value,
135                     c_linker_sym_t *sym, long *diffp);
136
137 static void     link_elf_unload_file(linker_file_t);
138 static int      link_elf_lookup_set(linker_file_t, const char *,
139                     void ***, void ***, int *);
140 static int      link_elf_each_function_name(linker_file_t,
141                     int (*)(const char *, void *), void *);
142 static int      link_elf_each_function_nameval(linker_file_t,
143                                 linker_function_nameval_callback_t,
144                                 void *);
145 static int      link_elf_reloc_local(linker_file_t);
146 static long     link_elf_symtab_get(linker_file_t, const Elf_Sym **);
147 static long     link_elf_strtab_get(linker_file_t, caddr_t *);
148
149 static int      elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
150                     Elf_Addr *);
151
152 static kobj_method_t link_elf_methods[] = {
153         KOBJMETHOD(linker_lookup_symbol,        link_elf_lookup_symbol),
154         KOBJMETHOD(linker_symbol_values,        link_elf_symbol_values),
155         KOBJMETHOD(linker_search_symbol,        link_elf_search_symbol),
156         KOBJMETHOD(linker_unload,               link_elf_unload_file),
157         KOBJMETHOD(linker_load_file,            link_elf_load_file),
158         KOBJMETHOD(linker_link_preload,         link_elf_link_preload),
159         KOBJMETHOD(linker_link_preload_finish,  link_elf_link_preload_finish),
160         KOBJMETHOD(linker_lookup_set,           link_elf_lookup_set),
161         KOBJMETHOD(linker_each_function_name,   link_elf_each_function_name),
162         KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
163         KOBJMETHOD(linker_ctf_get,              link_elf_ctf_get),
164         KOBJMETHOD(linker_symtab_get,           link_elf_symtab_get),
165         KOBJMETHOD(linker_strtab_get,           link_elf_strtab_get),
166         { 0, 0 }
167 };
168
169 static struct linker_class link_elf_class = {
170 #if ELF_TARG_CLASS == ELFCLASS32
171         "elf32_obj",
172 #else
173         "elf64_obj",
174 #endif
175         link_elf_methods, sizeof(struct elf_file)
176 };
177
178 static int      relocate_file(elf_file_t ef);
179 static void     elf_obj_cleanup_globals_cache(elf_file_t);
180
181 static void
182 link_elf_error(const char *filename, const char *s)
183 {
184         if (filename == NULL)
185                 printf("kldload: %s\n", s);
186         else
187                 printf("kldload: %s: %s\n", filename, s);
188 }
189
190 static void
191 link_elf_init(void *arg)
192 {
193
194         linker_add_class(&link_elf_class);
195 }
196
197 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
198
199 static int
200 link_elf_link_preload(linker_class_t cls, const char *filename,
201     linker_file_t *result)
202 {
203         Elf_Ehdr *hdr;
204         Elf_Shdr *shdr;
205         Elf_Sym *es;
206         void *modptr, *baseptr, *sizeptr;
207         char *type;
208         elf_file_t ef;
209         linker_file_t lf;
210         Elf_Addr off;
211         int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
212
213         /* Look to see if we have the file preloaded */
214         modptr = preload_search_by_name(filename);
215         if (modptr == NULL)
216                 return ENOENT;
217
218         type = (char *)preload_search_info(modptr, MODINFO_TYPE);
219         baseptr = preload_search_info(modptr, MODINFO_ADDR);
220         sizeptr = preload_search_info(modptr, MODINFO_SIZE);
221         hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
222             MODINFOMD_ELFHDR);
223         shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
224             MODINFOMD_SHDR);
225         if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
226             " obj module") != 0 &&
227             strcmp(type, "elf obj module") != 0)) {
228                 return (EFTYPE);
229         }
230         if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
231             shdr == NULL)
232                 return (EINVAL);
233
234         lf = linker_make_file(filename, &link_elf_class);
235         if (lf == NULL)
236                 return (ENOMEM);
237
238         ef = (elf_file_t)lf;
239         ef->preloaded = 1;
240         ef->address = *(caddr_t *)baseptr;
241         lf->address = *(caddr_t *)baseptr;
242         lf->size = *(size_t *)sizeptr;
243
244         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
245             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
246             hdr->e_ident[EI_VERSION] != EV_CURRENT ||
247             hdr->e_version != EV_CURRENT ||
248             hdr->e_type != ET_REL ||
249             hdr->e_machine != ELF_TARG_MACH) {
250                 error = EFTYPE;
251                 goto out;
252         }
253         ef->e_shdr = shdr;
254
255         /* Scan the section header for information and table sizing. */
256         symtabindex = -1;
257         symstrindex = -1;
258         for (i = 0; i < hdr->e_shnum; i++) {
259                 switch (shdr[i].sh_type) {
260                 case SHT_PROGBITS:
261                 case SHT_NOBITS:
262 #ifdef __amd64__
263                 case SHT_X86_64_UNWIND:
264 #endif
265                         ef->nprogtab++;
266                         break;
267                 case SHT_SYMTAB:
268                         symtabindex = i;
269                         symstrindex = shdr[i].sh_link;
270                         break;
271                 case SHT_REL:
272                         ef->nreltab++;
273                         break;
274                 case SHT_RELA:
275                         ef->nrelatab++;
276                         break;
277                 }
278         }
279
280         shstrindex = hdr->e_shstrndx;
281         if (ef->nprogtab == 0 || symstrindex < 0 ||
282             symstrindex >= hdr->e_shnum ||
283             shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
284             shstrindex >= hdr->e_shnum ||
285             shdr[shstrindex].sh_type != SHT_STRTAB) {
286                 printf("%s: bad/missing section headers\n", filename);
287                 error = ENOEXEC;
288                 goto out;
289         }
290
291         /* Allocate space for tracking the load chunks */
292         if (ef->nprogtab != 0)
293                 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
294                     M_LINKER, M_WAITOK | M_ZERO);
295         if (ef->nreltab != 0)
296                 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
297                     M_LINKER, M_WAITOK | M_ZERO);
298         if (ef->nrelatab != 0)
299                 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
300                     M_LINKER, M_WAITOK | M_ZERO);
301         if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
302             (ef->nreltab != 0 && ef->reltab == NULL) ||
303             (ef->nrelatab != 0 && ef->relatab == NULL)) {
304                 error = ENOMEM;
305                 goto out;
306         }
307
308         /* XXX, relocate the sh_addr fields saved by the loader. */
309         off = 0;
310         for (i = 0; i < hdr->e_shnum; i++) {
311                 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
312                         off = shdr[i].sh_addr;
313         }
314         for (i = 0; i < hdr->e_shnum; i++) {
315                 if (shdr[i].sh_addr != 0)
316                         shdr[i].sh_addr = shdr[i].sh_addr - off +
317                             (Elf_Addr)ef->address;
318         }
319
320         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
321         ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
322         ef->ddbstrcnt = shdr[symstrindex].sh_size;
323         ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
324         ef->shstrcnt = shdr[shstrindex].sh_size;
325         ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
326
327         /* Now fill out progtab and the relocation tables. */
328         pb = 0;
329         rl = 0;
330         ra = 0;
331         for (i = 0; i < hdr->e_shnum; i++) {
332                 switch (shdr[i].sh_type) {
333                 case SHT_PROGBITS:
334                 case SHT_NOBITS:
335 #ifdef __amd64__
336                 case SHT_X86_64_UNWIND:
337 #endif
338                         ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
339                         if (shdr[i].sh_type == SHT_PROGBITS)
340                                 ef->progtab[pb].name = "<<PROGBITS>>";
341 #ifdef __amd64__
342                         else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
343                                 ef->progtab[pb].name = "<<UNWIND>>";
344 #endif
345                         else
346                                 ef->progtab[pb].name = "<<NOBITS>>";
347                         ef->progtab[pb].size = shdr[i].sh_size;
348                         ef->progtab[pb].sec = i;
349                         if (ef->shstrtab && shdr[i].sh_name != 0)
350                                 ef->progtab[pb].name =
351                                     ef->shstrtab + shdr[i].sh_name;
352                         if (ef->progtab[pb].name != NULL && 
353                             !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
354                                 void *dpcpu;
355
356                                 dpcpu = dpcpu_alloc(shdr[i].sh_size);
357                                 if (dpcpu == NULL) {
358                                         error = ENOSPC;
359                                         goto out;
360                                 }
361                                 memcpy(dpcpu, ef->progtab[pb].addr,
362                                     ef->progtab[pb].size);
363                                 dpcpu_copy(dpcpu, shdr[i].sh_size);
364                                 ef->progtab[pb].addr = dpcpu;
365 #ifdef VIMAGE
366                         } else if (ef->progtab[pb].name != NULL &&
367                             !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
368                                 void *vnet_data;
369
370                                 vnet_data = vnet_data_alloc(shdr[i].sh_size);
371                                 if (vnet_data == NULL) {
372                                         error = ENOSPC;
373                                         goto out;
374                                 }
375                                 memcpy(vnet_data, ef->progtab[pb].addr,
376                                     ef->progtab[pb].size);
377                                 vnet_data_copy(vnet_data, shdr[i].sh_size);
378                                 ef->progtab[pb].addr = vnet_data;
379 #endif
380                         } else if (ef->progtab[pb].name != NULL &&
381                             !strcmp(ef->progtab[pb].name, ".ctors")) {
382                                 lf->ctors_addr = ef->progtab[pb].addr;
383                                 lf->ctors_size = shdr[i].sh_size;
384                         }
385
386                         /* Update all symbol values with the offset. */
387                         for (j = 0; j < ef->ddbsymcnt; j++) {
388                                 es = &ef->ddbsymtab[j];
389                                 if (es->st_shndx != i)
390                                         continue;
391                                 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
392                         }
393                         pb++;
394                         break;
395                 case SHT_REL:
396                         ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
397                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
398                         ef->reltab[rl].sec = shdr[i].sh_info;
399                         rl++;
400                         break;
401                 case SHT_RELA:
402                         ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
403                         ef->relatab[ra].nrela =
404                             shdr[i].sh_size / sizeof(Elf_Rela);
405                         ef->relatab[ra].sec = shdr[i].sh_info;
406                         ra++;
407                         break;
408                 }
409         }
410         if (pb != ef->nprogtab) {
411                 printf("%s: lost progbits\n", filename);
412                 error = ENOEXEC;
413                 goto out;
414         }
415         if (rl != ef->nreltab) {
416                 printf("%s: lost reltab\n", filename);
417                 error = ENOEXEC;
418                 goto out;
419         }
420         if (ra != ef->nrelatab) {
421                 printf("%s: lost relatab\n", filename);
422                 error = ENOEXEC;
423                 goto out;
424         }
425
426         /* Local intra-module relocations */
427         error = link_elf_reloc_local(lf);
428         if (error != 0)
429                 goto out;
430
431         *result = lf;
432         return (0);
433
434 out:
435         /* preload not done this way */
436         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
437         return (error);
438 }
439
440 static void
441 link_elf_invoke_ctors(caddr_t addr, size_t size)
442 {
443         void (**ctor)(void);
444         size_t i, cnt;
445
446         if (addr == NULL || size == 0)
447                 return;
448         cnt = size / sizeof(*ctor);
449         ctor = (void *)addr;
450         for (i = 0; i < cnt; i++) {
451                 if (ctor[i] != NULL)
452                         (*ctor[i])();
453         }
454 }
455
456 static int
457 link_elf_link_preload_finish(linker_file_t lf)
458 {
459         elf_file_t ef;
460         int error;
461
462         ef = (elf_file_t)lf;
463         error = relocate_file(ef);
464         if (error)
465                 return error;
466
467         /* Notify MD code that a module is being loaded. */
468         error = elf_cpu_load_file(lf);
469         if (error)
470                 return (error);
471
472         /* Invoke .ctors */
473         link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
474         return (0);
475 }
476
477 static int
478 link_elf_load_file(linker_class_t cls, const char *filename,
479     linker_file_t *result)
480 {
481         struct nameidata *nd;
482         struct thread *td = curthread;  /* XXX */
483         Elf_Ehdr *hdr;
484         Elf_Shdr *shdr;
485         Elf_Sym *es;
486         int nbytes, i, j;
487         vm_offset_t mapbase;
488         size_t mapsize;
489         int error = 0;
490         ssize_t resid;
491         int flags;
492         elf_file_t ef;
493         linker_file_t lf;
494         int symtabindex;
495         int symstrindex;
496         int shstrindex;
497         int nsym;
498         int pb, rl, ra;
499         int alignmask;
500
501         shdr = NULL;
502         lf = NULL;
503         mapsize = 0;
504         hdr = NULL;
505
506         nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
507         NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
508         flags = FREAD;
509         error = vn_open(nd, &flags, 0, NULL);
510         if (error) {
511                 free(nd, M_TEMP);
512                 return error;
513         }
514         NDFREE(nd, NDF_ONLY_PNBUF);
515         if (nd->ni_vp->v_type != VREG) {
516                 error = ENOEXEC;
517                 goto out;
518         }
519 #ifdef MAC
520         error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
521         if (error) {
522                 goto out;
523         }
524 #endif
525
526         /* Read the elf header from the file. */
527         hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
528         error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
529             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
530             &resid, td);
531         if (error)
532                 goto out;
533         if (resid != 0){
534                 error = ENOEXEC;
535                 goto out;
536         }
537
538         if (!IS_ELF(*hdr)) {
539                 error = ENOEXEC;
540                 goto out;
541         }
542
543         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
544             || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
545                 link_elf_error(filename, "Unsupported file layout");
546                 error = ENOEXEC;
547                 goto out;
548         }
549         if (hdr->e_ident[EI_VERSION] != EV_CURRENT
550             || hdr->e_version != EV_CURRENT) {
551                 link_elf_error(filename, "Unsupported file version");
552                 error = ENOEXEC;
553                 goto out;
554         }
555         if (hdr->e_type != ET_REL) {
556                 error = ENOSYS;
557                 goto out;
558         }
559         if (hdr->e_machine != ELF_TARG_MACH) {
560                 link_elf_error(filename, "Unsupported machine");
561                 error = ENOEXEC;
562                 goto out;
563         }
564
565         lf = linker_make_file(filename, &link_elf_class);
566         if (!lf) {
567                 error = ENOMEM;
568                 goto out;
569         }
570         ef = (elf_file_t) lf;
571         ef->nprogtab = 0;
572         ef->e_shdr = 0;
573         ef->nreltab = 0;
574         ef->nrelatab = 0;
575
576         /* Allocate and read in the section header */
577         nbytes = hdr->e_shnum * hdr->e_shentsize;
578         if (nbytes == 0 || hdr->e_shoff == 0 ||
579             hdr->e_shentsize != sizeof(Elf_Shdr)) {
580                 error = ENOEXEC;
581                 goto out;
582         }
583         shdr = malloc(nbytes, M_LINKER, M_WAITOK);
584         ef->e_shdr = shdr;
585         error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
586             hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
587             NOCRED, &resid, td);
588         if (error)
589                 goto out;
590         if (resid) {
591                 error = ENOEXEC;
592                 goto out;
593         }
594
595         /* Scan the section header for information and table sizing. */
596         nsym = 0;
597         symtabindex = -1;
598         symstrindex = -1;
599         for (i = 0; i < hdr->e_shnum; i++) {
600                 if (shdr[i].sh_size == 0)
601                         continue;
602                 switch (shdr[i].sh_type) {
603                 case SHT_PROGBITS:
604                 case SHT_NOBITS:
605 #ifdef __amd64__
606                 case SHT_X86_64_UNWIND:
607 #endif
608                         ef->nprogtab++;
609                         break;
610                 case SHT_SYMTAB:
611                         nsym++;
612                         symtabindex = i;
613                         symstrindex = shdr[i].sh_link;
614                         break;
615                 case SHT_REL:
616                         ef->nreltab++;
617                         break;
618                 case SHT_RELA:
619                         ef->nrelatab++;
620                         break;
621                 case SHT_STRTAB:
622                         break;
623                 }
624         }
625         if (ef->nprogtab == 0) {
626                 link_elf_error(filename, "file has no contents");
627                 error = ENOEXEC;
628                 goto out;
629         }
630         if (nsym != 1) {
631                 /* Only allow one symbol table for now */
632                 link_elf_error(filename, "file has no valid symbol table");
633                 error = ENOEXEC;
634                 goto out;
635         }
636         if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
637             shdr[symstrindex].sh_type != SHT_STRTAB) {
638                 link_elf_error(filename, "file has invalid symbol strings");
639                 error = ENOEXEC;
640                 goto out;
641         }
642
643         /* Allocate space for tracking the load chunks */
644         if (ef->nprogtab != 0)
645                 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
646                     M_LINKER, M_WAITOK | M_ZERO);
647         if (ef->nreltab != 0)
648                 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
649                     M_LINKER, M_WAITOK | M_ZERO);
650         if (ef->nrelatab != 0)
651                 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
652                     M_LINKER, M_WAITOK | M_ZERO);
653
654         if (symtabindex == -1) {
655                 link_elf_error(filename, "lost symbol table index");
656                 error = ENOEXEC;
657                 goto out;
658         }
659         /* Allocate space for and load the symbol table */
660         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
661         ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
662         error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
663             shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
664             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
665             &resid, td);
666         if (error)
667                 goto out;
668         if (resid != 0){
669                 error = EINVAL;
670                 goto out;
671         }
672
673         if (symstrindex == -1) {
674                 link_elf_error(filename, "lost symbol string index");
675                 error = ENOEXEC;
676                 goto out;
677         }
678         /* Allocate space for and load the symbol strings */
679         ef->ddbstrcnt = shdr[symstrindex].sh_size;
680         ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
681         error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
682             shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
683             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
684             &resid, td);
685         if (error)
686                 goto out;
687         if (resid != 0){
688                 error = EINVAL;
689                 goto out;
690         }
691
692         /* Do we have a string table for the section names?  */
693         shstrindex = -1;
694         if (hdr->e_shstrndx != 0 &&
695             shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
696                 shstrindex = hdr->e_shstrndx;
697                 ef->shstrcnt = shdr[shstrindex].sh_size;
698                 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
699                     M_WAITOK);
700                 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
701                     shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
702                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
703                     &resid, td);
704                 if (error)
705                         goto out;
706                 if (resid != 0){
707                         error = EINVAL;
708                         goto out;
709                 }
710         }
711
712         /* Size up code/data(progbits) and bss(nobits). */
713         alignmask = 0;
714         for (i = 0; i < hdr->e_shnum; i++) {
715                 if (shdr[i].sh_size == 0)
716                         continue;
717                 switch (shdr[i].sh_type) {
718                 case SHT_PROGBITS:
719                 case SHT_NOBITS:
720 #ifdef __amd64__
721                 case SHT_X86_64_UNWIND:
722 #endif
723                         alignmask = shdr[i].sh_addralign - 1;
724                         mapsize += alignmask;
725                         mapsize &= ~alignmask;
726                         mapsize += shdr[i].sh_size;
727                         break;
728                 }
729         }
730
731         /*
732          * We know how much space we need for the text/data/bss/etc.
733          * This stuff needs to be in a single chunk so that profiling etc
734          * can get the bounds and gdb can associate offsets with modules
735          */
736         ef->object = vm_object_allocate(OBJT_DEFAULT,
737             round_page(mapsize) >> PAGE_SHIFT);
738         if (ef->object == NULL) {
739                 error = ENOMEM;
740                 goto out;
741         }
742         ef->address = (caddr_t) vm_map_min(kernel_map);
743
744         /*
745          * In order to satisfy amd64's architectural requirements on the
746          * location of code and data in the kernel's address space, request a
747          * mapping that is above the kernel.  
748          */
749 #ifdef __amd64__
750         mapbase = KERNBASE;
751 #else
752         mapbase = VM_MIN_KERNEL_ADDRESS;
753 #endif
754         error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
755             round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
756             VM_PROT_ALL, 0);
757         if (error) {
758                 vm_object_deallocate(ef->object);
759                 ef->object = 0;
760                 goto out;
761         }
762
763         /* Wire the pages */
764         error = vm_map_wire(kernel_map, mapbase,
765             mapbase + round_page(mapsize),
766             VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
767         if (error != KERN_SUCCESS) {
768                 error = ENOMEM;
769                 goto out;
770         }
771
772         /* Inform the kld system about the situation */
773         lf->address = ef->address = (caddr_t)mapbase;
774         lf->size = mapsize;
775
776         /*
777          * Now load code/data(progbits), zero bss(nobits), allocate space for
778          * and load relocs
779          */
780         pb = 0;
781         rl = 0;
782         ra = 0;
783         alignmask = 0;
784         for (i = 0; i < hdr->e_shnum; i++) {
785                 if (shdr[i].sh_size == 0)
786                         continue;
787                 switch (shdr[i].sh_type) {
788                 case SHT_PROGBITS:
789                 case SHT_NOBITS:
790 #ifdef __amd64__
791                 case SHT_X86_64_UNWIND:
792 #endif
793                         alignmask = shdr[i].sh_addralign - 1;
794                         mapbase += alignmask;
795                         mapbase &= ~alignmask;
796                         if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
797                                 ef->progtab[pb].name =
798                                     ef->shstrtab + shdr[i].sh_name;
799                                 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
800                                         lf->ctors_addr = (caddr_t)mapbase;
801                                         lf->ctors_size = shdr[i].sh_size;
802                                 }
803                         } else if (shdr[i].sh_type == SHT_PROGBITS)
804                                 ef->progtab[pb].name = "<<PROGBITS>>";
805 #ifdef __amd64__
806                         else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
807                                 ef->progtab[pb].name = "<<UNWIND>>";
808 #endif
809                         else
810                                 ef->progtab[pb].name = "<<NOBITS>>";
811                         if (ef->progtab[pb].name != NULL && 
812                             !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
813                                 ef->progtab[pb].addr =
814                                     dpcpu_alloc(shdr[i].sh_size);
815 #ifdef VIMAGE
816                         else if (ef->progtab[pb].name != NULL &&
817                             !strcmp(ef->progtab[pb].name, VNET_SETNAME))
818                                 ef->progtab[pb].addr =
819                                     vnet_data_alloc(shdr[i].sh_size);
820 #endif
821                         else
822                                 ef->progtab[pb].addr =
823                                     (void *)(uintptr_t)mapbase;
824                         if (ef->progtab[pb].addr == NULL) {
825                                 error = ENOSPC;
826                                 goto out;
827                         }
828                         ef->progtab[pb].size = shdr[i].sh_size;
829                         ef->progtab[pb].sec = i;
830                         if (shdr[i].sh_type == SHT_PROGBITS
831 #ifdef __amd64__
832                             || shdr[i].sh_type == SHT_X86_64_UNWIND
833 #endif
834                             ) {
835                                 error = vn_rdwr(UIO_READ, nd->ni_vp,
836                                     ef->progtab[pb].addr,
837                                     shdr[i].sh_size, shdr[i].sh_offset,
838                                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
839                                     NOCRED, &resid, td);
840                                 if (error)
841                                         goto out;
842                                 if (resid != 0){
843                                         error = EINVAL;
844                                         goto out;
845                                 }
846                                 /* Initialize the per-cpu or vnet area. */
847                                 if (ef->progtab[pb].addr != (void *)mapbase &&
848                                     !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
849                                         dpcpu_copy(ef->progtab[pb].addr,
850                                             shdr[i].sh_size);
851 #ifdef VIMAGE
852                                 else if (ef->progtab[pb].addr !=
853                                     (void *)mapbase &&
854                                     !strcmp(ef->progtab[pb].name, VNET_SETNAME))
855                                         vnet_data_copy(ef->progtab[pb].addr,
856                                             shdr[i].sh_size);
857 #endif
858                         } else
859                                 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
860
861                         /* Update all symbol values with the offset. */
862                         for (j = 0; j < ef->ddbsymcnt; j++) {
863                                 es = &ef->ddbsymtab[j];
864                                 if (es->st_shndx != i)
865                                         continue;
866                                 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
867                         }
868                         mapbase += shdr[i].sh_size;
869                         pb++;
870                         break;
871                 case SHT_REL:
872                         ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
873                             M_WAITOK);
874                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
875                         ef->reltab[rl].sec = shdr[i].sh_info;
876                         error = vn_rdwr(UIO_READ, nd->ni_vp,
877                             (void *)ef->reltab[rl].rel,
878                             shdr[i].sh_size, shdr[i].sh_offset,
879                             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
880                             &resid, td);
881                         if (error)
882                                 goto out;
883                         if (resid != 0){
884                                 error = EINVAL;
885                                 goto out;
886                         }
887                         rl++;
888                         break;
889                 case SHT_RELA:
890                         ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
891                             M_WAITOK);
892                         ef->relatab[ra].nrela =
893                             shdr[i].sh_size / sizeof(Elf_Rela);
894                         ef->relatab[ra].sec = shdr[i].sh_info;
895                         error = vn_rdwr(UIO_READ, nd->ni_vp,
896                             (void *)ef->relatab[ra].rela,
897                             shdr[i].sh_size, shdr[i].sh_offset,
898                             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
899                             &resid, td);
900                         if (error)
901                                 goto out;
902                         if (resid != 0){
903                                 error = EINVAL;
904                                 goto out;
905                         }
906                         ra++;
907                         break;
908                 }
909         }
910         if (pb != ef->nprogtab) {
911                 link_elf_error(filename, "lost progbits");
912                 error = ENOEXEC;
913                 goto out;
914         }
915         if (rl != ef->nreltab) {
916                 link_elf_error(filename, "lost reltab");
917                 error = ENOEXEC;
918                 goto out;
919         }
920         if (ra != ef->nrelatab) {
921                 link_elf_error(filename, "lost relatab");
922                 error = ENOEXEC;
923                 goto out;
924         }
925         if (mapbase != (vm_offset_t)ef->address + mapsize) {
926                 printf(
927                     "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
928                     filename != NULL ? filename : "<none>",
929                     (u_long)mapbase, ef->address, (u_long)mapsize,
930                     (u_long)(vm_offset_t)ef->address + mapsize);
931                 error = ENOMEM;
932                 goto out;
933         }
934
935         /* Local intra-module relocations */
936         error = link_elf_reloc_local(lf);
937         if (error != 0)
938                 goto out;
939
940         /* Pull in dependencies */
941         VOP_UNLOCK(nd->ni_vp, 0);
942         error = linker_load_dependencies(lf);
943         vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
944         if (error)
945                 goto out;
946
947         /* External relocations */
948         error = relocate_file(ef);
949         if (error)
950                 goto out;
951
952         /* Notify MD code that a module is being loaded. */
953         error = elf_cpu_load_file(lf);
954         if (error)
955                 goto out;
956
957         /* Invoke .ctors */
958         link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
959
960         *result = lf;
961
962 out:
963         VOP_UNLOCK(nd->ni_vp, 0);
964         vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
965         free(nd, M_TEMP);
966         if (error && lf)
967                 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
968         free(hdr, M_LINKER);
969
970         return error;
971 }
972
973 static void
974 link_elf_unload_file(linker_file_t file)
975 {
976         elf_file_t ef = (elf_file_t) file;
977         int i;
978
979         /* Notify MD code that a module is being unloaded. */
980         elf_cpu_unload_file(file);
981
982         if (ef->progtab) {
983                 for (i = 0; i < ef->nprogtab; i++) {
984                         if (ef->progtab[i].size == 0)
985                                 continue;
986                         if (ef->progtab[i].name == NULL)
987                                 continue;
988                         if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
989                                 dpcpu_free(ef->progtab[i].addr,
990                                     ef->progtab[i].size);
991 #ifdef VIMAGE
992                         else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
993                                 vnet_data_free(ef->progtab[i].addr,
994                                     ef->progtab[i].size);
995 #endif
996                 }
997         }
998         if (ef->preloaded) {
999                 free(ef->reltab, M_LINKER);
1000                 free(ef->relatab, M_LINKER);
1001                 free(ef->progtab, M_LINKER);
1002                 free(ef->ctftab, M_LINKER);
1003                 free(ef->ctfoff, M_LINKER);
1004                 free(ef->typoff, M_LINKER);
1005                 if (file->filename != NULL)
1006                         preload_delete_name(file->filename);
1007                 /* XXX reclaim module memory? */
1008                 return;
1009         }
1010
1011         for (i = 0; i < ef->nreltab; i++)
1012                 free(ef->reltab[i].rel, M_LINKER);
1013         for (i = 0; i < ef->nrelatab; i++)
1014                 free(ef->relatab[i].rela, M_LINKER);
1015         free(ef->reltab, M_LINKER);
1016         free(ef->relatab, M_LINKER);
1017         free(ef->progtab, M_LINKER);
1018
1019         if (ef->object) {
1020                 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
1021                     (vm_offset_t) ef->address +
1022                     (ef->object->size << PAGE_SHIFT));
1023         }
1024         free(ef->e_shdr, M_LINKER);
1025         free(ef->ddbsymtab, M_LINKER);
1026         free(ef->ddbstrtab, M_LINKER);
1027         free(ef->shstrtab, M_LINKER);
1028         free(ef->ctftab, M_LINKER);
1029         free(ef->ctfoff, M_LINKER);
1030         free(ef->typoff, M_LINKER);
1031 }
1032
1033 static const char *
1034 symbol_name(elf_file_t ef, Elf_Size r_info)
1035 {
1036         const Elf_Sym *ref;
1037
1038         if (ELF_R_SYM(r_info)) {
1039                 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1040                 return ef->ddbstrtab + ref->st_name;
1041         } else
1042                 return NULL;
1043 }
1044
1045 static Elf_Addr
1046 findbase(elf_file_t ef, int sec)
1047 {
1048         int i;
1049         Elf_Addr base = 0;
1050
1051         for (i = 0; i < ef->nprogtab; i++) {
1052                 if (sec == ef->progtab[i].sec) {
1053                         base = (Elf_Addr)ef->progtab[i].addr;
1054                         break;
1055                 }
1056         }
1057         return base;
1058 }
1059
1060 static int
1061 relocate_file(elf_file_t ef)
1062 {
1063         const Elf_Rel *rellim;
1064         const Elf_Rel *rel;
1065         const Elf_Rela *relalim;
1066         const Elf_Rela *rela;
1067         const char *symname;
1068         const Elf_Sym *sym;
1069         int i;
1070         Elf_Size symidx;
1071         Elf_Addr base;
1072
1073
1074         /* Perform relocations without addend if there are any: */
1075         for (i = 0; i < ef->nreltab; i++) {
1076                 rel = ef->reltab[i].rel;
1077                 if (rel == NULL) {
1078                         link_elf_error(ef->lf.filename, "lost a reltab!");
1079                         return (ENOEXEC);
1080                 }
1081                 rellim = rel + ef->reltab[i].nrel;
1082                 base = findbase(ef, ef->reltab[i].sec);
1083                 if (base == 0) {
1084                         link_elf_error(ef->lf.filename, "lost base for reltab");
1085                         return (ENOEXEC);
1086                 }
1087                 for ( ; rel < rellim; rel++) {
1088                         symidx = ELF_R_SYM(rel->r_info);
1089                         if (symidx >= ef->ddbsymcnt)
1090                                 continue;
1091                         sym = ef->ddbsymtab + symidx;
1092                         /* Local relocs are already done */
1093                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1094                                 continue;
1095                         if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1096                             elf_obj_lookup)) {
1097                                 symname = symbol_name(ef, rel->r_info);
1098                                 printf("link_elf_obj: symbol %s undefined\n",
1099                                     symname);
1100                                 return (ENOENT);
1101                         }
1102                 }
1103         }
1104
1105         /* Perform relocations with addend if there are any: */
1106         for (i = 0; i < ef->nrelatab; i++) {
1107                 rela = ef->relatab[i].rela;
1108                 if (rela == NULL) {
1109                         link_elf_error(ef->lf.filename, "lost a relatab!");
1110                         return (ENOEXEC);
1111                 }
1112                 relalim = rela + ef->relatab[i].nrela;
1113                 base = findbase(ef, ef->relatab[i].sec);
1114                 if (base == 0) {
1115                         link_elf_error(ef->lf.filename,
1116                             "lost base for relatab");
1117                         return (ENOEXEC);
1118                 }
1119                 for ( ; rela < relalim; rela++) {
1120                         symidx = ELF_R_SYM(rela->r_info);
1121                         if (symidx >= ef->ddbsymcnt)
1122                                 continue;
1123                         sym = ef->ddbsymtab + symidx;
1124                         /* Local relocs are already done */
1125                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1126                                 continue;
1127                         if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1128                             elf_obj_lookup)) {
1129                                 symname = symbol_name(ef, rela->r_info);
1130                                 printf("link_elf_obj: symbol %s undefined\n",
1131                                     symname);
1132                                 return (ENOENT);
1133                         }
1134                 }
1135         }
1136
1137         /*
1138          * Only clean SHN_FBSD_CACHED for successful return.  If we
1139          * modified symbol table for the object but found an
1140          * unresolved symbol, there is no reason to roll back.
1141          */
1142         elf_obj_cleanup_globals_cache(ef);
1143
1144         return (0);
1145 }
1146
1147 static int
1148 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1149 {
1150         elf_file_t ef = (elf_file_t) lf;
1151         const Elf_Sym *symp;
1152         const char *strp;
1153         int i;
1154
1155         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1156                 strp = ef->ddbstrtab + symp->st_name;
1157                 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1158                         *sym = (c_linker_sym_t) symp;
1159                         return 0;
1160                 }
1161         }
1162         return ENOENT;
1163 }
1164
1165 static int
1166 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1167     linker_symval_t *symval)
1168 {
1169         elf_file_t ef = (elf_file_t) lf;
1170         const Elf_Sym *es = (const Elf_Sym*) sym;
1171
1172         if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1173                 symval->name = ef->ddbstrtab + es->st_name;
1174                 symval->value = (caddr_t)es->st_value;
1175                 symval->size = es->st_size;
1176                 return 0;
1177         }
1178         return ENOENT;
1179 }
1180
1181 static int
1182 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1183     c_linker_sym_t *sym, long *diffp)
1184 {
1185         elf_file_t ef = (elf_file_t) lf;
1186         u_long off = (uintptr_t) (void *) value;
1187         u_long diff = off;
1188         u_long st_value;
1189         const Elf_Sym *es;
1190         const Elf_Sym *best = NULL;
1191         int i;
1192
1193         for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1194                 if (es->st_name == 0)
1195                         continue;
1196                 st_value = es->st_value;
1197                 if (off >= st_value) {
1198                         if (off - st_value < diff) {
1199                                 diff = off - st_value;
1200                                 best = es;
1201                                 if (diff == 0)
1202                                         break;
1203                         } else if (off - st_value == diff) {
1204                                 best = es;
1205                         }
1206                 }
1207         }
1208         if (best == NULL)
1209                 *diffp = off;
1210         else
1211                 *diffp = diff;
1212         *sym = (c_linker_sym_t) best;
1213
1214         return 0;
1215 }
1216
1217 /*
1218  * Look up a linker set on an ELF system.
1219  */
1220 static int
1221 link_elf_lookup_set(linker_file_t lf, const char *name,
1222     void ***startp, void ***stopp, int *countp)
1223 {
1224         elf_file_t ef = (elf_file_t)lf;
1225         void **start, **stop;
1226         int i, count;
1227
1228         /* Relative to section number */
1229         for (i = 0; i < ef->nprogtab; i++) {
1230                 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1231                     strcmp(ef->progtab[i].name + 4, name) == 0) {
1232                         start  = (void **)ef->progtab[i].addr;
1233                         stop = (void **)((char *)ef->progtab[i].addr +
1234                             ef->progtab[i].size);
1235                         count = stop - start;
1236                         if (startp)
1237                                 *startp = start;
1238                         if (stopp)
1239                                 *stopp = stop;
1240                         if (countp)
1241                                 *countp = count;
1242                         return (0);
1243                 }
1244         }
1245         return (ESRCH);
1246 }
1247
1248 static int
1249 link_elf_each_function_name(linker_file_t file,
1250     int (*callback)(const char *, void *), void *opaque)
1251 {
1252         elf_file_t ef = (elf_file_t)file;
1253         const Elf_Sym *symp;
1254         int i, error;
1255         
1256         /* Exhaustive search */
1257         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1258                 if (symp->st_value != 0 &&
1259                     ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1260                         error = callback(ef->ddbstrtab + symp->st_name, opaque);
1261                         if (error)
1262                                 return (error);
1263                 }
1264         }
1265         return (0);
1266 }
1267
1268 static int
1269 link_elf_each_function_nameval(linker_file_t file,
1270     linker_function_nameval_callback_t callback, void *opaque)
1271 {
1272         linker_symval_t symval;
1273         elf_file_t ef = (elf_file_t)file;
1274         const Elf_Sym* symp;
1275         int i, error;
1276
1277         /* Exhaustive search */
1278         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1279                 if (symp->st_value != 0 &&
1280                     ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1281                         error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1282                         if (error)
1283                                 return (error);
1284                         error = callback(file, i, &symval, opaque);
1285                         if (error)
1286                                 return (error);
1287                 }
1288         }
1289         return (0);
1290 }
1291
1292 static void
1293 elf_obj_cleanup_globals_cache(elf_file_t ef)
1294 {
1295         Elf_Sym *sym;
1296         Elf_Size i;
1297
1298         for (i = 0; i < ef->ddbsymcnt; i++) {
1299                 sym = ef->ddbsymtab + i;
1300                 if (sym->st_shndx == SHN_FBSD_CACHED) {
1301                         sym->st_shndx = SHN_UNDEF;
1302                         sym->st_value = 0;
1303                 }
1304         }
1305 }
1306
1307 /*
1308  * Symbol lookup function that can be used when the symbol index is known (ie
1309  * in relocations). It uses the symbol index instead of doing a fully fledged
1310  * hash table based lookup when such is valid. For example for local symbols.
1311  * This is not only more efficient, it's also more correct. It's not always
1312  * the case that the symbol can be found through the hash table.
1313  */
1314 static int
1315 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1316 {
1317         elf_file_t ef = (elf_file_t)lf;
1318         Elf_Sym *sym;
1319         const char *symbol;
1320         Elf_Addr res1;
1321
1322         /* Don't even try to lookup the symbol if the index is bogus. */
1323         if (symidx >= ef->ddbsymcnt) {
1324                 *res = 0;
1325                 return (EINVAL);
1326         }
1327
1328         sym = ef->ddbsymtab + symidx;
1329
1330         /* Quick answer if there is a definition included. */
1331         if (sym->st_shndx != SHN_UNDEF) {
1332                 *res = sym->st_value;
1333                 return (0);
1334         }
1335
1336         /* If we get here, then it is undefined and needs a lookup. */
1337         switch (ELF_ST_BIND(sym->st_info)) {
1338         case STB_LOCAL:
1339                 /* Local, but undefined? huh? */
1340                 *res = 0;
1341                 return (EINVAL);
1342
1343         case STB_GLOBAL:
1344         case STB_WEAK:
1345                 /* Relative to Data or Function name */
1346                 symbol = ef->ddbstrtab + sym->st_name;
1347
1348                 /* Force a lookup failure if the symbol name is bogus. */
1349                 if (*symbol == 0) {
1350                         *res = 0;
1351                         return (EINVAL);
1352                 }
1353                 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1354
1355                 /*
1356                  * Cache global lookups during module relocation. The failure
1357                  * case is particularly expensive for callers, who must scan
1358                  * through the entire globals table doing strcmp(). Cache to
1359                  * avoid doing such work repeatedly.
1360                  *
1361                  * After relocation is complete, undefined globals will be
1362                  * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1363                  * above.
1364                  */
1365                 if (res1 != 0) {
1366                         sym->st_shndx = SHN_FBSD_CACHED;
1367                         sym->st_value = res1;
1368                         *res = res1;
1369                         return (0);
1370                 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1371                         sym->st_value = 0;
1372                         *res = 0;
1373                         return (0);
1374                 }
1375                 return (EINVAL);
1376
1377         default:
1378                 return (EINVAL);
1379         }
1380 }
1381
1382 static void
1383 link_elf_fix_link_set(elf_file_t ef)
1384 {
1385         static const char startn[] = "__start_";
1386         static const char stopn[] = "__stop_";
1387         Elf_Sym *sym;
1388         const char *sym_name, *linkset_name;
1389         Elf_Addr startp, stopp;
1390         Elf_Size symidx;
1391         int start, i;
1392
1393         startp = stopp = 0;
1394         for (symidx = 1 /* zero entry is special */;
1395                 symidx < ef->ddbsymcnt; symidx++) {
1396                 sym = ef->ddbsymtab + symidx;
1397                 if (sym->st_shndx != SHN_UNDEF)
1398                         continue;
1399
1400                 sym_name = ef->ddbstrtab + sym->st_name;
1401                 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1402                         start = 1;
1403                         linkset_name = sym_name + sizeof(startn) - 1;
1404                 }
1405                 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1406                         start = 0;
1407                         linkset_name = sym_name + sizeof(stopn) - 1;
1408                 }
1409                 else
1410                         continue;
1411
1412                 for (i = 0; i < ef->nprogtab; i++) {
1413                         if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1414                                 startp = (Elf_Addr)ef->progtab[i].addr;
1415                                 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1416                                 break;
1417                         }
1418                 }
1419                 if (i == ef->nprogtab)
1420                         continue;
1421
1422                 sym->st_value = start ? startp : stopp;
1423                 sym->st_shndx = i;
1424         }
1425 }
1426
1427 static int
1428 link_elf_reloc_local(linker_file_t lf)
1429 {
1430         elf_file_t ef = (elf_file_t)lf;
1431         const Elf_Rel *rellim;
1432         const Elf_Rel *rel;
1433         const Elf_Rela *relalim;
1434         const Elf_Rela *rela;
1435         const Elf_Sym *sym;
1436         Elf_Addr base;
1437         int i;
1438         Elf_Size symidx;
1439
1440         link_elf_fix_link_set(ef);
1441
1442         /* Perform relocations without addend if there are any: */
1443         for (i = 0; i < ef->nreltab; i++) {
1444                 rel = ef->reltab[i].rel;
1445                 if (rel == NULL) {
1446                         link_elf_error(ef->lf.filename, "lost a reltab");
1447                         return (ENOEXEC);
1448                 }
1449                 rellim = rel + ef->reltab[i].nrel;
1450                 base = findbase(ef, ef->reltab[i].sec);
1451                 if (base == 0) {
1452                         link_elf_error(ef->lf.filename, "lost base for reltab");
1453                         return (ENOEXEC);
1454                 }
1455                 for ( ; rel < rellim; rel++) {
1456                         symidx = ELF_R_SYM(rel->r_info);
1457                         if (symidx >= ef->ddbsymcnt)
1458                                 continue;
1459                         sym = ef->ddbsymtab + symidx;
1460                         /* Only do local relocs */
1461                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1462                                 continue;
1463                         elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1464                             elf_obj_lookup);
1465                 }
1466         }
1467
1468         /* Perform relocations with addend if there are any: */
1469         for (i = 0; i < ef->nrelatab; i++) {
1470                 rela = ef->relatab[i].rela;
1471                 if (rela == NULL) {
1472                         link_elf_error(ef->lf.filename, "lost a relatab!");
1473                         return (ENOEXEC);
1474                 }
1475                 relalim = rela + ef->relatab[i].nrela;
1476                 base = findbase(ef, ef->relatab[i].sec);
1477                 if (base == 0) {
1478                         link_elf_error(ef->lf.filename, "lost base for reltab");
1479                         return (ENOEXEC);
1480                 }
1481                 for ( ; rela < relalim; rela++) {
1482                         symidx = ELF_R_SYM(rela->r_info);
1483                         if (symidx >= ef->ddbsymcnt)
1484                                 continue;
1485                         sym = ef->ddbsymtab + symidx;
1486                         /* Only do local relocs */
1487                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1488                                 continue;
1489                         elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1490                             elf_obj_lookup);
1491                 }
1492         }
1493         return (0);
1494 }
1495
1496 static long
1497 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1498 {
1499     elf_file_t ef = (elf_file_t)lf;
1500     
1501     *symtab = ef->ddbsymtab;
1502     
1503     if (*symtab == NULL)
1504         return (0);
1505
1506     return (ef->ddbsymcnt);
1507 }
1508     
1509 static long
1510 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1511 {
1512     elf_file_t ef = (elf_file_t)lf;
1513
1514     *strtab = ef->ddbstrtab;
1515
1516     if (*strtab == NULL)
1517         return (0);
1518
1519     return (ef->ddbstrcnt);
1520 }