elf_object.cc revision 5543
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"
395543Ssaidi@eecs.umich.edu#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) {
825383Sgblack@eecs.umich.edu            if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
835383Sgblack@eecs.umich.edu                arch = ObjectFile::Mips;
845383Sgblack@eecs.umich.edu            } else {
855383Sgblack@eecs.umich.edu                fatal("The binary you're trying to load is compiled for big "
865383Sgblack@eecs.umich.edu                        "endian MIPS. M5\nonly supports little endian MIPS. "
875383Sgblack@eecs.umich.edu                        "Please recompile your binary.\n");
885383Sgblack@eecs.umich.edu            }
894166Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_X86_64 &&
904166Sgblack@eecs.umich.edu                ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
914166Sgblack@eecs.umich.edu            //In the future, we might want to differentiate between 32 bit
924166Sgblack@eecs.umich.edu            //and 64 bit x86 processes in case there are differences in their
934166Sgblack@eecs.umich.edu            //initial stack frame.
944166Sgblack@eecs.umich.edu            arch = ObjectFile::X86;
952207SN/A        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
962207SN/A            arch = ObjectFile::Alpha;
975335Shines@cs.fsu.edu        } else if (ehdr.e_machine == EM_ARM) {
985335Shines@cs.fsu.edu            arch = ObjectFile::Arm;
992207SN/A        } else {
1002600SN/A            warn("Unknown architecture: %d\n", ehdr.e_machine);
1012207SN/A            arch = ObjectFile::UnknownArch;
1022207SN/A        }
1032207SN/A
1042207SN/A        //Detect the operating system
1052207SN/A        switch (ehdr.e_ident[EI_OSABI])
1062207SN/A        {
1072238SN/A
1082207SN/A          case ELFOSABI_LINUX:
1095335Shines@cs.fsu.edu          case ELFOSABI_ARM:
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;
1182207SN/A          default:
1192207SN/A            opSys = ObjectFile::UnknownOpSys;
1202207SN/A        }
1212207SN/A
1222238SN/A        //take a look at the .note.ABI section
1232238SN/A        //It can let us know what's what.
1242600SN/A        if (opSys == ObjectFile::UnknownOpSys) {
1252238SN/A            Elf_Scn *section;
1262238SN/A            GElf_Shdr shdr;
1272238SN/A            Elf_Data *data;
1282238SN/A            uint32_t osAbi;;
1292238SN/A            int secIdx = 1;
1302238SN/A
1312238SN/A            // Get the first section
1322238SN/A            section = elf_getscn(elf, secIdx);
1332238SN/A
1342238SN/A            // While there are no more sections
1352600SN/A            while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
1362238SN/A                gelf_getshdr(section, &shdr);
1372238SN/A                if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
1382238SN/A                            elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
1392238SN/A                    // we have found a ABI note section
1402238SN/A                    // Check the 5th 32bit word for OS  0 == linux, 1 == hurd,
1412238SN/A                    // 2 == solaris, 3 == freebsd
1422238SN/A                    data = elf_rawdata(section, NULL);
1432238SN/A                    assert(data->d_buf);
1442238SN/A                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1452238SN/A                        osAbi = htole(((uint32_t*)data->d_buf)[4]);
1462238SN/A                    else
1472238SN/A                        osAbi = htobe(((uint32_t*)data->d_buf)[4]);
1482238SN/A
1492238SN/A                    switch(osAbi) {
1502238SN/A                      case 0:
1512238SN/A                        opSys = ObjectFile::Linux;
1522238SN/A                        break;
1532238SN/A                      case 2:
1542238SN/A                        opSys = ObjectFile::Solaris;
1552238SN/A                        break;
1562238SN/A                    }
1572238SN/A                } // if section found
1582600SN/A                if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1592600SN/A                        opSys = ObjectFile::Solaris;
1602600SN/A                if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1612600SN/A                        opSys = ObjectFile::Solaris;
1622600SN/A
1632238SN/A            section = elf_getscn(elf, ++secIdx);
1642238SN/A            } // while sections
1652238SN/A        }
1662472SN/A
1672976Sgblack@eecs.umich.edu        ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
1682976Sgblack@eecs.umich.edu
1692976Sgblack@eecs.umich.edu        //The number of headers in the file
1702976Sgblack@eecs.umich.edu        result->_programHeaderCount = ehdr.e_phnum;
1712976Sgblack@eecs.umich.edu        //Record the size of each entry
1722976Sgblack@eecs.umich.edu        result->_programHeaderSize = ehdr.e_phentsize;
1732976Sgblack@eecs.umich.edu        if(result->_programHeaderCount) //If there is a program header table
1742976Sgblack@eecs.umich.edu        {
1752976Sgblack@eecs.umich.edu            //Figure out the virtual address of the header table in the
1762976Sgblack@eecs.umich.edu            //final memory image. We use the program headers themselves
1772976Sgblack@eecs.umich.edu            //to translate from a file offset to the address in the image.
1782976Sgblack@eecs.umich.edu            GElf_Phdr phdr;
1792976Sgblack@eecs.umich.edu            uint64_t e_phoff = ehdr.e_phoff;
1802976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
1812976Sgblack@eecs.umich.edu            for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
1822976Sgblack@eecs.umich.edu            {
1832976Sgblack@eecs.umich.edu                gelf_getphdr(elf, hdrnum, &phdr);
1842976Sgblack@eecs.umich.edu                //Check if we've found the segment with the headers in it
1852976Sgblack@eecs.umich.edu                if(phdr.p_offset <= e_phoff &&
1862976Sgblack@eecs.umich.edu                        phdr.p_offset + phdr.p_filesz > e_phoff)
1872976Sgblack@eecs.umich.edu                {
1885143Sgblack@eecs.umich.edu                    result->_programHeaderTable = phdr.p_paddr + e_phoff;
1892976Sgblack@eecs.umich.edu                    break;
1902976Sgblack@eecs.umich.edu                }
1912976Sgblack@eecs.umich.edu            }
1922976Sgblack@eecs.umich.edu        }
1932976Sgblack@eecs.umich.edu        else
1942976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
1952976Sgblack@eecs.umich.edu
1962976Sgblack@eecs.umich.edu
1972238SN/A        elf_end(elf);
1982976Sgblack@eecs.umich.edu        return result;
19912SN/A    }
20012SN/A}
20112SN/A
20212SN/A
20312SN/AElfObject::ElfObject(const string &_filename, int _fd,
204360SN/A                     size_t _len, uint8_t *_data,
205360SN/A                     Arch _arch, OpSys _opSys)
206360SN/A    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
207443SN/A
20812SN/A{
209443SN/A    Elf *elf;
210443SN/A    GElf_Ehdr ehdr;
21112SN/A
212468SN/A    // check that header matches library version
2131708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
2141708SN/A        panic("wrong elf version number!");
21512SN/A
216468SN/A    // get a pointer to elf structure
217443SN/A    elf = elf_memory((char*)fileData,len);
218468SN/A    // will only fail if fd is invalid
219443SN/A    assert(elf != NULL);
22012SN/A
221468SN/A    // Check that we actually have a elf file
222468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
223443SN/A        panic("Not ELF, shouldn't be here");
22412SN/A    }
22512SN/A
226468SN/A    entry = ehdr.e_entry;
22712SN/A
228468SN/A    // initialize segment sizes to 0 in case they're not present
229468SN/A    text.size = data.size = bss.size = 0;
230468SN/A
2315090Sgblack@eecs.umich.edu    int secIdx = 1;
2325090Sgblack@eecs.umich.edu    Elf_Scn *section;
2335090Sgblack@eecs.umich.edu    GElf_Shdr shdr;
2345090Sgblack@eecs.umich.edu
2355090Sgblack@eecs.umich.edu    // The first address of some important sections.
2365090Sgblack@eecs.umich.edu    Addr textSecStart = 0;
2375090Sgblack@eecs.umich.edu    Addr dataSecStart = 0;
2385090Sgblack@eecs.umich.edu    Addr bssSecStart = 0;
2395090Sgblack@eecs.umich.edu
2405090Sgblack@eecs.umich.edu    // Get the first section
2415090Sgblack@eecs.umich.edu    section = elf_getscn(elf, secIdx);
2425090Sgblack@eecs.umich.edu
2435090Sgblack@eecs.umich.edu    // Find the beginning of the most interesting sections.
2445090Sgblack@eecs.umich.edu    while (section != NULL) {
2455090Sgblack@eecs.umich.edu        gelf_getshdr(section, &shdr);
2465090Sgblack@eecs.umich.edu        char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
2475090Sgblack@eecs.umich.edu
2485090Sgblack@eecs.umich.edu        if (!strcmp(".text", secName)) {
2495090Sgblack@eecs.umich.edu            textSecStart = shdr.sh_addr;
2505090Sgblack@eecs.umich.edu        } else if (!strcmp(".data", secName)) {
2515090Sgblack@eecs.umich.edu            dataSecStart = shdr.sh_addr;
2525090Sgblack@eecs.umich.edu        } else if (!strcmp(".bss", secName)) {
2535090Sgblack@eecs.umich.edu            bssSecStart = shdr.sh_addr;
2545090Sgblack@eecs.umich.edu        }
2555090Sgblack@eecs.umich.edu
2565090Sgblack@eecs.umich.edu        section = elf_getscn(elf, ++secIdx);
2575090Sgblack@eecs.umich.edu    }
2585090Sgblack@eecs.umich.edu
2595090Sgblack@eecs.umich.edu    // Go through all the segments in the program, record them, and scrape
2605090Sgblack@eecs.umich.edu    // out information about the text, data, and bss areas needed by other
2615090Sgblack@eecs.umich.edu    // code.
262468SN/A    for (int i = 0; i < ehdr.e_phnum; ++i) {
263468SN/A        GElf_Phdr phdr;
264468SN/A        if (gelf_getphdr(elf, i, &phdr) == 0) {
2655090Sgblack@eecs.umich.edu            panic("gelf_getphdr failed for segment %d.", i);
266468SN/A        }
267468SN/A
268468SN/A        // for now we don't care about non-loadable segments
269468SN/A        if (!(phdr.p_type & PT_LOAD))
270468SN/A            continue;
271468SN/A
2725090Sgblack@eecs.umich.edu        // Check to see if this segment contains the bss section.
2735143Sgblack@eecs.umich.edu        if (phdr.p_paddr <= bssSecStart &&
2745143Sgblack@eecs.umich.edu                phdr.p_paddr + phdr.p_memsz > bssSecStart &&
2755090Sgblack@eecs.umich.edu                phdr.p_memsz - phdr.p_filesz > 0) {
2765143Sgblack@eecs.umich.edu            bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
2775090Sgblack@eecs.umich.edu            bss.size = phdr.p_memsz - phdr.p_filesz;
2785090Sgblack@eecs.umich.edu            bss.fileImage = NULL;
2795090Sgblack@eecs.umich.edu        }
2805090Sgblack@eecs.umich.edu
2815090Sgblack@eecs.umich.edu        // Check to see if this is the text or data segment
2825152Sgblack@eecs.umich.edu        if (phdr.p_vaddr <= textSecStart &&
2835152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > textSecStart) {
2845143Sgblack@eecs.umich.edu            text.baseAddr = phdr.p_paddr;
285468SN/A            text.size = phdr.p_filesz;
2862420SN/A            text.fileImage = fileData + phdr.p_offset;
2875152Sgblack@eecs.umich.edu        } else if (phdr.p_vaddr <= dataSecStart &&
2885152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
2895143Sgblack@eecs.umich.edu            data.baseAddr = phdr.p_paddr;
290468SN/A            data.size = phdr.p_filesz;
2912420SN/A            data.fileImage = fileData + phdr.p_offset;
2922476SN/A        } else {
2935090Sgblack@eecs.umich.edu            Segment extra;
2945143Sgblack@eecs.umich.edu            extra.baseAddr = phdr.p_paddr;
2955090Sgblack@eecs.umich.edu            extra.size = phdr.p_filesz;
2965090Sgblack@eecs.umich.edu            extra.fileImage = fileData + phdr.p_offset;
2975090Sgblack@eecs.umich.edu            extraSegments.push_back(extra);
298468SN/A        }
299468SN/A    }
300468SN/A
301468SN/A    // should have found at least one loadable segment
302468SN/A    assert(text.size != 0);
303468SN/A
304468SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
305468SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
306468SN/A             bss.baseAddr, bss.size);
307468SN/A
308443SN/A    elf_end(elf);
309443SN/A
310468SN/A    // We will actually read the sections when we need to load them
31112SN/A}
31212SN/A
31312SN/A
31412SN/Abool
315468SN/AElfObject::loadSomeSymbols(SymbolTable *symtab, int binding)
31612SN/A{
317443SN/A    Elf *elf;
318766SN/A    int sec_idx = 1; // there is a 0 but it is nothing, go figure
319443SN/A    Elf_Scn *section;
320443SN/A    GElf_Shdr shdr;
321443SN/A    Elf_Data *data;
322443SN/A    int count, ii;
323443SN/A    bool found = false;
324443SN/A    GElf_Sym sym;
325443SN/A
326443SN/A    if (!symtab)
327443SN/A        return false;
328443SN/A
329468SN/A    // check that header matches library version
3301708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
3311708SN/A        panic("wrong elf version number!");
332443SN/A
333468SN/A    // get a pointer to elf structure
334443SN/A    elf = elf_memory((char*)fileData,len);
335443SN/A
336443SN/A    assert(elf != NULL);
337443SN/A
338468SN/A    // Get the first section
339454SN/A    section = elf_getscn(elf, sec_idx);
340443SN/A
341468SN/A    // While there are no more sections
342468SN/A    while (section != NULL) {
343443SN/A        gelf_getshdr(section, &shdr);
344443SN/A
345468SN/A        if (shdr.sh_type == SHT_SYMTAB) {
346443SN/A            found = true;
347443SN/A            data = elf_getdata(section, NULL);
348443SN/A            count = shdr.sh_size / shdr.sh_entsize;
349443SN/A            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
350443SN/A
351468SN/A            // loop through all the symbols, only loading global ones
352468SN/A            for (ii = 0; ii < count; ++ii) {
353443SN/A                gelf_getsym(data, ii, &sym);
354836SN/A                if (GELF_ST_BIND(sym.st_info) == binding) {
355468SN/A                   symtab->insert(sym.st_value,
356468SN/A                                  elf_strptr(elf, shdr.sh_link, sym.st_name));
357443SN/A                }
358443SN/A            }
359443SN/A        }
360454SN/A        ++sec_idx;
361454SN/A        section = elf_getscn(elf, sec_idx);
362443SN/A    }
363443SN/A
364443SN/A    elf_end(elf);
365443SN/A
366443SN/A    return found;
36712SN/A}
36812SN/A
36912SN/Abool
3703812Ssaidi@eecs.umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
371468SN/A{
372468SN/A    return loadSomeSymbols(symtab, STB_GLOBAL);
373468SN/A}
374468SN/A
375468SN/Abool
3763812Ssaidi@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
37712SN/A{
378468SN/A    return loadSomeSymbols(symtab, STB_LOCAL);
37912SN/A}
3803917Ssaidi@eecs.umich.edu
3815090Sgblack@eecs.umich.edubool
3825090Sgblack@eecs.umich.eduElfObject::loadSections(Port *memPort, Addr addrMask)
3835090Sgblack@eecs.umich.edu{
3845090Sgblack@eecs.umich.edu    if (!ObjectFile::loadSections(memPort, addrMask))
3855090Sgblack@eecs.umich.edu        return false;
3865090Sgblack@eecs.umich.edu
3875090Sgblack@eecs.umich.edu    vector<Segment>::iterator extraIt;
3885090Sgblack@eecs.umich.edu    for (extraIt = extraSegments.begin();
3895090Sgblack@eecs.umich.edu            extraIt != extraSegments.end(); extraIt++) {
3905090Sgblack@eecs.umich.edu        if (!loadSection(&(*extraIt), memPort, addrMask)) {
3915090Sgblack@eecs.umich.edu            return false;
3925090Sgblack@eecs.umich.edu        }
3935090Sgblack@eecs.umich.edu    }
3945090Sgblack@eecs.umich.edu    return true;
3955090Sgblack@eecs.umich.edu}
3965090Sgblack@eecs.umich.edu
3975070Ssaidi@eecs.umich.eduvoid
3985070Ssaidi@eecs.umich.eduElfObject::getSections()
3993917Ssaidi@eecs.umich.edu{
4003917Ssaidi@eecs.umich.edu    Elf *elf;
4013917Ssaidi@eecs.umich.edu    int sec_idx = 1; // there is a 0 but it is nothing, go figure
4023917Ssaidi@eecs.umich.edu    Elf_Scn *section;
4033917Ssaidi@eecs.umich.edu    GElf_Shdr shdr;
4043917Ssaidi@eecs.umich.edu
4053917Ssaidi@eecs.umich.edu    GElf_Ehdr ehdr;
4063917Ssaidi@eecs.umich.edu
4075070Ssaidi@eecs.umich.edu    assert(!sectionNames.size());
4085070Ssaidi@eecs.umich.edu
4093917Ssaidi@eecs.umich.edu    // check that header matches library version
4103917Ssaidi@eecs.umich.edu    if (elf_version(EV_CURRENT) == EV_NONE)
4113917Ssaidi@eecs.umich.edu        panic("wrong elf version number!");
4123917Ssaidi@eecs.umich.edu
4133917Ssaidi@eecs.umich.edu    // get a pointer to elf structure
4143917Ssaidi@eecs.umich.edu    elf = elf_memory((char*)fileData,len);
4153917Ssaidi@eecs.umich.edu    assert(elf != NULL);
4163917Ssaidi@eecs.umich.edu
4173917Ssaidi@eecs.umich.edu    // Check that we actually have a elf file
4183917Ssaidi@eecs.umich.edu    if (gelf_getehdr(elf, &ehdr) ==0) {
4193917Ssaidi@eecs.umich.edu        panic("Not ELF, shouldn't be here");
4203917Ssaidi@eecs.umich.edu    }
4213917Ssaidi@eecs.umich.edu
4223917Ssaidi@eecs.umich.edu    // Get the first section
4233917Ssaidi@eecs.umich.edu    section = elf_getscn(elf, sec_idx);
4243917Ssaidi@eecs.umich.edu
4253917Ssaidi@eecs.umich.edu    // While there are no more sections
4263917Ssaidi@eecs.umich.edu    while (section != NULL) {
4273917Ssaidi@eecs.umich.edu        gelf_getshdr(section, &shdr);
4285070Ssaidi@eecs.umich.edu        sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
4293917Ssaidi@eecs.umich.edu        section = elf_getscn(elf, ++sec_idx);
4303917Ssaidi@eecs.umich.edu    } // while sections
4313917Ssaidi@eecs.umich.edu}
4323917Ssaidi@eecs.umich.edu
4335070Ssaidi@eecs.umich.edubool
4345070Ssaidi@eecs.umich.eduElfObject::sectionExists(string sec)
4355070Ssaidi@eecs.umich.edu{
4365070Ssaidi@eecs.umich.edu    if (!sectionNames.size())
4375070Ssaidi@eecs.umich.edu        getSections();
4385070Ssaidi@eecs.umich.edu    return sectionNames.find(sec) != sectionNames.end();
4395070Ssaidi@eecs.umich.edu}
4403917Ssaidi@eecs.umich.edu
4415070Ssaidi@eecs.umich.edu
442