Skip to content

Commit fe518e7

Browse files
committed
ELFObjectWriter: Simplify STT_SECTION adjustment
1 parent 0004c37 commit fe518e7

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,6 @@ bool ELFObjectWriter::useSectionSymbol(const MCValue &Val,
12781278
// If we change such a relocation to use the section, the linker would think
12791279
// that it pointed to another string and subtracting 42 at runtime will
12801280
// produce the wrong value.
1281-
auto EMachine = TargetObjectWriter->getEMachine();
12821281
if (Sym->isInSection()) {
12831282
auto &Sec = cast<MCSectionELF>(Sym->getSection());
12841283
unsigned Flags = Sec.getFlags();
@@ -1288,7 +1287,8 @@ bool ELFObjectWriter::useSectionSymbol(const MCValue &Val,
12881287

12891288
// gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
12901289
// (http://sourceware.org/PR16794).
1291-
if (EMachine == ELF::EM_386 && Type == ELF::R_386_GOTOFF)
1290+
if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
1291+
Type == ELF::R_386_GOTOFF)
12921292
return false;
12931293

12941294
// ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
@@ -1298,7 +1298,8 @@ bool ELFObjectWriter::useSectionSymbol(const MCValue &Val,
12981298
// (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
12991299
// but the complexity is unnecessary given that GNU as keeps the original
13001300
// symbol for this case as well.
1301-
if (EMachine == ELF::EM_MIPS && !hasRelocationAddend())
1301+
if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
1302+
!hasRelocationAddend())
13021303
return false;
13031304
}
13041305

@@ -1331,14 +1332,14 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
13311332
const MCFixup &Fixup, MCValue Target,
13321333
uint64_t &FixedValue) {
13331334
MCAsmBackend &Backend = Asm->getBackend();
1334-
const MCSectionELF &FixupSection = cast<MCSectionELF>(*F.getParent());
1335+
const MCSectionELF &Section = cast<MCSectionELF>(*F.getParent());
13351336
MCContext &Ctx = getContext();
13361337

13371338
const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym());
13381339
const MCSectionELF *SecA = (SymA && SymA->isInSection())
13391340
? cast<MCSectionELF>(&SymA->getSection())
13401341
: nullptr;
1341-
if (DwoOS && !checkRelocation(Fixup.getLoc(), &FixupSection, SecA))
1342+
if (DwoOS && !checkRelocation(Fixup.getLoc(), &Section, SecA))
13421343
return;
13431344

13441345
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
@@ -1356,7 +1357,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
13561357

13571358
assert(!SymB.isAbsolute() && "Should have been folded");
13581359
const MCSection &SecB = SymB.getSection();
1359-
if (&SecB != &FixupSection) {
1360+
if (&SecB != &Section) {
13601361
Ctx.reportError(Fixup.getLoc(),
13611362
"Cannot represent a difference across sections");
13621363
return;
@@ -1373,31 +1374,23 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
13731374
else
13741375
Type = TargetObjectWriter->getRelocType(Fixup, Target, IsPCRel);
13751376

1376-
bool UseSectionSym =
1377-
SymA && SymA->getBinding() == ELF::STB_LOCAL && !SymA->isUndefined();
1378-
if (UseSectionSym) {
1379-
UseSectionSym = useSectionSymbol(Target, SymA, Addend, Type);
1380-
1381-
// Disable STT_SECTION adjustment for .reloc directives.
1382-
UseSectionSym &= !mc::isRelocRelocation(Fixup.getKind());
1383-
1384-
if (UseSectionSym)
1385-
Addend += Asm->getSymbolOffset(*SymA);
1386-
}
1387-
1388-
FixedValue = usesRela(Ctx.getTargetOptions(), FixupSection) ? 0 : Addend;
1389-
if (UseSectionSym) {
1377+
// Convert SymA to an STT_SECTION symbol if it's defined, local, and meets
1378+
// specific conditions, unless it's a .reloc directive, which disables
1379+
// STT_SECTION adjustment.
1380+
bool UseSectionSym = SymA && SymA->getBinding() == ELF::STB_LOCAL &&
1381+
!SymA->isUndefined() &&
1382+
!mc::isRelocRelocation(Fixup.getKind());
1383+
if (UseSectionSym && useSectionSymbol(Target, SymA, Addend, Type)) {
1384+
Addend += Asm->getSymbolOffset(*SymA);
13901385
SymA = cast<MCSymbolELF>(SecA->getBeginSymbol());
1386+
} else if (const MCSymbolELF *R = Renames.lookup(SymA)) {
1387+
SymA = R;
1388+
}
1389+
if (SymA)
13911390
SymA->setUsedInReloc();
1392-
} else {
1393-
if (SymA) {
1394-
if (const MCSymbolELF *R = Renames.lookup(SymA))
1395-
SymA = R;
13961391

1397-
SymA->setUsedInReloc();
1398-
}
1399-
}
1400-
Relocations[&FixupSection].emplace_back(FixupOffset, SymA, Type, Addend);
1392+
FixedValue = usesRela(Ctx.getTargetOptions(), Section) ? 0 : Addend;
1393+
Relocations[&Section].emplace_back(FixupOffset, SymA, Type, Addend);
14011394
}
14021395

14031396
bool ELFObjectWriter::usesRela(const MCTargetOptions *TO,

0 commit comments

Comments
 (0)