Skip to content

Rewrite MachOCASWriter to be a derived class of MachObjectWriter #9111

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
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
82 changes: 1 addition & 81 deletions llvm/include/llvm/MC/MCMachOCASWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ObjectStore;
class CASID;
} // namespace cas

class MachOCASWriter : public MCObjectWriter {
class MachOCASWriter : public MachObjectWriter {
public:
/// ObjectStore
const Triple Target;
Expand All @@ -57,99 +57,19 @@ class MachOCASWriter : public MCObjectWriter {

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

void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target,
uint64_t &FixedValue) override {
MOW.recordRelocation(Asm, Fragment, Fixup, Target, FixedValue);
}

void executePostLayoutBinding(MCAssembler &Asm) override {
MOW.executePostLayoutBinding(Asm);
}

bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
const MCSymbol &SymA,
const MCFragment &FB, bool InSet,
bool IsPCRel) const override {
return MOW.isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB, InSet,
IsPCRel);
}

uint64_t getPaddingSize(MCAssembler &Asm, const MCSection *SD) const {
return MOW.getPaddingSize(Asm, SD);
}

void prepareObject(MCAssembler &Asm) { MOW.prepareObject(Asm); }

void writeMachOHeader(MCAssembler &Asm) { MOW.writeMachOHeader(Asm); }

void writeSectionData(MCAssembler &Asm) { MOW.writeSectionData(Asm); }

void writeRelocations(MCAssembler &Asm) { MOW.writeRelocations(Asm); }

void writeDataInCodeRegion(MCAssembler &Asm) {
MOW.writeDataInCodeRegion(Asm);
}

void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
unsigned Update,
VersionTuple SDKVersion = VersionTuple()) override {
MOW.setVersionMin(Type, Major, Minor, Update, SDKVersion);
}
void setBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
unsigned Update,
VersionTuple SDKVersion = VersionTuple()) override {
MOW.setBuildVersion(Platform, Major, Minor, Update, SDKVersion);
}
void setTargetVariantBuildVersion(unsigned Platform, unsigned Major,
unsigned Minor, unsigned Update,
VersionTuple SDKVersion) override {
MOW.setTargetVariantBuildVersion(Platform, Major, Minor, Update,
SDKVersion);
}

std::optional<unsigned> getPtrAuthABIVersion() const override {
return MOW.getPtrAuthABIVersion();
}
void setPtrAuthABIVersion(unsigned V) override {
MOW.setPtrAuthABIVersion(V);
}
bool getPtrAuthKernelABIVersion() const override {
return MOW.getPtrAuthKernelABIVersion();
}
void setPtrAuthKernelABIVersion(bool V) override {
MOW.setPtrAuthKernelABIVersion(V);
}

bool getSubsectionsViaSymbols() const override {
return MOW.getSubsectionsViaSymbols();
}
void setSubsectionsViaSymbols(bool Value) override {
MOW.setSubsectionsViaSymbols(Value);
}

void writeSymbolTable(MCAssembler &Asm) { MOW.writeSymbolTable(Asm); }

uint64_t writeObject(MCAssembler &Asm) override;

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

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

DenseMap<const MCSection *, std::vector<MachObjectWriter::RelAndSymbol>> &
getRelocations() {
return MOW.getRelocations();
}

private:
raw_pwrite_stream &OS;
raw_pwrite_stream *CasIDOS;

SmallString<512> InternalBuffer;
raw_svector_ostream InternalOS;

MachObjectWriter MOW;

uint64_t OSOffset = 0;

