1 //===- DXContainer.cpp - DXContainer object file implementation -----------===// 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 "llvm/Object/DXContainer.h" 10 #include "llvm/BinaryFormat/DXContainer.h" 11 #include "llvm/Object/Error.h" 12 13 using namespace llvm; 14 using namespace llvm::object; 15 16 static Error parseFailed(const Twine &Msg) { 17 return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed); 18 } 19 20 template <typename T> 21 static Error readStruct(StringRef Buffer, const char *P, T &Struct) { 22 // Don't read before the beginning or past the end of the file 23 if (P < Buffer.begin() || P + sizeof(T) > Buffer.end()) 24 return parseFailed("Reading structure out of file bounds"); 25 26 memcpy(&Struct, P, sizeof(T)); 27 // DXContainer is always little endian 28 if (sys::IsBigEndianHost) 29 Struct.byteSwap(); 30 return Error::success(); 31 } 32 33 DXContainer::DXContainer(MemoryBufferRef O) : Data(O) {} 34 35 Error DXContainer::parseHeader() { 36 return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header); 37 } 38 39 Expected<DXContainer> DXContainer::create(MemoryBufferRef Object) { 40 DXContainer Container(Object); 41 if (Error Err = Container.parseHeader()) 42 return std::move(Err); 43 return Container; 44 } 45