1 /* $NetBSD: load_elf.cpp,v 1.14 2005/12/11 12:17:28 christos 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 55 _sym_blk.enable = FALSE; 56 _ph = NULL; 57 _sh = NULL; 58 59 DPRINTF((TEXT("Loader: ELF\n"))); 60 } 61 62 ElfLoader::~ElfLoader(void) 63 { 64 65 if (_sym_blk.header != NULL) 66 free(_sym_blk.header); 67 if (_ph != NULL) 68 free(_ph); 69 if (_sh != NULL) 70 free(_sh); 71 } 72 73 BOOL 74 ElfLoader::setFile(File *&file) 75 { 76 size_t sz; 77 Loader::setFile(file); 78 79 // read ELF header and check it 80 if (!read_header()) 81 return FALSE; 82 83 // read section header 84 sz = _eh.e_shnum * _eh.e_shentsize; 85 if ((_sh = static_cast<Elf_Shdr *>(malloc(sz))) == NULL) { 86 DPRINTF((TEXT("can't allocate section header table.\n"))); 87 return FALSE; 88 } 89 if (_file->read(_sh, sz, _eh.e_shoff) != sz) { 90 DPRINTF((TEXT("section header read error.\n"))); 91 return FALSE; 92 } 93 94 // read program header 95 sz = _eh.e_phnum * _eh.e_phentsize; 96 if ((_ph = static_cast<Elf_Phdr *>(malloc(sz))) == NULL) { 97 DPRINTF((TEXT("can't allocate program header table.\n"))); 98 return FALSE; 99 } 100 if (_file->read(_ph, sz, _eh.e_phoff) != sz) { 101 DPRINTF((TEXT("program header read error.\n"))); 102 return FALSE; 103 } 104 105 return TRUE; 106 } 107 108 size_t 109 ElfLoader::memorySize() 110 { 111 int i; 112 Elf_Phdr *ph = _ph; 113 size_t sz = 0; 114 115 DPRINTF((TEXT("file size: "))); 116 for (i = 0; i < _eh.e_phnum; i++, ph++) { 117 if (ph->p_type == PT_LOAD) { 118 size_t filesz = ph->p_filesz; 119 DPRINTF((TEXT("+0x%x"), filesz)); 120 sz += _mem->roundPage(filesz); 121 } 122 } 123 124 // reserve for symbols 125 size_t symblk_sz = symbol_block_size(); 126 if (symblk_sz) { 127 sz += symblk_sz; 128 DPRINTF((TEXT(" = 0x%x]"), symblk_sz)); 129 } 130 131 DPRINTF((TEXT(" = 0x%x byte\n"), sz)); 132 return sz; 133 } 134 135 kaddr_t 136 ElfLoader::jumpAddr() 137 { 138 DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _eh.e_entry)); 139 return _eh.e_entry; 140 } 141 142 BOOL 143 ElfLoader::load() 144 { 145 Elf_Phdr *ph; 146 vaddr_t kv; 147 int i; 148 149 _load_segment_start(); 150 151 for (i = 0, ph = _ph; i < _eh.e_phnum; i++, ph++) { 152 if (ph->p_type == PT_LOAD) { 153 size_t filesz = ph->p_filesz; 154 size_t memsz = ph->p_memsz; 155 kv = ph->p_vaddr; 156 off_t fileofs = ph->p_offset; 157 DPRINTF((TEXT("[%d] vaddr 0x%08x file size 0x%x mem size 0x%x\n"), 158 i, kv, filesz, memsz)); 159 _load_segment(kv, memsz, fileofs, filesz); 160 kv += ROUND4(memsz); 161 } 162 } 163 164 load_symbol_block(kv); 165 166 // tag chain still opening 167 168 return _load_success(); 169 } 170 171 // 172 // Prepare ELF headers for symbol table. 173 // 174 // ELF header 175 // section header 176 // shstrtab 177 // symtab 178 // strtab 179 // 180 size_t 181 ElfLoader::symbol_block_size() 182 { 183 size_t shstrsize = ROUND4(_sh[_eh.e_shstrndx].sh_size); 184 size_t shtab_sz = _eh.e_shentsize * _eh.e_shnum; 185 off_t shstrtab_offset = sizeof(Elf_Ehdr) + shtab_sz; 186 int i; 187 188 memset(&_sym_blk, 0, sizeof(_sym_blk)); 189 _sym_blk.enable = FALSE; 190 _sym_blk.header_size = sizeof(Elf_Ehdr) + shtab_sz + shstrsize; 191 192 // inquire string and symbol table size 193 _sym_blk.header = static_cast<char *>(malloc(_sym_blk.header_size)); 194 if (_sym_blk.header == NULL) { 195 MessageBox(HPC_MENU._root->_window, 196 TEXT("Can't determine symbol block size."), 197 TEXT("WARNING"), 198 MB_ICONWARNING | MB_OK); 199 UpdateWindow(HPC_MENU._root->_window); 200 return (0); 201 } 202 203 // set pointer for symbol block 204 Elf_Ehdr *eh = reinterpret_cast<Elf_Ehdr *>(_sym_blk.header); 205 Elf_Shdr *sh = reinterpret_cast<Elf_Shdr *> 206 (_sym_blk.header + sizeof(Elf_Ehdr)); 207 char *shstrtab = _sym_blk.header + shstrtab_offset; 208 209 // initialize headers 210 memset(_sym_blk.header, 0, _sym_blk.header_size); 211 memcpy(eh, &_eh, sizeof(Elf_Ehdr)); 212 eh->e_phoff = 0; 213 eh->e_phnum = 0; 214 eh->e_entry = 0; // XXX NetBSD kernel check this member. see machdep.c 215 eh->e_shoff = sizeof(Elf_Ehdr); 216 memcpy(sh, _sh, shtab_sz); 217 218 // inquire symbol/string table information 219 _file->read(shstrtab, shstrsize, _sh[_eh.e_shstrndx].sh_offset); 220 for (i = 0; i < _eh.e_shnum; i++, sh++) { 221 if (strcmp(".strtab", shstrtab + sh->sh_name) == 0) { 222 _sym_blk.shstr = sh; 223 _sym_blk.stroff = sh->sh_offset; 224 } else if (strcmp(".symtab", shstrtab + sh->sh_name) == 0) { 225 _sym_blk.shsym = sh; 226 _sym_blk.symoff = sh->sh_offset; 227 } 228 229 sh->sh_offset = (i == _eh.e_shstrndx) ? shstrtab_offset : 0; 230 } 231 232 if (_sym_blk.shstr == NULL || _sym_blk.shsym == NULL) { 233 if (HPC_PREFERENCE.safety_message) { 234 MessageBox(HPC_MENU._root->_window, 235 TEXT("No symbol and/or string table in binary.\n(not fatal)"), 236 TEXT("Information"), 237 MB_ICONINFORMATION | MB_OK); 238 UpdateWindow(HPC_MENU._root->_window); 239 } 240 free(_sym_blk.header); 241 _sym_blk.header = NULL; 242 243 return (0); 244 } 245 246 // set Section Headers for symbol/string table 247 _sym_blk.shsym->sh_offset = shstrtab_offset + shstrsize; 248 _sym_blk.shstr->sh_offset = shstrtab_offset + shstrsize + 249 ROUND4(_sym_blk.shsym->sh_size); 250 _sym_blk.enable = TRUE; 251 252 DPRINTF((TEXT("+[(symbol block: header %d symbol %d string %d byte)"), 253 _sym_blk.header_size,_sym_blk.shsym->sh_size, 254 _sym_blk.shstr->sh_size)); 255 256 // return total amount of symbol block 257 return (_sym_blk.header_size + ROUND4(_sym_blk.shsym->sh_size) + 258 _sym_blk.shstr->sh_size); 259 } 260 261 void 262 ElfLoader::load_symbol_block(vaddr_t kv) 263 { 264 size_t sz; 265 266 if (!_sym_blk.enable) 267 return; 268 269 // load header 270 _load_memory(kv, _sym_blk.header_size, _sym_blk.header); 271 kv += _sym_blk.header_size; 272 273 // load symbol table 274 sz = _sym_blk.shsym->sh_size; 275 _load_segment(kv, sz, _sym_blk.symoff, sz); 276 kv += ROUND4(sz); 277 278 // load string table 279 sz = _sym_blk.shstr->sh_size; 280 _load_segment(kv, sz, _sym_blk.stroff, sz); 281 } 282 283 BOOL 284 ElfLoader::read_header() 285 { 286 // read ELF header 287 _file->read(&_eh, sizeof(Elf_Ehdr), 0); 288 289 // check ELF Magic. 290 if (!is_elf_file()) { 291 DPRINTF((TEXT("not a ELF file.\n"))); 292 return FALSE; 293 } 294 295 // Windows CE is 32bit little-endian only. 296 if (_eh.e_ident[EI_DATA] != ELFDATA2LSB || 297 _eh.e_ident[EI_CLASS] != ELFCLASS32) { 298 DPRINTF((TEXT("invalid class/data(%d/%d)\n"), 299 _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA])); 300 return FALSE; 301 } 302 303 // Is native architecture? 304 switch(_eh.e_machine) { 305 ELF32_MACHDEP_ID_CASES; 306 default: 307 DPRINTF((TEXT("not a native architecture. machine = %d\n"), 308 _eh.e_machine)); 309 return FALSE; 310 } 311 312 // Check object type 313 if (_eh.e_type != ET_EXEC) { 314 DPRINTF((TEXT("not a executable file. type = %d\n"), 315 _eh.e_type)); 316 return FALSE; 317 } 318 319 if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 || 320 _eh.e_phentsize != sizeof(Elf_Phdr)) { 321 DPRINTF((TEXT("invalid program header information.\n"))); 322 return FALSE; 323 } 324 325 return TRUE; 326 } 327