object_file.cc revision 2519
1/*
2 * Copyright (c) 2002-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <list>
30#include <string>
31
32#include <sys/types.h>
33#include <sys/mman.h>
34#include <fcntl.h>
35#include <stdio.h>
36#include <unistd.h>
37
38#include "base/cprintf.hh"
39#include "base/loader/object_file.hh"
40#include "base/loader/symtab.hh"
41
42#include "base/loader/ecoff_object.hh"
43#include "base/loader/aout_object.hh"
44#include "base/loader/elf_object.hh"
45
46#include "mem/translating_port.hh"
47
48using namespace std;
49
50ObjectFile::ObjectFile(const string &_filename, int _fd,
51                       size_t _len, uint8_t *_data,
52                       Arch _arch, OpSys _opSys)
53    : filename(_filename), descriptor(_fd), fileData(_data), len(_len),
54      arch(_arch), opSys(_opSys)
55{
56}
57
58
59ObjectFile::~ObjectFile()
60{
61    close();
62}
63
64
65bool
66ObjectFile::loadSection(Section *sec, Port *memPort, bool loadPhys)
67{
68    if (sec->size != 0) {
69        Addr addr = sec->baseAddr;
70        if (loadPhys) {
71            // this is Alpha-specific... going to have to fix this
72            // for other architectures
73            addr &= (ULL(1) << 40) - 1;
74        }
75
76        if (sec->fileImage) {
77            memPort->writeBlob(addr, sec->fileImage, sec->size);
78        }
79        else {
80            // no image: must be bss
81            memPort->memsetBlob(addr, 0, sec->size);
82        }
83    }
84    return true;
85}
86
87
88bool
89ObjectFile::loadSections(Port *memPort, bool loadPhys)
90{
91    return (loadSection(&text, memPort, loadPhys)
92            && loadSection(&data, memPort, loadPhys)
93            && loadSection(&bss, memPort, loadPhys));
94}
95
96
97void
98ObjectFile::close()
99{
100    if (descriptor >= 0) {
101        ::close(descriptor);
102        descriptor = -1;
103    }
104
105    if (fileData) {
106        ::munmap(fileData, len);
107        fileData = NULL;
108    }
109}
110
111
112ObjectFile *
113createObjectFile(const string &fname)
114{
115    // open the file
116    int fd = open(fname.c_str(), O_RDONLY);
117    if (fd < 0) {
118        return NULL;
119    }
120
121    // find the length of the file by seeking to the end
122    size_t len = (size_t)lseek(fd, 0, SEEK_END);
123
124    // mmap the whole shebang
125    uint8_t *fileData =
126        (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
127    if (fileData == MAP_FAILED) {
128        close(fd);
129        return NULL;
130    }
131
132    ObjectFile *fileObj = NULL;
133
134    // figure out what we have here
135    if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) {
136        return fileObj;
137    }
138
139    if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) {
140        return fileObj;
141    }
142
143    if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) {
144        return fileObj;
145    }
146
147    // don't know what it is
148    close(fd);
149    munmap(fileData, len);
150    return NULL;
151}
152