gem5 v24.0.0.0
Loading...
Searching...
No Matches
image_file_data.cc
Go to the documentation of this file.
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
30
31#include <fcntl.h>
32#include <sys/mman.h>
33#include <sys/types.h>
34#include <unistd.h>
35#include <zlib.h>
36
37#include <cstdio>
38#include <vector>
39
40#include "base/logging.hh"
41
42namespace gem5
43{
44
45namespace loader
46{
47
48static bool
50{
51 uint8_t buf[2] = {0};
52 size_t sz = pread(fd, buf, 2, 0);
53 panic_if(sz != 2, "Couldn't read magic bytes from object file");
54 return ((buf[0] == 0x1f) && (buf[1] == 0x8b));
55}
56
57static int
59{
60 const size_t blk_sz = 4096;
61
62 gzFile fdz = gzdopen(fd, "rb");
63 if (!fdz) {
64 return -1;
65 }
66
67 std::string tmpnam_str = std::string(P_tmpdir) + "/gem5-gz-obj-XXXXXX";
68 char *tmpnam = const_cast<char*>(tmpnam_str.c_str());
69 fd = mkstemp(tmpnam); // repurposing fd variable for output
70 if (fd < 0) {
71 gzclose(fdz);
72 return fd;
73 }
74
75 if (unlink(tmpnam) != 0)
76 warn("couldn't remove temporary file %s\n", tmpnam);
77
78 auto buf = new uint8_t[blk_sz];
79 int r; // size of (r)emaining uncopied data in (buf)fer
80 while ((r = gzread(fdz, buf, blk_sz)) > 0) {
81 auto p = buf; // pointer into buffer
82 while (r > 0) {
83 auto sz = write(fd, p, r);
84 assert(sz <= r);
85 r -= sz;
86 p += sz;
87 }
88 }
89 delete[] buf;
90 gzclose(fdz);
91 if (r < 0) { // error
92 close(fd);
93 return -1;
94 }
95 assert(r == 0); // finished successfully
96 return fd; // return fd to decompressed temporary file for mmap()'ing
97}
98
99ImageFileData::ImageFileData(const std::string &fname)
100{
101 _filename = fname;
102
103 // Open the file.
104 int fd = open(fname.c_str(), O_RDONLY);
105 fatal_if(fd < 0, "Failed to open file %s.\n"
106 "This error typically occurs when the file path specified is "
107 "incorrect.\n", fname);
108
109 // Decompress GZ files.
110 if (hasGzipMagic(fd)) {
111 fd = doGzipLoad(fd);
112 panic_if(fd < 0, "Failed to unzip file %s.\n", fname);
113 }
114
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);
119
120 // Mmap the whole shebang.
121 _data = (uint8_t *)mmap(NULL, _len, PROT_READ, MAP_SHARED, fd, 0);
122 close(fd);
123
124 panic_if(_data == MAP_FAILED, "Failed to mmap file %s.\n", fname);
125}
126
128{
129 munmap((void *)_data, _len);
130}
131
132} // namespace loader
133} // namespace gem5
ImageFileData(const std::string &f_name)
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
#define warn(...)
Definition logging.hh:256
Bitfield< 14, 12 > fd
Definition types.hh:150
Bitfield< 0 > p
static bool hasGzipMagic(int fd)
static int doGzipLoad(int fd)
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0