Skip to content

Commit e373f7a

Browse files
committed
MC: Simplify recordRelocation
* Remove the MCAssembler * argument. Change subclasses to use MCAssembler *MCObjectWriter::Asm. * Remove pure specifier and add an empty implementation * Change MCFragment * to MCFragment &
1 parent 346a72f commit e373f7a

17 files changed

+63
-85
lines changed

llvm/include/llvm/MC/MCDXContainerWriter.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ class DXContainerObjectWriter final : public MCObjectWriter {
4242
raw_pwrite_stream &OS)
4343
: W(OS, llvm::endianness::little), TargetObjectWriter(std::move(MOTW)) {}
4444

45-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
46-
const MCFixup &Fixup, MCValue Target,
47-
uint64_t &FixedValue) override {}
48-
4945
uint64_t writeObject(MCAssembler &Asm) override;
5046
};
5147
} // end namespace llvm

llvm/include/llvm/MC/MCELFObjectWriter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@ class ELFObjectWriter final : public MCObjectWriter {
170170

171171
void reset() override;
172172
void executePostLayoutBinding() override;
173-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
174-
const MCFixup &Fixup, MCValue Target,
175-
uint64_t &FixedValue) override;
173+
void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
174+
MCValue Target, uint64_t &FixedValue) override;
176175
bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
177176
const MCFragment &FB, bool InSet,
178177
bool IsPCRel) const override;

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,8 @@ class MachObjectWriter final : public MCObjectWriter {
327327
Relocations[Sec].push_back(P);
328328
}
329329

330-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
331-
const MCFixup &Fixup, MCValue Target,
332-
uint64_t &FixedValue) override;
330+
void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
331+
MCValue Target, uint64_t &FixedValue) override;
333332

334333
void bindIndirectSymbols(MCAssembler &Asm);
335334

llvm/include/llvm/MC/MCObjectWriter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ class MCObjectWriter {
7878
/// post layout binding. The implementation is responsible for storing
7979
/// information about the relocation so that it can be emitted during
8080
/// writeObject().
81-
virtual void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
82-
const MCFixup &Fixup, MCValue Target,
83-
uint64_t &FixedValue) = 0;
81+
virtual void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
82+
MCValue Target, uint64_t &FixedValue);
8483

8584
/// Check whether the difference (A - B) between two symbol references is
8685
/// fully resolved.

llvm/include/llvm/MC/MCSPIRVObjectWriter.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ class SPIRVObjectWriter final : public MCObjectWriter {
4646
void setBuildVersion(unsigned Major, unsigned Minor, unsigned Bound);
4747

4848
private:
49-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
50-
const MCFixup &Fixup, MCValue Target,
51-
uint64_t &FixedValue) override {}
52-
5349
uint64_t writeObject(MCAssembler &Asm) override;
5450
void writeHeader(const MCAssembler &Asm);
5551
};

llvm/include/llvm/MC/MCWinCOFFObjectWriter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ class WinCOFFObjectWriter final : public MCObjectWriter {
6767
bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
6868
const MCFragment &FB, bool InSet,
6969
bool IsPCRel) const override;
70-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
71-
const MCFixup &Fixup, MCValue Target,
72-
uint64_t &FixedValue) override;
70+
void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
71+
MCValue Target, uint64_t &FixedValue) override;
7372
uint64_t writeObject(MCAssembler &Asm) override;
7473
int getSectionNumber(const MCSection &Section) const;
7574
};

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,12 +1322,11 @@ bool ELFObjectWriter::checkRelocation(MCContext &Ctx, SMLoc Loc,
13221322
return true;
13231323
}
13241324

1325-
void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1326-
const MCFragment *Fragment,
1325+
void ELFObjectWriter::recordRelocation(const MCFragment &F,
13271326
const MCFixup &Fixup, MCValue Target,
13281327
uint64_t &FixedValue) {
1329-
MCAsmBackend &Backend = Asm.getBackend();
1330-
const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
1328+
MCAsmBackend &Backend = Asm->getBackend();
1329+
const MCSectionELF &FixupSection = cast<MCSectionELF>(*F.getParent());
13311330
MCContext &Ctx = getContext();
13321331

13331332
const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym());
@@ -1350,7 +1349,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13501349

13511350
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
13521351
MCFixupKindInfo::FKF_IsPCRel;
1353-
uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
1352+
uint64_t FixupOffset = Asm->getFragmentOffset(F) + Fixup.getOffset();
13541353
uint64_t Addend = Target.getConstant();
13551354
if (auto *RefB = Target.getSubSym()) {
13561355
const auto &SymB = cast<MCSymbolELF>(*RefB);
@@ -1371,7 +1370,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13711370

13721371
assert(!IsPCRel && "should have been folded");
13731372
IsPCRel = true;
1374-
Addend += FixupOffset - Asm.getSymbolOffset(SymB);
1373+
Addend += FixupOffset - Asm->getSymbolOffset(SymB);
13751374
}
13761375

