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 | |
Chris Jackson | fa1fe93 | 2019-08-30 10:17:16 +0000 | [diff] [blame] | 180 | static Expected<NewSymbolInfo> parseNewSymbolInfo(StringRef FlagValue, |
| 181 | uint8_t DefaultVisibility) { |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 182 | // Parse value given with --add-symbol option and create the |
| 183 | // new symbol if possible. The value format for --add-symbol is: |
| 184 | // |
| 185 | // <name>=[<section>:]<value>[,<flags>] |
| 186 | // |
| 187 | // where: |
| 188 | // <name> - symbol name, can be empty string |
| 189 | // <section> - optional section name. If not given ABS symbol is created |
| 190 | // <value> - symbol value, can be decimal or hexadecimal number prefixed |
| 191 | // with 0x. |
| 192 | // <flags> - optional flags affecting symbol type, binding or visibility: |
| 193 | // The following are currently supported: |
| 194 | // |
| 195 | // global, local, weak, default, hidden, file, section, object, |
| 196 | // indirect-function. |
| 197 | // |
| 198 | // The following flags are ignored and provided for GNU |
| 199 | // compatibility only: |
| 200 | // |
| 201 | // warning, debug, constructor, indirect, synthetic, |
| 202 | // unique-object, before=<symbol>. |
| 203 | NewSymbolInfo SI; |
| 204 | StringRef Value; |
| 205 | std::tie(SI.SymbolName, Value) = FlagValue.split('='); |
| 206 | if (Value.empty()) |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 207 | return createStringError( |
| 208 | errc::invalid_argument, |
| 209 | "bad format for --add-symbol, missing '=' after '%s'", |
| 210 | SI.SymbolName.str().c_str()); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 211 | |
| 212 | if (Value.contains(':')) { |
| 213 | std::tie(SI.SectionName, Value) = Value.split(':'); |
| 214 | if (SI.SectionName.empty() || Value.empty()) |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 215 | return createStringError( |
| 216 | errc::invalid_argument, |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 217 | "bad format for --add-symbol, missing section name or symbol value"); |
| 218 | } |
| 219 | |
| 220 | SmallVector<StringRef, 6> Flags; |
| 221 | Value.split(Flags, ','); |
| 222 | if (Flags[0].getAsInteger(0, SI.Value)) |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 223 | return createStringError(errc::invalid_argument, "bad symbol value: '%s'", |
| 224 | Flags[0].str().c_str()); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 225 | |
Chris Jackson | fa1fe93 | 2019-08-30 10:17:16 +0000 | [diff] [blame] | 226 | SI.Visibility = DefaultVisibility; |
| 227 | |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 228 | using Functor = std::function<void(void)>; |
| 229 | SmallVector<StringRef, 6> UnsupportedFlags; |
| 230 | for (size_t I = 1, NumFlags = Flags.size(); I < NumFlags; ++I) |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 231 | static_cast<Functor>( |
| 232 | StringSwitch<Functor>(Flags[I]) |
| 233 | .CaseLower("global", [&SI] { SI.Bind = ELF::STB_GLOBAL; }) |
| 234 | .CaseLower("local", [&SI] { SI.Bind = ELF::STB_LOCAL; }) |
| 235 | .CaseLower("weak", [&SI] { SI.Bind = ELF::STB_WEAK; }) |
| 236 | .CaseLower("default", [&SI] { SI.Visibility = ELF::STV_DEFAULT; }) |
| 237 | .CaseLower("hidden", [&SI] { SI.Visibility = ELF::STV_HIDDEN; }) |
Chris Jackson | e5cdfbc | 2019-08-15 09:45:09 +0000 | [diff] [blame] | 238 | .CaseLower("protected", [&SI] { SI.Visibility = ELF::STV_PROTECTED; }) |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 239 | .CaseLower("file", [&SI] { SI.Type = ELF::STT_FILE; }) |
| 240 | .CaseLower("section", [&SI] { SI.Type = ELF::STT_SECTION; }) |
| 241 | .CaseLower("object", [&SI] { SI.Type = ELF::STT_OBJECT; }) |
| 242 | .CaseLower("function", [&SI] { SI.Type = ELF::STT_FUNC; }) |
| 243 | .CaseLower("indirect-function", |
| 244 | [&SI] { SI.Type = ELF::STT_GNU_IFUNC; }) |
| 245 | .CaseLower("debug", [] {}) |
| 246 | .CaseLower("constructor", [] {}) |
| 247 | .CaseLower("warning", [] {}) |
| 248 | .CaseLower("indirect", [] {}) |
| 249 | .CaseLower("synthetic", [] {}) |
| 250 | .CaseLower("unique-object", [] {}) |
| 251 | .StartsWithLower("before", [] {}) |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 252 | .Default([&] { UnsupportedFlags.push_back(Flags[I]); }))(); |
| 253 | if (!UnsupportedFlags.empty()) |
| 254 | return createStringError(errc::invalid_argument, |
| 255 | "unsupported flag%s for --add-symbol: '%s'", |
| 256 | UnsupportedFlags.size() > 1 ? "s" : "", |
| 257 | join(UnsupportedFlags, "', '").c_str()); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 258 | return SI; |
| 259 | } |
| 260 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 261 | static const StringMap<MachineInfo> ArchMap{ |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 262 | // Name, {EMachine, 64bit, LittleEndian} |
| 263 | {"aarch64", {ELF::EM_AARCH64, true, true}}, |
| 264 | {"arm", {ELF::EM_ARM, false, true}}, |
| 265 | {"i386", {ELF::EM_386, false, true}}, |
| 266 | {"i386:x86-64", {ELF::EM_X86_64, true, true}}, |
Jordan Rupprecht | 2b32902 | 2019-04-18 14:22:37 +0000 | [diff] [blame] | 267 | {"mips", {ELF::EM_MIPS, false, false}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 268 | {"powerpc:common64", {ELF::EM_PPC64, true, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 269 | {"riscv:rv32", {ELF::EM_RISCV, false, true}}, |
| 270 | {"riscv:rv64", {ELF::EM_RISCV, true, true}}, |
Seiya Nuta | b1027a4 | 2019-06-13 23:24:12 +0000 | [diff] [blame] | 271 | {"sparc", {ELF::EM_SPARC, false, false}}, |
| 272 | {"sparcel", {ELF::EM_SPARC, false, true}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 273 | {"x86-64", {ELF::EM_X86_64, true, true}}, |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 276 | static Expected<const MachineInfo &> getMachineInfo(StringRef Arch) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 277 | auto Iter = ArchMap.find(Arch); |
| 278 | if (Iter == std::end(ArchMap)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 279 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 280 | "invalid architecture: '%s'", Arch.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 281 | return Iter->getValue(); |
| 282 | } |
| 283 | |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 284 | struct TargetInfo { |
| 285 | FileFormat Format; |
| 286 | MachineInfo Machine; |
| 287 | }; |
| 288 | |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 289 | // FIXME: consolidate with the bfd parsing used by lld. |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 290 | static const StringMap<MachineInfo> TargetMap{ |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 291 | // Name, {EMachine, 64bit, LittleEndian} |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 292 | // x86 |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 293 | {"elf32-i386", {ELF::EM_386, false, true}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 294 | {"elf32-x86-64", {ELF::EM_X86_64, false, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 295 | {"elf64-x86-64", {ELF::EM_X86_64, true, true}}, |
| 296 | // Intel MCU |
| 297 | {"elf32-iamcu", {ELF::EM_IAMCU, false, true}}, |
| 298 | // ARM |
| 299 | {"elf32-littlearm", {ELF::EM_ARM, false, true}}, |
| 300 | // ARM AArch64 |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 301 | {"elf64-aarch64", {ELF::EM_AARCH64, true, true}}, |
| 302 | {"elf64-littleaarch64", {ELF::EM_AARCH64, true, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 303 | // RISC-V |
| 304 | {"elf32-littleriscv", {ELF::EM_RISCV, false, true}}, |
| 305 | {"elf64-littleriscv", {ELF::EM_RISCV, true, true}}, |
| 306 | // PowerPC |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 307 | {"elf32-powerpc", {ELF::EM_PPC, false, false}}, |
| 308 | {"elf32-powerpcle", {ELF::EM_PPC, false, true}}, |
| 309 | {"elf64-powerpc", {ELF::EM_PPC64, true, false}}, |
| 310 | {"elf64-powerpcle", {ELF::EM_PPC64, true, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 311 | // MIPS |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 312 | {"elf32-bigmips", {ELF::EM_MIPS, false, false}}, |
| 313 | {"elf32-ntradbigmips", {ELF::EM_MIPS, false, false}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 314 | {"elf32-ntradlittlemips", {ELF::EM_MIPS, false, true}}, |
Jordan Rupprecht | 96bbb1d | 2019-04-30 15:21:36 +0000 | [diff] [blame] | 315 | {"elf32-tradbigmips", {ELF::EM_MIPS, false, false}}, |
| 316 | {"elf32-tradlittlemips", {ELF::EM_MIPS, false, true}}, |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 317 | {"elf64-tradbigmips", {ELF::EM_MIPS, true, false}}, |
| 318 | {"elf64-tradlittlemips", {ELF::EM_MIPS, true, true}}, |
Seiya Nuta | 13de174 | 2019-06-17 02:03:45 +0000 | [diff] [blame] | 319 | // SPARC |
| 320 | {"elf32-sparc", {ELF::EM_SPARC, false, false}}, |
| 321 | {"elf32-sparcel", {ELF::EM_SPARC, false, true}}, |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 324 | static Expected<TargetInfo> |
| 325 | getOutputTargetInfoByTargetName(StringRef TargetName) { |
| 326 | StringRef OriginalTargetName = TargetName; |
| 327 | bool IsFreeBSD = TargetName.consume_back("-freebsd"); |
| 328 | auto Iter = TargetMap.find(TargetName); |
| 329 | if (Iter == std::end(TargetMap)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 330 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 331 | "invalid output format: '%s'", |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 332 | OriginalTargetName.str().c_str()); |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 333 | MachineInfo MI = Iter->getValue(); |
| 334 | if (IsFreeBSD) |
| 335 | MI.OSABI = ELF::ELFOSABI_FREEBSD; |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 336 | |
| 337 | FileFormat Format; |
| 338 | if (TargetName.startswith("elf")) |
| 339 | Format = FileFormat::ELF; |
| 340 | else |
| 341 | // This should never happen because `TargetName` is valid (it certainly |
| 342 | // exists in the TargetMap). |
| 343 | llvm_unreachable("unknown target prefix"); |
| 344 | |
| 345 | return {TargetInfo{Format, MI}}; |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 348 | static Error addSymbolsFromFile(NameMatcher &Symbols, BumpPtrAllocator &Alloc, |
| 349 | StringRef Filename, bool UseRegex) { |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 350 | StringSaver Saver(Alloc); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 351 | SmallVector<StringRef, 16> Lines; |
| 352 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 353 | if (!BufOrErr) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 354 | return createFileError(Filename, BufOrErr.getError()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 355 | |
| 356 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 357 | for (StringRef Line : Lines) { |
| 358 | // Ignore everything after '#', trim whitespace, and only add the symbol if |
| 359 | // it's not empty. |
| 360 | auto TrimmedLine = Line.split('#').first.trim(); |
| 361 | if (!TrimmedLine.empty()) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 362 | Symbols.addMatcher({Saver.save(TrimmedLine), UseRegex}); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 363 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 364 | |
| 365 | return Error::success(); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 368 | NameOrRegex::NameOrRegex(StringRef Pattern, bool IsRegex) { |
| 369 | if (!IsRegex) { |
| 370 | Name = Pattern; |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | SmallVector<char, 32> Data; |
| 375 | R = std::make_shared<Regex>( |
| 376 | ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)); |
| 377 | } |
| 378 | |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 379 | static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename, |
| 380 | BumpPtrAllocator &Alloc, |
| 381 | StringRef Filename) { |
| 382 | StringSaver Saver(Alloc); |
| 383 | SmallVector<StringRef, 16> Lines; |
| 384 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 385 | if (!BufOrErr) |
Eugene Leviant | 317f9e7 | 2019-02-11 09:49:37 +0000 | [diff] [blame] | 386 | return createFileError(Filename, BufOrErr.getError()); |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 387 | |
| 388 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 389 | size_t NumLines = Lines.size(); |
| 390 | for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) { |
| 391 | StringRef TrimmedLine = Lines[LineNo].split('#').first.trim(); |
| 392 | if (TrimmedLine.empty()) |
| 393 | continue; |
| 394 | |
| 395 | std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' '); |
| 396 | StringRef NewName = Pair.second.trim(); |
| 397 | if (NewName.empty()) |
| 398 | return createStringError(errc::invalid_argument, |
| 399 | "%s:%zu: missing new symbol name", |
| 400 | Filename.str().c_str(), LineNo + 1); |
| 401 | SymbolsToRename.insert({Pair.first, NewName}); |
| 402 | } |
| 403 | return Error::success(); |
| 404 | } |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 405 | |
| 406 | template <class T> static ErrorOr<T> getAsInteger(StringRef Val) { |
| 407 | T Result; |
| 408 | if (Val.getAsInteger(0, Result)) |
| 409 | return errc::invalid_argument; |
| 410 | return Result; |
| 411 | } |
| 412 | |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 413 | static void printHelp(const opt::OptTable &OptTable, raw_ostream &OS, |
| 414 | StringRef ToolName) { |
| 415 | OptTable.PrintHelp(OS, (ToolName + " input [output]").str().c_str(), |
| 416 | (ToolName + " tool").str().c_str()); |
| 417 | // TODO: Replace this with libOption call once it adds extrahelp support. |
| 418 | // The CommandLine library has a cl::extrahelp class to support this, |
| 419 | // but libOption does not have that yet. |
| 420 | OS << "\nPass @FILE as argument to read options from FILE.\n"; |
| 421 | } |
| 422 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 423 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 424 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 425 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 426 | Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) { |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 427 | DriverConfig DC; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 428 | ObjcopyOptTable T; |
| 429 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 430 | llvm::opt::InputArgList InputArgs = |
| 431 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 432 | |
| 433 | if (InputArgs.size() == 0) { |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 434 | printHelp(T, errs(), "llvm-objcopy"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 435 | exit(1); |
| 436 | } |
| 437 | |
| 438 | if (InputArgs.hasArg(OBJCOPY_help)) { |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 439 | printHelp(T, outs(), "llvm-objcopy"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 440 | exit(0); |
| 441 | } |
| 442 | |
| 443 | if (InputArgs.hasArg(OBJCOPY_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 444 | outs() << "llvm-objcopy, compatible with GNU objcopy\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 445 | cl::PrintVersionMessage(); |
| 446 | exit(0); |
| 447 | } |
| 448 | |
| 449 | SmallVector<const char *, 2> Positional; |
| 450 | |
| 451 | for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 452 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 453 | Arg->getAsString(InputArgs).c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 454 | |
| 455 | for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT)) |
| 456 | Positional.push_back(Arg->getValue()); |
| 457 | |
| 458 | if (Positional.empty()) |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 459 | return createStringError(errc::invalid_argument, "no input file specified"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 460 | |
| 461 | if (Positional.size() > 2) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 462 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 463 | "too many positional arguments"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 464 | |
| 465 | CopyConfig Config; |
| 466 | Config.InputFilename = Positional[0]; |
| 467 | Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 468 | if (InputArgs.hasArg(OBJCOPY_target) && |
| 469 | (InputArgs.hasArg(OBJCOPY_input_target) || |
| 470 | InputArgs.hasArg(OBJCOPY_output_target))) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 471 | return createStringError( |
| 472 | errc::invalid_argument, |
| 473 | "--target cannot be used with --input-target or --output-target"); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 474 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 475 | bool UseRegex = InputArgs.hasArg(OBJCOPY_regex); |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 476 | StringRef InputFormat, OutputFormat; |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 477 | if (InputArgs.hasArg(OBJCOPY_target)) { |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 478 | InputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 479 | OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 480 | } else { |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 481 | InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target); |
| 482 | OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 483 | } |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 484 | |
| 485 | // FIXME: Currently, we ignore the target for non-binary/ihex formats |
| 486 | // explicitly specified by -I option (e.g. -Ielf32-x86-64) and guess the |
| 487 | // format by llvm::object::createBinary regardless of the option value. |
| 488 | Config.InputFormat = StringSwitch<FileFormat>(InputFormat) |
| 489 | .Case("binary", FileFormat::Binary) |
| 490 | .Case("ihex", FileFormat::IHex) |
| 491 | .Default(FileFormat::Unspecified); |
| 492 | if (Config.InputFormat == FileFormat::Binary) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 493 | auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture); |
Fangrui Song | ba53030 | 2019-09-14 01:36:16 +0000 | [diff] [blame^] | 494 | if (!BinaryArch.empty()) { |
| 495 | Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch); |
| 496 | if (!MI) |
| 497 | return MI.takeError(); |
| 498 | Config.BinaryArch = *MI; |
| 499 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 500 | } |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 501 | |
Chris Jackson | fa1fe93 | 2019-08-30 10:17:16 +0000 | [diff] [blame] | 502 | if (opt::Arg *A = InputArgs.getLastArg(OBJCOPY_new_symbol_visibility)) { |
| 503 | const uint8_t Invalid = 0xff; |
| 504 | Config.NewSymbolVisibility = StringSwitch<uint8_t>(A->getValue()) |
| 505 | .Case("default", ELF::STV_DEFAULT) |
| 506 | .Case("hidden", ELF::STV_HIDDEN) |
| 507 | .Case("internal", ELF::STV_INTERNAL) |
| 508 | .Case("protected", ELF::STV_PROTECTED) |
| 509 | .Default(Invalid); |
| 510 | |
| 511 | if (Config.NewSymbolVisibility == Invalid) |
| 512 | return createStringError( |
| 513 | errc::invalid_argument, "'%s' is not a valid symbol visibility", |
| 514 | InputArgs.getLastArgValue(OBJCOPY_new_symbol_visibility).str().c_str()); |
| 515 | } |
| 516 | |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 517 | Config.OutputFormat = StringSwitch<FileFormat>(OutputFormat) |
| 518 | .Case("binary", FileFormat::Binary) |
| 519 | .Case("ihex", FileFormat::IHex) |
| 520 | .Default(FileFormat::Unspecified); |
Fangrui Song | ba53030 | 2019-09-14 01:36:16 +0000 | [diff] [blame^] | 521 | if (Config.OutputFormat == FileFormat::Unspecified) { |
| 522 | if (OutputFormat.empty()) { |
| 523 | Config.OutputFormat = Config.InputFormat; |
| 524 | } else { |
| 525 | Expected<TargetInfo> Target = |
| 526 | getOutputTargetInfoByTargetName(OutputFormat); |
| 527 | if (!Target) |
| 528 | return Target.takeError(); |
| 529 | Config.OutputFormat = Target->Format; |
| 530 | Config.OutputArch = Target->Machine; |
| 531 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 532 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 533 | |
| 534 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections, |
| 535 | OBJCOPY_compress_debug_sections_eq)) { |
| 536 | Config.CompressionType = DebugCompressionType::Z; |
| 537 | |
| 538 | if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) { |
| 539 | Config.CompressionType = |
| 540 | StringSwitch<DebugCompressionType>( |
| 541 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)) |
| 542 | .Case("zlib-gnu", DebugCompressionType::GNU) |
| 543 | .Case("zlib", DebugCompressionType::Z) |
| 544 | .Default(DebugCompressionType::None); |
| 545 | if (Config.CompressionType == DebugCompressionType::None) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 546 | return createStringError( |
| 547 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 548 | "invalid or unsupported --compress-debug-sections format: %s", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 549 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq) |
| 550 | .str() |
| 551 | .c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 552 | } |
George Rimar | 1e93080 | 2019-03-05 11:32:14 +0000 | [diff] [blame] | 553 | if (!zlib::isAvailable()) |
| 554 | return createStringError( |
| 555 | errc::invalid_argument, |
| 556 | "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 559 | Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); |
James Henderson | 9df3883 | 2019-05-14 10:59:04 +0000 | [diff] [blame] | 560 | // The gnu_debuglink's target is expected to not change or else its CRC would |
| 561 | // become invalidated and get rejected. We can avoid recalculating the |
| 562 | // checksum for every target file inside an archive by precomputing the CRC |
| 563 | // here. This prevents a significant amount of I/O. |
| 564 | if (!Config.AddGnuDebugLink.empty()) { |
| 565 | auto DebugOrErr = MemoryBuffer::getFile(Config.AddGnuDebugLink); |
| 566 | if (!DebugOrErr) |
| 567 | return createFileError(Config.AddGnuDebugLink, DebugOrErr.getError()); |
| 568 | auto Debug = std::move(*DebugOrErr); |
| 569 | JamCRC CRC; |
| 570 | CRC.update( |
| 571 | ArrayRef<char>(Debug->getBuffer().data(), Debug->getBuffer().size())); |
| 572 | // The CRC32 value needs to be complemented because the JamCRC doesn't |
| 573 | // finalize the CRC32 value. |
| 574 | Config.GnuDebugLinkCRC32 = ~CRC.getCRC(); |
| 575 | } |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 576 | Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir); |
| 577 | if (InputArgs.hasArg(OBJCOPY_build_id_link_input)) |
| 578 | Config.BuildIdLinkInput = |
| 579 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_input); |
| 580 | if (InputArgs.hasArg(OBJCOPY_build_id_link_output)) |
| 581 | Config.BuildIdLinkOutput = |
| 582 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_output); |
| 583 | Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 584 | Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols); |
James Henderson | fa11fb3 | 2019-05-08 09:49:35 +0000 | [diff] [blame] | 585 | Config.AllocSectionsPrefix = |
| 586 | InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections); |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 587 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_extract_partition)) |
| 588 | Config.ExtractPartition = Arg->getValue(); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 589 | |
| 590 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { |
| 591 | if (!StringRef(Arg->getValue()).contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 592 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 593 | "bad format for --redefine-sym"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 594 | auto Old2New = StringRef(Arg->getValue()).split('='); |
| 595 | if (!Config.SymbolsToRename.insert(Old2New).second) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 596 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 597 | "multiple redefinition of symbol '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 598 | Old2New.first.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 601 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols)) |
| 602 | if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc, |
| 603 | Arg->getValue())) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 604 | return std::move(E); |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 605 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 606 | for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 607 | Expected<SectionRename> SR = |
| 608 | parseRenameSectionValue(StringRef(Arg->getValue())); |
| 609 | if (!SR) |
| 610 | return SR.takeError(); |
| 611 | if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second) |
| 612 | return createStringError(errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 613 | "multiple renames of section '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 614 | SR->OriginalName.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 615 | } |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 616 | for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 617 | Expected<SectionFlagsUpdate> SFU = |
| 618 | parseSetSectionFlagValue(Arg->getValue()); |
| 619 | if (!SFU) |
| 620 | return SFU.takeError(); |
| 621 | if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second) |
| 622 | return createStringError( |
| 623 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 624 | "--set-section-flags set multiple times for section '%s'", |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 625 | SFU->Name.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 626 | } |
| 627 | // Prohibit combinations of --set-section-flags when the section name is used |
| 628 | // by --rename-section, either as a source or a destination. |
| 629 | for (const auto &E : Config.SectionsToRename) { |
| 630 | const SectionRename &SR = E.second; |
| 631 | if (Config.SetSectionFlags.count(SR.OriginalName)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 632 | return createStringError( |
| 633 | errc::invalid_argument, |
| 634 | "--set-section-flags=%s conflicts with --rename-section=%s=%s", |
| 635 | SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(), |
| 636 | SR.NewName.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 637 | if (Config.SetSectionFlags.count(SR.NewName)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 638 | return createStringError( |
| 639 | errc::invalid_argument, |
| 640 | "--set-section-flags=%s conflicts with --rename-section=%s=%s", |
| 641 | SR.NewName.str().c_str(), SR.OriginalName.str().c_str(), |
| 642 | SR.NewName.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 643 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 644 | |
| 645 | for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 646 | Config.ToRemove.addMatcher({Arg->getValue(), UseRegex}); |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 647 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 648 | Config.KeepSection.addMatcher({Arg->getValue(), UseRegex}); |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 649 | for (auto Arg : InputArgs.filtered(OBJCOPY_only_section)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 650 | Config.OnlySection.addMatcher({Arg->getValue(), UseRegex}); |
Sergey Dmitriev | 899bdaa | 2019-07-29 16:22:40 +0000 | [diff] [blame] | 651 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) { |
| 652 | StringRef ArgValue(Arg->getValue()); |
| 653 | if (!ArgValue.contains('=')) |
| 654 | return createStringError(errc::invalid_argument, |
| 655 | "bad format for --add-section: missing '='"); |
| 656 | if (ArgValue.split("=").second.empty()) |
| 657 | return createStringError( |
| 658 | errc::invalid_argument, |
| 659 | "bad format for --add-section: missing file name"); |
| 660 | Config.AddSection.push_back(ArgValue); |
| 661 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 662 | for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section)) |
| 663 | Config.DumpSection.push_back(Arg->getValue()); |
| 664 | Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all); |
| 665 | Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu); |
| 666 | Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug); |
| 667 | Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); |
| 668 | Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); |
| 669 | Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); |
| 670 | Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); |
| 671 | Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 672 | Config.ExtractMainPartition = |
| 673 | InputArgs.hasArg(OBJCOPY_extract_main_partition); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 674 | Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); |
| 675 | Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 676 | if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals)) |
| 677 | Config.DiscardMode = |
| 678 | InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals) |
| 679 | ? DiscardType::All |
| 680 | : DiscardType::Locals; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 681 | Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); |
| 682 | Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); |
| 683 | Config.DecompressDebugSections = |
| 684 | InputArgs.hasArg(OBJCOPY_decompress_debug_sections); |
Sid Manning | 5ad18a7 | 2019-05-03 14:14:01 +0000 | [diff] [blame] | 685 | if (Config.DiscardMode == DiscardType::All) |
| 686 | Config.StripDebug = true; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 687 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 688 | Config.SymbolsToLocalize.addMatcher({Arg->getValue(), UseRegex}); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 689 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 690 | if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, DC.Alloc, |
| 691 | Arg->getValue(), UseRegex)) |
| 692 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 693 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 694 | Config.SymbolsToKeepGlobal.addMatcher({Arg->getValue(), UseRegex}); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 695 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 696 | if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc, |
| 697 | Arg->getValue(), UseRegex)) |
| 698 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 699 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 700 | Config.SymbolsToGlobalize.addMatcher({Arg->getValue(), UseRegex}); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 701 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 702 | if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc, |
| 703 | Arg->getValue(), UseRegex)) |
| 704 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 705 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 706 | Config.SymbolsToWeaken.addMatcher({Arg->getValue(), UseRegex}); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 707 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 708 | if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, DC.Alloc, |
| 709 | Arg->getValue(), UseRegex)) |
| 710 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 711 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 712 | Config.SymbolsToRemove.addMatcher({Arg->getValue(), UseRegex}); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 713 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 714 | if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc, |
| 715 | Arg->getValue(), UseRegex)) |
| 716 | return std::move(E); |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 717 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 718 | Config.UnneededSymbolsToRemove.addMatcher({Arg->getValue(), UseRegex}); |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 719 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 720 | if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc, |
| 721 | Arg->getValue(), UseRegex)) |
| 722 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 723 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 724 | Config.SymbolsToKeep.addMatcher({Arg->getValue(), UseRegex}); |
Yi Kong | f2baddb | 2019-04-01 18:12:43 +0000 | [diff] [blame] | 725 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbols)) |
| 726 | if (Error E = addSymbolsFromFile(Config.SymbolsToKeep, DC.Alloc, |
| 727 | Arg->getValue(), UseRegex)) |
| 728 | return std::move(E); |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 729 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_symbol)) { |
Chris Jackson | fa1fe93 | 2019-08-30 10:17:16 +0000 | [diff] [blame] | 730 | Expected<NewSymbolInfo> NSI = parseNewSymbolInfo( |
| 731 | Arg->getValue(), |
Simon Pilgrim | 92e13f2 | 2019-09-04 12:51:40 +0000 | [diff] [blame] | 732 | Config.NewSymbolVisibility.getValueOr((uint8_t)ELF::STV_DEFAULT)); |
Jordan Rupprecht | 42bc1e2 | 2019-03-13 22:26:01 +0000 | [diff] [blame] | 733 | if (!NSI) |
| 734 | return NSI.takeError(); |
| 735 | Config.SymbolsToAdd.push_back(*NSI); |
| 736 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 737 | |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 738 | Config.AllowBrokenLinks = InputArgs.hasArg(OBJCOPY_allow_broken_links); |
| 739 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 740 | Config.DeterministicArchives = InputArgs.hasFlag( |
| 741 | OBJCOPY_enable_deterministic_archives, |
| 742 | OBJCOPY_disable_deterministic_archives, /*default=*/true); |
| 743 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 744 | Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); |
| 745 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 746 | if (Config.PreserveDates && |
| 747 | (Config.OutputFilename == "-" || Config.InputFilename == "-")) |
| 748 | return createStringError(errc::invalid_argument, |
| 749 | "--preserve-dates requires a file"); |
| 750 | |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 751 | for (auto Arg : InputArgs) |
| 752 | if (Arg->getOption().matches(OBJCOPY_set_start)) { |
| 753 | auto EAddr = getAsInteger<uint64_t>(Arg->getValue()); |
| 754 | if (!EAddr) |
| 755 | return createStringError( |
| 756 | EAddr.getError(), "bad entry point address: '%s'", Arg->getValue()); |
| 757 | |
| 758 | Config.EntryExpr = [EAddr](uint64_t) { return *EAddr; }; |
| 759 | } else if (Arg->getOption().matches(OBJCOPY_change_start)) { |
| 760 | auto EIncr = getAsInteger<int64_t>(Arg->getValue()); |
| 761 | if (!EIncr) |
| 762 | return createStringError(EIncr.getError(), |
| 763 | "bad entry point increment: '%s'", |
| 764 | Arg->getValue()); |
| 765 | auto Expr = Config.EntryExpr ? std::move(Config.EntryExpr) |
| 766 | : [](uint64_t A) { return A; }; |
| 767 | Config.EntryExpr = [Expr, EIncr](uint64_t EAddr) { |
| 768 | return Expr(EAddr) + *EIncr; |
| 769 | }; |
| 770 | } |
| 771 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 772 | if (Config.DecompressDebugSections && |
| 773 | Config.CompressionType != DebugCompressionType::None) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 774 | return createStringError( |
| 775 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 776 | "cannot specify both --compress-debug-sections and " |
| 777 | "--decompress-debug-sections"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | if (Config.DecompressDebugSections && !zlib::isAvailable()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 781 | return createStringError( |
| 782 | errc::invalid_argument, |
| 783 | "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 784 | |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 785 | if (Config.ExtractPartition && Config.ExtractMainPartition) |
| 786 | return createStringError(errc::invalid_argument, |
| 787 | "cannot specify --extract-partition together with " |
| 788 | "--extract-main-partition"); |
| 789 | |
Jordan Rupprecht | ab9f662 | 2018-10-23 20:54:51 +0000 | [diff] [blame] | 790 | DC.CopyConfigs.push_back(std::move(Config)); |
Jordan Rupprecht | 93ad8b3 | 2019-02-21 17:24:55 +0000 | [diff] [blame] | 791 | return std::move(DC); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 795 | // help flag is set then ParseStripOptions will print the help messege and |
| 796 | // exit. |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 797 | Expected<DriverConfig> |
| 798 | parseStripOptions(ArrayRef<const char *> ArgsArr, |
| 799 | std::function<Error(Error)> ErrorCallback) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 800 | StripOptTable T; |
| 801 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 802 | llvm::opt::InputArgList InputArgs = |
| 803 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 804 | |
| 805 | if (InputArgs.size() == 0) { |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 806 | printHelp(T, errs(), "llvm-strip"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 807 | exit(1); |
| 808 | } |
| 809 | |
| 810 | if (InputArgs.hasArg(STRIP_help)) { |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 811 | printHelp(T, outs(), "llvm-strip"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 812 | exit(0); |
| 813 | } |
| 814 | |
| 815 | if (InputArgs.hasArg(STRIP_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 816 | outs() << "llvm-strip, compatible with GNU strip\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 817 | cl::PrintVersionMessage(); |
| 818 | exit(0); |
| 819 | } |
| 820 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 821 | SmallVector<StringRef, 2> Positional; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 822 | for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 823 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 824 | Arg->getAsString(InputArgs).c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 825 | for (auto Arg : InputArgs.filtered(STRIP_INPUT)) |
| 826 | Positional.push_back(Arg->getValue()); |
| 827 | |
| 828 | if (Positional.empty()) |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 829 | return createStringError(errc::invalid_argument, "no input file specified"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 830 | |
| 831 | if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 832 | return createStringError( |
| 833 | errc::invalid_argument, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 834 | "multiple input files cannot be used in combination with -o"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 835 | |
| 836 | CopyConfig Config; |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 837 | bool UseRegexp = InputArgs.hasArg(STRIP_regex); |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 838 | Config.AllowBrokenLinks = InputArgs.hasArg(STRIP_allow_broken_links); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 839 | Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug); |
| 840 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 841 | if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals)) |
| 842 | Config.DiscardMode = |
| 843 | InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals) |
| 844 | ? DiscardType::All |
| 845 | : DiscardType::Locals; |
Wolfgang Pieb | ab751a7 | 2019-08-08 00:35:16 +0000 | [diff] [blame] | 846 | Config.StripSections = InputArgs.hasArg(STRIP_strip_sections); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 847 | Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); |
James Henderson | e4a89a1 | 2019-05-02 11:53:02 +0000 | [diff] [blame] | 848 | if (auto Arg = InputArgs.getLastArg(STRIP_strip_all, STRIP_no_strip_all)) |
| 849 | Config.StripAll = Arg->getOption().getID() == STRIP_strip_all; |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 850 | Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu); |
Jordan Rupprecht | 12ed01d | 2019-03-14 21:51:42 +0000 | [diff] [blame] | 851 | Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug); |
Eugene Leviant | 05a3f99 | 2019-02-01 15:25:15 +0000 | [diff] [blame] | 852 | Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 853 | |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 854 | for (auto Arg : InputArgs.filtered(STRIP_keep_section)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 855 | Config.KeepSection.addMatcher({Arg->getValue(), UseRegexp}); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 856 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 857 | for (auto Arg : InputArgs.filtered(STRIP_remove_section)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 858 | Config.ToRemove.addMatcher({Arg->getValue(), UseRegexp}); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 859 | |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 860 | for (auto Arg : InputArgs.filtered(STRIP_strip_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 861 | Config.SymbolsToRemove.addMatcher({Arg->getValue(), UseRegexp}); |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 862 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 863 | for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 864 | Config.SymbolsToKeep.addMatcher({Arg->getValue(), UseRegexp}); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 865 | |
James Henderson | e4a89a1 | 2019-05-02 11:53:02 +0000 | [diff] [blame] | 866 | if (!InputArgs.hasArg(STRIP_no_strip_all) && !Config.StripDebug && |
| 867 | !Config.StripUnneeded && Config.DiscardMode == DiscardType::None && |
| 868 | !Config.StripAllGNU && Config.SymbolsToRemove.empty()) |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 869 | Config.StripAll = true; |
| 870 | |
Sid Manning | 5ad18a7 | 2019-05-03 14:14:01 +0000 | [diff] [blame] | 871 | if (Config.DiscardMode == DiscardType::All) |
| 872 | Config.StripDebug = true; |
| 873 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 874 | Config.DeterministicArchives = |
| 875 | InputArgs.hasFlag(STRIP_enable_deterministic_archives, |
| 876 | STRIP_disable_deterministic_archives, /*default=*/true); |
| 877 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 878 | Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates); |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 879 | Config.InputFormat = FileFormat::Unspecified; |
| 880 | Config.OutputFormat = FileFormat::Unspecified; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 881 | |
| 882 | DriverConfig DC; |
| 883 | if (Positional.size() == 1) { |
| 884 | Config.InputFilename = Positional[0]; |
| 885 | Config.OutputFilename = |
| 886 | InputArgs.getLastArgValue(STRIP_output, Positional[0]); |
| 887 | DC.CopyConfigs.push_back(std::move(Config)); |
| 888 | } else { |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 889 | StringMap<unsigned> InputFiles; |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 890 | for (StringRef Filename : Positional) { |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 891 | if (InputFiles[Filename]++ == 1) { |
| 892 | if (Filename == "-") |
| 893 | return createStringError( |
| 894 | errc::invalid_argument, |
| 895 | "cannot specify '-' as an input file more than once"); |
| 896 | if (Error E = ErrorCallback(createStringError( |
| 897 | errc::invalid_argument, "'%s' was already specified", |
| 898 | Filename.str().c_str()))) |
| 899 | return std::move(E); |
| 900 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 901 | Config.InputFilename = Filename; |
| 902 | Config.OutputFilename = Filename; |
| 903 | DC.CopyConfigs.push_back(Config); |
| 904 | } |
| 905 | } |
| 906 | |
Alex Brachet | 899a307 | 2019-06-15 05:32:23 +0000 | [diff] [blame] | 907 | if (Config.PreserveDates && (is_contained(Positional, "-") || |
| 908 | InputArgs.getLastArgValue(STRIP_output) == "-")) |
| 909 | return createStringError(errc::invalid_argument, |
| 910 | "--preserve-dates requires a file"); |
| 911 | |
Jordan Rupprecht | 93ad8b3 | 2019-02-21 17:24:55 +0000 | [diff] [blame] | 912 | return std::move(DC); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | } // namespace objcopy |
| 916 | } // namespace llvm |