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