]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/common/load_elf.c
Merge llvm-project release/17.x llvmorg-17.0.1-25-g098e653a5bed
[FreeBSD/FreeBSD.git] / stand / common / load_elf.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/param.h>
29 #include <sys/endian.h>
30 #include <sys/exec.h>
31 #include <sys/linker.h>
32 #include <sys/module.h>
33 #include <sys/stdint.h>
34 #include <string.h>
35 #include <machine/elf.h>
36 #include <stand.h>
37 #define FREEBSD_ELF
38 #include <sys/link_elf.h>
39
40 #include "bootstrap.h"
41
42 #define COPYOUT(s,d,l)  archsw.arch_copyout((vm_offset_t)(s), d, l)
43
44 #if defined(__i386__) && __ELF_WORD_SIZE == 64
45 #undef ELF_TARG_CLASS
46 #undef ELF_TARG_MACH
47 #define ELF_TARG_CLASS  ELFCLASS64
48 #define ELF_TARG_MACH   EM_X86_64
49 #endif
50
51 typedef struct elf_file {
52         Elf_Phdr        *ph;
53         Elf_Ehdr        *ehdr;
54         Elf_Sym         *symtab;
55         Elf_Hashelt     *hashtab;
56         Elf_Hashelt     nbuckets;
57         Elf_Hashelt     nchains;
58         Elf_Hashelt     *buckets;
59         Elf_Hashelt     *chains;
60         Elf_Rel *rel;
61         size_t  relsz;
62         Elf_Rela        *rela;
63         size_t  relasz;
64         char    *strtab;
65         size_t  strsz;
66         int             fd;
67         caddr_t firstpage;
68         size_t  firstlen;
69         int             kernel;
70         uint64_t        off;
71 #ifdef LOADER_VERIEXEC_VECTX
72         struct vectx    *vctx;
73 #endif
74 } *elf_file_t;
75
76 #ifdef LOADER_VERIEXEC_VECTX
77 #define VECTX_HANDLE(ef) (ef)->vctx
78 #else
79 #define VECTX_HANDLE(ef) (ef)->fd
80 #endif
81
82 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef,
83     uint64_t loadaddr);
84 static int __elfN(lookup_symbol)(elf_file_t ef, const char* name,
85     Elf_Sym *sym, unsigned char type);
86 static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
87     Elf_Addr p, void *val, size_t len);
88 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
89     Elf_Addr p_start, Elf_Addr p_end);
90 static symaddr_fn __elfN(symaddr);
91 static char     *fake_modname(const char *name);
92
93 const char      *__elfN(kerneltype) = "elf kernel";
94 const char      *__elfN(moduletype) = "elf module";
95
96 uint64_t        __elfN(relocation_offset) = 0;
97
98 #ifdef __powerpc__
99 extern void elf_wrong_field_size(void);
100 #define CONVERT_FIELD(b, f, e)                  \
101         switch (sizeof((b)->f)) {               \
102         case 2:                                 \
103                 (b)->f = e ## 16toh((b)->f);    \
104                 break;                          \
105         case 4:                                 \
106                 (b)->f = e ## 32toh((b)->f);    \
107                 break;                          \
108         case 8:                                 \
109                 (b)->f = e ## 64toh((b)->f);    \
110                 break;                          \
111         default:                                \
112                 /* Force a link time error. */  \
113                 elf_wrong_field_size();         \
114                 break;                          \
115         }
116
117 #define CONVERT_SWITCH(h, d, f)                 \
118         switch ((h)->e_ident[EI_DATA]) {        \
119         case ELFDATA2MSB:                       \
120                 f(d, be);                       \
121                 break;                          \
122         case ELFDATA2LSB:                       \
123                 f(d, le);                       \
124                 break;                          \
125         default:                                \
126                 return (EINVAL);                \
127         }
128
129
130 static int elf_header_convert(Elf_Ehdr *ehdr)
131 {
132         /*
133          * Fixup ELF header endianness.
134          *
135          * The Xhdr structure was loaded using block read call to optimize file
136          * accesses. It might happen, that the endianness of the system memory
137          * is different that endianness of the ELF header.  Swap fields here to
138          * guarantee that Xhdr always contain valid data regardless of
139          * architecture.
140          */
141 #define HEADER_FIELDS(b, e)                     \
142         CONVERT_FIELD(b, e_type, e);            \
143         CONVERT_FIELD(b, e_machine, e);         \
144         CONVERT_FIELD(b, e_version, e);         \
145         CONVERT_FIELD(b, e_entry, e);           \
146         CONVERT_FIELD(b, e_phoff, e);           \
147         CONVERT_FIELD(b, e_shoff, e);           \
148         CONVERT_FIELD(b, e_flags, e);           \
149         CONVERT_FIELD(b, e_ehsize, e);          \
150         CONVERT_FIELD(b, e_phentsize, e);       \
151         CONVERT_FIELD(b, e_phnum, e);           \
152         CONVERT_FIELD(b, e_shentsize, e);       \
153         CONVERT_FIELD(b, e_shnum, e);           \
154         CONVERT_FIELD(b, e_shstrndx, e)
155
156         CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS);
157
158 #undef HEADER_FIELDS
159
160         return (0);
161 }
162
163 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
164 {
165 #define PROGRAM_HEADER_FIELDS(b, e)             \
166         CONVERT_FIELD(b, p_type, e);            \
167         CONVERT_FIELD(b, p_flags, e);           \
168         CONVERT_FIELD(b, p_offset, e);          \
169         CONVERT_FIELD(b, p_vaddr, e);           \
170         CONVERT_FIELD(b, p_paddr, e);           \
171         CONVERT_FIELD(b, p_filesz, e);          \
172         CONVERT_FIELD(b, p_memsz, e);           \
173         CONVERT_FIELD(b, p_align, e)
174
175         CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS);
176
177 #undef PROGRAM_HEADER_FIELDS
178
179         return (0);
180 }
181
182 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
183 {
184 #define SECTION_HEADER_FIELDS(b, e)             \
185         CONVERT_FIELD(b, sh_name, e);           \
186         CONVERT_FIELD(b, sh_type, e);           \
187         CONVERT_FIELD(b, sh_link, e);           \
188         CONVERT_FIELD(b, sh_info, e);           \
189         CONVERT_FIELD(b, sh_flags, e);          \
190         CONVERT_FIELD(b, sh_addr, e);           \
191         CONVERT_FIELD(b, sh_offset, e);         \
192         CONVERT_FIELD(b, sh_size, e);           \
193         CONVERT_FIELD(b, sh_addralign, e);      \
194         CONVERT_FIELD(b, sh_entsize, e)
195
196         CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS);
197
198 #undef SECTION_HEADER_FIELDS
199
200         return (0);
201 }
202 #undef CONVERT_SWITCH
203 #undef CONVERT_FIELD
204 #else
205 static int elf_header_convert(Elf_Ehdr *ehdr)
206 {
207         return (0);
208 }
209
210 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
211 {
212         return (0);
213 }
214
215 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
216 {
217         return (0);
218 }
219 #endif
220
221 #ifdef __amd64__
222 static bool
223 is_kernphys_relocatable(elf_file_t ef)
224 {
225         Elf_Sym sym;
226
227         return (__elfN(lookup_symbol)(ef, "kernphys", &sym, STT_OBJECT) == 0);
228 }
229 #endif
230
231 #ifdef __i386__
232 static bool
233 is_tg_kernel_support(struct preloaded_file *fp, elf_file_t ef)
234 {
235         Elf_Sym         sym;
236         Elf_Addr        p_start, p_end, v, p;
237         char            vd_name[16];
238         int             error;
239
240         if (__elfN(lookup_symbol)(ef, "__start_set_vt_drv_set", &sym, STT_NOTYPE) != 0)
241                 return (false);
242         p_start = sym.st_value + ef->off;
243         if (__elfN(lookup_symbol)(ef, "__stop_set_vt_drv_set", &sym, STT_NOTYPE) != 0)
244                 return (false);
245         p_end = sym.st_value + ef->off;
246
247         /*
248          * Walk through vt_drv_set, each vt driver structure starts with
249          * static 16 chars for driver name. If we have "vbefb", return true.
250          */
251         for (p = p_start; p < p_end; p += sizeof(Elf_Addr)) {
252                 COPYOUT(p, &v, sizeof(v));
253
254                 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
255                 if (error == EOPNOTSUPP)
256                         v += ef->off;
257                 else if (error != 0)
258                         return (false);
259                 COPYOUT(v, &vd_name, sizeof(vd_name));
260                 if (strncmp(vd_name, "vbefb", sizeof(vd_name)) == 0)
261                         return (true);
262         }
263
264         return (false);
265 }
266 #endif
267
268 static int
269 __elfN(load_elf_header)(char *filename, elf_file_t ef)
270 {
271         ssize_t                  bytes_read;
272         Elf_Ehdr                *ehdr;
273         int                      err;
274
275         /*
276          * Open the image, read and validate the ELF header
277          */
278         if (filename == NULL)   /* can't handle nameless */
279                 return (EFTYPE);
280         if ((ef->fd = open(filename, O_RDONLY)) == -1)
281                 return (errno);
282         ef->firstpage = malloc(PAGE_SIZE);
283         if (ef->firstpage == NULL) {
284                 close(ef->fd);
285                 return (ENOMEM);
286         }
287         preload(ef->fd);
288 #ifdef LOADER_VERIEXEC_VECTX
289         {
290                 int verror;
291
292                 ef->vctx = vectx_open(ef->fd, filename, 0L, NULL, &verror, __func__);
293                 if (verror) {
294                         printf("Unverified %s: %s\n", filename, ve_error_get());
295                         close(ef->fd);
296                         free(ef->vctx);
297                         return (EAUTH);
298                 }
299         }
300 #endif
301         bytes_read = VECTX_READ(VECTX_HANDLE(ef), ef->firstpage, PAGE_SIZE);
302         ef->firstlen = (size_t)bytes_read;
303         if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) {
304                 err = EFTYPE; /* could be EIO, but may be small file */
305                 goto error;
306         }
307         ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;
308
309         /* Is it ELF? */
310         if (!IS_ELF(*ehdr)) {
311                 err = EFTYPE;
312                 goto error;
313         }
314
315         if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
316             ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
317             ehdr->e_ident[EI_VERSION] != EV_CURRENT) /* Version ? */ {
318                 err = EFTYPE;
319                 goto error;
320         }
321
322         err = elf_header_convert(ehdr);
323         if (err)
324                 goto error;
325
326         if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) {
327                 /* Machine ? */
328                 err = EFTYPE;
329                 goto error;
330         }
331
332 #if defined(LOADER_VERIEXEC) && !defined(LOADER_VERIEXEC_VECTX)
333         if (verify_file(ef->fd, filename, bytes_read, VE_MUST, __func__) < 0) {
334                 err = EAUTH;
335                 goto error;
336         }
337 #endif
338         return (0);
339
340 error:
341         if (ef->firstpage != NULL) {
342                 free(ef->firstpage);
343                 ef->firstpage = NULL;
344         }
345         if (ef->fd != -1) {
346 #ifdef LOADER_VERIEXEC_VECTX
347                 free(ef->vctx);
348 #endif
349                 close(ef->fd);
350                 ef->fd = -1;
351         }
352         return (err);
353 }
354
355 /*
356  * Attempt to load the file (file) as an ELF module.  It will be stored at
357  * (dest), and a pointer to a module structure describing the loaded object
358  * will be saved in (result).
359  */
360 int
361 __elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result)
362 {
363         return (__elfN(loadfile_raw)(filename, dest, result, 0));
364 }
365
366 int
367 __elfN(loadfile_raw)(char *filename, uint64_t dest,
368     struct preloaded_file **result, int multiboot)
369 {
370         struct preloaded_file   *fp, *kfp;
371         struct elf_file         ef;
372         Elf_Ehdr                *ehdr;
373         int                     err;
374
375         fp = NULL;
376         bzero(&ef, sizeof(struct elf_file));
377         ef.fd = -1;
378
379         err = __elfN(load_elf_header)(filename, &ef);
380         if (err != 0)
381                 return (err);
382
383         ehdr = ef.ehdr;
384
385         /*
386          * Check to see what sort of module we are.
387          */
388         kfp = file_findfile(NULL, __elfN(kerneltype));
389 #ifdef __powerpc__
390         /*
391          * Kernels can be ET_DYN, so just assume the first loaded object is the
392          * kernel. This assumption will be checked later.
393          */
394         if (kfp == NULL)
395                 ef.kernel = 1;
396 #endif
397         if (ef.kernel || ehdr->e_type == ET_EXEC) {
398                 /* Looks like a kernel */
399                 if (kfp != NULL) {
400                         printf("elf" __XSTRING(__ELF_WORD_SIZE)
401                             "_loadfile: kernel already loaded\n");
402                         err = EPERM;
403                         goto oerr;
404                 }
405                 /*
406                  * Calculate destination address based on kernel entrypoint.
407                  *
408                  * For ARM, the destination address is independent of any values
409                  * in the elf header (an ARM kernel can be loaded at any 2MB
410                  * boundary), so we leave dest set to the value calculated by
411                  * archsw.arch_loadaddr() and passed in to this function.
412                  */
413 #ifndef __arm__
414                 if (ehdr->e_type == ET_EXEC)
415                         dest = (ehdr->e_entry & ~PAGE_MASK);
416 #endif
417                 if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
418                         printf("elf" __XSTRING(__ELF_WORD_SIZE)
419                             "_loadfile: not a kernel (maybe static binary?)\n");
420                         err = EPERM;
421                         goto oerr;
422                 }
423                 ef.kernel = 1;
424
425         } else if (ehdr->e_type == ET_DYN) {
426                 /* Looks like a kld module */
427                 if (multiboot != 0) {
428                         printf("elf" __XSTRING(__ELF_WORD_SIZE)
429                             "_loadfile: can't load module as multiboot\n");
430                         err = EPERM;
431                         goto oerr;
432                 }
433                 if (kfp == NULL) {
434                         printf("elf" __XSTRING(__ELF_WORD_SIZE)
435                             "_loadfile: can't load module before kernel\n");
436                         err = EPERM;
437                         goto oerr;
438                 }
439                 if (strcmp(__elfN(kerneltype), kfp->f_type)) {
440                         printf("elf" __XSTRING(__ELF_WORD_SIZE)
441                          "_loadfile: can't load module with kernel type '%s'\n",
442                             kfp->f_type);
443                         err = EPERM;
444                         goto oerr;
445                 }
446                 /* Looks OK, got ahead */
447                 ef.kernel = 0;
448         
449         } else {
450                 err = EFTYPE;
451                 goto oerr;
452         }
453
454         if (archsw.arch_loadaddr != NULL)
455                 dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
456         else
457                 dest = roundup(dest, PAGE_SIZE);
458
459         /*
460          * Ok, we think we should handle this.
461          */
462         fp = file_alloc();
463         if (fp == NULL) {
464                 printf("elf" __XSTRING(__ELF_WORD_SIZE)
465                     "_loadfile: cannot allocate module info\n");
466                 err = EPERM;
467                 goto out;
468         }
469         if (ef.kernel == 1 && multiboot == 0)
470                 setenv("kernelname", filename, 1);
471         fp->f_name = strdup(filename);
472         if (multiboot == 0)
473                 fp->f_type = strdup(ef.kernel ?
474                     __elfN(kerneltype) : __elfN(moduletype));
475         else
476                 fp->f_type = strdup("elf multiboot kernel");
477
478         if (module_verbose >= MODULE_VERBOSE_FULL) {
479                 if (ef.kernel)
480                         printf("%s entry at 0x%jx\n", filename,
481                             (uintmax_t)ehdr->e_entry);
482         } else if (module_verbose > MODULE_VERBOSE_SILENT)
483                 printf("%s ", filename);
484
485         fp->f_size = __elfN(loadimage)(fp, &ef, dest);
486         if (fp->f_size == 0 || fp->f_addr == 0)
487                 goto ioerr;
488
489         /* save exec header as metadata */
490         file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
491
492         /* Load OK, return module pointer */
493         *result = (struct preloaded_file *)fp;
494         err = 0;
495 #ifdef __amd64__
496         fp->f_kernphys_relocatable = multiboot || is_kernphys_relocatable(&ef);
497 #endif
498 #ifdef __i386__
499         fp->f_tg_kernel_support = is_tg_kernel_support(fp, &ef);
500 #endif
501         goto out;
502
503 ioerr:
504         err = EIO;
505 oerr:
506         file_discard(fp);
507 out:
508         if (ef.firstpage)
509                 free(ef.firstpage);
510         if (ef.fd != -1) {
511 #ifdef LOADER_VERIEXEC_VECTX
512                 if (!err && ef.vctx) {
513                         int verror;
514
515                         verror = vectx_close(ef.vctx, VE_MUST, __func__);
516                         if (verror) {
517                                 err = EAUTH;
518                                 file_discard(fp);
519                         }
520                 }
521 #endif
522                 close(ef.fd);
523         }
524         return (err);
525 }
526
527 /*
528  * With the file (fd) open on the image, and (ehdr) containing
529  * the Elf header, load the image at (off)
530  */
531 static int
532 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off)
533 {
534         int             i;
535         u_int           j;
536         Elf_Ehdr        *ehdr;
537         Elf_Phdr        *phdr, *php;
538         Elf_Shdr        *shdr;
539         char            *shstr;
540         int             ret;
541         vm_offset_t     firstaddr;
542         vm_offset_t     lastaddr;
543         size_t          chunk;
544         ssize_t         result;
545         Elf_Addr        ssym, esym;
546         Elf_Dyn         *dp;
547         Elf_Addr        adp;
548         Elf_Addr        ctors;
549         int             ndp;
550         int             symstrindex;
551         int             symtabindex;
552         Elf_Size        size;
553         u_int           fpcopy;
554         Elf_Sym         sym;
555         Elf_Addr        p_start, p_end;
556
557         dp = NULL;
558         shdr = NULL;
559         ret = 0;
560         firstaddr = lastaddr = 0;
561         ehdr = ef->ehdr;
562 #ifdef __powerpc__
563         if (ef->kernel) {
564 #else
565         if (ehdr->e_type == ET_EXEC) {
566 #endif
567 #if defined(__i386__) || defined(__amd64__)
568 #if __ELF_WORD_SIZE == 64
569                 /* x86_64 relocates after locore */
570                 off = - (off & 0xffffffffff000000ull);
571 #else
572                 /* i386 relocates after locore */
573                 off = - (off & 0xff000000u);
574 #endif
575 #elif defined(__powerpc__)
576                 /*
577                  * On the purely virtual memory machines like e500, the kernel
578                  * is linked against its final VA range, which is most often
579                  * not available at the loader stage, but only after kernel
580                  * initializes and completes its VM settings. In such cases we
581                  * cannot use p_vaddr field directly to load ELF segments, but
582                  * put them at some 'load-time' locations.
583                  */
584                 if (off & 0xf0000000u) {
585                         off = -(off & 0xf0000000u);
586                         /*
587                          * XXX the physical load address should not be
588                          * hardcoded. Note that the Book-E kernel assumes that
589                          * it's loaded at a 16MB boundary for now...
590                          */
591                         off += 0x01000000;
592                 }
593                 ehdr->e_entry += off;
594                 if (module_verbose >= MODULE_VERBOSE_FULL)
595                         printf("Converted entry 0x%jx\n",
596                             (uintmax_t)ehdr->e_entry);
597
598 #elif defined(__arm__) && !defined(EFI)
599                 /*
600                  * The elf headers in arm kernels specify virtual addresses in
601                  * all header fields, even the ones that should be physical
602                  * addresses.  We assume the entry point is in the first page,
603                  * and masking the page offset will leave us with the virtual
604                  * address the kernel was linked at.  We subtract that from the
605                  * load offset, making 'off' into the value which, when added
606                  * to a virtual address in an elf header, translates it to a
607                  * physical address.  We do the va->pa conversion on the entry
608                  * point address in the header now, so that later we can launch
609                  * the kernel by just jumping to that address.
610                  *
611                  * When booting from UEFI the copyin and copyout functions
612                  * handle adjusting the location relative to the first virtual
613                  * address.  Because of this there is no need to adjust the
614                  * offset or entry point address as these will both be handled
615                  * by the efi code.
616                  */
617                 off -= ehdr->e_entry & ~PAGE_MASK;
618                 ehdr->e_entry += off;
619                 if (module_verbose >= MODULE_VERBOSE_FULL)
620                         printf("ehdr->e_entry 0x%jx, va<->pa off %llx\n",
621                             (uintmax_t)ehdr->e_entry, off);
622 #else
623                 off = 0;        /* other archs use direct mapped kernels */
624 #endif
625         }
626         ef->off = off;
627
628         if (ef->kernel)
629                 __elfN(relocation_offset) = off;
630
631         if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
632                 printf("elf" __XSTRING(__ELF_WORD_SIZE)
633                     "_loadimage: program header not within first page\n");
634                 goto out;
635         }
636         phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
637
638         for (i = 0; i < ehdr->e_phnum; i++) {
639                 if (elf_program_header_convert(ehdr, phdr))
640                         continue;
641
642                 /* We want to load PT_LOAD segments only.. */
643                 if (phdr[i].p_type != PT_LOAD)
644                         continue;
645
646                 if (module_verbose >= MODULE_VERBOSE_FULL) {
647                         printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
648                             (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
649                             (long)(phdr[i].p_vaddr + off),
650                             (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
651                 } else if (module_verbose > MODULE_VERBOSE_SILENT) {
652                         if ((phdr[i].p_flags & PF_W) == 0) {
653                                 printf("text=0x%lx ", (long)phdr[i].p_filesz);
654                         } else {
655                                 printf("data=0x%lx", (long)phdr[i].p_filesz);
656                                 if (phdr[i].p_filesz < phdr[i].p_memsz)
657                                         printf("+0x%lx", (long)(phdr[i].p_memsz -
658                                                 phdr[i].p_filesz));
659                                 printf(" ");
660                         }
661                 }
662                 fpcopy = 0;
663                 if (ef->firstlen > phdr[i].p_offset) {
664                         fpcopy = ef->firstlen - phdr[i].p_offset;
665                         archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
666                             phdr[i].p_vaddr + off, fpcopy);
667                 }
668                 if (phdr[i].p_filesz > fpcopy) {
669                         if (kern_pread(VECTX_HANDLE(ef),
670                             phdr[i].p_vaddr + off + fpcopy,
671                             phdr[i].p_filesz - fpcopy,
672                             phdr[i].p_offset + fpcopy) != 0) {
673                                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
674                                     "_loadimage: read failed\n");
675                                 goto out;
676                         }
677                 }
678                 /* clear space from oversized segments; eg: bss */
679                 if (phdr[i].p_filesz < phdr[i].p_memsz) {
680                         if (module_verbose >= MODULE_VERBOSE_FULL) {
681                                 printf(" (bss: 0x%lx-0x%lx)",
682                                     (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
683                                     (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz -1));
684                         }       
685                         kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
686                             phdr[i].p_memsz - phdr[i].p_filesz);
687                 }
688                 if (module_verbose >= MODULE_VERBOSE_FULL)
689                         printf("\n");
690
691                 if (archsw.arch_loadseg != NULL)
692                         archsw.arch_loadseg(ehdr, phdr + i, off);
693
694                 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
695                         firstaddr = phdr[i].p_vaddr + off;
696                 if (lastaddr == 0 || lastaddr <
697                     (phdr[i].p_vaddr + off + phdr[i].p_memsz))
698                         lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
699         }
700         lastaddr = roundup(lastaddr, sizeof(long));
701
702         /*
703          * Get the section headers.  We need this for finding the .ctors
704          * section as well as for loading any symbols.  Both may be hard
705          * to do if reading from a .gz file as it involves seeking.  I
706          * think the rule is going to have to be that you must strip a
707          * file to remove symbols before gzipping it.
708          */
709         chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize;
710         if (chunk == 0 || ehdr->e_shoff == 0)
711                 goto nosyms;
712         shdr = alloc_pread(VECTX_HANDLE(ef), ehdr->e_shoff, chunk);
713         if (shdr == NULL) {
714                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
715                     "_loadimage: failed to read section headers");
716                 goto nosyms;
717         }
718
719         for (i = 0; i < ehdr->e_shnum; i++)
720                 elf_section_header_convert(ehdr, &shdr[i]);
721
722         file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
723
724         /*
725          * Read the section string table and look for the .ctors section.
726          * We need to tell the kernel where it is so that it can call the
727          * ctors.
728          */
729         chunk = shdr[ehdr->e_shstrndx].sh_size;
730         if (chunk) {
731                 shstr = alloc_pread(VECTX_HANDLE(ef),
732                     shdr[ehdr->e_shstrndx].sh_offset, chunk);
733                 if (shstr) {
734                         for (i = 0; i < ehdr->e_shnum; i++) {
735                                 if (strcmp(shstr + shdr[i].sh_name,
736                                     ".ctors") != 0)
737                                         continue;
738                                 ctors = shdr[i].sh_addr;
739                                 file_addmetadata(fp, MODINFOMD_CTORS_ADDR,
740                                     sizeof(ctors), &ctors);
741                                 size = shdr[i].sh_size;
742                                 file_addmetadata(fp, MODINFOMD_CTORS_SIZE,
743                                     sizeof(size), &size);
744                                 break;
745                         }
746                         free(shstr);
747                 }
748         }
749
750         /*
751          * Now load any symbols.
752          */
753         symtabindex = -1;
754         symstrindex = -1;
755         for (i = 0; i < ehdr->e_shnum; i++) {
756                 if (shdr[i].sh_type != SHT_SYMTAB)
757                         continue;
758                 for (j = 0; j < ehdr->e_phnum; j++) {
759                         if (phdr[j].p_type != PT_LOAD)
760                                 continue;
761                         if (shdr[i].sh_offset >= phdr[j].p_offset &&
762                             (shdr[i].sh_offset + shdr[i].sh_size <=
763                             phdr[j].p_offset + phdr[j].p_filesz)) {
764                                 shdr[i].sh_offset = 0;
765                                 shdr[i].sh_size = 0;
766                                 break;
767                         }
768                 }
769                 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
770                         continue;       /* alread loaded in a PT_LOAD above */
771                 /* Save it for loading below */
772                 symtabindex = i;
773                 symstrindex = shdr[i].sh_link;
774         }
775         if (symtabindex < 0 || symstrindex < 0)
776                 goto nosyms;
777
778         /* Ok, committed to a load. */
779         if (module_verbose >= MODULE_VERBOSE_FULL)
780                 printf("syms=[");
781         ssym = lastaddr;
782         for (i = symtabindex; i >= 0; i = symstrindex) {
783                 char    *secname;
784
785                 switch(shdr[i].sh_type) {
786                 case SHT_SYMTAB:                /* Symbol table */
787                         secname = "symtab";
788                         break;
789                 case SHT_STRTAB:                /* String table */
790                         secname = "strtab";
791                         break;
792                 default:
793                         secname = "WHOA!!";
794                         break;
795                 }
796                 size = shdr[i].sh_size;
797
798                 archsw.arch_copyin(&size, lastaddr, sizeof(size));
799                 lastaddr += sizeof(size);
800
801                 if (module_verbose >= MODULE_VERBOSE_FULL) {
802                         printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
803                             (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
804                             (uintmax_t)lastaddr,
805                             (uintmax_t)(lastaddr + shdr[i].sh_size));
806                 } else if (module_verbose > MODULE_VERBOSE_SILENT) {
807                         if (i == symstrindex)
808                                 printf("+");
809                         printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
810                 }
811                 if (VECTX_LSEEK(VECTX_HANDLE(ef), (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
812                         printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
813                            "_loadimage: could not seek for symbols - skipped!");
814                         lastaddr = ssym;
815                         ssym = 0;
816                         goto nosyms;
817                 }
818                 result = archsw.arch_readin(VECTX_HANDLE(ef), lastaddr, shdr[i].sh_size);
819                 if (result < 0 || (size_t)result != shdr[i].sh_size) {
820                         printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
821                             "_loadimage: could not read symbols - skipped! "
822                             "(%ju != %ju)", (uintmax_t)result,
823                             (uintmax_t)shdr[i].sh_size);
824                         lastaddr = ssym;
825                         ssym = 0;
826                         goto nosyms;
827                 }
828                 /* Reset offsets relative to ssym */
829                 lastaddr += shdr[i].sh_size;
830                 lastaddr = roundup(lastaddr, sizeof(size));
831                 if (i == symtabindex)
832                         symtabindex = -1;
833                 else if (i == symstrindex)
834                         symstrindex = -1;
835         }
836         esym = lastaddr;
837         if (module_verbose >= MODULE_VERBOSE_FULL)
838                 printf("]");
839
840         file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
841         file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
842
843 nosyms:
844         if (module_verbose > MODULE_VERBOSE_SILENT)
845                 printf("\n");
846
847         ret = lastaddr - firstaddr;
848         fp->f_addr = firstaddr;
849
850         php = NULL;
851         for (i = 0; i < ehdr->e_phnum; i++) {
852                 if (phdr[i].p_type == PT_DYNAMIC) {
853                         php = phdr + i;
854                         adp = php->p_vaddr;
855                         file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp),
856                             &adp);
857                         break;
858                 }
859         }
860
861         if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
862                 goto out;
863
864         ndp = php->p_filesz / sizeof(Elf_Dyn);
865         if (ndp == 0)
866                 goto out;
867         dp = malloc(php->p_filesz);
868         if (dp == NULL)
869                 goto out;
870         archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
871
872         ef->strsz = 0;
873         for (i = 0; i < ndp; i++) {
874                 if (dp[i].d_tag == 0)
875                         break;
876                 switch (dp[i].d_tag) {
877                 case DT_HASH:
878                         ef->hashtab =
879                             (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
880                         break;
881                 case DT_STRTAB:
882                         ef->strtab =
883                             (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
884                         break;
885                 case DT_STRSZ:
886                         ef->strsz = dp[i].d_un.d_val;
887                         break;
888                 case DT_SYMTAB:
889                         ef->symtab =
890                             (Elf_Sym *)(uintptr_t)(dp[i].d_un.d_ptr + off);
891                         break;
892                 case DT_REL:
893                         ef->rel =
894                             (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
895                         break;
896                 case DT_RELSZ:
897                         ef->relsz = dp[i].d_un.d_val;
898                         break;
899                 case DT_RELA:
900                         ef->rela =
901                             (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
902                         break;
903                 case DT_RELASZ:
904                         ef->relasz = dp[i].d_un.d_val;
905                         break;
906                 default:
907                         break;
908                 }
909         }
910         if (ef->hashtab == NULL || ef->symtab == NULL ||
911             ef->strtab == NULL || ef->strsz == 0)
912                 goto out;
913         COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
914         COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
915         ef->buckets = ef->hashtab + 2;
916         ef->chains = ef->buckets + ef->nbuckets;
917
918         if (__elfN(lookup_symbol)(ef, "__start_set_modmetadata_set", &sym,
919             STT_NOTYPE) != 0)
920                 return 0;
921         p_start = sym.st_value + ef->off;
922         if (__elfN(lookup_symbol)(ef, "__stop_set_modmetadata_set", &sym,
923             STT_NOTYPE) != 0)
924                 return 0;
925         p_end = sym.st_value + ef->off;
926
927         if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
928                 goto out;
929
930         if (ef->kernel)         /* kernel must not depend on anything */
931                 goto out;
932
933 out:
934         if (dp)
935                 free(dp);
936         if (shdr)
937                 free(shdr);
938         return ret;
939 }
940
941 static char invalid_name[] = "bad";
942
943 char *
944 fake_modname(const char *name)
945 {
946         const char *sp, *ep;
947         char *fp;
948         size_t len;
949
950         sp = strrchr(name, '/');
951         if (sp)
952                 sp++;
953         else
954                 sp = name;
955
956         ep = strrchr(sp, '.');
957         if (ep == NULL) {
958                 ep = sp + strlen(sp);
959         }
960         if (ep == sp) {
961                 sp = invalid_name;
962                 ep = invalid_name + sizeof(invalid_name) - 1;
963         }
964
965         len = ep - sp;
966         fp = malloc(len + 1);
967         if (fp == NULL)
968                 return NULL;
969         memcpy(fp, sp, len);
970         fp[len] = '\0';
971         return fp;
972 }
973
974 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
975 struct mod_metadata64 {
976         int             md_version;     /* structure version MDTV_* */
977         int             md_type;        /* type of entry MDT_* */
978         uint64_t        md_data;        /* specific data */
979         uint64_t        md_cval;        /* common string label */
980 };
981 #endif
982 #if defined(__amd64__) && __ELF_WORD_SIZE == 32
983 struct mod_metadata32 {
984         int             md_version;     /* structure version MDTV_* */
985         int             md_type;        /* type of entry MDT_* */
986         uint32_t        md_data;        /* specific data */
987         uint32_t        md_cval;        /* common string label */
988 };
989 #endif
990
991 int
992 __elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest)
993 {
994         struct elf_file          ef;
995         int                      err, i, j;
996         Elf_Shdr                *sh_meta, *shdr = NULL;
997         Elf_Shdr                *sh_data[2];
998         char                    *shstrtab = NULL;
999         size_t                   size;
1000         Elf_Addr                 p_start, p_end;
1001
1002         bzero(&ef, sizeof(struct elf_file));
1003         ef.fd = -1;
1004
1005         err = __elfN(load_elf_header)(fp->f_name, &ef);
1006         if (err != 0)
1007                 goto out;
1008
1009         if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
1010                 ef.kernel = 1;
1011         } else if (ef.ehdr->e_type != ET_DYN) {
1012                 err = EFTYPE;
1013                 goto out;
1014         }
1015
1016         size = (size_t)ef.ehdr->e_shnum * (size_t)ef.ehdr->e_shentsize;
1017         shdr = alloc_pread(VECTX_HANDLE(&ef), ef.ehdr->e_shoff, size);
1018         if (shdr == NULL) {
1019                 err = ENOMEM;
1020                 goto out;
1021         }
1022
1023         /* Load shstrtab. */
1024         shstrtab = alloc_pread(VECTX_HANDLE(&ef), shdr[ef.ehdr->e_shstrndx].sh_offset,
1025             shdr[ef.ehdr->e_shstrndx].sh_size);
1026         if (shstrtab == NULL) {
1027                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1028                     "load_modmetadata: unable to load shstrtab\n");
1029                 err = EFTYPE;
1030                 goto out;
1031         }
1032
1033         /* Find set_modmetadata_set and data sections. */
1034         sh_data[0] = sh_data[1] = sh_meta = NULL;
1035         for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
1036                 if (strcmp(&shstrtab[shdr[i].sh_name],
1037                     "set_modmetadata_set") == 0) {
1038                         sh_meta = &shdr[i];
1039                 }
1040                 if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
1041                     (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
1042                         sh_data[j++] = &shdr[i];
1043                 }
1044         }
1045         if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
1046                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1047     "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
1048                 err = EFTYPE;
1049                 goto out;
1050         }
1051
1052         /* Load set_modmetadata_set into memory */
1053         err = kern_pread(VECTX_HANDLE(&ef), dest, sh_meta->sh_size, sh_meta->sh_offset);
1054         if (err != 0) {
1055                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1056     "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
1057                 goto out;
1058         }
1059         p_start = dest;
1060         p_end = dest + sh_meta->sh_size;
1061         dest += sh_meta->sh_size;
1062
1063         /* Load data sections into memory. */
1064         err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[0]->sh_size,
1065             sh_data[0]->sh_offset);
1066         if (err != 0) {
1067                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1068                     "load_modmetadata: unable to load data: %d\n", err);
1069                 goto out;
1070         }
1071
1072         /*
1073          * We have to increment the dest, so that the offset is the same into
1074          * both the .rodata and .data sections.
1075          */
1076         ef.off = -(sh_data[0]->sh_addr - dest);
1077         dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr);
1078
1079         err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[1]->sh_size,
1080             sh_data[1]->sh_offset);
1081         if (err != 0) {
1082                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1083                     "load_modmetadata: unable to load data: %d\n", err);
1084                 goto out;
1085         }
1086
1087         err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
1088         if (err != 0) {
1089                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1090                     "load_modmetadata: unable to parse metadata: %d\n", err);
1091                 goto out;
1092         }
1093
1094 out:
1095         if (shstrtab != NULL)
1096                 free(shstrtab);
1097         if (shdr != NULL)
1098                 free(shdr);
1099         if (ef.firstpage != NULL)
1100                 free(ef.firstpage);
1101         if (ef.fd != -1) {
1102 #ifdef LOADER_VERIEXEC_VECTX
1103                 if (!err && ef.vctx) {
1104                         int verror;
1105
1106                         verror = vectx_close(ef.vctx, VE_MUST, __func__);
1107                         if (verror) {
1108                                 err = EAUTH;
1109                                 file_discard(fp);
1110                         }
1111                 }
1112 #endif
1113                 close(ef.fd);
1114         }
1115         return (err);
1116 }
1117
1118 int
1119 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
1120     Elf_Addr p_start, Elf_Addr p_end)
1121 {
1122         struct mod_metadata md;
1123 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1124         struct mod_metadata64 md64;
1125 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1126         struct mod_metadata32 md32;
1127 #endif
1128         struct mod_depend *mdepend;
1129         struct mod_version mver;
1130         char *s;
1131         int error, modcnt, minfolen;
1132         Elf_Addr v, p;
1133
1134         modcnt = 0;
1135         p = p_start;
1136         while (p < p_end) {
1137                 COPYOUT(p, &v, sizeof(v));
1138                 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
1139                 if (error == EOPNOTSUPP)
1140                         v += ef->off;
1141                 else if (error != 0)
1142                         return (error);
1143 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1144                 COPYOUT(v, &md64, sizeof(md64));
1145                 error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
1146                 if (error == EOPNOTSUPP) {
1147                         md64.md_cval += ef->off;
1148                         md64.md_data += ef->off;
1149                 } else if (error != 0)
1150                         return (error);
1151                 md.md_version = md64.md_version;
1152                 md.md_type = md64.md_type;
1153                 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
1154                 md.md_data = (void *)(uintptr_t)md64.md_data;
1155 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1156                 COPYOUT(v, &md32, sizeof(md32));
1157                 error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
1158                 if (error == EOPNOTSUPP) {
1159                         md32.md_cval += ef->off;
1160                         md32.md_data += ef->off;
1161                 } else if (error != 0)
1162                         return (error);
1163                 md.md_version = md32.md_version;
1164                 md.md_type = md32.md_type;
1165                 md.md_cval = (const char *)(uintptr_t)md32.md_cval;
1166                 md.md_data = (void *)(uintptr_t)md32.md_data;
1167 #else
1168                 COPYOUT(v, &md, sizeof(md));
1169                 error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
1170                 if (error == EOPNOTSUPP) {
1171                         md.md_cval += ef->off;
1172                         md.md_data = (void *)((uintptr_t)md.md_data +
1173                             (uintptr_t)ef->off);
1174                 } else if (error != 0)
1175                         return (error);
1176 #endif
1177                 p += sizeof(Elf_Addr);
1178                 switch(md.md_type) {
1179                 case MDT_DEPEND:
1180                         if (ef->kernel) /* kernel must not depend on anything */
1181                                 break;
1182                         s = strdupout((vm_offset_t)md.md_cval);
1183                         minfolen = sizeof(*mdepend) + strlen(s) + 1;
1184                         mdepend = malloc(minfolen);
1185                         if (mdepend == NULL)
1186                                 return ENOMEM;
1187                         COPYOUT((vm_offset_t)md.md_data, mdepend,
1188                             sizeof(*mdepend));
1189                         strcpy((char*)(mdepend + 1), s);
1190                         free(s);
1191                         file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
1192                             mdepend);
1193                         free(mdepend);
1194                         break;
1195                 case MDT_VERSION:
1196                         s = strdupout((vm_offset_t)md.md_cval);
1197                         COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
1198                         file_addmodule(fp, s, mver.mv_version, NULL);
1199                         free(s);
1200                         modcnt++;
1201                         break;
1202                 }
1203         }
1204         if (modcnt == 0) {
1205                 s = fake_modname(fp->f_name);
1206                 file_addmodule(fp, s, 1, NULL);
1207                 free(s);
1208         }
1209         return 0;
1210 }
1211
1212 static unsigned long
1213 elf_hash(const char *name)
1214 {
1215         const unsigned char *p = (const unsigned char *) name;
1216         unsigned long h = 0;
1217         unsigned long g;
1218
1219         while (*p != '\0') {
1220                 h = (h << 4) + *p++;
1221                 if ((g = h & 0xf0000000) != 0)
1222                         h ^= g >> 24;
1223                 h &= ~g;
1224         }
1225         return h;
1226 }
1227
1228 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE)
1229     "_lookup_symbol: corrupt symbol table\n";
1230 int
1231 __elfN(lookup_symbol)(elf_file_t ef, const char* name, Elf_Sym *symp,
1232     unsigned char type)
1233 {
1234         Elf_Hashelt symnum;
1235         Elf_Sym sym;
1236         char *strp;
1237         unsigned long hash;
1238
1239         if (ef->nbuckets == 0) {
1240                 printf(__elfN(bad_symtable));
1241                 return ENOENT;
1242         }
1243
1244         hash = elf_hash(name);
1245         COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
1246
1247         while (symnum != STN_UNDEF) {
1248                 if (symnum >= ef->nchains) {
1249                         printf(__elfN(bad_symtable));
1250                         return ENOENT;
1251                 }
1252
1253                 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
1254                 if (sym.st_name == 0) {
1255                         printf(__elfN(bad_symtable));
1256                         return ENOENT;
1257                 }
1258
1259                 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
1260                 if (strcmp(name, strp) == 0) {
1261                         free(strp);
1262                         if (sym.st_shndx != SHN_UNDEF && sym.st_value != 0 &&
1263                             ELF_ST_TYPE(sym.st_info) == type) {
1264                                 *symp = sym;
1265                                 return 0;
1266                         }
1267                         return ENOENT;
1268                 }
1269                 free(strp);
1270                 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
1271         }
1272         return ENOENT;
1273 }
1274
1275 /*
1276  * Apply any intra-module relocations to the value. p is the load address
1277  * of the value and val/len is the value to be modified. This does NOT modify
1278  * the image in-place, because this is done by kern_linker later on.
1279  *
1280  * Returns EOPNOTSUPP if no relocation method is supplied.
1281  */
1282 static int
1283 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
1284     Elf_Addr p, void *val, size_t len)
1285 {
1286         size_t n;
1287         Elf_Rela a;
1288         Elf_Rel r;
1289         int error;
1290
1291         /*
1292          * The kernel is already relocated, but we still want to apply
1293          * offset adjustments.
1294          */
1295         if (ef->kernel)
1296                 return (EOPNOTSUPP);
1297
1298         for (n = 0; n < ef->relsz / sizeof(r); n++) {
1299                 COPYOUT(ef->rel + n, &r, sizeof(r));
1300
1301                 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
1302                     ef->off, p, val, len);
1303                 if (error != 0)
1304                         return (error);
1305         }
1306         for (n = 0; n < ef->relasz / sizeof(a); n++) {
1307                 COPYOUT(ef->rela + n, &a, sizeof(a));
1308
1309                 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
1310                     ef->off, p, val, len);
1311                 if (error != 0)
1312                         return (error);
1313         }
1314
1315         return (0);
1316 }
1317
1318 static Elf_Addr
1319 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
1320 {
1321
1322         /* Symbol lookup by index not required here. */
1323         return (0);
1324 }