-
Notifications
You must be signed in to change notification settings - Fork 558
[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving block code. #24936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rolfbjarne
merged 1 commit into
main
from
dev/rolf/use-dynamic-dependency-attributes-preserveblockcode
Mar 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| using System; | ||
| using System.Linq; | ||
|
|
||
| using Mono.Cecil; | ||
|
|
||
| using Mono.Linker; | ||
| using Mono.Linker.Steps; | ||
| using Mono.Tuner; | ||
|
|
||
| using Xamarin.Bundler; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.Linker.Steps { | ||
|
|
||
| public class PreserveBlockCodeStep : AssemblyModifierStep { | ||
|
|
||
| protected override string Name { get; } = "Preserve Block Code"; | ||
| protected override int ErrorCode { get; } = 2240; | ||
|
|
||
| protected override bool IsActiveFor (AssemblyDefinition assembly) | ||
| { | ||
| // We only care about assemblies that are being linked. | ||
| if (Annotations.GetAction (assembly) != AssemblyAction.Link) | ||
| return false; | ||
|
|
||
| // Unless an assembly is or references our platform assembly, then it won't have anything we need to preserve | ||
| if (!Configuration.Profile.IsOrReferencesProductAssembly (assembly)) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| protected override bool ProcessType (TypeDefinition type) | ||
| { | ||
| if (!GetMembersToPreserve (type, out var field, out var method)) | ||
| return false; | ||
|
|
||
| var modified = false; | ||
| modified |= abr.AddDynamicDependencyAttributeToStaticConstructor (type, field); | ||
| modified |= abr.AddDynamicDependencyAttributeToStaticConstructor (type, method); | ||
| return modified; | ||
| } | ||
|
|
||
| public static bool GetMembersToPreserve (TypeDefinition type, [NotNullWhen (true)] out FieldDefinition? field, [NotNullWhen (true)] out MethodDefinition? method) | ||
| { | ||
| field = null; | ||
| method = null; | ||
|
|
||
| /* For the following class: | ||
|
|
||
| static internal class SDInnerBlock { | ||
| // this field is not preserved by other means, but it must not be linked away | ||
| static internal readonly DInnerBlock Handler = Invoke; | ||
|
|
||
| [MonoPInvokeCallback (typeof (DInnerBlock))] | ||
| static internal void Invoke (IntPtr block, int magic_number) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| * We need to make sure the linker doesn't remove the Handler field | ||
| * and the Invoke method. | ||
| */ | ||
|
|
||
| // First make sure we got the right class | ||
| // The type for the field we're looking for is abstract, sealed and nested and contains exactly 1 field. | ||
| if (!type.HasFields || !type.IsAbstract || !type.IsSealed || !type.IsNested) | ||
| return false; | ||
| if (type.Fields.Count != 1) | ||
| return false; | ||
|
|
||
| // The type is also nested inside ObjCRuntime.Trampolines class) | ||
| var nestingType = type.DeclaringType; | ||
| if (!nestingType.Is ("ObjCRuntime", "Trampolines")) | ||
| return false; | ||
|
|
||
| // The class has a readonly field named 'Handler' | ||
| field = type.Fields [0]; | ||
| if (!field.IsInitOnly) | ||
| return false; | ||
| if (field.Name != "Handler") | ||
| return false; | ||
|
|
||
| // The class has a parameterless 'Invoke' method with a 'MonoPInvokeCallback' attribute | ||
| if (!type.HasMethods) | ||
| return false; | ||
rolfbjarne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| method = type.Methods.SingleOrDefault (v => { | ||
| if (v.Name != "Invoke") | ||
| return false; | ||
| if (!v.HasParameters) | ||
| return false; | ||
| if (!v.HasCustomAttributes) | ||
| return false; | ||
| if (!v.CustomAttributes.Any (v => v.AttributeType.Name == "MonoPInvokeCallbackAttribute")) | ||
| return false; | ||
| return true; | ||
| }); | ||
|
|
||
| if (method is null) | ||
| return false; | ||
|
|
||
| // The type was used, so preserve the method and field | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.