<div dir="ltr">Thanks Alex! I was working on getting my windows box setup to reproduce this. I just got back from England today.</div><br><div class="gmail_quote"><div dir="ltr">On Tue, Apr 24, 2018 at 6:46 AM Alexander Shaposhnikov via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: alexshap<br>
Date: Mon Apr 23 22:43:32 2018<br>
New Revision: 330685<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=330685&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=330685&view=rev</a><br>
Log:<br>
Recommit "[llvm-objcopy] Switch over to using TableGen for parsing arguments"<br>
<br>
Add explicit dependency on ObjcopyTableGen <br>
and rerun the tests on Windows. <br>
I will double-check the build bots <br>
and revert this commit if necessary.<br>
<br>
Added:<br>
llvm/trunk/tools/llvm-objcopy/Opts.td<br>
- copied unchanged from r329866, llvm/trunk/tools/llvm-objcopy/Opts.td<br>
Modified:<br>
llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test<br>
llvm/trunk/test/tools/llvm-objcopy/add-section.test<br>
llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test<br>
llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test<br>
llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test<br>
llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test<br>
llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test<br>
llvm/trunk/test/tools/llvm-objcopy/remove-section.test<br>
llvm/trunk/tools/llvm-objcopy/CMakeLists.txt<br>
llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt<br>
llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp<br>
<br>
Modified: llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test (original)<br>
+++ llvm/trunk/test/tools/llvm-objcopy/add-section-remove.test Mon Apr 23 22:43:32 2018<br>
@@ -1,6 +1,6 @@<br>
# RUN: yaml2obj %s > %t<br>
# RUN: echo 0000 > %t.sec<br>
-# RUN: llvm-objcopy -R=.test2 -add-section=.test2=%t.sec %t %t2<br>
+# RUN: llvm-objcopy -R .test2 -add-section=.test2=%t.sec %t %t2<br>
# RUN: llvm-readobj -file-headers -sections -section-data %t2 | FileCheck %s<br>
<br>
!ELF<br>
<br>
Modified: llvm/trunk/test/tools/llvm-objcopy/add-section.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/add-section.test?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/add-section.test?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/llvm-objcopy/add-section.test (original)<br>
+++ llvm/trunk/test/tools/llvm-objcopy/add-section.test Mon Apr 23 22:43:32 2018<br>
@@ -1,6 +1,6 @@<br>
# RUN: yaml2obj %s > %t<br>
# RUN: llvm-objcopy -O binary -j .test2 %t %t.sec<br>
-# RUN: llvm-objcopy -R=.test2 %t %t2<br>
+# RUN: llvm-objcopy -R .test2 %t %t2<br>
# RUN: llvm-objcopy -add-section=.test2=%t.sec %t2 %t3<br>
# RUN: llvm-readobj -file-headers -sections -section-data %t3 | FileCheck %s<br>
<br>
<br>
Modified: llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test (original)<br>
+++ llvm/trunk/test/tools/llvm-objcopy/basic-only-keep.test Mon Apr 23 22:43:32 2018<br>
@@ -1,6 +1,6 @@<br>
# RUN: yaml2obj %s > %t<br>
# RUN: llvm-objcopy -only-keep=.test %t %t2<br>
-# RUN: llvm-objcopy -j=.test %t %t3<br>
+# RUN: llvm-objcopy -j .test %t %t3<br>
# RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s<br>
# RUN: diff %t2 %t3<br>
<br>
<br>
Modified: llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test (original)<br>
+++ llvm/trunk/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test Mon Apr 23 22:43:32 2018<br>
@@ -1,5 +1,5 @@<br>
# RUN: yaml2obj %s -o %t<br>
-# RUN: llvm-objcopy -R=.note -O binary %t %t2<br>
+# RUN: llvm-objcopy -R .note -O binary %t %t2<br>
# RUN: od -Ax -t x1 %t2 | FileCheck %s<br>
<br>
!ELF<br>
<br>
Modified: llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test (original)<br>
+++ llvm/trunk/test/tools/llvm-objcopy/keep-only-keep.test Mon Apr 23 22:43:32 2018<br>
@@ -1,6 +1,6 @@<br>
# RUN: yaml2obj %s > %t<br>
# RUN: llvm-objcopy -keep=.test2 -only-keep=.test %t %t2<br>
-# RUN: llvm-objcopy -j=.test -keep=.test2 %t %t3<br>
+# RUN: llvm-objcopy -j .test -keep=.test2 %t %t3<br>
# RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s<br>
# RUN: diff %t2 %t3<br>
<br>
<br>
Modified: llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test (original)<br>
+++ llvm/trunk/test/tools/llvm-objcopy/only-keep-remove-strtab.test Mon Apr 23 22:43:32 2018<br>
@@ -1,6 +1,6 @@<br>
# RUN: yaml2obj %s > %t<br>
# RUN: llvm-objcopy -R .symtab -R .strtab -only-keep=.test %t %t2<br>
-# RUN: llvm-objcopy -j=.test -R .strtab -R .symtab %t %t3<br>
+# RUN: llvm-objcopy -j .test -R .strtab -R .symtab %t %t3<br>
# RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s<br>
# RUN: diff %t2 %t3<br>
<br>
<br>
Modified: llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test (original)<br>
+++ llvm/trunk/test/tools/llvm-objcopy/parent-loop-check.test Mon Apr 23 22:43:32 2018<br>
@@ -9,7 +9,7 @@<br>
# don't.<br>
<br>
# RUN: yaml2obj %s -o %t<br>
-# RUN: llvm-objcopy -R=.note -O binary %t %t2<br>
+# RUN: llvm-objcopy -R .note -O binary %t %t2<br>
# RUN: od -Ax -t x1 %t2 | FileCheck %s<br>
<br>
!ELF<br>
<br>
Modified: llvm/trunk/test/tools/llvm-objcopy/remove-section.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/remove-section.test?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/remove-section.test?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/tools/llvm-objcopy/remove-section.test (original)<br>
+++ llvm/trunk/test/tools/llvm-objcopy/remove-section.test Mon Apr 23 22:43:32 2018<br>
@@ -1,5 +1,5 @@<br>
# RUN: yaml2obj %s > %t<br>
-# RUN: llvm-objcopy -R=.test2 %t %t2<br>
+# RUN: llvm-objcopy -R .test2 %t %t2<br>
# RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s<br>
<br>
!ELF<br>
<br>
Modified: llvm/trunk/tools/llvm-objcopy/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/CMakeLists.txt?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/CMakeLists.txt?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/llvm-objcopy/CMakeLists.txt (original)<br>
+++ llvm/trunk/tools/llvm-objcopy/CMakeLists.txt Mon Apr 23 22:43:32 2018<br>
@@ -1,11 +1,20 @@<br>
set(LLVM_LINK_COMPONENTS<br>
Object<br>
+ Option<br>
Support<br>
MC<br>
)<br>
+<br>
+set(LLVM_TARGET_DEFINITIONS Opts.td)<br>
+<br>
+tablegen(LLVM Opts.inc -gen-opt-parser-defs)<br>
+add_public_tablegen_target(ObjcopyTableGen)<br>
+<br>
add_llvm_tool(llvm-objcopy<br>
llvm-objcopy.cpp<br>
Object.cpp<br>
+ DEPENDS<br>
+ ObjcopyTableGen<br>
)<br>
<br>
if(LLVM_INSTALL_BINUTILS_SYMLINKS)<br>
<br>
Modified: llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt (original)<br>
+++ llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt Mon Apr 23 22:43:32 2018<br>
@@ -18,4 +18,4 @@<br>
type = Tool<br>
name = llvm-objcopy<br>
parent = Tools<br>
-required_libraries = Object Support MC<br>
+required_libraries = Object Option Support MC<br>
<br>
Modified: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp?rev=330685&r1=330684&r2=330685&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp?rev=330685&r1=330684&r2=330685&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp (original)<br>
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp Mon Apr 23 22:43:32 2018<br>
@@ -17,6 +17,9 @@<br>
#include "llvm/Object/ELFObjectFile.h"<br>
#include "llvm/Object/ELFTypes.h"<br>
#include "llvm/Object/Error.h"<br>
+#include "llvm/Option/Arg.h"<br>
+#include "llvm/Option/ArgList.h"<br>
+#include "llvm/Option/Option.h"<br>
#include "llvm/Support/Casting.h"<br>
#include "llvm/Support/CommandLine.h"<br>
#include "llvm/Support/Compiler.h"<br>
@@ -40,6 +43,39 @@ using namespace llvm;<br>
using namespace object;<br>
using namespace ELF;<br>
<br>
+namespace {<br>
+<br>
+enum ID {<br>
+ OBJCOPY_INVALID = 0, // This is not an option ID.<br>
+#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \<br>
+ HELPTEXT, METAVAR, VALUES) \<br>
+ OBJCOPY_##ID,<br>
+#include "Opts.inc"<br>
+#undef OPTION<br>
+};<br>
+<br>
+#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;<br>
+#include "Opts.inc"<br>
+#undef PREFIX<br>
+<br>
+static constexpr opt::OptTable::Info ObjcopyInfoTable[] = {<br>
+#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \<br>
+ HELPTEXT, METAVAR, VALUES) \<br>
+ {PREFIX, NAME, HELPTEXT, \<br>
+ METAVAR, OBJCOPY_##ID, opt::Option::KIND##Class, \<br>
+ PARAM, FLAGS, OBJCOPY_##GROUP, \<br>
+ OBJCOPY_##ALIAS, ALIASARGS, VALUES},<br>
+#include "Opts.inc"<br>
+#undef OPTION<br>
+};<br>
+<br>
+class ObjcopyOptTable : public opt::OptTable {<br>
+public:<br>
+ ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {}<br>
+};<br>
+<br>
+} // namespace<br>
+<br>
// The name this program was invoked as.<br>
static StringRef ToolName;<br>
<br>
@@ -69,60 +105,28 @@ LLVM_ATTRIBUTE_NORETURN void reportError<br>
<br>
} // end namespace llvm<br>
<br>
-static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));<br>
-static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("[ <output> ]"));<br>
-<br>
-static cl::opt<std::string><br>
- OutputFormat("O", cl::desc("Set output format to one of the following:"<br>
- "\n\tbinary"));<br>
-static cl::list<std::string> ToRemove("remove-section",<br>
- cl::desc("Remove <section>"),<br>
- cl::value_desc("section"));<br>
-static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),<br>
- cl::aliasopt(ToRemove));<br>
-static cl::opt<bool> StripAll(<br>
- "strip-all",<br>
- cl::desc(<br>
- "Removes non-allocated sections other than .gnu.warning* sections"));<br>
-static cl::opt<bool><br>
- StripAllGNU("strip-all-gnu",<br>
- cl::desc("Removes symbol, relocation, and debug information"));<br>
-static cl::list<std::string> Keep("keep", cl::desc("Keep <section>"),<br>
- cl::value_desc("section"));<br>
-static cl::list<std::string> OnlyKeep("only-keep",<br>
- cl::desc("Remove all but <section>"),<br>
- cl::value_desc("section"));<br>
-static cl::alias OnlyKeepA("j", cl::desc("Alias for only-keep"),<br>
- cl::aliasopt(OnlyKeep));<br>
-static cl::opt<bool> StripDebug("strip-debug",<br>
- cl::desc("Removes all debug information"));<br>
-static cl::opt<bool> StripSections("strip-sections",<br>
- cl::desc("Remove all section headers"));<br>
-static cl::opt<bool><br>
- StripNonAlloc("strip-non-alloc",<br>
- cl::desc("Remove all non-allocated sections"));<br>
-static cl::opt<bool><br>
- StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file"));<br>
-static cl::opt<bool> ExtractDWO(<br>
- "extract-dwo",<br>
- cl::desc("Remove all sections that are not DWARF .dwo sections from file"));<br>
-static cl::opt<std::string><br>
- SplitDWO("split-dwo",<br>
- cl::desc("Equivalent to extract-dwo on the input file to "<br>
- "<dwo-file>, then strip-dwo on the input file"),<br>
- cl::value_desc("dwo-file"));<br>
-static cl::list<std::string> AddSection(<br>
- "add-section",<br>
- cl::desc("Make a section named <section> with the contents of <file>."),<br>
- cl::value_desc("section=file"));<br>
-static cl::opt<bool> LocalizeHidden(<br>
- "localize-hidden",<br>
- cl::desc(<br>
- "Mark all symbols that have hidden or internal visibility as local"));<br>
-static cl::opt<std::string><br>
- AddGnuDebugLink("add-gnu-debuglink",<br>
- cl::desc("adds a .gnu_debuglink for <debug-file>"),<br>
- cl::value_desc("debug-file"));<br>
+struct CopyConfig {<br>
+ StringRef OutputFilename;<br>
+ StringRef InputFilename;<br>
+ StringRef OutputFormat;<br>
+ StringRef InputFormat;<br>
+ StringRef BinaryArch;<br>
+<br>
+ StringRef SplitDWO;<br>
+ StringRef AddGnuDebugLink;<br>
+ std::vector<StringRef> ToRemove;<br>
+ std::vector<StringRef> Keep;<br>
+ std::vector<StringRef> OnlyKeep;<br>
+ std::vector<StringRef> AddSection;<br>
+ bool StripAll;<br>
+ bool StripAllGNU;<br>
+ bool StripDebug;<br>
+ bool StripSections;<br>
+ bool StripNonAlloc;<br>
+ bool StripDWO;<br>
+ bool ExtractDWO;<br>
+ bool LocalizeHidden;<br>
+};<br>
<br>
using SectionPred = std::function<bool(const SectionBase &Sec)>;<br>
<br>
@@ -137,31 +141,35 @@ bool OnlyKeepDWOPred(const Object &Obj,<br>
return !IsDWOSection(Sec);<br>
}<br>
<br>
-static ElfType OutputElfType;<br>
-<br>
-std::unique_ptr<Writer> CreateWriter(Object &Obj, StringRef File) {<br>
- if (OutputFormat == "binary") {<br>
- return llvm::make_unique<BinaryWriter>(OutputFilename, Obj);<br>
+std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,<br>
+ StringRef File, ElfType OutputElfType) {<br>
+ if (Config.OutputFormat == "binary") {<br>
+ return llvm::make_unique<BinaryWriter>(File, Obj);<br>
}<br>
// Depending on the initial ELFT and OutputFormat we need a different Writer.<br>
switch (OutputElfType) {<br>
case ELFT_ELF32LE:<br>
- return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, !StripSections);<br>
+ return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,<br>
+ !Config.StripSections);<br>
case ELFT_ELF64LE:<br>
- return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj, !StripSections);<br>
+ return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,<br>
+ !Config.StripSections);<br>
case ELFT_ELF32BE:<br>
- return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj, !StripSections);<br>
+ return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,<br>
+ !Config.StripSections);<br>
case ELFT_ELF64BE:<br>
- return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj, !StripSections);<br>
+ return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,<br>
+ !Config.StripSections);<br>
}<br>
llvm_unreachable("Invalid output format");<br>
}<br>
<br>
-void SplitDWOToFile(const Reader &Reader, StringRef File) {<br>
+void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,<br>
+ StringRef File, ElfType OutputElfType) {<br>
auto DWOFile = Reader.create();<br>
DWOFile->removeSections(<br>
[&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });<br>
- auto Writer = CreateWriter(*DWOFile, File);<br>
+ auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);<br>
Writer->finalize();<br>
Writer->write();<br>
}<br>
@@ -173,15 +181,16 @@ void SplitDWOToFile(const Reader &Reader<br>
// any previous removals. Lastly whether or not something is removed shouldn't<br>
// depend a) on the order the options occur in or b) on some opaque priority<br>
// system. The only priority is that keeps/copies overrule removes.<br>
-void HandleArgs(Object &Obj, const Reader &Reader) {<br>
+void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,<br>
+ ElfType OutputElfType) {<br>
<br>
- if (!SplitDWO.empty()) {<br>
- SplitDWOToFile(Reader, SplitDWO);<br>
+ if (!Config.SplitDWO.empty()) {<br>
+ SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);<br>
}<br>
<br>
// Localize:<br>
<br>
- if (LocalizeHidden) {<br>
+ if (Config.LocalizeHidden) {<br>
Obj.SymbolTable->localize([](const Symbol &Sym) {<br>
return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL;<br>
});<br>
@@ -191,24 +200,24 @@ void HandleArgs(Object &Obj, const Reade<br>
<br>
// Removes:<br>
<br>
- if (!ToRemove.empty()) {<br>
- RemovePred = [&](const SectionBase &Sec) {<br>
- return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=<br>
- std::end(ToRemove);<br>
+ if (!Config.ToRemove.empty()) {<br>
+ RemovePred = [&Config](const SectionBase &Sec) {<br>
+ return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),<br>
+ Sec.Name) != std::end(Config.ToRemove);<br>
};<br>
}<br>
<br>
- if (StripDWO || !SplitDWO.empty())<br>
+ if (Config.StripDWO || !Config.SplitDWO.empty())<br>
RemovePred = [RemovePred](const SectionBase &Sec) {<br>
return IsDWOSection(Sec) || RemovePred(Sec);<br>
};<br>
<br>
- if (ExtractDWO)<br>
+ if (Config.ExtractDWO)<br>
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {<br>
return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);<br>
};<br>
<br>
- if (StripAllGNU)<br>
+ if (Config.StripAllGNU)<br>
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {<br>
if (RemovePred(Sec))<br>
return true;<br>
@@ -226,19 +235,19 @@ void HandleArgs(Object &Obj, const Reade<br>
return Sec.Name.startswith(".debug");<br>
};<br>
<br>
- if (StripSections) {<br>
+ if (Config.StripSections) {<br>
RemovePred = [RemovePred](const SectionBase &Sec) {<br>
return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;<br>
};<br>
}<br>
<br>
- if (StripDebug) {<br>
+ if (Config.StripDebug) {<br>
RemovePred = [RemovePred](const SectionBase &Sec) {<br>
return RemovePred(Sec) || Sec.Name.startswith(".debug");<br>
};<br>
}<br>
<br>
- if (StripNonAlloc)<br>
+ if (Config.StripNonAlloc)<br>
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {<br>
if (RemovePred(Sec))<br>
return true;<br>
@@ -247,7 +256,7 @@ void HandleArgs(Object &Obj, const Reade<br>
return (Sec.Flags & SHF_ALLOC) == 0;<br>
};<br>
<br>
- if (StripAll)<br>
+ if (Config.StripAll)<br>
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {<br>
if (RemovePred(Sec))<br>
return true;<br>
@@ -260,11 +269,11 @@ void HandleArgs(Object &Obj, const Reade<br>
<br>
// Explicit copies:<br>
<br>
- if (!OnlyKeep.empty()) {<br>
- RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {<br>
+ if (!Config.OnlyKeep.empty()) {<br>
+ RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {<br>
// Explicitly keep these sections regardless of previous removes.<br>
- if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) !=<br>
- std::end(OnlyKeep))<br>
+ if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),<br>
+ Sec.Name) != std::end(Config.OnlyKeep))<br>
return false;<br>
<br>
// Allow all implicit removes.<br>
@@ -282,11 +291,11 @@ void HandleArgs(Object &Obj, const Reade<br>
};<br>
}<br>
<br>
- if (!Keep.empty()) {<br>
- RemovePred = [RemovePred](const SectionBase &Sec) {<br>
+ if (!Config.Keep.empty()) {<br>
+ RemovePred = [Config, RemovePred](const SectionBase &Sec) {<br>
// Explicitly keep these sections regardless of previous removes.<br>
- if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) !=<br>
- std::end(Keep))<br>
+ if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=<br>
+ std::end(Config.Keep))<br>
return false;<br>
// Otherwise defer to RemovePred.<br>
return RemovePred(Sec);<br>
@@ -295,9 +304,9 @@ void HandleArgs(Object &Obj, const Reade<br>
<br>
Obj.removeSections(RemovePred);<br>
<br>
- if (!AddSection.empty()) {<br>
- for (const auto &Flag : AddSection) {<br>
- auto SecPair = StringRef(Flag).split("=");<br>
+ if (!Config.AddSection.empty()) {<br>
+ for (const auto &Flag : Config.AddSection) {<br>
+ auto SecPair = Flag.split("=");<br>
auto SecName = SecPair.first;<br>
auto File = SecPair.second;<br>
auto BufOrErr = MemoryBuffer::getFile(File);<br>
@@ -311,34 +320,92 @@ void HandleArgs(Object &Obj, const Reade<br>
}<br>
}<br>
<br>
- if (!AddGnuDebugLink.empty()) {<br>
- Obj.addSection<GnuDebugLinkSection>(StringRef(AddGnuDebugLink));<br>
+ if (!Config.AddGnuDebugLink.empty()) {<br>
+ Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);<br>
}<br>
}<br>
<br>
-std::unique_ptr<Reader> CreateReader() {<br>
+std::unique_ptr<Reader> CreateReader(StringRef InputFilename,<br>
+ ElfType &OutputElfType) {<br>
// Right now we can only read ELF files so there's only one reader;<br>
- auto Out = llvm::make_unique<ELFReader>(StringRef(InputFilename));<br>
+ auto Out = llvm::make_unique<ELFReader>(InputFilename);<br>
// We need to set the default ElfType for output.<br>
OutputElfType = Out->getElfType();<br>
return std::move(Out);<br>
}<br>
<br>
+void ExecuteElfObjcopy(const CopyConfig &Config) {<br>
+ ElfType OutputElfType;<br>
+ auto Reader = CreateReader(Config.InputFilename, OutputElfType);<br>
+ auto Obj = Reader->create();<br>
+ auto Writer =<br>
+ CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);<br>
+ HandleArgs(Config, *Obj, *Reader, OutputElfType);<br>
+ Writer->finalize();<br>
+ Writer->write();<br>
+}<br>
+<br>
+// ParseObjcopyOptions returns the config and sets the input arguments. If a<br>
+// help flag is set then ParseObjcopyOptions will print the help messege and<br>
+// exit.<br>
+CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {<br>
+ ObjcopyOptTable T;<br>
+ unsigned MissingArgumentIndex, MissingArgumentCount;<br>
+ llvm::opt::InputArgList InputArgs =<br>
+ T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);<br>
+<br>
+ if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {<br>
+ T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");<br>
+ exit(0);<br>
+ }<br>
+<br>
+ SmallVector<const char *, 2> Positional;<br>
+<br>
+ for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))<br>
+ error("unknown argument '" + Arg->getAsString(InputArgs) + "'");<br>
+<br>
+ for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))<br>
+ Positional.push_back(Arg->getValue());<br>
+<br>
+ if (Positional.empty())<br>
+ error("No input file specified");<br>
+<br>
+ if (Positional.size() > 2)<br>
+ error("Too many positional arguments");<br>
+<br>
+ CopyConfig Config;<br>
+ Config.InputFilename = Positional[0];<br>
+ Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];<br>
+ Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);<br>
+ Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);<br>
+ Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);<br>
+<br>
+ Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);<br>
+ Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);<br>
+ for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))<br>
+ Config.ToRemove.push_back(Arg->getValue());<br>
+ for (auto Arg : InputArgs.filtered(OBJCOPY_keep))<br>
+ Config.Keep.push_back(Arg->getValue());<br>
+ for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))<br>
+ Config.OnlyKeep.push_back(Arg->getValue());<br>
+ for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))<br>
+ Config.AddSection.push_back(Arg->getValue());<br>
+ Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);<br>
+ Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);<br>
+ Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);<br>
+ Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);<br>
+ Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);<br>
+ Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);<br>
+ Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);<br>
+ Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);<br>
+<br>
+ return Config;<br>
+}<br>
+<br>
int main(int argc, char **argv) {<br>
InitLLVM X(argc, argv);<br>
- cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n");<br>
ToolName = argv[0];<br>
- if (InputFilename.empty()) {<br>
- cl::PrintHelpMessage();<br>
- return 2;<br>
- }<br>
<br>
- auto Reader = CreateReader();<br>
- auto Obj = Reader->create();<br>
- StringRef Output =<br>
- OutputFilename.getNumOccurrences() ? OutputFilename : InputFilename;<br>
- auto Writer = CreateWriter(*Obj, Output);<br>
- HandleArgs(*Obj, *Reader);<br>
- Writer->finalize();<br>
- Writer->write();<br>
+ CopyConfig Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));<br>
+ ExecuteElfObjcopy(Config);<br>
}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>