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