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