b56dc5aa6be5d326a2276b3c399a06a439d5a8fb
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
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
41 #include "base/loader/elf_object.hh"
42 #include "base/misc.hh"
44 #include "base/loader/symtab.hh"
46 #include "base/trace.hh" // for DPRINTF
48 #include "sim/byteswap.hh"
54 ElfObject::tryFile(const string
&fname
, int fd
, size_t len
, uint8_t *data
)
58 Arch arch
= UnknownArch
;
59 OpSys opSys
= UnknownOpSys
;
61 // check that header matches library version
62 if (elf_version(EV_CURRENT
) == EV_NONE
)
63 panic("wrong elf version number!");
65 // get a pointer to elf structure
66 elf
= elf_memory((char*)data
,len
);
67 // will only fail if fd is invalid
70 // Check that we actually have a elf file
71 if (gelf_getehdr(elf
, &ehdr
) ==0) {
72 DPRINTFR(Loader
, "Not ELF\n");
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
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
;
95 warn("Unknown architecture: %d\n", ehdr
.e_machine
);
96 arch
= ObjectFile::UnknownArch
;
99 //Detect the operating system
100 switch (ehdr
.e_ident
[EI_OSABI
])
104 opSys
= ObjectFile::Linux
;
106 case ELFOSABI_SOLARIS
:
107 opSys
= ObjectFile::Solaris
;
110 opSys
= ObjectFile::Tru64
;
113 opSys
= ObjectFile::UnknownOpSys
;
116 //take a look at the .note.ABI section
117 //It can let us know what's what.
118 if (opSys
== ObjectFile::UnknownOpSys
) {
125 // Get the first section
126 section
= elf_getscn(elf
, secIdx
);
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
);
138 if(ehdr
.e_ident
[EI_DATA
] == ELFDATA2LSB
)
139 osAbi
= htole(((uint32_t*)data
->d_buf
)[4]);
141 osAbi
= htobe(((uint32_t*)data
->d_buf
)[4]);
145 opSys
= ObjectFile::Linux
;
148 opSys
= ObjectFile::Solaris
;
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
;
157 section
= elf_getscn(elf
, ++secIdx
);
161 ElfObject
* result
= new ElfObject(fname
, fd
, len
, data
, arch
, opSys
);
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
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.
173 uint64_t e_phoff
= ehdr
.e_phoff
;
174 result
->_programHeaderTable
= 0;
175 for(int hdrnum
= 0; hdrnum
< result
->_programHeaderCount
; hdrnum
++)
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
)
182 result
->_programHeaderTable
= phdr
.p_vaddr
+ e_phoff
;
188 result
->_programHeaderTable
= 0;
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
)
206 // check that header matches library version
207 if (elf_version(EV_CURRENT
) == EV_NONE
)
208 panic("wrong elf version number!");
210 // get a pointer to elf structure
211 elf
= elf_memory((char*)fileData
,len
);
212 // will only fail if fd is invalid
215 // Check that we actually have a elf file
216 if (gelf_getehdr(elf
, &ehdr
) ==0) {
217 panic("Not ELF, shouldn't be here");
220 entry
= ehdr
.e_entry
;
223 // initialize segment sizes to 0 in case they're not present
224 text
.size
= data
.size
= bss
.size
= 0;
226 for (int i
= 0; i
< ehdr
.e_phnum
; ++i
) {
228 if (gelf_getphdr(elf
, i
, &phdr
) == 0) {
229 panic("gelf_getphdr failed for section %d", i
);
232 // for now we don't care about non-loadable segments
233 if (!(phdr
.p_type
& PT_LOAD
))
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
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");
259 bss
.size
= phdr
.p_memsz
- phdr
.p_filesz
;
260 bss
.baseAddr
= phdr
.p_vaddr
+ phdr
.p_filesz
;
261 bss
.fileImage
= NULL
;
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
);
269 // should have found at least one loadable segment
270 assert(text
.size
!= 0);
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
);
278 // We will actually read the sections when we need to load them
283 ElfObject::loadSomeSymbols(SymbolTable
*symtab
, int binding
)
286 int sec_idx
= 1; // there is a 0 but it is nothing, go figure
297 // check that header matches library version
298 if (elf_version(EV_CURRENT
) == EV_NONE
)
299 panic("wrong elf version number!");
301 // get a pointer to elf structure
302 elf
= elf_memory((char*)fileData
,len
);
306 // Get the first section
307 section
= elf_getscn(elf
, sec_idx
);
309 // While there are no more sections
310 while (section
!= NULL
) {
311 gelf_getshdr(section
, &shdr
);
313 if (shdr
.sh_type
== SHT_SYMTAB
) {
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
);
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
));
329 section
= elf_getscn(elf
, sec_idx
);
338 ElfObject::loadGlobalSymbols(SymbolTable
*symtab
, Addr addrMask
)
340 return loadSomeSymbols(symtab
, STB_GLOBAL
);
344 ElfObject::loadLocalSymbols(SymbolTable
*symtab
, Addr addrMask
)
346 return loadSomeSymbols(symtab
, STB_LOCAL
);
350 ElfObject::isDynamic()
353 int sec_idx
= 1; // there is a 0 but it is nothing, go figure
359 // check that header matches library version
360 if (elf_version(EV_CURRENT
) == EV_NONE
)
361 panic("wrong elf version number!");
363 // get a pointer to elf structure
364 elf
= elf_memory((char*)fileData
,len
);
367 // Check that we actually have a elf file
368 if (gelf_getehdr(elf
, &ehdr
) ==0) {
369 panic("Not ELF, shouldn't be here");
372 // Get the first section
373 section
= elf_getscn(elf
, sec_idx
);
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
)))
380 section
= elf_getscn(elf
, ++sec_idx
);