Skip to content

Commit 354ec46

Browse files
Allow hiding UnmanagedCallersOnly exports (#110152)
Should fix #109341. We currently rely on the linkerscript not exporting these, now it's a per-assembly compiler switch.
1 parent 89adcf9 commit 354ec46

File tree

11 files changed

+35
-24
lines changed

11 files changed

+35
-24
lines changed

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ The .NET Foundation licenses this file to you under the MIT license.
102102
</PropertyGroup>
103103

104104
<ItemGroup Condition="$(IlcSystemModule) == ''">
105-
<UnmanagedEntryPointsAssembly Include="System.Private.CoreLib" />
105+
<UnmanagedEntryPointsAssembly Include="System.Private.CoreLib,HIDDEN" />
106106
<AutoInitializedAssemblies Include="System.Private.CoreLib" />
107107
<AutoInitializedAssemblies Include="System.Private.StackTraceMetadata" Condition="$(StackTraceSupport) != 'false'" />
108108
<AutoInitializedAssemblies Include="System.Private.TypeLoader" />

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,12 +1423,16 @@ public StructMarshallingDataNode StructMarshallingData(DefType type)
14231423
/// Returns alternative symbol name that object writer should produce for given symbols
14241424
/// in addition to the regular one.
14251425
/// </summary>
1426-
public string GetSymbolAlternateName(ISymbolNode node)
1426+
public string GetSymbolAlternateName(ISymbolNode node, out bool isHidden)
14271427
{
1428-
string value;
1429-
if (!NodeAliases.TryGetValue(node, out value))
1428+
if (!NodeAliases.TryGetValue(node, out var value))
1429+
{
1430+
isHidden = false;
14301431
return null;
1431-
return value;
1432+
}
1433+
1434+
isHidden = value.Hidden;
1435+
return value.Name;
14321436
}
14331437

14341438
public ArrayOfEmbeddedPointersNode<GCStaticsNode> GCStaticsRegion = new ArrayOfEmbeddedPointersNode<GCStaticsNode>(
@@ -1451,7 +1455,7 @@ public string GetSymbolAlternateName(ISymbolNode node)
14511455

14521456
public ReadyToRunHeaderNode ReadyToRunHeader;
14531457

1454-
public Dictionary<ISymbolNode, string> NodeAliases = new Dictionary<ISymbolNode, string>();
1458+
public Dictionary<ISymbolNode, (string Name, bool Hidden)> NodeAliases = new Dictionary<ISymbolNode, (string, bool)>();
14551459

14561460
protected internal TypeManagerIndirectionNode TypeManagerIndirection = new TypeManagerIndirectionNode();
14571461

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExpectedIsaFeaturesRootProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void ICompilationRootProvider.AddCompilationRoots(IRootingServiceProvider rootPr
2424
{
2525
int isaFlags = HardwareIntrinsicHelpers.GetRuntimeRequiredIsaFlags(_isaSupport);
2626
byte[] bytes = BitConverter.GetBytes(isaFlags);
27-
rootProvider.RootReadOnlyDataBlob(bytes, 4, "ISA support flags", "g_requiredCpuFeatures");
27+
rootProvider.RootReadOnlyDataBlob(bytes, 4, "ISA support flags", "g_requiredCpuFeatures", exportHidden: true);
2828
}
2929
}
3030
}

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ExportsFileWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ExportsFileWriter(TypeSystemContext context, string exportsFile, string[]
2626
}
2727

2828
public void AddExportedMethods(IEnumerable<EcmaMethod> methods)
29-
=> _methods.AddRange(methods.Where(m => m.Module != _context.SystemModule));
29+
=> _methods.AddRange(methods);
3030

3131
public void EmitExportedMethods()
3232
{

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/IRootingServiceProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ILCompiler
1010
/// </summary>
1111
public interface IRootingServiceProvider
1212
{
13-
void AddCompilationRoot(MethodDesc method, string reason, string exportName = null);
13+
void AddCompilationRoot(MethodDesc method, string reason, string exportName = null, bool exportHidden = false);
1414
void AddCompilationRoot(TypeDesc type, string reason);
1515
void AddReflectionRoot(TypeDesc type, string reason);
1616
void AddReflectionRoot(MethodDesc method, string reason);
@@ -19,7 +19,7 @@ public interface IRootingServiceProvider
1919
void RootGCStaticBaseForType(TypeDesc type, string reason);
2020
void RootNonGCStaticBaseForType(TypeDesc type, string reason);
2121
void RootModuleMetadata(ModuleDesc module, string reason);
22-
void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName);
22+
void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName, bool exportHidden);
2323
void RootDelegateMarshallingData(DefType type, string reason);
2424
void RootStructMarshallingData(DefType type, string reason);
2525
void AddCompilationRoot(object o, string reason);

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectDataInterner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private void EnsureMap(NodeFactory factory)
3636

3737
// Bodies that are visible from outside should not be folded because we don't know
3838
// if they're address taken.
39-
if (factory.GetSymbolAlternateName(body) != null)
39+
if (factory.GetSymbolAlternateName(body, out _) != null)
4040
continue;
4141

4242
var key = new MethodInternKey(body, factory);

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/ObjectWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,14 @@ private void EmitObject(string objectFilePath, IReadOnlyCollection<DependencyNod
422422
n == node ? currentSymbolName : GetMangledName(n),
423423
n.Offset + thumbBit,
424424
n.Offset == 0 && isMethod ? nodeContents.Data.Length : 0);
425-
if (_nodeFactory.GetSymbolAlternateName(n) is string alternateName)
425+
if (_nodeFactory.GetSymbolAlternateName(n, out bool isHidden) is string alternateName)
426426
{
427427
string alternateCName = ExternCName(alternateName);
428428
sectionWriter.EmitSymbolDefinition(
429429
alternateCName,
430430
n.Offset + thumbBit,
431431
n.Offset == 0 && isMethod ? nodeContents.Data.Length : 0,
432-
global: true);
432+
global: !isHidden);
433433

434434
if (n is IMethodNode)
435435
{

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/RootingServiceProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public RootingServiceProvider(NodeFactory factory, RootAdder rootAdder)
2323
_rootAdder = rootAdder;
2424
}
2525

26-
public void AddCompilationRoot(MethodDesc method, string reason, string exportName = null)
26+
public void AddCompilationRoot(MethodDesc method, string reason, string exportName = null, bool exportHidden = false)
2727
{
2828
MethodDesc canonMethod = method.GetCanonMethodTarget(CanonicalFormKind.Specific);
2929
IMethodNode methodEntryPoint = _factory.MethodEntrypoint(canonMethod);
@@ -32,7 +32,7 @@ public void AddCompilationRoot(MethodDesc method, string reason, string exportNa
3232
if (exportName != null)
3333
{
3434
exportName = _factory.NameMangler.NodeMangler.ExternMethod(exportName, method);
35-
_factory.NodeAliases.Add(methodEntryPoint, exportName);
35+
_factory.NodeAliases.Add(methodEntryPoint, (exportName, exportHidden));
3636
}
3737

3838
if (canonMethod != method && method.HasInstantiation)
@@ -140,12 +140,12 @@ public void RootModuleMetadata(ModuleDesc module, string reason)
140140
}
141141
}
142142

143-
public void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName)
143+
public void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName, bool exportHidden)
144144
{
145145
var blob = _factory.ReadOnlyDataBlob("__readonlydata_" + exportName, data, alignment);
146146
_rootAdder(blob, reason);
147147
exportName = _factory.NameMangler.NodeMangler.ExternVariable(exportName);
148-
_factory.NodeAliases.Add(blob, exportName);
148+
_factory.NodeAliases.Add(blob, (exportName, exportHidden));
149149
}
150150

