#ifndef LLVM_MC_MCASSEMBLER_H
#define LLVM_MC_MCASSEMBLER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/MCDirectives.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCLinkerOptimizationHint.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/VersionTuple.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
namespace llvm {
class MCBoundaryAlignFragment;
class MCCVDefRangeFragment;
class MCCVInlineLineTableFragment;
class MCDwarfCallFrameFragment;
class MCDwarfLineAddrFragment;
class MCEncodedFragment;
class MCFixup;
class MCLEBFragment;
class MCPseudoProbeAddrFragment;
class MCRelaxableFragment;
class MCSymbolRefExpr;
class raw_ostream;
class MCAsmBackend;
class MCAsmLayout;
class MCContext;
class MCCodeEmitter;
class MCFragment;
class MCObjectWriter;
class MCSection;
class MCValue;
struct IndirectSymbolData {
MCSymbol *Symbol;
MCSection *Section;
};
struct DataRegionData {
enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
MCSymbol *Start;
MCSymbol *End;
};
class MCAssembler {
friend class MCAsmLayout;
public:
using SectionListType = std::vector<MCSection *>;
using SymbolDataListType = std::vector<const MCSymbol *>;
using const_iterator = pointee_iterator<SectionListType::const_iterator>;
using iterator = pointee_iterator<SectionListType::iterator>;
using const_symbol_iterator =
pointee_iterator<SymbolDataListType::const_iterator>;
using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>;
using symbol_range = iterator_range<symbol_iterator>;
using const_symbol_range = iterator_range<const_symbol_iterator>;
using const_indirect_symbol_iterator =
std::vector<IndirectSymbolData>::const_iterator;
using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
using const_data_region_iterator =
std::vector<DataRegionData>::const_iterator;
using data_region_iterator = std::vector<DataRegionData>::iterator;
using VersionInfoType = struct {
bool EmitBuildVersion;
union {
MCVersionMinType Type; MachO::PlatformType Platform; } TypeOrPlatform;
unsigned Major;
unsigned Minor;
unsigned Update;
VersionTuple SDKVersion;
};
private:
MCContext &Context;
std::unique_ptr<MCAsmBackend> Backend;
std::unique_ptr<MCCodeEmitter> Emitter;
std::unique_ptr<MCObjectWriter> Writer;
SectionListType Sections;
SymbolDataListType Symbols;
std::vector<IndirectSymbolData> IndirectSymbols;
std::vector<DataRegionData> DataRegions;
std::vector<std::vector<std::string>> LinkerOptions;
std::vector<std::pair<std::string, size_t>> FileNames;
MCDwarfLineTableParams LTParams;
mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
unsigned BundleAlignSize;
bool RelaxAll : 1;
bool SubsectionsViaSymbols : 1;
bool IncrementalLinkerCompatible : 1;
unsigned ELFHeaderEFlags;
MCLOHContainer LOHContainer;
VersionInfoType VersionInfo;
VersionInfoType DarwinTargetVariantVersionInfo;
bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
const MCFragment *DF, MCValue &Target,
uint64_t &Value, bool &WasForced) const;
bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
const MCAsmLayout &Layout) const;
bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
const MCAsmLayout &Layout) const;
bool layoutOnce(MCAsmLayout &Layout);
bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
bool relaxFragment(MCAsmLayout &Layout, MCFragment &F);
bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
bool relaxBoundaryAlign(MCAsmLayout &Layout, MCBoundaryAlignFragment &BF);
bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
MCDwarfCallFrameFragment &DF);
bool relaxCVInlineLineTable(MCAsmLayout &Layout,
MCCVInlineLineTableFragment &DF);
bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
bool relaxPseudoProbeAddr(MCAsmLayout &Layout, MCPseudoProbeAddrFragment &DF);
void finishLayout(MCAsmLayout &Layout);
std::tuple<MCValue, uint64_t, bool>
handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
public:
struct Symver {
SMLoc Loc;
const MCSymbol *Sym;
StringRef Name;
bool KeepOriginalSym;
};
std::vector<Symver> Symvers;
MCAssembler(MCContext &Context, std::unique_ptr<MCAsmBackend> Backend,
std::unique_ptr<MCCodeEmitter> Emitter,
std::unique_ptr<MCObjectWriter> Writer);
MCAssembler(const MCAssembler &) = delete;
MCAssembler &operator=(const MCAssembler &) = delete;
~MCAssembler();
uint64_t computeFragmentSize(const MCAsmLayout &Layout,
const MCFragment &F) const;
const MCSymbol *getAtom(const MCSymbol &S) const;
bool isSymbolLinkerVisible(const MCSymbol &SD) const;
void writeSectionData(raw_ostream &OS, const MCSection *Section,
const MCAsmLayout &Layout) const;
bool isThumbFunc(const MCSymbol *Func) const;
void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
const VersionInfoType &getVersionInfo() const { return VersionInfo; }
void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
unsigned Update,
VersionTuple SDKVersion = VersionTuple()) {
VersionInfo.EmitBuildVersion = false;
VersionInfo.TypeOrPlatform.Type = Type;
VersionInfo.Major = Major;
VersionInfo.Minor = Minor;
VersionInfo.Update = Update;
VersionInfo.SDKVersion = SDKVersion;
}
void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
unsigned Minor, unsigned Update,
VersionTuple SDKVersion = VersionTuple()) {
VersionInfo.EmitBuildVersion = true;
VersionInfo.TypeOrPlatform.Platform = Platform;
VersionInfo.Major = Major;
VersionInfo.Minor = Minor;
VersionInfo.Update = Update;
VersionInfo.SDKVersion = SDKVersion;
}
const VersionInfoType &getDarwinTargetVariantVersionInfo() const {
return DarwinTargetVariantVersionInfo;
}
void setDarwinTargetVariantBuildVersion(MachO::PlatformType Platform,
unsigned Major, unsigned Minor,
unsigned Update,
VersionTuple SDKVersion) {
DarwinTargetVariantVersionInfo.EmitBuildVersion = true;
DarwinTargetVariantVersionInfo.TypeOrPlatform.Platform = Platform;
DarwinTargetVariantVersionInfo.Major = Major;
DarwinTargetVariantVersionInfo.Minor = Minor;
DarwinTargetVariantVersionInfo.Update = Update;
DarwinTargetVariantVersionInfo.SDKVersion = SDKVersion;
}
void reset();
MCContext &getContext() const { return Context; }
MCAsmBackend *getBackendPtr() const { return Backend.get(); }
MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
MCObjectWriter *getWriterPtr() const { return Writer.get(); }
MCAsmBackend &getBackend() const { return *Backend; }
MCCodeEmitter &getEmitter() const { return *Emitter; }
MCObjectWriter &getWriter() const { return *Writer; }
MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
void Finish();
void layout(MCAsmLayout &Layout);
bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
bool isIncrementalLinkerCompatible() const {
return IncrementalLinkerCompatible;
}
void setIncrementalLinkerCompatible(bool Value) {
IncrementalLinkerCompatible = Value;
}
bool getRelaxAll() const { return RelaxAll; }
void setRelaxAll(bool Value) { RelaxAll = Value; }
bool isBundlingEnabled() const { return BundleAlignSize != 0; }
unsigned getBundleAlignSize() const { return BundleAlignSize; }
void setBundleAlignSize(unsigned Size) {
assert((Size == 0 || !(Size & (Size - 1))) &&
"Expect a power-of-two bundle align size");
BundleAlignSize = Size;
}
iterator begin() { return Sections.begin(); }
const_iterator begin() const { return Sections.begin(); }
iterator end() { return Sections.end(); }
const_iterator end() const { return Sections.end(); }
size_t size() const { return Sections.size(); }
symbol_iterator symbol_begin() { return Symbols.begin(); }
const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
symbol_iterator symbol_end() { return Symbols.end(); }
const_symbol_iterator symbol_end() const { return Symbols.end(); }
symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
const_symbol_range symbols() const {
return make_range(symbol_begin(), symbol_end());
}
size_t symbol_size() const { return Symbols.size(); }
std::vector<IndirectSymbolData> &getIndirectSymbols() {
return IndirectSymbols;
}
indirect_symbol_iterator indirect_symbol_begin() {
return IndirectSymbols.begin();
}
const_indirect_symbol_iterator indirect_symbol_begin() const {
return IndirectSymbols.begin();
}
indirect_symbol_iterator indirect_symbol_end() {
return IndirectSymbols.end();
}
const_indirect_symbol_iterator indirect_symbol_end() const {
return IndirectSymbols.end();
}
size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
std::vector<std::vector<std::string>> &getLinkerOptions() {
return LinkerOptions;
}
std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
data_region_iterator data_region_begin() { return DataRegions.begin(); }
const_data_region_iterator data_region_begin() const {
return DataRegions.begin();
}
data_region_iterator data_region_end() { return DataRegions.end(); }
const_data_region_iterator data_region_end() const {
return DataRegions.end();
}
size_t data_region_size() const { return DataRegions.size(); }
MCLOHContainer &getLOHContainer() { return LOHContainer; }
const MCLOHContainer &getLOHContainer() const {
return const_cast<MCAssembler *>(this)->getLOHContainer();
}
struct CGProfileEntry {
const MCSymbolRefExpr *From;
const MCSymbolRefExpr *To;
uint64_t Count;
};
std::vector<CGProfileEntry> CGProfile;
bool registerSection(MCSection &Section);
void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
MutableArrayRef<std::pair<std::string, size_t>> getFileNames() {
return FileNames;
}
void addFileName(StringRef FileName) {
FileNames.emplace_back(std::string(FileName), Symbols.size());
}
void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F,
uint64_t FSize) const;
void dump() const;
};
uint64_t computeBundlePadding(const MCAssembler &Assembler,
const MCEncodedFragment *F, uint64_t FOffset,
uint64_t FSize);
}
#endif