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" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [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" |
| 17 | #include "llvm/Support/CommandLine.h" |
| 18 | #include "llvm/Support/Compression.h" |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Errc.h" |
James Henderson | 9df3883 | 2019-05-14 10:59:04 +0000 | [diff] [blame] | 20 | #include "llvm/Support/JamCRC.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 | |
| 66 | enum StripID { |
| 67 | STRIP_INVALID = 0, // This is not an option ID. |
| 68 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 69 | HELPTEXT, METAVAR, VALUES) \ |
| 70 | STRIP_##ID, |
| 71 | #include "StripOpts.inc" |
| 72 | #undef OPTION |
| 73 | }; |
| 74 | |
| 75 | #define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE; |
| 76 | #include "StripOpts.inc" |
| 77 | #undef PREFIX |
| 78 | |
| 79 | static const opt::OptTable::Info StripInfoTable[] = { |
| 80 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 81 | HELPTEXT, METAVAR, VALUES) \ |
| 82 | {STRIP_##PREFIX, NAME, HELPTEXT, \ |
| 83 | METAVAR, STRIP_##ID, opt::Option::KIND##Class, \ |
| 84 | PARAM, FLAGS, STRIP_##GROUP, \ |
| 85 | STRIP_##ALIAS, ALIASARGS, VALUES}, |
| 86 | #include "StripOpts.inc" |
| 87 | #undef OPTION |
| 88 | }; |
| 89 | |
| 90 | class StripOptTable : public opt::OptTable { |
| 91 | public: |
Jordan Rupprecht | aaeaa0a | 2018-10-23 18:46:33 +0000 | [diff] [blame] | 92 | StripOptTable() : OptTable(StripInfoTable) {} |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| 97 | static SectionFlag parseSectionRenameFlag(StringRef SectionName) { |
| 98 | return llvm::StringSwitch<SectionFlag>(SectionName) |
James Henderson | d931cf3 | 2019-04-03 14:40:27 +0000 | [diff] [blame] | 99 | .CaseLower("alloc", SectionFlag::SecAlloc) |
| 100 | .CaseLower("load", SectionFlag::SecLoad) |
| 101 | .CaseLower("noload", SectionFlag::SecNoload) |
| 102 | .CaseLower("readonly", SectionFlag::SecReadonly) |
| 103 | .CaseLower("debug", SectionFlag::SecDebug) |
| 104 | .CaseLower("code", SectionFlag::SecCode) |
| 105 | .CaseLower("data", SectionFlag::SecData) |
| 106 | .CaseLower("rom", SectionFlag::SecRom) |
| 107 | .CaseLower("merge", SectionFlag::SecMerge) |
| 108 | .CaseLower("strings", SectionFlag::SecStrings) |
| 109 | .CaseLower("contents", SectionFlag::SecContents) |
| 110 | .CaseLower("share", SectionFlag::SecShare) |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 111 | .Default(SectionFlag::SecNone); |
| 112 | } |
| 113 | |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 114 | static Expected<SectionFlag> |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 115 | parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) { |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 116 | SectionFlag ParsedFlags = SectionFlag::SecNone; |
| 117 | for (StringRef Flag : SectionFlags) { |
| 118 | SectionFlag ParsedFlag = parseSectionRenameFlag(Flag); |
| 119 | if (ParsedFlag == SectionFlag::SecNone) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 120 | return createStringError( |
| 121 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 122 | "unrecognized section flag '%s'. Flags supported for GNU " |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 123 | "compatibility: alloc, load, noload, readonly, debug, code, data, " |
| 124 | "rom, share, contents, merge, strings", |
| 125 | Flag.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 126 | ParsedFlags |= ParsedFlag; |
| 127 | } |
| 128 | |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 129 | return ParsedFlags; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 132 | static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 133 | if (!FlagValue.contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 134 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 135 | "bad format for --rename-section: missing '='"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 136 | |
| 137 | // Initial split: ".foo" = ".bar,f1,f2,..." |
| 138 | auto Old2New = FlagValue.split('='); |
| 139 | SectionRename SR; |
| 140 | SR.OriginalName = Old2New.first; |
| 141 | |
| 142 | // Flags split: ".bar" "f1" "f2" ... |
| 143 | SmallVector<StringRef, 6> NameAndFlags; |
| 144 | Old2New.second.split(NameAndFlags, ','); |
| 145 | SR.NewName = NameAndFlags[0]; |
| 146 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 147 | if (NameAndFlags.size() > 1) { |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 148 | Expected<SectionFlag> ParsedFlagSet = |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 149 | parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front()); |
| 150 | if (!ParsedFlagSet) |
| 151 | return ParsedFlagSet.takeError(); |
| 152 | SR.NewFlags = *ParsedFlagSet; |
| 153 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 154 | |
| 155 | return SR; |
| 156 | } |
| 157 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 158 | static Expected<SectionFlagsUpdate> |
| 159 | parseSetSectionFlagValue(StringRef FlagValue) { |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 160 | if (!StringRef(FlagValue).contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 161 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 162 | "bad format for --set-section-flags: missing '='"); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 163 | |
| 164 | // Initial split: ".foo" = "f1,f2,..." |
| 165 | auto Section2Flags = StringRef(FlagValue).split('='); |
| 166 | SectionFlagsUpdate SFU; |
| 167 | SFU.Name = Section2Flags.first; |
| 168 | |
| 169 | // Flags split: "f1" "f2" ... |
| 170 | SmallVector<StringRef, 6> SectionFlags; |
| 171 | Section2Flags.second.split(SectionFlags, ','); |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 172 | Expected<SectionFlag> ParsedFlagSet = parseSectionFlagSet(SectionFlags); |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 173 | if (!ParsedFlagSet) |
| 174 | return ParsedFlagSet.takeError(); |
| 175 | SFU.NewFlags = *ParsedFlagSet; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 176 | |
| 177 | return SFU; |
| 178 | } |
| 179 | |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 180 | static Expected<NewSymbolInfo> parseNewSymbolInfo(StringRef FlagValue) { |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 181 | // Parse value given with --add-symbol option and create the |
| 182 | // new symbol if possible. The value format for --add-symbol is: |
| 183 | // |
| 184 | // <name>=[<section>:]<value>[,<flags>] |
| 185 | // |
| 186 | // where: |
| 187 | // <name> - symbol name, can be empty string |
| 188 | // <section> - optional section name. If not given ABS symbol is created |
| 189 | // <value> - symbol value, can be decimal or hexadecimal number prefixed |
| 190 | // with 0x. |
| 191 | // <flags> - optional flags affecting symbol type, binding or visibility: |
| 192 | // The following are currently supported: |
| 193 | // |
| 194 | // global, local, weak, default, hidden, file, section, object, |
| 195 | // indirect-function. |
| 196 | // |
| 197 | // The following flags are ignored and provided for GNU |
| 198 | // compatibility only: |
| 199 | // |
| 200 | // warning, debug, constructor, indirect, synthetic, |
| 201 | // unique-object, before=<symbol>. |
| 202 | NewSymbolInfo SI; |
| 203 | StringRef Value; |
| 204 | std::tie(SI.SymbolName, Value) = FlagValue.split('='); |
| 205 | if (Value.empty()) |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 206 | return createStringError( |
| 207 | errc::invalid_argument, |
| 208 | "bad format for --add-symbol, missing '=' after '%s'", |
| 209 | SI.SymbolName.str().c_str()); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 210 | |
| 211 | if (Value.contains(':')) { |
| 212 | std::tie(SI.SectionName, Value) = Value.split(':'); |
| 213 | if (SI.SectionName.empty() || Value.empty()) |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 214 | return createStringError( |
| 215 | errc::invalid_argument, |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 216 | "bad format for --add-symbol, missing section name or symbol value"); |
| 217 | } |
| 218 | |
| 219 | SmallVector<StringRef, 6> Flags; |
| 220 | Value.split(Flags, ','); |
| 221 | if (Flags[0].getAsInteger(0, SI.Value)) |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 222 | return createStringError(errc::invalid_argument, "bad symbol value: '%s'", |
| 223 | Flags[0].str().c_str()); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 224 | |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 225 | using Functor = std::function<void(void)>; |
| 226 | SmallVector<StringRef, 6> UnsupportedFlags; |
| 227 | for (size_t I = 1, NumFlags = Flags.size(); I < NumFlags; ++I) |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 228 | static_cast<Functor>( |
| 229 | StringSwitch<Functor>(Flags[I]) |
| 230 | .CaseLower("global", [&SI] { SI.Bind = ELF::STB_GLOBAL; }) |
| 231 | .CaseLower("local", [&SI] { SI.Bind = ELF::STB_LOCAL; }) |
| 232 | .CaseLower("weak", [&SI] { SI.Bind = ELF::STB_WEAK; }) |
| 233 | .CaseLower("default", [&SI] { SI.Visibility = ELF::STV_DEFAULT; }) |
| 234 | .CaseLower("hidden", [&SI] { SI.Visibility = ELF::STV_HIDDEN; }) |
| 235 | .CaseLower("file", [&SI] { SI.Type = ELF::STT_FILE; }) |
| 236 | .CaseLower("section", [&SI] { SI.Type = ELF::STT_SECTION; }) |
| 237 | .CaseLower("object", [&SI] { SI.Type = ELF::STT_OBJECT; }) |
| 238 | .CaseLower("function", [&SI] { SI.Type = ELF::STT_FUNC; }) |
| 239 | .CaseLower("indirect-function", |
| 240 | [&SI] { SI.Type = ELF::STT_GNU_IFUNC; }) |
| 241 | .CaseLower("debug", [] {}) |
| 242 | .CaseLower("constructor", [] {}) |
| 243 | .CaseLower("warning", [] {}) |
| 244 | .CaseLower("indirect", [] {}) |
| 245 | .CaseLower("synthetic", [] {}) |
| 246 | .CaseLower("unique-object", [] {}) |
| 247 | .StartsWithLower("before", [] {}) |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 248 | .Default([&] { UnsupportedFlags.push_back(Flags[I]); }))(); |
| 249 | if (!UnsupportedFlags.empty()) |
| 250 | return createStringError(errc::invalid_argument, |
| 251 | "unsupported flag%s for --add-symbol: '%s'", |
| 252 | UnsupportedFlags.size() > 1 ? "s" : "", |
| 253 | join(UnsupportedFlags, "', '").c_str()); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 254 | return SI; |
| 255 | } |
| 256 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 257 | static const StringMap<MachineInfo> ArchMap{ |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 258 | // Name, {EMachine, 64bit, LittleEndian} |
| 259 | {"aarch64", {ELF::EM_AARCH64, true, true}}, |
| 260 | {"arm", {ELF::EM_ARM, false, true}}, |
| 261 | {"i386", {ELF::EM_386, false, true}}, |
| 262 | {"i386:x86-64", {ELF::EM_X86_64, true, true}}, |
Jordan Rupprecht | 2b32902 | 2019-04-18 14:22:37 +0000 | [diff] [blame] | 263 | {"mips", {ELF::EM_MIPS, false, false}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 264 | {"powerpc:common64", {ELF::EM_PPC64, true, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 265 | {"riscv:rv32", {ELF::EM_RISCV, false, true}}, |
| 266 | {"riscv:rv64", {ELF::EM_RISCV, true, true}}, |
Seiya Nuta | b1027a4 | 2019-06-13 23:24:12 +0000 | [diff] [blame] | 267 | {"sparc", {ELF::EM_SPARC, false, false}}, |
| 268 | {"sparcel", {ELF::EM_SPARC, false, true}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 269 | {"x86-64", {ELF::EM_X86_64, true, true}}, |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 272 | static Expected<const MachineInfo &> getMachineInfo(StringRef Arch) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 273 | auto Iter = ArchMap.find(Arch); |
| 274 | if (Iter == std::end(ArchMap)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 275 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 276 | "invalid architecture: '%s'", Arch.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 277 | return Iter->getValue(); |
| 278 | } |
| 279 | |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 280 | // FIXME: consolidate with the bfd parsing used by lld. |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 281 | static const StringMap<MachineInfo> OutputFormatMap{ |
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}}, |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 315 | static Expected<MachineInfo> getOutputFormatMachineInfo(StringRef Format) { |
| 316 | StringRef OriginalFormat = Format; |
| 317 | bool IsFreeBSD = Format.consume_back("-freebsd"); |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 318 | auto Iter = OutputFormatMap.find(Format); |
| 319 | if (Iter == std::end(OutputFormatMap)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 320 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 321 | "invalid output format: '%s'", |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 322 | OriginalFormat.str().c_str()); |
| 323 | MachineInfo MI = Iter->getValue(); |
| 324 | if (IsFreeBSD) |
| 325 | MI.OSABI = ELF::ELFOSABI_FREEBSD; |
| 326 | return {MI}; |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 329 | static Error addSymbolsFromFile(std::vector<NameOrRegex> &Symbols, |
| 330 | BumpPtrAllocator &Alloc, StringRef Filename, |
| 331 | bool UseRegex) { |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 332 | StringSaver Saver(Alloc); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 333 | SmallVector<StringRef, 16> Lines; |
| 334 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 335 | if (!BufOrErr) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 336 | return createFileError(Filename, BufOrErr.getError()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 337 | |
| 338 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 339 | for (StringRef Line : Lines) { |
| 340 | // Ignore everything after '#', trim whitespace, and only add the symbol if |
| 341 | // it's not empty. |
| 342 | auto TrimmedLine = Line.split('#').first.trim(); |
| 343 | if (!TrimmedLine.empty()) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 344 | Symbols.emplace_back(Saver.save(TrimmedLine), UseRegex); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 345 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 346 | |
| 347 | return Error::success(); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 350 | NameOrRegex::NameOrRegex(StringRef Pattern, bool IsRegex) { |
| 351 | if (!IsRegex) { |
| 352 | Name = Pattern; |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | SmallVector<char, 32> Data; |
| 357 | R = std::make_shared<Regex>( |
| 358 | ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)); |
| 359 | } |
| 360 | |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 361 | static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename, |
| 362 | BumpPtrAllocator &Alloc, |
| 363 | StringRef Filename) { |
| 364 | StringSaver Saver(Alloc); |
| 365 | SmallVector<StringRef, 16> Lines; |
| 366 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 367 | if (!BufOrErr) |
Eugene Leviant | 317f9e7 | 2019-02-11 09:49:37 +0000 | [diff] [blame] | 368 | return createFileError(Filename, BufOrErr.getError()); |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 369 | |
| 370 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 371 | size_t NumLines = Lines.size(); |
| 372 | for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) { |
| 373 | StringRef TrimmedLine = Lines[LineNo].split('#').first.trim(); |
| 374 | if (TrimmedLine.empty()) |
| 375 | continue; |
| 376 | |
| 377 | std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' '); |
| 378 | StringRef NewName = Pair.second.trim(); |
| 379 | if (NewName.empty()) |
| 380 | return createStringError(errc::invalid_argument, |
| 381 | "%s:%zu: missing new symbol name", |
| 382 | Filename.str().c_str(), LineNo + 1); |
| 383 | SymbolsToRename.insert({Pair.first, NewName}); |
| 384 | } |
| 385 | return Error::success(); |
| 386 | } |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 387 | |
| 388 | template <class T> static ErrorOr<T> getAsInteger(StringRef Val) { |
| 389 | T Result; |
| 390 | if (Val.getAsInteger(0, Result)) |
| 391 | return errc::invalid_argument; |
| 392 | return Result; |
| 393 | } |
| 394 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 395 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 396 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 397 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 398 | Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) { |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 399 | DriverConfig DC; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 400 | ObjcopyOptTable T; |
| 401 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 402 | llvm::opt::InputArgList InputArgs = |
| 403 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 404 | |
| 405 | if (InputArgs.size() == 0) { |
| 406 | T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool"); |
| 407 | exit(1); |
| 408 | } |
| 409 | |
| 410 | if (InputArgs.hasArg(OBJCOPY_help)) { |
| 411 | T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool"); |
| 412 | exit(0); |
| 413 | } |
| 414 | |
| 415 | if (InputArgs.hasArg(OBJCOPY_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 416 | outs() << "llvm-objcopy, compatible with GNU objcopy\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 417 | cl::PrintVersionMessage(); |
| 418 | exit(0); |
| 419 | } |
| 420 | |
| 421 | SmallVector<const char *, 2> Positional; |
| 422 | |
| 423 | for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 424 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 425 | Arg->getAsString(InputArgs).c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 426 | |
| 427 | for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT)) |
| 428 | Positional.push_back(Arg->getValue()); |
| 429 | |
| 430 | if (Positional.empty()) |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 431 | return createStringError(errc::invalid_argument, "no input file specified"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 432 | |
| 433 | if (Positional.size() > 2) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 434 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 435 | "too many positional arguments"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 436 | |
| 437 | CopyConfig Config; |
| 438 | Config.InputFilename = Positional[0]; |
| 439 | Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 440 | if (InputArgs.hasArg(OBJCOPY_target) && |
| 441 | (InputArgs.hasArg(OBJCOPY_input_target) || |
| 442 | InputArgs.hasArg(OBJCOPY_output_target))) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 443 | return createStringError( |
| 444 | errc::invalid_argument, |
| 445 | "--target cannot be used with --input-target or --output-target"); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 446 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 447 | bool UseRegex = InputArgs.hasArg(OBJCOPY_regex); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 448 | if (InputArgs.hasArg(OBJCOPY_target)) { |
| 449 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 450 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 451 | } else { |
| 452 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target); |
| 453 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); |
| 454 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 455 | if (Config.InputFormat == "binary") { |
| 456 | auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture); |
| 457 | if (BinaryArch.empty()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 458 | return createStringError( |
| 459 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 460 | "specified binary input without specifiying an architecture"); |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 461 | Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch); |
| 462 | if (!MI) |
| 463 | return MI.takeError(); |
| 464 | Config.BinaryArch = *MI; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 465 | } |
Eugene Leviant | a6fb183 | 2019-05-29 11:37:16 +0000 | [diff] [blame] | 466 | if (!Config.OutputFormat.empty() && Config.OutputFormat != "binary" && |
| 467 | Config.OutputFormat != "ihex") { |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 468 | Expected<MachineInfo> MI = getOutputFormatMachineInfo(Config.OutputFormat); |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 469 | if (!MI) |
| 470 | return MI.takeError(); |
| 471 | Config.OutputArch = *MI; |
| 472 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 473 | |
| 474 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections, |
| 475 | OBJCOPY_compress_debug_sections_eq)) { |
| 476 | Config.CompressionType = DebugCompressionType::Z; |
| 477 | |
| 478 | if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) { |
| 479 | Config.CompressionType = |
| 480 | StringSwitch<DebugCompressionType>( |
| 481 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)) |
| 482 | .Case("zlib-gnu", DebugCompressionType::GNU) |
| 483 | .Case("zlib", DebugCompressionType::Z) |
| 484 | .Default(DebugCompressionType::None); |
| 485 | if (Config.CompressionType == DebugCompressionType::None) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 486 | return createStringError( |
| 487 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 488 | "invalid or unsupported --compress-debug-sections format: %s", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 489 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq) |
| 490 | .str() |
| 491 | .c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 492 | } |
George Rimar | 1e93080 | 2019-03-05 11:32:14 +0000 | [diff] [blame] | 493 | if (!zlib::isAvailable()) |
| 494 | return createStringError( |
| 495 | errc::invalid_argument, |
| 496 | "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 499 | Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); |
James Henderson | 9df3883 | 2019-05-14 10:59:04 +0000 | [diff] [blame] | 500 | // The gnu_debuglink's target is expected to not change or else its CRC would |
| 501 | // become invalidated and get rejected. We can avoid recalculating the |
| 502 | // checksum for every target file inside an archive by precomputing the CRC |
| 503 | // here. This prevents a significant amount of I/O. |
| 504 | if (!Config.AddGnuDebugLink.empty()) { |
| 505 | auto DebugOrErr = MemoryBuffer::getFile(Config.AddGnuDebugLink); |
| 506 | if (!DebugOrErr) |
| 507 | return createFileError(Config.AddGnuDebugLink, DebugOrErr.getError()); |
| 508 | auto Debug = std::move(*DebugOrErr); |
| 509 | JamCRC CRC; |
| 510 | CRC.update( |
| 511 | ArrayRef<char>(Debug->getBuffer().data(), Debug->getBuffer().size())); |
| 512 | // The CRC32 value needs to be complemented because the JamCRC doesn't |
| 513 | // finalize the CRC32 value. |
| 514 | Config.GnuDebugLinkCRC32 = ~CRC.getCRC(); |
| 515 | } |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 516 | Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir); |
| 517 | if (InputArgs.hasArg(OBJCOPY_build_id_link_input)) |
| 518 | Config.BuildIdLinkInput = |
| 519 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_input); |
| 520 | if (InputArgs.hasArg(OBJCOPY_build_id_link_output)) |
| 521 | Config.BuildIdLinkOutput = |
| 522 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_output); |
| 523 | Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 524 | Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols); |
James Henderson | fa11fb3 | 2019-05-08 09:49:35 +0000 | [diff] [blame] | 525 | Config.AllocSectionsPrefix = |
| 526 | InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections); |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 527 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_extract_partition)) |
| 528 | Config.ExtractPartition = Arg->getValue(); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 529 | |
| 530 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { |
| 531 | if (!StringRef(Arg->getValue()).contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 532 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 533 | "bad format for --redefine-sym"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 534 | auto Old2New = StringRef(Arg->getValue()).split('='); |
| 535 | if (!Config.SymbolsToRename.insert(Old2New).second) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 536 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 537 | "multiple redefinition of symbol '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 538 | Old2New.first.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 541 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols)) |
| 542 | if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc, |
| 543 | Arg->getValue())) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 544 | return std::move(E); |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 545 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 546 | for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 547 | Expected<SectionRename> SR = |
| 548 | parseRenameSectionValue(StringRef(Arg->getValue())); |
| 549 | if (!SR) |
| 550 | return SR.takeError(); |
| 551 | if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second) |
| 552 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 553 | "multiple renames of section '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 554 | SR->OriginalName.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 555 | } |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 556 | for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 557 | Expected<SectionFlagsUpdate> SFU = |
| 558 | parseSetSectionFlagValue(Arg->getValue()); |
| 559 | if (!SFU) |
| 560 | return SFU.takeError(); |
| 561 | if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second) |
| 562 | return createStringError( |
| 563 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 564 | "--set-section-flags set multiple times for section '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 565 | SFU->Name.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 566 | } |
| 567 | // Prohibit combinations of --set-section-flags when the section name is used |
| 568 | // by --rename-section, either as a source or a destination. |
| 569 | for (const auto &E : Config.SectionsToRename) { |
| 570 | const SectionRename &SR = E.second; |
| 571 | if (Config.SetSectionFlags.count(SR.OriginalName)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 572 | return createStringError( |
| 573 | errc::invalid_argument, |
| 574 | "--set-section-flags=%s conflicts with --rename-section=%s=%s", |
| 575 | SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(), |
| 576 | SR.NewName.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 577 | if (Config.SetSectionFlags.count(SR.NewName)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 578 | return createStringError( |
| 579 | errc::invalid_argument, |
| 580 | "--set-section-flags=%s conflicts with --rename-section=%s=%s", |
| 581 | SR.NewName.str().c_str(), SR.OriginalName.str().c_str(), |
| 582 | SR.NewName.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 583 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 584 | |
| 585 | for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 586 | Config.ToRemove.emplace_back(Arg->getValue(), UseRegex); |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 587 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 588 | Config.KeepSection.emplace_back(Arg->getValue(), UseRegex); |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 589 | for (auto Arg : InputArgs.filtered(OBJCOPY_only_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 590 | Config.OnlySection.emplace_back(Arg->getValue(), UseRegex); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 591 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) |
| 592 | Config.AddSection.push_back(Arg->getValue()); |
| 593 | for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section)) |
| 594 | Config.DumpSection.push_back(Arg->getValue()); |
| 595 | Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all); |
| 596 | Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu); |
| 597 | Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug); |
| 598 | Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); |
| 599 | Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); |
| 600 | Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); |
| 601 | Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); |
| 602 | Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 603 | Config.ExtractMainPartition = |
| 604 | InputArgs.hasArg(OBJCOPY_extract_main_partition); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 605 | Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); |
| 606 | Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 607 | if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals)) |
| 608 | Config.DiscardMode = |
| 609 | InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals) |
| 610 | ? DiscardType::All |
| 611 | : DiscardType::Locals; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 612 | Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); |
| 613 | Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); |
| 614 | Config.DecompressDebugSections = |
| 615 | InputArgs.hasArg(OBJCOPY_decompress_debug_sections); |
Sid Manning | 5ad18a7 | 2019-05-03 14:14:01 +0000 | [diff] [blame] | 616 | if (Config.DiscardMode == DiscardType::All) |
| 617 | Config.StripDebug = true; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 618 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 619 | Config.SymbolsToLocalize.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 620 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 621 | if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, DC.Alloc, |
| 622 | Arg->getValue(), UseRegex)) |
| 623 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 624 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 625 | Config.SymbolsToKeepGlobal.emplace_back(Arg->getValue(), UseRegex); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 626 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 627 | if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc, |
| 628 | Arg->getValue(), UseRegex)) |
| 629 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 630 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 631 | Config.SymbolsToGlobalize.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 632 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 633 | if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc, |
| 634 | Arg->getValue(), UseRegex)) |
| 635 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 636 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 637 | Config.SymbolsToWeaken.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 638 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 639 | if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, DC.Alloc, |
| 640 | Arg->getValue(), UseRegex)) |
| 641 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 642 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 643 | Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 644 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 645 | if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc, |
| 646 | Arg->getValue(), UseRegex)) |
| 647 | return std::move(E); |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 648 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol)) |
| 649 | Config.UnneededSymbolsToRemove.emplace_back(Arg->getValue(), UseRegex); |
| 650 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 651 | if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc, |
| 652 | Arg->getValue(), UseRegex)) |
| 653 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 654 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 655 | Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegex); |
Yi Kong | f2baddb | 2019-04-01 18:12:43 +0000 | [diff] [blame] | 656 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbols)) |
| 657 | if (Error E = addSymbolsFromFile(Config.SymbolsToKeep, DC.Alloc, |
| 658 | Arg->getValue(), UseRegex)) |
| 659 | return std::move(E); |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 660 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_symbol)) { |
| 661 | Expected<NewSymbolInfo> NSI = parseNewSymbolInfo(Arg->getValue()); |
| 662 | if (!NSI) |
| 663 | return NSI.takeError(); |
| 664 | Config.SymbolsToAdd.push_back(*NSI); |
| 665 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 666 | |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 667 | Config.AllowBrokenLinks = InputArgs.hasArg(OBJCOPY_allow_broken_links); |
| 668 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 669 | Config.DeterministicArchives = InputArgs.hasFlag( |
| 670 | OBJCOPY_enable_deterministic_archives, |
| 671 | OBJCOPY_disable_deterministic_archives, /*default=*/true); |
| 672 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 673 | Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); |
| 674 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 675 | if (Config.PreserveDates && |
| 676 | (Config.OutputFilename == "-" || Config.InputFilename == "-")) |
| 677 | return createStringError(errc::invalid_argument, |
| 678 | "--preserve-dates requires a file"); |
| 679 | |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 680 | for (auto Arg : InputArgs) |
| 681 | if (Arg->getOption().matches(OBJCOPY_set_start)) { |
| 682 | auto EAddr = getAsInteger<uint64_t>(Arg->getValue()); |
| 683 | if (!EAddr) |
| 684 | return createStringError( |
| 685 | EAddr.getError(), "bad entry point address: '%s'", Arg->getValue()); |
| 686 | |
| 687 | Config.EntryExpr = [EAddr](uint64_t) { return *EAddr; }; |
| 688 | } else if (Arg->getOption().matches(OBJCOPY_change_start)) { |
| 689 | auto EIncr = getAsInteger<int64_t>(Arg->getValue()); |
| 690 | if (!EIncr) |
| 691 | return createStringError(EIncr.getError(), |
| 692 | "bad entry point increment: '%s'", |
| 693 | Arg->getValue()); |
| 694 | auto Expr = Config.EntryExpr ? std::move(Config.EntryExpr) |
| 695 | : [](uint64_t A) { return A; }; |
| 696 | Config.EntryExpr = [Expr, EIncr](uint64_t EAddr) { |
| 697 | return Expr(EAddr) + *EIncr; |
| 698 | }; |
| 699 | } |
| 700 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 701 | if (Config.DecompressDebugSections && |
| 702 | Config.CompressionType != DebugCompressionType::None) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 703 | return createStringError( |
| 704 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 705 | "cannot specify both --compress-debug-sections and " |
| 706 | "--decompress-debug-sections"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (Config.DecompressDebugSections && !zlib::isAvailable()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 710 | return createStringError( |
| 711 | errc::invalid_argument, |
| 712 | "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 713 | |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 714 | if (Config.ExtractPartition && Config.ExtractMainPartition) |
| 715 | return createStringError(errc::invalid_argument, |
| 716 | "cannot specify --extract-partition together with " |
| 717 | "--extract-main-partition"); |
| 718 | |
Jordan Rupprecht | ab9f662 | 2018-10-23 20:54:51 +0000 | [diff] [blame] | 719 | DC.CopyConfigs.push_back(std::move(Config)); |
Jordan Rupprecht | 93ad8b3 | 2019-02-21 17:24:55 +0000 | [diff] [blame] | 720 | return std::move(DC); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 724 | // help flag is set then ParseStripOptions will print the help messege and |
| 725 | // exit. |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame^] | 726 | Expected<DriverConfig> |
| 727 | parseStripOptions(ArrayRef<const char *> ArgsArr, |
| 728 | std::function<Error(Error)> ErrorCallback) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 729 | StripOptTable T; |
| 730 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 731 | llvm::opt::InputArgList InputArgs = |
| 732 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 733 | |
| 734 | if (InputArgs.size() == 0) { |
| 735 | T.PrintHelp(errs(), "llvm-strip [options] file...", "strip tool"); |
| 736 | exit(1); |
| 737 | } |
| 738 | |
| 739 | if (InputArgs.hasArg(STRIP_help)) { |
| 740 | T.PrintHelp(outs(), "llvm-strip [options] file...", "strip tool"); |
| 741 | exit(0); |
| 742 | } |
| 743 | |
| 744 | if (InputArgs.hasArg(STRIP_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 745 | outs() << "llvm-strip, compatible with GNU strip\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 746 | cl::PrintVersionMessage(); |
| 747 | exit(0); |
| 748 | } |
| 749 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 750 | SmallVector<StringRef, 2> Positional; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 751 | for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 752 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 753 | Arg->getAsString(InputArgs).c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 754 | for (auto Arg : InputArgs.filtered(STRIP_INPUT)) |
| 755 | Positional.push_back(Arg->getValue()); |
| 756 | |
| 757 | if (Positional.empty()) |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 758 | return createStringError(errc::invalid_argument, "no input file specified"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 759 | |
| 760 | if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 761 | return createStringError( |
| 762 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 763 | "multiple input files cannot be used in combination with -o"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 764 | |
| 765 | CopyConfig Config; |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 766 | bool UseRegexp = InputArgs.hasArg(STRIP_regex); |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 767 | Config.AllowBrokenLinks = InputArgs.hasArg(STRIP_allow_broken_links); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 768 | Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug); |
| 769 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 770 | if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals)) |
| 771 | Config.DiscardMode = |
| 772 | InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals) |
| 773 | ? DiscardType::All |
| 774 | : DiscardType::Locals; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 775 | Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); |
James Henderson | e4a89a1 | 2019-05-02 11:53:02 +0000 | [diff] [blame] | 776 | if (auto Arg = InputArgs.getLastArg(STRIP_strip_all, STRIP_no_strip_all)) |
| 777 | Config.StripAll = Arg->getOption().getID() == STRIP_strip_all; |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 778 | Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu); |
Jordan Rupprecht | 12ed01d | 2019-03-14 21:51:42 +0000 | [diff] [blame] | 779 | Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug); |
Eugene Leviant | 05a3f99 | 2019-02-01 15:25:15 +0000 | [diff] [blame] | 780 | Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 781 | |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 782 | for (auto Arg : InputArgs.filtered(STRIP_keep_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 783 | Config.KeepSection.emplace_back(Arg->getValue(), UseRegexp); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 784 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 785 | for (auto Arg : InputArgs.filtered(STRIP_remove_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 786 | Config.ToRemove.emplace_back(Arg->getValue(), UseRegexp); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 787 | |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 788 | for (auto Arg : InputArgs.filtered(STRIP_strip_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 789 | Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegexp); |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 790 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 791 | for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 792 | Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegexp); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 793 | |
James Henderson | e4a89a1 | 2019-05-02 11:53:02 +0000 | [diff] [blame] | 794 | if (!InputArgs.hasArg(STRIP_no_strip_all) && !Config.StripDebug && |
| 795 | !Config.StripUnneeded && Config.DiscardMode == DiscardType::None && |
| 796 | !Config.StripAllGNU && Config.SymbolsToRemove.empty()) |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 797 | Config.StripAll = true; |
| 798 | |
Sid Manning | 5ad18a7 | 2019-05-03 14:14:01 +0000 | [diff] [blame] | 799 | if (Config.DiscardMode == DiscardType::All) |
| 800 | Config.StripDebug = true; |
| 801 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 802 | Config.DeterministicArchives = |
| 803 | InputArgs.hasFlag(STRIP_enable_deterministic_archives, |
| 804 | STRIP_disable_deterministic_archives, /*default=*/true); |
| 805 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 806 | Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates); |
| 807 | |
| 808 | DriverConfig DC; |
| 809 | if (Positional.size() == 1) { |
| 810 | Config.InputFilename = Positional[0]; |
| 811 | Config.OutputFilename = |
| 812 | InputArgs.getLastArgValue(STRIP_output, Positional[0]); |
| 813 | DC.CopyConfigs.push_back(std::move(Config)); |
| 814 | } else { |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame^] | 815 | StringMap<unsigned> InputFiles; |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 816 | for (StringRef Filename : Positional) { |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame^] | 817 | if (InputFiles[Filename]++ == 1) { |
| 818 | if (Filename == "-") |
| 819 | return createStringError( |
| 820 | errc::invalid_argument, |
| 821 | "cannot specify '-' as an input file more than once"); |
| 822 | if (Error E = ErrorCallback(createStringError( |
| 823 | errc::invalid_argument, "'%s' was already specified", |
| 824 | Filename.str().c_str()))) |
| 825 | return std::move(E); |
| 826 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 827 | Config.InputFilename = Filename; |
| 828 | Config.OutputFilename = Filename; |
| 829 | DC.CopyConfigs.push_back(Config); |
| 830 | } |
| 831 | } |
| 832 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 833 | if (Config.PreserveDates && (is_contained(Positional, "-") || |
| 834 | InputArgs.getLastArgValue(STRIP_output) == "-")) |
| 835 | return createStringError(errc::invalid_argument, |
| 836 | "--preserve-dates requires a file"); |
| 837 | |
Jordan Rupprecht | 93ad8b3 | 2019-02-21 17:24:55 +0000 | [diff] [blame] | 838 | return std::move(DC); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | } // namespace objcopy |
| 842 | } // namespace llvm |