Skip to content

Conversation

aaupov
Copy link
Contributor

@aaupov aaupov commented Oct 9, 2025

Observed in GCC-produced binary. Emit a warning for the user.

Test Plan: added bolt/test/X86/fragment-alias.s

Observed in GCC-produced binary. Emit a warning for the user.

Test Plan: added bolt/test/X86/fragment-alias.s
@llvmbot
Copy link
Member

llvmbot commented Oct 9, 2025

@llvm/pr-subscribers-bolt

Author: Amir Ayupov (aaupov)

Changes

Observed in GCC-produced binary. Emit a warning for the user.

Test Plan: added bolt/test/X86/fragment-alias.s


Full diff: https://github.com/llvm/llvm-project/pull/162727.diff

2 Files Affected:

  • (modified) bolt/lib/Rewrite/RewriteInstance.cpp (+6)
  • (added) bolt/test/X86/fragment-alias.s (+13)
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index c428828956ca0..59dd1c301de4a 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -1514,6 +1514,12 @@ void RewriteInstance::registerFragments() {
       }
       if (BD) {
         BinaryFunction *BF = BC->getFunctionForSymbol(BD->getSymbol());
+        if (BF == &Function) {
+          BC->errs()
+              << "BOLT-WARNING: fragment maps to the same function as parent: "
+              << Function << '\n';
+          continue;
+        }
         if (BF) {
           BC->registerFragment(Function, *BF);
           continue;
diff --git a/bolt/test/X86/fragment-alias.s b/bolt/test/X86/fragment-alias.s
new file mode 100644
index 0000000000000..3392dd564f417
--- /dev/null
+++ b/bolt/test/X86/fragment-alias.s
@@ -0,0 +1,13 @@
+## This test reproduces the issue where a fragment has the same address as
+## parent function.
+# RUN: llvm-mc --filetype=obj --triple x86_64-unknown-unknown %s -o %t.o
+# RUN: %clang %cflags %t.o -o %t
+# RUN: llvm-bolt %t -o %t.out 2>&1 | FileCheck %s
+# CHECK: BOLT-WARNING: fragment maps to the same function as parent: main/1(*2)
+.type main, @function
+.type main.cold, @function
+main.cold:
+main:
+  ret
+.size main, .-main
+.size main.cold, .-main.cold

Copy link
Contributor

@maksfb maksfb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix.

@aaupov aaupov merged commit ab897ae into llvm:main Oct 9, 2025
9 of 10 checks passed
@aaupov aaupov deleted the frag-alias branch October 9, 2025 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants