Skip to content

Commit 8f9beda

Browse files
Merge pull request #9111 from rastogishubham/MachOCASWriterRewriteStable
Rewrite MachOCASWriter to be a derived class of MachObjectWriter
2 parents 583f7a8 + 61aec53 commit 8f9beda

File tree

10 files changed

+106
-197
lines changed

10 files changed

+106
-197
lines changed

llvm/include/llvm/MC/MCMachOCASWriter.h

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ObjectStore;
3434
class CASID;
3535
} // namespace cas
3636

37-
class MachOCASWriter : public MCObjectWriter {
37+
class MachOCASWriter : public MachObjectWriter {
3838
public:
3939
/// ObjectStore
4040
const Triple Target;
@@ -57,99 +57,19 @@ class MachOCASWriter : public MCObjectWriter {
5757

5858
uint8_t getAddressSize() { return Target.isArch32Bit() ? 4 : 8; }
5959

60-
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
61-
const MCFixup &Fixup, MCValue Target,
62-
uint64_t &FixedValue) override {
63-
MOW.recordRelocation(Asm, Fragment, Fixup, Target, FixedValue);
64-
}
65-
66-
void executePostLayoutBinding(MCAssembler &Asm) override {
67-
MOW.executePostLayoutBinding(Asm);
68-
}
69-
70-
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
71-
const MCSymbol &SymA,
72-
const MCFragment &FB, bool InSet,
73-
bool IsPCRel) const override {
74-
return MOW.isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB, InSet,
75-
IsPCRel);
76-
}
77-
78-
uint64_t getPaddingSize(MCAssembler &Asm, const MCSection *SD) const {
79-
return MOW.getPaddingSize(Asm, SD);
80-
}
81-
82-
void prepareObject(MCAssembler &Asm) { MOW.prepareObject(Asm); }
83-
84-
void writeMachOHeader(MCAssembler &Asm) { MOW.writeMachOHeader(Asm); }
85-
86-
void writeSectionData(MCAssembler &Asm) { MOW.writeSectionData(Asm); }
87-
88-
void writeRelocations(MCAssembler &Asm) { MOW.writeRelocations(Asm); }
89-
90-
void writeDataInCodeRegion(MCAssembler &Asm) {
91-
MOW.writeDataInCodeRegion(Asm);
92-
}
93-
94-
void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
95-
unsigned Update,
96-
VersionTuple SDKVersion = VersionTuple()) override {
97-
MOW.setVersionMin(Type, Major, Minor, Update, SDKVersion);
98-
}
99-
void setBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
100-
unsigned Update,
101-
VersionTuple SDKVersion = VersionTuple()) override {
102-
MOW.setBuildVersion(Platform, Major, Minor, Update, SDKVersion);
103-
}
104-
void setTargetVariantBuildVersion(unsigned Platform, unsigned Major,
105-
unsigned Minor, unsigned Update,
106-
VersionTuple SDKVersion) override {
107-
MOW.setTargetVariantBuildVersion(Platform, Major, Minor, Update,
108-
SDKVersion);
109-
}
110-
111-
std::optional<unsigned> getPtrAuthABIVersion() const override {
112-
return MOW.getPtrAuthABIVersion();
113-
}
114-
void setPtrAuthABIVersion(unsigned V) override {
115-
MOW.setPtrAuthABIVersion(V);
116-
}
117-
bool getPtrAuthKernelABIVersion() const override {
118-
return MOW.getPtrAuthKernelABIVersion();
119-
}
120-
void setPtrAuthKernelABIVersion(bool V) override {
121-
MOW.setPtrAuthKernelABIVersion(V);
122-
}
123-
124-
bool getSubsectionsViaSymbols() const override {
125-
return MOW.getSubsectionsViaSymbols();
126-
}
127-
void setSubsectionsViaSymbols(bool Value) override {
128-
MOW.setSubsectionsViaSymbols(Value);
129-
}
130-
131-
void writeSymbolTable(MCAssembler &Asm) { MOW.writeSymbolTable(Asm); }
132-
13360
uint64_t writeObject(MCAssembler &Asm) override;
13461

13562
void resetBuffer() { OSOffset = InternalOS.tell(); }
13663

13764
StringRef getContent() const { return InternalBuffer.substr(OSOffset); }
13865

139-
DenseMap<const MCSection *, std::vector<MachObjectWriter::RelAndSymbol>> &
140-
getRelocations() {
141-
return MOW.getRelocations();
142-
}
143-
14466
private:
14567
raw_pwrite_stream &OS;
14668
raw_pwrite_stream *CasIDOS;
14769

14870
SmallString<512> InternalBuffer;
14971
raw_svector_ostream InternalOS;
15072

151-
MachObjectWriter MOW;
152-
15373
uint64_t OSOffset = 0;
15474

