object_file.cc revision 2665
12SN/A/* 21762SN/A * Copyright (c) 2002-2004 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292665Ssaidi@eecs.umich.edu * Steve Reinhardt 302SN/A */ 312SN/A 322SN/A#include <list> 332SN/A#include <string> 342SN/A 352SN/A#include <sys/types.h> 362SN/A#include <sys/mman.h> 372SN/A#include <fcntl.h> 382SN/A#include <stdio.h> 392SN/A#include <unistd.h> 402SN/A 4156SN/A#include "base/cprintf.hh" 4256SN/A#include "base/loader/object_file.hh" 4356SN/A#include "base/loader/symtab.hh" 442SN/A 4556SN/A#include "base/loader/ecoff_object.hh" 4656SN/A#include "base/loader/aout_object.hh" 4756SN/A#include "base/loader/elf_object.hh" 4812SN/A 492420SN/A#include "mem/translating_port.hh" 502420SN/A 512SN/Ausing namespace std; 522SN/A 5312SN/AObjectFile::ObjectFile(const string &_filename, int _fd, 54360SN/A size_t _len, uint8_t *_data, 55360SN/A Arch _arch, OpSys _opSys) 56360SN/A : filename(_filename), descriptor(_fd), fileData(_data), len(_len), 57360SN/A arch(_arch), opSys(_opSys) 5812SN/A{ 5912SN/A} 602SN/A 612SN/A 622SN/AObjectFile::~ObjectFile() 632SN/A{ 642SN/A close(); 6512SN/A} 662SN/A 672SN/A 682420SN/Abool 692520SN/AObjectFile::loadSection(Section *sec, Port *memPort, Addr addrMask) 702420SN/A{ 712420SN/A if (sec->size != 0) { 722520SN/A Addr addr = sec->baseAddr & addrMask; 732420SN/A if (sec->fileImage) { 742519SN/A memPort->writeBlob(addr, sec->fileImage, sec->size); 752420SN/A } 762420SN/A else { 772420SN/A // no image: must be bss 782519SN/A memPort->memsetBlob(addr, 0, sec->size); 792420SN/A } 802420SN/A } 812420SN/A return true; 822420SN/A} 832420SN/A 842420SN/A 852420SN/Abool 862520SN/AObjectFile::loadSections(Port *memPort, Addr addrMask) 872420SN/A{ 882520SN/A return (loadSection(&text, memPort, addrMask) 892520SN/A && loadSection(&data, memPort, addrMask) 902520SN/A && loadSection(&bss, memPort, addrMask)); 912420SN/A} 922420SN/A 932420SN/A 942SN/Avoid 952SN/AObjectFile::close() 962SN/A{ 9712SN/A if (descriptor >= 0) { 982SN/A ::close(descriptor); 9912SN/A descriptor = -1; 10012SN/A } 1012SN/A 10212SN/A if (fileData) { 10312SN/A ::munmap(fileData, len); 10412SN/A fileData = NULL; 10512SN/A } 1062SN/A} 1072SN/A 10812SN/A 10912SN/AObjectFile * 11012SN/AcreateObjectFile(const string &fname) 1112SN/A{ 11212SN/A // open the file 11312SN/A int fd = open(fname.c_str(), O_RDONLY); 11412SN/A if (fd < 0) { 11512SN/A return NULL; 1162SN/A } 1172SN/A 11812SN/A // find the length of the file by seeking to the end 11912SN/A size_t len = (size_t)lseek(fd, 0, SEEK_END); 12012SN/A 12112SN/A // mmap the whole shebang 12212SN/A uint8_t *fileData = 12312SN/A (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); 12412SN/A if (fileData == MAP_FAILED) { 12512SN/A close(fd); 12612SN/A return NULL; 1272SN/A } 1282SN/A 12912SN/A ObjectFile *fileObj = NULL; 1302SN/A 13112SN/A // figure out what we have here 13212SN/A if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) { 13312SN/A return fileObj; 1342SN/A } 1352SN/A 13612SN/A if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) { 13712SN/A return fileObj; 1382SN/A } 1392SN/A 14012SN/A if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) { 14112SN/A return fileObj; 1422SN/A } 1432SN/A 14412SN/A // don't know what it is 14512SN/A close(fd); 14612SN/A munmap(fileData, len); 14712SN/A return NULL; 1482SN/A} 149