-
Notifications
You must be signed in to change notification settings - Fork 17
/
mgzd.h
100 lines (82 loc) · 2.81 KB
/
mgzd.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef __MGZD_H
#define __MGZD_H
/*****************************************************************************\
* Qemu Simulation Framework (qsim) *
* Qsim is a modified version of the Qemu emulator (www.qemu.org), coupled *
* a C++ API, for the use of computer architecture researchers. *
* *
* This work is licensed under the terms of the GNU GPL, version 2. See the *
* COPYING file in the top-level directory. *
\*****************************************************************************/
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char* TMP_DIR = "/tmp/";
static const char* TMP_PFX = "qsim_XXXXXX";
namespace Mgzd {
struct lib_t {
void* handle;
std::string file;
};
static lib_t __attribute__((unused)) open(const char *libfile) {
lib_t lib;
// Use $QSIM_TMP, if it's set.
const char* tmpdir = getenv("QSIM_TMP");
if (tmpdir) TMP_DIR = tmpdir;
const int buf_size = 1024; // 1KB
char tmpfile[buf_size], buf[buf_size];
size_t size = strlen(TMP_DIR);
strncpy(tmpfile, TMP_DIR, size);
tmpfile[size] = '\0';
strcat(tmpfile, TMP_PFX);
int fd = mkstemp(tmpfile);
FILE* fp = fdopen(fd, "wb");
if (!fp) {
std::cerr << "Cannot open tmp file " << tmpfile << std::endl;
exit(1);
}
FILE* libfp = fopen(libfile, "r");
if (!libfp) {
std::cerr << "Cannot open library " << libfile << std::endl;
exit(1);
}
while ((size = fread(buf, 1, buf_size, libfp)) > 0) {
if (fwrite(buf, 1, size, fp) != size) {
std::cerr << "couldn't write whole buffer" << std::endl;
exit(1);
}
}
fclose(fp);
// Make temporary copy of libfile, so opening multiple copies of the same
// file results in independent copies of global variables.
lib.file = strdup(tmpfile);
std::cout << "Opening " << lib.file.c_str() << std::endl;
lib.handle = dlopen(lib.file.c_str(), RTLD_NOW|RTLD_LOCAL);
if (lib.handle == NULL) {
std::cerr << "dlopen(\"" << lib.file.c_str() << "\") failed: "
<< dlerror() << '\n';
}
return lib;
}
static void __attribute__((unused)) close(const lib_t &lib) {
//dlclose(lib.handle);
unlink(lib.file.c_str());
}
template <typename T> static void sym(T *&ret,
const lib_t lib,
const char *sym) {
(void*&)ret = dlsym(lib.handle, sym);
if (char *err = dlerror()) {
std::cerr << "dlsym(\"" << lib.file.c_str() << "\", \"" << sym
<< "\") failed: " << err << '\n';
exit(1);
}
}
};
#endif