33 #include <sys/types.h>
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");
75 if (unlink(tmpnam) != 0)
76 warn(
"couldn't remove temporary file %s\n", tmpnam);
80 auto buf =
new uint8_t[blk_sz];
82 while ((
r = gzread(fdz, buf, blk_sz)) > 0) {
85 auto sz = write(
fd,
p,
r);
106 int fd = open(fname.c_str(), O_RDONLY);
107 panic_if(
fd < 0,
"Failed to open file %s.\n", fname);
112 panic_if(
fd < 0,
"Failed to unzip file %s.\n", fname);
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);
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);