arch,base: Separate the idea of a memory image and object file.
[gem5.git] / src / base / loader / object_file.cc
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>
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
54 using namespace std;
55
56 ObjectFile::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)
61 {}
62
63
64 ObjectFile::~ObjectFile()
65 {
66 if (fileData) {
67 ::munmap((char*)fileData, len);
68 fileData = NULL;
69 }
70 }
71
72
73 namespace
74 {
75
76 typedef std::vector<ObjectFile::Loader *> LoaderList;
77
78 LoaderList &
79 object_file_loaders()
80 {
81 static LoaderList loaders;
82 return loaders;
83 }
84
85 } // anonymous namespace
86
87 ObjectFile::Loader::Loader()
88 {
89 object_file_loaders().emplace_back(this);
90 }
91
92 Process *
93 ObjectFile::tryLoaders(ProcessParams *params, ObjectFile *obj_file)
94 {
95 for (auto &loader: object_file_loaders()) {
96 Process *p = loader->load(params, obj_file);
97 if (p)
98 return p;
99 }
100
101 return nullptr;
102 }
103
104 static bool
105 hasGzipMagic(int fd)
106 {
107 uint8_t buf[2] = {0};
108 size_t sz = pread(fd, buf, 2, 0);
109 panic_if(sz != 2, "Couldn't read magic bytes from object file");
110 return ((buf[0] == 0x1f) && (buf[1] == 0x8b));
111 }
112
113 static int
114 doGzipLoad(int fd)
115 {
116 const size_t blk_sz = 4096;
117
118 gzFile fdz = gzdopen(fd, "rb");
119 if (!fdz) {
120 return -1;
121 }
122
123 size_t tmp_len = strlen(P_tmpdir);
124 char *tmpnam = (char*) malloc(tmp_len + 20);
125 strcpy(tmpnam, P_tmpdir);
126 strcpy(tmpnam+tmp_len, "/gem5-gz-obj-XXXXXX"); // 19 chars
127 fd = mkstemp(tmpnam); // repurposing fd variable for output
128 if (fd < 0) {
129 free(tmpnam);
130 gzclose(fdz);
131 return fd;
132 }
133
134 if (unlink(tmpnam) != 0)
135 warn("couldn't remove temporary file %s\n", tmpnam);
136
137 free(tmpnam);
138
139 auto buf = new uint8_t[blk_sz];
140 int r; // size of (r)emaining uncopied data in (buf)fer
141 while ((r = gzread(fdz, buf, blk_sz)) > 0) {
142 auto p = buf; // pointer into buffer
143 while (r > 0) {
144 auto sz = write(fd, p, r);
145 assert(sz <= r);
146 r -= sz;
147 p += sz;
148 }
149 }
150 delete[] buf;
151 gzclose(fdz);
152 if (r < 0) { // error
153 close(fd);
154 return -1;
155 }
156 assert(r == 0); // finished successfully
157 return fd; // return fd to decompressed temporary file for mmap()'ing
158 }
159
160 ObjectFile *
161 createObjectFile(const string &fname, bool raw)
162 {
163 // open the file
164 int fd = open(fname.c_str(), O_RDONLY);
165 if (fd < 0) {
166 return NULL;
167 }
168
169 // decompress GZ files
170 if (hasGzipMagic(fd)) {
171 fd = doGzipLoad(fd);
172 if (fd < 0) {
173 return NULL;
174 }
175 }
176
177 // find the length of the file by seeking to the end
178 off_t off = lseek(fd, 0, SEEK_END);
179 fatal_if(off < 0,
180 "Failed to determine size of object file %s\n", fname);
181 auto len = static_cast<size_t>(off);
182
183 // mmap the whole shebang
184 uint8_t *file_data = (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED,
185 fd, 0);
186 close(fd);
187
188 if (file_data == MAP_FAILED) {
189 return NULL;
190 }
191
192 ObjectFile *file_obj = NULL;
193
194 // figure out what we have here
195 if ((file_obj = ElfObject::tryFile(fname, len, file_data)) != NULL) {
196 return file_obj;
197 }
198
199 if ((file_obj = EcoffObject::tryFile(fname, len, file_data)) != NULL) {
200 return file_obj;
201 }
202
203 if ((file_obj = AoutObject::tryFile(fname, len, file_data)) != NULL) {
204 return file_obj;
205 }
206
207 if ((file_obj = DtbObject::tryFile(fname, len, file_data)) != NULL) {
208 return file_obj;
209 }
210
211 if (raw)
212 return RawObject::tryFile(fname, len, file_data);
213
214 // don't know what it is
215 munmap((char*)file_data, len);
216 return NULL;
217 }