@@ -1309,15 +1309,13 @@ bool ELFObjectWriter::useSectionSymbol(const MCAssembler &Asm,
1309
1309
bool ELFObjectWriter::checkRelocation (MCContext &Ctx, SMLoc Loc,
1310
1310
const MCSectionELF *From,
1311
1311
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 ;
1321
1319
}
1322
1320
return true ;
1323
1321
}
@@ -1327,19 +1325,36 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1327
1325
const MCFixup &Fixup, MCValue Target,
1328
1326
uint64_t &FixedValue) {
1329
1327
MCAsmBackend &Backend = Asm.getBackend ();
1330
- bool IsPCRel = Backend.getFixupKindInfo (Fixup.getKind ()).Flags &
1331
- MCFixupKindInfo::FKF_IsPCRel;
1332
1328
const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent ());
1333
- uint64_t C = Target.getConstant ();
1334
- uint64_t FixupOffset = Asm.getFragmentOffset (*Fragment) + Fixup.getOffset ();
1335
1329
MCContext &Ctx = Asm.getContext ();
1336
- const MCTargetOptions *TO = Ctx.getTargetOptions ();
1337
1330
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 ();
1338
1353
if (auto *RefB = Target.getSubSym ()) {
1339
1354
// When there is no relocation specifier, a linker relaxation target may
1340
1355
// 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))
1343
1358
return ;
1344
1359
1345
1360
const auto &SymB = cast<MCSymbolELF>(*RefB);
@@ -1360,29 +1375,9 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1360
1375
1361
1376
assert (!IsPCRel && " should have been folded" );
1362
1377
IsPCRel = true ;
1363
- C += FixupOffset - Asm.getSymbolOffset (SymB);
1378
+ Addend += FixupOffset - Asm.getSymbolOffset (SymB);
1364
1379
}
1365
1380
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
-
1386
1381
auto EMachine = TargetObjectWriter->getEMachine ();
1387
1382
unsigned Type;
1388
1383
if (mc::isRelocRelocation (Fixup.getKind ()))
@@ -1393,14 +1388,16 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1393
1388
bool UseSectionSym =
1394
1389
SymA && SymA->getBinding () == ELF::STB_LOCAL && !SymA->isUndefined ();
1395
1390
if (UseSectionSym) {
1396
- UseSectionSym = useSectionSymbol (Asm, Target, SymA, C , Type);
1391
+ UseSectionSym = useSectionSymbol (Asm, Target, SymA, Addend , Type);
1397
1392
1398
1393
// Disable STT_SECTION adjustment for .reloc directives.
1399
1394
UseSectionSym &= !mc::isRelocRelocation (Fixup.getKind ());
1395
+
1396
+ if (UseSectionSym)
1397
+ Addend += Asm.getSymbolOffset (*SymA);
1400
1398
}
1401
1399
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;
1404
1401
if (UseSectionSym) {
1405
1402
SymA = cast<MCSymbolELF>(SecA->getBeginSymbol ());
1406
1403
SymA->setUsedInReloc ();
0 commit comments