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