Home | History | Annotate | Line # | Download | only in hpcboot
load_elf.cpp revision 1.6.6.2
      1  1.6.6.2  nathanw /*	$NetBSD: load_elf.cpp,v 1.6.6.2 2002/02/28 04:09:45 nathanw Exp $	*/
      2  1.6.6.2  nathanw 
      3  1.6.6.2  nathanw /*-
      4  1.6.6.2  nathanw  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.6.6.2  nathanw  * All rights reserved.
      6  1.6.6.2  nathanw  *
      7  1.6.6.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.6.6.2  nathanw  * by UCHIYAMA Yasushi.
      9  1.6.6.2  nathanw  *
     10  1.6.6.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.6.6.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.6.6.2  nathanw  * are met:
     13  1.6.6.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.6.6.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.6.6.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.6.6.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.6.6.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.6.6.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.6.6.2  nathanw  *    must display the following acknowledgement:
     20  1.6.6.2  nathanw  *        This product includes software developed by the NetBSD
     21  1.6.6.2  nathanw  *        Foundation, Inc. and its contributors.
     22  1.6.6.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.6.6.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.6.6.2  nathanw  *    from this software without specific prior written permission.
     25  1.6.6.2  nathanw  *
     26  1.6.6.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.6.6.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.6.6.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.6.6.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.6.6.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.6.6.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.6.6.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.6.6.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.6.6.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.6.6.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.6.6.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.6.6.2  nathanw  */
     38  1.6.6.2  nathanw 
     39  1.6.6.2  nathanw #include <hpcmenu.h>
     40  1.6.6.2  nathanw #include <menu/window.h>
     41  1.6.6.2  nathanw #include <menu/rootwindow.h>
     42  1.6.6.2  nathanw 
     43  1.6.6.2  nathanw #include <load.h>
     44  1.6.6.2  nathanw #include <load_elf.h>
     45  1.6.6.2  nathanw #include <console.h>
     46  1.6.6.2  nathanw #include <memory.h>
     47  1.6.6.2  nathanw #include <file.h>
     48  1.6.6.2  nathanw 
     49  1.6.6.2  nathanw #define ROUND4(x)	(((x) + 3) & ~3)
     50  1.6.6.2  nathanw 
     51  1.6.6.2  nathanw ElfLoader::ElfLoader(Console *&cons, MemoryManager *&mem)
     52  1.6.6.2  nathanw 	: Loader(cons, mem)
     53  1.6.6.2  nathanw {
     54  1.6.6.2  nathanw 	_sym_blk.enable = FALSE;
     55  1.6.6.2  nathanw 
     56  1.6.6.2  nathanw 	DPRINTF((TEXT("Loader: ELF\n")));
     57  1.6.6.2  nathanw }
     58  1.6.6.2  nathanw 
     59  1.6.6.2  nathanw ElfLoader::~ElfLoader(void)
     60  1.6.6.2  nathanw {
     61  1.6.6.2  nathanw 	if (_sym_blk.header != NULL)
     62  1.6.6.2  nathanw 		free (_sym_blk.header);
     63  1.6.6.2  nathanw }
     64  1.6.6.2  nathanw 
     65  1.6.6.2  nathanw BOOL
     66  1.6.6.2  nathanw ElfLoader::setFile(File *&file)
     67  1.6.6.2  nathanw {
     68  1.6.6.2  nathanw 	size_t sz;
     69  1.6.6.2  nathanw 	Loader::setFile(file);
     70  1.6.6.2  nathanw 
     71  1.6.6.2  nathanw 	// read ELF header and check it
     72  1.6.6.2  nathanw 	if (!read_header())
     73  1.6.6.2  nathanw 		return FALSE;
     74  1.6.6.2  nathanw 	// read section header
     75  1.6.6.2  nathanw 	sz = _eh.e_shnum * _eh.e_shentsize;
     76  1.6.6.2  nathanw 	_file->read(_sh, _eh.e_shentsize * _eh.e_shnum, _eh.e_shoff);
     77  1.6.6.2  nathanw 
     78  1.6.6.2  nathanw 	// read program header
     79  1.6.6.2  nathanw 	sz = _eh.e_phnum * _eh.e_phentsize;
     80  1.6.6.2  nathanw 
     81  1.6.6.2  nathanw 	return _file->read(_ph, sz, _eh.e_phoff) == sz;
     82  1.6.6.2  nathanw }
     83  1.6.6.2  nathanw 
     84  1.6.6.2  nathanw size_t
     85  1.6.6.2  nathanw ElfLoader::memorySize()
     86  1.6.6.2  nathanw {
     87  1.6.6.2  nathanw 	int i;
     88  1.6.6.2  nathanw 	Elf_Phdr *ph = _ph;
     89  1.6.6.2  nathanw 	size_t sz = 0;
     90  1.6.6.2  nathanw 
     91  1.6.6.2  nathanw 	DPRINTF((TEXT("file size: ")));
     92  1.6.6.2  nathanw 	for (i = 0; i < _eh.e_phnum; i++, ph++) {
     93  1.6.6.2  nathanw 		if (ph->p_type == PT_LOAD) {
     94  1.6.6.2  nathanw 			size_t filesz = ph->p_filesz;
     95  1.6.6.2  nathanw 			DPRINTF((TEXT("+0x%x"), filesz));
     96  1.6.6.2  nathanw 			sz += _mem->roundPage(filesz);
     97  1.6.6.2  nathanw 		}
     98  1.6.6.2  nathanw 	}
     99  1.6.6.2  nathanw 
    100  1.6.6.2  nathanw 	// reserve for symbols
    101  1.6.6.2  nathanw 	size_t symblk_sz = symbol_block_size();
    102  1.6.6.2  nathanw 	if (symblk_sz) {
    103  1.6.6.2  nathanw 		sz += symblk_sz;
    104  1.6.6.2  nathanw 		DPRINTF((TEXT(" = 0x%x]"), symblk_sz));
    105  1.6.6.2  nathanw 	}
    106  1.6.6.2  nathanw 
    107  1.6.6.2  nathanw 	DPRINTF((TEXT(" = 0x%x byte\n"), sz));
    108  1.6.6.2  nathanw 	return sz;
    109  1.6.6.2  nathanw }
    110  1.6.6.2  nathanw 
    111  1.6.6.2  nathanw kaddr_t
    112  1.6.6.2  nathanw ElfLoader::jumpAddr()
    113  1.6.6.2  nathanw {
    114  1.6.6.2  nathanw 	DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _eh.e_entry));
    115  1.6.6.2  nathanw 	return _eh.e_entry;
    116  1.6.6.2  nathanw }
    117  1.6.6.2  nathanw 
    118  1.6.6.2  nathanw BOOL
    119  1.6.6.2  nathanw ElfLoader::load()
    120  1.6.6.2  nathanw {
    121  1.6.6.2  nathanw 	Elf_Phdr *ph;
    122  1.6.6.2  nathanw 	vaddr_t kv;
    123  1.6.6.2  nathanw 	int i;
    124  1.6.6.2  nathanw 
    125  1.6.6.2  nathanw 	_load_segment_start();
    126  1.6.6.2  nathanw 
    127  1.6.6.2  nathanw 	for (i = 0, ph = _ph; i < _eh.e_phnum; i++, ph++) {
    128  1.6.6.2  nathanw 		if (ph->p_type == PT_LOAD) {
    129  1.6.6.2  nathanw 			size_t filesz = ph->p_filesz;
    130  1.6.6.2  nathanw 			size_t memsz = ph->p_memsz;
    131  1.6.6.2  nathanw 			kv = ph->p_vaddr;
    132  1.6.6.2  nathanw 			off_t fileofs = ph->p_offset;
    133  1.6.6.2  nathanw 			DPRINTF((TEXT("[%d] vaddr 0x%08x file size 0x%x mem size 0x%x\n"),
    134  1.6.6.2  nathanw 			    i, kv, filesz, memsz));
    135  1.6.6.2  nathanw 			_load_segment(kv, memsz, fileofs, filesz);
    136  1.6.6.2  nathanw 			kv += memsz;
    137  1.6.6.2  nathanw 		}
    138  1.6.6.2  nathanw 	}
    139  1.6.6.2  nathanw 
    140  1.6.6.2  nathanw 	load_symbol_block(kv);
    141  1.6.6.2  nathanw 
    142  1.6.6.2  nathanw 	// tag chain still opening
    143  1.6.6.2  nathanw 
    144  1.6.6.2  nathanw 	return _load_success();
    145  1.6.6.2  nathanw }
    146  1.6.6.2  nathanw 
    147  1.6.6.2  nathanw //
    148  1.6.6.2  nathanw // Prepare ELF headers for symbol table.
    149  1.6.6.2  nathanw //
    150  1.6.6.2  nathanw //   ELF header
    151  1.6.6.2  nathanw //   section header
    152  1.6.6.2  nathanw //   shstrtab
    153  1.6.6.2  nathanw //   strtab
    154  1.6.6.2  nathanw //   symtab
    155  1.6.6.2  nathanw //
    156  1.6.6.2  nathanw size_t
    157  1.6.6.2  nathanw ElfLoader::symbol_block_size()
    158  1.6.6.2  nathanw {
    159  1.6.6.2  nathanw 	size_t shstrsize = ROUND4(_sh[_eh.e_shstrndx].sh_size);
    160  1.6.6.2  nathanw 	size_t shtab_sz = _eh.e_shentsize * _eh.e_shnum;
    161  1.6.6.2  nathanw 	off_t shstrtab_offset = sizeof(Elf_Ehdr) + shtab_sz;
    162  1.6.6.2  nathanw 	int i;
    163  1.6.6.2  nathanw 
    164  1.6.6.2  nathanw 	memset(&_sym_blk, 0, sizeof(_sym_blk));
    165  1.6.6.2  nathanw 	_sym_blk.enable = FALSE;
    166  1.6.6.2  nathanw 	_sym_blk.header_size = sizeof(Elf_Ehdr) + shtab_sz + shstrsize;
    167  1.6.6.2  nathanw 
    168  1.6.6.2  nathanw 	// inquire string and symbol table size
    169  1.6.6.2  nathanw 	_sym_blk.header = static_cast<char *>(malloc(_sym_blk.header_size));
    170  1.6.6.2  nathanw 	if (_sym_blk.header == NULL) {
    171  1.6.6.2  nathanw 		MessageBox(HPC_MENU._root->_window,
    172  1.6.6.2  nathanw 		    TEXT("couldn't determine symbol block size"),
    173  1.6.6.2  nathanw 		    TEXT("WARNING"), 0);
    174  1.6.6.2  nathanw 		return (0);
    175  1.6.6.2  nathanw 	}
    176  1.6.6.2  nathanw 
    177  1.6.6.2  nathanw 	// set pointer for symbol block
    178  1.6.6.2  nathanw 	Elf_Ehdr *eh = reinterpret_cast<Elf_Ehdr *>(_sym_blk.header);
    179  1.6.6.2  nathanw 	Elf_Shdr *sh = reinterpret_cast<Elf_Shdr *>
    180  1.6.6.2  nathanw 	    (_sym_blk.header + sizeof(Elf_Ehdr));
    181  1.6.6.2  nathanw 	char *shstrtab = _sym_blk.header + shstrtab_offset;
    182  1.6.6.2  nathanw 
    183  1.6.6.2  nathanw 	// initialize headers
    184  1.6.6.2  nathanw 	memset(_sym_blk.header, 0, _sym_blk.header_size);
    185  1.6.6.2  nathanw 	memcpy(eh, &_eh, sizeof(Elf_Ehdr));
    186  1.6.6.2  nathanw 	eh->e_phoff = 0;
    187  1.6.6.2  nathanw 	eh->e_phnum = 0;
    188  1.6.6.2  nathanw 	eh->e_entry = 0; // XXX NetBSD kernel check this member. see machdep.c
    189  1.6.6.2  nathanw 	eh->e_shoff = sizeof(Elf_Ehdr);
    190  1.6.6.2  nathanw 	memcpy(sh, _sh, shtab_sz);
    191  1.6.6.2  nathanw 
    192  1.6.6.2  nathanw 	// inquire symbol/string table information
    193  1.6.6.2  nathanw 	_file->read(shstrtab, shstrsize, _sh[_eh.e_shstrndx].sh_offset);
    194  1.6.6.2  nathanw 	for (i = 0; i < _eh.e_shnum; i++, sh++) {
    195  1.6.6.2  nathanw 		if (strcmp(".strtab", shstrtab + sh->sh_name) == 0) {
    196  1.6.6.2  nathanw 			_sym_blk.shstr = sh;
    197  1.6.6.2  nathanw 			_sym_blk.stroff = sh->sh_offset;
    198  1.6.6.2  nathanw 		} else if (strcmp(".symtab", shstrtab + sh->sh_name) == 0) {
    199  1.6.6.2  nathanw 			_sym_blk.shsym = sh;
    200  1.6.6.2  nathanw 			_sym_blk.symoff = sh->sh_offset;
    201  1.6.6.2  nathanw 		}
    202  1.6.6.2  nathanw 
    203  1.6.6.2  nathanw 		sh->sh_offset = (i == _eh.e_shstrndx) ? shstrtab_offset : 0;
    204  1.6.6.2  nathanw 	}
    205  1.6.6.2  nathanw 
    206  1.6.6.2  nathanw 	if (_sym_blk.shstr == NULL || _sym_blk.shsym == NULL) {
    207  1.6.6.2  nathanw 		if (HPC_PREFERENCE.safety_message)
    208  1.6.6.2  nathanw 			MessageBox(HPC_MENU._root->_window,
    209  1.6.6.2  nathanw 			    TEXT("no symbol and/or string table in binary. (not fatal)"),
    210  1.6.6.2  nathanw 			    TEXT("Information"), 0);
    211  1.6.6.2  nathanw 		free(_sym_blk.header);
    212  1.6.6.2  nathanw 		_sym_blk.header = NULL;
    213  1.6.6.2  nathanw 
    214  1.6.6.2  nathanw 		return (0);
    215  1.6.6.2  nathanw 	}
    216  1.6.6.2  nathanw 
    217  1.6.6.2  nathanw 	// set Section Headers for symbol/string table
    218  1.6.6.2  nathanw 	_sym_blk.shstr->sh_offset = shstrtab_offset + shstrsize;
    219  1.6.6.2  nathanw 	_sym_blk.shsym->sh_offset = shstrtab_offset + shstrsize +
    220  1.6.6.2  nathanw 	    ROUND4(_sym_blk.shstr->sh_size);
    221  1.6.6.2  nathanw 	_sym_blk.enable = TRUE;
    222  1.6.6.2  nathanw 
    223  1.6.6.2  nathanw 	DPRINTF((TEXT("+[(symbol block: header %d string %d symbol %d byte)"),
    224  1.6.6.2  nathanw 	    _sym_blk.header_size,_sym_blk.shstr->sh_size,
    225  1.6.6.2  nathanw 	    _sym_blk.shsym->sh_size));
    226  1.6.6.2  nathanw 
    227  1.6.6.2  nathanw 	// return total amount of symbol block
    228  1.6.6.2  nathanw 	return (_sym_blk.header_size + ROUND4(_sym_blk.shstr->sh_size) +
    229  1.6.6.2  nathanw 	    _sym_blk.shsym->sh_size);
    230  1.6.6.2  nathanw }
    231  1.6.6.2  nathanw 
    232  1.6.6.2  nathanw void
    233  1.6.6.2  nathanw ElfLoader::load_symbol_block(vaddr_t kv)
    234  1.6.6.2  nathanw {
    235  1.6.6.2  nathanw 	size_t sz;
    236  1.6.6.2  nathanw 
    237  1.6.6.2  nathanw 	if (!_sym_blk.enable)
    238  1.6.6.2  nathanw 		return;
    239  1.6.6.2  nathanw 
    240  1.6.6.2  nathanw 	// load header
    241  1.6.6.2  nathanw 	_load_memory(kv, _sym_blk.header_size, _sym_blk.header);
    242  1.6.6.2  nathanw 	kv += _sym_blk.header_size;
    243  1.6.6.2  nathanw 
    244  1.6.6.2  nathanw 	// load string table
    245  1.6.6.2  nathanw 	sz = _sym_blk.shstr->sh_size;
    246  1.6.6.2  nathanw 	_load_segment(kv, sz, _sym_blk.stroff, sz);
    247  1.6.6.2  nathanw 	kv += ROUND4(sz);
    248  1.6.6.2  nathanw 
    249  1.6.6.2  nathanw 	// load symbol table
    250  1.6.6.2  nathanw 	sz = _sym_blk.shsym->sh_size;
    251  1.6.6.2  nathanw 	_load_segment(kv, sz, _sym_blk.symoff, sz);
    252  1.6.6.2  nathanw }
    253  1.6.6.2  nathanw 
    254  1.6.6.2  nathanw BOOL
    255  1.6.6.2  nathanw ElfLoader::read_header()
    256  1.6.6.2  nathanw {
    257  1.6.6.2  nathanw 	// read ELF header
    258  1.6.6.2  nathanw 	_file->read(&_eh, sizeof(Elf_Ehdr), 0);
    259  1.6.6.2  nathanw 
    260  1.6.6.2  nathanw 	// check ELF Magic.
    261  1.6.6.2  nathanw 	if (!is_elf_file()) {
    262  1.6.6.2  nathanw 		DPRINTF((TEXT("not a ELF file.\n")));
    263  1.6.6.2  nathanw 		return FALSE;
    264  1.6.6.2  nathanw 	}
    265  1.6.6.2  nathanw 
    266  1.6.6.2  nathanw 	// Windows CE is 32bit little-endian only.
    267  1.6.6.2  nathanw 	if (_eh.e_ident[EI_DATA] != ELFDATA2LSB ||
    268  1.6.6.2  nathanw 	    _eh.e_ident[EI_CLASS] != ELFCLASS32) {
    269  1.6.6.2  nathanw 		DPRINTF((TEXT("invalid class/data(%d/%d)\n"),
    270  1.6.6.2  nathanw 		    _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA]));
    271  1.6.6.2  nathanw 		return FALSE;
    272  1.6.6.2  nathanw 	}
    273  1.6.6.2  nathanw 
    274  1.6.6.2  nathanw 	// Is native architecture?
    275  1.6.6.2  nathanw 	switch(_eh.e_machine) {
    276  1.6.6.2  nathanw 		ELF32_MACHDEP_ID_CASES;
    277  1.6.6.2  nathanw 	default:
    278  1.6.6.2  nathanw 		DPRINTF((TEXT("not a native architecture. machine = %d\n"),
    279  1.6.6.2  nathanw 		    _eh.e_machine));
    280  1.6.6.2  nathanw 		return FALSE;
    281  1.6.6.2  nathanw 	}
    282  1.6.6.2  nathanw 
    283  1.6.6.2  nathanw 	// Check object type
    284  1.6.6.2  nathanw 	if (_eh.e_type != ET_EXEC) {
    285  1.6.6.2  nathanw 		DPRINTF((TEXT("not a executable file. type = %d\n"),
    286  1.6.6.2  nathanw 		    _eh.e_type));
    287  1.6.6.2  nathanw 		return FALSE;
    288  1.6.6.2  nathanw 	}
    289  1.6.6.2  nathanw 
    290  1.6.6.2  nathanw 	if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 ||
    291  1.6.6.2  nathanw 	    _eh.e_phentsize != sizeof(Elf_Phdr)) {
    292  1.6.6.2  nathanw 		DPRINTF((TEXT("invalid program header information.\n")));
    293  1.6.6.2  nathanw 		return FALSE;
    294  1.6.6.2  nathanw 	}
    295  1.6.6.2  nathanw 
    296  1.6.6.2  nathanw 	return TRUE;
    297  1.6.6.2  nathanw }
    298