2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
31 #include <sys/imgact_aout.h>
32 #include <sys/reboot.h>
33 #include <sys/linker.h>
35 #include <machine/bootinfo.h>
41 #include "bootstrap.h"
43 static int aout_loadimage(struct preloaded_file *fp, int fd, vm_offset_t loadaddr, struct exec *ehdr, int kernel);
46 static vm_offset_t aout_findkldident(struct preloaded_file *fp, struct exec *ehdr);
47 static int aout_fixupkldmod(struct preloaded_file *fp, struct exec *ehdr);
50 const char *aout_kerneltype = "a.out kernel";
51 const char *aout_moduletype = "a.out module";
54 * Attempt to load the file (file) as an a.out module. It will be stored at
55 * (dest), and a pointer to a module structure describing the loaded object
56 * will be saved in (result).
59 aout_loadfile(char *filename, vm_offset_t dest, struct preloaded_file **result)
61 struct preloaded_file *fp, *kfp;
71 * Open the image, read and validate the a.out header
73 if (filename == NULL) /* can't handle nameless */
75 if ((fd = open(filename, O_RDONLY)) == -1)
77 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
78 err = EFTYPE; /* could be EIO, but may be small file */
87 * Check to see what sort of module we are.
89 * XXX should check N_GETMID()
91 kfp = file_findfile(NULL, NULL);
92 if ((N_GETFLAG(ehdr)) & EX_DYNAMIC) {
93 /* Looks like a kld module */
95 printf("aout_loadfile: can't load module before kernel\n");
99 if (strcmp(aout_kerneltype, kfp->f_type)) {
100 printf("aout_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
104 /* Looks OK, got ahead */
107 } else if (N_GETFLAG(ehdr) == 0) {
108 /* Looks like a kernel */
110 printf("aout_loadfile: kernel already loaded\n");
115 * Calculate destination address based on kernel entrypoint
116 * XXX this is i386-freebsd-aout specific
118 dest = ehdr.a_entry & 0x100000;
120 printf("aout_loadfile: not a kernel (maybe static binary?)\n");
131 * Ok, we think we should handle this.
135 setenv("kernelname", filename, 1);
136 fp->f_name = strdup(filename);
137 fp->f_type = strdup(kernel ? aout_kerneltype : aout_moduletype);
139 /* Page-align the load address */
141 pad = (u_int)addr & PAGE_MASK;
143 pad = PAGE_SIZE - pad;
146 fp->f_addr = addr; /* save the aligned load address */
148 printf("%s at %p\n", filename, (void *) addr);
150 fp->f_size = aout_loadimage(fp, fd, addr, &ehdr, kernel);
155 /* Handle KLD module data */
156 if (!kernel && ((err = aout_fixupkldmod(fp, &ehdr)) != 0))
160 /* save exec header as metadata */
161 file_addmetadata(fp, MODINFOMD_AOUTEXEC, sizeof(struct exec), &ehdr);
163 /* Load OK, return module pointer */
178 * With the file (fd) open on the image, and (ehdr) containing
179 * the exec header, load the image at (addr)
181 * Fixup the a_bss field in (ehdr) to reflect the padding added to
182 * align the symbol table.
185 aout_loadimage(struct preloaded_file *fp, int fd, vm_offset_t loadaddr, struct exec *ehdr, int kernel)
191 vm_offset_t ssym, esym;
194 lseek(fd, (off_t)N_TXTOFF(*ehdr), SEEK_SET);
197 printf(" text=0x%lx ", ehdr->a_text);
198 result = archsw.arch_readin(fd, addr, ehdr->a_text);
199 if (result < 0 || (size_t)result != ehdr->a_text)
201 addr += ehdr->a_text;
204 printf("data=0x%lx ", ehdr->a_data);
205 result = archsw.arch_readin(fd, addr, ehdr->a_data);
206 if (result < 0 || (size_t)result != ehdr->a_data)
208 addr += ehdr->a_data;
210 /* For kernels, we pad the BSS to a page boundary */
212 pad = (u_int)ehdr->a_bss & PAGE_MASK;
214 pad = PAGE_SIZE - pad;
218 printf("bss=0x%lx ", ehdr->a_bss);
221 /* symbol table size */
223 if(ehdr->a_syms!=NULL) {
224 archsw.arch_copyin(&ehdr->a_syms, addr, sizeof(ehdr->a_syms));
225 addr += sizeof(ehdr->a_syms);
228 printf("symbols=[0x%lx+0x%lx", (long)sizeof(ehdr->a_syms),ehdr->a_syms);
229 result = archsw.arch_readin(fd, addr, ehdr->a_syms);
230 if (result < 0 || (size_t)result != ehdr->a_syms)
232 addr += ehdr->a_syms;
235 read(fd, &ss, sizeof(ss));
236 archsw.arch_copyin(&ss, addr, sizeof(ss));
239 printf("+0x%lx+0x%x]", (long)sizeof(ss), ss);
240 result = archsw.arch_readin(fd, addr, ss);
241 if (result < 0 || (size_t)result != ss)
246 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
247 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
249 printf("symbols=[none]");
253 return(addr - loadaddr);