|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#include <stdint.h> |
| 5 | + |
| 6 | +// |
| 7 | +// This is the mechanism whereby multiple linked modules contribute their global data for initialization at |
| 8 | +// startup of the application. |
| 9 | +// |
| 10 | +// ILC creates sections in the output obj file to mark the beginning and end of merged global data. |
| 11 | +// It defines sentinel symbols that are used to get the addresses of the start and end of global data |
| 12 | +// at runtime. The section names are platform-specific to match platform-specific linker conventions. |
| 13 | +// |
| 14 | +#if defined(_MSC_VER) |
| 15 | + |
| 16 | +#pragma section(".modules$A", read) |
| 17 | +#pragma section(".modules$Z", read) |
| 18 | +extern "C" __declspec(allocate(".modules$A")) void * __modules_a[]; |
| 19 | +extern "C" __declspec(allocate(".modules$Z")) void * __modules_z[]; |
| 20 | + |
| 21 | +__declspec(allocate(".modules$A")) void * __modules_a[] = { nullptr }; |
| 22 | +__declspec(allocate(".modules$Z")) void * __modules_z[] = { nullptr }; |
| 23 | + |
| 24 | +// |
| 25 | +// Each obj file compiled from managed code has a .modules$I section containing a pointer to its ReadyToRun |
| 26 | +// data (which points at eager class constructors, frozen strings, etc). |
| 27 | +// |
| 28 | +// The #pragma ... /merge directive folds the book-end sections and all .modules$I sections from all input |
| 29 | +// obj files into .rdata in alphabetical order. |
| 30 | +// |
| 31 | +#pragma comment(linker, "/merge:.modules=.rdata") |
| 32 | + |
| 33 | +// |
| 34 | +// Unboxing stubs need to be merged, folded and sorted. They are delimited by two special sections (.unbox$A |
| 35 | +// and .unbox$Z). All unboxing stubs are in .unbox$M sections. |
| 36 | +// |
| 37 | +#pragma comment(linker, "/merge:.unbox=.text") |
| 38 | + |
| 39 | +char _bookend_a; |
| 40 | +char _bookend_z; |
| 41 | + |
| 42 | +// |
| 43 | +// Generate bookends for the managed code section. |
| 44 | +// We give them unique bodies to prevent folding. |
| 45 | +// |
| 46 | + |
| 47 | +#pragma code_seg(".managedcode$A") |
| 48 | +void* __managedcode_a() { return &_bookend_a; } |
| 49 | +#pragma code_seg(".managedcode$Z") |
| 50 | +void* __managedcode_z() { return &_bookend_z; } |
| 51 | +#pragma code_seg() |
| 52 | + |
| 53 | +// |
| 54 | +// Generate bookends for the unboxing stub section. |
| 55 | +// We give them unique bodies to prevent folding. |
| 56 | +// |
| 57 | + |
| 58 | +#pragma code_seg(".unbox$A") |
| 59 | +void* __unbox_a() { return &_bookend_a; } |
| 60 | +#pragma code_seg(".unbox$Z") |
| 61 | +void* __unbox_z() { return &_bookend_z; } |
| 62 | +#pragma code_seg() |
| 63 | + |
| 64 | +#else // _MSC_VER |
| 65 | + |
| 66 | +#if defined(__APPLE__) |
| 67 | + |
| 68 | +extern void * __modules_a[] __asm("section$start$__DATA$__modules"); |
| 69 | +extern void * __modules_z[] __asm("section$end$__DATA$__modules"); |
| 70 | +extern char __managedcode_a __asm("section$start$__TEXT$__managedcode"); |
| 71 | +extern char __managedcode_z __asm("section$end$__TEXT$__managedcode"); |
| 72 | +extern char __unbox_a __asm("section$start$__TEXT$__unbox"); |
| 73 | +extern char __unbox_z __asm("section$end$__TEXT$__unbox"); |
| 74 | + |
| 75 | +#else // __APPLE__ |
| 76 | + |
| 77 | +extern "C" void * __start___modules[]; |
| 78 | +extern "C" void * __stop___modules[]; |
| 79 | +static void * (&__modules_a)[] = __start___modules; |
| 80 | +static void * (&__modules_z)[] = __stop___modules; |
| 81 | + |
| 82 | +extern "C" char __start___managedcode; |
| 83 | +extern "C" char __stop___managedcode; |
| 84 | +static char& __managedcode_a = __start___managedcode; |
| 85 | +static char& __managedcode_z = __stop___managedcode; |
| 86 | + |
| 87 | +extern "C" char __start___unbox; |
| 88 | +extern "C" char __stop___unbox; |
| 89 | +static char& __unbox_a = __start___unbox; |
| 90 | +static char& __unbox_z = __stop___unbox; |
| 91 | + |
| 92 | +#endif // __APPLE__ |
| 93 | + |
| 94 | +#endif // _MSC_VER |
| 95 | + |
| 96 | +extern "C" bool RhInitialize(); |
| 97 | +extern "C" void RhpEnableConservativeStackReporting(); |
| 98 | +extern "C" void RhpShutdown(); |
| 99 | +extern "C" void RhSetRuntimeInitializationCallback(int (*fPtr)()); |
| 100 | + |
| 101 | +extern "C" bool RhRegisterOSModule(void * pModule, |
| 102 | + void * pvManagedCodeStartRange, uint32_t cbManagedCodeRange, |
| 103 | + void * pvUnboxingStubsStartRange, uint32_t cbUnboxingStubsRange, |
| 104 | + void ** pClasslibFunctions, uint32_t nClasslibFunctions); |
| 105 | + |
| 106 | +extern "C" void* PalGetModuleHandleFromPointer(void* pointer); |
| 107 | + |
| 108 | +extern "C" void GetRuntimeException(); |
| 109 | +extern "C" void FailFast(); |
| 110 | +extern "C" void AppendExceptionStackFrame(); |
| 111 | +extern "C" void GetSystemArrayEEType(); |
| 112 | +extern "C" void OnFirstChanceException(); |
| 113 | +extern "C" void OnUnhandledException(); |
| 114 | +extern "C" void IDynamicCastableIsInterfaceImplemented(); |
| 115 | +extern "C" void IDynamicCastableGetInterfaceImplementation(); |
| 116 | + |
| 117 | +typedef void(*pfn)(); |
| 118 | + |
| 119 | +static const pfn c_classlibFunctions[] = { |
| 120 | + &GetRuntimeException, |
| 121 | + &FailFast, |
| 122 | + nullptr, // &UnhandledExceptionHandler, |
| 123 | + &AppendExceptionStackFrame, |
| 124 | + nullptr, // &CheckStaticClassConstruction, |
| 125 | + &GetSystemArrayEEType, |
| 126 | + &OnFirstChanceException, |
| 127 | + &OnUnhandledException, |
| 128 | + &IDynamicCastableIsInterfaceImplemented, |
| 129 | + &IDynamicCastableGetInterfaceImplementation, |
| 130 | +}; |
| 131 | + |
| 132 | +#ifndef _countof |
| 133 | +#define _countof(_array) (sizeof(_array)/sizeof(_array[0])) |
| 134 | +#endif |
| 135 | + |
| 136 | +extern "C" void InitializeModules(void* osModule, void ** modules, int count, void ** pClasslibFunctions, int nClasslibFunctions); |
| 137 | + |
| 138 | +#ifndef CORERT_DLL |
| 139 | +#define CORERT_ENTRYPOINT __managed__Main |
| 140 | +#if defined(_WIN32) |
| 141 | +extern "C" int __managed__Main(int argc, wchar_t* argv[]); |
| 142 | +#else |
| 143 | +extern "C" int __managed__Main(int argc, char* argv[]); |
| 144 | +#endif |
| 145 | +#else |
| 146 | +#define CORERT_ENTRYPOINT __managed__Startup |
| 147 | +extern "C" void __managed__Startup(); |
| 148 | +#endif // !CORERT_DLL |
| 149 | + |
| 150 | +static int InitializeRuntime() |
| 151 | +{ |
| 152 | + if (!RhInitialize()) |
| 153 | + return -1; |
| 154 | + |
| 155 | + // RhpEnableConservativeStackReporting(); |
| 156 | + |
| 157 | + void * osModule = PalGetModuleHandleFromPointer((void*)&CORERT_ENTRYPOINT); |
| 158 | + |
| 159 | + // TODO: pass struct with parameters instead of the large signature of RhRegisterOSModule |
| 160 | + if (!RhRegisterOSModule( |
| 161 | + osModule, |
| 162 | + (void*)&__managedcode_a, (uint32_t)((char *)&__managedcode_z - (char*)&__managedcode_a), |
| 163 | + (void*)&__unbox_a, (uint32_t)((char *)&__unbox_z - (char*)&__unbox_a), |
| 164 | + (void **)&c_classlibFunctions, _countof(c_classlibFunctions))) |
| 165 | + { |
| 166 | + return -1; |
| 167 | + } |
| 168 | + |
| 169 | + InitializeModules(osModule, __modules_a, (int)((__modules_z - __modules_a)), (void **)&c_classlibFunctions, _countof(c_classlibFunctions)); |
| 170 | + |
| 171 | +#ifdef CORERT_DLL |
| 172 | + // Run startup method immediately for a native library |
| 173 | + __managed__Startup(); |
| 174 | +#endif // CORERT_DLL |
| 175 | + |
| 176 | + return 0; |
| 177 | +} |
| 178 | + |
| 179 | +#ifndef CORERT_DLL |
| 180 | + |
| 181 | +#ifdef ENSURE_PRIMARY_STACK_SIZE |
| 182 | +__attribute__((noinline, optnone)) |
| 183 | +static void EnsureStackSize(int stackSize) |
| 184 | +{ |
| 185 | + volatile char* s = (char*)_alloca(stackSize); |
| 186 | + *s = 0; |
| 187 | +} |
| 188 | +#endif // ENSURE_PRIMARY_STACK_SIZE |
| 189 | + |
| 190 | +#if defined(_WIN32) |
| 191 | +int __cdecl wmain(int argc, wchar_t* argv[]) |
| 192 | +#else |
| 193 | +int main(int argc, char* argv[]) |
| 194 | +#endif |
| 195 | +{ |
| 196 | +#ifdef ENSURE_PRIMARY_STACK_SIZE |
| 197 | + // TODO: https://github.com/dotnet/runtimelab/issues/791 |
| 198 | + EnsureStackSize(1536 * 1024); |
| 199 | +#endif |
| 200 | + |
| 201 | + int initval = InitializeRuntime(); |
| 202 | + if (initval != 0) |
| 203 | + return initval; |
| 204 | + |
| 205 | + int retval = __managed__Main(argc, argv); |
| 206 | + |
| 207 | + RhpShutdown(); |
| 208 | + |
| 209 | + return retval; |
| 210 | +} |
| 211 | +#endif // !CORERT_DLL |
| 212 | + |
| 213 | +#ifdef CORERT_DLL |
| 214 | +static struct InitializeRuntimePointerHelper |
| 215 | +{ |
| 216 | + InitializeRuntimePointerHelper() |
| 217 | + { |
| 218 | + RhSetRuntimeInitializationCallback(&InitializeRuntime); |
| 219 | + } |
| 220 | +} initializeRuntimePointerHelper; |
| 221 | + |
| 222 | +extern "C" void* CoreRT_StaticInitialization(); |
| 223 | + |
| 224 | +void* CoreRT_StaticInitialization() |
| 225 | +{ |
| 226 | + return &initializeRuntimePointerHelper; |
| 227 | +} |
| 228 | +#endif // CORERT_DLL |
0 commit comments