Skip to content

Commit a3ed16c

Browse files
MaskRayyuxuanchen1997
authored andcommitted
[MC] Move VersionInfo to MachObjectWriter
1 parent 3756673 commit a3ed16c

File tree

5 files changed

+63
-78
lines changed

5 files changed

+63
-78
lines changed

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
#include "llvm/ADT/StringRef.h"
1616
#include "llvm/ADT/iterator.h"
1717
#include "llvm/ADT/iterator_range.h"
18-
#include "llvm/BinaryFormat/MachO.h"
19-
#include "llvm/MC/MCDirectives.h"
2018
#include "llvm/MC/MCDwarf.h"
2119
#include "llvm/MC/MCSymbol.h"
2220
#include "llvm/Support/SMLoc.h"
23-
#include "llvm/Support/VersionTuple.h"
2421
#include <algorithm>
2522
#include <cassert>
2623
#include <cstddef>
@@ -58,22 +55,6 @@ class MCAssembler {
5855
using SectionListType = SmallVector<MCSection *, 0>;
5956
using const_iterator = pointee_iterator<SectionListType::const_iterator>;
6057

61-
/// MachO specific deployment target version info.
62-
// A Major version of 0 indicates that no version information was supplied
63-
// and so the corresponding load command should not be emitted.
64-
using VersionInfoType = struct {
65-
bool EmitBuildVersion;
66-
union {
67-
MCVersionMinType Type; ///< Used when EmitBuildVersion==false.
68-
MachO::PlatformType Platform; ///< Used when EmitBuildVersion==true.
69-
} TypeOrPlatform;
70-
unsigned Major;
71-
unsigned Minor;
72-
unsigned Update;
73-
/// An optional version of the SDK that was used to build the source.
74-
VersionTuple SDKVersion;
75-
};
76-
7758
private:
7859
MCContext &Context;
7960

@@ -120,9 +101,6 @@ class MCAssembler {
120101
// which flags to be set.
121102
unsigned ELFHeaderEFlags = 0;
122103

123-
VersionInfoType VersionInfo;
124-
VersionInfoType DarwinTargetVariantVersionInfo;
125-
126104
/// Evaluate a fixup to a relocatable expression and the value which should be
127105
/// placed into the fixup.
128106
///
@@ -226,44 +204,6 @@ class MCAssembler {
226204
unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
227205
void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
228206

229-
/// MachO deployment target version information.
230-
const VersionInfoType &getVersionInfo() const { return VersionInfo; }
231-
void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
232-
unsigned Update,
233-
VersionTuple SDKVersion = VersionTuple()) {
234-
VersionInfo.EmitBuildVersion = false;
235-
VersionInfo.TypeOrPlatform.Type = Type;
236-
VersionInfo.Major = Major;
237-
VersionInfo.Minor = Minor;
238-
VersionInfo.Update = Update;
239-
VersionInfo.SDKVersion = SDKVersion;
240-
}
241-
void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
242-
unsigned Minor, unsigned Update,
243-
VersionTuple SDKVersion = VersionTuple()) {
244-
VersionInfo.EmitBuildVersion = true;
245-
VersionInfo.TypeOrPlatform.Platform = Platform;
246-
VersionInfo.Major = Major;
247-
VersionInfo.Minor = Minor;
248-
VersionInfo.Update = Update;
249-
VersionInfo.SDKVersion = SDKVersion;
250-
}
251-
252-
const VersionInfoType &getDarwinTargetVariantVersionInfo() const {
253-
return DarwinTargetVariantVersionInfo;
254-
}
255-
void setDarwinTargetVariantBuildVersion(MachO::PlatformType Platform,
256-
unsigned Major, unsigned Minor,
257-
unsigned Update,
258-
VersionTuple SDKVersion) {
259-
DarwinTargetVariantVersionInfo.EmitBuildVersion = true;
260-
DarwinTargetVariantVersionInfo.TypeOrPlatform.Platform = Platform;
261-
DarwinTargetVariantVersionInfo.Major = Major;
262-
DarwinTargetVariantVersionInfo.Minor = Minor;
263-
DarwinTargetVariantVersionInfo.Update = Update;
264-
DarwinTargetVariantVersionInfo.SDKVersion = SDKVersion;
265-
}
266-
267207
/// Reuse an assembler instance
268208
///
269209
void reset();

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
#include "llvm/ADT/DenseMap.h"
1313
#include "llvm/ADT/StringRef.h"
1414
#include "llvm/BinaryFormat/MachO.h"
15+
#include "llvm/MC/MCDirectives.h"
1516
#include "llvm/MC/MCExpr.h"
1617
#include "llvm/MC/MCLinkerOptimizationHint.h"
1718
#include "llvm/MC/MCObjectWriter.h"
1819
#include "llvm/MC/MCSection.h"
1920
#include "llvm/MC/StringTableBuilder.h"
2021
#include "llvm/Support/EndianStream.h"
22+
#include "llvm/Support/VersionTuple.h"
2123
#include <cstdint>
2224
#include <memory>
2325
#include <string>
@@ -89,6 +91,21 @@ class MachObjectWriter : public MCObjectWriter {
8991
MCSymbol *End;
9092
};
9193

94+
// A Major version of 0 indicates that no version information was supplied
95+
// and so the corresponding load command should not be emitted.
96+
using VersionInfoType = struct {
97+
bool EmitBuildVersion;
98+
union {
99+
MCVersionMinType Type; ///< Used when EmitBuildVersion==false.
100+
MachO::PlatformType Platform; ///< Used when EmitBuildVersion==true.
101+
} TypeOrPlatform;
102+
unsigned Major;
103+
unsigned Minor;
104+
unsigned Update;
105+
/// An optional version of the SDK that was used to build the source.
106+
VersionTuple SDKVersion;
107+
};
108+
92109
private:
93110
/// Helper struct for containing some precomputed information on symbols.
94111
struct MachSymbolData {
@@ -144,6 +161,9 @@ class MachObjectWriter : public MCObjectWriter {
144161
// Used to communicate Linker Optimization Hint information.
145162
MCLOHContainer LOHContainer;
146163

164+
VersionInfoType VersionInfo{};
165+
VersionInfoType TargetVariantVersionInfo{};
166+
147167
MachSymbolData *findSymbolData(const MCSymbol &Sym);
148168

149169
void writeWithPadding(StringRef Str, uint64_t Size);
@@ -197,6 +217,38 @@ class MachObjectWriter : public MCObjectWriter {
197217

198218
bool doesSymbolRequireExternRelocation(const MCSymbol &S);
199219

220+
/// Mach-O deployment target version information.
221+
void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
222+
unsigned Update,
223+
VersionTuple SDKVersion = VersionTuple()) {
224+
VersionInfo.EmitBuildVersion = false;
225+
VersionInfo.TypeOrPlatform.Type = Type;
226+
VersionInfo.Major = Major;
227+
VersionInfo.Minor = Minor;
228+
VersionInfo.Update = Update;
229+
VersionInfo.SDKVersion = SDKVersion;
230+
}
231+
void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
232+
unsigned Minor, unsigned Update,
233+
VersionTuple SDKVersion = VersionTuple()) {
234+
VersionInfo.EmitBuildVersion = true;
235+
VersionInfo.TypeOrPlatform.Platform = Platform;
236+
VersionInfo.Major = Major;
237+
VersionInfo.Minor = Minor;
238+
VersionInfo.Update = Update;
239+
VersionInfo.SDKVersion = SDKVersion;
240+
}
241+
void setTargetVariantBuildVersion(MachO::PlatformType Platform,
242+
unsigned Major, unsigned Minor,
243+
unsigned Update, VersionTuple SDKVersion) {
244+
TargetVariantVersionInfo.EmitBuildVersion = true;
245+
TargetVariantVersionInfo.TypeOrPlatform.Platform = Platform;
246+
TargetVariantVersionInfo.Major = Major;
247+
TargetVariantVersionInfo.Minor = Minor;
248+
TargetVariantVersionInfo.Update = Update;
249+
TargetVariantVersionInfo.SDKVersion = SDKVersion;
250+
}
251+
200252
/// @}
201253

202254
/// \name Target Writer Proxy Accessors

llvm/lib/MC/MCAssembler.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ MCAssembler::MCAssembler(MCContext &Context,
8484
std::unique_ptr<MCCodeEmitter> Emitter,
8585
std::unique_ptr<MCObjectWriter> Writer)
8686
: Context(Context), Backend(std::move(Backend)),
87-
Emitter(std::move(Emitter)), Writer(std::move(Writer)) {
88-
VersionInfo.Major = 0; // Major version == 0 for "none specified"
89-
DarwinTargetVariantVersionInfo.Major = 0;
90-
}
87+
Emitter(std::move(Emitter)), Writer(std::move(Writer)) {}
9188

9289
MCAssembler::~MCAssembler() = default;
9390

@@ -101,10 +98,6 @@ void MCAssembler::reset() {
10198
ThumbFuncs.clear();
10299
BundleAlignSize = 0;
103100
ELFHeaderEFlags = 0;
104-
VersionInfo.Major = 0;
105-
VersionInfo.SDKVersion = VersionTuple();
106-
DarwinTargetVariantVersionInfo.Major = 0;
107-
DarwinTargetVariantVersionInfo.SDKVersion = VersionTuple();
108101

109102
// reset objects owned by us
110103
if (getBackendPtr())

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,21 +252,21 @@ void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind) {
252252
void MCMachOStreamer::emitVersionMin(MCVersionMinType Kind, unsigned Major,
253253
unsigned Minor, unsigned Update,
254254
VersionTuple SDKVersion) {
255-
getAssembler().setVersionMin(Kind, Major, Minor, Update, SDKVersion);
255+
getWriter().setVersionMin(Kind, Major, Minor, Update, SDKVersion);
256256
}
257257

258258
void MCMachOStreamer::emitBuildVersion(unsigned Platform, unsigned Major,
259259
unsigned Minor, unsigned Update,
260260
VersionTuple SDKVersion) {
261-
getAssembler().setBuildVersion((MachO::PlatformType)Platform, Major, Minor,
262-
Update, SDKVersion);
261+
getWriter().setBuildVersion((MachO::PlatformType)Platform, Major, Minor,
262+
Update, SDKVersion);
263263
}
264264

265265
void MCMachOStreamer::emitDarwinTargetVariantBuildVersion(
266266
unsigned Platform, unsigned Major, unsigned Minor, unsigned Update,
267267
VersionTuple SDKVersion) {
268-
getAssembler().setDarwinTargetVariantBuildVersion(
269-
(MachO::PlatformType)Platform, Major, Minor, Update, SDKVersion);
268+
getWriter().setTargetVariantBuildVersion((MachO::PlatformType)Platform, Major,
269+
Minor, Update, SDKVersion);
270270
}
271271

272272
void MCMachOStreamer::emitThumbFunc(MCSymbol *Symbol) {

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ void MachObjectWriter::reset() {
5656
ExternalSymbolData.clear();
5757
UndefinedSymbolData.clear();
5858
LOHContainer.reset();
59+
VersionInfo.Major = 0;
60+
VersionInfo.SDKVersion = VersionTuple();
61+
TargetVariantVersionInfo.Major = 0;
62+
TargetVariantVersionInfo.SDKVersion = VersionTuple();
5963
MCObjectWriter::reset();
6064
}
6165

@@ -803,7 +807,6 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
803807
}
804808

805809
unsigned NumSections = Asm.end() - Asm.begin();
806-
const MCAssembler::VersionInfoType &VersionInfo = Asm.getVersionInfo();
807810

808811
// The section data starts after the header, the segment load command (and
809812
// section headers) and the symbol table.
@@ -821,9 +824,6 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
821824
LoadCommandsSize += sizeof(MachO::version_min_command);
822825
}
823826

824-
const MCAssembler::VersionInfoType &TargetVariantVersionInfo =
825-
Asm.getDarwinTargetVariantVersionInfo();
826-
827827
// Add the target variant version info load command size, if used.
828828
if (TargetVariantVersionInfo.Major != 0) {
829829
++NumLoadCommands;
@@ -928,7 +928,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
928928

929929
// Write out the deployment target information, if it's available.
930930
auto EmitDeploymentTargetVersion =
931-
[&](const MCAssembler::VersionInfoType &VersionInfo) {
931+
[&](const VersionInfoType &VersionInfo) {
932932
auto EncodeVersion = [](VersionTuple V) -> uint32_t {
933933
assert(!V.empty() && "empty version");
934934
unsigned Update = V.getSubminor().value_or(0);

0 commit comments

Comments
 (0)