elf_object.cc revision 10810:683ab55819fd
1/* 2 * Copyright (c) 2011-2013 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Copyright (c) 2003-2005 The Regents of The University of Michigan 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions are 19 * met: redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer; 21 * redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution; 24 * neither the name of the copyright holders nor the names of its 25 * contributors may be used to endorse or promote products derived from 26 * this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 * 40 * Authors: Steve Reinhardt 41 * Ali Saidi 42 */ 43 44#include <cassert> 45#include <string> 46 47#include "base/loader/elf_object.hh" 48#include "base/loader/symtab.hh" 49#include "base/bitfield.hh" 50#include "base/misc.hh" 51#include "base/trace.hh" 52#include "debug/Loader.hh" 53#include "sim/byteswap.hh" 54#include "gelf.h" 55 56using namespace std; 57 58ObjectFile * 59ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 60{ 61 Elf *elf; 62 GElf_Ehdr ehdr; 63 Arch arch = UnknownArch; 64 OpSys opSys = UnknownOpSys; 65 66 // check that header matches library version 67 if (elf_version(EV_CURRENT) == EV_NONE) 68 panic("wrong elf version number!"); 69 70 // get a pointer to elf structure 71 elf = elf_memory((char*)data,len); 72 // will only fail if fd is invalid 73 assert(elf != NULL); 74 75 // Check that we actually have a elf file 76 if (gelf_getehdr(elf, &ehdr) == 0) { 77 DPRINTFR(Loader, "Not ELF\n"); 78 elf_end(elf); 79 return NULL; 80 } else { 81 //Detect the architecture 82 //Since we don't know how to check for alpha right now, we'll 83 //just assume if it wasn't something else and it's 64 bit, that's 84 //what it must be. 85 if (ehdr.e_machine == EM_SPARC64 || 86 (ehdr.e_machine == EM_SPARC && 87 ehdr.e_ident[EI_CLASS] == ELFCLASS64)|| 88 ehdr.e_machine == EM_SPARCV9) { 89 arch = ObjectFile::SPARC64; 90 } else if (ehdr.e_machine == EM_SPARC32PLUS || 91 (ehdr.e_machine == EM_SPARC && 92 ehdr.e_ident[EI_CLASS] == ELFCLASS32)) { 93 arch = ObjectFile::SPARC32; 94 } else if (ehdr.e_machine == EM_MIPS 95 && ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 96 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) { 97 arch = ObjectFile::Mips; 98 } else { 99 fatal("The binary you're trying to load is compiled for big " 100 "endian MIPS. M5\nonly supports little endian MIPS. " 101 "Please recompile your binary.\n"); 102 } 103 } else if (ehdr.e_machine == EM_X86_64 && 104 ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 105 arch = ObjectFile::X86_64; 106 } else if (ehdr.e_machine == EM_386 && 107 ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 108 arch = ObjectFile::I386; 109 } else if (ehdr.e_machine == EM_ARM && 110 ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 111 if (bits(ehdr.e_entry, 0)) { 112 arch = ObjectFile::Thumb; 113 } else { 114 arch = ObjectFile::Arm; 115 } 116 } else if ((ehdr.e_machine == EM_AARCH64) && 117 ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 118 arch = ObjectFile::Arm64; 119 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 120 arch = ObjectFile::Alpha; 121 } else if (ehdr.e_machine == EM_PPC && 122 ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 123 if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) { 124 arch = ObjectFile::Power; 125 } else { 126 fatal("The binary you're trying to load is compiled for " 127 "little endian Power.\nM5 only supports big " 128 "endian Power. Please recompile your binary.\n"); 129 } 130 } else if (ehdr.e_machine == EM_PPC64) { 131 fatal("The binary you're trying to load is compiled for 64-bit " 132 "Power. M5\n only supports 32-bit Power. Please " 133 "recompile your binary.\n"); 134 } else { 135 warn("Unknown architecture: %d\n", ehdr.e_machine); 136 arch = ObjectFile::UnknownArch; 137 } 138 139 //Detect the operating system 140 switch (ehdr.e_ident[EI_OSABI]) { 141 case ELFOSABI_LINUX: 142 opSys = ObjectFile::Linux; 143 break; 144 case ELFOSABI_SOLARIS: 145 opSys = ObjectFile::Solaris; 146 break; 147 case ELFOSABI_TRU64: 148 opSys = ObjectFile::Tru64; 149 break; 150 case ELFOSABI_ARM: 151 opSys = ObjectFile::LinuxArmOABI; 152 break; 153 case ELFOSABI_FREEBSD: 154 opSys = ObjectFile::FreeBSD; 155 break; 156 default: 157 opSys = ObjectFile::UnknownOpSys; 158 } 159 160 //take a look at the .note.ABI section 161 //It can let us know what's what. 162 if (opSys == ObjectFile::UnknownOpSys) { 163 Elf_Scn *section; 164 GElf_Shdr shdr; 165 Elf_Data *data; 166 uint32_t osAbi; 167 uint32_t *elem; 168 int secIdx = 1; 169 170 // Get the first section 171 section = elf_getscn(elf, secIdx); 172 173 // While there are no more sections 174 while (section != NULL && opSys == ObjectFile::UnknownOpSys) { 175 gelf_getshdr(section, &shdr); 176 if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag", 177 elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) { 178 // we have found a ABI note section 179 // Check the 5th 32bit word for OS 0 == linux, 1 == hurd, 180 // 2 == solaris, 3 == freebsd 181 data = elf_rawdata(section, NULL); 182 assert(data->d_buf); 183 if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB) 184 osAbi = htole(((uint32_t*)data->d_buf)[4]); 185 else 186 osAbi = htobe(((uint32_t*)data->d_buf)[4]); 187 188 switch(osAbi) { 189 case 0: 190 opSys = ObjectFile::Linux; 191 break; 192 case 2: 193 opSys = ObjectFile::Solaris; 194 break; 195 } 196 } // if section found 197 if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 198 opSys = ObjectFile::Solaris; 199 if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 200 opSys = ObjectFile::Solaris; 201 if (shdr.sh_type == SHT_NOTE && !strcmp(".note.tag", 202 elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) { 203 data = elf_rawdata(section, NULL); 204 assert(data->d_buf); 205 elem = (uint32_t *)data->d_buf; 206 if (elem[0] == 0x8) { //size of name 207 if (memcmp((void *)&elem[3], "FreeBSD", 0x8) == 0) 208 opSys = ObjectFile::FreeBSD; 209 } 210 } 211 212 section = elf_getscn(elf, ++secIdx); 213 } // while sections 214 } 215 216 ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys); 217 218 //The number of headers in the file 219 result->_programHeaderCount = ehdr.e_phnum; 220 //Record the size of each entry 221 result->_programHeaderSize = ehdr.e_phentsize; 222 if(result->_programHeaderCount) //If there is a program header table 223 { 224 //Figure out the virtual address of the header table in the 225 //final memory image. We use the program headers themselves 226 //to translate from a file offset to the address in the image. 227 GElf_Phdr phdr; 228 uint64_t e_phoff = ehdr.e_phoff; 229 result->_programHeaderTable = 0; 230 for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++) 231 { 232 gelf_getphdr(elf, hdrnum, &phdr); 233 //Check if we've found the segment with the headers in it 234 if(phdr.p_offset <= e_phoff && 235 phdr.p_offset + phdr.p_filesz > e_phoff) 236 { 237 result->_programHeaderTable = 238 phdr.p_paddr + (e_phoff - phdr.p_offset); 239 break; 240 } 241 } 242 } 243 else 244 result->_programHeaderTable = 0; 245 246 247 elf_end(elf); 248 return result; 249 } 250} 251 252 253ElfObject::ElfObject(const string &_filename, int _fd, 254 size_t _len, uint8_t *_data, 255 Arch _arch, OpSys _opSys) 256 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys), 257 _programHeaderTable(0), _programHeaderSize(0), _programHeaderCount(0) 258 259{ 260 Elf *elf; 261 GElf_Ehdr ehdr; 262 263 // check that header matches library version 264 if (elf_version(EV_CURRENT) == EV_NONE) 265 panic("wrong elf version number!"); 266 267 // get a pointer to elf structure 268 elf = elf_memory((char*)fileData,len); 269 // will only fail if fd is invalid 270 assert(elf != NULL); 271 272 // Check that we actually have a elf file 273 if (gelf_getehdr(elf, &ehdr) ==0) { 274 panic("Not ELF, shouldn't be here"); 275 } 276 277 entry = ehdr.e_entry; 278 279 // initialize segment sizes to 0 in case they're not present 280 text.size = data.size = bss.size = 0; 281 text.baseAddr = data.baseAddr = bss.baseAddr = 0; 282 283 int secIdx = 1; 284 Elf_Scn *section; 285 GElf_Shdr shdr; 286 287 // The first address of some important sections. 288 Addr textSecStart = 0; 289 Addr dataSecStart = 0; 290 Addr bssSecStart = 0; 291 292 // Get the first section 293 section = elf_getscn(elf, secIdx); 294 295 // Find the beginning of the most interesting sections. 296 while (section != NULL) { 297 gelf_getshdr(section, &shdr); 298 char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name); 299 300 if (secName) { 301 if (!strcmp(".text", secName)) { 302 textSecStart = shdr.sh_addr; 303 } else if (!strcmp(".data", secName)) { 304 dataSecStart = shdr.sh_addr; 305 } else if (!strcmp(".bss", secName)) { 306 bssSecStart = shdr.sh_addr; 307 } 308 } else { 309 Elf_Error errorNum = (Elf_Error)elf_errno(); 310 if (errorNum != ELF_E_NONE) { 311 const char *errorMessage = elf_errmsg(errorNum); 312 fatal("Error from libelf: %s.\n", errorMessage); 313 } 314 } 315 316 section = elf_getscn(elf, ++secIdx); 317 } 318 319 // Go through all the segments in the program, record them, and scrape 320 // out information about the text, data, and bss areas needed by other 321 // code. 322 for (int i = 0; i < ehdr.e_phnum; ++i) { 323 GElf_Phdr phdr; 324 if (gelf_getphdr(elf, i, &phdr) == 0) { 325 panic("gelf_getphdr failed for segment %d.", i); 326 } 327 328 // for now we don't care about non-loadable segments 329 if (!(phdr.p_type & PT_LOAD)) 330 continue; 331 332 // Check to see if this segment contains the bss section. 333 if (phdr.p_paddr <= bssSecStart && 334 phdr.p_paddr + phdr.p_memsz > bssSecStart && 335 phdr.p_memsz - phdr.p_filesz > 0) { 336 bss.baseAddr = phdr.p_paddr + phdr.p_filesz; 337 bss.size = phdr.p_memsz - phdr.p_filesz; 338 bss.fileImage = NULL; 339 } 340 341 // Check to see if this is the text or data segment 342 if (phdr.p_vaddr <= textSecStart && 343 phdr.p_vaddr + phdr.p_filesz > textSecStart) { 344 text.baseAddr = phdr.p_paddr; 345 text.size = phdr.p_filesz; 346 text.fileImage = fileData + phdr.p_offset; 347 } else if (phdr.p_vaddr <= dataSecStart && 348 phdr.p_vaddr + phdr.p_filesz > dataSecStart) { 349 data.baseAddr = phdr.p_paddr; 350 data.size = phdr.p_filesz; 351 data.fileImage = fileData + phdr.p_offset; 352 } else { 353 // If it's none of the above but is loadable, 354 // load the filesize worth of data 355 Segment extra; 356 extra.baseAddr = phdr.p_paddr; 357 extra.size = phdr.p_filesz; 358 extra.fileImage = fileData + phdr.p_offset; 359 extraSegments.push_back(extra); 360 } 361 } 362 363 // should have found at least one loadable segment 364 assert(text.size != 0); 365 366 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 367 text.baseAddr, text.size, data.baseAddr, data.size, 368 bss.baseAddr, bss.size); 369 370 elf_end(elf); 371 372 // We will actually read the sections when we need to load them 373} 374 375 376bool 377ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask) 378{ 379 Elf *elf; 380 int sec_idx = 1; // there is a 0 but it is nothing, go figure 381 Elf_Scn *section; 382 GElf_Shdr shdr; 383 Elf_Data *data; 384 int count, ii; 385 bool found = false; 386 GElf_Sym sym; 387 388 if (!symtab) 389 return false; 390 391 // check that header matches library version 392 if (elf_version(EV_CURRENT) == EV_NONE) 393 panic("wrong elf version number!"); 394 395 // get a pointer to elf structure 396 elf = elf_memory((char*)fileData,len); 397 398 assert(elf != NULL); 399 400 // Get the first section 401 section = elf_getscn(elf, sec_idx); 402 403 // While there are no more sections 404 while (section != NULL) { 405 gelf_getshdr(section, &shdr); 406 407 if (shdr.sh_type == SHT_SYMTAB) { 408 found = true; 409 data = elf_getdata(section, NULL); 410 count = shdr.sh_size / shdr.sh_entsize; 411 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count); 412 413 // loop through all the symbols, only loading global ones 414 for (ii = 0; ii < count; ++ii) { 415 gelf_getsym(data, ii, &sym); 416 if (GELF_ST_BIND(sym.st_info) == binding) { 417 char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name); 418 if (sym_name && sym_name[0] != '$') { 419 DPRINTF(Loader, "Symbol: %-40s value %#x\n", 420 sym_name, sym.st_value); 421 symtab->insert(sym.st_value & mask, sym_name); 422 } 423 } 424 } 425 } 426 ++sec_idx; 427 section = elf_getscn(elf, sec_idx); 428 } 429 430 elf_end(elf); 431 432 return found; 433} 434 435bool 436ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask) 437{ 438 return loadSomeSymbols(symtab, STB_GLOBAL, addrMask); 439} 440 441bool 442ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask) 443{ 444 bool found_local = loadSomeSymbols(symtab, STB_LOCAL, addrMask); 445 bool found_weak = loadSomeSymbols(symtab, STB_WEAK, addrMask); 446 return found_local || found_weak; 447} 448 449bool 450ElfObject::loadWeakSymbols(SymbolTable *symtab, Addr addrMask) 451{ 452 return loadSomeSymbols(symtab, STB_WEAK, addrMask); 453} 454 455bool 456ElfObject::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset) 457{ 458 if (!ObjectFile::loadSections(memProxy, addrMask, offset)) 459 return false; 460 461 vector<Segment>::iterator extraIt; 462 for (extraIt = extraSegments.begin(); 463 extraIt != extraSegments.end(); extraIt++) { 464 if (!loadSection(&(*extraIt), memProxy, addrMask, offset)) { 465 return false; 466 } 467 } 468 return true; 469} 470 471void 472ElfObject::getSections() 473{ 474 Elf *elf; 475 int sec_idx = 1; // there is a 0 but it is nothing, go figure 476 Elf_Scn *section; 477 GElf_Shdr shdr; 478 479 GElf_Ehdr ehdr; 480 481 assert(!sectionNames.size()); 482 483 // check that header matches library version 484 if (elf_version(EV_CURRENT) == EV_NONE) 485 panic("wrong elf version number!"); 486 487 // get a pointer to elf structure 488 elf = elf_memory((char*)fileData,len); 489 assert(elf != NULL); 490 491 // Check that we actually have a elf file 492 if (gelf_getehdr(elf, &ehdr) ==0) { 493 panic("Not ELF, shouldn't be here"); 494 } 495 496 // Get the first section 497 section = elf_getscn(elf, sec_idx); 498 499 // While there are no more sections 500 while (section != NULL) { 501 gelf_getshdr(section, &shdr); 502 sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)); 503 section = elf_getscn(elf, ++sec_idx); 504 } // while sections 505} 506 507bool 508ElfObject::sectionExists(string sec) 509{ 510 if (!sectionNames.size()) 511 getSections(); 512 return sectionNames.find(sec) != sectionNames.end(); 513} 514 515 516