1998-08-31 21:10:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-08-31 21:10:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/imgact_aout.h>
|
|
|
|
#include <sys/reboot.h>
|
1998-10-09 23:15:39 +00:00
|
|
|
#include <sys/linker.h>
|
1998-08-31 21:10:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <machine/bootinfo.h>
|
|
|
|
#include <stand.h>
|
1998-09-18 01:12:23 +00:00
|
|
|
#include <a.out.h>
|
1998-09-17 23:52:16 +00:00
|
|
|
#define FREEBSD_AOUT
|
1998-09-03 02:10:09 +00:00
|
|
|
#include <link.h>
|
1998-08-31 21:10:43 +00:00
|
|
|
|
|
|
|
#include "bootstrap.h"
|
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
static int aout_loadimage(struct preloaded_file *fp, int fd, vm_offset_t loadaddr, struct exec *ehdr, int kernel);
|
1998-10-09 23:15:39 +00:00
|
|
|
|
|
|
|
#if 0
|
2000-05-01 17:41:25 +00:00
|
|
|
static vm_offset_t aout_findkldident(struct preloaded_file *fp, struct exec *ehdr);
|
|
|
|
static int aout_fixupkldmod(struct preloaded_file *fp, struct exec *ehdr);
|
1998-10-09 23:15:39 +00:00
|
|
|
#endif
|
1998-08-31 21:10:43 +00:00
|
|
|
|
|
|
|
char *aout_kerneltype = "a.out kernel";
|
|
|
|
char *aout_moduletype = "a.out module";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to load the file (file) as an a.out module. It will be stored at
|
|
|
|
* (dest), and a pointer to a module structure describing the loaded object
|
|
|
|
* will be saved in (result).
|
|
|
|
*/
|
|
|
|
int
|
2000-05-01 17:41:25 +00:00
|
|
|
aout_loadfile(char *filename, vm_offset_t dest, struct preloaded_file **result)
|
1998-08-31 21:10:43 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp, *kfp;
|
1998-08-31 21:10:43 +00:00
|
|
|
struct exec ehdr;
|
|
|
|
int fd;
|
|
|
|
vm_offset_t addr;
|
|
|
|
int err, kernel;
|
1998-09-03 02:10:09 +00:00
|
|
|
u_int pad;
|
1998-10-09 23:15:39 +00:00
|
|
|
char *s;
|
1998-08-31 21:10:43 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
fp = NULL;
|
1998-09-03 02:10:09 +00:00
|
|
|
|
1998-08-31 21:10:43 +00:00
|
|
|
/*
|
|
|
|
* Open the image, read and validate the a.out header
|
|
|
|
*/
|
|
|
|
if (filename == NULL) /* can't handle nameless */
|
|
|
|
return(EFTYPE);
|
|
|
|
if ((fd = open(filename, O_RDONLY)) == -1)
|
|
|
|
return(errno);
|
1998-09-03 02:10:09 +00:00
|
|
|
if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
|
|
|
|
err = EFTYPE; /* could be EIO, but may be small file */
|
|
|
|
goto oerr;
|
|
|
|
}
|
|
|
|
if (N_BADMAG(ehdr)) {
|
|
|
|
err = EFTYPE;
|
|
|
|
goto oerr;
|
|
|
|
}
|
1998-08-31 21:10:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see what sort of module we are.
|
|
|
|
*
|
|
|
|
* XXX should check N_GETMID()
|
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
kfp = file_findfile(NULL, NULL);
|
1998-10-09 23:15:39 +00:00
|
|
|
if ((N_GETFLAG(ehdr)) & EX_DYNAMIC) {
|
1998-08-31 21:10:43 +00:00
|
|
|
/* Looks like a kld module */
|
2000-05-01 17:41:25 +00:00
|
|
|
if (kfp == NULL) {
|
|
|
|
printf("aout_loadfile: can't load module before kernel\n");
|
1998-09-03 02:10:09 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto oerr;
|
1998-08-31 21:10:43 +00:00
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
if (strcmp(aout_kerneltype, kfp->f_type)) {
|
|
|
|
printf("aout_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
|
1998-09-03 02:10:09 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto oerr;
|
1998-08-31 21:10:43 +00:00
|
|
|
}
|
|
|
|
/* Looks OK, got ahead */
|
|
|
|
kernel = 0;
|
|
|
|
|
|
|
|
} else if (N_GETFLAG(ehdr) == 0) {
|
|
|
|
/* Looks like a kernel */
|
2000-05-01 17:41:25 +00:00
|
|
|
if (kfp != NULL) {
|
|
|
|
printf("aout_loadfile: kernel already loaded\n");
|
1998-09-03 02:10:09 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto oerr;
|
1998-08-31 21:10:43 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Calculate destination address based on kernel entrypoint
|
|
|
|
* XXX this is i386-freebsd-aout specific
|
|
|
|
*/
|
|
|
|
dest = ehdr.a_entry & 0x100000;
|
|
|
|
if (dest == 0) {
|
2000-05-01 17:41:25 +00:00
|
|
|
printf("aout_loadfile: not a kernel (maybe static binary?)\n");
|
1998-09-03 02:10:09 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto oerr;
|
1998-08-31 21:10:43 +00:00
|
|
|
}
|
|
|
|
kernel = 1;
|
|
|
|
} else {
|
1998-09-03 02:10:09 +00:00
|
|
|
err = EFTYPE;
|
|
|
|
goto oerr;
|
1998-08-31 21:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, we think we should handle this.
|
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
fp = file_alloc();
|
1998-09-14 18:27:06 +00:00
|
|
|
if (kernel)
|
1998-10-09 23:15:39 +00:00
|
|
|
setenv("kernelname", filename, 1);
|
|
|
|
s = strrchr(filename, '/');
|
|
|
|
if (s)
|
2000-05-01 17:41:25 +00:00
|
|
|
fp->f_name = strdup(s + 1);
|
1998-10-09 23:15:39 +00:00
|
|
|
else
|
2000-05-01 17:41:25 +00:00
|
|
|
fp->f_name = strdup(filename);
|
|
|
|
fp->f_type = strdup(kernel ? aout_kerneltype : aout_moduletype);
|
1998-09-14 18:27:06 +00:00
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/* Page-align the load address */
|
|
|
|
addr = dest;
|
|
|
|
pad = (u_int)addr & PAGE_MASK;
|
|
|
|
if (pad != 0) {
|
|
|
|
pad = PAGE_SIZE - pad;
|
|
|
|
addr += pad;
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
fp->f_addr = addr; /* save the aligned load address */
|
1998-10-12 09:05:12 +00:00
|
|
|
if (kernel)
|
|
|
|
printf("%s at %p\n", filename, (void *) addr);
|
1998-08-31 21:10:43 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
fp->f_size = aout_loadimage(fp, fd, addr, &ehdr, kernel);
|
|
|
|
if (fp->f_size == 0)
|
1998-08-31 21:10:43 +00:00
|
|
|
goto ioerr;
|
|
|
|
|
1998-10-09 23:15:39 +00:00
|
|
|
#if 0
|
1998-09-03 02:10:09 +00:00
|
|
|
/* Handle KLD module data */
|
2000-05-01 17:41:25 +00:00
|
|
|
if (!kernel && ((err = aout_fixupkldmod(fp, &ehdr)) != 0))
|
1998-09-03 02:10:09 +00:00
|
|
|
goto oerr;
|
1998-10-09 23:15:39 +00:00
|
|
|
#endif
|
1998-09-03 02:10:09 +00:00
|
|
|
|
1998-08-31 21:10:43 +00:00
|
|
|
/* save exec header as metadata */
|
2000-05-01 17:41:25 +00:00
|
|
|
file_addmetadata(fp, MODINFOMD_AOUTEXEC, sizeof(struct exec), &ehdr);
|
1998-08-31 21:10:43 +00:00
|
|
|
|
|
|
|
/* Load OK, return module pointer */
|
2000-05-01 17:41:25 +00:00
|
|
|
*result = fp;
|
1998-09-03 02:10:09 +00:00
|
|
|
err = 0;
|
|
|
|
goto out;
|
|
|
|
|
1998-08-31 21:10:43 +00:00
|
|
|
ioerr:
|
|
|
|
err = EIO;
|
1998-09-03 02:10:09 +00:00
|
|
|
oerr:
|
2000-05-01 17:41:25 +00:00
|
|
|
file_discard(fp);
|
1998-09-03 02:10:09 +00:00
|
|
|
out:
|
|
|
|
close(fd);
|
1998-08-31 21:10:43 +00:00
|
|
|
return(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* With the file (fd) open on the image, and (ehdr) containing
|
|
|
|
* the exec header, load the image at (addr)
|
|
|
|
*
|
|
|
|
* Fixup the a_bss field in (ehdr) to reflect the padding added to
|
|
|
|
* align the symbol table.
|
|
|
|
*/
|
|
|
|
static int
|
2000-05-01 17:41:25 +00:00
|
|
|
aout_loadimage(struct preloaded_file *fp, int fd, vm_offset_t loadaddr, struct exec *ehdr, int kernel)
|
1998-08-31 21:10:43 +00:00
|
|
|
{
|
|
|
|
u_int pad;
|
|
|
|
vm_offset_t addr;
|
|
|
|
int ss;
|
1998-10-09 23:15:39 +00:00
|
|
|
vm_offset_t ssym, esym;
|
1998-09-03 02:10:09 +00:00
|
|
|
|
1998-08-31 21:10:43 +00:00
|
|
|
addr = loadaddr;
|
|
|
|
lseek(fd, N_TXTOFF(*ehdr), SEEK_SET);
|
1998-09-03 02:10:09 +00:00
|
|
|
|
1998-08-31 21:10:43 +00:00
|
|
|
/* text segment */
|
1998-09-03 02:10:09 +00:00
|
|
|
printf(" text=0x%lx ", ehdr->a_text);
|
1998-08-31 21:10:43 +00:00
|
|
|
if (archsw.arch_readin(fd, addr, ehdr->a_text) != ehdr->a_text)
|
|
|
|
return(0);
|
|
|
|
addr += ehdr->a_text;
|
|
|
|
|
|
|
|
/* data segment */
|
|
|
|
printf("data=0x%lx ", ehdr->a_data);
|
|
|
|
if (archsw.arch_readin(fd, addr, ehdr->a_data) != ehdr->a_data)
|
|
|
|
return(0);
|
|
|
|
addr += ehdr->a_data;
|
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/* For kernels, we pad the BSS to a page boundary */
|
|
|
|
if (kernel) {
|
|
|
|
pad = (u_int)ehdr->a_bss & PAGE_MASK;
|
|
|
|
if (pad != 0) {
|
|
|
|
pad = PAGE_SIZE - pad;
|
|
|
|
ehdr->a_bss += pad;
|
|
|
|
}
|
|
|
|
}
|
1998-08-31 21:10:43 +00:00
|
|
|
printf("bss=0x%lx ", ehdr->a_bss);
|
|
|
|
addr += ehdr->a_bss;
|
|
|
|
|
|
|
|
/* symbol table size */
|
1999-01-25 10:34:35 +00:00
|
|
|
ssym = esym = addr;
|
1998-12-22 11:41:51 +00:00
|
|
|
if(ehdr->a_syms!=NULL) {
|
|
|
|
archsw.arch_copyin(&ehdr->a_syms, addr, sizeof(ehdr->a_syms));
|
|
|
|
addr += sizeof(ehdr->a_syms);
|
1998-08-31 21:10:43 +00:00
|
|
|
|
1998-12-22 11:41:51 +00:00
|
|
|
/* symbol table */
|
2000-05-01 18:02:38 +00:00
|
|
|
printf("symbols=[0x%lx+0x%lx", (long)sizeof(ehdr->a_syms),ehdr->a_syms);
|
1998-12-22 11:41:51 +00:00
|
|
|
if (archsw.arch_readin(fd, addr, ehdr->a_syms) != ehdr->a_syms)
|
|
|
|
return(0);
|
|
|
|
addr += ehdr->a_syms;
|
1998-08-31 21:10:43 +00:00
|
|
|
|
1998-12-22 11:41:51 +00:00
|
|
|
/* string table */
|
|
|
|
read(fd, &ss, sizeof(ss));
|
|
|
|
archsw.arch_copyin(&ss, addr, sizeof(ss));
|
|
|
|
addr += sizeof(ss);
|
|
|
|
ss -= sizeof(ss);
|
2000-05-01 18:02:38 +00:00
|
|
|
printf("+0x%lx+0x%x]", (long)sizeof(ss), ss);
|
1998-12-22 11:41:51 +00:00
|
|
|
if (archsw.arch_readin(fd, addr, ss) != ss)
|
|
|
|
return(0);
|
|
|
|
addr += ss;
|
1999-01-25 10:34:35 +00:00
|
|
|
esym = addr;
|
1998-10-09 23:15:39 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
|
|
|
|
file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
|
1998-12-22 11:41:51 +00:00
|
|
|
} else {
|
|
|
|
printf("symbols=[none]");
|
|
|
|
}
|
|
|
|
printf("\n");
|
1998-08-31 21:10:43 +00:00
|
|
|
|
|
|
|
return(addr - loadaddr);
|
|
|
|
}
|
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
|