Skip to content

Commit

Permalink
Populate NativeAOT sources from runtimelab (#62563)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalStrehovsky authored Dec 9, 2021
1 parent c9cd367 commit b2dc37b
Show file tree
Hide file tree
Showing 1,311 changed files with 309,697 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/coreclr/nativeaot/Bootstrap/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(base)
add_subdirectory(dll)
15 changes: 15 additions & 0 deletions src/coreclr/nativeaot/Bootstrap/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
project(bootstrapper)

set(SOURCES
../main.cpp
)

add_library(bootstrapper STATIC ${SOURCES})

install_static_library(bootstrapper aotsdk nativeaot)

if (CLR_CMAKE_TARGET_WIN32)
add_library(bootstrapper.GuardCF STATIC ${SOURCES})
install_static_library(bootstrapper.GuardCF aotsdk nativeaot)
target_compile_options(bootstrapper.GuardCF PRIVATE $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/guard:cf>)
endif()
17 changes: 17 additions & 0 deletions src/coreclr/nativeaot/Bootstrap/dll/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
project(bootstrapperdll)

add_definitions(-DCORERT_DLL)

set(SOURCES
../main.cpp
)

add_library(bootstrapperdll STATIC ${SOURCES})

install_static_library(bootstrapperdll aotsdk nativeaot)

if (CLR_CMAKE_TARGET_WIN32)
add_library(bootstrapperdll.GuardCF STATIC ${SOURCES})
install_static_library(bootstrapperdll.GuardCF aotsdk nativeaot)
target_compile_options(bootstrapperdll.GuardCF PRIVATE $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/guard:cf>)
endif()
228 changes: 228 additions & 0 deletions src/coreclr/nativeaot/Bootstrap/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <stdint.h>

//
// This is the mechanism whereby multiple linked modules contribute their global data for initialization at
// startup of the application.
//
// ILC creates sections in the output obj file to mark the beginning and end of merged global data.
// It defines sentinel symbols that are used to get the addresses of the start and end of global data
// at runtime. The section names are platform-specific to match platform-specific linker conventions.
//
#if defined(_MSC_VER)

#pragma section(".modules$A", read)
#pragma section(".modules$Z", read)
extern "C" __declspec(allocate(".modules$A")) void * __modules_a[];
extern "C" __declspec(allocate(".modules$Z")) void * __modules_z[];

__declspec(allocate(".modules$A")) void * __modules_a[] = { nullptr };
__declspec(allocate(".modules$Z")) void * __modules_z[] = { nullptr };

//
// Each obj file compiled from managed code has a .modules$I section containing a pointer to its ReadyToRun
// data (which points at eager class constructors, frozen strings, etc).
//
// The #pragma ... /merge directive folds the book-end sections and all .modules$I sections from all input
// obj files into .rdata in alphabetical order.
//
#pragma comment(linker, "/merge:.modules=.rdata")

//
// Unboxing stubs need to be merged, folded and sorted. They are delimited by two special sections (.unbox$A
// and .unbox$Z). All unboxing stubs are in .unbox$M sections.
//
#pragma comment(linker, "/merge:.unbox=.text")

char _bookend_a;
char _bookend_z;

//
// Generate bookends for the managed code section.
// We give them unique bodies to prevent folding.
//

#pragma code_seg(".managedcode$A")
void* __managedcode_a() { return &_bookend_a; }
#pragma code_seg(".managedcode$Z")
void* __managedcode_z() { return &_bookend_z; }
#pragma code_seg()

//
// Generate bookends for the unboxing stub section.
// We give them unique bodies to prevent folding.
//

#pragma code_seg(".unbox$A")
void* __unbox_a() { return &_bookend_a; }
#pragma code_seg(".unbox$Z")
void* __unbox_z() { return &_bookend_z; }
#pragma code_seg()

#else // _MSC_VER

#if defined(__APPLE__)

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");

#else // __APPLE__

extern "C" void * __start___modules[];
extern "C" void * __stop___modules[];
static void * (&__modules_a)[] = __start___modules;
static void * (&__modules_z)[] = __stop___modules;

extern "C" char __start___managedcode;
extern "C" char __stop___managedcode;
static char& __managedcode_a = __start___managedcode;
static char& __managedcode_z = __stop___managedcode;

extern "C" char __start___unbox;
extern "C" char __stop___unbox;
static char& __unbox_a = __start___unbox;
static char& __unbox_z = __stop___unbox;

#endif // __APPLE__

#endif // _MSC_VER

extern "C" bool RhInitialize();
extern "C" void RhpEnableConservativeStackReporting();
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);

#ifndef CORERT_DLL
#define CORERT_ENTRYPOINT __managed__Main
#if defined(_WIN32)
extern "C" int __managed__Main(int argc, wchar_t* argv[]);
#else
extern "C" int __managed__Main(int argc, char* argv[]);
#endif
#else
#define CORERT_ENTRYPOINT __managed__Startup
extern "C" void __managed__Startup();
#endif // !CORERT_DLL

