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.
29 #include "base/loader/image_file_data.hh"
33 #include <sys/types.h>
40 #include "base/logging.hh"
49 size_t sz
= pread(fd
, buf
, 2, 0);
50 panic_if(sz
!= 2, "Couldn't read magic bytes from object file");
51 return ((buf
[0] == 0x1f) && (buf
[1] == 0x8b));
57 const size_t blk_sz
= 4096;
59 gzFile fdz
= gzdopen(fd
, "rb");
64 size_t tmp_len
= strlen(P_tmpdir
);
65 char *tmpnam
= (char*) malloc(tmp_len
+ 20);
66 strcpy(tmpnam
, P_tmpdir
);
67 strcpy(tmpnam
+tmp_len
, "/gem5-gz-obj-XXXXXX"); // 19 chars
68 fd
= mkstemp(tmpnam
); // repurposing fd variable for output
75 if (unlink(tmpnam
) != 0)
76 warn("couldn't remove temporary file %s\n", tmpnam
);
80 auto buf
= new uint8_t[blk_sz
];
81 int r
; // size of (r)emaining uncopied data in (buf)fer
82 while ((r
= gzread(fdz
, buf
, blk_sz
)) > 0) {
83 auto p
= buf
; // pointer into buffer
85 auto sz
= write(fd
, p
, r
);
97 assert(r
== 0); // finished successfully
98 return fd
; // return fd to decompressed temporary file for mmap()'ing
101 ImageFileData::ImageFileData(const std::string
&fname
)
106 int fd
= open(fname
.c_str(), O_RDONLY
);
107 panic_if(fd
< 0, "Failed to open file %s.\n", fname
);
109 // Decompress GZ files.
110 if (hasGzipMagic(fd
)) {
112 panic_if(fd
< 0, "Failed to unzip file %s.\n", fname
);
115 // Find the length of the file by seeking to the end.
116 off_t off
= lseek(fd
, 0, SEEK_END
);
117 fatal_if(off
< 0, "Failed to determine size of file %s.\n", fname
);
118 _len
= static_cast<size_t>(off
);
120 // Mmap the whole shebang.
121 _data
= (uint8_t *)mmap(NULL
, _len
, PROT_READ
, MAP_SHARED
, fd
, 0);
124 panic_if(_data
== MAP_FAILED
, "Failed to mmap file %s.\n", fname
);
127 ImageFileData::~ImageFileData()
129 munmap((void *)_data
, _len
);
132 } // namespace Loader