LLVM 22.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"
15
16namespace llvm {
17namespace objcopy {
18namespace dxbc {
19
20using namespace object;
21
22static Error extractPartAsObject(StringRef PartName, StringRef OutFilename,
23 StringRef InputFilename, const Object &Obj) {
24 for (const Part &P : Obj.Parts)
25 if (P.Name == PartName) {
26 Object PartObj;
27 PartObj.Header = Obj.Header;
28 PartObj.Parts.push_back({P.Name, P.Data});
29 PartObj.recomputeHeader();
30
31 auto Write = [&OutFilename, &PartObj](raw_ostream &Out) -> Error {
32 DXContainerWriter Writer(PartObj, Out);
33 if (Error E = Writer.write())
34 return createFileError(OutFilename, std::move(E));
35 return Error::success();
36 };
37
38 return writeToOutput(OutFilename, Write);
39 }
40
41 return createFileError(InputFilename, object_error::parse_failed,
42 "part '%s' not found", PartName.str().c_str());
43}
44
45static Error handleArgs(const CommonConfig &Config, Object &Obj) {
46 // Extract all sections before any modifications.
47 for (StringRef Flag : Config.ExtractSection) {
49 StringRef FileName;
50 std::tie(SectionName, FileName) = Flag.split('=');
51 if (Error E = extractPartAsObject(SectionName, FileName,
52 Config.InputFilename, Obj))
53 return E;
54 }
55
56 std::function<bool(const Part &)> RemovePred = [](const Part &) {
57 return false;
58 };
59
60 if (!Config.ToRemove.empty())
61 RemovePred = [&Config](const Part &P) {
62 return Config.ToRemove.matches(P.Name);
63 };
64
65 if (!Config.OnlySection.empty())
66 RemovePred = [&Config](const Part &P) {
67 // Explicitly keep these sections regardless of previous removes and
68 // remove everything else.
69 return !Config.OnlySection.matches(P.Name);
70 };
71
72 if (auto E = Obj.removeParts(RemovePred))
73 return E;
74
75 Obj.recomputeHeader();
76 return Error::success();
77}
78
80 const DXContainerConfig &,
82 DXContainerReader Reader(In);
83 Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create();
84 if (!ObjOrErr)
85 return createFileError(Config.InputFilename, ObjOrErr.takeError());
86 Object *Obj = ObjOrErr->get();
87 assert(Obj && "Unable to deserialize DXContainer object");
88
89 if (Error E = handleArgs(Config, *Obj))
90 return E;
91
92 DXContainerWriter Writer(*Obj, Out);
93 if (Error E = Writer.write())
94 return createFileError(Config.OutputFilename, std::move(E));
95 return Error::success();
96}
97
98} // end namespace dxbc
99} // end namespace objcopy
100} // 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("-"))
#define P(N)
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
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:233
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)
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.
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.
SmallVector< StringRef, 0 > ExtractSection
Error removeParts(PartPred ToRemove)
::llvm::dxbc::Header Header