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