1 //===- llvm-objcopy.cpp ---------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm-objcopy.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"
38 #include <system_error>
42 using namespace object;
45 // The name this program was invoked as.
46 static StringRef ToolName;
50 LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
51 errs() << ToolName << ": " << Message << ".\n";
56 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
58 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
62 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
65 raw_string_ostream OS(Buf);
66 logAllUnhandledErrors(std::move(E), OS, "");
68 errs() << ToolName << ": '" << File << "': " << Buf;
72 } // end namespace llvm
74 static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
75 static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"),
77 static cl::opt<std::string>
78 OutputFormat("O", cl::desc("Set output format to one of the following:"
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(
88 "Removes non-allocated sections other than .gnu.warning* sections"));
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"));
104 StripNonAlloc("strip-non-alloc",
105 cl::desc("Remove all non-allocated sections"));
107 StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file"));
108 static cl::opt<bool> ExtractDWO(
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 static cl::list<std::string> AddSection(
118 cl::desc("Make a section named <section> with the contents of <file>."),
119 cl::value_desc("section=file"));
121 using SectionPred = std::function<bool(const SectionBase &Sec)>;
123 bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
125 template <class ELFT>
126 bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) {
127 // We can't remove the section header string table.
128 if (&Sec == Obj.getSectionHeaderStrTab())
130 // Short of keeping the string table we want to keep everything that is a DWO
131 // section and remove everything else.
132 return !IsDWOSection(Sec);
135 template <class ELFT>
136 void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) {
137 std::unique_ptr<FileOutputBuffer> Buffer;
138 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
139 FileOutputBuffer::create(File, Obj.totalSize(),
140 FileOutputBuffer::F_executable);
141 handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) {
142 error("failed to open " + OutputFilename);
144 Buffer = std::move(*BufferOrErr);
147 if (auto E = Buffer->commit())
148 reportError(File, errorToErrorCode(std::move(E)));
151 template <class ELFT>
152 void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) {
153 // Construct a second output file for the DWO sections.
154 ELFObject<ELFT> DWOFile(ObjFile);
156 DWOFile.removeSections([&](const SectionBase &Sec) {
157 return OnlyKeepDWOPred<ELFT>(DWOFile, Sec);
160 WriteObjectFile(DWOFile, File);
163 // This function handles the high level operations of GNU objcopy including
164 // handling command line options. It's important to outline certain properties
165 // we expect to hold of the command line operations. Any operation that "keeps"
166 // should keep regardless of a remove. Additionally any removal should respect
167 // any previous removals. Lastly whether or not something is removed shouldn't
168 // depend a) on the order the options occur in or b) on some opaque priority
169 // system. The only priority is that keeps/copies overrule removes.
170 template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) {
171 std::unique_ptr<Object<ELFT>> Obj;
173 if (!OutputFormat.empty() && OutputFormat != "binary")
174 error("invalid output format '" + OutputFormat + "'");
175 if (!OutputFormat.empty() && OutputFormat == "binary")
176 Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile);
178 Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile);
180 if (!SplitDWO.empty())
181 SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue());
183 SectionPred RemovePred = [](const SectionBase &) { return false; };
187 if (!ToRemove.empty()) {
188 RemovePred = [&](const SectionBase &Sec) {
189 return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
194 if (StripDWO || !SplitDWO.empty())
195 RemovePred = [RemovePred](const SectionBase &Sec) {
196 return IsDWOSection(Sec) || RemovePred(Sec);
200 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
201 return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec);
205 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
208 if ((Sec.Flags & SHF_ALLOC) != 0)
210 if (&Sec == Obj->getSectionHeaderStrTab())
219 return Sec.Name.startswith(".debug");
223 RemovePred = [RemovePred](const SectionBase &Sec) {
224 return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
226 Obj->WriteSectionHeaders = false;
230 RemovePred = [RemovePred](const SectionBase &Sec) {
231 return RemovePred(Sec) || Sec.Name.startswith(".debug");
236 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
239 if (&Sec == Obj->getSectionHeaderStrTab())
241 return (Sec.Flags & SHF_ALLOC) == 0;
245 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
248 if (&Sec == Obj->getSectionHeaderStrTab())
250 if (Sec.Name.startswith(".gnu.warning"))
252 return (Sec.Flags & SHF_ALLOC) == 0;
257 if (!OnlyKeep.empty()) {
258 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
259 // Explicitly keep these sections regardless of previous removes.
260 if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) !=
264 // Allow all implicit removes.
265 if (RemovePred(Sec)) {
269 // Keep special sections.
270 if (Obj->getSectionHeaderStrTab() == &Sec) {
273 if (Obj->getSymTab() == &Sec || Obj->getSymTab()->getStrTab() == &Sec) {
276 // Remove everything else.
282 RemovePred = [RemovePred](const SectionBase &Sec) {
283 // Explicitly keep these sections regardless of previous removes.
284 if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) !=
287 // Otherwise defer to RemovePred.
288 return RemovePred(Sec);
292 Obj->removeSections(RemovePred);
294 if (!AddSection.empty()) {
295 for (const auto &Flag : AddSection) {
296 auto SecPair = StringRef(Flag).split("=");
297 auto SecName = SecPair.first;
298 auto File = SecPair.second;
299 auto BufOrErr = MemoryBuffer::getFile(File);
301 reportError(File, BufOrErr.getError());
302 auto Buf = std::move(*BufOrErr);
303 auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
304 auto BufSize = Buf->getBufferSize();
305 Obj->addSection(SecName, ArrayRef<uint8_t>(BufPtr, BufSize));
310 WriteObjectFile(*Obj, OutputFilename.getValue());
313 int main(int argc, char **argv) {
314 // Print a stack trace if we signal out.
315 sys::PrintStackTraceOnErrorSignal(argv[0]);
316 PrettyStackTraceProgram X(argc, argv);
317 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
318 cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n");
320 if (InputFilename.empty()) {
321 cl::PrintHelpMessage();
324 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename);
326 reportError(InputFilename, BinaryOrErr.takeError());
327 Binary &Binary = *BinaryOrErr.get().getBinary();
328 if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) {
332 if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(&Binary)) {
336 if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(&Binary)) {
340 if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(&Binary)) {
344 reportError(InputFilename, object_error::invalid_file_type);