object_file.cc revision 3918:1f9a98d198e8
112027Sjungma@eit.uni-kl.de/* 212027Sjungma@eit.uni-kl.de * Copyright (c) 2002-2004 The Regents of The University of Michigan 312027Sjungma@eit.uni-kl.de * All rights reserved. 412027Sjungma@eit.uni-kl.de * 512027Sjungma@eit.uni-kl.de * Redistribution and use in source and binary forms, with or without 612027Sjungma@eit.uni-kl.de * modification, are permitted provided that the following conditions are 712027Sjungma@eit.uni-kl.de * met: redistributions of source code must retain the above copyright 812027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer; 912027Sjungma@eit.uni-kl.de * redistributions in binary form must reproduce the above copyright 1012027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer in the 1112027Sjungma@eit.uni-kl.de * documentation and/or other materials provided with the distribution; 1212027Sjungma@eit.uni-kl.de * neither the name of the copyright holders nor the names of its 1312027Sjungma@eit.uni-kl.de * contributors may be used to endorse or promote products derived from 1412027Sjungma@eit.uni-kl.de * this software without specific prior written permission. 1512027Sjungma@eit.uni-kl.de * 1612027Sjungma@eit.uni-kl.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712027Sjungma@eit.uni-kl.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812027Sjungma@eit.uni-kl.de * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912027Sjungma@eit.uni-kl.de * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012027Sjungma@eit.uni-kl.de * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112027Sjungma@eit.uni-kl.de * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212027Sjungma@eit.uni-kl.de * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312027Sjungma@eit.uni-kl.de * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412027Sjungma@eit.uni-kl.de * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512027Sjungma@eit.uni-kl.de * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612027Sjungma@eit.uni-kl.de * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712027Sjungma@eit.uni-kl.de * 2812027Sjungma@eit.uni-kl.de * Authors: Nathan Binkert 2912027Sjungma@eit.uni-kl.de * Steve Reinhardt 3012027Sjungma@eit.uni-kl.de */ 3112027Sjungma@eit.uni-kl.de 3212027Sjungma@eit.uni-kl.de#include <list> 3312027Sjungma@eit.uni-kl.de#include <string> 3412027Sjungma@eit.uni-kl.de 3512027Sjungma@eit.uni-kl.de#include <sys/types.h> 3612027Sjungma@eit.uni-kl.de#include <sys/mman.h> 3712027Sjungma@eit.uni-kl.de#include <fcntl.h> 3812027Sjungma@eit.uni-kl.de#include <stdio.h> 3912027Sjungma@eit.uni-kl.de#include <unistd.h> 4012027Sjungma@eit.uni-kl.de 4112027Sjungma@eit.uni-kl.de#include "base/cprintf.hh" 4212027Sjungma@eit.uni-kl.de#include "base/loader/object_file.hh" 4312027Sjungma@eit.uni-kl.de#include "base/loader/symtab.hh" 4412027Sjungma@eit.uni-kl.de 4512027Sjungma@eit.uni-kl.de#include "base/loader/ecoff_object.hh" 4612027Sjungma@eit.uni-kl.de#include "base/loader/aout_object.hh" 4712027Sjungma@eit.uni-kl.de#include "base/loader/elf_object.hh" 4812027Sjungma@eit.uni-kl.de#include "base/loader/raw_object.hh" 4912027Sjungma@eit.uni-kl.de 5012027Sjungma@eit.uni-kl.de#include "mem/translating_port.hh" 5112027Sjungma@eit.uni-kl.de 5212027Sjungma@eit.uni-kl.deusing namespace std; 5312027Sjungma@eit.uni-kl.de 5412027Sjungma@eit.uni-kl.deObjectFile::ObjectFile(const string &_filename, int _fd, 5512027Sjungma@eit.uni-kl.de size_t _len, uint8_t *_data, 5612027Sjungma@eit.uni-kl.de Arch _arch, OpSys _opSys) 5712027Sjungma@eit.uni-kl.de : filename(_filename), descriptor(_fd), fileData(_data), len(_len), 5812027Sjungma@eit.uni-kl.de arch(_arch), opSys(_opSys) 5912027Sjungma@eit.uni-kl.de{ 6012027Sjungma@eit.uni-kl.de} 6112027Sjungma@eit.uni-kl.de 6212027Sjungma@eit.uni-kl.de 6312027Sjungma@eit.uni-kl.deObjectFile::~ObjectFile() 6412027Sjungma@eit.uni-kl.de{ 6512027Sjungma@eit.uni-kl.de close(); 6612027Sjungma@eit.uni-kl.de} 6712027Sjungma@eit.uni-kl.de 6812027Sjungma@eit.uni-kl.de 6912027Sjungma@eit.uni-kl.debool 7012027Sjungma@eit.uni-kl.deObjectFile::loadSection(Section *sec, Port *memPort, Addr addrMask) 7112027Sjungma@eit.uni-kl.de{ 7212027Sjungma@eit.uni-kl.de if (sec->size != 0) { 7312027Sjungma@eit.uni-kl.de Addr addr = sec->baseAddr & addrMask; 7412027Sjungma@eit.uni-kl.de if (sec->fileImage) { 7512027Sjungma@eit.uni-kl.de memPort->writeBlob(addr, sec->fileImage, sec->size); 7612027Sjungma@eit.uni-kl.de } 7712027Sjungma@eit.uni-kl.de else { 7812027Sjungma@eit.uni-kl.de // no image: must be bss 7912027Sjungma@eit.uni-kl.de memPort->memsetBlob(addr, 0, sec->size); 8012027Sjungma@eit.uni-kl.de } 8112027Sjungma@eit.uni-kl.de } 8212027Sjungma@eit.uni-kl.de return true; 8312027Sjungma@eit.uni-kl.de} 8412027Sjungma@eit.uni-kl.de 8512027Sjungma@eit.uni-kl.de 8612027Sjungma@eit.uni-kl.debool 8712027Sjungma@eit.uni-kl.deObjectFile::loadSections(Port *memPort, Addr addrMask) 8812027Sjungma@eit.uni-kl.de{ 8912027Sjungma@eit.uni-kl.de return (loadSection(&text, memPort, addrMask) 9012027Sjungma@eit.uni-kl.de && loadSection(&data, memPort, addrMask) 9112027Sjungma@eit.uni-kl.de && loadSection(&bss, memPort, addrMask)); 9212027Sjungma@eit.uni-kl.de} 9312027Sjungma@eit.uni-kl.de 9412027Sjungma@eit.uni-kl.de 9512027Sjungma@eit.uni-kl.devoid 9612027Sjungma@eit.uni-kl.deObjectFile::close() 9712027Sjungma@eit.uni-kl.de{ 9812027Sjungma@eit.uni-kl.de if (descriptor >= 0) { 9912027Sjungma@eit.uni-kl.de ::close(descriptor); 10012027Sjungma@eit.uni-kl.de descriptor = -1; 10112027Sjungma@eit.uni-kl.de } 10212027Sjungma@eit.uni-kl.de 10312027Sjungma@eit.uni-kl.de if (fileData) { 10412027Sjungma@eit.uni-kl.de ::munmap((char*)fileData, len); 10512027Sjungma@eit.uni-kl.de fileData = NULL; 10612027Sjungma@eit.uni-kl.de } 10712027Sjungma@eit.uni-kl.de} 10812027Sjungma@eit.uni-kl.de 10912027Sjungma@eit.uni-kl.de 11012027Sjungma@eit.uni-kl.deObjectFile * 11112027Sjungma@eit.uni-kl.decreateObjectFile(const string &fname, bool raw) 11212027Sjungma@eit.uni-kl.de{ 11312027Sjungma@eit.uni-kl.de // open the file 11412027Sjungma@eit.uni-kl.de int fd = open(fname.c_str(), O_RDONLY); 11512027Sjungma@eit.uni-kl.de if (fd < 0) { 11612027Sjungma@eit.uni-kl.de return NULL; 11712027Sjungma@eit.uni-kl.de } 11812027Sjungma@eit.uni-kl.de 11912027Sjungma@eit.uni-kl.de // find the length of the file by seeking to the end 12012027Sjungma@eit.uni-kl.de size_t len = (size_t)lseek(fd, 0, SEEK_END); 12112027Sjungma@eit.uni-kl.de 12212027Sjungma@eit.uni-kl.de // mmap the whole shebang 12312027Sjungma@eit.uni-kl.de uint8_t *fileData = 12412027Sjungma@eit.uni-kl.de (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); 12512027Sjungma@eit.uni-kl.de if (fileData == MAP_FAILED) { 12612027Sjungma@eit.uni-kl.de close(fd); 12712027Sjungma@eit.uni-kl.de return NULL; 12812027Sjungma@eit.uni-kl.de } 12912027Sjungma@eit.uni-kl.de 13012027Sjungma@eit.uni-kl.de ObjectFile *fileObj = NULL; 13112027Sjungma@eit.uni-kl.de 13212027Sjungma@eit.uni-kl.de // figure out what we have here 13312027Sjungma@eit.uni-kl.de if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) { 13412027Sjungma@eit.uni-kl.de return fileObj; 13512027Sjungma@eit.uni-kl.de } 13612027Sjungma@eit.uni-kl.de 13712027Sjungma@eit.uni-kl.de if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) { 13812027Sjungma@eit.uni-kl.de return fileObj; 13912027Sjungma@eit.uni-kl.de } 14012027Sjungma@eit.uni-kl.de 14112027Sjungma@eit.uni-kl.de if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) { 14212027Sjungma@eit.uni-kl.de return fileObj; 14312027Sjungma@eit.uni-kl.de } 14412027Sjungma@eit.uni-kl.de 14512027Sjungma@eit.uni-kl.de if (raw) 14612027Sjungma@eit.uni-kl.de return RawObject::tryFile(fname, fd, len, fileData); 14712027Sjungma@eit.uni-kl.de 14812027Sjungma@eit.uni-kl.de // don't know what it is 14912027Sjungma@eit.uni-kl.de close(fd); 15012027Sjungma@eit.uni-kl.de munmap((char*)fileData, len); 15112027Sjungma@eit.uni-kl.de return NULL; 15212027Sjungma@eit.uni-kl.de} 15312027Sjungma@eit.uni-kl.de