elf_object.cc revision 468
1/* 2 * Copyright (c) 2003 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <string> 30 31// Because of the -Wundef flag we have to do this 32#define __LIBELF_INTERNAL__ 0 33#define __LIBELF64_LINUX 1 34#define __LIBELF_NEED_LINK_H 0 35 36#include <libelf/libelf.h> 37#include <libelf/gelf.h> 38 39#include "base/loader/elf_object.hh" 40 41#include "mem/functional_mem/functional_memory.hh" 42#include "base/loader/symtab.hh" 43 44#include "base/trace.hh" // for DPRINTF 45 46 47using namespace std; 48 49ObjectFile * 50ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 51{ 52 Elf *elf; 53 GElf_Ehdr ehdr; 54 55 // check that header matches library version 56 assert(elf_version(EV_CURRENT) != EV_NONE); 57 58 // get a pointer to elf structure 59 elf = elf_memory((char*)data,len); 60 // will only fail if fd is invalid 61 assert(elf != NULL); 62 63 // Check that we actually have a elf file 64 if (gelf_getehdr(elf, &ehdr) ==0) { 65 DPRINTFR(Loader, "Not ELF\n"); 66 elf_end(elf); 67 return NULL; 68 } 69 else { 70 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) 71 panic("32 bit ELF Binary, Not Supported"); 72 if (ehdr.e_machine != EM_ALPHA) 73 panic("Non Alpha Binary, Not Supported"); 74 75 elf_end(elf); 76 77 return new ElfObject(fname, fd, len, data, 78 ObjectFile::Alpha, ObjectFile::Linux); 79 } 80} 81 82 83ElfObject::ElfObject(const string &_filename, int _fd, 84 size_t _len, uint8_t *_data, 85 Arch _arch, OpSys _opSys) 86 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 87 88{ 89 Elf *elf; 90 GElf_Ehdr ehdr; 91 92 // check that header matches library version 93 assert(elf_version(EV_CURRENT) != EV_NONE); 94 95 // get a pointer to elf structure 96 elf = elf_memory((char*)fileData,len); 97 // will only fail if fd is invalid 98 assert(elf != NULL); 99 100 // Check that we actually have a elf file 101 if (gelf_getehdr(elf, &ehdr) ==0) { 102 panic("Not ELF, shouldn't be here"); 103 } 104 105 entry = ehdr.e_entry; 106 107 // initialize segment sizes to 0 in case they're not present 108 text.size = data.size = bss.size = 0; 109 110 for (int i = 0; i < ehdr.e_phnum; ++i) { 111 GElf_Phdr phdr; 112 if (gelf_getphdr(elf, i, &phdr) == 0) { 113 panic("gelf_getphdr failed for section %d", i); 114 } 115 116 // for now we don't care about non-loadable segments 117 if (!(phdr.p_type & PT_LOAD)) 118 continue; 119 120 // the headers don't explicitly distinguish text from data, 121 // but empirically the text segment comes first. 122 if (text.size == 0) { // haven't seen text segment yet 123 text.baseAddr = phdr.p_vaddr; 124 text.size = phdr.p_filesz; 125 // remember where the data is for loadSections() 126 fileTextBits = fileData + phdr.p_offset; 127 // if there's any padding at the end that's not in the 128 // file, call it the bss. This happens in the "text" 129 // segment if there's only one loadable segment (as for 130 // kernel images). 131 bss.size = phdr.p_memsz - phdr.p_filesz; 132 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 133 } 134 else if (data.size == 0) { // have text, this must be data 135 data.baseAddr = phdr.p_vaddr; 136 data.size = phdr.p_filesz; 137 // remember where the data is for loadSections() 138 fileDataBits = fileData + phdr.p_offset; 139 // if there's any padding at the end that's not in the 140 // file, call it the bss. Warn if this happens for both 141 // the text & data segments (should only have one bss). 142 if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) { 143 warn("Two implied bss segments in file!\n"); 144 } 145 bss.size = phdr.p_memsz - phdr.p_filesz; 146 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 147 } 148 } 149 150 // should have found at least one loadable segment 151 assert(text.size != 0); 152 153 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 154 text.baseAddr, text.size, data.baseAddr, data.size, 155 bss.baseAddr, bss.size); 156 157 elf_end(elf); 158 159 // We will actually read the sections when we need to load them 160} 161 162 163bool 164ElfObject::loadSections(FunctionalMemory *mem, bool loadPhys) 165{ 166 Addr textAddr = text.baseAddr; 167 Addr dataAddr = data.baseAddr; 168 169 if (loadPhys) { 170 textAddr &= (ULL(1) << 40) - 1; 171 dataAddr &= (ULL(1) << 40) - 1; 172 } 173 174 // Since we don't really have an MMU and all memory is 175 // zero-filled, there's no need to set up the BSS segment. 176 if (text.size != 0) 177 mem->prot_write(textAddr, fileTextBits, text.size); 178 if (data.size != 0) 179 mem->prot_write(dataAddr, fileDataBits, data.size); 180 181 return true; 182} 183 184 185bool 186ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding) 187{ 188 Elf *elf; 189 int secidx = 1; // there is a 0 but it is nothing, go figure 190 Elf_Scn *section; 191 GElf_Shdr shdr; 192 Elf_Data *data; 193 int count, ii; 194 bool found = false; 195 GElf_Sym sym; 196 197 if (!symtab) 198 return false; 199 200 // check that header matches library version 201 assert(elf_version(EV_CURRENT) != EV_NONE); 202 203 // get a pointer to elf structure 204 elf = elf_memory((char*)fileData,len); 205 206 assert(elf != NULL); 207 208 // Get the first section 209 section = elf_getscn(elf, secidx); 210 211 // While there are no more sections 212 while (section != NULL) { 213 gelf_getshdr(section, &shdr); 214 215 if (shdr.sh_type == SHT_SYMTAB) { 216 found = true; 217 data = elf_getdata(section, NULL); 218 count = shdr.sh_size / shdr.sh_entsize; 219 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count); 220 221 // loop through all the symbols, only loading global ones 222 for (ii = 0; ii < count; ++ii) { 223 gelf_getsym(data, ii, &sym); 224 if (GELF_ST_BIND(sym.st_info) & binding) { 225 symtab->insert(sym.st_value, 226 elf_strptr(elf, shdr.sh_link, sym.st_name)); 227 } 228 } 229 } 230 ++secidx; 231 section = elf_getscn(elf, secidx); 232 } 233 234 elf_end(elf); 235 236 return found; 237} 238 239bool 240ElfObject::loadGlobalSymbols(SymbolTable *symtab) 241{ 242 return loadSomeSymbols(symtab, STB_GLOBAL); 243} 244 245bool 246ElfObject::loadLocalSymbols(SymbolTable *symtab) 247{ 248 return loadSomeSymbols(symtab, STB_LOCAL); 249} 250