Repository navigation
Description
Summary
A .NET for macOS (and iOS) app built with PublishAot is not a normal executable. The SDK links NativeAOT's output into Apple's host, libxamarin-dotnet-nativeaot.a. That host owns main and calls back into managed code through named symbols.
csharpc can't produce what the host expects yet. I prototyped the missing pieces, and with them two apps run, compiled from IL with csharpc and linked into the same host:
dotnet new macos: output identical to NativeAOT, including the window drawn by a managedDrawRect- eM Client, a large WinForms-on-Cocoa app: reaches its first-run wizard
This issue describes the contract and the prototype, to agree on a design before a PR.
The contract
For eM Client, the SDK invokes ILC with these options (from obj/.../native/<app>.ilc.rsp):
--nativelib
--splitinit
--exportsfile:obj/.../native/eM Client.exports
--generateunmanagedentrypoints:System.Private.CoreLib,HIDDEN
--generateunmanagedentrypoints:<every assembly of the app> (255 of them)
--directpinvoke:__Internal ...
The host and the managed code meet in three places:
- Entry point. The host's
maincallsxamarin_main, which calls__managed__Main(argc, argv). So the program'smainmust be emitted under that name, and the runtime must initialize there. - Exports by name.
[UnmanagedCallersOnly(EntryPoint = "...")]methods must exist as global symbols under that name. The Objective-C bridge looks them up withdlsym:xamarin_objcruntime_runtime_nativeaotinitialize, and the managed static registrar's callbacks inregistrar.o(one per exported Objective-C method). The host may call one of them before__managed__Mainhas run, so an export must make sure the runtime is initialized. - Module initializers of every assembly.
Microsoft.macOS.dlland the app's other assemblies have module constructors that set up the registrar's assembly map ([ModuleInitializer], compiled into<Module>::.cctor). Upstream runs only the program's own module constructor (ModuleCodeGen.cs, "A program given as IL").
What the prototype does
The prototype lives in a scratch clone; I can push it as a branch if useful.
-
Exports. A new
ModuleCodeGen.NativeExports.cs.- It queues every static, non-generic
[UnmanagedCallersOnly(EntryPoint = X)]method of the IL assemblies (not CoreLib or the framework) as a root. - After lowering, it renames the method's existing C-ABI entry thunk (
NativeEntryThunk) toX, with external linkage and default visibility. The ABI attributes (sret,byval) are the thunk's own. - Two exports with the same name are a diagnostic.
- The names stay external through runtime LTO, and the driver passes
-Wl,-u,_Xso ld64 doesn't dead-strip them from an executable.
- It queues every static, non-generic
-
Entry symbol and split init.
EmitNativeMainis split into a guardedcsharpc.runtime.init(argc, argv), which runs once and covers:csrt_init- type and variance registrations
- static init
- module constructors
main(renamed via the prototype'sCSHARPC_ENTRY_SYMBOL=__managed__Main; the real switch should follow NativeAOT's--nativelib/--splitinit/CustomNativeMainnaming) calls it and thenMain. Every export calls the same init first, with_NSGetArgc/_NSGetArgvon Apple platforms. -
Module constructors of all IL assemblies. They run in reference order, the program last (
ModuleConstructorOrder()).IlAssemblySet.ModuleConstructorSteps()also had to understand two more<Module>::.cctorshapes:ldnull; call GC.KeepAlive(skipped), andnewobj C::.ctor(); call static M(C).
-
Linking is the SDK's own inputs:
--directpinvoke __Internalmain.arm64.oandregistrar.ofromobj/.../nativelibraries- the
libxamarin-dotnet-nativeaot.awhose runtime hash matches the app'sMicrosoft.macOS.dll - the app's frameworks,
swiftCore/swiftFoundation -rpath @executable_path/../MonoBundle- csharpc's P/Invoke probing also looks in
Contents/MonoBundlefor the bundled dylibs
Smaller fixes the bridge needed along the way. Each can be its own PR:
NFloat, a shadow overdouble, blittable (Interop: nint.ToPointer/Add/MaxValue, NFloat, Marshal.SizeOf(Type)/PtrToStructure(Type)/GetHRForException, NativeLibrary.Load(..., Assembly, ...), UnsafeAccessor constructors #99)- P/Invoke delegate and
SafeHandleparameters (AddRef/Releasearound the call) - AArch64 HFA / single-scalar structs in reverse thunks
ldtokenof a method passed asRuntimeMethodHandle
Questions
- Driver surface. Should csharpc accept the SDK's ILC options as they are (
--nativelib,--splitinit,--exportsfile,--generateunmanagedentrypoints)? Or should there be MSBuild-level names behindUseCsharpc, as ADR 0016 does for other options? - Exports scope. Only assemblies named by
--generateunmanagedentrypoints, as ILC does? Or every IL assembly'sEntryPointmethods? - Runtime init from exports. Prepending the guarded init to every export is the simplest correct thing. An alternative is an explicit
__managed__Startup-style init the host calls; NativeAOT's--splitinithas the host call__managed__Mainonly.
Happy to turn this into PRs: exports first, then split init, then module constructors of referenced assemblies, each with a sample.
Activity
jonathanperis commentedon Nov 25, 2026
Thanks for the detailed write-up. Here are answers to the three questions, following the project's rule that new driver and SDK surface uses NativeAOT's names and behaviour (AGENTS.md, ADR 0016).
1. Driver surface: accept ILC's options as they are. The macOS/iOS SDK writes them into
<app>.ilc.rsp, so the drop-in path is csharpc reading the same flags:--nativelib,--splitinit,--exportsfile:<file>;--generateunmanagedentrypoints:<assembly>[,HIDDEN];--directpinvoke:<lib>.At the MSBuild level, the existing NativeAOT properties map onto them:
NativeLib=Shared|Static,CustomNativeMain,ExportsFile. No csharpc-specific names, so theCSHARPC_ENTRY_SYMBOLvariable goes away in favour of--splitinit→__managed__Main. This also gives plain NativeAOT parity:NativeLib=Sharedwith[UnmanagedCallersOnly(EntryPoint = …)]exports is a mainstream NativeAOT feature that csharpc lacks today.2. Exports scope: as ILC does. Export only the
EntryPointmethods of the assemblies named by--generateunmanagedentrypoints.HIDDENgives hidden visibility. For aNativeLibbuild without the flag, the entry assemblies are the defaults, as ILC uses them. Two exports with the same name stay a diagnostic, as in your prototype.3. Runtime init from exports: the guarded init in every export. That is NativeAOT's model as well: its reverse-P/Invoke prolog initializes the runtime lazily on first entry, so a host may call an export before
__managed__Main. A once-only guard, cheap after the first call, keeps that behaviour.--splitinitthen only splitsmaininto the init and__managed__Main, as you have it.Module constructors of every IL assembly (reference order, the program last), with the two extra
<Module>::.cctorshapes, also match what NativeAOT runs.PR order: your proposal works, each with a sample that runs on Linux CI where possible:
The exports and split-init pieces can be tested on Linux with a small C host calling
__managed__Mainand an export. The Apple host linking can then be a project test on the macOS runner. Several of your smaller fixes are already in or in review: NFloat in #296 (merging now) and P/Invoke delegate andSafeHandleparameters in #297/#298. Theldtokenmethod-handle fix is not in yet, so it can come with the exports PR.If you can push the prototype branch, I'm happy to review it in that order, or to take over pieces you'd rather not finish.