[llvm] r329867 - Revert "[llvm-objcopy] Switch over to using TableGen for parsing arguments"

Jake Ehrlich via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 11 17:40:50 PDT 2018


Author: jakehehrlich
Date: Wed Apr 11 17:40:50 2018
New Revision: 329867

URL: http://llvm.org/viewvc/llvm-project?rev=329867&view=rev
Log:
Revert "[llvm-objcopy] Switch over to using TableGen for parsing arguments"

TableGen seems to work differently on windows. I'll need to revert this

This reverts commit 7a153ddea067b24da59f6a66c733d79205969501.

Removed:
    llvm/trunk/tools/llvm-objcopy/Opts.td
Modified:
    llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test
    llvm/trunk/test/tools/llvm-objcopy/add-section.test
    llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test
    llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test
    llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test
    llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test
    llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test
    llvm/trunk/test/tools/llvm-objcopy/remove-section.test
    llvm/trunk/tools/llvm-objcopy/CMakeLists.txt
    llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt
    llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp

Modified: llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test Wed Apr 11 17:40:50 2018
@@ -1,6 +1,6 @@
 # RUN: yaml2obj %s > %t
 # RUN: echo 0000 > %t.sec
-# RUN: llvm-objcopy -R .test2 -add-section=.test2=%t.sec %t %t2
+# RUN: llvm-objcopy -R=.test2 -add-section=.test2=%t.sec %t %t2
 # RUN: llvm-readobj -file-headers -sections -section-data %t2 | FileCheck %s
 
 !ELF

Modified: llvm/trunk/test/tools/llvm-objcopy/add-section.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/add-section.test?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/add-section.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/add-section.test Wed Apr 11 17:40:50 2018
@@ -1,6 +1,6 @@
 # RUN: yaml2obj %s > %t
 # RUN: llvm-objcopy -O binary -j .test2 %t %t.sec
-# RUN: llvm-objcopy -R .test2 %t %t2
+# RUN: llvm-objcopy -R=.test2 %t %t2
 # RUN: llvm-objcopy -add-section=.test2=%t.sec %t2 %t3
 # RUN: llvm-readobj -file-headers -sections -section-data %t3 | FileCheck %s
 

Modified: llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test Wed Apr 11 17:40:50 2018
@@ -1,6 +1,6 @@
 # RUN: yaml2obj %s > %t
 # RUN: llvm-objcopy -only-keep=.test %t %t2
-# RUN: llvm-objcopy -j .test %t %t3
+# RUN: llvm-objcopy -j=.test %t %t3
 # RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s
 # RUN: diff %t2 %t3
 

Modified: llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test Wed Apr 11 17:40:50 2018
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s -o %t
-# RUN: llvm-objcopy -R .note -O binary %t %t2
+# RUN: llvm-objcopy -R=.note -O binary %t %t2
 # RUN: od -Ax -t x1 %t2 | FileCheck %s
 
 !ELF

Modified: llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test Wed Apr 11 17:40:50 2018
@@ -1,6 +1,6 @@
 # RUN: yaml2obj %s > %t
 # RUN: llvm-objcopy -keep=.test2 -only-keep=.test %t %t2
-# RUN: llvm-objcopy -j .test -keep=.test2 %t %t3
+# RUN: llvm-objcopy -j=.test -keep=.test2 %t %t3
 # RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s
 # RUN: diff %t2 %t3
 

Modified: llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test Wed Apr 11 17:40:50 2018
@@ -1,6 +1,6 @@
 # RUN: yaml2obj %s > %t
 # RUN: llvm-objcopy -R .symtab -R .strtab -only-keep=.test %t %t2
-# RUN: llvm-objcopy -j .test -R .strtab -R .symtab %t %t3
+# RUN: llvm-objcopy -j=.test -R .strtab -R .symtab %t %t3
 # RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s
 # RUN: diff %t2 %t3
 

