| 1 | //===- ObjcopyOptions.cpp -------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ObjcopyOptions.h" |
| 10 | #include "llvm/ADT/SmallVector.h" |
| 11 | #include "llvm/ADT/StringExtras.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/ADT/StringSwitch.h" |
| 14 | #include "llvm/BinaryFormat/COFF.h" |
| 15 | #include "llvm/ObjCopy/CommonConfig.h" |
| 16 | #include "llvm/ObjCopy/ConfigManager.h" |
| 17 | #include "llvm/ObjCopy/MachO/MachOConfig.h" |
| 18 | #include "llvm/Object/Binary.h" |
| 19 | #include "llvm/Object/OffloadBundle.h" |
| 20 | #include "llvm/Option/Arg.h" |
| 21 | #include "llvm/Option/ArgList.h" |
| 22 | #include "llvm/Support/CRC.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Compression.h" |
| 25 | #include "llvm/Support/Errc.h" |
| 26 | #include "llvm/Support/Error.h" |
| 27 | #include "llvm/Support/MemoryBuffer.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | using namespace llvm::objcopy; |
| 31 | using namespace llvm::object; |
| 32 | using namespace llvm::opt; |
| 33 | |
| 34 | namespace { |
| 35 | enum ObjcopyID { |
| 36 | OBJCOPY_INVALID = 0, // This is not an option ID. |
| 37 | #define OPTION(...) LLVM_MAKE_OPT_ID_WITH_ID_PREFIX(OBJCOPY_, __VA_ARGS__), |
| 38 | #include "ObjcopyOpts.inc" |
| 39 | #undef OPTION |
| 40 | }; |
| 41 | |
| 42 | namespace objcopy_opt { |
| 43 | #define OPTTABLE_STR_TABLE_CODE |
| 44 | #include "ObjcopyOpts.inc" |
| 45 | #undef OPTTABLE_STR_TABLE_CODE |
| 46 | |
| 47 | #define OPTTABLE_PREFIXES_TABLE_CODE |
| 48 | #include "ObjcopyOpts.inc" |
| 49 | #undef OPTTABLE_PREFIXES_TABLE_CODE |
| 50 | |
| 51 | static constexpr opt::OptTable::Info ObjcopyInfoTable[] = { |
| 52 | #define OPTION(...) \ |
| 53 | LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(OBJCOPY_, __VA_ARGS__), |
| 54 | #include "ObjcopyOpts.inc" |
| 55 | #undef OPTION |
| 56 | }; |
| 57 | } // namespace objcopy_opt |
| 58 | |
| 59 | class ObjcopyOptTable : public opt::GenericOptTable { |
| 60 | public: |
| 61 | ObjcopyOptTable() |
| 62 | : opt::GenericOptTable(objcopy_opt::OptionStrTable, |
| 63 | objcopy_opt::OptionPrefixesTable, |
| 64 | objcopy_opt::ObjcopyInfoTable) { |
| 65 | setGroupedShortOptions(true); |
| 66 | setDashDashParsing(true); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | enum InstallNameToolID { |
| 71 | INSTALL_NAME_TOOL_INVALID = 0, // This is not an option ID. |
| 72 | #define OPTION(...) \ |
| 73 | LLVM_MAKE_OPT_ID_WITH_ID_PREFIX(INSTALL_NAME_TOOL_, __VA_ARGS__), |
| 74 | #include "InstallNameToolOpts.inc" |
| 75 | #undef OPTION |
| 76 | }; |
| 77 | |
| 78 | namespace install_name_tool { |
| 79 | #define OPTTABLE_STR_TABLE_CODE |
| 80 | #include "InstallNameToolOpts.inc" |
| 81 | #undef OPTTABLE_STR_TABLE_CODE |
| 82 | |
| 83 | #define OPTTABLE_PREFIXES_TABLE_CODE |
| 84 | #include "InstallNameToolOpts.inc" |
| 85 | #undef OPTTABLE_PREFIXES_TABLE_CODE |
| 86 | |
| 87 | static constexpr opt::OptTable::Info InstallNameToolInfoTable[] = { |
| 88 | #define OPTION(...) \ |
| 89 | LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(INSTALL_NAME_TOOL_, __VA_ARGS__), |
| 90 | #include "InstallNameToolOpts.inc" |
| 91 | #undef OPTION |
| 92 | }; |
| 93 | } // namespace install_name_tool |
| 94 | |
| 95 | class InstallNameToolOptTable : public opt::GenericOptTable { |
| 96 | public: |
| 97 | InstallNameToolOptTable() |
| 98 | : GenericOptTable(install_name_tool::OptionStrTable, |
| 99 | install_name_tool::OptionPrefixesTable, |
| 100 | install_name_tool::InstallNameToolInfoTable) {} |
| 101 | }; |
| 102 | |
| 103 | enum BitcodeStripID { |
| 104 | BITCODE_STRIP_INVALID = 0, // This is not an option ID. |
| 105 | #define OPTION(...) \ |
| 106 | LLVM_MAKE_OPT_ID_WITH_ID_PREFIX(BITCODE_STRIP_, __VA_ARGS__), |
| 107 | #include "BitcodeStripOpts.inc" |
| 108 | #undef OPTION |
| 109 | }; |
| 110 | |
| 111 | namespace bitcode_strip { |
| 112 | #define OPTTABLE_STR_TABLE_CODE |
| 113 | #include "BitcodeStripOpts.inc" |
| 114 | #undef OPTTABLE_STR_TABLE_CODE |
| 115 | |
| 116 | #define OPTTABLE_PREFIXES_TABLE_CODE |
| 117 | #include "BitcodeStripOpts.inc" |
| 118 | #undef OPTTABLE_PREFIXES_TABLE_CODE |
| 119 | |
| 120 | static constexpr opt::OptTable::Info BitcodeStripInfoTable[] = { |
| 121 | #define OPTION(...) \ |
| 122 | LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(BITCODE_STRIP_, __VA_ARGS__), |
| 123 | #include "BitcodeStripOpts.inc" |
| 124 | #undef OPTION |
| 125 | }; |
| 126 | } // namespace bitcode_strip |
| 127 | |
| 128 | class BitcodeStripOptTable : public opt::GenericOptTable { |
| 129 | public: |
| 130 | BitcodeStripOptTable() |
| 131 | : opt::GenericOptTable(bitcode_strip::OptionStrTable, |
| 132 | bitcode_strip::OptionPrefixesTable, |
| 133 | bitcode_strip::BitcodeStripInfoTable) {} |
| 134 | }; |
| 135 | |
| 136 | enum StripID { |
| 137 | STRIP_INVALID = 0, // This is not an option ID. |
| 138 | #define OPTION(...) LLVM_MAKE_OPT_ID_WITH_ID_PREFIX(STRIP_, __VA_ARGS__), |
| 139 | #include "StripOpts.inc" |
| 140 | #undef OPTION |
| 141 | }; |
| 142 | |
| 143 | namespace strip { |
| 144 | #define OPTTABLE_STR_TABLE_CODE |
| 145 | #include "StripOpts.inc" |
| 146 | #undef OPTTABLE_STR_TABLE_CODE |
| 147 | |
| 148 | #define OPTTABLE_PREFIXES_TABLE_CODE |
| 149 | #include "StripOpts.inc" |
| 150 | #undef OPTTABLE_PREFIXES_TABLE_CODE |
| 151 | |
| 152 | static constexpr opt::OptTable::Info StripInfoTable[] = { |
| 153 | #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(STRIP_, __VA_ARGS__), |
| 154 | #include "StripOpts.inc" |
| 155 | #undef OPTION |
| 156 | }; |
| 157 | } // namespace strip |
| 158 | |
| 159 | class StripOptTable : public opt::GenericOptTable { |
| 160 | public: |
| 161 | StripOptTable() |
| 162 | : GenericOptTable(strip::OptionStrTable, strip::OptionPrefixesTable, |
| 163 | strip::StripInfoTable) { |
| 164 | setGroupedShortOptions(true); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | enum { |
| 169 | = 0, // This is not an option ID. |
| 170 | #define OPTION(...) \ |
| 171 | LLVM_MAKE_OPT_ID_WITH_ID_PREFIX(EXTRACT_BUNDLE_ENTRY_, __VA_ARGS__), |
| 172 | #include "ExtractBundleEntryOpts.inc" |
| 173 | #undef OPTION |
| 174 | }; |
| 175 | |
| 176 | namespace extract_bundle_entry { |
| 177 | #define OPTTABLE_STR_TABLE_CODE |
| 178 | #include "ExtractBundleEntryOpts.inc" |
| 179 | #undef OPTTABLE_STR_TABLE_CODE |
| 180 | |
| 181 | #define OPTTABLE_PREFIXES_TABLE_CODE |
| 182 | #include "ExtractBundleEntryOpts.inc" |
| 183 | #undef OPTTABLE_PREFIXES_TABLE_CODE |
| 184 | |
| 185 | static constexpr opt::OptTable::Info [] = { |
| 186 | #define OPTION(...) \ |
| 187 | LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(EXTRACT_BUNDLE_ENTRY_, __VA_ARGS__), |
| 188 | #include "ExtractBundleEntryOpts.inc" |
| 189 | #undef OPTION |
| 190 | }; |
| 191 | } // namespace extract_bundle_entry |
| 192 | |
| 193 | class : public opt::GenericOptTable { |
| 194 | public: |
| 195 | () |
| 196 | : GenericOptTable(extract_bundle_entry::OptionStrTable, |
| 197 | extract_bundle_entry::OptionPrefixesTable, |
| 198 | extract_bundle_entry::ExtractBundleEntryInfoTable) { |
| 199 | setGroupedShortOptions(true); |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | } // namespace |
| 204 | |
| 205 | static SectionFlag parseSectionRenameFlag(StringRef SectionName) { |
| 206 | return llvm::StringSwitch<SectionFlag>(SectionName) |
| 207 | .CaseLower(S: "alloc" , Value: SectionFlag::SecAlloc) |
| 208 | .CaseLower(S: "load" , Value: SectionFlag::SecLoad) |
| 209 | .CaseLower(S: "noload" , Value: SectionFlag::SecNoload) |
| 210 | .CaseLower(S: "readonly" , Value: SectionFlag::SecReadonly) |
| 211 | .CaseLower(S: "debug" , Value: SectionFlag::SecDebug) |
| 212 | .CaseLower(S: "code" , Value: SectionFlag::SecCode) |
| 213 | .CaseLower(S: "data" , Value: SectionFlag::SecData) |
| 214 | .CaseLower(S: "rom" , Value: SectionFlag::SecRom) |
| 215 | .CaseLower(S: "merge" , Value: SectionFlag::SecMerge) |
| 216 | .CaseLower(S: "strings" , Value: SectionFlag::SecStrings) |
| 217 | .CaseLower(S: "contents" , Value: SectionFlag::SecContents) |
| 218 | .CaseLower(S: "share" , Value: SectionFlag::SecShare) |
| 219 | .CaseLower(S: "exclude" , Value: SectionFlag::SecExclude) |
| 220 | .CaseLower(S: "large" , Value: SectionFlag::SecLarge) |
| 221 | .Default(Value: SectionFlag::SecNone); |
| 222 | } |
| 223 | |
| 224 | static Expected<SectionFlag> |
| 225 | parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) { |
| 226 | SectionFlag ParsedFlags = SectionFlag::SecNone; |
| 227 | for (StringRef Flag : SectionFlags) { |
| 228 | SectionFlag ParsedFlag = parseSectionRenameFlag(SectionName: Flag); |
| 229 | if (ParsedFlag == SectionFlag::SecNone) |
| 230 | return createStringError( |
| 231 | EC: errc::invalid_argument, |
| 232 | Fmt: "unrecognized section flag '%s'. Flags supported for GNU " |
| 233 | "compatibility: alloc, load, noload, readonly, exclude, debug, " |
| 234 | "code, data, rom, share, contents, merge, strings, large" , |
| 235 | Vals: Flag.str().c_str()); |
| 236 | ParsedFlags |= ParsedFlag; |
| 237 | } |
| 238 | |
| 239 | return ParsedFlags; |
| 240 | } |
| 241 | |
| 242 | static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) { |
| 243 | if (!FlagValue.contains(C: '=')) |
| 244 | return createStringError(EC: errc::invalid_argument, |
| 245 | S: "bad format for --rename-section: missing '='" ); |
| 246 | |
| 247 | // Initial split: ".foo" = ".bar,f1,f2,..." |
| 248 | auto Old2New = FlagValue.split(Separator: '='); |
| 249 | SectionRename SR; |
| 250 | SR.OriginalName = Old2New.first; |
| 251 | |
| 252 | // Flags split: ".bar" "f1" "f2" ... |
| 253 | SmallVector<StringRef, 6> NameAndFlags; |
| 254 | Old2New.second.split(A&: NameAndFlags, Separator: ','); |
| 255 | SR.NewName = NameAndFlags[0]; |
| 256 | |
| 257 | if (NameAndFlags.size() > 1) { |
| 258 | Expected<SectionFlag> ParsedFlagSet = |
| 259 | parseSectionFlagSet(SectionFlags: ArrayRef(NameAndFlags).drop_front()); |
| 260 | if (!ParsedFlagSet) |
| 261 | return ParsedFlagSet.takeError(); |
| 262 | SR.NewFlags = *ParsedFlagSet; |
| 263 | } |
| 264 | |
| 265 | return SR; |
| 266 | } |
| 267 | |
| 268 | static Expected<std::pair<StringRef, uint64_t>> |
| 269 | parseSetSectionAttribute(StringRef Option, StringRef FlagValue) { |
| 270 | if (!FlagValue.contains(C: '=')) |
| 271 | return make_error<StringError>(Args: "bad format for " + Option + ": missing '='" , |
| 272 | Args: errc::invalid_argument); |
| 273 | auto Split = StringRef(FlagValue).split(Separator: '='); |
| 274 | if (Split.first.empty()) |
| 275 | return make_error<StringError>(Args: "bad format for " + Option + |
| 276 | ": missing section name" , |
| 277 | Args: errc::invalid_argument); |
| 278 | uint64_t Value; |
| 279 | if (Split.second.getAsInteger(Radix: 0, Result&: Value)) |
| 280 | return make_error<StringError>(Args: "invalid value for " + Option + ": '" + |
| 281 | Split.second + "'" , |
| 282 | Args: errc::invalid_argument); |
| 283 | return std::make_pair(x&: Split.first, y&: Value); |
| 284 | } |
| 285 | |
| 286 | static Expected<SectionFlagsUpdate> |
| 287 | parseSetSectionFlagValue(StringRef FlagValue) { |
| 288 | if (!StringRef(FlagValue).contains(C: '=')) |
| 289 | return createStringError(EC: errc::invalid_argument, |
| 290 | S: "bad format for --set-section-flags: missing '='" ); |
| 291 | |
| 292 | // Initial split: ".foo" = "f1,f2,..." |
| 293 | auto Section2Flags = StringRef(FlagValue).split(Separator: '='); |
| 294 | SectionFlagsUpdate SFU; |
| 295 | SFU.Name = Section2Flags.first; |
| 296 | |
| 297 | // Flags split: "f1" "f2" ... |
| 298 | SmallVector<StringRef, 6> SectionFlags; |
| 299 | Section2Flags.second.split(A&: SectionFlags, Separator: ','); |
| 300 | Expected<SectionFlag> ParsedFlagSet = parseSectionFlagSet(SectionFlags); |
| 301 | if (!ParsedFlagSet) |
| 302 | return ParsedFlagSet.takeError(); |
| 303 | SFU.NewFlags = *ParsedFlagSet; |
| 304 | |
| 305 | return SFU; |
| 306 | } |
| 307 | |
| 308 | static Expected<uint8_t> parseVisibilityType(StringRef VisType) { |
| 309 | const uint8_t Invalid = 0xff; |
| 310 | uint8_t type = StringSwitch<uint8_t>(VisType) |
| 311 | .Case(S: "default" , Value: ELF::STV_DEFAULT) |
| 312 | .Case(S: "hidden" , Value: ELF::STV_HIDDEN) |
| 313 | .Case(S: "internal" , Value: ELF::STV_INTERNAL) |
| 314 | .Case(S: "protected" , Value: ELF::STV_PROTECTED) |
| 315 | .Default(Value: Invalid); |
| 316 | if (type == Invalid) |
| 317 | return createStringError(EC: errc::invalid_argument, |
| 318 | Fmt: "'%s' is not a valid symbol visibility" , |
| 319 | Vals: VisType.str().c_str()); |
| 320 | return type; |
| 321 | } |
| 322 | |
| 323 | namespace { |
| 324 | struct TargetInfo { |
| 325 | FileFormat Format; |
| 326 | MachineInfo Machine; |
| 327 | }; |
| 328 | } // namespace |
| 329 | |
| 330 | // FIXME: consolidate with the bfd parsing used by lld. |
| 331 | static const StringMap<MachineInfo> TargetMap{ |
| 332 | // Name, {EMachine, 64bit, LittleEndian} |
| 333 | // x86 |
| 334 | {"elf32-i386" , {ELF::EM_386, false, true}}, |
| 335 | {"elf32-x86-64" , {ELF::EM_X86_64, false, true}}, |
| 336 | {"elf64-x86-64" , {ELF::EM_X86_64, true, true}}, |
| 337 | // Intel MCU |
| 338 | {"elf32-iamcu" , {ELF::EM_IAMCU, false, true}}, |
| 339 | // ARM |
| 340 | {"elf32-littlearm" , {ELF::EM_ARM, false, true}}, |
| 341 | // ARM AArch64 |
| 342 | {"elf64-aarch64" , {ELF::EM_AARCH64, true, true}}, |
| 343 | {"elf64-littleaarch64" , {ELF::EM_AARCH64, true, true}}, |
| 344 | // RISC-V |
| 345 | {"elf32-littleriscv" , {ELF::EM_RISCV, false, true}}, |
| 346 | {"elf64-littleriscv" , {ELF::EM_RISCV, true, true}}, |
| 347 | {"elf32-bigriscv" , {ELF::EM_RISCV, false, false}}, |
| 348 | {"elf64-bigriscv" , {ELF::EM_RISCV, true, false}}, |
| 349 | // PowerPC |
| 350 | {"elf32-powerpc" , {ELF::EM_PPC, false, false}}, |
| 351 | {"elf32-powerpcle" , {ELF::EM_PPC, false, true}}, |
| 352 | {"elf64-powerpc" , {ELF::EM_PPC64, true, false}}, |
| 353 | {"elf64-powerpcle" , {ELF::EM_PPC64, true, true}}, |
| 354 | // MIPS |
| 355 | {"elf32-bigmips" , {ELF::EM_MIPS, false, false}}, |
| 356 | {"elf32-ntradbigmips" , {ELF::EM_MIPS, false, false}}, |
| 357 | {"elf32-ntradlittlemips" , {ELF::EM_MIPS, false, true}}, |
| 358 | {"elf32-tradbigmips" , {ELF::EM_MIPS, false, false}}, |
| 359 | {"elf32-tradlittlemips" , {ELF::EM_MIPS, false, true}}, |
| 360 | {"elf64-tradbigmips" , {ELF::EM_MIPS, true, false}}, |
| 361 | {"elf64-tradlittlemips" , {ELF::EM_MIPS, true, true}}, |
| 362 | // SPARC |
| 363 | {"elf32-sparc" , {ELF::EM_SPARC, false, false}}, |
| 364 | {"elf32-sparcel" , {ELF::EM_SPARC, false, true}}, |
| 365 | // Hexagon |
| 366 | {"elf32-hexagon" , {ELF::EM_HEXAGON, false, true}}, |
| 367 | // LoongArch |
| 368 | {"elf32-loongarch" , {ELF::EM_LOONGARCH, false, true}}, |
| 369 | {"elf64-loongarch" , {ELF::EM_LOONGARCH, true, true}}, |
| 370 | // SystemZ |
| 371 | {"elf64-s390" , {ELF::EM_S390, true, false}}, |
| 372 | }; |
| 373 | |
| 374 | static Expected<TargetInfo> |
| 375 | getOutputTargetInfoByTargetName(StringRef TargetName) { |
| 376 | StringRef OriginalTargetName = TargetName; |
| 377 | bool IsFreeBSD = TargetName.consume_back(Suffix: "-freebsd" ); |
| 378 | auto Iter = TargetMap.find(Key: TargetName); |
| 379 | if (Iter == std::end(cont: TargetMap)) |
| 380 | return createStringError(EC: errc::invalid_argument, |
| 381 | Fmt: "invalid output format: '%s'" , |
| 382 | Vals: OriginalTargetName.str().c_str()); |
| 383 | MachineInfo MI = Iter->getValue(); |
| 384 | if (IsFreeBSD) |
| 385 | MI.OSABI = ELF::ELFOSABI_FREEBSD; |
| 386 | |
| 387 | FileFormat Format; |
| 388 | if (TargetName.starts_with(Prefix: "elf" )) |
| 389 | Format = FileFormat::ELF; |
| 390 | else |
| 391 | // This should never happen because `TargetName` is valid (it certainly |
| 392 | // exists in the TargetMap). |
| 393 | llvm_unreachable("unknown target prefix" ); |
| 394 | |
| 395 | return {TargetInfo{.Format: Format, .Machine: MI}}; |
| 396 | } |
| 397 | |
| 398 | static Error addSymbolsFromFile(NameMatcher &Symbols, BumpPtrAllocator &Alloc, |
| 399 | StringRef Filename, MatchStyle MS, |
| 400 | function_ref<Error(Error)> ErrorCallback) { |
| 401 | StringSaver Saver(Alloc); |
| 402 | SmallVector<StringRef, 16> Lines; |
| 403 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 404 | if (!BufOrErr) |
| 405 | return createFileError(F: Filename, EC: BufOrErr.getError()); |
| 406 | |
| 407 | BufOrErr.get()->getBuffer().split(A&: Lines, Separator: '\n'); |
| 408 | for (StringRef Line : Lines) { |
| 409 | // Ignore everything after '#', trim whitespace, and only add the symbol if |
| 410 | // it's not empty. |
| 411 | auto TrimmedLine = Line.split(Separator: '#').first.trim(); |
| 412 | if (!TrimmedLine.empty()) |
| 413 | if (Error E = Symbols.addMatcher(Matcher: NameOrPattern::create( |
| 414 | Pattern: Saver.save(S: TrimmedLine), MS, ErrorCallback))) |
| 415 | return E; |
| 416 | } |
| 417 | |
| 418 | return Error::success(); |
| 419 | } |
| 420 | |
| 421 | static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename, |
| 422 | BumpPtrAllocator &Alloc, |
| 423 | StringRef Filename) { |
| 424 | StringSaver Saver(Alloc); |
| 425 | SmallVector<StringRef, 16> Lines; |
| 426 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 427 | if (!BufOrErr) |
| 428 | return createFileError(F: Filename, EC: BufOrErr.getError()); |
| 429 | |
| 430 | BufOrErr.get()->getBuffer().split(A&: Lines, Separator: '\n'); |
| 431 | size_t NumLines = Lines.size(); |
| 432 | for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) { |
| 433 | StringRef TrimmedLine = Lines[LineNo].split(Separator: '#').first.trim(); |
| 434 | if (TrimmedLine.empty()) |
| 435 | continue; |
| 436 | |
| 437 | std::pair<StringRef, StringRef> Pair = Saver.save(S: TrimmedLine).split(Separator: ' '); |
| 438 | StringRef NewName = Pair.second.trim(); |
| 439 | if (NewName.empty()) |
| 440 | return createStringError(EC: errc::invalid_argument, |
| 441 | Fmt: "%s:%zu: missing new symbol name" , |
| 442 | Vals: Filename.str().c_str(), Vals: LineNo + 1); |
| 443 | SymbolsToRename.insert(KV: {Pair.first, NewName}); |
| 444 | } |
| 445 | return Error::success(); |
| 446 | } |
| 447 | |
| 448 | template <class T> static ErrorOr<T> getAsInteger(StringRef Val) { |
| 449 | T Result; |
| 450 | if (Val.getAsInteger(0, Result)) |
| 451 | return errc::invalid_argument; |
| 452 | return Result; |
| 453 | } |
| 454 | |
| 455 | namespace { |
| 456 | |
| 457 | enum class ToolType { |
| 458 | Objcopy, |
| 459 | Strip, |
| 460 | InstallNameTool, |
| 461 | BitcodeStrip, |
| 462 | |
| 463 | }; |
| 464 | |
| 465 | } // anonymous namespace |
| 466 | |
| 467 | static void printHelp(const opt::OptTable &OptTable, raw_ostream &OS, |
| 468 | ToolType Tool) { |
| 469 | StringRef HelpText, ToolName; |
| 470 | switch (Tool) { |
| 471 | case ToolType::Objcopy: |
| 472 | ToolName = "llvm-objcopy" ; |
| 473 | HelpText = " [options] input [output]" ; |
| 474 | break; |
| 475 | case ToolType::Strip: |
| 476 | ToolName = "llvm-strip" ; |
| 477 | HelpText = " [options] inputs..." ; |
| 478 | break; |
| 479 | case ToolType::InstallNameTool: |
| 480 | ToolName = "llvm-install-name-tool" ; |
| 481 | HelpText = " [options] input" ; |
| 482 | break; |
| 483 | case ToolType::BitcodeStrip: |
| 484 | ToolName = "llvm-bitcode-strip" ; |
| 485 | HelpText = " [options] input" ; |
| 486 | break; |
| 487 | case ToolType::ExtractBundleEntry: |
| 488 | ToolName = "llvm-extract-bundle-entry" ; |
| 489 | HelpText = " URI" ; |
| 490 | break; |
| 491 | } |
| 492 | OptTable.printHelp(OS, Usage: (ToolName + HelpText).str().c_str(), |
| 493 | Title: (ToolName + " tool" ).str().c_str()); |
| 494 | // TODO: Replace this with libOption call once it adds extrahelp support. |
| 495 | // The CommandLine library has a cl::extrahelp class to support this, |
| 496 | // but libOption does not have that yet. |
| 497 | OS << "\nPass @FILE as argument to read options from FILE.\n" ; |
| 498 | } |
| 499 | |
| 500 | static Expected<NewSymbolInfo> parseNewSymbolInfo(StringRef FlagValue) { |
| 501 | // Parse value given with --add-symbol option and create the |
| 502 | // new symbol if possible. The value format for --add-symbol is: |
| 503 | // |
| 504 | // <name>=[<section>:]<value>[,<flags>] |
| 505 | // |
| 506 | // where: |
| 507 | // <name> - symbol name, can be empty string |
| 508 | // <section> - optional section name. If not given ABS symbol is created |
| 509 | // <value> - symbol value, can be decimal or hexadecimal number prefixed |
| 510 | // with 0x. |
| 511 | // <flags> - optional flags affecting symbol type, binding or visibility. |
| 512 | NewSymbolInfo SI; |
| 513 | StringRef Value; |
| 514 | std::tie(args&: SI.SymbolName, args&: Value) = FlagValue.split(Separator: '='); |
| 515 | if (Value.empty()) |
| 516 | return createStringError( |
| 517 | EC: errc::invalid_argument, |
| 518 | Fmt: "bad format for --add-symbol, missing '=' after '%s'" , |
| 519 | Vals: SI.SymbolName.str().c_str()); |
| 520 | |
| 521 | if (Value.contains(C: ':')) { |
| 522 | std::tie(args&: SI.SectionName, args&: Value) = Value.split(Separator: ':'); |
| 523 | if (SI.SectionName.empty() || Value.empty()) |
| 524 | return createStringError( |
| 525 | EC: errc::invalid_argument, |
| 526 | S: "bad format for --add-symbol, missing section name or symbol value" ); |
| 527 | } |
| 528 | |
| 529 | SmallVector<StringRef, 6> Flags; |
| 530 | Value.split(A&: Flags, Separator: ','); |
| 531 | if (Flags[0].getAsInteger(Radix: 0, Result&: SI.Value)) |
| 532 | return createStringError(EC: errc::invalid_argument, Fmt: "bad symbol value: '%s'" , |
| 533 | Vals: Flags[0].str().c_str()); |
| 534 | |
| 535 | using Functor = std::function<void()>; |
| 536 | SmallVector<StringRef, 6> UnsupportedFlags; |
| 537 | for (size_t I = 1, NumFlags = Flags.size(); I < NumFlags; ++I) |
| 538 | static_cast<Functor>( |
| 539 | StringSwitch<Functor>(Flags[I]) |
| 540 | .CaseLower(S: "global" , |
| 541 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Global); }) |
| 542 | .CaseLower(S: "local" , Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Local); }) |
| 543 | .CaseLower(S: "weak" , Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Weak); }) |
| 544 | .CaseLower(S: "default" , |
| 545 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Default); }) |
| 546 | .CaseLower(S: "hidden" , |
| 547 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Hidden); }) |
| 548 | .CaseLower(S: "protected" , |
| 549 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Protected); }) |
| 550 | .CaseLower(S: "file" , Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::File); }) |
| 551 | .CaseLower(S: "section" , |
| 552 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Section); }) |
| 553 | .CaseLower(S: "object" , |
| 554 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Object); }) |
| 555 | .CaseLower(S: "function" , |
| 556 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Function); }) |
| 557 | .CaseLower( |
| 558 | S: "indirect-function" , |
| 559 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::IndirectFunction); }) |
| 560 | .CaseLower(S: "debug" , Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Debug); }) |
| 561 | .CaseLower(S: "constructor" , |
| 562 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Constructor); }) |
| 563 | .CaseLower(S: "warning" , |
| 564 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Warning); }) |
| 565 | .CaseLower(S: "indirect" , |
| 566 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Indirect); }) |
| 567 | .CaseLower(S: "synthetic" , |
| 568 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::Synthetic); }) |
| 569 | .CaseLower(S: "unique-object" , |
| 570 | Value: [&] { SI.Flags.push_back(Elt: SymbolFlag::UniqueObject); }) |
| 571 | .StartsWithLower(S: "before=" , |
| 572 | Value: [&] { |
| 573 | StringRef SymNamePart = |
| 574 | Flags[I].split(Separator: '=').second; |
| 575 | |
| 576 | if (!SymNamePart.empty()) |
| 577 | SI.BeforeSyms.push_back(Elt: SymNamePart); |
| 578 | }) |
| 579 | .Default(Value: [&] { UnsupportedFlags.push_back(Elt: Flags[I]); }))(); |
| 580 | if (!UnsupportedFlags.empty()) |
| 581 | return createStringError(EC: errc::invalid_argument, |
| 582 | Fmt: "unsupported flag%s for --add-symbol: '%s'" , |
| 583 | Vals: UnsupportedFlags.size() > 1 ? "s" : "" , |
| 584 | Vals: join(R&: UnsupportedFlags, Separator: "', '" ).c_str()); |
| 585 | |
| 586 | return SI; |
| 587 | } |
| 588 | |
| 589 | static Expected<RemoveNoteInfo> parseRemoveNoteInfo(StringRef FlagValue) { |
| 590 | // Parse value given with --remove-note option. The format is: |
| 591 | // |
| 592 | // [name/]type_id |
| 593 | // |
| 594 | // where: |
| 595 | // <name> - optional note name. If not given, all notes with the specified |
| 596 | // <type_id> are removed. |
| 597 | // <type_id> - note type value, can be decimal or hexadecimal number prefixed |
| 598 | // with 0x. |
| 599 | RemoveNoteInfo NI; |
| 600 | StringRef TypeIdStr; |
| 601 | if (auto Idx = FlagValue.find(C: '/'); Idx != StringRef::npos) { |
| 602 | if (Idx == 0) |
| 603 | return createStringError( |
| 604 | EC: errc::invalid_argument, |
| 605 | S: "bad format for --remove-note, note name is empty" ); |
| 606 | NI.Name = FlagValue.slice(Start: 0, End: Idx); |
| 607 | TypeIdStr = FlagValue.substr(Start: Idx + 1); |
| 608 | } else { |
| 609 | TypeIdStr = FlagValue; |
| 610 | } |
| 611 | if (TypeIdStr.empty()) |
| 612 | return createStringError(EC: errc::invalid_argument, |
| 613 | S: "bad format for --remove-note, missing type_id" ); |
| 614 | if (TypeIdStr.getAsInteger(Radix: 0, Result&: NI.TypeId)) |
| 615 | return createStringError(EC: errc::invalid_argument, |
| 616 | Fmt: "bad note type_id for --remove-note: '%s'" , |
| 617 | Vals: TypeIdStr.str().c_str()); |
| 618 | return NI; |
| 619 | } |
| 620 | |
| 621 | // Parse input option \p ArgValue and load section data. This function |
| 622 | // extracts section name and name of the file keeping section data from |
| 623 | // ArgValue, loads data from the file, and stores section name and data |
| 624 | // into the vector of new sections \p NewSections. |
| 625 | static Error loadNewSectionData(StringRef ArgValue, StringRef OptionName, |
| 626 | SmallVector<NewSectionInfo, 0> &NewSections) { |
| 627 | if (!ArgValue.contains(C: '=')) |
| 628 | return createStringError(EC: errc::invalid_argument, |
| 629 | S: "bad format for " + OptionName + ": missing '='" ); |
| 630 | |
| 631 | std::pair<StringRef, StringRef> SecPair = ArgValue.split(Separator: "=" ); |
| 632 | if (SecPair.second.empty()) |
| 633 | return createStringError(EC: errc::invalid_argument, S: "bad format for " + |
| 634 | OptionName + |
| 635 | ": missing file name" ); |
| 636 | |
| 637 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = |
| 638 | MemoryBuffer::getFile(Filename: SecPair.second); |
| 639 | if (!BufOrErr) |
| 640 | return createFileError(F: SecPair.second, |
| 641 | E: errorCodeToError(EC: BufOrErr.getError())); |
| 642 | |
| 643 | NewSections.push_back(Elt: {SecPair.first, std::move(*BufOrErr)}); |
| 644 | return Error::success(); |
| 645 | } |
| 646 | |
| 647 | static Expected<int64_t> parseChangeSectionLMA(StringRef ArgValue, |
| 648 | StringRef OptionName) { |
| 649 | StringRef StringValue; |
| 650 | if (ArgValue.starts_with(Prefix: "*+" )) { |
| 651 | StringValue = ArgValue.substr(Start: 2); |
| 652 | } else if (ArgValue.starts_with(Prefix: "*-" )) { |
| 653 | StringValue = ArgValue.substr(Start: 1); |
| 654 | } else if (ArgValue.contains(Other: "=" )) { |
| 655 | return createStringError(EC: errc::invalid_argument, |
| 656 | S: "bad format for " + OptionName + |
| 657 | ": changing LMA to a specific value is not " |
| 658 | "supported. Use *+val or *-val instead" ); |
| 659 | } else if (ArgValue.contains(Other: "+" ) || ArgValue.contains(Other: "-" )) { |
| 660 | return createStringError(EC: errc::invalid_argument, |
| 661 | S: "bad format for " + OptionName + |
| 662 | ": changing a specific section LMA is not " |
| 663 | "supported. Use *+val or *-val instead" ); |
| 664 | } |
| 665 | if (StringValue.empty()) |
| 666 | return createStringError(EC: errc::invalid_argument, |
| 667 | S: "bad format for " + OptionName + |
| 668 | ": missing LMA offset" ); |
| 669 | |
| 670 | auto LMAValue = getAsInteger<int64_t>(Val: StringValue); |
| 671 | if (!LMAValue) |
| 672 | return createStringError(EC: LMAValue.getError(), |
| 673 | S: "bad format for " + OptionName + ": value after " + |
| 674 | ArgValue.slice(Start: 0, End: 2) + " is " + StringValue + |
| 675 | " when it should be an integer" ); |
| 676 | return *LMAValue; |
| 677 | } |
| 678 | |
| 679 | static Expected<SectionPatternAddressUpdate> |
| 680 | parseChangeSectionAddr(StringRef ArgValue, StringRef OptionName, |
| 681 | MatchStyle SectionMatchStyle, |
| 682 | function_ref<Error(Error)> ErrorCallback) { |
| 683 | SectionPatternAddressUpdate PatternUpdate; |
| 684 | |
| 685 | size_t LastSymbolIndex = ArgValue.find_last_of(Chars: "+-=" ); |
| 686 | if (LastSymbolIndex == StringRef::npos) |
| 687 | return createStringError(EC: errc::invalid_argument, |
| 688 | S: "bad format for " + OptionName + |
| 689 | ": argument value " + ArgValue + |
| 690 | " is invalid. See --help" ); |
| 691 | char UpdateSymbol = ArgValue[LastSymbolIndex]; |
| 692 | |
| 693 | StringRef SectionPattern = ArgValue.slice(Start: 0, End: LastSymbolIndex); |
| 694 | if (SectionPattern.empty()) |
| 695 | return createStringError( |
| 696 | EC: errc::invalid_argument, |
| 697 | S: "bad format for " + OptionName + |
| 698 | ": missing section pattern to apply address change to" ); |
| 699 | if (Error E = PatternUpdate.SectionPattern.addMatcher(Matcher: NameOrPattern::create( |
| 700 | Pattern: SectionPattern, MS: SectionMatchStyle, ErrorCallback))) |
| 701 | return std::move(E); |
| 702 | |
| 703 | StringRef Value = ArgValue.substr(Start: LastSymbolIndex + 1); |
| 704 | if (Value.empty()) { |
| 705 | switch (UpdateSymbol) { |
| 706 | case '+': |
| 707 | case '-': |
| 708 | return createStringError(EC: errc::invalid_argument, |
| 709 | S: "bad format for " + OptionName + |
| 710 | ": missing value of offset after '" + |
| 711 | std::string({UpdateSymbol}) + "'" ); |
| 712 | |
| 713 | case '=': |
| 714 | return createStringError(EC: errc::invalid_argument, |
| 715 | S: "bad format for " + OptionName + |
| 716 | ": missing address value after '='" ); |
| 717 | } |
| 718 | } |
| 719 | auto AddrValue = getAsInteger<uint64_t>(Val: Value); |
| 720 | if (!AddrValue) |
| 721 | return createStringError(EC: AddrValue.getError(), |
| 722 | S: "bad format for " + OptionName + ": value after " + |
| 723 | std::string({UpdateSymbol}) + " is " + Value + |
| 724 | " when it should be a 64-bit integer" ); |
| 725 | |
| 726 | switch (UpdateSymbol) { |
| 727 | case '+': |
| 728 | PatternUpdate.Update.Kind = AdjustKind::Add; |
| 729 | break; |
| 730 | case '-': |
| 731 | PatternUpdate.Update.Kind = AdjustKind::Subtract; |
| 732 | break; |
| 733 | case '=': |
| 734 | PatternUpdate.Update.Kind = AdjustKind::Set; |
| 735 | } |
| 736 | |
| 737 | PatternUpdate.Update.Value = *AddrValue; |
| 738 | return PatternUpdate; |
| 739 | } |
| 740 | |
| 741 | // parseObjcopyOptions returns the config and sets the input arguments. If a |
| 742 | // help flag is set then parseObjcopyOptions will print the help messege and |
| 743 | // exit. |
| 744 | Expected<DriverConfig> |
| 745 | objcopy::parseObjcopyOptions(ArrayRef<const char *> ArgsArr, |
| 746 | function_ref<Error(Error)> ErrorCallback) { |
| 747 | DriverConfig DC; |
| 748 | ObjcopyOptTable T; |
| 749 | |
| 750 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 751 | llvm::opt::InputArgList InputArgs = |
| 752 | T.ParseArgs(Args: ArgsArr, MissingArgIndex&: MissingArgumentIndex, MissingArgCount&: MissingArgumentCount); |
| 753 | |
| 754 | if (MissingArgumentCount) |
| 755 | return createStringError( |
| 756 | EC: errc::invalid_argument, |
| 757 | Fmt: "argument to '%s' is missing (expected %d value(s))" , |
| 758 | Vals: InputArgs.getArgString(Index: MissingArgumentIndex), Vals: MissingArgumentCount); |
| 759 | |
| 760 | if (InputArgs.size() == 0) { |
| 761 | printHelp(OptTable: T, OS&: errs(), Tool: ToolType::Objcopy); |
| 762 | exit(status: 1); |
| 763 | } |
| 764 | |
| 765 | if (InputArgs.hasArg(Ids: OBJCOPY_help)) { |
| 766 | printHelp(OptTable: T, OS&: outs(), Tool: ToolType::Objcopy); |
| 767 | exit(status: 0); |
| 768 | } |
| 769 | |
| 770 | if (InputArgs.hasArg(Ids: OBJCOPY_version)) { |
| 771 | outs() << "llvm-objcopy, compatible with GNU objcopy\n" ; |
| 772 | cl::PrintVersionMessage(); |
| 773 | exit(status: 0); |
| 774 | } |
| 775 | |
| 776 | SmallVector<const char *, 2> Positional; |
| 777 | |
| 778 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_UNKNOWN)) |
| 779 | return createStringError(EC: errc::invalid_argument, Fmt: "unknown argument '%s'" , |
| 780 | Vals: Arg->getAsString(Args: InputArgs).c_str()); |
| 781 | |
| 782 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_INPUT)) |
| 783 | Positional.push_back(Elt: Arg->getValue()); |
| 784 | |
| 785 | if (Positional.empty()) |
| 786 | return createStringError(EC: errc::invalid_argument, S: "no input file specified" ); |
| 787 | |
| 788 | if (Positional.size() > 2) |
| 789 | return createStringError(EC: errc::invalid_argument, |
| 790 | S: "too many positional arguments" ); |
| 791 | |
| 792 | ConfigManager ConfigMgr; |
| 793 | CommonConfig &Config = ConfigMgr.Common; |
| 794 | COFFConfig &COFFConfig = ConfigMgr.COFF; |
| 795 | ELFConfig &ELFConfig = ConfigMgr.ELF; |
| 796 | MachOConfig &MachOConfig = ConfigMgr.MachO; |
| 797 | Config.InputFilename = Positional[0]; |
| 798 | Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; |
| 799 | if (InputArgs.hasArg(Ids: OBJCOPY_target) && |
| 800 | (InputArgs.hasArg(Ids: OBJCOPY_input_target) || |
| 801 | InputArgs.hasArg(Ids: OBJCOPY_output_target))) |
| 802 | return createStringError( |
| 803 | EC: errc::invalid_argument, |
| 804 | S: "--target cannot be used with --input-target or --output-target" ); |
| 805 | |
| 806 | if (InputArgs.hasArg(Ids: OBJCOPY_regex) && InputArgs.hasArg(Ids: OBJCOPY_wildcard)) |
| 807 | return createStringError(EC: errc::invalid_argument, |
| 808 | S: "--regex and --wildcard are incompatible" ); |
| 809 | |
| 810 | MatchStyle SectionMatchStyle = InputArgs.hasArg(Ids: OBJCOPY_regex) |
| 811 | ? MatchStyle::Regex |
| 812 | : MatchStyle::Wildcard; |
| 813 | MatchStyle SymbolMatchStyle |
| 814 | = InputArgs.hasArg(Ids: OBJCOPY_regex) ? MatchStyle::Regex |
| 815 | : InputArgs.hasArg(Ids: OBJCOPY_wildcard) ? MatchStyle::Wildcard |
| 816 | : MatchStyle::Literal; |
| 817 | StringRef InputFormat, OutputFormat; |
| 818 | if (InputArgs.hasArg(Ids: OBJCOPY_target)) { |
| 819 | InputFormat = InputArgs.getLastArgValue(Id: OBJCOPY_target); |
| 820 | OutputFormat = InputArgs.getLastArgValue(Id: OBJCOPY_target); |
| 821 | } else { |
| 822 | InputFormat = InputArgs.getLastArgValue(Id: OBJCOPY_input_target); |
| 823 | OutputFormat = InputArgs.getLastArgValue(Id: OBJCOPY_output_target); |
| 824 | } |
| 825 | |
| 826 | // FIXME: Currently, we ignore the target for non-binary/ihex formats |
| 827 | // explicitly specified by -I option (e.g. -Ielf32-x86-64) and guess the |
| 828 | // format by llvm::object::createBinary regardless of the option value. |
| 829 | Config.InputFormat = StringSwitch<FileFormat>(InputFormat) |
| 830 | .Case(S: "binary" , Value: FileFormat::Binary) |
| 831 | .Case(S: "ihex" , Value: FileFormat::IHex) |
| 832 | .Default(Value: FileFormat::Unspecified); |
| 833 | |
| 834 | if (InputArgs.hasArg(Ids: OBJCOPY_new_symbol_visibility)) { |
| 835 | const uint8_t Invalid = 0xff; |
| 836 | StringRef VisibilityStr = |
| 837 | InputArgs.getLastArgValue(Id: OBJCOPY_new_symbol_visibility); |
| 838 | |
| 839 | ELFConfig.NewSymbolVisibility = StringSwitch<uint8_t>(VisibilityStr) |
| 840 | .Case(S: "default" , Value: ELF::STV_DEFAULT) |
| 841 | .Case(S: "hidden" , Value: ELF::STV_HIDDEN) |
| 842 | .Case(S: "internal" , Value: ELF::STV_INTERNAL) |
| 843 | .Case(S: "protected" , Value: ELF::STV_PROTECTED) |
| 844 | .Default(Value: Invalid); |
| 845 | |
| 846 | if (ELFConfig.NewSymbolVisibility == Invalid) |
| 847 | return createStringError(EC: errc::invalid_argument, |
| 848 | Fmt: "'%s' is not a valid symbol visibility" , |
| 849 | Vals: VisibilityStr.str().c_str()); |
| 850 | } |
| 851 | |
| 852 | for (const auto *Arg : InputArgs.filtered(Ids: OBJCOPY_subsystem)) { |
| 853 | StringRef Subsystem, Version; |
| 854 | std::tie(args&: Subsystem, args&: Version) = StringRef(Arg->getValue()).split(Separator: ':'); |
| 855 | COFFConfig.Subsystem = |
| 856 | StringSwitch<unsigned>(Subsystem.lower()) |
| 857 | .Case(S: "boot_application" , |
| 858 | Value: COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION) |
| 859 | .Case(S: "console" , Value: COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI) |
| 860 | .Cases(CaseStrings: {"efi_application" , "efi-app" }, |
| 861 | Value: COFF::IMAGE_SUBSYSTEM_EFI_APPLICATION) |
| 862 | .Cases(CaseStrings: {"efi_boot_service_driver" , "efi-bsd" }, |
| 863 | Value: COFF::IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) |
| 864 | .Case(S: "efi_rom" , Value: COFF::IMAGE_SUBSYSTEM_EFI_ROM) |
| 865 | .Cases(CaseStrings: {"efi_runtime_driver" , "efi-rtd" }, |
| 866 | Value: COFF::IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) |
| 867 | .Case(S: "native" , Value: COFF::IMAGE_SUBSYSTEM_NATIVE) |
| 868 | .Case(S: "posix" , Value: COFF::IMAGE_SUBSYSTEM_POSIX_CUI) |
| 869 | .Case(S: "windows" , Value: COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI) |
| 870 | .Case(S: "xbox" , Value: COFF::IMAGE_SUBSYSTEM_XBOX) |
| 871 | .Default(Value: COFF::IMAGE_SUBSYSTEM_UNKNOWN); |
| 872 | if (*COFFConfig.Subsystem == COFF::IMAGE_SUBSYSTEM_UNKNOWN) |
| 873 | return createStringError(EC: errc::invalid_argument, |
| 874 | Fmt: "'%s' is not a valid subsystem" , |
| 875 | Vals: Subsystem.str().c_str()); |
| 876 | if (!Version.empty()) { |
| 877 | StringRef Major, Minor; |
| 878 | std::tie(args&: Major, args&: Minor) = Version.split(Separator: '.'); |
| 879 | unsigned Number; |
| 880 | if (Major.getAsInteger(Radix: 10, Result&: Number)) |
| 881 | return createStringError(EC: errc::invalid_argument, |
| 882 | Fmt: "'%s' is not a valid subsystem major version" , |
| 883 | Vals: Major.str().c_str()); |
| 884 | COFFConfig.MajorSubsystemVersion = Number; |
| 885 | Number = 0; |
| 886 | if (!Minor.empty() && Minor.getAsInteger(Radix: 10, Result&: Number)) |
| 887 | return createStringError(EC: errc::invalid_argument, |
| 888 | Fmt: "'%s' is not a valid subsystem minor version" , |
| 889 | Vals: Minor.str().c_str()); |
| 890 | COFFConfig.MinorSubsystemVersion = Number; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | Config.OutputFormat = StringSwitch<FileFormat>(OutputFormat) |
| 895 | .Case(S: "binary" , Value: FileFormat::Binary) |
| 896 | .Case(S: "ihex" , Value: FileFormat::IHex) |
| 897 | .Case(S: "srec" , Value: FileFormat::SREC) |
| 898 | .Default(Value: FileFormat::Unspecified); |
| 899 | if (Config.OutputFormat == FileFormat::Unspecified) { |
| 900 | if (OutputFormat.empty()) { |
| 901 | Config.OutputFormat = Config.InputFormat; |
| 902 | } else { |
| 903 | Expected<TargetInfo> Target = |
| 904 | getOutputTargetInfoByTargetName(TargetName: OutputFormat); |
| 905 | if (!Target) |
| 906 | return Target.takeError(); |
| 907 | Config.OutputFormat = Target->Format; |
| 908 | Config.OutputArch = Target->Machine; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (const auto *A = InputArgs.getLastArg(Ids: OBJCOPY_compress_debug_sections)) { |
| 913 | Config.CompressionType = StringSwitch<DebugCompressionType>(A->getValue()) |
| 914 | .Case(S: "zlib" , Value: DebugCompressionType::Zlib) |
| 915 | .Case(S: "zstd" , Value: DebugCompressionType::Zstd) |
| 916 | .Default(Value: DebugCompressionType::None); |
| 917 | if (Config.CompressionType == DebugCompressionType::None) { |
| 918 | return createStringError( |
| 919 | EC: errc::invalid_argument, |
| 920 | Fmt: "invalid or unsupported --compress-debug-sections format: %s" , |
| 921 | Vals: A->getValue()); |
| 922 | } |
| 923 | if (const char *Reason = compression::getReasonIfUnsupported( |
| 924 | F: compression::formatFor(Type: Config.CompressionType))) |
| 925 | return createStringError(EC: errc::invalid_argument, S: Reason); |
| 926 | } |
| 927 | |
| 928 | for (const auto *A : InputArgs.filtered(Ids: OBJCOPY_compress_sections)) { |
| 929 | SmallVector<StringRef, 0> Fields; |
| 930 | StringRef(A->getValue()).split(A&: Fields, Separator: '='); |
| 931 | if (Fields.size() != 2 || Fields[1].empty()) { |
| 932 | return createStringError( |
| 933 | EC: errc::invalid_argument, |
| 934 | S: A->getSpelling() + |
| 935 | ": parse error, not 'section-glob=[none|zlib|zstd]'" ); |
| 936 | } |
| 937 | |
| 938 | auto Type = StringSwitch<DebugCompressionType>(Fields[1]) |
| 939 | .Case(S: "zlib" , Value: DebugCompressionType::Zlib) |
| 940 | .Case(S: "zstd" , Value: DebugCompressionType::Zstd) |
| 941 | .Default(Value: DebugCompressionType::None); |
| 942 | if (Type == DebugCompressionType::None && Fields[1] != "none" ) { |
| 943 | return createStringError( |
| 944 | EC: errc::invalid_argument, |
| 945 | Fmt: "invalid or unsupported --compress-sections format: %s" , |
| 946 | Vals: A->getValue()); |
| 947 | } |
| 948 | if (Type != DebugCompressionType::None) { |
| 949 | if (const char *Reason = |
| 950 | compression::getReasonIfUnsupported(F: compression::formatFor(Type))) |
| 951 | return createStringError(EC: errc::invalid_argument, S: Reason); |
| 952 | } |
| 953 | |
| 954 | auto &P = Config.compressSections.emplace_back(); |
| 955 | P.second = Type; |
| 956 | auto Matcher = |
| 957 | NameOrPattern::create(Pattern: Fields[0], MS: SectionMatchStyle, ErrorCallback); |
| 958 | // =none allows overriding a previous =zlib or =zstd. Reject negative |
| 959 | // patterns, which would be confusing. |
| 960 | if (Matcher && !Matcher->isPositiveMatch()) { |
| 961 | return createStringError( |
| 962 | EC: errc::invalid_argument, |
| 963 | S: "--compress-sections: negative pattern is unsupported" ); |
| 964 | } |
| 965 | if (Error E = P.first.addMatcher(Matcher: std::move(Matcher))) |
| 966 | return std::move(E); |
| 967 | } |
| 968 | |
| 969 | Config.AddGnuDebugLink = InputArgs.getLastArgValue(Id: OBJCOPY_add_gnu_debuglink); |
| 970 | // The gnu_debuglink's target is expected to not change or else its CRC would |
| 971 | // become invalidated and get rejected. We can avoid recalculating the |
| 972 | // checksum for every target file inside an archive by precomputing the CRC |
| 973 | // here. This prevents a significant amount of I/O. |
| 974 | if (!Config.AddGnuDebugLink.empty()) { |
| 975 | auto DebugOrErr = MemoryBuffer::getFile(Filename: Config.AddGnuDebugLink); |
| 976 | if (!DebugOrErr) |
| 977 | return createFileError(F: Config.AddGnuDebugLink, EC: DebugOrErr.getError()); |
| 978 | auto Debug = std::move(*DebugOrErr); |
| 979 | Config.GnuDebugLinkCRC32 = |
| 980 | llvm::crc32(Data: arrayRefFromStringRef(Input: Debug->getBuffer())); |
| 981 | } |
| 982 | Config.SplitDWO = InputArgs.getLastArgValue(Id: OBJCOPY_split_dwo); |
| 983 | |
| 984 | Config.SymbolsPrefix = InputArgs.getLastArgValue(Id: OBJCOPY_prefix_symbols); |
| 985 | Config.SymbolsPrefixRemove = |
| 986 | InputArgs.getLastArgValue(Id: OBJCOPY_remove_symbol_prefix); |
| 987 | |
| 988 | Config.AllocSectionsPrefix = |
| 989 | InputArgs.getLastArgValue(Id: OBJCOPY_prefix_alloc_sections); |
| 990 | if (auto Arg = InputArgs.getLastArg(Ids: OBJCOPY_extract_partition)) |
| 991 | Config.ExtractPartition = Arg->getValue(); |
| 992 | |
| 993 | if (const auto *A = InputArgs.getLastArg(Ids: OBJCOPY_gap_fill)) { |
| 994 | if (Config.OutputFormat != FileFormat::Binary) |
| 995 | return createStringError( |
| 996 | EC: errc::invalid_argument, |
| 997 | S: "'--gap-fill' is only supported for binary output" ); |
| 998 | ErrorOr<uint64_t> Val = getAsInteger<uint64_t>(Val: A->getValue()); |
| 999 | if (!Val) |
| 1000 | return createStringError(EC: Val.getError(), Fmt: "--gap-fill: bad number: %s" , |
| 1001 | Vals: A->getValue()); |
| 1002 | uint8_t ByteVal = Val.get(); |
| 1003 | if (ByteVal != Val.get()) |
| 1004 | return createStringError(EC: std::errc::value_too_large, |
| 1005 | Fmt: "gap-fill value %s is out of range (0 to 0xff)" , |
| 1006 | Vals: A->getValue()); |
| 1007 | Config.GapFill = ByteVal; |
| 1008 | } |
| 1009 | |
| 1010 | if (const auto *A = InputArgs.getLastArg(Ids: OBJCOPY_pad_to)) { |
| 1011 | if (Config.OutputFormat != FileFormat::Binary) |
| 1012 | return createStringError( |
| 1013 | EC: errc::invalid_argument, |
| 1014 | S: "'--pad-to' is only supported for binary output" ); |
| 1015 | ErrorOr<uint64_t> Addr = getAsInteger<uint64_t>(Val: A->getValue()); |
| 1016 | if (!Addr) |
| 1017 | return createStringError(EC: Addr.getError(), Fmt: "--pad-to: bad number: %s" , |
| 1018 | Vals: A->getValue()); |
| 1019 | Config.PadTo = *Addr; |
| 1020 | } |
| 1021 | |
| 1022 | if (const auto *Arg = InputArgs.getLastArg(Ids: OBJCOPY_change_section_lma)) { |
| 1023 | Expected<int64_t> LMAValue = |
| 1024 | parseChangeSectionLMA(ArgValue: Arg->getValue(), OptionName: Arg->getSpelling()); |
| 1025 | if (!LMAValue) |
| 1026 | return LMAValue.takeError(); |
| 1027 | Config.ChangeSectionLMAValAll = *LMAValue; |
| 1028 | } |
| 1029 | |
| 1030 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_change_section_address)) { |
| 1031 | Expected<SectionPatternAddressUpdate> AddressUpdate = |
| 1032 | parseChangeSectionAddr(ArgValue: Arg->getValue(), OptionName: Arg->getSpelling(), |
| 1033 | SectionMatchStyle, ErrorCallback); |
| 1034 | if (!AddressUpdate) |
| 1035 | return AddressUpdate.takeError(); |
| 1036 | Config.ChangeSectionAddress.push_back(Elt: *AddressUpdate); |
| 1037 | } |
| 1038 | |
| 1039 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_redefine_symbol)) { |
| 1040 | if (!StringRef(Arg->getValue()).contains(C: '=')) |
| 1041 | return createStringError(EC: errc::invalid_argument, |
| 1042 | S: "bad format for --redefine-sym" ); |
| 1043 | auto Old2New = StringRef(Arg->getValue()).split(Separator: '='); |
| 1044 | if (!Config.SymbolsToRename.insert(KV: Old2New).second) |
| 1045 | return createStringError(EC: errc::invalid_argument, |
| 1046 | Fmt: "multiple redefinition of symbol '%s'" , |
| 1047 | Vals: Old2New.first.str().c_str()); |
| 1048 | } |
| 1049 | |
| 1050 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_redefine_symbols)) |
| 1051 | if (Error E = addSymbolsToRenameFromFile(SymbolsToRename&: Config.SymbolsToRename, Alloc&: DC.Alloc, |
| 1052 | Filename: Arg->getValue())) |
| 1053 | return std::move(E); |
| 1054 | |
| 1055 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_rename_section)) { |
| 1056 | Expected<SectionRename> SR = |
| 1057 | parseRenameSectionValue(FlagValue: StringRef(Arg->getValue())); |
| 1058 | if (!SR) |
| 1059 | return SR.takeError(); |
| 1060 | if (!Config.SectionsToRename.try_emplace(Key: SR->OriginalName, Args&: *SR).second) |
| 1061 | return createStringError(EC: errc::invalid_argument, |
| 1062 | Fmt: "multiple renames of section '%s'" , |
| 1063 | Vals: SR->OriginalName.str().c_str()); |
| 1064 | } |
| 1065 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_set_section_alignment)) { |
| 1066 | Expected<std::pair<StringRef, uint64_t>> NameAndAlign = |
| 1067 | parseSetSectionAttribute(Option: "--set-section-alignment" , FlagValue: Arg->getValue()); |
| 1068 | if (!NameAndAlign) |
| 1069 | return NameAndAlign.takeError(); |
| 1070 | Config.SetSectionAlignment[NameAndAlign->first] = NameAndAlign->second; |
| 1071 | } |
| 1072 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_set_section_flags)) { |
| 1073 | Expected<SectionFlagsUpdate> SFU = |
| 1074 | parseSetSectionFlagValue(FlagValue: Arg->getValue()); |
| 1075 | if (!SFU) |
| 1076 | return SFU.takeError(); |
| 1077 | if (!Config.SetSectionFlags.try_emplace(Key: SFU->Name, Args&: *SFU).second) |
| 1078 | return createStringError( |
| 1079 | EC: errc::invalid_argument, |
| 1080 | Fmt: "--set-section-flags set multiple times for section '%s'" , |
| 1081 | Vals: SFU->Name.str().c_str()); |
| 1082 | } |
| 1083 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_set_section_type)) { |
| 1084 | Expected<std::pair<StringRef, uint64_t>> NameAndType = |
| 1085 | parseSetSectionAttribute(Option: "--set-section-type" , FlagValue: Arg->getValue()); |
| 1086 | if (!NameAndType) |
| 1087 | return NameAndType.takeError(); |
| 1088 | Config.SetSectionType[NameAndType->first] = NameAndType->second; |
| 1089 | } |
| 1090 | // Prohibit combinations of --set-section-{flags,type} when the section name |
| 1091 | // is used as the destination of a --rename-section. |
| 1092 | for (const auto &E : Config.SectionsToRename) { |
| 1093 | const SectionRename &SR = E.second; |
| 1094 | auto Err = [&](const char *Option) { |
| 1095 | return createStringError( |
| 1096 | EC: errc::invalid_argument, |
| 1097 | Fmt: "--set-section-%s=%s conflicts with --rename-section=%s=%s" , Vals: Option, |
| 1098 | Vals: SR.NewName.str().c_str(), Vals: SR.OriginalName.str().c_str(), |
| 1099 | Vals: SR.NewName.str().c_str()); |
| 1100 | }; |
| 1101 | if (Config.SetSectionFlags.count(Key: SR.NewName)) |
| 1102 | return Err("flags" ); |
| 1103 | if (Config.SetSectionType.count(Key: SR.NewName)) |
| 1104 | return Err("type" ); |
| 1105 | } |
| 1106 | |
| 1107 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_remove_section)) |
| 1108 | if (Error E = Config.ToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1109 | Pattern: Arg->getValue(), MS: SectionMatchStyle, ErrorCallback))) |
| 1110 | return std::move(E); |
| 1111 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_keep_section)) |
| 1112 | if (Error E = Config.KeepSection.addMatcher(Matcher: NameOrPattern::create( |
| 1113 | Pattern: Arg->getValue(), MS: SectionMatchStyle, ErrorCallback))) |
| 1114 | return std::move(E); |
| 1115 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_only_section)) |
| 1116 | if (Error E = Config.OnlySection.addMatcher(Matcher: NameOrPattern::create( |
| 1117 | Pattern: Arg->getValue(), MS: SectionMatchStyle, ErrorCallback))) |
| 1118 | return std::move(E); |
| 1119 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_add_section)) { |
| 1120 | if (Error Err = loadNewSectionData(ArgValue: Arg->getValue(), OptionName: "--add-section" , |
| 1121 | NewSections&: Config.AddSection)) |
| 1122 | return std::move(Err); |
| 1123 | } |
| 1124 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_update_section)) { |
| 1125 | if (Error Err = loadNewSectionData(ArgValue: Arg->getValue(), OptionName: "--update-section" , |
| 1126 | NewSections&: Config.UpdateSection)) |
| 1127 | return std::move(Err); |
| 1128 | } |
| 1129 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_dump_section)) { |
| 1130 | StringRef Value(Arg->getValue()); |
| 1131 | if (Value.split(Separator: '=').second.empty()) |
| 1132 | return createStringError( |
| 1133 | EC: errc::invalid_argument, |
| 1134 | S: "bad format for --dump-section, expected section=file" ); |
| 1135 | Config.DumpSection.push_back(Elt: Value); |
| 1136 | } |
| 1137 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_extract_section)) { |
| 1138 | StringRef Value(Arg->getValue()); |
| 1139 | if (Value.split(Separator: '=').second.empty()) |
| 1140 | return createStringError( |
| 1141 | EC: errc::invalid_argument, |
| 1142 | S: "bad format for --extract-section, expected section=file" ); |
| 1143 | Config.ExtractSection.push_back(Elt: Value); |
| 1144 | } |
| 1145 | Config.StripAll = InputArgs.hasArg(Ids: OBJCOPY_strip_all); |
| 1146 | Config.StripAllGNU = InputArgs.hasArg(Ids: OBJCOPY_strip_all_gnu); |
| 1147 | Config.StripDebug = InputArgs.hasArg(Ids: OBJCOPY_strip_debug); |
| 1148 | Config.StripDWO = InputArgs.hasArg(Ids: OBJCOPY_strip_dwo); |
| 1149 | Config.StripSections = InputArgs.hasArg(Ids: OBJCOPY_strip_sections); |
| 1150 | Config.StripNonAlloc = InputArgs.hasArg(Ids: OBJCOPY_strip_non_alloc); |
| 1151 | Config.StripUnneeded = InputArgs.hasArg(Ids: OBJCOPY_strip_unneeded); |
| 1152 | Config.ExtractDWO = InputArgs.hasArg(Ids: OBJCOPY_extract_dwo); |
| 1153 | Config.ExtractMainPartition = |
| 1154 | InputArgs.hasArg(Ids: OBJCOPY_extract_main_partition); |
| 1155 | ELFConfig.LocalizeHidden = InputArgs.hasArg(Ids: OBJCOPY_localize_hidden); |
| 1156 | Config.Verbose = InputArgs.hasArg(Ids: OBJCOPY_verbose); |
| 1157 | Config.Weaken = InputArgs.hasArg(Ids: OBJCOPY_weaken); |
| 1158 | if (auto *Arg = |
| 1159 | InputArgs.getLastArg(Ids: OBJCOPY_discard_all, Ids: OBJCOPY_discard_locals)) { |
| 1160 | Config.DiscardMode = Arg->getOption().matches(ID: OBJCOPY_discard_all) |
| 1161 | ? DiscardType::All |
| 1162 | : DiscardType::Locals; |
| 1163 | } |
| 1164 | |
| 1165 | ELFConfig.VerifyNoteSections = InputArgs.hasFlag( |
| 1166 | Pos: OBJCOPY_verify_note_sections, Neg: OBJCOPY_no_verify_note_sections, Default: true); |
| 1167 | |
| 1168 | Config.OnlyKeepDebug = InputArgs.hasArg(Ids: OBJCOPY_only_keep_debug); |
| 1169 | ELFConfig.KeepFileSymbols = InputArgs.hasArg(Ids: OBJCOPY_keep_file_symbols); |
| 1170 | MachOConfig.KeepUndefined = InputArgs.hasArg(Ids: OBJCOPY_keep_undefined); |
| 1171 | Config.DecompressDebugSections = |
| 1172 | InputArgs.hasArg(Ids: OBJCOPY_decompress_debug_sections); |
| 1173 | if (Config.DiscardMode == DiscardType::All) { |
| 1174 | Config.StripDebug = true; |
| 1175 | ELFConfig.KeepFileSymbols = true; |
| 1176 | } |
| 1177 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_localize_symbol)) |
| 1178 | if (Error E = Config.SymbolsToLocalize.addMatcher(Matcher: NameOrPattern::create( |
| 1179 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1180 | return std::move(E); |
| 1181 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_localize_symbols)) |
| 1182 | if (Error E = addSymbolsFromFile(Symbols&: Config.SymbolsToLocalize, Alloc&: DC.Alloc, |
| 1183 | Filename: Arg->getValue(), MS: SymbolMatchStyle, |
| 1184 | ErrorCallback)) |
| 1185 | return std::move(E); |
| 1186 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_keep_global_symbol)) |
| 1187 | if (Error E = Config.SymbolsToKeepGlobal.addMatcher(Matcher: NameOrPattern::create( |
| 1188 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1189 | return std::move(E); |
| 1190 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_keep_global_symbols)) |
| 1191 | if (Error E = addSymbolsFromFile(Symbols&: Config.SymbolsToKeepGlobal, Alloc&: DC.Alloc, |
| 1192 | Filename: Arg->getValue(), MS: SymbolMatchStyle, |
| 1193 | ErrorCallback)) |
| 1194 | return std::move(E); |
| 1195 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_globalize_symbol)) |
| 1196 | if (Error E = Config.SymbolsToGlobalize.addMatcher(Matcher: NameOrPattern::create( |
| 1197 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1198 | return std::move(E); |
| 1199 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_globalize_symbols)) |
| 1200 | if (Error E = addSymbolsFromFile(Symbols&: Config.SymbolsToGlobalize, Alloc&: DC.Alloc, |
| 1201 | Filename: Arg->getValue(), MS: SymbolMatchStyle, |
| 1202 | ErrorCallback)) |
| 1203 | return std::move(E); |
| 1204 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_weaken_symbol)) |
| 1205 | if (Error E = Config.SymbolsToWeaken.addMatcher(Matcher: NameOrPattern::create( |
| 1206 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1207 | return std::move(E); |
| 1208 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_weaken_symbols)) |
| 1209 | if (Error E = addSymbolsFromFile(Symbols&: Config.SymbolsToWeaken, Alloc&: DC.Alloc, |
| 1210 | Filename: Arg->getValue(), MS: SymbolMatchStyle, |
| 1211 | ErrorCallback)) |
| 1212 | return std::move(E); |
| 1213 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_strip_symbol)) |
| 1214 | if (Error E = Config.SymbolsToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1215 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1216 | return std::move(E); |
| 1217 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_strip_symbols)) |
| 1218 | if (Error E = addSymbolsFromFile(Symbols&: Config.SymbolsToRemove, Alloc&: DC.Alloc, |
| 1219 | Filename: Arg->getValue(), MS: SymbolMatchStyle, |
| 1220 | ErrorCallback)) |
| 1221 | return std::move(E); |
| 1222 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_strip_unneeded_symbol)) |
| 1223 | if (Error E = |
| 1224 | Config.UnneededSymbolsToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1225 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1226 | return std::move(E); |
| 1227 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_strip_unneeded_symbols)) |
| 1228 | if (Error E = addSymbolsFromFile(Symbols&: Config.UnneededSymbolsToRemove, Alloc&: DC.Alloc, |
| 1229 | Filename: Arg->getValue(), MS: SymbolMatchStyle, |
| 1230 | ErrorCallback)) |
| 1231 | return std::move(E); |
| 1232 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_keep_symbol)) |
| 1233 | if (Error E = Config.SymbolsToKeep.addMatcher(Matcher: NameOrPattern::create( |
| 1234 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1235 | return std::move(E); |
| 1236 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_keep_symbols)) |
| 1237 | if (Error E = |
| 1238 | addSymbolsFromFile(Symbols&: Config.SymbolsToKeep, Alloc&: DC.Alloc, Filename: Arg->getValue(), |
| 1239 | MS: SymbolMatchStyle, ErrorCallback)) |
| 1240 | return std::move(E); |
| 1241 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_skip_symbol)) |
| 1242 | if (Error E = Config.SymbolsToSkip.addMatcher(Matcher: NameOrPattern::create( |
| 1243 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1244 | return std::move(E); |
| 1245 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_skip_symbols)) |
| 1246 | if (Error E = |
| 1247 | addSymbolsFromFile(Symbols&: Config.SymbolsToSkip, Alloc&: DC.Alloc, Filename: Arg->getValue(), |
| 1248 | MS: SymbolMatchStyle, ErrorCallback)) |
| 1249 | return std::move(E); |
| 1250 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_add_symbol)) { |
| 1251 | Expected<NewSymbolInfo> SymInfo = parseNewSymbolInfo(FlagValue: Arg->getValue()); |
| 1252 | if (!SymInfo) |
| 1253 | return SymInfo.takeError(); |
| 1254 | |
| 1255 | Config.SymbolsToAdd.push_back(Elt: *SymInfo); |
| 1256 | } |
| 1257 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_set_symbol_visibility)) { |
| 1258 | if (!StringRef(Arg->getValue()).contains(C: '=')) |
| 1259 | return createStringError(EC: errc::invalid_argument, |
| 1260 | S: "bad format for --set-symbol-visibility" ); |
| 1261 | auto [Sym, Visibility] = StringRef(Arg->getValue()).split(Separator: '='); |
| 1262 | Expected<uint8_t> Type = parseVisibilityType(VisType: Visibility); |
| 1263 | if (!Type) |
| 1264 | return Type.takeError(); |
| 1265 | ELFConfig.SymbolsToSetVisibility.emplace_back(args: NameMatcher(), args&: *Type); |
| 1266 | if (Error E = ELFConfig.SymbolsToSetVisibility.back().first.addMatcher( |
| 1267 | Matcher: NameOrPattern::create(Pattern: Sym, MS: SymbolMatchStyle, ErrorCallback))) |
| 1268 | return std::move(E); |
| 1269 | } |
| 1270 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_set_symbols_visibility)) { |
| 1271 | if (!StringRef(Arg->getValue()).contains(C: '=')) |
| 1272 | return createStringError(EC: errc::invalid_argument, |
| 1273 | S: "bad format for --set-symbols-visibility" ); |
| 1274 | auto [File, Visibility] = StringRef(Arg->getValue()).split(Separator: '='); |
| 1275 | Expected<uint8_t> Type = parseVisibilityType(VisType: Visibility); |
| 1276 | if (!Type) |
| 1277 | return Type.takeError(); |
| 1278 | ELFConfig.SymbolsToSetVisibility.emplace_back(args: NameMatcher(), args&: *Type); |
| 1279 | if (Error E = |
| 1280 | addSymbolsFromFile(Symbols&: ELFConfig.SymbolsToSetVisibility.back().first, |
| 1281 | Alloc&: DC.Alloc, Filename: File, MS: SymbolMatchStyle, ErrorCallback)) |
| 1282 | return std::move(E); |
| 1283 | } |
| 1284 | |
| 1285 | ELFConfig.AllowBrokenLinks = InputArgs.hasArg(Ids: OBJCOPY_allow_broken_links); |
| 1286 | |
| 1287 | Config.DeterministicArchives = InputArgs.hasFlag( |
| 1288 | Pos: OBJCOPY_enable_deterministic_archives, |
| 1289 | Neg: OBJCOPY_disable_deterministic_archives, /*default=*/Default: true); |
| 1290 | |
| 1291 | Config.PreserveDates = InputArgs.hasArg(Ids: OBJCOPY_preserve_dates); |
| 1292 | |
| 1293 | if (Config.PreserveDates && |
| 1294 | (Config.OutputFilename == "-" || Config.InputFilename == "-" )) |
| 1295 | return createStringError(EC: errc::invalid_argument, |
| 1296 | S: "--preserve-dates requires a file" ); |
| 1297 | |
| 1298 | for (auto *Arg : InputArgs) |
| 1299 | if (Arg->getOption().matches(ID: OBJCOPY_set_start)) { |
| 1300 | auto EAddr = getAsInteger<uint64_t>(Val: Arg->getValue()); |
| 1301 | if (!EAddr) |
| 1302 | return createStringError( |
| 1303 | EC: EAddr.getError(), Fmt: "bad entry point address: '%s'" , Vals: Arg->getValue()); |
| 1304 | |
| 1305 | ELFConfig.EntryExpr = [EAddr](uint64_t) { return *EAddr; }; |
| 1306 | } else if (Arg->getOption().matches(ID: OBJCOPY_change_start)) { |
| 1307 | auto EIncr = getAsInteger<int64_t>(Val: Arg->getValue()); |
| 1308 | if (!EIncr) |
| 1309 | return createStringError(EC: EIncr.getError(), |
| 1310 | Fmt: "bad entry point increment: '%s'" , |
| 1311 | Vals: Arg->getValue()); |
| 1312 | auto Expr = ELFConfig.EntryExpr ? std::move(ELFConfig.EntryExpr) |
| 1313 | : [](uint64_t A) { return A; }; |
| 1314 | ELFConfig.EntryExpr = [Expr, EIncr](uint64_t EAddr) { |
| 1315 | return Expr(EAddr) + *EIncr; |
| 1316 | }; |
| 1317 | } |
| 1318 | |
| 1319 | for (auto *Arg : InputArgs.filtered(Ids: OBJCOPY_remove_note)) { |
| 1320 | Expected<RemoveNoteInfo> NoteInfo = parseRemoveNoteInfo(FlagValue: Arg->getValue()); |
| 1321 | if (!NoteInfo) |
| 1322 | return NoteInfo.takeError(); |
| 1323 | |
| 1324 | ELFConfig.NotesToRemove.push_back(Elt: *NoteInfo); |
| 1325 | } |
| 1326 | |
| 1327 | if (!ELFConfig.NotesToRemove.empty()) { |
| 1328 | if (!Config.ToRemove.empty()) |
| 1329 | return createStringError( |
| 1330 | EC: errc::invalid_argument, |
| 1331 | S: "cannot specify both --remove-note and --remove-section" ); |
| 1332 | if (!Config.AddSection.empty()) |
| 1333 | return createStringError( |
| 1334 | EC: errc::invalid_argument, |
| 1335 | S: "cannot specify both --remove-note and --add-section" ); |
| 1336 | if (!Config.UpdateSection.empty()) |
| 1337 | return createStringError( |
| 1338 | EC: errc::invalid_argument, |
| 1339 | S: "cannot specify both --remove-note and --update-section" ); |
| 1340 | } |
| 1341 | |
| 1342 | if (Config.DecompressDebugSections && |
| 1343 | Config.CompressionType != DebugCompressionType::None) { |
| 1344 | return createStringError( |
| 1345 | EC: errc::invalid_argument, |
| 1346 | S: "cannot specify both --compress-debug-sections and " |
| 1347 | "--decompress-debug-sections" ); |
| 1348 | } |
| 1349 | |
| 1350 | if (Config.ExtractPartition && Config.ExtractMainPartition) |
| 1351 | return createStringError(EC: errc::invalid_argument, |
| 1352 | S: "cannot specify --extract-partition together with " |
| 1353 | "--extract-main-partition" ); |
| 1354 | |
| 1355 | DC.CopyConfigs.push_back(Elt: std::move(ConfigMgr)); |
| 1356 | return std::move(DC); |
| 1357 | } |
| 1358 | |
| 1359 | // parseInstallNameToolOptions returns the config and sets the input arguments. |
| 1360 | // If a help flag is set then parseInstallNameToolOptions will print the help |
| 1361 | // messege and exit. |
| 1362 | Expected<DriverConfig> |
| 1363 | objcopy::parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) { |
| 1364 | DriverConfig DC; |
| 1365 | ConfigManager ConfigMgr; |
| 1366 | CommonConfig &Config = ConfigMgr.Common; |
| 1367 | MachOConfig &MachOConfig = ConfigMgr.MachO; |
| 1368 | InstallNameToolOptTable T; |
| 1369 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 1370 | llvm::opt::InputArgList InputArgs = |
| 1371 | T.ParseArgs(Args: ArgsArr, MissingArgIndex&: MissingArgumentIndex, MissingArgCount&: MissingArgumentCount); |
| 1372 | |
| 1373 | if (MissingArgumentCount) |
| 1374 | return createStringError( |
| 1375 | EC: errc::invalid_argument, |
| 1376 | S: "missing argument to " + |
| 1377 | StringRef(InputArgs.getArgString(Index: MissingArgumentIndex)) + |
| 1378 | " option" ); |
| 1379 | |
| 1380 | if (InputArgs.size() == 0) { |
| 1381 | printHelp(OptTable: T, OS&: errs(), Tool: ToolType::InstallNameTool); |
| 1382 | exit(status: 1); |
| 1383 | } |
| 1384 | |
| 1385 | if (InputArgs.hasArg(Ids: INSTALL_NAME_TOOL_help)) { |
| 1386 | printHelp(OptTable: T, OS&: outs(), Tool: ToolType::InstallNameTool); |
| 1387 | exit(status: 0); |
| 1388 | } |
| 1389 | |
| 1390 | if (InputArgs.hasArg(Ids: INSTALL_NAME_TOOL_version)) { |
| 1391 | outs() << "llvm-install-name-tool, compatible with cctools " |
| 1392 | "install_name_tool\n" ; |
| 1393 | cl::PrintVersionMessage(); |
| 1394 | exit(status: 0); |
| 1395 | } |
| 1396 | |
| 1397 | for (auto *Arg : InputArgs.filtered(Ids: INSTALL_NAME_TOOL_add_rpath)) |
| 1398 | MachOConfig.RPathToAdd.push_back(x: Arg->getValue()); |
| 1399 | |
| 1400 | for (auto *Arg : InputArgs.filtered(Ids: INSTALL_NAME_TOOL_prepend_rpath)) |
| 1401 | MachOConfig.RPathToPrepend.push_back(x: Arg->getValue()); |
| 1402 | |
| 1403 | for (auto *Arg : InputArgs.filtered(Ids: INSTALL_NAME_TOOL_delete_rpath)) { |
| 1404 | StringRef RPath = Arg->getValue(); |
| 1405 | |
| 1406 | // Cannot add and delete the same rpath at the same time. |
| 1407 | if (is_contained(Range&: MachOConfig.RPathToAdd, Element: RPath)) |
| 1408 | return createStringError( |
| 1409 | EC: errc::invalid_argument, |
| 1410 | Fmt: "cannot specify both -add_rpath '%s' and -delete_rpath '%s'" , |
| 1411 | Vals: RPath.str().c_str(), Vals: RPath.str().c_str()); |
| 1412 | if (is_contained(Range&: MachOConfig.RPathToPrepend, Element: RPath)) |
| 1413 | return createStringError( |
| 1414 | EC: errc::invalid_argument, |
| 1415 | Fmt: "cannot specify both -prepend_rpath '%s' and -delete_rpath '%s'" , |
| 1416 | Vals: RPath.str().c_str(), Vals: RPath.str().c_str()); |
| 1417 | |
| 1418 | MachOConfig.RPathsToRemove.insert(V: RPath); |
| 1419 | } |
| 1420 | |
| 1421 | for (auto *Arg : InputArgs.filtered(Ids: INSTALL_NAME_TOOL_rpath)) { |
| 1422 | StringRef Old = Arg->getValue(N: 0); |
| 1423 | StringRef New = Arg->getValue(N: 1); |
| 1424 | |
| 1425 | auto Match = [=](StringRef RPath) { return RPath == Old || RPath == New; }; |
| 1426 | |
| 1427 | // Cannot specify duplicate -rpath entries |
| 1428 | auto It1 = find_if( |
| 1429 | Range&: MachOConfig.RPathsToUpdate, |
| 1430 | P: [&Match](const DenseMap<StringRef, StringRef>::value_type &OldNew) { |
| 1431 | return Match(OldNew.getFirst()) || Match(OldNew.getSecond()); |
| 1432 | }); |
| 1433 | if (It1 != MachOConfig.RPathsToUpdate.end()) |
| 1434 | return createStringError(EC: errc::invalid_argument, |
| 1435 | S: "cannot specify both -rpath '" + |
| 1436 | It1->getFirst() + "' '" + It1->getSecond() + |
| 1437 | "' and -rpath '" + Old + "' '" + New + "'" ); |
| 1438 | |
| 1439 | // Cannot specify the same rpath under both -delete_rpath and -rpath |
| 1440 | auto It2 = find_if(Range&: MachOConfig.RPathsToRemove, P: Match); |
| 1441 | if (It2 != MachOConfig.RPathsToRemove.end()) |
| 1442 | return createStringError(EC: errc::invalid_argument, |
| 1443 | S: "cannot specify both -delete_rpath '" + *It2 + |
| 1444 | "' and -rpath '" + Old + "' '" + New + "'" ); |
| 1445 | |
| 1446 | // Cannot specify the same rpath under both -add_rpath and -rpath |
| 1447 | auto It3 = find_if(Range&: MachOConfig.RPathToAdd, P: Match); |
| 1448 | if (It3 != MachOConfig.RPathToAdd.end()) |
| 1449 | return createStringError(EC: errc::invalid_argument, |
| 1450 | S: "cannot specify both -add_rpath '" + *It3 + |
| 1451 | "' and -rpath '" + Old + "' '" + New + "'" ); |
| 1452 | |
| 1453 | // Cannot specify the same rpath under both -prepend_rpath and -rpath. |
| 1454 | auto It4 = find_if(Range&: MachOConfig.RPathToPrepend, P: Match); |
| 1455 | if (It4 != MachOConfig.RPathToPrepend.end()) |
| 1456 | return createStringError(EC: errc::invalid_argument, |
| 1457 | S: "cannot specify both -prepend_rpath '" + *It4 + |
| 1458 | "' and -rpath '" + Old + "' '" + New + "'" ); |
| 1459 | |
| 1460 | MachOConfig.RPathsToUpdate.insert(KV: {Old, New}); |
| 1461 | } |
| 1462 | |
| 1463 | if (auto *Arg = InputArgs.getLastArg(Ids: INSTALL_NAME_TOOL_id)) { |
| 1464 | MachOConfig.SharedLibId = Arg->getValue(); |
| 1465 | if (MachOConfig.SharedLibId->empty()) |
| 1466 | return createStringError(EC: errc::invalid_argument, |
| 1467 | S: "cannot specify an empty id" ); |
| 1468 | } |
| 1469 | |
| 1470 | for (auto *Arg : InputArgs.filtered(Ids: INSTALL_NAME_TOOL_change)) |
| 1471 | MachOConfig.InstallNamesToUpdate.insert( |
| 1472 | KV: {Arg->getValue(N: 0), Arg->getValue(N: 1)}); |
| 1473 | |
| 1474 | MachOConfig.RemoveAllRpaths = |
| 1475 | InputArgs.hasArg(Ids: INSTALL_NAME_TOOL_delete_all_rpaths); |
| 1476 | |
| 1477 | SmallVector<StringRef, 2> Positional; |
| 1478 | for (auto *Arg : InputArgs.filtered(Ids: INSTALL_NAME_TOOL_UNKNOWN)) |
| 1479 | return createStringError(EC: errc::invalid_argument, Fmt: "unknown argument '%s'" , |
| 1480 | Vals: Arg->getAsString(Args: InputArgs).c_str()); |
| 1481 | for (auto *Arg : InputArgs.filtered(Ids: INSTALL_NAME_TOOL_INPUT)) |
| 1482 | Positional.push_back(Elt: Arg->getValue()); |
| 1483 | if (Positional.empty()) |
| 1484 | return createStringError(EC: errc::invalid_argument, S: "no input file specified" ); |
| 1485 | if (Positional.size() > 1) |
| 1486 | return createStringError( |
| 1487 | EC: errc::invalid_argument, |
| 1488 | S: "llvm-install-name-tool expects a single input file" ); |
| 1489 | Config.InputFilename = Positional[0]; |
| 1490 | Config.OutputFilename = |
| 1491 | InputArgs.getLastArgValue(Id: INSTALL_NAME_TOOL_output, Default: Positional[0]); |
| 1492 | |
| 1493 | Expected<OwningBinary<Binary>> BinaryOrErr = |
| 1494 | createBinary(Path: Config.InputFilename); |
| 1495 | if (!BinaryOrErr) |
| 1496 | return createFileError(F: Config.InputFilename, E: BinaryOrErr.takeError()); |
| 1497 | auto *Binary = (*BinaryOrErr).getBinary(); |
| 1498 | if (!Binary->isMachO() && !Binary->isMachOUniversalBinary()) |
| 1499 | return createStringError(EC: errc::invalid_argument, |
| 1500 | Fmt: "input file: %s is not a Mach-O file" , |
| 1501 | Vals: Config.InputFilename.str().c_str()); |
| 1502 | |
| 1503 | DC.CopyConfigs.push_back(Elt: std::move(ConfigMgr)); |
| 1504 | return std::move(DC); |
| 1505 | } |
| 1506 | |
| 1507 | Expected<DriverConfig> |
| 1508 | objcopy::parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr, |
| 1509 | function_ref<Error(Error)> ErrorCallback) { |
| 1510 | DriverConfig DC; |
| 1511 | ConfigManager ConfigMgr; |
| 1512 | CommonConfig &Config = ConfigMgr.Common; |
| 1513 | MachOConfig &MachOConfig = ConfigMgr.MachO; |
| 1514 | BitcodeStripOptTable T; |
| 1515 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 1516 | opt::InputArgList InputArgs = |
| 1517 | T.ParseArgs(Args: ArgsArr, MissingArgIndex&: MissingArgumentIndex, MissingArgCount&: MissingArgumentCount); |
| 1518 | |
| 1519 | if (InputArgs.size() == 0) { |
| 1520 | printHelp(OptTable: T, OS&: errs(), Tool: ToolType::BitcodeStrip); |
| 1521 | exit(status: 1); |
| 1522 | } |
| 1523 | |
| 1524 | if (InputArgs.hasArg(Ids: BITCODE_STRIP_help)) { |
| 1525 | printHelp(OptTable: T, OS&: outs(), Tool: ToolType::BitcodeStrip); |
| 1526 | exit(status: 0); |
| 1527 | } |
| 1528 | |
| 1529 | if (InputArgs.hasArg(Ids: BITCODE_STRIP_version)) { |
| 1530 | outs() << "llvm-bitcode-strip, compatible with cctools " |
| 1531 | "bitcode_strip\n" ; |
| 1532 | cl::PrintVersionMessage(); |
| 1533 | exit(status: 0); |
| 1534 | } |
| 1535 | |
| 1536 | for (auto *Arg : InputArgs.filtered(Ids: BITCODE_STRIP_UNKNOWN)) |
| 1537 | return createStringError(EC: errc::invalid_argument, Fmt: "unknown argument '%s'" , |
| 1538 | Vals: Arg->getAsString(Args: InputArgs).c_str()); |
| 1539 | |
| 1540 | SmallVector<StringRef, 2> Positional; |
| 1541 | for (auto *Arg : InputArgs.filtered(Ids: BITCODE_STRIP_INPUT)) |
| 1542 | Positional.push_back(Elt: Arg->getValue()); |
| 1543 | if (Positional.size() > 1) |
| 1544 | return createStringError(EC: errc::invalid_argument, |
| 1545 | S: "llvm-bitcode-strip expects a single input file" ); |
| 1546 | assert(!Positional.empty()); |
| 1547 | Config.InputFilename = Positional[0]; |
| 1548 | |
| 1549 | if (!InputArgs.hasArg(Ids: BITCODE_STRIP_output)) { |
| 1550 | return createStringError(EC: errc::invalid_argument, |
| 1551 | S: "-o is a required argument" ); |
| 1552 | } |
| 1553 | Config.OutputFilename = InputArgs.getLastArgValue(Id: BITCODE_STRIP_output); |
| 1554 | |
| 1555 | if (!InputArgs.hasArg(Ids: BITCODE_STRIP_remove)) |
| 1556 | return createStringError(EC: errc::invalid_argument, S: "no action specified" ); |
| 1557 | |
| 1558 | // We only support -r for now, which removes all bitcode sections and |
| 1559 | // the __LLVM segment if it's now empty. |
| 1560 | cantFail(Err: Config.ToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1561 | Pattern: "__LLVM,__asm" , MS: MatchStyle::Literal, ErrorCallback))); |
| 1562 | cantFail(Err: Config.ToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1563 | Pattern: "__LLVM,__bitcode" , MS: MatchStyle::Literal, ErrorCallback))); |
| 1564 | cantFail(Err: Config.ToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1565 | Pattern: "__LLVM,__bundle" , MS: MatchStyle::Literal, ErrorCallback))); |
| 1566 | cantFail(Err: Config.ToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1567 | Pattern: "__LLVM,__cmdline" , MS: MatchStyle::Literal, ErrorCallback))); |
| 1568 | cantFail(Err: Config.ToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1569 | Pattern: "__LLVM,__swift_cmdline" , MS: MatchStyle::Literal, ErrorCallback))); |
| 1570 | MachOConfig.EmptySegmentsToRemove.insert(V: "__LLVM" ); |
| 1571 | |
| 1572 | DC.CopyConfigs.push_back(Elt: std::move(ConfigMgr)); |
| 1573 | return std::move(DC); |
| 1574 | } |
| 1575 | |
| 1576 | // parseStripOptions returns the config and sets the input arguments. If a |
| 1577 | // help flag is set then parseStripOptions will print the help messege and |
| 1578 | // exit. |
| 1579 | Expected<DriverConfig> |
| 1580 | objcopy::parseStripOptions(ArrayRef<const char *> RawArgsArr, |
| 1581 | function_ref<Error(Error)> ErrorCallback) { |
| 1582 | const char *const *DashDash = |
| 1583 | llvm::find_if(Range&: RawArgsArr, P: [](StringRef Str) { return Str == "--" ; }); |
| 1584 | ArrayRef<const char *> ArgsArr = ArrayRef(RawArgsArr.begin(), DashDash); |
| 1585 | if (DashDash != RawArgsArr.end()) |
| 1586 | DashDash = std::next(x: DashDash); |
| 1587 | |
| 1588 | StripOptTable T; |
| 1589 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 1590 | llvm::opt::InputArgList InputArgs = |
| 1591 | T.ParseArgs(Args: ArgsArr, MissingArgIndex&: MissingArgumentIndex, MissingArgCount&: MissingArgumentCount); |
| 1592 | |
| 1593 | if (InputArgs.size() == 0 && DashDash == RawArgsArr.end()) { |
| 1594 | printHelp(OptTable: T, OS&: errs(), Tool: ToolType::Strip); |
| 1595 | exit(status: 1); |
| 1596 | } |
| 1597 | |
| 1598 | if (InputArgs.hasArg(Ids: STRIP_help)) { |
| 1599 | printHelp(OptTable: T, OS&: outs(), Tool: ToolType::Strip); |
| 1600 | exit(status: 0); |
| 1601 | } |
| 1602 | |
| 1603 | if (InputArgs.hasArg(Ids: STRIP_version)) { |
| 1604 | outs() << "llvm-strip, compatible with GNU strip\n" ; |
| 1605 | cl::PrintVersionMessage(); |
| 1606 | exit(status: 0); |
| 1607 | } |
| 1608 | |
| 1609 | SmallVector<StringRef, 2> Positional; |
| 1610 | for (auto *Arg : InputArgs.filtered(Ids: STRIP_UNKNOWN)) |
| 1611 | return createStringError(EC: errc::invalid_argument, Fmt: "unknown argument '%s'" , |
| 1612 | Vals: Arg->getAsString(Args: InputArgs).c_str()); |
| 1613 | for (auto *Arg : InputArgs.filtered(Ids: STRIP_INPUT)) |
| 1614 | Positional.push_back(Elt: Arg->getValue()); |
| 1615 | std::copy(first: DashDash, last: RawArgsArr.end(), result: std::back_inserter(x&: Positional)); |
| 1616 | |
| 1617 | if (Positional.empty()) |
| 1618 | return createStringError(EC: errc::invalid_argument, S: "no input file specified" ); |
| 1619 | |
| 1620 | if (Positional.size() > 1 && InputArgs.hasArg(Ids: STRIP_output)) |
| 1621 | return createStringError( |
| 1622 | EC: errc::invalid_argument, |
| 1623 | S: "multiple input files cannot be used in combination with -o" ); |
| 1624 | |
| 1625 | ConfigManager ConfigMgr; |
| 1626 | CommonConfig &Config = ConfigMgr.Common; |
| 1627 | ELFConfig &ELFConfig = ConfigMgr.ELF; |
| 1628 | MachOConfig &MachOConfig = ConfigMgr.MachO; |
| 1629 | |
| 1630 | if (InputArgs.hasArg(Ids: STRIP_regex) && InputArgs.hasArg(Ids: STRIP_wildcard)) |
| 1631 | return createStringError(EC: errc::invalid_argument, |
| 1632 | S: "--regex and --wildcard are incompatible" ); |
| 1633 | MatchStyle SectionMatchStyle = |
| 1634 | InputArgs.hasArg(Ids: STRIP_regex) ? MatchStyle::Regex : MatchStyle::Wildcard; |
| 1635 | MatchStyle SymbolMatchStyle |
| 1636 | = InputArgs.hasArg(Ids: STRIP_regex) ? MatchStyle::Regex |
| 1637 | : InputArgs.hasArg(Ids: STRIP_wildcard) ? MatchStyle::Wildcard |
| 1638 | : MatchStyle::Literal; |
| 1639 | ELFConfig.AllowBrokenLinks = InputArgs.hasArg(Ids: STRIP_allow_broken_links); |
| 1640 | Config.StripDebug = InputArgs.hasArg(Ids: STRIP_strip_debug); |
| 1641 | |
| 1642 | if (auto *Arg = InputArgs.getLastArg(Ids: STRIP_discard_all, Ids: STRIP_discard_locals)) |
| 1643 | Config.DiscardMode = Arg->getOption().matches(ID: STRIP_discard_all) |
| 1644 | ? DiscardType::All |
| 1645 | : DiscardType::Locals; |
| 1646 | Config.StripSections = InputArgs.hasArg(Ids: STRIP_strip_sections); |
| 1647 | Config.StripUnneeded = InputArgs.hasArg(Ids: STRIP_strip_unneeded); |
| 1648 | if (auto Arg = InputArgs.getLastArg(Ids: STRIP_strip_all, Ids: STRIP_no_strip_all)) |
| 1649 | Config.StripAll = Arg->getOption().getID() == STRIP_strip_all; |
| 1650 | Config.StripAllGNU = InputArgs.hasArg(Ids: STRIP_strip_all_gnu); |
| 1651 | MachOConfig.StripSwiftSymbols = InputArgs.hasArg(Ids: STRIP_strip_swift_symbols); |
| 1652 | Config.OnlyKeepDebug = InputArgs.hasArg(Ids: STRIP_only_keep_debug); |
| 1653 | ELFConfig.KeepFileSymbols = InputArgs.hasArg(Ids: STRIP_keep_file_symbols); |
| 1654 | MachOConfig.KeepUndefined = InputArgs.hasArg(Ids: STRIP_keep_undefined); |
| 1655 | |
| 1656 | for (auto *Arg : InputArgs.filtered(Ids: STRIP_keep_section)) |
| 1657 | if (Error E = Config.KeepSection.addMatcher(Matcher: NameOrPattern::create( |
| 1658 | Pattern: Arg->getValue(), MS: SectionMatchStyle, ErrorCallback))) |
| 1659 | return std::move(E); |
| 1660 | |
| 1661 | for (auto *Arg : InputArgs.filtered(Ids: STRIP_remove_section)) |
| 1662 | if (Error E = Config.ToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1663 | Pattern: Arg->getValue(), MS: SectionMatchStyle, ErrorCallback))) |
| 1664 | return std::move(E); |
| 1665 | |
| 1666 | for (auto *Arg : InputArgs.filtered(Ids: STRIP_strip_symbol)) |
| 1667 | if (Error E = Config.SymbolsToRemove.addMatcher(Matcher: NameOrPattern::create( |
| 1668 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1669 | return std::move(E); |
| 1670 | |
| 1671 | for (auto *Arg : InputArgs.filtered(Ids: STRIP_keep_symbol)) |
| 1672 | if (Error E = Config.SymbolsToKeep.addMatcher(Matcher: NameOrPattern::create( |
| 1673 | Pattern: Arg->getValue(), MS: SymbolMatchStyle, ErrorCallback))) |
| 1674 | return std::move(E); |
| 1675 | |
| 1676 | if (!InputArgs.hasArg(Ids: STRIP_no_strip_all) && !Config.StripDebug && |
| 1677 | !Config.OnlyKeepDebug && !Config.StripUnneeded && |
| 1678 | Config.DiscardMode == DiscardType::None && !Config.StripAllGNU && |
| 1679 | Config.SymbolsToRemove.empty()) |
| 1680 | Config.StripAll = true; |
| 1681 | |
| 1682 | if (Config.DiscardMode == DiscardType::All) { |
| 1683 | Config.StripDebug = true; |
| 1684 | ELFConfig.KeepFileSymbols = true; |
| 1685 | } |
| 1686 | |
| 1687 | Config.DeterministicArchives = |
| 1688 | InputArgs.hasFlag(Pos: STRIP_enable_deterministic_archives, |
| 1689 | Neg: STRIP_disable_deterministic_archives, /*default=*/Default: true); |
| 1690 | |
| 1691 | Config.PreserveDates = InputArgs.hasArg(Ids: STRIP_preserve_dates); |
| 1692 | Config.Verbose = InputArgs.hasArg(Ids: STRIP_verbose); |
| 1693 | Config.InputFormat = FileFormat::Unspecified; |
| 1694 | Config.OutputFormat = FileFormat::Unspecified; |
| 1695 | |
| 1696 | DriverConfig DC; |
| 1697 | if (Positional.size() == 1) { |
| 1698 | Config.InputFilename = Positional[0]; |
| 1699 | Config.OutputFilename = |
| 1700 | InputArgs.getLastArgValue(Id: STRIP_output, Default: Positional[0]); |
| 1701 | DC.CopyConfigs.push_back(Elt: std::move(ConfigMgr)); |
| 1702 | } else { |
| 1703 | StringMap<unsigned> InputFiles; |
| 1704 | for (StringRef Filename : Positional) { |
| 1705 | if (InputFiles[Filename]++ == 1) { |
| 1706 | if (Filename == "-" ) |
| 1707 | return createStringError( |
| 1708 | EC: errc::invalid_argument, |
| 1709 | S: "cannot specify '-' as an input file more than once" ); |
| 1710 | if (Error E = ErrorCallback(createStringError( |
| 1711 | EC: errc::invalid_argument, Fmt: "'%s' was already specified" , |
| 1712 | Vals: Filename.str().c_str()))) |
| 1713 | return std::move(E); |
| 1714 | } |
| 1715 | Config.InputFilename = Filename; |
| 1716 | Config.OutputFilename = Filename; |
| 1717 | DC.CopyConfigs.push_back(Elt: ConfigMgr); |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | if (Config.PreserveDates && (is_contained(Range&: Positional, Element: "-" ) || |
| 1722 | InputArgs.getLastArgValue(Id: STRIP_output) == "-" )) |
| 1723 | return createStringError(EC: errc::invalid_argument, |
| 1724 | S: "--preserve-dates requires a file" ); |
| 1725 | |
| 1726 | return std::move(DC); |
| 1727 | } |
| 1728 | |
| 1729 | Error llvm::objcopy::( |
| 1730 | const SmallVectorImpl<StringRef> &Args) { |
| 1731 | for (StringRef Input : Args) |
| 1732 | if (Error Err = object::extractOffloadBundleByURI(URIstr: Input)) |
| 1733 | return Err; |
| 1734 | |
| 1735 | return Error::success(); |
| 1736 | } |
| 1737 | |
| 1738 | Expected<SmallVector<StringRef>> |
| 1739 | objcopy::(ArrayRef<const char *> ArgsArr) { |
| 1740 | ExtractBundleEntryOptTable T; |
| 1741 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 1742 | opt::InputArgList InputArgs = |
| 1743 | T.ParseArgs(Args: ArgsArr, MissingArgIndex&: MissingArgumentIndex, MissingArgCount&: MissingArgumentCount); |
| 1744 | |
| 1745 | if (InputArgs.size() == 0) { |
| 1746 | printHelp(OptTable: T, OS&: errs(), Tool: ToolType::ExtractBundleEntry); |
| 1747 | exit(status: 1); |
| 1748 | } |
| 1749 | |
| 1750 | if (InputArgs.hasArg(Ids: EXTRACT_BUNDLE_ENTRY_help)) { |
| 1751 | printHelp(OptTable: T, OS&: outs(), Tool: ToolType::ExtractBundleEntry); |
| 1752 | exit(status: 0); |
| 1753 | } |
| 1754 | |
| 1755 | if (InputArgs.hasArg(Ids: EXTRACT_BUNDLE_ENTRY_version)) { |
| 1756 | outs() << "llvm-extract-bundle-entry\n" ; |
| 1757 | cl::PrintVersionMessage(); |
| 1758 | exit(status: 0); |
| 1759 | } |
| 1760 | |
| 1761 | for (auto *Arg : InputArgs.filtered(Ids: EXTRACT_BUNDLE_ENTRY_UNKNOWN)) |
| 1762 | return createStringError(EC: errc::invalid_argument, Fmt: "unknown argument '%s'" , |
| 1763 | Vals: Arg->getAsString(Args: InputArgs).c_str()); |
| 1764 | |
| 1765 | SmallVector<StringRef> Arguments; |
| 1766 | |
| 1767 | for (auto *Arg : InputArgs.filtered(Ids: EXTRACT_BUNDLE_ENTRY_INPUT)) |
| 1768 | Arguments.push_back(Elt: Arg->getValue()); |
| 1769 | assert(!Arguments.empty()); |
| 1770 | |
| 1771 | return Arguments; |
| 1772 | } |
| 1773 | |