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