Modified: llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test Wed Apr 11 17:40:50 2018
@@ -9,7 +9,7 @@
 # don't.
 
 # RUN: yaml2obj %s -o %t
-# RUN: llvm-objcopy -R .note -O binary %t %t2
+# RUN: llvm-objcopy -R=.note -O binary %t %t2
 # RUN: od -Ax -t x1 %t2 | FileCheck %s
 
 !ELF

Modified: llvm/trunk/test/tools/llvm-objcopy/remove-section.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/remove-section.test?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/remove-section.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/remove-section.test Wed Apr 11 17:40:50 2018
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s > %t
-# RUN: llvm-objcopy -R .test2 %t %t2
+# RUN: llvm-objcopy -R=.test2 %t %t2
 # RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s
 
 !ELF

Modified: llvm/trunk/tools/llvm-objcopy/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/CMakeLists.txt?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/CMakeLists.txt (original)
+++ llvm/trunk/tools/llvm-objcopy/CMakeLists.txt Wed Apr 11 17:40:50 2018
@@ -1,15 +1,8 @@
 set(LLVM_LINK_COMPONENTS
   Object
-  Option
   Support
   MC
   )
-
-set(LLVM_TARGET_DEFINITIONS Opts.td)
-
-tablegen(LLVM Opts.inc -gen-opt-parser-defs)
-add_public_tablegen_target(ObjcopyTableGen)
-
 add_llvm_tool(llvm-objcopy
   llvm-objcopy.cpp
   Object.cpp

Modified: llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt (original)
+++ llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt Wed Apr 11 17:40:50 2018
@@ -18,4 +18,4 @@
 type = Tool
 name = llvm-objcopy
 parent = Tools
-required_libraries = Object Option Support MC
+required_libraries = Object Support MC

Removed: llvm/trunk/tools/llvm-objcopy/Opts.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Opts.td?rev=329866&view=auto
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Opts.td (original)
+++ llvm/trunk/tools/llvm-objcopy/Opts.td (removed)
@@ -1,58 +0,0 @@
-include "llvm/Option/OptParser.td"
-
-multiclass Eq<string name> {
-  def NAME: Separate<["--", "-"], name>;
-  def NAME # _eq: Joined<["--", "-"], name # "=">, Alias<!cast<Separate>(NAME)>;
-}
-
-def help : Flag<["-", "--"], "help">;
-defm binary_architecture : Eq<"binary-architecture">,
-                           HelpText<"Used when transforming an architecture-less format (such as binary) to another format">;
-def B : JoinedOrSeparate<["-"], "B">,
-        Alias<binary_architecture>;
-defm input_target : Eq<"input-target">,
-                    HelpText<"Format of the input file">,
-                    Values<"binary">;
-defm output_target : Eq<"output-target">,
-                     HelpText<"Format of the output file">,
-                     Values<"binary">;
-def O : JoinedOrSeparate<["-"], "O">,
-        Alias<output_target>;
-defm split_dwo : Eq<"split-dwo">,
-                 MetaVarName<"dwo-file">,
-                 HelpText<"Equivalent to extract-dwo on the input file to <dwo-file>, then strip-dwo on the input file">;
-defm add_gnu_debuglink : Eq<"add-gnu-debuglink">,
-                         MetaVarName<"debug-file">,
-                         HelpText<"adds a .gnu_debuglink for <debug-file>">;
-defm remove_section : Eq<"remove-section">,
-                      MetaVarName<"section">,
-                      HelpText<"Remove <section>">;
-def R : JoinedOrSeparate<["-"], "R">,
-        Alias<remove_section>;
-defm keep : Eq<"keep">,
-            MetaVarName<"section">,
-            HelpText<"Keep <section>">;
-defm only_keep : Eq<"only-keep">,
-                 MetaVarName<"section">,
-                 HelpText<"Remove all but <section>">;
-def j : JoinedOrSeparate<["-"], "j">,
-                      Alias<only_keep>;
-defm add_section : Eq<"add-section">,
-                   MetaVarName<"section=file">,
-                   HelpText<"Make a section named <section> with the contents of <file>.">;
-def strip_all : Flag<["-", "--"], "strip-all">,
-                HelpText<"Removes non-allocated sections other than .gnu.warning* sections">;
-def strip_all_gnu : Flag<["-", "--"], "strip-all-gnu">,
-                    HelpText<"Compaitable with GNU objcopy's --strip-all">;
-def strip_debug : Flag<["-", "--"], "strip-debug">,
-                  HelpText<"Removes all debug information">;
-def strip_dwo : Flag<["-", "--"], "strip-dwo">,
-                HelpText<"Remove all DWARF .dwo sections from file">;
-def strip_sections : Flag<["-", "--"], "strip-sections">,
-                     HelpText<"Remove all section headers">;
-def strip_non_alloc : Flag<["-", "--"], "strip-non-alloc">,
-                      HelpText<"Remove all non-allocated sections">;
-def extract_dwo : Flag<["-", "--"], "extract-dwo">,
-                  HelpText<"Remove all sections that are not DWARF .dwo sections from file">;
-def localize_hidden : Flag<["-", "--"], "localize-hidden">,
-                      HelpText<"Mark all symbols that have hidden or internal visibility as local">;

Modified: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp?rev=329867&r1=329866&r2=329867&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp Wed Apr 11 17:40:50 2018
@@ -17,9 +17,6 @@
 #include "llvm/Object/ELFObjectFile.h"
 #include "llvm/Object/ELFTypes.h"
 #include "llvm/Object/Error.h"
-#include "llvm/Option/Arg.h"
-#include "llvm/Option/ArgList.h"
-#include "llvm/Option/Option.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
@@ -45,39 +42,6 @@ using namespace llvm;
 using namespace object;
 using namespace ELF;
 
-namespace {
-
-enum ID {
-  OBJCOPY_INVALID = 0, // This is not an option ID.
-#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
-               HELPTEXT, METAVAR, VALUES)                                      \
-  OBJCOPY_##ID,
-#include "Opts.inc"
-#undef OPTION
-};
-
-#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
-#include "Opts.inc"
-#undef PREFIX
-
-static constexpr opt::OptTable::Info ObjcopyInfoTable[] = {
-#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
-               HELPTEXT, METAVAR, VALUES)                                      \
-  {PREFIX,          NAME,         HELPTEXT,                                    \
-   METAVAR,         OBJCOPY_##ID, opt::Option::KIND##Class,                    \
-   PARAM,           FLAGS,        OBJCOPY_##GROUP,                             \
-   OBJCOPY_##ALIAS, ALIASARGS,    VALUES},
-#include "Opts.inc"
-#undef OPTION
-};
-
-class ObjcopyOptTable : public opt::OptTable {
-public:
-  ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {}
-};
-
-} // namespace
-
 // The name this program was invoked as.
 static StringRef ToolName;
 
