[tools/msbuild] Add InlineDlfcnMethodsStep as an opt-in custom linker step#24888
[tools/msbuild] Add InlineDlfcnMethodsStep as an opt-in custom linker step#24888rolfbjarne wants to merge 2 commits intomainfrom
Conversation
There was a problem hiding this comment.
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+GenerateInlinedDlfcnNativeCodeStepand correspondingInlineDlfcnMethodsMSBuild 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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
7a40438 to
ee828e6
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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 (even though this adds a *new* custom trimmer step, it's a pre-marking step that's easy to move out of the linker later on, and it's a partial replacement for the ListExportedSymbols step, which can't be moved out of the trimmer).
3f494fb to
b36cd77
Compare
✅ [CI Build #b36cd77] Build passed (Build packages) ✅Pipeline on Agent |
✅ [PR Build #b36cd77] Build passed (Detect API changes) ✅Pipeline on Agent |
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
✅ [CI Build #b36cd77] Build passed (Build macOS tests) ✅Pipeline on Agent |
🔥 [CI Build #b36cd77] Test results 🔥Test results❌ Tests failed on VSTS: test results 1 tests crashed, 7 tests failed, 154 tests passed. Failures❌ cecil tests🔥 Failed catastrophically on VSTS: test results - cecil (no summary found). Html Report (VSDrops) Download ❌ monotouch tests (iOS)3 tests failed, 11 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (macOS)1 tests failed, 11 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (tvOS)3 tests failed, 11 tests passed.Failed tests
Html Report (VSDrops) Download Successes✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download macOS tests✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
Add a new custom linker step that replaces runtime
Dlfcn.dlsymcalls withdirect native symbol lookups via P/Invokes to
__Internal. This improvesstartup 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
InlineDlfcnMethodsMSBuildproperty:
strict: inlines all calls toObjCRuntime.DlfcnAPIs.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 survivinginlined dlfcn P/Invokes, with per-assembly caching for incremental builds.
_CollectPostNativeAOTTrimInformation: reads unresolved symbols from theNativeAOT object file or static library and filters for inlined dlfcn symbols.
_PostTrimmingProcessing: generates native C code for surviving symbols andadds it to the native link inputs.
Extends
MachO.cswith LC_SYMTAB parsing to read unresolved symbols from bothMach-O object files and static libraries.
Contributes towards #17693.