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