[llvm-objcopy] [COFF] Implement --only-section

Differential Revision: https://reviews.llvm.org/D56873

llvm-svn: 351663
diff --git a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
index 60afbf7..99929d1 100644
--- a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
+++ b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
@@ -33,6 +33,12 @@
 static Error handleArgs(const CopyConfig &Config, Object &Obj) {
   // Perform the actual section removals.
   Obj.removeSections([&Config](const Section &Sec) {
+    // Contrary to --only-keep-debug, --only-section fully removes sections that
+    // aren't mentioned.
+    if (!Config.OnlySection.empty() &&
+        !is_contained(Config.OnlySection, Sec.Name))
+      return true;
+
     if (Config.StripDebug || Config.StripAll || Config.StripAllGNU ||
         Config.DiscardAll || Config.StripUnneeded) {
       if (isDebugSection(Sec) &&