Skip to content

Commit e9b6438

Browse files
MaskRayyuxuanchen1997
authored andcommitted
[MC] Export llvm::ELFObjectWriter
Summary: Similar to commit 28fcafb (2011) for MachObjectWriter and commit 9539a77 for WinCOFFObjectWriter. MCELFStreamer can now access ELFObjectWriter directly without adding ELF-specific markGnuAbi (https://reviews.llvm.org/D97976) and setOverrideABIVersion to MCObjectWriter. A few member variables have to be made public since we cannot use a friend declaration for ELFWriter. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251522
1 parent d9463ec commit e9b6438

File tree

6 files changed

+70
-74
lines changed

6 files changed

+70
-74
lines changed

llvm/include/llvm/MC/MCELFObjectWriter.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
#ifndef LLVM_MC_MCELFOBJECTWRITER_H
1010
#define LLVM_MC_MCELFOBJECTWRITER_H
1111

12+
#include "llvm/ADT/DenseMap.h"
1213
#include "llvm/BinaryFormat/ELF.h"
1314
#include "llvm/MC/MCObjectWriter.h"
1415
#include "llvm/MC/MCSectionELF.h"
1516
#include "llvm/Support/Casting.h"
1617
#include "llvm/Support/raw_ostream.h"
1718
#include "llvm/TargetParser/Triple.h"
1819
#include <cstdint>
20+
#include <memory>
21+
#include <optional>
1922
#include <vector>
2023

2124
namespace llvm {
@@ -25,6 +28,7 @@ class MCContext;
2528
class MCFixup;
2629
class MCSymbol;
2730
class MCSymbolELF;
31+
class MCTargetOptions;
2832
class MCValue;
2933

3034
struct ELFRelocationEntry {
@@ -149,6 +153,48 @@ class MCELFObjectTargetWriter : public MCObjectTargetWriter {
149153
}
150154
};
151155

156+
class ELFObjectWriter : public MCObjectWriter {
157+
public:
158+
std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
159+
DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations;
160+
DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
161+
bool SeenGnuAbi = false;
162+
std::optional<uint8_t> OverrideABIVersion;
163+
164+
ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
165+
: TargetObjectWriter(std::move(MOTW)) {}
166+
167+
void reset() override;
168+
void executePostLayoutBinding(MCAssembler &Asm) override;
169+
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
170+
const MCFixup &Fixup, MCValue Target,
171+
uint64_t &FixedValue) override;
172+
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
173+
const MCSymbol &SymA,
174+
const MCFragment &FB, bool InSet,
175+
bool IsPCRel) const override;
176+
177+
bool hasRelocationAddend() const;
178+
bool usesRela(const MCTargetOptions *TO, const MCSectionELF &Sec) const;
179+
180+
bool shouldRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
181+
const MCSymbolELF *Sym, uint64_t C,
182+
unsigned Type) const;
183+
184+
virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
185+
const MCSectionELF *From,
186+
const MCSectionELF *To) {
187+
return true;
188+
}
189+
190+
// Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN, STB_GNU_UNIQUE).
191+
void markGnuAbi() { SeenGnuAbi = true; }
192+
bool seenGnuAbi() const { return SeenGnuAbi; }
193+
194+
// Override the default e_ident[EI_ABIVERSION] in the ELF header.
195+
void setOverrideABIVersion(uint8_t V) { OverrideABIVersion = V; }
196+
};
197+
152198
/// Construct a new ELF writer instance.
153199
///
154200
/// \param MOTW - The target specific ELF writer subclass.

llvm/include/llvm/MC/MCELFStreamer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "llvm/ADT/SmallVector.h"
1313
#include "llvm/MC/MCDirectives.h"
14+
#include "llvm/MC/MCELFObjectWriter.h"
1415
#include "llvm/MC/MCObjectStreamer.h"
1516

1617
namespace llvm {
@@ -42,6 +43,8 @@ class MCELFStreamer : public MCObjectStreamer {
4243
MCObjectStreamer::reset();
4344
}
4445

46+
ELFObjectWriter &getWriter();
47+
4548
/// \name MCStreamer Interface
4649
/// @{
4750

llvm/include/llvm/MC/MCObjectWriter.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ class MCObjectWriter {
8181
bool InSet,
8282
bool IsPCRel) const;
8383

84-
/// ELF only. Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN).
85-
virtual void markGnuAbi() {}
86-
87-
/// ELF only, override the default ABIVersion in the ELF header.
88-
virtual void setOverrideABIVersion(uint8_t ABIVersion) {}
89-
9084
/// Tell the object writer to emit an address-significance table during
9185
/// writeObject(). If this function is not called, all symbols are treated as
9286
/// address-significant.

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ using namespace llvm;
6767

6868
namespace {
6969

70-
class ELFObjectWriter;
7170
struct ELFWriter;
7271

7372
bool isDwoSection(const MCSectionELF &Sec) {
@@ -200,64 +199,6 @@ struct ELFWriter {
200199
const MCSectionELF &Section);
201200
};
202201

203-
class ELFObjectWriter : public MCObjectWriter {
204-
/// The target specific ELF writer instance.
205-
std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
206-
207-
DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations;
208-
209-
DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
210-
211-
bool SeenGnuAbi = false;
212-
213-
std::optional<uint8_t> OverrideABIVersion;
214-
215-
bool hasRelocationAddend() const;
216-
217-
bool shouldRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
218-
const MCSymbolELF *Sym, uint64_t C,
219-
unsigned Type) const;
220-
221-
public:
222-
ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
223-
: TargetObjectWriter(std::move(MOTW)) {}
224-
225-
void reset() override {
226-
SeenGnuAbi = false;
227-
OverrideABIVersion.reset();
228-
Relocations.clear();
229-
Renames.clear();
230-
MCObjectWriter::reset();
231-
}
232-
233-
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
234-
const MCSymbol &SymA,
235-
const MCFragment &FB, bool InSet,
236-
bool IsPCRel) const override;
237-
238-
virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
239-
const MCSectionELF *From,
240-
const MCSectionELF *To) {
241-
return true;
242-
}
243-
244-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
245-
const MCFixup &Fixup, MCValue Target,
246-
uint64_t &FixedValue) override;
247-
bool usesRela(const MCTargetOptions *TO, const MCSectionELF &Sec) const;
248-
249-
void executePostLayoutBinding(MCAssembler &Asm) override;
250-
251-
void markGnuAbi() override { SeenGnuAbi = true; }
252-
bool seenGnuAbi() const { return SeenGnuAbi; }
253-
254-
bool seenOverrideABIVersion() const { return OverrideABIVersion.has_value(); }
255-
uint8_t getOverrideABIVersion() const { return OverrideABIVersion.value(); }
256-
void setOverrideABIVersion(uint8_t V) override { OverrideABIVersion = V; }
257-
258-
friend struct ELFWriter;
259-
};
260-
261202
class ELFSingleObjectWriter : public ELFObjectWriter {
262203
raw_pwrite_stream &OS;
263204
bool IsLittleEndian;
@@ -403,8 +344,8 @@ void ELFWriter::writeHeader(const MCAssembler &Asm) {
403344
? int(ELF::ELFOSABI_GNU)
404345
: OSABI);
405346
// e_ident[EI_ABIVERSION]
406-
W.OS << char(OWriter.seenOverrideABIVersion()
407-
? OWriter.getOverrideABIVersion()
347+
W.OS << char(OWriter.OverrideABIVersion
348+
? *OWriter.OverrideABIVersion
408349
: OWriter.TargetObjectWriter->getABIVersion());
409350

410351
W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
@@ -779,7 +720,7 @@ void ELFWriter::computeSymbolTable(MCAssembler &Asm,
779720
}
780721

781722
void ELFWriter::writeAddrsigSection() {
782-
for (const MCSymbol *Sym : OWriter.AddrsigSyms)
723+
for (const MCSymbol *Sym : OWriter.getAddrsigSyms())
783724
if (Sym->getIndex() != 0)
784725
encodeULEB128(Sym->getIndex(), W.OS);
785726
}
@@ -1150,7 +1091,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
11501091
StrTabBuilder.finalize();
11511092
} else {
11521093
MCSectionELF *AddrsigSection;
1153-
if (OWriter.EmitAddrsigSection) {
1094+
if (OWriter.getEmitAddrsigSection()) {
11541095
AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
11551096
ELF::SHF_EXCLUDE);
11561097
addToSectionTable(AddrsigSection);
@@ -1170,7 +1111,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
11701111
RelSection->setOffsets(SecStart, SecEnd);
11711112
}
11721113

1173-
if (OWriter.EmitAddrsigSection) {
1114+
if (OWriter.getEmitAddrsigSection()) {
11741115
uint64_t SecStart = W.OS.tell();
11751116
writeAddrsigSection();
11761117
uint64_t SecEnd = W.OS.tell();
@@ -1215,6 +1156,14 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
12151156
return W.OS.tell() - StartOffset;
12161157
}
12171158

1159+
void ELFObjectWriter::reset() {
1160+
SeenGnuAbi = false;
1161+
OverrideABIVersion.reset();
1162+
Relocations.clear();
1163+
Renames.clear();
1164+
MCObjectWriter::reset();
1165+
}
1166+
12181167
bool ELFObjectWriter::hasRelocationAddend() const {
12191168
return TargetObjectWriter->hasRelocationAddend();
12201169
}

llvm/lib/MC/MCELFStreamer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ MCELFStreamer::MCELFStreamer(MCContext &Context,
4646
: MCObjectStreamer(Context, std::move(TAB), std::move(OW),
4747
std::move(Emitter)) {}
4848

49+
ELFObjectWriter &MCELFStreamer::getWriter() {
50+
return static_cast<ELFObjectWriter &>(getAssembler().getWriter());
51+
}
52+
4953
bool MCELFStreamer::isBundleLocked() const {
5054
return getCurrentSectionOnly()->isBundleLocked();
5155
}
@@ -120,7 +124,7 @@ void MCELFStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
120124
if (Grp)
121125
Asm.registerSymbol(*Grp);
122126
if (SectionELF->getFlags() & ELF::SHF_GNU_RETAIN)
123-
Asm.getWriter().markGnuAbi();
127+
getWriter().markGnuAbi();
124128

125129
changeSectionImpl(Section, Subsection);
126130
Asm.registerSymbol(*Section->getBeginSymbol());
@@ -188,7 +192,7 @@ bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
188192
case MCSA_ELF_TypeGnuUniqueObject:
189193
Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
190194
Symbol->setBinding(ELF::STB_GNU_UNIQUE);
191-
getAssembler().getWriter().markGnuAbi();
195+
getWriter().markGnuAbi();
192196
break;
193197

194198
case MCSA_Global:
@@ -226,7 +230,7 @@ bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
226230

227231
case MCSA_ELF_TypeIndFunction:
228232
Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
229-
getAssembler().getWriter().markGnuAbi();
233+
getWriter().markGnuAbi();
230234
break;
231235

232236
case MCSA_ELF_TypeObject:

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ MCELFStreamer &AMDGPUTargetELFStreamer::getStreamer() {
607607
void AMDGPUTargetELFStreamer::finish() {
608608
MCAssembler &MCA = getStreamer().getAssembler();
609609
MCA.setELFHeaderEFlags(getEFlags());
610-
MCA.getWriter().setOverrideABIVersion(
610+
getStreamer().getWriter().setOverrideABIVersion(
611611
getELFABIVersion(STI.getTargetTriple(), CodeObjectVersion));
612612

613613
std::string Blob;

0 commit comments

Comments
 (0)