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
26 * $FreeBSD: src/sys/boot/common/load_aout.c,v 1.16.2.2 2000/12/28 13:12:35 ps Exp $
27 * $DragonFly: src/sys/boot/common/Attic/load_aout.c,v 1.2 2003/06/17 04:28:16 dillon Exp $
30 #include <sys/param.h>
32 #include <sys/imgact_aout.h>
33 #include <sys/reboot.h>
34 #include <sys/linker.h>
36 #include <machine/bootinfo.h>
42 #include "bootstrap.h"
44 static int aout_loadimage(struct loaded_module *mp, int fd, vm_offset_t loadaddr, struct exec *ehdr, int kernel);
47 static vm_offset_t aout_findkldident(struct loaded_module *mp, struct exec *ehdr);
48 static int aout_fixupkldmod(struct loaded_module *mp, struct exec *ehdr);
51 const char *aout_kerneltype = "a.out kernel";
52 const char *aout_moduletype = "a.out module";
55 * Attempt to load the file (file) as an a.out module. It will be stored at
56 * (dest), and a pointer to a module structure describing the loaded object
57 * will be saved in (result).
60 aout_loadmodule(char *filename, vm_offset_t dest, struct loaded_module **result)
62 struct loaded_module *mp, *kmp;
73 * Open the image, read and validate the a.out header
75 if (filename == NULL) /* can't handle nameless */
77 if ((fd = open(filename, O_RDONLY)) == -1)
79 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
80 err = EFTYPE; /* could be EIO, but may be small file */
89 * Check to see what sort of module we are.
91 * XXX should check N_GETMID()
93 kmp = mod_findmodule(NULL, NULL);
94 if ((N_GETFLAG(ehdr)) & EX_DYNAMIC) {
95 /* Looks like a kld module */
97 printf("aout_loadmodule: can't load module before kernel\n");
101 if (strcmp(aout_kerneltype, kmp->m_type)) {
102 printf("aout_loadmodule: can't load module with kernel type '%s'\n", kmp->m_type);
106 /* Looks OK, got ahead */
109 } else if (N_GETFLAG(ehdr) == 0) {
110 /* Looks like a kernel */
112 printf("aout_loadmodule: kernel already loaded\n");
117 * Calculate destination address based on kernel entrypoint
118 * XXX this is i386-freebsd-aout specific
120 dest = ehdr.a_entry & 0x100000;
122 printf("aout_loadmodule: not a kernel (maybe static binary?)\n");
133 * Ok, we think we should handle this.
135 mp = mod_allocmodule();
137 setenv("kernelname", filename, 1);
138 s = strrchr(filename, '/');
140 mp->m_name = strdup(s + 1);
142 mp->m_name = strdup(filename);
143 mp->m_type = strdup(kernel ? aout_kerneltype : aout_moduletype);
145 /* Page-align the load address */
147 pad = (u_int)addr & PAGE_MASK;
149 pad = PAGE_SIZE - pad;
152 mp->m_addr = addr; /* save the aligned load address */
154 printf("%s at %p\n", filename, (void *) addr);
156 mp->m_size = aout_loadimage(mp, fd, addr, &ehdr, kernel);
161 /* Handle KLD module data */
162 if (!kernel && ((err = aout_fixupkldmod(mp, &ehdr)) != 0))
166 /* save exec header as metadata */
167 mod_addmetadata(mp, MODINFOMD_AOUTEXEC, sizeof(struct exec), &ehdr);
169 /* Load OK, return module pointer */
170 *result = (struct loaded_module *)mp;
184 * With the file (fd) open on the image, and (ehdr) containing
185 * the exec header, load the image at (addr)
187 * Fixup the a_bss field in (ehdr) to reflect the padding added to
188 * align the symbol table.
191 aout_loadimage(struct loaded_module *mp, int fd, vm_offset_t loadaddr, struct exec *ehdr, int kernel)
197 vm_offset_t ssym, esym;
200 lseek(fd, (off_t)N_TXTOFF(*ehdr), SEEK_SET);
203 printf(" text=0x%lx ", ehdr->a_text);
204 result = archsw.arch_readin(fd, addr, ehdr->a_text);
205 if (result < 0 || (size_t)result != ehdr->a_text)
207 addr += ehdr->a_text;
210 printf("data=0x%lx ", ehdr->a_data);
211 result = archsw.arch_readin(fd, addr, ehdr->a_data);
212 if (result < 0 || (size_t)result != ehdr->a_data)
214 addr += ehdr->a_data;
216 /* For kernels, we pad the BSS to a page boundary */
218 pad = (u_int)ehdr->a_bss & PAGE_MASK;
220 pad = PAGE_SIZE - pad;
224 printf("bss=0x%lx ", ehdr->a_bss);
227 /* symbol table size */
229 if(ehdr->a_syms!=NULL) {
230 archsw.arch_copyin(&ehdr->a_syms, addr, sizeof(ehdr->a_syms));
231 addr += sizeof(ehdr->a_syms);
234 printf("symbols=[0x%lx+0x%lx", (long)sizeof(ehdr->a_syms),ehdr->a_syms);
235 result = archsw.arch_readin(fd, addr, ehdr->a_syms);
236 if (result < 0 || (size_t)result != ehdr->a_syms)
238 addr += ehdr->a_syms;
241 read(fd, &ss, sizeof(ss));
242 archsw.arch_copyin(&ss, addr, sizeof(ss));
245 printf("+0x%lx+0x%x]", (long)sizeof(ss), ss);
246 result = archsw.arch_readin(fd, addr, ss);
247 if (result < 0 || (size_t)result != ss)
252 mod_addmetadata(mp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
253 mod_addmetadata(mp, MODINFOMD_ESYM, sizeof(esym), &esym);
255 printf("symbols=[none]");
259 return(addr - loadaddr);