Set LIBELF_LINUX to 0 to build on zax. Builds
[gem5.git] / base / loader / elf_object.cc
1 /*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <string>
30
31 // Because of the -Wundef flag we have to do this
32 #define __LIBELF_INTERNAL__ 0
33 // counterintuitive, but the flag below causes libelf to define
34 // 64-bit elf types that apparently didn't exist in some older
35 // versions of Linux. They seem to be there in 2.4.x, so don't
36 // set this now (it causes things to break on 64-bit platforms).
37 #define __LIBELF64_LINUX 0
38 #define __LIBELF_NEED_LINK_H 0
39
40 #include <libelf/libelf.h>
41 #include <libelf/gelf.h>
42
43 #include "base/loader/elf_object.hh"
44
45 #include "mem/functional_mem/functional_memory.hh"
46 #include "base/loader/symtab.hh"
47
48 #include "base/trace.hh" // for DPRINTF
49
50
51 using namespace std;
52
53 ObjectFile *
54 ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
55 {
56 Elf *elf;
57 GElf_Ehdr ehdr;
58
59 // check that header matches library version
60 assert(elf_version(EV_CURRENT) != EV_NONE);
61
62 // get a pointer to elf structure
63 elf = elf_memory((char*)data,len);
64 // will only fail if fd is invalid
65 assert(elf != NULL);
66
67 // Check that we actually have a elf file
68 if (gelf_getehdr(elf, &ehdr) ==0) {
69 DPRINTFR(Loader, "Not ELF\n");
70 elf_end(elf);
71 return NULL;
72 }
73 else {
74 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
75 panic("32 bit ELF Binary, Not Supported");
76 if (ehdr.e_machine != EM_ALPHA)
77 panic("Non Alpha Binary, Not Supported");
78
79 elf_end(elf);
80
81 return new ElfObject(fname, fd, len, data,
82 ObjectFile::Alpha, ObjectFile::Linux);
83 }
84 }
85
86
87 ElfObject::ElfObject(const string &_filename, int _fd,
88 size_t _len, uint8_t *_data,
89 Arch _arch, OpSys _opSys)
90 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
91
92 {
93 Elf *elf;
94 GElf_Ehdr ehdr;
95
96 // check that header matches library version
97 assert(elf_version(EV_CURRENT) != EV_NONE);
98
99 // get a pointer to elf structure
100 elf = elf_memory((char*)fileData,len);
101 // will only fail if fd is invalid
102 assert(elf != NULL);
103
104 // Check that we actually have a elf file
105 if (gelf_getehdr(elf, &ehdr) ==0) {
106 panic("Not ELF, shouldn't be here");
107 }
108
109 entry = ehdr.e_entry;
110
111 // initialize segment sizes to 0 in case they're not present
112 text.size = data.size = bss.size = 0;
113
114 for (int i = 0; i < ehdr.e_phnum; ++i) {
115 GElf_Phdr phdr;
116 if (gelf_getphdr(elf, i, &phdr) == 0) {
117 panic("gelf_getphdr failed for section %d", i);
118 }
119
120 // for now we don't care about non-loadable segments
121 if (!(phdr.p_type & PT_LOAD))
122 continue;
123
124 // the headers don't explicitly distinguish text from data,
125 // but empirically the text segment comes first.
126 if (text.size == 0) { // haven't seen text segment yet
127 text.baseAddr = phdr.p_vaddr;
128 text.size = phdr.p_filesz;
129 // remember where the data is for loadSections()
130 fileTextBits = fileData + phdr.p_offset;
131 // if there's any padding at the end that's not in the
132 // file, call it the bss. This happens in the "text"
133 // segment if there's only one loadable segment (as for
134 // kernel images).
135 bss.size = phdr.p_memsz - phdr.p_filesz;
136 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
137 }
138 else if (data.size == 0) { // have text, this must be data
139 data.baseAddr = phdr.p_vaddr;
140 data.size = phdr.p_filesz;
141 // remember where the data is for loadSections()
142 fileDataBits = fileData + phdr.p_offset;
143 // if there's any padding at the end that's not in the
144 // file, call it the bss. Warn if this happens for both
145 // the text & data segments (should only have one bss).
146 if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) {
147 warn("Two implied bss segments in file!\n");
148 }
149 bss.size = phdr.p_memsz - phdr.p_filesz;
150 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
151 }
152 }
153
154 // should have found at least one loadable segment
155 assert(text.size != 0);
156
157 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
158 text.baseAddr, text.size, data.baseAddr, data.size,
159 bss.baseAddr, bss.size);
160
161 elf_end(elf);
162
163 // We will actually read the sections when we need to load them
164 }
165
166
167 bool
168 ElfObject::loadSections(FunctionalMemory *mem, bool loadPhys)
169 {
170 Addr textAddr = text.baseAddr;
171 Addr dataAddr = data.baseAddr;
172
173 if (loadPhys) {
174 textAddr &= (ULL(1) << 40) - 1;
175 dataAddr &= (ULL(1) << 40) - 1;
176 }
177
178 // Since we don't really have an MMU and all memory is
179 // zero-filled, there's no need to set up the BSS segment.
180 if (text.size != 0)
181 mem->prot_write(textAddr, fileTextBits, text.size);
182 if (data.size != 0)
183 mem->prot_write(dataAddr, fileDataBits, data.size);
184
185 return true;
186 }
187
188
189 bool
190 ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding)
191 {
192 Elf *elf;
193 int secidx = 1; // there is a 0 but it is nothing, go figure
194 Elf_Scn *section;
195 GElf_Shdr shdr;
196 Elf_Data *data;
197 int count, ii;
198 bool found = false;
199 GElf_Sym sym;
200
201 if (!symtab)
202 return false;
203
204 // check that header matches library version
205 assert(elf_version(EV_CURRENT) != EV_NONE);
206
207 // get a pointer to elf structure
208 elf = elf_memory((char*)fileData,len);
209
210 assert(elf != NULL);
211
212 // Get the first section
213 section = elf_getscn(elf, secidx);
214
215 // While there are no more sections
216 while (section != NULL) {
217 gelf_getshdr(section, &shdr);
218
219 if (shdr.sh_type == SHT_SYMTAB) {
220 found = true;
221 data = elf_getdata(section, NULL);
222 count = shdr.sh_size / shdr.sh_entsize;
223 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
224
225 // loop through all the symbols, only loading global ones
226 for (ii = 0; ii < count; ++ii) {
227 gelf_getsym(data, ii, &sym);
228 if (GELF_ST_BIND(sym.st_info) & binding) {
229 symtab->insert(sym.st_value,
230 elf_strptr(elf, shdr.sh_link, sym.st_name));
231 }
232 }
233 }
234 ++secidx;
235 section = elf_getscn(elf, secidx);
236 }
237
238 elf_end(elf);
239
240 return found;
241 }
242
243 bool
244 ElfObject::loadGlobalSymbols(SymbolTable *symtab)
245 {
246 return loadSomeSymbols(symtab, STB_GLOBAL);
247 }
248
249 bool
250 ElfObject::loadLocalSymbols(SymbolTable *symtab)
251 {
252 return loadSomeSymbols(symtab, STB_LOCAL);
253 }