object_file.cc revision 10422
1/* 2 * Copyright (c) 2002-2004 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 * Authors: Nathan Binkert 29 * Steve Reinhardt 30 */ 31 32#include <sys/mman.h> 33#include <sys/types.h> 34#include <fcntl.h> 35#include <unistd.h> 36 37#include <cstdio> 38#include <list> 39#include <string> 40 41#include "base/loader/aout_object.hh" 42#include "base/loader/dtb_object.hh" 43#include "base/loader/ecoff_object.hh" 44#include "base/loader/elf_object.hh" 45#include "base/loader/object_file.hh" 46#include "base/loader/raw_object.hh" 47#include "base/loader/symtab.hh" 48#include "base/cprintf.hh" 49#include "mem/port_proxy.hh" 50 51using namespace std; 52 53ObjectFile::ObjectFile(const string &_filename, int _fd, 54 size_t _len, uint8_t *_data, 55 Arch _arch, OpSys _opSys) 56 : filename(_filename), descriptor(_fd), fileData(_data), len(_len), 57 arch(_arch), opSys(_opSys), entry(0), globalPtr(0), 58 text{0, nullptr, 0}, data{0, nullptr, 0}, bss{0, nullptr, 0} 59{ 60} 61 62 63ObjectFile::~ObjectFile() 64{ 65 close(); 66} 67 68 69bool 70ObjectFile::loadSection(Section *sec, PortProxy& memProxy, Addr addrMask, Addr offset) 71{ 72 if (sec->size != 0) { 73 Addr addr = (sec->baseAddr & addrMask) + offset; 74 if (sec->fileImage) { 75 memProxy.writeBlob(addr, sec->fileImage, sec->size); 76 } 77 else { 78 // no image: must be bss 79 memProxy.memsetBlob(addr, 0, sec->size); 80 } 81 } 82 return true; 83} 84 85 86bool 87ObjectFile::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset) 88{ 89 return (loadSection(&text, memProxy, addrMask, offset) 90 && loadSection(&data, memProxy, addrMask, offset) 91 && loadSection(&bss, memProxy, addrMask, offset)); 92} 93 94 95void 96ObjectFile::close() 97{ 98 if (descriptor >= 0) { 99 ::close(descriptor); 100 descriptor = -1; 101 } 102 103 if (fileData) { 104 ::munmap((char*)fileData, len); 105 fileData = NULL; 106 } 107} 108 109 110ObjectFile * 111createObjectFile(const string &fname, bool raw) 112{ 113 // open the file 114 int fd = open(fname.c_str(), O_RDONLY); 115 if (fd < 0) { 116 return NULL; 117 } 118 119 // find the length of the file by seeking to the end 120 off_t off = lseek(fd, 0, SEEK_END); 121 fatal_if(off < 0, "Failed to determine size of object file %s\n", fname); 122 size_t len = static_cast<size_t>(off); 123 124 // mmap the whole shebang 125 uint8_t *fileData = 126 (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); 127 if (fileData == MAP_FAILED) { 128 close(fd); 129 return NULL; 130 } 131 132 ObjectFile *fileObj = NULL; 133 134 // figure out what we have here 135 if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) { 136 return fileObj; 137 } 138 139 if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) { 140 return fileObj; 141 } 142 143 if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) { 144 return fileObj; 145 } 146 147 if ((fileObj = DtbObject::tryFile(fname, fd, len, fileData)) != NULL) { 148 return fileObj; 149 } 150 151 if (raw) 152 return RawObject::tryFile(fname, fd, len, fileData); 153 154 // don't know what it is 155 close(fd); 156 munmap((char*)fileData, len); 157 return NULL; 158} 159