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" |
| 20 | #include "llvm/Support/Casting.h" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/Error.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/ErrorOr.h" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FileOutputBuffer.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/InitLLVM.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include <algorithm> |
| 30 | #include <cassert> |
| 31 | #include <cstdlib> |
| 32 | #include <functional> |
| 33 | #include <iterator> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 34 | #include <memory> |
| 35 | #include <string> |
| 36 | #include <system_error> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 37 | #include <utility> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace llvm; |
| 40 | using namespace object; |
| 41 | using namespace ELF; |
| 42 | |
| 43 | // The name this program was invoked as. |
| 44 | static StringRef ToolName; |
| 45 | |
| 46 | namespace llvm { |
| 47 | |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 48 | LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 49 | errs() << ToolName << ": " << Message << ".\n"; |
| 50 | errs().flush(); |
| 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) { |
| 55 | assert(EC); |
| 56 | errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n"; |
| 57 | exit(1); |
| 58 | } |
| 59 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 60 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 61 | assert(E); |
| 62 | std::string Buf; |
| 63 | raw_string_ostream OS(Buf); |
| 64 | logAllUnhandledErrors(std::move(E), OS, ""); |
| 65 | OS.flush(); |
| 66 | errs() << ToolName << ": '" << File << "': " << Buf; |
| 67 | exit(1); |
| 68 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 69 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 70 | } // end namespace llvm |
| 71 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 72 | static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>")); |
| 73 | static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("[ <output> ]")); |
Alexander Shaposhnikov | fedb016 | 2018-02-09 23:33:31 +0000 | [diff] [blame] | 74 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 75 | static cl::opt<std::string> |
| 76 | OutputFormat("O", cl::desc("Set output format to one of the following:" |
| 77 | "\n\tbinary")); |
| 78 | static cl::list<std::string> ToRemove("remove-section", |
| 79 | cl::desc("Remove <section>"), |
| 80 | cl::value_desc("section")); |
| 81 | static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"), |
| 82 | cl::aliasopt(ToRemove)); |
| 83 | static cl::opt<bool> StripAll( |
| 84 | "strip-all", |
| 85 | cl::desc( |
| 86 | "Removes non-allocated sections other than .gnu.warning* sections")); |
| 87 | static cl::opt<bool> |
| 88 | StripAllGNU("strip-all-gnu", |
| 89 | cl::desc("Removes symbol, relocation, and debug information")); |
| 90 | static cl::list<std::string> Keep("keep", cl::desc("Keep <section>"), |
| 91 | cl::value_desc("section")); |
| 92 | static cl::list<std::string> OnlyKeep("only-keep", |
| 93 | cl::desc("Remove all but <section>"), |
| 94 | cl::value_desc("section")); |
| 95 | static cl::alias OnlyKeepA("j", cl::desc("Alias for only-keep"), |
| 96 | cl::aliasopt(OnlyKeep)); |
| 97 | static cl::opt<bool> StripDebug("strip-debug", |
| 98 | cl::desc("Removes all debug information")); |
| 99 | static cl::opt<bool> StripSections("strip-sections", |
| 100 | cl::desc("Remove all section headers")); |
| 101 | static cl::opt<bool> |
| 102 | StripNonAlloc("strip-non-alloc", |
| 103 | cl::desc("Remove all non-allocated sections")); |
| 104 | static cl::opt<bool> |
| 105 | StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file")); |
| 106 | static cl::opt<bool> ExtractDWO( |
| 107 | "extract-dwo", |
| 108 | cl::desc("Remove all sections that are not DWARF .dwo sections from file")); |
| 109 | static cl::opt<std::string> |
| 110 | SplitDWO("split-dwo", |
| 111 | cl::desc("Equivalent to extract-dwo on the input file to " |
| 112 | "<dwo-file>, then strip-dwo on the input file"), |
| 113 | cl::value_desc("dwo-file")); |
| 114 | static cl::list<std::string> AddSection( |
| 115 | "add-section", |
| 116 | cl::desc("Make a section named <section> with the contents of <file>."), |
| 117 | cl::value_desc("section=file")); |
| 118 | static cl::opt<bool> LocalizeHidden( |
| 119 | "localize-hidden", |
| 120 | cl::desc( |
| 121 | "Mark all symbols that have hidden or internal visibility as local")); |
| 122 | static cl::opt<std::string> |
| 123 | AddGnuDebugLink("add-gnu-debuglink", |
| 124 | cl::desc("adds a .gnu_debuglink for <debug-file>"), |
| 125 | cl::value_desc("debug-file")); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 126 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 127 | using SectionPred = std::function<bool(const SectionBase &Sec)>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 128 | |
Jake Ehrlich | 777fb00 | 2017-12-15 20:17:55 +0000 | [diff] [blame] | 129 | bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); } |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 130 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 131 | bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 132 | // We can't remove the section header string table. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 133 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 134 | return false; |
| 135 | // Short of keeping the string table we want to keep everything that is a DWO |
| 136 | // section and remove everything else. |
| 137 | return !IsDWOSection(Sec); |
| 138 | } |
| 139 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 140 | static ElfType OutputElfType; |
| 141 | |
| 142 | std::unique_ptr<Writer> CreateWriter(Object &Obj, StringRef File) { |
| 143 | if (OutputFormat == "binary") { |
| 144 | return llvm::make_unique<BinaryWriter>(OutputFilename, Obj); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 145 | } |
| 146 | // Depending on the initial ELFT and OutputFormat we need a different Writer. |
| 147 | switch (OutputElfType) { |
| 148 | case ELFT_ELF32LE: |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 149 | return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, !StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 150 | case ELFT_ELF64LE: |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 151 | return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj, !StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 152 | case ELFT_ELF32BE: |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 153 | return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj, !StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 154 | case ELFT_ELF64BE: |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 155 | return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj, !StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 156 | } |
| 157 | llvm_unreachable("Invalid output format"); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 160 | void SplitDWOToFile(const Reader &Reader, StringRef File) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 161 | auto DWOFile = Reader.create(); |
| 162 | DWOFile->removeSections( |
| 163 | [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); }); |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 164 | auto Writer = CreateWriter(*DWOFile, File); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 165 | Writer->finalize(); |
| 166 | Writer->write(); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 169 | // This function handles the high level operations of GNU objcopy including |
| 170 | // handling command line options. It's important to outline certain properties |
| 171 | // we expect to hold of the command line operations. Any operation that "keeps" |
| 172 | // should keep regardless of a remove. Additionally any removal should respect |
| 173 | // any previous removals. Lastly whether or not something is removed shouldn't |
| 174 | // depend a) on the order the options occur in or b) on some opaque priority |
| 175 | // system. The only priority is that keeps/copies overrule removes. |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 176 | void HandleArgs(Object &Obj, const Reader &Reader) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 177 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 178 | if (!SplitDWO.empty()) { |
| 179 | SplitDWOToFile(Reader, SplitDWO); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 180 | } |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 181 | |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame] | 182 | // Localize: |
| 183 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 184 | if (LocalizeHidden) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 185 | Obj.SymbolTable->localize([](const Symbol &Sym) { |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame] | 186 | return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL; |
| 187 | }); |
| 188 | } |
| 189 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 190 | SectionPred RemovePred = [](const SectionBase &) { return false; }; |
| 191 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 192 | // Removes: |
| 193 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 194 | if (!ToRemove.empty()) { |
| 195 | RemovePred = [&](const SectionBase &Sec) { |
| 196 | return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) != |
| 197 | std::end(ToRemove); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 198 | }; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 199 | } |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 200 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 201 | if (StripDWO || !SplitDWO.empty()) |
David Blaikie | 998ff81 | 2017-11-03 20:57:09 +0000 | [diff] [blame] | 202 | RemovePred = [RemovePred](const SectionBase &Sec) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 203 | return IsDWOSection(Sec) || RemovePred(Sec); |
| 204 | }; |
| 205 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 206 | if (ExtractDWO) |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 207 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 208 | return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 211 | if (StripAllGNU) |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 212 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 213 | if (RemovePred(Sec)) |
| 214 | return true; |
| 215 | if ((Sec.Flags & SHF_ALLOC) != 0) |
| 216 | return false; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 217 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 218 | return false; |
Jake Ehrlich | 777fb00 | 2017-12-15 20:17:55 +0000 | [diff] [blame] | 219 | switch (Sec.Type) { |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 220 | case SHT_SYMTAB: |
| 221 | case SHT_REL: |
| 222 | case SHT_RELA: |
| 223 | case SHT_STRTAB: |
| 224 | return true; |
| 225 | } |
| 226 | return Sec.Name.startswith(".debug"); |
| 227 | }; |
| 228 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 229 | if (StripSections) { |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 230 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 231 | return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; |
| 232 | }; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 235 | if (StripDebug) { |
Jake Ehrlich | 1bfefc1 | 2017-11-13 22:13:08 +0000 | [diff] [blame] | 236 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 237 | return RemovePred(Sec) || Sec.Name.startswith(".debug"); |
| 238 | }; |
| 239 | } |
| 240 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 241 | if (StripNonAlloc) |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame] | 242 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 243 | if (RemovePred(Sec)) |
| 244 | return true; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 245 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame] | 246 | return false; |
| 247 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 248 | }; |
| 249 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 250 | if (StripAll) |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 251 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 252 | if (RemovePred(Sec)) |
| 253 | return true; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 254 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 255 | return false; |
| 256 | if (Sec.Name.startswith(".gnu.warning")) |
| 257 | return false; |
| 258 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 259 | }; |
| 260 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 261 | // Explicit copies: |
| 262 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 263 | if (!OnlyKeep.empty()) { |
| 264 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 265 | // Explicitly keep these sections regardless of previous removes. |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 266 | if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) != |
| 267 | std::end(OnlyKeep)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 268 | return false; |
| 269 | |
| 270 | // Allow all implicit removes. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 271 | if (RemovePred(Sec)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 272 | return true; |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 273 | |
| 274 | // Keep special sections. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 275 | if (Obj.SectionNames == &Sec) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 276 | return false; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 277 | if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 278 | return false; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 279 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 280 | // Remove everything else. |
| 281 | return true; |
| 282 | }; |
| 283 | } |
| 284 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 285 | if (!Keep.empty()) { |
| 286 | RemovePred = [RemovePred](const SectionBase &Sec) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 287 | // Explicitly keep these sections regardless of previous removes. |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 288 | if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) != |
| 289 | std::end(Keep)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 290 | return false; |
| 291 | // Otherwise defer to RemovePred. |
| 292 | return RemovePred(Sec); |
| 293 | }; |
| 294 | } |
| 295 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 296 | Obj.removeSections(RemovePred); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 297 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 298 | if (!AddSection.empty()) { |
| 299 | for (const auto &Flag : AddSection) { |
| 300 | auto SecPair = StringRef(Flag).split("="); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 301 | auto SecName = SecPair.first; |
| 302 | auto File = SecPair.second; |
| 303 | auto BufOrErr = MemoryBuffer::getFile(File); |
| 304 | if (!BufOrErr) |
| 305 | reportError(File, BufOrErr.getError()); |
| 306 | auto Buf = std::move(*BufOrErr); |
| 307 | auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart()); |
| 308 | auto BufSize = Buf->getBufferSize(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 309 | Obj.addSection<OwnedDataSection>(SecName, |
| 310 | ArrayRef<uint8_t>(BufPtr, BufSize)); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 314 | if (!AddGnuDebugLink.empty()) { |
| 315 | Obj.addSection<GnuDebugLinkSection>(StringRef(AddGnuDebugLink)); |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 316 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 317 | } |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 318 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 319 | std::unique_ptr<Reader> CreateReader() { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 320 | // Right now we can only read ELF files so there's only one reader; |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 321 | auto Out = llvm::make_unique<ELFReader>(StringRef(InputFilename)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 322 | // We need to set the default ElfType for output. |
| 323 | OutputElfType = Out->getElfType(); |
| 324 | return std::move(Out); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 328 | InitLLVM X(argc, argv); |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 329 | cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n"); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 330 | ToolName = argv[0]; |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 331 | if (InputFilename.empty()) { |
| 332 | cl::PrintHelpMessage(); |
| 333 | return 2; |
| 334 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 335 | |
Jake Ehrlich | a8c689e | 2018-04-12 00:40:50 +0000 | [diff] [blame] | 336 | auto Reader = CreateReader(); |
| 337 | auto Obj = Reader->create(); |
| 338 | StringRef Output = |
| 339 | OutputFilename.getNumOccurrences() ? OutputFilename : InputFilename; |
| 340 | auto Writer = CreateWriter(*Obj, Output); |
| 341 | HandleArgs(*Obj, *Reader); |
| 342 | Writer->finalize(); |
| 343 | Writer->write(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 344 | } |