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