Index: llvm/test/tools/llvm-objcopy/add-section-remove.test =================================================================== --- llvm/test/tools/llvm-objcopy/add-section-remove.test +++ llvm/test/tools/llvm-objcopy/add-section-remove.test @@ -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 Index: llvm/test/tools/llvm-objcopy/add-section.test =================================================================== --- llvm/test/tools/llvm-objcopy/add-section.test +++ llvm/test/tools/llvm-objcopy/add-section.test @@ -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 Index: llvm/test/tools/llvm-objcopy/basic-only-keep.test =================================================================== --- llvm/test/tools/llvm-objcopy/basic-only-keep.test +++ llvm/test/tools/llvm-objcopy/basic-only-keep.test @@ -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 Index: llvm/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test =================================================================== --- llvm/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test +++ llvm/test/tools/llvm-objcopy/binary-first-seg-offset-zero.test @@ -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 Index: llvm/test/tools/llvm-objcopy/keep-only-keep.test =================================================================== --- llvm/test/tools/llvm-objcopy/keep-only-keep.test +++ llvm/test/tools/llvm-objcopy/keep-only-keep.test @@ -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 Index: llvm/test/tools/llvm-objcopy/only-keep-remove-strtab.test =================================================================== --- llvm/test/tools/llvm-objcopy/only-keep-remove-strtab.test +++ llvm/test/tools/llvm-objcopy/only-keep-remove-strtab.test @@ -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 Index: llvm/test/tools/llvm-objcopy/parent-loop-check.test =================================================================== --- llvm/test/tools/llvm-objcopy/parent-loop-check.test +++ llvm/test/tools/llvm-objcopy/parent-loop-check.test @@ -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 Index: llvm/test/tools/llvm-objcopy/remove-section.test =================================================================== --- llvm/test/tools/llvm-objcopy/remove-section.test +++ llvm/test/tools/llvm-objcopy/remove-section.test @@ -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 Index: llvm/tools/llvm-objcopy/CMakeLists.txt =================================================================== --- llvm/tools/llvm-objcopy/CMakeLists.txt +++ llvm/tools/llvm-objcopy/CMakeLists.txt @@ -1,8 +1,15 @@ 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 Index: llvm/tools/llvm-objcopy/LLVMBuild.txt =================================================================== --- llvm/tools/llvm-objcopy/LLVMBuild.txt +++ llvm/tools/llvm-objcopy/LLVMBuild.txt @@ -18,4 +18,4 @@ type = Tool name = llvm-objcopy parent = Tools -required_libraries = Object Support MC +required_libraries = Object Option Support MC Index: llvm/tools/llvm-objcopy/Opts.td =================================================================== --- /dev/null +++ llvm/tools/llvm-objcopy/Opts.td @@ -0,0 +1,53 @@ +include "llvm/Option/OptParser.td" + +multiclass Eq { + def NAME: Separate<["--", "-"], name>; + def NAME # _eq: Joined<["--", "-"], name # "=">, Alias(NAME)>; +} + +def help : Flag<["-", "--"], "help">; +defm output_format : Eq<"output-format">, + HelpText<"Format of the output file">, + Values<"binary">; +def output_format_alias : JoinedOrSeparate<["-"], "O">, + Alias; +defm split_dwo : Eq<"split-dwo">, + MetaVarName<"dwo-file">, + HelpText<"Equivalent to extract-dwo on the input file to , then strip-dwo on the input file">; +defm add_gnu_debuglink : Eq<"add-gnu-debuglink">, + MetaVarName<"debug-file">, + HelpText<"adds a .gnu_debuglink for ">; +def add_gnu_debuglink_alias : Joined<["-", "--"], "add-gnu-debuglink=">, + Alias; +defm to_remove : Eq<"remove-section">, + MetaVarName<"section">, + HelpText<"Remove
">; +def to_remove_alias : JoinedOrSeparate<["-"], "R">, + Alias; +defm keep : Eq<"keep">, + MetaVarName<"section">, + HelpText<"Keep
">; +defm only_keep : Eq<"only-keep">, + MetaVarName<"section">, + HelpText<"Remove all but
">; +def only_keep_alias : JoinedOrSeparate<["-"], "j">, + Alias; +defm add_section : Eq<"add-section">, + MetaVarName<"section=file">, + HelpText<"Make a section named
with the contents of .">; +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">; Index: llvm/tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -7,6 +7,9 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" #include "llvm-objcopy.h" #include "Object.h" #include "llvm/ADT/STLExtras.h" @@ -42,6 +45,40 @@ 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 const 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; @@ -71,60 +108,33 @@ } // end namespace llvm -static cl::opt InputFilename(cl::Positional, cl::desc("")); -static cl::opt OutputFilename(cl::Positional, cl::desc("[ ]")); - -static cl::opt - OutputFormat("O", cl::desc("Set output format to one of the following:" - "\n\tbinary")); -static cl::list ToRemove("remove-section", - cl::desc("Remove
"), - cl::value_desc("section")); -static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"), - cl::aliasopt(ToRemove)); -static cl::opt StripAll( - "strip-all", - cl::desc( - "Removes non-allocated sections other than .gnu.warning* sections")); -static cl::opt - StripAllGNU("strip-all-gnu", - cl::desc("Removes symbol, relocation, and debug information")); -static cl::list Keep("keep", cl::desc("Keep
"), - cl::value_desc("section")); -static cl::list OnlyKeep("only-keep", - cl::desc("Remove all but
"), - cl::value_desc("section")); -static cl::alias OnlyKeepA("j", cl::desc("Alias for only-keep"), - cl::aliasopt(OnlyKeep)); -static cl::opt StripDebug("strip-debug", - cl::desc("Removes all debug information")); -static cl::opt StripSections("strip-sections", - cl::desc("Remove all section headers")); -static cl::opt - StripNonAlloc("strip-non-alloc", - cl::desc("Remove all non-allocated sections")); -static cl::opt - StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file")); -static cl::opt ExtractDWO( - "extract-dwo", - cl::desc("Remove all sections that are not DWARF .dwo sections from file")); -static cl::opt - SplitDWO("split-dwo", - cl::desc("Equivalent to extract-dwo on the input file to " - ", then strip-dwo on the input file"), - cl::value_desc("dwo-file")); -static cl::list AddSection( - "add-section", - cl::desc("Make a section named
with the contents of ."), - cl::value_desc("section=file")); -static cl::opt LocalizeHidden( - "localize-hidden", - cl::desc( - "Mark all symbols that have hidden or internal visibility as local")); -static cl::opt - AddGnuDebugLink("add-gnu-debuglink", - cl::desc("adds a .gnu_debuglink for "), - cl::value_desc("debug-file")); +struct Config { + StringRef InputFilename; + StringRef OutputFilename; + StringRef OutputFormat; + StringRef SplitDWO; + StringRef AddGnuDebugLink; + SmallVector ToRemove; + SmallVector Keep; + SmallVector OnlyKeep; + SmallVector AddSection; + bool StripAll; + bool StripAllGNU; + bool StripDebug; + bool StripSections; + bool StripNonAlloc; + bool StripDWO; + bool ExtractDWO; + bool LocalizeHidden; + + void Execute() const; + +private: + void HandleArgs(Object &Obj, const Reader &Reader, ElfType OutputElfType) const; + std::unique_ptr CreateWriter(Object &Obj, StringRef File, ElfType OutputElfType) const; + std::unique_ptr CreateReader(ElfType& OutputElfType) const; + void SplitDWOToFile(const Reader &Reader, StringRef File, ElfType OutputElfType) const; +}; using SectionPred = std::function; @@ -139,11 +149,9 @@ return !IsDWOSection(Sec); } -static ElfType OutputElfType; - -std::unique_ptr CreateWriter(Object &Obj, StringRef File) { +std::unique_ptr Config::CreateWriter(Object &Obj, StringRef File, ElfType OutputElfType) const { if (OutputFormat == "binary") { - return llvm::make_unique(OutputFilename, Obj); + return llvm::make_unique(File, Obj); } // Depending on the initial ELFT and OutputFormat we need a different Writer. switch (OutputElfType) { @@ -159,11 +167,11 @@ llvm_unreachable("Invalid output format"); } -void SplitDWOToFile(const Reader &Reader, StringRef File) { +void Config::SplitDWOToFile(const Reader &Reader, StringRef File, ElfType OutputElfType) const { auto DWOFile = Reader.create(); DWOFile->removeSections( [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); }); - auto Writer = CreateWriter(*DWOFile, File); + auto Writer = CreateWriter(*DWOFile, File, OutputElfType); Writer->finalize(); Writer->write(); } @@ -175,10 +183,10 @@ // 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(Object &Obj, const Reader &Reader) { +void Config::HandleArgs(Object &Obj, const Reader &Reader, ElfType OutputElfType) const { if (!SplitDWO.empty()) { - SplitDWOToFile(Reader, SplitDWO); + SplitDWOToFile(Reader, SplitDWO, OutputElfType); } // Localize: @@ -194,7 +202,7 @@ // Removes: if (!ToRemove.empty()) { - RemovePred = [&](const SectionBase &Sec) { + RemovePred = [this](const SectionBase &Sec) { return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) != std::end(ToRemove); }; @@ -263,7 +271,7 @@ // Explicit copies: if (!OnlyKeep.empty()) { - RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { + RemovePred = [this, RemovePred, &Obj](const SectionBase &Sec) { // Explicitly keep these sections regardless of previous removes. if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) != std::end(OnlyKeep)) @@ -285,7 +293,7 @@ } if (!Keep.empty()) { - RemovePred = [RemovePred](const SectionBase &Sec) { + RemovePred = [this, RemovePred](const SectionBase &Sec) { // Explicitly keep these sections regardless of previous removes. if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) != std::end(Keep)) @@ -318,7 +326,7 @@ } } -std::unique_ptr CreateReader() { +std::unique_ptr Config::CreateReader(ElfType &OutputElfType) const { // Right now we can only read ELF files so there's only one reader; auto Out = llvm::make_unique(StringRef(InputFilename)); // We need to set the default ElfType for output. @@ -326,24 +334,76 @@ return std::move(Out); } +void Config::Execute() const { + ElfType OutputElfType; + auto Reader = CreateReader(OutputElfType); + auto Obj = Reader->create(); + auto Writer = CreateWriter(*Obj, OutputFilename, OutputElfType); + HandleArgs(*Obj, *Reader, OutputElfType); + Writer->finalize(); + Writer->write(); +} + +void ObjcopyDriver(ArrayRef ArgsArr) { + ObjcopyOptTable T; + unsigned MAI, MAC; + auto InputArgs = T.ParseArgs(ArgsArr, MAI, MAC); + if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) { + T.PrintHelp(outs(), "llvm-objcopy [ ]", "objcopy tool"); + return; + } + Config config; + SmallVector 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.size() == 0) + error("No input file specified"); + + if (Positional.size() > 2) + error("Too many positional arguments"); + + config.InputFilename = Positional[0]; + if (Positional.size() == 1) + config.OutputFilename = Positional[0]; + else + config.OutputFilename = Positional[1]; + + config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_format); + config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); + config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); + for(auto Arg : InputArgs.filtered(OBJCOPY_to_remove)) + 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); + + config.Execute(); +} + 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; - } - 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(); + ObjcopyDriver(makeArrayRef(argv + 1, argc)); + + return 0; }