Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions tests/linker/link all/LinkAllTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,6 @@ public void LinkedAwayGenericTypeAsOptionalMemberInProtocol ()
Assert.Ignore ("This test only applies to the trimmable static registrar.");

// https://github.com/dotnet/macios/issues/3523
// TEMP-IGNORE-25915: temporarily ignored on net11.0 until https://github.com/dotnet/macios/pull/25915 is merged.
Assert.Ignore ("Temporarily ignored on net11.0: the trimmable static registrar's TypeMap path preserves the full exported method surface of registered types (e.g. UIResponder.PressesBegan takes an NSSet<UIPress>), which keeps NSSet<T> alive. Fixed by https://github.com/dotnet/macios/pull/25915.");
// Don't use constants here, because the linker can see what we're trying to do and keeps the type we're verifying has been removed.
string prefix = NamespacePrefix;
string suffix = AssemblyName;
Expand Down
27 changes: 25 additions & 2 deletions tools/dotnet-linker/Steps/ManagedRegistrarStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void CreateUnmanagedCallersMethod (MethodDefinition method, AssemblyTrampolineIn
}

var callback = callbackType.AddMethod (name, MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig, placeholderType);
callback.CustomAttributes.Add (CreateUnmanagedCallersAttribute (name));
callback.CustomAttributes.Add (CreateUnmanagedCallersAttribute (name, method.DeclaringType));
infos.Add (new TrampolineInfo (callback, method, name));

// If the target method is marked, then we must mark the trampoline as well.
Expand Down Expand Up @@ -1269,11 +1269,34 @@ StaticRegistrar StaticRegistrar {
get { return DerivedLinkContext.StaticRegistrar; }
}

CustomAttribute CreateUnmanagedCallersAttribute (string entryPoint)
CustomAttribute CreateUnmanagedCallersAttribute (string entryPoint, TypeReference? associatedSourceType = null)
{
var unmanagedCallersAttribute = new CustomAttribute (abr.UnmanagedCallersOnlyAttribute_Constructor);

// The AssociatedSourceType field tells the NativeAOT compiler (ILC) that the trampoline's
// native export is only needed if the associated type is kept. This only works safely when
// all of these are true:
// * We're compiling for NativeAOT (only ILC understands the field; the CoreCLR runtime's
// attribute parser even rejects any UnmanagedCallersOnly named argument it doesn't know).
// * We're using the trimmable static registrar, whose native glue references the trampoline
// symbols directly. This means ILC may drop the native export for a trampoline whose
// associated type can't be constructed, while the generated native registrar code still
// references it - which would result in an undefined symbol at native link time.
// * We're preparing assemblies (PrepareAssemblies), which is the mode that has a post-ILC
// step (the _PostprocessAssembliesAfterIlc target) that regenerates the native registrar
// code after ILC, routing any trampoline that didn't survive ILC through the dlsym fallback
// instead of a direct native reference. Without that reconciliation step (i.e. in the
// non-prepare mode) we must not let ILC drop any trampoline export, so we don't emit the
// field there.
if (associatedSourceType is not null
&& App.XamarinRuntime == XamarinRuntime.NativeAOT
&& App.Registrar == RegistrarMode.TrimmableStatic
&& App.PrepareAssemblies)
unmanagedCallersAttribute.Fields.Add (new CustomAttributeNamedArgument ("AssociatedSourceType", new CustomAttributeArgument (abr.System_Type, associatedSourceType)));

if (App.XamarinRuntime != XamarinRuntime.CoreCLR)
unmanagedCallersAttribute.Fields.Add (new CustomAttributeNamedArgument ("EntryPoint", new CustomAttributeArgument (abr.System_String, entryPoint)));

return unmanagedCallersAttribute;
}

Expand Down
Loading