Skip to content

Expose extension points needed by UnityLinker. #90688

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
Merged
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
36 changes: 30 additions & 6 deletions src/tools/illink/src/linker/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,12 @@
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.AnnotationStore.get_TypeMapInfo</Target>
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.AnnotationStore.GetAction(Mono.Cecil.MethodDefinition)</Target>
Expand Down Expand Up @@ -1255,6 +1261,12 @@
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.#ctor(Mono.Linker.Pipeline,Mono.Linker.ILogger,System.String,Mono.Linker.UnintializedContextFactory)</Target>
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.#ctor(Mono.Linker.Pipeline,Mono.Linker.ILogger,System.String)</Target>
Expand Down Expand Up @@ -1345,6 +1357,12 @@
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.get_EmbeddedXmlInfo</Target>
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.get_EnableReducedTracing</Target>
Expand Down Expand Up @@ -1405,6 +1423,12 @@
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.get_KeepMembersForDebugger</Target>
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.get_KeepUsedAttributeTypesOnly</Target>
Expand Down Expand Up @@ -1524,12 +1548,6 @@
<Target>M:Mono.Linker.LinkContext.get_Tracer</Target>
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.get_EmbeddedXmlInfo</Target>
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
Expand Down Expand Up @@ -1855,6 +1873,12 @@
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.set_KeepMembersForDebugger(System.Boolean)</Target>
<Left>ref/net8.0/illink.dll</Left>
<Right>lib/net8.0/illink.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Mono.Linker.LinkContext.set_KeepUsedAttributeTypesOnly(System.Boolean)</Target>
Expand Down
38 changes: 19 additions & 19 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public virtual void Process (LinkContext context)
Complete ();
}

void Initialize ()
protected virtual void Initialize ()
{
InitializeCorelibAttributeXml ();
Context.Pipeline.InitializeMarkHandlers (Context, MarkContext);
Expand Down Expand Up @@ -282,7 +282,7 @@ void InitializeCorelibAttributeXml ()
Context.CustomAttributes.PrimaryAttributeInfo.CustomAttributesOrigins.Add (ca, origin);
}

void Complete ()
protected virtual void Complete ()
{
foreach ((var body, var _) in _unreachableBodies) {
Annotations.SetAction (body.Method, MethodAction.ConvertToThrow);
Expand Down Expand Up @@ -1395,7 +1395,7 @@ protected bool CheckProcessed (IMetadataTokenProvider provider)
return !Annotations.SetProcessed (provider);
}

protected void MarkAssembly (AssemblyDefinition assembly, DependencyInfo reason)
protected virtual void MarkAssembly (AssemblyDefinition assembly, DependencyInfo reason)
{
Annotations.Mark (assembly, reason, ScopeStack.CurrentScope.Origin);
if (CheckProcessed (assembly))
Expand Down Expand Up @@ -2439,28 +2439,28 @@ void MarkInterfaceImplementations (TypeDefinition type)
if (ShouldMarkInterfaceImplementation (type, iface))
MarkInterfaceImplementation (iface, new MessageOrigin (type));
}
}

bool ShouldMarkInterfaceImplementation (TypeDefinition type, InterfaceImplementation iface)
{
if (Annotations.IsMarked (iface))
return false;
protected virtual bool ShouldMarkInterfaceImplementation (TypeDefinition type, InterfaceImplementation iface)
{
if (Annotations.IsMarked (iface))
return false;

if (!Context.IsOptimizationEnabled (CodeOptimizations.UnusedInterfaces, type))
return true;
if (!Context.IsOptimizationEnabled (CodeOptimizations.UnusedInterfaces, type))
return true;

if (Context.Resolve (iface.InterfaceType) is not TypeDefinition resolvedInterfaceType)
return false;
if (Context.Resolve (iface.InterfaceType) is not TypeDefinition resolvedInterfaceType)
return false;

if (Annotations.IsMarked (resolvedInterfaceType))
return true;
if (Annotations.IsMarked (resolvedInterfaceType))
return true;

// It's hard to know if a com or windows runtime interface will be needed from managed code alone,
// so as a precaution we will mark these interfaces once the type is instantiated
if (resolvedInterfaceType.IsImport || resolvedInterfaceType.IsWindowsRuntime)
return true;
// It's hard to know if a com or windows runtime interface will be needed from managed code alone,
// so as a precaution we will mark these interfaces once the type is instantiated
if (resolvedInterfaceType.IsImport || resolvedInterfaceType.IsWindowsRuntime)
return true;

return IsFullyPreserved (type);
}
return IsFullyPreserved (type);
}

