Skip to content

Commit 09a5ebe

Browse files
committed
[DirectX] Add suport to remove specified sections (parts)
1 parent d450f5e commit 09a5ebe

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

llvm/lib/ObjCopy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ source_group("Source Files\\DXContainer" REGULAR_EXPRESSION
4444
add_llvm_component_library(LLVMObjCopy
4545
Archive.cpp
4646
DXContainer/DXContainerObjcopy.cpp
47+
DXContainer/DXContainerObject.cpp
4748
DXContainer/DXContainerReader.cpp
4849
DXContainer/DXContainerWriter.cpp
4950
CommonConfig.cpp

llvm/lib/ObjCopy/DXContainer/DXContainerObjcopy.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ namespace dxbc {
1919
using namespace object;
2020

2121
static Error handleArgs(const CommonConfig &Config, Object &Obj) {
22+
std::function<bool(const Part &)> RemovePred = [](const Part &) {
23+
return false;
24+
};
25+
26+
if (!Config.ToRemove.empty())
27+
RemovePred = [&Config](const Part &P) {
28+
return Config.ToRemove.matches(P.Name);
29+
};
30+
31+
if (auto E = Obj.removeParts(RemovePred))
32+
return E;
33+
2234
return Error::success();
2335
}
2436

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- DXContainerObject.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 "DXContainerObject.h"
10+
11+
namespace llvm {
12+
namespace objcopy {
13+
namespace dxbc {
14+
15+
Error Object::removeParts(PartPred ToRemove) {
16+
erase_if(Parts, ToRemove);
17+
recomputeHeader();
18+
return Error::success();
19+
}
20+
21+
void Object::recomputeHeader() {
22+
Header.FileSize = headerSize();
23+
Header.PartCount = Parts.size();
24+
for (const Part &P : Parts)
25+
Header.FileSize += P.size();
26+
}
27+
28+
} // end namespace dxbc
29+
} // end namespace objcopy
30+
} // end namespace llvm

llvm/lib/ObjCopy/DXContainer/DXContainerObject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct Part {
3030
}
3131
};
3232

33+
using PartPred = llvm::function_ref<bool(const Part &)>;
34+
3335
struct Object {
3436
::llvm::dxbc::Header Header;
3537
SmallVector<Part> Parts;
@@ -38,6 +40,11 @@ struct Object {
3840
return sizeof(::llvm::dxbc::Header) // base header
3941
+ sizeof(uint32_t) * Parts.size(); // part offset values
4042
}
43+
44+
Error removeParts(PartPred ToRemove);
45+
46+
private:
47+
void recomputeHeader();
4148
};
4249

4350
} // end namespace dxbc

0 commit comments

Comments
 (0)