Skip to content

Fix CAS errors related to moving of LinkerOptions amongst other things from MCAssember to MachObjectWriter #9062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions llvm/include/llvm/MC/MCMachObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ class MCMachObjectTargetWriter : public MCObjectTargetWriter {

class MachObjectWriter : public MCObjectWriter {
public:
struct DataRegionData {
MachO::DataRegionType Kind;
MCSymbol *Start;
MCSymbol *End;
};

// A Major version of 0 indicates that no version information was supplied
// and so the corresponding load command should not be emitted.
using VersionInfoType = struct {
Expand All @@ -118,11 +112,6 @@ class MachObjectWriter : public MCObjectWriter {
bool operator<(const MachSymbolData &RHS) const;
};

struct IndirectSymbolData {
MCSymbol *Symbol;
MCSection *Section;
};

/// The target specific Mach-O writer instance.
std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;

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

private:
DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
std::vector<IndirectSymbolData> IndirectSymbols;
DenseMap<const MCSection *, unsigned> IndirectSymBase;

std::vector<DataRegionData> DataRegions;

SectionAddrMap SectionAddress;

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

/// @}

// Used to communicate Linker Optimization Hint information.
MCLOHContainer LOHContainer;

VersionInfoType VersionInfo{};
VersionInfoType TargetVariantVersionInfo{};

std::optional<unsigned> PtrAuthABIVersion;
bool PtrAuthKernelABIVersion;

// The list of linker options for LC_LINKER_OPTION.
std::vector<std::vector<std::string>> LinkerOptions;

MachSymbolData *findSymbolData(const MCSymbol &Sym);

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

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

std::vector<IndirectSymbolData> &getIndirectSymbols() {
return IndirectSymbols;
}
std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
return SectionOrder;
}
SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
MCLOHContainer &getLOHContainer() { return LOHContainer; }

uint64_t getSectionAddress(const MCSection *Sec) const {
return SectionAddress.lookup(Sec);
Expand Down Expand Up @@ -281,9 +256,6 @@ class MachObjectWriter : public MCObjectWriter {
void setPtrAuthKernelABIVersion(bool V) override {
PtrAuthKernelABIVersion = V;
}
std::vector<std::vector<std::string>> &getLinkerOptions() {
return LinkerOptions;
}

/// @}

Expand Down
35 changes: 35 additions & 0 deletions llvm/include/llvm/MC/MCObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_MC_MCOBJECTWRITER_H

#include "llvm/MC/MCDirectives.h"
#include "llvm/MC/MCLinkerOptimizationHint.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/TargetParser/Triple.h"
#include <cstdint>
Expand All @@ -32,7 +33,29 @@ class MCValue;
/// MCAssembler instance, which contains all the symbol and section data which
/// should be emitted as part of writeObject().
class MCObjectWriter {
public:
struct DataRegionData {
unsigned Kind;
MCSymbol *Start;
MCSymbol *End;
};

protected:
struct IndirectSymbolData {
MCSymbol *Symbol;
MCSection *Section;
};

std::vector<IndirectSymbolData> IndirectSymbols;

std::vector<DataRegionData> DataRegions;

// The list of linker options for LC_LINKER_OPTION.
std::vector<std::vector<std::string>> LinkerOptions;

// Used to communicate Linker Optimization Hint information.
MCLOHContainer LOHContainer;

/// List of declared file names
SmallVector<std::pair<std::string, size_t>, 0> FileNames;
// XCOFF specific: Optional compiler version.
Expand Down Expand Up @@ -61,6 +84,18 @@ class MCObjectWriter {
/// \name High-Level API
/// @{

std::vector<std::vector<std::string>> &getLinkerOptions() {
return LinkerOptions;
}

std::vector<IndirectSymbolData> &getIndirectSymbols() {
return IndirectSymbols;
}

MCLOHContainer &getLOHContainer() { return LOHContainer; }

std::vector<DataRegionData> &getDataRegions() { return DataRegions; }

/// Perform any late binding of symbols (for example, to assign symbol
/// indices for use when generating relocations).
///
Expand Down
27 changes: 15 additions & 12 deletions llvm/lib/MC/MCMachOStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ class MCMachOStreamer : public MCObjectStreamer {
MCObjectStreamer::reset();
}

MachObjectWriter &getWriter() {
return static_cast<MachObjectWriter &>(getAssembler().getWriter());
}
// This function is commented out downstream because it is unsafe to use a
// MachObjectWriter in the McMachOStreamer which may hold a MachOCASWriter
// instead.
// MachObjectWriter &getWriter() {
// return static_cast<MachObjectWriter &>(getAssembler().getWriter());
// }

MCObjectWriter &getMCObjectWriter() {
return static_cast<MCObjectWriter &>(getAssembler().getWriter());
Expand Down Expand Up @@ -124,12 +127,12 @@ class MCMachOStreamer : public MCObjectStreamer {
}

void emitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
getWriter().getLOHContainer().addDirective(Kind, Args);
getMCObjectWriter().getLOHContainer().addDirective(Kind, Args);
}
void emitCGProfileEntry(const MCSymbolRefExpr *From,
const MCSymbolRefExpr *To, uint64_t Count) override {
if (!From->getSymbol().isTemporary() && !To->getSymbol().isTemporary())
getWriter().getCGProfile().push_back({From, To, Count});
getMCObjectWriter().getCGProfile().push_back({From, To, Count});
}

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

void MCMachOStreamer::emitDataRegionEnd() {
auto &Regions = getWriter().getDataRegions();
auto &Regions = getMCObjectWriter().getDataRegions();
assert(!Regions.empty() && "Mismatched .end_data_region!");
auto &Data = Regions.back();
assert(!Data.End && "Mismatched .end_data_region!");
Expand All @@ -232,7 +235,7 @@ void MCMachOStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
}

void MCMachOStreamer::emitLinkerOptions(ArrayRef<std::string> Options) {
getWriter().getLinkerOptions().push_back(Options);
getMCObjectWriter().getLinkerOptions().push_back(Options);
}

void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind) {
Expand Down Expand Up @@ -277,8 +280,8 @@ void MCMachOStreamer::emitDarwinTargetVariantBuildVersion(

void MCMachOStreamer::EmitPtrAuthABIVersion(unsigned PtrAuthABIVersion,
bool PtrAuthKernelABIVersion) {
getWriter().setPtrAuthABIVersion(PtrAuthABIVersion);
getWriter().setPtrAuthKernelABIVersion(PtrAuthKernelABIVersion);
getMCObjectWriter().setPtrAuthABIVersion(PtrAuthABIVersion);
getMCObjectWriter().setPtrAuthKernelABIVersion(PtrAuthKernelABIVersion);
}

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

void MCMachOStreamer::finalizeCGProfile() {
MCAssembler &Asm = getAssembler();
MCObjectWriter &W = getWriter();
MCObjectWriter &W = getMCObjectWriter();
if (W.getCGProfile().empty())
return;
for (auto &E : W.getCGProfile()) {
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/MC/MCObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ void MCObjectWriter::reset() {
EmitAddrsigSection = false;
SubsectionsViaSymbols = false;
CGProfile.clear();
LinkerOptions.clear();
LOHContainer.reset();
DataRegions.clear();
IndirectSymbols.clear();
}

bool MCObjectWriter::isSymbolRefDifferenceFullyResolved(
Expand Down
12 changes: 5 additions & 7 deletions llvm/lib/MC/MachObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,18 @@ void MachObjectWriter::reset() {
Relocations.clear();
IndirectSymBase.clear();
IndirectSymbols.clear();
DataRegions.clear();
SectionAddress.clear();
SectionOrder.clear();
StringTable.clear();
LocalSymbolData.clear();
ExternalSymbolData.clear();
UndefinedSymbolData.clear();
LOHContainer.reset();
VersionInfo.Major = 0;
VersionInfo.SDKVersion = VersionTuple();
TargetVariantVersionInfo.Major = 0;
TargetVariantVersionInfo.SDKVersion = VersionTuple();
PtrAuthABIVersion = std::nullopt;
PtrAuthKernelABIVersion = false;
LinkerOptions.clear();
MCObjectWriter::reset();
}

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

LLVM_DEBUG(dbgs() << "data in code region-- kind: " << Data.Kind
<< " start: " << Start << "(" << Data.Start->getName()
<< ")" << " end: " << End << "(" << Data.End->getName()
<< ")" << " size: " << End - Start << "\n");
LLVM_DEBUG(dbgs() << "data in code region-- kind: "
<< (MachO::DataRegionType)Data.Kind << " start: "
<< Start << "(" << Data.Start->getName() << ")"
<< " end: " << End << "(" << Data.End->getName() << ")"
<< " size: " << End - Start << "\n");
W.write<uint32_t>(Start);
W.write<uint16_t>(End - Start);
W.write<uint16_t>(Data.Kind);
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/MC/MachO/linker-options.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

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

; CHECK-OBJ: Linker Options {
; CHECK-OBJ: Size: 16
Expand Down