Skip to content

Commit 329a675

Browse files
committed
ELFObjectWriter: Simplify STT_SECTION adjustment. NFC
1 parent 3e7be49 commit 329a675

File tree

2 files changed

+32
-64
lines changed

2 files changed

+32
-64
lines changed

llvm/include/llvm/MC/MCELFObjectWriter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ class ELFObjectWriter final : public MCObjectWriter {
182182
bool hasRelocationAddend() const;
183183
bool usesRela(const MCTargetOptions *TO, const MCSectionELF &Sec) const;
184184

185-
bool shouldRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
186-
const MCSymbolELF *Sym, uint64_t C,
187-
unsigned Type) const;
185+
bool useSectionSymbol(const MCAssembler &Asm, const MCValue &Val,
186+
const MCSymbolELF *Sym, uint64_t C,
187+
unsigned Type) const;
188188

189189
bool checkRelocation(MCContext &Ctx, SMLoc Loc, const MCSectionELF *From,
190190
const MCSectionELF *To);

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,53 +1246,15 @@ void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
12461246
// It is always valid to create a relocation with a symbol. It is preferable
12471247
// to use a relocation with a section if that is possible. Using the section
12481248
// allows us to omit some local symbols from the symbol table.
1249-
bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
1250-
const MCValue &Val,
1251-
const MCSymbolELF *Sym,
1252-
uint64_t C,
1253-
unsigned Type) const {
1254-
// A PCRel relocation to an absolute value has no symbol (or section). We
1255-
// represent that with a relocation to a null section.
1256-
if (!Val.getAddSym())
1257-
return false;
1258-
1259-
// An undefined symbol is not in any section, so the relocation has to point
1260-
// to the symbol itself.
1261-
assert(Sym && "Expected a symbol");
1262-
if (Sym->isUndefined()) {
1263-
// The .odp creation emits a relocation against the symbol ".TOC." which
1264-
// create a R_PPC64_TOC relocation. However the relocation symbol name
1265-
// in final object creation should be NULL, since the symbol does not
1266-
// really exist, it is just the reference to TOC base for the current
1267-
// object file. Since the symbol is undefined, returning false results
1268-
// in a relocation with a null section which is the desired result.
1269-
return !(Type == ELF::R_PPC64_TOC &&
1270-
TargetObjectWriter->getEMachine() == ELF::EM_PPC64);
1271-
}
1272-
1273-
unsigned Binding = Sym->getBinding();
1274-
switch(Binding) {
1275-
default:
1276-
llvm_unreachable("Invalid Binding");
1277-
case ELF::STB_LOCAL:
1278-
break;
1279-
case ELF::STB_WEAK:
1280-
// If the symbol is weak, it might be overridden by a symbol in another
1281-
// file. The relocation has to point to the symbol so that the linker
1282-
// can update it.
1283-
return true;
1284-
case ELF::STB_GLOBAL:
1285-
case ELF::STB_GNU_UNIQUE:
1286-
// Global ELF symbols can be preempted by the dynamic linker. The relocation
1287-
// has to point to the symbol for a reason analogous to the STB_WEAK case.
1288-
return true;
1289-
}
1290-
1249+
bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
1250+
const MCValue &Val,
1251+
const MCSymbolELF *Sym, uint64_t C,
1252+
unsigned Type) const {
12911253
// Keep symbol type for a local ifunc because it may result in an IRELATIVE
12921254
// reloc that the dynamic loader will use to resolve the address at startup
12931255
// time.
12941256
if (Sym->getType() == ELF::STT_GNU_IFUNC)
1295-
return true;
1257+
return false;
12961258

12971259
// If a relocation points to a mergeable section, we have to be careful.
12981260
// If the offset is zero, a relocation with the section will encode the
@@ -1306,13 +1268,13 @@ bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
13061268
unsigned Flags = Sec.getFlags();
13071269
if (Flags & ELF::SHF_MERGE) {
13081270
if (C != 0)
1309-
return true;
1271+
return false;
13101272

13111273
// gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
13121274
// (http://sourceware.org/PR16794).
13131275
if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
13141276
Type == ELF::R_386_GOTOFF)
1315-
return true;
1277+
return false;
13161278

13171279
// ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
13181280
// it doesn't know that an R_MIPS_HI16 with implicit addend 1 and an
@@ -1323,27 +1285,25 @@ bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
13231285
// symbol for this case as well.
13241286
if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
13251287
!hasRelocationAddend())
1326-
return true;
1288+
return false;
13271289
}
13281290

