Skip to content

MC: Emit symbols for R_X86_64_PLT32 relocation pointing to symbols with non-zero values. #138795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion llvm/include/llvm/MC/MCELFObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class MCELFObjectTargetWriter : public MCObjectTargetWriter {
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const = 0;

virtual bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
virtual bool needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &Val, const MCSymbol &Sym,
unsigned Type) const;

virtual void sortRelocs(const MCAssembler &Asm,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/MC/ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
if (Asm.isThumbFunc(Sym))
return false;

return !TargetObjectWriter->needsRelocateWithSymbol(Val, *Sym, Type);
return !TargetObjectWriter->needsRelocateWithSymbol(Asm, Val, *Sym, Type);
}

bool ELFObjectWriter::checkRelocation(MCContext &Ctx, SMLoc Loc,
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/MC/MCELFObjectTargetWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ MCELFObjectTargetWriter::MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
: OSABI(OSABI_), ABIVersion(ABIVersion_), EMachine(EMachine_),
HasRelocationAddend(HasRelocationAddend_), Is64Bit(Is64Bit_) {}

bool MCELFObjectTargetWriter::needsRelocateWithSymbol(const MCValue &,
bool MCELFObjectTargetWriter::needsRelocateWithSymbol(const MCAssembler &,
const MCValue &,
const MCSymbol &,
unsigned Type) const {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class AArch64ELFObjectWriter : public MCELFObjectTargetWriter {
protected:
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
bool IsILP32;
};
Expand Down Expand Up @@ -534,7 +535,8 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
llvm_unreachable("Unimplemented fixup -> relocation");
}

bool AArch64ELFObjectWriter::needsRelocateWithSymbol(const MCValue &Val,
bool AArch64ELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &Val,
const MCSymbol &,
unsigned) const {
// For memory-tagged symbols, ensure that the relocation uses the symbol. For
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ namespace {
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;

bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};

Expand All @@ -50,7 +51,8 @@ ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
ELF::EM_ARM,
/*HasRelocationAddend*/ false) {}

bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &,
const MCSymbol &,
unsigned Type) const {
// FIXME: This is extremely conservative. This really needs to use an
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/CSKY/MCTargetDesc/CSKYELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class CSKYELFObjectWriter : public MCELFObjectTargetWriter {

unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};

Expand Down Expand Up @@ -167,7 +168,8 @@ unsigned CSKYELFObjectWriter::getRelocType(MCContext &Ctx,
}
}

bool CSKYELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
bool CSKYELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &V,
const MCSymbol &,
unsigned Type) const {
switch (V.getSpecifier()) {
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/Lanai/MCTargetDesc/LanaiELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class LanaiELFObjectWriter : public MCELFObjectTargetWriter {
protected:
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};

Expand Down Expand Up @@ -72,7 +73,8 @@ unsigned LanaiELFObjectWriter::getRelocType(MCContext & /*Ctx*/,
return Type;
}

bool LanaiELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
bool LanaiELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &,
const MCSymbol &,
unsigned Type) const {
switch (Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {

~LoongArchELFObjectWriter() override;

bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override {
return EnableRelax;
}
Expand Down
12 changes: 7 additions & 5 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class MipsELFObjectWriter : public MCELFObjectTargetWriter {

unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
void sortRelocs(const MCAssembler &Asm,
std::vector<ELFRelocationEntry> &Relocs) override;
Expand Down Expand Up @@ -444,15 +445,16 @@ void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
Relocs[CopyTo++] = R.R;
}

bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &Val,
bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const {
// If it's a compound relocation for N64 then we need the relocation if any
// sub-relocation needs it.
if (!isUInt<8>(Type))
return needsRelocateWithSymbol(Val, Sym, Type & 0xff) ||
needsRelocateWithSymbol(Val, Sym, (Type >> 8) & 0xff) ||
needsRelocateWithSymbol(Val, Sym, (Type >> 16) & 0xff);
return needsRelocateWithSymbol(Asm, Val, Sym, Type & 0xff) ||
needsRelocateWithSymbol(Asm, Val, Sym, (Type >> 8) & 0xff) ||
needsRelocateWithSymbol(Asm, Val, Sym, (Type >> 16) & 0xff);

switch (Type) {
default:
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace {
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;

bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};
}
Expand Down Expand Up @@ -488,7 +489,8 @@ unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
return Type;
}

bool PPCELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
bool PPCELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &,
const MCSymbol &Sym,
unsigned Type) const {
switch (Type) {
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class RISCVELFObjectWriter : public MCELFObjectTargetWriter {

// Return true if the given relocation must be with a symbol rather than
// section plus offset.
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override {
// TODO: this is very conservative, update once RISC-V psABI requirements
// are clarified.
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace {
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;

bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};
}
Expand Down Expand Up @@ -144,7 +145,8 @@ unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
return ELF::R_SPARC_NONE;
}

bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &,
const MCSymbol &,
unsigned Type) const {
switch (Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class SystemZELFObjectWriter : public MCELFObjectTargetWriter {
// Override MCELFObjectTargetWriter.
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};

Expand Down Expand Up @@ -215,7 +216,8 @@ unsigned SystemZELFObjectWriter::getRelocType(MCContext &Ctx,
}
}

bool SystemZELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
bool SystemZELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &V,
const MCSymbol &Sym,
unsigned Type) const {
switch (V.getSpecifier()) {
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class VEELFObjectWriter : public MCELFObjectTargetWriter {
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;

bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};
} // namespace
Expand Down Expand Up @@ -146,7 +147,8 @@ unsigned VEELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
return ELF::R_VE_NONE;
}

bool VEELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
bool VEELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &,
const MCSymbol &,
unsigned Type) const {
switch (Type) {
Expand Down
10 changes: 7 additions & 3 deletions llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "MCTargetDesc/X86MCTargetDesc.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCExpr.h"
Expand All @@ -33,7 +34,8 @@ class X86ELFObjectWriter : public MCELFObjectTargetWriter {
protected:
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};

Expand Down Expand Up @@ -386,7 +388,8 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
return getRelocType32(Ctx, Fixup.getLoc(), Specifier, RelType, IsPCRel, Kind);
}

bool X86ELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
bool X86ELFObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &V,
const MCSymbol &Sym,
unsigned Type) const {
switch (V.getSpecifier()) {
Expand All @@ -396,7 +399,8 @@ bool X86ELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
case X86MCExpr::VK_GOTPCREL_NORELAX:
return true;
default:
return false;
return Type == ELF::R_X86_64_PLT32 &&
(Asm.getFragmentOffset(*Sym.getFragment()) + Sym.getOffset()) != 0;
}
}

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/Xtensa/MCTargetDesc/XtensaELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class XtensaObjectWriter : public MCELFObjectTargetWriter {
protected:
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
bool needsRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
const MCSymbol &Sym,
unsigned Type) const override;
};
} // namespace
Expand Down Expand Up @@ -60,7 +61,8 @@ llvm::createXtensaObjectWriter(uint8_t OSABI, bool IsLittleEndian) {
return std::make_unique<XtensaObjectWriter>(OSABI);
}

bool XtensaObjectWriter::needsRelocateWithSymbol(const MCValue &,
bool XtensaObjectWriter::needsRelocateWithSymbol(const MCAssembler &Asm,
const MCValue &,
const MCSymbol &,
unsigned Type) const {
return false;
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/MC/ELF/plt-temporary.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -r - | FileCheck %s

// Test that this produces a R_X86_64_PLT32 pointing to .Lfoo
// instead of the section symbol.

.section .text.a,"ax",@progbits
jmp .Lfoo

.text
ret
.Lfoo:
ret

// CHECK: R_X86_64_PLT32 .Lfoo 0xFFFFFFFFFFFFFFFC