load_elf.cpp revision 1.1.2.3 1 1.1.2.3 bouyer /* $NetBSD: load_elf.cpp,v 1.1.2.3 2001/03/27 15:30:48 bouyer Exp $ */
2 1.1.2.2 bouyer
3 1.1.2.2 bouyer /*-
4 1.1.2.2 bouyer * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1.2.2 bouyer * All rights reserved.
6 1.1.2.2 bouyer *
7 1.1.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 bouyer * by UCHIYAMA Yasushi.
9 1.1.2.2 bouyer *
10 1.1.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.1.2.2 bouyer * are met:
13 1.1.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.1.2.2 bouyer * must display the following acknowledgement:
20 1.1.2.2 bouyer * This product includes software developed by the NetBSD
21 1.1.2.2 bouyer * Foundation, Inc. and its contributors.
22 1.1.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.2 bouyer * contributors may be used to endorse or promote products derived
24 1.1.2.2 bouyer * from this software without specific prior written permission.
25 1.1.2.2 bouyer *
26 1.1.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.2 bouyer */
38 1.1.2.2 bouyer
39 1.1.2.2 bouyer #include <load.h>
40 1.1.2.2 bouyer #include <load_elf.h>
41 1.1.2.2 bouyer #include <console.h>
42 1.1.2.2 bouyer #include <memory.h>
43 1.1.2.2 bouyer #include <file.h>
44 1.1.2.2 bouyer
45 1.1.2.2 bouyer ElfLoader::ElfLoader(Console *&cons, MemoryManager *&mem)
46 1.1.2.2 bouyer : Loader(cons, mem)
47 1.1.2.2 bouyer {
48 1.1.2.2 bouyer DPRINTF((TEXT("Loader: ELF\n")));
49 1.1.2.2 bouyer }
50 1.1.2.2 bouyer
51 1.1.2.2 bouyer ElfLoader::~ElfLoader(void)
52 1.1.2.2 bouyer {
53 1.1.2.2 bouyer }
54 1.1.2.2 bouyer
55 1.1.2.2 bouyer BOOL
56 1.1.2.2 bouyer ElfLoader::setFile(File *&file)
57 1.1.2.2 bouyer {
58 1.1.2.3 bouyer size_t sz;
59 1.1.2.2 bouyer Loader::setFile(file);
60 1.1.2.2 bouyer
61 1.1.2.2 bouyer /* read ELF header and check it */
62 1.1.2.2 bouyer if (!read_header())
63 1.1.2.2 bouyer return FALSE;
64 1.1.2.3 bouyer /* read section header */
65 1.1.2.3 bouyer sz = _eh.e_shnum * _eh.e_shentsize;
66 1.1.2.3 bouyer _file->read(_sh, _eh.e_shentsize * _eh.e_shnum, _eh.e_shoff);
67 1.1.2.3 bouyer
68 1.1.2.2 bouyer /* read program header */
69 1.1.2.3 bouyer sz = _eh.e_phnum * _eh.e_phentsize;
70 1.1.2.2 bouyer
71 1.1.2.2 bouyer return _file->read(_ph, sz, _eh.e_phoff) == sz;
72 1.1.2.2 bouyer }
73 1.1.2.2 bouyer
74 1.1.2.2 bouyer size_t
75 1.1.2.2 bouyer ElfLoader::memorySize()
76 1.1.2.2 bouyer {
77 1.1.2.2 bouyer int i;
78 1.1.2.2 bouyer Elf_Phdr *ph = _ph;
79 1.1.2.2 bouyer size_t sz = 0;
80 1.1.2.2 bouyer
81 1.1.2.2 bouyer DPRINTF((TEXT("file size: ")));
82 1.1.2.2 bouyer for (i = 0; i < _eh.e_phnum; i++, ph++) {
83 1.1.2.2 bouyer if (ph->p_type == PT_LOAD) {
84 1.1.2.2 bouyer size_t filesz = ph->p_filesz;
85 1.1.2.2 bouyer DPRINTF((TEXT("+0x%x"), filesz));
86 1.1.2.2 bouyer sz += _mem->roundPage(filesz);
87 1.1.2.2 bouyer }
88 1.1.2.2 bouyer }
89 1.1.2.3 bouyer /* XXX reserve 192kB for symbols */
90 1.1.2.3 bouyer sz += 0x30000;
91 1.1.2.3 bouyer
92 1.1.2.2 bouyer DPRINTF((TEXT(" = 0x%x byte\n"), sz));
93 1.1.2.2 bouyer return sz;
94 1.1.2.2 bouyer }
95 1.1.2.2 bouyer
96 1.1.2.2 bouyer kaddr_t
97 1.1.2.2 bouyer ElfLoader::jumpAddr()
98 1.1.2.2 bouyer {
99 1.1.2.2 bouyer DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _eh.e_entry));
100 1.1.2.2 bouyer return _eh.e_entry;
101 1.1.2.2 bouyer }
102 1.1.2.2 bouyer
103 1.1.2.2 bouyer BOOL
104 1.1.2.2 bouyer ElfLoader::load()
105 1.1.2.2 bouyer {
106 1.1.2.2 bouyer Elf_Phdr *ph;
107 1.1.2.3 bouyer Elf_Shdr *sh, *shstr, *shsym;
108 1.1.2.3 bouyer off_t stroff = 0, symoff = 0, off;
109 1.1.2.3 bouyer vaddr_t kv;
110 1.1.2.3 bouyer size_t shstrsize;
111 1.1.2.3 bouyer char buf[1024];
112 1.1.2.2 bouyer int i;
113 1.1.2.2 bouyer
114 1.1.2.2 bouyer _load_segment_start();
115 1.1.2.2 bouyer
116 1.1.2.2 bouyer for (i = 0, ph = _ph; i < _eh.e_phnum; i++, ph++) {
117 1.1.2.2 bouyer if (ph->p_type == PT_LOAD) {
118 1.1.2.2 bouyer size_t filesz = ph->p_filesz;
119 1.1.2.2 bouyer size_t memsz = ph->p_memsz;
120 1.1.2.3 bouyer kv = ph->p_vaddr;
121 1.1.2.2 bouyer off_t fileofs = ph->p_offset;
122 1.1.2.2 bouyer DPRINTF((TEXT("[%d] vaddr 0x%08x file size 0x%x mem size 0x%x\n"),
123 1.1.2.2 bouyer i, kv, filesz, memsz));
124 1.1.2.2 bouyer _load_segment(kv, memsz, fileofs, filesz);
125 1.1.2.3 bouyer kv += memsz;
126 1.1.2.2 bouyer }
127 1.1.2.2 bouyer }
128 1.1.2.3 bouyer
129 1.1.2.3 bouyer /*
130 1.1.2.3 bouyer * Prepare ELF headers for symbol table.
131 1.1.2.3 bouyer *
132 1.1.2.3 bouyer * ELF header
133 1.1.2.3 bouyer * section header
134 1.1.2.3 bouyer * shstrtab
135 1.1.2.3 bouyer * strtab
136 1.1.2.3 bouyer * symtab
137 1.1.2.3 bouyer */
138 1.1.2.3 bouyer memcpy(buf, &_eh, sizeof(_eh));
139 1.1.2.3 bouyer ((Elf_Ehdr *)buf)->e_phoff = 0;
140 1.1.2.3 bouyer ((Elf_Ehdr *)buf)->e_phnum = 0;
141 1.1.2.3 bouyer ((Elf_Ehdr *)buf)->e_entry = 0;
142 1.1.2.3 bouyer ((Elf_Ehdr *)buf)->e_shoff = sizeof(_eh);
143 1.1.2.3 bouyer off = ((Elf_Ehdr *)buf)->e_shoff;
144 1.1.2.3 bouyer memcpy(buf + off, _sh, _eh.e_shentsize * _eh.e_shnum);
145 1.1.2.3 bouyer sh = (Elf_Shdr *)(buf + off);
146 1.1.2.3 bouyer off += _eh.e_shentsize * _eh.e_shnum;
147 1.1.2.3 bouyer
148 1.1.2.3 bouyer /* load shstrtab and find desired sections */
149 1.1.2.3 bouyer shstrsize = (sh[_eh.e_shstrndx].sh_size + 3) & ~0x3;
150 1.1.2.3 bouyer _file->read(buf + off, shstrsize, sh[_eh.e_shstrndx].sh_offset);
151 1.1.2.3 bouyer for(i = 0; i < _eh.e_shnum; i++, sh++) {
152 1.1.2.3 bouyer if (strcmp(".strtab", buf + off + sh->sh_name) == 0) {
153 1.1.2.3 bouyer stroff = sh->sh_offset;
154 1.1.2.3 bouyer shstr = sh;
155 1.1.2.3 bouyer } else if (strcmp(".symtab", buf + off + sh->sh_name) == 0) {
156 1.1.2.3 bouyer symoff = sh->sh_offset;
157 1.1.2.3 bouyer shsym = sh;
158 1.1.2.3 bouyer }
159 1.1.2.3 bouyer if (i == _eh.e_shstrndx)
160 1.1.2.3 bouyer sh->sh_offset = off;
161 1.1.2.3 bouyer else
162 1.1.2.3 bouyer sh->sh_offset = 0;
163 1.1.2.3 bouyer }
164 1.1.2.3 bouyer /* silently return if strtab or symtab can't be found */
165 1.1.2.3 bouyer if (! stroff || ! symoff)
166 1.1.2.3 bouyer return TRUE;
167 1.1.2.3 bouyer
168 1.1.2.3 bouyer shstr->sh_offset = off + shstrsize;
169 1.1.2.3 bouyer shsym->sh_offset = off + shstrsize + ((shstr->sh_size + 3) & ~0x3);
170 1.1.2.3 bouyer _load_memory(kv, off + shstrsize, buf);
171 1.1.2.3 bouyer kv += off + shstrsize;
172 1.1.2.3 bouyer _load_segment(kv, shstr->sh_size, stroff, shstr->sh_size);
173 1.1.2.3 bouyer kv += (shstr->sh_size + 3) & ~0x3;
174 1.1.2.3 bouyer _load_segment(kv, shsym->sh_size, symoff, shsym->sh_size);
175 1.1.2.3 bouyer
176 1.1.2.2 bouyer /* tag chain still opening */
177 1.1.2.2 bouyer
178 1.1.2.2 bouyer return TRUE;
179 1.1.2.2 bouyer }
180 1.1.2.2 bouyer
181 1.1.2.2 bouyer BOOL
182 1.1.2.2 bouyer ElfLoader::read_header(void)
183 1.1.2.2 bouyer {
184 1.1.2.2 bouyer // read ELF header
185 1.1.2.2 bouyer _file->read(&_eh, sizeof(Elf_Ehdr), 0);
186 1.1.2.2 bouyer
187 1.1.2.2 bouyer // check ELF Magic.
188 1.1.2.2 bouyer if (!is_elf_file()) {
189 1.1.2.2 bouyer DPRINTF((TEXT("not a ELF file.\n")));
190 1.1.2.2 bouyer return FALSE;
191 1.1.2.2 bouyer }
192 1.1.2.2 bouyer
193 1.1.2.2 bouyer // Windows CE is 32bit little-endian only.
194 1.1.2.2 bouyer if (_eh.e_ident[EI_DATA] != ELFDATA2LSB ||
195 1.1.2.2 bouyer _eh.e_ident[EI_CLASS] != ELFCLASS32) {
196 1.1.2.2 bouyer DPRINTF((TEXT("invalid class/data(%d/%d)\n"),
197 1.1.2.2 bouyer _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA]));
198 1.1.2.2 bouyer return FALSE;
199 1.1.2.2 bouyer }
200 1.1.2.2 bouyer
201 1.1.2.2 bouyer // Is native architecture?
202 1.1.2.2 bouyer switch(_eh.e_machine) {
203 1.1.2.2 bouyer ELF32_MACHDEP_ID_CASES;
204 1.1.2.2 bouyer default:
205 1.1.2.2 bouyer DPRINTF((TEXT("not a native architecture. machine = %d\n"),
206 1.1.2.2 bouyer _eh.e_machine));
207 1.1.2.2 bouyer return FALSE;
208 1.1.2.2 bouyer }
209 1.1.2.2 bouyer
210 1.1.2.2 bouyer /* Check object type */
211 1.1.2.2 bouyer if (_eh.e_type != ET_EXEC) {
212 1.1.2.2 bouyer DPRINTF((TEXT("not a executable file. type = %d\n"),
213 1.1.2.2 bouyer _eh.e_type));
214 1.1.2.2 bouyer return FALSE;
215 1.1.2.2 bouyer }
216 1.1.2.2 bouyer
217 1.1.2.2 bouyer if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 ||
218 1.1.2.2 bouyer _eh.e_phentsize != sizeof(Elf_Phdr)) {
219 1.1.2.2 bouyer DPRINTF((TEXT("invalid program header infomation.\n")));
220 1.1.2.2 bouyer return FALSE;
221 1.1.2.2 bouyer }
222 1.1.2.2 bouyer
223 1.1.2.2 bouyer return TRUE;
224 1.1.2.2 bouyer }
225