Skip to content

Commit 9f8b1e0

Browse files
Sergio0694pull[bot]
authored andcommitted
Automatically export '__managed__Main' when needed (#105353)
* Automatically export '__managed__Main' if needed * Add unit test for stub .exe configuration * Move '__managed__Main' export to shared .targets * Use 'LoadLibrary' and 'GetProcAddress' instead * Update 'CMakeLists.txt' from 'SharedLibrary' * Fix .cs filename in .csproj file * Port scripts from shared library test
1 parent ea8de1d commit 9f8b1e0

5 files changed

Lines changed: 150 additions & 0 deletions

File tree

‎src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ The .NET Foundation licenses this file to you under the MIT license.
291291

292292
<!-- The managed debugging support in libraries is unused - trim it -->
293293
<IlcArg Condition="'$(DebuggerSupport)' != 'true'" Include="--feature:System.Diagnostics.Debugger.IsSupported=false" />
294+
295+
<!-- Export the managed entry point if building a native library in custom main mode -->
296+
<IlcArg Condition="'$(NativeLib)' == 'Shared' and '$(CustomNativeMain)' == 'true'" Include="--export-dynamic-symbol:__managed__Main" />
294297
</ItemGroup>
295298

296299
<MakeDir Directories="$(NativeIntermediateOutputPath)" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
project (CustomMainWithStubExeNative)
2+
include_directories(${INC_PLATFORM_DIR})
3+
4+
add_executable (CustomMainWithStubExeNative CustomMainWithStubExeNative.cpp)
5+
6+
if (CLR_CMAKE_TARGET_UNIX)
7+
target_link_libraries (CustomMainWithStubExeNative PRIVATE ${CMAKE_DL_LIBS})
8+
endif()
9+
10+
# add the install targets
11+
install (TARGETS CustomMainWithStubExeNative DESTINATION bin)
12+
13+
# If there's a dynamic ASAN runtime, then copy it to project output.
14+
if (NOT "${ASAN_RUNTIME}" STREQUAL "")
15+
file(COPY "${ASAN_RUNTIME}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
16+
endif()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
using System;
5+
using System.Runtime.CompilerServices;
6+
using System.Runtime.InteropServices;
7+
8+
class Program
9+
{
10+
// Each of the module initializer, class constructor, and IncrementExitCode
11+
// should be executed exactly once, causing this to each 100 by program exit.
12+
static int s_exitCode;
13+
14+
[ModuleInitializer]
15+
internal static void InitializeModule()
16+
{
17+
s_exitCode += 8;
18+
}
19+
20+
static Program()
21+
{
22+
s_exitCode += 31;
23+
// A side-effecting operation to prevent this cctor from being pre-inited at compile time.
24+
Console.WriteLine("hello from static constructor");
25+
}
26+
27+
[UnmanagedCallersOnly(EntryPoint = "IncrementExitCode")]
28+
static void IncrementExitCode(int amount)
29+
{
30+
s_exitCode += amount;
31+
}
32+
33+
int ExitCode;
34+
35+
static int Main(string[] args)
36+
{
37+
Console.WriteLine("hello from managed main");
38+
return s_exitCode;
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<CustomNativeMain>true</CustomNativeMain>
5+
<NativeLib>Shared</NativeLib>
6+
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<CLRTestBatchPreCommands><![CDATA[
10+
$(CLRTestBatchPreCommands)
11+
mkdir native 2>nul
12+
copy /y clang_rt.* native\
13+
copy /y CustomMainWithStubExeNative.exe native\CustomMainWithStubExe.exe
14+
]]></CLRTestBatchPreCommands>
15+
16+
<CLRTestBashPreCommands><![CDATA[
17+
$(CLRTestBashPreCommands)
18+
mkdir -p native
19+
cp libclang_rt.* native/
20+
cp CustomMainWithStubExeNative native/CustomMainWithStubExe
21+
]]></CLRTestBashPreCommands>
22+
</PropertyGroup>
23+
24+
<ItemGroup>
25+
<Compile Include="CustomMainWithStubExe.cs" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<CMakeProjectReference Include="CMakeLists.txt" />
30+
</ItemGroup>
31+
</Project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
#ifdef TARGET_WINDOWS
5+
#include "windows.h"
6+
#else
7+
#include "dlfcn.h"
8+
#endif
9+
#include "stdio.h"
10+
#include <stdint.h>
11+
#include "string.h"
12+
13+
#ifndef TARGET_WINDOWS
14+
#define __stdcall
15+
#endif
16+
17+
// typedef for shared lib exported methods
18+
#if defined(_WIN32)
19+
typedef int(__stdcall *f___managed__Main)(int argc, wchar_t* argv[]);
20+
#else
21+
typedef int(__stdcall *f___managed__Main)(int argc, char* argv[]);
22+
#endif
23+
typedef void(__stdcall *f_IncrementExitCode)(int32_t amount);
24+
25+
#if defined(_WIN32)
26+
int __cdecl wmain(int argc, wchar_t* argv[])
27+
#else
28+
int main(int argc, char* argv[])
29+
#endif
30+
{
31+
#ifdef TARGET_WINDOWS
32+
HINSTANCE handle = LoadLibrary("CustomMainWithStubExe.dll");
33+
#elif __APPLE__
34+
void *handle = dlopen(strcat(argv[0], ".dylib"), RTLD_LAZY);
35+
#else
36+
void *handle = dlopen(strcat(argv[0], ".so"), RTLD_LAZY);
37+
#endif
38+
39+
if (!handle)
40+
return 1;
41+
42+
#ifdef TARGET_WINDOWS
43+
f___managed__Main __managed__MainFunc = (f___managed__Main)GetProcAddress(handle, "__managed__Main");
44+
f_IncrementExitCode IncrementExitCodeFunc = (f_IncrementExitCode)GetProcAddress(handle, "IncrementExitCode");
45+
#else
46+
f___managed__Main __managed__MainFunc = (f___managed__Main)dlsym(handle, "__managed__Main");
47+
f_IncrementExitCode IncrementExitCodeFunc = (f_IncrementExitCode)dlsym(handle, "IncrementExitCode");
48+
#endif
49+
50+
puts("hello from native main");
51+
IncrementExitCodeFunc(61);
52+
return __managed__MainFunc(argc, argv);
53+
}
54+
55+
extern "C" const char* __stdcall __asan_default_options()
56+
{
57+
// NativeAOT is not designed to be unloadable, so we'll leak a few allocations from the shared library.
58+
// Disable leak detection as we don't care about these leaks as of now.
59+
return "detect_leaks=0 use_sigaltstack=0";
60+
}

0 commit comments

Comments
 (0)