elf_object.cc revision 9810
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
3556SN/A#include "base/loader/elf_object.hh"
364484Sbinkertn@umich.edu#include "base/loader/symtab.hh"
378229Snate@binkert.org#include "base/bitfield.hh"
382439SN/A#include "base/misc.hh"
397676Snate@binkert.org#include "base/trace.hh"
408232Snate@binkert.org#include "debug/Loader.hh"
412423SN/A#include "sim/byteswap.hh"
428229Snate@binkert.org#include "gelf.h"
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;
2519186SAli.Saidi@ARM.com    text.baseAddr = data.baseAddr = bss.baseAddr = 0;
252468SN/A
2535090Sgblack@eecs.umich.edu    int secIdx = 1;
2545090Sgblack@eecs.umich.edu    Elf_Scn *section;
2555090Sgblack@eecs.umich.edu    GElf_Shdr shdr;
2565090Sgblack@eecs.umich.edu
2575090Sgblack@eecs.umich.edu    // The first address of some important sections.
2585090Sgblack@eecs.umich.edu    Addr textSecStart = 0;
2595090Sgblack@eecs.umich.edu    Addr dataSecStart = 0;
2605090Sgblack@eecs.umich.edu    Addr bssSecStart = 0;
2615090Sgblack@eecs.umich.edu
2625090Sgblack@eecs.umich.edu    // Get the first section
2635090Sgblack@eecs.umich.edu    section = elf_getscn(elf, secIdx);
2645090Sgblack@eecs.umich.edu
2655090Sgblack@eecs.umich.edu    // Find the beginning of the most interesting sections.
2665090Sgblack@eecs.umich.edu    while (section != NULL) {
2675090Sgblack@eecs.umich.edu        gelf_getshdr(section, &shdr);
2685090Sgblack@eecs.umich.edu        char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
2695090Sgblack@eecs.umich.edu
2708350Sgblack@eecs.umich.edu        if (secName) {
2718350Sgblack@eecs.umich.edu            if (!strcmp(".text", secName)) {
2728350Sgblack@eecs.umich.edu                textSecStart = shdr.sh_addr;
2738350Sgblack@eecs.umich.edu            } else if (!strcmp(".data", secName)) {
2748350Sgblack@eecs.umich.edu                dataSecStart = shdr.sh_addr;
2758350Sgblack@eecs.umich.edu            } else if (!strcmp(".bss", secName)) {
2768350Sgblack@eecs.umich.edu                bssSecStart = shdr.sh_addr;
2778350Sgblack@eecs.umich.edu            }
2788350Sgblack@eecs.umich.edu        } else {
2798350Sgblack@eecs.umich.edu            Elf_Error errorNum = (Elf_Error)elf_errno();
2808350Sgblack@eecs.umich.edu            if (errorNum != ELF_E_NONE) {
2818350Sgblack@eecs.umich.edu                const char *errorMessage = elf_errmsg(errorNum);
2828350Sgblack@eecs.umich.edu                fatal("Error from libelf: %s.\n", errorMessage);
2838350Sgblack@eecs.umich.edu            }
2845090Sgblack@eecs.umich.edu        }
2855090Sgblack@eecs.umich.edu
2865090Sgblack@eecs.umich.edu        section = elf_getscn(elf, ++secIdx);
2875090Sgblack@eecs.umich.edu    }
2885090Sgblack@eecs.umich.edu
2895090Sgblack@eecs.umich.edu    // Go through all the segments in the program, record them, and scrape
2905090Sgblack@eecs.umich.edu    // out information about the text, data, and bss areas needed by other
2915090Sgblack@eecs.umich.edu    // code.
292468SN/A    for (int i = 0; i < ehdr.e_phnum; ++i) {
293468SN/A        GElf_Phdr phdr;
294468SN/A        if (gelf_getphdr(elf, i, &phdr) == 0) {
2955090Sgblack@eecs.umich.edu            panic("gelf_getphdr failed for segment %d.", i);
296468SN/A        }
297468SN/A
298468SN/A        // for now we don't care about non-loadable segments
299468SN/A        if (!(phdr.p_type & PT_LOAD))
300468SN/A            continue;
301468SN/A
3025090Sgblack@eecs.umich.edu        // Check to see if this segment contains the bss section.
3035143Sgblack@eecs.umich.edu        if (phdr.p_paddr <= bssSecStart &&
3045143Sgblack@eecs.umich.edu                phdr.p_paddr + phdr.p_memsz > bssSecStart &&
3055090Sgblack@eecs.umich.edu                phdr.p_memsz - phdr.p_filesz > 0) {
3065143Sgblack@eecs.umich.edu            bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
3075090Sgblack@eecs.umich.edu            bss.size = phdr.p_memsz - phdr.p_filesz;
3085090Sgblack@eecs.umich.edu            bss.fileImage = NULL;
3095090Sgblack@eecs.umich.edu        }
3105090Sgblack@eecs.umich.edu
3115090Sgblack@eecs.umich.edu        // Check to see if this is the text or data segment
3125152Sgblack@eecs.umich.edu        if (phdr.p_vaddr <= textSecStart &&
3135152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > textSecStart) {
3145143Sgblack@eecs.umich.edu            text.baseAddr = phdr.p_paddr;
315468SN/A            text.size = phdr.p_filesz;
3162420SN/A            text.fileImage = fileData + phdr.p_offset;
3175152Sgblack@eecs.umich.edu        } else if (phdr.p_vaddr <= dataSecStart &&
3185152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
3195143Sgblack@eecs.umich.edu            data.baseAddr = phdr.p_paddr;
320468SN/A            data.size = phdr.p_filesz;
3212420SN/A            data.fileImage = fileData + phdr.p_offset;
3222476SN/A        } else {
3235759Shsul@eecs.umich.edu            // If it's none of the above but is loadable,
3245759Shsul@eecs.umich.edu            // load the filesize worth of data
3255090Sgblack@eecs.umich.edu            Segment extra;
3265143Sgblack@eecs.umich.edu            extra.baseAddr = phdr.p_paddr;
3275090Sgblack@eecs.umich.edu            extra.size = phdr.p_filesz;
3285090Sgblack@eecs.umich.edu            extra.fileImage = fileData + phdr.p_offset;
3295090Sgblack@eecs.umich.edu            extraSegments.push_back(extra);
330468SN/A        }
331468SN/A    }
332468SN/A
333468SN/A    // should have found at least one loadable segment
334468SN/A    assert(text.size != 0);
335468SN/A
336468SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
337468SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
338468SN/A             bss.baseAddr, bss.size);
339468SN/A
340443SN/A    elf_end(elf);
341443SN/A
342468SN/A    // We will actually read the sections when we need to load them
34312SN/A}
34412SN/A
34512SN/A
34612SN/Abool
3477581SAli.Saidi@arm.comElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
34812SN/A{
349443SN/A    Elf *elf;
350766SN/A    int sec_idx = 1; // there is a 0 but it is nothing, go figure
351443SN/A    Elf_Scn *section;
352443SN/A    GElf_Shdr shdr;
353443SN/A    Elf_Data *data;
354443SN/A    int count, ii;
355443SN/A    bool found = false;
356443SN/A    GElf_Sym sym;
357443SN/A
358443SN/A    if (!symtab)
359443SN/A        return false;
360443SN/A
361468SN/A    // check that header matches library version
3621708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
3631708SN/A        panic("wrong elf version number!");
364443SN/A
365468SN/A    // get a pointer to elf structure
366443SN/A    elf = elf_memory((char*)fileData,len);
367443SN/A
368443SN/A    assert(elf != NULL);
369443SN/A
370468SN/A    // Get the first section
371454SN/A    section = elf_getscn(elf, sec_idx);
372443SN/A
373468SN/A    // While there are no more sections
374468SN/A    while (section != NULL) {
375443SN/A        gelf_getshdr(section, &shdr);
376443SN/A
377468SN/A        if (shdr.sh_type == SHT_SYMTAB) {
378443SN/A            found = true;
379443SN/A            data = elf_getdata(section, NULL);
380443SN/A            count = shdr.sh_size / shdr.sh_entsize;
381443SN/A            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
382443SN/A
383468SN/A            // loop through all the symbols, only loading global ones
384468SN/A            for (ii = 0; ii < count; ++ii) {
385443SN/A                gelf_getsym(data, ii, &sym);
386836SN/A                if (GELF_ST_BIND(sym.st_info) == binding) {
3877589SAli.Saidi@arm.com                    char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
3887589SAli.Saidi@arm.com                    if (sym_name && sym_name[0] != '$') {
3897589SAli.Saidi@arm.com                        DPRINTF(Loader, "Symbol: %-40s value %#x\n",
3907589SAli.Saidi@arm.com                                sym_name, sym.st_value);
3917589SAli.Saidi@arm.com                        symtab->insert(sym.st_value & mask, sym_name);
3927589SAli.Saidi@arm.com                    }
393443SN/A                }
394443SN/A            }
395443SN/A        }
396454SN/A        ++sec_idx;
397454SN/A        section = elf_getscn(elf, sec_idx);
398443SN/A    }
399443SN/A
400443SN/A    elf_end(elf);
401443SN/A
402443SN/A    return found;
40312SN/A}
40412SN/A
40512SN/Abool
4063812Ssaidi@eecs.umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
407468SN/A{
4087581SAli.Saidi@arm.com    return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
409468SN/A}
410468SN/A
411468SN/Abool
4123812Ssaidi@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
41312SN/A{
4149810Sguodeyuan@tsinghua.org.cn    bool found_local = loadSomeSymbols(symtab, STB_LOCAL, addrMask);
4159810Sguodeyuan@tsinghua.org.cn    bool found_weak = loadSomeSymbols(symtab, STB_WEAK, addrMask);
4169810Sguodeyuan@tsinghua.org.cn    return found_local || found_weak;
41712SN/A}
4183917Ssaidi@eecs.umich.edu
4195090Sgblack@eecs.umich.edubool
4209641Sguodeyuan@tsinghua.org.cnElfObject::loadWeakSymbols(SymbolTable *symtab, Addr addrMask)
4219641Sguodeyuan@tsinghua.org.cn{
4229641Sguodeyuan@tsinghua.org.cn    return loadSomeSymbols(symtab, STB_WEAK, addrMask);
4239641Sguodeyuan@tsinghua.org.cn}
4249641Sguodeyuan@tsinghua.org.cn
4259641Sguodeyuan@tsinghua.org.cnbool
4268852Sandreas.hansson@arm.comElfObject::loadSections(PortProxy& memProxy, Addr addrMask)
4275090Sgblack@eecs.umich.edu{
4288706Sandreas.hansson@arm.com    if (!ObjectFile::loadSections(memProxy, addrMask))
4295090Sgblack@eecs.umich.edu        return false;
4305090Sgblack@eecs.umich.edu
4315090Sgblack@eecs.umich.edu    vector<Segment>::iterator extraIt;
4325090Sgblack@eecs.umich.edu    for (extraIt = extraSegments.begin();
4335090Sgblack@eecs.umich.edu            extraIt != extraSegments.end(); extraIt++) {
4348706Sandreas.hansson@arm.com        if (!loadSection(&(*extraIt), memProxy, addrMask)) {
4355090Sgblack@eecs.umich.edu            return false;
4365090Sgblack@eecs.umich.edu        }
4375090Sgblack@eecs.umich.edu    }
4385090Sgblack@eecs.umich.edu    return true;
4395090Sgblack@eecs.umich.edu}
4405090Sgblack@eecs.umich.edu
4415070Ssaidi@eecs.umich.eduvoid
4425070Ssaidi@eecs.umich.eduElfObject::getSections()
4433917Ssaidi@eecs.umich.edu{
4443917Ssaidi@eecs.umich.edu    Elf *elf;
4453917Ssaidi@eecs.umich.edu    int sec_idx = 1; // there is a 0 but it is nothing, go figure
4463917Ssaidi@eecs.umich.edu    Elf_Scn *section;
4473917Ssaidi@eecs.umich.edu    GElf_Shdr shdr;
4483917Ssaidi@eecs.umich.edu
4493917Ssaidi@eecs.umich.edu    GElf_Ehdr ehdr;
4503917Ssaidi@eecs.umich.edu
4515070Ssaidi@eecs.umich.edu    assert(!sectionNames.size());
4525070Ssaidi@eecs.umich.edu
4533917Ssaidi@eecs.umich.edu    // check that header matches library version
4543917Ssaidi@eecs.umich.edu    if (elf_version(EV_CURRENT) == EV_NONE)
4553917Ssaidi@eecs.umich.edu        panic("wrong elf version number!");
4563917Ssaidi@eecs.umich.edu
4573917Ssaidi@eecs.umich.edu    // get a pointer to elf structure
4583917Ssaidi@eecs.umich.edu    elf = elf_memory((char*)fileData,len);
4593917Ssaidi@eecs.umich.edu    assert(elf != NULL);
4603917Ssaidi@eecs.umich.edu
4613917Ssaidi@eecs.umich.edu    // Check that we actually have a elf file
4623917Ssaidi@eecs.umich.edu    if (gelf_getehdr(elf, &ehdr) ==0) {
4633917Ssaidi@eecs.umich.edu        panic("Not ELF, shouldn't be here");
4643917Ssaidi@eecs.umich.edu    }
4653917Ssaidi@eecs.umich.edu
4663917Ssaidi@eecs.umich.edu    // Get the first section
4673917Ssaidi@eecs.umich.edu    section = elf_getscn(elf, sec_idx);
4683917Ssaidi@eecs.umich.edu
4693917Ssaidi@eecs.umich.edu    // While there are no more sections
4703917Ssaidi@eecs.umich.edu    while (section != NULL) {
4713917Ssaidi@eecs.umich.edu        gelf_getshdr(section, &shdr);
4725070Ssaidi@eecs.umich.edu        sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
4733917Ssaidi@eecs.umich.edu        section = elf_getscn(elf, ++sec_idx);
4743917Ssaidi@eecs.umich.edu    } // while sections
4753917Ssaidi@eecs.umich.edu}
4763917Ssaidi@eecs.umich.edu
4775070Ssaidi@eecs.umich.edubool
4785070Ssaidi@eecs.umich.eduElfObject::sectionExists(string sec)
4795070Ssaidi@eecs.umich.edu{
4805070Ssaidi@eecs.umich.edu    if (!sectionNames.size())
4815070Ssaidi@eecs.umich.edu        getSections();
4825070Ssaidi@eecs.umich.edu    return sectionNames.find(sec) != sectionNames.end();
4835070Ssaidi@eecs.umich.edu}
4843917Ssaidi@eecs.umich.edu
4855070Ssaidi@eecs.umich.edu
486