@@ -107,28 +71,60 @@ LLVM_ATTRIBUTE_NORETURN void reportError
 
 } // end namespace llvm
 
-struct CopyConfig {
-  StringRef OutputFilename;
-  StringRef InputFilename;
-  StringRef OutputFormat;
-  StringRef InputFormat;
-  StringRef BinaryArch;
-
-  StringRef SplitDWO;
-  StringRef AddGnuDebugLink;
-  std::vector<StringRef> ToRemove;
-  std::vector<StringRef> Keep;
-  std::vector<StringRef> OnlyKeep;
-  std::vector<StringRef> AddSection;
-  bool StripAll;
-  bool StripAllGNU;
-  bool StripDebug;
-  bool StripSections;
-  bool StripNonAlloc;
-  bool StripDWO;
-  bool ExtractDWO;
-  bool LocalizeHidden;
-};
+static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
+static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("[ <output> ]"));
+
+static cl::opt<std::string>
+    OutputFormat("O", cl::desc("Set output format to one of the following:"
+                               "\n\tbinary"));
+static cl::list<std::string> ToRemove("remove-section",
+                                      cl::desc("Remove <section>"),
+                                      cl::value_desc("section"));
+static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
+                           cl::aliasopt(ToRemove));
+static cl::opt<bool> StripAll(
+    "strip-all",
+    cl::desc(
+        "Removes non-allocated sections other than .gnu.warning* sections"));
+static cl::opt<bool>
+    StripAllGNU("strip-all-gnu",
+                cl::desc("Removes symbol, relocation, and debug information"));
+static cl::list<std::string> Keep("keep", cl::desc("Keep <section>"),
+                                  cl::value_desc("section"));
+static cl::list<std::string> OnlyKeep("only-keep",
+                                      cl::desc("Remove all but <section>"),
+                                      cl::value_desc("section"));
+static cl::alias OnlyKeepA("j", cl::desc("Alias for only-keep"),
+                           cl::aliasopt(OnlyKeep));
+static cl::opt<bool> StripDebug("strip-debug",
+                                cl::desc("Removes all debug information"));
+static cl::opt<bool> StripSections("strip-sections",
+                                   cl::desc("Remove all section headers"));
+static cl::opt<bool>
+    StripNonAlloc("strip-non-alloc",
+                  cl::desc("Remove all non-allocated sections"));
+static cl::opt<bool>
+    StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file"));
+static cl::opt<bool> ExtractDWO(
+    "extract-dwo",
+    cl::desc("Remove all sections that are not DWARF .dwo sections from file"));
+static cl::opt<std::string>
+    SplitDWO("split-dwo",
+             cl::desc("Equivalent to extract-dwo on the input file to "
+                      "<dwo-file>, then strip-dwo on the input file"),
+             cl::value_desc("dwo-file"));
+static cl::list<std::string> AddSection(
+    "add-section",
+    cl::desc("Make a section named <section> with the contents of <file>."),
+    cl::value_desc("section=file"));
+static cl::opt<bool> LocalizeHidden(
+    "localize-hidden",
+    cl::desc(
+        "Mark all symbols that have hidden or internal visibility as local"));
+static cl::opt<std::string>
+    AddGnuDebugLink("add-gnu-debuglink",
+                    cl::desc("adds a .gnu_debuglink for <debug-file>"),
+                    cl::value_desc("debug-file"));
 
 using SectionPred = std::function<bool(const SectionBase &Sec)>;
 
