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" |
| 10 | #include "llvm-objcopy.h" |
| 11 | |
| 12 | #include "llvm/ADT/BitmaskEnum.h" |
| 13 | #include "llvm/ADT/Optional.h" |
| 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 16 | #include "llvm/Option/Arg.h" |
| 17 | #include "llvm/Option/ArgList.h" |
| 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "llvm/Support/Compression.h" |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Errc.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 22 | #include "llvm/Support/StringSaver.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 23 | #include <memory> |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 24 | |
| 25 | namespace llvm { |
| 26 | namespace objcopy { |
| 27 | |
| 28 | namespace { |
| 29 | enum ObjcopyID { |
| 30 | OBJCOPY_INVALID = 0, // This is not an option ID. |
| 31 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 32 | HELPTEXT, METAVAR, VALUES) \ |
| 33 | OBJCOPY_##ID, |
| 34 | #include "ObjcopyOpts.inc" |
| 35 | #undef OPTION |
| 36 | }; |
| 37 | |
| 38 | #define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE; |
| 39 | #include "ObjcopyOpts.inc" |
| 40 | #undef PREFIX |
| 41 | |
| 42 | static const opt::OptTable::Info ObjcopyInfoTable[] = { |
| 43 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 44 | HELPTEXT, METAVAR, VALUES) \ |
| 45 | {OBJCOPY_##PREFIX, \ |
| 46 | NAME, \ |
| 47 | HELPTEXT, \ |
| 48 | METAVAR, \ |
| 49 | OBJCOPY_##ID, \ |
| 50 | opt::Option::KIND##Class, \ |
| 51 | PARAM, \ |
| 52 | FLAGS, \ |
| 53 | OBJCOPY_##GROUP, \ |
| 54 | OBJCOPY_##ALIAS, \ |
| 55 | ALIASARGS, \ |
| 56 | VALUES}, |
| 57 | #include "ObjcopyOpts.inc" |
| 58 | #undef OPTION |
| 59 | }; |
| 60 | |
| 61 | class ObjcopyOptTable : public opt::OptTable { |
| 62 | public: |
Jordan Rupprecht | aaeaa0a | 2018-10-23 18:46:33 +0000 | [diff] [blame] | 63 | ObjcopyOptTable() : OptTable(ObjcopyInfoTable) {} |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 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 | |
| 95 | enum SectionFlag { |
| 96 | SecNone = 0, |
| 97 | SecAlloc = 1 << 0, |
| 98 | SecLoad = 1 << 1, |
| 99 | SecNoload = 1 << 2, |
| 100 | SecReadonly = 1 << 3, |
| 101 | SecDebug = 1 << 4, |
| 102 | SecCode = 1 << 5, |
| 103 | SecData = 1 << 6, |
| 104 | SecRom = 1 << 7, |
| 105 | SecMerge = 1 << 8, |
| 106 | SecStrings = 1 << 9, |
| 107 | SecContents = 1 << 10, |
| 108 | SecShare = 1 << 11, |
| 109 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare) |
| 110 | }; |
| 111 | |
| 112 | } // namespace |
| 113 | |
| 114 | static SectionFlag parseSectionRenameFlag(StringRef SectionName) { |
| 115 | return llvm::StringSwitch<SectionFlag>(SectionName) |
| 116 | .Case("alloc", SectionFlag::SecAlloc) |
| 117 | .Case("load", SectionFlag::SecLoad) |
| 118 | .Case("noload", SectionFlag::SecNoload) |
| 119 | .Case("readonly", SectionFlag::SecReadonly) |
| 120 | .Case("debug", SectionFlag::SecDebug) |
| 121 | .Case("code", SectionFlag::SecCode) |
| 122 | .Case("data", SectionFlag::SecData) |
| 123 | .Case("rom", SectionFlag::SecRom) |
| 124 | .Case("merge", SectionFlag::SecMerge) |
| 125 | .Case("strings", SectionFlag::SecStrings) |
| 126 | .Case("contents", SectionFlag::SecContents) |
| 127 | .Case("share", SectionFlag::SecShare) |
| 128 | .Default(SectionFlag::SecNone); |
| 129 | } |
| 130 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 131 | static Expected<uint64_t> |
| 132 | parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) { |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 133 | SectionFlag ParsedFlags = SectionFlag::SecNone; |
| 134 | for (StringRef Flag : SectionFlags) { |
| 135 | SectionFlag ParsedFlag = parseSectionRenameFlag(Flag); |
| 136 | if (ParsedFlag == SectionFlag::SecNone) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 137 | return createStringError( |
| 138 | errc::invalid_argument, |
| 139 | "Unrecognized section flag '%s'. Flags supported for GNU " |
| 140 | "compatibility: alloc, load, noload, readonly, debug, code, data, " |
| 141 | "rom, share, contents, merge, strings", |
| 142 | Flag.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 143 | ParsedFlags |= ParsedFlag; |
| 144 | } |
| 145 | |
| 146 | uint64_t NewFlags = 0; |
| 147 | if (ParsedFlags & SectionFlag::SecAlloc) |
| 148 | NewFlags |= ELF::SHF_ALLOC; |
| 149 | if (!(ParsedFlags & SectionFlag::SecReadonly)) |
| 150 | NewFlags |= ELF::SHF_WRITE; |
| 151 | if (ParsedFlags & SectionFlag::SecCode) |
| 152 | NewFlags |= ELF::SHF_EXECINSTR; |
| 153 | if (ParsedFlags & SectionFlag::SecMerge) |
| 154 | NewFlags |= ELF::SHF_MERGE; |
| 155 | if (ParsedFlags & SectionFlag::SecStrings) |
| 156 | NewFlags |= ELF::SHF_STRINGS; |
| 157 | return NewFlags; |
| 158 | } |
| 159 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 160 | static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 161 | if (!FlagValue.contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 162 | return createStringError(errc::invalid_argument, |
| 163 | "Bad format for --rename-section: missing '='"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 164 | |
| 165 | // Initial split: ".foo" = ".bar,f1,f2,..." |
| 166 | auto Old2New = FlagValue.split('='); |
| 167 | SectionRename SR; |
| 168 | SR.OriginalName = Old2New.first; |
| 169 | |
| 170 | // Flags split: ".bar" "f1" "f2" ... |
| 171 | SmallVector<StringRef, 6> NameAndFlags; |
| 172 | Old2New.second.split(NameAndFlags, ','); |
| 173 | SR.NewName = NameAndFlags[0]; |
| 174 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 175 | if (NameAndFlags.size() > 1) { |
| 176 | Expected<uint64_t> ParsedFlagSet = |
| 177 | parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front()); |
| 178 | if (!ParsedFlagSet) |
| 179 | return ParsedFlagSet.takeError(); |
| 180 | SR.NewFlags = *ParsedFlagSet; |
| 181 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 182 | |
| 183 | return SR; |
| 184 | } |
| 185 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 186 | static Expected<SectionFlagsUpdate> |
| 187 | parseSetSectionFlagValue(StringRef FlagValue) { |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 188 | if (!StringRef(FlagValue).contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 189 | return createStringError(errc::invalid_argument, |
| 190 | "Bad format for --set-section-flags: missing '='"); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 191 | |
| 192 | // Initial split: ".foo" = "f1,f2,..." |
| 193 | auto Section2Flags = StringRef(FlagValue).split('='); |
| 194 | SectionFlagsUpdate SFU; |
| 195 | SFU.Name = Section2Flags.first; |
| 196 | |
| 197 | // Flags split: "f1" "f2" ... |
| 198 | SmallVector<StringRef, 6> SectionFlags; |
| 199 | Section2Flags.second.split(SectionFlags, ','); |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 200 | Expected<uint64_t> ParsedFlagSet = parseSectionFlagSet(SectionFlags); |
| 201 | if (!ParsedFlagSet) |
| 202 | return ParsedFlagSet.takeError(); |
| 203 | SFU.NewFlags = *ParsedFlagSet; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 204 | |
| 205 | return SFU; |
| 206 | } |
| 207 | |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame^] | 208 | static NewSymbolInfo parseNewSymbolInfo(StringRef FlagValue) { |
| 209 | // Parse value given with --add-symbol option and create the |
| 210 | // new symbol if possible. The value format for --add-symbol is: |
| 211 | // |
| 212 | // <name>=[<section>:]<value>[,<flags>] |
| 213 | // |
| 214 | // where: |
| 215 | // <name> - symbol name, can be empty string |
| 216 | // <section> - optional section name. If not given ABS symbol is created |
| 217 | // <value> - symbol value, can be decimal or hexadecimal number prefixed |
| 218 | // with 0x. |
| 219 | // <flags> - optional flags affecting symbol type, binding or visibility: |
| 220 | // The following are currently supported: |
| 221 | // |
| 222 | // global, local, weak, default, hidden, file, section, object, |
| 223 | // indirect-function. |
| 224 | // |
| 225 | // The following flags are ignored and provided for GNU |
| 226 | // compatibility only: |
| 227 | // |
| 228 | // warning, debug, constructor, indirect, synthetic, |
| 229 | // unique-object, before=<symbol>. |
| 230 | NewSymbolInfo SI; |
| 231 | StringRef Value; |
| 232 | std::tie(SI.SymbolName, Value) = FlagValue.split('='); |
| 233 | if (Value.empty()) |
| 234 | error("bad format for --add-symbol, missing '=' after '" + SI.SymbolName + |
| 235 | "'"); |
| 236 | |
| 237 | if (Value.contains(':')) { |
| 238 | std::tie(SI.SectionName, Value) = Value.split(':'); |
| 239 | if (SI.SectionName.empty() || Value.empty()) |
| 240 | error( |
| 241 | "bad format for --add-symbol, missing section name or symbol value"); |
| 242 | } |
| 243 | |
| 244 | SmallVector<StringRef, 6> Flags; |
| 245 | Value.split(Flags, ','); |
| 246 | if (Flags[0].getAsInteger(0, SI.Value)) |
| 247 | error("bad symbol value: '" + Flags[0] + "'"); |
| 248 | |
| 249 | typedef std::function<void(void)> Functor; |
| 250 | size_t NumFlags = Flags.size(); |
| 251 | for (size_t I = 1; I < NumFlags; ++I) |
| 252 | static_cast<Functor>( |
| 253 | StringSwitch<Functor>(Flags[I]) |
| 254 | .CaseLower("global", [&SI] { SI.Bind = ELF::STB_GLOBAL; }) |
| 255 | .CaseLower("local", [&SI] { SI.Bind = ELF::STB_LOCAL; }) |
| 256 | .CaseLower("weak", [&SI] { SI.Bind = ELF::STB_WEAK; }) |
| 257 | .CaseLower("default", [&SI] { SI.Visibility = ELF::STV_DEFAULT; }) |
| 258 | .CaseLower("hidden", [&SI] { SI.Visibility = ELF::STV_HIDDEN; }) |
| 259 | .CaseLower("file", [&SI] { SI.Type = ELF::STT_FILE; }) |
| 260 | .CaseLower("section", [&SI] { SI.Type = ELF::STT_SECTION; }) |
| 261 | .CaseLower("object", [&SI] { SI.Type = ELF::STT_OBJECT; }) |
| 262 | .CaseLower("function", [&SI] { SI.Type = ELF::STT_FUNC; }) |
| 263 | .CaseLower("indirect-function", |
| 264 | [&SI] { SI.Type = ELF::STT_GNU_IFUNC; }) |
| 265 | .CaseLower("debug", [] {}) |
| 266 | .CaseLower("constructor", [] {}) |
| 267 | .CaseLower("warning", [] {}) |
| 268 | .CaseLower("indirect", [] {}) |
| 269 | .CaseLower("synthetic", [] {}) |
| 270 | .CaseLower("unique-object", [] {}) |
| 271 | .StartsWithLower("before", [] {}) |
| 272 | .Default([&] { |
| 273 | error("unsupported flag '" + Flags[I] + "' for --add-symbol"); |
| 274 | }))(); |
| 275 | return SI; |
| 276 | } |
| 277 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 278 | static const StringMap<MachineInfo> ArchMap{ |
| 279 | // Name, {EMachine, 64bit, LittleEndian} |
| 280 | {"aarch64", {ELF::EM_AARCH64, true, true}}, |
| 281 | {"arm", {ELF::EM_ARM, false, true}}, |
| 282 | {"i386", {ELF::EM_386, false, true}}, |
| 283 | {"i386:x86-64", {ELF::EM_X86_64, true, true}}, |
| 284 | {"powerpc:common64", {ELF::EM_PPC64, true, true}}, |
| 285 | {"sparc", {ELF::EM_SPARC, false, true}}, |
| 286 | {"x86-64", {ELF::EM_X86_64, true, true}}, |
| 287 | }; |
| 288 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 289 | static Expected<const MachineInfo &> getMachineInfo(StringRef Arch) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 290 | auto Iter = ArchMap.find(Arch); |
| 291 | if (Iter == std::end(ArchMap)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 292 | return createStringError(errc::invalid_argument, |
| 293 | "Invalid architecture: '%s'", Arch.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 294 | return Iter->getValue(); |
| 295 | } |
| 296 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 297 | static const StringMap<MachineInfo> OutputFormatMap{ |
| 298 | // Name, {EMachine, 64bit, LittleEndian} |
| 299 | {"elf32-i386", {ELF::EM_386, false, true}}, |
| 300 | {"elf32-powerpcle", {ELF::EM_PPC, false, true}}, |
| 301 | {"elf32-x86-64", {ELF::EM_X86_64, false, true}}, |
| 302 | {"elf64-powerpcle", {ELF::EM_PPC64, true, true}}, |
| 303 | {"elf64-x86-64", {ELF::EM_X86_64, true, true}}, |
| 304 | }; |
| 305 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 306 | static Expected<const MachineInfo &> |
| 307 | getOutputFormatMachineInfo(StringRef Format) { |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 308 | auto Iter = OutputFormatMap.find(Format); |
| 309 | if (Iter == std::end(OutputFormatMap)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 310 | return createStringError(errc::invalid_argument, |
| 311 | "Invalid output format: '%s'", |
| 312 | Format.str().c_str()); |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 313 | return Iter->getValue(); |
| 314 | } |
| 315 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 316 | static Error addSymbolsFromFile(std::vector<NameOrRegex> &Symbols, |
| 317 | BumpPtrAllocator &Alloc, StringRef Filename, |
| 318 | bool UseRegex) { |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 319 | StringSaver Saver(Alloc); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 320 | SmallVector<StringRef, 16> Lines; |
| 321 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 322 | if (!BufOrErr) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 323 | return createFileError(Filename, BufOrErr.getError()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 324 | |
| 325 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 326 | for (StringRef Line : Lines) { |
| 327 | // Ignore everything after '#', trim whitespace, and only add the symbol if |
| 328 | // it's not empty. |
| 329 | auto TrimmedLine = Line.split('#').first.trim(); |
| 330 | if (!TrimmedLine.empty()) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 331 | Symbols.emplace_back(Saver.save(TrimmedLine), UseRegex); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 332 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 333 | |
| 334 | return Error::success(); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 337 | NameOrRegex::NameOrRegex(StringRef Pattern, bool IsRegex) { |
| 338 | if (!IsRegex) { |
| 339 | Name = Pattern; |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | SmallVector<char, 32> Data; |
| 344 | R = std::make_shared<Regex>( |
| 345 | ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)); |
| 346 | } |
| 347 | |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 348 | static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename, |
| 349 | BumpPtrAllocator &Alloc, |
| 350 | StringRef Filename) { |
| 351 | StringSaver Saver(Alloc); |
| 352 | SmallVector<StringRef, 16> Lines; |
| 353 | auto BufOrErr = MemoryBuffer::getFile(Filename); |
| 354 | if (!BufOrErr) |
Eugene Leviant | 317f9e7 | 2019-02-11 09:49:37 +0000 | [diff] [blame] | 355 | return createFileError(Filename, BufOrErr.getError()); |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 356 | |
| 357 | BufOrErr.get()->getBuffer().split(Lines, '\n'); |
| 358 | size_t NumLines = Lines.size(); |
| 359 | for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) { |
| 360 | StringRef TrimmedLine = Lines[LineNo].split('#').first.trim(); |
| 361 | if (TrimmedLine.empty()) |
| 362 | continue; |
| 363 | |
| 364 | std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' '); |
| 365 | StringRef NewName = Pair.second.trim(); |
| 366 | if (NewName.empty()) |
| 367 | return createStringError(errc::invalid_argument, |
| 368 | "%s:%zu: missing new symbol name", |
| 369 | Filename.str().c_str(), LineNo + 1); |
| 370 | SymbolsToRename.insert({Pair.first, NewName}); |
| 371 | } |
| 372 | return Error::success(); |
| 373 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 374 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 375 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 376 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 377 | Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) { |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 378 | DriverConfig DC; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 379 | ObjcopyOptTable T; |
| 380 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 381 | llvm::opt::InputArgList InputArgs = |
| 382 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 383 | |
| 384 | if (InputArgs.size() == 0) { |
| 385 | T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool"); |
| 386 | exit(1); |
| 387 | } |
| 388 | |
| 389 | if (InputArgs.hasArg(OBJCOPY_help)) { |
| 390 | T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool"); |
| 391 | exit(0); |
| 392 | } |
| 393 | |
| 394 | if (InputArgs.hasArg(OBJCOPY_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 395 | outs() << "llvm-objcopy, compatible with GNU objcopy\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 396 | cl::PrintVersionMessage(); |
| 397 | exit(0); |
| 398 | } |
| 399 | |
| 400 | SmallVector<const char *, 2> Positional; |
| 401 | |
| 402 | for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 403 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 404 | Arg->getAsString(InputArgs).c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 405 | |
| 406 | for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT)) |
| 407 | Positional.push_back(Arg->getValue()); |
| 408 | |
| 409 | if (Positional.empty()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 410 | return createStringError(errc::invalid_argument, "No input file specified"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 411 | |
| 412 | if (Positional.size() > 2) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 413 | return createStringError(errc::invalid_argument, |
| 414 | "Too many positional arguments"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 415 | |
| 416 | CopyConfig Config; |
| 417 | Config.InputFilename = Positional[0]; |
| 418 | Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 419 | if (InputArgs.hasArg(OBJCOPY_target) && |
| 420 | (InputArgs.hasArg(OBJCOPY_input_target) || |
| 421 | InputArgs.hasArg(OBJCOPY_output_target))) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 422 | return createStringError( |
| 423 | errc::invalid_argument, |
| 424 | "--target cannot be used with --input-target or --output-target"); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 425 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 426 | bool UseRegex = InputArgs.hasArg(OBJCOPY_regex); |
Jordan Rupprecht | bb4588e | 2018-10-12 00:36:01 +0000 | [diff] [blame] | 427 | if (InputArgs.hasArg(OBJCOPY_target)) { |
| 428 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 429 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target); |
| 430 | } else { |
| 431 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target); |
| 432 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); |
| 433 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 434 | if (Config.InputFormat == "binary") { |
| 435 | auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture); |
| 436 | if (BinaryArch.empty()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 437 | return createStringError( |
| 438 | errc::invalid_argument, |
| 439 | "Specified binary input without specifiying an architecture"); |
| 440 | Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch); |
| 441 | if (!MI) |
| 442 | return MI.takeError(); |
| 443 | Config.BinaryArch = *MI; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 444 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 445 | if (!Config.OutputFormat.empty() && Config.OutputFormat != "binary") { |
| 446 | Expected<const MachineInfo &> MI = |
| 447 | getOutputFormatMachineInfo(Config.OutputFormat); |
| 448 | if (!MI) |
| 449 | return MI.takeError(); |
| 450 | Config.OutputArch = *MI; |
| 451 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 452 | |
| 453 | if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections, |
| 454 | OBJCOPY_compress_debug_sections_eq)) { |
| 455 | Config.CompressionType = DebugCompressionType::Z; |
| 456 | |
| 457 | if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) { |
| 458 | Config.CompressionType = |
| 459 | StringSwitch<DebugCompressionType>( |
| 460 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)) |
| 461 | .Case("zlib-gnu", DebugCompressionType::GNU) |
| 462 | .Case("zlib", DebugCompressionType::Z) |
| 463 | .Default(DebugCompressionType::None); |
| 464 | if (Config.CompressionType == DebugCompressionType::None) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 465 | return createStringError( |
| 466 | errc::invalid_argument, |
| 467 | "Invalid or unsupported --compress-debug-sections format: %s", |
| 468 | InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq) |
| 469 | .str() |
| 470 | .c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 471 | if (!zlib::isAvailable()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 472 | return createStringError( |
| 473 | errc::invalid_argument, |
| 474 | "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 478 | Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 479 | Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir); |
| 480 | if (InputArgs.hasArg(OBJCOPY_build_id_link_input)) |
| 481 | Config.BuildIdLinkInput = |
| 482 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_input); |
| 483 | if (InputArgs.hasArg(OBJCOPY_build_id_link_output)) |
| 484 | Config.BuildIdLinkOutput = |
| 485 | InputArgs.getLastArgValue(OBJCOPY_build_id_link_output); |
| 486 | Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 487 | Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols); |
| 488 | |
| 489 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { |
| 490 | if (!StringRef(Arg->getValue()).contains('=')) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 491 | return createStringError(errc::invalid_argument, |
| 492 | "Bad format for --redefine-sym"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 493 | auto Old2New = StringRef(Arg->getValue()).split('='); |
| 494 | if (!Config.SymbolsToRename.insert(Old2New).second) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 495 | return createStringError(errc::invalid_argument, |
| 496 | "Multiple redefinition of symbol %s", |
| 497 | Old2New.first.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 500 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols)) |
| 501 | if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc, |
| 502 | Arg->getValue())) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 503 | return std::move(E); |
Eugene Leviant | 340cb87 | 2019-02-08 10:33:16 +0000 | [diff] [blame] | 504 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 505 | for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 506 | Expected<SectionRename> SR = |
| 507 | parseRenameSectionValue(StringRef(Arg->getValue())); |
| 508 | if (!SR) |
| 509 | return SR.takeError(); |
| 510 | if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second) |
| 511 | return createStringError(errc::invalid_argument, |
| 512 | "Multiple renames of section %s", |
| 513 | SR->OriginalName.str().c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 514 | } |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 515 | for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 516 | Expected<SectionFlagsUpdate> SFU = |
| 517 | parseSetSectionFlagValue(Arg->getValue()); |
| 518 | if (!SFU) |
| 519 | return SFU.takeError(); |
| 520 | if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second) |
| 521 | return createStringError( |
| 522 | errc::invalid_argument, |
| 523 | "--set-section-flags set multiple times for section %s", |
| 524 | SFU->Name.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 525 | } |
| 526 | // Prohibit combinations of --set-section-flags when the section name is used |
| 527 | // by --rename-section, either as a source or a destination. |
| 528 | for (const auto &E : Config.SectionsToRename) { |
| 529 | const SectionRename &SR = E.second; |
| 530 | if (Config.SetSectionFlags.count(SR.OriginalName)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 531 | return createStringError( |
| 532 | errc::invalid_argument, |
| 533 | "--set-section-flags=%s conflicts with --rename-section=%s=%s", |
| 534 | SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(), |
| 535 | SR.NewName.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 536 | if (Config.SetSectionFlags.count(SR.NewName)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 537 | return createStringError( |
| 538 | errc::invalid_argument, |
| 539 | "--set-section-flags=%s conflicts with --rename-section=%s=%s", |
| 540 | SR.NewName.str().c_str(), SR.OriginalName.str().c_str(), |
| 541 | SR.NewName.str().c_str()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 542 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 543 | |
| 544 | for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 545 | Config.ToRemove.emplace_back(Arg->getValue(), UseRegex); |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 546 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 547 | Config.KeepSection.emplace_back(Arg->getValue(), UseRegex); |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 548 | for (auto Arg : InputArgs.filtered(OBJCOPY_only_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 549 | Config.OnlySection.emplace_back(Arg->getValue(), UseRegex); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 550 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) |
| 551 | Config.AddSection.push_back(Arg->getValue()); |
| 552 | for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section)) |
| 553 | Config.DumpSection.push_back(Arg->getValue()); |
| 554 | Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all); |
| 555 | Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu); |
| 556 | Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug); |
| 557 | Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); |
| 558 | Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); |
| 559 | Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); |
| 560 | Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); |
| 561 | Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); |
| 562 | Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); |
| 563 | Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 564 | if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals)) |
| 565 | Config.DiscardMode = |
| 566 | InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals) |
| 567 | ? DiscardType::All |
| 568 | : DiscardType::Locals; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 569 | Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); |
| 570 | Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); |
| 571 | Config.DecompressDebugSections = |
| 572 | InputArgs.hasArg(OBJCOPY_decompress_debug_sections); |
| 573 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 574 | Config.SymbolsToLocalize.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 575 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 576 | if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, DC.Alloc, |
| 577 | Arg->getValue(), UseRegex)) |
| 578 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 579 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 580 | Config.SymbolsToKeepGlobal.emplace_back(Arg->getValue(), UseRegex); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 581 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 582 | if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc, |
| 583 | Arg->getValue(), UseRegex)) |
| 584 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 585 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 586 | Config.SymbolsToGlobalize.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 587 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 588 | if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc, |
| 589 | Arg->getValue(), UseRegex)) |
| 590 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 591 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 592 | Config.SymbolsToWeaken.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 593 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 594 | if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, DC.Alloc, |
| 595 | Arg->getValue(), UseRegex)) |
| 596 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 597 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 598 | Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | e08fe35 | 2019-02-08 14:37:54 +0000 | [diff] [blame] | 599 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 600 | if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc, |
| 601 | Arg->getValue(), UseRegex)) |
| 602 | return std::move(E); |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 603 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol)) |
| 604 | Config.UnneededSymbolsToRemove.emplace_back(Arg->getValue(), UseRegex); |
| 605 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 606 | if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc, |
| 607 | Arg->getValue(), UseRegex)) |
| 608 | return std::move(E); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 609 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 610 | Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegex); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame^] | 611 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_symbol)) |
| 612 | Config.SymbolsToAdd.push_back(parseNewSymbolInfo(Arg->getValue())); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 613 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 614 | Config.DeterministicArchives = InputArgs.hasFlag( |
| 615 | OBJCOPY_enable_deterministic_archives, |
| 616 | OBJCOPY_disable_deterministic_archives, /*default=*/true); |
| 617 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 618 | Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); |
| 619 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 620 | if (Config.DecompressDebugSections && |
| 621 | Config.CompressionType != DebugCompressionType::None) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 622 | return createStringError( |
| 623 | errc::invalid_argument, |
| 624 | "Cannot specify --compress-debug-sections at the same time as " |
| 625 | "--decompress-debug-sections at the same time"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (Config.DecompressDebugSections && !zlib::isAvailable()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 629 | return createStringError( |
| 630 | errc::invalid_argument, |
| 631 | "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 632 | |
Jordan Rupprecht | ab9f662 | 2018-10-23 20:54:51 +0000 | [diff] [blame] | 633 | DC.CopyConfigs.push_back(std::move(Config)); |
Jordan Rupprecht | 93ad8b3 | 2019-02-21 17:24:55 +0000 | [diff] [blame] | 634 | return std::move(DC); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 638 | // help flag is set then ParseStripOptions will print the help messege and |
| 639 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 640 | Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) { |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 641 | StripOptTable T; |
| 642 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 643 | llvm::opt::InputArgList InputArgs = |
| 644 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 645 | |
| 646 | if (InputArgs.size() == 0) { |
| 647 | T.PrintHelp(errs(), "llvm-strip [options] file...", "strip tool"); |
| 648 | exit(1); |
| 649 | } |
| 650 | |
| 651 | if (InputArgs.hasArg(STRIP_help)) { |
| 652 | T.PrintHelp(outs(), "llvm-strip [options] file...", "strip tool"); |
| 653 | exit(0); |
| 654 | } |
| 655 | |
| 656 | if (InputArgs.hasArg(STRIP_version)) { |
Martin Storsjo | e9af715 | 2018-11-28 06:51:50 +0000 | [diff] [blame] | 657 | outs() << "llvm-strip, compatible with GNU strip\n"; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 658 | cl::PrintVersionMessage(); |
| 659 | exit(0); |
| 660 | } |
| 661 | |
| 662 | SmallVector<const char *, 2> Positional; |
| 663 | for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 664 | return createStringError(errc::invalid_argument, "unknown argument '%s'", |
| 665 | Arg->getAsString(InputArgs).c_str()); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 666 | for (auto Arg : InputArgs.filtered(STRIP_INPUT)) |
| 667 | Positional.push_back(Arg->getValue()); |
| 668 | |
| 669 | if (Positional.empty()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 670 | return createStringError(errc::invalid_argument, "No input file specified"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 671 | |
| 672 | if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 673 | return createStringError( |
| 674 | errc::invalid_argument, |
| 675 | "Multiple input files cannot be used in combination with -o"); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 676 | |
| 677 | CopyConfig Config; |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 678 | bool UseRegexp = InputArgs.hasArg(STRIP_regex); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 679 | Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug); |
| 680 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 681 | if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals)) |
| 682 | Config.DiscardMode = |
| 683 | InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals) |
| 684 | ? DiscardType::All |
| 685 | : DiscardType::Locals; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 686 | Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); |
| 687 | Config.StripAll = InputArgs.hasArg(STRIP_strip_all); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 688 | Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu); |
Eugene Leviant | 05a3f99 | 2019-02-01 15:25:15 +0000 | [diff] [blame] | 689 | Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 690 | |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 691 | for (auto Arg : InputArgs.filtered(STRIP_keep_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 692 | Config.KeepSection.emplace_back(Arg->getValue(), UseRegexp); |
Jordan Rupprecht | 30d1b19 | 2018-11-01 17:48:46 +0000 | [diff] [blame] | 693 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 694 | for (auto Arg : InputArgs.filtered(STRIP_remove_section)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 695 | Config.ToRemove.emplace_back(Arg->getValue(), UseRegexp); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 696 | |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 697 | for (auto Arg : InputArgs.filtered(STRIP_strip_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 698 | Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegexp); |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 699 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 700 | for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 701 | Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegexp); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 702 | |
Eugene Leviant | 2267c58 | 2019-01-31 12:16:20 +0000 | [diff] [blame] | 703 | if (!Config.StripDebug && !Config.StripUnneeded && |
| 704 | Config.DiscardMode == DiscardType::None && !Config.StripAllGNU && Config.SymbolsToRemove.empty()) |
| 705 | Config.StripAll = true; |
| 706 | |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 707 | Config.DeterministicArchives = |
| 708 | InputArgs.hasFlag(STRIP_enable_deterministic_archives, |
| 709 | STRIP_disable_deterministic_archives, /*default=*/true); |
| 710 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 711 | Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates); |
| 712 | |
| 713 | DriverConfig DC; |
| 714 | if (Positional.size() == 1) { |
| 715 | Config.InputFilename = Positional[0]; |
| 716 | Config.OutputFilename = |
| 717 | InputArgs.getLastArgValue(STRIP_output, Positional[0]); |
| 718 | DC.CopyConfigs.push_back(std::move(Config)); |
| 719 | } else { |
| 720 | for (const char *Filename : Positional) { |
| 721 | Config.InputFilename = Filename; |
| 722 | Config.OutputFilename = Filename; |
| 723 | DC.CopyConfigs.push_back(Config); |
| 724 | } |
| 725 | } |
| 726 | |
Jordan Rupprecht | 93ad8b3 | 2019-02-21 17:24:55 +0000 | [diff] [blame] | 727 | return std::move(DC); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | } // namespace objcopy |
| 731 | } // namespace llvm |