From def6158f957e77d67f74aa9c8bb342c4b64a159b Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 16 Jun 2023 17:08:58 -0700 Subject: [PATCH] [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 --- llvm/lib/MC/MCExpr.cpp | 6 ++++++ llvm/test/MC/ELF/subsection-if.s | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 llvm/test/MC/ELF/subsection-if.s diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp index 46a0367..df305c8 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 difference 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 0000000..7f2cba6 --- /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 -- 2.7.4