elf32_freebsd.c revision 39902
139833Speter/*-
239833Speter * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
339833Speter * All rights reserved.
439833Speter *
539833Speter * Redistribution and use in source and binary forms, with or without
639833Speter * modification, are permitted provided that the following conditions
739833Speter * are met:
839833Speter * 1. Redistributions of source code must retain the above copyright
939833Speter *    notice, this list of conditions and the following disclaimer.
1039833Speter * 2. Redistributions in binary form must reproduce the above copyright
1139833Speter *    notice, this list of conditions and the following disclaimer in the
1239833Speter *    documentation and/or other materials provided with the distribution.
1339833Speter *
1439833Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1539833Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1639833Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1739833Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1839833Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1939833Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2039833Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2139833Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2239833Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2339833Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2439833Speter * SUCH DAMAGE.
2539833Speter *
2639902Smsmith *	$Id: elf_freebsd.c,v 1.2 1998/10/02 08:04:56 peter Exp $
2739833Speter */
2839833Speter
2939833Speter#include <sys/param.h>
3039833Speter#include <sys/exec.h>
3139833Speter#include <sys/reboot.h>
3239833Speter#include <sys/linker.h>
3339833Speter#include <string.h>
3439833Speter#include <machine/bootinfo.h>
3539833Speter#include <machine/elf.h>
3639833Speter#include <stand.h>
3739833Speter
3839833Speter#include "bootstrap.h"
3939833Speter#include "libi386.h"
4039833Speter#include "btxv86.h"
4139833Speter
4239833Speterstatic int	elf_exec(struct loaded_module *amp);
4339833Speter
4439833Speterstruct module_format i386_elf = { elf_loadmodule, elf_exec };
4539833Speter
4639833Speterstatic struct bootinfo	bi;
4739833Speter
4839833Speter/*
4939833Speter * There is an a.out kernel and one or more a.out modules loaded.
5039833Speter * We wish to start executing the kernel image, so make such
5139833Speter * preparations as are required, and do so.
5239833Speter */
5339833Speterstatic int
5439833Speterelf_exec(struct loaded_module *mp)
5539833Speter{
5639833Speter    struct module_metadata	*md;
5739833Speter    Elf_Ehdr 			*ehdr;
5839902Smsmith    vm_offset_t			entry, bootinfop;
5939902Smsmith    int				boothowto, err, bootdev;
6039902Smsmith    struct bootinfo		*bi;
6139902Smsmith    vm_offset_t			ssym, esym;
6239833Speter
6339833Speter    if ((md = mod_findmetadata(mp, MODINFOMD_ELFHDR)) == NULL)
6439833Speter	return(EFTYPE);			/* XXX actually EFUCKUP */
6539833Speter    ehdr = (Elf_Ehdr *)&(md->md_data);
6639833Speter
6739902Smsmith    /* XXX allow override? */
6839902Smsmith    setenv("kernelname", mp->m_name, 1);
6939833Speter
7039902Smsmith    if ((err = bi_load(mp->m_args, &boothowto, &bootdev, &bootinfop)) != 0)
7139902Smsmith	return(err);
7239902Smsmith    entry = ehdr->e_entry & 0xffffff;
7339902Smsmith
7439887Speter    if ((md = mod_findmetadata(mp, MODINFOMD_ELFSSYM)) != NULL)
7539887Speter	ssym = *((vm_offset_t *)&(md->md_data));
7639887Speter    if ((md = mod_findmetadata(mp, MODINFOMD_ELFESYM)) != NULL)
7739887Speter	esym = *((vm_offset_t *)&(md->md_data));
7839887Speter    if (ssym == 0 || esym == 0)
7939887Speter	ssym = esym = 0;		/* sanity */
8039887Speter
8139902Smsmith    bi = (struct bootinfo *)PTOV(bootinfop);
8239902Smsmith    bi->bi_symtab = ssym;
8339902Smsmith    bi->bi_esymtab = esym;
8439833Speter
8539833Speter
8639833Speter#ifdef DEBUG
8739833Speter    printf("Start @ 0x%lx ...\n", entry);
8839833Speter#endif
8939833Speter
9039902Smsmith    __exec((void *)entry, boothowto, bootdev, 0, 0, 0, bootinfop);
9139833Speter
9239833Speter    panic("exec returned");
9339833Speter}
94