13771376
unsigned Type;
@@ -1383,13 +1382,13 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13831382
bool UseSectionSym =
13841383
SymA && SymA->getBinding() == ELF::STB_LOCAL && !SymA->isUndefined();
13851384
if (UseSectionSym) {
1386-
UseSectionSym = useSectionSymbol(Asm, Target, SymA, Addend, Type);
1385+
UseSectionSym = useSectionSymbol(*Asm, Target, SymA, Addend, Type);
13871386

13881387
// Disable STT_SECTION adjustment for .reloc directives.
13891388
UseSectionSym &= !mc::isRelocRelocation(Fixup.getKind());
13901389

13911390
if (UseSectionSym)
1392-
Addend += Asm.getSymbolOffset(*SymA);
1391+
Addend += Asm->getSymbolOffset(*SymA);
13931392
}
13941393

13951394
FixedValue = usesRela(Ctx.getTargetOptions(), FixupSection) ? 0 : Addend;

llvm/lib/MC/GOFFObjectWriter.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,8 @@ class GOFFObjectWriter : public MCObjectWriter {
243243
void writeEnd();
244244

245245
// Implementation of the MCObjectWriter interface.
246-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
247-
const MCFixup &Fixup, MCValue Target,
248-
uint64_t &FixedValue) override {}
246+
void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
247+
MCValue Target, uint64_t &FixedValue) override {}
249248
uint64_t writeObject(MCAssembler &Asm) override;
250249
};
251250
} // end anonymous namespace

llvm/lib/MC/MCAsmBackend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ bool MCAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
126126
if (IsResolved && shouldForceRelocation(Fixup, Target))
127127
IsResolved = false;
128128
if (!IsResolved)
129-
Asm->getWriter().recordRelocation(*Asm, &F, Fixup, Target, FixedValue);
129+
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
130130
return IsResolved;
131131
}
132132

llvm/lib/MC/MCObjectWriter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/MC/MCExpr.h"
1212
#include "llvm/MC/MCFragment.h"
1313
#include "llvm/MC/MCSymbol.h"
14+
#include "llvm/MC/MCValue.h"
1415
namespace llvm {
1516
class MCSection;
1617
}
@@ -29,6 +30,9 @@ void MCObjectWriter::reset() {
2930
CGProfile.clear();
3031
}
3132

