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 | |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 131 | static uint64_t parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) { |
| 132 | SectionFlag ParsedFlags = SectionFlag::SecNone; |
| 133 | for (StringRef Flag : SectionFlags) { |
| 134 | SectionFlag ParsedFlag = parseSectionRenameFlag(Flag); |
| 135 | if (ParsedFlag == SectionFlag::SecNone) |
| 136 | error("Unrecognized section flag '" + Flag + |
| 137 | "'. Flags supported for GNU compatibility: alloc, load, noload, " |
| 138 | "readonly, debug, code, data, rom, share, contents, merge, " |
| 139 | "strings."); |
| 140 | ParsedFlags |= ParsedFlag; |
| 141 | } |
| 142 | |
| 143 | uint64_t NewFlags = 0; |
| 144 | if (ParsedFlags & SectionFlag::SecAlloc) |
| 145 | NewFlags |= ELF::SHF_ALLOC; |
| 146 | if (!(ParsedFlags & SectionFlag::SecReadonly)) |
| 147 | NewFlags |= ELF::SHF_WRITE; |
| 148 | if (ParsedFlags & SectionFlag::SecCode) |
| 149 | NewFlags |= ELF::SHF_EXECINSTR; |
| 150 | if (ParsedFlags & SectionFlag::SecMerge) |
| 151 | NewFlags |= ELF::SHF_MERGE; |
| 152 | if (ParsedFlags & SectionFlag::SecStrings) |
| 153 | NewFlags |= ELF::SHF_STRINGS; |
| 154 | return NewFlags; |
| 155 | } |
| 156 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 157 | static SectionRename parseRenameSectionValue(StringRef FlagValue) { |
| 158 | if (!FlagValue.contains('=')) |
| 159 | error("Bad format for --rename-section: missing '='"); |
| 160 | |
| 161 | // Initial split: ".foo" = ".bar,f1,f2,..." |
| 162 | auto Old2New = FlagValue.split('='); |
| 163 | SectionRename SR; |
| 164 | SR.OriginalName = Old2New.first; |
| 165 | |
| 166 | // Flags split: ".bar" "f1" "f2" ... |
| 167 | SmallVector<StringRef, 6> NameAndFlags; |
| 168 | Old2New.second.split(NameAndFlags, ','); |
| 169 | SR.NewName = NameAndFlags[0]; |
| 170 | |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 171 | if (NameAndFlags.size() > 1) |
| 172 | SR.NewFlags = parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 173 | |
| 174 | return SR; |
| 175 | } |
| 176 | |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 177 | static SectionFlagsUpdate parseSetSectionFlagValue(StringRef FlagValue) { |
| 178 | if (!StringRef(FlagValue).contains('=')) |
| 179 | error("Bad format for --set-section-flags: missing '='"); |
| 180 | |
| 181 | // Initial split: ".foo" = "f1,f2,..." |
| 182 | auto Section2Flags = StringRef(FlagValue).split('='); |
| 183 | SectionFlagsUpdate SFU; |
| 184 | SFU.Name = Section2Flags.first; |
| 185 | |
| 186 | // Flags split: "f1" "f2" ... |
| 187 | SmallVector<StringRef, 6> SectionFlags; |
| 188 | Section2Flags.second.split(SectionFlags, ','); |
| 189 | SFU.NewFlags = parseSectionFlagSet(SectionFlags); |
| 190 | |
| 191 | return SFU; |
| 192 | } |
| 193 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 194 | static const StringMap<MachineInfo> ArchMap{ |
| 195 | // Name, {EMachine, 64bit, LittleEndian} |
| 196 | {"aarch64", {ELF::EM_AARCH64, true, true}}, |
| 197 | {"arm", {ELF::EM_ARM, false, true}}, |
| 198 | {"i386", {ELF::EM_386, false, true}}, |
| 199 | {"i386:x86-64", {ELF::EM_X86_64, true, true}}, |
| 200 | {"powerpc:common64", {ELF::EM_PPC64, true, true}}, |
| 201 | {"sparc", {ELF::EM_SPARC, false, true}}, |
| 202 | {"x86-64", {ELF::EM_X86_64, true, true}}, |
| 203 | }; |
| 204 | |
| 205 | static const MachineInfo &getMachineInfo(StringRef Arch) { |
| 206 | auto Iter = ArchMap.find(Arch); |
| 207 | if (Iter == std::end(ArchMap)) |
| 208 | error("Invalid architecture: '" + Arch + "'"); |
| 209 | return Iter->getValue(); |
| 210 | } |
| 211 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 212 | static const StringMap<MachineInfo> OutputFormatMap{ |
| 213 | // Name, {EMachine, 64bit, LittleEndian} |
| 214 | {"elf32-i386", {ELF::EM_386, false, true}}, |
| 215 | {"elf32-powerpcle", {ELF::EM_PPC, false, true}}, |
| 216 | {"elf32-x86-64", {ELF::EM_X86_64, false, true}}, |
| 217 | {"elf64-powerpcle", {ELF::EM_PPC64, true, true}}, |
| 218 | {"elf64-x86-64", {ELF::EM_X86_64, true, true}}, |
| 219 | }; |
| 220 | |
| 221 | static const MachineInfo &getOutputFormatMachineInfo(StringRef Format) { |
| 222 | auto Iter = OutputFormatMap.find(Format); |
| 223 | if (Iter == std::end(OutputFormatMap)) |
| 224 | error("Invalid output format: '" + Format + "'"); |
| 225 | return Iter->getValue(); |
| 226 | } |
| 227 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 228 | static void addGlobalSymbolsFromFile(std::vector<std::string> &Symbols, |
| 229 | StringRef Filename) { |
| 230 | SmallVector<StringRef, 16> Lines; |
| 231 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 232 | if (!BufOrErr) |
| 233 | reportError(Filename, BufOrErr.getError()); |
| 234 | |
| 235 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 236 | for (StringRef Line : Lines) { |
| 237 | // Ignore everything after '#', trim whitespace, and only add the symbol if |
| 238 | // it's not empty. |
| 239 | auto TrimmedLine = Line.split('#').first.trim(); |
| 240 | if (!TrimmedLine.empty()) |
| 241 | Symbols.push_back(TrimmedLine.str()); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 246 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 247 | // exit. |
| 248 | DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) { |
| 249 | ObjcopyOptTable T; |
| 250 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 251 | llvm::opt::InputArgList InputArgs = |
| 252 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 253 | |
| 254 | if (InputArgs.size() == 0) { |
| 255 | T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool"); |
| 256 | exit(1); |
| 257 | } |
| 258 | |
| 259 | if (InputArgs.hasArg(OBJCOPY_help)) { |
| 260 | T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool"); |
| 261 | exit(0); |
| 262 | } |
| 263 | |
| 264 | if (InputArgs.hasArg(OBJCOPY_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 265 | outs() << "llvm-objcopy, compatible with GNU objcopy\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 266 | cl::PrintVersionMessage(); |
| 267 | exit(0); |
| 268 | } |
| 269 | |
| 270 | SmallVector<const char *, 2> Positional; |
| 271 | |
| 272 | for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN)) |
| 273 | error("unknown argument '" + Arg->getAsString(InputArgs) + "'"); |
| 274 | |
| 275 | for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT)) |
| 276 | Positional.push_back(Arg->getValue()); |
| 277 | |
| 278 | if (Positional.empty()) |
| 279 | error("No input file specified"); |
| 280 | |
| 281 | if (Positional.size() > 2) |
| 282 | error("Too many positional arguments"); |
| 283 | |
| 284 | CopyConfig Config; |
| 285 | Config.InputFilename = Positional[0]; |
| 286 | Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 287 | if (InputArgs.hasArg(OBJCOPY_target) && |
| 288 | (InputArgs.hasArg(OBJCOPY_input_target) || |
| 289 | InputArgs.hasArg(OBJCOPY_output_target))) |
| 290 | error("--target cannot be used with --input-target or --output-target"); |
| 291 | |
| 292 | if (InputArgs.hasArg(OBJCOPY_target)) { |
| 293 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 294 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 295 | } else { |
| 296 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target); |
| 297 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); |
| 298 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 299 | if (Config.InputFormat == "binary") { |
| 300 | auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture); |
| 301 | if (BinaryArch.empty()) |
| 302 | error("Specified binary input without specifiying an architecture"); |
| 303 | Config.BinaryArch = getMachineInfo(BinaryArch); |
| 304 | } |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 305 | if (!Config.OutputFormat.empty() && Config.OutputFormat != "binary") |
| 306 | Config.OutputArch = getOutputFormatMachineInfo(Config.OutputFormat); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 307 | |
| 308 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections, |
| 309 | OBJCOPY_compress_debug_sections_eq)) { |
| 310 | Config.CompressionType = DebugCompressionType::Z; |
| 311 | |
| 312 | if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) { |
| 313 | Config.CompressionType = |
| 314 | StringSwitch<DebugCompressionType>( |
| 315 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)) |
| 316 | .Case("zlib-gnu", DebugCompressionType::GNU) |
| 317 | .Case("zlib", DebugCompressionType::Z) |
| 318 | .Default(DebugCompressionType::None); |
| 319 | if (Config.CompressionType == DebugCompressionType::None) |
| 320 | error("Invalid or unsupported --compress-debug-sections format: " + |
| 321 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)); |
| 322 | if (!zlib::isAvailable()) |
| 323 | error("LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress."); |
| 324 | } |
| 325 | } |
| 326 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 327 | Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 328 | Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir); |
| 329 | if (InputArgs.hasArg(OBJCOPY_build_id_link_input)) |
| 330 | Config.BuildIdLinkInput = |
| 331 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_input); |
| 332 | if (InputArgs.hasArg(OBJCOPY_build_id_link_output)) |
| 333 | Config.BuildIdLinkOutput = |
| 334 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_output); |
| 335 | Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 336 | Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols); |
| 337 | |
| 338 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { |
| 339 | if (!StringRef(Arg->getValue()).contains('=')) |
| 340 | error("Bad format for --redefine-sym"); |
| 341 | auto Old2New = StringRef(Arg->getValue()).split('='); |
| 342 | if (!Config.SymbolsToRename.insert(Old2New).second) |
| 343 | error("Multiple redefinition of symbol " + Old2New.first); |
| 344 | } |
| 345 | |
| 346 | for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) { |
| 347 | SectionRename SR = parseRenameSectionValue(StringRef(Arg->getValue())); |
| 348 | if (!Config.SectionsToRename.try_emplace(SR.OriginalName, SR).second) |
| 349 | error("Multiple renames of section " + SR.OriginalName); |
| 350 | } |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 351 | for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) { |
| 352 | SectionFlagsUpdate SFU = parseSetSectionFlagValue(Arg->getValue()); |
| 353 | if (!Config.SetSectionFlags.try_emplace(SFU.Name, SFU).second) |
| 354 | error("--set-section-flags set multiple times for section " + SFU.Name); |
| 355 | } |
| 356 | // Prohibit combinations of --set-section-flags when the section name is used |
| 357 | // by --rename-section, either as a source or a destination. |
| 358 | for (const auto &E : Config.SectionsToRename) { |
| 359 | const SectionRename &SR = E.second; |
| 360 | if (Config.SetSectionFlags.count(SR.OriginalName)) |
| 361 | error("--set-section-flags=" + SR.OriginalName + |
| 362 | " conflicts with --rename-section=" + SR.OriginalName + "=" + |
| 363 | SR.NewName); |
| 364 | if (Config.SetSectionFlags.count(SR.NewName)) |
| 365 | error("--set-section-flags=" + SR.NewName + |
| 366 | " conflicts with --rename-section=" + SR.OriginalName + "=" + |
| 367 | SR.NewName); |
| 368 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 369 | |
| 370 | for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section)) |
| 371 | Config.ToRemove.push_back(Arg->getValue()); |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 372 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section)) |
| 373 | Config.KeepSection.push_back(Arg->getValue()); |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 374 | for (auto Arg : InputArgs.filtered(OBJCOPY_only_section)) |
| 375 | Config.OnlySection.push_back(Arg->getValue()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 376 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) |
| 377 | Config.AddSection.push_back(Arg->getValue()); |
| 378 | for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section)) |
| 379 | Config.DumpSection.push_back(Arg->getValue()); |
| 380 | Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all); |
| 381 | Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu); |
| 382 | Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug); |
| 383 | Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); |
| 384 | Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); |
| 385 | Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); |
| 386 | Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); |
| 387 | Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); |
| 388 | Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); |
| 389 | Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame^] | 390 | if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals)) |
| 391 | Config.DiscardMode = |
| 392 | InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals) |
| 393 | ? DiscardType::All |
| 394 | : DiscardType::Locals; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 395 | Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); |
| 396 | Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); |
| 397 | Config.DecompressDebugSections = |
| 398 | InputArgs.hasArg(OBJCOPY_decompress_debug_sections); |
| 399 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) |
| 400 | Config.SymbolsToLocalize.push_back(Arg->getValue()); |
| 401 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol)) |
| 402 | Config.SymbolsToKeepGlobal.push_back(Arg->getValue()); |
| 403 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols)) |
| 404 | addGlobalSymbolsFromFile(Config.SymbolsToKeepGlobal, Arg->getValue()); |
| 405 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) |
| 406 | Config.SymbolsToGlobalize.push_back(Arg->getValue()); |
| 407 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) |
| 408 | Config.SymbolsToWeaken.push_back(Arg->getValue()); |
| 409 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) |
| 410 | Config.SymbolsToRemove.push_back(Arg->getValue()); |
| 411 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) |
| 412 | Config.SymbolsToKeep.push_back(Arg->getValue()); |
| 413 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 414 | Config.DeterministicArchives = InputArgs.hasFlag( |
| 415 | OBJCOPY_enable_deterministic_archives, |
| 416 | OBJCOPY_disable_deterministic_archives, /*default=*/true); |
| 417 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 418 | Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); |
| 419 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 420 | if (Config.DecompressDebugSections && |
| 421 | Config.CompressionType != DebugCompressionType::None) { |
| 422 | error("Cannot specify --compress-debug-sections at the same time as " |
| 423 | "--decompress-debug-sections at the same time"); |
| 424 | } |
| 425 | |
| 426 | if (Config.DecompressDebugSections && !zlib::isAvailable()) |
| 427 | error("LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress."); |
| 428 | |
Jordan Rupprecht | ab9f662 | 2018-10-23 20:54:51 +0000 | [diff] [blame] | 429 | DriverConfig DC; |
| 430 | DC.CopyConfigs.push_back(std::move(Config)); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 431 | return DC; |
| 432 | } |
| 433 | |
| 434 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 435 | // help flag is set then ParseStripOptions will print the help messege and |
| 436 | // exit. |
| 437 | DriverConfig parseStripOptions(ArrayRef<const char *> ArgsArr) { |
| 438 | StripOptTable T; |
| 439 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 440 | llvm::opt::InputArgList InputArgs = |
| 441 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 442 | |
| 443 | if (InputArgs.size() == 0) { |
| 444 | T.PrintHelp(errs(), "llvm-strip [options] file...", "strip tool"); |
| 445 | exit(1); |
| 446 | } |
| 447 | |
| 448 | if (InputArgs.hasArg(STRIP_help)) { |
| 449 | T.PrintHelp(outs(), "llvm-strip [options] file...", "strip tool"); |
| 450 | exit(0); |
| 451 | } |
| 452 | |
| 453 | if (InputArgs.hasArg(STRIP_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 454 | outs() << "llvm-strip, compatible with GNU strip\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 455 | cl::PrintVersionMessage(); |
| 456 | exit(0); |
| 457 | } |
| 458 | |
| 459 | SmallVector<const char *, 2> Positional; |
| 460 | for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) |
| 461 | error("unknown argument '" + Arg->getAsString(InputArgs) + "'"); |
| 462 | for (auto Arg : InputArgs.filtered(STRIP_INPUT)) |
| 463 | Positional.push_back(Arg->getValue()); |
| 464 | |
| 465 | if (Positional.empty()) |
| 466 | error("No input file specified"); |
| 467 | |
| 468 | if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output)) |
| 469 | error("Multiple input files cannot be used in combination with -o"); |
| 470 | |
| 471 | CopyConfig Config; |
| 472 | Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug); |
| 473 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame^] | 474 | if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals)) |
| 475 | Config.DiscardMode = |
| 476 | InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals) |
| 477 | ? DiscardType::All |
| 478 | : DiscardType::Locals; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 479 | Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); |
| 480 | Config.StripAll = InputArgs.hasArg(STRIP_strip_all); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 481 | Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 482 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame^] | 483 | if (!Config.StripDebug && !Config.StripUnneeded && |
| 484 | Config.DiscardMode == DiscardType::None && !Config.StripAllGNU) |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 485 | Config.StripAll = true; |
| 486 | |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 487 | for (auto Arg : InputArgs.filtered(STRIP_keep_section)) |
| 488 | Config.KeepSection.push_back(Arg->getValue()); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 489 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 490 | for (auto Arg : InputArgs.filtered(STRIP_remove_section)) |
| 491 | Config.ToRemove.push_back(Arg->getValue()); |
| 492 | |
| 493 | for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) |
| 494 | Config.SymbolsToKeep.push_back(Arg->getValue()); |
| 495 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 496 | Config.DeterministicArchives = |
| 497 | InputArgs.hasFlag(STRIP_enable_deterministic_archives, |
| 498 | STRIP_disable_deterministic_archives, /*default=*/true); |
| 499 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 500 | Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates); |
| 501 | |
| 502 | DriverConfig DC; |
| 503 | if (Positional.size() == 1) { |
| 504 | Config.InputFilename = Positional[0]; |
| 505 | Config.OutputFilename = |
| 506 | InputArgs.getLastArgValue(STRIP_output, Positional[0]); |
| 507 | DC.CopyConfigs.push_back(std::move(Config)); |
| 508 | } else { |
| 509 | for (const char *Filename : Positional) { |
| 510 | Config.InputFilename = Filename; |
| 511 | Config.OutputFilename = Filename; |
| 512 | DC.CopyConfigs.push_back(Config); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return DC; |
| 517 | } |
| 518 | |
| 519 | } // namespace objcopy |
| 520 | } // namespace llvm |