load_elf.cpp revision 1.1.2.2 1 1.1.2.2 bouyer /* $NetBSD: load_elf.cpp,v 1.1.2.2 2001/02/11 19:09:56 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.2 bouyer Loader::setFile(file);
59 1.1.2.2 bouyer
60 1.1.2.2 bouyer /* read ELF header and check it */
61 1.1.2.2 bouyer if (!read_header())
62 1.1.2.2 bouyer return FALSE;
63 1.1.2.2 bouyer /* read program header */
64 1.1.2.2 bouyer size_t sz = _eh.e_phnum * _eh.e_phentsize;
65 1.1.2.2 bouyer
66 1.1.2.2 bouyer return _file->read(_ph, sz, _eh.e_phoff) == sz;
67 1.1.2.2 bouyer }
68 1.1.2.2 bouyer
69 1.1.2.2 bouyer size_t
70 1.1.2.2 bouyer ElfLoader::memorySize()
71 1.1.2.2 bouyer {
72 1.1.2.2 bouyer int i;
73 1.1.2.2 bouyer Elf_Phdr *ph = _ph;
74 1.1.2.2 bouyer size_t sz = 0;
75 1.1.2.2 bouyer
76 1.1.2.2 bouyer DPRINTF((TEXT("file size: ")));
77 1.1.2.2 bouyer for (i = 0; i < _eh.e_phnum; i++, ph++) {
78 1.1.2.2 bouyer if (ph->p_type == PT_LOAD) {
79 1.1.2.2 bouyer size_t filesz = ph->p_filesz;
80 1.1.2.2 bouyer DPRINTF((TEXT("+0x%x"), filesz));
81 1.1.2.2 bouyer sz += _mem->roundPage(filesz);
82 1.1.2.2 bouyer }
83 1.1.2.2 bouyer }
84 1.1.2.2 bouyer DPRINTF((TEXT(" = 0x%x byte\n"), sz));
85 1.1.2.2 bouyer return sz;
86 1.1.2.2 bouyer }
87 1.1.2.2 bouyer
88 1.1.2.2 bouyer kaddr_t
89 1.1.2.2 bouyer ElfLoader::jumpAddr()
90 1.1.2.2 bouyer {
91 1.1.2.2 bouyer DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _eh.e_entry));
92 1.1.2.2 bouyer return _eh.e_entry;
93 1.1.2.2 bouyer }
94 1.1.2.2 bouyer
95 1.1.2.2 bouyer BOOL
96 1.1.2.2 bouyer ElfLoader::load()
97 1.1.2.2 bouyer {
98 1.1.2.2 bouyer Elf_Phdr *ph;
99 1.1.2.2 bouyer int i;
100 1.1.2.2 bouyer
101 1.1.2.2 bouyer _load_segment_start();
102 1.1.2.2 bouyer
103 1.1.2.2 bouyer for (i = 0, ph = _ph; i < _eh.e_phnum; i++, ph++) {
104 1.1.2.2 bouyer if (ph->p_type == PT_LOAD) {
105 1.1.2.2 bouyer size_t filesz = ph->p_filesz;
106 1.1.2.2 bouyer size_t memsz = ph->p_memsz;
107 1.1.2.2 bouyer vaddr_t kv = ph->p_vaddr;
108 1.1.2.2 bouyer off_t fileofs = ph->p_offset;
109 1.1.2.2 bouyer DPRINTF((TEXT("[%d] vaddr 0x%08x file size 0x%x mem size 0x%x\n"),
110 1.1.2.2 bouyer i, kv, filesz, memsz));
111 1.1.2.2 bouyer _load_segment(kv, memsz, fileofs, filesz);
112 1.1.2.2 bouyer }
113 1.1.2.2 bouyer }
114 1.1.2.2 bouyer /* tag chain still opening */
115 1.1.2.2 bouyer
116 1.1.2.2 bouyer return TRUE;
117 1.1.2.2 bouyer }
118 1.1.2.2 bouyer
119 1.1.2.2 bouyer BOOL
120 1.1.2.2 bouyer ElfLoader::read_header(void)
121 1.1.2.2 bouyer {
122 1.1.2.2 bouyer // read ELF header
123 1.1.2.2 bouyer _file->read(&_eh, sizeof(Elf_Ehdr), 0);
124 1.1.2.2 bouyer
125 1.1.2.2 bouyer // check ELF Magic.
126 1.1.2.2 bouyer if (!is_elf_file()) {
127 1.1.2.2 bouyer DPRINTF((TEXT("not a ELF file.\n")));
128 1.1.2.2 bouyer return FALSE;
129 1.1.2.2 bouyer }
130 1.1.2.2 bouyer
131 1.1.2.2 bouyer // Windows CE is 32bit little-endian only.
132 1.1.2.2 bouyer if (_eh.e_ident[EI_DATA] != ELFDATA2LSB ||
133 1.1.2.2 bouyer _eh.e_ident[EI_CLASS] != ELFCLASS32) {
134 1.1.2.2 bouyer DPRINTF((TEXT("invalid class/data(%d/%d)\n"),
135 1.1.2.2 bouyer _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA]));
136 1.1.2.2 bouyer return FALSE;
137 1.1.2.2 bouyer }
138 1.1.2.2 bouyer
139 1.1.2.2 bouyer // Is native architecture?
140 1.1.2.2 bouyer switch(_eh.e_machine) {
141 1.1.2.2 bouyer ELF32_MACHDEP_ID_CASES;
142 1.1.2.2 bouyer default:
143 1.1.2.2 bouyer DPRINTF((TEXT("not a native architecture. machine = %d\n"),
144 1.1.2.2 bouyer _eh.e_machine));
145 1.1.2.2 bouyer return FALSE;
146 1.1.2.2 bouyer }
147 1.1.2.2 bouyer
148 1.1.2.2 bouyer /* Check object type */
149 1.1.2.2 bouyer if (_eh.e_type != ET_EXEC) {
150 1.1.2.2 bouyer DPRINTF((TEXT("not a executable file. type = %d\n"),
151 1.1.2.2 bouyer _eh.e_type));
152 1.1.2.2 bouyer return FALSE;
153 1.1.2.2 bouyer }
154 1.1.2.2 bouyer
155 1.1.2.2 bouyer if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 ||
156 1.1.2.2 bouyer _eh.e_phentsize != sizeof(Elf_Phdr)) {
157 1.1.2.2 bouyer DPRINTF((TEXT("invalid program header infomation.\n")));
158 1.1.2.2 bouyer return FALSE;
159 1.1.2.2 bouyer }
160 1.1.2.2 bouyer
161 1.1.2.2 bouyer return TRUE;
162 1.1.2.2 bouyer }
163