diff --git a/llvm/tools/llvm-objcopy/CopyConfig.h b/llvm/tools/llvm-objcopy/CopyConfig.h --- a/llvm/tools/llvm-objcopy/CopyConfig.h +++ b/llvm/tools/llvm-objcopy/CopyConfig.h @@ -9,7 +9,6 @@ #ifndef LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H #define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H -#include "ELF/ELFConfig.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/BitmaskEnum.h" #include "llvm/ADT/DenseSet.h" @@ -147,9 +146,6 @@ // Configuration for copying/stripping a single file. struct CopyConfig { - // Format-specific options to be initialized lazily when needed. - Optional ELF; - // Main input/output options StringRef InputFilename; FileFormat InputFormat = FileFormat::Unspecified; @@ -233,18 +229,6 @@ bool RemoveAllRpaths = false; DebugCompressionType CompressionType = DebugCompressionType::None; - - // parseELFConfig performs ELF-specific command-line parsing. Fills `ELF` on - // success or returns an Error otherwise. - Error parseELFConfig() { - if (!ELF) { - Expected ELFConfig = elf::parseConfig(*this); - if (!ELFConfig) - return ELFConfig.takeError(); - ELF = *ELFConfig; - } - return Error::success(); - } }; // Configuration for the overall invocation of this tool. When invoked as diff --git a/llvm/tools/llvm-objcopy/ELF/ELFConfig.h b/llvm/tools/llvm-objcopy/ELF/ELFConfig.h --- a/llvm/tools/llvm-objcopy/ELF/ELFConfig.h +++ b/llvm/tools/llvm-objcopy/ELF/ELFConfig.h @@ -35,10 +35,11 @@ std::vector SymbolsToAdd; }; +/// Parse ELF specific options. Expected parseConfig(const CopyConfig &Config); } // namespace elf } // namespace objcopy } // namespace llvm -#endif +#endif // LLVM_TOOLS_OBJCOPY_ELFCONFIG_H diff --git a/llvm/tools/llvm-objcopy/ELF/ELFConfig.cpp b/llvm/tools/llvm-objcopy/ELF/ELFConfig.cpp --- a/llvm/tools/llvm-objcopy/ELF/ELFConfig.cpp +++ b/llvm/tools/llvm-objcopy/ELF/ELFConfig.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "ELFConfig.h" #include "CopyConfig.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringSwitch.h" diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp --- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -8,6 +8,7 @@ #include "ELFObjcopy.h" #include "CopyConfig.h" +#include "ELFConfig.h" #include "Object.h" #include "llvm-objcopy.h" #include "llvm/ADT/BitmaskEnum.h" @@ -506,7 +507,8 @@ // 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. -static Error handleArgs(const CopyConfig &Config, Object &Obj) { +static Error handleArgs(const CopyConfig &Config, + const ELFCopyConfig &ELFConfig, Object &Obj) { if (Config.StripSwiftSymbols || Config.KeepUndefined) return createStringError(llvm::errc::invalid_argument, "option not supported by llvm-objcopy for ELF"); @@ -635,11 +637,11 @@ // If the symbol table was previously removed, we need to create a new one // before adding new symbols. - if (!Obj.SymbolTable && !Config.ELF->SymbolsToAdd.empty()) + if (!Obj.SymbolTable && !ELFConfig.SymbolsToAdd.empty()) if (Error E = Obj.addNewSymbolTable()) return E; - for (const NewSymbolInfo &SI : Config.ELF->SymbolsToAdd) { + for (const NewSymbolInfo &SI : ELFConfig.SymbolsToAdd) { SectionBase *Sec = Obj.findSection(SI.SectionName); uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value; Obj.SymbolTable->addSymbol( @@ -674,6 +676,10 @@ Error executeObjcopyOnIHex(const CopyConfig &Config, MemoryBuffer &In, raw_ostream &Out) { + Expected ELFConfig = parseConfig(Config); + if (!ELFConfig) + return ELFConfig.takeError(); + IHexReader Reader(&In); Expected> Obj = Reader.create(true); if (!Obj) @@ -681,16 +687,20 @@ const ElfType OutputElfType = getOutputElfType(Config.OutputArch.getValueOr(MachineInfo())); - if (Error E = handleArgs(Config, **Obj)) + + if (Error E = handleArgs(Config, *ELFConfig, **Obj)) return E; return writeOutput(Config, **Obj, Out, OutputElfType); } Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, raw_ostream &Out) { - uint8_t NewSymbolVisibility = - Config.ELF->NewSymbolVisibility.getValueOr((uint8_t)ELF::STV_DEFAULT); - BinaryReader Reader(&In, NewSymbolVisibility); + Expected ELFConfig = parseConfig(Config); + if (!ELFConfig) + return ELFConfig.takeError(); + + BinaryReader Reader(&In, ELFConfig->NewSymbolVisibility.getValueOr( + (uint8_t)ELF::STV_DEFAULT)); Expected> Obj = Reader.create(true); if (!Obj) return Obj.takeError(); @@ -699,13 +709,17 @@ // (-B). const ElfType OutputElfType = getOutputElfType(Config.OutputArch.getValueOr(MachineInfo())); - if (Error E = handleArgs(Config, **Obj)) + if (Error E = handleArgs(Config, *ELFConfig, **Obj)) return E; return writeOutput(Config, **Obj, Out, OutputElfType); } Error executeObjcopyOnBinary(const CopyConfig &Config, object::ELFObjectFileBase &In, raw_ostream &Out) { + Expected ELFConfig = parseConfig(Config); + if (!ELFConfig) + return ELFConfig.takeError(); + ELFReader Reader(&In, Config.ExtractPartition); Expected> Obj = Reader.create(!Config.SymbolsToAdd.empty()); @@ -716,7 +730,7 @@ Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue()) : getOutputElfType(In); - if (Error E = handleArgs(Config, **Obj)) + if (Error E = handleArgs(Config, *ELFConfig, **Obj)) return createFileError(Config.InputFilename, std::move(E)); if (Error E = writeOutput(Config, **Obj, Out, OutputElfType)) diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -136,8 +136,6 @@ static Error executeObjcopyOnIHex(CopyConfig &Config, MemoryBuffer &In, raw_ostream &Out) { // TODO: support output formats other than ELF. - if (Error E = Config.parseELFConfig()) - return E; return elf::executeObjcopyOnIHex(Config, In, Out); } @@ -153,8 +151,6 @@ case FileFormat::Binary: case FileFormat::IHex: case FileFormat::Unspecified: - if (Error E = Config.parseELFConfig()) - return E; return elf::executeObjcopyOnRawBinary(Config, In, Out); } @@ -166,8 +162,6 @@ static Error executeObjcopyOnBinary(CopyConfig &Config, object::Binary &In, raw_ostream &Out) { if (auto *ELFBinary = dyn_cast(&In)) { - if (Error E = Config.parseELFConfig()) - return E; return elf::executeObjcopyOnBinary(Config, *ELFBinary, Out); } else if (auto *COFFBinary = dyn_cast(&In)) return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out);