xamarin-macios/tools/linker/MobileApplyPreserveAttribut...

123 строки
3.8 KiB
C#
Исходник Обычный вид История

[dotnet-linker] Add ApplyPreserveAttribute into the pipeline. (#9529) This means: * Move the parts of the ApplyPreserveAttribute step that we don't need for.NET into a new MobileApplyPreserveAttribute step, and have mtouch and mmp use that step instead of the ApplyPreserveAttribute step. * Copy ApplyPreserveAttributeBase into dotnet-linker from the upstream tuner source (with minor modifications) so that our ApplyPreserveAttribute step compiles. * Create a DotNetSubStepDispatcher class that we're going to use as our substep dispatcher, create an instance of it and insert it into the list of linker steps. * Also a workaround for the lack of LinkContext.GetAssemblies (): add a step that collects all the assemblies and stores them in a list, so that we can have our own GetAssemblies implementation. I filed a linker issue to see if we can get LinkContext.GetAssemblies () exposed to us: https://github.com/mono/linker/issues/1455. Fixes this startup crash with the linkall test: 2020-08-26 19:47:10.219697+0200 link all[32709:6065783] Xamarin.iOS: Fatal error: failed to load the method 'ObjCRuntime.Runtime.Initialize'. ================================================================= Native Crash Reporting ================================================================= Got a abrt while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application. ================================================================= ================================================================= Native stacktrace: ================================================================= 0x104007b0e - /Users/rolf/Library/Developer/CoreSimulator/Devices/289E372A-501C-4499-A1A6-59C5B3B6A9AE/data/Containers/Bundle/Application/4A5F3968-6980-4E90-88A2-2E77AE039C40/link all.app/libmonosgen-2.0.dylib : mono_dump_native_crash_info 0x103fb4437 - /Users/rolf/Library/Developer/CoreSimulator/Devices/289E372A-501C-4499-A1A6-59C5B3B6A9AE/data/Containers/Bundle/Application/4A5F3968-6980-4E90-88A2-2E77AE039C40/link all.app/libmonosgen-2.0.dylib : mono_handle_native_crash 0x104007365 - /Users/rolf/Library/Developer/CoreSimulator/Devices/289E372A-501C-4499-A1A6-59C5B3B6A9AE/data/Containers/Bundle/Application/4A5F3968-6980-4E90-88A2-2E77AE039C40/link all.app/libmonosgen-2.0.dylib : sigabrt_signal_handler 0x7fff51c005fd - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.5.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_platform.dylib : _sigtramp 0x0 - Unknown 0x7fff51af0b7c - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.5.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_c.dylib : abort 0x103daad98 - /Users/rolf/Library/Developer/CoreSimulator/Devices/289E372A-501C-4499-A1A6-59C5B3B6A9AE/data/Containers/Bundle/Application/4A5F3968-6980-4E90-88A2-2E77AE039C40/link all.app/libxamarin-debug.dylib : xamarin_assertion_message 0x103dade77 - /Users/rolf/Library/Developer/CoreSimulator/Devices/289E372A-501C-4499-A1A6-59C5B3B6A9AE/data/Containers/Bundle/Application/4A5F3968-6980-4E90-88A2-2E77AE039C40/link all.app/libxamarin-debug.dylib : xamarin_initialize 0x103dbf80b - /Users/rolf/Library/Developer/CoreSimulator/Devices/289E372A-501C-4499-A1A6-59C5B3B6A9AE/data/Containers/Bundle/Application/4A5F3968-6980-4E90-88A2-2E77AE039C40/link all.app/libxamarin-debug.dylib : xamarin_main 0x103cd2f0d - /Users/rolf/Library/Developer/CoreSimulator/Devices/289E372A-501C-4499-A1A6-59C5B3B6A9AE/data/Containers/Bundle/Application/4A5F3968-6980-4E90-88A2-2E77AE039C40/link all.app/link all : main 0x7fff51a231fd - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.5.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libdyld.dylib : start This is a partial/modified port of the initial linker support (bc88790201) Co-authored-by: Sebastien Pouliot <sebastien@xamarin.com>
2020-08-27 19:30:19 +03:00
// Copyright 2011-2013 Xamarin Inc. All rights reserved.
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Linker;
using Mono.Tuner;
using Xamarin.Tuner;
namespace Xamarin.Linker.Steps {
public class MobileApplyPreserveAttribute : ApplyPreserveAttribute {
bool is_sdk;
DerivedLinkContext LinkContext {
get { return (DerivedLinkContext) base.context; }
}
// System.ServiceModel.dll is an SDK assembly but it does contain types with [DataMember] attributes
// just like System.Xml.dll contais [Xml*] attributes - we do not want to keep them unless the application
// shows the feature is being used
public override bool IsActiveFor (AssemblyDefinition assembly)
{
is_sdk = Profile.IsSdkAssembly (assembly);
return base.IsActiveFor (assembly);
}
protected override bool IsPreservedAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, out bool removeAttribute)
{
removeAttribute = false;
TypeReference type = attribute.Constructor.DeclaringType;
switch (type.Namespace) {
case "System.Runtime.Serialization":
bool srs = false;
// http://bugzilla.xamarin.com/show_bug.cgi?id=1415
// http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx
if (provider is PropertyDefinition || provider is FieldDefinition || provider is EventDefinition)
srs = (type.Name == "DataMemberAttribute");
else if (provider is TypeDefinition)
srs = (type.Name == "DataContractAttribute");
if (srs) {
MarkDefaultConstructor (provider, is_sdk ? LinkContext.DataContract : null);
return !is_sdk;
}
break;
case "System.Xml.Serialization":
// http://msdn.microsoft.com/en-us/library/83y7df3e.aspx
string name = type.Name;
if ((name.StartsWith ("Xml", StringComparison.Ordinal) && name.EndsWith ("Attribute", StringComparison.Ordinal))) {
// but we do not have to keep things that XML serialization will ignore anyway!
if (name != "XmlIgnoreAttribute") {
// the default constructor of the type *being used* is needed
MarkDefaultConstructor (provider, is_sdk ? LinkContext.XmlSerialization : null);
return !is_sdk;
}
}
break;
default:
return base.IsPreservedAttribute (provider, attribute, out removeAttribute);
}
// keep them (provider and attribute)
return false;
}
// xml serialization requires the default .ctor to be present
void MarkDefaultConstructor (ICustomAttributeProvider provider, IList<ICustomAttributeProvider> list)
{
if (list != null) {
list.Add (provider);
return;
}
TypeDefinition td = (provider as TypeDefinition);
if (td == null) {
PropertyDefinition pd = (provider as PropertyDefinition);
if (pd != null) {
MarkDefaultConstructor (pd.DeclaringType);
MarkGenericType (pd.PropertyType as GenericInstanceType);
td = pd.PropertyType.Resolve ();
} else {
FieldDefinition fd = (provider as FieldDefinition);
if (fd != null) {
MarkDefaultConstructor (fd.DeclaringType);
MarkGenericType (fd.FieldType as GenericInstanceType);
td = (fd.FieldType as TypeReference).Resolve ();
}
}
}
// e.g. <T> property (see bug #5543) or field (see linkall unit tests)
if (td != null)
MarkDefaultConstructor (td);
}
void MarkGenericType (GenericInstanceType git)
{
if (git == null || !git.HasGenericArguments)
return;
foreach (TypeReference tr in git.GenericArguments)
MarkDefaultConstructor (tr.Resolve ());
}
void MarkDefaultConstructor (TypeDefinition type)
{
if ((type == null) || !type.HasMethods)
return;
foreach (MethodDefinition ctor in type.Methods) {
if (!ctor.IsConstructor || ctor.IsStatic || ctor.HasParameters)
continue;
Annotations.AddPreservedMethod (type, ctor);
}
}
}
}