elf_object.cc revision 10360
112SN/A/*
210037SARM gem5 Developers * Copyright (c) 2011-2013 ARM Limited
310037SARM gem5 Developers * All rights reserved
410037SARM gem5 Developers *
510037SARM gem5 Developers * The license below extends only to copyright in the software and shall
610037SARM gem5 Developers * not be construed as granting a license to any other intellectual
710037SARM gem5 Developers * property including but not limited to intellectual property relating
810037SARM gem5 Developers * to a hardware implementation of the functionality of the software
910037SARM gem5 Developers * licensed hereunder.  You may use the software subject to the license
1010037SARM gem5 Developers * terms below provided that you ensure that this notice is replicated
1110037SARM gem5 Developers * unmodified and in its entirety in all distributions of the software,
1210037SARM gem5 Developers * modified or unmodified, in source code or in binary form.
1310037SARM gem5 Developers *
141762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
1512SN/A * All rights reserved.
1612SN/A *
1712SN/A * Redistribution and use in source and binary forms, with or without
1812SN/A * modification, are permitted provided that the following conditions are
1912SN/A * met: redistributions of source code must retain the above copyright
2012SN/A * notice, this list of conditions and the following disclaimer;
2112SN/A * redistributions in binary form must reproduce the above copyright
2212SN/A * notice, this list of conditions and the following disclaimer in the
2312SN/A * documentation and/or other materials provided with the distribution;
2412SN/A * neither the name of the copyright holders nor the names of its
2512SN/A * contributors may be used to endorse or promote products derived from
2612SN/A * this software without specific prior written permission.
2712SN/A *
2812SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2912SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3012SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3112SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3212SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3312SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3412SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3512SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3612SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3712SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3812SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
412665Ssaidi@eecs.umich.edu *          Ali Saidi
4212SN/A */
4312SN/A
445616Snate@binkert.org#include <cassert>
4512SN/A#include <string>
4612SN/A
4756SN/A#include "base/loader/elf_object.hh"
484484Sbinkertn@umich.edu#include "base/loader/symtab.hh"
498229Snate@binkert.org#include "base/bitfield.hh"
502439SN/A#include "base/misc.hh"
517676Snate@binkert.org#include "base/trace.hh"
528232Snate@binkert.org#include "debug/Loader.hh"
532423SN/A#include "sim/byteswap.hh"
548229Snate@binkert.org#include "gelf.h"
552423SN/A
5612SN/Ausing namespace std;
5712SN/A
5812SN/AObjectFile *
5912SN/AElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
6012SN/A{
61443SN/A    Elf *elf;
62443SN/A    GElf_Ehdr ehdr;
632207SN/A    Arch arch = UnknownArch;
642207SN/A    OpSys opSys = UnknownOpSys;
65443SN/A
66468SN/A    // check that header matches library version
671708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
681708SN/A        panic("wrong elf version number!");
69443SN/A
70468SN/A    // get a pointer to elf structure
71443SN/A    elf = elf_memory((char*)data,len);
72468SN/A    // will only fail if fd is invalid
73443SN/A    assert(elf != NULL);
74443SN/A
75468SN/A    // Check that we actually have a elf file
7610037SARM gem5 Developers    if (gelf_getehdr(elf, &ehdr) == 0) {
77443SN/A        DPRINTFR(Loader, "Not ELF\n");
78443SN/A        elf_end(elf);
79443SN/A        return NULL;
802476SN/A    } else {
812207SN/A        //Detect the architecture
822207SN/A        //Since we don't know how to check for alpha right now, we'll
832207SN/A        //just assume if it wasn't something else and it's 64 bit, that's
842207SN/A        //what it must be.
852207SN/A        if (ehdr.e_machine == EM_SPARC64 ||
864111Sgblack@eecs.umich.edu                (ehdr.e_machine == EM_SPARC &&
874111Sgblack@eecs.umich.edu                 ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
882620SN/A                ehdr.e_machine == EM_SPARCV9) {
894111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC64;
904111Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_SPARC32PLUS ||
914111Sgblack@eecs.umich.edu                        (ehdr.e_machine == EM_SPARC &&
924111Sgblack@eecs.umich.edu                         ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
934111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC32;
942207SN/A        } else if (ehdr.e_machine == EM_MIPS
952207SN/A                && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
965383Sgblack@eecs.umich.edu            if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
975383Sgblack@eecs.umich.edu                arch = ObjectFile::Mips;
985383Sgblack@eecs.umich.edu            } else {
995383Sgblack@eecs.umich.edu                fatal("The binary you're trying to load is compiled for big "
1005383Sgblack@eecs.umich.edu                        "endian MIPS. M5\nonly supports little endian MIPS. "
1015383Sgblack@eecs.umich.edu                        "Please recompile your binary.\n");
1025383Sgblack@eecs.umich.edu            }
1034166Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_X86_64 &&
1044166Sgblack@eecs.umich.edu                ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1055874Sgblack@eecs.umich.edu            arch = ObjectFile::X86_64;
1065874Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_386 &&
1075874Sgblack@eecs.umich.edu                ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1085874Sgblack@eecs.umich.edu            arch = ObjectFile::I386;
10910037SARM gem5 Developers        } else if (ehdr.e_machine == EM_ARM &&
11010037SARM gem5 Developers                ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1117095Sgblack@eecs.umich.edu            if (bits(ehdr.e_entry, 0)) {
1127095Sgblack@eecs.umich.edu                arch = ObjectFile::Thumb;
1137095Sgblack@eecs.umich.edu            } else {
1147095Sgblack@eecs.umich.edu                arch = ObjectFile::Arm;
1157095Sgblack@eecs.umich.edu            }
11610037SARM gem5 Developers        } else if ((ehdr.e_machine == EM_AARCH64) &&
11710037SARM gem5 Developers                ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
11810037SARM gem5 Developers            arch = ObjectFile::Arm64;
11910037SARM gem5 Developers        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
12010037SARM gem5 Developers            arch = ObjectFile::Alpha;
1216691Stjones1@inf.ed.ac.uk        } else if (ehdr.e_machine == EM_PPC &&
1226691Stjones1@inf.ed.ac.uk                ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
12310037SARM gem5 Developers            if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
12410037SARM gem5 Developers                  arch = ObjectFile::Power;
12510037SARM gem5 Developers            } else {
12610037SARM gem5 Developers                  fatal("The binary you're trying to load is compiled for "
1276691Stjones1@inf.ed.ac.uk                        "little endian Power.\nM5 only supports big "
1286691Stjones1@inf.ed.ac.uk                        "endian Power. Please recompile your binary.\n");
12910037SARM gem5 Developers            }
1306691Stjones1@inf.ed.ac.uk        } else if (ehdr.e_machine == EM_PPC64) {
1316691Stjones1@inf.ed.ac.uk            fatal("The binary you're trying to load is compiled for 64-bit "
1326691Stjones1@inf.ed.ac.uk                  "Power. M5\n only supports 32-bit Power. Please "
1336691Stjones1@inf.ed.ac.uk                  "recompile your binary.\n");
1342207SN/A        } else {
1352600SN/A            warn("Unknown architecture: %d\n", ehdr.e_machine);
1362207SN/A            arch = ObjectFile::UnknownArch;
1372207SN/A        }
1382207SN/A
1392207SN/A        //Detect the operating system
14010037SARM gem5 Developers        switch (ehdr.e_ident[EI_OSABI]) {
1412207SN/A          case ELFOSABI_LINUX:
1422207SN/A            opSys = ObjectFile::Linux;
1432207SN/A            break;
1442207SN/A          case ELFOSABI_SOLARIS:
1452207SN/A            opSys = ObjectFile::Solaris;
1462238SN/A            break;
1472207SN/A          case ELFOSABI_TRU64:
1482207SN/A            opSys = ObjectFile::Tru64;
1492238SN/A            break;
1506392Ssaidi@eecs.umich.edu          case ELFOSABI_ARM:
1516392Ssaidi@eecs.umich.edu            opSys = ObjectFile::LinuxArmOABI;
1526392Ssaidi@eecs.umich.edu            break;
1532207SN/A          default:
1542207SN/A            opSys = ObjectFile::UnknownOpSys;
1552207SN/A        }
1562207SN/A
1572238SN/A        //take a look at the .note.ABI section
1582238SN/A        //It can let us know what's what.
1592600SN/A        if (opSys == ObjectFile::UnknownOpSys) {
1602238SN/A            Elf_Scn *section;
1612238SN/A            GElf_Shdr shdr;
1622238SN/A            Elf_Data *data;
1632238SN/A            uint32_t osAbi;;
1642238SN/A            int secIdx = 1;
1652238SN/A
1662238SN/A            // Get the first section
1672238SN/A            section = elf_getscn(elf, secIdx);
1682238SN/A
1692238SN/A            // While there are no more sections
1702600SN/A            while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
1712238SN/A                gelf_getshdr(section, &shdr);
1722238SN/A                if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
1732238SN/A                            elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
1742238SN/A                    // we have found a ABI note section
1752238SN/A                    // Check the 5th 32bit word for OS  0 == linux, 1 == hurd,
1762238SN/A                    // 2 == solaris, 3 == freebsd
1772238SN/A                    data = elf_rawdata(section, NULL);
1782238SN/A                    assert(data->d_buf);
1792238SN/A                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1802238SN/A                        osAbi = htole(((uint32_t*)data->d_buf)[4]);
1812238SN/A                    else
1822238SN/A                        osAbi = htobe(((uint32_t*)data->d_buf)[4]);
1832238SN/A
1842238SN/A                    switch(osAbi) {
1852238SN/A                      case 0:
1862238SN/A                        opSys = ObjectFile::Linux;
1872238SN/A                        break;
1882238SN/A                      case 2:
1892238SN/A                        opSys = ObjectFile::Solaris;
1902238SN/A                        break;
1912238SN/A                    }
1922238SN/A                } // if section found
1932600SN/A                if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1942600SN/A                        opSys = ObjectFile::Solaris;
1952600SN/A                if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1962600SN/A                        opSys = ObjectFile::Solaris;
1972600SN/A
1982238SN/A            section = elf_getscn(elf, ++secIdx);
1992238SN/A            } // while sections
2002238SN/A        }
2012472SN/A
2022976Sgblack@eecs.umich.edu        ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
2032976Sgblack@eecs.umich.edu
2042976Sgblack@eecs.umich.edu        //The number of headers in the file
2052976Sgblack@eecs.umich.edu        result->_programHeaderCount = ehdr.e_phnum;
2062976Sgblack@eecs.umich.edu        //Record the size of each entry
2072976Sgblack@eecs.umich.edu        result->_programHeaderSize = ehdr.e_phentsize;
2082976Sgblack@eecs.umich.edu        if(result->_programHeaderCount) //If there is a program header table
2092976Sgblack@eecs.umich.edu        {
2102976Sgblack@eecs.umich.edu            //Figure out the virtual address of the header table in the
2112976Sgblack@eecs.umich.edu            //final memory image. We use the program headers themselves
2122976Sgblack@eecs.umich.edu            //to translate from a file offset to the address in the image.
2132976Sgblack@eecs.umich.edu            GElf_Phdr phdr;
2142976Sgblack@eecs.umich.edu            uint64_t e_phoff = ehdr.e_phoff;
2152976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
2162976Sgblack@eecs.umich.edu            for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
2172976Sgblack@eecs.umich.edu            {
2182976Sgblack@eecs.umich.edu                gelf_getphdr(elf, hdrnum, &phdr);
2192976Sgblack@eecs.umich.edu                //Check if we've found the segment with the headers in it
2202976Sgblack@eecs.umich.edu                if(phdr.p_offset <= e_phoff &&
2212976Sgblack@eecs.umich.edu                        phdr.p_offset + phdr.p_filesz > e_phoff)
2222976Sgblack@eecs.umich.edu                {
22310037SARM gem5 Developers                    result->_programHeaderTable =
22410037SARM gem5 Developers                        phdr.p_paddr + (e_phoff - phdr.p_offset);
2252976Sgblack@eecs.umich.edu                    break;
2262976Sgblack@eecs.umich.edu                }
2272976Sgblack@eecs.umich.edu            }
2282976Sgblack@eecs.umich.edu        }
2292976Sgblack@eecs.umich.edu        else
2302976Sgblack@eecs.umich.edu            result->_programHeaderTable = 0;
2312976Sgblack@eecs.umich.edu
2322976Sgblack@eecs.umich.edu
2332238SN/A        elf_end(elf);
2342976Sgblack@eecs.umich.edu        return result;
23512SN/A    }
23612SN/A}
23712SN/A
23812SN/A
23912SN/AElfObject::ElfObject(const string &_filename, int _fd,
240360SN/A                     size_t _len, uint8_t *_data,
241360SN/A                     Arch _arch, OpSys _opSys)
24210360Sandreas.hansson@arm.com    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys),
24310360Sandreas.hansson@arm.com      _programHeaderTable(0), _programHeaderSize(0), _programHeaderCount(0)
244443SN/A
24512SN/A{
246443SN/A    Elf *elf;
247443SN/A    GElf_Ehdr ehdr;
24812SN/A
249468SN/A    // check that header matches library version
2501708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
2511708SN/A        panic("wrong elf version number!");
25212SN/A
253468SN/A    // get a pointer to elf structure
254443SN/A    elf = elf_memory((char*)fileData,len);
255468SN/A    // will only fail if fd is invalid
256443SN/A    assert(elf != NULL);
25712SN/A
258468SN/A    // Check that we actually have a elf file
259468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
260443SN/A        panic("Not ELF, shouldn't be here");
26112SN/A    }
26212SN/A
263468SN/A    entry = ehdr.e_entry;
26412SN/A
265468SN/A    // initialize segment sizes to 0 in case they're not present
266468SN/A    text.size = data.size = bss.size = 0;
2679186SAli.Saidi@ARM.com    text.baseAddr = data.baseAddr = bss.baseAddr = 0;
268468SN/A
2695090Sgblack@eecs.umich.edu    int secIdx = 1;
2705090Sgblack@eecs.umich.edu    Elf_Scn *section;
2715090Sgblack@eecs.umich.edu    GElf_Shdr shdr;
2725090Sgblack@eecs.umich.edu
2735090Sgblack@eecs.umich.edu    // The first address of some important sections.
2745090Sgblack@eecs.umich.edu    Addr textSecStart = 0;
2755090Sgblack@eecs.umich.edu    Addr dataSecStart = 0;
2765090Sgblack@eecs.umich.edu    Addr bssSecStart = 0;
2775090Sgblack@eecs.umich.edu
2785090Sgblack@eecs.umich.edu    // Get the first section
2795090Sgblack@eecs.umich.edu    section = elf_getscn(elf, secIdx);
2805090Sgblack@eecs.umich.edu
2815090Sgblack@eecs.umich.edu    // Find the beginning of the most interesting sections.
2825090Sgblack@eecs.umich.edu    while (section != NULL) {
2835090Sgblack@eecs.umich.edu        gelf_getshdr(section, &shdr);
2845090Sgblack@eecs.umich.edu        char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
2855090Sgblack@eecs.umich.edu
2868350Sgblack@eecs.umich.edu        if (secName) {
2878350Sgblack@eecs.umich.edu            if (!strcmp(".text", secName)) {
2888350Sgblack@eecs.umich.edu                textSecStart = shdr.sh_addr;
2898350Sgblack@eecs.umich.edu            } else if (!strcmp(".data", secName)) {
2908350Sgblack@eecs.umich.edu                dataSecStart = shdr.sh_addr;
2918350Sgblack@eecs.umich.edu            } else if (!strcmp(".bss", secName)) {
2928350Sgblack@eecs.umich.edu                bssSecStart = shdr.sh_addr;
2938350Sgblack@eecs.umich.edu            }
2948350Sgblack@eecs.umich.edu        } else {
2958350Sgblack@eecs.umich.edu            Elf_Error errorNum = (Elf_Error)elf_errno();
2968350Sgblack@eecs.umich.edu            if (errorNum != ELF_E_NONE) {
2978350Sgblack@eecs.umich.edu                const char *errorMessage = elf_errmsg(errorNum);
2988350Sgblack@eecs.umich.edu                fatal("Error from libelf: %s.\n", errorMessage);
2998350Sgblack@eecs.umich.edu            }
3005090Sgblack@eecs.umich.edu        }
3015090Sgblack@eecs.umich.edu
3025090Sgblack@eecs.umich.edu        section = elf_getscn(elf, ++secIdx);
3035090Sgblack@eecs.umich.edu    }
3045090Sgblack@eecs.umich.edu
3055090Sgblack@eecs.umich.edu    // Go through all the segments in the program, record them, and scrape
3065090Sgblack@eecs.umich.edu    // out information about the text, data, and bss areas needed by other
3075090Sgblack@eecs.umich.edu    // code.
308468SN/A    for (int i = 0; i < ehdr.e_phnum; ++i) {
309468SN/A        GElf_Phdr phdr;
310468SN/A        if (gelf_getphdr(elf, i, &phdr) == 0) {
3115090Sgblack@eecs.umich.edu            panic("gelf_getphdr failed for segment %d.", i);
312468SN/A        }
313468SN/A
314468SN/A        // for now we don't care about non-loadable segments
315468SN/A        if (!(phdr.p_type & PT_LOAD))
316468SN/A            continue;
317468SN/A
3185090Sgblack@eecs.umich.edu        // Check to see if this segment contains the bss section.
3195143Sgblack@eecs.umich.edu        if (phdr.p_paddr <= bssSecStart &&
3205143Sgblack@eecs.umich.edu                phdr.p_paddr + phdr.p_memsz > bssSecStart &&
3215090Sgblack@eecs.umich.edu                phdr.p_memsz - phdr.p_filesz > 0) {
3225143Sgblack@eecs.umich.edu            bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
3235090Sgblack@eecs.umich.edu            bss.size = phdr.p_memsz - phdr.p_filesz;
3245090Sgblack@eecs.umich.edu            bss.fileImage = NULL;
3255090Sgblack@eecs.umich.edu        }
3265090Sgblack@eecs.umich.edu
3275090Sgblack@eecs.umich.edu        // Check to see if this is the text or data segment
3285152Sgblack@eecs.umich.edu        if (phdr.p_vaddr <= textSecStart &&
3295152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > textSecStart) {
3305143Sgblack@eecs.umich.edu            text.baseAddr = phdr.p_paddr;
331468SN/A            text.size = phdr.p_filesz;
3322420SN/A            text.fileImage = fileData + phdr.p_offset;
3335152Sgblack@eecs.umich.edu        } else if (phdr.p_vaddr <= dataSecStart &&
3345152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
3355143Sgblack@eecs.umich.edu            data.baseAddr = phdr.p_paddr;
336468SN/A            data.size = phdr.p_filesz;
3372420SN/A            data.fileImage = fileData + phdr.p_offset;
3382476SN/A        } else {
3395759Shsul@eecs.umich.edu            // If it's none of the above but is loadable,
3405759Shsul@eecs.umich.edu            // load the filesize worth of data
3415090Sgblack@eecs.umich.edu            Segment extra;
3425143Sgblack@eecs.umich.edu            extra.baseAddr = phdr.p_paddr;
3435090Sgblack@eecs.umich.edu            extra.size = phdr.p_filesz;
3445090Sgblack@eecs.umich.edu            extra.fileImage = fileData + phdr.p_offset;
3455090Sgblack@eecs.umich.edu            extraSegments.push_back(extra);
346468SN/A        }
347468SN/A    }
348468SN/A
349468SN/A    // should have found at least one loadable segment
350468SN/A    assert(text.size != 0);
351468SN/A
352468SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
353468SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
354468SN/A             bss.baseAddr, bss.size);
355468SN/A
356443SN/A    elf_end(elf);
357443SN/A
358468SN/A    // We will actually read the sections when we need to load them
35912SN/A}
36012SN/A
36112SN/A
36212SN/Abool
3637581SAli.Saidi@arm.comElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
36412SN/A{
365443SN/A    Elf *elf;
366766SN/A    int sec_idx = 1; // there is a 0 but it is nothing, go figure
367443SN/A    Elf_Scn *section;
368443SN/A    GElf_Shdr shdr;
369443SN/A    Elf_Data *data;
370443SN/A    int count, ii;
371443SN/A    bool found = false;
372443SN/A    GElf_Sym sym;
373443SN/A
374443SN/A    if (!symtab)
375443SN/A        return false;
376443SN/A
377468SN/A    // check that header matches library version
3781708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
3791708SN/A        panic("wrong elf version number!");
380443SN/A
381468SN/A    // get a pointer to elf structure
382443SN/A    elf = elf_memory((char*)fileData,len);
383443SN/A
384443SN/A    assert(elf != NULL);
385443SN/A
386468SN/A    // Get the first section
387454SN/A    section = elf_getscn(elf, sec_idx);
388443SN/A
389468SN/A    // While there are no more sections
390468SN/A    while (section != NULL) {
391443SN/A        gelf_getshdr(section, &shdr);
392443SN/A
393468SN/A        if (shdr.sh_type == SHT_SYMTAB) {
394443SN/A            found = true;
395443SN/A            data = elf_getdata(section, NULL);
396443SN/A            count = shdr.sh_size / shdr.sh_entsize;
397443SN/A            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
398443SN/A
399468SN/A            // loop through all the symbols, only loading global ones
400468SN/A            for (ii = 0; ii < count; ++ii) {
401443SN/A                gelf_getsym(data, ii, &sym);
402836SN/A                if (GELF_ST_BIND(sym.st_info) == binding) {
4037589SAli.Saidi@arm.com                    char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
4047589SAli.Saidi@arm.com                    if (sym_name && sym_name[0] != '$') {
4057589SAli.Saidi@arm.com                        DPRINTF(Loader, "Symbol: %-40s value %#x\n",
4067589SAli.Saidi@arm.com                                sym_name, sym.st_value);
4077589SAli.Saidi@arm.com                        symtab->insert(sym.st_value & mask, sym_name);
4087589SAli.Saidi@arm.com                    }
409443SN/A                }
410443SN/A            }
411443SN/A        }
412454SN/A        ++sec_idx;
413454SN/A        section = elf_getscn(elf, sec_idx);
414443SN/A    }
415443SN/A
416443SN/A    elf_end(elf);
417443SN/A
418443SN/A    return found;
41912SN/A}
42012SN/A
42112SN/Abool
4223812Ssaidi@eecs.umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
423468SN/A{
4247581SAli.Saidi@arm.com    return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
425468SN/A}
426468SN/A
427468SN/Abool
4283812Ssaidi@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
42912SN/A{
4309810Sguodeyuan@tsinghua.org.cn    bool found_local = loadSomeSymbols(symtab, STB_LOCAL, addrMask);
4319810Sguodeyuan@tsinghua.org.cn    bool found_weak = loadSomeSymbols(symtab, STB_WEAK, addrMask);
4329810Sguodeyuan@tsinghua.org.cn    return found_local || found_weak;
43312SN/A}
4343917Ssaidi@eecs.umich.edu
4355090Sgblack@eecs.umich.edubool
4369641Sguodeyuan@tsinghua.org.cnElfObject::loadWeakSymbols(SymbolTable *symtab, Addr addrMask)
4379641Sguodeyuan@tsinghua.org.cn{
4389641Sguodeyuan@tsinghua.org.cn    return loadSomeSymbols(symtab, STB_WEAK, addrMask);
4399641Sguodeyuan@tsinghua.org.cn}
4409641Sguodeyuan@tsinghua.org.cn
4419641Sguodeyuan@tsinghua.org.cnbool
44210037SARM gem5 DevelopersElfObject::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset)
4435090Sgblack@eecs.umich.edu{
44410037SARM gem5 Developers    if (!ObjectFile::loadSections(memProxy, addrMask, offset))
4455090Sgblack@eecs.umich.edu        return false;
4465090Sgblack@eecs.umich.edu
4475090Sgblack@eecs.umich.edu    vector<Segment>::iterator extraIt;
4485090Sgblack@eecs.umich.edu    for (extraIt = extraSegments.begin();
4495090Sgblack@eecs.umich.edu            extraIt != extraSegments.end(); extraIt++) {
45010037SARM gem5 Developers        if (!loadSection(&(*extraIt), memProxy, addrMask, offset)) {
4515090Sgblack@eecs.umich.edu            return false;
4525090Sgblack@eecs.umich.edu        }
4535090Sgblack@eecs.umich.edu    }
4545090Sgblack@eecs.umich.edu    return true;
4555090Sgblack@eecs.umich.edu}
4565090Sgblack@eecs.umich.edu
4575070Ssaidi@eecs.umich.eduvoid
4585070Ssaidi@eecs.umich.eduElfObject::getSections()
4593917Ssaidi@eecs.umich.edu{
4603917Ssaidi@eecs.umich.edu    Elf *elf;
4613917Ssaidi@eecs.umich.edu    int sec_idx = 1; // there is a 0 but it is nothing, go figure
4623917Ssaidi@eecs.umich.edu    Elf_Scn *section;
4633917Ssaidi@eecs.umich.edu    GElf_Shdr shdr;
4643917Ssaidi@eecs.umich.edu
4653917Ssaidi@eecs.umich.edu    GElf_Ehdr ehdr;
4663917Ssaidi@eecs.umich.edu
4675070Ssaidi@eecs.umich.edu    assert(!sectionNames.size());
4685070Ssaidi@eecs.umich.edu
4693917Ssaidi@eecs.umich.edu    // check that header matches library version
4703917Ssaidi@eecs.umich.edu    if (elf_version(EV_CURRENT) == EV_NONE)
4713917Ssaidi@eecs.umich.edu        panic("wrong elf version number!");
4723917Ssaidi@eecs.umich.edu
4733917Ssaidi@eecs.umich.edu    // get a pointer to elf structure
4743917Ssaidi@eecs.umich.edu    elf = elf_memory((char*)fileData,len);
4753917Ssaidi@eecs.umich.edu    assert(elf != NULL);
4763917Ssaidi@eecs.umich.edu
4773917Ssaidi@eecs.umich.edu    // Check that we actually have a elf file
4783917Ssaidi@eecs.umich.edu    if (gelf_getehdr(elf, &ehdr) ==0) {
4793917Ssaidi@eecs.umich.edu        panic("Not ELF, shouldn't be here");
4803917Ssaidi@eecs.umich.edu    }
4813917Ssaidi@eecs.umich.edu
4823917Ssaidi@eecs.umich.edu    // Get the first section
4833917Ssaidi@eecs.umich.edu    section = elf_getscn(elf, sec_idx);
4843917Ssaidi@eecs.umich.edu
4853917Ssaidi@eecs.umich.edu    // While there are no more sections
4863917Ssaidi@eecs.umich.edu    while (section != NULL) {
4873917Ssaidi@eecs.umich.edu        gelf_getshdr(section, &shdr);
4885070Ssaidi@eecs.umich.edu        sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
4893917Ssaidi@eecs.umich.edu        section = elf_getscn(elf, ++sec_idx);
4903917Ssaidi@eecs.umich.edu    } // while sections
4913917Ssaidi@eecs.umich.edu}
4923917Ssaidi@eecs.umich.edu
4935070Ssaidi@eecs.umich.edubool
4945070Ssaidi@eecs.umich.eduElfObject::sectionExists(string sec)
4955070Ssaidi@eecs.umich.edu{
4965070Ssaidi@eecs.umich.edu    if (!sectionNames.size())
4975070Ssaidi@eecs.umich.edu        getSections();
4985070Ssaidi@eecs.umich.edu    return sectionNames.find(sec) != sectionNames.end();
4995070Ssaidi@eecs.umich.edu}
5003917Ssaidi@eecs.umich.edu
5015070Ssaidi@eecs.umich.edu
502