Improve unwind support on Windows x64.
[mono.git] / mono / mini / mini-windows-dlldac.c
1 #include <config.h>
2
3 #ifdef HOST_WIN32
4 #include <winsock2.h>
5 #include <windows.h>
6 #include <winnt.h>
7
8 #if defined(TARGET_AMD64) && !defined(DISABLE_JIT)
9 #include "mono/mini/mini.h"
10 #include "mono/mini/mini-amd64.h"
11 #include "mono/utils/mono-publib.h"
12
13 typedef enum _FUNCTION_TABLE_TYPE {
14     RF_SORTED,
15     RF_UNSORTED,
16     RF_CALLBACK
17 } FUNCTION_TABLE_TYPE;
18
19 typedef struct _DYNAMIC_FUNCTION_TABLE {
20     LIST_ENTRY Links;
21     PRUNTIME_FUNCTION FunctionTable;
22     LARGE_INTEGER TimeStamp;
23     ULONG64 MinimumAddress;
24     ULONG64 MaximumAddress;
25     ULONG64 BaseAddress;
26     PGET_RUNTIME_FUNCTION_CALLBACK Callback;
27     PVOID Context;
28     PWSTR OutOfProcessCallbackDll;
29     FUNCTION_TABLE_TYPE Type;
30     ULONG EntryCount;
31 } DYNAMIC_FUNCTION_TABLE, *PDYNAMIC_FUNCTION_TABLE;
32
33 typedef BOOL (ReadMemoryFunction)(PVOID user_context, LPCVOID base_address, PVOID buffer, SIZE_T size, SIZE_T *read);
34 BOOL read_memory(PVOID user_context, LPCVOID base_address, PVOID buffer, SIZE_T size, SIZE_T* read)
35 {
36     return ReadProcessMemory((HANDLE)user_context, base_address, buffer, size, read);
37 }
38
39 MONO_API_EXPORT DWORD OutOfProcessFunctionTableCallbackEx (ReadMemoryFunction read_memory, PVOID user_context, PVOID table_address, PDWORD entries, PRUNTIME_FUNCTION *functions)
40 {
41         DYNAMIC_FUNCTION_TABLE func_table = { 0 };
42         DynamicFunctionTableEntry func_table_entry = { 0 };
43         PRUNTIME_FUNCTION rt_funcs = NULL;
44         size_t reads = 0;
45         DWORD result = 0xC0000001;
46
47         if (read_memory (user_context, table_address, &func_table, sizeof (func_table), &reads)) {
48                 if (func_table.Context != NULL && read_memory (user_context, func_table.Context, &func_table_entry, sizeof (func_table_entry), &reads)) {
49                         if (func_table_entry.rt_funcs_current_count != 0) {
50                                 rt_funcs = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, func_table_entry.rt_funcs_current_count * sizeof (RUNTIME_FUNCTION));
51                                 if (rt_funcs) {
52                                         if (read_memory (user_context, func_table_entry.rt_funcs, rt_funcs, func_table_entry.rt_funcs_current_count * sizeof (RUNTIME_FUNCTION), &reads)) {
53                                                 *entries = func_table_entry.rt_funcs_current_count;
54                                                 *functions = rt_funcs;
55                                                 result = 0x00000000;
56                                         }
57                                 }
58                         }
59                 }
60         }
61
62         return result;
63 }
64
65 MONO_API_EXPORT DWORD OutOfProcessFunctionTableCallback (HANDLE process, PVOID table_address, PDWORD entries, PRUNTIME_FUNCTION *functions)
66 {
67         return OutOfProcessFunctionTableCallbackEx (&read_memory, process, table_address, entries, functions);
68 }
69 #endif /* defined(TARGET_AMD64) && !defined(DISABLE_JIT) */
70
71 #ifdef _MSC_VER
72 BOOL APIENTRY DllMain (HMODULE module_handle, DWORD reason, LPVOID reserved)
73 {
74         return TRUE;
75 }
76 #endif
77
78 #else
79
80 MONO_EMPTY_SOURCE_FILE (mini_windows_dlldac);
81 #endif /* HOST_WIN32 */