blob: 812d27b403ffda88ff9c6332b99144147a45dbba [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));
Jake Ehrlichfabddf12017-11-13 22:02:07 +000084static cl::opt<bool> StripAll("strip-all",
85 cl::desc("Removes symbol, relocation, and debug information"));
Jake Ehrlich1bfefc12017-11-13 22:13:08 +000086static cl::opt<bool> StripDebug("strip-debug",
87 cl::desc("Removes all debug information"));
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000088static cl::opt<bool> StripSections("strip-sections",
89 cl::desc("Remove all section headers"));
Jake Ehrlichd56725a2017-11-14 18:50:24 +000090static cl::opt<bool> StripNonAlloc("strip-non-alloc",
91 cl::desc("Remove all non-allocated sections"));
Jake Ehrlich5de70d92017-11-03 18:58:41 +000092static cl::opt<bool>
93 StripDWO("strip-dwo", cl::desc("remove all DWARF .dwo sections from file"));
94static cl::opt<bool> ExtractDWO(
95 "extract-dwo",
96 cl::desc("remove all sections that are not DWARF .dwo sections from file"));
97static 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 Ehrlichf03384d2017-10-11 18:09:18 +0000102
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000103using SectionPred = std::function<bool(const SectionBase &Sec)>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000104
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000105bool IsDWOSection(const SectionBase &Sec) {
106 return Sec.Name.endswith(".dwo");
107}
108
109template <class ELFT>
110bool 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
119template <class ELFT>
120void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000121 std::unique_ptr<FileOutputBuffer> Buffer;
Rafael Espindolae0df3572017-11-08 01:05:44 +0000122 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000123 FileOutputBuffer::create(File, Obj.totalSize(),
124 FileOutputBuffer::F_executable);
Rafael Espindola85593c22017-11-08 21:15:21 +0000125 handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000126 error("failed to open " + OutputFilename);
Rafael Espindola85593c22017-11-08 21:15:21 +0000127 });
128 Buffer = std::move(*BufferOrErr);
129
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000130 Obj.write(*Buffer);
Rafael Espindola0d7a38a2017-11-08 01:50:29 +0000131 if (auto E = Buffer->commit())
132 reportError(File, errorToErrorCode(std::move(E)));
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000133}
134
135template <class ELFT>
136void 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 Ehrlich99e2c412017-11-14 18:41:47 +0000147template <class ELFT>
148void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) {
149 std::unique_ptr<Object<ELFT>> Obj;
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000150
Petr Hosekc4df10e2017-08-04 21:09:26 +0000151 if (!OutputFormat.empty() && OutputFormat != "binary")
152 error("invalid output format '" + OutputFormat + "'");
Petr Hosekc4df10e2017-08-04 21:09:26 +0000153 if (!OutputFormat.empty() && OutputFormat == "binary")
Jake Ehrlich99e2c412017-11-14 18:41:47 +0000154 Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000155 else
Jake Ehrlich99e2c412017-11-14 18:41:47 +0000156 Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile);
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000157
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000158 if (!SplitDWO.empty())
Jake Ehrlich99e2c412017-11-14 18:41:47 +0000159 SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue());
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000160
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000161 SectionPred RemovePred = [](const SectionBase &) { return false; };
162
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000163 if (!ToRemove.empty()) {
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000164 RemovePred = [&](const SectionBase &Sec) {
Jake Ehrlichfcc05622017-10-10 23:02:43 +0000165 return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
166 std::end(ToRemove);
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000167 };
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000168 }
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000169
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000170 if (StripDWO || !SplitDWO.empty())
David Blaikie998ff812017-11-03 20:57:09 +0000171 RemovePred = [RemovePred](const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000172 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 Ehrlichfabddf12017-11-13 22:02:07 +0000180 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 Ehrlichf03384d2017-10-11 18:09:18 +0000198 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 Ehrlich1bfefc12017-11-13 22:13:08 +0000205 if (StripDebug) {
206 RemovePred = [RemovePred](const SectionBase &Sec) {
207 return RemovePred(Sec) || Sec.Name.startswith(".debug");
208 };
209 }
210
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000211 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 Ehrlichf03384d2017-10-11 18:09:18 +0000220 Obj->removeSections(RemovePred);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000221 Obj->finalize();
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000222 WriteObjectFile(*Obj, OutputFilename.getValue());
Petr Hosek05a04cb2017-08-01 00:33:58 +0000223}
224
225int 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 Ehrlich99e2c412017-11-14 18:41:47 +0000240 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 Hosek05a04cb2017-08-01 00:33:58 +0000253 CopyBinary(*o);
254 return 0;
255 }
256 reportError(InputFilename, object_error::invalid_file_type);
257}