elf_object.cc revision 6691
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 <cassert> 33#include <string> 34 35#include "gelf.h" 36 37#include "base/loader/elf_object.hh" 38#include "base/loader/symtab.hh" 39#include "base/misc.hh" 40#include "base/trace.hh" // for DPRINTF 41#include "sim/byteswap.hh" 42 43using namespace std; 44 45ObjectFile * 46ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 47{ 48 Elf *elf; 49 GElf_Ehdr ehdr; 50 Arch arch = UnknownArch; 51 OpSys opSys = UnknownOpSys; 52 53 // check that header matches library version 54 if (elf_version(EV_CURRENT) == EV_NONE) 55 panic("wrong elf version number!"); 56 57 // get a pointer to elf structure 58 elf = elf_memory((char*)data,len); 59 // will only fail if fd is invalid 60 assert(elf != NULL); 61 62 // Check that we actually have a elf file 63 if (gelf_getehdr(elf, &ehdr) ==0) { 64 DPRINTFR(Loader, "Not ELF\n"); 65 elf_end(elf); 66 return NULL; 67 } else { 68 //Detect the architecture 69 //Since we don't know how to check for alpha right now, we'll 70 //just assume if it wasn't something else and it's 64 bit, that's 71 //what it must be. 72 if (ehdr.e_machine == EM_SPARC64 || 73 (ehdr.e_machine == EM_SPARC && 74 ehdr.e_ident[EI_CLASS] == ELFCLASS64)|| 75 ehdr.e_machine == EM_SPARCV9) { 76 arch = ObjectFile::SPARC64; 77 } else if (ehdr.e_machine == EM_SPARC32PLUS || 78 (ehdr.e_machine == EM_SPARC && 79 ehdr.e_ident[EI_CLASS] == ELFCLASS32)) { 80 arch = ObjectFile::SPARC32; 81 } else if (ehdr.e_machine == EM_MIPS 82 && ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 83 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) { 84 arch = ObjectFile::Mips; 85 } else { 86 fatal("The binary you're trying to load is compiled for big " 87 "endian MIPS. M5\nonly supports little endian MIPS. " 88 "Please recompile your binary.\n"); 89 } 90 } else if (ehdr.e_machine == EM_X86_64 && 91 ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 92 arch = ObjectFile::X86_64; 93 } else if (ehdr.e_machine == EM_386 && 94 ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 95 arch = ObjectFile::I386; 96 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 97 arch = ObjectFile::Alpha; 98 } else if (ehdr.e_machine == EM_ARM) { 99 arch = ObjectFile::Arm; 100 } else if (ehdr.e_machine == EM_PPC && 101 ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 102 if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) { 103 arch = ObjectFile::Power; 104 } else { 105 fatal("The binary you're trying to load is compiled for " 106 "little endian Power.\nM5 only supports big " 107 "endian Power. Please recompile your binary.\n"); 108 } 109 } else if (ehdr.e_machine == EM_PPC64) { 110 fatal("The binary you're trying to load is compiled for 64-bit " 111 "Power. M5\n only supports 32-bit Power. Please " 112 "recompile your binary.\n"); 113 } else { 114 warn("Unknown architecture: %d\n", ehdr.e_machine); 115 arch = ObjectFile::UnknownArch; 116 } 117 118 //Detect the operating system 119 switch (ehdr.e_ident[EI_OSABI]) 120 { 121 122 case ELFOSABI_LINUX: 123 opSys = ObjectFile::Linux; 124 break; 125 case ELFOSABI_SOLARIS: 126 opSys = ObjectFile::Solaris; 127 break; 128 case ELFOSABI_TRU64: 129 opSys = ObjectFile::Tru64; 130 break; 131 case ELFOSABI_ARM: 132 opSys = ObjectFile::LinuxArmOABI; 133 break; 134 default: 135 opSys = ObjectFile::UnknownOpSys; 136 } 137 138 //take a look at the .note.ABI section 139 //It can let us know what's what. 140 if (opSys == ObjectFile::UnknownOpSys) { 141 Elf_Scn *section; 142 GElf_Shdr shdr; 143 Elf_Data *data; 144 uint32_t osAbi;; 145 int secIdx = 1; 146 147 // Get the first section 148 section = elf_getscn(elf, secIdx); 149 150 // While there are no more sections 151 while (section != NULL && opSys == ObjectFile::UnknownOpSys) { 152 gelf_getshdr(section, &shdr); 153 if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag", 154 elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) { 155 // we have found a ABI note section 156 // Check the 5th 32bit word for OS 0 == linux, 1 == hurd, 157 // 2 == solaris, 3 == freebsd 158 data = elf_rawdata(section, NULL); 159 assert(data->d_buf); 160 if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB) 161 osAbi = htole(((uint32_t*)data->d_buf)[4]); 162 else 163 osAbi = htobe(((uint32_t*)data->d_buf)[4]); 164 165 switch(osAbi) { 166 case 0: 167 opSys = ObjectFile::Linux; 168 break; 169 case 2: 170 opSys = ObjectFile::Solaris; 171 break; 172 } 173 } // if section found 174 if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 175 opSys = ObjectFile::Solaris; 176 if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) 177 opSys = ObjectFile::Solaris; 178 179 section = elf_getscn(elf, ++secIdx); 180 } // while sections 181 } 182 183 ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys); 184 185 //The number of headers in the file 186 result->_programHeaderCount = ehdr.e_phnum; 187 //Record the size of each entry 188 result->_programHeaderSize = ehdr.e_phentsize; 189 if(result->_programHeaderCount) //If there is a program header table 190 { 191 //Figure out the virtual address of the header table in the 192 //final memory image. We use the program headers themselves 193 //to translate from a file offset to the address in the image. 194 GElf_Phdr phdr; 195 uint64_t e_phoff = ehdr.e_phoff; 196 result->_programHeaderTable = 0; 197 for(int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++) 198 { 199 gelf_getphdr(elf, hdrnum, &phdr); 200 //Check if we've found the segment with the headers in it 201 if(phdr.p_offset <= e_phoff && 202 phdr.p_offset + phdr.p_filesz > e_phoff) 203 { 204 result->_programHeaderTable = phdr.p_paddr + e_phoff; 205 break; 206 } 207 } 208 } 209 else 210 result->_programHeaderTable = 0; 211 212 213 elf_end(elf); 214 return result; 215 } 216} 217 218 219ElfObject::ElfObject(const string &_filename, int _fd, 220 size_t _len, uint8_t *_data, 221 Arch _arch, OpSys _opSys) 222 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 223 224{ 225 Elf *elf; 226 GElf_Ehdr ehdr; 227 228 // check that header matches library version 229 if (elf_version(EV_CURRENT) == EV_NONE) 230 panic("wrong elf version number!"); 231 232 // get a pointer to elf structure 233 elf = elf_memory((char*)fileData,len); 234 // will only fail if fd is invalid 235 assert(elf != NULL); 236 237 // Check that we actually have a elf file 238 if (gelf_getehdr(elf, &ehdr) ==0) { 239 panic("Not ELF, shouldn't be here"); 240 } 241 242 entry = ehdr.e_entry; 243 244 // initialize segment sizes to 0 in case they're not present 245 text.size = data.size = bss.size = 0; 246 247 int secIdx = 1; 248 Elf_Scn *section; 249 GElf_Shdr shdr; 250 251 // The first address of some important sections. 252 Addr textSecStart = 0; 253 Addr dataSecStart = 0; 254 Addr bssSecStart = 0; 255 256 // Get the first section 257 section = elf_getscn(elf, secIdx); 258 259 // Find the beginning of the most interesting sections. 260 while (section != NULL) { 261 gelf_getshdr(section, &shdr); 262 char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name); 263 264 if (!strcmp(".text", secName)) { 265 textSecStart = shdr.sh_addr; 266 } else if (!strcmp(".data", secName)) { 267 dataSecStart = shdr.sh_addr; 268 } else if (!strcmp(".bss", secName)) { 269 bssSecStart = shdr.sh_addr; 270 } 271 272 section = elf_getscn(elf, ++secIdx); 273 } 274 275 // Go through all the segments in the program, record them, and scrape 276 // out information about the text, data, and bss areas needed by other 277 // code. 278 for (int i = 0; i < ehdr.e_phnum; ++i) { 279 GElf_Phdr phdr; 280 if (gelf_getphdr(elf, i, &phdr) == 0) { 281 panic("gelf_getphdr failed for segment %d.", i); 282 } 283 284 // for now we don't care about non-loadable segments 285 if (!(phdr.p_type & PT_LOAD)) 286 continue; 287 288 // Check to see if this segment contains the bss section. 289 if (phdr.p_paddr <= bssSecStart && 290 phdr.p_paddr + phdr.p_memsz > bssSecStart && 291 phdr.p_memsz - phdr.p_filesz > 0) { 292 bss.baseAddr = phdr.p_paddr + phdr.p_filesz; 293 bss.size = phdr.p_memsz - phdr.p_filesz; 294 bss.fileImage = NULL; 295 } 296 297 // Check to see if this is the text or data segment 298 if (phdr.p_vaddr <= textSecStart && 299 phdr.p_vaddr + phdr.p_filesz > textSecStart) { 300 text.baseAddr = phdr.p_paddr; 301 text.size = phdr.p_filesz; 302 text.fileImage = fileData + phdr.p_offset; 303 } else if (phdr.p_vaddr <= dataSecStart && 304 phdr.p_vaddr + phdr.p_filesz > dataSecStart) { 305 data.baseAddr = phdr.p_paddr; 306 data.size = phdr.p_filesz; 307 data.fileImage = fileData + phdr.p_offset; 308 } else { 309 // If it's none of the above but is loadable, 310 // load the filesize worth of data 311 Segment extra; 312 extra.baseAddr = phdr.p_paddr; 313 extra.size = phdr.p_filesz; 314 extra.fileImage = fileData + phdr.p_offset; 315 extraSegments.push_back(extra); 316 } 317 } 318 319 // should have found at least one loadable segment 320 assert(text.size != 0); 321 322 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 323 text.baseAddr, text.size, data.baseAddr, data.size, 324 bss.baseAddr, bss.size); 325 326 elf_end(elf); 327 328 // We will actually read the sections when we need to load them 329} 330 331 332bool 333ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding) 334{ 335 Elf *elf; 336 int sec_idx = 1; // there is a 0 but it is nothing, go figure 337 Elf_Scn *section; 338 GElf_Shdr shdr; 339 Elf_Data *data; 340 int count, ii; 341 bool found = false; 342 GElf_Sym sym; 343 344 if (!symtab) 345 return false; 346 347 // check that header matches library version 348 if (elf_version(EV_CURRENT) == EV_NONE) 349 panic("wrong elf version number!"); 350 351 // get a pointer to elf structure 352 elf = elf_memory((char*)fileData,len); 353 354 assert(elf != NULL); 355 356 // Get the first section 357 section = elf_getscn(elf, sec_idx); 358 359 // While there are no more sections 360 while (section != NULL) { 361 gelf_getshdr(section, &shdr); 362 363 if (shdr.sh_type == SHT_SYMTAB) { 364 found = true; 365 data = elf_getdata(section, NULL); 366 count = shdr.sh_size / shdr.sh_entsize; 367 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count); 368 369 // loop through all the symbols, only loading global ones 370 for (ii = 0; ii < count; ++ii) { 371 gelf_getsym(data, ii, &sym); 372 if (GELF_ST_BIND(sym.st_info) == binding) { 373 symtab->insert(sym.st_value, 374 elf_strptr(elf, shdr.sh_link, sym.st_name)); 375 } 376 } 377 } 378 ++sec_idx; 379 section = elf_getscn(elf, sec_idx); 380 } 381 382 elf_end(elf); 383 384 return found; 385} 386 387bool 388ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask) 389{ 390 return loadSomeSymbols(symtab, STB_GLOBAL); 391} 392 393bool 394ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask) 395{ 396 return loadSomeSymbols(symtab, STB_LOCAL); 397} 398 399bool 400ElfObject::loadSections(Port *memPort, Addr addrMask) 401{ 402 if (!ObjectFile::loadSections(memPort, addrMask)) 403 return false; 404 405 vector<Segment>::iterator extraIt; 406 for (extraIt = extraSegments.begin(); 407 extraIt != extraSegments.end(); extraIt++) { 408 if (!loadSection(&(*extraIt), memPort, addrMask)) { 409 return false; 410 } 411 } 412 return true; 413} 414 415void 416ElfObject::getSections() 417{ 418 Elf *elf; 419 int sec_idx = 1; // there is a 0 but it is nothing, go figure 420 Elf_Scn *section; 421 GElf_Shdr shdr; 422 423 GElf_Ehdr ehdr; 424 425 assert(!sectionNames.size()); 426 427 // check that header matches library version 428 if (elf_version(EV_CURRENT) == EV_NONE) 429 panic("wrong elf version number!"); 430 431 // get a pointer to elf structure 432 elf = elf_memory((char*)fileData,len); 433 assert(elf != NULL); 434 435 // Check that we actually have a elf file 436 if (gelf_getehdr(elf, &ehdr) ==0) { 437 panic("Not ELF, shouldn't be here"); 438 } 439 440 // Get the first section 441 section = elf_getscn(elf, sec_idx); 442 443 // While there are no more sections 444 while (section != NULL) { 445 gelf_getshdr(section, &shdr); 446 sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)); 447 section = elf_getscn(elf, ++sec_idx); 448 } // while sections 449} 450 451bool 452ElfObject::sectionExists(string sec) 453{ 454 if (!sectionNames.size()) 455 getSections(); 456 return sectionNames.find(sec) != sectionNames.end(); 457} 458 459 460