elf_object.cc revision 5090:ac0d2ccc9c3b
11758SN/A/* 21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 31758SN/A * All rights reserved. 41758SN/A * 51758SN/A * Redistribution and use in source and binary forms, with or without 61758SN/A * modification, are permitted provided that the following conditions are 71758SN/A * met: redistributions of source code must retain the above copyright 81758SN/A * notice, this list of conditions and the following disclaimer; 91758SN/A * redistributions in binary form must reproduce the above copyright 101758SN/A * notice, this list of conditions and the following disclaimer in the 111758SN/A * documentation and/or other materials provided with the distribution; 121758SN/A * neither the name of the copyright holders nor the names of its 131758SN/A * contributors may be used to endorse or promote products derived from 141758SN/A * this software without specific prior written permission. 151758SN/A * 161758SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171758SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181758SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191758SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201758SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211758SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221758SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231758SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241758SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251758SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261758SN/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 301758SN/A */ 312SN/A 322984Sgblack@eecs.umich.edu#include <string> 33732SN/A 343565Sgblack@eecs.umich.edu#include "gelf.h" 35732SN/A 362984Sgblack@eecs.umich.edu#include "base/loader/elf_object.hh" 373536Sgblack@eecs.umich.edu#include "base/loader/symtab.hh" 385953Ssaidi@eecs.umich.edu#include "base/misc.hh" 395882Snate@binkert.org#include "base/trace.hh" // for DPRINTF 40732SN/A#include "sim/byteswap.hh" 41732SN/A 421858SN/Ausing namespace std; 431717SN/A 442683Sktlim@umich.eduObjectFile * 452680Sktlim@umich.eduElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 462710Sstever@eecs.umich.edu{ 472SN/A Elf *elf; 485568Snate@binkert.org GElf_Ehdr ehdr; 495566Snate@binkert.org Arch arch = UnknownArch; 501858SN/A OpSys opSys = UnknownOpSys; 512SN/A 522SN/A // check that header matches library version 532SN/A if (elf_version(EV_CURRENT) == EV_NONE) 542SN/A panic("wrong elf version number!"); 552SN/A 562SN/A // get a pointer to elf structure 575568Snate@binkert.org elf = elf_memory((char*)data,len); 582SN/A // will only fail if fd is invalid 592680Sktlim@umich.edu assert(elf != NULL); 60190SN/A 612680Sktlim@umich.edu // Check that we actually have a elf file 622680Sktlim@umich.edu if (gelf_getehdr(elf, &ehdr) ==0) { 632114SN/A DPRINTFR(Loader, "Not ELF\n"); 645568Snate@binkert.org elf_end(elf); 652700Sktlim@umich.edu return NULL; 664172Ssaidi@eecs.umich.edu } else { 672680Sktlim@umich.edu //Detect the architecture 682700Sktlim@umich.edu //Since we don't know how to check for alpha right now, we'll 692700Sktlim@umich.edu //just assume if it wasn't something else and it's 64 bit, that's 702SN/A //what it must be. 712SN/A if (ehdr.e_machine == EM_SPARC64 || 722SN/A (ehdr.e_machine == EM_SPARC && 731133SN/A ehdr.e_ident[EI_CLASS] == ELFCLASS64)|| 74716SN/A ehdr.e_machine == EM_SPARCV9) { 755568Snate@binkert.org arch = ObjectFile::SPARC64; 76716SN/A } else if (ehdr.e_machine == EM_SPARC32PLUS || 77716SN/A (ehdr.e_machine == EM_SPARC && 78716SN/A ehdr.e_ident[EI_CLASS] == ELFCLASS32)) { 79716SN/A arch = ObjectFile::SPARC32; 80716SN/A } else if (ehdr.e_machine == EM_MIPS 81716SN/A && ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 824172Ssaidi@eecs.umich.edu arch = ObjectFile::Mips; 83716SN/A } else if (ehdr.e_machine == EM_X86_64 && 84716SN/A ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 854172Ssaidi@eecs.umich.edu //In the future, we might want to differentiate between 32 bit 86716SN/A //and 64 bit x86 processes in case there are differences in their 87716SN/A //initial stack frame. 884172Ssaidi@eecs.umich.edu arch = ObjectFile::X86; 89716SN/A } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 90716SN/A arch = ObjectFile::Alpha; 91716SN/A } else { 92716SN/A warn("Unknown architecture: %d\n", ehdr.e_machine); 93716SN/A arch = ObjectFile::UnknownArch; 94716SN/A } 95716SN/A 961133SN/A //Detect the operating system 97716SN/A switch (ehdr.e_ident[EI_OSABI]) 98716SN/A { 99716SN/A 100716SN/A case ELFOSABI_LINUX: 101716SN/A opSys = ObjectFile::Linux; 102716SN/A break; 103716SN/A case ELFOSABI_SOLARIS: 104716SN/A opSys = ObjectFile::Solaris; 105716SN/A break; 106716SN/A case ELFOSABI_TRU64: 107716SN/A opSys = ObjectFile::Tru64; 108716SN/A break; 1094172Ssaidi@eecs.umich.edu default: 1104172Ssaidi@eecs.umich.edu opSys = ObjectFile::UnknownOpSys; 1114172Ssaidi@eecs.umich.edu } 1122147SN/A 113716SN/A //take a look at the .note.ABI section 1144172Ssaidi@eecs.umich.edu //It can let us know what's what. 115716SN/A if (opSys == ObjectFile::UnknownOpSys) { 116716SN/A Elf_Scn *section; 117716SN/A GElf_Shdr shdr; 118716SN/A Elf_Data *data; 1191133SN/A uint32_t osAbi;; 120716SN/A int secIdx = 1; 1215568Snate@binkert.org 122716SN/A // Get the first section 123716SN/A section = elf_getscn(elf, secIdx); 124739SN/A 125739SN/A // While there are no more sections 1262683Sktlim@umich.edu while (section != NULL && opSys == ObjectFile::UnknownOpSys) { 1272683Sktlim@umich.edu gelf_getshdr(section, &shdr); 128716SN/A if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag", 129716SN/A elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) { 1305568Snate@binkert.org // we have found a ABI note section 1315568Snate@binkert.org // Check the 5th 32bit word for OS 0 == linux, 1 == hurd, 1322SN/A // 2 == solaris, 3 == freebsd 1335568Snate@binkert.org data = elf_rawdata(section, NULL); 1342SN/A assert(data->d_buf); 1352SN/A if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB) 1362245SN/A osAbi = htole(((uint32_t*)data->d_buf)[4]); 1375568Snate@binkert.org else 1382245SN/A osAbi = htobe(((uint32_t*)data->d_buf)[4]); 1395568Snate@binkert.org 1402245SN/A switch(osAbi) { 1412245SN/A case 0: 1424997Sgblack@eecs.umich.edu opSys = ObjectFile::Linux; 1434997Sgblack@eecs.umich.edu break; 1444997Sgblack@eecs.umich.edu case 2: 1454997Sgblack@eecs.umich.edu opSys = ObjectFile::Solaris; 1464997Sgblack@eecs.umich.edu break; 1474997Sgblack@eecs.umich.edu } 1484997Sgblack@eecs.umich.edu } // if section found 1495568Snate@binkert.org if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 1504997Sgblack@eecs.umich.edu opSys = ObjectFile::Solaris; 1514997Sgblack@eecs.umich.edu if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 1524997Sgblack@eecs.umich.edu opSys = ObjectFile::Solaris; 1534997Sgblack@eecs.umich.edu 1544997Sgblack@eecs.umich.edu section = elf_getscn(elf, ++secIdx); 1555568Snate@binkert.org } // while sections 1564997Sgblack@eecs.umich.edu } 1574997Sgblack@eecs.umich.edu 1584997Sgblack@eecs.umich.edu ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys); 1594997Sgblack@eecs.umich.edu 1605568Snate@binkert.org //The number of headers in the file 1615568Snate@binkert.org result->_programHeaderCount = ehdr.e_phnum; 1622159SN/A //Record the size of each entry 1635543Ssaidi@eecs.umich.edu result->_programHeaderSize = ehdr.e_phentsize; 1642SN/A if(result->_programHeaderCount) //If there is a program header table 1652SN/A { 1665568Snate@binkert.org //Figure out the virtual address of the header table in the 1675568Snate@binkert.org //final memory image. We use the program headers themselves 1685568Snate@binkert.org //to translate from a file offset to the address in the image. 1695568Snate@binkert.org GElf_Phdr phdr; 1705568Snate@binkert.org uint64_t e_phoff = ehdr.e_phoff; 1715568Snate@binkert.org result->_programHeaderTable = 0; 1725568Snate@binkert.org for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++) 1735568Snate@binkert.org { 1745568Snate@binkert.org gelf_getphdr(elf, hdrnum, &phdr); 1755568Snate@binkert.org //Check if we've found the segment with the headers in it 1765568Snate@binkert.org if(phdr.p_offset <= e_phoff && 1775568Snate@binkert.org phdr.p_offset + phdr.p_filesz > e_phoff) 1785568Snate@binkert.org { 1795568Snate@binkert.org result->_programHeaderTable = phdr.p_vaddr + e_phoff; 1805568Snate@binkert.org break; 1815568Snate@binkert.org } 1825568Snate@binkert.org } 1835568Snate@binkert.org } 1845568Snate@binkert.org else 1855568Snate@binkert.org result->_programHeaderTable = 0; 1865568Snate@binkert.org 1875568Snate@binkert.org 1885568Snate@binkert.org elf_end(elf); 1895568Snate@binkert.org return result; 1905568Snate@binkert.org } 1912SN/A} 1925568Snate@binkert.org 1935568Snate@binkert.org 1945568Snate@binkert.orgElfObject::ElfObject(const string &_filename, int _fd, 1955568Snate@binkert.org size_t _len, uint8_t *_data, 1965568Snate@binkert.org Arch _arch, OpSys _opSys) 1975568Snate@binkert.org : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 1985568Snate@binkert.org 1995568Snate@binkert.org{ 2005568Snate@binkert.org Elf *elf; 2015568Snate@binkert.org GElf_Ehdr ehdr; 2025568Snate@binkert.org 2035568Snate@binkert.org // check that header matches library version 2045568Snate@binkert.org if (elf_version(EV_CURRENT) == EV_NONE) 2055568Snate@binkert.org panic("wrong elf version number!"); 2065568Snate@binkert.org 2075568Snate@binkert.org // get a pointer to elf structure 2085568Snate@binkert.org elf = elf_memory((char*)fileData,len); 2092SN/A // will only fail if fd is invalid 2102SN/A assert(elf != NULL); 2112SN/A 2122SN/A // Check that we actually have a elf file 2135568Snate@binkert.org if (gelf_getehdr(elf, &ehdr) ==0) { 214597SN/A panic("Not ELF, shouldn't be here"); 2152680Sktlim@umich.edu } 216597SN/A 217597SN/A entry = ehdr.e_entry; 2185568Snate@binkert.org 2192SN/A // initialize segment sizes to 0 in case they're not present 2202SN/A text.size = data.size = bss.size = 0; 2212SN/A 2225568Snate@binkert.org int secIdx = 1; 2235568Snate@binkert.org Elf_Scn *section; 2245568Snate@binkert.org GElf_Shdr shdr; 2255568Snate@binkert.org 2265568Snate@binkert.org // The first address of some important sections. 2272SN/A Addr textSecStart = 0; 2282SN/A Addr dataSecStart = 0; 2292SN/A Addr bssSecStart = 0; 2305568Snate@binkert.org 2312SN/A // Get the first section 2325568Snate@binkert.org section = elf_getscn(elf, secIdx); 2335004Sgblack@eecs.umich.edu 2342SN/A // Find the beginning of the most interesting sections. 2355004Sgblack@eecs.umich.edu while (section != NULL) { 2365004Sgblack@eecs.umich.edu gelf_getshdr(section, &shdr); 2375004Sgblack@eecs.umich.edu char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name); 2385004Sgblack@eecs.umich.edu 2395004Sgblack@eecs.umich.edu if (!strcmp(".text", secName)) { 2405004Sgblack@eecs.umich.edu textSecStart = shdr.sh_addr; 2415004Sgblack@eecs.umich.edu } else if (!strcmp(".data", secName)) { 2422SN/A dataSecStart = shdr.sh_addr; 2432SN/A } else if (!strcmp(".bss", secName)) { 2442SN/A bssSecStart = shdr.sh_addr; 2452SN/A } 2465568Snate@binkert.org 2475568Snate@binkert.org section = elf_getscn(elf, ++secIdx); 2485568Snate@binkert.org } 2495568Snate@binkert.org 2505568Snate@binkert.org // Go through all the segments in the program, record them, and scrape 2515568Snate@binkert.org // out information about the text, data, and bss areas needed by other 2525568Snate@binkert.org // code. 2535568Snate@binkert.org for (int i = 0; i < ehdr.e_phnum; ++i) { 2545568Snate@binkert.org GElf_Phdr phdr; 2553468Sgblack@eecs.umich.edu if (gelf_getphdr(elf, i, &phdr) == 0) { 2562SN/A panic("gelf_getphdr failed for segment %d.", i); 2572SN/A } 2582SN/A 2592SN/A // for now we don't care about non-loadable segments 2603468Sgblack@eecs.umich.edu if (!(phdr.p_type & PT_LOAD)) 2612SN/A continue; 2622SN/A 2632SN/A // Check to see if this segment contains the bss section. 2642SN/A if (phdr.p_vaddr <= bssSecStart && 2652SN/A phdr.p_vaddr + phdr.p_memsz > bssSecStart && 2662SN/A phdr.p_memsz - phdr.p_filesz > 0) { 2672SN/A bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 2682SN/A bss.size = phdr.p_memsz - phdr.p_filesz; 2692SN/A bss.fileImage = NULL; 2702SN/A } 2712SN/A 2723468Sgblack@eecs.umich.edu // Check to see if this is the text or data segment 2735568Snate@binkert.org if (phdr.p_vaddr <= textSecStart && 2742SN/A phdr.p_vaddr + phdr.p_filesz > textSecStart) { 275710SN/A text.baseAddr = phdr.p_vaddr; 2762SN/A text.size = phdr.p_filesz; 2772680Sktlim@umich.edu text.fileImage = fileData + phdr.p_offset; 2783468Sgblack@eecs.umich.edu } else if (phdr.p_vaddr <= dataSecStart && 2792SN/A phdr.p_vaddr + phdr.p_filesz > dataSecStart) { 2802SN/A data.baseAddr = phdr.p_vaddr; 2815568Snate@binkert.org data.size = phdr.p_filesz; 2825568Snate@binkert.org data.fileImage = fileData + phdr.p_offset; 2835568Snate@binkert.org } else { 2845568Snate@binkert.org Segment extra; 2855568Snate@binkert.org extra.baseAddr = phdr.p_vaddr; 2865568Snate@binkert.org extra.size = phdr.p_filesz; 2875568Snate@binkert.org extra.fileImage = fileData + phdr.p_offset; 2885568Snate@binkert.org extraSegments.push_back(extra); 2895568Snate@binkert.org } 2905568Snate@binkert.org } 2915568Snate@binkert.org 2925568Snate@binkert.org // should have found at least one loadable segment 2935568Snate@binkert.org assert(text.size != 0); 2945568Snate@binkert.org 2955568Snate@binkert.org DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 2965568Snate@binkert.org text.baseAddr, text.size, data.baseAddr, data.size, 2975568Snate@binkert.org bss.baseAddr, bss.size); 2985568Snate@binkert.org 2995568Snate@binkert.org elf_end(elf); 3005568Snate@binkert.org 3015568Snate@binkert.org // We will actually read the sections when we need to load them 3025568Snate@binkert.org} 3035568Snate@binkert.org 3045568Snate@binkert.org 3055568Snate@binkert.orgbool 3065568Snate@binkert.orgElfObject::loadSomeSymbols(SymbolTable *symtab, int binding) 3075568Snate@binkert.org{ 3082SN/A Elf *elf; 3092SN/A int sec_idx = 1; // there is a 0 but it is nothing, go figure 3102SN/A Elf_Scn *section; 3112SN/A GElf_Shdr shdr; 3125568Snate@binkert.org Elf_Data *data; 313596SN/A int count, ii; 314596SN/A bool found = false; 315596SN/A GElf_Sym sym; 316596SN/A 317596SN/A if (!symtab) 318596SN/A return false; 3195568Snate@binkert.org 320596SN/A // check that header matches library version 321596SN/A if (elf_version(EV_CURRENT) == EV_NONE) 322596SN/A panic("wrong elf version number!"); 323596SN/A 324596SN/A // get a pointer to elf structure 325596SN/A elf = elf_memory((char*)fileData,len); 3265568Snate@binkert.org 3272SN/A assert(elf != NULL); 328710SN/A 3292SN/A // Get the first section 3304997Sgblack@eecs.umich.edu section = elf_getscn(elf, sec_idx); 3312680Sktlim@umich.edu 3322680Sktlim@umich.edu // While there are no more sections 3334997Sgblack@eecs.umich.edu while (section != NULL) { 3342SN/A gelf_getshdr(section, &shdr); 3352SN/A 3365568Snate@binkert.org if (shdr.sh_type == SHT_SYMTAB) { 3372SN/A found = true; 3382SN/A data = elf_getdata(section, NULL); 3392SN/A count = shdr.sh_size / shdr.sh_entsize; 3402SN/A DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count); 3415568Snate@binkert.org 3422SN/A // loop through all the symbols, only loading global ones 3432SN/A for (ii = 0; ii < count; ++ii) { 3442SN/A gelf_getsym(data, ii, &sym); 3452SN/A if (GELF_ST_BIND(sym.st_info) == binding) { 3465568Snate@binkert.org symtab->insert(sym.st_value, 3475568Snate@binkert.org elf_strptr(elf, shdr.sh_link, sym.st_name)); 3482SN/A } 3492SN/A } 3502SN/A } 3512SN/A ++sec_idx; 3525568Snate@binkert.org section = elf_getscn(elf, sec_idx); 3532SN/A } 3546227Snate@binkert.org 3552SN/A elf_end(elf); 3562SN/A 3572SN/A return found; 3582SN/A} 3592SN/A 3604997Sgblack@eecs.umich.edubool 3612680Sktlim@umich.eduElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask) 3622680Sktlim@umich.edu{ 3634997Sgblack@eecs.umich.edu return loadSomeSymbols(symtab, STB_GLOBAL); 3642SN/A} 3652SN/A 3665568Snate@binkert.orgbool 3674997Sgblack@eecs.umich.eduElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask) 3682330SN/A{ 3692680Sktlim@umich.edu return loadSomeSymbols(symtab, STB_LOCAL); 3705568Snate@binkert.org} 3712341SN/A 3722680Sktlim@umich.edubool 3735568Snate@binkert.orgElfObject::loadSections(Port *memPort, Addr addrMask) 3742330SN/A{ 3754997Sgblack@eecs.umich.edu if (!ObjectFile::loadSections(memPort, addrMask)) 3762SN/A return false; 3775568Snate@binkert.org 3782SN/A vector<Segment>::iterator extraIt; 3792SN/A for (extraIt = extraSegments.begin(); 3802SN/A extraIt != extraSegments.end(); extraIt++) { 3812SN/A if (!loadSection(&(*extraIt), memPort, addrMask)) { 3825568Snate@binkert.org return false; 3832SN/A } 3842SN/A } 3852SN/A return true; 3862SN/A} 3875568Snate@binkert.org 3882SN/Avoid 3892SN/AElfObject::getSections() 3902SN/A{ 3912SN/A Elf *elf; 3925568Snate@binkert.org int sec_idx = 1; // there is a 0 but it is nothing, go figure 3932SN/A Elf_Scn *section; 3942SN/A GElf_Shdr shdr; 3952SN/A 3962SN/A GElf_Ehdr ehdr; 3975568Snate@binkert.org 3982SN/A assert(!sectionNames.size()); 3992SN/A 4002SN/A // check that header matches library version 4015568Snate@binkert.org if (elf_version(EV_CURRENT) == EV_NONE) 4025568Snate@binkert.org panic("wrong elf version number!"); 4032SN/A 4042SN/A // get a pointer to elf structure 4052SN/A elf = elf_memory((char*)fileData,len); 4065568Snate@binkert.org assert(elf != NULL); 4072SN/A 4082SN/A // Check that we actually have a elf file 4092SN/A if (gelf_getehdr(elf, &ehdr) ==0) { 4105568Snate@binkert.org panic("Not ELF, shouldn't be here"); 4115568Snate@binkert.org } 4122SN/A 4132SN/A // Get the first section 4142SN/A section = elf_getscn(elf, sec_idx); 4155568Snate@binkert.org 4162SN/A // While there are no more sections 4172SN/A while (section != NULL) { 4182SN/A gelf_getshdr(section, &shdr); 4195568Snate@binkert.org sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)); 4202SN/A section = elf_getscn(elf, ++sec_idx); 4212SN/A } // while sections 4222SN/A} 4235568Snate@binkert.org 4245568Snate@binkert.orgbool 4252SN/AElfObject::sectionExists(string sec) 4262SN/A{ 4272SN/A if (!sectionNames.size()) 4282SN/A getSections(); 4295568Snate@binkert.org return sectionNames.find(sec) != sectionNames.end(); 4305568Snate@binkert.org} 4315568Snate@binkert.org 4325568Snate@binkert.org 4335568Snate@binkert.org