b56dc5aa6be5d326a2276b3c399a06a439d5a8fb
[gem5.git] / src / base / loader / elf_object.cc
1 /*
2 * Copyright (c) 2003-2005 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 * Authors: Steve Reinhardt
29 * Ali Saidi
30 */
31
32 #include <string>
33
34 // Because of the -Wundef flag we have to do this
35 #define __LIBELF_INTERNAL__ 0
36 #define __LIBELF_NEED_LINK_H 0
37 #define __LIBELF_SYMBOL_VERSIONS 0
38
39 #include "gelf.h"
40
41 #include "base/loader/elf_object.hh"
42 #include "base/misc.hh"
43
44 #include "base/loader/symtab.hh"
45
46 #include "base/trace.hh" // for DPRINTF
47
48 #include "sim/byteswap.hh"
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 Arch arch = UnknownArch;
59 OpSys opSys = UnknownOpSys;
60
61 // check that header matches library version
62 if (elf_version(EV_CURRENT) == EV_NONE)
63 panic("wrong elf version number!");
64
65 // get a pointer to elf structure
66 elf = elf_memory((char*)data,len);
67 // will only fail if fd is invalid
68 assert(elf != NULL);
69
70 // Check that we actually have a elf file
71 if (gelf_getehdr(elf, &ehdr) ==0) {
72 DPRINTFR(Loader, "Not ELF\n");
73 elf_end(elf);
74 return NULL;
75 } else {
76 //Detect the architecture
77 //Since we don't know how to check for alpha right now, we'll
78 //just assume if it wasn't something else and it's 64 bit, that's
79 //what it must be.
80 if (ehdr.e_machine == EM_SPARC64 ||
81 (ehdr.e_machine == EM_SPARC &&
82 ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
83 ehdr.e_machine == EM_SPARCV9) {
84 arch = ObjectFile::SPARC64;
85 } else if (ehdr.e_machine == EM_SPARC32PLUS ||
86 (ehdr.e_machine == EM_SPARC &&
87 ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
88 arch = ObjectFile::SPARC32;
89 } else if (ehdr.e_machine == EM_MIPS
90 && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
91 arch = ObjectFile::Mips;
92 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
93 arch = ObjectFile::Alpha;
94 } else {
95 warn("Unknown architecture: %d\n", ehdr.e_machine);
96 arch = ObjectFile::UnknownArch;
97 }
98
99 //Detect the operating system
100 switch (ehdr.e_ident[EI_OSABI])
101 {
102
103 case ELFOSABI_LINUX:
104 opSys = ObjectFile::Linux;
105 break;
106 case ELFOSABI_SOLARIS:
107 opSys = ObjectFile::Solaris;
108 break;
109 case ELFOSABI_TRU64:
110 opSys = ObjectFile::Tru64;
111 break;
112 default:
113 opSys = ObjectFile::UnknownOpSys;
114 }
115
116 //take a look at the .note.ABI section
117 //It can let us know what's what.
118 if (opSys == ObjectFile::UnknownOpSys) {
119 Elf_Scn *section;
120 GElf_Shdr shdr;
121 Elf_Data *data;
122 uint32_t osAbi;;
123 int secIdx = 1;
124
125 // Get the first section
126 section = elf_getscn(elf, secIdx);
127
128 // While there are no more sections
129 while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
130 gelf_getshdr(section, &shdr);
131 if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
132 elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
133 // we have found a ABI note section
134 // Check the 5th 32bit word for OS 0 == linux, 1 == hurd,
135 // 2 == solaris, 3 == freebsd
136 data = elf_rawdata(section, NULL);
137 assert(data->d_buf);
138 if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
139 osAbi = htole(((uint32_t*)data->d_buf)[4]);
140 else
141 osAbi = htobe(((uint32_t*)data->d_buf)[4]);
142
143 switch(osAbi) {
144 case 0:
145 opSys = ObjectFile::Linux;
146 break;
147 case 2:
148 opSys = ObjectFile::Solaris;
149 break;
150 }
151 } // if section found
152 if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
153 opSys = ObjectFile::Solaris;
154 if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
155 opSys = ObjectFile::Solaris;
156
157 section = elf_getscn(elf, ++secIdx);
158 } // while sections
159 }
160
161 ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
162
163 //The number of headers in the file
164 result->_programHeaderCount = ehdr.e_phnum;
165 //Record the size of each entry
166 result->_programHeaderSize = ehdr.e_phentsize;
167 if(result->_programHeaderCount) //If there is a program header table
168 {
169 //Figure out the virtual address of the header table in the
170 //final memory image. We use the program headers themselves
171 //to translate from a file offset to the address in the image.
172 GElf_Phdr phdr;
173 uint64_t e_phoff = ehdr.e_phoff;
174 result->_programHeaderTable = 0;
175 for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
176 {
177 gelf_getphdr(elf, hdrnum, &phdr);
178 //Check if we've found the segment with the headers in it
179 if(phdr.p_offset <= e_phoff &&
180 phdr.p_offset + phdr.p_filesz > e_phoff)
181 {
182 result->_programHeaderTable = phdr.p_vaddr + e_phoff;
183 break;
184 }
185 }
186 }
187 else
188 result->_programHeaderTable = 0;
189
190
191 elf_end(elf);
192 return result;
193 }
194 }
195
196
197 ElfObject::ElfObject(const string &_filename, int _fd,
198 size_t _len, uint8_t *_data,
199 Arch _arch, OpSys _opSys)
200 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
201
202 {
203 Elf *elf;
204 GElf_Ehdr ehdr;
205
206 // check that header matches library version
207 if (elf_version(EV_CURRENT) == EV_NONE)
208 panic("wrong elf version number!");
209
210 // get a pointer to elf structure
211 elf = elf_memory((char*)fileData,len);
212 // will only fail if fd is invalid
213 assert(elf != NULL);
214
215 // Check that we actually have a elf file
216 if (gelf_getehdr(elf, &ehdr) ==0) {
217 panic("Not ELF, shouldn't be here");
218 }
219
220 entry = ehdr.e_entry;
221
222
223 // initialize segment sizes to 0 in case they're not present
224 text.size = data.size = bss.size = 0;
225
226 for (int i = 0; i < ehdr.e_phnum; ++i) {
227 GElf_Phdr phdr;
228 if (gelf_getphdr(elf, i, &phdr) == 0) {
229 panic("gelf_getphdr failed for section %d", i);
230 }
231
232 // for now we don't care about non-loadable segments
233 if (!(phdr.p_type & PT_LOAD))
234 continue;
235
236 // the headers don't explicitly distinguish text from data,
237 // but empirically the text segment comes first.
238 if (text.size == 0) { // haven't seen text segment yet
239 text.baseAddr = phdr.p_vaddr;
240 text.size = phdr.p_filesz;
241 text.fileImage = fileData + phdr.p_offset;
242 // if there's any padding at the end that's not in the
243 // file, call it the bss. This happens in the "text"
244 // segment if there's only one loadable segment (as for
245 // kernel images).
246 bss.size = phdr.p_memsz - phdr.p_filesz;
247 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
248 bss.fileImage = NULL;
249 } else if (data.size == 0) { // have text, this must be data
250 data.baseAddr = phdr.p_vaddr;
251 data.size = phdr.p_filesz;
252 data.fileImage = fileData + phdr.p_offset;
253 // if there's any padding at the end that's not in the
254 // file, call it the bss. Warn if this happens for both
255 // the text & data segments (should only have one bss).
256 if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) {
257 warn("Two implied bss segments in file!\n");
258 }
259 bss.size = phdr.p_memsz - phdr.p_filesz;
260 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
261 bss.fileImage = NULL;
262 } else {
263 warn("More than two loadable segments in ELF object.");
264 warn("Ignoring segment @ 0x%x length 0x%x.",
265 phdr.p_vaddr, phdr.p_filesz);
266 }
267 }
268
269 // should have found at least one loadable segment
270 assert(text.size != 0);
271
272 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
273 text.baseAddr, text.size, data.baseAddr, data.size,
274 bss.baseAddr, bss.size);
275
276 elf_end(elf);
277
278 // We will actually read the sections when we need to load them
279 }
280
281
282 bool
283 ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding)
284 {
285 Elf *elf;
286 int sec_idx = 1; // there is a 0 but it is nothing, go figure
287 Elf_Scn *section;
288 GElf_Shdr shdr;
289 Elf_Data *data;
290 int count, ii;
291 bool found = false;
292 GElf_Sym sym;
293
294 if (!symtab)
295 return false;
296
297 // check that header matches library version
298 if (elf_version(EV_CURRENT) == EV_NONE)
299 panic("wrong elf version number!");
300
301 // get a pointer to elf structure
302 elf = elf_memory((char*)fileData,len);
303
304 assert(elf != NULL);
305
306 // Get the first section
307 section = elf_getscn(elf, sec_idx);
308
309 // While there are no more sections
310 while (section != NULL) {
311 gelf_getshdr(section, &shdr);
312
313 if (shdr.sh_type == SHT_SYMTAB) {
314 found = true;
315 data = elf_getdata(section, NULL);
316 count = shdr.sh_size / shdr.sh_entsize;
317 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
318
319 // loop through all the symbols, only loading global ones
320 for (ii = 0; ii < count; ++ii) {
321 gelf_getsym(data, ii, &sym);
322 if (GELF_ST_BIND(sym.st_info) == binding) {
323 symtab->insert(sym.st_value,
324 elf_strptr(elf, shdr.sh_link, sym.st_name));
325 }
326 }
327 }
328 ++sec_idx;
329 section = elf_getscn(elf, sec_idx);
330 }
331
332 elf_end(elf);
333
334 return found;
335 }
336
337 bool
338 ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
339 {
340 return loadSomeSymbols(symtab, STB_GLOBAL);
341 }
342
343 bool
344 ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
345 {
346 return loadSomeSymbols(symtab, STB_LOCAL);
347 }
348
349 bool
350 ElfObject::isDynamic()
351 {
352 Elf *elf;
353 int sec_idx = 1; // there is a 0 but it is nothing, go figure
354 Elf_Scn *section;
355 GElf_Shdr shdr;
356
357 GElf_Ehdr ehdr;
358
359 // check that header matches library version
360 if (elf_version(EV_CURRENT) == EV_NONE)
361 panic("wrong elf version number!");
362
363 // get a pointer to elf structure
364 elf = elf_memory((char*)fileData,len);
365 assert(elf != NULL);
366
367 // Check that we actually have a elf file
368 if (gelf_getehdr(elf, &ehdr) ==0) {
369 panic("Not ELF, shouldn't be here");
370 }
371
372 // Get the first section
373 section = elf_getscn(elf, sec_idx);
374
375 // While there are no more sections
376 while (section != NULL) {
377 gelf_getshdr(section, &shdr);
378 if (!strcmp(".interp", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
379 return true;
380 section = elf_getscn(elf, ++sec_idx);
381 } // while sections
382 return false;
383 }
384
385