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