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