-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.mm
272 lines (227 loc) · 7.67 KB
/
main.mm
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdint.h>
#include <stdio.h>
#include <string.h>
extern "C" const char *xamarin_icu_dat_file_name;
static void xamarin_initialize_dotnet ()
{
xamarin_icu_dat_file_name = "";
}
extern "C" void xamarin_initialize_dotnet();
// extern "C" void xamarin_create_classes_Microsoft_macOS();
static void xamarin_invoke_registration_methods ()
{
xamarin_initialize_dotnet();
// xamarin_create_classes_Microsoft_macOS();
}
#include "xamarin/xamarin.h"
void xamarin_register_modules_impl ()
{
}
void xamarin_register_assemblies_impl ()
{
}
static const char *xamarin_runtime_libraries_array[] = {
// "libSystem.Globalization.Native",
// "libSystem.IO.Compression.Native",
// "libSystem.Native",
// "libSystem.Net.Security.Native",
// "libSystem.Security.Cryptography.Native.Apple",
// "libSystem.Security.Cryptography.Native.OpenSsl",
// "libclrgc",
// "libclrjit",
// "libcoreclr",
// "libhostfxr",
// "libhostpolicy",
// "libmscordaccore",
// "libmscordbi",
NULL
};
void xamarin_setup_impl ()
{
xamarin_invoke_registration_methods ();
xamarin_libmono_native_link_mode = XamarinNativeLinkModeDynamicLibrary;
xamarin_runtime_libraries = xamarin_runtime_libraries_array;
// xamarin_gc_pump = FALSE;
xamarin_init_mono_debug = TRUE;
xamarin_executable_name = "second_macos_test.dll";
xamarin_log_level = 0;
xamarin_arch_name = "arm64";
xamarin_marshal_objectivec_exception_mode = MarshalObjectiveCExceptionModeThrowManagedException;
xamarin_debug_mode = TRUE;
setenv ("MONO_GC_PARAMS", "major=marksweep", 1);
xamarin_supports_dynamic_registration = TRUE;
xamarin_runtime_configuration_name = "runtimeconfig.bin";
}
int main (int argc, char **argv)
{
puts("austin's custom main");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int rv = xamarin_main (argc, argv, XamarinLaunchModeApp);
[pool drain];
return rv;
}
void xamarin_initialize_callbacks () __attribute__ ((constructor));
void xamarin_initialize_callbacks ()
{
xamarin_setup = xamarin_setup_impl;
xamarin_register_assemblies = xamarin_register_assemblies_impl;
xamarin_register_modules = xamarin_register_modules_impl;
}
// ----------------------------------------- NativeAOT bootstrap
extern void * __modules_a[] __asm("section$start$__DATA$__modules");
extern void * __modules_z[] __asm("section$end$__DATA$__modules");
extern char __managedcode_a __asm("section$start$__TEXT$__managedcode");
extern char __managedcode_z __asm("section$end$__TEXT$__managedcode");
extern char __unbox_a __asm("section$start$__TEXT$__unbox");
extern char __unbox_z __asm("section$end$__TEXT$__unbox");
extern "C" bool RhInitialize();
extern "C" void RhpShutdown();
extern "C" void RhSetRuntimeInitializationCallback(int (*fPtr)());
extern "C" bool RhRegisterOSModule(void * pModule,
void * pvManagedCodeStartRange, uint32_t cbManagedCodeRange,
void * pvUnboxingStubsStartRange, uint32_t cbUnboxingStubsRange,
void ** pClasslibFunctions, uint32_t nClasslibFunctions);
extern "C" void* PalGetModuleHandleFromPointer(void* pointer);
extern "C" void GetRuntimeException();
extern "C" void FailFast();
extern "C" void AppendExceptionStackFrame();
extern "C" void GetSystemArrayEEType();
extern "C" void OnFirstChanceException();
extern "C" void OnUnhandledException();
extern "C" void IDynamicCastableIsInterfaceImplemented();
extern "C" void IDynamicCastableGetInterfaceImplementation();
typedef void(*pfn)();
static const pfn c_classlibFunctions[] = {
&GetRuntimeException,
&FailFast,
nullptr, // &UnhandledExceptionHandler,
&AppendExceptionStackFrame,
nullptr, // &CheckStaticClassConstruction,
&GetSystemArrayEEType,
&OnFirstChanceException,
&OnUnhandledException,
&IDynamicCastableIsInterfaceImplemented,
&IDynamicCastableGetInterfaceImplementation,
};
#ifndef _countof
#define _countof(_array) (sizeof(_array)/sizeof(_array[0]))
#endif
extern "C" void InitializeModules(void* osModule, void ** modules, int count, void ** pClasslibFunctions, int nClasslibFunctions);
#define NATIVEAOT_ENTRYPOINT __managed__Main
extern "C" int __managed__Main(int argc, char* argv[]);
extern "C" void __managed__Startup();
static int InitializeRuntime()
{
if (!RhInitialize())
return -1;
void * osModule = PalGetModuleHandleFromPointer((void*)&NATIVEAOT_ENTRYPOINT);
// TODO: pass struct with parameters instead of the large signature of RhRegisterOSModule
if (!RhRegisterOSModule(
osModule,
(void*)&__managedcode_a, (uint32_t)((char *)&__managedcode_z - (char*)&__managedcode_a),
(void*)&__unbox_a, (uint32_t)((char *)&__unbox_z - (char*)&__unbox_a),
(void **)&c_classlibFunctions, _countof(c_classlibFunctions)))
{
return -1;
}
InitializeModules(osModule, __modules_a, (int)((__modules_z - __modules_a)), (void **)&c_classlibFunctions, _countof(c_classlibFunctions));
// Run startup method immediately for a native library
__managed__Startup();
return 0;
}
// ----------------------------------------- coreclr compatability APIs
// TODO: figure out where to get these macros
#define S_OK 0x0
#define E_FAIL 0x80004005
#ifdef _MSC_VER
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT __attribute__((visibility("default")))
#endif
extern "C"
DLLEXPORT
int coreclr_initialize(
const char* exePath,
const char* appDomainFriendlyName,
int propertyCount,
const char** propertyKeys,
const char** propertyValues,
void** hostHandle,
unsigned int* domainId)
{
// TODO: validate arguments
// TODO: check that this function is called only once.
int rc = InitializeRuntime();
printf("InitializeRuntime called, return %d\n", rc);
if (rc == 0)
{
*hostHandle = nullptr;
*domainId = 1;
return S_OK;
}
else
{
return E_FAIL;
}
}
extern "C"
DLLEXPORT
int coreclr_shutdown(
void* hostHandle,
unsigned int domainId)
{
// TODO: validate arguments
// TODO: check that this function is called only once.
RhpShutdown();
return S_OK;
}
extern "C"
DLLEXPORT
int coreclr_execute_assembly(
void* hostHandle,
unsigned int domainId,
int argc,
const char** argv,
const char* managedAssemblyPath,
unsigned int* exitCode)
{
if (exitCode == nullptr)
{
return E_FAIL;
}
// TODO: validate other arguments
// NativeAOT does not like having argc == 0
if (argc == 0)
{
argc = 1;
const char* arg_0 = "exe";
argv = &arg_0;
}
// TODO: convert to WSTR for windows
*exitCode = __managed__Main(argc, const_cast<char**>(argv));
return S_OK;
}
extern "C" void AustinXamarinRuntimeInitialize(void* options);
extern "C"
DLLEXPORT
int coreclr_create_delegate(
void* hostHandle,
unsigned int domainId,
const char* entryPointAssemblyName,
const char* entryPointTypeName,
const char* entryPointMethodName,
void** delegate)
{
// TODO: validate args more carefully
if (strcmp(entryPointTypeName, "ObjCRuntime.Runtime") == 0 &&
strcmp(entryPointMethodName, "Initialize") == 0)
{
*delegate = (void*)AustinXamarinRuntimeInitialize;
return S_OK;
}
else
{
printf("coreclr_create_delegate: expected request, type: %s method: %s\n", entryPointTypeName, entryPointMethodName);
}
return E_FAIL;
}