elf_object.cc revision 6392
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
325616Snate@binkert.org#include <cassert>
3312SN/A#include <string>
3412SN/A
352634Sstever@eecs.umich.edu#include "gelf.h"
36468SN/A
3756SN/A#include "base/loader/elf_object.hh"
384484Sbinkertn@umich.edu#include "base/loader/symtab.hh"
392439SN/A#include "base/misc.hh"
405543Ssaidi@eecs.umich.edu#include "base/trace.hh"        // for DPRINTF
412423SN/A#include "sim/byteswap.hh"
422423SN/A
4312SN/Ausing namespace std;
4412SN/A
4512SN/AObjectFile *
4612SN/AElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
4712SN/A{
48443SN/A    Elf *elf;
49443SN/A    GElf_Ehdr ehdr;
502207SN/A    Arch arch = UnknownArch;
512207SN/A    OpSys opSys = UnknownOpSys;
52443SN/A
53468SN/A    // check that header matches library version
541708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
551708SN/A        panic("wrong elf version number!");
56443SN/A
57468SN/A    // get a pointer to elf structure
58443SN/A    elf = elf_memory((char*)data,len);
59468SN/A    // will only fail if fd is invalid
60443SN/A    assert(elf != NULL);
61443SN/A
62468SN/A    // Check that we actually have a elf file
63468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
64443SN/A        DPRINTFR(Loader, "Not ELF\n");
65443SN/A        elf_end(elf);
66443SN/A        return NULL;
672476SN/A    } else {
682207SN/A        //Detect the architecture
692207SN/A        //Since we don't know how to check for alpha right now, we'll
702207SN/A        //just assume if it wasn't something else and it's 64 bit, that's
712207SN/A        //what it must be.
722207SN/A        if (ehdr.e_machine == EM_SPARC64 ||
734111Sgblack@eecs.umich.edu                (ehdr.e_machine == EM_SPARC &&
744111Sgblack@eecs.umich.edu                 ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
752620SN/A                ehdr.e_machine == EM_SPARCV9) {
764111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC64;
774111Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_SPARC32PLUS ||
784111Sgblack@eecs.umich.edu                        (ehdr.e_machine == EM_SPARC &&
794111Sgblack@eecs.umich.edu                         ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
804111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC32;
812207SN/A        } else if (ehdr.e_machine == EM_MIPS
822207SN/A                && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
835383Sgblack@eecs.umich.edu            if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
845383Sgblack@eecs.umich.edu                arch = ObjectFile::Mips;
855383Sgblack@eecs.umich.edu            } else {
865383Sgblack@eecs.umich.edu                fatal("The binary you're trying to load is compiled for big "
875383Sgblack@eecs.umich.edu                        "endian MIPS. M5\nonly supports little endian MIPS. "
885383Sgblack@eecs.umich.edu                        "Please recompile your binary.\n");
895383Sgblack@eecs.umich.edu            }
904166Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_X86_64 &&
914166Sgblack@eecs.umich.edu                ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
925874Sgblack@eecs.umich.edu            arch = ObjectFile::X86_64;
935874Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_386 &&
945874Sgblack@eecs.umich.edu                ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
955874Sgblack@eecs.umich.edu            arch = ObjectFile::I386;
962207SN/A        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
972207SN/A            arch = ObjectFile::Alpha;
985335Shines@cs.fsu.edu        } else if (ehdr.e_machine == EM_ARM) {
995335Shines@cs.fsu.edu            arch = ObjectFile::Arm;
1002207SN/A        } else {
1012600SN/A            warn("Unknown architecture: %d\n", ehdr.e_machine);
1022207SN/A            arch = ObjectFile::UnknownArch;
1032207SN/A        }
1042207SN/A
1052207SN/A        //Detect the operating system
1062207SN/A        switch (ehdr.e_ident[EI_OSABI])
1072207SN/A        {
1082238SN/A
1092207SN/A          case ELFOSABI_LINUX:
1102207SN/A            opSys = ObjectFile::Linux;
1112207SN/A            break;
1122207SN/A          case ELFOSABI_SOLARIS:
1132207SN/A            opSys = ObjectFile::Solaris;
1142238SN/A            break;
1152207SN/A          case ELFOSABI_TRU64:
1162207SN/A            opSys = ObjectFile::Tru64;
1172238SN/A            break;
1186392Ssaidi@eecs.umich.edu          case ELFOSABI_ARM:
1196392Ssaidi@eecs.umich.edu            opSys = ObjectFile::LinuxArmOABI;
1206392Ssaidi@eecs.umich.edu            break;
1212207SN/A          default:
1222207SN/A            opSys = ObjectFile::UnknownOpSys;
1232207SN/A        }
1242207SN/A
1252238SN/A        //take a look at the .note.ABI section
1262238SN/A        //It can let us know what's what.
1272600SN/A        if (opSys == ObjectFile::UnknownOpSys) {
1282238SN/A            Elf_Scn *section;
1292238SN/A            GElf_Shdr shdr;
1302238SN/A            Elf_Data *data;
1312238SN/A            uint32_t osAbi;;
1322238SN/A            int secIdx = 1;
1332238SN/A
1342238SN/A            // Get the first section
1352238SN/A            section = elf_getscn(elf, secIdx);
1362238SN/A
1372238SN/A            // While there are no more sections
1382600SN/A            while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
1392238SN/A                gelf_getshdr(section, &shdr);
1402238SN/A                if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
1412238SN/A                            elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
1422238SN/A                    // we have found a ABI note section
1432238SN/A                    // Check the 5th 32bit word for OS  0 == linux, 1 == hurd,
1442238SN/A                    // 2 == solaris, 3 == freebsd
1452238SN/A                    data = elf_rawdata(section, NULL);
1462238SN/A                    assert(data->d_buf);
1472238SN/A                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1482238SN/A                        osAbi = htole(((uint32_t*)data->d_buf)[4]);
1492238SN/A                    else
1502238SN/A                        osAbi = htobe(((uint32_t*)data->d_buf)[4]);
1512238SN/A
1522238SN/A                    switch(osAbi) {
1532238SN/A                      case 0:
1542238SN/A                        opSys = ObjectFile::Linux;
1552238SN/A                        break;
1562238SN/A                      case 2:
1572238SN/A                        opSys = ObjectFile::Solaris;
1582238SN/A                        break;
1592238SN/A                    }
1602238SN/A                } // if section found
1612600SN/A                if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1622600SN/A                        opSys = ObjectFile::Solaris;
1632600SN/A                if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1642600SN/A                        opSys = ObjectFile::Solaris;
1652600SN/A
1662238SN/A            section = elf_getscn(elf, ++secIdx);
1672238SN/A            } // while sections
1682238SN/A        }
1692472SN/A
1702976Sgblack@eecs.umich.edu        ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
1712976Sgblack@eecs.umich.edu
1722976Sgblack@eecs.umich.edu        //The number of headers in the file
1732976Sgblack@eecs.umich.edu        result->_programHeaderCount = ehdr.e_phnum;
1742976Sgblack@eecs.umich.edu        //Record the size of each entry
1752976Sgblack@eecs.umich.edu        result->_programHeaderSize = ehdr.e_phentsize;
1762976Sgblack@eecs.umich.edu        if(result->_programHeaderCount) //If there is a program header table
1772976Sgblack@eecs.umich.edu        {
1782976Sgblack@eecs.umich.edu            //Figure out the virtual address of the header table in the
1792976Sgblack@eecs.umich.edu            //final memory image. We use the program headers themselves
1802976Sgblack@eecs.umich.edu            //to translate from a file offset to the address in the image.
1812976Sgblack@eecs.umich.edu            GElf_Phdr phdr;
1822976Sgblack@eecs.umich.edu            uint64_t e_phoff = ehdr.e_phoff;
1832976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
1842976Sgblack@eecs.umich.edu            for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
1852976Sgblack@eecs.umich.edu            {
1862976Sgblack@eecs.umich.edu                gelf_getphdr(elf, hdrnum, &phdr);
1872976Sgblack@eecs.umich.edu                //Check if we've found the segment with the headers in it
1882976Sgblack@eecs.umich.edu                if(phdr.p_offset <= e_phoff &&
1892976Sgblack@eecs.umich.edu                        phdr.p_offset + phdr.p_filesz > e_phoff)
1902976Sgblack@eecs.umich.edu                {
1915143Sgblack@eecs.umich.edu                    result->_programHeaderTable = phdr.p_paddr + e_phoff;
1922976Sgblack@eecs.umich.edu                    break;
1932976Sgblack@eecs.umich.edu                }
1942976Sgblack@eecs.umich.edu            }
1952976Sgblack@eecs.umich.edu        }
1962976Sgblack@eecs.umich.edu        else
1972976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
1982976Sgblack@eecs.umich.edu
1992976Sgblack@eecs.umich.edu
2002238SN/A        elf_end(elf);
2012976Sgblack@eecs.umich.edu        return result;
20212SN/A    }
20312SN/A}
20412SN/A
20512SN/A
20612SN/AElfObject::ElfObject(const string &_filename, int _fd,
207360SN/A                     size_t _len, uint8_t *_data,
208360SN/A                     Arch _arch, OpSys _opSys)
209360SN/A    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
210443SN/A
21112SN/A{
212443SN/A    Elf *elf;
213443SN/A    GElf_Ehdr ehdr;
21412SN/A
215468SN/A    // check that header matches library version
2161708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
2171708SN/A        panic("wrong elf version number!");
21812SN/A
219468SN/A    // get a pointer to elf structure
220443SN/A    elf = elf_memory((char*)fileData,len);
221468SN/A    // will only fail if fd is invalid
222443SN/A    assert(elf != NULL);
22312SN/A
224468SN/A    // Check that we actually have a elf file
225468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
226443SN/A        panic("Not ELF, shouldn't be here");
22712SN/A    }
22812SN/A
229468SN/A    entry = ehdr.e_entry;
23012SN/A
231468SN/A    // initialize segment sizes to 0 in case they're not present
232468SN/A    text.size = data.size = bss.size = 0;
233468SN/A
2345090Sgblack@eecs.umich.edu    int secIdx = 1;
2355090Sgblack@eecs.umich.edu    Elf_Scn *section;
2365090Sgblack@eecs.umich.edu    GElf_Shdr shdr;
2375090Sgblack@eecs.umich.edu
2385090Sgblack@eecs.umich.edu    // The first address of some important sections.
2395090Sgblack@eecs.umich.edu    Addr textSecStart = 0;
2405090Sgblack@eecs.umich.edu    Addr dataSecStart = 0;
2415090Sgblack@eecs.umich.edu    Addr bssSecStart = 0;
2425090Sgblack@eecs.umich.edu
2435090Sgblack@eecs.umich.edu    // Get the first section
2445090Sgblack@eecs.umich.edu    section = elf_getscn(elf, secIdx);
2455090Sgblack@eecs.umich.edu
2465090Sgblack@eecs.umich.edu    // Find the beginning of the most interesting sections.
2475090Sgblack@eecs.umich.edu    while (section != NULL) {
2485090Sgblack@eecs.umich.edu        gelf_getshdr(section, &shdr);
2495090Sgblack@eecs.umich.edu        char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
2505090Sgblack@eecs.umich.edu
2515090Sgblack@eecs.umich.edu        if (!strcmp(".text", secName)) {
2525090Sgblack@eecs.umich.edu            textSecStart = shdr.sh_addr;
2535090Sgblack@eecs.umich.edu        } else if (!strcmp(".data", secName)) {
2545090Sgblack@eecs.umich.edu            dataSecStart = shdr.sh_addr;
2555090Sgblack@eecs.umich.edu        } else if (!strcmp(".bss", secName)) {
2565090Sgblack@eecs.umich.edu            bssSecStart = shdr.sh_addr;
2575090Sgblack@eecs.umich.edu        }
2585090Sgblack@eecs.umich.edu
2595090Sgblack@eecs.umich.edu        section = elf_getscn(elf, ++secIdx);
2605090Sgblack@eecs.umich.edu    }
2615090Sgblack@eecs.umich.edu
2625090Sgblack@eecs.umich.edu    // Go through all the segments in the program, record them, and scrape
2635090Sgblack@eecs.umich.edu    // out information about the text, data, and bss areas needed by other
2645090Sgblack@eecs.umich.edu    // code.
265468SN/A    for (int i = 0; i < ehdr.e_phnum; ++i) {
266468SN/A        GElf_Phdr phdr;
267468SN/A        if (gelf_getphdr(elf, i, &phdr) == 0) {
2685090Sgblack@eecs.umich.edu            panic("gelf_getphdr failed for segment %d.", i);
269468SN/A        }
270468SN/A
271468SN/A        // for now we don't care about non-loadable segments
272468SN/A        if (!(phdr.p_type & PT_LOAD))
273468SN/A            continue;
274468SN/A
2755090Sgblack@eecs.umich.edu        // Check to see if this segment contains the bss section.
2765143Sgblack@eecs.umich.edu        if (phdr.p_paddr <= bssSecStart &&
2775143Sgblack@eecs.umich.edu                phdr.p_paddr + phdr.p_memsz > bssSecStart &&
2785090Sgblack@eecs.umich.edu                phdr.p_memsz - phdr.p_filesz > 0) {
2795143Sgblack@eecs.umich.edu            bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
2805090Sgblack@eecs.umich.edu            bss.size = phdr.p_memsz - phdr.p_filesz;
2815090Sgblack@eecs.umich.edu            bss.fileImage = NULL;
2825090Sgblack@eecs.umich.edu        }
2835090Sgblack@eecs.umich.edu
2845090Sgblack@eecs.umich.edu        // Check to see if this is the text or data segment
2855152Sgblack@eecs.umich.edu        if (phdr.p_vaddr <= textSecStart &&
2865152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > textSecStart) {
2875143Sgblack@eecs.umich.edu            text.baseAddr = phdr.p_paddr;
288468SN/A            text.size = phdr.p_filesz;
2892420SN/A            text.fileImage = fileData + phdr.p_offset;
2905152Sgblack@eecs.umich.edu        } else if (phdr.p_vaddr <= dataSecStart &&
2915152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
2925143Sgblack@eecs.umich.edu            data.baseAddr = phdr.p_paddr;
293468SN/A            data.size = phdr.p_filesz;
2942420SN/A            data.fileImage = fileData + phdr.p_offset;
2952476SN/A        } else {
2965759Shsul@eecs.umich.edu            // If it's none of the above but is loadable,
2975759Shsul@eecs.umich.edu            // load the filesize worth of data
2985090Sgblack@eecs.umich.edu            Segment extra;
2995143Sgblack@eecs.umich.edu            extra.baseAddr = phdr.p_paddr;
3005090Sgblack@eecs.umich.edu            extra.size = phdr.p_filesz;
3015090Sgblack@eecs.umich.edu            extra.fileImage = fileData + phdr.p_offset;
3025090Sgblack@eecs.umich.edu            extraSegments.push_back(extra);
303468SN/A        }
304468SN/A    }
305468SN/A
306468SN/A    // should have found at least one loadable segment
307468SN/A    assert(text.size != 0);
308468SN/A
309468SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
310468SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
311468SN/A             bss.baseAddr, bss.size);
312468SN/A
313443SN/A    elf_end(elf);
314443SN/A
315468SN/A    // We will actually read the sections when we need to load them
31612SN/A}
31712SN/A
31812SN/A
31912SN/Abool
320468SN/AElfObject::loadSomeSymbols(SymbolTable *symtab, int binding)
32112SN/A{
322443SN/A    Elf *elf;
323766SN/A    int sec_idx = 1; // there is a 0 but it is nothing, go figure
324443SN/A    Elf_Scn *section;
325443SN/A    GElf_Shdr shdr;
326443SN/A    Elf_Data *data;
327443SN/A    int count, ii;
328443SN/A    bool found = false;
329443SN/A    GElf_Sym sym;
330443SN/A
331443SN/A    if (!symtab)
332443SN/A        return false;
333443SN/A
334468SN/A    // check that header matches library version
3351708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
3361708SN/A        panic("wrong elf version number!");
337443SN/A
338468SN/A    // get a pointer to elf structure
339443SN/A    elf = elf_memory((char*)fileData,len);
340443SN/A
341443SN/A    assert(elf != NULL);
342443SN/A
343468SN/A    // Get the first section
344454SN/A    section = elf_getscn(elf, sec_idx);
345443SN/A
346468SN/A    // While there are no more sections
347468SN/A    while (section != NULL) {
348443SN/A        gelf_getshdr(section, &shdr);
349443SN/A
350468SN/A        if (shdr.sh_type == SHT_SYMTAB) {
351443SN/A            found = true;
352443SN/A            data = elf_getdata(section, NULL);
353443SN/A            count = shdr.sh_size / shdr.sh_entsize;
354443SN/A            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
355443SN/A
356468SN/A            // loop through all the symbols, only loading global ones
357468SN/A            for (ii = 0; ii < count; ++ii) {
358443SN/A                gelf_getsym(data, ii, &sym);
359836SN/A                if (GELF_ST_BIND(sym.st_info) == binding) {
360468SN/A                   symtab->insert(sym.st_value,
361468SN/A                                  elf_strptr(elf, shdr.sh_link, sym.st_name));
362443SN/A                }
363443SN/A            }
364443SN/A        }
365454SN/A        ++sec_idx;
366454SN/A        section = elf_getscn(elf, sec_idx);
367443SN/A    }
368443SN/A
369443SN/A    elf_end(elf);
370443SN/A
371443SN/A    return found;
37212SN/A}
37312SN/A
37412SN/Abool
3753812Ssaidi@eecs.umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
376468SN/A{
377468SN/A    return loadSomeSymbols(symtab, STB_GLOBAL);
378468SN/A}
379468SN/A
380468SN/Abool
3813812Ssaidi@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
38212SN/A{
383468SN/A    return loadSomeSymbols(symtab, STB_LOCAL);
38412SN/A}
3853917Ssaidi@eecs.umich.edu
3865090Sgblack@eecs.umich.edubool
3875090Sgblack@eecs.umich.eduElfObject::loadSections(Port *memPort, Addr addrMask)
3885090Sgblack@eecs.umich.edu{
3895090Sgblack@eecs.umich.edu    if (!ObjectFile::loadSections(memPort, addrMask))
3905090Sgblack@eecs.umich.edu        return false;
3915090Sgblack@eecs.umich.edu
3925090Sgblack@eecs.umich.edu    vector<Segment>::iterator extraIt;
3935090Sgblack@eecs.umich.edu    for (extraIt = extraSegments.begin();
3945090Sgblack@eecs.umich.edu            extraIt != extraSegments.end(); extraIt++) {
3955090Sgblack@eecs.umich.edu        if (!loadSection(&(*extraIt), memPort, addrMask)) {
3965090Sgblack@eecs.umich.edu            return false;
3975090Sgblack@eecs.umich.edu        }
3985090Sgblack@eecs.umich.edu    }
3995090Sgblack@eecs.umich.edu    return true;
4005090Sgblack@eecs.umich.edu}
4015090Sgblack@eecs.umich.edu
4025070Ssaidi@eecs.umich.eduvoid
4035070Ssaidi@eecs.umich.eduElfObject::getSections()
4043917Ssaidi@eecs.umich.edu{
4053917Ssaidi@eecs.umich.edu    Elf *elf;
4063917Ssaidi@eecs.umich.edu    int sec_idx = 1; // there is a 0 but it is nothing, go figure
4073917Ssaidi@eecs.umich.edu    Elf_Scn *section;
4083917Ssaidi@eecs.umich.edu    GElf_Shdr shdr;
4093917Ssaidi@eecs.umich.edu
4103917Ssaidi@eecs.umich.edu    GElf_Ehdr ehdr;
4113917Ssaidi@eecs.umich.edu
4125070Ssaidi@eecs.umich.edu    assert(!sectionNames.size());
4135070Ssaidi@eecs.umich.edu
4143917Ssaidi@eecs.umich.edu    // check that header matches library version
4153917Ssaidi@eecs.umich.edu    if (elf_version(EV_CURRENT) == EV_NONE)
4163917Ssaidi@eecs.umich.edu        panic("wrong elf version number!");
4173917Ssaidi@eecs.umich.edu
4183917Ssaidi@eecs.umich.edu    // get a pointer to elf structure
4193917Ssaidi@eecs.umich.edu    elf = elf_memory((char*)fileData,len);
4203917Ssaidi@eecs.umich.edu    assert(elf != NULL);
4213917Ssaidi@eecs.umich.edu
4223917Ssaidi@eecs.umich.edu    // Check that we actually have a elf file
4233917Ssaidi@eecs.umich.edu    if (gelf_getehdr(elf, &ehdr) ==0) {
4243917Ssaidi@eecs.umich.edu        panic("Not ELF, shouldn't be here");
4253917Ssaidi@eecs.umich.edu    }
4263917Ssaidi@eecs.umich.edu
4273917Ssaidi@eecs.umich.edu    // Get the first section
4283917Ssaidi@eecs.umich.edu    section = elf_getscn(elf, sec_idx);
4293917Ssaidi@eecs.umich.edu
4303917Ssaidi@eecs.umich.edu    // While there are no more sections
4313917Ssaidi@eecs.umich.edu    while (section != NULL) {
4323917Ssaidi@eecs.umich.edu        gelf_getshdr(section, &shdr);
4335070Ssaidi@eecs.umich.edu        sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
4343917Ssaidi@eecs.umich.edu        section = elf_getscn(elf, ++sec_idx);
4353917Ssaidi@eecs.umich.edu    } // while sections
4363917Ssaidi@eecs.umich.edu}
4373917Ssaidi@eecs.umich.edu
4385070Ssaidi@eecs.umich.edubool
4395070Ssaidi@eecs.umich.eduElfObject::sectionExists(string sec)
4405070Ssaidi@eecs.umich.edu{
4415070Ssaidi@eecs.umich.edu    if (!sectionNames.size())
4425070Ssaidi@eecs.umich.edu        getSections();
4435070Ssaidi@eecs.umich.edu    return sectionNames.find(sec) != sectionNames.end();
4445070Ssaidi@eecs.umich.edu}
4453917Ssaidi@eecs.umich.edu
4465070Ssaidi@eecs.umich.edu
447