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