diff options
Diffstat (limited to 'llvm/tools/llvm-objcopy')
-rw-r--r-- | llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp | 10 | ||||
-rw-r--r-- | llvm/tools/llvm-objcopy/COFF/Object.cpp | 10 | ||||
-rw-r--r-- | llvm/tools/llvm-objcopy/COFF/Object.h | 1 |
3 files changed, 21 insertions, 0 deletions
diff --git a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp index 13d8efde37c..60afbf7bb54 100644 --- a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -46,6 +46,16 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) { return false; }); + if (Config.OnlyKeepDebug) { + // For --only-keep-debug, we keep all other sections, but remove their + // content. The VirtualSize field in the section header is kept intact. + Obj.truncateSections([](const Section &Sec) { + return !isDebugSection(Sec) && Sec.Name != ".buildid" && + ((Sec.Header.Characteristics & + (IMAGE_SCN_CNT_CODE | IMAGE_SCN_CNT_INITIALIZED_DATA)) != 0); + }); + } + // StripAll removes all symbols and thus also removes all relocations. if (Config.StripAll || Config.StripAllGNU) for (Section &Sec : Obj.getMutableSections()) diff --git a/llvm/tools/llvm-objcopy/COFF/Object.cpp b/llvm/tools/llvm-objcopy/COFF/Object.cpp index e19cea6aa9d..fc87d9e574d 100644 --- a/llvm/tools/llvm-objcopy/COFF/Object.cpp +++ b/llvm/tools/llvm-objcopy/COFF/Object.cpp @@ -127,6 +127,16 @@ void Object::removeSections(function_ref<bool(const Section &)> ToRemove) { updateSymbols(); } +void Object::truncateSections(function_ref<bool(const Section &)> ToTruncate) { + for (Section &Sec : Sections) { + if (ToTruncate(Sec)) { + Sec.Contents = ArrayRef<uint8_t>(); + Sec.Relocs.clear(); + Sec.Header.SizeOfRawData = 0; + } + } +} + } // end namespace coff } // end namespace objcopy } // end namespace llvm diff --git a/llvm/tools/llvm-objcopy/COFF/Object.h b/llvm/tools/llvm-objcopy/COFF/Object.h index a73e93620d3..8e200369f0b 100644 --- a/llvm/tools/llvm-objcopy/COFF/Object.h +++ b/llvm/tools/llvm-objcopy/COFF/Object.h @@ -93,6 +93,7 @@ struct Object { void addSections(ArrayRef<Section> NewSections); void removeSections(function_ref<bool(const Section &)> ToRemove); + void truncateSections(function_ref<bool(const Section &)> ToTruncate); private: std::vector<Symbol> Symbols; |