1 /* $NetBSD: load_elf.cpp,v 1.3 2001/05/08 18:51:23 uch 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 <load.h> 40 #include <load_elf.h> 41 #include <console.h> 42 #include <memory.h> 43 #include <file.h> 44 45 ElfLoader::ElfLoader(Console *&cons, MemoryManager *&mem) 46 : Loader(cons, mem) 47 { 48 DPRINTF((TEXT("Loader: ELF\n"))); 49 } 50 51 ElfLoader::~ElfLoader(void) 52 { 53 } 54 55 BOOL 56 ElfLoader::setFile(File *&file) 57 { 58 size_t sz; 59 Loader::setFile(file); 60 61 /* read ELF header and check it */ 62 if (!read_header()) 63 return FALSE; 64 /* read section header */ 65 sz = _eh.e_shnum * _eh.e_shentsize; 66 _file->read(_sh, _eh.e_shentsize * _eh.e_shnum, _eh.e_shoff); 67 68 /* read program header */ 69 sz = _eh.e_phnum * _eh.e_phentsize; 70 71 return _file->read(_ph, sz, _eh.e_phoff) == sz; 72 } 73 74 size_t 75 ElfLoader::memorySize() 76 { 77 int i; 78 Elf_Phdr *ph = _ph; 79 size_t sz = 0; 80 81 DPRINTF((TEXT("file size: "))); 82 for (i = 0; i < _eh.e_phnum; i++, ph++) { 83 if (ph->p_type == PT_LOAD) { 84 size_t filesz = ph->p_filesz; 85 DPRINTF((TEXT("+0x%x"), filesz)); 86 sz += _mem->roundPage(filesz); 87 } 88 } 89 /* XXX reserve 192kB for symbols */ 90 sz += 0x30000; 91 92 DPRINTF((TEXT(" = 0x%x byte\n"), sz)); 93 return sz; 94 } 95 96 kaddr_t 97 ElfLoader::jumpAddr() 98 { 99 DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _eh.e_entry)); 100 return _eh.e_entry; 101 } 102 103 BOOL 104 ElfLoader::load() 105 { 106 Elf_Phdr *ph; 107 Elf_Shdr *sh, *shstr, *shsym; 108 off_t stroff = 0, symoff = 0, off; 109 vaddr_t kv; 110 111 size_t shstrsize; 112 char buf[1024]; 113 int i; 114 115 _load_segment_start(); 116 117 for (i = 0, ph = _ph; i < _eh.e_phnum; i++, ph++) { 118 if (ph->p_type == PT_LOAD) { 119 size_t filesz = ph->p_filesz; 120 size_t memsz = ph->p_memsz; 121 kv = ph->p_vaddr; 122 off_t fileofs = ph->p_offset; 123 DPRINTF((TEXT("[%d] vaddr 0x%08x file size 0x%x mem size 0x%x\n"), 124 i, kv, filesz, memsz)); 125 _load_segment(kv, memsz, fileofs, filesz); 126 kv += memsz; 127 } 128 } 129 130 /* 131 * Prepare ELF headers for symbol table. 132 * 133 * ELF header 134 * section header 135 * shstrtab 136 * strtab 137 * symtab 138 */ 139 memcpy(buf, &_eh, sizeof(_eh)); 140 ((Elf_Ehdr *)buf)->e_phoff = 0; 141 ((Elf_Ehdr *)buf)->e_phnum = 0; 142 ((Elf_Ehdr *)buf)->e_entry = 0; 143 ((Elf_Ehdr *)buf)->e_shoff = sizeof(_eh); 144 off = ((Elf_Ehdr *)buf)->e_shoff; 145 memcpy(buf + off, _sh, _eh.e_shentsize * _eh.e_shnum); 146 sh = (Elf_Shdr *)(buf + off); 147 off += _eh.e_shentsize * _eh.e_shnum; 148 149 /* load shstrtab and find desired sections */ 150 shstrsize = (sh[_eh.e_shstrndx].sh_size + 3) & ~0x3; 151 _file->read(buf + off, shstrsize, sh[_eh.e_shstrndx].sh_offset); 152 for(i = 0; i < _eh.e_shnum; i++, sh++) { 153 if (strcmp(".strtab", buf + off + sh->sh_name) == 0) { 154 stroff = sh->sh_offset; 155 shstr = sh; 156 } else if (strcmp(".symtab", buf + off + sh->sh_name) == 0) { 157 symoff = sh->sh_offset; 158 shsym = sh; 159 } 160 if (i == _eh.e_shstrndx) 161 sh->sh_offset = off; 162 else 163 sh->sh_offset = 0; 164 } 165 /* silently return if strtab or symtab can't be found */ 166 if (! stroff || ! symoff) 167 return TRUE; 168 169 shstr->sh_offset = off + shstrsize; 170 shsym->sh_offset = off + shstrsize + ((shstr->sh_size + 3) & ~0x3); 171 _load_memory(kv, off + shstrsize, buf); 172 kv += off + shstrsize; 173 _load_segment(kv, shstr->sh_size, stroff, shstr->sh_size); 174 kv += (shstr->sh_size + 3) & ~0x3; 175 _load_segment(kv, shsym->sh_size, symoff, shsym->sh_size); 176 177 /* tag chain still opening */ 178 179 return TRUE; 180 } 181 182 BOOL 183 ElfLoader::read_header(void) 184 { 185 // read ELF header 186 _file->read(&_eh, sizeof(Elf_Ehdr), 0); 187 188 // check ELF Magic. 189 if (!is_elf_file()) { 190 DPRINTF((TEXT("not a ELF file.\n"))); 191 return FALSE; 192 } 193 194 // Windows CE is 32bit little-endian only. 195 if (_eh.e_ident[EI_DATA] != ELFDATA2LSB || 196 _eh.e_ident[EI_CLASS] != ELFCLASS32) { 197 DPRINTF((TEXT("invalid class/data(%d/%d)\n"), 198 _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA])); 199 return FALSE; 200 } 201 202 // Is native architecture? 203 switch(_eh.e_machine) { 204 ELF32_MACHDEP_ID_CASES; 205 default: 206 DPRINTF((TEXT("not a native architecture. machine = %d\n"), 207 _eh.e_machine)); 208 return FALSE; 209 } 210 211 /* Check object type */ 212 if (_eh.e_type != ET_EXEC) { 213 DPRINTF((TEXT("not a executable file. type = %d\n"), 214 _eh.e_type)); 215 return FALSE; 216 } 217 218 if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 || 219 _eh.e_phentsize != sizeof(Elf_Phdr)) { 220 DPRINTF((TEXT("invalid program header infomation.\n"))); 221 return FALSE; 222 } 223 224 return TRUE; 225 } 226