-
Notifications
You must be signed in to change notification settings - Fork 508
/
ListExportedSymbols.cs
206 lines (176 loc) · 6.33 KB
/
ListExportedSymbols.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;
using Mono.Tuner;
using Xamarin.Bundler;
using Xamarin.Linker;
using Xamarin.Tuner;
using Xamarin.Utils;
namespace Xamarin.Linker.Steps
{
public class ListExportedSymbols : BaseStep
{
PInvokeWrapperGenerator state;
bool skip_sdk_assemblies;
public DerivedLinkContext DerivedLinkContext {
get {
#if NET
return LinkerConfiguration.GetInstance (Context).DerivedLinkContext;
#else
return (DerivedLinkContext) Context;
#endif
}
}
public ListExportedSymbols () : this (null)
{
}
internal ListExportedSymbols (PInvokeWrapperGenerator state, bool skip_sdk_assemblies = false)
{
this.state = state;
this.skip_sdk_assemblies = skip_sdk_assemblies;
}
protected override void ProcessAssembly (AssemblyDefinition assembly)
{
base.ProcessAssembly (assembly);
if (Annotations.GetAction (assembly) == AssemblyAction.Delete)
return;
#if !NET
if (skip_sdk_assemblies && Profile.IsSdkAssembly (assembly))
return;
#endif
if (!assembly.MainModule.HasTypes)
return;
var hasSymbols = false;
if (assembly.MainModule.HasModuleReferences) {
hasSymbols = true;
} else if (assembly.MainModule.HasTypeReference (Namespaces.Foundation + ".FieldAttribute")) {
hasSymbols = true;
}
if (!hasSymbols)
return;
foreach (var type in assembly.MainModule.Types)
ProcessType (type);
}
void ProcessType (TypeDefinition type)
{
if (type.HasNestedTypes) {
foreach (var nested in type.NestedTypes)
ProcessType (nested);
}
if (type.HasMethods) {
foreach (var method in type.Methods)
ProcessMethod (method);
}
AddRequiredObjectiveCType (type);
}
void AddRequiredObjectiveCType (TypeDefinition type)
{
var registerAttribute = DerivedLinkContext.StaticRegistrar?.GetRegisterAttribute (type);
if (registerAttribute == null)
return;
if (!registerAttribute.IsWrapper)
return;
if (DerivedLinkContext.StaticRegistrar.HasProtocolAttribute (type))
return;
Assembly asm;
bool has_linkwith_attributes = false;
if (DerivedLinkContext.Target.Assemblies.TryGetValue (type.Module.Assembly, out asm))
has_linkwith_attributes = asm.HasLinkWithAttributes;
if (has_linkwith_attributes) {
var exportedName = DerivedLinkContext.StaticRegistrar.GetExportedTypeName (type, registerAttribute);
DerivedLinkContext.RequiredSymbols.AddObjectiveCClass (exportedName).AddMember (type);
}
}
void ProcessMethod (MethodDefinition method)
{
if (method.IsPInvokeImpl && method.HasPInvokeInfo && method.PInvokeInfo != null) {
var pinfo = method.PInvokeInfo;
bool addPInvokeSymbol = false;
if (state != null) {
switch (pinfo.EntryPoint) {
case "objc_msgSend":
case "objc_msgSendSuper":
case "objc_msgSend_stret":
case "objc_msgSendSuper_stret":
case "objc_msgSend_fpret":
state.ProcessMethod (method);
break;
default:
break;
}
}
switch (pinfo.Module.Name) {
case "__Internal":
Driver.Log (4, "Adding native reference to {0} in {1} because it's referenced by {2} in {3}.", pinfo.EntryPoint, pinfo.Module.Name, method.FullName, method.Module.Name);
DerivedLinkContext.RequiredSymbols.AddFunction (pinfo.EntryPoint).AddMember (method);
break;
case "libSystem.Net.Security.Native":
case "System.Net.Security.Native":
#if NET
// tvOS does not ship with System.Net.Security.Native due to https://github.com/dotnet/runtime/issues/45535
if (DerivedLinkContext.App.Platform == ApplePlatform.TVOS) {
Driver.Log (4, "Did not add native reference to {0} in {1} referenced by {2} in {3}.", pinfo.EntryPoint, pinfo.Module.Name, method.FullName, method.Module.Name);
break;
}
#endif
addPInvokeSymbol = true;
break;
case "libSystem.Security.Cryptography.Native.Apple":
case "System.Security.Cryptography.Native.Apple":
#if NET
// https://github.com/dotnet/runtime/issues/47533
if (DerivedLinkContext.App.Platform != ApplePlatform.MacOSX) {
Driver.Log (4, "Did not add native reference to {0} in {1} referenced by {2} in {3}.", pinfo.EntryPoint, pinfo.Module.Name, method.FullName, method.Module.Name);
break;
}
#endif
addPInvokeSymbol = true;
break;
case "libSystem.Native":
case "System.Native":
#if NET
// https://github.com/dotnet/runtime/issues/47533
if (DerivedLinkContext.App.Platform != ApplePlatform.MacOSX && pinfo.EntryPoint == "SystemNative_ConfigureTerminalForChildProcess") {
Driver.Log (4, "Did not add native reference to {0} in {1} referenced by {2} in {3}.", pinfo.EntryPoint, pinfo.Module.Name, method.FullName, method.Module.Name);
break;
}
#endif
addPInvokeSymbol = true;
break;
case "libSystem.Globalization.Native":
case "System.Globalization.Native":
#if NET
// https://github.com/xamarin/xamarin-macios/issues/11392
if (DerivedLinkContext.App.Platform == ApplePlatform.MacCatalyst) {
Driver.Log (4, "Did not add native reference to {0} in {1} referenced by {2} in {3}.", pinfo.EntryPoint, pinfo.Module.Name, method.FullName, method.Module.Name);
break;
}
#endif
addPInvokeSymbol = true;
break;
default:
Driver.Log (4, "Did not add native reference to {0} in {1} referenced by {2} in {3}.", pinfo.EntryPoint, pinfo.Module.Name, method.FullName, method.Module.Name);
break;
}
if (addPInvokeSymbol) {
Driver.Log (4, "Adding native reference to {0} in {1} because it's referenced by {2} in {3}.", pinfo.EntryPoint, pinfo.Module.Name, method.FullName, method.Module.Name);
DerivedLinkContext.RequireMonoNative = true;
if (DerivedLinkContext.App.Platform != ApplePlatform.MacOSX &&
DerivedLinkContext.App.LibMonoNativeLinkMode == AssemblyBuildTarget.StaticObject) {
DerivedLinkContext.RequiredSymbols.AddFunction (pinfo.EntryPoint).AddMember (method);
}
}
}
if (method.IsPropertyMethod ()) {
var property = method.GetProperty ();
object symbol;
// The Field attribute may have been linked away, but we've stored it in an annotation.
if (property != null && Annotations.GetCustomAnnotations ("ExportedFields").TryGetValue (property, out symbol)) {
DerivedLinkContext.RequiredSymbols.AddField ((string) symbol).AddMember (property);
}
}
}
}
}