151151
public void RootDelegateMarshallingData(DefType type, string reason)

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UnmanagedEntryPointsRootProvider.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ public class UnmanagedEntryPointsRootProvider : ICompilationRootProvider
1616
{
1717
private EcmaModule _module;
1818

19-
public UnmanagedEntryPointsRootProvider(EcmaModule module)
19+
public UnmanagedEntryPointsRootProvider(EcmaModule module, bool hidden = false)
2020
{
2121
_module = module;
22+
Hidden = hidden;
2223
}
2324

25+
public bool Hidden { get; }
26+
2427
public IEnumerable<EcmaMethod> ExportedMethods
2528
{
2629
get
@@ -62,12 +65,12 @@ public void AddCompilationRoots(IRootingServiceProvider rootProvider)
6265
if (ecmaMethod.IsUnmanagedCallersOnly)
6366
{
6467
string unmanagedCallersOnlyExportName = ecmaMethod.GetUnmanagedCallersOnlyExportName();
65-
rootProvider.AddCompilationRoot((MethodDesc)ecmaMethod, "Native callable", unmanagedCallersOnlyExportName);
68+
rootProvider.AddCompilationRoot((MethodDesc)ecmaMethod, "Native callable", unmanagedCallersOnlyExportName, Hidden);
6669
}
6770
else
6871
{
6972
string runtimeExportName = ecmaMethod.GetRuntimeExportName();
70-
rootProvider.AddCompilationRoot((MethodDesc)ecmaMethod, "Runtime export", runtimeExportName);
73+
rootProvider.AddCompilationRoot((MethodDesc)ecmaMethod, "Runtime export", runtimeExportName, Hidden);
7174
}
7275
}
7376
}

src/coreclr/tools/aot/ILCompiler/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,19 @@ public int Run()
281281
compilationRoots.Add(new Win32ResourcesRootProvider(module));
282282
}
283283

284-
foreach (var unmanagedEntryPointsAssembly in Get(_command.UnmanagedEntryPointsAssemblies))
284+
foreach (var unmanagedEntryPointsAssemblyValue in Get(_command.UnmanagedEntryPointsAssemblies))
285285
{
286+
const string hiddenSuffix = ",HIDDEN";
287+
bool hidden = unmanagedEntryPointsAssemblyValue.EndsWith(hiddenSuffix, StringComparison.Ordinal);
288+
string unmanagedEntryPointsAssembly = hidden ? unmanagedEntryPointsAssemblyValue[..^hiddenSuffix.Length] : unmanagedEntryPointsAssemblyValue;
289+
286290
if (typeSystemContext.InputFilePaths.ContainsKey(unmanagedEntryPointsAssembly))
287291
{
288292
// Skip adding UnmanagedEntryPointsRootProvider for modules that have been already registered as an input module
289293
continue;
290294
}
291295
EcmaModule module = typeSystemContext.GetModuleForSimpleName(unmanagedEntryPointsAssembly);
292-
compilationRoots.Add(new UnmanagedEntryPointsRootProvider(module));
296+
compilationRoots.Add(new UnmanagedEntryPointsRootProvider(module, hidden));
293297
}
294298

295299
foreach (var rdXmlFilePath in Get(_command.RdXmlFilePaths))
@@ -621,7 +625,7 @@ void RunScanner()
621625
{
622626
foreach (var compilationRoot in compilationRoots)
623627
{
624-
if (compilationRoot is UnmanagedEntryPointsRootProvider provider)
628+
if (compilationRoot is UnmanagedEntryPointsRootProvider provider && !provider.Hidden)
625629
defFileWriter.AddExportedMethods(provider.ExportedMethods);
626630
}
627631
}

0 commit comments

Comments
 (0)