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