ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
TCCJIT.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// This file is part of ETISS. It is licensed under the BSD 3-Clause License; you may not use this file except in
4// compliance with the License. You should have received a copy of the license along with this project. If not, see the
5// LICENSE file.
6
7#include "TCCJIT.h"
8#include "etiss/Misc.h"
9#include "etiss/config.h"
10
11#include "libtcc.h"
12#if ETISS_USE_GETPROC
13#include "etiss/ETISS.h"
14#include <windows.h>
15#include <dbghelp.h>
16#include <iostream>
17
18BOOL CALLBACK etiss_tcc_allsym(PSYMBOL_INFO pSymInfo, ULONG SymbolSize, PVOID UserContext)
19{
20 std::map<std::string, void *> &map = *(std::map<std::string, void *> *)UserContext;
22 map.insert(std::pair<std::string, void *>(pSymInfo->Name, (void *)pSymInfo->Address));
23 std::cout << "Found Symbol: " << pSymInfo->Name << "@0x" << std::hex << (void *)pSymInfo->Address << std::endl;
24 return TRUE;
25}
26
27static void addAllSymbols(std::map<std::string, void *> &ptrs)
28{
29
30 HANDLE hProcess = GetCurrentProcess();
31 // DWORD64 BaseOfDll;
32 BOOL status;
33
34 status = SymInitialize(hProcess, NULL, TRUE);
35 if (status == FALSE)
36 {
37 return;
38 }
39
40 auto libs = etiss::listLibraryPrefixes();
41 for (auto lib : libs)
42 {
43 std::string filter;
44 filter = std::string("*!") + lib + "_*";
45 std::cout << "searching in library " << lib << std::endl;
46 if (SymEnumSymbols(hProcess, // Process handle from SymInitialize.
47 NULL, // Base address of module.
48 filter.c_str(), // Name of symbols to match.
49 etiss_tcc_allsym, // Symbol handler procedure.
50 (PVOID)&ptrs)) // User context.
51 {
52 // SymEnumSymbols succeeded
53 }
54 else
55 {
56 std::cout << "Failed to iterate over all symbols for library " << lib << ":" << GetLastError() << std::endl;
57 }
58 }
59
60 SymCleanup(hProcess);
61}
62
63#endif
64
65TCCJIT::TCCJIT() : JIT("tcc")
66{
67#if ETISS_USE_GETPROC
68 addAllSymbols(extsymbols);
69#endif
70}
71
72void *TCCJIT::translate(std::string code, std::set<std::string> headerpaths, std::set<std::string> librarypaths,
73 std::set<std::string> libraries, std::string &error, bool debug)
74{
75
76 TCCState *s = tcc_new();
77 if (!s)
78 {
79 error = "Could not create tcc state";
80 return 0;
81 }
82
83 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
84 tcc_set_lib_path(s, (etiss::jitFiles() + "/tcc").c_str());
85
86 // init
87 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
88
89 // init headers
90 for (auto iter = headerpaths.begin(); iter != headerpaths.end(); iter++)
91 {
92 tcc_add_include_path(s, iter->c_str());
93 }
94 tcc_add_include_path(s, (etiss::jitFiles() + "/tcc_stdlib").c_str());
95
96 if (tcc_compile_string(s, code.c_str()) == -1)
97 { // compile
98 error = "Failed to compile code: \n";
99 error += code;
100 return 0;
101 }
102
103#if ETISS_USE_GETPROC
104 for (auto sym : extsymbols)
105 {
106 tcc_add_symbol(s, sym.first.c_str(), sym.second);
107 }
108 for (const auto &jitPath : etiss::jitExtLibPaths())
109 {
111 }
112#endif
113
114 // init libs
115 for (const auto &libpath : librarypaths)
116 {
117 if (tcc_add_library_path(s, libpath.c_str()))
118 {
119 error += "could not add library path: " + libpath;
120 return 0;
121 }
122 }
123 for (const auto &lib : libraries)
124 {
125 if (tcc_add_library(s, lib.c_str()))
126 {
127 error += "could not add library: " + lib;
128 return 0;
129 }
130 }
131
132 /* relocate the code */
133 if (tcc_relocate(s) < 0)
134 { // link
135 error = "Failed to link";
136 return 0;
137 }
138
139 /* return TCCState as handle*/
140 return (void *)s;
141}
142void *TCCJIT::getFunction(void *handle, std::string name, std::string &error)
143{
144
145 return tcc_get_symbol((TCCState *)handle, name.c_str());
146}
147void TCCJIT::free(void *handle)
148{
149 tcc_delete((TCCState *)handle);
150}
Header file of the ETISS library.
general configuration and logging
__device__ __2f16 float bool s
std::map< std::string, void * > extsymbols
only use for windows.
Definition TCCJIT.h:20
virtual void free(void *handle)
clean up handled returned by etiss::JIT::translate
Definition TCCJIT.cpp:147
virtual void * translate(std::string code, std::set< std::string > headerpaths, std::set< std::string > librarypaths, std::set< std::string > libraries, std::string &error, bool debug=false)
translate C code to executable code and return a handle/pointer that identifies the compilation resul...
Definition TCCJIT.cpp:72
virtual void * getFunction(void *handle, std::string name, std::string &error)
returns a function pointer to a compiled function from the handle returned by etiss::JIT::translate
Definition TCCJIT.cpp:142
TCCJIT()
Definition TCCJIT.cpp:65
static void addSearchPath(const std::string &path)
contains defines to configure ETISS.
std::set< std::string > listLibraryPrefixes()
Create a set with strings of the library names.
Definition ETISS.cpp:315
std::string jitFiles()
Get ETISS JIT files path.
Definition Misc.cpp:563
std::vector< std::string > jitExtLibPaths()
Get ETISS JIT external path.
Definition Misc.cpp:592
#define NULL