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