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