Author: Petr Hosek Date: 2025-04-01T09:31:46-07:00 New Revision: dd68750d7e2077020552e83e680522226f179d98
URL: https://github.com/llvm/llvm-project/commit/dd68750d7e2077020552e83e680522226f179d98 DIFF: https://github.com/llvm/llvm-project/commit/dd68750d7e2077020552e83e680522226f179d98.diff LOG: Revert "AsmPrinter: Remove ELF's special lowerRelativeReference for unnamed_a…" This reverts commit dd862356e20d2d7e0d0356dff5bd80623c14febc. Added: llvm/test/CodeGen/ARM/plt-relative-reloc.ll llvm/test/CodeGen/RISCV/plt-relative-reloc.ll llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll Modified: llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Removed: llvm/test/CodeGen/ARM/relative-reloc.ll llvm/test/CodeGen/RISCV/relative-reloc.ll llvm/test/CodeGen/X86/relative-reloc-32.ll llvm/test/CodeGen/X86/relative-reloc-64.ll ################################################################################ diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h index f035d81e85ddb..8b0e5798d1b61 100644 --- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h +++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h @@ -125,6 +125,10 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile { lowerSymbolDifference(const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend, std::optional<int64_t> PCRelativeOffset) const; + const MCExpr *lowerRelativeReference(const GlobalValue *LHS, + const GlobalValue *RHS, int64_t Addend, + std::optional<int64_t> PCRelativeOffset, + const TargetMachine &TM) const override; const MCExpr *lowerDSOLocalEquivalent(const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend, diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index c9415292e88f7..4c20c5dc74d9a 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1233,6 +1233,24 @@ const MCExpr *TargetLoweringObjectFileELF::lowerSymbolDifference( return Res; } +const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( + const GlobalValue *LHS, const GlobalValue *RHS, int64_t Addend, + std::optional<int64_t> PCRelativeOffset, const TargetMachine &TM) const { + // We may only use a PLT-relative relocation to refer to unnamed_addr + // functions. + if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) + return nullptr; + + // Basic correctness checks. + if (LHS->getType()->getPointerAddressSpace() != 0 || + RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || + RHS->isThreadLocal()) + return nullptr; + + return lowerSymbolDifference(TM.getSymbol(LHS), TM.getSymbol(RHS), Addend, + PCRelativeOffset); +} + // Reference the PLT entry of a function, optionally with a subtrahend (`RHS`). const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent( const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend, diff --git a/llvm/test/CodeGen/ARM/relative-reloc.ll b/llvm/test/CodeGen/ARM/plt-relative-reloc.ll similarity index 78% rename from llvm/test/CodeGen/ARM/relative-reloc.ll rename to llvm/test/CodeGen/ARM/plt-relative-reloc.ll index 65053726e66bf..ede891900e6d0 100644 --- a/llvm/test/CodeGen/ARM/relative-reloc.ll +++ b/llvm/test/CodeGen/ARM/plt-relative-reloc.ll @@ -10,8 +10,7 @@ declare void @fn1() unnamed_addr declare void @fn2() unnamed_addr declare void @fn3() -;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible. ; CHECK: .long 0 -; CHECK-NEXT: .long fn1-vtable-4 -; CHECK-NEXT: .long fn2-vtable-4 +; CHECK-NEXT: .long fn1(prel31)-vtable-4 +; CHECK-NEXT: .long fn2(prel31)-vtable-4 ; CHECK-NEXT: .long fn3-vtable-4 diff --git a/llvm/test/CodeGen/RISCV/relative-reloc.ll b/llvm/test/CodeGen/RISCV/plt-relative-reloc.ll similarity index 84% rename from llvm/test/CodeGen/RISCV/relative-reloc.ll rename to llvm/test/CodeGen/RISCV/plt-relative-reloc.ll index 6c94b9fce9308..d2dceb773b2e9 100644 --- a/llvm/test/CodeGen/RISCV/relative-reloc.ll +++ b/llvm/test/CodeGen/RISCV/plt-relative-reloc.ll @@ -12,11 +12,10 @@ declare void @fn2() unnamed_addr declare void @fn3() @global4 = external unnamed_addr global i8 -;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible. ; CHECK: vtable: ; CHECK-NEXT: .word 0 # 0x0 -; CHECK-NEXT: .word fn1-vtable-4 -; CHECK-NEXT: .word fn2-vtable-4 +; CHECK-NEXT: .word %pltpcrel(fn1) +; CHECK-NEXT: .word %pltpcrel(fn2+4) ; CHECK-NEXT: .word fn3-vtable-4 ; CHECK-NEXT: .word global4-vtable-4 ; CHECK-NEXT: .size vtable, 20 diff --git a/llvm/test/CodeGen/X86/relative-reloc-64.ll b/llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll similarity index 84% rename from llvm/test/CodeGen/X86/relative-reloc-64.ll rename to llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll index 6f88edfa075b8..54736c94af248 100644 --- a/llvm/test/CodeGen/X86/relative-reloc-64.ll +++ b/llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll @@ -12,9 +12,8 @@ declare void @fn2() unnamed_addr declare void @fn3() @global4 = external unnamed_addr global i8 -;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible. ; CHECK: .long 0 -; CHECK-NEXT: .long fn1-vtable-4 -; CHECK-NEXT: .long fn2-vtable-4 +; CHECK-NEXT: .long fn1@PLT-vtable-4 +; CHECK-NEXT: .long fn2@PLT-vtable-4 ; CHECK-NEXT: .long fn3-vtable-4 ; CHECK-NEXT: .long global4-vtable-4 diff --git a/llvm/test/CodeGen/X86/relative-reloc-32.ll b/llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll similarity index 89% rename from llvm/test/CodeGen/X86/relative-reloc-32.ll rename to llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll index 7d0b1fd546a00..d5e80285b160d 100644 --- a/llvm/test/CodeGen/X86/relative-reloc-32.ll +++ b/llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll @@ -11,6 +11,6 @@ declare void @fn2() unnamed_addr declare void @fn3() ; CHECK: .long 0 -; CHECK-NEXT: .long fn1-vtable-4 -; CHECK-NEXT: .long fn2-vtable-4 +; CHECK-NEXT: .long fn1@PLT-vtable-4 +; CHECK-NEXT: .long fn2@PLT-vtable-4 ; CHECK-NEXT: .long fn3-vtable-4 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits