Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 1 | //===- llvm-objcopy.cpp ---------------------------------------------------===// |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 9 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 10 | #include "llvm-objcopy.h" |
| 11 | #include "Object.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
| 14 | #include "llvm/ADT/Twine.h" |
| 15 | #include "llvm/BinaryFormat/ELF.h" |
| 16 | #include "llvm/Object/Binary.h" |
| 17 | #include "llvm/Object/ELFObjectFile.h" |
| 18 | #include "llvm/Object/ELFTypes.h" |
| 19 | #include "llvm/Object/Error.h" |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 20 | #include "llvm/Option/Arg.h" |
| 21 | #include "llvm/Option/ArgList.h" |
| 22 | #include "llvm/Option/Option.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Casting.h" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include "llvm/Support/Error.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/ErrorOr.h" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 29 | #include "llvm/Support/FileOutputBuffer.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/InitLLVM.h" |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Path.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include <algorithm> |
| 34 | #include <cassert> |
| 35 | #include <cstdlib> |
| 36 | #include <functional> |
| 37 | #include <iterator> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 38 | #include <memory> |
| 39 | #include <string> |
| 40 | #include <system_error> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 41 | #include <utility> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace llvm; |
| 44 | using namespace object; |
| 45 | using namespace ELF; |
| 46 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 49 | enum ObjcopyID { |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 50 | OBJCOPY_INVALID = 0, // This is not an option ID. |
| 51 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 52 | HELPTEXT, METAVAR, VALUES) \ |
| 53 | OBJCOPY_##ID, |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 54 | #include "ObjcopyOpts.inc" |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 55 | #undef OPTION |
| 56 | }; |
| 57 | |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 58 | #define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE; |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 59 | #include "ObjcopyOpts.inc" |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 60 | #undef PREFIX |
| 61 | |
Alexander Shaposhnikov | 3326e78 | 2018-04-24 06:23:22 +0000 | [diff] [blame] | 62 | static const opt::OptTable::Info ObjcopyInfoTable[] = { |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 63 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 64 | HELPTEXT, METAVAR, VALUES) \ |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 65 | {OBJCOPY_##PREFIX, \ |
| 66 | NAME, \ |
| 67 | HELPTEXT, \ |
| 68 | METAVAR, \ |
| 69 | OBJCOPY_##ID, \ |
| 70 | opt::Option::KIND##Class, \ |
| 71 | PARAM, \ |
| 72 | FLAGS, \ |
| 73 | OBJCOPY_##GROUP, \ |
| 74 | OBJCOPY_##ALIAS, \ |
| 75 | ALIASARGS, \ |
| 76 | VALUES}, |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 77 | #include "ObjcopyOpts.inc" |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 78 | #undef OPTION |
| 79 | }; |
| 80 | |
| 81 | class ObjcopyOptTable : public opt::OptTable { |
| 82 | public: |
| 83 | ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {} |
| 84 | }; |
| 85 | |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 86 | enum StripID { |
| 87 | STRIP_INVALID = 0, // This is not an option ID. |
| 88 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 89 | HELPTEXT, METAVAR, VALUES) \ |
| 90 | STRIP_##ID, |
| 91 | #include "StripOpts.inc" |
| 92 | #undef OPTION |
| 93 | }; |
| 94 | |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 95 | #define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE; |
| 96 | #include "StripOpts.inc" |
| 97 | #undef PREFIX |
| 98 | |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 99 | static const opt::OptTable::Info StripInfoTable[] = { |
| 100 | #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ |
| 101 | HELPTEXT, METAVAR, VALUES) \ |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 102 | {STRIP_##PREFIX, NAME, HELPTEXT, \ |
| 103 | METAVAR, STRIP_##ID, opt::Option::KIND##Class, \ |
| 104 | PARAM, FLAGS, STRIP_##GROUP, \ |
| 105 | STRIP_##ALIAS, ALIASARGS, VALUES}, |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 106 | #include "StripOpts.inc" |
| 107 | #undef OPTION |
| 108 | }; |
| 109 | |
| 110 | class StripOptTable : public opt::OptTable { |
| 111 | public: |
| 112 | StripOptTable() : OptTable(StripInfoTable, true) {} |
| 113 | }; |
| 114 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 115 | } // namespace |
| 116 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 117 | // The name this program was invoked as. |
| 118 | static StringRef ToolName; |
| 119 | |
| 120 | namespace llvm { |
| 121 | |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 122 | LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 123 | errs() << ToolName << ": " << Message << ".\n"; |
| 124 | errs().flush(); |
| 125 | exit(1); |
| 126 | } |
| 127 | |
| 128 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) { |
| 129 | assert(EC); |
| 130 | errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n"; |
| 131 | exit(1); |
| 132 | } |
| 133 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 134 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 135 | assert(E); |
| 136 | std::string Buf; |
| 137 | raw_string_ostream OS(Buf); |
| 138 | logAllUnhandledErrors(std::move(E), OS, ""); |
| 139 | OS.flush(); |
| 140 | errs() << ToolName << ": '" << File << "': " << Buf; |
| 141 | exit(1); |
| 142 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 143 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 144 | } // end namespace llvm |
| 145 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 146 | struct CopyConfig { |
| 147 | StringRef OutputFilename; |
| 148 | StringRef InputFilename; |
| 149 | StringRef OutputFormat; |
| 150 | StringRef InputFormat; |
| 151 | StringRef BinaryArch; |
Alexander Shaposhnikov | fedb016 | 2018-02-09 23:33:31 +0000 | [diff] [blame] | 152 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 153 | StringRef SplitDWO; |
| 154 | StringRef AddGnuDebugLink; |
| 155 | std::vector<StringRef> ToRemove; |
| 156 | std::vector<StringRef> Keep; |
| 157 | std::vector<StringRef> OnlyKeep; |
| 158 | std::vector<StringRef> AddSection; |
Alexander Shaposhnikov | 40e9bdf | 2018-04-26 18:28:17 +0000 | [diff] [blame] | 159 | std::vector<StringRef> SymbolsToLocalize; |
Paul Semel | ee5be79 | 2018-04-27 19:09:44 +0000 | [diff] [blame] | 160 | std::vector<StringRef> SymbolsToGlobalize; |
Paul Semel | 3a8a56b | 2018-04-27 19:16:27 +0000 | [diff] [blame] | 161 | std::vector<StringRef> SymbolsToWeaken; |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 162 | std::vector<StringRef> SymbolsToRemove; |
Paul Semel | 5d97c82 | 2018-05-15 14:09:37 +0000 | [diff] [blame] | 163 | std::vector<StringRef> SymbolsToKeep; |
Alexander Shaposhnikov | 40e9bdf | 2018-04-26 18:28:17 +0000 | [diff] [blame] | 164 | StringMap<StringRef> SymbolsToRename; |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 165 | bool StripAll = false; |
| 166 | bool StripAllGNU = false; |
| 167 | bool StripDebug = false; |
| 168 | bool StripSections = false; |
| 169 | bool StripNonAlloc = false; |
| 170 | bool StripDWO = false; |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 171 | bool StripUnneeded = false; |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 172 | bool ExtractDWO = false; |
| 173 | bool LocalizeHidden = false; |
| 174 | bool Weaken = false; |
| 175 | bool DiscardAll = false; |
Jake Ehrlich | e40398a | 2018-05-15 20:53:53 +0000 | [diff] [blame] | 176 | bool OnlyKeepDebug = false; |
Paul Semel | cf51c80 | 2018-05-26 08:10:37 +0000 | [diff] [blame] | 177 | bool KeepFileSymbols = false; |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 178 | }; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 179 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 180 | using SectionPred = std::function<bool(const SectionBase &Sec)>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 181 | |
Jake Ehrlich | 777fb00 | 2017-12-15 20:17:55 +0000 | [diff] [blame] | 182 | bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); } |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 183 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 184 | bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 185 | // We can't remove the section header string table. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 186 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 187 | return false; |
| 188 | // Short of keeping the string table we want to keep everything that is a DWO |
| 189 | // section and remove everything else. |
| 190 | return !IsDWOSection(Sec); |
| 191 | } |
| 192 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 193 | std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj, |
| 194 | StringRef File, ElfType OutputElfType) { |
| 195 | if (Config.OutputFormat == "binary") { |
| 196 | return llvm::make_unique<BinaryWriter>(File, Obj); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 197 | } |
| 198 | // Depending on the initial ELFT and OutputFormat we need a different Writer. |
| 199 | switch (OutputElfType) { |
| 200 | case ELFT_ELF32LE: |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 201 | return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, |
| 202 | !Config.StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 203 | case ELFT_ELF64LE: |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 204 | return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj, |
| 205 | !Config.StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 206 | case ELFT_ELF32BE: |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 207 | return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj, |
| 208 | !Config.StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 209 | case ELFT_ELF64BE: |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 210 | return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj, |
| 211 | !Config.StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 212 | } |
| 213 | llvm_unreachable("Invalid output format"); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 216 | void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader, |
| 217 | StringRef File, ElfType OutputElfType) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 218 | auto DWOFile = Reader.create(); |
| 219 | DWOFile->removeSections( |
| 220 | [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); }); |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 221 | auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 222 | Writer->finalize(); |
| 223 | Writer->write(); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 226 | // This function handles the high level operations of GNU objcopy including |
| 227 | // handling command line options. It's important to outline certain properties |
| 228 | // we expect to hold of the command line operations. Any operation that "keeps" |
| 229 | // should keep regardless of a remove. Additionally any removal should respect |
| 230 | // any previous removals. Lastly whether or not something is removed shouldn't |
| 231 | // depend a) on the order the options occur in or b) on some opaque priority |
| 232 | // system. The only priority is that keeps/copies overrule removes. |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 233 | void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader, |
| 234 | ElfType OutputElfType) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 235 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 236 | if (!Config.SplitDWO.empty()) { |
| 237 | SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 238 | } |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 239 | |
| 240 | // TODO: update or remove symbols only if there is an option that affects |
| 241 | // them. |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 242 | if (Obj.SymbolTable) { |
| 243 | Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { |
| 244 | if ((Config.LocalizeHidden && |
| 245 | (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || |
| 246 | (!Config.SymbolsToLocalize.empty() && |
| 247 | is_contained(Config.SymbolsToLocalize, Sym.Name))) |
| 248 | Sym.Binding = STB_LOCAL; |
| 249 | |
| 250 | if (!Config.SymbolsToGlobalize.empty() && |
| 251 | is_contained(Config.SymbolsToGlobalize, Sym.Name)) |
| 252 | Sym.Binding = STB_GLOBAL; |
| 253 | |
| 254 | if (!Config.SymbolsToWeaken.empty() && |
| 255 | is_contained(Config.SymbolsToWeaken, Sym.Name) && |
| 256 | Sym.Binding == STB_GLOBAL) |
| 257 | Sym.Binding = STB_WEAK; |
| 258 | |
| 259 | if (Config.Weaken && Sym.Binding == STB_GLOBAL && |
| 260 | Sym.getShndx() != SHN_UNDEF) |
| 261 | Sym.Binding = STB_WEAK; |
| 262 | |
| 263 | const auto I = Config.SymbolsToRename.find(Sym.Name); |
| 264 | if (I != Config.SymbolsToRename.end()) |
| 265 | Sym.Name = I->getValue(); |
| 266 | }); |
| 267 | |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 268 | // The purpose of this loop is to mark symbols referenced by sections |
| 269 | // (like GroupSection or RelocationSection). This way, we know which |
| 270 | // symbols are still 'needed' and wich are not. |
| 271 | if (Config.StripUnneeded) { |
| 272 | for (auto &Section : Obj.sections()) |
| 273 | Section.markSymbols(); |
| 274 | } |
| 275 | |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 276 | Obj.removeSymbols([&](const Symbol &Sym) { |
Paul Semel | cf51c80 | 2018-05-26 08:10:37 +0000 | [diff] [blame] | 277 | if ((!Config.SymbolsToKeep.empty() && |
| 278 | is_contained(Config.SymbolsToKeep, Sym.Name)) || |
| 279 | (Config.KeepFileSymbols && Sym.Type == STT_FILE)) |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 280 | return false; |
| 281 | |
| 282 | if (Config.DiscardAll && Sym.Binding == STB_LOCAL && |
| 283 | Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE && |
| 284 | Sym.Type != STT_SECTION) |
| 285 | return true; |
| 286 | |
| 287 | if (Config.StripAll || Config.StripAllGNU) |
| 288 | return true; |
| 289 | |
| 290 | if (!Config.SymbolsToRemove.empty() && |
| 291 | is_contained(Config.SymbolsToRemove, Sym.Name)) { |
| 292 | return true; |
| 293 | } |
| 294 | |
Paul Semel | 46201fb | 2018-06-01 16:19:46 +0000 | [diff] [blame] | 295 | if (Config.StripUnneeded && !Sym.Referenced && |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 296 | (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && |
| 297 | Sym.Type != STT_FILE && Sym.Type != STT_SECTION) |
| 298 | return true; |
| 299 | |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 300 | return false; |
| 301 | }); |
| 302 | } |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 303 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 304 | SectionPred RemovePred = [](const SectionBase &) { return false; }; |
| 305 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 306 | // Removes: |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 307 | if (!Config.ToRemove.empty()) { |
| 308 | RemovePred = [&Config](const SectionBase &Sec) { |
| 309 | return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove), |
| 310 | Sec.Name) != std::end(Config.ToRemove); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 311 | }; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 312 | } |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 313 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 314 | if (Config.StripDWO || !Config.SplitDWO.empty()) |
David Blaikie | 998ff81 | 2017-11-03 20:57:09 +0000 | [diff] [blame] | 315 | RemovePred = [RemovePred](const SectionBase &Sec) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 316 | return IsDWOSection(Sec) || RemovePred(Sec); |
| 317 | }; |
| 318 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 319 | if (Config.ExtractDWO) |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 320 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 321 | return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 324 | if (Config.StripAllGNU) |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 325 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 326 | if (RemovePred(Sec)) |
| 327 | return true; |
| 328 | if ((Sec.Flags & SHF_ALLOC) != 0) |
| 329 | return false; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 330 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 331 | return false; |
Jake Ehrlich | 777fb00 | 2017-12-15 20:17:55 +0000 | [diff] [blame] | 332 | switch (Sec.Type) { |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 333 | case SHT_SYMTAB: |
| 334 | case SHT_REL: |
| 335 | case SHT_RELA: |
| 336 | case SHT_STRTAB: |
| 337 | return true; |
| 338 | } |
| 339 | return Sec.Name.startswith(".debug"); |
| 340 | }; |
| 341 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 342 | if (Config.StripSections) { |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 343 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 344 | return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; |
| 345 | }; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 348 | if (Config.StripDebug) { |
Jake Ehrlich | 1bfefc1 | 2017-11-13 22:13:08 +0000 | [diff] [blame] | 349 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 350 | return RemovePred(Sec) || Sec.Name.startswith(".debug"); |
| 351 | }; |
| 352 | } |
| 353 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 354 | if (Config.StripNonAlloc) |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame] | 355 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 356 | if (RemovePred(Sec)) |
| 357 | return true; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 358 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame] | 359 | return false; |
| 360 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 361 | }; |
| 362 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 363 | if (Config.StripAll) |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 364 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 365 | if (RemovePred(Sec)) |
| 366 | return true; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 367 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 368 | return false; |
| 369 | if (Sec.Name.startswith(".gnu.warning")) |
| 370 | return false; |
| 371 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 372 | }; |
| 373 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 374 | // Explicit copies: |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 375 | if (!Config.OnlyKeep.empty()) { |
| 376 | RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 377 | // Explicitly keep these sections regardless of previous removes. |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 378 | if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep), |
| 379 | Sec.Name) != std::end(Config.OnlyKeep)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 380 | return false; |
| 381 | |
| 382 | // Allow all implicit removes. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 383 | if (RemovePred(Sec)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 384 | return true; |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 385 | |
| 386 | // Keep special sections. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 387 | if (Obj.SectionNames == &Sec) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 388 | return false; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 389 | if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 390 | return false; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 391 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 392 | // Remove everything else. |
| 393 | return true; |
| 394 | }; |
| 395 | } |
| 396 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 397 | if (!Config.Keep.empty()) { |
| 398 | RemovePred = [Config, RemovePred](const SectionBase &Sec) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 399 | // Explicitly keep these sections regardless of previous removes. |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 400 | if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) != |
| 401 | std::end(Config.Keep)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 402 | return false; |
| 403 | // Otherwise defer to RemovePred. |
| 404 | return RemovePred(Sec); |
| 405 | }; |
| 406 | } |
| 407 | |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 408 | // This has to be the last predicate assignment. |
| 409 | // If the option --keep-symbol has been specified |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 410 | // and at least one of those symbols is present |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 411 | // (equivalently, the updated symbol table is not empty) |
| 412 | // the symbol table and the string table should not be removed. |
Paul Semel | cf51c80 | 2018-05-26 08:10:37 +0000 | [diff] [blame] | 413 | if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) && |
| 414 | !Obj.SymbolTable->empty()) { |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 415 | RemovePred = [&Obj, RemovePred](const SectionBase &Sec) { |
| 416 | if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab()) |
| 417 | return false; |
| 418 | return RemovePred(Sec); |
| 419 | }; |
| 420 | } |
| 421 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 422 | Obj.removeSections(RemovePred); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 423 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 424 | if (!Config.AddSection.empty()) { |
| 425 | for (const auto &Flag : Config.AddSection) { |
| 426 | auto SecPair = Flag.split("="); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 427 | auto SecName = SecPair.first; |
| 428 | auto File = SecPair.second; |
| 429 | auto BufOrErr = MemoryBuffer::getFile(File); |
| 430 | if (!BufOrErr) |
| 431 | reportError(File, BufOrErr.getError()); |
| 432 | auto Buf = std::move(*BufOrErr); |
| 433 | auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart()); |
| 434 | auto BufSize = Buf->getBufferSize(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 435 | Obj.addSection<OwnedDataSection>(SecName, |
| 436 | ArrayRef<uint8_t>(BufPtr, BufSize)); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Alexander Shaposhnikov | 40e9bdf | 2018-04-26 18:28:17 +0000 | [diff] [blame] | 440 | if (!Config.AddGnuDebugLink.empty()) |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 441 | Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 442 | } |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 443 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 444 | std::unique_ptr<Reader> CreateReader(StringRef InputFilename, |
| 445 | ElfType &OutputElfType) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 446 | // Right now we can only read ELF files so there's only one reader; |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 447 | auto Out = llvm::make_unique<ELFReader>(InputFilename); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 448 | // We need to set the default ElfType for output. |
| 449 | OutputElfType = Out->getElfType(); |
| 450 | return std::move(Out); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 453 | void ExecuteElfObjcopy(const CopyConfig &Config) { |
| 454 | ElfType OutputElfType; |
| 455 | auto Reader = CreateReader(Config.InputFilename, OutputElfType); |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 456 | auto Obj = Reader->create(); |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 457 | auto Writer = |
| 458 | CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType); |
| 459 | HandleArgs(Config, *Obj, *Reader, OutputElfType); |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 460 | Writer->finalize(); |
| 461 | Writer->write(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 462 | } |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 463 | |
| 464 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 465 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 466 | // exit. |
| 467 | CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) { |
| 468 | ObjcopyOptTable T; |
| 469 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 470 | llvm::opt::InputArgList InputArgs = |
| 471 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
Alexander Shaposhnikov | d29bf4c | 2018-05-18 04:18:41 +0000 | [diff] [blame] | 472 | |
Alexander Shaposhnikov | b07c22b | 2018-05-08 17:12:54 +0000 | [diff] [blame] | 473 | if (InputArgs.size() == 0) { |
| 474 | T.PrintHelp(errs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool"); |
| 475 | exit(1); |
| 476 | } |
Alexander Shaposhnikov | d29bf4c | 2018-05-18 04:18:41 +0000 | [diff] [blame] | 477 | |
Alexander Shaposhnikov | b07c22b | 2018-05-08 17:12:54 +0000 | [diff] [blame] | 478 | if (InputArgs.hasArg(OBJCOPY_help)) { |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 479 | T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool"); |
| 480 | exit(0); |
| 481 | } |
| 482 | |
| 483 | SmallVector<const char *, 2> Positional; |
| 484 | |
| 485 | for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN)) |
| 486 | error("unknown argument '" + Arg->getAsString(InputArgs) + "'"); |
| 487 | |
| 488 | for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT)) |
| 489 | Positional.push_back(Arg->getValue()); |
| 490 | |
| 491 | if (Positional.empty()) |
| 492 | error("No input file specified"); |
| 493 | |
| 494 | if (Positional.size() > 2) |
| 495 | error("Too many positional arguments"); |
| 496 | |
| 497 | CopyConfig Config; |
| 498 | Config.InputFilename = Positional[0]; |
| 499 | Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1]; |
| 500 | Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target); |
| 501 | Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); |
| 502 | Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture); |
| 503 | |
| 504 | Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); |
| 505 | Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); |
Alexander Shaposhnikov | 40e9bdf | 2018-04-26 18:28:17 +0000 | [diff] [blame] | 506 | |
| 507 | for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { |
| 508 | if (!StringRef(Arg->getValue()).contains('=')) |
| 509 | error("Bad format for --redefine-sym"); |
| 510 | auto Old2New = StringRef(Arg->getValue()).split('='); |
| 511 | if (!Config.SymbolsToRename.insert(Old2New).second) |
| 512 | error("Multiple redefinition of symbol " + Old2New.first); |
| 513 | } |
| 514 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 515 | for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section)) |
| 516 | Config.ToRemove.push_back(Arg->getValue()); |
| 517 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep)) |
| 518 | Config.Keep.push_back(Arg->getValue()); |
| 519 | for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep)) |
| 520 | Config.OnlyKeep.push_back(Arg->getValue()); |
| 521 | for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) |
| 522 | Config.AddSection.push_back(Arg->getValue()); |
| 523 | Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all); |
| 524 | Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu); |
| 525 | Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug); |
| 526 | Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo); |
| 527 | Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections); |
| 528 | Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 529 | Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded); |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 530 | Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); |
| 531 | Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); |
Paul Semel | 2c0510f | 2018-05-02 20:14:49 +0000 | [diff] [blame] | 532 | Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); |
Paul Semel | 41695f8 | 2018-05-02 20:19:22 +0000 | [diff] [blame] | 533 | Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all); |
Jake Ehrlich | e40398a | 2018-05-15 20:53:53 +0000 | [diff] [blame] | 534 | Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); |
Paul Semel | cf51c80 | 2018-05-26 08:10:37 +0000 | [diff] [blame] | 535 | Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); |
Paul Semel | b492494 | 2018-04-26 17:44:43 +0000 | [diff] [blame] | 536 | for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) |
Alexander Shaposhnikov | 40e9bdf | 2018-04-26 18:28:17 +0000 | [diff] [blame] | 537 | Config.SymbolsToLocalize.push_back(Arg->getValue()); |
Paul Semel | ee5be79 | 2018-04-27 19:09:44 +0000 | [diff] [blame] | 538 | for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) |
| 539 | Config.SymbolsToGlobalize.push_back(Arg->getValue()); |
Paul Semel | 3a8a56b | 2018-04-27 19:16:27 +0000 | [diff] [blame] | 540 | for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol)) |
| 541 | Config.SymbolsToWeaken.push_back(Arg->getValue()); |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 542 | for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol)) |
| 543 | Config.SymbolsToRemove.push_back(Arg->getValue()); |
Paul Semel | 5d97c82 | 2018-05-15 14:09:37 +0000 | [diff] [blame] | 544 | for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) |
| 545 | Config.SymbolsToKeep.push_back(Arg->getValue()); |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 546 | |
| 547 | return Config; |
| 548 | } |
| 549 | |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 550 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 551 | // help flag is set then ParseStripOptions will print the help messege and |
| 552 | // exit. |
| 553 | CopyConfig ParseStripOptions(ArrayRef<const char *> ArgsArr) { |
| 554 | StripOptTable T; |
| 555 | unsigned MissingArgumentIndex, MissingArgumentCount; |
| 556 | llvm::opt::InputArgList InputArgs = |
| 557 | T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount); |
| 558 | |
Alexander Shaposhnikov | b07c22b | 2018-05-08 17:12:54 +0000 | [diff] [blame] | 559 | if (InputArgs.size() == 0) { |
| 560 | T.PrintHelp(errs(), "llvm-strip <input> [ <output> ]", "strip tool"); |
| 561 | exit(1); |
| 562 | } |
Alexander Shaposhnikov | d29bf4c | 2018-05-18 04:18:41 +0000 | [diff] [blame] | 563 | |
Alexander Shaposhnikov | b07c22b | 2018-05-08 17:12:54 +0000 | [diff] [blame] | 564 | if (InputArgs.hasArg(STRIP_help)) { |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 565 | T.PrintHelp(outs(), "llvm-strip <input> [ <output> ]", "strip tool"); |
| 566 | exit(0); |
| 567 | } |
| 568 | |
| 569 | SmallVector<const char *, 2> Positional; |
| 570 | for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN)) |
| 571 | error("unknown argument '" + Arg->getAsString(InputArgs) + "'"); |
| 572 | for (auto Arg : InputArgs.filtered(STRIP_INPUT)) |
| 573 | Positional.push_back(Arg->getValue()); |
| 574 | |
| 575 | if (Positional.empty()) |
| 576 | error("No input file specified"); |
| 577 | |
| 578 | if (Positional.size() > 2) |
| 579 | error("Support for multiple input files is not implemented yet"); |
| 580 | |
| 581 | CopyConfig Config; |
| 582 | Config.InputFilename = Positional[0]; |
Alexander Shaposhnikov | ecc8483 | 2018-05-31 20:42:13 +0000 | [diff] [blame] | 583 | Config.OutputFilename = |
| 584 | InputArgs.getLastArgValue(STRIP_output, Positional[0]); |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 585 | |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 586 | Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug); |
Alexander Shaposhnikov | 29407f3 | 2018-06-06 21:23:19 +0000 | [diff] [blame] | 587 | |
| 588 | Config.DiscardAll = InputArgs.hasArg(STRIP_discard_all); |
Paul Semel | e57bc78 | 2018-06-07 10:05:25 +0000 | [diff] [blame] | 589 | Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); |
| 590 | |
| 591 | if (!Config.StripDebug && !Config.StripUnneeded && !Config.DiscardAll) |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 592 | Config.StripAll = true; |
Alexander Shaposhnikov | d29bf4c | 2018-05-18 04:18:41 +0000 | [diff] [blame] | 593 | |
Alexander Shaposhnikov | 18b5fb7 | 2018-05-11 05:27:06 +0000 | [diff] [blame] | 594 | for (auto Arg : InputArgs.filtered(STRIP_remove_section)) |
| 595 | Config.ToRemove.push_back(Arg->getValue()); |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 596 | |
Alexander Shaposhnikov | 35bee3e | 2018-05-23 19:44:19 +0000 | [diff] [blame] | 597 | for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) |
| 598 | Config.SymbolsToKeep.push_back(Arg->getValue()); |
Alexander Shaposhnikov | 18b5fb7 | 2018-05-11 05:27:06 +0000 | [diff] [blame] | 599 | |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 600 | return Config; |
| 601 | } |
| 602 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 603 | int main(int argc, char **argv) { |
| 604 | InitLLVM X(argc, argv); |
| 605 | ToolName = argv[0]; |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 606 | CopyConfig Config; |
| 607 | if (sys::path::stem(ToolName).endswith_lower("strip")) |
| 608 | Config = ParseStripOptions(makeArrayRef(argv + 1, argc)); |
| 609 | else |
| 610 | Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc)); |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 611 | ExecuteElfObjcopy(Config); |
| 612 | } |