Deleted Added
full compact
load_elf.c (215811) load_elf.c (220311)
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>
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 215811 2010-11-25 03:16:31Z emaste $");
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;
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 u_int pad;
101 ssize_t bytes_read;
102
103 fp = NULL;
104 bzero(&ef, sizeof(struct elf_file));
105
106 /*
107 * Open the image, read and validate the ELF header
108 */

--- 43 unchanged lines hidden (view full) ---

152 if (strcmp(__elfN(kerneltype), kfp->f_type)) {
153 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
154 err = EPERM;
155 goto oerr;
156 }
157 /* Looks OK, got ahead */
158 ef.kernel = 0;
159
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
160 /* Page-align the load address */
161 pad = (u_int)dest & PAGE_MASK;
162 if (pad != 0) {
163 pad = PAGE_SIZE - pad;
164 dest += pad;
165 }
166 } else if (ehdr->e_type == ET_EXEC) {
167 /* Looks like a kernel */
168 if (kfp != NULL) {
169 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
170 err = EPERM;
171 goto oerr;
172 }
173 /*
174 * Calculate destination address based on kernel entrypoint
175 */
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 */
176 dest = ehdr->e_entry;
169 dest = (ehdr->e_entry & ~PAGE_MASK);
177 if (dest == 0) {
178 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
179 err = EPERM;
180 goto oerr;
181 }
182 ef.kernel = 1;
183
184 } else {
185 err = EFTYPE;
186 goto oerr;
187 }
188
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
189 /*
190 * Ok, we think we should handle this.
191 */
192 fp = file_alloc();
193 if (fp == NULL) {
194 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
195 err = EPERM;
196 goto out;
197 }
198 if (ef.kernel)
199 setenv("kernelname", filename, 1);
200 fp->f_name = strdup(filename);
201 fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype));
202
203#ifdef ELF_VERBOSE
204 if (ef.kernel)
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)
205 printf("%s entry at 0x%jx\n", filename, (uintmax_t)dest);
203 printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry);
206#else
207 printf("%s ", filename);
208#endif
209
210 fp->f_size = __elfN(loadimage)(fp, &ef, dest);
211 if (fp->f_size == 0 || fp->f_addr == 0)
212 goto ioerr;
213

--- 143 unchanged lines hidden (view full) ---

357
358 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
359 phdr[i].p_memsz - phdr[i].p_filesz);
360 }
361#ifdef ELF_VERBOSE
362 printf("\n");
363#endif
364
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
365 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
366 firstaddr = phdr[i].p_vaddr + off;
367 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
368 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
369 }
370 lastaddr = roundup(lastaddr, sizeof(long));
371
372 /*

--- 417 unchanged lines hidden ---
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 ---