Skip to content

Commit adaf170

Browse files
committed
ELFObjectWriter: Simplify useSectionSymbol and writeSymbol
1 parent 6bbaef1 commit adaf170

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

llvm/include/llvm/MC/MCELFObjectWriter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ class ELFObjectWriter final : public MCObjectWriter {
180180
bool hasRelocationAddend() const;
181181
bool usesRela(const MCTargetOptions *TO, const MCSectionELF &Sec) const;
182182

183-
bool useSectionSymbol(const MCAssembler &Asm, const MCValue &Val,
184-
const MCSymbolELF *Sym, uint64_t C,
183+
bool useSectionSymbol(const MCValue &Val, const MCSymbolELF *Sym, uint64_t C,
185184
unsigned Type) const;
186185

187186
bool checkRelocation(MCContext &Ctx, SMLoc Loc, const MCSectionELF *From,

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ struct ELFWriter {
183183

184184
void writeHeader(const MCAssembler &Asm);
185185

186-
void writeSymbol(const MCAssembler &Asm, SymbolTableWriter &Writer,
187-
uint32_t StringIndex, ELFSymbolData &MSD);
186+
void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
187+
ELFSymbolData &MSD);
188188

189189
// Map from a signature symbol to the group section index
190190
using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>;
@@ -408,8 +408,8 @@ static bool isIFunc(const MCSymbolELF *Symbol) {
408408
return true;
409409
}
410410

411-
void ELFWriter::writeSymbol(const MCAssembler &Asm, SymbolTableWriter &Writer,
412-
uint32_t StringIndex, ELFSymbolData &MSD) {
411+
void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
412+
ELFSymbolData &MSD) {
413413
const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
414414
const MCSymbolELF *Base =
415415
cast_or_null<MCSymbolELF>(Asm.getBaseSymbol(Symbol));
@@ -647,7 +647,7 @@ void ELFWriter::computeSymbolTable(MCAssembler &Asm,
647647
? 0
648648
: StrTabBuilder.getOffset(MSD.Name);
649649
MSD.Symbol->setIndex(Index++);
650-
writeSymbol(Asm, Writer, StringIndex, MSD);
650+
writeSymbol(Writer, StringIndex, MSD);
651651
}
652652
for (; FileNameIt != FileNames.end(); ++FileNameIt) {
653653
Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
@@ -662,7 +662,7 @@ void ELFWriter::computeSymbolTable(MCAssembler &Asm,
662662
for (ELFSymbolData &MSD : ExternalSymbolData) {
663663
unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
664664
MSD.Symbol->setIndex(Index++);
665-
writeSymbol(Asm, Writer, StringIndex, MSD);
665+
writeSymbol(Writer, StringIndex, MSD);
666666
assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
667667
}
668668

@@ -1248,8 +1248,7 @@ void ELFObjectWriter::executePostLayoutBinding() {
12481248
// It is always valid to create a relocation with a symbol. It is preferable
12491249
// to use a relocation with a section if that is possible. Using the section
12501250
// allows us to omit some local symbols from the symbol table.
1251-
bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
1252-
const MCValue &Val,
1251+
bool ELFObjectWriter::useSectionSymbol(const MCValue &Val,
12531252
const MCSymbolELF *Sym, uint64_t C,
12541253
unsigned Type) const {
12551254
// Keep symbol type for a local ifunc because it may result in an IRELATIVE
@@ -1265,6 +1264,7 @@ bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
12651264
// If we change such a relocation to use the section, the linker would think
12661265
// that it pointed to another string and subtracting 42 at runtime will
12671266
// produce the wrong value.
1267+
auto EMachine = TargetObjectWriter->getEMachine();
12681268
if (Sym->isInSection()) {
12691269
auto &Sec = cast<MCSectionELF>(Sym->getSection());
12701270
unsigned Flags = Sec.getFlags();
@@ -1274,8 +1274,7 @@ bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
12741274

12751275
// gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
12761276
// (http://sourceware.org/PR16794).
1277-
if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
1278-
Type == ELF::R_386_GOTOFF)
1277+
if (EMachine == ELF::EM_386 && Type == ELF::R_386_GOTOFF)
12791278
return false;
12801279

12811280
// ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
@@ -1285,8 +1284,7 @@ bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
12851284
// (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
12861285
// but the complexity is unnecessary given that GNU as keeps the original
12871286
// symbol for this case as well.
1288-
if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
1289-
!hasRelocationAddend())
1287+
if (EMachine == ELF::EM_MIPS && !hasRelocationAddend())
12901288
return false;
12911289
}
12921290

@@ -1302,7 +1300,7 @@ bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
13021300
// bit. With a symbol that is done by just having the symbol have that bit
13031301
// set, so we would lose the bit if we relocated with the section.
13041302
// FIXME: We could use the section but add the bit to the relocation value.
1305-
if (Asm.isThumbFunc(Sym))
1303+
if (EMachine == ELF::EM_ARM && Asm->isThumbFunc(Sym))
13061304
return false;
13071305

13081306
return !TargetObjectWriter->needsRelocateWithSymbol(Val, *Sym, Type);
@@ -1382,7 +1380,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
13821380
bool UseSectionSym =
13831381
SymA && SymA->getBinding() == ELF::STB_LOCAL && !SymA->isUndefined();
13841382
if (UseSectionSym) {
1385-
UseSectionSym = useSectionSymbol(*Asm, Target, SymA, Addend, Type);
1383+
UseSectionSym = useSectionSymbol(Target, SymA, Addend, Type);
13861384

13871385
// Disable STT_SECTION adjustment for .reloc directives.
13881386
UseSectionSym &= !mc::isRelocRelocation(Fixup.getKind());

0 commit comments

Comments
 (0)