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