elf_object.cc (9810:e895db06e69f) elf_object.cc (10037:5cac77888310)
1/*
1/*
2 * Copyright (c) 2011-2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
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 "base/loader/elf_object.hh"
36#include "base/loader/symtab.hh"
37#include "base/bitfield.hh"
38#include "base/misc.hh"
39#include "base/trace.hh"
40#include "debug/Loader.hh"
41#include "sim/byteswap.hh"
42#include "gelf.h"
43
44using namespace std;
45
46ObjectFile *
47ElfObject::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
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Steve Reinhardt
41 * Ali Saidi
42 */
43
44#include <cassert>
45#include <string>
46
47#include "base/loader/elf_object.hh"
48#include "base/loader/symtab.hh"
49#include "base/bitfield.hh"
50#include "base/misc.hh"
51#include "base/trace.hh"
52#include "debug/Loader.hh"
53#include "sim/byteswap.hh"
54#include "gelf.h"
55
56using namespace std;
57
58ObjectFile *
59ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
60{
61 Elf *elf;
62 GElf_Ehdr ehdr;
63 Arch arch = UnknownArch;
64 OpSys opSys = UnknownOpSys;
65
66 // check that header matches library version
67 if (elf_version(EV_CURRENT) == EV_NONE)
68 panic("wrong elf version number!");
69
70 // get a pointer to elf structure
71 elf = elf_memory((char*)data,len);
72 // will only fail if fd is invalid
73 assert(elf != NULL);
74
75 // Check that we actually have a elf file
64 if (gelf_getehdr(elf, &ehdr) ==0) {
76 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;
77 DPRINTFR(Loader, "Not ELF\n");
78 elf_end(elf);
79 return NULL;
80 } else {
81 //Detect the architecture
82 //Since we don't know how to check for alpha right now, we'll
83 //just assume if it wasn't something else and it's 64 bit, that's
84 //what it must be.
85 if (ehdr.e_machine == EM_SPARC64 ||
86 (ehdr.e_machine == EM_SPARC &&
87 ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
88 ehdr.e_machine == EM_SPARCV9) {
89 arch = ObjectFile::SPARC64;
90 } else if (ehdr.e_machine == EM_SPARC32PLUS ||
91 (ehdr.e_machine == EM_SPARC &&
92 ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
93 arch = ObjectFile::SPARC32;
94 } else if (ehdr.e_machine == EM_MIPS
95 && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
96 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
97 arch = ObjectFile::Mips;
98 } else {
99 fatal("The binary you're trying to load is compiled for big "
100 "endian MIPS. M5\nonly supports little endian MIPS. "
101 "Please recompile your binary.\n");
102 }
103 } else if (ehdr.e_machine == EM_X86_64 &&
104 ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
105 arch = ObjectFile::X86_64;
106 } else if (ehdr.e_machine == EM_386 &&
107 ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
108 arch = ObjectFile::I386;
97 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
98 arch = ObjectFile::Alpha;
99 } else if (ehdr.e_machine == EM_ARM) {
109 } else if (ehdr.e_machine == EM_ARM &&
110 ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
100 if (bits(ehdr.e_entry, 0)) {
101 arch = ObjectFile::Thumb;
102 } else {
103 arch = ObjectFile::Arm;
104 }
111 if (bits(ehdr.e_entry, 0)) {
112 arch = ObjectFile::Thumb;
113 } else {
114 arch = ObjectFile::Arm;
115 }
116 } else if ((ehdr.e_machine == EM_AARCH64) &&
117 ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
118 arch = ObjectFile::Arm64;
119 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
120 arch = ObjectFile::Alpha;
105 } else if (ehdr.e_machine == EM_PPC &&
106 ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
121 } else if (ehdr.e_machine == EM_PPC &&
122 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 "
123 if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
124 arch = ObjectFile::Power;
125 } else {
126 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");
127 "little endian Power.\nM5 only supports big "
128 "endian Power. Please recompile your binary.\n");
113 }
129 }
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
130 } else if (ehdr.e_machine == EM_PPC64) {
131 fatal("The binary you're trying to load is compiled for 64-bit "
132 "Power. M5\n only supports 32-bit Power. Please "
133 "recompile your binary.\n");
134 } else {
135 warn("Unknown architecture: %d\n", ehdr.e_machine);
136 arch = ObjectFile::UnknownArch;
137 }
138
139 //Detect the operating system
124 switch (ehdr.e_ident[EI_OSABI])
125 {
126
140 switch (ehdr.e_ident[EI_OSABI]) {
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 {
141 case ELFOSABI_LINUX:
142 opSys = ObjectFile::Linux;
143 break;
144 case ELFOSABI_SOLARIS:
145 opSys = ObjectFile::Solaris;
146 break;
147 case ELFOSABI_TRU64:
148 opSys = ObjectFile::Tru64;
149 break;
150 case ELFOSABI_ARM:
151 opSys = ObjectFile::LinuxArmOABI;
152 break;
153 default:
154 opSys = ObjectFile::UnknownOpSys;
155 }
156
157 //take a look at the .note.ABI section
158 //It can let us know what's what.
159 if (opSys == ObjectFile::UnknownOpSys) {
160 Elf_Scn *section;
161 GElf_Shdr shdr;
162 Elf_Data *data;
163 uint32_t osAbi;;
164 int secIdx = 1;
165
166 // Get the first section
167 section = elf_getscn(elf, secIdx);
168
169 // While there are no more sections
170 while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
171 gelf_getshdr(section, &shdr);
172 if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
173 elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
174 // we have found a ABI note section
175 // Check the 5th 32bit word for OS 0 == linux, 1 == hurd,
176 // 2 == solaris, 3 == freebsd
177 data = elf_rawdata(section, NULL);
178 assert(data->d_buf);
179 if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
180 osAbi = htole(((uint32_t*)data->d_buf)[4]);
181 else
182 osAbi = htobe(((uint32_t*)data->d_buf)[4]);
183
184 switch(osAbi) {
185 case 0:
186 opSys = ObjectFile::Linux;
187 break;
188 case 2:
189 opSys = ObjectFile::Solaris;
190 break;
191 }
192 } // if section found
193 if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
194 opSys = ObjectFile::Solaris;
195 if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
196 opSys = ObjectFile::Solaris;
197
198 section = elf_getscn(elf, ++secIdx);
199 } // while sections
200 }
201
202 ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
203
204 //The number of headers in the file
205 result->_programHeaderCount = ehdr.e_phnum;
206 //Record the size of each entry
207 result->_programHeaderSize = ehdr.e_phentsize;
208 if(result->_programHeaderCount) //If there is a program header table
209 {
210 //Figure out the virtual address of the header table in the
211 //final memory image. We use the program headers themselves
212 //to translate from a file offset to the address in the image.
213 GElf_Phdr phdr;
214 uint64_t e_phoff = ehdr.e_phoff;
215 result->_programHeaderTable = 0;
216 for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
217 {
218 gelf_getphdr(elf, hdrnum, &phdr);
219 //Check if we've found the segment with the headers in it
220 if(phdr.p_offset <= e_phoff &&
221 phdr.p_offset + phdr.p_filesz > e_phoff)
222 {
209 result->_programHeaderTable = phdr.p_paddr + e_phoff;
223 result->_programHeaderTable =
224 phdr.p_paddr + (e_phoff - phdr.p_offset);
210 break;
211 }
212 }
213 }
214 else
215 result->_programHeaderTable = 0;
216
217
218 elf_end(elf);
219 return result;
220 }
221}
222
223
224ElfObject::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 text.baseAddr = data.baseAddr = bss.baseAddr = 0;
252
253 int secIdx = 1;
254 Elf_Scn *section;
255 GElf_Shdr shdr;
256
257 // The first address of some important sections.
258 Addr textSecStart = 0;
259 Addr dataSecStart = 0;
260 Addr bssSecStart = 0;
261
262 // Get the first section
263 section = elf_getscn(elf, secIdx);
264
265 // Find the beginning of the most interesting sections.
266 while (section != NULL) {
267 gelf_getshdr(section, &shdr);
268 char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
269
270 if (secName) {
271 if (!strcmp(".text", secName)) {
272 textSecStart = shdr.sh_addr;
273 } else if (!strcmp(".data", secName)) {
274 dataSecStart = shdr.sh_addr;
275 } else if (!strcmp(".bss", secName)) {
276 bssSecStart = shdr.sh_addr;
277 }
278 } else {
279 Elf_Error errorNum = (Elf_Error)elf_errno();
280 if (errorNum != ELF_E_NONE) {
281 const char *errorMessage = elf_errmsg(errorNum);
282 fatal("Error from libelf: %s.\n", errorMessage);
283 }
284 }
285
286 section = elf_getscn(elf, ++secIdx);
287 }
288
289 // Go through all the segments in the program, record them, and scrape
290 // out information about the text, data, and bss areas needed by other
291 // code.
292 for (int i = 0; i < ehdr.e_phnum; ++i) {
293 GElf_Phdr phdr;
294 if (gelf_getphdr(elf, i, &phdr) == 0) {
295 panic("gelf_getphdr failed for segment %d.", i);
296 }
297
298 // for now we don't care about non-loadable segments
299 if (!(phdr.p_type & PT_LOAD))
300 continue;
301
302 // Check to see if this segment contains the bss section.
303 if (phdr.p_paddr <= bssSecStart &&
304 phdr.p_paddr + phdr.p_memsz > bssSecStart &&
305 phdr.p_memsz - phdr.p_filesz > 0) {
306 bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
307 bss.size = phdr.p_memsz - phdr.p_filesz;
308 bss.fileImage = NULL;
309 }
310
311 // Check to see if this is the text or data segment
312 if (phdr.p_vaddr <= textSecStart &&
313 phdr.p_vaddr + phdr.p_filesz > textSecStart) {
314 text.baseAddr = phdr.p_paddr;
315 text.size = phdr.p_filesz;
316 text.fileImage = fileData + phdr.p_offset;
317 } else if (phdr.p_vaddr <= dataSecStart &&
318 phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
319 data.baseAddr = phdr.p_paddr;
320 data.size = phdr.p_filesz;
321 data.fileImage = fileData + phdr.p_offset;
322 } else {
323 // If it's none of the above but is loadable,
324 // load the filesize worth of data
325 Segment extra;
326 extra.baseAddr = phdr.p_paddr;
327 extra.size = phdr.p_filesz;
328 extra.fileImage = fileData + phdr.p_offset;
329 extraSegments.push_back(extra);
330 }
331 }
332
333 // should have found at least one loadable segment
334 assert(text.size != 0);
335
336 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
337 text.baseAddr, text.size, data.baseAddr, data.size,
338 bss.baseAddr, bss.size);
339
340 elf_end(elf);
341
342 // We will actually read the sections when we need to load them
343}
344
345
346bool
347ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
348{
349 Elf *elf;
350 int sec_idx = 1; // there is a 0 but it is nothing, go figure
351 Elf_Scn *section;
352 GElf_Shdr shdr;
353 Elf_Data *data;
354 int count, ii;
355 bool found = false;
356 GElf_Sym sym;
357
358 if (!symtab)
359 return false;
360
361 // check that header matches library version
362 if (elf_version(EV_CURRENT) == EV_NONE)
363 panic("wrong elf version number!");
364
365 // get a pointer to elf structure
366 elf = elf_memory((char*)fileData,len);
367
368 assert(elf != NULL);
369
370 // Get the first section
371 section = elf_getscn(elf, sec_idx);
372
373 // While there are no more sections
374 while (section != NULL) {
375 gelf_getshdr(section, &shdr);
376
377 if (shdr.sh_type == SHT_SYMTAB) {
378 found = true;
379 data = elf_getdata(section, NULL);
380 count = shdr.sh_size / shdr.sh_entsize;
381 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
382
383 // loop through all the symbols, only loading global ones
384 for (ii = 0; ii < count; ++ii) {
385 gelf_getsym(data, ii, &sym);
386 if (GELF_ST_BIND(sym.st_info) == binding) {
387 char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
388 if (sym_name && sym_name[0] != '$') {
389 DPRINTF(Loader, "Symbol: %-40s value %#x\n",
390 sym_name, sym.st_value);
391 symtab->insert(sym.st_value & mask, sym_name);
392 }
393 }
394 }
395 }
396 ++sec_idx;
397 section = elf_getscn(elf, sec_idx);
398 }
399
400 elf_end(elf);
401
402 return found;
403}
404
405bool
406ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
407{
408 return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
409}
410
411bool
412ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
413{
414 bool found_local = loadSomeSymbols(symtab, STB_LOCAL, addrMask);
415 bool found_weak = loadSomeSymbols(symtab, STB_WEAK, addrMask);
416 return found_local || found_weak;
417}
418
419bool
420ElfObject::loadWeakSymbols(SymbolTable *symtab, Addr addrMask)
421{
422 return loadSomeSymbols(symtab, STB_WEAK, addrMask);
423}
424
425bool
225 break;
226 }
227 }
228 }
229 else
230 result->_programHeaderTable = 0;
231
232
233 elf_end(elf);
234 return result;
235 }
236}
237
238
239ElfObject::ElfObject(const string &_filename, int _fd,
240 size_t _len, uint8_t *_data,
241 Arch _arch, OpSys _opSys)
242 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
243
244{
245 Elf *elf;
246 GElf_Ehdr ehdr;
247
248 // check that header matches library version
249 if (elf_version(EV_CURRENT) == EV_NONE)
250 panic("wrong elf version number!");
251
252 // get a pointer to elf structure
253 elf = elf_memory((char*)fileData,len);
254 // will only fail if fd is invalid
255 assert(elf != NULL);
256
257 // Check that we actually have a elf file
258 if (gelf_getehdr(elf, &ehdr) ==0) {
259 panic("Not ELF, shouldn't be here");
260 }
261
262 entry = ehdr.e_entry;
263
264 // initialize segment sizes to 0 in case they're not present
265 text.size = data.size = bss.size = 0;
266 text.baseAddr = data.baseAddr = bss.baseAddr = 0;
267
268 int secIdx = 1;
269 Elf_Scn *section;
270 GElf_Shdr shdr;
271
272 // The first address of some important sections.
273 Addr textSecStart = 0;
274 Addr dataSecStart = 0;
275 Addr bssSecStart = 0;
276
277 // Get the first section
278 section = elf_getscn(elf, secIdx);
279
280 // Find the beginning of the most interesting sections.
281 while (section != NULL) {
282 gelf_getshdr(section, &shdr);
283 char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
284
285 if (secName) {
286 if (!strcmp(".text", secName)) {
287 textSecStart = shdr.sh_addr;
288 } else if (!strcmp(".data", secName)) {
289 dataSecStart = shdr.sh_addr;
290 } else if (!strcmp(".bss", secName)) {
291 bssSecStart = shdr.sh_addr;
292 }
293 } else {
294 Elf_Error errorNum = (Elf_Error)elf_errno();
295 if (errorNum != ELF_E_NONE) {
296 const char *errorMessage = elf_errmsg(errorNum);
297 fatal("Error from libelf: %s.\n", errorMessage);
298 }
299 }
300
301 section = elf_getscn(elf, ++secIdx);
302 }
303
304 // Go through all the segments in the program, record them, and scrape
305 // out information about the text, data, and bss areas needed by other
306 // code.
307 for (int i = 0; i < ehdr.e_phnum; ++i) {
308 GElf_Phdr phdr;
309 if (gelf_getphdr(elf, i, &phdr) == 0) {
310 panic("gelf_getphdr failed for segment %d.", i);
311 }
312
313 // for now we don't care about non-loadable segments
314 if (!(phdr.p_type & PT_LOAD))
315 continue;
316
317 // Check to see if this segment contains the bss section.
318 if (phdr.p_paddr <= bssSecStart &&
319 phdr.p_paddr + phdr.p_memsz > bssSecStart &&
320 phdr.p_memsz - phdr.p_filesz > 0) {
321 bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
322 bss.size = phdr.p_memsz - phdr.p_filesz;
323 bss.fileImage = NULL;
324 }
325
326 // Check to see if this is the text or data segment
327 if (phdr.p_vaddr <= textSecStart &&
328 phdr.p_vaddr + phdr.p_filesz > textSecStart) {
329 text.baseAddr = phdr.p_paddr;
330 text.size = phdr.p_filesz;
331 text.fileImage = fileData + phdr.p_offset;
332 } else if (phdr.p_vaddr <= dataSecStart &&
333 phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
334 data.baseAddr = phdr.p_paddr;
335 data.size = phdr.p_filesz;
336 data.fileImage = fileData + phdr.p_offset;
337 } else {
338 // If it's none of the above but is loadable,
339 // load the filesize worth of data
340 Segment extra;
341 extra.baseAddr = phdr.p_paddr;
342 extra.size = phdr.p_filesz;
343 extra.fileImage = fileData + phdr.p_offset;
344 extraSegments.push_back(extra);
345 }
346 }
347
348 // should have found at least one loadable segment
349 assert(text.size != 0);
350
351 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
352 text.baseAddr, text.size, data.baseAddr, data.size,
353 bss.baseAddr, bss.size);
354
355 elf_end(elf);
356
357 // We will actually read the sections when we need to load them
358}
359
360
361bool
362ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
363{
364 Elf *elf;
365 int sec_idx = 1; // there is a 0 but it is nothing, go figure
366 Elf_Scn *section;
367 GElf_Shdr shdr;
368 Elf_Data *data;
369 int count, ii;
370 bool found = false;
371 GElf_Sym sym;
372
373 if (!symtab)
374 return false;
375
376 // check that header matches library version
377 if (elf_version(EV_CURRENT) == EV_NONE)
378 panic("wrong elf version number!");
379
380 // get a pointer to elf structure
381 elf = elf_memory((char*)fileData,len);
382
383 assert(elf != NULL);
384
385 // Get the first section
386 section = elf_getscn(elf, sec_idx);
387
388 // While there are no more sections
389 while (section != NULL) {
390 gelf_getshdr(section, &shdr);
391
392 if (shdr.sh_type == SHT_SYMTAB) {
393 found = true;
394 data = elf_getdata(section, NULL);
395 count = shdr.sh_size / shdr.sh_entsize;
396 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
397
398 // loop through all the symbols, only loading global ones
399 for (ii = 0; ii < count; ++ii) {
400 gelf_getsym(data, ii, &sym);
401 if (GELF_ST_BIND(sym.st_info) == binding) {
402 char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
403 if (sym_name && sym_name[0] != '$') {
404 DPRINTF(Loader, "Symbol: %-40s value %#x\n",
405 sym_name, sym.st_value);
406 symtab->insert(sym.st_value & mask, sym_name);
407 }
408 }
409 }
410 }
411 ++sec_idx;
412 section = elf_getscn(elf, sec_idx);
413 }
414
415 elf_end(elf);
416
417 return found;
418}
419
420bool
421ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
422{
423 return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
424}
425
426bool
427ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
428{
429 bool found_local = loadSomeSymbols(symtab, STB_LOCAL, addrMask);
430 bool found_weak = loadSomeSymbols(symtab, STB_WEAK, addrMask);
431 return found_local || found_weak;
432}
433
434bool
435ElfObject::loadWeakSymbols(SymbolTable *symtab, Addr addrMask)
436{
437 return loadSomeSymbols(symtab, STB_WEAK, addrMask);
438}
439
440bool
426ElfObject::loadSections(PortProxy& memProxy, Addr addrMask)
441ElfObject::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset)
427{
442{
428 if (!ObjectFile::loadSections(memProxy, addrMask))
443 if (!ObjectFile::loadSections(memProxy, addrMask, offset))
429 return false;
430
431 vector<Segment>::iterator extraIt;
432 for (extraIt = extraSegments.begin();
433 extraIt != extraSegments.end(); extraIt++) {
444 return false;
445
446 vector<Segment>::iterator extraIt;
447 for (extraIt = extraSegments.begin();
448 extraIt != extraSegments.end(); extraIt++) {
434 if (!loadSection(&(*extraIt), memProxy, addrMask)) {
449 if (!loadSection(&(*extraIt), memProxy, addrMask, offset)) {
435 return false;
436 }
437 }
438 return true;
439}
440
441void
442ElfObject::getSections()
443{
444 Elf *elf;
445 int sec_idx = 1; // there is a 0 but it is nothing, go figure
446 Elf_Scn *section;
447 GElf_Shdr shdr;
448
449 GElf_Ehdr ehdr;
450
451 assert(!sectionNames.size());
452
453 // check that header matches library version
454 if (elf_version(EV_CURRENT) == EV_NONE)
455 panic("wrong elf version number!");
456
457 // get a pointer to elf structure
458 elf = elf_memory((char*)fileData,len);
459 assert(elf != NULL);
460
461 // Check that we actually have a elf file
462 if (gelf_getehdr(elf, &ehdr) ==0) {
463 panic("Not ELF, shouldn't be here");
464 }
465
466 // Get the first section
467 section = elf_getscn(elf, sec_idx);
468
469 // While there are no more sections
470 while (section != NULL) {
471 gelf_getshdr(section, &shdr);
472 sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
473 section = elf_getscn(elf, ++sec_idx);
474 } // while sections
475}
476
477bool
478ElfObject::sectionExists(string sec)
479{
480 if (!sectionNames.size())
481 getSections();
482 return sectionNames.find(sec) != sectionNames.end();
483}
484
485
450 return false;
451 }
452 }
453 return true;
454}
455
456void
457ElfObject::getSections()
458{
459 Elf *elf;
460 int sec_idx = 1; // there is a 0 but it is nothing, go figure
461 Elf_Scn *section;
462 GElf_Shdr shdr;
463
464 GElf_Ehdr ehdr;
465
466 assert(!sectionNames.size());
467
468 // check that header matches library version
469 if (elf_version(EV_CURRENT) == EV_NONE)
470 panic("wrong elf version number!");
471
472 // get a pointer to elf structure
473 elf = elf_memory((char*)fileData,len);
474 assert(elf != NULL);
475
476 // Check that we actually have a elf file
477 if (gelf_getehdr(elf, &ehdr) ==0) {
478 panic("Not ELF, shouldn't be here");
479 }
480
481 // Get the first section
482 section = elf_getscn(elf, sec_idx);
483
484 // While there are no more sections
485 while (section != NULL) {
486 gelf_getshdr(section, &shdr);
487 sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
488 section = elf_getscn(elf, ++sec_idx);
489 } // while sections
490}
491
492bool
493ElfObject::sectionExists(string sec)
494{
495 if (!sectionNames.size())
496 getSections();
497 return sectionNames.find(sec) != sectionNames.end();
498}
499
500