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