Skip to content

Commit

Permalink
[MC] Simplify ELFObjectWriter. NFC
Browse files Browse the repository at this point in the history
And fix `if (hasRelocationAddend())` to `usesRela` to properly treat
SHT_LLVM_CALL_GRAPH_PROFILE as SHT_REL. The incorrect does not cause a
problem because the synthesized SHT_LLVM_CALL_GRAPH_PROFILE has zero
addends.
  • Loading branch information
MaskRay committed Mar 28, 2024
1 parent 6b7ecc7 commit a41bfea
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions llvm/lib/MC/ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ struct ELFWriter {

// TargetObjectWriter wrappers.
bool is64Bit() const;
bool usesRela(const MCSectionELF &Sec) const;

uint64_t align(Align Alignment);

Expand Down Expand Up @@ -260,6 +259,7 @@ class ELFObjectWriter : public MCObjectWriter {
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue) override;
bool usesRela(const MCSectionELF &Sec) const;

void executePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) override;
Expand Down Expand Up @@ -394,11 +394,6 @@ bool ELFWriter::is64Bit() const {
return OWriter.TargetObjectWriter->is64Bit();
}

bool ELFWriter::usesRela(const MCSectionELF &Sec) const {
return OWriter.hasRelocationAddend() &&
Sec.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
}

// Emit the ELF header.
void ELFWriter::writeHeader(const MCAssembler &Asm) {
// ELF Header
Expand Down Expand Up @@ -825,24 +820,22 @@ MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
if (OWriter.Relocations[&Sec].empty())
return nullptr;

const StringRef SectionName = Sec.getName();
bool Rela = usesRela(Sec);
std::string RelaSectionName = Rela ? ".rela" : ".rel";
RelaSectionName += SectionName;
unsigned Flags = ELF::SHF_INFO_LINK;
if (Sec.getFlags() & ELF::SHF_GROUP)
Flags = ELF::SHF_GROUP;

const StringRef SectionName = Sec.getName();
const bool Rela = OWriter.usesRela(Sec);
unsigned EntrySize;
if (Rela)
EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
else
EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);

unsigned Flags = ELF::SHF_INFO_LINK;
if (Sec.getFlags() & ELF::SHF_GROUP)
Flags = ELF::SHF_GROUP;

MCSectionELF *RelaSection = Ctx.createELFRelSection(
RelaSectionName, Rela ? ELF::SHT_RELA : ELF::SHT_REL, Flags, EntrySize,
Sec.getGroup(), &Sec);
MCSectionELF *RelaSection =
Ctx.createELFRelSection(((Rela ? ".rela" : ".rel") + SectionName),
Rela ? ELF::SHT_RELA : ELF::SHT_REL, Flags,
EntrySize, Sec.getGroup(), &Sec);
RelaSection->setAlignment(is64Bit() ? Align(8) : Align(4));
return RelaSection;
}
Expand Down Expand Up @@ -938,11 +931,11 @@ void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
void ELFWriter::writeRelocations(const MCAssembler &Asm,
const MCSectionELF &Sec) {
std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
const bool Rela = OWriter.usesRela(Sec);

// Sort the relocation entries. MIPS needs this.
OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);

const bool Rela = usesRela(Sec);
if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
for (const ELFRelocationEntry &Entry : Relocs) {
uint32_t Symidx = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
Expand Down Expand Up @@ -1499,7 +1492,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
FixedValue = !RelocateWithSymbol && SymA && !SymA->isUndefined()
? C + Layout.getSymbolOffset(*SymA)
: C;
if (hasRelocationAddend()) {
if (usesRela(FixupSection)) {
Addend = FixedValue;
FixedValue = 0;
}
Expand Down Expand Up @@ -1528,6 +1521,11 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
Relocations[&FixupSection].push_back(Rec);
}

bool ELFObjectWriter::usesRela(const MCSectionELF &Sec) const {
return hasRelocationAddend() &&
Sec.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
}

bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
bool InSet, bool IsPCRel) const {
Expand Down

0 comments on commit a41bfea

Please sign in to comment.