Skip to content

[tools/msbuild] Add InlineDlfcnMethodsStep as an opt-in custom linker step - #24888

Merged
rolfbjarne merged 14 commits into
mainfrom
dev/rolf/inline-dlfcn
May 7, 2026
Merged

[tools/msbuild] Add InlineDlfcnMethodsStep as an opt-in custom linker step#24888
rolfbjarne merged 14 commits into
mainfrom
dev/rolf/inline-dlfcn

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Add a new custom linker step that replaces runtime Dlfcn.dlsym calls with
direct native symbol lookups via P/Invokes to __Internal. This improves
startup performance and app size, and ensures the native linker can see symbol
references that would otherwise only be resolved at runtime.

Two modes are supported, controlled by the InlineDlfcnMethods MSBuild
property:

  • strict: inlines all calls to ObjCRuntime.Dlfcn APIs.
  • compatibility: only inlines calls that reference symbols from [Field]
    attributes.

Post-trimming MSBuild targets ensure native code is only generated for symbols
that survive trimming (ILTrim or NativeAOT):

  • _CollectPostILTrimInformation: scans trimmed assemblies for surviving
    inlined dlfcn P/Invokes, with per-assembly caching for incremental builds.
  • _CollectPostNativeAOTTrimInformation: reads unresolved symbols from the
    NativeAOT object file or static library and filters for inlined dlfcn symbols.
  • _PostTrimmingProcessing: generates native C code for surviving symbols and
    adds it to the native link inputs.

Extends MachO.cs with LC_SYMTAB parsing to read unresolved symbols from both
Mach-O object files and static libraries.

Contributes towards #17693.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in build+linker pipeline to inline ObjCRuntime.Dlfcn symbol lookups into direct __Internal P/Invokes, with post-trimming symbol discovery (ILTrim + NativeAOT) and native C code generation to keep only surviving symbol references.

Changes:

  • Introduces InlineDlfcnMethodsStep + GenerateInlinedDlfcnNativeCodeStep and corresponding InlineDlfcnMethods MSBuild property wiring.
  • Adds post-trimming MSBuild tasks/targets to detect surviving inlined symbols (ILTrim scanning + NativeAOT unresolved-symbol parsing) and generate/compile native glue code.
  • Extends Mach-O parsing to read unresolved symbols and adds/adjusts tests and test variations for the new modes.

Reviewed changes

Copilot reviewed 22 out of 23 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tools/mtouch/Errors.resx Adds new MX2254–MX2257 warning strings for the inlining step.
tools/mtouch/Errors.designer.cs Regenerated resource accessors for new MX225x entries.
tools/linker/MonoTouch.Tuner/ProcessExportedFields.cs Collects [Field] symbol names for compatibility mode.
tools/linker/MonoTouch.Tuner/ListExportedSymbols.cs Skips adding required field symbols when dlfcn inlining is enabled.
tools/dotnet-linker/Steps/InlineDlfcnMethodsStep.cs New custom linker step that rewrites Dlfcn calls into generated helpers + P/Invokes.
tools/dotnet-linker/Steps/GenerateInlinedDlfcnNativeCodeStep.cs Emits a symbol list file for later post-trimming MSBuild steps.
tools/dotnet-linker/LinkerConfiguration.cs Adds InlineDlfcnMethods configuration and symbol tracking collections.
tools/dotnet-linker/AppBundleRewriter.cs Adds type/method references used by the new inlining step.
tools/common/MachO.cs Adds LC_SYMTAB parsing and unresolved-symbol extraction for Mach-O/static libraries.
tests/xharness/Jenkins/TestVariationsFactory.cs Adds new CI variations to exercise inline-dlfcn modes (incl. NativeAOT).
tests/monotouch-test/ObjCRuntime/DlfcnTest.cs Adds tests for [Field]-backed constants and adjusts behavior under strict mode.
tests/monotouch-test/Foundation/AttributedStringTest.cs Removes legacy field-absence test that would conflict with new behavior.
tests/monotouch-test/CoreFoundation/ProxyTest.cs Removes legacy field-absence test that would conflict with new behavior.
tests/common/test-variations.csproj Adds inline-dlfcn-methods-compat and inline-dlfcn-methods-strict test variations.
msbuild/Xamarin.MacDev.Tasks/Tasks/PostTrimmingProcessing.cs Generates native C glue for surviving symbols and outputs compile/link items.
msbuild/Xamarin.MacDev.Tasks/Tasks/ComputeNativeAOTSurvivingNativeSymbols.cs Filters NativeAOT unresolved symbols to determine surviving inlined-dlfcn symbols.
msbuild/Xamarin.MacDev.Tasks/Tasks/CompileNativeCode.cs Ensures output directory exists before compiling native files.
msbuild/Xamarin.MacDev.Tasks/Tasks/CollectUnresolvedNativeSymbols.cs New task to extract unresolved symbols from a NativeAOT static library.
msbuild/Xamarin.MacDev.Tasks/Tasks/CollectPostILTrimInformation.cs New task to scan trimmed assemblies and cache surviving inlined-dlfcn P/Invokes.
dotnet/targets/Xamarin.Shared.Sdk.targets Wires property defaults, custom linker steps, and post-trimming targets into the SDK.
docs/code/native-symbols.md New doc explaining the native symbol pipeline and InlineDlfcnMethods modes.
docs/building-apps/build-properties.md Documents the new InlineDlfcnMethods MSBuild property and defaults.
.github/copilot-instructions.md Adds MSBuild guidance (DeviceSpecificIntermediateOutputPath) to repo copilot instructions.
Files not reviewed (1)
  • tools/mtouch/Errors.designer.cs: Language not supported

