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" |
Simon Pilgrim | 090cf45 | 2020-05-17 18:51:21 +0100 | [diff] [blame] | 10 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Optional.h" |
| 12 | #include "llvm/ADT/SmallVector.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringSet.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 15 | #include "llvm/Option/Arg.h" |
| 16 | #include "llvm/Option/ArgList.h" |
Hans Wennborg | 1e1e3ba | 2019-10-09 09:06:30 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CRC.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "llvm/Support/Compression.h" |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Errc.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 22 | #include "llvm/Support/StringSaver.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 23 | #include <memory> |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 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 | |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 66 | enum InstallNameToolID { |
| 67 | INSTALL_NAME_TOOL_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 | INSTALL_NAME_TOOL_##ID, |
| 71 | #include "InstallNameToolOpts.inc" |
| 72 | #undef OPTION |
| 73 | }; |
| 74 | |
| 75 | #define PREFIX(NAME, VALUE) \ |
| 76 | const char *const INSTALL_NAME_TOOL_##NAME[] = VALUE; |
| 77 | #include "InstallNameToolOpts.inc" |
| 78 | #undef PREFIX |
| 79 | |
| 80 | static const opt::OptTable::Info InstallNameToolInfoTable[] = { |
| 81 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 82 | HELPTEXT, METAVAR, VALUES) \ |
| 83 | {INSTALL_NAME_TOOL_##PREFIX, \ |
| 84 | NAME, \ |
| 85 | HELPTEXT, \ |
| 86 | METAVAR, \ |
| 87 | INSTALL_NAME_TOOL_##ID, \ |
| 88 | opt::Option::KIND##Class, \ |
| 89 | PARAM, \ |
| 90 | FLAGS, \ |
| 91 | INSTALL_NAME_TOOL_##GROUP, \ |
| 92 | INSTALL_NAME_TOOL_##ALIAS, \ |
| 93 | ALIASARGS, \ |
| 94 | VALUES}, |
| 95 | #include "InstallNameToolOpts.inc" |
| 96 | #undef OPTION |
| 97 | }; |
| 98 | |
| 99 | class InstallNameToolOptTable : public opt::OptTable { |
| 100 | public: |
| 101 | InstallNameToolOptTable() : OptTable(InstallNameToolInfoTable) {} |
| 102 | }; |
| 103 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 104 | enum StripID { |
| 105 | STRIP_INVALID = 0, // This is not an option ID. |
| 106 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 107 | HELPTEXT, METAVAR, VALUES) \ |
| 108 | STRIP_##ID, |
| 109 | #include "StripOpts.inc" |
| 110 | #undef OPTION |
| 111 | }; |
| 112 | |
| 113 | #define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE; |
| 114 | #include "StripOpts.inc" |
| 115 | #undef PREFIX |
| 116 | |
| 117 | static const opt::OptTable::Info StripInfoTable[] = { |
| 118 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 119 | HELPTEXT, METAVAR, VALUES) \ |
| 120 | {STRIP_##PREFIX, NAME, HELPTEXT, \ |
| 121 | METAVAR, STRIP_##ID, opt::Option::KIND##Class, \ |
| 122 | PARAM, FLAGS, STRIP_##GROUP, \ |
| 123 | STRIP_##ALIAS, ALIASARGS, VALUES}, |
| 124 | #include "StripOpts.inc" |
| 125 | #undef OPTION |
| 126 | }; |
| 127 | |
| 128 | class StripOptTable : public opt::OptTable { |
| 129 | public: |
Jordan Rupprecht | aaeaa0a | 2018-10-23 18:46:33 +0000 | [diff] [blame] | 130 | StripOptTable() : OptTable(StripInfoTable) {} |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 133 | } // namespace |
| 134 | |
| 135 | static SectionFlag parseSectionRenameFlag(StringRef SectionName) { |
| 136 | return llvm::StringSwitch<SectionFlag>(SectionName) |
James Henderson | d931cf3 | 2019-04-03 14:40:27 +0000 | [diff] [blame] | 137 | .CaseLower("alloc", SectionFlag::SecAlloc) |
| 138 | .CaseLower("load", SectionFlag::SecLoad) |
| 139 | .CaseLower("noload", SectionFlag::SecNoload) |
| 140 | .CaseLower("readonly", SectionFlag::SecReadonly) |
| 141 | .CaseLower("debug", SectionFlag::SecDebug) |
| 142 | .CaseLower("code", SectionFlag::SecCode) |
| 143 | .CaseLower("data", SectionFlag::SecData) |
| 144 | .CaseLower("rom", SectionFlag::SecRom) |
| 145 | .CaseLower("merge", SectionFlag::SecMerge) |
| 146 | .CaseLower("strings", SectionFlag::SecStrings) |
| 147 | .CaseLower("contents", SectionFlag::SecContents) |
| 148 | .CaseLower("share", SectionFlag::SecShare) |
Sergey Dmitriev | e446322 | 2020-01-20 17:06:03 -0800 | [diff] [blame] | 149 | .CaseLower("exclude", SectionFlag::SecExclude) |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 150 | .Default(SectionFlag::SecNone); |
| 151 | } |
| 152 | |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 153 | static Expected<SectionFlag> |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 154 | parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) { |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 155 | SectionFlag ParsedFlags = SectionFlag::SecNone; |
| 156 | for (StringRef Flag : SectionFlags) { |
| 157 | SectionFlag ParsedFlag = parseSectionRenameFlag(Flag); |
| 158 | if (ParsedFlag == SectionFlag::SecNone) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 159 | return createStringError( |
| 160 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 161 | "unrecognized section flag '%s'. Flags supported for GNU " |
Sergey Dmitriev | e446322 | 2020-01-20 17:06:03 -0800 | [diff] [blame] | 162 | "compatibility: alloc, load, noload, readonly, exclude, debug, " |
| 163 | "code, data, rom, share, contents, merge, strings", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 164 | Flag.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 165 | ParsedFlags |= ParsedFlag; |
| 166 | } |
| 167 | |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 168 | return ParsedFlags; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 171 | static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 172 | if (!FlagValue.contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 173 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 174 | "bad format for --rename-section: missing '='"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 175 | |
| 176 | // Initial split: ".foo" = ".bar,f1,f2,..." |
| 177 | auto Old2New = FlagValue.split('='); |
| 178 | SectionRename SR; |
| 179 | SR.OriginalName = Old2New.first; |
| 180 | |
| 181 | // Flags split: ".bar" "f1" "f2" ... |
| 182 | SmallVector<StringRef, 6> NameAndFlags; |
| 183 | Old2New.second.split(NameAndFlags, ','); |
| 184 | SR.NewName = NameAndFlags[0]; |
| 185 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 186 | if (NameAndFlags.size() > 1) { |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 187 | Expected<SectionFlag> ParsedFlagSet = |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 188 | parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front()); |
| 189 | if (!ParsedFlagSet) |
| 190 | return ParsedFlagSet.takeError(); |
| 191 | SR.NewFlags = *ParsedFlagSet; |
| 192 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 193 | |
| 194 | return SR; |
| 195 | } |
| 196 | |
Fangrui Song | 671fb34 | 2019-10-02 12:41:25 +0000 | [diff] [blame] | 197 | static Expected<std::pair<StringRef, uint64_t>> |
| 198 | parseSetSectionAlignment(StringRef FlagValue) { |
| 199 | if (!FlagValue.contains('=')) |
| 200 | return createStringError( |
| 201 | errc::invalid_argument, |
| 202 | "bad format for --set-section-alignment: missing '='"); |
| 203 | auto Split = StringRef(FlagValue).split('='); |
| 204 | if (Split.first.empty()) |
| 205 | return createStringError( |
| 206 | errc::invalid_argument, |
| 207 | "bad format for --set-section-alignment: missing section name"); |
| 208 | uint64_t NewAlign; |
| 209 | if (Split.second.getAsInteger(0, NewAlign)) |
| 210 | return createStringError(errc::invalid_argument, |
| 211 | "invalid alignment for --set-section-alignment: '%s'", |
| 212 | Split.second.str().c_str()); |
| 213 | return std::make_pair(Split.first, NewAlign); |
| 214 | } |
| 215 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 216 | static Expected<SectionFlagsUpdate> |
| 217 | parseSetSectionFlagValue(StringRef FlagValue) { |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 218 | if (!StringRef(FlagValue).contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 219 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 220 | "bad format for --set-section-flags: missing '='"); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 221 | |
| 222 | // Initial split: ".foo" = "f1,f2,..." |
| 223 | auto Section2Flags = StringRef(FlagValue).split('='); |
| 224 | SectionFlagsUpdate SFU; |
| 225 | SFU.Name = Section2Flags.first; |
| 226 | |
| 227 | // Flags split: "f1" "f2" ... |
| 228 | SmallVector<StringRef, 6> SectionFlags; |
| 229 | Section2Flags.second.split(SectionFlags, ','); |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 230 | Expected<SectionFlag> ParsedFlagSet = parseSectionFlagSet(SectionFlags); |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 231 | if (!ParsedFlagSet) |
| 232 | return ParsedFlagSet.takeError(); |
| 233 | SFU.NewFlags = *ParsedFlagSet; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 234 | |
| 235 | return SFU; |
| 236 | } |
| 237 | |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 238 | struct TargetInfo { |
| 239 | FileFormat Format; |
| 240 | MachineInfo Machine; |
| 241 | }; |
| 242 | |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 243 | // FIXME: consolidate with the bfd parsing used by lld. |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 244 | static const StringMap<MachineInfo> TargetMap{ |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 245 | // Name, {EMachine, 64bit, LittleEndian} |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 246 | // x86 |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 247 | {"elf32-i386", {ELF::EM_386, false, true}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 248 | {"elf32-x86-64", {ELF::EM_X86_64, false, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 249 | {"elf64-x86-64", {ELF::EM_X86_64, true, true}}, |
| 250 | // Intel MCU |
| 251 | {"elf32-iamcu", {ELF::EM_IAMCU, false, true}}, |
| 252 | // ARM |
| 253 | {"elf32-littlearm", {ELF::EM_ARM, false, true}}, |
| 254 | // ARM AArch64 |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 255 | {"elf64-aarch64", {ELF::EM_AARCH64, true, true}}, |
| 256 | {"elf64-littleaarch64", {ELF::EM_AARCH64, true, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 257 | // RISC-V |
| 258 | {"elf32-littleriscv", {ELF::EM_RISCV, false, true}}, |
| 259 | {"elf64-littleriscv", {ELF::EM_RISCV, true, true}}, |
| 260 | // PowerPC |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 261 | {"elf32-powerpc", {ELF::EM_PPC, false, false}}, |
| 262 | {"elf32-powerpcle", {ELF::EM_PPC, false, true}}, |
| 263 | {"elf64-powerpc", {ELF::EM_PPC64, true, false}}, |
| 264 | {"elf64-powerpcle", {ELF::EM_PPC64, true, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 265 | // MIPS |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 266 | {"elf32-bigmips", {ELF::EM_MIPS, false, false}}, |
| 267 | {"elf32-ntradbigmips", {ELF::EM_MIPS, false, false}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 268 | {"elf32-ntradlittlemips", {ELF::EM_MIPS, false, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 269 | {"elf32-tradbigmips", {ELF::EM_MIPS, false, false}}, |
| 270 | {"elf32-tradlittlemips", {ELF::EM_MIPS, false, true}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 271 | {"elf64-tradbigmips", {ELF::EM_MIPS, true, false}}, |
| 272 | {"elf64-tradlittlemips", {ELF::EM_MIPS, true, true}}, |
Seiya Nuta | 13de174 | 2019-06-17 02:03:45 +0000 | [diff] [blame] | 273 | // SPARC |
| 274 | {"elf32-sparc", {ELF::EM_SPARC, false, false}}, |
| 275 | {"elf32-sparcel", {ELF::EM_SPARC, false, true}}, |
Sid Manning | 5002863 | 2020-04-06 12:40:19 -0500 | [diff] [blame] | 276 | {"elf32-hexagon", {ELF::EM_HEXAGON, false, true}}, |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 279 | static Expected<TargetInfo> |
| 280 | getOutputTargetInfoByTargetName(StringRef TargetName) { |
| 281 | StringRef OriginalTargetName = TargetName; |
| 282 | bool IsFreeBSD = TargetName.consume_back("-freebsd"); |
| 283 | auto Iter = TargetMap.find(TargetName); |
| 284 | if (Iter == std::end(TargetMap)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 285 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 286 | "invalid output format: '%s'", |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 287 | OriginalTargetName.str().c_str()); |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 288 | MachineInfo MI = Iter->getValue(); |
| 289 | if (IsFreeBSD) |
| 290 | MI.OSABI = ELF::ELFOSABI_FREEBSD; |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 291 | |
| 292 | FileFormat Format; |
| 293 | if (TargetName.startswith("elf")) |
| 294 | Format = FileFormat::ELF; |
| 295 | else |
| 296 | // This should never happen because `TargetName` is valid (it certainly |
| 297 | // exists in the TargetMap). |
| 298 | llvm_unreachable("unknown target prefix"); |
| 299 | |
| 300 | return {TargetInfo{Format, MI}}; |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 303 | static Error |
| 304 | addSymbolsFromFile(NameMatcher &Symbols, BumpPtrAllocator &Alloc, |
| 305 | StringRef Filename, MatchStyle MS, |
| 306 | llvm::function_ref<Error(Error)> ErrorCallback) { |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 307 | StringSaver Saver(Alloc); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 308 | SmallVector<StringRef, 16> Lines; |
| 309 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 310 | if (!BufOrErr) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 311 | return createFileError(Filename, BufOrErr.getError()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 312 | |
| 313 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 314 | for (StringRef Line : Lines) { |
| 315 | // Ignore everything after '#', trim whitespace, and only add the symbol if |
| 316 | // it's not empty. |
| 317 | auto TrimmedLine = Line.split('#').first.trim(); |
| 318 | if (!TrimmedLine.empty()) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 319 | if (Error E = Symbols.addMatcher(NameOrPattern::create( |
| 320 | Saver.save(TrimmedLine), MS, ErrorCallback))) |
| 321 | return E; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 322 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 323 | |
| 324 | return Error::success(); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 327 | Expected<NameOrPattern> |
| 328 | NameOrPattern::create(StringRef Pattern, MatchStyle MS, |
| 329 | llvm::function_ref<Error(Error)> ErrorCallback) { |
| 330 | switch (MS) { |
| 331 | case MatchStyle::Literal: |
| 332 | return NameOrPattern(Pattern); |
| 333 | case MatchStyle::Wildcard: { |
| 334 | SmallVector<char, 32> Data; |
| 335 | bool IsPositiveMatch = true; |
| 336 | if (Pattern[0] == '!') { |
| 337 | IsPositiveMatch = false; |
| 338 | Pattern = Pattern.drop_front(); |
| 339 | } |
| 340 | Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pattern); |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 341 | |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 342 | // If we couldn't create it as a glob, report the error, but try again with |
| 343 | // a literal if the error reporting is non-fatal. |
| 344 | if (!GlobOrErr) { |
| 345 | if (Error E = ErrorCallback(GlobOrErr.takeError())) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 346 | return std::move(E); |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 347 | return create(Pattern, MatchStyle::Literal, ErrorCallback); |
| 348 | } |
| 349 | |
| 350 | return NameOrPattern(std::make_shared<GlobPattern>(*GlobOrErr), |
| 351 | IsPositiveMatch); |
| 352 | } |
| 353 | case MatchStyle::Regex: { |
| 354 | SmallVector<char, 32> Data; |
| 355 | return NameOrPattern(std::make_shared<Regex>( |
| 356 | ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data))); |
| 357 | } |
| 358 | } |
Simon Pilgrim | 3bd61b2 | 2019-10-18 09:59:40 +0000 | [diff] [blame] | 359 | llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum"); |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 362 | static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename, |
| 363 | BumpPtrAllocator &Alloc, |
| 364 | StringRef Filename) { |
| 365 | StringSaver Saver(Alloc); |
| 366 | SmallVector<StringRef, 16> Lines; |
| 367 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 368 | if (!BufOrErr) |
Eugene Leviant | 317f9e7 | 2019-02-11 09:49:37 +0000 | [diff] [blame] | 369 | return createFileError(Filename, BufOrErr.getError()); |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 370 | |
| 371 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 372 | size_t NumLines = Lines.size(); |
| 373 | for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) { |
| 374 | StringRef TrimmedLine = Lines[LineNo].split('#').first.trim(); |
| 375 | if (TrimmedLine.empty()) |
| 376 | continue; |
| 377 | |
| 378 | std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' '); |
| 379 | StringRef NewName = Pair.second.trim(); |
| 380 | if (NewName.empty()) |
| 381 | return createStringError(errc::invalid_argument, |
| 382 | "%s:%zu: missing new symbol name", |
| 383 | Filename.str().c_str(), LineNo + 1); |
| 384 | SymbolsToRename.insert({Pair.first, NewName}); |
| 385 | } |
| 386 | return Error::success(); |
| 387 | } |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 388 | |
| 389 | template <class T> static ErrorOr<T> getAsInteger(StringRef Val) { |
| 390 | T Result; |
| 391 | if (Val.getAsInteger(0, Result)) |
| 392 | return errc::invalid_argument; |
| 393 | return Result; |
| 394 | } |
| 395 | |
Alexander Shaposhnikov | ca133cd | 2020-06-24 11:19:31 -0700 | [diff] [blame] | 396 | namespace { |
| 397 | |
| 398 | enum class ToolType { Objcopy, Strip, InstallNameTool }; |
| 399 | |
| 400 | } // anonymous namespace |
| 401 | |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 402 | static void printHelp(const opt::OptTable &OptTable, raw_ostream &OS, |
Alexander Shaposhnikov | ca133cd | 2020-06-24 11:19:31 -0700 | [diff] [blame] | 403 | ToolType Tool) { |
| 404 | StringRef HelpText, ToolName; |
| 405 | switch (Tool) { |
| 406 | case ToolType::Objcopy: |
| 407 | ToolName = "llvm-objcopy"; |
| 408 | HelpText = " [options] input [output]"; |
| 409 | break; |
| 410 | case ToolType::Strip: |
| 411 | ToolName = "llvm-strip"; |
| 412 | HelpText = " [options] inputs..."; |
| 413 | break; |
| 414 | case ToolType::InstallNameTool: |
| 415 | ToolName = "llvm-install-name-tool"; |
| 416 | HelpText = " [options] input"; |
| 417 | break; |
| 418 | } |
| 419 | OptTable.PrintHelp(OS, (ToolName + HelpText).str().c_str(), |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 420 | (ToolName + " tool").str().c_str()); |
| 421 | // TODO: Replace this with libOption call once it adds extrahelp support. |
| 422 | // The CommandLine library has a cl::extrahelp class to support this, |
| 423 | // but libOption does not have that yet. |
| 424 | OS << "\nPass @FILE as argument to read options from FILE.\n"; |
| 425 | } |
| 426 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 427 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 428 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 429 | // exit. |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 430 | Expected<DriverConfig> |
| 431 | parseObjcopyOptions(ArrayRef<const char *> ArgsArr, |
| 432 | llvm::function_ref<Error(Error)> ErrorCallback) { |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 433 | DriverConfig DC; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 434 | ObjcopyOptTable T; |
| 435 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 436 | llvm::opt::InputArgList InputArgs = |
| 437 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 438 | |
| 439 | if (InputArgs.size() == 0) { |
Alexander Shaposhnikov | ca133cd | 2020-06-24 11:19:31 -0700 | [diff] [blame] | 440 | printHelp(T, errs(), ToolType::Objcopy); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 441 | exit(1); |
| 442 | } |
| 443 | |
| 444 | if (InputArgs.hasArg(OBJCOPY_help)) { |
Alexander Shaposhnikov | ca133cd | 2020-06-24 11:19:31 -0700 | [diff] [blame] | 445 | printHelp(T, outs(), ToolType::Objcopy); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 446 | exit(0); |
| 447 | } |
| 448 | |
| 449 | if (InputArgs.hasArg(OBJCOPY_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 450 | outs() << "llvm-objcopy, compatible with GNU objcopy\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 451 | cl::PrintVersionMessage(); |
| 452 | exit(0); |
| 453 | } |
| 454 | |
| 455 | SmallVector<const char *, 2> Positional; |
| 456 | |
| 457 | for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 458 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 459 | Arg->getAsString(InputArgs).c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 460 | |
| 461 | for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT)) |
| 462 | Positional.push_back(Arg->getValue()); |
| 463 | |
| 464 | if (Positional.empty()) |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 465 | return createStringError(errc::invalid_argument, "no input file specified"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 466 | |
| 467 | if (Positional.size() > 2) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 468 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 469 | "too many positional arguments"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 470 | |
| 471 | CopyConfig Config; |
| 472 | Config.InputFilename = Positional[0]; |
| 473 | Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 474 | if (InputArgs.hasArg(OBJCOPY_target) && |
| 475 | (InputArgs.hasArg(OBJCOPY_input_target) || |
| 476 | InputArgs.hasArg(OBJCOPY_output_target))) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 477 | return createStringError( |
| 478 | errc::invalid_argument, |
| 479 | "--target cannot be used with --input-target or --output-target"); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 480 | |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 481 | if (InputArgs.hasArg(OBJCOPY_regex) && InputArgs.hasArg(OBJCOPY_wildcard)) |
| 482 | return createStringError(errc::invalid_argument, |
| 483 | "--regex and --wildcard are incompatible"); |
| 484 | |
| 485 | MatchStyle SectionMatchStyle = InputArgs.hasArg(OBJCOPY_regex) |
| 486 | ? MatchStyle::Regex |
| 487 | : MatchStyle::Wildcard; |
| 488 | MatchStyle SymbolMatchStyle = InputArgs.hasArg(OBJCOPY_regex) |
| 489 | ? MatchStyle::Regex |
| 490 | : InputArgs.hasArg(OBJCOPY_wildcard) |
| 491 | ? MatchStyle::Wildcard |
| 492 | : MatchStyle::Literal; |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 493 | StringRef InputFormat, OutputFormat; |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 494 | if (InputArgs.hasArg(OBJCOPY_target)) { |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 495 | InputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 496 | OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 497 | } else { |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 498 | InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target); |
| 499 | OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 500 | } |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 501 | |
| 502 | // FIXME: Currently, we ignore the target for non-binary/ihex formats |
| 503 | // explicitly specified by -I option (e.g. -Ielf32-x86-64) and guess the |
| 504 | // format by llvm::object::createBinary regardless of the option value. |
| 505 | Config.InputFormat = StringSwitch<FileFormat>(InputFormat) |
| 506 | .Case("binary", FileFormat::Binary) |
| 507 | .Case("ihex", FileFormat::IHex) |
| 508 | .Default(FileFormat::Unspecified); |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 509 | |
Michael Liao | d19fb46 | 2019-09-24 12:43:44 +0000 | [diff] [blame] | 510 | if (InputArgs.hasArg(OBJCOPY_new_symbol_visibility)) |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 511 | Config.NewSymbolVisibility = |
| 512 | InputArgs.getLastArgValue(OBJCOPY_new_symbol_visibility); |
Chris Jackson | fa1fe93 | 2019-08-30 10:17:16 +0000 | [diff] [blame] | 513 | |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 514 | Config.OutputFormat = StringSwitch<FileFormat>(OutputFormat) |
| 515 | .Case("binary", FileFormat::Binary) |
| 516 | .Case("ihex", FileFormat::IHex) |
| 517 | .Default(FileFormat::Unspecified); |
Fangrui Song | ba53030 | 2019-09-14 01:36:16 +0000 | [diff] [blame] | 518 | if (Config.OutputFormat == FileFormat::Unspecified) { |
| 519 | if (OutputFormat.empty()) { |
| 520 | Config.OutputFormat = Config.InputFormat; |
| 521 | } else { |
| 522 | Expected<TargetInfo> Target = |
| 523 | getOutputTargetInfoByTargetName(OutputFormat); |
| 524 | if (!Target) |
| 525 | return Target.takeError(); |
| 526 | Config.OutputFormat = Target->Format; |
| 527 | Config.OutputArch = Target->Machine; |
| 528 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 529 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 530 | |
| 531 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections, |
| 532 | OBJCOPY_compress_debug_sections_eq)) { |
| 533 | Config.CompressionType = DebugCompressionType::Z; |
| 534 | |
| 535 | if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) { |
| 536 | Config.CompressionType = |
| 537 | StringSwitch<DebugCompressionType>( |
| 538 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)) |
| 539 | .Case("zlib-gnu", DebugCompressionType::GNU) |
| 540 | .Case("zlib", DebugCompressionType::Z) |
| 541 | .Default(DebugCompressionType::None); |
| 542 | if (Config.CompressionType == DebugCompressionType::None) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 543 | return createStringError( |
| 544 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 545 | "invalid or unsupported --compress-debug-sections format: %s", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 546 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq) |
| 547 | .str() |
| 548 | .c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 549 | } |
George Rimar | 1e93080 | 2019-03-05 11:32:14 +0000 | [diff] [blame] | 550 | if (!zlib::isAvailable()) |
| 551 | return createStringError( |
| 552 | errc::invalid_argument, |
| 553 | "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 556 | Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); |
James Henderson | 9df3883 | 2019-05-14 10:59:04 +0000 | [diff] [blame] | 557 | // The gnu_debuglink's target is expected to not change or else its CRC would |
| 558 | // become invalidated and get rejected. We can avoid recalculating the |
| 559 | // checksum for every target file inside an archive by precomputing the CRC |
| 560 | // here. This prevents a significant amount of I/O. |
| 561 | if (!Config.AddGnuDebugLink.empty()) { |
| 562 | auto DebugOrErr = MemoryBuffer::getFile(Config.AddGnuDebugLink); |
| 563 | if (!DebugOrErr) |
| 564 | return createFileError(Config.AddGnuDebugLink, DebugOrErr.getError()); |
| 565 | auto Debug = std::move(*DebugOrErr); |
Hans Wennborg | 1e1e3ba | 2019-10-09 09:06:30 +0000 | [diff] [blame] | 566 | Config.GnuDebugLinkCRC32 = |
| 567 | llvm::crc32(arrayRefFromStringRef(Debug->getBuffer())); |
James Henderson | 9df3883 | 2019-05-14 10:59:04 +0000 | [diff] [blame] | 568 | } |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 569 | Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir); |
| 570 | if (InputArgs.hasArg(OBJCOPY_build_id_link_input)) |
| 571 | Config.BuildIdLinkInput = |
| 572 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_input); |
| 573 | if (InputArgs.hasArg(OBJCOPY_build_id_link_output)) |
| 574 | Config.BuildIdLinkOutput = |
| 575 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_output); |
| 576 | Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 577 | Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols); |
James Henderson | fa11fb3 | 2019-05-08 09:49:35 +0000 | [diff] [blame] | 578 | Config.AllocSectionsPrefix = |
| 579 | InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections); |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 580 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_extract_partition)) |
| 581 | Config.ExtractPartition = Arg->getValue(); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 582 | |
| 583 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { |
| 584 | if (!StringRef(Arg->getValue()).contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 585 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 586 | "bad format for --redefine-sym"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 587 | auto Old2New = StringRef(Arg->getValue()).split('='); |
| 588 | if (!Config.SymbolsToRename.insert(Old2New).second) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 589 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 590 | "multiple redefinition of symbol '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 591 | Old2New.first.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 594 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols)) |
| 595 | if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc, |
| 596 | Arg->getValue())) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 597 | return std::move(E); |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 598 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 599 | for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 600 | Expected<SectionRename> SR = |
| 601 | parseRenameSectionValue(StringRef(Arg->getValue())); |
| 602 | if (!SR) |
| 603 | return SR.takeError(); |
| 604 | if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second) |
| 605 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 606 | "multiple renames of section '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 607 | SR->OriginalName.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 608 | } |
Fangrui Song | 671fb34 | 2019-10-02 12:41:25 +0000 | [diff] [blame] | 609 | for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_alignment)) { |
| 610 | Expected<std::pair<StringRef, uint64_t>> NameAndAlign = |
| 611 | parseSetSectionAlignment(Arg->getValue()); |
| 612 | if (!NameAndAlign) |
| 613 | return NameAndAlign.takeError(); |
| 614 | Config.SetSectionAlignment[NameAndAlign->first] = NameAndAlign->second; |
| 615 | } |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 616 | for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 617 | Expected<SectionFlagsUpdate> SFU = |
| 618 | parseSetSectionFlagValue(Arg->getValue()); |
| 619 | if (!SFU) |
| 620 | return SFU.takeError(); |
| 621 | if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second) |
| 622 | return createStringError( |
| 623 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 624 | "--set-section-flags set multiple times for section '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 625 | SFU->Name.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 626 | } |
| 627 | // Prohibit combinations of --set-section-flags when the section name is used |
| 628 | // by --rename-section, either as a source or a destination. |
| 629 | for (const auto &E : Config.SectionsToRename) { |
| 630 | const SectionRename &SR = E.second; |
| 631 | if (Config.SetSectionFlags.count(SR.OriginalName)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 632 | return createStringError( |
| 633 | errc::invalid_argument, |
| 634 | "--set-section-flags=%s conflicts with --rename-section=%s=%s", |
| 635 | SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(), |
| 636 | SR.NewName.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 637 | if (Config.SetSectionFlags.count(SR.NewName)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 638 | return createStringError( |
| 639 | errc::invalid_argument, |
| 640 | "--set-section-flags=%s conflicts with --rename-section=%s=%s", |
| 641 | SR.NewName.str().c_str(), SR.OriginalName.str().c_str(), |
| 642 | SR.NewName.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 643 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 644 | |
| 645 | for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 646 | if (Error E = Config.ToRemove.addMatcher(NameOrPattern::create( |
| 647 | Arg->getValue(), SectionMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 648 | return std::move(E); |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 649 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 650 | if (Error E = Config.KeepSection.addMatcher(NameOrPattern::create( |
| 651 | Arg->getValue(), SectionMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 652 | return std::move(E); |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 653 | for (auto Arg : InputArgs.filtered(OBJCOPY_only_section)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 654 | if (Error E = Config.OnlySection.addMatcher(NameOrPattern::create( |
| 655 | Arg->getValue(), SectionMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 656 | return std::move(E); |
Sergey Dmitriev | 899bdaa | 2019-07-29 16:22:40 +0000 | [diff] [blame] | 657 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) { |
| 658 | StringRef ArgValue(Arg->getValue()); |
| 659 | if (!ArgValue.contains('=')) |
| 660 | return createStringError(errc::invalid_argument, |
| 661 | "bad format for --add-section: missing '='"); |
| 662 | if (ArgValue.split("=").second.empty()) |
| 663 | return createStringError( |
| 664 | errc::invalid_argument, |
| 665 | "bad format for --add-section: missing file name"); |
| 666 | Config.AddSection.push_back(ArgValue); |
| 667 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 668 | for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section)) |
| 669 | Config.DumpSection.push_back(Arg->getValue()); |
| 670 | Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all); |
| 671 | Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu); |
| 672 | Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug); |
| 673 | Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); |
| 674 | Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); |
| 675 | Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); |
| 676 | Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); |
| 677 | Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 678 | Config.ExtractMainPartition = |
| 679 | InputArgs.hasArg(OBJCOPY_extract_main_partition); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 680 | Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); |
| 681 | Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 682 | if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals)) |
| 683 | Config.DiscardMode = |
| 684 | InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals) |
| 685 | ? DiscardType::All |
| 686 | : DiscardType::Locals; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 687 | Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); |
| 688 | Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); |
| 689 | Config.DecompressDebugSections = |
| 690 | InputArgs.hasArg(OBJCOPY_decompress_debug_sections); |
Fangrui Song | b14e9e3 | 2020-03-24 15:38:48 +0800 | [diff] [blame] | 691 | if (Config.DiscardMode == DiscardType::All) { |
Sid Manning | 5ad18a7 | 2019-05-03 14:14:01 +0000 | [diff] [blame] | 692 | Config.StripDebug = true; |
Fangrui Song | b14e9e3 | 2020-03-24 15:38:48 +0800 | [diff] [blame] | 693 | Config.KeepFileSymbols = true; |
| 694 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 695 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 696 | if (Error E = Config.SymbolsToLocalize.addMatcher(NameOrPattern::create( |
| 697 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 698 | return std::move(E); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 699 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 700 | if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, DC.Alloc, |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 701 | Arg->getValue(), SymbolMatchStyle, |
| 702 | ErrorCallback)) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 703 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 704 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 705 | if (Error E = Config.SymbolsToKeepGlobal.addMatcher(NameOrPattern::create( |
| 706 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 707 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 708 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 709 | if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc, |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 710 | Arg->getValue(), SymbolMatchStyle, |
| 711 | ErrorCallback)) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 712 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 713 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 714 | if (Error E = Config.SymbolsToGlobalize.addMatcher(NameOrPattern::create( |
| 715 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 716 | return std::move(E); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 717 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 718 | if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc, |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 719 | Arg->getValue(), SymbolMatchStyle, |
| 720 | ErrorCallback)) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 721 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 722 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 723 | if (Error E = Config.SymbolsToWeaken.addMatcher(NameOrPattern::create( |
| 724 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 725 | return std::move(E); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 726 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 727 | if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, DC.Alloc, |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 728 | Arg->getValue(), SymbolMatchStyle, |
| 729 | ErrorCallback)) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 730 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 731 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 732 | if (Error E = Config.SymbolsToRemove.addMatcher(NameOrPattern::create( |
| 733 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 734 | return std::move(E); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 735 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 736 | if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc, |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 737 | Arg->getValue(), SymbolMatchStyle, |
| 738 | ErrorCallback)) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 739 | return std::move(E); |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 740 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 741 | if (Error E = |
| 742 | Config.UnneededSymbolsToRemove.addMatcher(NameOrPattern::create( |
| 743 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 744 | return std::move(E); |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 745 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 746 | if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc, |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 747 | Arg->getValue(), SymbolMatchStyle, |
| 748 | ErrorCallback)) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 749 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 750 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 751 | if (Error E = Config.SymbolsToKeep.addMatcher(NameOrPattern::create( |
| 752 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 753 | return std::move(E); |
Yi Kong | f2baddb | 2019-04-01 18:12:43 +0000 | [diff] [blame] | 754 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbols)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 755 | if (Error E = |
| 756 | addSymbolsFromFile(Config.SymbolsToKeep, DC.Alloc, Arg->getValue(), |
| 757 | SymbolMatchStyle, ErrorCallback)) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 758 | return std::move(E); |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 759 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_symbol)) |
| 760 | Config.SymbolsToAdd.push_back(Arg->getValue()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 761 | |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 762 | Config.AllowBrokenLinks = InputArgs.hasArg(OBJCOPY_allow_broken_links); |
| 763 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 764 | Config.DeterministicArchives = InputArgs.hasFlag( |
| 765 | OBJCOPY_enable_deterministic_archives, |
| 766 | OBJCOPY_disable_deterministic_archives, /*default=*/true); |
| 767 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 768 | Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); |
| 769 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 770 | if (Config.PreserveDates && |
| 771 | (Config.OutputFilename == "-" || Config.InputFilename == "-")) |
| 772 | return createStringError(errc::invalid_argument, |
| 773 | "--preserve-dates requires a file"); |
| 774 | |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 775 | for (auto Arg : InputArgs) |
| 776 | if (Arg->getOption().matches(OBJCOPY_set_start)) { |
| 777 | auto EAddr = getAsInteger<uint64_t>(Arg->getValue()); |
| 778 | if (!EAddr) |
| 779 | return createStringError( |
| 780 | EAddr.getError(), "bad entry point address: '%s'", Arg->getValue()); |
| 781 | |
| 782 | Config.EntryExpr = [EAddr](uint64_t) { return *EAddr; }; |
| 783 | } else if (Arg->getOption().matches(OBJCOPY_change_start)) { |
| 784 | auto EIncr = getAsInteger<int64_t>(Arg->getValue()); |
| 785 | if (!EIncr) |
| 786 | return createStringError(EIncr.getError(), |
| 787 | "bad entry point increment: '%s'", |
| 788 | Arg->getValue()); |
| 789 | auto Expr = Config.EntryExpr ? std::move(Config.EntryExpr) |
| 790 | : [](uint64_t A) { return A; }; |
| 791 | Config.EntryExpr = [Expr, EIncr](uint64_t EAddr) { |
| 792 | return Expr(EAddr) + *EIncr; |
| 793 | }; |
| 794 | } |
| 795 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 796 | if (Config.DecompressDebugSections && |
| 797 | Config.CompressionType != DebugCompressionType::None) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 798 | return createStringError( |
| 799 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 800 | "cannot specify both --compress-debug-sections and " |
| 801 | "--decompress-debug-sections"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | if (Config.DecompressDebugSections && !zlib::isAvailable()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 805 | return createStringError( |
| 806 | errc::invalid_argument, |
| 807 | "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 808 | |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 809 | if (Config.ExtractPartition && Config.ExtractMainPartition) |
| 810 | return createStringError(errc::invalid_argument, |
| 811 | "cannot specify --extract-partition together with " |
| 812 | "--extract-main-partition"); |
| 813 | |
Jordan Rupprecht | ab9f662 | 2018-10-23 20:54:51 +0000 | [diff] [blame] | 814 | DC.CopyConfigs.push_back(std::move(Config)); |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 815 | return std::move(DC); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 818 | // ParseInstallNameToolOptions returns the config and sets the input arguments. |
| 819 | // If a help flag is set then ParseInstallNameToolOptions will print the help |
| 820 | // messege and exit. |
| 821 | Expected<DriverConfig> |
| 822 | parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) { |
| 823 | DriverConfig DC; |
| 824 | CopyConfig Config; |
| 825 | InstallNameToolOptTable T; |
| 826 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 827 | llvm::opt::InputArgList InputArgs = |
| 828 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 829 | |
Alexander Shaposhnikov | b925ca3 | 2020-06-26 17:22:15 -0700 | [diff] [blame] | 830 | if (MissingArgumentCount) |
| 831 | return createStringError( |
| 832 | errc::invalid_argument, |
| 833 | "missing argument to " + |
| 834 | StringRef(InputArgs.getArgString(MissingArgumentIndex)) + |
| 835 | " option"); |
| 836 | |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 837 | if (InputArgs.size() == 0) { |
Alexander Shaposhnikov | ca133cd | 2020-06-24 11:19:31 -0700 | [diff] [blame] | 838 | printHelp(T, errs(), ToolType::InstallNameTool); |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 839 | exit(1); |
| 840 | } |
| 841 | |
| 842 | if (InputArgs.hasArg(INSTALL_NAME_TOOL_help)) { |
Alexander Shaposhnikov | ca133cd | 2020-06-24 11:19:31 -0700 | [diff] [blame] | 843 | printHelp(T, outs(), ToolType::InstallNameTool); |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 844 | exit(0); |
| 845 | } |
| 846 | |
| 847 | if (InputArgs.hasArg(INSTALL_NAME_TOOL_version)) { |
| 848 | outs() << "llvm-install-name-tool, compatible with cctools " |
| 849 | "install_name_tool\n"; |
| 850 | cl::PrintVersionMessage(); |
| 851 | exit(0); |
| 852 | } |
| 853 | |
| 854 | for (auto Arg : InputArgs.filtered(INSTALL_NAME_TOOL_add_rpath)) |
| 855 | Config.RPathToAdd.push_back(Arg->getValue()); |
| 856 | |
Alexander Shaposhnikov | d332ec9 | 2020-06-22 16:49:14 -0700 | [diff] [blame] | 857 | for (auto Arg : InputArgs.filtered(INSTALL_NAME_TOOL_delete_rpath)) { |
| 858 | StringRef RPath = Arg->getValue(); |
| 859 | |
| 860 | // Cannot add and delete the same rpath at the same time. |
| 861 | if (is_contained(Config.RPathToAdd, RPath)) |
| 862 | return createStringError( |
| 863 | errc::invalid_argument, |
| 864 | "cannot specify both -add_rpath %s and -delete_rpath %s", |
| 865 | RPath.str().c_str(), RPath.str().c_str()); |
| 866 | |
| 867 | Config.RPathsToRemove.insert(RPath); |
| 868 | } |
| 869 | |
Alexander Shaposhnikov | b925ca3 | 2020-06-26 17:22:15 -0700 | [diff] [blame] | 870 | for (auto *Arg : InputArgs.filtered(INSTALL_NAME_TOOL_rpath)) { |
| 871 | StringRef Old = Arg->getValue(0); |
| 872 | StringRef New = Arg->getValue(1); |
| 873 | |
| 874 | auto Match = [=](StringRef RPath) { return RPath == Old || RPath == New; }; |
| 875 | |
| 876 | // Cannot specify duplicate -rpath entries |
Sameer Arora | 3b5db7f | 2020-07-06 14:53:24 -0700 | [diff] [blame] | 877 | auto It1 = find_if( |
| 878 | Config.RPathsToUpdate, |
| 879 | [&Match](const DenseMap<StringRef, StringRef>::value_type &OldNew) { |
| 880 | return Match(OldNew.getFirst()) || Match(OldNew.getSecond()); |
| 881 | }); |
Alexander Shaposhnikov | b925ca3 | 2020-06-26 17:22:15 -0700 | [diff] [blame] | 882 | if (It1 != Config.RPathsToUpdate.end()) |
Sameer Arora | 3b5db7f | 2020-07-06 14:53:24 -0700 | [diff] [blame] | 883 | return createStringError(errc::invalid_argument, |
| 884 | "cannot specify both -rpath " + It1->getFirst() + |
| 885 | " " + It1->getSecond() + " and -rpath " + |
| 886 | Old + " " + New); |
Alexander Shaposhnikov | b925ca3 | 2020-06-26 17:22:15 -0700 | [diff] [blame] | 887 | |
| 888 | // Cannot specify the same rpath under both -delete_rpath and -rpath |
| 889 | auto It2 = find_if(Config.RPathsToRemove, Match); |
| 890 | if (It2 != Config.RPathsToRemove.end()) |
Sameer Arora | 3b5db7f | 2020-07-06 14:53:24 -0700 | [diff] [blame] | 891 | return createStringError(errc::invalid_argument, |
| 892 | "cannot specify both -delete_rpath " + *It2 + |
| 893 | " and -rpath " + Old + " " + New); |
Alexander Shaposhnikov | b925ca3 | 2020-06-26 17:22:15 -0700 | [diff] [blame] | 894 | |
| 895 | // Cannot specify the same rpath under both -add_rpath and -rpath |
| 896 | auto It3 = find_if(Config.RPathToAdd, Match); |
| 897 | if (It3 != Config.RPathToAdd.end()) |
Sameer Arora | 3b5db7f | 2020-07-06 14:53:24 -0700 | [diff] [blame] | 898 | return createStringError(errc::invalid_argument, |
| 899 | "cannot specify both -add_rpath " + *It3 + |
| 900 | " and -rpath " + Old + " " + New); |
Alexander Shaposhnikov | b925ca3 | 2020-06-26 17:22:15 -0700 | [diff] [blame] | 901 | |
Sameer Arora | 3b5db7f | 2020-07-06 14:53:24 -0700 | [diff] [blame] | 902 | Config.RPathsToUpdate.insert({Old, New}); |
Alexander Shaposhnikov | b925ca3 | 2020-06-26 17:22:15 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Alexander Shaposhnikov | e9f9027 | 2020-09-18 17:50:08 -0700 | [diff] [blame^] | 905 | if (auto *Arg = InputArgs.getLastArg(INSTALL_NAME_TOOL_id)) { |
Sameer Arora | ca518c4 | 2020-06-30 11:01:45 -0700 | [diff] [blame] | 906 | Config.SharedLibId = Arg->getValue(); |
Alexander Shaposhnikov | e9f9027 | 2020-09-18 17:50:08 -0700 | [diff] [blame^] | 907 | if (Config.SharedLibId->empty()) |
| 908 | return createStringError(errc::invalid_argument, |
| 909 | "cannot specify an empty id"); |
Sameer Arora | 2bdcd8b | 2020-06-30 11:01:51 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Alexander Shaposhnikov | e9f9027 | 2020-09-18 17:50:08 -0700 | [diff] [blame^] | 912 | for (auto *Arg : InputArgs.filtered(INSTALL_NAME_TOOL_change)) |
| 913 | Config.InstallNamesToUpdate.insert({Arg->getValue(0), Arg->getValue(1)}); |
| 914 | |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 915 | SmallVector<StringRef, 2> Positional; |
| 916 | for (auto Arg : InputArgs.filtered(INSTALL_NAME_TOOL_UNKNOWN)) |
| 917 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 918 | Arg->getAsString(InputArgs).c_str()); |
| 919 | for (auto Arg : InputArgs.filtered(INSTALL_NAME_TOOL_INPUT)) |
| 920 | Positional.push_back(Arg->getValue()); |
| 921 | if (Positional.empty()) |
| 922 | return createStringError(errc::invalid_argument, "no input file specified"); |
| 923 | if (Positional.size() > 1) |
| 924 | return createStringError( |
| 925 | errc::invalid_argument, |
| 926 | "llvm-install-name-tool expects a single input file"); |
| 927 | Config.InputFilename = Positional[0]; |
| 928 | Config.OutputFilename = Positional[0]; |
| 929 | |
| 930 | DC.CopyConfigs.push_back(std::move(Config)); |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 931 | return std::move(DC); |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 934 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 935 | // help flag is set then ParseStripOptions will print the help messege and |
| 936 | // exit. |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 937 | Expected<DriverConfig> |
| 938 | parseStripOptions(ArrayRef<const char *> ArgsArr, |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 939 | llvm::function_ref<Error(Error)> ErrorCallback) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 940 | StripOptTable T; |
| 941 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 942 | llvm::opt::InputArgList InputArgs = |
| 943 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 944 | |
| 945 | if (InputArgs.size() == 0) { |
Alexander Shaposhnikov | ca133cd | 2020-06-24 11:19:31 -0700 | [diff] [blame] | 946 | printHelp(T, errs(), ToolType::Strip); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 947 | exit(1); |
| 948 | } |
| 949 | |
| 950 | if (InputArgs.hasArg(STRIP_help)) { |
Alexander Shaposhnikov | ca133cd | 2020-06-24 11:19:31 -0700 | [diff] [blame] | 951 | printHelp(T, outs(), ToolType::Strip); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 952 | exit(0); |
| 953 | } |
| 954 | |
| 955 | if (InputArgs.hasArg(STRIP_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 956 | outs() << "llvm-strip, compatible with GNU strip\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 957 | cl::PrintVersionMessage(); |
| 958 | exit(0); |
| 959 | } |
| 960 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 961 | SmallVector<StringRef, 2> Positional; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 962 | for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 963 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 964 | Arg->getAsString(InputArgs).c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 965 | for (auto Arg : InputArgs.filtered(STRIP_INPUT)) |
| 966 | Positional.push_back(Arg->getValue()); |
| 967 | |
| 968 | if (Positional.empty()) |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 969 | return createStringError(errc::invalid_argument, "no input file specified"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 970 | |
| 971 | if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 972 | return createStringError( |
| 973 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 974 | "multiple input files cannot be used in combination with -o"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 975 | |
| 976 | CopyConfig Config; |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 977 | |
| 978 | if (InputArgs.hasArg(STRIP_regex) && InputArgs.hasArg(STRIP_wildcard)) |
| 979 | return createStringError(errc::invalid_argument, |
| 980 | "--regex and --wildcard are incompatible"); |
| 981 | MatchStyle SectionMatchStyle = |
| 982 | InputArgs.hasArg(STRIP_regex) ? MatchStyle::Regex : MatchStyle::Wildcard; |
| 983 | MatchStyle SymbolMatchStyle = InputArgs.hasArg(STRIP_regex) |
| 984 | ? MatchStyle::Regex |
| 985 | : InputArgs.hasArg(STRIP_wildcard) |
| 986 | ? MatchStyle::Wildcard |
| 987 | : MatchStyle::Literal; |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 988 | Config.AllowBrokenLinks = InputArgs.hasArg(STRIP_allow_broken_links); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 989 | Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug); |
| 990 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 991 | if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals)) |
| 992 | Config.DiscardMode = |
| 993 | InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals) |
| 994 | ? DiscardType::All |
| 995 | : DiscardType::Locals; |
Wolfgang Pieb | ab751a7 | 2019-08-08 00:35:16 +0000 | [diff] [blame] | 996 | Config.StripSections = InputArgs.hasArg(STRIP_strip_sections); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 997 | Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); |
James Henderson | e4a89a1 | 2019-05-02 11:53:02 +0000 | [diff] [blame] | 998 | if (auto Arg = InputArgs.getLastArg(STRIP_strip_all, STRIP_no_strip_all)) |
| 999 | Config.StripAll = Arg->getOption().getID() == STRIP_strip_all; |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 1000 | Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu); |
Alexander Shaposhnikov | 842a8cc | 2020-05-26 16:49:56 -0700 | [diff] [blame] | 1001 | Config.StripSwiftSymbols = InputArgs.hasArg(STRIP_strip_swift_symbols); |
Jordan Rupprecht | 12ed01d | 2019-03-14 21:51:42 +0000 | [diff] [blame] | 1002 | Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug); |
Eugene Leviant | 05a3f99 | 2019-02-01 15:25:15 +0000 | [diff] [blame] | 1003 | Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1004 | |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 1005 | for (auto Arg : InputArgs.filtered(STRIP_keep_section)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 1006 | if (Error E = Config.KeepSection.addMatcher(NameOrPattern::create( |
| 1007 | Arg->getValue(), SectionMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 1008 | return std::move(E); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 1009 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1010 | for (auto Arg : InputArgs.filtered(STRIP_remove_section)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 1011 | if (Error E = Config.ToRemove.addMatcher(NameOrPattern::create( |
| 1012 | Arg->getValue(), SectionMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 1013 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1014 | |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 1015 | for (auto Arg : InputArgs.filtered(STRIP_strip_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 1016 | if (Error E = Config.SymbolsToRemove.addMatcher(NameOrPattern::create( |
| 1017 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 1018 | return std::move(E); |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 1019 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1020 | for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) |
Jordan Rupprecht | edeebad | 2019-10-17 20:51:00 +0000 | [diff] [blame] | 1021 | if (Error E = Config.SymbolsToKeep.addMatcher(NameOrPattern::create( |
| 1022 | Arg->getValue(), SymbolMatchStyle, ErrorCallback))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 1023 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1024 | |
James Henderson | e4a89a1 | 2019-05-02 11:53:02 +0000 | [diff] [blame] | 1025 | if (!InputArgs.hasArg(STRIP_no_strip_all) && !Config.StripDebug && |
| 1026 | !Config.StripUnneeded && Config.DiscardMode == DiscardType::None && |
| 1027 | !Config.StripAllGNU && Config.SymbolsToRemove.empty()) |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 1028 | Config.StripAll = true; |
| 1029 | |
Fangrui Song | b14e9e3 | 2020-03-24 15:38:48 +0800 | [diff] [blame] | 1030 | if (Config.DiscardMode == DiscardType::All) { |
Sid Manning | 5ad18a7 | 2019-05-03 14:14:01 +0000 | [diff] [blame] | 1031 | Config.StripDebug = true; |
Fangrui Song | b14e9e3 | 2020-03-24 15:38:48 +0800 | [diff] [blame] | 1032 | Config.KeepFileSymbols = true; |
| 1033 | } |
Sid Manning | 5ad18a7 | 2019-05-03 14:14:01 +0000 | [diff] [blame] | 1034 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 1035 | Config.DeterministicArchives = |
| 1036 | InputArgs.hasFlag(STRIP_enable_deterministic_archives, |
| 1037 | STRIP_disable_deterministic_archives, /*default=*/true); |
| 1038 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1039 | Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates); |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 1040 | Config.InputFormat = FileFormat::Unspecified; |
| 1041 | Config.OutputFormat = FileFormat::Unspecified; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1042 | |
| 1043 | DriverConfig DC; |
| 1044 | if (Positional.size() == 1) { |
| 1045 | Config.InputFilename = Positional[0]; |
| 1046 | Config.OutputFilename = |
| 1047 | InputArgs.getLastArgValue(STRIP_output, Positional[0]); |
| 1048 | DC.CopyConfigs.push_back(std::move(Config)); |
| 1049 | } else { |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 1050 | StringMap<unsigned> InputFiles; |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 1051 | for (StringRef Filename : Positional) { |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 1052 | if (InputFiles[Filename]++ == 1) { |
| 1053 | if (Filename == "-") |
| 1054 | return createStringError( |
| 1055 | errc::invalid_argument, |
| 1056 | "cannot specify '-' as an input file more than once"); |
| 1057 | if (Error E = ErrorCallback(createStringError( |
| 1058 | errc::invalid_argument, "'%s' was already specified", |
| 1059 | Filename.str().c_str()))) |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 1060 | return std::move(E); |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 1061 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1062 | Config.InputFilename = Filename; |
| 1063 | Config.OutputFilename = Filename; |
| 1064 | DC.CopyConfigs.push_back(Config); |
| 1065 | } |
| 1066 | } |
| 1067 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 1068 | if (Config.PreserveDates && (is_contained(Positional, "-") || |
| 1069 | InputArgs.getLastArgValue(STRIP_output) == "-")) |
| 1070 | return createStringError(errc::invalid_argument, |
| 1071 | "--preserve-dates requires a file"); |
| 1072 | |
Bill Wendling | c55cf4a | 2020-02-10 07:06:45 -0800 | [diff] [blame] | 1073 | return std::move(DC); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | } // namespace objcopy |
| 1077 | } // namespace llvm |