Skip to content

Commit 19730e3

Browse files
committed
ELFObjectWriter: Make .reloc test generic
Move `Fixup.getKind() >= FirstLiteralRelocationKind` from target hooks to ELFObjectWriter::recordRelocation. Currently, getRelocType cannot be skipped for LoongArch due to #135519
1 parent 734660b commit 19730e3

File tree

11 files changed

+9
-28
lines changed

11 files changed

+9
-28
lines changed

llvm/lib/MC/ELFObjectWriter.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,14 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13831383
if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
13841384
return;
13851385

1386-
unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
1386+
auto EMachine = TargetObjectWriter->getEMachine();
1387+
unsigned Type;
1388+
if (Fixup.getKind() >= FirstLiteralRelocationKind &&
1389+
EMachine != ELF::EM_LOONGARCH)
1390+
Type = Fixup.getKind() - FirstLiteralRelocationKind;
1391+
else
1392+
Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
1393+
13871394
bool UseSectionSym =
13881395
SymA && SymA->getBinding() == ELF::STB_LOCAL && !SymA->isUndefined();
13891396
if (UseSectionSym) {
@@ -1402,8 +1409,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
14021409
} else {
14031410
// In PPC64 ELFv1, .quad .TOC.@tocbase in the .opd section is expected to
14041411
// reference the null symbol.
1405-
if (Type == ELF::R_PPC64_TOC &&
1406-
TargetObjectWriter->getEMachine() == ELF::EM_PPC64)
1412+
if (Type == ELF::R_PPC64_TOC && EMachine == ELF::EM_PPC64)
14071413
SymA = nullptr;
14081414

14091415
if (SymA) {

llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
109109
const MCFixup &Fixup,
110110
bool IsPCRel) const {
111111
unsigned Kind = Fixup.getTargetKind();
112-
if (Kind >= FirstLiteralRelocationKind)
113-
return Kind - FirstLiteralRelocationKind;
114112
AArch64MCExpr::Specifier RefKind =
115113
static_cast<AArch64MCExpr::Specifier>(Target.getSpecifier());
116114
AArch64MCExpr::Specifier SymLoc = AArch64MCExpr::getSymbolLoc(RefKind);

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx,
6868
}
6969

7070
MCFixupKind Kind = Fixup.getKind();
71-
if (Kind >= FirstLiteralRelocationKind)
72-
return Kind - FirstLiteralRelocationKind;
7371
switch (Kind) {
7472
default: break;
7573
case FK_PCRel_4:

llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
8080
bool IsPCRel,
8181
MCContext &Ctx) const {
8282
unsigned Kind = Fixup.getTargetKind();
83-
if (Kind >= FirstLiteralRelocationKind)
84-
return Kind - FirstLiteralRelocationKind;
8583
uint8_t Specifier = Target.getSpecifier();
8684
auto CheckFDPIC = [&](uint32_t Type) {
8785
if (getOSABI() != ELF::ELFOSABI_ARM_FDPIC)

llvm/lib/Target/AVR/MCTargetDesc/AVRELFObjectWriter.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI)
3737
unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
3838
const MCFixup &Fixup,
3939
bool IsPCRel) const {
40-
const unsigned Kind = Fixup.getTargetKind();
41-
if (Kind >= FirstLiteralRelocationKind)
42-
return Kind - FirstLiteralRelocationKind;
4340
auto Modifier = AVRMCExpr::Specifier(Target.getSpecifier());
4441
switch ((unsigned)Fixup.getKind()) {
4542
case FK_Data_1:

llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@ unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
159159
bool IsPCRel) const {
160160
// Determine the type of the relocation.
161161
unsigned Kind = Fixup.getTargetKind();
162-
if (Kind >= FirstLiteralRelocationKind)
163-
return Kind - FirstLiteralRelocationKind;
164-
165162
switch (Target.getSpecifier()) {
166163
case MipsMCExpr::MEK_DTPREL:
167164
case MipsMCExpr::MEK_DTPREL_HI:

llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
4242
const MCFixup &Fixup,
4343
bool IsPCRel) const {
4444
MCFixupKind Kind = Fixup.getKind();
45-
if (Kind >= FirstLiteralRelocationKind)
46-
return Kind - FirstLiteralRelocationKind;
4745
SMLoc Loc = Fixup.getValue()->getLoc();
4846
auto Spec = static_cast<PPCMCExpr::Specifier>(Target.getSpecifier());
4947
switch (Spec) {

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
5353
const MCExpr *Expr = Fixup.getValue();
5454
// Determine the type of the relocation
5555
unsigned Kind = Fixup.getTargetKind();
56-
if (Kind >= FirstLiteralRelocationKind)
57-
return Kind - FirstLiteralRelocationKind;
58-
5956
auto Spec = RISCVMCExpr::Specifier(Target.getSpecifier());
6057
switch (Spec) {
6158
case RISCVMCExpr::VK_TPREL_HI:

llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
4242
const MCValue &Target,
4343
const MCFixup &Fixup,
4444
bool IsPCRel) const {
45-
MCFixupKind Kind = Fixup.getKind();
46-
if (Kind >= FirstLiteralRelocationKind)
47-
return Kind - FirstLiteralRelocationKind;
48-
4945
switch (Target.getSpecifier()) {
5046
case SparcMCExpr::VK_TLS_GD_HI22:
5147
case SparcMCExpr::VK_TLS_GD_LO10:

llvm/lib/Target/SystemZ/MCTargetDesc/SystemZELFObjectWriter.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ unsigned SystemZELFObjectWriter::getRelocType(MCContext &Ctx,
156156
bool IsPCRel) const {
157157
SMLoc Loc = Fixup.getLoc();
158158
unsigned Kind = Fixup.getKind();
159-
if (Kind >= FirstLiteralRelocationKind)
160-
return Kind - FirstLiteralRelocationKind;
161159
auto Specifier = SystemZMCExpr::Specifier(Target.getSpecifier());
162160
switch (Specifier) {
163161
case SystemZMCExpr::VK_INDNTPOFF:

llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,6 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
338338
const MCFixup &Fixup,
339339
bool IsPCRel) const {
340340
MCFixupKind Kind = Fixup.getKind();
341-
if (Kind >= FirstLiteralRelocationKind)
342-
return Kind - FirstLiteralRelocationKind;
343341
auto Specifier = X86MCExpr::Specifier(Target.getSpecifier());
344342
switch (Specifier) {
345343
case X86MCExpr::VK_GOTTPOFF:

0 commit comments

Comments
 (0)