summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
diff options
context:
space:
mode:
authorJake Ehrlich <jakehehrlich@google.com>2017-10-11 01:59:06 +0000
committerJake Ehrlich <jakehehrlich@google.com>2017-10-11 01:59:06 +0000
commitb5152447baba04d240ab2a831c9558b55f2dfd7d (patch)
tree03dd1acf3cb1b47735b91c8ec4a4486870aaf8a0 /llvm/tools/llvm-objcopy/llvm-objcopy.cpp
parent492cf98a8a3924935ffd30f53fc6f14e58b578dc (diff)
downloadbcm5719-llvm-b5152447baba04d240ab2a831c9558b55f2dfd7d.tar.gz
bcm5719-llvm-b5152447baba04d240ab2a831c9558b55f2dfd7d.zip
[llvm-objcopy] Add support for --strip-sections to remove all section headers leaving only program headers and loadable segment data
elf utils implements a particularly extreme form of stripping that I'd like to support. eu-strip has an option called "strip-sections" that removes all section headers and leaves only program headers and the segment data. I have implemented this option partly as a test but mainly because in Fuchsia we would like to use this option to minimize the size of our executables. The other strip options that are on my list include --strip-all and --strip-debug. This is a preliminary implementation that I'd like to start using in Fuchsia builds if possible. This change implements such a stripping option for llvm-objcopy Differential Revision: https://reviews.llvm.org/D38335 llvm-svn: 315412
Diffstat (limited to 'llvm/tools/llvm-objcopy/llvm-objcopy.cpp')
-rw-r--r--llvm/tools/llvm-objcopy/llvm-objcopy.cpp22
1 files changed, 19 insertions, 3 deletions
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
index d76735482d6..7f55a434b33 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -56,11 +56,14 @@ cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"),
cl::opt<std::string>
OutputFormat("O", cl::desc("set output format to one of the following:"
"\n\tbinary"));
-
cl::list<std::string> ToRemove("remove-section",
cl::desc("Remove a specific section"));
cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
cl::aliasopt(ToRemove));
+cl::opt<bool> StripSections("strip-sections",
+ cl::desc("Remove all section headers"));
+
+typedef std::function<bool(const SectionBase &Sec)> SectionPred;
void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) {
std::unique_ptr<FileOutputBuffer> Buffer;
@@ -71,12 +74,25 @@ void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) {
Obj = llvm::make_unique<BinaryObject<ELF64LE>>(ObjFile);
else
Obj = llvm::make_unique<ELFObject<ELF64LE>>(ObjFile);
+
+ SectionPred RemovePred = [](const SectionBase &) { return false; };
+
if (!ToRemove.empty()) {
- Obj->removeSections([&](const SectionBase &Sec) {
+ RemovePred = [&](const SectionBase &Sec) {
return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
std::end(ToRemove);
- });
+ };
}
+
+ if (StripSections) {
+ RemovePred = [RemovePred](const SectionBase &Sec) {
+ return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
+ };
+ Obj->WriteSectionHeaders = false;
+ }
+
+ Obj->removeSections(RemovePred);
+
Obj->finalize();
ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
FileOutputBuffer::create(OutputFilename, Obj->totalSize(),
OpenPOWER on IntegriCloud