elf_object.cc revision 10037
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)
242360SN/A    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
243443SN/A
24412SN/A{
245443SN/A    Elf *elf;
246443SN/A    GElf_Ehdr ehdr;
24712SN/A
248468SN/A    // check that header matches library version
2491708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
2501708SN/A        panic("wrong elf version number!");
25112SN/A
252468SN/A    // get a pointer to elf structure
253443SN/A    elf = elf_memory((char*)fileData,len);
254468SN/A    // will only fail if fd is invalid
255443SN/A    assert(elf != NULL);
25612SN/A
257468SN/A    // Check that we actually have a elf file
258468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
259443SN/A        panic("Not ELF, shouldn't be here");
26012SN/A    }
26112SN/A
262468SN/A    entry = ehdr.e_entry;
26312SN/A
264468SN/A    // initialize segment sizes to 0 in case they're not present
265468SN/A    text.size = data.size = bss.size = 0;
2669186SAli.Saidi@ARM.com    text.baseAddr = data.baseAddr = bss.baseAddr = 0;
267468SN/A
2685090Sgblack@eecs.umich.edu    int secIdx = 1;
2695090Sgblack@eecs.umich.edu    Elf_Scn *section;
2705090Sgblack@eecs.umich.edu    GElf_Shdr shdr;
2715090Sgblack@eecs.umich.edu
2725090Sgblack@eecs.umich.edu    // The first address of some important sections.
2735090Sgblack@eecs.umich.edu    Addr textSecStart = 0;
2745090Sgblack@eecs.umich.edu    Addr dataSecStart = 0;
2755090Sgblack@eecs.umich.edu    Addr bssSecStart = 0;
2765090Sgblack@eecs.umich.edu
2775090Sgblack@eecs.umich.edu    // Get the first section
2785090Sgblack@eecs.umich.edu    section = elf_getscn(elf, secIdx);
2795090Sgblack@eecs.umich.edu
2805090Sgblack@eecs.umich.edu    // Find the beginning of the most interesting sections.
2815090Sgblack@eecs.umich.edu    while (section != NULL) {
2825090Sgblack@eecs.umich.edu        gelf_getshdr(section, &shdr);
2835090Sgblack@eecs.umich.edu        char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
2845090Sgblack@eecs.umich.edu
2858350Sgblack@eecs.umich.edu        if (secName) {
2868350Sgblack@eecs.umich.edu            if (!strcmp(".text", secName)) {
2878350Sgblack@eecs.umich.edu                textSecStart = shdr.sh_addr;
2888350Sgblack@eecs.umich.edu            } else if (!strcmp(".data", secName)) {
2898350Sgblack@eecs.umich.edu                dataSecStart = shdr.sh_addr;
2908350Sgblack@eecs.umich.edu            } else if (!strcmp(".bss", secName)) {
2918350Sgblack@eecs.umich.edu                bssSecStart = shdr.sh_addr;
2928350Sgblack@eecs.umich.edu            }
2938350Sgblack@eecs.umich.edu        } else {
2948350Sgblack@eecs.umich.edu            Elf_Error errorNum = (Elf_Error)elf_errno();
2958350Sgblack@eecs.umich.edu            if (errorNum != ELF_E_NONE) {
2968350Sgblack@eecs.umich.edu                const char *errorMessage = elf_errmsg(errorNum);
2978350Sgblack@eecs.umich.edu                fatal("Error from libelf: %s.\n", errorMessage);
2988350Sgblack@eecs.umich.edu            }
2995090Sgblack@eecs.umich.edu        }
3005090Sgblack@eecs.umich.edu
3015090Sgblack@eecs.umich.edu        section = elf_getscn(elf, ++secIdx);
3025090Sgblack@eecs.umich.edu    }
3035090Sgblack@eecs.umich.edu
3045090Sgblack@eecs.umich.edu    // Go through all the segments in the program, record them, and scrape
3055090Sgblack@eecs.umich.edu    // out information about the text, data, and bss areas needed by other
3065090Sgblack@eecs.umich.edu    // code.
307468SN/A    for (int i = 0; i < ehdr.e_phnum; ++i) {
308468SN/A        GElf_Phdr phdr;
309468SN/A        if (gelf_getphdr(elf, i, &phdr) == 0) {
3105090Sgblack@eecs.umich.edu            panic("gelf_getphdr failed for segment %d.", i);
311468SN/A        }
312468SN/A
313468SN/A        // for now we don't care about non-loadable segments
314468SN/A        if (!(phdr.p_type & PT_LOAD))
315468SN/A            continue;
316468SN/A
3175090Sgblack@eecs.umich.edu        // Check to see if this segment contains the bss section.
3185143Sgblack@eecs.umich.edu        if (phdr.p_paddr <= bssSecStart &&
3195143Sgblack@eecs.umich.edu                phdr.p_paddr + phdr.p_memsz > bssSecStart &&
3205090Sgblack@eecs.umich.edu                phdr.p_memsz - phdr.p_filesz > 0) {
3215143Sgblack@eecs.umich.edu            bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
3225090Sgblack@eecs.umich.edu            bss.size = phdr.p_memsz - phdr.p_filesz;
3235090Sgblack@eecs.umich.edu            bss.fileImage = NULL;
3245090Sgblack@eecs.umich.edu        }
3255090Sgblack@eecs.umich.edu
3265090Sgblack@eecs.umich.edu        // Check to see if this is the text or data segment
3275152Sgblack@eecs.umich.edu        if (phdr.p_vaddr <= textSecStart &&
3285152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > textSecStart) {
3295143Sgblack@eecs.umich.edu            text.baseAddr = phdr.p_paddr;
330468SN/A            text.size = phdr.p_filesz;
3312420SN/A            text.fileImage = fileData + phdr.p_offset;
3325152Sgblack@eecs.umich.edu        } else if (phdr.p_vaddr <= dataSecStart &&
3335152Sgblack@eecs.umich.edu                phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
3345143Sgblack@eecs.umich.edu            data.baseAddr = phdr.p_paddr;
335468SN/A            data.size = phdr.p_filesz;
3362420SN/A            data.fileImage = fileData + phdr.p_offset;
3372476SN/A        } else {
3385759Shsul@eecs.umich.edu            // If it's none of the above but is loadable,
3395759Shsul@eecs.umich.edu            // load the filesize worth of data
3405090Sgblack@eecs.umich.edu            Segment extra;
3415143Sgblack@eecs.umich.edu            extra.baseAddr = phdr.p_paddr;
3425090Sgblack@eecs.umich.edu            extra.size = phdr.p_filesz;
3435090Sgblack@eecs.umich.edu            extra.fileImage = fileData + phdr.p_offset;
3445090Sgblack@eecs.umich.edu            extraSegments.push_back(extra);
345468SN/A        }
346468SN/A    }
347468SN/A
348468SN/A    // should have found at least one loadable segment
349468SN/A    assert(text.size != 0);
350468SN/A
351468SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
352468SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
353468SN/A             bss.baseAddr, bss.size);
354468SN/A
355443SN/A    elf_end(elf);
356443SN/A
357468SN/A    // We will actually read the sections when we need to load them
35812SN/A}
35912SN/A
36012SN/A
36112SN/Abool
3627581SAli.Saidi@arm.comElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
36312SN/A{
364443SN/A    Elf *elf;
365766SN/A    int sec_idx = 1; // there is a 0 but it is nothing, go figure
366443SN/A    Elf_Scn *section;
367443SN/A    GElf_Shdr shdr;
368443SN/A    Elf_Data *data;
369443SN/A    int count, ii;
370443SN/A    bool found = false;
371443SN/A    GElf_Sym sym;
372443SN/A
373443SN/A    if (!symtab)
374443SN/A        return false;
375443SN/A
376468SN/A    // check that header matches library version
3771708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
3781708SN/A        panic("wrong elf version number!");
379443SN/A
380468SN/A    // get a pointer to elf structure
381443SN/A    elf = elf_memory((char*)fileData,len);
382443SN/A
383443SN/A    assert(elf != NULL);
384443SN/A
385468SN/A    // Get the first section
386454SN/A    section = elf_getscn(elf, sec_idx);
387443SN/A
388468SN/A    // While there are no more sections
389468SN/A    while (section != NULL) {
390443SN/A        gelf_getshdr(section, &shdr);
391443SN/A
392468SN/A        if (shdr.sh_type == SHT_SYMTAB) {
393443SN/A            found = true;
394443SN/A            data = elf_getdata(section, NULL);
395443SN/A            count = shdr.sh_size / shdr.sh_entsize;
396443SN/A            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
397443SN/A
398468SN/A            // loop through all the symbols, only loading global ones
399468SN/A            for (ii = 0; ii < count; ++ii) {
400443SN/A                gelf_getsym(data, ii, &sym);
401836SN/A                if (GELF_ST_BIND(sym.st_info) == binding) {
4027589SAli.Saidi@arm.com                    char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
4037589SAli.Saidi@arm.com                    if (sym_name && sym_name[0] != '$') {
4047589SAli.Saidi@arm.com                        DPRINTF(Loader, "Symbol: %-40s value %#x\n",
4057589SAli.Saidi@arm.com                                sym_name, sym.st_value);
4067589SAli.Saidi@arm.com                        symtab->insert(sym.st_value & mask, sym_name);
4077589SAli.Saidi@arm.com                    }
408443SN/A                }
409443SN/A            }
410443SN/A        }
411454SN/A        ++sec_idx;
412454SN/A        section = elf_getscn(elf, sec_idx);
413443SN/A    }
414443SN/A
415443SN/A    elf_end(elf);
416443SN/A
417443SN/A    return found;
41812SN/A}
41912SN/A
42012SN/Abool
4213812Ssaidi@eecs.umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
422468SN/A{
4237581SAli.Saidi@arm.com    return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
424468SN/A}
425468SN/A
426468SN/Abool
4273812Ssaidi@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
42812SN/A{
4299810Sguodeyuan@tsinghua.org.cn    bool found_local = loadSomeSymbols(symtab, STB_LOCAL, addrMask);
4309810Sguodeyuan@tsinghua.org.cn    bool found_weak = loadSomeSymbols(symtab, STB_WEAK, addrMask);
4319810Sguodeyuan@tsinghua.org.cn    return found_local || found_weak;
43212SN/A}
4333917Ssaidi@eecs.umich.edu
4345090Sgblack@eecs.umich.edubool
4359641Sguodeyuan@tsinghua.org.cnElfObject::loadWeakSymbols(SymbolTable *symtab, Addr addrMask)
4369641Sguodeyuan@tsinghua.org.cn{
4379641Sguodeyuan@tsinghua.org.cn    return loadSomeSymbols(symtab, STB_WEAK, addrMask);
4389641Sguodeyuan@tsinghua.org.cn}
4399641Sguodeyuan@tsinghua.org.cn
4409641Sguodeyuan@tsinghua.org.cnbool
44110037SARM gem5 DevelopersElfObject::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset)
4425090Sgblack@eecs.umich.edu{
44310037SARM gem5 Developers    if (!ObjectFile::loadSections(memProxy, addrMask, offset))
4445090Sgblack@eecs.umich.edu        return false;
4455090Sgblack@eecs.umich.edu
4465090Sgblack@eecs.umich.edu    vector<Segment>::iterator extraIt;
4475090Sgblack@eecs.umich.edu    for (extraIt = extraSegments.begin();
4485090Sgblack@eecs.umich.edu            extraIt != extraSegments.end(); extraIt++) {
44910037SARM gem5 Developers        if (!loadSection(&(*extraIt), memProxy, addrMask, offset)) {
4505090Sgblack@eecs.umich.edu            return false;
4515090Sgblack@eecs.umich.edu        }
4525090Sgblack@eecs.umich.edu    }
4535090Sgblack@eecs.umich.edu    return true;
4545090Sgblack@eecs.umich.edu}
4555090Sgblack@eecs.umich.edu
4565070Ssaidi@eecs.umich.eduvoid
4575070Ssaidi@eecs.umich.eduElfObject::getSections()
4583917Ssaidi@eecs.umich.edu{
4593917Ssaidi@eecs.umich.edu    Elf *elf;
4603917Ssaidi@eecs.umich.edu    int sec_idx = 1; // there is a 0 but it is nothing, go figure
4613917Ssaidi@eecs.umich.edu    Elf_Scn *section;
4623917Ssaidi@eecs.umich.edu    GElf_Shdr shdr;
4633917Ssaidi@eecs.umich.edu
4643917Ssaidi@eecs.umich.edu    GElf_Ehdr ehdr;
4653917Ssaidi@eecs.umich.edu
4665070Ssaidi@eecs.umich.edu    assert(!sectionNames.size());
4675070Ssaidi@eecs.umich.edu
4683917Ssaidi@eecs.umich.edu    // check that header matches library version
4693917Ssaidi@eecs.umich.edu    if (elf_version(EV_CURRENT) == EV_NONE)
4703917Ssaidi@eecs.umich.edu        panic("wrong elf version number!");
4713917Ssaidi@eecs.umich.edu
4723917Ssaidi@eecs.umich.edu    // get a pointer to elf structure
4733917Ssaidi@eecs.umich.edu    elf = elf_memory((char*)fileData,len);
4743917Ssaidi@eecs.umich.edu    assert(elf != NULL);
4753917Ssaidi@eecs.umich.edu
4763917Ssaidi@eecs.umich.edu    // Check that we actually have a elf file
4773917Ssaidi@eecs.umich.edu    if (gelf_getehdr(elf, &ehdr) ==0) {
4783917Ssaidi@eecs.umich.edu        panic("Not ELF, shouldn't be here");
4793917Ssaidi@eecs.umich.edu    }
4803917Ssaidi@eecs.umich.edu
4813917Ssaidi@eecs.umich.edu    // Get the first section
4823917Ssaidi@eecs.umich.edu    section = elf_getscn(elf, sec_idx);
4833917Ssaidi@eecs.umich.edu
4843917Ssaidi@eecs.umich.edu    // While there are no more sections
4853917Ssaidi@eecs.umich.edu    while (section != NULL) {
4863917Ssaidi@eecs.umich.edu        gelf_getshdr(section, &shdr);
4875070Ssaidi@eecs.umich.edu        sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
4883917Ssaidi@eecs.umich.edu        section = elf_getscn(elf, ++sec_idx);
4893917Ssaidi@eecs.umich.edu    } // while sections
4903917Ssaidi@eecs.umich.edu}
4913917Ssaidi@eecs.umich.edu
4925070Ssaidi@eecs.umich.edubool
4935070Ssaidi@eecs.umich.eduElfObject::sectionExists(string sec)
4945070Ssaidi@eecs.umich.edu{
4955070Ssaidi@eecs.umich.edu    if (!sectionNames.size())
4965070Ssaidi@eecs.umich.edu        getSections();
4975070Ssaidi@eecs.umich.edu    return sectionNames.find(sec) != sectionNames.end();
4985070Ssaidi@eecs.umich.edu}
4993917Ssaidi@eecs.umich.edu
5005070Ssaidi@eecs.umich.edu
501