elf_object.cc revision 2423
113325Sgabeblack@google.com/* 213325Sgabeblack@google.com * Copyright (c) 2003-2005 The Regents of The University of Michigan 313325Sgabeblack@google.com * All rights reserved. 413325Sgabeblack@google.com * 513325Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 613325Sgabeblack@google.com * modification, are permitted provided that the following conditions are 713325Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 813325Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 913325Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1013325Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1113325Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1213325Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1313325Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1413325Sgabeblack@google.com * this software without specific prior written permission. 1513325Sgabeblack@google.com * 1613325Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1713325Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1813325Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1913325Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2013325Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2113325Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2213325Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2313325Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2413325Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2513325Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2613325Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2713325Sgabeblack@google.com */ 2813325Sgabeblack@google.com 2913325Sgabeblack@google.com#include <string> 3013325Sgabeblack@google.com 3113325Sgabeblack@google.com// Because of the -Wundef flag we have to do this 3213325Sgabeblack@google.com#define __LIBELF_INTERNAL__ 0 3313325Sgabeblack@google.com// counterintuitive, but the flag below causes libelf to define 3413325Sgabeblack@google.com// 64-bit elf types that apparently didn't exist in some older 3513325Sgabeblack@google.com// versions of Linux. They seem to be there in 2.4.x, so don't 3613325Sgabeblack@google.com// set this now (it causes things to break on 64-bit platforms). 3713325Sgabeblack@google.com#define __LIBELF64_LINUX 0 3813325Sgabeblack@google.com#define __LIBELF_NEED_LINK_H 0 3913325Sgabeblack@google.com#define __LIBELF_SYMBOL_VERSIONS 0 4013325Sgabeblack@google.com 4113325Sgabeblack@google.com#include "libelf/libelf.h" 4213325Sgabeblack@google.com#include "libelf/gelf.h" 4313325Sgabeblack@google.com 4413325Sgabeblack@google.com#include "base/loader/elf_object.hh" 4513325Sgabeblack@google.com 4613325Sgabeblack@google.com#include "base/loader/symtab.hh" 4713325Sgabeblack@google.com 4813325Sgabeblack@google.com#include "base/trace.hh" // for DPRINTF 4913325Sgabeblack@google.com 5013325Sgabeblack@google.com#include "sim/byteswap.hh" 5113325Sgabeblack@google.com 5213325Sgabeblack@google.com 5313325Sgabeblack@google.comusing namespace std; 5413325Sgabeblack@google.com 5513325Sgabeblack@google.comObjectFile * 5613325Sgabeblack@google.comElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 5713325Sgabeblack@google.com{ 5813325Sgabeblack@google.com Elf *elf; 5913325Sgabeblack@google.com GElf_Ehdr ehdr; 6013325Sgabeblack@google.com Arch arch = UnknownArch; 6113325Sgabeblack@google.com OpSys opSys = UnknownOpSys; 6213325Sgabeblack@google.com 6313325Sgabeblack@google.com // check that header matches library version 6413325Sgabeblack@google.com if (elf_version(EV_CURRENT) == EV_NONE) 6513325Sgabeblack@google.com panic("wrong elf version number!"); 6613325Sgabeblack@google.com 6713325Sgabeblack@google.com // get a pointer to elf structure 6813325Sgabeblack@google.com elf = elf_memory((char*)data,len); 6913325Sgabeblack@google.com // will only fail if fd is invalid 70 assert(elf != NULL); 71 72 // Check that we actually have a elf file 73 if (gelf_getehdr(elf, &ehdr) ==0) { 74 DPRINTFR(Loader, "Not ELF\n"); 75 elf_end(elf); 76 return NULL; 77 } 78 else { 79 //Detect the architecture 80 //Versioning issues in libelf need to be resolved to get the correct 81 //SPARC constants. 82 //If MIPS supports 32 bit executables, this may need to be changed. 83 //Also, there are other MIPS constants which may be used, like 84 //EM_MIPS_RS3_LE and EM_MIPS_X 85 //Since we don't know how to check for alpha right now, we'll 86 //just assume if it wasn't something else and it's 64 bit, that's 87 //what it must be. 88 if (ehdr.e_machine == EM_SPARC64 || 89 ehdr.e_machine == EM_SPARC || 90 ehdr.e_machine == EM_SPARCV9) { 91 arch = ObjectFile::SPARC; 92 } else if (ehdr.e_machine == EM_MIPS 93 && ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 94 arch = ObjectFile::MIPS; 95 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 96 arch = ObjectFile::Alpha; 97 } else { 98 arch = ObjectFile::UnknownArch; 99 } 100 101 //Detect the operating system 102 switch (ehdr.e_ident[EI_OSABI]) 103 { 104 105 case ELFOSABI_LINUX: 106 opSys = ObjectFile::Linux; 107 break; 108 case ELFOSABI_SOLARIS: 109 opSys = ObjectFile::Solaris; 110 break; 111 case ELFOSABI_TRU64: 112 opSys = ObjectFile::Tru64; 113 break; 114 default: 115 opSys = ObjectFile::UnknownOpSys; 116 } 117 118 //take a look at the .note.ABI section 119 //It can let us know what's what. 120 if (opSys == ObjectFile::UnknownOpSys) 121 { 122 Elf_Scn *section; 123 GElf_Shdr shdr; 124 Elf_Data *data; 125 uint32_t osAbi;; 126 int secIdx = 1; 127 128 // Get the first section 129 section = elf_getscn(elf, secIdx); 130 131 // While there are no more sections 132 while (section != NULL) { 133 gelf_getshdr(section, &shdr); 134 if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag", 135 elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) { 136 // we have found a ABI note section 137 // Check the 5th 32bit word for OS 0 == linux, 1 == hurd, 138 // 2 == solaris, 3 == freebsd 139 data = elf_rawdata(section, NULL); 140 assert(data->d_buf); 141 if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB) 142 osAbi = htole(((uint32_t*)data->d_buf)[4]); 143 else 144 osAbi = htobe(((uint32_t*)data->d_buf)[4]); 145 146 switch(osAbi) { 147 case 0: 148 opSys = ObjectFile::Linux; 149 break; 150 case 2: 151 opSys = ObjectFile::Solaris; 152 break; 153 } 154 } // if section found 155 section = elf_getscn(elf, ++secIdx); 156 } // while sections 157 } 158 elf_end(elf); 159 return new ElfObject(fname, fd, len, data, arch, opSys); 160 } 161} 162 163 164ElfObject::ElfObject(const string &_filename, int _fd, 165 size_t _len, uint8_t *_data, 166 Arch _arch, OpSys _opSys) 167 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 168 169{ 170 Elf *elf; 171 GElf_Ehdr ehdr; 172 173 // check that header matches library version 174 if (elf_version(EV_CURRENT) == EV_NONE) 175 panic("wrong elf version number!"); 176 177 // get a pointer to elf structure 178 elf = elf_memory((char*)fileData,len); 179 // will only fail if fd is invalid 180 assert(elf != NULL); 181 182 // Check that we actually have a elf file 183 if (gelf_getehdr(elf, &ehdr) ==0) { 184 panic("Not ELF, shouldn't be here"); 185 } 186 187 entry = ehdr.e_entry; 188 189 // initialize segment sizes to 0 in case they're not present 190 text.size = data.size = bss.size = 0; 191 192 for (int i = 0; i < ehdr.e_phnum; ++i) { 193 GElf_Phdr phdr; 194 if (gelf_getphdr(elf, i, &phdr) == 0) { 195 panic("gelf_getphdr failed for section %d", i); 196 } 197 198 // for now we don't care about non-loadable segments 199 if (!(phdr.p_type & PT_LOAD)) 200 continue; 201 202 // the headers don't explicitly distinguish text from data, 203 // but empirically the text segment comes first. 204 if (text.size == 0) { // haven't seen text segment yet 205 text.baseAddr = phdr.p_vaddr; 206 text.size = phdr.p_filesz; 207 text.fileImage = fileData + phdr.p_offset; 208 // if there's any padding at the end that's not in the 209 // file, call it the bss. This happens in the "text" 210 // segment if there's only one loadable segment (as for 211 // kernel images). 212 bss.size = phdr.p_memsz - phdr.p_filesz; 213 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 214 bss.fileImage = NULL; 215 } 216 else if (data.size == 0) { // have text, this must be data 217 data.baseAddr = phdr.p_vaddr; 218 data.size = phdr.p_filesz; 219 data.fileImage = fileData + phdr.p_offset; 220 // if there's any padding at the end that's not in the 221 // file, call it the bss. Warn if this happens for both 222 // the text & data segments (should only have one bss). 223 if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) { 224 warn("Two implied bss segments in file!\n"); 225 } 226 bss.size = phdr.p_memsz - phdr.p_filesz; 227 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 228 bss.fileImage = NULL; 229 } 230 } 231 232 // should have found at least one loadable segment 233 assert(text.size != 0); 234 235 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 236 text.baseAddr, text.size, data.baseAddr, data.size, 237 bss.baseAddr, bss.size); 238 239 elf_end(elf); 240 241 // We will actually read the sections when we need to load them 242} 243 244 245bool 246ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding) 247{ 248 Elf *elf; 249 int sec_idx = 1; // there is a 0 but it is nothing, go figure 250 Elf_Scn *section; 251 GElf_Shdr shdr; 252 Elf_Data *data; 253 int count, ii; 254 bool found = false; 255 GElf_Sym sym; 256 257 if (!symtab) 258 return false; 259 260 // check that header matches library version 261 if (elf_version(EV_CURRENT) == EV_NONE) 262 panic("wrong elf version number!"); 263 264 // get a pointer to elf structure 265 elf = elf_memory((char*)fileData,len); 266 267 assert(elf != NULL); 268 269 // Get the first section 270 section = elf_getscn(elf, sec_idx); 271 272 // While there are no more sections 273 while (section != NULL) { 274 gelf_getshdr(section, &shdr); 275 276 if (shdr.sh_type == SHT_SYMTAB) { 277 found = true; 278 data = elf_getdata(section, NULL); 279 count = shdr.sh_size / shdr.sh_entsize; 280 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count); 281 282 // loop through all the symbols, only loading global ones 283 for (ii = 0; ii < count; ++ii) { 284 gelf_getsym(data, ii, &sym); 285 if (GELF_ST_BIND(sym.st_info) == binding) { 286 symtab->insert(sym.st_value, 287 elf_strptr(elf, shdr.sh_link, sym.st_name)); 288 } 289 } 290 } 291 ++sec_idx; 292 section = elf_getscn(elf, sec_idx); 293 } 294 295 elf_end(elf); 296 297 return found; 298} 299 300bool 301ElfObject::loadGlobalSymbols(SymbolTable *symtab) 302{ 303 return loadSomeSymbols(symtab, STB_GLOBAL); 304} 305 306bool 307ElfObject::loadLocalSymbols(SymbolTable *symtab) 308{ 309 return loadSomeSymbols(symtab, STB_LOCAL); 310} 311