Skip to content

.NET for macOS/iOS apps (PublishAot): host contract — __managed__Main, [UnmanagedCallersOnly(EntryPoint)] exports, module initializers of every assembly #294

Open

Description

@filipnavara

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 managed DrawRect
  • 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:

  1. Entry point. The host's main calls xamarin_main, which calls __managed__Main(argc, argv). So the program's main must be emitted under that name, and the runtime must initialize there.
  2. Exports by name. [UnmanagedCallersOnly(EntryPoint = "...")] methods must exist as global symbols under that name. The Objective-C bridge looks them up with dlsym: xamarin_objcruntime_runtime_nativeaotinitialize, and the managed static registrar's callbacks in registrar.o (one per exported Objective-C method). The host may call one of them before __managed__Main has run, so an export must make sure the runtime is initialized.
  3. Module initializers of every assembly. Microsoft.macOS.dll and 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) to X, 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,_X so ld64 doesn't dead-strip them from an executable.
  • Entry symbol and split init. EmitNativeMain is split into a guarded csharpc.runtime.init(argc, argv), which runs once and covers:

    • csrt_init
    • type and variance registrations
    • static init
    • module constructors

    main (renamed via the prototype's CSHARPC_ENTRY_SYMBOL=__managed__Main; the real switch should follow NativeAOT's --nativelib/--splitinit/CustomNativeMain naming) calls it and then Main. Every export calls the same init first, with _NSGetArgc/_NSGetArgv on 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>::.cctor shapes: ldnull; call GC.KeepAlive (skipped), and newobj C::.ctor(); call static M(C).
  • Linking is the SDK's own inputs:

    • --directpinvoke __Internal
    • main.arm64.o and registrar.o from obj/.../nativelibraries
    • the libxamarin-dotnet-nativeaot.a whose runtime hash matches the app's Microsoft.macOS.dll
    • the app's frameworks, swiftCore/swiftFoundation
    • -rpath @executable_path/../MonoBundle
    • csharpc's P/Invoke probing also looks in Contents/MonoBundle for the bundled dylibs

Smaller fixes the bridge needed along the way. Each can be its own PR:

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 behind UseCsharpc, as ADR 0016 does for other options?
  • Exports scope. Only assemblies named by --generateunmanagedentrypoints, as ILC does? Or every IL assembly's EntryPoint methods?
  • 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 --splitinit has the host call __managed__Main only.

Happy to turn this into PRs: exports first, then split init, then module constructors of referenced assemblies, each with a sample.

Activity

  1. jonathanperis commented on Nov 25, 2026

    @jonathanperis
    Owner

    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 the CSHARPC_ENTRY_SYMBOL variable goes away in favour of --splitinit → __managed__Main. This also gives plain NativeAOT parity: NativeLib=Shared with [UnmanagedCallersOnly(EntryPoint = …)] exports is a mainstream NativeAOT feature that csharpc lacks today.

    2. Exports scope: as ILC does. Export only the EntryPoint methods of the assemblies named by --generateunmanagedentrypoints. HIDDEN gives hidden visibility. For a NativeLib build 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. --splitinit then only splits main into 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>::.cctor shapes, also match what NativeAOT runs.

    PR order: your proposal works, each with a sample that runs on Linux CI where possible:

    1. exports;
    2. split init;
    3. module constructors of referenced assemblies.

    The exports and split-init pieces can be tested on Linux with a small C host calling __managed__Main and 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 and SafeHandle parameters in #297/#298. The ldtoken method-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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions