Skip to content

Commit b65760b

Browse files
committed
MCObjectTargetWriter: Add getContext/reportError and use it for ELF
Prepare for removing MCContext from getRelocType functions.
1 parent 7687261 commit b65760b

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

llvm/include/llvm/MC/MCELFObjectWriter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class ELFObjectWriter final : public MCObjectWriter {
168168
bool IsLittleEndian);
169169

170170
void reset() override;
171+
void setAssembler(MCAssembler *Asm) override;
171172
void executePostLayoutBinding() override;
172173
void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
173174
MCValue Target, uint64_t &FixedValue) override;
@@ -182,7 +183,7 @@ class ELFObjectWriter final : public MCObjectWriter {
182183
bool useSectionSymbol(const MCValue &Val, const MCSymbolELF *Sym, uint64_t C,
183184
unsigned Type) const;
184185

185-
bool checkRelocation(MCContext &Ctx, SMLoc Loc, const MCSectionELF *From,
186+
bool checkRelocation(SMLoc Loc, const MCSectionELF *From,
186187
const MCSectionELF *To);
187188

188189
unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }

llvm/include/llvm/MC/MCObjectWriter.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MCObjectWriter {
5555
MCObjectWriter &operator=(const MCObjectWriter &) = delete;
5656
virtual ~MCObjectWriter();
5757

58-
void setAssembler(MCAssembler *A) { Asm = A; }
58+
virtual void setAssembler(MCAssembler *A) { Asm = A; }
5959

6060
MCContext &getContext() const;
6161

@@ -135,7 +135,14 @@ class MCObjectWriter {
135135
class MCObjectTargetWriter {
136136
public:
137137
virtual ~MCObjectTargetWriter() = default;
138+
void setAssembler(MCAssembler *A) { Asm = A; }
138139
virtual Triple::ObjectFormatType getFormat() const = 0;
140+
141+
protected:
142+
MCContext &getContext() const;
143+
void reportError(SMLoc L, const Twine &Msg) const;
144+
145+
MCAssembler *Asm = nullptr;
139146
};
140147

141148
} // end namespace llvm

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,11 @@ void ELFObjectWriter::reset() {
11821182
MCObjectWriter::reset();
11831183
}
11841184

1185+
void ELFObjectWriter::setAssembler(MCAssembler *Asm) {
1186+
MCObjectWriter::setAssembler(Asm);
1187+
TargetObjectWriter->setAssembler(Asm);
1188+
}
1189+
11851190
bool ELFObjectWriter::hasRelocationAddend() const {
11861191
return TargetObjectWriter->hasRelocationAddend();
11871192
}
@@ -1303,15 +1308,15 @@ bool ELFObjectWriter::useSectionSymbol(const MCValue &Val,
13031308
return !TargetObjectWriter->needsRelocateWithSymbol(Val, *Sym, Type);
13041309
}
13051310

1306-
bool ELFObjectWriter::checkRelocation(MCContext &Ctx, SMLoc Loc,
1307-
const MCSectionELF *From,
1311+
bool ELFObjectWriter::checkRelocation(SMLoc Loc, const MCSectionELF *From,
13081312
const MCSectionELF *To) {
13091313
if (isDwoSection(*From)) {
1310-
Ctx.reportError(Loc, "A dwo section may not contain relocations");
1314+
getContext().reportError(Loc, "A dwo section may not contain relocations");
13111315
return false;
13121316
}
13131317
if (To && isDwoSection(*To)) {
1314-
Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
1318+
getContext().reportError(Loc,
1319+
"A relocation may not refer to a dwo section");
13151320
return false;
13161321
}
13171322
return true;
@@ -1339,7 +1344,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
13391344
const MCSectionELF *SecA = (SymA && SymA->isInSection())
13401345
? cast<MCSectionELF>(&SymA->getSection())
13411346
: nullptr;
1342-
if (DwoOS && !checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
1347+
if (DwoOS && !checkRelocation(Fixup.getLoc(), &FixupSection, SecA))
13431348
return;
13441349

13451350
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &

llvm/lib/MC/MCObjectWriter.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/MC/MCObjectWriter.h"
1010
#include "llvm/MC/MCAssembler.h"
11+
#include "llvm/MC/MCContext.h"
1112
#include "llvm/MC/MCExpr.h"
1213
#include "llvm/MC/MCFragment.h"
1314
#include "llvm/MC/MCSymbol.h"
@@ -53,3 +54,11 @@ bool MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
5354
void MCObjectWriter::addFileName(MCAssembler &Asm, StringRef FileName) {
5455
FileNames.emplace_back(std::string(FileName), Asm.Symbols.size());
5556
}
57+
58+
MCContext &MCObjectTargetWriter::getContext() const {
59+
return Asm->getContext();
60+
}
61+
62+
void MCObjectTargetWriter::reportError(SMLoc L, const Twine &Msg) const {
63+
return Asm->getContext().reportError(L, Msg);
64+
}

llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
370370
case RT64_NONE:
371371
break;
372372
case RT64_64:
373-
Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
373+
reportError(Fixup.getLoc(), "unsupported relocation type");
374374
return ELF::R_386_NONE;
375375
case RT64_32:
376376
case RT64_32S:

0 commit comments

Comments
 (0)