blob: f3e9c7750a64add391b6e348cd9d63a13ee799cc [file] [log] [blame]
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001//===- llvm-objcopy.cpp ---------------------------------------------------===//
Petr Hosek05a04cb2017-08-01 00:33:58 +00002//
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 Zelenko0ad18f82017-11-01 21:16:06 +00009
Petr Hosek05a04cb2017-08-01 00:33:58 +000010#include "llvm-objcopy.h"
11#include "Object.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000012#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 Hosek05a04cb2017-08-01 00:33:58 +000021#include "llvm/Support/CommandLine.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000022#include "llvm/Support/Compiler.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/ErrorOr.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000026#include "llvm/Support/FileOutputBuffer.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000027#include "llvm/Support/ManagedStatic.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000028#include "llvm/Support/PrettyStackTrace.h"
29#include "llvm/Support/Signals.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000030#include "llvm/Support/raw_ostream.h"
31#include <algorithm>
32#include <cassert>
33#include <cstdlib>
34#include <functional>
35#include <iterator>
Petr Hosek05a04cb2017-08-01 00:33:58 +000036#include <memory>
37#include <string>
38#include <system_error>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000039#include <utility>
Petr Hosek05a04cb2017-08-01 00:33:58 +000040
41using namespace llvm;
42using namespace object;
43using namespace ELF;
44
45// The name this program was invoked as.
46static StringRef ToolName;
47
48namespace llvm {
49
Zachary Turner41a9ee92017-10-11 23:54:34 +000050LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
Petr Hosek05a04cb2017-08-01 00:33:58 +000051 errs() << ToolName << ": " << Message << ".\n";
52 errs().flush();
53 exit(1);
54}
55
56LLVM_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 Zelenko0ad18f82017-11-01 21:16:06 +000062LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
Petr Hosek05a04cb2017-08-01 00:33:58 +000063 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 Hosek05a04cb2017-08-01 00:33:58 +000071
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000072} // end namespace llvm
73
74static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
75static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"),
Petr Hosek05a04cb2017-08-01 00:33:58 +000076 cl::init("-"));
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000077static cl::opt<std::string>
Petr Hosekc4df10e2017-08-04 21:09:26 +000078 OutputFormat("O", cl::desc("set output format to one of the following:"
79 "\n\tbinary"));
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000080static cl::list<std::string> ToRemove("remove-section",
81 cl::desc("Remove a specific section"));
82static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
83 cl::aliasopt(ToRemove));
84static cl::opt<bool> StripSections("strip-sections",
85 cl::desc("Remove all section headers"));
Jake Ehrlichf03384d2017-10-11 18:09:18 +000086
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000087using SectionPred = std::function<bool(const SectionBase &Sec)>;
Petr Hosek05a04cb2017-08-01 00:33:58 +000088
89void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) {
90 std::unique_ptr<FileOutputBuffer> Buffer;
Petr Hosekc4df10e2017-08-04 21:09:26 +000091 std::unique_ptr<Object<ELF64LE>> Obj;
92 if (!OutputFormat.empty() && OutputFormat != "binary")
93 error("invalid output format '" + OutputFormat + "'");
Petr Hosekc4df10e2017-08-04 21:09:26 +000094 if (!OutputFormat.empty() && OutputFormat == "binary")
95 Obj = llvm::make_unique<BinaryObject<ELF64LE>>(ObjFile);
96 else
97 Obj = llvm::make_unique<ELFObject<ELF64LE>>(ObjFile);
Jake Ehrlichf03384d2017-10-11 18:09:18 +000098
99 SectionPred RemovePred = [](const SectionBase &) { return false; };
100
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000101 if (!ToRemove.empty()) {
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000102 RemovePred = [&](const SectionBase &Sec) {
Jake Ehrlichfcc05622017-10-10 23:02:43 +0000103 return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
104 std::end(ToRemove);
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000105 };
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000106 }
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000107
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 Hosekc4df10e2017-08-04 21:09:26 +0000117 Obj->finalize();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000118 ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
Petr Hosekc4df10e2017-08-04 21:09:26 +0000119 FileOutputBuffer::create(OutputFilename, Obj->totalSize(),
Petr Hosek05a04cb2017-08-01 00:33:58 +0000120 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 Hosekc4df10e2017-08-04 21:09:26 +0000128 Obj->write(*Buffer);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000129 if (auto EC = Buffer->commit())
130 reportError(OutputFilename, EC);
131}
132
133int 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}