elf_object.cc revision 8229
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" 402423SN/A#include "sim/byteswap.hh" 418229Snate@binkert.org#include "gelf.h" 422423SN/A 4312SN/Ausing namespace std; 4412SN/A 4512SN/AObjectFile * 4612SN/AElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 4712SN/A{ 48443SN/A Elf *elf; 49443SN/A GElf_Ehdr ehdr; 502207SN/A Arch arch = UnknownArch; 512207SN/A OpSys opSys = UnknownOpSys; 52443SN/A 53468SN/A // check that header matches library version 541708SN/A if (elf_version(EV_CURRENT) == EV_NONE) 551708SN/A panic("wrong elf version number!"); 56443SN/A 57468SN/A // get a pointer to elf structure 58443SN/A elf = elf_memory((char*)data,len); 59468SN/A // will only fail if fd is invalid 60443SN/A assert(elf != NULL); 61443SN/A 62468SN/A // Check that we actually have a elf file 63468SN/A if (gelf_getehdr(elf, &ehdr) ==0) { 64443SN/A DPRINTFR(Loader, "Not ELF\n"); 65443SN/A elf_end(elf); 66443SN/A return NULL; 672476SN/A } else { 682207SN/A //Detect the architecture 692207SN/A //Since we don't know how to check for alpha right now, we'll 702207SN/A //just assume if it wasn't something else and it's 64 bit, that's 712207SN/A //what it must be. 722207SN/A if (ehdr.e_machine == EM_SPARC64 || 734111Sgblack@eecs.umich.edu (ehdr.e_machine == EM_SPARC && 744111Sgblack@eecs.umich.edu ehdr.e_ident[EI_CLASS] == ELFCLASS64)|| 752620SN/A ehdr.e_machine == EM_SPARCV9) { 764111Sgblack@eecs.umich.edu arch = ObjectFile::SPARC64; 774111Sgblack@eecs.umich.edu } else if (ehdr.e_machine == EM_SPARC32PLUS || 784111Sgblack@eecs.umich.edu (ehdr.e_machine == EM_SPARC && 794111Sgblack@eecs.umich.edu ehdr.e_ident[EI_CLASS] == ELFCLASS32)) { 804111Sgblack@eecs.umich.edu arch = ObjectFile::SPARC32; 812207SN/A } else if (ehdr.e_machine == EM_MIPS 822207SN/A && ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 835383Sgblack@eecs.umich.edu if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) { 845383Sgblack@eecs.umich.edu arch = ObjectFile::Mips; 855383Sgblack@eecs.umich.edu } else { 865383Sgblack@eecs.umich.edu fatal("The binary you're trying to load is compiled for big " 875383Sgblack@eecs.umich.edu "endian MIPS. M5\nonly supports little endian MIPS. " 885383Sgblack@eecs.umich.edu "Please recompile your binary.\n"); 895383Sgblack@eecs.umich.edu } 904166Sgblack@eecs.umich.edu } else if (ehdr.e_machine == EM_X86_64 && 914166Sgblack@eecs.umich.edu ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 925874Sgblack@eecs.umich.edu arch = ObjectFile::X86_64; 935874Sgblack@eecs.umich.edu } else if (ehdr.e_machine == EM_386 && 945874Sgblack@eecs.umich.edu ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 955874Sgblack@eecs.umich.edu arch = ObjectFile::I386; 962207SN/A } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 972207SN/A arch = ObjectFile::Alpha; 985335Shines@cs.fsu.edu } else if (ehdr.e_machine == EM_ARM) { 997095Sgblack@eecs.umich.edu if (bits(ehdr.e_entry, 0)) { 1007095Sgblack@eecs.umich.edu arch = ObjectFile::Thumb; 1017095Sgblack@eecs.umich.edu } else { 1027095Sgblack@eecs.umich.edu arch = ObjectFile::Arm; 1037095Sgblack@eecs.umich.edu } 1046691Stjones1@inf.ed.ac.uk } else if (ehdr.e_machine == EM_PPC && 1056691Stjones1@inf.ed.ac.uk ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 1066691Stjones1@inf.ed.ac.uk if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) { 1076691Stjones1@inf.ed.ac.uk arch = ObjectFile::Power; 1086691Stjones1@inf.ed.ac.uk } else { 1096691Stjones1@inf.ed.ac.uk fatal("The binary you're trying to load is compiled for " 1106691Stjones1@inf.ed.ac.uk "little endian Power.\nM5 only supports big " 1116691Stjones1@inf.ed.ac.uk "endian Power. Please recompile your binary.\n"); 1126691Stjones1@inf.ed.ac.uk } 1136691Stjones1@inf.ed.ac.uk } else if (ehdr.e_machine == EM_PPC64) { 1146691Stjones1@inf.ed.ac.uk fatal("The binary you're trying to load is compiled for 64-bit " 1156691Stjones1@inf.ed.ac.uk "Power. M5\n only supports 32-bit Power. Please " 1166691Stjones1@inf.ed.ac.uk "recompile your binary.\n"); 1172207SN/A } else { 1182600SN/A warn("Unknown architecture: %d\n", ehdr.e_machine); 1192207SN/A arch = ObjectFile::UnknownArch; 1202207SN/A } 1212207SN/A 1222207SN/A //Detect the operating system 1232207SN/A switch (ehdr.e_ident[EI_OSABI]) 1242207SN/A { 1252238SN/A 1262207SN/A case ELFOSABI_LINUX: 1272207SN/A opSys = ObjectFile::Linux; 1282207SN/A break; 1292207SN/A case ELFOSABI_SOLARIS: 1302207SN/A opSys = ObjectFile::Solaris; 1312238SN/A break; 1322207SN/A case ELFOSABI_TRU64: 1332207SN/A opSys = ObjectFile::Tru64; 1342238SN/A break; 1356392Ssaidi@eecs.umich.edu case ELFOSABI_ARM: 1366392Ssaidi@eecs.umich.edu opSys = ObjectFile::LinuxArmOABI; 1376392Ssaidi@eecs.umich.edu break; 1382207SN/A default: 1392207SN/A opSys = ObjectFile::UnknownOpSys; 1402207SN/A } 1412207SN/A 1422238SN/A //take a look at the .note.ABI section 1432238SN/A //It can let us know what's what. 1442600SN/A if (opSys == ObjectFile::UnknownOpSys) { 1452238SN/A Elf_Scn *section; 1462238SN/A GElf_Shdr shdr; 1472238SN/A Elf_Data *data; 1482238SN/A uint32_t osAbi;; 1492238SN/A int secIdx = 1; 1502238SN/A 1512238SN/A // Get the first section 1522238SN/A section = elf_getscn(elf, secIdx); 1532238SN/A 1542238SN/A // While there are no more sections 1552600SN/A while (section != NULL && opSys == ObjectFile::UnknownOpSys) { 1562238SN/A gelf_getshdr(section, &shdr); 1572238SN/A if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag", 1582238SN/A elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) { 1592238SN/A // we have found a ABI note section 1602238SN/A // Check the 5th 32bit word for OS 0 == linux, 1 == hurd, 1612238SN/A // 2 == solaris, 3 == freebsd 1622238SN/A data = elf_rawdata(section, NULL); 1632238SN/A assert(data->d_buf); 1642238SN/A if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB) 1652238SN/A osAbi = htole(((uint32_t*)data->d_buf)[4]); 1662238SN/A else 1672238SN/A osAbi = htobe(((uint32_t*)data->d_buf)[4]); 1682238SN/A 1692238SN/A switch(osAbi) { 1702238SN/A case 0: 1712238SN/A opSys = ObjectFile::Linux; 1722238SN/A break; 1732238SN/A case 2: 1742238SN/A opSys = ObjectFile::Solaris; 1752238SN/A break; 1762238SN/A } 1772238SN/A } // if section found 1782600SN/A if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 1792600SN/A opSys = ObjectFile::Solaris; 1802600SN/A if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 1812600SN/A opSys = ObjectFile::Solaris; 1822600SN/A 1832238SN/A section = elf_getscn(elf, ++secIdx); 1842238SN/A } // while sections 1852238SN/A } 1862472SN/A 1872976Sgblack@eecs.umich.edu ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys); 1882976Sgblack@eecs.umich.edu 1892976Sgblack@eecs.umich.edu //The number of headers in the file 1902976Sgblack@eecs.umich.edu result->_programHeaderCount = ehdr.e_phnum; 1912976Sgblack@eecs.umich.edu //Record the size of each entry 1922976Sgblack@eecs.umich.edu result->_programHeaderSize = ehdr.e_phentsize; 1932976Sgblack@eecs.umich.edu if(result->_programHeaderCount) //If there is a program header table 1942976Sgblack@eecs.umich.edu { 1952976Sgblack@eecs.umich.edu //Figure out the virtual address of the header table in the 1962976Sgblack@eecs.umich.edu //final memory image. We use the program headers themselves 1972976Sgblack@eecs.umich.edu //to translate from a file offset to the address in the image. 1982976Sgblack@eecs.umich.edu GElf_Phdr phdr; 1992976Sgblack@eecs.umich.edu uint64_t e_phoff = ehdr.e_phoff; 2002976Sgblack@eecs.umich.edu result->_programHeaderTable = 0; 2012976Sgblack@eecs.umich.edu for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++) 2022976Sgblack@eecs.umich.edu { 2032976Sgblack@eecs.umich.edu gelf_getphdr(elf, hdrnum, &phdr); 2042976Sgblack@eecs.umich.edu //Check if we've found the segment with the headers in it 2052976Sgblack@eecs.umich.edu if(phdr.p_offset <= e_phoff && 2062976Sgblack@eecs.umich.edu phdr.p_offset + phdr.p_filesz > e_phoff) 2072976Sgblack@eecs.umich.edu { 2085143Sgblack@eecs.umich.edu result->_programHeaderTable = phdr.p_paddr + e_phoff; 2092976Sgblack@eecs.umich.edu break; 2102976Sgblack@eecs.umich.edu } 2112976Sgblack@eecs.umich.edu } 2122976Sgblack@eecs.umich.edu } 2132976Sgblack@eecs.umich.edu else 2142976Sgblack@eecs.umich.edu result->_programHeaderTable = 0; 2152976Sgblack@eecs.umich.edu 2162976Sgblack@eecs.umich.edu 2172238SN/A elf_end(elf); 2182976Sgblack@eecs.umich.edu return result; 21912SN/A } 22012SN/A} 22112SN/A 22212SN/A 22312SN/AElfObject::ElfObject(const string &_filename, int _fd, 224360SN/A size_t _len, uint8_t *_data, 225360SN/A Arch _arch, OpSys _opSys) 226360SN/A : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 227443SN/A 22812SN/A{ 229443SN/A Elf *elf; 230443SN/A GElf_Ehdr ehdr; 23112SN/A 232468SN/A // check that header matches library version 2331708SN/A if (elf_version(EV_CURRENT) == EV_NONE) 2341708SN/A panic("wrong elf version number!"); 23512SN/A 236468SN/A // get a pointer to elf structure 237443SN/A elf = elf_memory((char*)fileData,len); 238468SN/A // will only fail if fd is invalid 239443SN/A assert(elf != NULL); 24012SN/A 241468SN/A // Check that we actually have a elf file 242468SN/A if (gelf_getehdr(elf, &ehdr) ==0) { 243443SN/A panic("Not ELF, shouldn't be here"); 24412SN/A } 24512SN/A 246468SN/A entry = ehdr.e_entry; 24712SN/A 248468SN/A // initialize segment sizes to 0 in case they're not present 249468SN/A text.size = data.size = bss.size = 0; 250468SN/A 2515090Sgblack@eecs.umich.edu int secIdx = 1; 2525090Sgblack@eecs.umich.edu Elf_Scn *section; 2535090Sgblack@eecs.umich.edu GElf_Shdr shdr; 2545090Sgblack@eecs.umich.edu 2555090Sgblack@eecs.umich.edu // The first address of some important sections. 2565090Sgblack@eecs.umich.edu Addr textSecStart = 0; 2575090Sgblack@eecs.umich.edu Addr dataSecStart = 0; 2585090Sgblack@eecs.umich.edu Addr bssSecStart = 0; 2595090Sgblack@eecs.umich.edu 2605090Sgblack@eecs.umich.edu // Get the first section 2615090Sgblack@eecs.umich.edu section = elf_getscn(elf, secIdx); 2625090Sgblack@eecs.umich.edu 2635090Sgblack@eecs.umich.edu // Find the beginning of the most interesting sections. 2645090Sgblack@eecs.umich.edu while (section != NULL) { 2655090Sgblack@eecs.umich.edu gelf_getshdr(section, &shdr); 2665090Sgblack@eecs.umich.edu char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name); 2675090Sgblack@eecs.umich.edu 2685090Sgblack@eecs.umich.edu if (!strcmp(".text", secName)) { 2695090Sgblack@eecs.umich.edu textSecStart = shdr.sh_addr; 2705090Sgblack@eecs.umich.edu } else if (!strcmp(".data", secName)) { 2715090Sgblack@eecs.umich.edu dataSecStart = shdr.sh_addr; 2725090Sgblack@eecs.umich.edu } else if (!strcmp(".bss", secName)) { 2735090Sgblack@eecs.umich.edu bssSecStart = shdr.sh_addr; 2745090Sgblack@eecs.umich.edu } 2755090Sgblack@eecs.umich.edu 2765090Sgblack@eecs.umich.edu section = elf_getscn(elf, ++secIdx); 2775090Sgblack@eecs.umich.edu } 2785090Sgblack@eecs.umich.edu 2795090Sgblack@eecs.umich.edu // Go through all the segments in the program, record them, and scrape 2805090Sgblack@eecs.umich.edu // out information about the text, data, and bss areas needed by other 2815090Sgblack@eecs.umich.edu // code. 282468SN/A for (int i = 0; i < ehdr.e_phnum; ++i) { 283468SN/A GElf_Phdr phdr; 284468SN/A if (gelf_getphdr(elf, i, &phdr) == 0) { 2855090Sgblack@eecs.umich.edu panic("gelf_getphdr failed for segment %d.", i); 286468SN/A } 287468SN/A 288468SN/A // for now we don't care about non-loadable segments 289468SN/A if (!(phdr.p_type & PT_LOAD)) 290468SN/A continue; 291468SN/A 2925090Sgblack@eecs.umich.edu // Check to see if this segment contains the bss section. 2935143Sgblack@eecs.umich.edu if (phdr.p_paddr <= bssSecStart && 2945143Sgblack@eecs.umich.edu phdr.p_paddr + phdr.p_memsz > bssSecStart && 2955090Sgblack@eecs.umich.edu phdr.p_memsz - phdr.p_filesz > 0) { 2965143Sgblack@eecs.umich.edu bss.baseAddr = phdr.p_paddr + phdr.p_filesz; 2975090Sgblack@eecs.umich.edu bss.size = phdr.p_memsz - phdr.p_filesz; 2985090Sgblack@eecs.umich.edu bss.fileImage = NULL; 2995090Sgblack@eecs.umich.edu } 3005090Sgblack@eecs.umich.edu 3015090Sgblack@eecs.umich.edu // Check to see if this is the text or data segment 3025152Sgblack@eecs.umich.edu if (phdr.p_vaddr <= textSecStart && 3035152Sgblack@eecs.umich.edu phdr.p_vaddr + phdr.p_filesz > textSecStart) { 3045143Sgblack@eecs.umich.edu text.baseAddr = phdr.p_paddr; 305468SN/A text.size = phdr.p_filesz; 3062420SN/A text.fileImage = fileData + phdr.p_offset; 3075152Sgblack@eecs.umich.edu } else if (phdr.p_vaddr <= dataSecStart && 3085152Sgblack@eecs.umich.edu phdr.p_vaddr + phdr.p_filesz > dataSecStart) { 3095143Sgblack@eecs.umich.edu data.baseAddr = phdr.p_paddr; 310468SN/A data.size = phdr.p_filesz; 3112420SN/A data.fileImage = fileData + phdr.p_offset; 3122476SN/A } else { 3135759Shsul@eecs.umich.edu // If it's none of the above but is loadable, 3145759Shsul@eecs.umich.edu // load the filesize worth of data 3155090Sgblack@eecs.umich.edu Segment extra; 3165143Sgblack@eecs.umich.edu extra.baseAddr = phdr.p_paddr; 3175090Sgblack@eecs.umich.edu extra.size = phdr.p_filesz; 3185090Sgblack@eecs.umich.edu extra.fileImage = fileData + phdr.p_offset; 3195090Sgblack@eecs.umich.edu extraSegments.push_back(extra); 320468SN/A } 321468SN/A } 322468SN/A 323468SN/A // should have found at least one loadable segment 324468SN/A assert(text.size != 0); 325468SN/A 326468SN/A DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 327468SN/A text.baseAddr, text.size, data.baseAddr, data.size, 328468SN/A bss.baseAddr, bss.size); 329468SN/A 330443SN/A elf_end(elf); 331443SN/A 332468SN/A // We will actually read the sections when we need to load them 33312SN/A} 33412SN/A 33512SN/A 33612SN/Abool 3377581SAli.Saidi@arm.comElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask) 33812SN/A{ 339443SN/A Elf *elf; 340766SN/A int sec_idx = 1; // there is a 0 but it is nothing, go figure 341443SN/A Elf_Scn *section; 342443SN/A GElf_Shdr shdr; 343443SN/A Elf_Data *data; 344443SN/A int count, ii; 345443SN/A bool found = false; 346443SN/A GElf_Sym sym; 347443SN/A 348443SN/A if (!symtab) 349443SN/A return false; 350443SN/A 351468SN/A // check that header matches library version 3521708SN/A if (elf_version(EV_CURRENT) == EV_NONE) 3531708SN/A panic("wrong elf version number!"); 354443SN/A 355468SN/A // get a pointer to elf structure 356443SN/A elf = elf_memory((char*)fileData,len); 357443SN/A 358443SN/A assert(elf != NULL); 359443SN/A 360468SN/A // Get the first section 361454SN/A section = elf_getscn(elf, sec_idx); 362443SN/A 363468SN/A // While there are no more sections 364468SN/A while (section != NULL) { 365443SN/A gelf_getshdr(section, &shdr); 366443SN/A 367468SN/A if (shdr.sh_type == SHT_SYMTAB) { 368443SN/A found = true; 369443SN/A data = elf_getdata(section, NULL); 370443SN/A count = shdr.sh_size / shdr.sh_entsize; 371443SN/A DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count); 372443SN/A 373468SN/A // loop through all the symbols, only loading global ones 374468SN/A for (ii = 0; ii < count; ++ii) { 375443SN/A gelf_getsym(data, ii, &sym); 376836SN/A if (GELF_ST_BIND(sym.st_info) == binding) { 3777589SAli.Saidi@arm.com char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name); 3787589SAli.Saidi@arm.com if (sym_name && sym_name[0] != '$') { 3797589SAli.Saidi@arm.com DPRINTF(Loader, "Symbol: %-40s value %#x\n", 3807589SAli.Saidi@arm.com sym_name, sym.st_value); 3817589SAli.Saidi@arm.com symtab->insert(sym.st_value & mask, sym_name); 3827589SAli.Saidi@arm.com } 383443SN/A } 384443SN/A } 385443SN/A } 386454SN/A ++sec_idx; 387454SN/A section = elf_getscn(elf, sec_idx); 388443SN/A } 389443SN/A 390443SN/A elf_end(elf); 391443SN/A 392443SN/A return found; 39312SN/A} 39412SN/A 39512SN/Abool 3963812Ssaidi@eecs.umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask) 397468SN/A{ 3987581SAli.Saidi@arm.com return loadSomeSymbols(symtab, STB_GLOBAL, addrMask); 399468SN/A} 400468SN/A 401468SN/Abool 4023812Ssaidi@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask) 40312SN/A{ 4047581SAli.Saidi@arm.com return loadSomeSymbols(symtab, STB_LOCAL, addrMask); 40512SN/A} 4063917Ssaidi@eecs.umich.edu 4075090Sgblack@eecs.umich.edubool 4085090Sgblack@eecs.umich.eduElfObject::loadSections(Port *memPort, Addr addrMask) 4095090Sgblack@eecs.umich.edu{ 4105090Sgblack@eecs.umich.edu if (!ObjectFile::loadSections(memPort, addrMask)) 4115090Sgblack@eecs.umich.edu return false; 4125090Sgblack@eecs.umich.edu 4135090Sgblack@eecs.umich.edu vector<Segment>::iterator extraIt; 4145090Sgblack@eecs.umich.edu for (extraIt = extraSegments.begin(); 4155090Sgblack@eecs.umich.edu extraIt != extraSegments.end(); extraIt++) { 4165090Sgblack@eecs.umich.edu if (!loadSection(&(*extraIt), memPort, addrMask)) { 4175090Sgblack@eecs.umich.edu return false; 4185090Sgblack@eecs.umich.edu } 4195090Sgblack@eecs.umich.edu } 4205090Sgblack@eecs.umich.edu return true; 4215090Sgblack@eecs.umich.edu} 4225090Sgblack@eecs.umich.edu 4235070Ssaidi@eecs.umich.eduvoid 4245070Ssaidi@eecs.umich.eduElfObject::getSections() 4253917Ssaidi@eecs.umich.edu{ 4263917Ssaidi@eecs.umich.edu Elf *elf; 4273917Ssaidi@eecs.umich.edu int sec_idx = 1; // there is a 0 but it is nothing, go figure 4283917Ssaidi@eecs.umich.edu Elf_Scn *section; 4293917Ssaidi@eecs.umich.edu GElf_Shdr shdr; 4303917Ssaidi@eecs.umich.edu 4313917Ssaidi@eecs.umich.edu GElf_Ehdr ehdr; 4323917Ssaidi@eecs.umich.edu 4335070Ssaidi@eecs.umich.edu assert(!sectionNames.size()); 4345070Ssaidi@eecs.umich.edu 4353917Ssaidi@eecs.umich.edu // check that header matches library version 4363917Ssaidi@eecs.umich.edu if (elf_version(EV_CURRENT) == EV_NONE) 4373917Ssaidi@eecs.umich.edu panic("wrong elf version number!"); 4383917Ssaidi@eecs.umich.edu 4393917Ssaidi@eecs.umich.edu // get a pointer to elf structure 4403917Ssaidi@eecs.umich.edu elf = elf_memory((char*)fileData,len); 4413917Ssaidi@eecs.umich.edu assert(elf != NULL); 4423917Ssaidi@eecs.umich.edu 4433917Ssaidi@eecs.umich.edu // Check that we actually have a elf file 4443917Ssaidi@eecs.umich.edu if (gelf_getehdr(elf, &ehdr) ==0) { 4453917Ssaidi@eecs.umich.edu panic("Not ELF, shouldn't be here"); 4463917Ssaidi@eecs.umich.edu } 4473917Ssaidi@eecs.umich.edu 4483917Ssaidi@eecs.umich.edu // Get the first section 4493917Ssaidi@eecs.umich.edu section = elf_getscn(elf, sec_idx); 4503917Ssaidi@eecs.umich.edu 4513917Ssaidi@eecs.umich.edu // While there are no more sections 4523917Ssaidi@eecs.umich.edu while (section != NULL) { 4533917Ssaidi@eecs.umich.edu gelf_getshdr(section, &shdr); 4545070Ssaidi@eecs.umich.edu sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)); 4553917Ssaidi@eecs.umich.edu section = elf_getscn(elf, ++sec_idx); 4563917Ssaidi@eecs.umich.edu } // while sections 4573917Ssaidi@eecs.umich.edu} 4583917Ssaidi@eecs.umich.edu 4595070Ssaidi@eecs.umich.edubool 4605070Ssaidi@eecs.umich.eduElfObject::sectionExists(string sec) 4615070Ssaidi@eecs.umich.edu{ 4625070Ssaidi@eecs.umich.edu if (!sectionNames.size()) 4635070Ssaidi@eecs.umich.edu getSections(); 4645070Ssaidi@eecs.umich.edu return sectionNames.find(sec) != sectionNames.end(); 4655070Ssaidi@eecs.umich.edu} 4663917Ssaidi@eecs.umich.edu 4675070Ssaidi@eecs.umich.edu 468