0
0
mirror of https://github.com/llvm/llvm-project.git synced 2025-04-21 19:57:00 +00:00

ELFObjectWriter: Make .reloc test generic

Move `Fixup.getKind() >= FirstLiteralRelocationKind` from target hooks
to ELFObjectWriter::recordRelocation.

Currently, getRelocType cannot be skipped for LoongArch due to 
This commit is contained in:
Fangrui Song 2025-04-12 23:02:24 -07:00
parent 734660b6dd
commit 19730e37ec
11 changed files with 9 additions and 28 deletions
llvm/lib
MC
Target
AArch64/MCTargetDesc
AMDGPU/MCTargetDesc
ARM/MCTargetDesc
AVR/MCTargetDesc
Mips/MCTargetDesc
PowerPC/MCTargetDesc
RISCV/MCTargetDesc
Sparc/MCTargetDesc
SystemZ/MCTargetDesc
X86/MCTargetDesc

@ -1383,7 +1383,14 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
return;
unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
auto EMachine = TargetObjectWriter->getEMachine();
unsigned Type;
if (Fixup.getKind() >= FirstLiteralRelocationKind &&
EMachine != ELF::EM_LOONGARCH)
Type = Fixup.getKind() - FirstLiteralRelocationKind;
else
Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
bool UseSectionSym =
SymA && SymA->getBinding() == ELF::STB_LOCAL && !SymA->isUndefined();
if (UseSectionSym) {
@ -1402,8 +1409,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
} else {
// In PPC64 ELFv1, .quad .TOC.@tocbase in the .opd section is expected to
// reference the null symbol.
if (Type == ELF::R_PPC64_TOC &&
TargetObjectWriter->getEMachine() == ELF::EM_PPC64)
if (Type == ELF::R_PPC64_TOC && EMachine == ELF::EM_PPC64)
SymA = nullptr;
if (SymA) {

@ -109,8 +109,6 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
const MCFixup &Fixup,
bool IsPCRel) const {
unsigned Kind = Fixup.getTargetKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
AArch64MCExpr::Specifier RefKind =
static_cast<AArch64MCExpr::Specifier>(Target.getSpecifier());
AArch64MCExpr::Specifier SymLoc = AArch64MCExpr::getSymbolLoc(RefKind);

@ -68,8 +68,6 @@ unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx,
}
MCFixupKind Kind = Fixup.getKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
switch (Kind) {
default: break;
case FK_PCRel_4:

@ -80,8 +80,6 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
bool IsPCRel,
MCContext &Ctx) const {
unsigned Kind = Fixup.getTargetKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
uint8_t Specifier = Target.getSpecifier();
auto CheckFDPIC = [&](uint32_t Type) {
if (getOSABI() != ELF::ELFOSABI_ARM_FDPIC)

@ -37,9 +37,6 @@ AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI)
unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup,
bool IsPCRel) const {
const unsigned Kind = Fixup.getTargetKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
auto Modifier = AVRMCExpr::Specifier(Target.getSpecifier());
switch ((unsigned)Fixup.getKind()) {
case FK_Data_1:

@ -159,9 +159,6 @@ unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
bool IsPCRel) const {
// Determine the type of the relocation.
unsigned Kind = Fixup.getTargetKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
switch (Target.getSpecifier()) {
case MipsMCExpr::MEK_DTPREL:
case MipsMCExpr::MEK_DTPREL_HI:

@ -42,8 +42,6 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup,
bool IsPCRel) const {
MCFixupKind Kind = Fixup.getKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
SMLoc Loc = Fixup.getValue()->getLoc();
auto Spec = static_cast<PPCMCExpr::Specifier>(Target.getSpecifier());
switch (Spec) {

@ -53,9 +53,6 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
const MCExpr *Expr = Fixup.getValue();
// Determine the type of the relocation
unsigned Kind = Fixup.getTargetKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
auto Spec = RISCVMCExpr::Specifier(Target.getSpecifier());
switch (Spec) {
case RISCVMCExpr::VK_TPREL_HI:

@ -42,10 +42,6 @@ unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
const MCValue &Target,
const MCFixup &Fixup,
bool IsPCRel) const {
MCFixupKind Kind = Fixup.getKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
switch (Target.getSpecifier()) {
case SparcMCExpr::VK_TLS_GD_HI22:
case SparcMCExpr::VK_TLS_GD_LO10:

@ -156,8 +156,6 @@ unsigned SystemZELFObjectWriter::getRelocType(MCContext &Ctx,
bool IsPCRel) const {
SMLoc Loc = Fixup.getLoc();
unsigned Kind = Fixup.getKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
auto Specifier = SystemZMCExpr::Specifier(Target.getSpecifier());
switch (Specifier) {
case SystemZMCExpr::VK_INDNTPOFF:

@ -338,8 +338,6 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup,
bool IsPCRel) const {
MCFixupKind Kind = Fixup.getKind();
if (Kind >= FirstLiteralRelocationKind)
return Kind - FirstLiteralRelocationKind;
auto Specifier = X86MCExpr::Specifier(Target.getSpecifier());
switch (Specifier) {
case X86MCExpr::VK_GOTTPOFF: