Skip to content

Commit ac45220

Browse files
authoredFeb 16, 2024··
[llvm] Support indirect symbol replacement with R_ARM_GOT_PREL (#81916)
R_ARM_GOT_PREL is equivalent to GOTPCREL on other architectures, so we can use it for indirect symbol replacement the same way. This is the equivalent of #67754 for x86_64 and #78003 for AArch64 and 64-bit RISC-V.
1 parent 39e32b4 commit ac45220

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
 

‎llvm/lib/Target/ARM/ARMTargetObjectFile.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/MC/MCExpr.h"
1717
#include "llvm/MC/MCSectionELF.h"
1818
#include "llvm/MC/MCTargetOptions.h"
19+
#include "llvm/MC/MCValue.h"
1920
#include "llvm/MC/SectionKind.h"
2021
#include "llvm/Target/TargetMachine.h"
2122
#include <cassert>
@@ -56,6 +57,16 @@ void ARMElfTargetObjectFile::Initialize(MCContext &Ctx,
5657

5758
MCRegister ARMElfTargetObjectFile::getStaticBase() const { return ARM::R9; }
5859

60+
const MCExpr *ARMElfTargetObjectFile::getIndirectSymViaGOTPCRel(
61+
const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
62+
int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
63+
int64_t FinalOffset = Offset + MV.getConstant();
64+
const MCExpr *Res = MCSymbolRefExpr::create(
65+
Sym, MCSymbolRefExpr::VK_ARM_GOT_PREL, getContext());
66+
const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
67+
return MCBinaryExpr::createAdd(Res, Off, getContext());
68+
}
69+
5970
const MCExpr *ARMElfTargetObjectFile::
6071
getIndirectSymViaRWPI(const MCSymbol *Sym) const {
6172
return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_ARM_SBREL,

‎llvm/lib/Target/ARM/ARMTargetObjectFile.h

+7
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ class ARMElfTargetObjectFile : public TargetLoweringObjectFileELF {
1919
public:
2020
ARMElfTargetObjectFile() {
2121
PLTRelativeVariantKind = MCSymbolRefExpr::VK_ARM_PREL31;
22+
SupportIndirectSymViaGOTPCRel = true;
2223
}
2324

2425
void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
2526

2627
MCRegister getStaticBase() const override;
2728

29+
const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV,
30+
const MCSymbol *Sym,
31+
const MCValue &MV, int64_t Offset,
32+
MachineModuleInfo *MMI,
33+
MCStreamer &Streamer) const override;
34+
2835
const MCExpr *getIndirectSymViaRWPI(const MCSymbol *Sym) const override;
2936

3037
const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; REQUIRES: arm-registered-target
2+
3+
;; Verify that the generated assembly omits the RTTI proxy and uses the correct
4+
;; relocations and addends.
5+
; RUN: llc %s -mtriple=armv7 -o - | FileCheck %s
6+
7+
;; Verify that the generated object uses the correct relocations and addends.
8+
; RUN: llc %s -filetype=obj -mtriple=thumbv7 -o %t.o
9+
; RUN: llvm-readelf --relocs --hex-dump=.rodata %t.o | FileCheck --check-prefix=OBJ %s
10+
11+
@vtable = dso_local unnamed_addr constant i32 sub (i32 ptrtoint (ptr @rtti.proxy to i32), i32 ptrtoint (ptr @vtable to i32)), align 4
12+
13+
@vtable_with_offset = dso_local unnamed_addr constant [2 x i32] [i32 0, i32 sub (i32 ptrtoint (ptr @rtti.proxy to i32), i32 ptrtoint (ptr @vtable_with_offset to i32))], align 4
14+
15+
@vtable_with_negative_offset = dso_local unnamed_addr constant [2 x i32] [
16+
i32 sub (
17+
i32 ptrtoint (ptr @rtti.proxy to i32),
18+
i32 ptrtoint (ptr getelementptr inbounds ([2 x i32], ptr @vtable_with_negative_offset, i32 0, i32 1) to i32)
19+
),
20+
i32 0
21+
], align 4
22+
23+
@rtti = external global i8, align 4
24+
@rtti.proxy = linkonce_odr hidden unnamed_addr constant ptr @rtti
25+
26+
; CHECK-NOT: rtti.proxy
27+
28+
; CHECK-LABEL: vtable:
29+
; CHECK-NEXT: .long rtti(GOT_PREL)+0{{$}}
30+
31+
; CHECK-LABEL: vtable_with_offset:
32+
; CHECK-NEXT: .long 0
33+
; CHECK-NEXT: .long rtti(GOT_PREL)+4{{$}}
34+
35+
; CHECK-LABEL: vtable_with_negative_offset:
36+
; CHECK-NEXT: .long rtti(GOT_PREL)-4{{$}}
37+
; CHECK-NEXT: .long 0
38+
39+
; OBJ-LABEL: Relocation section '.rel.rodata' at offset [[#%#x,]] contains 3 entries:
40+
; OBJ: {{^}}00000000 [[#]] R_ARM_GOT_PREL [[#]] rtti{{$}}
41+
; OBJ-NEXT: {{^}}00000008 [[#]] R_ARM_GOT_PREL [[#]] rtti{{$}}
42+
; OBJ-NEXT: {{^}}0000000c [[#]] R_ARM_GOT_PREL [[#]] rtti{{$}}
43+
44+
; OBJ-LABEL: Hex dump of section '.rodata':
45+
; OBJ-NEXT: 0x00000000 00000000 00000000 04000000 fcffffff

0 commit comments

Comments
 (0)
Please sign in to comment.