load_elf.cpp revision 1.2 1 /* $NetBSD: load_elf.cpp,v 1.2 2001/03/21 14:06:25 toshii 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 size_t shstrsize;
111 char buf[1024];
112 int i;
113
114 _load_segment_start();
115
116 for (i = 0, ph = _ph; i < _eh.e_phnum; i++, ph++) {
117 if (ph->p_type == PT_LOAD) {
118 size_t filesz = ph->p_filesz;
119 size_t memsz = ph->p_memsz;
120 kv = ph->p_vaddr;
121 off_t fileofs = ph->p_offset;
122 DPRINTF((TEXT("[%d] vaddr 0x%08x file size 0x%x mem size 0x%x\n"),
123 i, kv, filesz, memsz));
124 _load_segment(kv, memsz, fileofs, filesz);
125 kv += memsz;
126 }
127 }
128
129 /*
130 * Prepare ELF headers for symbol table.
131 *
132 * ELF header
133 * section header
134 * shstrtab
135 * strtab
136 * symtab
137 */
138 memcpy(buf, &_eh, sizeof(_eh));
139 ((Elf_Ehdr *)buf)->e_phoff = 0;
140 ((Elf_Ehdr *)buf)->e_phnum = 0;
141 ((Elf_Ehdr *)buf)->e_entry = 0;
142 ((Elf_Ehdr *)buf)->e_shoff = sizeof(_eh);
143 off = ((Elf_Ehdr *)buf)->e_shoff;
144 memcpy(buf + off, _sh, _eh.e_shentsize * _eh.e_shnum);
145 sh = (Elf_Shdr *)(buf + off);
146 off += _eh.e_shentsize * _eh.e_shnum;
147
148 /* load shstrtab and find desired sections */
149 shstrsize = (sh[_eh.e_shstrndx].sh_size + 3) & ~0x3;
150 _file->read(buf + off, shstrsize, sh[_eh.e_shstrndx].sh_offset);
151 for(i = 0; i < _eh.e_shnum; i++, sh++) {
152 if (strcmp(".strtab", buf + off + sh->sh_name) == 0) {
153 stroff = sh->sh_offset;
154 shstr = sh;
155 } else if (strcmp(".symtab", buf + off + sh->sh_name) == 0) {
156 symoff = sh->sh_offset;
157 shsym = sh;
158 }
159 if (i == _eh.e_shstrndx)
160 sh->sh_offset = off;
161 else
162 sh->sh_offset = 0;
163 }
164 /* silently return if strtab or symtab can't be found */
165 if (! stroff || ! symoff)
166 return TRUE;
167
168 shstr->sh_offset = off + shstrsize;
169 shsym->sh_offset = off + shstrsize + ((shstr->sh_size + 3) & ~0x3);
170 _load_memory(kv, off + shstrsize, buf);
171 kv += off + shstrsize;
172 _load_segment(kv, shstr->sh_size, stroff, shstr->sh_size);
173 kv += (shstr->sh_size + 3) & ~0x3;
174 _load_segment(kv, shsym->sh_size, symoff, shsym->sh_size);
175
176 /* tag chain still opening */
177
178 return TRUE;
179 }
180
181 BOOL
182 ElfLoader::read_header(void)
183 {
184 // read ELF header
185 _file->read(&_eh, sizeof(Elf_Ehdr), 0);
186
187 // check ELF Magic.
188 if (!is_elf_file()) {
189 DPRINTF((TEXT("not a ELF file.\n")));
190 return FALSE;
191 }
192
193 // Windows CE is 32bit little-endian only.
194 if (_eh.e_ident[EI_DATA] != ELFDATA2LSB ||
195 _eh.e_ident[EI_CLASS] != ELFCLASS32) {
196 DPRINTF((TEXT("invalid class/data(%d/%d)\n"),
197 _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA]));
198 return FALSE;
199 }
200
201 // Is native architecture?
202 switch(_eh.e_machine) {
203 ELF32_MACHDEP_ID_CASES;
204 default:
205 DPRINTF((TEXT("not a native architecture. machine = %d\n"),
206 _eh.e_machine));
207 return FALSE;
208 }
209
210 /* Check object type */
211 if (_eh.e_type != ET_EXEC) {
212 DPRINTF((TEXT("not a executable file. type = %d\n"),
213 _eh.e_type));
214 return FALSE;
215 }
216
217 if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 ||
218 _eh.e_phentsize != sizeof(Elf_Phdr)) {
219 DPRINTF((TEXT("invalid program header infomation.\n")));
220 return FALSE;
221 }
222
223 return TRUE;
224 }
225