2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Steve Reinhardt
37 #include "base/loader/elf_object.hh"
38 #include "base/loader/symtab.hh"
39 #include "base/misc.hh"
40 #include "base/trace.hh" // for DPRINTF
41 #include "sim/byteswap.hh"
46 ElfObject::tryFile(const string
&fname
, int fd
, size_t len
, uint8_t *data
)
50 Arch arch
= UnknownArch
;
51 OpSys opSys
= UnknownOpSys
;
53 // check that header matches library version
54 if (elf_version(EV_CURRENT
) == EV_NONE
)
55 panic("wrong elf version number!");
57 // get a pointer to elf structure
58 elf
= elf_memory((char*)data
,len
);
59 // will only fail if fd is invalid
62 // Check that we actually have a elf file
63 if (gelf_getehdr(elf
, &ehdr
) ==0) {
64 DPRINTFR(Loader
, "Not ELF\n");
68 //Detect the architecture
69 //Since we don't know how to check for alpha right now, we'll
70 //just assume if it wasn't something else and it's 64 bit, that's
72 if (ehdr
.e_machine
== EM_SPARC64
||
73 (ehdr
.e_machine
== EM_SPARC
&&
74 ehdr
.e_ident
[EI_CLASS
] == ELFCLASS64
)||
75 ehdr
.e_machine
== EM_SPARCV9
) {
76 arch
= ObjectFile::SPARC64
;
77 } else if (ehdr
.e_machine
== EM_SPARC32PLUS
||
78 (ehdr
.e_machine
== EM_SPARC
&&
79 ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
)) {
80 arch
= ObjectFile::SPARC32
;
81 } else if (ehdr
.e_machine
== EM_MIPS
82 && ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
) {
83 if (ehdr
.e_ident
[EI_DATA
] == ELFDATA2LSB
) {
84 arch
= ObjectFile::Mips
;
86 fatal("The binary you're trying to load is compiled for big "
87 "endian MIPS. M5\nonly supports little endian MIPS. "
88 "Please recompile your binary.\n");
90 } else if (ehdr
.e_machine
== EM_X86_64
&&
91 ehdr
.e_ident
[EI_CLASS
] == ELFCLASS64
) {
92 arch
= ObjectFile::X86_64
;
93 } else if (ehdr
.e_machine
== EM_386
&&
94 ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
) {
95 arch
= ObjectFile::I386
;
96 } else if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS64
) {
97 arch
= ObjectFile::Alpha
;
98 } else if (ehdr
.e_machine
== EM_ARM
) {
99 arch
= ObjectFile::Arm
;
100 } else if (ehdr
.e_machine
== EM_PPC
&&
101 ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
) {
102 if (ehdr
.e_ident
[EI_DATA
] == ELFDATA2MSB
) {
103 arch
= ObjectFile::Power
;
105 fatal("The binary you're trying to load is compiled for "
106 "little endian Power.\nM5 only supports big "
107 "endian Power. Please recompile your binary.\n");
109 } else if (ehdr
.e_machine
== EM_PPC64
) {
110 fatal("The binary you're trying to load is compiled for 64-bit "
111 "Power. M5\n only supports 32-bit Power. Please "
112 "recompile your binary.\n");
114 warn("Unknown architecture: %d\n", ehdr
.e_machine
);
115 arch
= ObjectFile::UnknownArch
;
118 //Detect the operating system
119 switch (ehdr
.e_ident
[EI_OSABI
])
123 opSys
= ObjectFile::Linux
;
125 case ELFOSABI_SOLARIS
:
126 opSys
= ObjectFile::Solaris
;
129 opSys
= ObjectFile::Tru64
;
132 opSys
= ObjectFile::LinuxArmOABI
;
135 opSys
= ObjectFile::UnknownOpSys
;
138 //take a look at the .note.ABI section
139 //It can let us know what's what.
140 if (opSys
== ObjectFile::UnknownOpSys
) {
147 // Get the first section
148 section
= elf_getscn(elf
, secIdx
);
150 // While there are no more sections
151 while (section
!= NULL
&& opSys
== ObjectFile::UnknownOpSys
) {
152 gelf_getshdr(section
, &shdr
);
153 if (shdr
.sh_type
== SHT_NOTE
&& !strcmp(".note.ABI-tag",
154 elf_strptr(elf
, ehdr
.e_shstrndx
, shdr
.sh_name
))) {
155 // we have found a ABI note section
156 // Check the 5th 32bit word for OS 0 == linux, 1 == hurd,
157 // 2 == solaris, 3 == freebsd
158 data
= elf_rawdata(section
, NULL
);
160 if(ehdr
.e_ident
[EI_DATA
] == ELFDATA2LSB
)
161 osAbi
= htole(((uint32_t*)data
->d_buf
)[4]);
163 osAbi
= htobe(((uint32_t*)data
->d_buf
)[4]);
167 opSys
= ObjectFile::Linux
;
170 opSys
= ObjectFile::Solaris
;
173 } // if section found
174 if (!strcmp(".SUNW_version", elf_strptr(elf
, ehdr
.e_shstrndx
, shdr
.sh_name
)))
175 opSys
= ObjectFile::Solaris
;
176 if (!strcmp(".stab.index", elf_strptr(elf
, ehdr
.e_shstrndx
, shdr
.sh_name
)))
177 opSys
= ObjectFile::Solaris
;
179 section
= elf_getscn(elf
, ++secIdx
);
183 ElfObject
* result
= new ElfObject(fname
, fd
, len
, data
, arch
, opSys
);
185 //The number of headers in the file
186 result
->_programHeaderCount
= ehdr
.e_phnum
;
187 //Record the size of each entry
188 result
->_programHeaderSize
= ehdr
.e_phentsize
;
189 if(result
->_programHeaderCount
) //If there is a program header table
191 //Figure out the virtual address of the header table in the
192 //final memory image. We use the program headers themselves
193 //to translate from a file offset to the address in the image.
195 uint64_t e_phoff
= ehdr
.e_phoff
;
196 result
->_programHeaderTable
= 0;
197 for(int hdrnum
= 0; hdrnum
< result
->_programHeaderCount
; hdrnum
++)
199 gelf_getphdr(elf
, hdrnum
, &phdr
);
200 //Check if we've found the segment with the headers in it
201 if(phdr
.p_offset
<= e_phoff
&&
202 phdr
.p_offset
+ phdr
.p_filesz
> e_phoff
)
204 result
->_programHeaderTable
= phdr
.p_paddr
+ e_phoff
;
210 result
->_programHeaderTable
= 0;
219 ElfObject::ElfObject(const string
&_filename
, int _fd
,
220 size_t _len
, uint8_t *_data
,
221 Arch _arch
, OpSys _opSys
)
222 : ObjectFile(_filename
, _fd
, _len
, _data
, _arch
, _opSys
)
228 // check that header matches library version
229 if (elf_version(EV_CURRENT
) == EV_NONE
)
230 panic("wrong elf version number!");
232 // get a pointer to elf structure
233 elf
= elf_memory((char*)fileData
,len
);
234 // will only fail if fd is invalid
237 // Check that we actually have a elf file
238 if (gelf_getehdr(elf
, &ehdr
) ==0) {
239 panic("Not ELF, shouldn't be here");
242 entry
= ehdr
.e_entry
;
244 // initialize segment sizes to 0 in case they're not present
245 text
.size
= data
.size
= bss
.size
= 0;
251 // The first address of some important sections.
252 Addr textSecStart
= 0;
253 Addr dataSecStart
= 0;
254 Addr bssSecStart
= 0;
256 // Get the first section
257 section
= elf_getscn(elf
, secIdx
);
259 // Find the beginning of the most interesting sections.
260 while (section
!= NULL
) {
261 gelf_getshdr(section
, &shdr
);
262 char * secName
= elf_strptr(elf
, ehdr
.e_shstrndx
, shdr
.sh_name
);
264 if (!strcmp(".text", secName
)) {
265 textSecStart
= shdr
.sh_addr
;
266 } else if (!strcmp(".data", secName
)) {
267 dataSecStart
= shdr
.sh_addr
;
268 } else if (!strcmp(".bss", secName
)) {
269 bssSecStart
= shdr
.sh_addr
;
272 section
= elf_getscn(elf
, ++secIdx
);
275 // Go through all the segments in the program, record them, and scrape
276 // out information about the text, data, and bss areas needed by other
278 for (int i
= 0; i
< ehdr
.e_phnum
; ++i
) {
280 if (gelf_getphdr(elf
, i
, &phdr
) == 0) {
281 panic("gelf_getphdr failed for segment %d.", i
);
284 // for now we don't care about non-loadable segments
285 if (!(phdr
.p_type
& PT_LOAD
))
288 // Check to see if this segment contains the bss section.
289 if (phdr
.p_paddr
<= bssSecStart
&&
290 phdr
.p_paddr
+ phdr
.p_memsz
> bssSecStart
&&
291 phdr
.p_memsz
- phdr
.p_filesz
> 0) {
292 bss
.baseAddr
= phdr
.p_paddr
+ phdr
.p_filesz
;
293 bss
.size
= phdr
.p_memsz
- phdr
.p_filesz
;
294 bss
.fileImage
= NULL
;
297 // Check to see if this is the text or data segment
298 if (phdr
.p_vaddr
<= textSecStart
&&
299 phdr
.p_vaddr
+ phdr
.p_filesz
> textSecStart
) {
300 text
.baseAddr
= phdr
.p_paddr
;
301 text
.size
= phdr
.p_filesz
;
302 text
.fileImage
= fileData
+ phdr
.p_offset
;
303 } else if (phdr
.p_vaddr
<= dataSecStart
&&
304 phdr
.p_vaddr
+ phdr
.p_filesz
> dataSecStart
) {
305 data
.baseAddr
= phdr
.p_paddr
;
306 data
.size
= phdr
.p_filesz
;
307 data
.fileImage
= fileData
+ phdr
.p_offset
;
309 // If it's none of the above but is loadable,
310 // load the filesize worth of data
312 extra
.baseAddr
= phdr
.p_paddr
;
313 extra
.size
= phdr
.p_filesz
;
314 extra
.fileImage
= fileData
+ phdr
.p_offset
;
315 extraSegments
.push_back(extra
);
319 // should have found at least one loadable segment
320 assert(text
.size
!= 0);
322 DPRINTFR(Loader
, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
323 text
.baseAddr
, text
.size
, data
.baseAddr
, data
.size
,
324 bss
.baseAddr
, bss
.size
);
328 // We will actually read the sections when we need to load them
333 ElfObject::loadSomeSymbols(SymbolTable
*symtab
, int binding
)
336 int sec_idx
= 1; // there is a 0 but it is nothing, go figure
347 // check that header matches library version
348 if (elf_version(EV_CURRENT
) == EV_NONE
)
349 panic("wrong elf version number!");
351 // get a pointer to elf structure
352 elf
= elf_memory((char*)fileData
,len
);
356 // Get the first section
357 section
= elf_getscn(elf
, sec_idx
);
359 // While there are no more sections
360 while (section
!= NULL
) {
361 gelf_getshdr(section
, &shdr
);
363 if (shdr
.sh_type
== SHT_SYMTAB
) {
365 data
= elf_getdata(section
, NULL
);
366 count
= shdr
.sh_size
/ shdr
.sh_entsize
;
367 DPRINTF(Loader
, "Found Symbol Table, %d symbols present\n", count
);
369 // loop through all the symbols, only loading global ones
370 for (ii
= 0; ii
< count
; ++ii
) {
371 gelf_getsym(data
, ii
, &sym
);
372 if (GELF_ST_BIND(sym
.st_info
) == binding
) {
373 symtab
->insert(sym
.st_value
,
374 elf_strptr(elf
, shdr
.sh_link
, sym
.st_name
));
379 section
= elf_getscn(elf
, sec_idx
);
388 ElfObject::loadGlobalSymbols(SymbolTable
*symtab
, Addr addrMask
)
390 return loadSomeSymbols(symtab
, STB_GLOBAL
);
394 ElfObject::loadLocalSymbols(SymbolTable
*symtab
, Addr addrMask
)
396 return loadSomeSymbols(symtab
, STB_LOCAL
);
400 ElfObject::loadSections(Port
*memPort
, Addr addrMask
)
402 if (!ObjectFile::loadSections(memPort
, addrMask
))
405 vector
<Segment
>::iterator extraIt
;
406 for (extraIt
= extraSegments
.begin();
407 extraIt
!= extraSegments
.end(); extraIt
++) {
408 if (!loadSection(&(*extraIt
), memPort
, addrMask
)) {
416 ElfObject::getSections()
419 int sec_idx
= 1; // there is a 0 but it is nothing, go figure
425 assert(!sectionNames
.size());
427 // check that header matches library version
428 if (elf_version(EV_CURRENT
) == EV_NONE
)
429 panic("wrong elf version number!");
431 // get a pointer to elf structure
432 elf
= elf_memory((char*)fileData
,len
);
435 // Check that we actually have a elf file
436 if (gelf_getehdr(elf
, &ehdr
) ==0) {
437 panic("Not ELF, shouldn't be here");
440 // Get the first section
441 section
= elf_getscn(elf
, sec_idx
);
443 // While there are no more sections
444 while (section
!= NULL
) {
445 gelf_getshdr(section
, &shdr
);
446 sectionNames
.insert(elf_strptr(elf
, ehdr
.e_shstrndx
, shdr
.sh_name
));
447 section
= elf_getscn(elf
, ++sec_idx
);
452 ElfObject::sectionExists(string sec
)
454 if (!sectionNames
.size())
456 return sectionNames
.find(sec
) != sectionNames
.end();