elf_object.cc revision 56
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#include "base/loader/elf_object.hh" 32 33#include "mem/functional_mem/functional_memory.hh" 34#include "base/loader/symtab.hh" 35 36#include "base/trace.hh" // for DPRINTF 37 38#include "base/loader/exec_elf.h" 39 40using namespace std; 41 42ObjectFile * 43ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 44{ 45 if (memcmp(((Elf64_Ehdr *)data)->e_ident, ELFMAG, SELFMAG) == 0) { 46 // for now we'll assume it's a 64-bit Alpha binary 47 return new ElfObject(fname, fd, len, data); 48 } 49 else { 50 return NULL; 51 } 52} 53 54 55ElfObject::ElfObject(const string &_filename, int _fd, 56 size_t _len, uint8_t *_data) 57 : ObjectFile(_filename, _fd, _len, _data) 58{ 59 ehdr = (Elf64_Ehdr *)fileData; 60 61 entry = ehdr->e_entry; 62 63 phdr = (Elf64_Phdr *)(fileData + ehdr->e_phoff); 64 assert(sizeof(Elf64_Phdr) == ehdr->e_phentsize); 65 66 bool foundText = false; 67 bool foundData = false; 68 for (int i = 0; i < ehdr->e_phnum; ++i) { 69 Elf64_Phdr *p = &phdr[i]; 70 71 // for now we don't care about non-loadable segments 72 if (!(p->p_type & PT_LOAD)) 73 continue; 74 75 if (p->p_flags & PF_X) { 76 // executable: must be text 77 assert(!foundText); 78 foundText = true; 79 textPhdrIdx = i; 80 text.baseAddr = p->p_vaddr; 81 text.size = p->p_filesz; 82 assert(p->p_filesz == p->p_memsz); 83 } 84 else { 85 assert(p->p_flags & PF_R); 86 assert(!foundData); 87 foundData = true; 88 dataPhdrIdx = i; 89 data.baseAddr = p->p_vaddr; 90 data.size = p->p_filesz; 91 bss.baseAddr = data.baseAddr + data.size; 92 bss.size = p->p_memsz - p->p_filesz; 93 } 94 } 95 96 assert(foundText && foundData); 97 98 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 99 text.baseAddr, text.size, data.baseAddr, data.size, 100 bss.baseAddr, bss.size); 101} 102 103 104bool 105ElfObject::loadSections(FunctionalMemory *mem, bool loadPhys) 106{ 107 Addr textAddr = text.baseAddr; 108 Addr dataAddr = data.baseAddr; 109 110 if (loadPhys) { 111 textAddr &= (ULL(1) << 40) - 1; 112 dataAddr &= (ULL(1) << 40) - 1; 113 } 114 115 // Since we don't really have an MMU and all memory is 116 // zero-filled, there's no need to set up the BSS segment. 117 if (text.size != 0) 118 mem->prot_write(textAddr, fileData + phdr[textPhdrIdx].p_offset, 119 text.size); 120 if (data.size != 0) 121 mem->prot_write(dataAddr, fileData + phdr[dataPhdrIdx].p_offset, 122 data.size); 123 124 return true; 125} 126 127 128bool 129ElfObject::loadGlobalSymbols(SymbolTable *symtab) 130{ 131 // symbols not supported yet 132 return false; 133} 134 135bool 136ElfObject::loadLocalSymbols(SymbolTable *symtab) 137{ 138 // symbols not supported yet 139 return false; 140} 141