object_file.cc revision 11354:414abc839464
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 * Authors: Nathan Binkert
29 *          Steve Reinhardt
30 */
31
32#include <sys/mman.h>
33#include <sys/types.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <zlib.h>
37
38#include <cstdio>
39#include <list>
40#include <string>
41
42#include "base/loader/aout_object.hh"
43#include "base/loader/dtb_object.hh"
44#include "base/loader/ecoff_object.hh"
45#include "base/loader/elf_object.hh"
46#include "base/loader/object_file.hh"
47#include "base/loader/raw_object.hh"
48#include "base/loader/symtab.hh"
49#include "base/cprintf.hh"
50#include "mem/port_proxy.hh"
51
52using namespace std;
53
54ObjectFile::ObjectFile(const string &_filename,
55                       size_t _len, uint8_t *_data,
56                       Arch _arch, OpSys _opSys)
57    : filename(_filename), fileData(_data), len(_len),
58      arch(_arch), opSys(_opSys), entry(0), globalPtr(0),
59      text{0, nullptr, 0}, data{0, nullptr, 0}, bss{0, nullptr, 0}
60{
61}
62
63
64ObjectFile::~ObjectFile()
65{
66    close();
67}
68
69
70bool
71ObjectFile::loadSection(Section *sec, PortProxy& memProxy, Addr addrMask, Addr offset)
72{
73    if (sec->size != 0) {
74        Addr addr = (sec->baseAddr & addrMask) + offset;
75        if (sec->fileImage) {
76            memProxy.writeBlob(addr, sec->fileImage, sec->size);
77        }
78        else {
79            // no image: must be bss
80            memProxy.memsetBlob(addr, 0, sec->size);
81        }
82    }
83    return true;
84}
85
86
87bool
88ObjectFile::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset)
89{
90    return (loadSection(&text, memProxy, addrMask, offset)
91            && loadSection(&data, memProxy, addrMask, offset)
92            && loadSection(&bss, memProxy, addrMask, offset));
93}
94
95
96void
97ObjectFile::close()
98{
99    if (fileData) {
100        ::munmap((char*)fileData, len);
101        fileData = NULL;
102    }
103}
104
105static bool
106hasGzipMagic(int fd)
107{
108    uint8_t buf[2] = {0};
109    size_t sz = pread(fd, buf, 2, 0);
110    panic_if(sz != 2, "Couldn't read magic bytes from object file");
111    return ((buf[0] == 0x1f) && (buf[1] == 0x8b));
112}
113
114static int
115doGzipLoad(int fd)
116{
117    const size_t blk_sz = 4096;
118
119    gzFile fdz = gzdopen(fd, "rb");
120    if (!fdz) {
121        return -1;
122    }
123
124    size_t tmp_len = strlen(P_tmpdir);
125    char *tmpnam = (char*) malloc(tmp_len + 20);
126    strcpy(tmpnam, P_tmpdir);
127    strcpy(tmpnam+tmp_len, "/gem5-gz-obj-XXXXXX"); // 19 chars
128    fd = mkstemp(tmpnam); // repurposing fd variable for output
129    if (fd < 0) {
130        free(tmpnam);
131        gzclose(fdz);
132        return fd;
133    }
134
135    if (unlink(tmpnam) != 0)
136        warn("couldn't remove temporary file %s\n", tmpnam);
137
138    free(tmpnam);
139
140    auto buf = new uint8_t[blk_sz];
141    int r; // size of (r)emaining uncopied data in (buf)fer
142    while ((r = gzread(fdz, buf, blk_sz)) > 0) {
143        auto p = buf; // pointer into buffer
144        while (r > 0) {
145            auto sz = write(fd, p, r);
146            assert(sz <= r);
147            r -= sz;
148            p += sz;
149        }
150    }
151    delete[] buf;
152    gzclose(fdz);
153    if (r < 0) { // error
154        close(fd);
155        return -1;
156    }
157    assert(r == 0); // finished successfully
158    return fd; // return fd to decompressed temporary file for mmap()'ing
159}
160
161ObjectFile *
162createObjectFile(const string &fname, bool raw)
163{
164    // open the file
165    int fd = open(fname.c_str(), O_RDONLY);
166    if (fd < 0) {
167        return NULL;
168    }
169
170    // decompress GZ files
171    if (hasGzipMagic(fd)) {
172        fd = doGzipLoad(fd);
173        if (fd < 0) {
174            return NULL;
175        }
176    }
177
178    // find the length of the file by seeking to the end
179    off_t off = lseek(fd, 0, SEEK_END);
180    fatal_if(off < 0,
181             "Failed to determine size of object file %s\n", fname);
182    auto len = static_cast<size_t>(off);
183
184    // mmap the whole shebang
185    auto fileData = (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
186    close(fd);
187
188    if (fileData == MAP_FAILED) {
189        return NULL;
190    }
191
192    ObjectFile *fileObj = NULL;
193
194    // figure out what we have here
195    if ((fileObj = ElfObject::tryFile(fname, len, fileData)) != NULL) {
196        return fileObj;
197    }
198
199    if ((fileObj = EcoffObject::tryFile(fname, len, fileData)) != NULL) {
200        return fileObj;
201    }
202
203    if ((fileObj = AoutObject::tryFile(fname, len, fileData)) != NULL) {
204        return fileObj;
205    }
206
207    if ((fileObj = DtbObject::tryFile(fname, len, fileData)) != NULL) {
208        return fileObj;
209    }
210
211    if (raw)
212        return RawObject::tryFile(fname, len, fileData);
213
214    // don't know what it is
215    munmap((char*)fileData, len);
216    return NULL;
217}
218