Loader: Use address mask provided to load*Symbols when loading the symbols from the...
[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 <cassert>
33 #include <string>
34
35 #include "gelf.h"
36
37 #include "base/bitfield.hh"
38 #include "base/loader/elf_object.hh"
39 #include "base/loader/symtab.hh"
40 #include "base/misc.hh"
41 #include "base/trace.hh" // for DPRINTF
42 #include "sim/byteswap.hh"
43
44 using namespace std;
45
46 ObjectFile *
47 ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
48 {
49 Elf *elf;
50 GElf_Ehdr ehdr;
51 Arch arch = UnknownArch;
52 OpSys opSys = UnknownOpSys;
53
54 // check that header matches library version
55 if (elf_version(EV_CURRENT) == EV_NONE)
56 panic("wrong elf version number!");
57
58 // get a pointer to elf structure
59 elf = elf_memory((char*)data,len);
60 // will only fail if fd is invalid
61 assert(elf != NULL);
62
63 // Check that we actually have a elf file
64 if (gelf_getehdr(elf, &ehdr) ==0) {
65 DPRINTFR(Loader, "Not ELF\n");
66 elf_end(elf);
67 return NULL;
68 } else {
69 //Detect the architecture
70 //Since we don't know how to check for alpha right now, we'll
71 //just assume if it wasn't something else and it's 64 bit, that's
72 //what it must be.
73 if (ehdr.e_machine == EM_SPARC64 ||
74 (ehdr.e_machine == EM_SPARC &&
75 ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
76 ehdr.e_machine == EM_SPARCV9) {
77 arch = ObjectFile::SPARC64;
78 } else if (ehdr.e_machine == EM_SPARC32PLUS ||
79 (ehdr.e_machine == EM_SPARC &&
80 ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
81 arch = ObjectFile::SPARC32;
82 } else if (ehdr.e_machine == EM_MIPS
83 && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
84 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
85 arch = ObjectFile::Mips;
86 } else {
87 fatal("The binary you're trying to load is compiled for big "
88 "endian MIPS. M5\nonly supports little endian MIPS. "
89 "Please recompile your binary.\n");
90 }
91 } else if (ehdr.e_machine == EM_X86_64 &&
92 ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
93 arch = ObjectFile::X86_64;
94 } else if (ehdr.e_machine == EM_386 &&
95 ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
96 arch = ObjectFile::I386;
97 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
98 arch = ObjectFile::Alpha;
99 } else if (ehdr.e_machine == EM_ARM) {
100 if (bits(ehdr.e_entry, 0)) {
101 arch = ObjectFile::Thumb;
102 } else {
103 arch = ObjectFile::Arm;
104 }
105 } else if (ehdr.e_machine == EM_PPC &&
106 ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
107 if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
108 arch = ObjectFile::Power;
109 } else {
110 fatal("The binary you're trying to load is compiled for "
111 "little endian Power.\nM5 only supports big "
112 "endian Power. Please recompile your binary.\n");
113 }
114 } else if (ehdr.e_machine == EM_PPC64) {
115 fatal("The binary you're trying to load is compiled for 64-bit "
116 "Power. M5\n only supports 32-bit Power. Please "
117 "recompile your binary.\n");
118 } else {
119 warn("Unknown architecture: %d\n", ehdr.e_machine);
120 arch = ObjectFile::UnknownArch;
121 }
122
123 //Detect the operating system
124 switch (ehdr.e_ident[EI_OSABI])
125 {
126
127 case ELFOSABI_LINUX:
128 opSys = ObjectFile::Linux;
129 break;
130 case ELFOSABI_SOLARIS:
131 opSys = ObjectFile::Solaris;
132 break;
133 case ELFOSABI_TRU64:
134 opSys = ObjectFile::Tru64;
135 break;
136 case ELFOSABI_ARM:
137 opSys = ObjectFile::LinuxArmOABI;
138 break;
139 default:
140 opSys = ObjectFile::UnknownOpSys;
141 }
142
143 //take a look at the .note.ABI section
144 //It can let us know what's what.
145 if (opSys == ObjectFile::UnknownOpSys) {
146 Elf_Scn *section;
147 GElf_Shdr shdr;
148 Elf_Data *data;
149 uint32_t osAbi;;
150 int secIdx = 1;
151
152 // Get the first section
153 section = elf_getscn(elf, secIdx);
154
155 // While there are no more sections
156 while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
157 gelf_getshdr(section, &shdr);
158 if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
159 elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
160 // we have found a ABI note section
161 // Check the 5th 32bit word for OS 0 == linux, 1 == hurd,
162 // 2 == solaris, 3 == freebsd
163 data = elf_rawdata(section, NULL);
164 assert(data->d_buf);
165 if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
166 osAbi = htole(((uint32_t*)data->d_buf)[4]);
167 else
168 osAbi = htobe(((uint32_t*)data->d_buf)[4]);
169
170 switch(osAbi) {
171 case 0:
172 opSys = ObjectFile::Linux;
173 break;
174 case 2:
175 opSys = ObjectFile::Solaris;
176 break;
177 }
178 } // if section found
179 if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
180 opSys = ObjectFile::Solaris;
181 if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
182 opSys = ObjectFile::Solaris;
183
184 section = elf_getscn(elf, ++secIdx);
185 } // while sections
186 }
187
188 ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
189
190 //The number of headers in the file
191 result->_programHeaderCount = ehdr.e_phnum;
192 //Record the size of each entry
193 result->_programHeaderSize = ehdr.e_phentsize;
194 if(result->_programHeaderCount) //If there is a program header table
195 {
196 //Figure out the virtual address of the header table in the
197 //final memory image. We use the program headers themselves
198 //to translate from a file offset to the address in the image.
199 GElf_Phdr phdr;
200 uint64_t e_phoff = ehdr.e_phoff;
201 result->_programHeaderTable = 0;
202 for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
203 {
204 gelf_getphdr(elf, hdrnum, &phdr);
205 //Check if we've found the segment with the headers in it
206 if(phdr.p_offset <= e_phoff &&
207 phdr.p_offset + phdr.p_filesz > e_phoff)
208 {
209 result->_programHeaderTable = phdr.p_paddr + e_phoff;
210 break;
211 }
212 }
213 }
214 else
215 result->_programHeaderTable = 0;
216
217
218 elf_end(elf);
219 return result;
220 }
221 }
222
223
224 ElfObject::ElfObject(const string &_filename, int _fd,
225 size_t _len, uint8_t *_data,
226 Arch _arch, OpSys _opSys)
227 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
228
229 {
230 Elf *elf;
231 GElf_Ehdr ehdr;
232
233 // check that header matches library version
234 if (elf_version(EV_CURRENT) == EV_NONE)
235 panic("wrong elf version number!");
236
237 // get a pointer to elf structure
238 elf = elf_memory((char*)fileData,len);
239 // will only fail if fd is invalid
240 assert(elf != NULL);
241
242 // Check that we actually have a elf file
243 if (gelf_getehdr(elf, &ehdr) ==0) {
244 panic("Not ELF, shouldn't be here");
245 }
246
247 entry = ehdr.e_entry;
248
249 // initialize segment sizes to 0 in case they're not present
250 text.size = data.size = bss.size = 0;
251
252 int secIdx = 1;
253 Elf_Scn *section;
254 GElf_Shdr shdr;
255
256 // The first address of some important sections.
257 Addr textSecStart = 0;
258 Addr dataSecStart = 0;
259 Addr bssSecStart = 0;
260
261 // Get the first section
262 section = elf_getscn(elf, secIdx);
263
264 // Find the beginning of the most interesting sections.
265 while (section != NULL) {
266 gelf_getshdr(section, &shdr);
267 char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
268
269 if (!strcmp(".text", secName)) {
270 textSecStart = shdr.sh_addr;
271 } else if (!strcmp(".data", secName)) {
272 dataSecStart = shdr.sh_addr;
273 } else if (!strcmp(".bss", secName)) {
274 bssSecStart = shdr.sh_addr;
275 }
276
277 section = elf_getscn(elf, ++secIdx);
278 }
279
280 // Go through all the segments in the program, record them, and scrape
281 // out information about the text, data, and bss areas needed by other
282 // code.
283 for (int i = 0; i < ehdr.e_phnum; ++i) {
284 GElf_Phdr phdr;
285 if (gelf_getphdr(elf, i, &phdr) == 0) {
286 panic("gelf_getphdr failed for segment %d.", i);
287 }
288
289 // for now we don't care about non-loadable segments
290 if (!(phdr.p_type & PT_LOAD))
291 continue;
292
293 // Check to see if this segment contains the bss section.
294 if (phdr.p_paddr <= bssSecStart &&
295 phdr.p_paddr + phdr.p_memsz > bssSecStart &&
296 phdr.p_memsz - phdr.p_filesz > 0) {
297 bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
298 bss.size = phdr.p_memsz - phdr.p_filesz;
299 bss.fileImage = NULL;
300 }
301
302 // Check to see if this is the text or data segment
303 if (phdr.p_vaddr <= textSecStart &&
304 phdr.p_vaddr + phdr.p_filesz > textSecStart) {
305 text.baseAddr = phdr.p_paddr;
306 text.size = phdr.p_filesz;
307 text.fileImage = fileData + phdr.p_offset;
308 } else if (phdr.p_vaddr <= dataSecStart &&
309 phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
310 data.baseAddr = phdr.p_paddr;
311 data.size = phdr.p_filesz;
312 data.fileImage = fileData + phdr.p_offset;
313 } else {
314 // If it's none of the above but is loadable,
315 // load the filesize worth of data
316 Segment extra;
317 extra.baseAddr = phdr.p_paddr;
318 extra.size = phdr.p_filesz;
319 extra.fileImage = fileData + phdr.p_offset;
320 extraSegments.push_back(extra);
321 }
322 }
323
324 // should have found at least one loadable segment
325 assert(text.size != 0);
326
327 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
328 text.baseAddr, text.size, data.baseAddr, data.size,
329 bss.baseAddr, bss.size);
330
331 elf_end(elf);
332
333 // We will actually read the sections when we need to load them
334 }
335
336
337 bool
338 ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
339 {
340 Elf *elf;
341 int sec_idx = 1; // there is a 0 but it is nothing, go figure
342 Elf_Scn *section;
343 GElf_Shdr shdr;
344 Elf_Data *data;
345 int count, ii;
346 bool found = false;
347 GElf_Sym sym;
348
349 if (!symtab)
350 return false;
351
352 // check that header matches library version
353 if (elf_version(EV_CURRENT) == EV_NONE)
354 panic("wrong elf version number!");
355
356 // get a pointer to elf structure
357 elf = elf_memory((char*)fileData,len);
358
359 assert(elf != NULL);
360
361 // Get the first section
362 section = elf_getscn(elf, sec_idx);
363
364 // While there are no more sections
365 while (section != NULL) {
366 gelf_getshdr(section, &shdr);
367
368 if (shdr.sh_type == SHT_SYMTAB) {
369 found = true;
370 data = elf_getdata(section, NULL);
371 count = shdr.sh_size / shdr.sh_entsize;
372 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
373
374 // loop through all the symbols, only loading global ones
375 for (ii = 0; ii < count; ++ii) {
376 gelf_getsym(data, ii, &sym);
377 if (GELF_ST_BIND(sym.st_info) == binding) {
378 symtab->insert(sym.st_value & mask,
379 elf_strptr(elf, shdr.sh_link, sym.st_name));
380 }
381 }
382 }
383 ++sec_idx;
384 section = elf_getscn(elf, sec_idx);
385 }
386
387 elf_end(elf);
388
389 return found;
390 }
391
392 bool
393 ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
394 {
395 return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
396 }
397
398 bool
399 ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
400 {
401 return loadSomeSymbols(symtab, STB_LOCAL, addrMask);
402 }
403
404 bool
405 ElfObject::loadSections(Port *memPort, Addr addrMask)
406 {
407 if (!ObjectFile::loadSections(memPort, addrMask))
408 return false;
409
410 vector<Segment>::iterator extraIt;
411 for (extraIt = extraSegments.begin();
412 extraIt != extraSegments.end(); extraIt++) {
413 if (!loadSection(&(*extraIt), memPort, addrMask)) {
414 return false;
415 }
416 }
417 return true;
418 }
419
420 void
421 ElfObject::getSections()
422 {
423 Elf *elf;
424 int sec_idx = 1; // there is a 0 but it is nothing, go figure
425 Elf_Scn *section;
426 GElf_Shdr shdr;
427
428 GElf_Ehdr ehdr;
429
430 assert(!sectionNames.size());
431
432 // check that header matches library version
433 if (elf_version(EV_CURRENT) == EV_NONE)
434 panic("wrong elf version number!");
435
436 // get a pointer to elf structure
437 elf = elf_memory((char*)fileData,len);
438 assert(elf != NULL);
439
440 // Check that we actually have a elf file
441 if (gelf_getehdr(elf, &ehdr) ==0) {
442 panic("Not ELF, shouldn't be here");
443 }
444
445 // Get the first section
446 section = elf_getscn(elf, sec_idx);
447
448 // While there are no more sections
449 while (section != NULL) {
450 gelf_getshdr(section, &shdr);
451 sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
452 section = elf_getscn(elf, ++sec_idx);
453 } // while sections
454 }
455
456 bool
457 ElfObject::sectionExists(string sec)
458 {
459 if (!sectionNames.size())
460 getSections();
461 return sectionNames.find(sec) != sectionNames.end();
462 }
463
464