ReXGlue SDK
Loading...
Searching...
No Matches
module_object.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#include <memory>
17#include <string>
18#include <string_view>
19#include <unordered_map>
20#include <vector>
21
23#include <rex/platform.h>
24#include <rex/assert.h>
26#include <rex/platform/thread.h>
27#include <rex/utility.h>
28#include <rex/platform/timer.h>
29#include <rex/kernel/export.h>
30#include <rex/kernel/module.h>
31#include <rex/kernel/types/io.h>
34#include <rex/types.h>
35
36namespace rex {
37namespace kernel {
38class TitleRuntime;
39} // namespace kernel
40} // namespace rex
41
42namespace rex::kernel {
43
45
48 X_LIST_ENTRY in_load_order_links;
53
54 be_u32 dll_base;
55 be_u32 image_base;
56 be_u32 image_size;
57
58 X_UNICODE_STRING full_dll_name;
59 X_UNICODE_STRING base_dll_name;
60
61 be_u32 flags;
63 be_u32 entry_point;
64 be_u16 load_count;
65 be_u16 module_index;
66 be_u32 dll_base_original;
67 be_u32 checksum;
68 be_u32 load_flags;
69 be_u32 time_date_stamp;
70 be_u32 loaded_imports;
72 be_u32 closure_root;
73 be_u32 traversal_parent;
74};
75REX_STATIC_ASSERT_SIZE(X_LDR_DATA_TABLE_ENTRY, 0x64);
76
78enum class ModuleType : u8 { kKernel, kUser };
79
80enum class ModuleFormat : u8 { kUndefined, kXEX, kELF };
81
84 struct ThunkKey {
87 u16 ordinal;
88 bool operator==(const ThunkKey&) const = default;
89 };
90
91 struct ThunkKeyHash {
92 REX_KERNEL_API size_t operator()(const ThunkKey& key) const noexcept;
93 };
94
96 std::unordered_map<ThunkKey, u32, ThunkKeyHash> thunk_cache;
97
102 std::unique_ptr<rex::platform::HighResolutionTimer> timestamp_timer;
103};
104
108
109 ModuleFormat module_format = ModuleFormat::kUndefined;
110
112 bool is_dll_module = false;
113
115 u32 entry_point = 0;
116 u32 stack_size = 0;
117
119 std::string recompiled_key;
120
122 std::vector<X_HANDLE> dependencies;
123
125 std::unordered_map<KernelModuleState::ThunkKey, u32, KernelModuleState::ThunkKeyHash> thunk_cache;
126};
127
130 public:
131 virtual ~ILoadedModule() = default;
132
136 [[nodiscard]] virtual const std::string& GetPath() const = 0;
137
145 virtual X_STATUS GetSection(std::string_view section_name, u32* out_section_data,
146 u32* out_section_size) const = 0;
147
149 [[nodiscard]] virtual u32 GetTitleID() const = 0;
150
156 [[nodiscard]] virtual std::vector<u32> GetAlternateTitleIDs() const = 0;
157
164 virtual X_STATUS GetOptHeader(XEXHeaderKey key, void** out_ptr) = 0;
165
173 template <typename T>
174 X_STATUS OptHeader(XEXHeaderKey key, T* out_ptr) {
175 return GetOptHeader(key, reinterpret_cast<void**>(out_ptr));
176 }
177
186 virtual X_STATUS GetOptHeaderGuestAddress(XEXHeaderKey key, u32* out_guest_address) const = 0;
187};
188
196
199
201 std::string name;
202 std::string path;
205
207 u32 hmodule_ptr = 0;
211
218 REX_KERNEL_API bool IsNamed(std::string_view query) const;
219
220 REX_KERNEL_API X_STATUS GetSection(std::string_view section_name, u32* out_section_data,
221 u32* out_section_size) const override;
222
229 REX_KERNEL_API u32 GetExportAddress(TitleRuntime& owning_runtime, u16 ordinal,
230 u32 caller_address = 0);
231
237 REX_KERNEL_API u32 GetExportAddress(std::string_view export_name) const;
238
241
243
244 REX_KERNEL_API u32 GetTitleID() const override;
245
246 REX_KERNEL_API std::vector<u32> GetAlternateTitleIDs() const override;
247
248 const std::string& GetPath() const override { return path; }
249
255 REX_KERNEL_API X_STATUS LoadFromFile(TitleRuntime& owning_runtime, std::string_view guest_path);
256
263 REX_KERNEL_API X_STATUS LoadFromMemory(TitleRuntime& owning_runtime, const void* addr,
264 size_t length);
265
272
273 REX_KERNEL_API X_STATUS GetOptHeader(XEXHeaderKey key, void** out_ptr) override;
274
282 template <typename T>
283 X_STATUS OptHeader(XEXHeaderKey key, T* out_ptr) {
284 return GetOptHeader(key, reinterpret_cast<void**>(out_ptr));
285 }
286
288 u32* out_guest_address) const override;
289
296
298 void SetVariable(u16 ordinal, u32 address) { kernel.variables.SetAddress(ordinal, address); }
299
301 u32 GetVariable(u16 ordinal) const { return kernel.variables.GetAddress(ordinal); }
302
308 REX_KERNEL_API static u32 HandleFromHModule(void* hmodule);
309
316 REX_KERNEL_API static X_HANDLE FromHModule(TitleRuntime& runtime, void* hmodule);
317
324 REX_KERNEL_API static X_HANDLE CreateKernelModule(TitleRuntime& runtime, std::string_view path);
325
332
342 const XEXHeader* header, XEXHeaderKey key,
343 u32* out_header_guest_ptr);
344};
345
353
354} // namespace rex::kernel
Definition utility.h:37
Definition utility.h:264
Definition export.h:44
void SetAddress(u16 ordinal, u32 address)
Definition export.h:47
u32 GetAddress(u16 ordinal) const
Definition export.h:50
Definition heap.h:594
Definition module_object.h:129
X_STATUS OptHeader(XEXHeaderKey key, T *out_ptr)
Definition module_object.h:174
virtual X_STATUS GetOptHeaderGuestAddress(XEXHeaderKey key, u32 *out_guest_address) const =0
virtual X_STATUS GetOptHeader(XEXHeaderKey key, void **out_ptr)=0
virtual std::vector< u32 > GetAlternateTitleIDs() const =0
virtual u32 GetTitleID() const =0
virtual const std::string & GetPath() const =0
virtual X_STATUS GetSection(std::string_view section_name, u32 *out_section_data, u32 *out_section_size) const =0
Definition title_runtime.h:85
Definition module.h:177
Guest list and spin lock structures.
Definition export.h:24
u32 X_STATUS
Definition status.h:23
X_HANDLE CreateModule(TitleRuntime &runtime, ModuleType type)
ModuleType
Definition module_object.h:78
XEXHeaderKey
Definition xex.h:270
Definition endian.h:27
Definition app.h:36
Platform flags, packing macros and export markers.
#define REX_KERNEL_API
Definition platform.h:297
Definition module_object.h:84
u32 caller_module_base
Definition module_object.h:86
Definition module_object.h:82
std::unordered_map< ThunkKey, u32, ThunkKeyHash > thunk_cache
Definition module_object.h:96
rex::kernel::ExportVariableTable variables
Definition module_object.h:99
std::unique_ptr< rex::platform::HighResolutionTimer > timestamp_timer
Definition module_object.h:102
X_STATUS GetSection(std::string_view section_name, u32 *out_section_data, u32 *out_section_size) const override
u32 GetExportAddress(std::string_view export_name) const
ModuleType type
Definition module_object.h:200
static X_STATUS GetOptHeader(const kernel::GuestAddressSpace *memory, const XEXHeader *header, XEXHeaderKey key, u32 *out_header_guest_ptr)
X_STATUS LoadFromFile(TitleRuntime &owning_runtime, std::string_view guest_path)
KernelModuleState kernel
Definition module_object.h:210
u32 GetVariable(u16 ordinal) const
Definition module_object.h:301
X_STATUS GetOptHeader(XEXHeaderKey key, void **out_ptr) override
static X_HANDLE CreateKernelModule(TitleRuntime &runtime, std::string_view path)
u32 GetTitleID() const override
X_STATUS OptHeader(XEXHeaderKey key, T *out_ptr)
Definition module_object.h:283
bool IsNamed(std::string_view query) const
rex::kernel::XEXModule * GetXEX()
void SetVariable(u16 ordinal, u32 address)
Definition module_object.h:298
const std::string & GetPath() const override
Definition module_object.h:248
PropagateConst< std::unique_ptr< rex::kernel::GuestModule > > processor_module
Definition module_object.h:204
u32 GetExportAddress(TitleRuntime &owning_runtime, u16 ordinal, u32 caller_address=0)
X_STATUS LoadFromMemory(TitleRuntime &owning_runtime, const void *addr, size_t length)
static X_HANDLE FromHModule(TitleRuntime &runtime, void *hmodule)
std::vector< u32 > GetAlternateTitleIDs() const override
void InvalidateThunkCache(u32 lo, u32 hi)
static u32 HandleFromHModule(void *hmodule)
static X_HANDLE CreateUserModule(TitleRuntime &runtime)
std::string path
Definition module_object.h:202
ModuleObject(TitleRuntime *runtime_in, ModuleType type_in)
TitleRuntime * runtime
Definition module_object.h:208
u32 hmodule_ptr
Definition module_object.h:207
std::string name
Definition module_object.h:201
X_STATUS GetOptHeaderGuestAddress(XEXHeaderKey key, u32 *out_guest_address) const override
UserModuleState user
Definition module_object.h:209
Definition module_object.h:105
u32 stack_size
Definition module_object.h:116
std::unordered_map< KernelModuleState::ThunkKey, u32, KernelModuleState::ThunkKeyHash > thunk_cache
Definition module_object.h:125
std::string recompiled_key
Definition module_object.h:119
bool is_dll_module
Definition module_object.h:112
u32 entry_point
Definition module_object.h:115
u32 guest_xex_header
Definition module_object.h:107
std::vector< X_HANDLE > dependencies
Definition module_object.h:122
Definition xex.h:583
Definition module_object.h:47
be_u32 dll_base
Definition module_object.h:54
be_u16 load_count
Definition module_object.h:64
be_u32 xex_header_base
Definition module_object.h:71
X_LIST_ENTRY in_memory_order_links
Definition module_object.h:50
be_u32 full_image_size
Definition module_object.h:62
be_u32 checksum
Definition module_object.h:67
X_LIST_ENTRY in_initialization_order_links
Definition module_object.h:52
Definition list.h:24
Integer types, byte order wrappers and size literals.
Move only function, const propagating pointer and scope guard.