static int InitializeRuntime()
{
if (!RhInitialize())
return -1;

// RhpEnableConservativeStackReporting();

void * osModule = PalGetModuleHandleFromPointer((void*)&CORERT_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));

#ifdef CORERT_DLL
// Run startup method immediately for a native library
__managed__Startup();
#endif // CORERT_DLL

return 0;
}

#ifndef CORERT_DLL

#ifdef ENSURE_PRIMARY_STACK_SIZE
__attribute__((noinline, optnone))
static void EnsureStackSize(int stackSize)
{
volatile char* s = (char*)_alloca(stackSize);
*s = 0;
}
#endif // ENSURE_PRIMARY_STACK_SIZE

#if defined(_WIN32)
int __cdecl wmain(int argc, wchar_t* argv[])
#else
int main(int argc, char* argv[])
#endif
{
#ifdef ENSURE_PRIMARY_STACK_SIZE
// TODO: https://github.com/dotnet/runtimelab/issues/791
EnsureStackSize(1536 * 1024);
#endif

int initval = InitializeRuntime();
if (initval != 0)
return initval;

int retval = __managed__Main(argc, argv);

RhpShutdown();

return retval;
}
#endif // !CORERT_DLL

#ifdef CORERT_DLL
static struct InitializeRuntimePointerHelper
{
InitializeRuntimePointerHelper()
{
RhSetRuntimeInitializationCallback(&InitializeRuntime);
}
} initializeRuntimePointerHelper;

extern "C" void* CoreRT_StaticInitialization();

void* CoreRT_StaticInitialization()
{
return &initializeRuntimePointerHelper;
}
#endif // CORERT_DLL
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<Project DefaultTargets="CreateLib" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<IlcCompileDependsOn>ComputeIlcCompileInputs;BuildOneFrameworkLibrary;SetupOSSpecificProps</IlcCompileDependsOn>
<CreateLibDependsOn>BuildAllFrameworkLibrariesAsSingleLib</CreateLibDependsOn>
<IlcMultiModule>true</IlcMultiModule>
<NativeIntermediateOutputPath Condition="'$(FrameworkObjPath)' != ''">$(FrameworkObjPath)\</NativeIntermediateOutputPath>
<BuildingFrameworkLibrary>true</BuildingFrameworkLibrary>
<Optimize Condition="'$(Configuration)' == 'Release' and '$(Optimize)' == ''">true</Optimize>
<DebugSymbols Condition="'$(DebugSymbols)' == ''">true</DebugSymbols>
</PropertyGroup>

<Import Project="$(MSBuildThisFileDirectory)\Microsoft.DotNet.ILCompiler.targets" Condition="'$(IlcCalledViaPackage)' == 'true'" />
<Import Project="Microsoft.NETCore.Native.targets" Condition="'$(IlcCalledViaPackage)' == ''" />

<Target Name="BuildAllFrameworkLibraries"
Inputs="@(DefaultFrameworkAssemblies)"
Outputs="@(DefaultFrameworkAssemblies->'$(NativeIntermediateOutputPath)\%(Filename)$(NativeObjectExt)')">
<ItemGroup>
<ProjectToBuild Include="$(MSBuildProjectFullPath)">
<AdditionalProperties>
LibraryToCompile=%(DefaultFrameworkAssemblies.Identity)
</AdditionalProperties>
</ProjectToBuild>
</ItemGroup>
<MSBuild Projects="@(ProjectToBuild)" Targets="IlcCompile" BuildInParallel="true" />
</Target>

<Target Name="BuildAllFrameworkLibrariesAsSingleLib"
DependsOnTargets="BuildAllFrameworkLibraries">

<ItemGroup>
<LibInputs Include="$(NativeIntermediateOutputPath)\*$(NativeObjectExt)" />
</ItemGroup>
</Target>

<Target Name="BuildOneFrameworkLibrary">
<PropertyGroup>
<IlcGenerateMetadataLog>true</IlcGenerateMetadataLog>
</PropertyGroup>
<ItemGroup>
<ManagedBinary Include="$(LibraryToCompile)" />
<IlcCompileInput Include="@(ManagedBinary)" />
</ItemGroup>
</Target>

</Project>
17 changes: 17 additions & 0 deletions src/coreclr/nativeaot/BuildIntegration/BuildIntegration.proj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="../Directory.Build.props" />

<PropertyGroup>
<OutputPath>$(RuntimeBinDir)/build/</OutputPath>
</PropertyGroup>

<ItemGroup>
<Content Include="*.*" Exclude="$(MSBuildProjectFile)" />
</ItemGroup>

<Target Name="Build">
<Copy SourceFiles="@(Content)" DestinationFolder="$(OutputPath)" />
</Target>

<Target Name="Restore" />
</Project>
Loading

0 comments on commit b2dc37b

Please sign in to comment.