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>"), |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 76 | cl::init("-")); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 77 | static cl::opt<std::string> |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 78 | OutputFormat("O", cl::desc("set output format to one of the following:" |
| 79 | "\n\tbinary")); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 80 | static cl::list<std::string> ToRemove("remove-section", |
| 81 | cl::desc("Remove a specific section")); |
| 82 | static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"), |
| 83 | cl::aliasopt(ToRemove)); |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 84 | static cl::opt<bool> StripAll("strip-all", |
| 85 | cl::desc("Removes symbol, relocation, and debug information")); |
Jake Ehrlich | 1bfefc1 | 2017-11-13 22:13:08 +0000 | [diff] [blame] | 86 | static cl::opt<bool> StripDebug("strip-debug", |
| 87 | cl::desc("Removes all debug information")); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 88 | static cl::opt<bool> StripSections("strip-sections", |
| 89 | cl::desc("Remove all section headers")); |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame^] | 90 | static cl::opt<bool> StripNonAlloc("strip-non-alloc", |
| 91 | cl::desc("Remove all non-allocated sections")); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 92 | static cl::opt<bool> |
| 93 | StripDWO("strip-dwo", cl::desc("remove all DWARF .dwo sections from file")); |
| 94 | static cl::opt<bool> ExtractDWO( |
| 95 | "extract-dwo", |
| 96 | cl::desc("remove all sections that are not DWARF .dwo sections from file")); |
| 97 | static cl::opt<std::string> |
| 98 | SplitDWO("split-dwo", |
| 99 | cl::desc("equivalent to extract-dwo on the input file to " |
| 100 | "<dwo-file>, then strip-dwo on the input file"), |
| 101 | cl::value_desc("dwo-file")); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 102 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 103 | using SectionPred = std::function<bool(const SectionBase &Sec)>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 104 | |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 105 | bool IsDWOSection(const SectionBase &Sec) { |
| 106 | return Sec.Name.endswith(".dwo"); |
| 107 | } |
| 108 | |
| 109 | template <class ELFT> |
| 110 | bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) { |
| 111 | // We can't remove the section header string table. |
| 112 | if (&Sec == Obj.getSectionHeaderStrTab()) |
| 113 | return false; |
| 114 | // Short of keeping the string table we want to keep everything that is a DWO |
| 115 | // section and remove everything else. |
| 116 | return !IsDWOSection(Sec); |
| 117 | } |
| 118 | |
| 119 | template <class ELFT> |
| 120 | void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 121 | std::unique_ptr<FileOutputBuffer> Buffer; |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 122 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 123 | FileOutputBuffer::create(File, Obj.totalSize(), |
| 124 | FileOutputBuffer::F_executable); |
Rafael Espindola | 85593c2 | 2017-11-08 21:15:21 +0000 | [diff] [blame] | 125 | handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 126 | error("failed to open " + OutputFilename); |
Rafael Espindola | 85593c2 | 2017-11-08 21:15:21 +0000 | [diff] [blame] | 127 | }); |
| 128 | Buffer = std::move(*BufferOrErr); |
| 129 | |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 130 | Obj.write(*Buffer); |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame] | 131 | if (auto E = Buffer->commit()) |
| 132 | reportError(File, errorToErrorCode(std::move(E))); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | template <class ELFT> |
| 136 | void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) { |
| 137 | // Construct a second output file for the DWO sections. |
| 138 | ELFObject<ELFT> DWOFile(ObjFile); |
| 139 | |
| 140 | DWOFile.removeSections([&](const SectionBase &Sec) { |
| 141 | return OnlyKeepDWOPred<ELFT>(DWOFile, Sec); |
| 142 | }); |
| 143 | DWOFile.finalize(); |
| 144 | WriteObjectFile(DWOFile, File); |
| 145 | } |
| 146 | |
Jake Ehrlich | 99e2c41 | 2017-11-14 18:41:47 +0000 | [diff] [blame] | 147 | template <class ELFT> |
| 148 | void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { |
| 149 | std::unique_ptr<Object<ELFT>> Obj; |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 150 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 151 | if (!OutputFormat.empty() && OutputFormat != "binary") |
| 152 | error("invalid output format '" + OutputFormat + "'"); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 153 | if (!OutputFormat.empty() && OutputFormat == "binary") |
Jake Ehrlich | 99e2c41 | 2017-11-14 18:41:47 +0000 | [diff] [blame] | 154 | Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 155 | else |
Jake Ehrlich | 99e2c41 | 2017-11-14 18:41:47 +0000 | [diff] [blame] | 156 | Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 157 | |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 158 | if (!SplitDWO.empty()) |
Jake Ehrlich | 99e2c41 | 2017-11-14 18:41:47 +0000 | [diff] [blame] | 159 | SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue()); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 160 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 161 | SectionPred RemovePred = [](const SectionBase &) { return false; }; |
| 162 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 163 | if (!ToRemove.empty()) { |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 164 | RemovePred = [&](const SectionBase &Sec) { |
Jake Ehrlich | fcc0562 | 2017-10-10 23:02:43 +0000 | [diff] [blame] | 165 | return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) != |
| 166 | std::end(ToRemove); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 167 | }; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 168 | } |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 169 | |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 170 | if (StripDWO || !SplitDWO.empty()) |
David Blaikie | 998ff81 | 2017-11-03 20:57:09 +0000 | [diff] [blame] | 171 | RemovePred = [RemovePred](const SectionBase &Sec) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 172 | return IsDWOSection(Sec) || RemovePred(Sec); |
| 173 | }; |
| 174 | |
| 175 | if (ExtractDWO) |
| 176 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 177 | return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec); |
| 178 | }; |
| 179 | |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 180 | if (StripAll) |
| 181 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 182 | if (RemovePred(Sec)) |
| 183 | return true; |
| 184 | if ((Sec.Flags & SHF_ALLOC) != 0) |
| 185 | return false; |
| 186 | if (&Sec == Obj->getSectionHeaderStrTab()) |
| 187 | return false; |
| 188 | switch(Sec.Type) { |
| 189 | case SHT_SYMTAB: |
| 190 | case SHT_REL: |
| 191 | case SHT_RELA: |
| 192 | case SHT_STRTAB: |
| 193 | return true; |
| 194 | } |
| 195 | return Sec.Name.startswith(".debug"); |
| 196 | }; |
| 197 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 198 | if (StripSections) { |
| 199 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 200 | return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; |
| 201 | }; |
| 202 | Obj->WriteSectionHeaders = false; |
| 203 | } |
| 204 | |
Jake Ehrlich | 1bfefc1 | 2017-11-13 22:13:08 +0000 | [diff] [blame] | 205 | if (StripDebug) { |
| 206 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 207 | return RemovePred(Sec) || Sec.Name.startswith(".debug"); |
| 208 | }; |
| 209 | } |
| 210 | |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame^] | 211 | if (StripNonAlloc) |
| 212 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 213 | if (RemovePred(Sec)) |
| 214 | return true; |
| 215 | if (&Sec == Obj->getSectionHeaderStrTab()) |
| 216 | return false; |
| 217 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 218 | }; |
| 219 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 220 | Obj->removeSections(RemovePred); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 221 | Obj->finalize(); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 222 | WriteObjectFile(*Obj, OutputFilename.getValue()); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int main(int argc, char **argv) { |
| 226 | // Print a stack trace if we signal out. |
| 227 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 228 | PrettyStackTraceProgram X(argc, argv); |
| 229 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 230 | cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n"); |
| 231 | ToolName = argv[0]; |
| 232 | if (InputFilename.empty()) { |
| 233 | cl::PrintHelpMessage(); |
| 234 | return 2; |
| 235 | } |
| 236 | Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename); |
| 237 | if (!BinaryOrErr) |
| 238 | reportError(InputFilename, BinaryOrErr.takeError()); |
| 239 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
Jake Ehrlich | 99e2c41 | 2017-11-14 18:41:47 +0000 | [diff] [blame] | 240 | if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) { |
| 241 | CopyBinary(*o); |
| 242 | return 0; |
| 243 | } |
| 244 | if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(&Binary)) { |
| 245 | CopyBinary(*o); |
| 246 | return 0; |
| 247 | } |
| 248 | if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(&Binary)) { |
| 249 | CopyBinary(*o); |
| 250 | return 0; |
| 251 | } |
| 252 | if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(&Binary)) { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 253 | CopyBinary(*o); |
| 254 | return 0; |
| 255 | } |
| 256 | reportError(InputFilename, object_error::invalid_file_type); |
| 257 | } |