elf_object.cc revision 5070
112SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
312SN/A * All rights reserved.
412SN/A *
512SN/A * Redistribution and use in source and binary forms, with or without
612SN/A * modification, are permitted provided that the following conditions are
712SN/A * met: redistributions of source code must retain the above copyright
812SN/A * notice, this list of conditions and the following disclaimer;
912SN/A * redistributions in binary form must reproduce the above copyright
1012SN/A * notice, this list of conditions and the following disclaimer in the
1112SN/A * documentation and/or other materials provided with the distribution;
1212SN/A * neither the name of the copyright holders nor the names of its
1312SN/A * contributors may be used to endorse or promote products derived from
1412SN/A * this software without specific prior written permission.
1512SN/A *
1612SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Ali Saidi
3012SN/A */
3112SN/A
3212SN/A#include <string>
3312SN/A
342634Sstever@eecs.umich.edu#include "gelf.h"
35468SN/A
3656SN/A#include "base/loader/elf_object.hh"
374484Sbinkertn@umich.edu#include "base/loader/symtab.hh"
382439SN/A#include "base/misc.hh"
3956SN/A#include "base/trace.hh"	// for DPRINTF
402423SN/A#include "sim/byteswap.hh"
412423SN/A
4212SN/Ausing namespace std;
4312SN/A
4412SN/AObjectFile *
4512SN/AElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
4612SN/A{
47443SN/A    Elf *elf;
48443SN/A    GElf_Ehdr ehdr;
492207SN/A    Arch arch = UnknownArch;
502207SN/A    OpSys opSys = UnknownOpSys;
51443SN/A
52468SN/A    // check that header matches library version
531708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
541708SN/A        panic("wrong elf version number!");
55443SN/A
56468SN/A    // get a pointer to elf structure
57443SN/A    elf = elf_memory((char*)data,len);
58468SN/A    // will only fail if fd is invalid
59443SN/A    assert(elf != NULL);
60443SN/A
61468SN/A    // Check that we actually have a elf file
62468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
63443SN/A        DPRINTFR(Loader, "Not ELF\n");
64443SN/A        elf_end(elf);
65443SN/A        return NULL;
662476SN/A    } else {
672207SN/A        //Detect the architecture
682207SN/A        //Since we don't know how to check for alpha right now, we'll
692207SN/A        //just assume if it wasn't something else and it's 64 bit, that's
702207SN/A        //what it must be.
712207SN/A        if (ehdr.e_machine == EM_SPARC64 ||
724111Sgblack@eecs.umich.edu                (ehdr.e_machine == EM_SPARC &&
734111Sgblack@eecs.umich.edu                 ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
742620SN/A                ehdr.e_machine == EM_SPARCV9) {
754111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC64;
764111Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_SPARC32PLUS ||
774111Sgblack@eecs.umich.edu                        (ehdr.e_machine == EM_SPARC &&
784111Sgblack@eecs.umich.edu                         ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
794111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC32;
802207SN/A        } else if (ehdr.e_machine == EM_MIPS
812207SN/A                && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
822472SN/A            arch = ObjectFile::Mips;
834166Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_X86_64 &&
844166Sgblack@eecs.umich.edu                ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
854166Sgblack@eecs.umich.edu            //In the future, we might want to differentiate between 32 bit
864166Sgblack@eecs.umich.edu            //and 64 bit x86 processes in case there are differences in their
874166Sgblack@eecs.umich.edu            //initial stack frame.
884166Sgblack@eecs.umich.edu            arch = ObjectFile::X86;
892207SN/A        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
902207SN/A            arch = ObjectFile::Alpha;
912207SN/A        } else {
922600SN/A            warn("Unknown architecture: %d\n", ehdr.e_machine);
932207SN/A            arch = ObjectFile::UnknownArch;
942207SN/A        }
952207SN/A
962207SN/A        //Detect the operating system
972207SN/A        switch (ehdr.e_ident[EI_OSABI])
982207SN/A        {
992238SN/A
1002207SN/A          case ELFOSABI_LINUX:
1012207SN/A            opSys = ObjectFile::Linux;
1022207SN/A            break;
1032207SN/A          case ELFOSABI_SOLARIS:
1042207SN/A            opSys = ObjectFile::Solaris;
1052238SN/A            break;
1062207SN/A          case ELFOSABI_TRU64:
1072207SN/A            opSys = ObjectFile::Tru64;
1082238SN/A            break;
1092207SN/A          default:
1102207SN/A            opSys = ObjectFile::UnknownOpSys;
1112207SN/A        }
1122207SN/A
1132238SN/A        //take a look at the .note.ABI section
1142238SN/A        //It can let us know what's what.
1152600SN/A        if (opSys == ObjectFile::UnknownOpSys) {
1162238SN/A            Elf_Scn *section;
1172238SN/A            GElf_Shdr shdr;
1182238SN/A            Elf_Data *data;
1192238SN/A            uint32_t osAbi;;
1202238SN/A            int secIdx = 1;
1212238SN/A
1222238SN/A            // Get the first section
1232238SN/A            section = elf_getscn(elf, secIdx);
1242238SN/A
1252238SN/A            // While there are no more sections
1262600SN/A            while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
1272238SN/A                gelf_getshdr(section, &shdr);
1282238SN/A                if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
1292238SN/A                            elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
1302238SN/A                    // we have found a ABI note section
1312238SN/A                    // Check the 5th 32bit word for OS  0 == linux, 1 == hurd,
1322238SN/A                    // 2 == solaris, 3 == freebsd
1332238SN/A                    data = elf_rawdata(section, NULL);
1342238SN/A                    assert(data->d_buf);
1352238SN/A                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1362238SN/A                        osAbi = htole(((uint32_t*)data->d_buf)[4]);
1372238SN/A                    else
1382238SN/A                        osAbi = htobe(((uint32_t*)data->d_buf)[4]);
1392238SN/A
1402238SN/A                    switch(osAbi) {
1412238SN/A                      case 0:
1422238SN/A                        opSys = ObjectFile::Linux;
1432238SN/A                        break;
1442238SN/A                      case 2:
1452238SN/A                        opSys = ObjectFile::Solaris;
1462238SN/A                        break;
1472238SN/A                    }
1482238SN/A                } // if section found
1492600SN/A                if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1502600SN/A                        opSys = ObjectFile::Solaris;
1512600SN/A                if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1522600SN/A                        opSys = ObjectFile::Solaris;
1532600SN/A
1542238SN/A            section = elf_getscn(elf, ++secIdx);
1552238SN/A            } // while sections
1562238SN/A        }
1572472SN/A
1582976Sgblack@eecs.umich.edu        ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
1592976Sgblack@eecs.umich.edu
1602976Sgblack@eecs.umich.edu        //The number of headers in the file
1612976Sgblack@eecs.umich.edu        result->_programHeaderCount = ehdr.e_phnum;
1622976Sgblack@eecs.umich.edu        //Record the size of each entry
1632976Sgblack@eecs.umich.edu        result->_programHeaderSize = ehdr.e_phentsize;
1642976Sgblack@eecs.umich.edu        if(result->_programHeaderCount) //If there is a program header table
1652976Sgblack@eecs.umich.edu        {
1662976Sgblack@eecs.umich.edu            //Figure out the virtual address of the header table in the
1672976Sgblack@eecs.umich.edu            //final memory image. We use the program headers themselves
1682976Sgblack@eecs.umich.edu            //to translate from a file offset to the address in the image.
1692976Sgblack@eecs.umich.edu            GElf_Phdr phdr;
1702976Sgblack@eecs.umich.edu            uint64_t e_phoff = ehdr.e_phoff;
1712976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
1722976Sgblack@eecs.umich.edu            for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
1732976Sgblack@eecs.umich.edu            {
1742976Sgblack@eecs.umich.edu                gelf_getphdr(elf, hdrnum, &phdr);
1752976Sgblack@eecs.umich.edu                //Check if we've found the segment with the headers in it
1762976Sgblack@eecs.umich.edu                if(phdr.p_offset <= e_phoff &&
1772976Sgblack@eecs.umich.edu                        phdr.p_offset + phdr.p_filesz > e_phoff)
1782976Sgblack@eecs.umich.edu                {
1792976Sgblack@eecs.umich.edu                    result->_programHeaderTable = phdr.p_vaddr + e_phoff;
1802976Sgblack@eecs.umich.edu                    break;
1812976Sgblack@eecs.umich.edu                }
1822976Sgblack@eecs.umich.edu            }
1832976Sgblack@eecs.umich.edu        }
1842976Sgblack@eecs.umich.edu        else
1852976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
1862976Sgblack@eecs.umich.edu
1872976Sgblack@eecs.umich.edu
1882238SN/A        elf_end(elf);
1892976Sgblack@eecs.umich.edu        return result;
19012SN/A    }
19112SN/A}
19212SN/A
19312SN/A
19412SN/AElfObject::ElfObject(const string &_filename, int _fd,
195360SN/A                     size_t _len, uint8_t *_data,
196360SN/A                     Arch _arch, OpSys _opSys)
197360SN/A    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
198443SN/A
19912SN/A{
200443SN/A    Elf *elf;
201443SN/A    GElf_Ehdr ehdr;
20212SN/A
203468SN/A    // check that header matches library version
2041708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
2051708SN/A        panic("wrong elf version number!");
20612SN/A
207468SN/A    // get a pointer to elf structure
208443SN/A    elf = elf_memory((char*)fileData,len);
209468SN/A    // will only fail if fd is invalid
210443SN/A    assert(elf != NULL);
21112SN/A
212468SN/A    // Check that we actually have a elf file
213468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
214443SN/A        panic("Not ELF, shouldn't be here");
21512SN/A    }
21612SN/A
217468SN/A    entry = ehdr.e_entry;
21812SN/A
2192472SN/A
220468SN/A    // initialize segment sizes to 0 in case they're not present
221468SN/A    text.size = data.size = bss.size = 0;
222468SN/A
223468SN/A    for (int i = 0; i < ehdr.e_phnum; ++i) {
224468SN/A        GElf_Phdr phdr;
225468SN/A        if (gelf_getphdr(elf, i, &phdr) == 0) {
226468SN/A            panic("gelf_getphdr failed for section %d", i);
227468SN/A        }
228468SN/A
229468SN/A        // for now we don't care about non-loadable segments
230468SN/A        if (!(phdr.p_type & PT_LOAD))
231468SN/A            continue;
232468SN/A
233468SN/A        // the headers don't explicitly distinguish text from data,
234468SN/A        // but empirically the text segment comes first.
235468SN/A        if (text.size == 0) {  // haven't seen text segment yet
236468SN/A            text.baseAddr = phdr.p_vaddr;
237468SN/A            text.size = phdr.p_filesz;
2382420SN/A            text.fileImage = fileData + phdr.p_offset;
239468SN/A            // if there's any padding at the end that's not in the
240468SN/A            // file, call it the bss.  This happens in the "text"
241468SN/A            // segment if there's only one loadable segment (as for
242468SN/A            // kernel images).
243468SN/A            bss.size = phdr.p_memsz - phdr.p_filesz;
244468SN/A            bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
2452420SN/A            bss.fileImage = NULL;
2462476SN/A        } else if (data.size == 0) { // have text, this must be data
247468SN/A            data.baseAddr = phdr.p_vaddr;
248468SN/A            data.size = phdr.p_filesz;
2492420SN/A            data.fileImage = fileData + phdr.p_offset;
250468SN/A            // if there's any padding at the end that's not in the
251468SN/A            // file, call it the bss.  Warn if this happens for both
252468SN/A            // the text & data segments (should only have one bss).
253468SN/A            if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) {
254468SN/A                warn("Two implied bss segments in file!\n");
255468SN/A            }
256468SN/A            bss.size = phdr.p_memsz - phdr.p_filesz;
257468SN/A            bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
2582420SN/A            bss.fileImage = NULL;
2592476SN/A        } else {
2602476SN/A            warn("More than two loadable segments in ELF object.");
2612476SN/A            warn("Ignoring segment @ 0x%x length 0x%x.",
2622476SN/A                 phdr.p_vaddr, phdr.p_filesz);
263468SN/A        }
264468SN/A    }
265468SN/A
266468SN/A    // should have found at least one loadable segment
267468SN/A    assert(text.size != 0);
268468SN/A
269468SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
270468SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
271468SN/A             bss.baseAddr, bss.size);
272468SN/A
273443SN/A    elf_end(elf);
274443SN/A
275468SN/A    // We will actually read the sections when we need to load them
27612SN/A}
27712SN/A
27812SN/A
27912SN/Abool
280468SN/AElfObject::loadSomeSymbols(SymbolTable *symtab, int binding)
28112SN/A{
282443SN/A    Elf *elf;
283766SN/A    int sec_idx = 1; // there is a 0 but it is nothing, go figure
284443SN/A    Elf_Scn *section;
285443SN/A    GElf_Shdr shdr;
286443SN/A    Elf_Data *data;
287443SN/A    int count, ii;
288443SN/A    bool found = false;
289443SN/A    GElf_Sym sym;
290443SN/A
291443SN/A    if (!symtab)
292443SN/A        return false;
293443SN/A
294468SN/A    // check that header matches library version
2951708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
2961708SN/A        panic("wrong elf version number!");
297443SN/A
298468SN/A    // get a pointer to elf structure
299443SN/A    elf = elf_memory((char*)fileData,len);
300443SN/A
301443SN/A    assert(elf != NULL);
302443SN/A
303468SN/A    // Get the first section
304454SN/A    section = elf_getscn(elf, sec_idx);
305443SN/A
306468SN/A    // While there are no more sections
307468SN/A    while (section != NULL) {
308443SN/A        gelf_getshdr(section, &shdr);
309443SN/A
310468SN/A        if (shdr.sh_type == SHT_SYMTAB) {
311443SN/A            found = true;
312443SN/A            data = elf_getdata(section, NULL);
313443SN/A            count = shdr.sh_size / shdr.sh_entsize;
314443SN/A            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
315443SN/A
316468SN/A            // loop through all the symbols, only loading global ones
317468SN/A            for (ii = 0; ii < count; ++ii) {
318443SN/A                gelf_getsym(data, ii, &sym);
319836SN/A                if (GELF_ST_BIND(sym.st_info) == binding) {
320468SN/A                   symtab->insert(sym.st_value,
321468SN/A                                  elf_strptr(elf, shdr.sh_link, sym.st_name));
322443SN/A                }
323443SN/A            }
324443SN/A        }
325454SN/A        ++sec_idx;
326454SN/A        section = elf_getscn(elf, sec_idx);
327443SN/A    }
328443SN/A
329443SN/A    elf_end(elf);
330443SN/A
331443SN/A    return found;
33212SN/A}
33312SN/A
33412SN/Abool
3353812Ssaidi@eecs.umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
336468SN/A{
337468SN/A    return loadSomeSymbols(symtab, STB_GLOBAL);
338468SN/A}
339468SN/A
340468SN/Abool
3413812Ssaidi@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
34212SN/A{
343468SN/A    return loadSomeSymbols(symtab, STB_LOCAL);
34412SN/A}
3453917Ssaidi@eecs.umich.edu
3465070Ssaidi@eecs.umich.eduvoid
3475070Ssaidi@eecs.umich.eduElfObject::getSections()
3483917Ssaidi@eecs.umich.edu{
3493917Ssaidi@eecs.umich.edu    Elf *elf;
3503917Ssaidi@eecs.umich.edu    int sec_idx = 1; // there is a 0 but it is nothing, go figure
3513917Ssaidi@eecs.umich.edu    Elf_Scn *section;
3523917Ssaidi@eecs.umich.edu    GElf_Shdr shdr;
3533917Ssaidi@eecs.umich.edu
3543917Ssaidi@eecs.umich.edu    GElf_Ehdr ehdr;
3553917Ssaidi@eecs.umich.edu
3565070Ssaidi@eecs.umich.edu    assert(!sectionNames.size());
3575070Ssaidi@eecs.umich.edu
3583917Ssaidi@eecs.umich.edu    // check that header matches library version
3593917Ssaidi@eecs.umich.edu    if (elf_version(EV_CURRENT) == EV_NONE)
3603917Ssaidi@eecs.umich.edu        panic("wrong elf version number!");
3613917Ssaidi@eecs.umich.edu
3623917Ssaidi@eecs.umich.edu    // get a pointer to elf structure
3633917Ssaidi@eecs.umich.edu    elf = elf_memory((char*)fileData,len);
3643917Ssaidi@eecs.umich.edu    assert(elf != NULL);
3653917Ssaidi@eecs.umich.edu
3663917Ssaidi@eecs.umich.edu    // Check that we actually have a elf file
3673917Ssaidi@eecs.umich.edu    if (gelf_getehdr(elf, &ehdr) ==0) {
3683917Ssaidi@eecs.umich.edu        panic("Not ELF, shouldn't be here");
3693917Ssaidi@eecs.umich.edu    }
3703917Ssaidi@eecs.umich.edu
3713917Ssaidi@eecs.umich.edu    // Get the first section
3723917Ssaidi@eecs.umich.edu    section = elf_getscn(elf, sec_idx);
3733917Ssaidi@eecs.umich.edu
3743917Ssaidi@eecs.umich.edu    // While there are no more sections
3753917Ssaidi@eecs.umich.edu    while (section != NULL) {
3763917Ssaidi@eecs.umich.edu        gelf_getshdr(section, &shdr);
3775070Ssaidi@eecs.umich.edu        sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
3783917Ssaidi@eecs.umich.edu        section = elf_getscn(elf, ++sec_idx);
3793917Ssaidi@eecs.umich.edu    } // while sections
3803917Ssaidi@eecs.umich.edu}
3813917Ssaidi@eecs.umich.edu
3825070Ssaidi@eecs.umich.edubool
3835070Ssaidi@eecs.umich.eduElfObject::sectionExists(string sec)
3845070Ssaidi@eecs.umich.edu{
3855070Ssaidi@eecs.umich.edu    if (!sectionNames.size())
3865070Ssaidi@eecs.umich.edu        getSections();
3875070Ssaidi@eecs.umich.edu    return sectionNames.find(sec) != sectionNames.end();
3885070Ssaidi@eecs.umich.edu}
3893917Ssaidi@eecs.umich.edu
3905070Ssaidi@eecs.umich.edu
391