Skip to content

Commit 8001d51

Browse files
Merge pull request #9062 from rastogishubham/FixCASErrStable
Fix CAS errors related to moving of LinkerOptions amongst other things from MCAssember to MachObjectWriter
2 parents 04f5dc0 + 5c51eef commit 8001d51

File tree

6 files changed

+61
-47
lines changed

6 files changed

+61
-47
lines changed

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ 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-
9589
// A Major version of 0 indicates that no version information was supplied
9690
// and so the corresponding load command should not be emitted.
9791
using VersionInfoType = struct {
@@ -118,11 +112,6 @@ class MachObjectWriter : public MCObjectWriter {
118112
bool operator<(const MachSymbolData &RHS) const;
119113
};
120114

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

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

144133
private:
145134
DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
146-
std::vector<IndirectSymbolData> IndirectSymbols;
147135
DenseMap<const MCSection *, unsigned> IndirectSymBase;
148136

149-
std::vector<DataRegionData> DataRegions;
150-
151137
SectionAddrMap SectionAddress;
152138

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

166152
/// @}
167153

168-
// Used to communicate Linker Optimization Hint information.
169-
MCLOHContainer LOHContainer;
170-
171154
VersionInfoType VersionInfo{};
172155
VersionInfoType TargetVariantVersionInfo{};
173156

174157
std::optional<unsigned> PtrAuthABIVersion;
175158
bool PtrAuthKernelABIVersion;
176159

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

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

215195
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
216196

217-
std::vector<IndirectSymbolData> &getIndirectSymbols() {
218-
return IndirectSymbols;
219-
}
220-
std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
221197
const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
222198
return SectionOrder;
223199
}
224200
SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
225-
MCLOHContainer &getLOHContainer() { return LOHContainer; }
226201

