#include "llvm/MC/MCDXContainerWriter.h"
#include "llvm/BinaryFormat/DXContainer.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/EndianStream.h"
using namespace llvm;
MCDXContainerTargetWriter::~MCDXContainerTargetWriter() {}
namespace {
class DXContainerObjectWriter : public MCObjectWriter {
::support::endian::Writer W;
std::unique_ptr<MCDXContainerTargetWriter> TargetObjectWriter;
public:
DXContainerObjectWriter(std::unique_ptr<MCDXContainerTargetWriter> MOTW,
raw_pwrite_stream &OS)
: W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {}
~DXContainerObjectWriter() override {}
private:
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue) override {}
void executePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) override {}
uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
};
}
uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm,
const MCAsmLayout &Layout) {
llvm::SmallVector<uint64_t, 16> PartOffsets;
uint64_t PartOffset = 0;
for (const MCSection &Sec : Asm) {
uint64_t SectionSize = Layout.getSectionAddressSize(&Sec);
if (SectionSize == 0)
continue;
assert(SectionSize < std::numeric_limits<uint32_t>::max() &&
"Section size too large for DXContainer");
PartOffsets.push_back(PartOffset);
PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
PartOffset = alignTo(PartOffset, Align(4ul));
}
assert(PartOffset < std::numeric_limits<uint32_t>::max() &&
"Part data too large for DXContainer");
uint64_t PartStart =
sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));
uint64_t FileSize = PartStart + PartOffset;
assert(FileSize < std::numeric_limits<uint32_t>::max() &&
"File size too large for DXContainer");
W.write<char>({'D', 'X', 'B', 'C'});
W.OS.write_zeros(16);
W.write<uint16_t>(1u);
W.write<uint16_t>(0u);
W.write<uint32_t>(static_cast<uint32_t>(FileSize));
W.write<uint32_t>(static_cast<uint32_t>(PartOffsets.size()));
for (uint64_t Offset : PartOffsets)
W.write<uint32_t>(static_cast<uint32_t>(PartStart + Offset));
for (const MCSection &Sec : Asm) {
uint64_t SectionSize = Layout.getSectionAddressSize(&Sec);
if (SectionSize == 0)
continue;
unsigned Start = W.OS.tell();
W.write<char>(ArrayRef<char>(Sec.getName().data(), 4));
uint64_t PartSize = SectionSize + sizeof(dxbc::PartHeader);
if (Sec.getName() == "DXIL")
PartSize += sizeof(dxbc::ProgramHeader);
PartSize = alignTo(PartSize, Align(4));
W.write<uint32_t>(static_cast<uint32_t>(PartSize));
if (Sec.getName() == "DXIL") {
dxbc::ProgramHeader Header;
memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
const Triple &TT = Asm.getContext().getTargetTriple();
VersionTuple Version = TT.getOSVersion();
Header.MajorVersion = static_cast<uint8_t>(Version.getMajor());
if (Version.getMinor())
Header.MinorVersion = static_cast<uint8_t>(*Version.getMinor());
if (TT.hasEnvironment())
Header.ShaderKind =
static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
memcpy(Header.Bitcode.Magic, "DXIL", 4);
Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
Header.Bitcode.Size = SectionSize;
if (sys::IsBigEndianHost)
Header.swapBytes();
W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
sizeof(dxbc::ProgramHeader)));
}
Asm.writeSectionData(W.OS, &Sec, Layout);
unsigned Size = W.OS.tell() - Start;
W.OS.write_zeros(offsetToAlignment(Size, Align(4)));
}
return 0;
}
std::unique_ptr<MCObjectWriter> llvm::createDXContainerObjectWriter(
std::unique_ptr<MCDXContainerTargetWriter> MOTW, raw_pwrite_stream &OS) {
return std::make_unique<DXContainerObjectWriter>(std::move(MOTW), OS);
}