diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectDataInterner.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectDataInterner.cs
index 743fdaeda6ed05..496e775ed03208 100644
--- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectDataInterner.cs
+++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectDataInterner.cs
@@ -34,6 +34,11 @@ private void EnsureMap(NodeFactory factory)
if (body is ISpecialUnboxThunkNode unboxThunk && unboxThunk.IsSpecialUnboxingThunk)
continue;
+ // Bodies that are visible from outside should not be folded because we don't know
+ // if they're address taken.
+ if (factory.GetSymbolAlternateName(body) != null)
+ continue;
+
var key = new MethodInternKey(body, factory);
if (methodHash.TryGetValue(key, out MethodInternKey found))
{
diff --git a/src/tests/nativeaot/SmokeTests/StackTraceMetadata/BodyFoldingTest.cs b/src/tests/nativeaot/SmokeTests/StackTraceMetadata/BodyFoldingTest.cs
index 607e2761a53630..9ee3b83cbe3c99 100644
--- a/src/tests/nativeaot/SmokeTests/StackTraceMetadata/BodyFoldingTest.cs
+++ b/src/tests/nativeaot/SmokeTests/StackTraceMetadata/BodyFoldingTest.cs
@@ -3,9 +3,16 @@
using System;
using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
class BodyFoldingTest
{
+ [UnmanagedCallersOnly(EntryPoint = "FoldableMethod1")]
+ static void FoldableMethod1() { }
+
+ [UnmanagedCallersOnly(EntryPoint = "FoldableMethod2")]
+ static void FoldableMethod2() { }
+
class SimpleDelegateTargets
{
public static object Return1DelStatic() => new object();
diff --git a/src/tests/nativeaot/SmokeTests/StackTraceMetadata/StackTraceMetadata.csproj b/src/tests/nativeaot/SmokeTests/StackTraceMetadata/StackTraceMetadata.csproj
index 7e4d202e8fff69..2b2a43eff8dd56 100644
--- a/src/tests/nativeaot/SmokeTests/StackTraceMetadata/StackTraceMetadata.csproj
+++ b/src/tests/nativeaot/SmokeTests/StackTraceMetadata/StackTraceMetadata.csproj
@@ -12,4 +12,8 @@
+
+
+
+
diff --git a/src/tests/nativeaot/SmokeTests/StackTraceMetadata/StackTraceMetadata_Stripped.csproj b/src/tests/nativeaot/SmokeTests/StackTraceMetadata/StackTraceMetadata_Stripped.csproj
index c5aa06097b81e9..28a98ff79570af 100644
--- a/src/tests/nativeaot/SmokeTests/StackTraceMetadata/StackTraceMetadata_Stripped.csproj
+++ b/src/tests/nativeaot/SmokeTests/StackTraceMetadata/StackTraceMetadata_Stripped.csproj
@@ -14,4 +14,8 @@
+
+
+
+