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