2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
36 #include <machine/elf.h>
41 #include "bootstrap.h"
43 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
46 typedef struct elf_file {
64 static int elf_loadimage(struct preloaded_file *mp, elf_file_t ef, vm_offset_t loadaddr);
65 static int elf_lookup_symbol(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
66 static int elf_parse_modmetadata(struct preloaded_file *mp, elf_file_t ef);
67 static char *fake_modname(const char *name);
69 const char *elf_kerneltype = "elf kernel";
70 const char *elf_moduletype = "elf module";
73 * Attempt to load the file (file) as an ELF module. It will be stored at
74 * (dest), and a pointer to a module structure describing the loaded object
75 * will be saved in (result).
78 elf_loadfile(char *filename, vm_offset_t dest, struct preloaded_file **result)
80 struct preloaded_file *fp, *kfp;
88 bzero(&ef, sizeof(struct elf_file));
91 * Open the image, read and validate the ELF header
93 if (filename == NULL) /* can't handle nameless */
95 if ((ef.fd = open(filename, O_RDONLY)) == -1)
97 ef.firstpage = malloc(PAGE_SIZE);
98 if (ef.firstpage == NULL) {
102 bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE);
103 ef.firstlen = (size_t)bytes_read;
104 if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) {
105 err = EFTYPE; /* could be EIO, but may be small file */
108 ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage;
111 if (!IS_ELF(*ehdr)) {
115 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
116 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
117 ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
118 ehdr->e_version != EV_CURRENT ||
119 ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
126 * Check to see what sort of module we are.
128 kfp = file_findfile(NULL, NULL);
129 if (ehdr->e_type == ET_DYN) {
130 /* Looks like a kld module */
132 printf("elf_loadfile: can't load module before kernel\n");
136 if (strcmp(elf_kerneltype, kfp->f_type)) {
137 printf("elf_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
141 /* Looks OK, got ahead */
144 /* Page-align the load address */
145 pad = (u_int)dest & PAGE_MASK;
147 pad = PAGE_SIZE - pad;
150 } else if (ehdr->e_type == ET_EXEC) {
151 /* Looks like a kernel */
153 printf("elf_loadfile: kernel already loaded\n");
158 * Calculate destination address based on kernel entrypoint
160 dest = (vm_offset_t) ehdr->e_entry;
162 printf("elf_loadfile: not a kernel (maybe static binary?)\n");
174 * Ok, we think we should handle this.
178 printf("elf_loadfile: cannot allocate module info\n");
183 setenv("kernelname", filename, 1);
184 fp->f_name = strdup(filename);
185 fp->f_type = strdup(ef.kernel ? elf_kerneltype : elf_moduletype);
189 printf("%s entry at %p\n", filename, (void *) dest);
191 printf("%s ", filename);
194 fp->f_size = elf_loadimage(fp, &ef, dest);
195 if (fp->f_size == 0 || fp->f_addr == 0)
198 /* save exec header as metadata */
199 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
201 /* Load OK, return module pointer */
202 *result = (struct preloaded_file *)fp;
218 * With the file (fd) open on the image, and (ehdr) containing
219 * the Elf header, load the image at (off)
222 elf_loadimage(struct preloaded_file *fp, elf_file_t ef, vm_offset_t off)
227 Elf_Phdr *phdr, *php;
230 vm_offset_t firstaddr;
231 vm_offset_t lastaddr;
236 vm_offset_t ssym, esym;
247 firstaddr = lastaddr = 0;
251 off = - (off & 0xff000000u); /* i386 relocates after locore */
253 off = 0; /* alpha is direct mapped for kernels */
258 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
259 printf("elf_loadimage: program header not within first page\n");
262 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
264 for (i = 0; i < ehdr->e_phnum; i++) {
265 /* We want to load PT_LOAD segments only.. */
266 if (phdr[i].p_type != PT_LOAD)
270 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
271 (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
272 (long)(phdr[i].p_vaddr + off),
273 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
275 if ((phdr[i].p_flags & PF_W) == 0) {
276 printf("text=0x%lx ", (long)phdr[i].p_filesz);
278 printf("data=0x%lx", (long)phdr[i].p_filesz);
279 if (phdr[i].p_filesz < phdr[i].p_memsz)
280 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
285 if (ef->firstlen > phdr[i].p_offset) {
286 fpcopy = ef->firstlen - phdr[i].p_offset;
287 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
288 phdr[i].p_vaddr + off, fpcopy);
290 if (phdr[i].p_filesz > fpcopy) {
291 if (lseek(ef->fd, (off_t)(phdr[i].p_offset + fpcopy),
293 printf("\nelf_loadexec: cannot seek\n");
296 if (archsw.arch_readin(ef->fd, phdr[i].p_vaddr + off + fpcopy,
297 phdr[i].p_filesz - fpcopy) != (ssize_t)(phdr[i].p_filesz - fpcopy)) {
298 printf("\nelf_loadexec: archsw.readin failed\n");
302 /* clear space from oversized segments; eg: bss */
303 if (phdr[i].p_filesz < phdr[i].p_memsz) {
305 printf(" (bss: 0x%lx-0x%lx)",
306 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
307 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
310 /* no archsw.arch_bzero */
311 buf = malloc(PAGE_SIZE);
313 printf("\nelf_loadimage: malloc() failed\n");
316 bzero(buf, PAGE_SIZE);
317 resid = phdr[i].p_memsz - phdr[i].p_filesz;
318 dest = phdr[i].p_vaddr + off + phdr[i].p_filesz;
320 chunk = min(PAGE_SIZE, resid);
321 archsw.arch_copyin(buf, dest, chunk);
331 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
332 firstaddr = phdr[i].p_vaddr + off;
333 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
334 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
336 lastaddr = roundup(lastaddr, sizeof(long));
339 * Now grab the symbol tables. This isn't easy if we're reading a
340 * .gz file. I think the rule is going to have to be that you must
341 * strip a file to remove symbols before gzipping it so that we do not
342 * try to lseek() on it.
344 chunk = ehdr->e_shnum * ehdr->e_shentsize;
345 if (chunk == 0 || ehdr->e_shoff == 0)
347 shdr = malloc(chunk);
350 if (lseek(ef->fd, (off_t)ehdr->e_shoff, SEEK_SET) == -1) {
351 printf("\nelf_loadimage: cannot lseek() to section headers");
354 result = read(ef->fd, shdr, chunk);
355 if (result < 0 || (size_t)result != chunk) {
356 printf("\nelf_loadimage: read section headers failed");
361 for (i = 0; i < ehdr->e_shnum; i++) {
362 if (shdr[i].sh_type != SHT_SYMTAB)
364 for (j = 0; j < ehdr->e_phnum; j++) {
365 if (phdr[j].p_type != PT_LOAD)
367 if (shdr[i].sh_offset >= phdr[j].p_offset &&
368 (shdr[i].sh_offset + shdr[i].sh_size <=
369 phdr[j].p_offset + phdr[j].p_filesz)) {
370 shdr[i].sh_offset = 0;
375 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
376 continue; /* alread loaded in a PT_LOAD above */
377 /* Save it for loading below */
379 symstrindex = shdr[i].sh_link;
381 if (symtabindex < 0 || symstrindex < 0)
384 /* Ok, committed to a load. */
389 for (i = symtabindex; i >= 0; i = symstrindex) {
393 switch(shdr[i].sh_type) {
394 case SHT_SYMTAB: /* Symbol table */
397 case SHT_STRTAB: /* String table */
406 size = shdr[i].sh_size;
407 archsw.arch_copyin(&size, lastaddr, sizeof(size));
408 lastaddr += sizeof(long);
411 printf("\n%s: 0x%lx@0x%lx -> 0x%lx-0x%lx", secname,
412 shdr[i].sh_size, shdr[i].sh_offset,
413 lastaddr, lastaddr + shdr[i].sh_size);
415 if (i == symstrindex)
417 printf("0x%lx+0x%lx", (long)sizeof(size), size);
420 if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
421 printf("\nelf_loadimage: could not seek for symbols - skipped!");
426 result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
427 if (result < 0 || (size_t)result != shdr[i].sh_size) {
428 printf("\nelf_loadimage: could not read symbols - skipped!");
433 /* Reset offsets relative to ssym */
434 lastaddr += shdr[i].sh_size;
435 lastaddr = roundup(lastaddr, sizeof(long));
436 if (i == symtabindex)
438 else if (i == symstrindex)
446 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
447 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
452 ret = lastaddr - firstaddr;
453 fp->f_addr = firstaddr;
456 for (i = 0; i < ehdr->e_phnum; i++) {
457 if (phdr[i].p_type == PT_DYNAMIC) {
459 dp = (Elf_Dyn *)(php->p_vaddr);
460 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(dp), &dp);
466 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
469 ndp = php->p_filesz / sizeof(Elf_Dyn);
472 dp = malloc(php->p_filesz);
475 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
478 for (i = 0; i < ndp; i++) {
479 if (dp[i].d_tag == NULL)
481 switch (dp[i].d_tag) {
483 ef->hashtab = (Elf_Hashelt*)(dp[i].d_un.d_ptr + off);
486 ef->strtab = (char *)(dp[i].d_un.d_ptr + off);
489 ef->strsz = dp[i].d_un.d_val;
492 ef->symtab = (Elf_Sym*)(dp[i].d_un.d_ptr + off);
498 if (ef->hashtab == NULL || ef->symtab == NULL ||
499 ef->strtab == NULL || ef->strsz == 0)
501 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
502 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
503 ef->buckets = ef->hashtab + 2;
504 ef->chains = ef->buckets + ef->nbuckets;
505 if (elf_parse_modmetadata(fp, ef) == 0)
508 if (ef->kernel) /* kernel must not depend on anything */
519 static char invalid_name[] = "bad";
522 fake_modname(const char *name)
528 sp = strrchr(name, '/');
533 ep = strrchr(name, '.');
537 ep = invalid_name + sizeof(invalid_name) - 1;
540 ep = name + strlen(name);
542 fp = malloc(len + 1);
551 elf_parse_modmetadata(struct preloaded_file *fp, elf_file_t ef)
553 struct mod_metadata md;
554 struct mod_depend *mdepend;
555 struct mod_version mver;
557 char *s, *v, **p, **p_stop;
558 int modcnt, minfolen;
560 if (elf_lookup_symbol(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
562 p = (char **)(sym.st_value + ef->off);
563 if (elf_lookup_symbol(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
565 p_stop = (char **)(sym.st_value + ef->off);
569 COPYOUT(p++, &v, sizeof(v));
570 COPYOUT(v + ef->off, &md, sizeof(md));
573 if (ef->kernel) /* kernel must not depend on anything */
575 s = strdupout((vm_offset_t)(md.md_cval + ef->off));
576 minfolen = sizeof(*mdepend) + strlen(s) + 1;
577 mdepend = malloc(minfolen);
580 COPYOUT((vm_offset_t)(md.md_data + ef->off), mdepend, sizeof(*mdepend));
581 strcpy((char*)(mdepend + 1), s);
583 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
587 s = strdupout((vm_offset_t)(md.md_cval + ef->off));
588 COPYOUT((vm_offset_t)(md.md_data + ef->off), &mver, sizeof(mver));
589 file_addmodule(fp, s, mver.mv_version, NULL);
596 s = fake_modname(fp->f_name);
597 file_addmodule(fp, s, 1, NULL);
604 elf_hash(const char *name)
606 const unsigned char *p = (const unsigned char *) name;
612 if ((g = h & 0xf0000000) != 0)
619 static const char elf_bad_symtable[] = "elf_lookup_symbol: corrupt symbol table\n";
621 elf_lookup_symbol(struct preloaded_file *fp, elf_file_t ef, const char* name,
629 hash = elf_hash(name);
630 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
632 while (symnum != STN_UNDEF) {
633 if (symnum >= ef->nchains) {
634 printf(elf_bad_symtable);
638 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
639 if (sym.st_name == 0) {
640 printf(elf_bad_symtable);
644 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
645 if (strcmp(name, strp) == 0) {
647 if (sym.st_shndx != SHN_UNDEF ||
648 (sym.st_value != 0 &&
649 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
656 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));