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