Skip to content

Commit 2849b12

Browse files
committed
MCObjectwriter: Add getContext and simplify code
1 parent 1e48418 commit 2849b12

File tree

7 files changed

+42
-36
lines changed

7 files changed

+42
-36
lines changed

llvm/include/llvm/MC/MCObjectWriter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class MCObjectWriter {
5757

5858
void setAssembler(MCAssembler *A) { Asm = A; }
5959

60+
MCContext &getContext() const;
61+
6062
/// lifetime management
6163
virtual void reset();
6264

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class SymbolTableWriter {
110110
};
111111

112112
struct ELFWriter {
113+
MCAssembler &Asm;
113114
ELFObjectWriter &OWriter;
114115
support::endian::Writer W;
115116

@@ -160,12 +161,15 @@ struct ELFWriter {
160161
Align Alignment);
161162

162163
public:
163-
ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
164+
ELFWriter(MCAssembler &Asm, ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
164165
bool IsLittleEndian, DwoMode Mode)
165-
: OWriter(OWriter), W(OS, IsLittleEndian ? llvm::endianness::little
166-
: llvm::endianness::big),
166+
: Asm(Asm), OWriter(OWriter),
167+
W(OS,
168+
IsLittleEndian ? llvm::endianness::little : llvm::endianness::big),
167169
Mode(Mode) {}
168170

171+
MCContext &getContext() const { return Asm.getContext(); }
172+
169173
void writeWord(uint64_t Word) {
170174
if (is64Bit())
171175
W.write<uint64_t>(Word);
@@ -205,7 +209,7 @@ struct ELFWriter {
205209
uint32_t Link, uint32_t Info,
206210
MaybeAlign Alignment, uint64_t EntrySize);
207211

208-
void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
212+
void writeRelocations(const MCSectionELF &Sec);
209213

210214
uint64_t writeObject(MCAssembler &Asm);
211215
void writeSectionHeader(uint32_t GroupSymbolIndex, uint64_t Offset,
@@ -818,10 +822,9 @@ static void encodeCrel(ArrayRef<ELFRelocationEntry> Relocs, raw_ostream &OS) {
818822
});
819823
}
820824

821-
void ELFWriter::writeRelocations(const MCAssembler &Asm,
822-
const MCSectionELF &Sec) {
825+
void ELFWriter::writeRelocations(const MCSectionELF &Sec) {
823826
std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
824-
const MCTargetOptions *TO = Asm.getContext().getTargetOptions();
827+
const MCTargetOptions *TO = getContext().getTargetOptions();
825828
const bool Rela = OWriter.usesRela(TO, Sec);
826829

827830
// Sort the relocation entries. MIPS needs this.
@@ -1017,7 +1020,7 @@ void ELFWriter::writeSectionHeaders(const MCAssembler &Asm) {
10171020
uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
10181021
uint64_t StartOffset = W.OS.tell();
10191022

1020-
MCContext &Ctx = Asm.getContext();
1023+
MCContext &Ctx = getContext();
10211024
MCSectionELF *StrtabSection =
10221025
Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
10231026
StringTableIndex = addToSectionTable(StrtabSection);
@@ -1111,8 +1114,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
11111114
// Remember the offset into the file for this section.
11121115
const uint64_t SecStart = align(RelSection->getAlign());
11131116

1114-
writeRelocations(Asm,
1115-
cast<MCSectionELF>(*RelSection->getLinkedToSection()));
1117+
writeRelocations(cast<MCSectionELF>(*RelSection->getLinkedToSection()));
11161118

11171119
uint64_t SecEnd = W.OS.tell();
11181120
RelSection->setOffsets(SecStart, SecEnd);
@@ -1326,7 +1328,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13261328
uint64_t &FixedValue) {
13271329
MCAsmBackend &Backend = Asm.getBackend();
13281330
const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
1329-
MCContext &Ctx = Asm.getContext();
1331+
MCContext &Ctx = getContext();
13301332

13311333
const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym());
13321334
bool ViaWeakRef = false;
@@ -1429,11 +1431,11 @@ bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
14291431

14301432
uint64_t ELFObjectWriter::writeObject(MCAssembler &Asm) {
14311433
uint64_t Size =
1432-
ELFWriter(*this, OS, IsLittleEndian,
1434+
ELFWriter(Asm, *this, OS, IsLittleEndian,
14331435
DwoOS ? ELFWriter::NonDwoOnly : ELFWriter::AllSections)
14341436
.writeObject(Asm);
14351437
if (DwoOS)
1436-
Size += ELFWriter(*this, *DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
1438+
Size += ELFWriter(Asm, *this, *DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
14371439
.writeObject(Asm);
14381440
return Size;
14391441
}

llvm/lib/MC/MCDXContainerWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm) {
8686
dxbc::ProgramHeader Header;
8787
memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
8888

89-
const Triple &TT = Asm.getContext().getTargetTriple();
89+
const Triple &TT = getContext().getTargetTriple();
9090
VersionTuple Version = TT.getOSVersion();
9191
uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor());
9292
uint8_t MinorVersion =

llvm/lib/MC/MCObjectWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ using namespace llvm;
1919

2020
MCObjectWriter::~MCObjectWriter() = default;
2121

22+
MCContext &MCObjectWriter::getContext() const { return Asm->getContext(); }
23+
2224
void MCObjectWriter::reset() {
2325
FileNames.clear();
2426
AddrsigSyms.clear();

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ void MachObjectWriter::recordRelocation(MCAssembler &Asm,
516516
const MCFixup &Fixup, MCValue Target,
517517
uint64_t &FixedValue) {
518518
if (!isFixupTargetValid(Target)) {
519-
Asm.getContext().reportError(Fixup.getLoc(),
520-
"unsupported relocation expression");
519+
getContext().reportError(Fixup.getLoc(),
520+
"unsupported relocation expression");
521521
return;
522522
}
523523

@@ -778,7 +778,7 @@ static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type) {
778778

779779
void MachObjectWriter::populateAddrSigSection(MCAssembler &Asm) {
780780
MCSection *AddrSigSection =
781-
Asm.getContext().getObjectFileInfo()->getAddrSigSection();
781+
getContext().getObjectFileInfo()->getAddrSigSection();
782782
unsigned Log2Size = is64Bit() ? 3 : 2;
783783
for (const MCSymbol *S : getAddrsigSyms()) {
784784
if (!S->isRegistered())
@@ -801,7 +801,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
801801
UndefinedSymbolData);
802802

803803
if (!CGProfile.empty()) {
804-
MCSection *CGProfileSection = Asm.getContext().getMachOSection(
804+
MCSection *CGProfileSection = getContext().getMachOSection(
805805
"__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
806806
auto &Frag = cast<MCDataFragment>(*CGProfileSection->begin());
807807
Frag.getContents().clear();
@@ -920,12 +920,12 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
920920
Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS;
921921
if (!cast<MCSectionMachO>(Sec).isVirtualSection() &&
922922
!isUInt<32>(SectionStart)) {
923-
Asm.getContext().reportError(
923+
getContext().reportError(
924924
SMLoc(), "cannot encode offset of section; object file too large");
925925
return NumBytesWritten();
926926
}
927927
if (NumRelocs && !isUInt<32>(RelocTableEnd)) {
928-
Asm.getContext().reportError(
928+
getContext().reportError(
929929
SMLoc(),
930930
"cannot encode offset of relocations; object file too large");
931931
return NumBytesWritten();

llvm/lib/MC/WasmObjectWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
487487
const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
488488
uint64_t C = Target.getConstant();
489489
uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
490-
MCContext &Ctx = Asm.getContext();
490+
MCContext &Ctx = getContext();
491491
bool IsLocRel = false;
492492

493493
if (const auto *RefB = Target.getSubSym()) {
@@ -1929,7 +1929,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
19291929
writeGlobalSection(Globals);
19301930
writeExportSection(Exports);
19311931
const MCSymbol *IndirectFunctionTable =
1932-
Asm.getContext().lookupSymbol("__indirect_function_table");
1932+
getContext().lookupSymbol("__indirect_function_table");
19331933
writeElemSection(cast_or_null<const MCSymbolWasm>(IndirectFunctionTable),
19341934
TableElems);
19351935
writeDataCountSection();

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class llvm::WinCOFFWriter {
166166
int getSectionNumber(const MCSection &Section) const;
167167

168168
private:
169+
MCContext &getContext() const { return OWriter.getContext(); }
169170
COFFSymbol *createSymbol(StringRef Name);
170171
COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
171172
COFFSection *createSection(StringRef Name);
@@ -841,15 +842,14 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
841842

842843
const MCSymbol &A = *Target.getAddSym();
843844
if (!A.isRegistered()) {
844-
Asm.getContext().reportError(Fixup.getLoc(), Twine("symbol '") +
845-
A.getName() +
846-
"' can not be undefined");
845+
getContext().reportError(Fixup.getLoc(), Twine("symbol '") + A.getName() +
846+
"' can not be undefined");
847847
return;
848848
}
849849
if (A.isTemporary() && A.isUndefined()) {
850-
Asm.getContext().reportError(Fixup.getLoc(), Twine("assembler label '") +
851-
A.getName() +
852-
"' can not be undefined");
850+
getContext().reportError(Fixup.getLoc(), Twine("assembler label '") +
851+
A.getName() +
852+
"' can not be undefined");
853853
return;
854854
}
855855

@@ -862,7 +862,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
862862
COFFSection *Sec = SectionMap[MCSec];
863863
if (const MCSymbol *B = Target.getSubSym()) {
864864
if (!B->getFragment()) {
865-
Asm.getContext().reportError(
865+
getContext().reportError(
866866
Fixup.getLoc(),
867867
Twine("symbol '") + B->getName() +
868868
"' can not be undefined in a subtraction expression");
@@ -920,7 +920,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
920920

921921
Reloc.Data.VirtualAddress += Fixup.getOffset();
922922
Reloc.Data.Type = OWriter.TargetObjectWriter->getRelocType(
923-
Asm.getContext(), Target, Fixup, Target.getSubSym(), Asm.getBackend());
923+
getContext(), Target, Fixup, Target.getSubSym(), Asm.getBackend());
924924

925925
// The *_REL32 relocations are relative to the end of the relocation,
926926
// not to the start.
@@ -1053,7 +1053,7 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
10531053
// It's an error to try to associate with an undefined symbol or a symbol
10541054
// without a section.
10551055
if (!AssocMCSym->isInSection()) {
1056-
Asm.getContext().reportError(
1056+
getContext().reportError(
10571057
SMLoc(), Twine("cannot make section ") + MCSec.getName() +
10581058
Twine(" associative with sectionless symbol ") +
10591059
AssocMCSym->getName());
@@ -1073,8 +1073,8 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
10731073

10741074
// Create the contents of the .llvm_addrsig section.
10751075
if (Mode != DwoOnly && OWriter.getEmitAddrsigSection()) {
1076-
auto *Sec = Asm.getContext().getCOFFSection(
1077-
".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE);
1076+
auto *Sec = getContext().getCOFFSection(".llvm_addrsig",
1077+
COFF::IMAGE_SCN_LNK_REMOVE);
10781078
auto *Frag = cast<MCDataFragment>(Sec->curFragList()->Head);
10791079
raw_svector_ostream OS(Frag->getContents());
10801080
for (const MCSymbol *S : OWriter.AddrsigSyms) {
@@ -1095,8 +1095,8 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
10951095

10961096
// Create the contents of the .llvm.call-graph-profile section.
10971097
if (Mode != DwoOnly && !OWriter.getCGProfile().empty()) {
1098-
auto *Sec = Asm.getContext().getCOFFSection(
1099-
".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE);
1098+
auto *Sec = getContext().getCOFFSection(".llvm.call-graph-profile",
1099+
COFF::IMAGE_SCN_LNK_REMOVE);
11001100
auto *Frag = cast<MCDataFragment>(Sec->curFragList()->Head);
11011101
raw_svector_ostream OS(Frag->getContents());
11021102
for (const auto &CGPE : OWriter.getCGProfile()) {
@@ -1205,7 +1205,7 @@ void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
12051205
uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm) {
12061206
// If the assember had an error, then layout will not have completed, so we
12071207
// cannot write an object file.
1208-
if (Asm.getContext().hadError())
1208+
if (getContext().hadError())
12091209
return 0;
12101210

12111211
uint64_t TotalSize = ObjWriter->writeObject(Asm);

0 commit comments

Comments
 (0)