object_file.cc revision 10422
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" 429538Satgutier@umich.edu#include "base/loader/dtb_object.hh" 438229Snate@binkert.org#include "base/loader/ecoff_object.hh" 448229Snate@binkert.org#include "base/loader/elf_object.hh" 458229Snate@binkert.org#include "base/loader/object_file.hh" 468229Snate@binkert.org#include "base/loader/raw_object.hh" 478229Snate@binkert.org#include "base/loader/symtab.hh" 4856SN/A#include "base/cprintf.hh" 498706Sandreas.hansson@arm.com#include "mem/port_proxy.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), 5710422Sandreas.hansson@arm.com arch(_arch), opSys(_opSys), entry(0), globalPtr(0), 5810422Sandreas.hansson@arm.com text{0, nullptr, 0}, data{0, nullptr, 0}, bss{0, nullptr, 0} 5912SN/A{ 6012SN/A} 612SN/A 622SN/A 632SN/AObjectFile::~ObjectFile() 642SN/A{ 652SN/A close(); 6612SN/A} 672SN/A 682SN/A 692420SN/Abool 7010037SARM gem5 DevelopersObjectFile::loadSection(Section *sec, PortProxy& memProxy, Addr addrMask, Addr offset) 712420SN/A{ 722420SN/A if (sec->size != 0) { 7310037SARM gem5 Developers Addr addr = (sec->baseAddr & addrMask) + offset; 742420SN/A if (sec->fileImage) { 758852Sandreas.hansson@arm.com memProxy.writeBlob(addr, sec->fileImage, sec->size); 762420SN/A } 772420SN/A else { 782420SN/A // no image: must be bss 798852Sandreas.hansson@arm.com memProxy.memsetBlob(addr, 0, sec->size); 802420SN/A } 812420SN/A } 822420SN/A return true; 832420SN/A} 842420SN/A 852420SN/A 862420SN/Abool 8710037SARM gem5 DevelopersObjectFile::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset) 882420SN/A{ 8910037SARM gem5 Developers return (loadSection(&text, memProxy, addrMask, offset) 9010037SARM gem5 Developers && loadSection(&data, memProxy, addrMask, offset) 9110037SARM gem5 Developers && loadSection(&bss, memProxy, addrMask, offset)); 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 12010422Sandreas.hansson@arm.com off_t off = lseek(fd, 0, SEEK_END); 12110422Sandreas.hansson@arm.com fatal_if(off < 0, "Failed to determine size of object file %s\n", fname); 12210422Sandreas.hansson@arm.com size_t len = static_cast<size_t>(off); 12312SN/A 12412SN/A // mmap the whole shebang 12512SN/A uint8_t *fileData = 12612SN/A (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); 12712SN/A if (fileData == MAP_FAILED) { 12812SN/A close(fd); 12912SN/A return NULL; 1302SN/A } 1312SN/A 13212SN/A ObjectFile *fileObj = NULL; 1332SN/A 13412SN/A // figure out what we have here 13512SN/A if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) { 13612SN/A return fileObj; 1372SN/A } 1382SN/A 13912SN/A if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) { 14012SN/A return fileObj; 1412SN/A } 1422SN/A 14312SN/A if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) { 14412SN/A return fileObj; 1452SN/A } 1462SN/A 1479538Satgutier@umich.edu if ((fileObj = DtbObject::tryFile(fname, fd, len, fileData)) != NULL) { 1489538Satgutier@umich.edu return fileObj; 1499538Satgutier@umich.edu } 1509538Satgutier@umich.edu 1513584Ssaidi@eecs.umich.edu if (raw) 1523584Ssaidi@eecs.umich.edu return RawObject::tryFile(fname, fd, len, fileData); 1533584Ssaidi@eecs.umich.edu 15412SN/A // don't know what it is 15512SN/A close(fd); 1563918Ssaidi@eecs.umich.edu munmap((char*)fileData, len); 15712SN/A return NULL; 1582SN/A} 159