object_file.cc revision 9186
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 328229Snate@binkert.org#include <sys/mman.h> 338229Snate@binkert.org#include <sys/types.h> 348229Snate@binkert.org#include <fcntl.h> 358229Snate@binkert.org#include <unistd.h> 368229Snate@binkert.org 378229Snate@binkert.org#include <cstdio> 382SN/A#include <list> 392SN/A#include <string> 402SN/A 418229Snate@binkert.org#include "base/loader/aout_object.hh" 428229Snate@binkert.org#include "base/loader/ecoff_object.hh" 438229Snate@binkert.org#include "base/loader/elf_object.hh" 448229Snate@binkert.org#include "base/loader/object_file.hh" 458229Snate@binkert.org#include "base/loader/raw_object.hh" 468229Snate@binkert.org#include "base/loader/symtab.hh" 4756SN/A#include "base/cprintf.hh" 488706Sandreas.hansson@arm.com#include "mem/port_proxy.hh" 492420SN/A 502SN/Ausing namespace std; 512SN/A 5212SN/AObjectFile::ObjectFile(const string &_filename, int _fd, 53360SN/A size_t _len, uint8_t *_data, 54360SN/A Arch _arch, OpSys _opSys) 55360SN/A : filename(_filename), descriptor(_fd), fileData(_data), len(_len), 569186SAli.Saidi@ARM.com arch(_arch), opSys(_opSys), globalPtr(0) 5712SN/A{ 5812SN/A} 592SN/A 602SN/A 612SN/AObjectFile::~ObjectFile() 622SN/A{ 632SN/A close(); 6412SN/A} 652SN/A 662SN/A 672420SN/Abool 688852Sandreas.hansson@arm.comObjectFile::loadSection(Section *sec, PortProxy& memProxy, Addr addrMask) 692420SN/A{ 702420SN/A if (sec->size != 0) { 712520SN/A Addr addr = sec->baseAddr & addrMask; 722420SN/A if (sec->fileImage) { 738852Sandreas.hansson@arm.com memProxy.writeBlob(addr, sec->fileImage, sec->size); 742420SN/A } 752420SN/A else { 762420SN/A // no image: must be bss 778852Sandreas.hansson@arm.com memProxy.memsetBlob(addr, 0, sec->size); 782420SN/A } 792420SN/A } 802420SN/A return true; 812420SN/A} 822420SN/A 832420SN/A 842420SN/Abool 858852Sandreas.hansson@arm.comObjectFile::loadSections(PortProxy& memProxy, Addr addrMask) 862420SN/A{ 878706Sandreas.hansson@arm.com return (loadSection(&text, memProxy, addrMask) 888706Sandreas.hansson@arm.com && loadSection(&data, memProxy, addrMask) 898706Sandreas.hansson@arm.com && loadSection(&bss, memProxy, addrMask)); 902420SN/A} 912420SN/A 922420SN/A 932SN/Avoid 942SN/AObjectFile::close() 952SN/A{ 9612SN/A if (descriptor >= 0) { 972SN/A ::close(descriptor); 9812SN/A descriptor = -1; 9912SN/A } 1002SN/A 10112SN/A if (fileData) { 1023918Ssaidi@eecs.umich.edu ::munmap((char*)fileData, len); 10312SN/A fileData = NULL; 10412SN/A } 1052SN/A} 1062SN/A 10712SN/A 10812SN/AObjectFile * 1093584Ssaidi@eecs.umich.educreateObjectFile(const string &fname, bool raw) 1102SN/A{ 11112SN/A // open the file 11212SN/A int fd = open(fname.c_str(), O_RDONLY); 11312SN/A if (fd < 0) { 11412SN/A return NULL; 1152SN/A } 1162SN/A 11712SN/A // find the length of the file by seeking to the end 11812SN/A size_t len = (size_t)lseek(fd, 0, SEEK_END); 11912SN/A 12012SN/A // mmap the whole shebang 12112SN/A uint8_t *fileData = 12212SN/A (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); 12312SN/A if (fileData == MAP_FAILED) { 12412SN/A close(fd); 12512SN/A return NULL; 1262SN/A } 1272SN/A 12812SN/A ObjectFile *fileObj = NULL; 1292SN/A 13012SN/A // figure out what we have here 13112SN/A if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) { 13212SN/A return fileObj; 1332SN/A } 1342SN/A 13512SN/A if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) { 13612SN/A return fileObj; 1372SN/A } 1382SN/A 13912SN/A if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) { 14012SN/A return fileObj; 1412SN/A } 1422SN/A 1433584Ssaidi@eecs.umich.edu if (raw) 1443584Ssaidi@eecs.umich.edu return RawObject::tryFile(fname, fd, len, fileData); 1453584Ssaidi@eecs.umich.edu 14612SN/A // don't know what it is 14712SN/A close(fd); 1483918Ssaidi@eecs.umich.edu munmap((char*)fileData, len); 14912SN/A return NULL; 1502SN/A} 151