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)); |
| 84 | static cl::opt<bool> StripSections("strip-sections", |
| 85 | cl::desc("Remove all section headers")); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 86 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 87 | using SectionPred = std::function<bool(const SectionBase &Sec)>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 88 | |
| 89 | void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) { |
| 90 | std::unique_ptr<FileOutputBuffer> Buffer; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 91 | std::unique_ptr<Object<ELF64LE>> Obj; |
| 92 | if (!OutputFormat.empty() && OutputFormat != "binary") |
| 93 | error("invalid output format '" + OutputFormat + "'"); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 94 | if (!OutputFormat.empty() && OutputFormat == "binary") |
| 95 | Obj = llvm::make_unique<BinaryObject<ELF64LE>>(ObjFile); |
| 96 | else |
| 97 | Obj = llvm::make_unique<ELFObject<ELF64LE>>(ObjFile); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 98 | |
| 99 | SectionPred RemovePred = [](const SectionBase &) { return false; }; |
| 100 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 101 | if (!ToRemove.empty()) { |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 102 | RemovePred = [&](const SectionBase &Sec) { |
Jake Ehrlich | fcc0562 | 2017-10-10 23:02:43 +0000 | [diff] [blame] | 103 | return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) != |
| 104 | std::end(ToRemove); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 105 | }; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 106 | } |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 107 | |
| 108 | if (StripSections) { |
| 109 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 110 | return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; |
| 111 | }; |
| 112 | Obj->WriteSectionHeaders = false; |
| 113 | } |
| 114 | |
| 115 | Obj->removeSections(RemovePred); |
| 116 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 117 | Obj->finalize(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 118 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 119 | FileOutputBuffer::create(OutputFilename, Obj->totalSize(), |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 120 | FileOutputBuffer::F_executable); |
| 121 | if (BufferOrErr.getError()) |
| 122 | error("failed to open " + OutputFilename); |
| 123 | else |
| 124 | Buffer = std::move(*BufferOrErr); |
| 125 | std::error_code EC; |
| 126 | if (EC) |
| 127 | report_fatal_error(EC.message()); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 128 | Obj->write(*Buffer); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 129 | if (auto EC = Buffer->commit()) |
| 130 | reportError(OutputFilename, EC); |
| 131 | } |
| 132 | |
| 133 | int main(int argc, char **argv) { |
| 134 | // Print a stack trace if we signal out. |
| 135 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 136 | PrettyStackTraceProgram X(argc, argv); |
| 137 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 138 | cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n"); |
| 139 | ToolName = argv[0]; |
| 140 | if (InputFilename.empty()) { |
| 141 | cl::PrintHelpMessage(); |
| 142 | return 2; |
| 143 | } |
| 144 | Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename); |
| 145 | if (!BinaryOrErr) |
| 146 | reportError(InputFilename, BinaryOrErr.takeError()); |
| 147 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
| 148 | if (ELFObjectFile<ELF64LE> *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) { |
| 149 | CopyBinary(*o); |
| 150 | return 0; |
| 151 | } |
| 152 | reportError(InputFilename, object_error::invalid_file_type); |
| 153 | } |