Skip to content

Commit 81d2c3a

Browse files
committed
Hide almost all the exported symbols
1 parent 18f1d14 commit 81d2c3a

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/GetNativeRuntimeComponents.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,16 @@ void MakeArchiveItem (NativeRuntimeComponents.Archive archive, List<ITaskItem> a
9090
{
9191
foreach (ITaskItem resolvedArchive in ResolvedNativeArchives) {
9292
string fileName = Path.GetFileName (resolvedArchive.ItemSpec);
93-
if (String.Compare (fileName, archive.Name, StringComparison.OrdinalIgnoreCase) == 0) {
94-
ITaskItem newItem = DoMakeItem ("_ResolvedNativeArchive", resolvedArchive, uniqueAbis);
95-
newItem.SetMetadata (KnownMetadata.LinkWholeArchive, archive.WholeArchive.ToString ());
96-
archives.Add (newItem);
93+
if (String.Compare (fileName, archive.Name, StringComparison.OrdinalIgnoreCase) != 0) {
94+
continue;
95+
}
96+
97+
ITaskItem newItem = DoMakeItem ("_ResolvedNativeArchive", resolvedArchive, uniqueAbis);
98+
newItem.SetMetadata (KnownMetadata.NativeLinkWholeArchive, archive.WholeArchive.ToString ());
99+
if (archive.DontExportSymbols) {
100+
newItem.SetMetadata (KnownMetadata.NativeDontExportSymbols, "true");
97101
}
102+
archives.Add (newItem);
98103
}
99104
}
100105

src/Xamarin.Android.Build.Tasks/Utilities/KnownMetadata.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Xamarin.Android.Tasks;
33
static class KnownMetadata
44
{
55
public const string Abi = "Abi";
6-
public const string LinkWholeArchive = "LinkWholeArchive";
6+
public const string NativeLinkWholeArchive = "LinkWholeArchive";
77
public const string RuntimeIdentifier = "RuntimeIdentifier";
8+
public const string NativeDontExportSymbols = "DontExportSymbols";
89
}

src/Xamarin.Android.Build.Tasks/Utilities/NativeLinker.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,31 +114,36 @@ public bool Link (ITaskItem outputLibraryPath, List<ITaskItem> objectFiles, List
114114

115115
string libBaseName = Path.GetFileNameWithoutExtension (outputLibraryPath.ItemSpec);
116116
string respFilePath = Path.Combine (intermediateDir, $"ld.{libBaseName}.{abi}.rsp");
117-
using (var sw = new StreamWriter (File.Open (respFilePath, FileMode.Create, FileAccess.Write, FileShare.Read), new UTF8Encoding (false))) {
118-
foreach (string arg in standardArgs) {
119-
sw.WriteLine (arg);
120-
}
117+
using var sw = new StreamWriter (File.Open (respFilePath, FileMode.Create, FileAccess.Write, FileShare.Read), new UTF8Encoding (false));
118+
foreach (string arg in standardArgs) {
119+
sw.WriteLine (arg);
120+
}
121121

122-
foreach (string arg in extraArgs) {
123-
sw.WriteLine (arg);
124-
}
122+
foreach (string arg in extraArgs) {
123+
sw.WriteLine (arg);
124+
}
125125

126-
if (StripDebugSymbols && !SaveDebugSymbols) {
127-
sw.WriteLine ("-s");
128-
}
126+
if (StripDebugSymbols && !SaveDebugSymbols) {
127+
sw.WriteLine ("-s");
128+
}
129129

130-
WriteFilesToResponseFile (sw, linkStartFiles);
131-
WriteFilesToResponseFile (sw, objectFiles);
132-
WriteFilesToResponseFile (sw, archives);
130+
var excludeExportsLibs = new List<string> ();
131+
WriteFilesToResponseFile (sw, linkStartFiles);
132+
WriteFilesToResponseFile (sw, objectFiles);
133+
WriteFilesToResponseFile (sw, archives);
133134

134-
foreach (ITaskItem libItem in libraries) {
135-
sw.WriteLine ($"-l{libItem.ItemSpec}");
136-
}
135+
if (excludeExportsLibs.Count > 0) {
136+
string libs = String.Join (",", excludeExportsLibs);
137+
sw.WriteLine ($"--exclude-libs={libs}");
138+
}
137139

138-
WriteFilesToResponseFile (sw, linkEndFiles);
139-
sw.Flush ();
140+
foreach (ITaskItem libItem in libraries) {
141+
sw.WriteLine ($"-l{libItem.ItemSpec}");
140142
}
141143

144+
WriteFilesToResponseFile (sw, linkEndFiles);
145+
sw.Flush ();
146+
142147
var ldArgs = new List<string> {
143148
$"@{respFilePath}",
144149
"-o",
@@ -163,6 +168,10 @@ void WriteFilesToResponseFile (StreamWriter sw, List<ITaskItem> files)
163168
foreach (ITaskItem file in files) {
164169
bool wholeArchive = IncludeWholeArchive (file);
165170

171+
if (ExcludeFromExports (file)) {
172+
excludeExportsLibs.Add (Path.GetFileName (file.ItemSpec));
173+
}
174+
166175
if (wholeArchive) {
167176
sw.Write ("--whole-archive ");
168177
}
@@ -178,15 +187,18 @@ void WriteFilesToResponseFile (StreamWriter sw, List<ITaskItem> files)
178187
}
179188
}
180189

181-
bool IncludeWholeArchive (ITaskItem item)
190+
bool IncludeWholeArchive (ITaskItem item) => ParseBooleanMetadata (item, KnownMetadata.NativeLinkWholeArchive);
191+
bool ExcludeFromExports (ITaskItem item) => ParseBooleanMetadata (item, KnownMetadata.NativeDontExportSymbols);
192+
193+
bool ParseBooleanMetadata (ITaskItem item, string metadata)
182194
{
183-
string? wholeArchive = item.GetMetadata (KnownMetadata.LinkWholeArchive);
184-
if (String.IsNullOrEmpty (wholeArchive)) {
195+
string? value = item.GetMetadata (metadata);
196+
if (String.IsNullOrEmpty (value)) {
185197
return false;
186198
}
187199

188200
// Purposefully not calling TryParse, let it throw and let us know if the value isn't a boolean.
189-
return Boolean.Parse (wholeArchive);
201+
return Boolean.Parse (value);
190202
}
191203
}
192204

src/Xamarin.Android.Build.Tasks/Utilities/NativeRuntimeComponents.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal class Archive
1212
public readonly string Name;
1313
public bool Include => shouldInclude (this);
1414
public readonly bool WholeArchive;
15+
public bool DontExportSymbols { get; set; }
1516

1617
Func<Archive, bool> shouldInclude;
1718

@@ -31,6 +32,7 @@ public MonoComponentArchive (string name, string componentName, Func<Archive, bo
3132
: base (name, include)
3233
{
3334
ComponentName = componentName;
35+
DontExportSymbols = true;
3436
}
3537
}
3638

@@ -52,7 +54,9 @@ sealed class BclArchive : Archive
5254
{
5355
public BclArchive (string name, bool wholeArchive = false)
5456
: base (name, wholeArchive: wholeArchive)
55-
{}
57+
{
58+
DontExportSymbols = true;
59+
}
5660
}
5761

5862
readonly ITaskItem[] monoComponents;
@@ -77,7 +81,9 @@ public NativeRuntimeComponents (ITaskItem[] monoComponents)
7781
new MonoComponentArchive ("libmono-component-marshal-ilgen-stub-static.a", "marshal-ilgen", IncludeIfMonoComponentAbsent),
7882

7983
// MonoVM runtime + BCL
80-
new Archive ("libmonosgen-2.0.a"),
84+
new Archive ("libmonosgen-2.0.a") {
85+
DontExportSymbols = true,
86+
},
8187
new BclArchive ("libSystem.Globalization.Native.a"),
8288
new BclArchive ("libSystem.IO.Compression.Native.a"),
8389
new BclArchive ("libSystem.Native.a"),

0 commit comments

Comments
 (0)