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