15575
std::function<const cas::ObjectProxy(llvm::MachOCASWriter &,

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ class MCMachObjectTargetWriter : public MCObjectTargetWriter {
8686

8787
class MachObjectWriter : public MCObjectWriter {
8888
public:
89+
struct DataRegionData {
90+
MachO::DataRegionType Kind;
91+
MCSymbol *Start;
92+
MCSymbol *End;
93+
};
94+
8995
// A Major version of 0 indicates that no version information was supplied
9096
// and so the corresponding load command should not be emitted.
9197
using VersionInfoType = struct {
@@ -112,6 +118,11 @@ class MachObjectWriter : public MCObjectWriter {
112118
bool operator<(const MachSymbolData &RHS) const;
113119
};
114120

121+
struct IndirectSymbolData {
122+
MCSymbol *Symbol;
123+
MCSection *Section;
124+
};
125+
115126
/// The target specific Mach-O writer instance.
116127
std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
117128

@@ -132,8 +143,11 @@ class MachObjectWriter : public MCObjectWriter {
132143

133144
private:
134145
DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
146+
std::vector<IndirectSymbolData> IndirectSymbols;
135147
DenseMap<const MCSection *, unsigned> IndirectSymBase;
136148

149+
std::vector<DataRegionData> DataRegions;
150+
137151
SectionAddrMap SectionAddress;
138152

139153
// List of sections in layout order. Virtual sections are after non-virtual
@@ -151,12 +165,18 @@ class MachObjectWriter : public MCObjectWriter {
151165

152166
/// @}
153167

168+
// Used to communicate Linker Optimization Hint information.
169+
MCLOHContainer LOHContainer;
170+
154171
VersionInfoType VersionInfo{};
155172
VersionInfoType TargetVariantVersionInfo{};
156173

157174
std::optional<unsigned> PtrAuthABIVersion = std::nullopt;
158175
bool PtrAuthKernelABIVersion = false;
159176

177+
// The list of linker options for LC_LINKER_OPTION.
178+
std::vector<std::vector<std::string>> LinkerOptions;
179+
160180
MachSymbolData *findSymbolData(const MCSymbol &Sym);
161181

162182
void writeWithPadding(StringRef Str, uint64_t Size);
@@ -194,10 +214,15 @@ class MachObjectWriter : public MCObjectWriter {
194214

195215
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
196216

217+
std::vector<IndirectSymbolData> &getIndirectSymbols() {
218+
return IndirectSymbols;
219+
}
220+
std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
197221
const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
198222
return SectionOrder;
199223
}
200224
SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
225+
MCLOHContainer &getLOHContainer() { return LOHContainer; }
201226

202227
uint64_t getSectionAddress(const MCSection *Sec) const {
203228
return SectionAddress.lookup(Sec);
@@ -216,45 +241,43 @@ class MachObjectWriter : public MCObjectWriter {
216241
/// Mach-O deployment target version information.
217242
void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
218243
unsigned Update,
219-
VersionTuple SDKVersion = VersionTuple()) override {
244+
VersionTuple SDKVersion = VersionTuple()) {
220245
VersionInfo.EmitBuildVersion = false;
221246
VersionInfo.TypeOrPlatform.Type = Type;
222247
VersionInfo.Major = Major;
223248
VersionInfo.Minor = Minor;
224249
VersionInfo.Update = Update;
225250
VersionInfo.SDKVersion = SDKVersion;
226251
}
227-
void setBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
228-
unsigned Update,
229-
VersionTuple SDKVersion = VersionTuple()) override {
252+
void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
253+
unsigned Minor, unsigned Update,
254+
VersionTuple SDKVersion = VersionTuple()) {
230255
VersionInfo.EmitBuildVersion = true;
231-
VersionInfo.TypeOrPlatform.Platform = (MachO::PlatformType)Platform;
256+
VersionInfo.TypeOrPlatform.Platform = Platform;
232257
VersionInfo.Major = Major;
233258
VersionInfo.Minor = Minor;
234259
VersionInfo.Update = Update;
235260
VersionInfo.SDKVersion = SDKVersion;
236261
}
237-
void setTargetVariantBuildVersion(unsigned Platform, unsigned Major,
238-
unsigned Minor, unsigned Update,
239-
VersionTuple SDKVersion) override {
262+
void setTargetVariantBuildVersion(MachO::PlatformType Platform,
263+
unsigned Major, unsigned Minor,
264+
unsigned Update, VersionTuple SDKVersion) {
240265
TargetVariantVersionInfo.EmitBuildVersion = true;
241-
TargetVariantVersionInfo.TypeOrPlatform.Platform =
242-
(MachO::PlatformType)Platform;
266+
TargetVariantVersionInfo.TypeOrPlatform.Platform = Platform;
243267
TargetVariantVersionInfo.Major = Major;
244268
TargetVariantVersionInfo.Minor = Minor;
245269
TargetVariantVersionInfo.Update = Update;
246270
TargetVariantVersionInfo.SDKVersion = SDKVersion;
247271
}
248272

249-
std::optional<unsigned> getPtrAuthABIVersion() const override {
273+
std::optional<unsigned> getPtrAuthABIVersion() const {
250274
return PtrAuthABIVersion;
251275
}
252-
void setPtrAuthABIVersion(unsigned V) override { PtrAuthABIVersion = V; }
253-
bool getPtrAuthKernelABIVersion() const override {
254-
return PtrAuthKernelABIVersion;
255-
}
256-
void setPtrAuthKernelABIVersion(bool V) override {
257-
PtrAuthKernelABIVersion = V;
276+
void setPtrAuthABIVersion(unsigned V) { PtrAuthABIVersion = V; }
277+
bool getPtrAuthKernelABIVersion() const { return PtrAuthKernelABIVersion; }
278+
void setPtrAuthKernelABIVersion(bool V) { PtrAuthKernelABIVersion = V; }
279+
std::vector<std::vector<std::string>> &getLinkerOptions() {
280+
return LinkerOptions;
258281
}
259282

260283
/// @}

llvm/include/llvm/MC/MCObjectWriter.h

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#ifndef LLVM_MC_MCOBJECTWRITER_H
1010
#define LLVM_MC_MCOBJECTWRITER_H
1111

12-
#include "llvm/MC/MCDirectives.h"
13-
#include "llvm/MC/MCLinkerOptimizationHint.h"
1412
#include "llvm/MC/MCSymbol.h"
1513
#include "llvm/TargetParser/Triple.h"
1614
#include <cstdint>
@@ -33,29 +31,7 @@ class MCValue;
3331
/// MCAssembler instance, which contains all the symbol and section data which
3432
/// should be emitted as part of writeObject().
3533
class MCObjectWriter {
36-
public:
37-
struct DataRegionData {
38-
unsigned Kind;
39-
MCSymbol *Start;
40-
MCSymbol *End;
41-
};
42-
4334
protected:
44-
struct IndirectSymbolData {
45-
MCSymbol *Symbol;
46-
MCSection *Section;
47-
};
48-
49-
std::vector<IndirectSymbolData> IndirectSymbols;
50-
51-
std::vector<DataRegionData> DataRegions;
52-
53-
// The list of linker options for LC_LINKER_OPTION.
54-
std::vector<std::vector<std::string>> LinkerOptions;
55-
56-
// Used to communicate Linker Optimization Hint information.
57-
MCLOHContainer LOHContainer;
58-
5935
/// List of declared file names
6036
SmallVector<std::pair<std::string, size_t>, 0> FileNames;
6137
// XCOFF specific: Optional compiler version.
@@ -84,18 +60,6 @@ class MCObjectWriter {
8460
/// \name High-Level API
8561
/// @{
8662

87-
std::vector<std::vector<std::string>> &getLinkerOptions() {
88-
return LinkerOptions;
89-
}
90-
91-
std::vector<IndirectSymbolData> &getIndirectSymbols() {
92-
return IndirectSymbols;
93-
}
94-
95-
MCLOHContainer &getLOHContainer() { return LOHContainer; }
96-
97-
std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
98-
9963
/// Perform any late binding of symbols (for example, to assign symbol
10064
/// indices for use when generating relocations).
10165
///
@@ -152,12 +116,8 @@ class MCObjectWriter {
152116
SmallVector<CGProfileEntry, 0> &getCGProfile() { return CGProfile; }
153117

154118
// Mach-O specific: Whether .subsections_via_symbols is enabled.
155-
virtual bool getSubsectionsViaSymbols() const {
156-
return SubsectionsViaSymbols;
157-
}
158-
virtual void setSubsectionsViaSymbols(bool Value) {
159-
SubsectionsViaSymbols = Value;
160-
}
119+
bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
120+
void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
161121

162122
/// Write the object file and returns the number of bytes written.
163123
///
@@ -166,22 +126,6 @@ class MCObjectWriter {
166126
/// generated.
167127
virtual uint64_t writeObject(MCAssembler &Asm) = 0;
168128

169-
virtual void setVersionMin(MCVersionMinType Type, unsigned Major,
170-
unsigned Minor, unsigned Update,
171-
VersionTuple SDKVersion = VersionTuple()) {}
172-
virtual void setBuildVersion(unsigned Platform, unsigned Major,
173-
unsigned Minor, unsigned Update,
174-
VersionTuple SDKVersion = VersionTuple()) {}
175-
176-
virtual void setTargetVariantBuildVersion(unsigned Platform, unsigned Major,
177-
unsigned Minor, unsigned Update,
178-
VersionTuple SDKVersion) {}
179-
virtual std::optional<unsigned> getPtrAuthABIVersion() const {
180-
return std::nullopt;
181-
}
182-
virtual void setPtrAuthABIVersion(unsigned V) {}
183-
virtual bool getPtrAuthKernelABIVersion() const { return false; }
184-
virtual void setPtrAuthKernelABIVersion(bool V) {}
185129
/// @}
186130
};
187131

llvm/include/llvm/MC/MCSPIRVObjectWriter.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ class SPIRVObjectWriter final : public MCObjectWriter {
4545

4646
void setBuildVersion(unsigned Major, unsigned Minor, unsigned Bound);
4747

48-
void setBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
49-
unsigned Update,
50-
VersionTuple SDKVersion = VersionTuple()) override {}
51-
5248
private:
5349
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
5450
const MCFixup &Fixup, MCValue Target,

0 commit comments

Comments
 (0)