|
| 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