Skip to content

Commit 9d3ef8a

Browse files
committed
ELFObjectWriter: Simplify
1 parent 35ee462 commit 9d3ef8a

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,15 +1309,13 @@ bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
13091309
bool ELFObjectWriter::checkRelocation(MCContext &Ctx, SMLoc Loc,
13101310
const MCSectionELF *From,
13111311
const MCSectionELF *To) {
1312-
if (DwoOS) {
1313-
if (isDwoSection(*From)) {
1314-
Ctx.reportError(Loc, "A dwo section may not contain relocations");
1315-
return false;
1316-
}
1317-
if (To && isDwoSection(*To)) {
1318-
Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
1319-
return false;
1320-
}
1312+
if (isDwoSection(*From)) {
1313+
Ctx.reportError(Loc, "A dwo section may not contain relocations");
1314+
return false;
1315+
}
1316+
if (To && isDwoSection(*To)) {
1317+
Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
1318+
return false;
13211319
}
13221320
return true;
13231321
}
@@ -1327,19 +1325,36 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13271325
const MCFixup &Fixup, MCValue Target,
13281326
uint64_t &FixedValue) {
13291327
MCAsmBackend &Backend = Asm.getBackend();
1330-
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
1331-
MCFixupKindInfo::FKF_IsPCRel;
13321328
const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
1333-
uint64_t C = Target.getConstant();
1334-
uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
13351329
MCContext &Ctx = Asm.getContext();
1336-
const MCTargetOptions *TO = Ctx.getTargetOptions();
13371330

1331+
const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym());
1332+
bool ViaWeakRef = false;
1333+
if (SymA && SymA->isVariable()) {
1334+
const MCExpr *Expr = SymA->getVariableValue();
1335+
if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
1336+
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
1337+
SymA = cast<MCSymbolELF>(&Inner->getSymbol());
1338+
ViaWeakRef = true;
1339+
}
1340+
}
1341+
}
1342+
1343+
const MCSectionELF *SecA = (SymA && SymA->isInSection())
1344+
? cast<MCSectionELF>(&SymA->getSection())
1345+
: nullptr;
1346+
if (DwoOS && !checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
1347+
return;
1348+
1349+
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
1350+
MCFixupKindInfo::FKF_IsPCRel;
1351+
uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
1352+
uint64_t Addend = Target.getConstant();
13381353
if (auto *RefB = Target.getSubSym()) {
13391354
// When there is no relocation specifier, a linker relaxation target may
13401355
// emit ADD/SUB relocations for A-B+C.
1341-
if (Target.getAddSym() && Backend.handleAddSubRelocations(
1342-
Asm, *Fragment, Fixup, Target, FixedValue))
1356+
if (SymA && Backend.handleAddSubRelocations(Asm, *Fragment, Fixup, Target,
1357+
FixedValue))
13431358
return;
13441359

13451360
const auto &SymB = cast<MCSymbolELF>(*RefB);
@@ -1360,29 +1375,9 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13601375

13611376
assert(!IsPCRel && "should have been folded");
13621377
IsPCRel = true;
1363-
C += FixupOffset - Asm.getSymbolOffset(SymB);
1378+
Addend += FixupOffset - Asm.getSymbolOffset(SymB);
13641379
}
13651380

1366-
// We either rejected the fixup or folded B into C at this point.
1367-
const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym());
1368-
1369-
bool ViaWeakRef = false;
1370-
if (SymA && SymA->isVariable()) {
1371-
const MCExpr *Expr = SymA->getVariableValue();
1372-
if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
1373-
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
1374-
SymA = cast<MCSymbolELF>(&Inner->getSymbol());
1375-
ViaWeakRef = true;
1376-
}
1377-
}
1378-
}
1379-
1380-
const MCSectionELF *SecA = (SymA && SymA->isInSection())
1381-
? cast<MCSectionELF>(&SymA->getSection())
1382-
: nullptr;
1383-
if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
1384-
return;
1385-
13861381
auto EMachine = TargetObjectWriter->getEMachine();
13871382
unsigned Type;
13881383
if (mc::isRelocRelocation(Fixup.getKind()))
@@ -1393,14 +1388,16 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13931388
bool UseSectionSym =
13941389
SymA && SymA->getBinding() == ELF::STB_LOCAL && !SymA->isUndefined();
13951390
if (UseSectionSym) {
1396-
UseSectionSym = useSectionSymbol(Asm, Target, SymA, C, Type);
1391+
UseSectionSym = useSectionSymbol(Asm, Target, SymA, Addend, Type);
13971392

13981393
// Disable STT_SECTION adjustment for .reloc directives.
13991394
UseSectionSym &= !mc::isRelocRelocation(Fixup.getKind());
1395+
1396+
if (UseSectionSym)
1397+
Addend += Asm.getSymbolOffset(*SymA);
14001398
}
14011399

1402-
uint64_t Addend = UseSectionSym ? C + Asm.getSymbolOffset(*SymA) : C;
1403-
FixedValue = usesRela(TO, FixupSection) ? 0 : Addend;
1400+
FixedValue = usesRela(Ctx.getTargetOptions(), FixupSection) ? 0 : Addend;
14041401
if (UseSectionSym) {
14051402
SymA = cast<MCSymbolELF>(SecA->getBeginSymbol());
14061403
SymA->setUsedInReloc();

0 commit comments

Comments
 (0)