object_file.cc revision 3918
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" 483584Ssaidi@eecs.umich.edu#include "base/loader/raw_object.hh" 4912SN/A 502420SN/A#include "mem/translating_port.hh" 512420SN/A 522SN/Ausing namespace std; 532SN/A 5412SN/AObjectFile::ObjectFile(const string &_filename, int _fd, 55360SN/A size_t _len, uint8_t *_data, 56360SN/A Arch _arch, OpSys _opSys) 57360SN/A : filename(_filename), descriptor(_fd), fileData(_data), len(_len), 58360SN/A arch(_arch), opSys(_opSys) 5912SN/A{ 6012SN/A} 612SN/A 622SN/A 632SN/AObjectFile::~ObjectFile() 642SN/A{ 652SN/A close(); 6612SN/A} 672SN/A 682SN/A 692420SN/Abool 702520SN/AObjectFile::loadSection(Section *sec, Port *memPort, Addr addrMask) 712420SN/A{ 722420SN/A if (sec->size != 0) { 732520SN/A Addr addr = sec->baseAddr & addrMask; 742420SN/A if (sec->fileImage) { 752519SN/A memPort->writeBlob(addr, sec->fileImage, sec->size); 762420SN/A } 772420SN/A else { 782420SN/A // no image: must be bss 792519SN/A memPort->memsetBlob(addr, 0, sec->size); 802420SN/A } 812420SN/A } 822420SN/A return true; 832420SN/A} 842420SN/A 852420SN/A 862420SN/Abool 872520SN/AObjectFile::loadSections(Port *memPort, Addr addrMask) 882420SN/A{ 892520SN/A return (loadSection(&text, memPort, addrMask) 902520SN/A && loadSection(&data, memPort, addrMask) 912520SN/A && loadSection(&bss, memPort, addrMask)); 922420SN/A} 932420SN/A 942420SN/A 952SN/Avoid 962SN/AObjectFile::close() 972SN/A{ 9812SN/A if (descriptor >= 0) { 992SN/A ::close(descriptor); 10012SN/A descriptor = -1; 10112SN/A } 1022SN/A 10312SN/A if (fileData) { 1043918Ssaidi@eecs.umich.edu ::munmap((char*)fileData, len); 10512SN/A fileData = NULL; 10612SN/A } 1072SN/A} 1082SN/A 10912SN/A 11012SN/AObjectFile * 1113584Ssaidi@eecs.umich.educreateObjectFile(const string &fname, bool raw) 1122SN/A{ 11312SN/A // open the file 11412SN/A int fd = open(fname.c_str(), O_RDONLY); 11512SN/A if (fd < 0) { 11612SN/A return NULL; 1172SN/A } 1182SN/A 11912SN/A // find the length of the file by seeking to the end 12012SN/A size_t len = (size_t)lseek(fd, 0, SEEK_END); 12112SN/A 12212SN/A // mmap the whole shebang 12312SN/A uint8_t *fileData = 12412SN/A (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); 12512SN/A if (fileData == MAP_FAILED) { 12612SN/A close(fd); 12712SN/A return NULL; 1282SN/A } 1292SN/A 13012SN/A ObjectFile *fileObj = NULL; 1312SN/A 13212SN/A // figure out what we have here 13312SN/A if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) { 13412SN/A return fileObj; 1352SN/A } 1362SN/A 13712SN/A if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) { 13812SN/A return fileObj; 1392SN/A } 1402SN/A 14112SN/A if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) { 14212SN/A return fileObj; 1432SN/A } 1442SN/A 1453584Ssaidi@eecs.umich.edu if (raw) 1463584Ssaidi@eecs.umich.edu return RawObject::tryFile(fname, fd, len, fileData); 1473584Ssaidi@eecs.umich.edu 14812SN/A // don't know what it is 14912SN/A close(fd); 1503918Ssaidi@eecs.umich.edu munmap((char*)fileData, len); 15112SN/A return NULL; 1522SN/A} 153