2 * Copyright (c) 2004 Ian Dowse <iedowse@freebsd.org>
3 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
38 #include <machine/elf.h>
41 #include <sys/link_elf.h>
43 #include "bootstrap.h"
45 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
47 #if defined(__i386__) && __ELF_WORD_SIZE == 64
50 #define ELF_TARG_CLASS ELFCLASS64
51 #define ELF_TARG_MACH EM_X86_64
54 typedef struct elf_file {
58 int symtabindex; /* Index of symbol table */
59 int shstrindex; /* Index of section name string table */
63 #ifdef LOADER_VERIEXEC_VECTX
68 #ifdef LOADER_VERIEXEC_VECTX
69 #define VECTX_HANDLE(ef) (ef)->vctx
71 #define VECTX_HANDLE(ef) (ef)->fd
74 static int __elfN(obj_loadimage)(struct preloaded_file *mp, elf_file_t ef,
76 static int __elfN(obj_lookup_set)(struct preloaded_file *mp, elf_file_t ef,
77 const char *name, Elf_Addr *startp, Elf_Addr *stopp, int *countp);
78 static int __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
79 Elf_Addr p, void *val, size_t len);
80 static int __elfN(obj_parse_modmetadata)(struct preloaded_file *mp,
82 static Elf_Addr __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx);
84 const char *__elfN(obj_kerneltype) = "elf kernel";
85 const char *__elfN(obj_moduletype) = "elf obj module";
88 * Attempt to load the file (file) as an ELF module. It will be stored at
89 * (dest), and a pointer to a module structure describing the loaded object
90 * will be saved in (result).
93 __elfN(obj_loadfile)(char *filename, uint64_t dest,
94 struct preloaded_file **result)
96 struct preloaded_file *fp, *kfp;
103 bzero(&ef, sizeof(struct elf_file));
106 * Open the image, read and validate the ELF header
108 if (filename == NULL) /* can't handle nameless */
110 if ((ef.fd = open(filename, O_RDONLY)) == -1)
112 #ifdef LOADER_VERIEXEC_VECTX
116 ef.vctx = vectx_open(ef.fd, filename, 0L, NULL, &verror, __func__);
118 printf("Unverified %s: %s\n", filename, ve_error_get());
127 bytes_read = VECTX_READ(VECTX_HANDLE(&ef), hdr, sizeof(*hdr));
128 if (bytes_read != sizeof(*hdr)) {
129 err = EFTYPE; /* could be EIO, but may be small file */
138 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
139 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
140 hdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
141 hdr->e_version != EV_CURRENT ||
142 hdr->e_machine != ELF_TARG_MACH || /* Machine ? */
143 hdr->e_type != ET_REL) {
148 if (hdr->e_shnum * hdr->e_shentsize == 0 || hdr->e_shoff == 0 ||
149 hdr->e_shentsize != sizeof(Elf_Shdr)) {
154 #if defined(LOADER_VERIEXEC) && !defined(LOADER_VERIEXEC_VECTX)
155 if (verify_file(ef.fd, filename, bytes_read, VE_MUST, __func__) < 0) {
161 kfp = file_findfile(NULL, __elfN(obj_kerneltype));
163 printf("elf" __XSTRING(__ELF_WORD_SIZE)
164 "_obj_loadfile: can't load module before kernel\n");
169 if (archsw.arch_loadaddr != NULL)
170 dest = archsw.arch_loadaddr(LOAD_ELF, hdr, dest);
172 dest = roundup(dest, PAGE_SIZE);
175 * Ok, we think we should handle this.
179 printf("elf" __XSTRING(__ELF_WORD_SIZE)
180 "_obj_loadfile: cannot allocate module info\n");
184 fp->f_name = strdup(filename);
185 fp->f_type = strdup(__elfN(obj_moduletype));
187 printf("%s ", filename);
189 fp->f_size = __elfN(obj_loadimage)(fp, &ef, dest);
190 if (fp->f_size == 0 || fp->f_addr == 0)
193 /* save exec header as metadata */
194 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*hdr), hdr);
196 /* Load OK, return module pointer */
197 *result = (struct preloaded_file *)fp;
206 #ifdef LOADER_VERIEXEC_VECTX
207 if (!err && ef.vctx) {
210 verror = vectx_close(ef.vctx, VE_MUST, __func__);
218 if (ef.e_shdr != NULL)
225 * With the file (fd) open on the image, and (ehdr) containing
226 * the Elf header, load the image at (off)
229 __elfN(obj_loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off)
232 Elf_Shdr *shdr, *cshdr, *lshdr;
233 vm_offset_t firstaddr, lastaddr;
234 int i, nsym, res, ret, shdrbytes, symstrindex;
237 firstaddr = lastaddr = (vm_offset_t)off;
239 ef->off = (vm_offset_t)off;
241 /* Read in the section headers. */
242 shdrbytes = hdr->e_shnum * hdr->e_shentsize;
243 shdr = alloc_pread(VECTX_HANDLE(ef), (off_t)hdr->e_shoff, shdrbytes);
245 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
246 "_obj_loadimage: read section headers failed\n");
252 * Decide where to load everything, but don't read it yet.
253 * We store the load address as a non-zero sh_addr value.
254 * Start with the code/data and bss.
256 for (i = 0; i < hdr->e_shnum; i++)
258 for (i = 0; i < hdr->e_shnum; i++) {
259 if (shdr[i].sh_size == 0)
261 switch (shdr[i].sh_type) {
264 #if defined(__i386__) || defined(__amd64__)
265 case SHT_X86_64_UNWIND:
267 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
269 lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
270 shdr[i].sh_addr = (Elf_Addr)lastaddr;
271 lastaddr += shdr[i].sh_size;
278 for (i = 0; i < hdr->e_shnum; i++) {
279 switch (shdr[i].sh_type) {
283 shdr[i].sh_addr = (Elf_Addr)lastaddr;
284 lastaddr += shdr[i].sh_size;
289 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
290 "_obj_loadimage: file has no valid symbol table\n");
293 lastaddr = roundup(lastaddr, shdr[ef->symtabindex].sh_addralign);
294 shdr[ef->symtabindex].sh_addr = (Elf_Addr)lastaddr;
295 lastaddr += shdr[ef->symtabindex].sh_size;
297 symstrindex = shdr[ef->symtabindex].sh_link;
298 if (symstrindex < 0 || symstrindex >= hdr->e_shnum ||
299 shdr[symstrindex].sh_type != SHT_STRTAB) {
300 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
301 "_obj_loadimage: file has invalid symbol strings\n");
304 lastaddr = roundup(lastaddr, shdr[symstrindex].sh_addralign);
305 shdr[symstrindex].sh_addr = (Elf_Addr)lastaddr;
306 lastaddr += shdr[symstrindex].sh_size;
309 if (hdr->e_shstrndx == 0 || hdr->e_shstrndx >= hdr->e_shnum ||
310 shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
311 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
312 "_obj_loadimage: file has no section names\n");
315 ef->shstrindex = hdr->e_shstrndx;
316 lastaddr = roundup(lastaddr, shdr[ef->shstrindex].sh_addralign);
317 shdr[ef->shstrindex].sh_addr = (Elf_Addr)lastaddr;
318 lastaddr += shdr[ef->shstrindex].sh_size;
320 /* Relocation tables. */
321 for (i = 0; i < hdr->e_shnum; i++) {
322 switch (shdr[i].sh_type) {
325 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
327 lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
328 shdr[i].sh_addr = (Elf_Addr)lastaddr;
329 lastaddr += shdr[i].sh_size;
334 /* Clear the whole area, including bss regions. */
335 kern_bzero(firstaddr, lastaddr - firstaddr);
337 /* Figure section with the lowest file offset we haven't loaded yet. */
338 for (cshdr = NULL; /* none */; /* none */)
341 * Find next section to load. The complexity of this loop is
342 * O(n^2), but with the number of sections being typically
343 * small, we do not care.
347 for (i = 0; i < hdr->e_shnum; i++) {
348 if (shdr[i].sh_addr == 0 ||
349 shdr[i].sh_type == SHT_NOBITS)
351 /* Skip sections that were loaded already. */
353 lshdr->sh_offset >= shdr[i].sh_offset)
355 /* Find section with smallest offset. */
356 if (cshdr == lshdr ||
357 cshdr->sh_offset > shdr[i].sh_offset)
364 if (kern_pread(VECTX_HANDLE(ef), (vm_offset_t)cshdr->sh_addr,
365 cshdr->sh_size, (off_t)cshdr->sh_offset) != 0) {
366 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
367 "_obj_loadimage: read failed\n");
372 file_addmetadata(fp, MODINFOMD_SHDR, shdrbytes, shdr);
374 res = __elfN(obj_parse_modmetadata)(fp, ef);
378 ret = lastaddr - firstaddr;
379 fp->f_addr = firstaddr;
381 printf("size 0x%lx at 0x%lx", (u_long)ret, (u_long)firstaddr);
388 #if defined(__i386__) && __ELF_WORD_SIZE == 64
389 struct mod_metadata64 {
390 int md_version; /* structure version MDTV_* */
391 int md_type; /* type of entry MDT_* */
392 uint64_t md_data; /* specific data */
393 uint64_t md_cval; /* common string label */
398 __elfN(obj_parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
400 struct mod_metadata md;
401 #if defined(__i386__) && __ELF_WORD_SIZE == 64
402 struct mod_metadata64 md64;
404 struct mod_depend *mdepend;
405 struct mod_version mver;
407 int error, modcnt, minfolen;
408 Elf_Addr v, p, p_stop;
410 if (__elfN(obj_lookup_set)(fp, ef, "modmetadata_set", &p, &p_stop,
416 COPYOUT(p, &v, sizeof(v));
417 error = __elfN(obj_reloc_ptr)(fp, ef, p, &v, sizeof(v));
420 #if defined(__i386__) && __ELF_WORD_SIZE == 64
421 COPYOUT(v, &md64, sizeof(md64));
422 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
425 md.md_version = md64.md_version;
426 md.md_type = md64.md_type;
427 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
428 md.md_data = (void *)(uintptr_t)md64.md_data;
430 COPYOUT(v, &md, sizeof(md));
431 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md, sizeof(md));
435 p += sizeof(Elf_Addr);
438 s = strdupout((vm_offset_t)md.md_cval);
439 minfolen = sizeof(*mdepend) + strlen(s) + 1;
440 mdepend = malloc(minfolen);
443 COPYOUT((vm_offset_t)md.md_data, mdepend,
445 strcpy((char*)(mdepend + 1), s);
447 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
452 s = strdupout((vm_offset_t)md.md_cval);
453 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
454 file_addmodule(fp, s, mver.mv_version, NULL);
462 printf("unknown type %d\n", md.md_type);
470 __elfN(obj_lookup_set)(struct preloaded_file *fp, elf_file_t ef,
471 const char* name, Elf_Addr *startp, Elf_Addr *stopp, int *countp)
476 vm_offset_t shstrtab;
481 shstrtab = shdr[ef->shstrindex].sh_addr;
483 for (i = 0; i < hdr->e_shnum; i++) {
484 if (shdr[i].sh_type != SHT_PROGBITS)
486 if (shdr[i].sh_name == 0)
488 p = strdupout(shstrtab + shdr[i].sh_name);
489 if (strncmp(p, "set_", 4) == 0 && strcmp(p + 4, name) == 0) {
490 *startp = shdr[i].sh_addr;
491 *stopp = shdr[i].sh_addr + shdr[i].sh_size;
492 *countp = (*stopp - *startp) / sizeof(Elf_Addr);
503 * Apply any intra-module relocations to the value. p is the load address
504 * of the value and val/len is the value to be modified. This does NOT modify
505 * the image in-place, because this is done by kern_linker later on.
508 __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, Elf_Addr p,
509 void *val, size_t len)
517 int error, i, j, nrel, nrela;
522 for (i = 0; i < hdr->e_shnum; i++) {
523 if (shdr[i].sh_type != SHT_RELA && shdr[i].sh_type != SHT_REL)
525 base = shdr[shdr[i].sh_info].sh_addr;
526 if (base == 0 || shdr[i].sh_addr == 0)
528 if (off < base || off + len > base +
529 shdr[shdr[i].sh_info].sh_size)
532 switch (shdr[i].sh_type) {
534 abase = (Elf_Rela *)(intptr_t)shdr[i].sh_addr;
536 nrela = shdr[i].sh_size / sizeof(Elf_Rela);
537 for (j = 0; j < nrela; j++) {
538 COPYOUT(abase + j, &a, sizeof(a));
540 error = __elfN(reloc)(ef, __elfN(obj_symaddr),
541 &a, ELF_RELOC_RELA, base, off, val, len);
547 rbase = (Elf_Rel *)(intptr_t)shdr[i].sh_addr;
549 nrel = shdr[i].sh_size / sizeof(Elf_Rel);
550 for (j = 0; j < nrel; j++) {
551 COPYOUT(rbase + j, &r, sizeof(r));
553 error = __elfN(reloc)(ef, __elfN(obj_symaddr),
554 &r, ELF_RELOC_REL, base, off, val, len);
564 /* Look up the address of a specified symbol. */
566 __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx)
571 if (symidx >= ef->e_shdr[ef->symtabindex].sh_size / sizeof(Elf_Sym))
573 COPYOUT(ef->e_shdr[ef->symtabindex].sh_addr + symidx * sizeof(Elf_Sym),
575 if (sym.st_shndx == SHN_UNDEF || sym.st_shndx >= ef->hdr.e_shnum)
577 base = ef->e_shdr[sym.st_shndx].sh_addr;
580 return (base + sym.st_value);