]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
Merge clang trunk r321017 to contrib/llvm/tools/clang.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-objcopy / llvm-objcopy.cpp
1 //===- llvm-objcopy.cpp ---------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9
10 #include "llvm-objcopy.h"
11 #include "Object.h"
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"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Error.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/ErrorOr.h"
26 #include "llvm/Support/FileOutputBuffer.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/PrettyStackTrace.h"
29 #include "llvm/Support/Signals.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm>
32 #include <cassert>
33 #include <cstdlib>
34 #include <functional>
35 #include <iterator>
36 #include <memory>
37 #include <string>
38 #include <system_error>
39 #include <utility>
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
50 LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
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
62 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
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 }
71
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>"),
76                                            cl::init("-"));
77 static cl::opt<std::string>
78     OutputFormat("O", cl::desc("Set output format to one of the following:"
79                                "\n\tbinary"));
80 static cl::list<std::string> ToRemove("remove-section",
81                                       cl::desc("Remove <section>"),
82                                       cl::value_desc("section"));
83 static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
84                            cl::aliasopt(ToRemove));
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"));
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));
99 static cl::opt<bool> StripDebug("strip-debug",
100                                 cl::desc("Removes all debug information"));
101 static cl::opt<bool> StripSections("strip-sections",
102                                    cl::desc("Remove all section headers"));
103 static cl::opt<bool>
104     StripNonAlloc("strip-non-alloc",
105                   cl::desc("Remove all non-allocated sections"));
106 static cl::opt<bool>
107     StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file"));
108 static cl::opt<bool> ExtractDWO(
109     "extract-dwo",
110     cl::desc("Remove all sections that are not DWARF .dwo sections from file"));
111 static cl::opt<std::string>
112     SplitDWO("split-dwo",
113              cl::desc("Equivalent to extract-dwo on the input file to "
114                       "<dwo-file>, then strip-dwo on the input file"),
115              cl::value_desc("dwo-file"));
116
117 using SectionPred = std::function<bool(const SectionBase &Sec)>;
118
119 bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
120
121 template <class ELFT>
122 bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) {
123   // We can't remove the section header string table.
124   if (&Sec == Obj.getSectionHeaderStrTab())
125     return false;
126   // Short of keeping the string table we want to keep everything that is a DWO
127   // section and remove everything else.
128   return !IsDWOSection(Sec);
129 }
130
131 template <class ELFT>
132 void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) {
133   std::unique_ptr<FileOutputBuffer> Buffer;
134   Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
135       FileOutputBuffer::create(File, Obj.totalSize(),
136                                FileOutputBuffer::F_executable);
137   handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) {
138     error("failed to open " + OutputFilename);
139   });
140   Buffer = std::move(*BufferOrErr);
141
142   Obj.write(*Buffer);
143   if (auto E = Buffer->commit())
144     reportError(File, errorToErrorCode(std::move(E)));
145 }
146
147 template <class ELFT>
148 void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) {
149   // Construct a second output file for the DWO sections.
150   ELFObject<ELFT> DWOFile(ObjFile);
151
152   DWOFile.removeSections([&](const SectionBase &Sec) {
153     return OnlyKeepDWOPred<ELFT>(DWOFile, Sec);
154   });
155   DWOFile.finalize();
156   WriteObjectFile(DWOFile, File);
157 }
158
159 // This function handles the high level operations of GNU objcopy including
160 // handling command line options. It's important to outline certain properties
161 // we expect to hold of the command line operations. Any operation that "keeps"
162 // should keep regardless of a remove. Additionally any removal should respect
163 // any previous removals. Lastly whether or not something is removed shouldn't
164 // depend a) on the order the options occur in or b) on some opaque priority
165 // system. The only priority is that keeps/copies overrule removes.
166 template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) {
167   std::unique_ptr<Object<ELFT>> Obj;
168
169   if (!OutputFormat.empty() && OutputFormat != "binary")
170     error("invalid output format '" + OutputFormat + "'");
171   if (!OutputFormat.empty() && OutputFormat == "binary")
172     Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile);
173   else
174     Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile);
175
176   if (!SplitDWO.empty())
177     SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue()); 
178
179   SectionPred RemovePred = [](const SectionBase &) { return false; };
180
181   // Removes:
182
183   if (!ToRemove.empty()) {
184     RemovePred = [&](const SectionBase &Sec) {
185       return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
186              std::end(ToRemove);
187     };
188   }
189
190   if (StripDWO || !SplitDWO.empty())
191     RemovePred = [RemovePred](const SectionBase &Sec) {
192       return IsDWOSection(Sec) || RemovePred(Sec);
193     };
194
195   if (ExtractDWO)
196     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
197       return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec);
198     };
199
200   if (StripAllGNU)
201     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
202       if (RemovePred(Sec))
203         return true;
204       if ((Sec.Flags & SHF_ALLOC) != 0)
205         return false;
206       if (&Sec == Obj->getSectionHeaderStrTab())
207         return false;
208       switch (Sec.Type) {
209       case SHT_SYMTAB:
210       case SHT_REL:
211       case SHT_RELA:
212       case SHT_STRTAB:
213         return true;
214       }
215       return Sec.Name.startswith(".debug");
216     };
217
218   if (StripSections) {
219     RemovePred = [RemovePred](const SectionBase &Sec) {
220       return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
221     };
222     Obj->WriteSectionHeaders = false;
223   }
224
225   if (StripDebug) {
226     RemovePred = [RemovePred](const SectionBase &Sec) {
227       return RemovePred(Sec) || Sec.Name.startswith(".debug");
228     };
229   }
230
231   if (StripNonAlloc)
232     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
233       if (RemovePred(Sec))
234         return true;
235       if (&Sec == Obj->getSectionHeaderStrTab())
236         return false;
237       return (Sec.Flags & SHF_ALLOC) == 0;
238     };
239
240   if (StripAll)
241     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
242       if (RemovePred(Sec))
243         return true;
244       if (&Sec == Obj->getSectionHeaderStrTab())
245         return false;
246       if (Sec.Name.startswith(".gnu.warning"))
247         return false;
248       return (Sec.Flags & SHF_ALLOC) == 0;
249     };
250
251   // Explicit copies:
252
253   if (!OnlyKeep.empty()) {
254     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
255       // Explicitly keep these sections regardless of previous removes.
256       if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) !=
257           std::end(OnlyKeep))
258         return false;
259
260       // Allow all implicit removes.
261       if (RemovePred(Sec)) {
262         return true;
263       }
264
265       // Keep special sections.
266       if (Obj->getSectionHeaderStrTab() == &Sec) {
267         return false;
268       }
269       if (Obj->getSymTab() == &Sec || Obj->getSymTab()->getStrTab() == &Sec) {
270         return false;
271       }
272       // Remove everything else.
273       return true;
274     };
275   }
276
277   if (!Keep.empty()) {
278     RemovePred = [RemovePred](const SectionBase &Sec) {
279       // Explicitly keep these sections regardless of previous removes.
280       if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) !=
281           std::end(Keep))
282         return false;
283       // Otherwise defer to RemovePred.
284       return RemovePred(Sec);
285     };
286   }
287
288   Obj->removeSections(RemovePred);
289   Obj->finalize();
290   WriteObjectFile(*Obj, OutputFilename.getValue());
291 }
292
293 int main(int argc, char **argv) {
294   // Print a stack trace if we signal out.
295   sys::PrintStackTraceOnErrorSignal(argv[0]);
296   PrettyStackTraceProgram X(argc, argv);
297   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
298   cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n");
299   ToolName = argv[0];
300   if (InputFilename.empty()) {
301     cl::PrintHelpMessage();
302     return 2;
303   }
304   Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename);
305   if (!BinaryOrErr)
306     reportError(InputFilename, BinaryOrErr.takeError());
307   Binary &Binary = *BinaryOrErr.get().getBinary();
308   if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) {
309     CopyBinary(*o);
310     return 0;
311   }
312   if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(&Binary)) {
313     CopyBinary(*o);
314     return 0;
315   }
316   if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(&Binary)) {
317     CopyBinary(*o);
318     return 0;
319   }
320   if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(&Binary)) {
321     CopyBinary(*o);
322     return 0;
323   }
324   reportError(InputFilename, object_error::invalid_file_type);
325 }