]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/link_elf_obj.c
MFC r358586:
[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;  /* Section flags. */
74         int             sec;    /* Original section number. */
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         u_int           nprogtab;
101
102         Elf_relaent     *relatab;
103         u_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, bool);
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         KOBJMETHOD_END
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, NULL);
198
199 static void
200 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
201     vm_prot_t prot)
202 {
203         int error __unused;
204
205         KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
206             end <= round_page((vm_offset_t)ef->address + ef->lf.size),
207             ("link_elf_protect_range: invalid range %#jx-%#jx",
208             (uintmax_t)start, (uintmax_t)end));
209
210         if (start == end)
211                 return;
212         error = vm_map_protect(kernel_map, start, end, prot, FALSE);
213         KASSERT(error == KERN_SUCCESS,
214             ("link_elf_protect_range: vm_map_protect() returned %d", error));
215 }
216
217 /*
218  * Restrict permissions on linker file memory based on section flags.
219  * Sections need not be page-aligned, so overlap within a page is possible.
220  */
221 static void
222 link_elf_protect(elf_file_t ef)
223 {
224         vm_offset_t end, segend, segstart, start;
225         vm_prot_t gapprot, prot, segprot;
226         int i;
227
228         /*
229          * If the file was preloaded, the last page may contain other preloaded
230          * data which may need to be writeable.  ELF files are always
231          * page-aligned, but other preloaded data, such as entropy or CPU
232          * microcode may be loaded with a smaller alignment.
233          */
234         gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
235
236         start = end = (vm_offset_t)ef->address;
237         prot = VM_PROT_READ;
238         for (i = 0; i < ef->nprogtab; i++) {
239                 /*
240                  * VNET and DPCPU sections have their memory allocated by their
241                  * respective subsystems.
242                  */
243                 if (ef->progtab[i].name != NULL && (
244 #ifdef VIMAGE
245                     strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
246 #endif
247                     strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
248                         continue;
249
250                 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
251                 segend = round_page((vm_offset_t)ef->progtab[i].addr +
252                     ef->progtab[i].size);
253                 segprot = VM_PROT_READ;
254                 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
255                         segprot |= VM_PROT_WRITE;
256                 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
257                         segprot |= VM_PROT_EXECUTE;
258
259                 if (end <= segstart) {
260                         /*
261                          * Case 1: there is no overlap between the previous
262                          * segment and this one.  Apply protections to the
263                          * previous segment, and protect the gap between the
264                          * previous and current segments, if any.
265                          */
266                         link_elf_protect_range(ef, start, end, prot);
267                         link_elf_protect_range(ef, end, segstart, gapprot);
268
269                         start = segstart;
270                         end = segend;
271                         prot = segprot;
272                 } else if (start < segstart && end == segend) {
273                         /*
274                          * Case 2: the current segment is a subrange of the
275                          * previous segment.  Apply protections to the
276                          * non-overlapping portion of the previous segment.
277                          */
278                         link_elf_protect_range(ef, start, segstart, prot);
279
280                         start = segstart;
281                         prot |= segprot;
282                 } else if (end < segend) {
283                         /*
284                          * Case 3: there is partial overlap between the previous
285                          * and current segments.  Apply protections to the
286                          * non-overlapping portion of the previous segment, and
287                          * then the overlap, which must use the union of the two
288                          * segments' protections.
289                          */
290                         link_elf_protect_range(ef, start, segstart, prot);
291                         link_elf_protect_range(ef, segstart, end,
292                             prot | segprot);
293                         start = end;
294                         end = segend;
295                         prot = segprot;
296                 } else {
297                         /*
298                          * Case 4: the two segments reside in the same page.
299                          */
300                         prot |= segprot;
301                 }
302         }
303
304         /*
305          * Fix up the last unprotected segment and trailing data.
306          */
307         link_elf_protect_range(ef, start, end, prot);
308         link_elf_protect_range(ef, end,
309             round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
310 }
311
312 static int
313 link_elf_link_preload(linker_class_t cls, const char *filename,
314     linker_file_t *result)
315 {
316         Elf_Ehdr *hdr;
317         Elf_Shdr *shdr;
318         Elf_Sym *es;
319         void *modptr, *baseptr, *sizeptr;
320         char *type;
321         elf_file_t ef;
322         linker_file_t lf;
323         Elf_Addr off;
324         int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
325
326         /* Look to see if we have the file preloaded */
327         modptr = preload_search_by_name(filename);
328         if (modptr == NULL)
329                 return ENOENT;
330
331         type = (char *)preload_search_info(modptr, MODINFO_TYPE);
332         baseptr = preload_search_info(modptr, MODINFO_ADDR);
333         sizeptr = preload_search_info(modptr, MODINFO_SIZE);
334         hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
335             MODINFOMD_ELFHDR);
336         shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
337             MODINFOMD_SHDR);
338         if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
339             " obj module") != 0 &&
340             strcmp(type, "elf obj module") != 0)) {
341                 return (EFTYPE);
342         }
343         if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
344             shdr == NULL)
345                 return (EINVAL);
346
347         lf = linker_make_file(filename, &link_elf_class);
348         if (lf == NULL)
349                 return (ENOMEM);
350
351         ef = (elf_file_t)lf;
352         ef->preloaded = 1;
353         ef->address = *(caddr_t *)baseptr;
354         lf->address = *(caddr_t *)baseptr;
355         lf->size = *(size_t *)sizeptr;
356
357         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
358             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
359             hdr->e_ident[EI_VERSION] != EV_CURRENT ||
360             hdr->e_version != EV_CURRENT ||
361             hdr->e_type != ET_REL ||
362             hdr->e_machine != ELF_TARG_MACH) {
363                 error = EFTYPE;
364                 goto out;
365         }
366         ef->e_shdr = shdr;
367
368         /* Scan the section header for information and table sizing. */
369         symtabindex = -1;
370         symstrindex = -1;
371         for (i = 0; i < hdr->e_shnum; i++) {
372                 switch (shdr[i].sh_type) {
373                 case SHT_PROGBITS:
374                 case SHT_NOBITS:
375 #ifdef __amd64__
376                 case SHT_X86_64_UNWIND:
377 #endif
378                         /* Ignore sections not loaded by the loader. */
379                         if (shdr[i].sh_addr == 0)
380                                 break;
381                         ef->nprogtab++;
382                         break;
383                 case SHT_SYMTAB:
384                         symtabindex = i;
385                         symstrindex = shdr[i].sh_link;
386                         break;
387                 case SHT_REL:
388                         /*
389                          * Ignore relocation tables for sections not
390                          * loaded by the loader.
391                          */
392                         if (shdr[shdr[i].sh_info].sh_addr == 0)
393                                 break;
394                         ef->nreltab++;
395                         break;
396                 case SHT_RELA:
397                         if (shdr[shdr[i].sh_info].sh_addr == 0)
398                                 break;
399                         ef->nrelatab++;
400                         break;
401                 }
402         }
403
404         shstrindex = hdr->e_shstrndx;
405         if (ef->nprogtab == 0 || symstrindex < 0 ||
406             symstrindex >= hdr->e_shnum ||
407             shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
408             shstrindex >= hdr->e_shnum ||
409             shdr[shstrindex].sh_type != SHT_STRTAB) {
410                 printf("%s: bad/missing section headers\n", filename);
411                 error = ENOEXEC;
412                 goto out;
413         }
414
415         /* Allocate space for tracking the load chunks */
416         if (ef->nprogtab != 0)
417                 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
418                     M_LINKER, M_WAITOK | M_ZERO);
419         if (ef->nreltab != 0)
420                 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
421                     M_LINKER, M_WAITOK | M_ZERO);
422         if (ef->nrelatab != 0)
423                 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
424                     M_LINKER, M_WAITOK | M_ZERO);
425         if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
426             (ef->nreltab != 0 && ef->reltab == NULL) ||
427             (ef->nrelatab != 0 && ef->relatab == NULL)) {
428                 error = ENOMEM;
429                 goto out;
430         }
431
432         /* XXX, relocate the sh_addr fields saved by the loader. */
433         off = 0;
434         for (i = 0; i < hdr->e_shnum; i++) {
435                 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
436                         off = shdr[i].sh_addr;
437         }
438         for (i = 0; i < hdr->e_shnum; i++) {
439                 if (shdr[i].sh_addr != 0)
440                         shdr[i].sh_addr = shdr[i].sh_addr - off +
441                             (Elf_Addr)ef->address;
442         }
443
444         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
445         ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
446         ef->ddbstrcnt = shdr[symstrindex].sh_size;
447         ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
448         ef->shstrcnt = shdr[shstrindex].sh_size;
449         ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
450
451         /* Now fill out progtab and the relocation tables. */
452         pb = 0;
453         rl = 0;
454         ra = 0;
455         for (i = 0; i < hdr->e_shnum; i++) {
456                 switch (shdr[i].sh_type) {
457                 case SHT_PROGBITS:
458                 case SHT_NOBITS:
459 #ifdef __amd64__
460                 case SHT_X86_64_UNWIND:
461 #endif
462                         if (shdr[i].sh_addr == 0)
463                                 break;
464                         ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
465                         if (shdr[i].sh_type == SHT_PROGBITS)
466                                 ef->progtab[pb].name = "<<PROGBITS>>";
467 #ifdef __amd64__
468                         else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
469                                 ef->progtab[pb].name = "<<UNWIND>>";
470 #endif
471                         else
472                                 ef->progtab[pb].name = "<<NOBITS>>";
473                         ef->progtab[pb].size = shdr[i].sh_size;
474                         ef->progtab[pb].flags = shdr[i].sh_flags;
475                         ef->progtab[pb].sec = i;
476                         if (ef->shstrtab && shdr[i].sh_name != 0)
477                                 ef->progtab[pb].name =
478                                     ef->shstrtab + shdr[i].sh_name;
479                         if (ef->progtab[pb].name != NULL && 
480                             !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
481                                 void *dpcpu;
482
483                                 dpcpu = dpcpu_alloc(shdr[i].sh_size);
484                                 if (dpcpu == NULL) {
485                                         printf("%s: pcpu module space is out "
486                                             "of space; cannot allocate %#jx "
487                                             "for %s\n", __func__,
488                                             (uintmax_t)shdr[i].sh_size,
489                                             filename);
490                                         error = ENOSPC;
491                                         goto out;
492                                 }
493                                 memcpy(dpcpu, ef->progtab[pb].addr,
494                                     ef->progtab[pb].size);
495                                 dpcpu_copy(dpcpu, shdr[i].sh_size);
496                                 ef->progtab[pb].addr = dpcpu;
497 #ifdef VIMAGE
498                         } else if (ef->progtab[pb].name != NULL &&
499                             !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
500                                 void *vnet_data;
501
502                                 vnet_data = vnet_data_alloc(shdr[i].sh_size);
503                                 if (vnet_data == NULL) {
504                                         printf("%s: vnet module space is out "
505                                             "of space; cannot allocate %#jx "
506                                             "for %s\n", __func__,
507                                             (uintmax_t)shdr[i].sh_size,
508                                             filename);
509                                         error = ENOSPC;
510                                         goto out;
511                                 }
512                                 memcpy(vnet_data, ef->progtab[pb].addr,
513                                     ef->progtab[pb].size);
514                                 vnet_data_copy(vnet_data, shdr[i].sh_size);
515                                 ef->progtab[pb].addr = vnet_data;
516 #endif
517                         } else if (ef->progtab[pb].name != NULL &&
518                             !strcmp(ef->progtab[pb].name, ".ctors")) {
519                                 lf->ctors_addr = ef->progtab[pb].addr;
520                                 lf->ctors_size = shdr[i].sh_size;
521                         }
522
523                         /* Update all symbol values with the offset. */
524                         for (j = 0; j < ef->ddbsymcnt; j++) {
525                                 es = &ef->ddbsymtab[j];
526                                 if (es->st_shndx != i)
527                                         continue;
528                                 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
529                         }
530                         pb++;
531                         break;
532                 case SHT_REL:
533                         if (shdr[shdr[i].sh_info].sh_addr == 0)
534                                 break;
535                         ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
536                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
537                         ef->reltab[rl].sec = shdr[i].sh_info;
538                         rl++;
539                         break;
540                 case SHT_RELA:
541                         if (shdr[shdr[i].sh_info].sh_addr == 0)
542                                 break;
543                         ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
544                         ef->relatab[ra].nrela =
545                             shdr[i].sh_size / sizeof(Elf_Rela);
546                         ef->relatab[ra].sec = shdr[i].sh_info;
547                         ra++;
548                         break;
549                 }
550         }
551         if (pb != ef->nprogtab) {
552                 printf("%s: lost progbits\n", filename);
553                 error = ENOEXEC;
554                 goto out;
555         }
556         if (rl != ef->nreltab) {
557                 printf("%s: lost reltab\n", filename);
558                 error = ENOEXEC;
559                 goto out;
560         }
561         if (ra != ef->nrelatab) {
562                 printf("%s: lost relatab\n", filename);
563                 error = ENOEXEC;
564                 goto out;
565         }
566
567         /* Local intra-module relocations */
568         error = link_elf_reloc_local(lf, false);
569         if (error != 0)
570                 goto out;
571         *result = lf;
572         return (0);
573
574 out:
575         /* preload not done this way */
576         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
577         return (error);
578 }
579
580 static void
581 link_elf_invoke_ctors(caddr_t addr, size_t size)
582 {
583         void (**ctor)(void);
584         size_t i, cnt;
585
586         if (addr == NULL || size == 0)
587                 return;
588         cnt = size / sizeof(*ctor);
589         ctor = (void *)addr;
590         for (i = 0; i < cnt; i++) {
591                 if (ctor[i] != NULL)
592                         (*ctor[i])();
593         }
594 }
595
596 static int
597 link_elf_link_preload_finish(linker_file_t lf)
598 {
599         elf_file_t ef;
600         int error;
601
602         ef = (elf_file_t)lf;
603         error = relocate_file(ef);
604         if (error)
605                 return (error);
606
607         /* Notify MD code that a module is being loaded. */
608         error = elf_cpu_load_file(lf);
609         if (error)
610                 return (error);
611
612 #if defined(__i386__) || defined(__amd64__)
613         /* Now ifuncs. */
614         error = link_elf_reloc_local(lf, true);
615         if (error != 0)
616                 return (error);
617 #endif
618
619         /* Invoke .ctors */
620         link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
621         return (0);
622 }
623
624 static int
625 link_elf_load_file(linker_class_t cls, const char *filename,
626     linker_file_t *result)
627 {
628         struct nameidata *nd;
629         struct thread *td = curthread;  /* XXX */
630         Elf_Ehdr *hdr;
631         Elf_Shdr *shdr;
632         Elf_Sym *es;
633         int nbytes, i, j;
634         vm_offset_t mapbase;
635         size_t mapsize;
636         int error = 0;
637         ssize_t resid;
638         int flags;
639         elf_file_t ef;
640         linker_file_t lf;
641         int symtabindex;
642         int symstrindex;
643         int shstrindex;
644         int nsym;
645         int pb, rl, ra;
646         int alignmask;
647
648         shdr = NULL;
649         lf = NULL;
650         mapsize = 0;
651         hdr = NULL;
652
653         nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
654         NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
655         flags = FREAD;
656         error = vn_open(nd, &flags, 0, NULL);
657         if (error) {
658                 free(nd, M_TEMP);
659                 return error;
660         }
661         NDFREE(nd, NDF_ONLY_PNBUF);
662         if (nd->ni_vp->v_type != VREG) {
663                 error = ENOEXEC;
664                 goto out;
665         }
666 #ifdef MAC
667         error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
668         if (error) {
669                 goto out;
670         }
671 #endif
672
673         /* Read the elf header from the file. */
674         hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
675         error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
676             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
677             &resid, td);
678         if (error)
679                 goto out;
680         if (resid != 0){
681                 error = ENOEXEC;
682                 goto out;
683         }
684
685         if (!IS_ELF(*hdr)) {
686                 error = ENOEXEC;
687                 goto out;
688         }
689
690         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
691             || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
692                 link_elf_error(filename, "Unsupported file layout");
693                 error = ENOEXEC;
694                 goto out;
695         }
696         if (hdr->e_ident[EI_VERSION] != EV_CURRENT
697             || hdr->e_version != EV_CURRENT) {
698                 link_elf_error(filename, "Unsupported file version");
699                 error = ENOEXEC;
700                 goto out;
701         }
702         if (hdr->e_type != ET_REL) {
703                 error = ENOSYS;
704                 goto out;
705         }
706         if (hdr->e_machine != ELF_TARG_MACH) {
707                 link_elf_error(filename, "Unsupported machine");
708                 error = ENOEXEC;
709                 goto out;
710         }
711
712         lf = linker_make_file(filename, &link_elf_class);
713         if (!lf) {
714                 error = ENOMEM;
715                 goto out;
716         }
717         ef = (elf_file_t) lf;
718         ef->nprogtab = 0;
719         ef->e_shdr = 0;
720         ef->nreltab = 0;
721         ef->nrelatab = 0;
722
723         /* Allocate and read in the section header */
724         nbytes = hdr->e_shnum * hdr->e_shentsize;
725         if (nbytes == 0 || hdr->e_shoff == 0 ||
726             hdr->e_shentsize != sizeof(Elf_Shdr)) {
727                 error = ENOEXEC;
728                 goto out;
729         }
730         shdr = malloc(nbytes, M_LINKER, M_WAITOK);
731         ef->e_shdr = shdr;
732         error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
733             hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
734             NOCRED, &resid, td);
735         if (error)
736                 goto out;
737         if (resid) {
738                 error = ENOEXEC;
739                 goto out;
740         }
741
742         /* Scan the section header for information and table sizing. */
743         nsym = 0;
744         symtabindex = -1;
745         symstrindex = -1;
746         for (i = 0; i < hdr->e_shnum; i++) {
747                 if (shdr[i].sh_size == 0)
748                         continue;
749                 switch (shdr[i].sh_type) {
750                 case SHT_PROGBITS:
751                 case SHT_NOBITS:
752 #ifdef __amd64__
753                 case SHT_X86_64_UNWIND:
754 #endif
755                         if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
756                                 break;
757                         ef->nprogtab++;
758                         break;
759                 case SHT_SYMTAB:
760                         nsym++;
761                         symtabindex = i;
762                         symstrindex = shdr[i].sh_link;
763                         break;
764                 case SHT_REL:
765                         /*
766                          * Ignore relocation tables for unallocated
767                          * sections.
768                          */
769                         if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
770                                 break;
771                         ef->nreltab++;
772                         break;
773                 case SHT_RELA:
774                         if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
775                                 break;
776                         ef->nrelatab++;
777                         break;
778                 case SHT_STRTAB:
779                         break;
780                 }
781         }
782         if (ef->nprogtab == 0) {
783                 link_elf_error(filename, "file has no contents");
784                 error = ENOEXEC;
785                 goto out;
786         }
787         if (nsym != 1) {
788                 /* Only allow one symbol table for now */
789                 link_elf_error(filename,
790                     "file must have exactly one symbol table");
791                 error = ENOEXEC;
792                 goto out;
793         }
794         if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
795             shdr[symstrindex].sh_type != SHT_STRTAB) {
796                 link_elf_error(filename, "file has invalid symbol strings");
797                 error = ENOEXEC;
798                 goto out;
799         }
800
801         /* Allocate space for tracking the load chunks */
802         if (ef->nprogtab != 0)
803                 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
804                     M_LINKER, M_WAITOK | M_ZERO);
805         if (ef->nreltab != 0)
806                 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
807                     M_LINKER, M_WAITOK | M_ZERO);
808         if (ef->nrelatab != 0)
809                 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
810                     M_LINKER, M_WAITOK | M_ZERO);
811
812         if (symtabindex == -1) {
813                 link_elf_error(filename, "lost symbol table index");
814                 error = ENOEXEC;
815                 goto out;
816         }
817         /* Allocate space for and load the symbol table */
818         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
819         ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
820         error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
821             shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
822             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
823             &resid, td);
824         if (error)
825                 goto out;
826         if (resid != 0){
827                 error = EINVAL;
828                 goto out;
829         }
830
831         /* Allocate space for and load the symbol strings */
832         ef->ddbstrcnt = shdr[symstrindex].sh_size;
833         ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
834         error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
835             shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
836             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
837             &resid, td);
838         if (error)
839                 goto out;
840         if (resid != 0){
841                 error = EINVAL;
842                 goto out;
843         }
844
845         /* Do we have a string table for the section names?  */
846         shstrindex = -1;
847         if (hdr->e_shstrndx != 0 &&
848             shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
849                 shstrindex = hdr->e_shstrndx;
850                 ef->shstrcnt = shdr[shstrindex].sh_size;
851                 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
852                     M_WAITOK);
853                 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
854                     shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
855                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
856                     &resid, td);
857                 if (error)
858                         goto out;
859                 if (resid != 0){
860                         error = EINVAL;
861                         goto out;
862                 }
863         }
864
865         /* Size up code/data(progbits) and bss(nobits). */
866         alignmask = 0;
867         for (i = 0; i < hdr->e_shnum; i++) {
868                 if (shdr[i].sh_size == 0)
869                         continue;
870                 switch (shdr[i].sh_type) {
871                 case SHT_PROGBITS:
872                 case SHT_NOBITS:
873 #ifdef __amd64__
874                 case SHT_X86_64_UNWIND:
875 #endif
876                         if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
877                                 break;
878                         alignmask = shdr[i].sh_addralign - 1;
879                         mapsize += alignmask;
880                         mapsize &= ~alignmask;
881                         mapsize += shdr[i].sh_size;
882                         break;
883                 }
884         }
885
886         /*
887          * We know how much space we need for the text/data/bss/etc.
888          * This stuff needs to be in a single chunk so that profiling etc
889          * can get the bounds and gdb can associate offsets with modules
890          */
891         ef->object = vm_object_allocate(OBJT_PHYS, atop(round_page(mapsize)));
892         if (ef->object == NULL) {
893                 error = ENOMEM;
894                 goto out;
895         }
896
897         /*
898          * In order to satisfy amd64's architectural requirements on the
899          * location of code and data in the kernel's address space, request a
900          * mapping that is above the kernel.
901          *
902          * Protections will be restricted once relocations are applied.
903          */
904 #ifdef __amd64__
905         mapbase = KERNBASE;
906 #else
907         mapbase = VM_MIN_KERNEL_ADDRESS;
908 #endif
909         error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
910             round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
911             VM_PROT_ALL, 0);
912         if (error != KERN_SUCCESS) {
913                 vm_object_deallocate(ef->object);
914                 ef->object = NULL;
915                 error = ENOMEM;
916                 goto out;
917         }
918
919         /* Wire the pages */
920         error = vm_map_wire(kernel_map, mapbase,
921             mapbase + round_page(mapsize),
922             VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
923         if (error != KERN_SUCCESS) {
924                 error = ENOMEM;
925                 goto out;
926         }
927
928         /* Inform the kld system about the situation */
929         lf->address = ef->address = (caddr_t)mapbase;
930         lf->size = mapsize;
931
932         /*
933          * Now load code/data(progbits), zero bss(nobits), allocate space for
934          * and load relocs
935          */
936         pb = 0;
937         rl = 0;
938         ra = 0;
939         alignmask = 0;
940         for (i = 0; i < hdr->e_shnum; i++) {
941                 if (shdr[i].sh_size == 0)
942                         continue;
943                 switch (shdr[i].sh_type) {
944                 case SHT_PROGBITS:
945                 case SHT_NOBITS:
946 #ifdef __amd64__
947                 case SHT_X86_64_UNWIND:
948 #endif
949                         if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
950                                 break;
951                         alignmask = shdr[i].sh_addralign - 1;
952                         mapbase += alignmask;
953                         mapbase &= ~alignmask;
954                         if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
955                                 ef->progtab[pb].name =
956                                     ef->shstrtab + shdr[i].sh_name;
957                                 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
958                                         lf->ctors_addr = (caddr_t)mapbase;
959                                         lf->ctors_size = shdr[i].sh_size;
960                                 }
961                         } else if (shdr[i].sh_type == SHT_PROGBITS)
962                                 ef->progtab[pb].name = "<<PROGBITS>>";
963 #ifdef __amd64__
964                         else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
965                                 ef->progtab[pb].name = "<<UNWIND>>";
966 #endif
967                         else
968                                 ef->progtab[pb].name = "<<NOBITS>>";
969                         if (ef->progtab[pb].name != NULL && 
970                             !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
971                                 ef->progtab[pb].addr =
972                                     dpcpu_alloc(shdr[i].sh_size);
973                                 if (ef->progtab[pb].addr == NULL) {
974                                         printf("%s: pcpu module space is out "
975                                             "of space; cannot allocate %#jx "
976                                             "for %s\n", __func__,
977                                             (uintmax_t)shdr[i].sh_size,
978                                             filename);
979                                 }
980                         }
981 #ifdef VIMAGE
982                         else if (ef->progtab[pb].name != NULL &&
983                             !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
984                                 ef->progtab[pb].addr =
985                                     vnet_data_alloc(shdr[i].sh_size);
986                                 if (ef->progtab[pb].addr == NULL) {
987                                         printf("%s: vnet module space is out "
988                                             "of space; cannot allocate %#jx "
989                                             "for %s\n", __func__,
990                                             (uintmax_t)shdr[i].sh_size,
991                                             filename);
992                                 }
993                         }
994 #endif
995                         else
996                                 ef->progtab[pb].addr =
997                                     (void *)(uintptr_t)mapbase;
998                         if (ef->progtab[pb].addr == NULL) {
999                                 error = ENOSPC;
1000                                 goto out;
1001                         }
1002                         ef->progtab[pb].size = shdr[i].sh_size;
1003                         ef->progtab[pb].flags = shdr[i].sh_flags;
1004                         ef->progtab[pb].sec = i;
1005                         if (shdr[i].sh_type == SHT_PROGBITS
1006 #ifdef __amd64__
1007                             || shdr[i].sh_type == SHT_X86_64_UNWIND
1008 #endif
1009                             ) {
1010                                 error = vn_rdwr(UIO_READ, nd->ni_vp,
1011                                     ef->progtab[pb].addr,
1012                                     shdr[i].sh_size, shdr[i].sh_offset,
1013                                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1014                                     NOCRED, &resid, td);
1015                                 if (error)
1016                                         goto out;
1017                                 if (resid != 0){
1018                                         error = EINVAL;
1019                                         goto out;
1020                                 }
1021                                 /* Initialize the per-cpu or vnet area. */
1022                                 if (ef->progtab[pb].addr != (void *)mapbase &&
1023                                     !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1024                                         dpcpu_copy(ef->progtab[pb].addr,
1025                                             shdr[i].sh_size);
1026 #ifdef VIMAGE
1027                                 else if (ef->progtab[pb].addr !=
1028                                     (void *)mapbase &&
1029                                     !strcmp(ef->progtab[pb].name, VNET_SETNAME))
1030                                         vnet_data_copy(ef->progtab[pb].addr,
1031                                             shdr[i].sh_size);
1032 #endif
1033                         } else
1034                                 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1035
1036                         /* Update all symbol values with the offset. */
1037                         for (j = 0; j < ef->ddbsymcnt; j++) {
1038                                 es = &ef->ddbsymtab[j];
1039                                 if (es->st_shndx != i)
1040                                         continue;
1041                                 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1042                         }
1043                         mapbase += shdr[i].sh_size;
1044                         pb++;
1045                         break;
1046                 case SHT_REL:
1047                         if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1048                                 break;
1049                         ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1050                             M_WAITOK);
1051                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1052                         ef->reltab[rl].sec = shdr[i].sh_info;
1053                         error = vn_rdwr(UIO_READ, nd->ni_vp,
1054                             (void *)ef->reltab[rl].rel,
1055                             shdr[i].sh_size, shdr[i].sh_offset,
1056                             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1057                             &resid, td);
1058                         if (error)
1059                                 goto out;
1060                         if (resid != 0){
1061                                 error = EINVAL;
1062                                 goto out;
1063                         }
1064                         rl++;
1065                         break;
1066                 case SHT_RELA:
1067                         if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1068                                 break;
1069                         ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1070                             M_WAITOK);
1071                         ef->relatab[ra].nrela =
1072                             shdr[i].sh_size / sizeof(Elf_Rela);
1073                         ef->relatab[ra].sec = shdr[i].sh_info;
1074                         error = vn_rdwr(UIO_READ, nd->ni_vp,
1075                             (void *)ef->relatab[ra].rela,
1076                             shdr[i].sh_size, shdr[i].sh_offset,
1077                             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1078                             &resid, td);
1079                         if (error)
1080                                 goto out;
1081                         if (resid != 0){
1082                                 error = EINVAL;
1083                                 goto out;
1084                         }
1085                         ra++;
1086                         break;
1087                 }
1088         }
1089         if (pb != ef->nprogtab) {
1090                 link_elf_error(filename, "lost progbits");
1091                 error = ENOEXEC;
1092                 goto out;
1093         }
1094         if (rl != ef->nreltab) {
1095                 link_elf_error(filename, "lost reltab");
1096                 error = ENOEXEC;
1097                 goto out;
1098         }
1099         if (ra != ef->nrelatab) {
1100                 link_elf_error(filename, "lost relatab");
1101                 error = ENOEXEC;
1102                 goto out;
1103         }
1104         if (mapbase != (vm_offset_t)ef->address + mapsize) {
1105                 printf(
1106                     "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1107                     filename != NULL ? filename : "<none>",
1108                     (u_long)mapbase, ef->address, (u_long)mapsize,
1109                     (u_long)(vm_offset_t)ef->address + mapsize);
1110                 error = ENOMEM;
1111                 goto out;
1112         }
1113
1114         /* Local intra-module relocations */
1115         error = link_elf_reloc_local(lf, false);
1116         if (error != 0)
1117                 goto out;
1118
1119         /* Pull in dependencies */
1120         VOP_UNLOCK(nd->ni_vp, 0);
1121         error = linker_load_dependencies(lf);
1122         vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1123         if (error)
1124                 goto out;
1125
1126         /* External relocations */
1127         error = relocate_file(ef);
1128         if (error)
1129                 goto out;
1130
1131         /* Notify MD code that a module is being loaded. */
1132         error = elf_cpu_load_file(lf);
1133         if (error)
1134                 goto out;
1135
1136 #if defined(__i386__) || defined(__amd64__)
1137         /* Now ifuncs. */
1138         error = link_elf_reloc_local(lf, true);
1139         if (error != 0)
1140                 goto out;
1141 #endif
1142
1143         link_elf_protect(ef);
1144         link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
1145         *result = lf;
1146
1147 out:
1148         VOP_UNLOCK(nd->ni_vp, 0);
1149         vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1150         free(nd, M_TEMP);
1151         if (error && lf)
1152                 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1153         free(hdr, M_LINKER);
1154
1155         return error;
1156 }
1157
1158 static void
1159 link_elf_unload_file(linker_file_t file)
1160 {
1161         elf_file_t ef = (elf_file_t) file;
1162         u_int i;
1163
1164         /* Notify MD code that a module is being unloaded. */
1165         elf_cpu_unload_file(file);
1166
1167         if (ef->progtab) {
1168                 for (i = 0; i < ef->nprogtab; i++) {
1169                         if (ef->progtab[i].size == 0)
1170                                 continue;
1171                         if (ef->progtab[i].name == NULL)
1172                                 continue;
1173                         if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1174                                 dpcpu_free(ef->progtab[i].addr,
1175                                     ef->progtab[i].size);
1176 #ifdef VIMAGE
1177                         else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1178                                 vnet_data_free(ef->progtab[i].addr,
1179                                     ef->progtab[i].size);
1180 #endif
1181                 }
1182         }
1183         if (ef->preloaded) {
1184                 free(ef->reltab, M_LINKER);
1185                 free(ef->relatab, M_LINKER);
1186                 free(ef->progtab, M_LINKER);
1187                 free(ef->ctftab, M_LINKER);
1188                 free(ef->ctfoff, M_LINKER);
1189                 free(ef->typoff, M_LINKER);
1190                 if (file->pathname != NULL)
1191                         preload_delete_name(file->pathname);
1192                 return;
1193         }
1194
1195         for (i = 0; i < ef->nreltab; i++)
1196                 free(ef->reltab[i].rel, M_LINKER);
1197         for (i = 0; i < ef->nrelatab; i++)
1198                 free(ef->relatab[i].rela, M_LINKER);
1199         free(ef->reltab, M_LINKER);
1200         free(ef->relatab, M_LINKER);
1201         free(ef->progtab, M_LINKER);
1202
1203         if (ef->object != NULL)
1204                 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1205                     (vm_offset_t)ef->address + ptoa(ef->object->size));
1206         free(ef->e_shdr, M_LINKER);
1207         free(ef->ddbsymtab, M_LINKER);
1208         free(ef->ddbstrtab, M_LINKER);
1209         free(ef->shstrtab, M_LINKER);
1210         free(ef->ctftab, M_LINKER);
1211         free(ef->ctfoff, M_LINKER);
1212         free(ef->typoff, M_LINKER);
1213 }
1214
1215 static const char *
1216 symbol_name(elf_file_t ef, Elf_Size r_info)
1217 {
1218         const Elf_Sym *ref;
1219
1220         if (ELF_R_SYM(r_info)) {
1221                 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1222                 return ef->ddbstrtab + ref->st_name;
1223         } else
1224                 return NULL;
1225 }
1226
1227 static Elf_Addr
1228 findbase(elf_file_t ef, int sec)
1229 {
1230         int i;
1231         Elf_Addr base = 0;
1232
1233         for (i = 0; i < ef->nprogtab; i++) {
1234                 if (sec == ef->progtab[i].sec) {
1235                         base = (Elf_Addr)ef->progtab[i].addr;
1236                         break;
1237                 }
1238         }
1239         return base;
1240 }
1241
1242 static int
1243 relocate_file(elf_file_t ef)
1244 {
1245         const Elf_Rel *rellim;
1246         const Elf_Rel *rel;
1247         const Elf_Rela *relalim;
1248         const Elf_Rela *rela;
1249         const char *symname;
1250         const Elf_Sym *sym;
1251         int i;
1252         Elf_Size symidx;
1253         Elf_Addr base;
1254
1255
1256         /* Perform relocations without addend if there are any: */
1257         for (i = 0; i < ef->nreltab; i++) {
1258                 rel = ef->reltab[i].rel;
1259                 if (rel == NULL) {
1260                         link_elf_error(ef->lf.filename, "lost a reltab!");
1261                         return (ENOEXEC);
1262                 }
1263                 rellim = rel + ef->reltab[i].nrel;
1264                 base = findbase(ef, ef->reltab[i].sec);
1265                 if (base == 0) {
1266                         link_elf_error(ef->lf.filename, "lost base for reltab");
1267                         return (ENOEXEC);
1268                 }
1269                 for ( ; rel < rellim; rel++) {
1270                         symidx = ELF_R_SYM(rel->r_info);
1271                         if (symidx >= ef->ddbsymcnt)
1272                                 continue;
1273                         sym = ef->ddbsymtab + symidx;
1274                         /* Local relocs are already done */
1275                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1276                                 continue;
1277                         if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1278                             elf_obj_lookup)) {
1279                                 symname = symbol_name(ef, rel->r_info);
1280                                 printf("link_elf_obj: symbol %s undefined\n",
1281                                     symname);
1282                                 return (ENOENT);
1283                         }
1284                 }
1285         }
1286
1287         /* Perform relocations with addend if there are any: */
1288         for (i = 0; i < ef->nrelatab; i++) {
1289                 rela = ef->relatab[i].rela;
1290                 if (rela == NULL) {
1291                         link_elf_error(ef->lf.filename, "lost a relatab!");
1292                         return (ENOEXEC);
1293                 }
1294                 relalim = rela + ef->relatab[i].nrela;
1295                 base = findbase(ef, ef->relatab[i].sec);
1296                 if (base == 0) {
1297                         link_elf_error(ef->lf.filename,
1298                             "lost base for relatab");
1299                         return (ENOEXEC);
1300                 }
1301                 for ( ; rela < relalim; rela++) {
1302                         symidx = ELF_R_SYM(rela->r_info);
1303                         if (symidx >= ef->ddbsymcnt)
1304                                 continue;
1305                         sym = ef->ddbsymtab + symidx;
1306                         /* Local relocs are already done */
1307                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1308                                 continue;
1309                         if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1310                             elf_obj_lookup)) {
1311                                 symname = symbol_name(ef, rela->r_info);
1312                                 printf("link_elf_obj: symbol %s undefined\n",
1313                                     symname);
1314                                 return (ENOENT);
1315                         }
1316                 }
1317         }
1318
1319         /*
1320          * Only clean SHN_FBSD_CACHED for successful return.  If we
1321          * modified symbol table for the object but found an
1322          * unresolved symbol, there is no reason to roll back.
1323          */
1324         elf_obj_cleanup_globals_cache(ef);
1325
1326         return (0);
1327 }
1328
1329 static int
1330 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1331 {
1332         elf_file_t ef = (elf_file_t) lf;
1333         const Elf_Sym *symp;
1334         const char *strp;
1335         int i;
1336
1337         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1338                 strp = ef->ddbstrtab + symp->st_name;
1339                 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1340                         *sym = (c_linker_sym_t) symp;
1341                         return 0;
1342                 }
1343         }
1344         return ENOENT;
1345 }
1346
1347 static int
1348 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1349     linker_symval_t *symval)
1350 {
1351         elf_file_t ef;
1352         const Elf_Sym *es;
1353         caddr_t val;
1354
1355         ef = (elf_file_t) lf;
1356         es = (const Elf_Sym*) sym;
1357         val = (caddr_t)es->st_value;
1358         if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1359                 symval->name = ef->ddbstrtab + es->st_name;
1360                 val = (caddr_t)es->st_value;
1361                 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1362                         val = ((caddr_t (*)(void))val)();
1363                 symval->value = val;
1364                 symval->size = es->st_size;
1365                 return 0;
1366         }
1367         return ENOENT;
1368 }
1369
1370 static int
1371 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1372     c_linker_sym_t *sym, long *diffp)
1373 {
1374         elf_file_t ef = (elf_file_t) lf;
1375         u_long off = (uintptr_t) (void *) value;
1376         u_long diff = off;
1377         u_long st_value;
1378         const Elf_Sym *es;
1379         const Elf_Sym *best = NULL;
1380         int i;
1381
1382         for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1383                 if (es->st_name == 0)
1384                         continue;
1385                 st_value = es->st_value;
1386                 if (off >= st_value) {
1387                         if (off - st_value < diff) {
1388                                 diff = off - st_value;
1389                                 best = es;
1390                                 if (diff == 0)
1391                                         break;
1392                         } else if (off - st_value == diff) {
1393                                 best = es;
1394                         }
1395                 }
1396         }
1397         if (best == NULL)
1398                 *diffp = off;
1399         else
1400                 *diffp = diff;
1401         *sym = (c_linker_sym_t) best;
1402
1403         return 0;
1404 }
1405
1406 /*
1407  * Look up a linker set on an ELF system.
1408  */
1409 static int
1410 link_elf_lookup_set(linker_file_t lf, const char *name,
1411     void ***startp, void ***stopp, int *countp)
1412 {
1413         elf_file_t ef = (elf_file_t)lf;
1414         void **start, **stop;
1415         int i, count;
1416
1417         /* Relative to section number */
1418         for (i = 0; i < ef->nprogtab; i++) {
1419                 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1420                     strcmp(ef->progtab[i].name + 4, name) == 0) {
1421                         start  = (void **)ef->progtab[i].addr;
1422                         stop = (void **)((char *)ef->progtab[i].addr +
1423                             ef->progtab[i].size);
1424                         count = stop - start;
1425                         if (startp)
1426                                 *startp = start;
1427                         if (stopp)
1428                                 *stopp = stop;
1429                         if (countp)
1430                                 *countp = count;
1431                         return (0);
1432                 }
1433         }
1434         return (ESRCH);
1435 }
1436
1437 static int
1438 link_elf_each_function_name(linker_file_t file,
1439     int (*callback)(const char *, void *), void *opaque)
1440 {
1441         elf_file_t ef = (elf_file_t)file;
1442         const Elf_Sym *symp;
1443         int i, error;
1444         
1445         /* Exhaustive search */
1446         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1447                 if (symp->st_value != 0 &&
1448                     (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1449                     ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1450                         error = callback(ef->ddbstrtab + symp->st_name, opaque);
1451                         if (error)
1452                                 return (error);
1453                 }
1454         }
1455         return (0);
1456 }
1457
1458 static int
1459 link_elf_each_function_nameval(linker_file_t file,
1460     linker_function_nameval_callback_t callback, void *opaque)
1461 {
1462         linker_symval_t symval;
1463         elf_file_t ef = (elf_file_t)file;
1464         const Elf_Sym* symp;
1465         int i, error;
1466
1467         /* Exhaustive search */
1468         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1469                 if (symp->st_value != 0 &&
1470                     (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1471                     ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1472                         error = link_elf_symbol_values(file,
1473                             (c_linker_sym_t)symp, &symval);
1474                         if (error)
1475                                 return (error);
1476                         error = callback(file, i, &symval, opaque);
1477                         if (error)
1478                                 return (error);
1479                 }
1480         }
1481         return (0);
1482 }
1483
1484 static void
1485 elf_obj_cleanup_globals_cache(elf_file_t ef)
1486 {
1487         Elf_Sym *sym;
1488         Elf_Size i;
1489
1490         for (i = 0; i < ef->ddbsymcnt; i++) {
1491                 sym = ef->ddbsymtab + i;
1492                 if (sym->st_shndx == SHN_FBSD_CACHED) {
1493                         sym->st_shndx = SHN_UNDEF;
1494                         sym->st_value = 0;
1495                 }
1496         }
1497 }
1498
1499 /*
1500  * Symbol lookup function that can be used when the symbol index is known (ie
1501  * in relocations). It uses the symbol index instead of doing a fully fledged
1502  * hash table based lookup when such is valid. For example for local symbols.
1503  * This is not only more efficient, it's also more correct. It's not always
1504  * the case that the symbol can be found through the hash table.
1505  */
1506 static int
1507 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1508 {
1509         elf_file_t ef = (elf_file_t)lf;
1510         Elf_Sym *sym;
1511         const char *symbol;
1512         Elf_Addr res1;
1513
1514         /* Don't even try to lookup the symbol if the index is bogus. */
1515         if (symidx >= ef->ddbsymcnt) {
1516                 *res = 0;
1517                 return (EINVAL);
1518         }
1519
1520         sym = ef->ddbsymtab + symidx;
1521
1522         /* Quick answer if there is a definition included. */
1523         if (sym->st_shndx != SHN_UNDEF) {
1524                 res1 = (Elf_Addr)sym->st_value;
1525                 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1526                         res1 = ((Elf_Addr (*)(void))res1)();
1527                 *res = res1;
1528                 return (0);
1529         }
1530
1531         /* If we get here, then it is undefined and needs a lookup. */
1532         switch (ELF_ST_BIND(sym->st_info)) {
1533         case STB_LOCAL:
1534                 /* Local, but undefined? huh? */
1535                 *res = 0;
1536                 return (EINVAL);
1537
1538         case STB_GLOBAL:
1539         case STB_WEAK:
1540                 /* Relative to Data or Function name */
1541                 symbol = ef->ddbstrtab + sym->st_name;
1542
1543                 /* Force a lookup failure if the symbol name is bogus. */
1544                 if (*symbol == 0) {
1545                         *res = 0;
1546                         return (EINVAL);
1547                 }
1548                 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1549
1550                 /*
1551                  * Cache global lookups during module relocation. The failure
1552                  * case is particularly expensive for callers, who must scan
1553                  * through the entire globals table doing strcmp(). Cache to
1554                  * avoid doing such work repeatedly.
1555                  *
1556                  * After relocation is complete, undefined globals will be
1557                  * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1558                  * above.
1559                  */
1560                 if (res1 != 0) {
1561                         sym->st_shndx = SHN_FBSD_CACHED;
1562                         sym->st_value = res1;
1563                         *res = res1;
1564                         return (0);
1565                 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1566                         sym->st_value = 0;
1567                         *res = 0;
1568                         return (0);
1569                 }
1570                 return (EINVAL);
1571
1572         default:
1573                 return (EINVAL);
1574         }
1575 }
1576
1577 static void
1578 link_elf_fix_link_set(elf_file_t ef)
1579 {
1580         static const char startn[] = "__start_";
1581         static const char stopn[] = "__stop_";
1582         Elf_Sym *sym;
1583         const char *sym_name, *linkset_name;
1584         Elf_Addr startp, stopp;
1585         Elf_Size symidx;
1586         int start, i;
1587
1588         startp = stopp = 0;
1589         for (symidx = 1 /* zero entry is special */;
1590                 symidx < ef->ddbsymcnt; symidx++) {
1591                 sym = ef->ddbsymtab + symidx;
1592                 if (sym->st_shndx != SHN_UNDEF)
1593                         continue;
1594
1595                 sym_name = ef->ddbstrtab + sym->st_name;
1596                 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1597                         start = 1;
1598                         linkset_name = sym_name + sizeof(startn) - 1;
1599                 }
1600                 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1601                         start = 0;
1602                         linkset_name = sym_name + sizeof(stopn) - 1;
1603                 }
1604                 else
1605                         continue;
1606
1607                 for (i = 0; i < ef->nprogtab; i++) {
1608                         if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1609                                 startp = (Elf_Addr)ef->progtab[i].addr;
1610                                 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1611                                 break;
1612                         }
1613                 }
1614                 if (i == ef->nprogtab)
1615                         continue;
1616
1617                 sym->st_value = start ? startp : stopp;
1618                 sym->st_shndx = i;
1619         }
1620 }
1621
1622 static int
1623 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1624 {
1625         elf_file_t ef = (elf_file_t)lf;
1626         const Elf_Rel *rellim;
1627         const Elf_Rel *rel;
1628         const Elf_Rela *relalim;
1629         const Elf_Rela *rela;
1630         const Elf_Sym *sym;
1631         Elf_Addr base;
1632         int i;
1633         Elf_Size symidx;
1634
1635         link_elf_fix_link_set(ef);
1636
1637         /* Perform relocations without addend if there are any: */
1638         for (i = 0; i < ef->nreltab; i++) {
1639                 rel = ef->reltab[i].rel;
1640                 if (rel == NULL) {
1641                         link_elf_error(ef->lf.filename, "lost a reltab");
1642                         return (ENOEXEC);
1643                 }
1644                 rellim = rel + ef->reltab[i].nrel;
1645                 base = findbase(ef, ef->reltab[i].sec);
1646                 if (base == 0) {
1647                         link_elf_error(ef->lf.filename, "lost base for reltab");
1648                         return (ENOEXEC);
1649                 }
1650                 for ( ; rel < rellim; rel++) {
1651                         symidx = ELF_R_SYM(rel->r_info);
1652                         if (symidx >= ef->ddbsymcnt)
1653                                 continue;
1654                         sym = ef->ddbsymtab + symidx;
1655                         /* Only do local relocs */
1656                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1657                                 continue;
1658                         if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1659                             elf_is_ifunc_reloc(rel->r_info)) == ifuncs)
1660                                 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1661                                     elf_obj_lookup);
1662                 }
1663         }
1664
1665         /* Perform relocations with addend if there are any: */
1666         for (i = 0; i < ef->nrelatab; i++) {
1667                 rela = ef->relatab[i].rela;
1668                 if (rela == NULL) {
1669                         link_elf_error(ef->lf.filename, "lost a relatab!");
1670                         return (ENOEXEC);
1671                 }
1672                 relalim = rela + ef->relatab[i].nrela;
1673                 base = findbase(ef, ef->relatab[i].sec);
1674                 if (base == 0) {
1675                         link_elf_error(ef->lf.filename, "lost base for reltab");
1676                         return (ENOEXEC);
1677                 }
1678                 for ( ; rela < relalim; rela++) {
1679                         symidx = ELF_R_SYM(rela->r_info);
1680                         if (symidx >= ef->ddbsymcnt)
1681                                 continue;
1682                         sym = ef->ddbsymtab + symidx;
1683                         /* Only do local relocs */
1684                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1685                                 continue;
1686                         if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1687                             elf_is_ifunc_reloc(rela->r_info)) == ifuncs)
1688                                 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1689                                     elf_obj_lookup);
1690                 }
1691         }
1692         return (0);
1693 }
1694
1695 static long
1696 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1697 {
1698     elf_file_t ef = (elf_file_t)lf;
1699     
1700     *symtab = ef->ddbsymtab;
1701     
1702     if (*symtab == NULL)
1703         return (0);
1704
1705     return (ef->ddbsymcnt);
1706 }
1707     
1708 static long
1709 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1710 {
1711     elf_file_t ef = (elf_file_t)lf;
1712
1713     *strtab = ef->ddbstrtab;
1714
1715     if (*strtab == NULL)
1716         return (0);
1717
1718     return (ef->ddbstrcnt);
1719 }