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