You can also share your feedback on Copilot code review. Take the survey.

Comment thread tools/mtouch/Errors.resx Outdated
Comment thread tests/xharness/Jenkins/TestVariationsFactory.cs Outdated
Comment thread msbuild/Xamarin.MacDev.Tasks/Tasks/CollectUnresolvedNativeSymbols.cs Outdated
Comment thread msbuild/Xamarin.MacDev.Tasks/Tasks/CompileNativeCode.cs Outdated
Comment thread tools/dotnet-linker/Steps/InlineDlfcnMethodsStep.cs Outdated
Comment thread tools/dotnet-linker/Steps/InlineDlfcnMethodsStep.cs Outdated
Comment thread tools/dotnet-linker/Steps/InlineDlfcnMethodsStep.cs Outdated
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

1 similar comment
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

.NET for Apple Platforms PR Reviewer completed successfully!

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

This is a well-architected feature that adds build-time symbol resolution to replace runtime dlsym calls. The implementation demonstrates solid engineering with:

Excellent incremental build support with per-assembly caching
Correct thread-safety using volatile acquire/release semantics
Comprehensive error handling with detailed warnings for unsupported patterns
Clean separation between ILTrim and NativeAOT paths
Proper framework availability checks for simulator builds

Review Findings

I've left 2 inline comments with suggestions for defensive improvements:

  1. Symbol name validation (PostTrimmingProcessing.cs): Consider validating that symbol names are valid C identifiers before generating C code
  2. Documentation enhancement (InlineDlfcnMethodsStep.cs): The thread-safety implementation is correct, but additional comments would help future maintainers understand why the cached field doesn't need volatile

No Blocking Issues

The code is production-ready. The suggestions are defensive improvements rather than bug fixes. The memory model and threading aspects are correctly implemented.

Testing

The PR includes appropriate test coverage for the new functionality, including tests for string constants, and existing tests were updated where the optimization changes behavior (e.g., GeometryTest).

Great work on this optimization! 🚀

Generated by .NET for Apple Platforms PR Reviewer for issue #24888 · ● 1.4M

il.Append (il.Create (OpCodes.Brtrue, loadCachedFieldInstruction));

// Get__{symbolName}_Cached = xamarin_Dlfcn_{symbolName}_Native ();
il.Append (il.Create (OpCodes.Call, GetOrCreatePInvokeMethod (callingMethod, symbolName)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider clarifying thread-safety guarantees in comment

The current comment (lines 149-153) explains why the initialized field needs volatile semantics, which is correct. However, it might be helpful to also explain why the cached field write/read don't need to be volatile:

// The initialized field must use volatile semantics to prevent ARM64 store
// reordering: without it, another thread could see Initialized=true before
// the Cached value is written, and return a stale IntPtr.Zero.
// Note that we don't care if we call the P/Invoke more than once, that's
// a safe operation, we just don't want to call it every time.
//
// The cached field doesn't need volatile because:
// - The volatile write to Initialized (line 165) provides a release fence,
//   ensuring all prior stores (including this one) are visible.
// - The volatile read of Initialized (line 155) provides an acquire fence,
//   ensuring this thread sees all stores before the release.

This would make the correctness of the double-checked locking pattern more explicit for future maintainers.

Comment thread msbuild/Xamarin.MacDev.Tasks/Tasks/PostTrimmingProcessing.cs
rolfbjarne and others added 2 commits May 6, 2026 08:59
…cn namespace

The InlineDlfcnMethodsStep now checks for [Field] attributes on the calling
method (or its property), and if the attribute has a second string argument
(the library name), uses that as the namespace for the Dlfcn type instead of
the calling type's namespace.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #9a4cb3e] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 9a4cb3ea0629a91d1b27da3ff840a5866007e40b [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #9a4cb3e] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 9a4cb3ea0629a91d1b27da3ff840a5866007e40b [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 9a4cb3ea0629a91d1b27da3ff840a5866007e40b [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #9a4cb3e] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 9a4cb3ea0629a91d1b27da3ff840a5866007e40b [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build #9a4cb3e] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 175 tests passed 🎉

Tests counts

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ monotouch (iOS): All 16 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 16 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 9a4cb3ea0629a91d1b27da3ff840a5866007e40b [PR build]

@rolfbjarne

Copy link
Copy Markdown
Member Author

/review

@rolfbjarne
rolfbjarne enabled auto-merge (squash) May 7, 2026 05:53
@rolfbjarne

Copy link
Copy Markdown
Member Author

/review

@rolfbjarne
rolfbjarne disabled auto-merge May 7, 2026 06:00
@rolfbjarne
rolfbjarne merged commit b4a70d2 into main May 7, 2026
49 checks passed
@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

.NET for Apple Platforms PR Reviewer completed successfully!

Code review complete - PR already merged. High-quality implementation with comprehensive testing and documentation. No critical issues found, only minor suggestions for improvement.

@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

.NET for Apple Platforms PR Reviewer completed successfully!

Code review complete - PR #24888 is already merged with all CI checks passing. No blocking issues found. The implementation follows repository patterns for linker steps, MSBuild tasks, and post-trimming processing. The feature is well-documented and tested.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants