ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
Memory.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/*
8 * Taken from https://bitbucket.org/rafzi/hacklib
9 */
10
11#include "etiss/Memory.h"
12#include <stdexcept>
13
14#ifdef _WIN32
15
16#include <Windows.h>
17
18etiss::ModuleHandle etiss::GetModuleByName(const std::string &name)
19{
20 if (name == "")
21 {
22 return GetModuleHandleA(NULL);
23 }
24 else
25 {
26 return GetModuleHandleA(name.c_str());
27 }
28}
29
31{
33
34 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
35 (LPCTSTR)adr, &hModule) == 0)
36 {
37 if (GetLastError() != ERROR_MOD_NOT_FOUND)
38 {
39 throw std::runtime_error("GetModuleHandleEx failed");
40 }
41 }
42
43 return hModule;
44}
45
47{
48 char path[MAX_PATH];
49
50 if (GetModuleFileNameA(hModule, path, MAX_PATH) == 0)
51 {
52 throw std::runtime_error("GetModuleFileName failed");
53 }
54
55 return path;
56}
57#else
58
59#include <dlfcn.h>
60
62{
63 void *handle;
64 if (name == "")
65 {
66 handle = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
67 }
68 else
69 {
70 handle = dlopen(name.c_str(), RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
71 }
72
73 if (handle)
74 {
75 // This is undocumented, but works and is easy.
76 auto hModule = *(etiss::ModuleHandle *)handle;
77 if (!hModule)
78 {
79 hModule = (etiss::ModuleHandle)0x400000;
80 }
81 dlclose(handle);
82 return hModule;
83 }
84 else
85 {
87 }
88}
89
91{
92 Dl_info info = { 0 };
93 dladdr((void *)adr, &info);
94 return info.dli_fbase;
95}
96
98{
99 Dl_info info = { 0 };
100 if (dladdr((void *)hModule, &info) == 0)
101 {
102 throw std::runtime_error("dladdr failed");
103 }
104 return info.dli_fname;
105}
106
107#endif
108
110{
111 static etiss::ModuleHandle hModule = 0;
112
113 if (!hModule)
114 {
116 }
117
118 return hModule;
119}
121{
122 static std::string modulePath;
123
124 if (modulePath == "")
125 {
127 }
128
129 return modulePath;
130}
void * ModuleHandle
Represents a module handle. Analogous to Windows this represents the module base address,...
Definition Memory.h:37
etiss::ModuleHandle GetModuleByAddress(uintptr_t adr)
Definition Memory.cpp:90
std::string GetCurrentModulePath()
Returns the abolute path with filename to the own dynamic library.
Definition Memory.cpp:120
etiss::ModuleHandle GetModuleByName(const std::string &name="")
Definition Memory.cpp:61
static const ModuleHandle NullModuleHandle
Represents a null value for hl::ModuleHandle.
Definition Memory.h:40
std::string GetModulePath(etiss::ModuleHandle hModule)
Definition Memory.cpp:97
etiss::ModuleHandle GetCurrentModule()
Returns the module handle to the own dynamic library.
Definition Memory.cpp:109
#define NULL
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...