Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1 | //===- CopyConfig.cpp -----------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "CopyConfig.h" |
| 10 | #include "llvm-objcopy.h" |
| 11 | |
| 12 | #include "llvm/ADT/BitmaskEnum.h" |
| 13 | #include "llvm/ADT/Optional.h" |
| 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include "llvm/Object/ELFTypes.h" |
| 17 | #include "llvm/Option/Arg.h" |
| 18 | #include "llvm/Option/ArgList.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/Compression.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | |
| 25 | namespace llvm { |
| 26 | namespace objcopy { |
| 27 | |
| 28 | namespace { |
| 29 | enum ObjcopyID { |
| 30 | OBJCOPY_INVALID = 0, // This is not an option ID. |
| 31 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 32 | HELPTEXT, METAVAR, VALUES) \ |
| 33 | OBJCOPY_##ID, |
| 34 | #include "ObjcopyOpts.inc" |
| 35 | #undef OPTION |
| 36 | }; |
| 37 | |
| 38 | #define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE; |
| 39 | #include "ObjcopyOpts.inc" |
| 40 | #undef PREFIX |
| 41 | |
| 42 | static const opt::OptTable::Info ObjcopyInfoTable[] = { |
| 43 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 44 | HELPTEXT, METAVAR, VALUES) \ |
| 45 | {OBJCOPY_##PREFIX, \ |
| 46 | NAME, \ |
| 47 | HELPTEXT, \ |
| 48 | METAVAR, \ |
| 49 | OBJCOPY_##ID, \ |
| 50 | opt::Option::KIND##Class, \ |
| 51 | PARAM, \ |
| 52 | FLAGS, \ |
| 53 | OBJCOPY_##GROUP, \ |
| 54 | OBJCOPY_##ALIAS, \ |
| 55 | ALIASARGS, \ |
| 56 | VALUES}, |
| 57 | #include "ObjcopyOpts.inc" |
| 58 | #undef OPTION |
| 59 | }; |
| 60 | |
| 61 | class ObjcopyOptTable : public opt::OptTable { |
| 62 | public: |
Jordan Rupprecht | aaeaa0a | 2018-10-23 18:46:33 +0000 | [diff] [blame] | 63 | ObjcopyOptTable() : OptTable(ObjcopyInfoTable) {} |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | enum StripID { |
| 67 | STRIP_INVALID = 0, // This is not an option ID. |
| 68 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 69 | HELPTEXT, METAVAR, VALUES) \ |
| 70 | STRIP_##ID, |
| 71 | #include "StripOpts.inc" |
| 72 | #undef OPTION |
| 73 | }; |
| 74 | |
| 75 | #define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE; |
| 76 | #include "StripOpts.inc" |
| 77 | #undef PREFIX |
| 78 | |
| 79 | static const opt::OptTable::Info StripInfoTable[] = { |
| 80 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 81 | HELPTEXT, METAVAR, VALUES) \ |
| 82 | {STRIP_##PREFIX, NAME, HELPTEXT, \ |
| 83 | METAVAR, STRIP_##ID, opt::Option::KIND##Class, \ |
| 84 | PARAM, FLAGS, STRIP_##GROUP, \ |
| 85 | STRIP_##ALIAS, ALIASARGS, VALUES}, |
| 86 | #include "StripOpts.inc" |
| 87 | #undef OPTION |
| 88 | }; |
| 89 | |
| 90 | class StripOptTable : public opt::OptTable { |
| 91 | public: |
Jordan Rupprecht | aaeaa0a | 2018-10-23 18:46:33 +0000 | [diff] [blame] | 92 | StripOptTable() : OptTable(StripInfoTable) {} |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | enum SectionFlag { |
| 96 | SecNone = 0, |
| 97 | SecAlloc = 1 << 0, |
| 98 | SecLoad = 1 << 1, |
| 99 | SecNoload = 1 << 2, |
| 100 | SecReadonly = 1 << 3, |
| 101 | SecDebug = 1 << 4, |
| 102 | SecCode = 1 << 5, |
| 103 | SecData = 1 << 6, |
| 104 | SecRom = 1 << 7, |
| 105 | SecMerge = 1 << 8, |
| 106 | SecStrings = 1 << 9, |
| 107 | SecContents = 1 << 10, |
| 108 | SecShare = 1 << 11, |
| 109 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare) |
| 110 | }; |
| 111 | |
| 112 | } // namespace |
| 113 | |
| 114 | static SectionFlag parseSectionRenameFlag(StringRef SectionName) { |
| 115 | return llvm::StringSwitch<SectionFlag>(SectionName) |
| 116 | .Case("alloc", SectionFlag::SecAlloc) |
| 117 | .Case("load", SectionFlag::SecLoad) |
| 118 | .Case("noload", SectionFlag::SecNoload) |
| 119 | .Case("readonly", SectionFlag::SecReadonly) |
| 120 | .Case("debug", SectionFlag::SecDebug) |
| 121 | .Case("code", SectionFlag::SecCode) |
| 122 | .Case("data", SectionFlag::SecData) |
| 123 | .Case("rom", SectionFlag::SecRom) |
| 124 | .Case("merge", SectionFlag::SecMerge) |
| 125 | .Case("strings", SectionFlag::SecStrings) |
| 126 | .Case("contents", SectionFlag::SecContents) |
| 127 | .Case("share", SectionFlag::SecShare) |
| 128 | .Default(SectionFlag::SecNone); |
| 129 | } |
| 130 | |
| 131 | static SectionRename parseRenameSectionValue(StringRef FlagValue) { |
| 132 | if (!FlagValue.contains('=')) |
| 133 | error("Bad format for --rename-section: missing '='"); |
| 134 | |
| 135 | // Initial split: ".foo" = ".bar,f1,f2,..." |
| 136 | auto Old2New = FlagValue.split('='); |
| 137 | SectionRename SR; |
| 138 | SR.OriginalName = Old2New.first; |
| 139 | |
| 140 | // Flags split: ".bar" "f1" "f2" ... |
| 141 | SmallVector<StringRef, 6> NameAndFlags; |
| 142 | Old2New.second.split(NameAndFlags, ','); |
| 143 | SR.NewName = NameAndFlags[0]; |
| 144 | |
| 145 | if (NameAndFlags.size() > 1) { |
| 146 | SectionFlag Flags = SectionFlag::SecNone; |
| 147 | for (size_t I = 1, Size = NameAndFlags.size(); I < Size; ++I) { |
| 148 | SectionFlag Flag = parseSectionRenameFlag(NameAndFlags[I]); |
| 149 | if (Flag == SectionFlag::SecNone) |
| 150 | error("Unrecognized section flag '" + NameAndFlags[I] + |
| 151 | "'. Flags supported for GNU compatibility: alloc, load, noload, " |
| 152 | "readonly, debug, code, data, rom, share, contents, merge, " |
| 153 | "strings."); |
| 154 | Flags |= Flag; |
| 155 | } |
| 156 | |
| 157 | SR.NewFlags = 0; |
| 158 | if (Flags & SectionFlag::SecAlloc) |
| 159 | *SR.NewFlags |= ELF::SHF_ALLOC; |
| 160 | if (!(Flags & SectionFlag::SecReadonly)) |
| 161 | *SR.NewFlags |= ELF::SHF_WRITE; |
| 162 | if (Flags & SectionFlag::SecCode) |
| 163 | *SR.NewFlags |= ELF::SHF_EXECINSTR; |
| 164 | if (Flags & SectionFlag::SecMerge) |
| 165 | *SR.NewFlags |= ELF::SHF_MERGE; |
| 166 | if (Flags & SectionFlag::SecStrings) |
| 167 | *SR.NewFlags |= ELF::SHF_STRINGS; |
| 168 | } |
| 169 | |
| 170 | return SR; |
| 171 | } |
| 172 | |
| 173 | static const StringMap<MachineInfo> ArchMap{ |
| 174 | // Name, {EMachine, 64bit, LittleEndian} |
| 175 | {"aarch64", {ELF::EM_AARCH64, true, true}}, |
| 176 | {"arm", {ELF::EM_ARM, false, true}}, |
| 177 | {"i386", {ELF::EM_386, false, true}}, |
| 178 | {"i386:x86-64", {ELF::EM_X86_64, true, true}}, |
| 179 | {"powerpc:common64", {ELF::EM_PPC64, true, true}}, |
| 180 | {"sparc", {ELF::EM_SPARC, false, true}}, |
| 181 | {"x86-64", {ELF::EM_X86_64, true, true}}, |
| 182 | }; |
| 183 | |
| 184 | static const MachineInfo &getMachineInfo(StringRef Arch) { |
| 185 | auto Iter = ArchMap.find(Arch); |
| 186 | if (Iter == std::end(ArchMap)) |
| 187 | error("Invalid architecture: '" + Arch + "'"); |
| 188 | return Iter->getValue(); |
| 189 | } |
| 190 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 191 | static const StringMap<MachineInfo> OutputFormatMap{ |
| 192 | // Name, {EMachine, 64bit, LittleEndian} |
| 193 | {"elf32-i386", {ELF::EM_386, false, true}}, |
| 194 | {"elf32-powerpcle", {ELF::EM_PPC, false, true}}, |
| 195 | {"elf32-x86-64", {ELF::EM_X86_64, false, true}}, |
| 196 | {"elf64-powerpcle", {ELF::EM_PPC64, true, true}}, |
| 197 | {"elf64-x86-64", {ELF::EM_X86_64, true, true}}, |
| 198 | }; |
| 199 | |
| 200 | static const MachineInfo &getOutputFormatMachineInfo(StringRef Format) { |
| 201 | auto Iter = OutputFormatMap.find(Format); |
| 202 | if (Iter == std::end(OutputFormatMap)) |
| 203 | error("Invalid output format: '" + Format + "'"); |
| 204 | return Iter->getValue(); |
| 205 | } |
| 206 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 207 | static void addGlobalSymbolsFromFile(std::vector<std::string> &Symbols, |
| 208 | StringRef Filename) { |
| 209 | SmallVector<StringRef, 16> Lines; |
| 210 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 211 | if (!BufOrErr) |
| 212 | reportError(Filename, BufOrErr.getError()); |
| 213 | |
| 214 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 215 | for (StringRef Line : Lines) { |
| 216 | // Ignore everything after '#', trim whitespace, and only add the symbol if |
| 217 | // it's not empty. |
| 218 | auto TrimmedLine = Line.split('#').first.trim(); |
| 219 | if (!TrimmedLine.empty()) |
| 220 | Symbols.push_back(TrimmedLine.str()); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 225 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 226 | // exit. |
| 227 | DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) { |
| 228 | ObjcopyOptTable T; |
| 229 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 230 | llvm::opt::InputArgList InputArgs = |
| 231 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 232 | |
| 233 | if (InputArgs.size() == 0) { |
| 234 | T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool"); |
| 235 | exit(1); |
| 236 | } |
| 237 | |
| 238 | if (InputArgs.hasArg(OBJCOPY_help)) { |
| 239 | T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool"); |
| 240 | exit(0); |
| 241 | } |
| 242 | |
| 243 | if (InputArgs.hasArg(OBJCOPY_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 244 | outs() << "llvm-objcopy, compatible with GNU objcopy\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 245 | cl::PrintVersionMessage(); |
| 246 | exit(0); |
| 247 | } |
| 248 | |
| 249 | SmallVector<const char *, 2> Positional; |
| 250 | |
| 251 | for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN)) |
| 252 | error("unknown argument '" + Arg->getAsString(InputArgs) + "'"); |
| 253 | |
| 254 | for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT)) |
| 255 | Positional.push_back(Arg->getValue()); |
| 256 | |
| 257 | if (Positional.empty()) |
| 258 | error("No input file specified"); |
| 259 | |
| 260 | if (Positional.size() > 2) |
| 261 | error("Too many positional arguments"); |
| 262 | |
| 263 | CopyConfig Config; |
| 264 | Config.InputFilename = Positional[0]; |
| 265 | Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 266 | if (InputArgs.hasArg(OBJCOPY_target) && |
| 267 | (InputArgs.hasArg(OBJCOPY_input_target) || |
| 268 | InputArgs.hasArg(OBJCOPY_output_target))) |
| 269 | error("--target cannot be used with --input-target or --output-target"); |
| 270 | |
| 271 | if (InputArgs.hasArg(OBJCOPY_target)) { |
| 272 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 273 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 274 | } else { |
| 275 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target); |
| 276 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); |
| 277 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 278 | if (Config.InputFormat == "binary") { |
| 279 | auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture); |
| 280 | if (BinaryArch.empty()) |
| 281 | error("Specified binary input without specifiying an architecture"); |
| 282 | Config.BinaryArch = getMachineInfo(BinaryArch); |
| 283 | } |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 284 | if (!Config.OutputFormat.empty() && Config.OutputFormat != "binary") |
| 285 | Config.OutputArch = getOutputFormatMachineInfo(Config.OutputFormat); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 286 | |
| 287 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections, |
| 288 | OBJCOPY_compress_debug_sections_eq)) { |
| 289 | Config.CompressionType = DebugCompressionType::Z; |
| 290 | |
| 291 | if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) { |
| 292 | Config.CompressionType = |
| 293 | StringSwitch<DebugCompressionType>( |
| 294 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)) |
| 295 | .Case("zlib-gnu", DebugCompressionType::GNU) |
| 296 | .Case("zlib", DebugCompressionType::Z) |
| 297 | .Default(DebugCompressionType::None); |
| 298 | if (Config.CompressionType == DebugCompressionType::None) |
| 299 | error("Invalid or unsupported --compress-debug-sections format: " + |
| 300 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)); |
| 301 | if (!zlib::isAvailable()) |
| 302 | error("LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress."); |
| 303 | } |
| 304 | } |
| 305 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 306 | Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 307 | Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir); |
| 308 | if (InputArgs.hasArg(OBJCOPY_build_id_link_input)) |
| 309 | Config.BuildIdLinkInput = |
| 310 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_input); |
| 311 | if (InputArgs.hasArg(OBJCOPY_build_id_link_output)) |
| 312 | Config.BuildIdLinkOutput = |
| 313 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_output); |
| 314 | Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 315 | Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols); |
| 316 | |
| 317 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { |
| 318 | if (!StringRef(Arg->getValue()).contains('=')) |
| 319 | error("Bad format for --redefine-sym"); |
| 320 | auto Old2New = StringRef(Arg->getValue()).split('='); |
| 321 | if (!Config.SymbolsToRename.insert(Old2New).second) |
| 322 | error("Multiple redefinition of symbol " + Old2New.first); |
| 323 | } |
| 324 | |
| 325 | for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) { |
| 326 | SectionRename SR = parseRenameSectionValue(StringRef(Arg->getValue())); |
| 327 | if (!Config.SectionsToRename.try_emplace(SR.OriginalName, SR).second) |
| 328 | error("Multiple renames of section " + SR.OriginalName); |
| 329 | } |
| 330 | |
| 331 | for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section)) |
| 332 | Config.ToRemove.push_back(Arg->getValue()); |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 333 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section)) |
| 334 | Config.KeepSection.push_back(Arg->getValue()); |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 335 | for (auto Arg : InputArgs.filtered(OBJCOPY_only_section)) |
| 336 | Config.OnlySection.push_back(Arg->getValue()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 337 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) |
| 338 | Config.AddSection.push_back(Arg->getValue()); |
| 339 | for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section)) |
| 340 | Config.DumpSection.push_back(Arg->getValue()); |
| 341 | Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all); |
| 342 | Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu); |
| 343 | Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug); |
| 344 | Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); |
| 345 | Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); |
| 346 | Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); |
| 347 | Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); |
| 348 | Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); |
| 349 | Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); |
| 350 | Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); |
| 351 | Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all); |
| 352 | Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); |
| 353 | Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); |
| 354 | Config.DecompressDebugSections = |
| 355 | InputArgs.hasArg(OBJCOPY_decompress_debug_sections); |
| 356 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) |
| 357 | Config.SymbolsToLocalize.push_back(Arg->getValue()); |
| 358 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol)) |
| 359 | Config.SymbolsToKeepGlobal.push_back(Arg->getValue()); |
| 360 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols)) |
| 361 | addGlobalSymbolsFromFile(Config.SymbolsToKeepGlobal, Arg->getValue()); |
| 362 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) |
| 363 | Config.SymbolsToGlobalize.push_back(Arg->getValue()); |
| 364 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) |
| 365 | Config.SymbolsToWeaken.push_back(Arg->getValue()); |
| 366 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) |
| 367 | Config.SymbolsToRemove.push_back(Arg->getValue()); |
| 368 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) |
| 369 | Config.SymbolsToKeep.push_back(Arg->getValue()); |
| 370 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 371 | Config.DeterministicArchives = InputArgs.hasFlag( |
| 372 | OBJCOPY_enable_deterministic_archives, |
| 373 | OBJCOPY_disable_deterministic_archives, /*default=*/true); |
| 374 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 375 | Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); |
| 376 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 377 | if (Config.DecompressDebugSections && |
| 378 | Config.CompressionType != DebugCompressionType::None) { |
| 379 | error("Cannot specify --compress-debug-sections at the same time as " |
| 380 | "--decompress-debug-sections at the same time"); |
| 381 | } |
| 382 | |
| 383 | if (Config.DecompressDebugSections && !zlib::isAvailable()) |
| 384 | error("LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress."); |
| 385 | |
Jordan Rupprecht | ab9f662 | 2018-10-23 20:54:51 +0000 | [diff] [blame] | 386 | DriverConfig DC; |
| 387 | DC.CopyConfigs.push_back(std::move(Config)); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 388 | return DC; |
| 389 | } |
| 390 | |
| 391 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 392 | // help flag is set then ParseStripOptions will print the help messege and |
| 393 | // exit. |
| 394 | DriverConfig parseStripOptions(ArrayRef<const char *> ArgsArr) { |
| 395 | StripOptTable T; |
| 396 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 397 | llvm::opt::InputArgList InputArgs = |
| 398 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 399 | |
| 400 | if (InputArgs.size() == 0) { |
| 401 | T.PrintHelp(errs(), "llvm-strip [options] file...", "strip tool"); |
| 402 | exit(1); |
| 403 | } |
| 404 | |
| 405 | if (InputArgs.hasArg(STRIP_help)) { |
| 406 | T.PrintHelp(outs(), "llvm-strip [options] file...", "strip tool"); |
| 407 | exit(0); |
| 408 | } |
| 409 | |
| 410 | if (InputArgs.hasArg(STRIP_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 411 | outs() << "llvm-strip, compatible with GNU strip\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 412 | cl::PrintVersionMessage(); |
| 413 | exit(0); |
| 414 | } |
| 415 | |
| 416 | SmallVector<const char *, 2> Positional; |
| 417 | for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) |
| 418 | error("unknown argument '" + Arg->getAsString(InputArgs) + "'"); |
| 419 | for (auto Arg : InputArgs.filtered(STRIP_INPUT)) |
| 420 | Positional.push_back(Arg->getValue()); |
| 421 | |
| 422 | if (Positional.empty()) |
| 423 | error("No input file specified"); |
| 424 | |
| 425 | if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output)) |
| 426 | error("Multiple input files cannot be used in combination with -o"); |
| 427 | |
| 428 | CopyConfig Config; |
| 429 | Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug); |
| 430 | |
| 431 | Config.DiscardAll = InputArgs.hasArg(STRIP_discard_all); |
| 432 | Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); |
| 433 | Config.StripAll = InputArgs.hasArg(STRIP_strip_all); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 434 | Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 435 | |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 436 | if (!Config.StripDebug && !Config.StripUnneeded && !Config.DiscardAll && |
| 437 | !Config.StripAllGNU) |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 438 | Config.StripAll = true; |
| 439 | |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 440 | for (auto Arg : InputArgs.filtered(STRIP_keep_section)) |
| 441 | Config.KeepSection.push_back(Arg->getValue()); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 442 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 443 | for (auto Arg : InputArgs.filtered(STRIP_remove_section)) |
| 444 | Config.ToRemove.push_back(Arg->getValue()); |
| 445 | |
| 446 | for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) |
| 447 | Config.SymbolsToKeep.push_back(Arg->getValue()); |
| 448 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 449 | Config.DeterministicArchives = |
| 450 | InputArgs.hasFlag(STRIP_enable_deterministic_archives, |
| 451 | STRIP_disable_deterministic_archives, /*default=*/true); |
| 452 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 453 | Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates); |
| 454 | |
| 455 | DriverConfig DC; |
| 456 | if (Positional.size() == 1) { |
| 457 | Config.InputFilename = Positional[0]; |
| 458 | Config.OutputFilename = |
| 459 | InputArgs.getLastArgValue(STRIP_output, Positional[0]); |
| 460 | DC.CopyConfigs.push_back(std::move(Config)); |
| 461 | } else { |
| 462 | for (const char *Filename : Positional) { |
| 463 | Config.InputFilename = Filename; |
| 464 | Config.OutputFilename = Filename; |
| 465 | DC.CopyConfigs.push_back(Config); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return DC; |
| 470 | } |
| 471 | |
| 472 | } // namespace objcopy |
| 473 | } // namespace llvm |