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