| 1 | //===- DXContainerWriter.cpp ----------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "DXContainerWriter.h" |
| 10 | |
| 11 | namespace llvm { |
| 12 | namespace objcopy { |
| 13 | namespace dxbc { |
| 14 | |
| 15 | using namespace object; |
| 16 | |
| 17 | size_t DXContainerWriter::finalize() { |
| 18 | assert(Offsets.empty() && |
| 19 | "Attempted to finalize writer with already computed offsets"); |
| 20 | Offsets.reserve(N: Obj.Parts.size()); |
| 21 | size_t Offset = Obj.headerSize(); |
| 22 | for (const Part &P : Obj.Parts) { |
| 23 | Offsets.push_back(Elt: Offset); |
| 24 | Offset += P.size(); |
| 25 | } |
| 26 | return Obj.Header.FileSize; |
| 27 | } |
| 28 | |
| 29 | Error DXContainerWriter::write() { |
| 30 | size_t TotalSize = finalize(); |
| 31 | Out.reserveExtraSpace(ExtraSize: TotalSize); |
| 32 | |
| 33 | llvm::dxbc::Header Header = Obj.Header; |
| 34 | if (sys::IsBigEndianHost) |
| 35 | Header.swapBytes(); |
| 36 | Out.write(Ptr: reinterpret_cast<const char *>(&Header), |
| 37 | Size: sizeof(::llvm::dxbc::Header)); |
| 38 | if (sys::IsBigEndianHost) |
| 39 | for (auto &O : Offsets) |
| 40 | sys::swapByteOrder(Value&: O); |
| 41 | Out.write(Ptr: reinterpret_cast<const char *>(Offsets.data()), |
| 42 | Size: Offsets.size() * sizeof(uint32_t)); |
| 43 | |
| 44 | for (const Part &P : Obj.Parts) { |
| 45 | Out.write(Ptr: reinterpret_cast<const char *>(P.Name.data()), Size: 4); |
| 46 | uint32_t Size = P.Data.size(); |
| 47 | if (sys::IsBigEndianHost) |
| 48 | sys::swapByteOrder(Value&: Size); |
| 49 | Out.write(Ptr: reinterpret_cast<const char *>(&Size), Size: sizeof(uint32_t)); |
| 50 | Out.write(Ptr: reinterpret_cast<const char *>(P.Data.data()), Size: P.Data.size()); |
| 51 | } |
| 52 | |
| 53 | return Error::success(); |
| 54 | } |
| 55 | |
| 56 | } // end namespace dxbc |
| 57 | } // end namespace objcopy |
| 58 | } // end namespace llvm |
| 59 |