33+
void MCObjectWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,
34+
MCValue Target, uint64_t &FixedValue) {}
35+
3236
bool MCObjectWriter::isSymbolRefDifferenceFullyResolved(const MCSymbol &SA,
3337
const MCSymbol &SB,
3438
bool InSet) const {

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,7 @@ static bool isFixupTargetValid(const MCValue &Target) {
511511
return true;
512512
}
513513

514-
void MachObjectWriter::recordRelocation(MCAssembler &Asm,
515-
const MCFragment *Fragment,
514+
void MachObjectWriter::recordRelocation(const MCFragment &F,
516515
const MCFixup &Fixup, MCValue Target,
517516
uint64_t &FixedValue) {
518517
if (!isFixupTargetValid(Target)) {
@@ -521,7 +520,7 @@ void MachObjectWriter::recordRelocation(MCAssembler &Asm,
521520
return;
522521
}
523522

524-
TargetObjectWriter->recordRelocation(this, Asm, Fragment, Fixup, Target,
523+
TargetObjectWriter->recordRelocation(this, *Asm, &F, Fixup, Target,
525524
FixedValue);
526525
}
527526

llvm/lib/MC/WasmObjectWriter.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,8 @@ class WasmObjectWriter : public MCObjectWriter {
292292

293293
void writeHeader(const MCAssembler &Asm);
294294

295-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
296-
const MCFixup &Fixup, MCValue Target,
297-
uint64_t &FixedValue) override;
295+
void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
296+
MCValue Target, uint64_t &FixedValue) override;
298297

299298
void executePostLayoutBinding() override;
300299
void prepareImports(SmallVectorImpl<wasm::WasmImport> &Imports,
@@ -476,17 +475,16 @@ void WasmObjectWriter::executePostLayoutBinding() {
476475
}
477476
}
478477

479-
void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
480-
const MCFragment *Fragment,
478+
void WasmObjectWriter::recordRelocation(const MCFragment &F,
481479
const MCFixup &Fixup, MCValue Target,
482480
uint64_t &FixedValue) {
483481
// The WebAssembly backend should never generate FKF_IsPCRel fixups
484-
assert(!(Asm.getBackend().getFixupKindInfo(Fixup.getKind()).Flags &
482+
assert(!(Asm->getBackend().getFixupKindInfo(Fixup.getKind()).Flags &
485483
MCFixupKindInfo::FKF_IsPCRel));
486484

487-
const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
485+
const auto &FixupSection = cast<MCSectionWasm>(*F.getParent());
488486
uint64_t C = Target.getConstant();
489-
uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
487+
uint64_t FixupOffset = Asm->getFragmentOffset(F) + Fixup.getOffset();
490488
MCContext &Ctx = getContext();
491489
bool IsLocRel = false;
492490

@@ -515,7 +513,7 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
515513
return;
516514
}
517515
IsLocRel = true;
518-
C += FixupOffset - Asm.getSymbolOffset(SymB);
516+
C += FixupOffset - Asm->getSymbolOffset(SymB);
519517
}
520518

521519
// We either rejected the fixup or folded B into C at this point.
@@ -571,7 +569,7 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
571569
if (!SectionSymbol)
572570
report_fatal_error("section symbol is required for relocation");
573571

574-
C += Asm.getSymbolOffset(*SymA);
572+
C += Asm->getSymbolOffset(*SymA);
575573
SymA = cast<MCSymbolWasm>(SectionSymbol);
576574
}
577575

@@ -592,7 +590,7 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
592590
report_fatal_error("__indirect_function_table symbol has wrong type");
593591
// Ensure that __indirect_function_table reaches the output.
594592
Sym->setNoStrip();
595-
Asm.registerSymbol(*Sym);
593+
Asm->registerSymbol(*Sym);
596594
}
597595
}
598596

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class llvm::WinCOFFWriter {
159159

160160
void reset();
161161
void executePostLayoutBinding(MCAssembler &Asm);
162-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
162+
void recordRelocation(MCAssembler &Asm, const MCFragment &F,
163163
const MCFixup &Fixup, MCValue Target,
164164
uint64_t &FixedValue);
165165
uint64_t writeObject(MCAssembler &Asm);
@@ -834,8 +834,7 @@ void WinCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
834834
assignSectionNumbers();
835835
}
836836

837-
void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
838-
const MCFragment *Fragment,
837+
void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
839838
const MCFixup &Fixup, MCValue Target,
840839
uint64_t &FixedValue) {
841840
assert(Target.getAddSym() && "Relocation must reference a symbol!");
@@ -853,7 +852,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
853852
return;
854853
}
855854

856-
MCSection *MCSec = Fragment->getParent();
855+
MCSection *MCSec = F.getParent();
857856

