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 $
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 loaded_module *mp, int fd, vm_offset_t loadaddr, struct exec *ehdr, int kernel);
46 static vm_offset_t aout_findkldident(struct loaded_module *mp, struct exec *ehdr);
47 static int aout_fixupkldmod(struct loaded_module *mp, 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_loadmodule(char *filename, vm_offset_t dest, struct loaded_module **result)
61 struct loaded_module *mp, *kmp;
72 * Open the image, read and validate the a.out header
74 if (filename == NULL) /* can't handle nameless */
76 if ((fd = open(filename, O_RDONLY)) == -1)
78 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
79 err = EFTYPE; /* could be EIO, but may be small file */
88 * Check to see what sort of module we are.
90 * XXX should check N_GETMID()
92 kmp = mod_findmodule(NULL, NULL);
93 if ((N_GETFLAG(ehdr)) & EX_DYNAMIC) {
94 /* Looks like a kld module */
96 printf("aout_loadmodule: can't load module before kernel\n");
100 if (strcmp(aout_kerneltype, kmp->m_type)) {
101 printf("aout_loadmodule: can't load module with kernel type '%s'\n", kmp->m_type);
105 /* Looks OK, got ahead */
108 } else if (N_GETFLAG(ehdr) == 0) {
109 /* Looks like a kernel */
111 printf("aout_loadmodule: kernel already loaded\n");
116 * Calculate destination address based on kernel entrypoint
117 * XXX this is i386-freebsd-aout specific
119 dest = ehdr.a_entry & 0x100000;
121 printf("aout_loadmodule: not a kernel (maybe static binary?)\n");
132 * Ok, we think we should handle this.
134 mp = mod_allocmodule();
136 setenv("kernelname", filename, 1);
137 s = strrchr(filename, '/');
139 mp->m_name = strdup(s + 1);
141 mp->m_name = strdup(filename);
142 mp->m_type = strdup(kernel ? aout_kerneltype : aout_moduletype);
144 /* Page-align the load address */
146 pad = (u_int)addr & PAGE_MASK;
148 pad = PAGE_SIZE - pad;
151 mp->m_addr = addr; /* save the aligned load address */
153 printf("%s at %p\n", filename, (void *) addr);
155 mp->m_size = aout_loadimage(mp, fd, addr, &ehdr, kernel);
160 /* Handle KLD module data */
161 if (!kernel && ((err = aout_fixupkldmod(mp, &ehdr)) != 0))
165 /* save exec header as metadata */
166 mod_addmetadata(mp, MODINFOMD_AOUTEXEC, sizeof(struct exec), &ehdr);
168 /* Load OK, return module pointer */
169 *result = (struct loaded_module *)mp;
183 * With the file (fd) open on the image, and (ehdr) containing
184 * the exec header, load the image at (addr)
186 * Fixup the a_bss field in (ehdr) to reflect the padding added to
187 * align the symbol table.
190 aout_loadimage(struct loaded_module *mp, int fd, vm_offset_t loadaddr, struct exec *ehdr, int kernel)
196 vm_offset_t ssym, esym;
199 lseek(fd, (off_t)N_TXTOFF(*ehdr), SEEK_SET);
202 printf(" text=0x%lx ", ehdr->a_text);
203 result = archsw.arch_readin(fd, addr, ehdr->a_text);
204 if (result < 0 || (size_t)result != ehdr->a_text)
206 addr += ehdr->a_text;
209 printf("data=0x%lx ", ehdr->a_data);
210 result = archsw.arch_readin(fd, addr, ehdr->a_data);
211 if (result < 0 || (size_t)result != ehdr->a_data)
213 addr += ehdr->a_data;
215 /* For kernels, we pad the BSS to a page boundary */
217 pad = (u_int)ehdr->a_bss & PAGE_MASK;
219 pad = PAGE_SIZE - pad;
223 printf("bss=0x%lx ", ehdr->a_bss);
226 /* symbol table size */
228 if(ehdr->a_syms!=NULL) {
229 archsw.arch_copyin(&ehdr->a_syms, addr, sizeof(ehdr->a_syms));
230 addr += sizeof(ehdr->a_syms);
233 printf("symbols=[0x%lx+0x%lx", (long)sizeof(ehdr->a_syms),ehdr->a_syms);
234 result = archsw.arch_readin(fd, addr, ehdr->a_syms);
235 if (result < 0 || (size_t)result != ehdr->a_syms)
237 addr += ehdr->a_syms;
240 read(fd, &ss, sizeof(ss));
241 archsw.arch_copyin(&ss, addr, sizeof(ss));
244 printf("+0x%lx+0x%x]", (long)sizeof(ss), ss);
245 result = archsw.arch_readin(fd, addr, ss);
246 if (result < 0 || (size_t)result != ss)
251 mod_addmetadata(mp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
252 mod_addmetadata(mp, MODINFOMD_ESYM, sizeof(esym), &esym);
254 printf("symbols=[none]");
258 return(addr - loadaddr);