LLVM 23.0.0git
DXContainerObjcopy.cpp
Go to the documentation of this file.
1//===- DXContainerObjcopy.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
10#include "DXContainerReader.h"
11#include "DXContainerWriter.h"
17
18namespace llvm {
19namespace objcopy {
20namespace dxbc {
21
22using namespace object;
23
24static Error extractPartAsObject(StringRef PartName, StringRef OutFilename,
25 StringRef InputFilename, const Object &Obj) {
26 auto *PartIter = llvm::find_if(
27 Obj.Parts, [&PartName](const Part &P) { return P.Name == PartName; });
28 if (PartIter == Obj.Parts.end())
30 std::make_error_code(std::errc::invalid_argument),
31 "part '%s' not found", PartName.str().c_str());
32
33 Object PartObj;
34 PartObj.Header = Obj.Header;
35 PartObj.Parts.push_back({PartIter->Name, PartIter->Data});
36 PartObj.recomputeHeader();
37
38 auto Write = [&OutFilename, &PartObj](raw_ostream &Out) -> Error {
39 DXContainerWriter Writer(PartObj, Out);
40 if (Error E = Writer.write())
41 return createFileError(OutFilename, std::move(E));
42 return Error::success();
43 };
44
45 return writeToOutput(OutFilename, Write);
46}
47
50 auto *PartIter = llvm::find_if(
51 Obj.Parts, [&PartName](const Part &P) { return P.Name == PartName; });
52 if (PartIter == Obj.Parts.end())
54 std::make_error_code(std::errc::invalid_argument),
55 "part '%s' not found", PartName.str().c_str());
56
57 ArrayRef<uint8_t> Contents = PartIter->Data;
58 // The DXContainer format is a bit odd because the part-specific headers are
59 // contained inside the part data itself. For parts that contain LLVM bitcode
60 // when we dump the part we want to skip the part-specific header so that we
61 // get a valid .bc file that we can inspect. All the data contained inside the
62 // program header is pulled out of the bitcode, so the header can be
63 // reconstructed if needed from the bitcode itself. More comprehensive
64 // documentation on the DXContainer format can be found at
65 // https://llvm.org/docs/DirectX/DXContainer.html.
66
67 if (PartName == "DXIL" || PartName == "STAT")
68 Contents = Contents.drop_front(sizeof(llvm::dxbc::ProgramHeader));
69 if (Contents.empty())
70 return createFileError(Filename, object_error::parse_failed,
71 "part '%s' is empty", PartName.str().c_str());
74 if (!BufferOrErr)
75 return createFileError(Filename, BufferOrErr.takeError());
76 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
77 llvm::copy(Contents, Buf->getBufferStart());
78 if (Error E = Buf->commit())
79 return createFileError(Filename, std::move(E));
80 return Error::success();
81}
82
83static Error handleArgs(const CommonConfig &Config, Object &Obj) {
84 for (StringRef Flag : Config.DumpSection) {
85 auto [SectionName, FileName] = Flag.split("=");
86 if (Error E =
87 dumpPartToFile(SectionName, FileName, Config.InputFilename, Obj))
88 return E;
89 }
90
91 // Extract all sections before any modifications.
92 for (StringRef Flag : Config.ExtractSection) {
93 auto [SectionName, FileName] = Flag.split('=');
94 if (Error E = extractPartAsObject(SectionName, FileName,
95 Config.InputFilename, Obj))
96 return E;
97 }
98
99 std::function<bool(const Part &)> RemovePred = [](const Part &) {
100 return false;
101 };
102
103 if (!Config.ToRemove.empty())
104 RemovePred = [&Config](const Part &P) {
105 return Config.ToRemove.matches(P.Name);
106 };
107
108 if (!Config.OnlySection.empty())
109 RemovePred = [&Config](const Part &P) {
110 // Explicitly keep these sections regardless of previous removes and
111 // remove everything else.
112 return !Config.OnlySection.matches(P.Name);
113 };
114
115 if (auto E = Obj.removeParts(RemovePred))
116 return E;
117
118 Obj.recomputeHeader();
119 return Error::success();
120}
121
123 const DXContainerConfig &,
125 DXContainerReader Reader(In);
126 Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create();
127 if (!ObjOrErr)
128 return createFileError(Config.InputFilename, ObjOrErr.takeError());
129 Object *Obj = ObjOrErr->get();
130 assert(Obj && "Unable to deserialize DXContainer object");
131
132 if (Error E = handleArgs(Config, *Obj))
133 return E;
134
135 DXContainerWriter Writer(*Obj, Out);
136 if (Error E = Writer.write())
137 return createFileError(Config.OutputFilename, std::move(E));
138 return Error::success();
139}
140
141} // end namespace dxbc
142} // end namespace objcopy
143} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static cl::opt< std::string > InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"))
static constexpr StringLiteral Filename
#define P(N)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:195
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
static LLVM_ABI Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:225
bool matches(StringRef S) const
Expected< std::unique_ptr< Object > > create() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
static Error extractPartAsObject(StringRef PartName, StringRef OutFilename, StringRef InputFilename, const Object &Obj)
static Error dumpPartToFile(StringRef PartName, StringRef Filename, StringRef InputFilename, Object &Obj)
Error executeObjcopyOnBinary(const CommonConfig &Config, const DXContainerConfig &, object::DXContainerObjectFile &In, raw_ostream &Out)
Apply the transformations described by Config and DXContainerConfig to In and writes the result into ...
static Error handleArgs(const CommonConfig &Config, Object &Obj)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1399
LLVM_ABI Error writeToOutput(StringRef OutputFileName, std::function< Error(raw_ostream &)> Write)
This helper creates an output stream and then passes it to Write.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1883
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1770
SmallVector< StringRef, 0 > ExtractSection
SmallVector< StringRef, 0 > DumpSection
::llvm::dxbc::Header Header