227202
uint64_t getSectionAddress(const MCSection *Sec) const {
228203
return SectionAddress.lookup(Sec);
@@ -281,9 +256,6 @@ class MachObjectWriter : public MCObjectWriter {
281256
void setPtrAuthKernelABIVersion(bool V) override {
282257
PtrAuthKernelABIVersion = V;
283258
}
284-
std::vector<std::vector<std::string>> &getLinkerOptions() {
285-
return LinkerOptions;
286-
}
287259

288260
/// @}
289261

llvm/include/llvm/MC/MCObjectWriter.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_MC_MCOBJECTWRITER_H
1111

1212
#include "llvm/MC/MCDirectives.h"
13+
#include "llvm/MC/MCLinkerOptimizationHint.h"
1314
#include "llvm/MC/MCSymbol.h"
1415
#include "llvm/TargetParser/Triple.h"
1516
#include <cstdint>
@@ -32,7 +33,29 @@ class MCValue;
3233
/// MCAssembler instance, which contains all the symbol and section data which
3334
/// should be emitted as part of writeObject().
3435
class MCObjectWriter {
36+
public:
37+
struct DataRegionData {
38+
unsigned Kind;
39+
MCSymbol *Start;
40+
MCSymbol *End;
41+
};
42+
3543
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+
3659
/// List of declared file names
3760
SmallVector<std::pair<std::string, size_t>, 0> FileNames;
3861
// XCOFF specific: Optional compiler version.
@@ -61,6 +84,18 @@ class MCObjectWriter {
6184
/// \name High-Level API
6285
/// @{
6386

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+
6499
/// Perform any late binding of symbols (for example, to assign symbol
65100
/// indices for use when generating relocations).
66101
///

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ class MCMachOStreamer : public MCObjectStreamer {
7878
MCObjectStreamer::reset();
7979
}
8080

81-
MachObjectWriter &getWriter() {
82-
return static_cast<MachObjectWriter &>(getAssembler().getWriter());
83-
}
81+
// This function is commented out downstream because it is unsafe to use a
82+
// MachObjectWriter in the McMachOStreamer which may hold a MachOCASWriter
83+
// instead.
84+
// MachObjectWriter &getWriter() {
85+
// return static_cast<MachObjectWriter &>(getAssembler().getWriter());
86+
// }
8487

8588
MCObjectWriter &getMCObjectWriter() {
8689
return static_cast<MCObjectWriter &>(getAssembler().getWriter());
@@ -124,12 +127,12 @@ class MCMachOStreamer : public MCObjectStreamer {
124127
}
125128

126129
void emitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
127-
getWriter().getLOHContainer().addDirective(Kind, Args);
130+
getMCObjectWriter().getLOHContainer().addDirective(Kind, Args);
128131
}
129132
void emitCGProfileEntry(const MCSymbolRefExpr *From,
130133
const MCSymbolRefExpr *To, uint64_t Count) override {
131134
if (!From->getSymbol().isTemporary() && !To->getSymbol().isTemporary())
132-
getWriter().getCGProfile().push_back({From, To, Count});
135+
getMCObjectWriter().getCGProfile().push_back({From, To, Count});
133136
}
134137

135138
void finishImpl() override;
@@ -203,11 +206,11 @@ void MCMachOStreamer::emitDataRegion(MachO::DataRegionType Kind) {
203206
MCSymbol *Start = getContext().createTempSymbol();
204207
emitLabel(Start);
205208
// Record the region for the object writer to use.
206-
getWriter().getDataRegions().push_back({Kind, Start, nullptr});
209+
getMCObjectWriter().getDataRegions().push_back({Kind, Start, nullptr});
207210
}
208211

209212
void MCMachOStreamer::emitDataRegionEnd() {
210-
auto &Regions = getWriter().getDataRegions();
213+
auto &Regions = getMCObjectWriter().getDataRegions();
211214
assert(!Regions.empty() && "Mismatched .end_data_region!");
212215
auto &Data = Regions.back();
213216
assert(!Data.End && "Mismatched .end_data_region!");
@@ -232,7 +235,7 @@ void MCMachOStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
232235
}
233236

234237
void MCMachOStreamer::emitLinkerOptions(ArrayRef<std::string> Options) {
235-
getWriter().getLinkerOptions().push_back(Options);
238+
getMCObjectWriter().getLinkerOptions().push_back(Options);
236239
}
237240

238241
void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind) {
@@ -277,8 +280,8 @@ void MCMachOStreamer::emitDarwinTargetVariantBuildVersion(
277280

278281
void MCMachOStreamer::EmitPtrAuthABIVersion(unsigned PtrAuthABIVersion,
279282
bool PtrAuthKernelABIVersion) {
280-
getWriter().setPtrAuthABIVersion(PtrAuthABIVersion);
281-
getWriter().setPtrAuthKernelABIVersion(PtrAuthKernelABIVersion);
283+
getMCObjectWriter().setPtrAuthABIVersion(PtrAuthABIVersion);
284+
getMCObjectWriter().setPtrAuthKernelABIVersion(PtrAuthKernelABIVersion);
282285
}
283286

284287
void MCMachOStreamer::emitThumbFunc(MCSymbol *Symbol) {
@@ -297,7 +300,7 @@ bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym,
297300
if (Attribute == MCSA_IndirectSymbol) {
298301
// Note that we intentionally cannot use the symbol data here; this is
299302
// important for matching the string table that 'as' generates.
300-
getWriter().getIndirectSymbols().push_back(
303+
getMCObjectWriter().getIndirectSymbols().push_back(
301304
{Symbol, getCurrentSectionOnly()});
302305
return true;
303306
}
@@ -518,7 +521,7 @@ void MCMachOStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
518521

519522
void MCMachOStreamer::finalizeCGProfile() {
520523
MCAssembler &Asm = getAssembler();
521-
MCObjectWriter &W = getWriter();
524+
MCObjectWriter &W = getMCObjectWriter();
522525
if (W.getCGProfile().empty())
523526
return;
524527
for (auto &E : W.getCGProfile()) {

llvm/lib/MC/MCObjectWriter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ void MCObjectWriter::reset() {
2525
EmitAddrsigSection = false;
2626
SubsectionsViaSymbols = false;
2727
CGProfile.clear();
28+
LinkerOptions.clear();
29+
LOHContainer.reset();
30+
DataRegions.clear();
31+
IndirectSymbols.clear();
2832
}
2933

3034
bool MCObjectWriter::isSymbolRefDifferenceFullyResolved(

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,18 @@ void MachObjectWriter::reset() {
4949
Relocations.clear();
5050
IndirectSymBase.clear();
5151
IndirectSymbols.clear();
52-
DataRegions.clear();
5352
SectionAddress.clear();
5453
SectionOrder.clear();
5554
StringTable.clear();
5655
LocalSymbolData.clear();
5756
ExternalSymbolData.clear();
5857
UndefinedSymbolData.clear();
59-
LOHContainer.reset();
6058
VersionInfo.Major = 0;
6159
VersionInfo.SDKVersion = VersionTuple();
6260
TargetVariantVersionInfo.Major = 0;
6361
TargetVariantVersionInfo.SDKVersion = VersionTuple();
6462
PtrAuthABIVersion = std::nullopt;
6563
PtrAuthKernelABIVersion = false;
66-
LinkerOptions.clear();
6764
MCObjectWriter::reset();
6865
}
6966

@@ -1097,10 +1094,11 @@ void MachObjectWriter::writeDataInCodeRegion(MCAssembler &Asm) {
10971094
else
10981095
report_fatal_error("Data region not terminated");
10991096

1100-
LLVM_DEBUG(dbgs() << "data in code region-- kind: " << Data.Kind
1101-
<< " start: " << Start << "(" << Data.Start->getName()
1102-
<< ")" << " end: " << End << "(" << Data.End->getName()
1103-
<< ")" << " size: " << End - Start << "\n");
1097+
LLVM_DEBUG(dbgs() << "data in code region-- kind: "
1098+
<< (MachO::DataRegionType)Data.Kind << " start: "
1099+
<< Start << "(" << Data.Start->getName() << ")"
1100+
<< " end: " << End << "(" << Data.End->getName() << ")"
1101+
<< " size: " << End - Start << "\n");
11041102
W.write<uint32_t>(Start);
11051103
W.write<uint16_t>(End - Start);
11061104
W.write<uint16_t>(Data.Kind);

llvm/test/MC/MachO/linker-options.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
; RUN: llc -O0 -mtriple=x86_64-apple-darwin -filetype=obj -o - %s | llvm-readobj --macho-linker-options - > %t
88
; RUN: FileCheck --check-prefix=CHECK-OBJ < %t %s
9+
; RUN: mkdir -p %t.dir/cas
10+
; RUN: llc -O0 -mtriple=x86_64-apple-darwin -filetype=obj --cas %t.dir/cas -cas-backend -o - %s | llvm-readobj --macho-linker-options - > %t
911

1012
; CHECK-OBJ: Linker Options {
1113
; CHECK-OBJ: Size: 16

0 commit comments

Comments
 (0)