elf_object.cc revision 5070
1/* 2 * Copyright (c) 2003-2005 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 * Authors: Steve Reinhardt 29 * Ali Saidi 30 */ 31 32#include <string> 33 34#include "gelf.h" 35 36#include "base/loader/elf_object.hh" 37#include "base/loader/symtab.hh" 38#include "base/misc.hh" 39#include "base/trace.hh" // for DPRINTF 40#include "sim/byteswap.hh" 41 42using namespace std; 43 44ObjectFile * 45ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 46{ 47 Elf *elf; 48 GElf_Ehdr ehdr; 49 Arch arch = UnknownArch; 50 OpSys opSys = UnknownOpSys; 51 52 // check that header matches library version 53 if (elf_version(EV_CURRENT) == EV_NONE) 54 panic("wrong elf version number!"); 55 56 // get a pointer to elf structure 57 elf = elf_memory((char*)data,len); 58 // will only fail if fd is invalid 59 assert(elf != NULL); 60 61 // Check that we actually have a elf file 62 if (gelf_getehdr(elf, &ehdr) ==0) { 63 DPRINTFR(Loader, "Not ELF\n"); 64 elf_end(elf); 65 return NULL; 66 } else { 67 //Detect the architecture 68 //Since we don't know how to check for alpha right now, we'll 69 //just assume if it wasn't something else and it's 64 bit, that's 70 //what it must be. 71 if (ehdr.e_machine == EM_SPARC64 || 72 (ehdr.e_machine == EM_SPARC && 73 ehdr.e_ident[EI_CLASS] == ELFCLASS64)|| 74 ehdr.e_machine == EM_SPARCV9) { 75 arch = ObjectFile::SPARC64; 76 } else if (ehdr.e_machine == EM_SPARC32PLUS || 77 (ehdr.e_machine == EM_SPARC && 78 ehdr.e_ident[EI_CLASS] == ELFCLASS32)) { 79 arch = ObjectFile::SPARC32; 80 } else if (ehdr.e_machine == EM_MIPS 81 && ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 82 arch = ObjectFile::Mips; 83 } else if (ehdr.e_machine == EM_X86_64 && 84 ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 85 //In the future, we might want to differentiate between 32 bit 86 //and 64 bit x86 processes in case there are differences in their 87 //initial stack frame. 88 arch = ObjectFile::X86; 89 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 90 arch = ObjectFile::Alpha; 91 } else { 92 warn("Unknown architecture: %d\n", ehdr.e_machine); 93 arch = ObjectFile::UnknownArch; 94 } 95 96 //Detect the operating system 97 switch (ehdr.e_ident[EI_OSABI]) 98 { 99 100 case ELFOSABI_LINUX: 101 opSys = ObjectFile::Linux; 102 break; 103 case ELFOSABI_SOLARIS: 104 opSys = ObjectFile::Solaris; 105 break; 106 case ELFOSABI_TRU64: 107 opSys = ObjectFile::Tru64; 108 break; 109 default: 110 opSys = ObjectFile::UnknownOpSys; 111 } 112 113 //take a look at the .note.ABI section 114 //It can let us know what's what. 115 if (opSys == ObjectFile::UnknownOpSys) { 116 Elf_Scn *section; 117 GElf_Shdr shdr; 118 Elf_Data *data; 119 uint32_t osAbi;; 120 int secIdx = 1; 121 122 // Get the first section 123 section = elf_getscn(elf, secIdx); 124 125 // While there are no more sections 126 while (section != NULL && opSys == ObjectFile::UnknownOpSys) { 127 gelf_getshdr(section, &shdr); 128 if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag", 129 elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) { 130 // we have found a ABI note section 131 // Check the 5th 32bit word for OS 0 == linux, 1 == hurd, 132 // 2 == solaris, 3 == freebsd 133 data = elf_rawdata(section, NULL); 134 assert(data->d_buf); 135 if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB) 136 osAbi = htole(((uint32_t*)data->d_buf)[4]); 137 else 138 osAbi = htobe(((uint32_t*)data->d_buf)[4]); 139 140 switch(osAbi) { 141 case 0: 142 opSys = ObjectFile::Linux; 143 break; 144 case 2: 145 opSys = ObjectFile::Solaris; 146 break; 147 } 148 } // if section found 149 if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 150 opSys = ObjectFile::Solaris; 151 if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 152 opSys = ObjectFile::Solaris; 153 154 section = elf_getscn(elf, ++secIdx); 155 } // while sections 156 } 157 158 ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys); 159 160 //The number of headers in the file 161 result->_programHeaderCount = ehdr.e_phnum; 162 //Record the size of each entry 163 result->_programHeaderSize = ehdr.e_phentsize; 164 if(result->_programHeaderCount) //If there is a program header table 165 { 166 //Figure out the virtual address of the header table in the 167 //final memory image. We use the program headers themselves 168 //to translate from a file offset to the address in the image. 169 GElf_Phdr phdr; 170 uint64_t e_phoff = ehdr.e_phoff; 171 result->_programHeaderTable = 0; 172 for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++) 173 { 174 gelf_getphdr(elf, hdrnum, &phdr); 175 //Check if we've found the segment with the headers in it 176 if(phdr.p_offset <= e_phoff && 177 phdr.p_offset + phdr.p_filesz > e_phoff) 178 { 179 result->_programHeaderTable = phdr.p_vaddr + e_phoff; 180 break; 181 } 182 } 183 } 184 else 185 result->_programHeaderTable = 0; 186 187 188 elf_end(elf); 189 return result; 190 } 191} 192 193 194ElfObject::ElfObject(const string &_filename, int _fd, 195 size_t _len, uint8_t *_data, 196 Arch _arch, OpSys _opSys) 197 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 198 199{ 200 Elf *elf; 201 GElf_Ehdr ehdr; 202 203 // check that header matches library version 204 if (elf_version(EV_CURRENT) == EV_NONE) 205 panic("wrong elf version number!"); 206 207 // get a pointer to elf structure 208 elf = elf_memory((char*)fileData,len); 209 // will only fail if fd is invalid 210 assert(elf != NULL); 211 212 // Check that we actually have a elf file 213 if (gelf_getehdr(elf, &ehdr) ==0) { 214 panic("Not ELF, shouldn't be here"); 215 } 216 217 entry = ehdr.e_entry; 218 219 220 // initialize segment sizes to 0 in case they're not present 221 text.size = data.size = bss.size = 0; 222 223 for (int i = 0; i < ehdr.e_phnum; ++i) { 224 GElf_Phdr phdr; 225 if (gelf_getphdr(elf, i, &phdr) == 0) { 226 panic("gelf_getphdr failed for section %d", i); 227 } 228 229 // for now we don't care about non-loadable segments 230 if (!(phdr.p_type & PT_LOAD)) 231 continue; 232 233 // the headers don't explicitly distinguish text from data, 234 // but empirically the text segment comes first. 235 if (text.size == 0) { // haven't seen text segment yet 236 text.baseAddr = phdr.p_vaddr; 237 text.size = phdr.p_filesz; 238 text.fileImage = fileData + phdr.p_offset; 239 // if there's any padding at the end that's not in the 240 // file, call it the bss. This happens in the "text" 241 // segment if there's only one loadable segment (as for 242 // kernel images). 243 bss.size = phdr.p_memsz - phdr.p_filesz; 244 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 245 bss.fileImage = NULL; 246 } else if (data.size == 0) { // have text, this must be data 247 data.baseAddr = phdr.p_vaddr; 248 data.size = phdr.p_filesz; 249 data.fileImage = fileData + phdr.p_offset; 250 // if there's any padding at the end that's not in the 251 // file, call it the bss. Warn if this happens for both 252 // the text & data segments (should only have one bss). 253 if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) { 254 warn("Two implied bss segments in file!\n"); 255 } 256 bss.size = phdr.p_memsz - phdr.p_filesz; 257 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 258 bss.fileImage = NULL; 259 } else { 260 warn("More than two loadable segments in ELF object."); 261 warn("Ignoring segment @ 0x%x length 0x%x.", 262 phdr.p_vaddr, phdr.p_filesz); 263 } 264 } 265 266 // should have found at least one loadable segment 267 assert(text.size != 0); 268 269 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 270 text.baseAddr, text.size, data.baseAddr, data.size, 271 bss.baseAddr, bss.size); 272 273 elf_end(elf); 274 275 // We will actually read the sections when we need to load them 276} 277 278 279bool 280ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding) 281{ 282 Elf *elf; 283 int sec_idx = 1; // there is a 0 but it is nothing, go figure 284 Elf_Scn *section; 285 GElf_Shdr shdr; 286 Elf_Data *data; 287 int count, ii; 288 bool found = false; 289 GElf_Sym sym; 290 291 if (!symtab) 292 return false; 293 294 // check that header matches library version 295 if (elf_version(EV_CURRENT) == EV_NONE) 296 panic("wrong elf version number!"); 297 298 // get a pointer to elf structure 299 elf = elf_memory((char*)fileData,len); 300 301 assert(elf != NULL); 302 303 // Get the first section 304 section = elf_getscn(elf, sec_idx); 305 306 // While there are no more sections 307 while (section != NULL) { 308 gelf_getshdr(section, &shdr); 309 310 if (shdr.sh_type == SHT_SYMTAB) { 311 found = true; 312 data = elf_getdata(section, NULL); 313 count = shdr.sh_size / shdr.sh_entsize; 314 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count); 315 316 // loop through all the symbols, only loading global ones 317 for (ii = 0; ii < count; ++ii) { 318 gelf_getsym(data, ii, &sym); 319 if (GELF_ST_BIND(sym.st_info) == binding) { 320 symtab->insert(sym.st_value, 321 elf_strptr(elf, shdr.sh_link, sym.st_name)); 322 } 323 } 324 } 325 ++sec_idx; 326 section = elf_getscn(elf, sec_idx); 327 } 328 329 elf_end(elf); 330 331 return found; 332} 333 334bool 335ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask) 336{ 337 return loadSomeSymbols(symtab, STB_GLOBAL); 338} 339 340bool 341ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask) 342{ 343 return loadSomeSymbols(symtab, STB_LOCAL); 344} 345 346void 347ElfObject::getSections() 348{ 349 Elf *elf; 350 int sec_idx = 1; // there is a 0 but it is nothing, go figure 351 Elf_Scn *section; 352 GElf_Shdr shdr; 353 354 GElf_Ehdr ehdr; 355 356 assert(!sectionNames.size()); 357 358 // check that header matches library version 359 if (elf_version(EV_CURRENT) == EV_NONE) 360 panic("wrong elf version number!"); 361 362 // get a pointer to elf structure 363 elf = elf_memory((char*)fileData,len); 364 assert(elf != NULL); 365 366 // Check that we actually have a elf file 367 if (gelf_getehdr(elf, &ehdr) ==0) { 368 panic("Not ELF, shouldn't be here"); 369 } 370 371 // Get the first section 372 section = elf_getscn(elf, sec_idx); 373 374 // While there are no more sections 375 while (section != NULL) { 376 gelf_getshdr(section, &shdr); 377 sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)); 378 section = elf_getscn(elf, ++sec_idx); 379 } // while sections 380} 381 382bool 383ElfObject::sectionExists(string sec) 384{ 385 if (!sectionNames.size()) 386 getSections(); 387 return sectionNames.find(sec) != sectionNames.end(); 388} 389 390 391