std::function<const cas::ObjectProxy(llvm::MachOCASWriter &,
Expand Down
57 changes: 40 additions & 17 deletions llvm/include/llvm/MC/MCMachObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ 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 @@ -112,6 +118,11 @@ 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 @@ -132,8 +143,11 @@ 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 @@ -151,12 +165,18 @@ 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 @@ -194,10 +214,15 @@ 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 All @@ -216,45 +241,43 @@ class MachObjectWriter : public MCObjectWriter {
/// Mach-O deployment target version information.
void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
unsigned Update,
VersionTuple SDKVersion = VersionTuple()) override {
VersionTuple SDKVersion = VersionTuple()) {
VersionInfo.EmitBuildVersion = false;
VersionInfo.TypeOrPlatform.Type = Type;
VersionInfo.Major = Major;
VersionInfo.Minor = Minor;
VersionInfo.Update = Update;
VersionInfo.SDKVersion = SDKVersion;
}
void setBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
unsigned Update,
VersionTuple SDKVersion = VersionTuple()) override {
void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
unsigned Minor, unsigned Update,
VersionTuple SDKVersion = VersionTuple()) {
VersionInfo.EmitBuildVersion = true;
VersionInfo.TypeOrPlatform.Platform = (MachO::PlatformType)Platform;
VersionInfo.TypeOrPlatform.Platform = Platform;
VersionInfo.Major = Major;
VersionInfo.Minor = Minor;
VersionInfo.Update = Update;
VersionInfo.SDKVersion = SDKVersion;
}
void setTargetVariantBuildVersion(unsigned Platform, unsigned Major,
unsigned Minor, unsigned Update,
VersionTuple SDKVersion) override {
void setTargetVariantBuildVersion(MachO::PlatformType Platform,
unsigned Major, unsigned Minor,
unsigned Update, VersionTuple SDKVersion) {
TargetVariantVersionInfo.EmitBuildVersion = true;
TargetVariantVersionInfo.TypeOrPlatform.Platform =
(MachO::PlatformType)Platform;
TargetVariantVersionInfo.TypeOrPlatform.Platform = Platform;
TargetVariantVersionInfo.Major = Major;
TargetVariantVersionInfo.Minor = Minor;
TargetVariantVersionInfo.Update = Update;
TargetVariantVersionInfo.SDKVersion = SDKVersion;
}

std::optional<unsigned> getPtrAuthABIVersion() const override {
std::optional<unsigned> getPtrAuthABIVersion() const {
return PtrAuthABIVersion;
}
void setPtrAuthABIVersion(unsigned V) override { PtrAuthABIVersion = V; }
bool getPtrAuthKernelABIVersion() const override {
return PtrAuthKernelABIVersion;
}
void setPtrAuthKernelABIVersion(bool V) override {
PtrAuthKernelABIVersion = V;
void setPtrAuthABIVersion(unsigned V) { PtrAuthABIVersion = V; }
bool getPtrAuthKernelABIVersion() const { return PtrAuthKernelABIVersion; }
void setPtrAuthKernelABIVersion(bool V) { PtrAuthKernelABIVersion = V; }
std::vector<std::vector<std::string>> &getLinkerOptions() {
return LinkerOptions;
}

/// @}
Expand Down
60 changes: 2 additions & 58 deletions llvm/include/llvm/MC/MCObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#ifndef LLVM_MC_MCOBJECTWRITER_H
#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 @@ -33,29 +31,7 @@ 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 @@ -84,18 +60,6 @@ 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 Expand Up @@ -152,12 +116,8 @@ class MCObjectWriter {
SmallVector<CGProfileEntry, 0> &getCGProfile() { return CGProfile; }

// Mach-O specific: Whether .subsections_via_symbols is enabled.
virtual bool getSubsectionsViaSymbols() const {
return SubsectionsViaSymbols;
}
virtual void setSubsectionsViaSymbols(bool Value) {
SubsectionsViaSymbols = Value;
}
bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }

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

virtual void setVersionMin(MCVersionMinType Type, unsigned Major,
unsigned Minor, unsigned Update,
VersionTuple SDKVersion = VersionTuple()) {}
virtual void setBuildVersion(unsigned Platform, unsigned Major,
unsigned Minor, unsigned Update,
VersionTuple SDKVersion = VersionTuple()) {}

virtual void setTargetVariantBuildVersion(unsigned Platform, unsigned Major,
unsigned Minor, unsigned Update,
VersionTuple SDKVersion) {}
virtual std::optional<unsigned> getPtrAuthABIVersion() const {
return std::nullopt;
}
virtual void setPtrAuthABIVersion(unsigned V) {}
virtual bool getPtrAuthKernelABIVersion() const { return false; }
virtual void setPtrAuthKernelABIVersion(bool V) {}
/// @}
};

Expand Down
4 changes: 0 additions & 4 deletions llvm/include/llvm/MC/MCSPIRVObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class SPIRVObjectWriter final : public MCObjectWriter {

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

void setBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
unsigned Update,
VersionTuple SDKVersion = VersionTuple()) override {}

private:
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target,
Expand Down
Loading