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 | |
| 74 | static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>")); |
| 75 | static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"), |
Jake Ehrlich | 777fb00 | 2017-12-15 20:17:55 +0000 | [diff] [blame] | 76 | cl::init("-")); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 77 | static cl::opt<std::string> |
Jake Ehrlich | 1121662 | 2017-11-14 20:36:04 +0000 | [diff] [blame] | 78 | OutputFormat("O", cl::desc("Set output format to one of the following:" |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 79 | "\n\tbinary")); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 80 | static cl::list<std::string> ToRemove("remove-section", |
Jake Ehrlich | 1121662 | 2017-11-14 20:36:04 +0000 | [diff] [blame] | 81 | cl::desc("Remove <section>"), |
| 82 | cl::value_desc("section")); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 83 | static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"), |
| 84 | cl::aliasopt(ToRemove)); |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 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")); |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 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)); |
Jake Ehrlich | 1bfefc1 | 2017-11-13 22:13:08 +0000 | [diff] [blame] | 99 | static cl::opt<bool> StripDebug("strip-debug", |
| 100 | cl::desc("Removes all debug information")); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 101 | static cl::opt<bool> StripSections("strip-sections", |
| 102 | cl::desc("Remove all section headers")); |
Jake Ehrlich | 777fb00 | 2017-12-15 20:17:55 +0000 | [diff] [blame] | 103 | static cl::opt<bool> |
| 104 | StripNonAlloc("strip-non-alloc", |
| 105 | cl::desc("Remove all non-allocated sections")); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 106 | static cl::opt<bool> |
Jake Ehrlich | 1121662 | 2017-11-14 20:36:04 +0000 | [diff] [blame] | 107 | StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file")); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 108 | static cl::opt<bool> ExtractDWO( |
| 109 | "extract-dwo", |
Jake Ehrlich | 1121662 | 2017-11-14 20:36:04 +0000 | [diff] [blame] | 110 | cl::desc("Remove all sections that are not DWARF .dwo sections from file")); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 111 | static cl::opt<std::string> |
| 112 | SplitDWO("split-dwo", |
Jake Ehrlich | 1121662 | 2017-11-14 20:36:04 +0000 | [diff] [blame] | 113 | cl::desc("Equivalent to extract-dwo on the input file to " |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 114 | "<dwo-file>, then strip-dwo on the input file"), |
| 115 | cl::value_desc("dwo-file")); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 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")); |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame] | 120 | static cl::opt<bool> LocalizeHidden( |
| 121 | "localize-hidden", |
| 122 | cl::desc( |
| 123 | "Mark all symbols that have hidden or internal visibility as local")); |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 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 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 133 | template <class ELFT> |
| 134 | bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 135 | // We can't remove the section header string table. |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 136 | if (&Sec == Obj.getSectionHeaderStrTab()) |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 137 | return false; |
| 138 | // Short of keeping the string table we want to keep everything that is a DWO |
| 139 | // section and remove everything else. |
| 140 | return !IsDWOSection(Sec); |
| 141 | } |
| 142 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 143 | template <class ELFT> |
| 144 | void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) { |
| 145 | std::unique_ptr<FileOutputBuffer> Buffer; |
| 146 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 147 | FileOutputBuffer::create(File, Obj.totalSize(), |
| 148 | FileOutputBuffer::F_executable); |
| 149 | handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) { |
| 150 | error("failed to open " + OutputFilename); |
| 151 | }); |
| 152 | Buffer = std::move(*BufferOrErr); |
Rafael Espindola | 85593c2 | 2017-11-08 21:15:21 +0000 | [diff] [blame] | 153 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 154 | Obj.write(*Buffer); |
| 155 | if (auto E = Buffer->commit()) |
| 156 | reportError(File, errorToErrorCode(std::move(E))); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 159 | template <class ELFT> |
| 160 | void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) { |
| 161 | // Construct a second output file for the DWO sections. |
| 162 | ELFObject<ELFT> DWOFile(ObjFile); |
| 163 | |
| 164 | DWOFile.removeSections([&](const SectionBase &Sec) { |
| 165 | return OnlyKeepDWOPred<ELFT>(DWOFile, Sec); |
| 166 | }); |
| 167 | DWOFile.finalize(); |
| 168 | WriteObjectFile(DWOFile, File); |
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. |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 178 | template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { |
| 179 | std::unique_ptr<Object<ELFT>> Obj; |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 180 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 181 | if (!OutputFormat.empty() && OutputFormat != "binary") |
| 182 | error("invalid output format '" + OutputFormat + "'"); |
| 183 | if (!OutputFormat.empty() && OutputFormat == "binary") |
| 184 | Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile); |
| 185 | else |
| 186 | Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile); |
| 187 | |
| 188 | if (!SplitDWO.empty()) |
| 189 | SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue()); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 190 | |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame] | 191 | // Localize: |
| 192 | |
| 193 | if (LocalizeHidden) { |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 194 | Obj->getSymTab()->localize([](const Symbol &Sym) { |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame] | 195 | return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL; |
| 196 | }); |
| 197 | } |
| 198 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 199 | SectionPred RemovePred = [](const SectionBase &) { return false; }; |
| 200 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 201 | // Removes: |
| 202 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 203 | if (!ToRemove.empty()) { |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 204 | RemovePred = [&](const SectionBase &Sec) { |
Jake Ehrlich | fcc0562 | 2017-10-10 23:02:43 +0000 | [diff] [blame] | 205 | return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) != |
| 206 | std::end(ToRemove); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 207 | }; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 208 | } |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 209 | |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 210 | if (StripDWO || !SplitDWO.empty()) |
David Blaikie | 998ff81 | 2017-11-03 20:57:09 +0000 | [diff] [blame] | 211 | RemovePred = [RemovePred](const SectionBase &Sec) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 212 | return IsDWOSection(Sec) || RemovePred(Sec); |
| 213 | }; |
| 214 | |
| 215 | if (ExtractDWO) |
| 216 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 217 | return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 220 | if (StripAllGNU) |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 221 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 222 | if (RemovePred(Sec)) |
| 223 | return true; |
| 224 | if ((Sec.Flags & SHF_ALLOC) != 0) |
| 225 | return false; |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 226 | if (&Sec == Obj->getSectionHeaderStrTab()) |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 227 | return false; |
Jake Ehrlich | 777fb00 | 2017-12-15 20:17:55 +0000 | [diff] [blame] | 228 | switch (Sec.Type) { |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 229 | case SHT_SYMTAB: |
| 230 | case SHT_REL: |
| 231 | case SHT_RELA: |
| 232 | case SHT_STRTAB: |
| 233 | return true; |
| 234 | } |
| 235 | return Sec.Name.startswith(".debug"); |
| 236 | }; |
| 237 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 238 | if (StripSections) { |
| 239 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 240 | return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; |
| 241 | }; |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 242 | Obj->WriteSectionHeaders = false; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Jake Ehrlich | 1bfefc1 | 2017-11-13 22:13:08 +0000 | [diff] [blame] | 245 | if (StripDebug) { |
| 246 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 247 | return RemovePred(Sec) || Sec.Name.startswith(".debug"); |
| 248 | }; |
| 249 | } |
| 250 | |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame] | 251 | if (StripNonAlloc) |
| 252 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 253 | if (RemovePred(Sec)) |
| 254 | return true; |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 255 | if (&Sec == Obj->getSectionHeaderStrTab()) |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame] | 256 | return false; |
| 257 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 258 | }; |
| 259 | |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 260 | if (StripAll) |
| 261 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 262 | if (RemovePred(Sec)) |
| 263 | return true; |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 264 | if (&Sec == Obj->getSectionHeaderStrTab()) |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 265 | return false; |
| 266 | if (Sec.Name.startswith(".gnu.warning")) |
| 267 | return false; |
| 268 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 269 | }; |
| 270 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 271 | // Explicit copies: |
| 272 | |
| 273 | if (!OnlyKeep.empty()) { |
| 274 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 275 | // Explicitly keep these sections regardless of previous removes. |
| 276 | if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) != |
| 277 | std::end(OnlyKeep)) |
| 278 | return false; |
| 279 | |
| 280 | // Allow all implicit removes. |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 281 | if (RemovePred(Sec)) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 282 | return true; |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 283 | } |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 284 | |
| 285 | // Keep special sections. |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 286 | if (Obj->getSectionHeaderStrTab() == &Sec) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 287 | return false; |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 288 | } |
| 289 | if (Obj->getSymTab() == &Sec || Obj->getSymTab()->getStrTab() == &Sec) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 290 | return false; |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 291 | } |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 292 | // Remove everything else. |
| 293 | return true; |
| 294 | }; |
| 295 | } |
| 296 | |
| 297 | if (!Keep.empty()) { |
| 298 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 299 | // Explicitly keep these sections regardless of previous removes. |
| 300 | if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) != |
| 301 | std::end(Keep)) |
| 302 | return false; |
| 303 | // Otherwise defer to RemovePred. |
| 304 | return RemovePred(Sec); |
| 305 | }; |
| 306 | } |
| 307 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 308 | Obj->removeSections(RemovePred); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 309 | |
| 310 | if (!AddSection.empty()) { |
| 311 | for (const auto &Flag : AddSection) { |
| 312 | auto SecPair = StringRef(Flag).split("="); |
| 313 | auto SecName = SecPair.first; |
| 314 | auto File = SecPair.second; |
| 315 | auto BufOrErr = MemoryBuffer::getFile(File); |
| 316 | if (!BufOrErr) |
| 317 | reportError(File, BufOrErr.getError()); |
| 318 | auto Buf = std::move(*BufOrErr); |
| 319 | auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart()); |
| 320 | auto BufSize = Buf->getBufferSize(); |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 321 | Obj->addSection(SecName, ArrayRef<uint8_t>(BufPtr, BufSize)); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 325 | if (!AddGnuDebugLink.empty()) { |
| 326 | Obj->addGnuDebugLink(AddGnuDebugLink); |
| 327 | } |
| 328 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 329 | Obj->finalize(); |
| 330 | WriteObjectFile(*Obj, OutputFilename.getValue()); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | int main(int argc, char **argv) { |
| 334 | // Print a stack trace if we signal out. |
| 335 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 336 | PrettyStackTraceProgram X(argc, argv); |
| 337 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 338 | cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n"); |
| 339 | ToolName = argv[0]; |
| 340 | if (InputFilename.empty()) { |
| 341 | cl::PrintHelpMessage(); |
| 342 | return 2; |
| 343 | } |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 344 | Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename); |
| 345 | if (!BinaryOrErr) |
| 346 | reportError(InputFilename, BinaryOrErr.takeError()); |
| 347 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
| 348 | if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) { |
| 349 | CopyBinary(*o); |
| 350 | return 0; |
| 351 | } |
| 352 | if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(&Binary)) { |
| 353 | CopyBinary(*o); |
| 354 | return 0; |
| 355 | } |
| 356 | if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(&Binary)) { |
| 357 | CopyBinary(*o); |
| 358 | return 0; |
| 359 | } |
| 360 | if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(&Binary)) { |
| 361 | CopyBinary(*o); |
| 362 | return 0; |
| 363 | } |
| 364 | reportError(InputFilename, object_error::invalid_file_type); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 365 | } |