Skip to content

Commit 4b19db6

Browse files
authored
Revert "AsmPrinter: Remove ELF's special lowerRelativeReference for unnamed_addr function" (#133935)
Reverts #132684
1 parent 00e6d4f commit 4b19db6

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
125125
lowerSymbolDifference(const MCSymbol *LHS, const MCSymbol *RHS,
126126
int64_t Addend,
127127
std::optional<int64_t> PCRelativeOffset) const;
128+
const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
129+
const GlobalValue *RHS, int64_t Addend,
130+
std::optional<int64_t> PCRelativeOffset,
131+
const TargetMachine &TM) const override;
128132

129133
const MCExpr *lowerDSOLocalEquivalent(const MCSymbol *LHS,
130134
const MCSymbol *RHS, int64_t Addend,

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,24 @@ const MCExpr *TargetLoweringObjectFileELF::lowerSymbolDifference(
12331233
return Res;
12341234
}
12351235

1236+
const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
1237+
const GlobalValue *LHS, const GlobalValue *RHS, int64_t Addend,
1238+
std::optional<int64_t> PCRelativeOffset, const TargetMachine &TM) const {
1239+
// We may only use a PLT-relative relocation to refer to unnamed_addr
1240+
// functions.
1241+
if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
1242+
return nullptr;
1243+
1244+
// Basic correctness checks.
1245+
if (LHS->getType()->getPointerAddressSpace() != 0 ||
1246+
RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
1247+
RHS->isThreadLocal())
1248+
return nullptr;
1249+
1250+
return lowerSymbolDifference(TM.getSymbol(LHS), TM.getSymbol(RHS), Addend,
1251+
PCRelativeOffset);
1252+
}
1253+
12361254
// Reference the PLT entry of a function, optionally with a subtrahend (`RHS`).
12371255
const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent(
12381256
const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend,

llvm/test/CodeGen/ARM/relative-reloc.ll renamed to llvm/test/CodeGen/ARM/plt-relative-reloc.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ declare void @fn1() unnamed_addr
1010
declare void @fn2() unnamed_addr
1111
declare void @fn3()
1212

13-
;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
1413
; CHECK: .long 0
15-
; CHECK-NEXT: .long fn1-vtable-4
16-
; CHECK-NEXT: .long fn2-vtable-4
14+
; CHECK-NEXT: .long fn1(prel31)-vtable-4
15+
; CHECK-NEXT: .long fn2(prel31)-vtable-4
1716
; CHECK-NEXT: .long fn3-vtable-4

llvm/test/CodeGen/RISCV/relative-reloc.ll renamed to llvm/test/CodeGen/RISCV/plt-relative-reloc.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ declare void @fn2() unnamed_addr
1212
declare void @fn3()
1313
@global4 = external unnamed_addr global i8
1414

15-
;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
1615
; CHECK: vtable:
1716
; CHECK-NEXT: .word 0 # 0x0
18-
; CHECK-NEXT: .word fn1-vtable-4
19-
; CHECK-NEXT: .word fn2-vtable-4
17+
; CHECK-NEXT: .word %pltpcrel(fn1)
18+
; CHECK-NEXT: .word %pltpcrel(fn2+4)
2019
; CHECK-NEXT: .word fn3-vtable-4
2120
; CHECK-NEXT: .word global4-vtable-4
2221
; CHECK-NEXT: .size vtable, 20

llvm/test/CodeGen/X86/relative-reloc-64.ll renamed to llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ declare void @fn2() unnamed_addr
1212
declare void @fn3()
1313
@global4 = external unnamed_addr global i8
1414

15-
;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
1615
; CHECK: .long 0
17-
; CHECK-NEXT: .long fn1-vtable-4
18-
; CHECK-NEXT: .long fn2-vtable-4
16+
; CHECK-NEXT: .long fn1@PLT-vtable-4
17+
; CHECK-NEXT: .long fn2@PLT-vtable-4
1918
; CHECK-NEXT: .long fn3-vtable-4
2019
; CHECK-NEXT: .long global4-vtable-4

llvm/test/CodeGen/X86/relative-reloc-32.ll renamed to llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ declare void @fn2() unnamed_addr
1111
declare void @fn3()
1212

1313
; CHECK: .long 0
14-
; CHECK-NEXT: .long fn1-vtable-4
15-
; CHECK-NEXT: .long fn2-vtable-4
14+
; CHECK-NEXT: .long fn1@PLT-vtable-4
15+
; CHECK-NEXT: .long fn2@PLT-vtable-4
1616
; CHECK-NEXT: .long fn3-vtable-4

0 commit comments

Comments
 (0)