Deleted Added
sdiff udiff text old ( 11391:484c04261226 ) new ( 11793:ef606668d247 )
full compact
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 _op_sys)
57 : filename(_filename), fileData(_data), len(_len),
58 arch(_arch), opSys(_op_sys), 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 if (fileData) {
67 ::munmap((char*)fileData, len);
68 fileData = NULL;
69 }
70}
71
72
73bool
74ObjectFile::loadSection(Section *sec, PortProxy& mem_proxy, Addr addr_mask,
75 Addr offset)
76{
77 if (sec->size != 0) {
78 Addr addr = (sec->baseAddr & addr_mask) + offset;
79 if (sec->fileImage) {
80 mem_proxy.writeBlob(addr, sec->fileImage, sec->size);
81 }
82 else {
83 // no image: must be bss
84 mem_proxy.memsetBlob(addr, 0, sec->size);
85 }
86 }
87 return true;
88}
89
90
91bool
92ObjectFile::loadSections(PortProxy& mem_proxy, Addr addr_mask, Addr offset)
93{
94 return (loadSection(&text, mem_proxy, addr_mask, offset)
95 && loadSection(&data, mem_proxy, addr_mask, offset)
96 && loadSection(&bss, mem_proxy, addr_mask, offset));
97}
98
99static bool
100hasGzipMagic(int fd)
101{
102 uint8_t buf[2] = {0};
103 size_t sz = pread(fd, buf, 2, 0);
104 panic_if(sz != 2, "Couldn't read magic bytes from object file");
105 return ((buf[0] == 0x1f) && (buf[1] == 0x8b));
106}
107
108static int
109doGzipLoad(int fd)
110{
111 const size_t blk_sz = 4096;
112
113 gzFile fdz = gzdopen(fd, "rb");
114 if (!fdz) {
115 return -1;
116 }
117
118 size_t tmp_len = strlen(P_tmpdir);
119 char *tmpnam = (char*) malloc(tmp_len + 20);
120 strcpy(tmpnam, P_tmpdir);
121 strcpy(tmpnam+tmp_len, "/gem5-gz-obj-XXXXXX"); // 19 chars
122 fd = mkstemp(tmpnam); // repurposing fd variable for output
123 if (fd < 0) {
124 free(tmpnam);
125 gzclose(fdz);
126 return fd;
127 }
128
129 if (unlink(tmpnam) != 0)
130 warn("couldn't remove temporary file %s\n", tmpnam);
131
132 free(tmpnam);
133
134 auto buf = new uint8_t[blk_sz];
135 int r; // size of (r)emaining uncopied data in (buf)fer
136 while ((r = gzread(fdz, buf, blk_sz)) > 0) {
137 auto p = buf; // pointer into buffer
138 while (r > 0) {
139 auto sz = write(fd, p, r);
140 assert(sz <= r);
141 r -= sz;
142 p += sz;
143 }
144 }
145 delete[] buf;
146 gzclose(fdz);
147 if (r < 0) { // error
148 close(fd);
149 return -1;
150 }
151 assert(r == 0); // finished successfully
152 return fd; // return fd to decompressed temporary file for mmap()'ing
153}
154
155ObjectFile *
156createObjectFile(const string &fname, bool raw)
157{
158 // open the file
159 int fd = open(fname.c_str(), O_RDONLY);
160 if (fd < 0) {
161 return NULL;
162 }
163
164 // decompress GZ files
165 if (hasGzipMagic(fd)) {
166 fd = doGzipLoad(fd);
167 if (fd < 0) {
168 return NULL;
169 }
170 }
171
172 // find the length of the file by seeking to the end
173 off_t off = lseek(fd, 0, SEEK_END);
174 fatal_if(off < 0,
175 "Failed to determine size of object file %s\n", fname);
176 auto len = static_cast<size_t>(off);
177
178 // mmap the whole shebang
179 uint8_t *file_data = (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED,
180 fd, 0);
181 close(fd);
182
183 if (file_data == MAP_FAILED) {
184 return NULL;
185 }
186
187 ObjectFile *file_obj = NULL;
188
189 // figure out what we have here
190 if ((file_obj = ElfObject::tryFile(fname, len, file_data)) != NULL) {
191 return file_obj;
192 }
193
194 if ((file_obj = EcoffObject::tryFile(fname, len, file_data)) != NULL) {
195 return file_obj;
196 }
197
198 if ((file_obj = AoutObject::tryFile(fname, len, file_data)) != NULL) {
199 return file_obj;
200 }
201
202 if ((file_obj = DtbObject::tryFile(fname, len, file_data)) != NULL) {
203 return file_obj;
204 }
205
206 if (raw)
207 return RawObject::tryFile(fname, len, file_data);
208
209 // don't know what it is
210 munmap((char*)file_data, len);
211 return NULL;
212}