[llvm] def6158 - [MC] Restore a special case to support limited A-B folding when A/B are in the same fragment being laided out

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 16 17:09:02 PDT 2023


Author: Fangrui Song
Date: 2023-06-16T17:08:58-07:00
New Revision: def6158f957e77d67f74aa9c8bb342c4b64a159b

URL: https://github.com/llvm/llvm-project/commit/def6158f957e77d67f74aa9c8bb342c4b64a159b
DIFF: https://github.com/llvm/llvm-project/commit/def6158f957e77d67f74aa9c8bb342c4b64a159b.diff

LOG: [MC] Restore a special case to support limited A-B folding when A/B are in the same fragment being laided out

Add subsection-if.s to test what we can fold (in the same fragment) and what we cannot.

Fix https://github.com/ClangBuiltLinux/linux/issues/1876
Fixes: 4bdc7f7a331f82cca1637388cf68bdc5b32ab43b

Added: 
    llvm/test/MC/ELF/subsection-if.s

Modified: 
    llvm/lib/MC/MCExpr.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index 46a0367a23f6f..df305c843047d 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -625,6 +625,12 @@ static void AttemptToFoldSymbolOffsetDifference(
     return;
 
   if (Layout) {
+    // If both symbols are in the same fragment, return the 
diff erence of their
+    // offsets. canGetFragmentOffset(FA) may be false.
+    if (FA == FB && !SA.isVariable() && !SB.isVariable()) {
+      Addend += SA.getOffset() - SB.getOffset();
+      return FinalizeFolding();
+    }
     // One of the symbol involved is part of a fragment being laid out. Quit now
     // to avoid a self loop.
     if (!Layout->canGetFragmentOffset(FA) || !Layout->canGetFragmentOffset(FB))

diff  --git a/llvm/test/MC/ELF/subsection-if.s b/llvm/test/MC/ELF/subsection-if.s
new file mode 100644
index 0000000000000..7f2cba6f52100
--- /dev/null
+++ b/llvm/test/MC/ELF/subsection-if.s
@@ -0,0 +1,26 @@
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
+# RUN: llvm-readelf -x .text %t | FileCheck %s
+# RUN: not llvm-mc -filetype=obj -triple=x86_64 --defsym ERR=1 %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR
+
+# CHECK: 0x00000000 9090
+
+.subsection 1
+661:
+  nop
+662:
+.previous
+## 661 and 662 are in the same fragment being laied out.
+  .org . - (662b-661b) + (662b-661b)
+  nop
+
+.ifdef ERR
+.subsection 1
+661:
+  .p2align 2
+  nop
+662:
+.previous
+# ERR: :[[#@LINE+1]]:8: error: expected assembly-time absolute expression
+  .org . - (662b-661b) + (662b-661b)
+  nop
+.endif


        


More information about the llvm-commits mailing list