1*dd332648Srafal /* $NetBSD: load_elf.cpp,v 1.19 2008/11/07 16:28:26 rafal Exp $ */
29173eae7Such
39173eae7Such /*-
49173eae7Such * Copyright (c) 2001 The NetBSD Foundation, Inc.
59173eae7Such * All rights reserved.
69173eae7Such *
79173eae7Such * This code is derived from software contributed to The NetBSD Foundation
89173eae7Such * by UCHIYAMA Yasushi.
99173eae7Such *
109173eae7Such * Redistribution and use in source and binary forms, with or without
119173eae7Such * modification, are permitted provided that the following conditions
129173eae7Such * are met:
139173eae7Such * 1. Redistributions of source code must retain the above copyright
149173eae7Such * notice, this list of conditions and the following disclaimer.
159173eae7Such * 2. Redistributions in binary form must reproduce the above copyright
169173eae7Such * notice, this list of conditions and the following disclaimer in the
179173eae7Such * documentation and/or other materials provided with the distribution.
189173eae7Such *
199173eae7Such * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209173eae7Such * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219173eae7Such * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229173eae7Such * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239173eae7Such * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249173eae7Such * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259173eae7Such * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269173eae7Such * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279173eae7Such * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289173eae7Such * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299173eae7Such * POSSIBILITY OF SUCH DAMAGE.
309173eae7Such */
319173eae7Such
32d9d3df29Such #include <hpcmenu.h>
33d9d3df29Such #include <menu/window.h>
34d9d3df29Such #include <menu/rootwindow.h>
35d9d3df29Such
369173eae7Such #include <load.h>
379173eae7Such #include <load_elf.h>
389173eae7Such #include <console.h>
399173eae7Such #include <memory.h>
409173eae7Such #include <file.h>
419173eae7Such
42d9d3df29Such #define ROUND4(x) (((x) + 3) & ~3)
43d9d3df29Such
ElfLoader(Console * & cons,MemoryManager * & mem)449173eae7Such ElfLoader::ElfLoader(Console *&cons, MemoryManager *&mem)
459173eae7Such : Loader(cons, mem)
469173eae7Such {
474d7bd279Such
48d9d3df29Such _sym_blk.enable = FALSE;
494d7bd279Such _ph = NULL;
504d7bd279Such _sh = NULL;
51d9d3df29Such
529173eae7Such DPRINTF((TEXT("Loader: ELF\n")));
539173eae7Such }
549173eae7Such
~ElfLoader(void)559173eae7Such ElfLoader::~ElfLoader(void)
569173eae7Such {
574d7bd279Such
58d9d3df29Such if (_sym_blk.header != NULL)
59d9d3df29Such free(_sym_blk.header);
604d7bd279Such if (_ph != NULL)
614d7bd279Such free(_ph);
624d7bd279Such if (_sh != NULL)
634d7bd279Such free(_sh);
649173eae7Such }
659173eae7Such
669173eae7Such BOOL
setFile(File * & file)679173eae7Such ElfLoader::setFile(File *&file)
689173eae7Such {
69358a8ee7Stoshii size_t sz;
709173eae7Such Loader::setFile(file);
719173eae7Such
72d9d3df29Such // read ELF header and check it
739173eae7Such if (!read_header())
749173eae7Such return FALSE;
754d7bd279Such
76d9d3df29Such // read section header
77358a8ee7Stoshii sz = _eh.e_shnum * _eh.e_shentsize;
784d7bd279Such if ((_sh = static_cast<Elf_Shdr *>(malloc(sz))) == NULL) {
794d7bd279Such DPRINTF((TEXT("can't allocate section header table.\n")));
804d7bd279Such return FALSE;
814d7bd279Such }
824d7bd279Such if (_file->read(_sh, sz, _eh.e_shoff) != sz) {
834d7bd279Such DPRINTF((TEXT("section header read error.\n")));
844d7bd279Such return FALSE;
854d7bd279Such }
86358a8ee7Stoshii
87d9d3df29Such // read program header
88358a8ee7Stoshii sz = _eh.e_phnum * _eh.e_phentsize;
894d7bd279Such if ((_ph = static_cast<Elf_Phdr *>(malloc(sz))) == NULL) {
904d7bd279Such DPRINTF((TEXT("can't allocate program header table.\n")));
914d7bd279Such return FALSE;
924d7bd279Such }
934d7bd279Such if (_file->read(_ph, sz, _eh.e_phoff) != sz) {
944d7bd279Such DPRINTF((TEXT("program header read error.\n")));
954d7bd279Such return FALSE;
964d7bd279Such }
979173eae7Such
984d7bd279Such return TRUE;
999173eae7Such }
1009173eae7Such
1019173eae7Such size_t
memorySize()1029173eae7Such ElfLoader::memorySize()
1039173eae7Such {
1049173eae7Such int i;
1059173eae7Such Elf_Phdr *ph = _ph;
1069173eae7Such size_t sz = 0;
107de58f024Suwe size_t extra = 0;
1089173eae7Such
1099173eae7Such DPRINTF((TEXT("file size: ")));
1109173eae7Such for (i = 0; i < _eh.e_phnum; i++, ph++) {
1119173eae7Such if (ph->p_type == PT_LOAD) {
1129173eae7Such size_t filesz = ph->p_filesz;
1137a546fd8Suwe DPRINTF((TEXT("%s0x%x"),
1147a546fd8Suwe sz == 0 ? "" : "+",
1157a546fd8Suwe filesz));
1169173eae7Such sz += _mem->roundPage(filesz);
117de58f024Suwe // compensate for partial last tag
118de58f024Suwe extra += _mem->getTaggedPageSize();
119de58f024Suwe if (filesz < ph->p_memsz)
120de58f024Suwe // compensate for zero clear
121de58f024Suwe extra += _mem->getTaggedPageSize();
122de58f024Suwe
1239173eae7Such }
1249173eae7Such }
125d9d3df29Such
126d9d3df29Such // reserve for symbols
127d9d3df29Such size_t symblk_sz = symbol_block_size();
128d9d3df29Such if (symblk_sz) {
129d9d3df29Such sz += symblk_sz;
130d9d3df29Such DPRINTF((TEXT(" = 0x%x]"), symblk_sz));
131de58f024Suwe // XXX: compensate for partial tags after ELF header and symtab
132de58f024Suwe extra += 2 * _mem->getTaggedPageSize();
133d9d3df29Such }
134358a8ee7Stoshii
135de58f024Suwe sz += extra;
136de58f024Suwe DPRINTF((TEXT("+[extra: 0x%x]"), extra));
137de58f024Suwe
138dfe0035aSuwe DPRINTF((TEXT(" = 0x%x bytes\n"), sz));
1399173eae7Such return sz;
1409173eae7Such }
1419173eae7Such
1429173eae7Such kaddr_t
jumpAddr()1439173eae7Such ElfLoader::jumpAddr()
1449173eae7Such {
1459173eae7Such DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _eh.e_entry));
1469173eae7Such return _eh.e_entry;
1479173eae7Such }
1489173eae7Such
1499173eae7Such BOOL
load()1509173eae7Such ElfLoader::load()
1519173eae7Such {
1529173eae7Such Elf_Phdr *ph;
153358a8ee7Stoshii vaddr_t kv;
1549173eae7Such int i;
1559173eae7Such
1569173eae7Such _load_segment_start();
1579173eae7Such
1589173eae7Such for (i = 0, ph = _ph; i < _eh.e_phnum; i++, ph++) {
1599173eae7Such if (ph->p_type == PT_LOAD) {
1609173eae7Such size_t filesz = ph->p_filesz;
1619173eae7Such size_t memsz = ph->p_memsz;
162*dd332648Srafal kv = ph->p_paddr;
1639173eae7Such off_t fileofs = ph->p_offset;
164*dd332648Srafal DPRINTF((TEXT("seg[%d] paddr 0x%08x file size 0x%x mem size 0x%x\n"),
1659173eae7Such i, kv, filesz, memsz));
1669173eae7Such _load_segment(kv, memsz, fileofs, filesz);
167fe28a593Such kv += ROUND4(memsz);
1689173eae7Such }
1699173eae7Such }
170358a8ee7Stoshii
171d9d3df29Such load_symbol_block(kv);
172358a8ee7Stoshii
173d9d3df29Such // tag chain still opening
174d9d3df29Such
175acb09f98Such return _load_success();
176d9d3df29Such }
177d9d3df29Such
178d9d3df29Such //
179d9d3df29Such // Prepare ELF headers for symbol table.
180d9d3df29Such //
181d9d3df29Such // ELF header
182d9d3df29Such // section header
183d9d3df29Such // shstrtab
184d9d3df29Such // symtab
185c87372b7Suwe // strtab
186d9d3df29Such //
187d9d3df29Such size_t
symbol_block_size()188d9d3df29Such ElfLoader::symbol_block_size()
189d9d3df29Such {
190d9d3df29Such size_t shstrsize = ROUND4(_sh[_eh.e_shstrndx].sh_size);
191d9d3df29Such size_t shtab_sz = _eh.e_shentsize * _eh.e_shnum;
192d9d3df29Such off_t shstrtab_offset = sizeof(Elf_Ehdr) + shtab_sz;
193d9d3df29Such int i;
194d9d3df29Such
195d9d3df29Such memset(&_sym_blk, 0, sizeof(_sym_blk));
196d9d3df29Such _sym_blk.enable = FALSE;
197d9d3df29Such _sym_blk.header_size = sizeof(Elf_Ehdr) + shtab_sz + shstrsize;
198d9d3df29Such
199d9d3df29Such // inquire string and symbol table size
200d9d3df29Such _sym_blk.header = static_cast<char *>(malloc(_sym_blk.header_size));
201d9d3df29Such if (_sym_blk.header == NULL) {
202d9d3df29Such MessageBox(HPC_MENU._root->_window,
203545bd4c3Suwe TEXT("Can't determine symbol block size."),
204545bd4c3Suwe TEXT("WARNING"),
205545bd4c3Suwe MB_ICONWARNING | MB_OK);
2068c941b84Suwe UpdateWindow(HPC_MENU._root->_window);
207d9d3df29Such return (0);
208d9d3df29Such }
209d9d3df29Such
210d9d3df29Such // set pointer for symbol block
211d9d3df29Such Elf_Ehdr *eh = reinterpret_cast<Elf_Ehdr *>(_sym_blk.header);
212d9d3df29Such Elf_Shdr *sh = reinterpret_cast<Elf_Shdr *>
213d9d3df29Such (_sym_blk.header + sizeof(Elf_Ehdr));
214d9d3df29Such char *shstrtab = _sym_blk.header + shstrtab_offset;
215d9d3df29Such
216d9d3df29Such // initialize headers
217d9d3df29Such memset(_sym_blk.header, 0, _sym_blk.header_size);
218d9d3df29Such memcpy(eh, &_eh, sizeof(Elf_Ehdr));
219d9d3df29Such eh->e_phoff = 0;
220d9d3df29Such eh->e_phnum = 0;
221d9d3df29Such eh->e_entry = 0; // XXX NetBSD kernel check this member. see machdep.c
222d9d3df29Such eh->e_shoff = sizeof(Elf_Ehdr);
223d9d3df29Such memcpy(sh, _sh, shtab_sz);
224d9d3df29Such
225d9d3df29Such // inquire symbol/string table information
226d9d3df29Such _file->read(shstrtab, shstrsize, _sh[_eh.e_shstrndx].sh_offset);
227358a8ee7Stoshii for (i = 0; i < _eh.e_shnum; i++, sh++) {
228d9d3df29Such if (strcmp(".strtab", shstrtab + sh->sh_name) == 0) {
229d9d3df29Such _sym_blk.shstr = sh;
230d9d3df29Such _sym_blk.stroff = sh->sh_offset;
231d9d3df29Such } else if (strcmp(".symtab", shstrtab + sh->sh_name) == 0) {
232d9d3df29Such _sym_blk.shsym = sh;
233d9d3df29Such _sym_blk.symoff = sh->sh_offset;
234358a8ee7Stoshii }
235d9d3df29Such
236d9d3df29Such sh->sh_offset = (i == _eh.e_shstrndx) ? shstrtab_offset : 0;
237358a8ee7Stoshii }
238358a8ee7Stoshii
239d9d3df29Such if (_sym_blk.shstr == NULL || _sym_blk.shsym == NULL) {
2408c941b84Suwe if (HPC_PREFERENCE.safety_message) {
241d9d3df29Such MessageBox(HPC_MENU._root->_window,
242545bd4c3Suwe TEXT("No symbol and/or string table in binary.\n(not fatal)"),
243545bd4c3Suwe TEXT("Information"),
244545bd4c3Suwe MB_ICONINFORMATION | MB_OK);
2458c941b84Suwe UpdateWindow(HPC_MENU._root->_window);
2468c941b84Suwe }
247d9d3df29Such free(_sym_blk.header);
248d9d3df29Such _sym_blk.header = NULL;
249358a8ee7Stoshii
250d9d3df29Such return (0);
251d9d3df29Such }
2529173eae7Such
253d9d3df29Such // set Section Headers for symbol/string table
254c87372b7Suwe _sym_blk.shsym->sh_offset = shstrtab_offset + shstrsize;
255c87372b7Suwe _sym_blk.shstr->sh_offset = shstrtab_offset + shstrsize +
256c87372b7Suwe ROUND4(_sym_blk.shsym->sh_size);
257d9d3df29Such _sym_blk.enable = TRUE;
258d9d3df29Such
259dfe0035aSuwe DPRINTF((TEXT("+[ksyms: header 0x%x, symtab 0x%x, strtab 0x%x"),
260c87372b7Suwe _sym_blk.header_size, _sym_blk.shsym->sh_size,
261c87372b7Suwe _sym_blk.shstr->sh_size));
262d9d3df29Such
263d9d3df29Such // return total amount of symbol block
264c87372b7Suwe return (_sym_blk.header_size + ROUND4(_sym_blk.shsym->sh_size) +
265c87372b7Suwe _sym_blk.shstr->sh_size);
266d9d3df29Such }
267d9d3df29Such
268d9d3df29Such void
load_symbol_block(vaddr_t kv)269d9d3df29Such ElfLoader::load_symbol_block(vaddr_t kv)
270d9d3df29Such {
271d9d3df29Such size_t sz;
272d9d3df29Such
273d9d3df29Such if (!_sym_blk.enable)
274d9d3df29Such return;
275d9d3df29Such
276dfe0035aSuwe DPRINTF((TEXT("ksyms\n")));
277dfe0035aSuwe
278d9d3df29Such // load header
279d9d3df29Such _load_memory(kv, _sym_blk.header_size, _sym_blk.header);
280d9d3df29Such kv += _sym_blk.header_size;
281d9d3df29Such
282d9d3df29Such // load symbol table
283d9d3df29Such sz = _sym_blk.shsym->sh_size;
284d9d3df29Such _load_segment(kv, sz, _sym_blk.symoff, sz);
285c87372b7Suwe kv += ROUND4(sz);
286c87372b7Suwe
287c87372b7Suwe // load string table
288c87372b7Suwe sz = _sym_blk.shstr->sh_size;
289c87372b7Suwe _load_segment(kv, sz, _sym_blk.stroff, sz);
2909173eae7Such }
2919173eae7Such
2929173eae7Such BOOL
read_header()293d9d3df29Such ElfLoader::read_header()
2949173eae7Such {
2959173eae7Such // read ELF header
2969173eae7Such _file->read(&_eh, sizeof(Elf_Ehdr), 0);
2979173eae7Such
2989173eae7Such // check ELF Magic.
2999173eae7Such if (!is_elf_file()) {
3009173eae7Such DPRINTF((TEXT("not a ELF file.\n")));
3019173eae7Such return FALSE;
3029173eae7Such }
3039173eae7Such
3049173eae7Such // Windows CE is 32bit little-endian only.
3059173eae7Such if (_eh.e_ident[EI_DATA] != ELFDATA2LSB ||
3069173eae7Such _eh.e_ident[EI_CLASS] != ELFCLASS32) {
3079173eae7Such DPRINTF((TEXT("invalid class/data(%d/%d)\n"),
3089173eae7Such _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA]));
3099173eae7Such return FALSE;
3109173eae7Such }
3119173eae7Such
3129173eae7Such // Is native architecture?
3139173eae7Such switch(_eh.e_machine) {
3149173eae7Such ELF32_MACHDEP_ID_CASES;
3159173eae7Such default:
3169173eae7Such DPRINTF((TEXT("not a native architecture. machine = %d\n"),
3179173eae7Such _eh.e_machine));
3189173eae7Such return FALSE;
3199173eae7Such }
3209173eae7Such
321d9d3df29Such // Check object type
3229173eae7Such if (_eh.e_type != ET_EXEC) {
3239173eae7Such DPRINTF((TEXT("not a executable file. type = %d\n"),
3249173eae7Such _eh.e_type));
3259173eae7Such return FALSE;
3269173eae7Such }
3279173eae7Such
3289173eae7Such if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 ||
3299173eae7Such _eh.e_phentsize != sizeof(Elf_Phdr)) {
3301bc6d2ceSwiz DPRINTF((TEXT("invalid program header information.\n")));
3319173eae7Such return FALSE;
3329173eae7Such }
3339173eae7Such
3349173eae7Such return TRUE;
3359173eae7Such }
336