object_file.cc revision 9538
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),
579186SAli.Saidi@ARM.com      arch(_arch), opSys(_opSys), globalPtr(0)
5812SN/A{
5912SN/A}
602SN/A
612SN/A
622SN/AObjectFile::~ObjectFile()
632SN/A{
642SN/A    close();
6512SN/A}
662SN/A
672SN/A
682420SN/Abool
698852Sandreas.hansson@arm.comObjectFile::loadSection(Section *sec, PortProxy& memProxy, Addr addrMask)
702420SN/A{
712420SN/A    if (sec->size != 0) {
722520SN/A        Addr addr = sec->baseAddr & addrMask;
732420SN/A        if (sec->fileImage) {
748852Sandreas.hansson@arm.com            memProxy.writeBlob(addr, sec->fileImage, sec->size);
752420SN/A        }
762420SN/A        else {
772420SN/A            // no image: must be bss
788852Sandreas.hansson@arm.com            memProxy.memsetBlob(addr, 0, sec->size);
792420SN/A        }
802420SN/A    }
812420SN/A    return true;
822420SN/A}
832420SN/A
842420SN/A
852420SN/Abool
868852Sandreas.hansson@arm.comObjectFile::loadSections(PortProxy& memProxy, Addr addrMask)
872420SN/A{
888706Sandreas.hansson@arm.com    return (loadSection(&text, memProxy, addrMask)
898706Sandreas.hansson@arm.com            && loadSection(&data, memProxy, addrMask)
908706Sandreas.hansson@arm.com            && loadSection(&bss, memProxy, 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) {
1033918Ssaidi@eecs.umich.edu        ::munmap((char*)fileData, len);
10412SN/A        fileData = NULL;
10512SN/A    }
1062SN/A}
1072SN/A
10812SN/A
10912SN/AObjectFile *
1103584Ssaidi@eecs.umich.educreateObjectFile(const string &fname, bool raw)
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
1449538Satgutier@umich.edu    if ((fileObj = DtbObject::tryFile(fname, fd, len, fileData)) != NULL) {
1459538Satgutier@umich.edu        return fileObj;
1469538Satgutier@umich.edu    }
1479538Satgutier@umich.edu
1483584Ssaidi@eecs.umich.edu    if (raw)
1493584Ssaidi@eecs.umich.edu        return RawObject::tryFile(fname, fd, len, fileData);
1503584Ssaidi@eecs.umich.edu
15112SN/A    // don't know what it is
15212SN/A    close(fd);
1533918Ssaidi@eecs.umich.edu    munmap((char*)fileData, len);
15412SN/A    return NULL;
1552SN/A}
156