elf_object.cc revision 8852:c744483edfcf
17584SAli.Saidi@arm.com/*
211011SAndreas.Sandberg@ARM.com * Copyright (c) 2003-2005 The Regents of The University of Michigan
37584SAli.Saidi@arm.com * All rights reserved.
47584SAli.Saidi@arm.com *
57584SAli.Saidi@arm.com * Redistribution and use in source and binary forms, with or without
67584SAli.Saidi@arm.com * modification, are permitted provided that the following conditions are
77584SAli.Saidi@arm.com * met: redistributions of source code must retain the above copyright
87584SAli.Saidi@arm.com * notice, this list of conditions and the following disclaimer;
97584SAli.Saidi@arm.com * redistributions in binary form must reproduce the above copyright
107584SAli.Saidi@arm.com * notice, this list of conditions and the following disclaimer in the
117584SAli.Saidi@arm.com * documentation and/or other materials provided with the distribution;
127584SAli.Saidi@arm.com * neither the name of the copyright holders nor the names of its
137584SAli.Saidi@arm.com * contributors may be used to endorse or promote products derived from
147584SAli.Saidi@arm.com * this software without specific prior written permission.
157584SAli.Saidi@arm.com *
167584SAli.Saidi@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177584SAli.Saidi@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187584SAli.Saidi@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197584SAli.Saidi@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207584SAli.Saidi@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217584SAli.Saidi@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227584SAli.Saidi@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237584SAli.Saidi@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247584SAli.Saidi@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257584SAli.Saidi@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267584SAli.Saidi@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277584SAli.Saidi@arm.com *
287584SAli.Saidi@arm.com * Authors: Steve Reinhardt
297584SAli.Saidi@arm.com *          Ali Saidi
307584SAli.Saidi@arm.com */
317584SAli.Saidi@arm.com
327584SAli.Saidi@arm.com#include <cassert>
337584SAli.Saidi@arm.com#include <string>
347584SAli.Saidi@arm.com
357584SAli.Saidi@arm.com#include "base/loader/elf_object.hh"
367584SAli.Saidi@arm.com#include "base/loader/symtab.hh"
377584SAli.Saidi@arm.com#include "base/bitfield.hh"
387584SAli.Saidi@arm.com#include "base/misc.hh"
397584SAli.Saidi@arm.com#include "base/trace.hh"
407584SAli.Saidi@arm.com#include "debug/Loader.hh"
417584SAli.Saidi@arm.com#include "sim/byteswap.hh"
427584SAli.Saidi@arm.com#include "gelf.h"
437950SAli.Saidi@ARM.com
447584SAli.Saidi@arm.comusing namespace std;
457584SAli.Saidi@arm.com
4611011SAndreas.Sandberg@ARM.comObjectFile *
477584SAli.Saidi@arm.comElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
487584SAli.Saidi@arm.com{
497584SAli.Saidi@arm.com    Elf *elf;
507584SAli.Saidi@arm.com    GElf_Ehdr ehdr;
517584SAli.Saidi@arm.com    Arch arch = UnknownArch;
527584SAli.Saidi@arm.com    OpSys opSys = UnknownOpSys;
537584SAli.Saidi@arm.com
5411011SAndreas.Sandberg@ARM.com    // check that header matches library version
5511011SAndreas.Sandberg@ARM.com    if (elf_version(EV_CURRENT) == EV_NONE)
5611011SAndreas.Sandberg@ARM.com        panic("wrong elf version number!");
5711011SAndreas.Sandberg@ARM.com
5811011SAndreas.Sandberg@ARM.com    // get a pointer to elf structure
5911011SAndreas.Sandberg@ARM.com    elf = elf_memory((char*)data,len);
6011011SAndreas.Sandberg@ARM.com    // will only fail if fd is invalid
6111011SAndreas.Sandberg@ARM.com    assert(elf != NULL);
6211011SAndreas.Sandberg@ARM.com
6311011SAndreas.Sandberg@ARM.com    // Check that we actually have a elf file
6411011SAndreas.Sandberg@ARM.com    if (gelf_getehdr(elf, &ehdr) ==0) {
6511011SAndreas.Sandberg@ARM.com        DPRINTFR(Loader, "Not ELF\n");
6611011SAndreas.Sandberg@ARM.com        elf_end(elf);
6711011SAndreas.Sandberg@ARM.com        return NULL;
6811011SAndreas.Sandberg@ARM.com    } else {
6911011SAndreas.Sandberg@ARM.com        //Detect the architecture
7011011SAndreas.Sandberg@ARM.com        //Since we don't know how to check for alpha right now, we'll
7111011SAndreas.Sandberg@ARM.com        //just assume if it wasn't something else and it's 64 bit, that's
7211011SAndreas.Sandberg@ARM.com        //what it must be.
7311011SAndreas.Sandberg@ARM.com        if (ehdr.e_machine == EM_SPARC64 ||
7411011SAndreas.Sandberg@ARM.com                (ehdr.e_machine == EM_SPARC &&
7511011SAndreas.Sandberg@ARM.com                 ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
7611011SAndreas.Sandberg@ARM.com                ehdr.e_machine == EM_SPARCV9) {
7711011SAndreas.Sandberg@ARM.com            arch = ObjectFile::SPARC64;
7811011SAndreas.Sandberg@ARM.com        } else if (ehdr.e_machine == EM_SPARC32PLUS ||
7911011SAndreas.Sandberg@ARM.com                        (ehdr.e_machine == EM_SPARC &&
8011011SAndreas.Sandberg@ARM.com                         ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
8111011SAndreas.Sandberg@ARM.com            arch = ObjectFile::SPARC32;
8211011SAndreas.Sandberg@ARM.com        } else if (ehdr.e_machine == EM_MIPS
8311011SAndreas.Sandberg@ARM.com                && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
8411011SAndreas.Sandberg@ARM.com            if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
857584SAli.Saidi@arm.com                arch = ObjectFile::Mips;
867584SAli.Saidi@arm.com            } else {
877584SAli.Saidi@arm.com                fatal("The binary you're trying to load is compiled for big "
887584SAli.Saidi@arm.com                        "endian MIPS. M5\nonly supports little endian MIPS. "
897584SAli.Saidi@arm.com                        "Please recompile your binary.\n");
907584SAli.Saidi@arm.com            }
917584SAli.Saidi@arm.com        } else if (ehdr.e_machine == EM_X86_64 &&
927584SAli.Saidi@arm.com                ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
937584SAli.Saidi@arm.com            arch = ObjectFile::X86_64;
947584SAli.Saidi@arm.com        } else if (ehdr.e_machine == EM_386 &&
957584SAli.Saidi@arm.com                ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
967584SAli.Saidi@arm.com            arch = ObjectFile::I386;
977584SAli.Saidi@arm.com        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
987584SAli.Saidi@arm.com            arch = ObjectFile::Alpha;
997584SAli.Saidi@arm.com        } else if (ehdr.e_machine == EM_ARM) {
1007584SAli.Saidi@arm.com            if (bits(ehdr.e_entry, 0)) {
1017584SAli.Saidi@arm.com                arch = ObjectFile::Thumb;
1027584SAli.Saidi@arm.com            } else {
1037584SAli.Saidi@arm.com                arch = ObjectFile::Arm;
1047584SAli.Saidi@arm.com            }
1057584SAli.Saidi@arm.com        } else if (ehdr.e_machine == EM_PPC &&
1067584SAli.Saidi@arm.com                ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1077584SAli.Saidi@arm.com          if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
1087584SAli.Saidi@arm.com                arch = ObjectFile::Power;
1097584SAli.Saidi@arm.com          } else {
1107584SAli.Saidi@arm.com                fatal("The binary you're trying to load is compiled for "
1117584SAli.Saidi@arm.com                        "little endian Power.\nM5 only supports big "
1127584SAli.Saidi@arm.com                        "endian Power. Please recompile your binary.\n");
1138524SAli.Saidi@ARM.com          }
1148524SAli.Saidi@ARM.com        } else if (ehdr.e_machine == EM_PPC64) {
1159958Smatt.evans@arm.com            fatal("The binary you're trying to load is compiled for 64-bit "
1169958Smatt.evans@arm.com                  "Power. M5\n only supports 32-bit Power. Please "
1178524SAli.Saidi@ARM.com                  "recompile your binary.\n");
1187584SAli.Saidi@arm.com        } else {
1197584SAli.Saidi@arm.com            warn("Unknown architecture: %d\n", ehdr.e_machine);
1207584SAli.Saidi@arm.com            arch = ObjectFile::UnknownArch;
1217584SAli.Saidi@arm.com        }
1227584SAli.Saidi@arm.com
1237584SAli.Saidi@arm.com        //Detect the operating system
1247584SAli.Saidi@arm.com        switch (ehdr.e_ident[EI_OSABI])
1257950SAli.Saidi@ARM.com        {
1267950SAli.Saidi@ARM.com
1277950SAli.Saidi@ARM.com          case ELFOSABI_LINUX:
1287950SAli.Saidi@ARM.com            opSys = ObjectFile::Linux;
1297950SAli.Saidi@ARM.com            break;
1307950SAli.Saidi@ARM.com          case ELFOSABI_SOLARIS:
13111011SAndreas.Sandberg@ARM.com            opSys = ObjectFile::Solaris;
13211011SAndreas.Sandberg@ARM.com            break;
13311011SAndreas.Sandberg@ARM.com          case ELFOSABI_TRU64:
13411011SAndreas.Sandberg@ARM.com            opSys = ObjectFile::Tru64;
13511011SAndreas.Sandberg@ARM.com            break;
13611011SAndreas.Sandberg@ARM.com          case ELFOSABI_ARM:
13711011SAndreas.Sandberg@ARM.com            opSys = ObjectFile::LinuxArmOABI;
13811011SAndreas.Sandberg@ARM.com            break;
13911011SAndreas.Sandberg@ARM.com          default:
14011011SAndreas.Sandberg@ARM.com            opSys = ObjectFile::UnknownOpSys;
14111011SAndreas.Sandberg@ARM.com        }
14211011SAndreas.Sandberg@ARM.com
1437950SAli.Saidi@ARM.com        //take a look at the .note.ABI section
1447950SAli.Saidi@ARM.com        //It can let us know what's what.
1458281SAli.Saidi@ARM.com        if (opSys == ObjectFile::UnknownOpSys) {
1468281SAli.Saidi@ARM.com            Elf_Scn *section;
1478281SAli.Saidi@ARM.com            GElf_Shdr shdr;
1488281SAli.Saidi@ARM.com            Elf_Data *data;
1498281SAli.Saidi@ARM.com            uint32_t osAbi;;
1508281SAli.Saidi@ARM.com            int secIdx = 1;
1518281SAli.Saidi@ARM.com
1529958Smatt.evans@arm.com            // Get the first section
1539958Smatt.evans@arm.com            section = elf_getscn(elf, secIdx);
1549958Smatt.evans@arm.com
1559958Smatt.evans@arm.com            // While there are no more sections
1567584SAli.Saidi@arm.com            while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
1577584SAli.Saidi@arm.com                gelf_getshdr(section, &shdr);
1587584SAli.Saidi@arm.com                if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
1597584SAli.Saidi@arm.com                            elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
1607584SAli.Saidi@arm.com                    // we have found a ABI note section
1617584SAli.Saidi@arm.com                    // Check the 5th 32bit word for OS  0 == linux, 1 == hurd,
1627584SAli.Saidi@arm.com                    // 2 == solaris, 3 == freebsd
1637584SAli.Saidi@arm.com                    data = elf_rawdata(section, NULL);
1647584SAli.Saidi@arm.com                    assert(data->d_buf);
1657584SAli.Saidi@arm.com                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1667584SAli.Saidi@arm.com                        osAbi = htole(((uint32_t*)data->d_buf)[4]);
1677584SAli.Saidi@arm.com                    else
1687584SAli.Saidi@arm.com                        osAbi = htobe(((uint32_t*)data->d_buf)[4]);
1697584SAli.Saidi@arm.com
1707584SAli.Saidi@arm.com                    switch(osAbi) {
1717584SAli.Saidi@arm.com                      case 0:
1727584SAli.Saidi@arm.com                        opSys = ObjectFile::Linux;
1737584SAli.Saidi@arm.com                        break;
17411011SAndreas.Sandberg@ARM.com                      case 2:
1757584SAli.Saidi@arm.com                        opSys = ObjectFile::Solaris;
1767584SAli.Saidi@arm.com                        break;
1777584SAli.Saidi@arm.com                    }
1787584SAli.Saidi@arm.com                } // if section found
1797584SAli.Saidi@arm.com                if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1807584SAli.Saidi@arm.com                        opSys = ObjectFile::Solaris;
18111011SAndreas.Sandberg@ARM.com                if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1827584SAli.Saidi@arm.com                        opSys = ObjectFile::Solaris;
18310905Sandreas.sandberg@arm.com
18410905Sandreas.sandberg@arm.com            section = elf_getscn(elf, ++secIdx);
18511011SAndreas.Sandberg@ARM.com            } // while sections
18611011SAndreas.Sandberg@ARM.com        }
18711011SAndreas.Sandberg@ARM.com
18811011SAndreas.Sandberg@ARM.com        ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
18911011SAndreas.Sandberg@ARM.com
19011011SAndreas.Sandberg@ARM.com        //The number of headers in the file
19111011SAndreas.Sandberg@ARM.com        result->_programHeaderCount = ehdr.e_phnum;
19211011SAndreas.Sandberg@ARM.com        //Record the size of each entry
1937584SAli.Saidi@arm.com        result->_programHeaderSize = ehdr.e_phentsize;
1947584SAli.Saidi@arm.com        if(result->_programHeaderCount) //If there is a program header table
19511011SAndreas.Sandberg@ARM.com        {
19611011SAndreas.Sandberg@ARM.com            //Figure out the virtual address of the header table in the
19711011SAndreas.Sandberg@ARM.com            //final memory image. We use the program headers themselves
19811011SAndreas.Sandberg@ARM.com            //to translate from a file offset to the address in the image.
19911011SAndreas.Sandberg@ARM.com            GElf_Phdr phdr;
20011011SAndreas.Sandberg@ARM.com            uint64_t e_phoff = ehdr.e_phoff;
20111011SAndreas.Sandberg@ARM.com            result->_programHeaderTable = 0;
20211011SAndreas.Sandberg@ARM.com            for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
20311011SAndreas.Sandberg@ARM.com            {
20411011SAndreas.Sandberg@ARM.com                gelf_getphdr(elf, hdrnum, &phdr);
20511011SAndreas.Sandberg@ARM.com                //Check if we've found the segment with the headers in it
20611011SAndreas.Sandberg@ARM.com                if(phdr.p_offset <= e_phoff &&
20711011SAndreas.Sandberg@ARM.com                        phdr.p_offset + phdr.p_filesz > e_phoff)
20811011SAndreas.Sandberg@ARM.com                {
20911011SAndreas.Sandberg@ARM.com                    result->_programHeaderTable = phdr.p_paddr + e_phoff;
21011011SAndreas.Sandberg@ARM.com                    break;
21111011SAndreas.Sandberg@ARM.com                }
21211011SAndreas.Sandberg@ARM.com            }
21311011SAndreas.Sandberg@ARM.com        }
21411011SAndreas.Sandberg@ARM.com        else
21511011SAndreas.Sandberg@ARM.com            result->_programHeaderTable = 0;
21611011SAndreas.Sandberg@ARM.com
21711011SAndreas.Sandberg@ARM.com
21811011SAndreas.Sandberg@ARM.com        elf_end(elf);
21911011SAndreas.Sandberg@ARM.com        return result;
22011011SAndreas.Sandberg@ARM.com    }
22111011SAndreas.Sandberg@ARM.com}
2227584SAli.Saidi@arm.com
2237584SAli.Saidi@arm.com
224ElfObject::ElfObject(const string &_filename, int _fd,
225                     size_t _len, uint8_t *_data,
226                     Arch _arch, OpSys _opSys)
227    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
228
229{
230    Elf *elf;
231    GElf_Ehdr ehdr;
232
233    // check that header matches library version
234    if (elf_version(EV_CURRENT) == EV_NONE)
235        panic("wrong elf version number!");
236
237    // get a pointer to elf structure
238    elf = elf_memory((char*)fileData,len);
239    // will only fail if fd is invalid
240    assert(elf != NULL);
241
242    // Check that we actually have a elf file
243    if (gelf_getehdr(elf, &ehdr) ==0) {
244        panic("Not ELF, shouldn't be here");
245    }
246
247    entry = ehdr.e_entry;
248
249    // initialize segment sizes to 0 in case they're not present
250    text.size = data.size = bss.size = 0;
251
252    int secIdx = 1;
253    Elf_Scn *section;
254    GElf_Shdr shdr;
255
256    // The first address of some important sections.
257    Addr textSecStart = 0;
258    Addr dataSecStart = 0;
259    Addr bssSecStart = 0;
260
261    // Get the first section
262    section = elf_getscn(elf, secIdx);
263
264    // Find the beginning of the most interesting sections.
265    while (section != NULL) {
266        gelf_getshdr(section, &shdr);
267        char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
268
269        if (secName) {
270            if (!strcmp(".text", secName)) {
271                textSecStart = shdr.sh_addr;
272            } else if (!strcmp(".data", secName)) {
273                dataSecStart = shdr.sh_addr;
274            } else if (!strcmp(".bss", secName)) {
275                bssSecStart = shdr.sh_addr;
276            }
277        } else {
278            Elf_Error errorNum = (Elf_Error)elf_errno();
279            if (errorNum != ELF_E_NONE) {
280                const char *errorMessage = elf_errmsg(errorNum);
281                fatal("Error from libelf: %s.\n", errorMessage);
282            }
283        }
284
285        section = elf_getscn(elf, ++secIdx);
286    }
287
288    // Go through all the segments in the program, record them, and scrape
289    // out information about the text, data, and bss areas needed by other
290    // code.
291    for (int i = 0; i < ehdr.e_phnum; ++i) {
292        GElf_Phdr phdr;
293        if (gelf_getphdr(elf, i, &phdr) == 0) {
294            panic("gelf_getphdr failed for segment %d.", i);
295        }
296
297        // for now we don't care about non-loadable segments
298        if (!(phdr.p_type & PT_LOAD))
299            continue;
300
301        // Check to see if this segment contains the bss section.
302        if (phdr.p_paddr <= bssSecStart &&
303                phdr.p_paddr + phdr.p_memsz > bssSecStart &&
304                phdr.p_memsz - phdr.p_filesz > 0) {
305            bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
306            bss.size = phdr.p_memsz - phdr.p_filesz;
307            bss.fileImage = NULL;
308        }
309
310        // Check to see if this is the text or data segment
311        if (phdr.p_vaddr <= textSecStart &&
312                phdr.p_vaddr + phdr.p_filesz > textSecStart) {
313            text.baseAddr = phdr.p_paddr;
314            text.size = phdr.p_filesz;
315            text.fileImage = fileData + phdr.p_offset;
316        } else if (phdr.p_vaddr <= dataSecStart &&
317                phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
318            data.baseAddr = phdr.p_paddr;
319            data.size = phdr.p_filesz;
320            data.fileImage = fileData + phdr.p_offset;
321        } else {
322            // If it's none of the above but is loadable,
323            // load the filesize worth of data
324            Segment extra;
325            extra.baseAddr = phdr.p_paddr;
326            extra.size = phdr.p_filesz;
327            extra.fileImage = fileData + phdr.p_offset;
328            extraSegments.push_back(extra);
329        }
330    }
331
332    // should have found at least one loadable segment
333    assert(text.size != 0);
334
335    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
336             text.baseAddr, text.size, data.baseAddr, data.size,
337             bss.baseAddr, bss.size);
338
339    elf_end(elf);
340
341    // We will actually read the sections when we need to load them
342}
343
344
345bool
346ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
347{
348    Elf *elf;
349    int sec_idx = 1; // there is a 0 but it is nothing, go figure
350    Elf_Scn *section;
351    GElf_Shdr shdr;
352    Elf_Data *data;
353    int count, ii;
354    bool found = false;
355    GElf_Sym sym;
356
357    if (!symtab)
358        return false;
359
360    // check that header matches library version
361    if (elf_version(EV_CURRENT) == EV_NONE)
362        panic("wrong elf version number!");
363
364    // get a pointer to elf structure
365    elf = elf_memory((char*)fileData,len);
366
367    assert(elf != NULL);
368
369    // Get the first section
370    section = elf_getscn(elf, sec_idx);
371
372    // While there are no more sections
373    while (section != NULL) {
374        gelf_getshdr(section, &shdr);
375
376        if (shdr.sh_type == SHT_SYMTAB) {
377            found = true;
378            data = elf_getdata(section, NULL);
379            count = shdr.sh_size / shdr.sh_entsize;
380            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
381
382            // loop through all the symbols, only loading global ones
383            for (ii = 0; ii < count; ++ii) {
384                gelf_getsym(data, ii, &sym);
385                if (GELF_ST_BIND(sym.st_info) == binding) {
386                    char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
387                    if (sym_name && sym_name[0] != '$') {
388                        DPRINTF(Loader, "Symbol: %-40s value %#x\n",
389                                sym_name, sym.st_value);
390                        symtab->insert(sym.st_value & mask, sym_name);
391                    }
392                }
393            }
394        }
395        ++sec_idx;
396        section = elf_getscn(elf, sec_idx);
397    }
398
399    elf_end(elf);
400
401    return found;
402}
403
404bool
405ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
406{
407    return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
408}
409
410bool
411ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
412{
413    return loadSomeSymbols(symtab, STB_LOCAL, addrMask);
414}
415
416bool
417ElfObject::loadSections(PortProxy& memProxy, Addr addrMask)
418{
419    if (!ObjectFile::loadSections(memProxy, addrMask))
420        return false;
421
422    vector<Segment>::iterator extraIt;
423    for (extraIt = extraSegments.begin();
424            extraIt != extraSegments.end(); extraIt++) {
425        if (!loadSection(&(*extraIt), memProxy, addrMask)) {
426            return false;
427        }
428    }
429    return true;
430}
431
432void
433ElfObject::getSections()
434{
435    Elf *elf;
436    int sec_idx = 1; // there is a 0 but it is nothing, go figure
437    Elf_Scn *section;
438    GElf_Shdr shdr;
439
440    GElf_Ehdr ehdr;
441
442    assert(!sectionNames.size());
443
444    // check that header matches library version
445    if (elf_version(EV_CURRENT) == EV_NONE)
446        panic("wrong elf version number!");
447
448    // get a pointer to elf structure
449    elf = elf_memory((char*)fileData,len);
450    assert(elf != NULL);
451
452    // Check that we actually have a elf file
453    if (gelf_getehdr(elf, &ehdr) ==0) {
454        panic("Not ELF, shouldn't be here");
455    }
456
457    // Get the first section
458    section = elf_getscn(elf, sec_idx);
459
460    // While there are no more sections
461    while (section != NULL) {
462        gelf_getshdr(section, &shdr);
463        sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
464        section = elf_getscn(elf, ++sec_idx);
465    } // while sections
466}
467
468bool
469ElfObject::sectionExists(string sec)
470{
471    if (!sectionNames.size())
472        getSections();
473    return sectionNames.find(sec) != sectionNames.end();
474}
475
476
477