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: --- 12 unchanged lines hidden (view full) --- 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> |
29__FBSDID("$FreeBSD: head/sys/boot/common/load_elf.c 220311 2011-04-03 22:31:51Z marcel $"); |
30 31#include <sys/param.h> 32#include <sys/exec.h> 33#include <sys/linker.h> 34#include <sys/module.h> 35#include <sys/stdint.h> 36#include <string.h> 37#include <machine/elf.h> --- 54 unchanged lines hidden (view full) --- 92 */ 93int 94__elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result) 95{ 96 struct preloaded_file *fp, *kfp; 97 struct elf_file ef; 98 Elf_Ehdr *ehdr; 99 int err; |
100 ssize_t bytes_read; 101 102 fp = NULL; 103 bzero(&ef, sizeof(struct elf_file)); 104 105 /* 106 * Open the image, read and validate the ELF header 107 */ --- 43 unchanged lines hidden (view full) --- 151 if (strcmp(__elfN(kerneltype), kfp->f_type)) { 152 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type); 153 err = EPERM; 154 goto oerr; 155 } 156 /* Looks OK, got ahead */ 157 ef.kernel = 0; 158 |
159 } else if (ehdr->e_type == ET_EXEC) { 160 /* Looks like a kernel */ 161 if (kfp != NULL) { 162 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n"); 163 err = EPERM; 164 goto oerr; 165 } 166 /* 167 * Calculate destination address based on kernel entrypoint 168 */ |
169 dest = (ehdr->e_entry & ~PAGE_MASK); |
170 if (dest == 0) { 171 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n"); 172 err = EPERM; 173 goto oerr; 174 } 175 ef.kernel = 1; 176 177 } else { 178 err = EFTYPE; 179 goto oerr; 180 } 181 |
182 if (archsw.arch_loadaddr != NULL) 183 dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest); 184 else 185 dest = roundup(dest, PAGE_SIZE); 186 |
187 /* 188 * Ok, we think we should handle this. 189 */ 190 fp = file_alloc(); 191 if (fp == NULL) { 192 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n"); 193 err = EPERM; 194 goto out; 195 } 196 if (ef.kernel) 197 setenv("kernelname", filename, 1); 198 fp->f_name = strdup(filename); 199 fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype)); 200 201#ifdef ELF_VERBOSE 202 if (ef.kernel) |
203 printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry); |
204#else 205 printf("%s ", filename); 206#endif 207 208 fp->f_size = __elfN(loadimage)(fp, &ef, dest); 209 if (fp->f_size == 0 || fp->f_addr == 0) 210 goto ioerr; 211 --- 143 unchanged lines hidden (view full) --- 355 356 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz, 357 phdr[i].p_memsz - phdr[i].p_filesz); 358 } 359#ifdef ELF_VERBOSE 360 printf("\n"); 361#endif 362 |
363 if (archsw.arch_loadseg != NULL) 364 archsw.arch_loadseg(ehdr, phdr + i, off); 365 |
366 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off)) 367 firstaddr = phdr[i].p_vaddr + off; 368 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz)) 369 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz; 370 } 371 lastaddr = roundup(lastaddr, sizeof(long)); 372 373 /* --- 417 unchanged lines hidden --- |