@@ -143,35 +139,31 @@ bool OnlyKeepDWOPred(const Object &Obj,
   return !IsDWOSection(Sec);
 }
 
-std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
-                                     StringRef File, ElfType OutputElfType) {
-  if (Config.OutputFormat == "binary") {
-    return llvm::make_unique<BinaryWriter>(File, Obj);
+static ElfType OutputElfType;
+
+std::unique_ptr<Writer> CreateWriter(Object &Obj, StringRef File) {
+  if (OutputFormat == "binary") {
+    return llvm::make_unique<BinaryWriter>(OutputFilename, Obj);
   }
   // Depending on the initial ELFT and OutputFormat we need a different Writer.
   switch (OutputElfType) {
   case ELFT_ELF32LE:
-    return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
-                                                 !Config.StripSections);
+    return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, !StripSections);
   case ELFT_ELF64LE:
-    return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
-                                                 !Config.StripSections);
+    return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj, !StripSections);
   case ELFT_ELF32BE:
-    return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
-                                                 !Config.StripSections);
+    return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj, !StripSections);
   case ELFT_ELF64BE:
-    return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
-                                                 !Config.StripSections);
+    return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj, !StripSections);
   }
   llvm_unreachable("Invalid output format");
 }
 
-void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
-                    StringRef File, ElfType OutputElfType) {
+void SplitDWOToFile(const Reader &Reader, StringRef File) {
   auto DWOFile = Reader.create();
   DWOFile->removeSections(
       [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
-  auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
+  auto Writer = CreateWriter(*DWOFile, File);
   Writer->finalize();
   Writer->write();
 }
@@ -183,16 +175,15 @@ void SplitDWOToFile(const CopyConfig &Co
 // any previous removals. Lastly whether or not something is removed shouldn't
 // depend a) on the order the options occur in or b) on some opaque priority
 // system. The only priority is that keeps/copies overrule removes.
-void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
-                ElfType OutputElfType) {
+void HandleArgs(Object &Obj, const Reader &Reader) {
 
-  if (!Config.SplitDWO.empty()) {
-    SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
+  if (!SplitDWO.empty()) {
+    SplitDWOToFile(Reader, SplitDWO);
   }
 
   // Localize:
 
-  if (Config.LocalizeHidden) {
+  if (LocalizeHidden) {
     Obj.SymbolTable->localize([](const Symbol &Sym) {
       return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL;
     });
@@ -202,24 +193,24 @@ void HandleArgs(const CopyConfig &Config
 
   // Removes:
 
-  if (!Config.ToRemove.empty()) {
-    RemovePred = [&Config](const SectionBase &Sec) {
-      return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
-                       Sec.Name) != std::end(Config.ToRemove);
+  if (!ToRemove.empty()) {
+    RemovePred = [&](const SectionBase &Sec) {
+      return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
+             std::end(ToRemove);
     };
   }
 
-  if (Config.StripDWO || !Config.SplitDWO.empty())
+  if (StripDWO || !SplitDWO.empty())
     RemovePred = [RemovePred](const SectionBase &Sec) {
       return IsDWOSection(Sec) || RemovePred(Sec);
     };
 
-  if (Config.ExtractDWO)
+  if (ExtractDWO)
     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
       return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
     };
 
-  if (Config.StripAllGNU)
+  if (StripAllGNU)
     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
       if (RemovePred(Sec))
         return true;
@@ -237,19 +228,19 @@ void HandleArgs(const CopyConfig &Config
       return Sec.Name.startswith(".debug");
     };
 
-  if (Config.StripSections) {
+  if (StripSections) {
     RemovePred = [RemovePred](const SectionBase &Sec) {
       return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
     };
   }
 
-  if (Config.StripDebug) {
+  if (StripDebug) {
     RemovePred = [RemovePred](const SectionBase &Sec) {
       return RemovePred(Sec) || Sec.Name.startswith(".debug");
     };
   }
 
-  if (Config.StripNonAlloc)
+  if (StripNonAlloc)
     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
       if (RemovePred(Sec))
         return true;
@@ -258,7 +249,7 @@ void HandleArgs(const CopyConfig &Config
       return (Sec.Flags & SHF_ALLOC) == 0;
     };
 
-  if (Config.StripAll)
+  if (StripAll)
     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
       if (RemovePred(Sec))
         return true;
@@ -271,11 +262,11 @@ void HandleArgs(const CopyConfig &Config
 
   // Explicit copies:
 
-  if (!Config.OnlyKeep.empty()) {
-    RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
+  if (!OnlyKeep.empty()) {
+    RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
       // Explicitly keep these sections regardless of previous removes.
-      if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
-                    Sec.Name) != std::end(Config.OnlyKeep))
+      if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) !=
+          std::end(OnlyKeep))
         return false;
 
       // Allow all implicit removes.
@@ -293,11 +284,11 @@ void HandleArgs(const CopyConfig &Config
     };
   }
 
-  if (!Config.Keep.empty()) {
-    RemovePred = [Config, RemovePred](const SectionBase &Sec) {
+  if (!Keep.empty()) {
+    RemovePred = [RemovePred](const SectionBase &Sec) {
       // Explicitly keep these sections regardless of previous removes.
-      if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
-          std::end(Config.Keep))
+      if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) !=
+          std::end(Keep))
         return false;
       // Otherwise defer to RemovePred.
       return RemovePred(Sec);
@@ -306,9 +297,9 @@ void HandleArgs(const CopyConfig &Config
 
   Obj.removeSections(RemovePred);
 
-  if (!Config.AddSection.empty()) {
-    for (const auto &Flag : Config.AddSection) {
-      auto SecPair = Flag.split("=");
+  if (!AddSection.empty()) {
+    for (const auto &Flag : AddSection) {
+      auto SecPair = StringRef(Flag).split("=");
       auto SecName = SecPair.first;
       auto File = SecPair.second;
       auto BufOrErr = MemoryBuffer::getFile(File);
@@ -322,95 +313,37 @@ void HandleArgs(const CopyConfig &Config
     }
   }
 
-  if (!Config.AddGnuDebugLink.empty()) {
-    Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
+  if (!AddGnuDebugLink.empty()) {
+    Obj.addSection<GnuDebugLinkSection>(StringRef(AddGnuDebugLink));
   }
 }
 
-std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
-                                     ElfType &OutputElfType) {
+std::unique_ptr<Reader> CreateReader() {
   // Right now we can only read ELF files so there's only one reader;
-  auto Out = llvm::make_unique<ELFReader>(InputFilename);
+  auto Out = llvm::make_unique<ELFReader>(StringRef(InputFilename));
   // We need to set the default ElfType for output.
   OutputElfType = Out->getElfType();
   return std::move(Out);
 }
 
-void ExecuteElfObjcopy(const CopyConfig &Config) {
-  ElfType OutputElfType;
-  auto Reader = CreateReader(Config.InputFilename, OutputElfType);
-  auto Obj = Reader->create();
-  auto Writer =
-      CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
-  HandleArgs(Config, *Obj, *Reader, OutputElfType);
-  Writer->finalize();
-  Writer->write();
-}
-
-// ParseObjcopyOptions returns the config and sets the input arguments. If a
-// help flag is set then ParseObjcopyOptions will print the help messege and
-// exit.
-CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
-  ObjcopyOptTable T;
-  unsigned MissingArgumentIndex, MissingArgumentCount;
-  llvm::opt::InputArgList InputArgs =
-      T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
-
-  if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {
-    T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
-    exit(0);
-  }
-
-  SmallVector<const char *, 2> Positional;
-
-  for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
-    error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
-
-  for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
-    Positional.push_back(Arg->getValue());
-
-  if (Positional.empty())
-    error("No input file specified");
-
-  if (Positional.size() > 2)
-    error("Too many positional arguments");
-
-  CopyConfig Config;
-  Config.InputFilename = Positional[0];
-  Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
-  Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
-  Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
-  Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
-
-  Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
-  Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
-  for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
-    Config.ToRemove.push_back(Arg->getValue());
-  for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
-    Config.Keep.push_back(Arg->getValue());
-  for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
-    Config.OnlyKeep.push_back(Arg->getValue());
-  for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
-    Config.AddSection.push_back(Arg->getValue());
-  Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
-  Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
-  Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
-  Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
-  Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
-  Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
-  Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
-  Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
-
-  return Config;
-}
-
 int main(int argc, char **argv) {
   // Print a stack trace if we signal out.
   sys::PrintStackTraceOnErrorSignal(argv[0]);
   PrettyStackTraceProgram X(argc, argv);
   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
+  cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n");
   ToolName = argv[0];
+  if (InputFilename.empty()) {
+    cl::PrintHelpMessage();
+    return 2;
+  }
 
-  CopyConfig Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
-  ExecuteElfObjcopy(Config);
+  auto Reader = CreateReader();
+  auto Obj = Reader->create();
+  StringRef Output =
+      OutputFilename.getNumOccurrences() ? OutputFilename : InputFilename;
+  auto Writer = CreateWriter(*Obj, Output);
+  HandleArgs(*Obj, *Reader);
+  Writer->finalize();
+  Writer->write();
 }




More information about the llvm-commits mailing list