2 * Copyright (c) 2002-2004 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
32 #include "base/loader/object_file.hh"
36 #include <sys/types.h>
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"
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)
64 ObjectFile::~ObjectFile()
67 ::munmap((char*)fileData
, len
);
76 typedef std::vector
<ObjectFile::Loader
*> LoaderList
;
81 static LoaderList loaders
;
85 } // anonymous namespace
87 ObjectFile::Loader::Loader()
89 object_file_loaders().emplace_back(this);
93 ObjectFile::tryLoaders(ProcessParams
*params
, ObjectFile
*obj_file
)
95 for (auto &loader
: object_file_loaders()) {
96 Process
*p
= loader
->load(params
, obj_file
);
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));
116 const size_t blk_sz
= 4096;
118 gzFile fdz
= gzdopen(fd
, "rb");
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
134 if (unlink(tmpnam
) != 0)
135 warn("couldn't remove temporary file %s\n", tmpnam
);
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
144 auto sz
= write(fd
, p
, r
);
152 if (r
< 0) { // error
156 assert(r
== 0); // finished successfully
157 return fd
; // return fd to decompressed temporary file for mmap()'ing
161 createObjectFile(const string
&fname
, bool raw
)
164 int fd
= open(fname
.c_str(), O_RDONLY
);
169 // decompress GZ files
170 if (hasGzipMagic(fd
)) {
177 // find the length of the file by seeking to the end
178 off_t off
= lseek(fd
, 0, SEEK_END
);
180 "Failed to determine size of object file %s\n", fname
);
181 auto len
= static_cast<size_t>(off
);
183 // mmap the whole shebang
184 uint8_t *file_data
= (uint8_t *)mmap(NULL
, len
, PROT_READ
, MAP_SHARED
,
188 if (file_data
== MAP_FAILED
) {
192 ObjectFile
*file_obj
= NULL
;
194 // figure out what we have here
195 if ((file_obj
= ElfObject::tryFile(fname
, len
, file_data
)) != NULL
) {
199 if ((file_obj
= EcoffObject::tryFile(fname
, len
, file_data
)) != NULL
) {
203 if ((file_obj
= AoutObject::tryFile(fname
, len
, file_data
)) != NULL
) {
207 if ((file_obj
= DtbObject::tryFile(fname
, len
, file_data
)) != NULL
) {
212 return RawObject::tryFile(fname
, len
, file_data
);
214 // don't know what it is
215 munmap((char*)file_data
, len
);