elf_object.cc revision 7581
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
377095Sgblack@eecs.umich.edu#include "base/bitfield.hh"
3856SN/A#include "base/loader/elf_object.hh"
394484Sbinkertn@umich.edu#include "base/loader/symtab.hh"
402439SN/A#include "base/misc.hh"
415543Ssaidi@eecs.umich.edu#include "base/trace.hh"        // for DPRINTF
422423SN/A#include "sim/byteswap.hh"
432423SN/A
4412SN/Ausing namespace std;
4512SN/A
4612SN/AObjectFile *
4712SN/AElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
4812SN/A{
49443SN/A    Elf *elf;
50443SN/A    GElf_Ehdr ehdr;
512207SN/A    Arch arch = UnknownArch;
522207SN/A    OpSys opSys = UnknownOpSys;
53443SN/A
54468SN/A    // check that header matches library version
551708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
561708SN/A        panic("wrong elf version number!");
57443SN/A
58468SN/A    // get a pointer to elf structure
59443SN/A    elf = elf_memory((char*)data,len);
60468SN/A    // will only fail if fd is invalid
61443SN/A    assert(elf != NULL);
62443SN/A
63468SN/A    // Check that we actually have a elf file
64468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
65443SN/A        DPRINTFR(Loader, "Not ELF\n");
66443SN/A        elf_end(elf);
67443SN/A        return NULL;
682476SN/A    } else {
692207SN/A        //Detect the architecture
702207SN/A        //Since we don't know how to check for alpha right now, we'll
712207SN/A        //just assume if it wasn't something else and it's 64 bit, that's
722207SN/A        //what it must be.
732207SN/A        if (ehdr.e_machine == EM_SPARC64 ||
744111Sgblack@eecs.umich.edu                (ehdr.e_machine == EM_SPARC &&
754111Sgblack@eecs.umich.edu                 ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
762620SN/A                ehdr.e_machine == EM_SPARCV9) {
774111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC64;
784111Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_SPARC32PLUS ||
794111Sgblack@eecs.umich.edu                        (ehdr.e_machine == EM_SPARC &&
804111Sgblack@eecs.umich.edu                         ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
814111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC32;
822207SN/A        } else if (ehdr.e_machine == EM_MIPS
832207SN/A                && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
845383Sgblack@eecs.umich.edu            if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
855383Sgblack@eecs.umich.edu                arch = ObjectFile::Mips;
865383Sgblack@eecs.umich.edu            } else {
875383Sgblack@eecs.umich.edu                fatal("The binary you're trying to load is compiled for big "
885383Sgblack@eecs.umich.edu                        "endian MIPS. M5\nonly supports little endian MIPS. "
895383Sgblack@eecs.umich.edu                        "Please recompile your binary.\n");
905383Sgblack@eecs.umich.edu            }
914166Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_X86_64 &&
924166Sgblack@eecs.umich.edu                ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
935874Sgblack@eecs.umich.edu            arch = ObjectFile::X86_64;
945874Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_386 &&
955874Sgblack@eecs.umich.edu                ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
965874Sgblack@eecs.umich.edu            arch = ObjectFile::I386;
972207SN/A        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
982207SN/A            arch = ObjectFile::Alpha;
995335Shines@cs.fsu.edu        } else if (ehdr.e_machine == EM_ARM) {
1007095Sgblack@eecs.umich.edu            if (bits(ehdr.e_entry, 0)) {
1017095Sgblack@eecs.umich.edu                arch = ObjectFile::Thumb;
1027095Sgblack@eecs.umich.edu            } else {
1037095Sgblack@eecs.umich.edu                arch = ObjectFile::Arm;
1047095Sgblack@eecs.umich.edu            }
1056691Stjones1@inf.ed.ac.uk        } else if (ehdr.e_machine == EM_PPC &&
1066691Stjones1@inf.ed.ac.uk                ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1076691Stjones1@inf.ed.ac.uk          if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
1086691Stjones1@inf.ed.ac.uk                arch = ObjectFile::Power;
1096691Stjones1@inf.ed.ac.uk          } else {
1106691Stjones1@inf.ed.ac.uk                fatal("The binary you're trying to load is compiled for "
1116691Stjones1@inf.ed.ac.uk                        "little endian Power.\nM5 only supports big "
1126691Stjones1@inf.ed.ac.uk                        "endian Power. Please recompile your binary.\n");
1136691Stjones1@inf.ed.ac.uk          }
1146691Stjones1@inf.ed.ac.uk        } else if (ehdr.e_machine == EM_PPC64) {
1156691Stjones1@inf.ed.ac.uk            fatal("The binary you're trying to load is compiled for 64-bit "
1166691Stjones1@inf.ed.ac.uk                  "Power. M5\n only supports 32-bit Power. Please "
1176691Stjones1@inf.ed.ac.uk                  "recompile your binary.\n");
1182207SN/A        } else {
1192600SN/A            warn("Unknown architecture: %d\n", ehdr.e_machine);
1202207SN/A            arch = ObjectFile::UnknownArch;
1212207SN/A        }
1222207SN/A
1232207SN/A        //Detect the operating system
1242207SN/A        switch (ehdr.e_ident[EI_OSABI])
1252207SN/A        {
1262238SN/A
1272207SN/A          case ELFOSABI_LINUX:
1282207SN/A            opSys = ObjectFile::Linux;
1292207SN/A            break;
1302207SN/A          case ELFOSABI_SOLARIS:
1312207SN/A            opSys = ObjectFile::Solaris;
1322238SN/A            break;
1332207SN/A          case ELFOSABI_TRU64:
1342207SN/A            opSys = ObjectFile::Tru64;
1352238SN/A            break;
1366392Ssaidi@eecs.umich.edu          case ELFOSABI_ARM:
1376392Ssaidi@eecs.umich.edu            opSys = ObjectFile::LinuxArmOABI;
1386392Ssaidi@eecs.umich.edu            break;
1392207SN/A          default:
1402207SN/A            opSys = ObjectFile::UnknownOpSys;
1412207SN/A        }
1422207SN/A
1432238SN/A        //take a look at the .note.ABI section
1442238SN/A        //It can let us know what's what.
1452600SN/A        if (opSys == ObjectFile::UnknownOpSys) {
1462238SN/A            Elf_Scn *section;
1472238SN/A            GElf_Shdr shdr;
1482238SN/A            Elf_Data *data;
1492238SN/A            uint32_t osAbi;;
1502238SN/A            int secIdx = 1;
1512238SN/A
1522238SN/A            // Get the first section
1532238SN/A            section = elf_getscn(elf, secIdx);
1542238SN/A
1552238SN/A            // While there are no more sections
1562600SN/A            while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
1572238SN/A                gelf_getshdr(section, &shdr);
1582238SN/A                if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
1592238SN/A                            elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
1602238SN/A                    // we have found a ABI note section
1612238SN/A                    // Check the 5th 32bit word for OS  0 == linux, 1 == hurd,
1622238SN/A                    // 2 == solaris, 3 == freebsd
1632238SN/A                    data = elf_rawdata(section, NULL);
1642238SN/A                    assert(data->d_buf);
1652238SN/A                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1662238SN/A                        osAbi = htole(((uint32_t*)data->d_buf)[4]);
1672238SN/A                    else
1682238SN/A                        osAbi = htobe(((uint32_t*)data->d_buf)[4]);
1692238SN/A
1702238SN/A                    switch(osAbi) {
1712238SN/A                      case 0:
1722238SN/A                        opSys = ObjectFile::Linux;
1732238SN/A                        break;
1742238SN/A                      case 2:
1752238SN/A                        opSys = ObjectFile::Solaris;
1762238SN/A                        break;
1772238SN/A                    }
1782238SN/A                } // if section found
1792600SN/A                if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1802600SN/A                        opSys = ObjectFile::Solaris;
1812600SN/A                if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1822600SN/A                        opSys = ObjectFile::Solaris;
1832600SN/A
1842238SN/A            section = elf_getscn(elf, ++secIdx);
1852238SN/A            } // while sections
1862238SN/A        }
1872472SN/A
1882976Sgblack@eecs.umich.edu        ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
1892976Sgblack@eecs.umich.edu
1902976Sgblack@eecs.umich.edu        //The number of headers in the file
1912976Sgblack@eecs.umich.edu        result->_programHeaderCount = ehdr.e_phnum;
1922976Sgblack@eecs.umich.edu        //Record the size of each entry
1932976Sgblack@eecs.umich.edu        result->_programHeaderSize = ehdr.e_phentsize;
1942976Sgblack@eecs.umich.edu        if(result->_programHeaderCount) //If there is a program header table
1952976Sgblack@eecs.umich.edu        {
1962976Sgblack@eecs.umich.edu            //Figure out the virtual address of the header table in the
1972976Sgblack@eecs.umich.edu            //final memory image. We use the program headers themselves
1982976Sgblack@eecs.umich.edu            //to translate from a file offset to the address in the image.
1992976Sgblack@eecs.umich.edu            GElf_Phdr phdr;
2002976Sgblack@eecs.umich.edu            uint64_t e_phoff = ehdr.e_phoff;
2012976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
2022976Sgblack@eecs.umich.edu            for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
2032976Sgblack@eecs.umich.edu            {
2042976Sgblack@eecs.umich.edu                gelf_getphdr(elf, hdrnum, &phdr);
2052976Sgblack@eecs.umich.edu                //Check if we've found the segment with the headers in it
2062976Sgblack@eecs.umich.edu                if(phdr.p_offset <= e_phoff &&
2072976Sgblack@eecs.umich.edu                        phdr.p_offset + phdr.p_filesz > e_phoff)
2082976Sgblack@eecs.umich.edu                {
2095143Sgblack@eecs.umich.edu                    result->_programHeaderTable = phdr.p_paddr + e_phoff;
2102976Sgblack@eecs.umich.edu                    break;
2112976Sgblack@eecs.umich.edu                }
2122976Sgblack@eecs.umich.edu            }
2132976Sgblack@eecs.umich.edu        }
2142976Sgblack@eecs.umich.edu        else
2152976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
2162976Sgblack@eecs.umich.edu
2172976Sgblack@eecs.umich.edu
2182238SN/A        elf_end(elf);
2192976Sgblack@eecs.umich.edu        return result;
22012SN/A    }
22112SN/A}
22212SN/A
22312SN/A
22412SN/AElfObject::ElfObject(const string &_filename, int _fd,
225360SN/A                     size_t _len, uint8_t *_data,
226360SN/A                     Arch _arch, OpSys _opSys)
227360SN/A    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
228443SN/A
22912SN/A{
230443SN/A    Elf *elf;
231443SN/A    GElf_Ehdr ehdr;
23212SN/A
233468SN/A    // check that header matches library version
2341708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
2351708SN/A        panic("wrong elf version number!");
23612SN/A
237468SN/A    // get a pointer to elf structure
238443SN/A    elf = elf_memory((char*)fileData,len);
239468SN/A    // will only fail if fd is invalid
240443SN/A    assert(elf != NULL);
24112SN/A
242468SN/A    // Check that we actually have a elf file
243468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
244443SN/A        panic("Not ELF, shouldn't be here");
24512SN/A    }
24612SN/A
247468SN/A    entry = ehdr.e_entry;
24812SN/A
249468SN/A    // initialize segment sizes to 0 in case they're not present
250468SN/A    text.size = data.size = bss.size = 0;
251468SN/A
2525090Sgblack@eecs.umich.edu    int secIdx = 1;
2535090Sgblack@eecs.umich.edu    Elf_Scn *section;
2545090Sgblack@eecs.umich.edu    GElf_Shdr shdr;
2555090Sgblack@eecs.umich.edu
2565090Sgblack@eecs.umich.edu    // The first address of some important sections.
2575090Sgblack@eecs.umich.edu    Addr textSecStart = 0;
2585090Sgblack@eecs.umich.edu    Addr dataSecStart = 0;
2595090Sgblack@eecs.umich.edu    Addr bssSecStart = 0;
2605090Sgblack@eecs.umich.edu
2615090Sgblack@eecs.umich.edu    // Get the first section
2625090Sgblack@eecs.umich.edu    section = elf_getscn(elf, secIdx);
2635090Sgblack@eecs.umich.edu
2645090Sgblack@eecs.umich.edu    // Find the beginning of the most interesting sections.
2655090Sgblack@eecs.umich.edu    while (section != NULL) {
2665090Sgblack@eecs.umich.edu        gelf_getshdr(section, &shdr);
2675090Sgblack@eecs.umich.edu        char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
2685090Sgblack@eecs.umich.edu
2695090Sgblack@eecs.umich.edu        if (!strcmp(".text", secName)) {
2705090Sgblack@eecs.umich.edu            textSecStart = shdr.sh_addr;
2715090Sgblack@eecs.umich.edu        } else if (!strcmp(".data", secName)) {
2725090Sgblack@eecs.umich.edu            dataSecStart = shdr.sh_addr;
2735090Sgblack@eecs.umich.edu        } else if (!strcmp(".bss", secName)) {
2745090Sgblack@eecs.umich.edu            bssSecStart = shdr.sh_addr;
2755090Sgblack@eecs.umich.edu        }
2765090Sgblack@eecs.umich.edu
2775090Sgblack@eecs.umich.edu        section = elf_getscn(elf, ++secIdx);
2785090Sgblack@eecs.umich.edu    }
2795090Sgblack@eecs.umich.edu
2805090Sgblack@eecs.umich.edu    // Go through all the segments in the program, record them, and scrape
2815090Sgblack@eecs.umich.edu    // out information about the text, data, and bss areas needed by other
2825090Sgblack@eecs.umich.edu    // code.
283468SN/A    for (int i = 0; i < ehdr.e_phnum; ++i) {
284468SN/A        GElf_Phdr phdr;
285468SN/A        if (gelf_getphdr(elf, i, &phdr) == 0) {
2865090Sgblack@eecs.umich.edu            panic("gelf_getphdr failed for segment %d.", i);
287468SN/A        }
288468SN/A
289468SN/A        // for now we don't care about non-loadable segments
290468SN/A        if (!(phdr.p_type & PT_LOAD))
291468SN/A            continue;
292468SN/A
2935090Sgblack@eecs.umich.edu        // Check to see if this segment contains the bss section.
2945143Sgblack@eecs.umich.edu        if (phdr.p_paddr <= bssSecStart &&
2955143Sgblack@eecs.umich.edu                phdr.p_paddr + phdr.p_memsz > bssSecStart &&
2965090Sgblack@eecs.umich.edu                phdr.p_memsz - phdr.p_filesz > 0) {
2975143Sgblack@eecs.umich.edu            bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
2985090Sgblack@eecs.umich.edu            bss.size = phdr.p_memsz - phdr.p_filesz;
2995090Sgblack@eecs.umich.edu            bss.fileImage = NULL;
3005090Sgblack@eecs.umich.edu        }
3015090Sgblack@eecs.umich.edu
3025090Sgblack@eecs.umich.edu        // Check to see if this is the text or data segment
3035152Sgblack@eecs.umich.edu        if (phdr.p_vaddr <= textSecStart &&
3045152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > textSecStart) {
3055143Sgblack@eecs.umich.edu            text.baseAddr = phdr.p_paddr;
306468SN/A            text.size = phdr.p_filesz;
3072420SN/A            text.fileImage = fileData + phdr.p_offset;
3085152Sgblack@eecs.umich.edu        } else if (phdr.p_vaddr <= dataSecStart &&
3095152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
3105143Sgblack@eecs.umich.edu            data.baseAddr = phdr.p_paddr;
311468SN/A            data.size = phdr.p_filesz;
3122420SN/A            data.fileImage = fileData + phdr.p_offset;
3132476SN/A        } else {
3145759Shsul@eecs.umich.edu            // If it's none of the above but is loadable,
3155759Shsul@eecs.umich.edu            // load the filesize worth of data
3165090Sgblack@eecs.umich.edu            Segment extra;
3175143Sgblack@eecs.umich.edu            extra.baseAddr = phdr.p_paddr;
3185090Sgblack@eecs.umich.edu            extra.size = phdr.p_filesz;
3195090Sgblack@eecs.umich.edu            extra.fileImage = fileData + phdr.p_offset;
3205090Sgblack@eecs.umich.edu            extraSegments.push_back(extra);
321468SN/A        }
322468SN/A    }
323468SN/A
324468SN/A    // should have found at least one loadable segment
325468SN/A    assert(text.size != 0);
326468SN/A
327468SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
328468SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
329468SN/A             bss.baseAddr, bss.size);
330468SN/A
331443SN/A    elf_end(elf);
332443SN/A
333468SN/A    // We will actually read the sections when we need to load them
33412SN/A}
33512SN/A
33612SN/A
33712SN/Abool
3387581SAli.Saidi@arm.comElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
33912SN/A{
340443SN/A    Elf *elf;
341766SN/A    int sec_idx = 1; // there is a 0 but it is nothing, go figure
342443SN/A    Elf_Scn *section;
343443SN/A    GElf_Shdr shdr;
344443SN/A    Elf_Data *data;
345443SN/A    int count, ii;
346443SN/A    bool found = false;
347443SN/A    GElf_Sym sym;
348443SN/A
349443SN/A    if (!symtab)
350443SN/A        return false;
351443SN/A
352468SN/A    // check that header matches library version
3531708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
3541708SN/A        panic("wrong elf version number!");
355443SN/A
356468SN/A    // get a pointer to elf structure
357443SN/A    elf = elf_memory((char*)fileData,len);
358443SN/A
359443SN/A    assert(elf != NULL);
360443SN/A
361468SN/A    // Get the first section
362454SN/A    section = elf_getscn(elf, sec_idx);
363443SN/A
364468SN/A    // While there are no more sections
365468SN/A    while (section != NULL) {
366443SN/A        gelf_getshdr(section, &shdr);
367443SN/A
368468SN/A        if (shdr.sh_type == SHT_SYMTAB) {
369443SN/A            found = true;
370443SN/A            data = elf_getdata(section, NULL);
371443SN/A            count = shdr.sh_size / shdr.sh_entsize;
372443SN/A            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
373443SN/A
374468SN/A            // loop through all the symbols, only loading global ones
375468SN/A            for (ii = 0; ii < count; ++ii) {
376443SN/A                gelf_getsym(data, ii, &sym);
377836SN/A                if (GELF_ST_BIND(sym.st_info) == binding) {
3787581SAli.Saidi@arm.com                   symtab->insert(sym.st_value & mask,
379468SN/A                                  elf_strptr(elf, shdr.sh_link, sym.st_name));
380443SN/A                }
381443SN/A            }
382443SN/A        }
383454SN/A        ++sec_idx;
384454SN/A        section = elf_getscn(elf, sec_idx);
385443SN/A    }
386443SN/A
387443SN/A    elf_end(elf);
388443SN/A
389443SN/A    return found;
39012SN/A}
39112SN/A
39212SN/Abool
3933812Ssaidi@eecs.umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
394468SN/A{
3957581SAli.Saidi@arm.com    return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
396468SN/A}
397468SN/A
398468SN/Abool
3993812Ssaidi@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
40012SN/A{
4017581SAli.Saidi@arm.com    return loadSomeSymbols(symtab, STB_LOCAL, addrMask);
40212SN/A}
4033917Ssaidi@eecs.umich.edu
4045090Sgblack@eecs.umich.edubool
4055090Sgblack@eecs.umich.eduElfObject::loadSections(Port *memPort, Addr addrMask)
4065090Sgblack@eecs.umich.edu{
4075090Sgblack@eecs.umich.edu    if (!ObjectFile::loadSections(memPort, addrMask))
4085090Sgblack@eecs.umich.edu        return false;
4095090Sgblack@eecs.umich.edu
4105090Sgblack@eecs.umich.edu    vector<Segment>::iterator extraIt;
4115090Sgblack@eecs.umich.edu    for (extraIt = extraSegments.begin();
4125090Sgblack@eecs.umich.edu            extraIt != extraSegments.end(); extraIt++) {
4135090Sgblack@eecs.umich.edu        if (!loadSection(&(*extraIt), memPort, addrMask)) {
4145090Sgblack@eecs.umich.edu            return false;
4155090Sgblack@eecs.umich.edu        }
4165090Sgblack@eecs.umich.edu    }
4175090Sgblack@eecs.umich.edu    return true;
4185090Sgblack@eecs.umich.edu}
4195090Sgblack@eecs.umich.edu
4205070Ssaidi@eecs.umich.eduvoid
4215070Ssaidi@eecs.umich.eduElfObject::getSections()
4223917Ssaidi@eecs.umich.edu{
4233917Ssaidi@eecs.umich.edu    Elf *elf;
4243917Ssaidi@eecs.umich.edu    int sec_idx = 1; // there is a 0 but it is nothing, go figure
4253917Ssaidi@eecs.umich.edu    Elf_Scn *section;
4263917Ssaidi@eecs.umich.edu    GElf_Shdr shdr;
4273917Ssaidi@eecs.umich.edu
4283917Ssaidi@eecs.umich.edu    GElf_Ehdr ehdr;
4293917Ssaidi@eecs.umich.edu
4305070Ssaidi@eecs.umich.edu    assert(!sectionNames.size());
4315070Ssaidi@eecs.umich.edu
4323917Ssaidi@eecs.umich.edu    // check that header matches library version
4333917Ssaidi@eecs.umich.edu    if (elf_version(EV_CURRENT) == EV_NONE)
4343917Ssaidi@eecs.umich.edu        panic("wrong elf version number!");
4353917Ssaidi@eecs.umich.edu
4363917Ssaidi@eecs.umich.edu    // get a pointer to elf structure
4373917Ssaidi@eecs.umich.edu    elf = elf_memory((char*)fileData,len);
4383917Ssaidi@eecs.umich.edu    assert(elf != NULL);
4393917Ssaidi@eecs.umich.edu
4403917Ssaidi@eecs.umich.edu    // Check that we actually have a elf file
4413917Ssaidi@eecs.umich.edu    if (gelf_getehdr(elf, &ehdr) ==0) {
4423917Ssaidi@eecs.umich.edu        panic("Not ELF, shouldn't be here");
4433917Ssaidi@eecs.umich.edu    }
4443917Ssaidi@eecs.umich.edu
4453917Ssaidi@eecs.umich.edu    // Get the first section
4463917Ssaidi@eecs.umich.edu    section = elf_getscn(elf, sec_idx);
4473917Ssaidi@eecs.umich.edu
4483917Ssaidi@eecs.umich.edu    // While there are no more sections
4493917Ssaidi@eecs.umich.edu    while (section != NULL) {
4503917Ssaidi@eecs.umich.edu        gelf_getshdr(section, &shdr);
4515070Ssaidi@eecs.umich.edu        sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
4523917Ssaidi@eecs.umich.edu        section = elf_getscn(elf, ++sec_idx);
4533917Ssaidi@eecs.umich.edu    } // while sections
4543917Ssaidi@eecs.umich.edu}
4553917Ssaidi@eecs.umich.edu
4565070Ssaidi@eecs.umich.edubool
4575070Ssaidi@eecs.umich.eduElfObject::sectionExists(string sec)
4585070Ssaidi@eecs.umich.edu{
4595070Ssaidi@eecs.umich.edu    if (!sectionNames.size())
4605070Ssaidi@eecs.umich.edu        getSections();
4615070Ssaidi@eecs.umich.edu    return sectionNames.find(sec) != sectionNames.end();
4625070Ssaidi@eecs.umich.edu}
4633917Ssaidi@eecs.umich.edu
4645070Ssaidi@eecs.umich.edu
465