load_elf.cpp revision 1.5.2.2 1 /* $NetBSD: load_elf.cpp,v 1.5.2.2 2002/03/16 15:57:51 jdolecek 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("couldn't determine symbol block size"),
173 TEXT("WARNING"), 0);
174 return (0);
175 }
176
177 // set pointer for symbol block
178 Elf_Ehdr *eh = reinterpret_cast<Elf_Ehdr *>(_sym_blk.header);
179 Elf_Shdr *sh = reinterpret_cast<Elf_Shdr *>
180 (_sym_blk.header + sizeof(Elf_Ehdr));
181 char *shstrtab = _sym_blk.header + shstrtab_offset;
182
183 // initialize headers
184 memset(_sym_blk.header, 0, _sym_blk.header_size);
185 memcpy(eh, &_eh, sizeof(Elf_Ehdr));
186 eh->e_phoff = 0;
187 eh->e_phnum = 0;
188 eh->e_entry = 0; // XXX NetBSD kernel check this member. see machdep.c
189 eh->e_shoff = sizeof(Elf_Ehdr);
190 memcpy(sh, _sh, shtab_sz);
191
192 // inquire symbol/string table information
193 _file->read(shstrtab, shstrsize, _sh[_eh.e_shstrndx].sh_offset);
194 for (i = 0; i < _eh.e_shnum; i++, sh++) {
195 if (strcmp(".strtab", shstrtab + sh->sh_name) == 0) {
196 _sym_blk.shstr = sh;
197 _sym_blk.stroff = sh->sh_offset;
198 } else if (strcmp(".symtab", shstrtab + sh->sh_name) == 0) {
199 _sym_blk.shsym = sh;
200 _sym_blk.symoff = sh->sh_offset;
201 }
202
203 sh->sh_offset = (i == _eh.e_shstrndx) ? shstrtab_offset : 0;
204 }
205
206 if (_sym_blk.shstr == NULL || _sym_blk.shsym == NULL) {
207 if (HPC_PREFERENCE.safety_message)
208 MessageBox(HPC_MENU._root->_window,
209 TEXT("no symbol and/or string table in binary. (not fatal)"),
210 TEXT("Information"), 0);
211 free(_sym_blk.header);
212 _sym_blk.header = NULL;
213
214 return (0);
215 }
216
217 // set Section Headers for symbol/string table
218 _sym_blk.shstr->sh_offset = shstrtab_offset + shstrsize;
219 _sym_blk.shsym->sh_offset = shstrtab_offset + shstrsize +
220 ROUND4(_sym_blk.shstr->sh_size);
221 _sym_blk.enable = TRUE;
222
223 DPRINTF((TEXT("+[(symbol block: header %d string %d symbol %d byte)"),
224 _sym_blk.header_size,_sym_blk.shstr->sh_size,
225 _sym_blk.shsym->sh_size));
226
227 // return total amount of symbol block
228 return (_sym_blk.header_size + ROUND4(_sym_blk.shstr->sh_size) +
229 _sym_blk.shsym->sh_size);
230 }
231
232 void
233 ElfLoader::load_symbol_block(vaddr_t kv)
234 {
235 size_t sz;
236
237 if (!_sym_blk.enable)
238 return;
239
240 // load header
241 _load_memory(kv, _sym_blk.header_size, _sym_blk.header);
242 kv += _sym_blk.header_size;
243
244 // load string table
245 sz = _sym_blk.shstr->sh_size;
246 _load_segment(kv, sz, _sym_blk.stroff, sz);
247 kv += ROUND4(sz);
248
249 // load symbol table
250 sz = _sym_blk.shsym->sh_size;
251 _load_segment(kv, sz, _sym_blk.symoff, sz);
252 }
253
254 BOOL
255 ElfLoader::read_header()
256 {
257 // read ELF header
258 _file->read(&_eh, sizeof(Elf_Ehdr), 0);
259
260 // check ELF Magic.
261 if (!is_elf_file()) {
262 DPRINTF((TEXT("not a ELF file.\n")));
263 return FALSE;
264 }
265
266 // Windows CE is 32bit little-endian only.
267 if (_eh.e_ident[EI_DATA] != ELFDATA2LSB ||
268 _eh.e_ident[EI_CLASS] != ELFCLASS32) {
269 DPRINTF((TEXT("invalid class/data(%d/%d)\n"),
270 _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA]));
271 return FALSE;
272 }
273
274 // Is native architecture?
275 switch(_eh.e_machine) {
276 ELF32_MACHDEP_ID_CASES;
277 default:
278 DPRINTF((TEXT("not a native architecture. machine = %d\n"),
279 _eh.e_machine));
280 return FALSE;
281 }
282
283 // Check object type
284 if (_eh.e_type != ET_EXEC) {
285 DPRINTF((TEXT("not a executable file. type = %d\n"),
286 _eh.e_type));
287 return FALSE;
288 }
289
290 if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 ||
291 _eh.e_phentsize != sizeof(Elf_Phdr)) {
292 DPRINTF((TEXT("invalid program header information.\n")));
293 return FALSE;
294 }
295
296 return TRUE;
297 }
298