Skip to content

Commit b2dc37b

Browse files
Populate NativeAOT sources from runtimelab (#62563)
1 parent c9cd367 commit b2dc37b

File tree

1,311 files changed

+309697
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,311 files changed

+309697
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory(base)
2+
add_subdirectory(dll)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
project(bootstrapper)
2+
3+
set(SOURCES
4+
../main.cpp
5+
)
6+
7+
add_library(bootstrapper STATIC ${SOURCES})
8+
9+
install_static_library(bootstrapper aotsdk nativeaot)
10+
11+
if (CLR_CMAKE_TARGET_WIN32)
12+
add_library(bootstrapper.GuardCF STATIC ${SOURCES})
13+
install_static_library(bootstrapper.GuardCF aotsdk nativeaot)
14+
target_compile_options(bootstrapper.GuardCF PRIVATE $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/guard:cf>)
15+
endif()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
project(bootstrapperdll)
2+
3+
add_definitions(-DCORERT_DLL)
4+
5+
set(SOURCES
6+
../main.cpp
7+
)
8+
9+
add_library(bootstrapperdll STATIC ${SOURCES})
10+
11+
install_static_library(bootstrapperdll aotsdk nativeaot)
12+
13+
if (CLR_CMAKE_TARGET_WIN32)
14+
add_library(bootstrapperdll.GuardCF STATIC ${SOURCES})
15+
install_static_library(bootstrapperdll.GuardCF aotsdk nativeaot)
16+
target_compile_options(bootstrapperdll.GuardCF PRIVATE $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/guard:cf>)
17+
endif()
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Project DefaultTargets="CreateLib" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
3+
<PropertyGroup>
4+
<IlcCompileDependsOn>ComputeIlcCompileInputs;BuildOneFrameworkLibrary;SetupOSSpecificProps</IlcCompileDependsOn>
5+
<CreateLibDependsOn>BuildAllFrameworkLibrariesAsSingleLib</CreateLibDependsOn>
6+
<IlcMultiModule>true</IlcMultiModule>
7+
<NativeIntermediateOutputPath Condition="'$(FrameworkObjPath)' != ''">$(FrameworkObjPath)\</NativeIntermediateOutputPath>
8+
<BuildingFrameworkLibrary>true</BuildingFrameworkLibrary>
9+
<Optimize Condition="'$(Configuration)' == 'Release' and '$(Optimize)' == ''">true</Optimize>
10+
<DebugSymbols Condition="'$(DebugSymbols)' == ''">true</DebugSymbols>
11+
</PropertyGroup>
12+
13+
<Import Project="$(MSBuildThisFileDirectory)\Microsoft.DotNet.ILCompiler.targets" Condition="'$(IlcCalledViaPackage)' == 'true'" />
14+
<Import Project="Microsoft.NETCore.Native.targets" Condition="'$(IlcCalledViaPackage)' == ''" />
15+
16+
<Target Name="BuildAllFrameworkLibraries"
17+
Inputs="@(DefaultFrameworkAssemblies)"
18+
Outputs="@(DefaultFrameworkAssemblies->'$(NativeIntermediateOutputPath)\%(Filename)$(NativeObjectExt)')">
19+
<ItemGroup>
20+
<ProjectToBuild Include="$(MSBuildProjectFullPath)">
21+
<AdditionalProperties>
22+
LibraryToCompile=%(DefaultFrameworkAssemblies.Identity)
23+
</AdditionalProperties>
24+
</ProjectToBuild>
25+
</ItemGroup>
26+
<MSBuild Projects="@(ProjectToBuild)" Targets="IlcCompile" BuildInParallel="true" />
27+
</Target>
28+
29+
<Target Name="BuildAllFrameworkLibrariesAsSingleLib"
30+
DependsOnTargets="BuildAllFrameworkLibraries">
31+
32+
<ItemGroup>
33+
<LibInputs Include="$(NativeIntermediateOutputPath)\*$(NativeObjectExt)" />
34+
</ItemGroup>
35+
</Target>
36+
37+
<Target Name="BuildOneFrameworkLibrary">
38+
<PropertyGroup>
39+
<IlcGenerateMetadataLog>true</IlcGenerateMetadataLog>
40+
</PropertyGroup>
41+
<ItemGroup>
42+
<ManagedBinary Include="$(LibraryToCompile)" />
43+
<IlcCompileInput Include="@(ManagedBinary)" />
44+
</ItemGroup>
45+
</Target>
46+
47+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
2+
<Import Project="../Directory.Build.props" />
3+
4+
<PropertyGroup>
5+
<OutputPath>$(RuntimeBinDir)/build/</OutputPath>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Content Include="*.*" Exclude="$(MSBuildProjectFile)" />
10+
</ItemGroup>
11+
12+
<Target Name="Build">
13+
<Copy SourceFiles="@(Content)" DestinationFolder="$(OutputPath)" />
14+
</Target>
15+
16+
<Target Name="Restore" />
17+
</Project>

0 commit comments

Comments
 (0)