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