@@ -1246,53 +1246,15 @@ void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
1246
1246
// It is always valid to create a relocation with a symbol. It is preferable
1247
1247
// to use a relocation with a section if that is possible. Using the section
1248
1248
// 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 {
1291
1253
// Keep symbol type for a local ifunc because it may result in an IRELATIVE
1292
1254
// reloc that the dynamic loader will use to resolve the address at startup
1293
1255
// time.
1294
1256
if (Sym->getType () == ELF::STT_GNU_IFUNC)
1295
- return true ;
1257
+ return false ;
1296
1258
1297
1259
// If a relocation points to a mergeable section, we have to be careful.
1298
1260
// If the offset is zero, a relocation with the section will encode the
@@ -1306,13 +1268,13 @@ bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
1306
1268
unsigned Flags = Sec.getFlags ();
1307
1269
if (Flags & ELF::SHF_MERGE) {
1308
1270
if (C != 0 )
1309
- return true ;
1271
+ return false ;
1310
1272
1311
1273
// gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
1312
1274
// (http://sourceware.org/PR16794).
1313
1275
if (TargetObjectWriter->getEMachine () == ELF::EM_386 &&
1314
1276
Type == ELF::R_386_GOTOFF)
1315
- return true ;
1277
+ return false ;
1316
1278
1317
1279
// ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
1318
1280
// 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,
1323
1285
// symbol for this case as well.
1324
1286
if (TargetObjectWriter->getEMachine () == ELF::EM_MIPS &&
1325
1287
!hasRelocationAddend ())
1326
- return true ;
1288
+ return false ;
1327
1289
}
1328
1290
1329
1291
// Most TLS relocations use a got, so they need the symbol. Even those that
1330
1292
// are just an offset (@tpoff), require a symbol in gold versions before
1331
1293
// 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
1332
1294
// http://sourceware.org/PR16773.
1333
1295
if (Flags & ELF::SHF_TLS)
1334
- return true ;
1296
+ return false ;
1335
1297
}
1336
1298
1337
1299
// If the symbol is a thumb function the final relocation must set the lowest
1338
1300
// bit. With a symbol that is done by just having the symbol have that bit
1339
1301
// set, so we would lose the bit if we relocated with the section.
1340
1302
// FIXME: We could use the section but add the bit to the relocation value.
1341
1303
if (Asm.isThumbFunc (Sym))
1342
- return true ;
1304
+ return false ;
1343
1305
1344
- if (TargetObjectWriter->needsRelocateWithSymbol (Val, *Sym, Type))
1345
- return true ;
1346
- return false ;
1306
+ return !TargetObjectWriter->needsRelocateWithSymbol (Val, *Sym, Type);
1347
1307
}
1348
1308
1349
1309
bool ELFObjectWriter::checkRelocation (MCContext &Ctx, SMLoc Loc,
@@ -1425,17 +1385,19 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1425
1385
return ;
1426
1386
1427
1387
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
+ }
1437
1397
1438
- if (!RelocateWithSymbol) {
1398
+ uint64_t Addend = UseSectionSym ? C + Asm.getSymbolOffset (*SymA) : C;
1399
+ FixedValue = usesRela (TO, FixupSection) ? 0 : Addend;
1400
+ if (UseSectionSym) {
1439
1401
const auto *SectionSymbol =
1440
1402
SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol ()) : nullptr ;
1441
1403
if (SectionSymbol)
@@ -1445,6 +1407,12 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1445
1407
return ;
1446
1408
}
1447
1409
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
+
1448
1416
const MCSymbolELF *RenamedSymA = SymA;
1449
1417
if (SymA) {
1450
1418
if (const MCSymbolELF *R = Renames.lookup (SymA))
0 commit comments