13291291
// Most TLS relocations use a got, so they need the symbol. Even those that
13301292
// are just an offset (@tpoff), require a symbol in gold versions before
13311293
// 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
13321294
// http://sourceware.org/PR16773.
13331295
if (Flags & ELF::SHF_TLS)
1334-
return true;
1296+
return false;
13351297
}
13361298

13371299
// If the symbol is a thumb function the final relocation must set the lowest
13381300
// bit. With a symbol that is done by just having the symbol have that bit
13391301
// set, so we would lose the bit if we relocated with the section.
13401302
// FIXME: We could use the section but add the bit to the relocation value.
13411303
if (Asm.isThumbFunc(Sym))
1342-
return true;
1304+
return false;
13431305

1344-
if (TargetObjectWriter->needsRelocateWithSymbol(Val, *Sym, Type))
1345-
return true;
1346-
return false;
1306+
return !TargetObjectWriter->needsRelocateWithSymbol(Val, *Sym, Type);
13471307
}
13481308

13491309
bool ELFObjectWriter::checkRelocation(MCContext &Ctx, SMLoc Loc,
@@ -1425,17 +1385,19 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
14251385
return;
14261386

14271387
unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
1428-
const auto *Parent = cast<MCSectionELF>(Fragment->getParent());
1429-
// Emiting relocation with sybmol for CG Profile to help with --cg-profile.
1430-
bool RelocateWithSymbol =
1431-
shouldRelocateWithSymbol(Asm, Target, SymA, C, Type) ||
1432-
(Parent->getType() == ELF::SHT_LLVM_CALL_GRAPH_PROFILE);
1433-
uint64_t Addend = !RelocateWithSymbol && SymA && !SymA->isUndefined()
1434-
? C + Asm.getSymbolOffset(*SymA)
1435-
: C;
1436-
FixedValue = usesRela(TO, FixupSection) ? 0 : Addend;
1388+
bool UseSectionSym =
1389+
SymA && SymA->getBinding() == ELF::STB_LOCAL && !SymA->isUndefined();
1390+
if (UseSectionSym) {
1391+
UseSectionSym = useSectionSymbol(Asm, Target, SymA, C, Type);
1392+
1393+
// Disable STT_SECTION adjustment for CG Profile to help with --cg-profile.
1394+
const auto *Parent = cast<MCSectionELF>(Fragment->getParent());
1395+
UseSectionSym &= Parent->getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
1396+
}
14371397

1438-
if (!RelocateWithSymbol) {
1398+
uint64_t Addend = UseSectionSym ? C + Asm.getSymbolOffset(*SymA) : C;
1399+
FixedValue = usesRela(TO, FixupSection) ? 0 : Addend;
1400+
if (UseSectionSym) {
14391401
const auto *SectionSymbol =
14401402
SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
14411403
if (SectionSymbol)
@@ -1445,6 +1407,12 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
14451407
return;
14461408
}
14471409

1410+
// In PPC64 ELFv1, .quad .TOC.@tocbase in the .opd section is expected to
1411+
// reference the null symbol.
1412+
if (Type == ELF::R_PPC64_TOC &&
1413+
TargetObjectWriter->getEMachine() == ELF::EM_PPC64)
1414+
SymA = nullptr;
1415+
14481416
const MCSymbolELF *RenamedSymA = SymA;
14491417
if (SymA) {
14501418
if (const MCSymbolELF *R = Renames.lookup(SymA))

0 commit comments

Comments
 (0)