void MarkGenericParameterProvider (IGenericParameterProvider provider)
Expand Down
2 changes: 1 addition & 1 deletion src/tools/illink/src/linker/Linker/Annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected Tracer Tracer {

internal HashSet<MethodDefinition> VirtualMethodsWithAnnotationsToValidate { get; }

TypeMapInfo TypeMapInfo { get; }
public TypeMapInfo TypeMapInfo { get; }

public MemberActionStore MemberActions { get; }

Expand Down
13 changes: 6 additions & 7 deletions src/tools/illink/src/linker/Linker/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ public class AssemblyResolver : IAssemblyResolver
HashSet<string>? _unresolvedAssemblies;
HashSet<string>? _reportedUnresolvedAssemblies;

public AssemblyResolver (LinkContext context)
public AssemblyResolver (LinkContext context, ReaderParameters readerParameters)
{
readerParameters.AssemblyResolver = this;
_context = context;
_defaultReaderParameters = new ReaderParameters () {
AssemblyResolver = this
};
_defaultReaderParameters = readerParameters;
}

public IDictionary<string, AssemblyDefinition> AssemblyCache { get; } = new Dictionary<string, AssemblyDefinition> (StringComparer.OrdinalIgnoreCase);
Expand Down Expand Up @@ -130,7 +129,7 @@ void ReportUnresolvedAssembly (AssemblyNameReference reference)
_context.LogError (null, DiagnosticId.CouldNotFindAssemblyReference, reference.Name);
}

public void AddSearchDirectory (string directory)
public virtual void AddSearchDirectory (string directory)
{
_directories.Add (directory);
}
Expand Down Expand Up @@ -178,7 +177,7 @@ AssemblyDefinition IAssemblyResolver.Resolve (AssemblyNameReference name, Reader
throw new NotSupportedException ();
}

static readonly string[] Extensions = new[] { ".dll", ".exe" };
static readonly string[] Extensions = new[] { ".dll", ".exe", ".winmd" };

AssemblyDefinition? SearchDirectory (AssemblyNameReference name)
{
Expand All @@ -198,7 +197,7 @@ AssemblyDefinition IAssemblyResolver.Resolve (AssemblyNameReference name, Reader
return null;
}

public void CacheAssembly (AssemblyDefinition assembly)
public virtual void CacheAssembly (AssemblyDefinition assembly)
{
if (AssemblyCache.TryGetValue (assembly.Name.Name, out var existing)) {
if (existing != assembly)
Expand Down
11 changes: 8 additions & 3 deletions src/tools/illink/src/linker/Linker/LinkContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class UnintializedContextFactory
public virtual MarkingHelpers CreateMarkingHelpers (LinkContext context) => new MarkingHelpers (context);
public virtual Tracer CreateTracer (LinkContext context) => new Tracer (context);
public virtual EmbeddedXmlInfo CreateEmbeddedXmlInfo () => new ();
public virtual AssemblyResolver CreateResolver (LinkContext context) => new AssemblyResolver (context, new ReaderParameters ());
}

public static class TargetRuntimeVersion
Expand Down Expand Up @@ -107,7 +108,7 @@ public Pipeline Pipeline {

public bool LinkSymbols { get; set; }

public readonly bool KeepMembersForDebugger = true;
public bool KeepMembersForDebugger { get; set; } = true;

public bool IgnoreUnresolved { get; set; }

Expand Down Expand Up @@ -194,11 +195,16 @@ internal TypeNameResolver TypeNameResolver {
public SerializationMarker SerializationMarker { get; }

public LinkContext (Pipeline pipeline, ILogger logger, string outputDirectory)
: this(pipeline, logger, outputDirectory, new UnintializedContextFactory ())
{
}

protected LinkContext (Pipeline pipeline, ILogger logger, string outputDirectory, UnintializedContextFactory factory)
{
_pipeline = pipeline;
_logger = logger ?? throw new ArgumentNullException (nameof (logger));

_resolver = new AssemblyResolver (this);
_resolver = factory.CreateResolver (this);
_typeNameResolver = new TypeNameResolver (this);
_actions = new Dictionary<string, AssemblyAction> ();
_parameters = new Dictionary<string, string> (StringComparer.Ordinal);
Expand All @@ -211,7 +217,6 @@ public LinkContext (Pipeline pipeline, ILogger logger, string outputDirectory)

SymbolReaderProvider = new DefaultSymbolReaderProvider (false);

var factory = new UnintializedContextFactory ();
_annotations = factory.CreateAnnotationStore (this);
MarkingHelpers = factory.CreateMarkingHelpers (this);
SerializationMarker = new SerializationMarker (this);
Expand Down
4 changes: 3 additions & 1 deletion src/tools/illink/src/linker/Linker/TypeMapInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public TypeMapInfo (LinkContext context)
this.context = context;
}

void EnsureProcessed (AssemblyDefinition assembly)
public void EnsureProcessed (AssemblyDefinition assembly)
{
if (!assemblies.Add (assembly))
return;
Expand All @@ -58,6 +58,8 @@ void EnsureProcessed (AssemblyDefinition assembly)
MapType (type);
}

public ICollection<MethodDefinition> MethodsWithOverrideInformation => override_methods.Keys;

/// <summary>
/// Returns a list of all known methods that override <paramref name="method"/>. The list may be incomplete if other overrides exist in assemblies that haven't been processed by TypeMapInfo yet
/// </summary>
Expand Down