elf_object.cc revision 661
1/* 2 * Copyright (c) 2003 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 46#include "mem/functional_mem/functional_memory.hh" 47#include "base/loader/symtab.hh" 48 49#include "base/trace.hh" // for DPRINTF 50 51 52using namespace std; 53 54ObjectFile * 55ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 56{ 57 Elf *elf; 58 GElf_Ehdr ehdr; 59 60 // check that header matches library version 61 assert(elf_version(EV_CURRENT) != EV_NONE); 62 63 // get a pointer to elf structure 64 elf = elf_memory((char*)data,len); 65 // will only fail if fd is invalid 66 assert(elf != NULL); 67 68 // Check that we actually have a elf file 69 if (gelf_getehdr(elf, &ehdr) ==0) { 70 DPRINTFR(Loader, "Not ELF\n"); 71 elf_end(elf); 72 return NULL; 73 } 74 else { 75 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) 76 panic("32 bit ELF Binary, Not Supported"); 77 if (ehdr.e_machine != EM_ALPHA) 78 panic("Non Alpha Binary, Not Supported"); 79 80 elf_end(elf); 81 82 return new ElfObject(fname, fd, len, data, 83 ObjectFile::Alpha, ObjectFile::Linux); 84 } 85} 86 87 88ElfObject::ElfObject(const string &_filename, int _fd, 89 size_t _len, uint8_t *_data, 90 Arch _arch, OpSys _opSys) 91 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 92 93{ 94 Elf *elf; 95 GElf_Ehdr ehdr; 96 97 // check that header matches library version 98 assert(elf_version(EV_CURRENT) != EV_NONE); 99 100 // get a pointer to elf structure 101 elf = elf_memory((char*)fileData,len); 102 // will only fail if fd is invalid 103 assert(elf != NULL); 104 105 // Check that we actually have a elf file 106 if (gelf_getehdr(elf, &ehdr) ==0) { 107 panic("Not ELF, shouldn't be here"); 108 } 109 110 entry = ehdr.e_entry; 111 112 // initialize segment sizes to 0 in case they're not present 113 text.size = data.size = bss.size = 0; 114 115 for (int i = 0; i < ehdr.e_phnum; ++i) { 116 GElf_Phdr phdr; 117 if (gelf_getphdr(elf, i, &phdr) == 0) { 118 panic("gelf_getphdr failed for section %d", i); 119 } 120 121 // for now we don't care about non-loadable segments 122 if (!(phdr.p_type & PT_LOAD)) 123 continue; 124 125 // the headers don't explicitly distinguish text from data, 126 // but empirically the text segment comes first. 127 if (text.size == 0) { // haven't seen text segment yet 128 text.baseAddr = phdr.p_vaddr; 129 text.size = phdr.p_filesz; 130 // remember where the data is for loadSections() 131 fileTextBits = fileData + phdr.p_offset; 132 // if there's any padding at the end that's not in the 133 // file, call it the bss. This happens in the "text" 134 // segment if there's only one loadable segment (as for 135 // kernel images). 136 bss.size = phdr.p_memsz - phdr.p_filesz; 137 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 138 } 139 else if (data.size == 0) { // have text, this must be data 140 data.baseAddr = phdr.p_vaddr; 141 data.size = phdr.p_filesz; 142 // remember where the data is for loadSections() 143 fileDataBits = fileData + phdr.p_offset; 144 // if there's any padding at the end that's not in the 145 // file, call it the bss. Warn if this happens for both 146 // the text & data segments (should only have one bss). 147 if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) { 148 warn("Two implied bss segments in file!\n"); 149 } 150 bss.size = phdr.p_memsz - phdr.p_filesz; 151 bss.baseAddr = phdr.p_vaddr + phdr.p_filesz; 152 } 153 } 154 155 // should have found at least one loadable segment 156 assert(text.size != 0); 157 158 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 159 text.baseAddr, text.size, data.baseAddr, data.size, 160 bss.baseAddr, bss.size); 161 162 elf_end(elf); 163 164 // We will actually read the sections when we need to load them 165} 166 167 168bool 169ElfObject::loadSections(FunctionalMemory *mem, bool loadPhys) 170{ 171 Addr textAddr = text.baseAddr; 172 Addr dataAddr = data.baseAddr; 173 174 if (loadPhys) { 175 textAddr &= (ULL(1) << 40) - 1; 176 dataAddr &= (ULL(1) << 40) - 1; 177 } 178 179 // Since we don't really have an MMU and all memory is 180 // zero-filled, there's no need to set up the BSS segment. 181 if (text.size != 0) 182 mem->prot_write(textAddr, fileTextBits, text.size); 183 if (data.size != 0) 184 mem->prot_write(dataAddr, fileDataBits, data.size); 185 186 return true; 187} 188 189 190bool 191ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding) 192{ 193 Elf *elf; 194 int secidx = 1; // there is a 0 but it is nothing, go figure 195 Elf_Scn *section; 196 GElf_Shdr shdr; 197 Elf_Data *data; 198 int count, ii; 199 bool found = false; 200 GElf_Sym sym; 201 202 if (!symtab) 203 return false; 204 205 // check that header matches library version 206 assert(elf_version(EV_CURRENT) != EV_NONE); 207 208 // get a pointer to elf structure 209 elf = elf_memory((char*)fileData,len); 210 211 assert(elf != NULL); 212 213 // Get the first section 214 section = elf_getscn(elf, secidx); 215 216 // While there are no more sections 217 while (section != NULL) { 218 gelf_getshdr(section, &shdr); 219 220 if (shdr.sh_type == SHT_SYMTAB) { 221 found = true; 222 data = elf_getdata(section, NULL); 223 count = shdr.sh_size / shdr.sh_entsize; 224 DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count); 225 226 // loop through all the symbols, only loading global ones 227 for (ii = 0; ii < count; ++ii) { 228 gelf_getsym(data, ii, &sym); 229 if (GELF_ST_BIND(sym.st_info) & binding) { 230 symtab->insert(sym.st_value, 231 elf_strptr(elf, shdr.sh_link, sym.st_name)); 232 } 233 } 234 } 235 ++secidx; 236 section = elf_getscn(elf, secidx); 237 } 238 239 elf_end(elf); 240 241 return found; 242} 243 244bool 245ElfObject::loadGlobalSymbols(SymbolTable *symtab) 246{ 247 return loadSomeSymbols(symtab, STB_GLOBAL); 248} 249 250bool 251ElfObject::loadLocalSymbols(SymbolTable *symtab) 252{ 253 return loadSomeSymbols(symtab, STB_LOCAL); 254} 255