858857
// Mark this symbol as requiring an entry in the symbol table.
859858
assert(SectionMap.contains(MCSec) &&
@@ -873,8 +872,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
873872
int64_t OffsetOfB = Asm.getSymbolOffset(*B);
874873

875874
// Offset of the relocation in the section
876-
int64_t OffsetOfRelocation =
877-
Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
875+
int64_t OffsetOfRelocation = Asm.getFragmentOffset(F) + Fixup.getOffset();
878876

879877
FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
880878
} else {
@@ -884,7 +882,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
884882
COFFRelocation Reloc;
885883

886884
Reloc.Data.SymbolTableIndex = 0;
887-
Reloc.Data.VirtualAddress = Asm.getFragmentOffset(*Fragment);
885+
Reloc.Data.VirtualAddress = Asm.getFragmentOffset(F);
888886

889887
// Turn relocations for temporary symbols into section relocations.
890888
if (A.isTemporary() && !SymbolMap[&A]) {
@@ -1193,13 +1191,11 @@ void WinCOFFObjectWriter::executePostLayoutBinding() {
11931191
DwoWriter->executePostLayoutBinding(*Asm);
11941192
}
11951193

1196-
void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
1197-
const MCFragment *Fragment,
1194+
void WinCOFFObjectWriter::recordRelocation(const MCFragment &F,
11981195
const MCFixup &Fixup, MCValue Target,
11991196
uint64_t &FixedValue) {
1200-
assert(!isDwoSection(*Fragment->getParent()) &&
1201-
"No relocation in Dwo sections");
1202-
ObjWriter->recordRelocation(Asm, Fragment, Fixup, Target, FixedValue);
1197+
assert(!isDwoSection(*F.getParent()) && "No relocation in Dwo sections");
1198+
ObjWriter->recordRelocation(*Asm, F, Fixup, Target, FixedValue);
12031199
}
12041200

12051201
uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm) {

llvm/lib/MC/XCOFFObjectWriter.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ class XCOFFWriter final : public XCOFFObjectWriter {
350350

351351
void executePostLayoutBinding() override;
352352

353-
void recordRelocation(MCAssembler &, const MCFragment *, const MCFixup &,
354-
MCValue, uint64_t &) override;
353+
void recordRelocation(const MCFragment &, const MCFixup &, MCValue,
354+
uint64_t &) override;
355355

356356
uint64_t writeObject(MCAssembler &) override;
357357

@@ -656,9 +656,8 @@ void XCOFFWriter::executePostLayoutBinding() {
656656
assignAddressesAndIndices(*Asm);
657657
}
658658

659-
void XCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
660-
const MCFixup &Fixup, MCValue Target,
661-
uint64_t &FixedValue) {
659+
void XCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,
660+
MCValue Target, uint64_t &FixedValue) {
662661
auto getIndex = [this](const MCSymbol *Sym,
663662
const MCSectionXCOFF *ContainingCsect) {
664663
// If we could not find the symbol directly in SymbolIndexMap, this symbol
@@ -671,24 +670,24 @@ void XCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
671670
};
672671

673672
auto getVirtualAddress =
674-
[this, &Asm](const MCSymbol *Sym,
675-
const MCSectionXCOFF *ContainingSect) -> uint64_t {
673+
[this](const MCSymbol *Sym,
674+
const MCSectionXCOFF *ContainingSect) -> uint64_t {
676675
// A DWARF section.
677676
if (ContainingSect->isDwarfSect())
678-
return Asm.getSymbolOffset(*Sym);
677+
return Asm->getSymbolOffset(*Sym);
679678

680679
// A csect.
681680
if (!Sym->isDefined())
682681
return SectionMap[ContainingSect]->Address;
683682

684683
// A label.
685684
assert(Sym->isDefined() && "not a valid object that has address!");
686-
return SectionMap[ContainingSect]->Address + Asm.getSymbolOffset(*Sym);
685+
return SectionMap[ContainingSect]->Address + Asm->getSymbolOffset(*Sym);
687686
};
688687

689688
const MCSymbol *const SymA = Target.getAddSym();
690689

691-
MCAsmBackend &Backend = Asm.getBackend();
690+
MCAsmBackend &Backend = Asm->getBackend();
692691
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
693692
MCFixupKindInfo::FKF_IsPCRel;
694693

@@ -701,11 +700,9 @@ void XCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
701700
assert(SectionMap.contains(SymASec) &&
702701
"Expected containing csect to exist in map.");
703702

704-
assert((Fixup.getOffset() <=
705-
MaxRawDataSize - Asm.getFragmentOffset(*Fragment)) &&
703+
assert((Fixup.getOffset() <= MaxRawDataSize - Asm->getFragmentOffset(F)) &&
706704
"Fragment offset + fixup offset is overflowed.");
707-
uint32_t FixupOffsetInCsect =
708-
Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
705+
uint32_t FixupOffsetInCsect = Asm->getFragmentOffset(F) + Fixup.getOffset();
709706

710707
const uint32_t Index = getIndex(SymA, SymASec);
711708
if (Type == XCOFF::RelocationType::R_POS ||
@@ -756,7 +753,7 @@ void XCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
756753
FixedValue = TOCEntryOffset;
757754
}
758755
} else if (Type == XCOFF::RelocationType::R_RBR) {
759-
MCSectionXCOFF *ParentSec = cast<MCSectionXCOFF>(Fragment->getParent());
756+
MCSectionXCOFF *ParentSec = cast<MCSectionXCOFF>(F.getParent());
760757
assert((SymASec->getMappingClass() == XCOFF::XMC_PR &&
761758
ParentSec->getMappingClass() == XCOFF::XMC_PR) &&
762759
"Only XMC_PR csect may have the R_RBR relocation.");
@@ -777,7 +774,7 @@ void XCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
777774
}
778775

779776
XCOFFRelocation Reloc = {Index, FixupOffsetInCsect, SignAndSize, Type};
780-
MCSectionXCOFF *RelocationSec = cast<MCSectionXCOFF>(Fragment->getParent());
777+
MCSectionXCOFF *RelocationSec = cast<MCSectionXCOFF>(F.getParent());
781778
assert(SectionMap.contains(RelocationSec) &&
782779
"Expected containing csect to exist in map.");
783780
SectionMap[RelocationSec]->Relocations.push_back(Reloc);

llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ bool AVRAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
380380
IsResolved = false;
381381
}
382382
if (!IsResolved)
383-
Asm->getWriter().recordRelocation(*Asm, &F, Fixup, Target, FixedValue);
383+
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
384384
return IsResolved;
385385
}
386386

0 commit comments

Comments
 (0)