xref: /llvm-project/llvm/tools/llvm-objcopy/llvm-objcopy.cpp (revision cca69985045e00a1fe5612e11587cf8065b6e8be)
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/Option/Arg.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Option/Option.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/ErrorOr.h"
29 #include "llvm/Support/FileOutputBuffer.h"
30 #include "llvm/Support/InitLLVM.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include <algorithm>
34 #include <cassert>
35 #include <cstdlib>
36 #include <functional>
37 #include <iterator>
38 #include <memory>
39 #include <string>
40 #include <system_error>
41 #include <utility>
42 
43 using namespace llvm;
44 using namespace object;
45 using namespace ELF;
46 
47 namespace {
48 
49 enum ObjcopyID {
50   OBJCOPY_INVALID = 0, // This is not an option ID.
51 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
52                HELPTEXT, METAVAR, VALUES)                                      \
53   OBJCOPY_##ID,
54 #include "ObjcopyOpts.inc"
55 #undef OPTION
56 };
57 
58 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
59 #include "ObjcopyOpts.inc"
60 #undef PREFIX
61 
62 static const opt::OptTable::Info ObjcopyInfoTable[] = {
63 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
64                HELPTEXT, METAVAR, VALUES)                                      \
65   {PREFIX,          NAME,         HELPTEXT,                                    \
66    METAVAR,         OBJCOPY_##ID, opt::Option::KIND##Class,                    \
67    PARAM,           FLAGS,        OBJCOPY_##GROUP,                             \
68    OBJCOPY_##ALIAS, ALIASARGS,    VALUES},
69 #include "ObjcopyOpts.inc"
70 #undef OPTION
71 };
72 
73 class ObjcopyOptTable : public opt::OptTable {
74 public:
75   ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {}
76 };
77 
78 enum StripID {
79   STRIP_INVALID = 0, // This is not an option ID.
80 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
81                HELPTEXT, METAVAR, VALUES)                                      \
82   STRIP_##ID,
83 #include "StripOpts.inc"
84 #undef OPTION
85 };
86 
87 static const opt::OptTable::Info StripInfoTable[] = {
88 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
89                HELPTEXT, METAVAR, VALUES)                                      \
90   {PREFIX,          NAME,         HELPTEXT,                                    \
91    METAVAR,         STRIP_##ID, opt::Option::KIND##Class,                      \
92    PARAM,           FLAGS,        STRIP_##GROUP,                               \
93    STRIP_##ALIAS, ALIASARGS,    VALUES},
94 #include "StripOpts.inc"
95 #undef OPTION
96 };
97 
98 class StripOptTable : public opt::OptTable {
99 public:
100   StripOptTable() : OptTable(StripInfoTable, true) {}
101 };
102 
103 } // namespace
104 
105 // The name this program was invoked as.
106 static StringRef ToolName;
107 
108 namespace llvm {
109 
110 LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
111   errs() << ToolName << ": " << Message << ".\n";
112   errs().flush();
113   exit(1);
114 }
115 
116 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
117   assert(EC);
118   errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
119   exit(1);
120 }
121 
122 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
123   assert(E);
124   std::string Buf;
125   raw_string_ostream OS(Buf);
126   logAllUnhandledErrors(std::move(E), OS, "");
127   OS.flush();
128   errs() << ToolName << ": '" << File << "': " << Buf;
129   exit(1);
130 }
131 
132 } // end namespace llvm
133 
134 struct CopyConfig {
135   StringRef OutputFilename;
136   StringRef InputFilename;
137   StringRef OutputFormat;
138   StringRef InputFormat;
139   StringRef BinaryArch;
140 
141   StringRef SplitDWO;
142   StringRef AddGnuDebugLink;
143   std::vector<StringRef> ToRemove;
144   std::vector<StringRef> Keep;
145   std::vector<StringRef> OnlyKeep;
146   std::vector<StringRef> AddSection;
147   std::vector<StringRef> SymbolsToLocalize;
148   std::vector<StringRef> SymbolsToGlobalize;
149   std::vector<StringRef> SymbolsToWeaken;
150   StringMap<StringRef> SymbolsToRename;
151   bool StripAll = false;
152   bool StripAllGNU = false;
153   bool StripDebug = false;
154   bool StripSections = false;
155   bool StripNonAlloc = false;
156   bool StripDWO = false;
157   bool ExtractDWO = false;
158   bool LocalizeHidden = false;
159   bool Weaken = false;
160   bool DiscardAll = false;
161 };
162 
163 using SectionPred = std::function<bool(const SectionBase &Sec)>;
164 
165 bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
166 
167 bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
168   // We can't remove the section header string table.
169   if (&Sec == Obj.SectionNames)
170     return false;
171   // Short of keeping the string table we want to keep everything that is a DWO
172   // section and remove everything else.
173   return !IsDWOSection(Sec);
174 }
175 
176 std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
177                                      StringRef File, ElfType OutputElfType) {
178   if (Config.OutputFormat == "binary") {
179     return llvm::make_unique<BinaryWriter>(File, Obj);
180   }
181   // Depending on the initial ELFT and OutputFormat we need a different Writer.
182   switch (OutputElfType) {
183   case ELFT_ELF32LE:
184     return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
185                                                  !Config.StripSections);
186   case ELFT_ELF64LE:
187     return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
188                                                  !Config.StripSections);
189   case ELFT_ELF32BE:
190     return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
191                                                  !Config.StripSections);
192   case ELFT_ELF64BE:
193     return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
194                                                  !Config.StripSections);
195   }
196   llvm_unreachable("Invalid output format");
197 }
198 
199 void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
200                     StringRef File, ElfType OutputElfType) {
201   auto DWOFile = Reader.create();
202   DWOFile->removeSections(
203       [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
204   auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
205   Writer->finalize();
206   Writer->write();
207 }
208 
209 // This function handles the high level operations of GNU objcopy including
210 // handling command line options. It's important to outline certain properties
211 // we expect to hold of the command line operations. Any operation that "keeps"
212 // should keep regardless of a remove. Additionally any removal should respect
213 // any previous removals. Lastly whether or not something is removed shouldn't
214 // depend a) on the order the options occur in or b) on some opaque priority
215 // system. The only priority is that keeps/copies overrule removes.
216 void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
217                 ElfType OutputElfType) {
218 
219   if (!Config.SplitDWO.empty()) {
220     SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
221   }
222 
223   SectionPred RemovePred = [](const SectionBase &) { return false; };
224 
225   // Removes:
226   if (!Config.ToRemove.empty()) {
227     RemovePred = [&Config](const SectionBase &Sec) {
228       return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
229                        Sec.Name) != std::end(Config.ToRemove);
230     };
231   }
232 
233   if (Config.StripDWO || !Config.SplitDWO.empty())
234     RemovePred = [RemovePred](const SectionBase &Sec) {
235       return IsDWOSection(Sec) || RemovePred(Sec);
236     };
237 
238   if (Config.ExtractDWO)
239     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
240       return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
241     };
242 
243   if (Config.StripAllGNU)
244     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
245       if (RemovePred(Sec))
246         return true;
247       if ((Sec.Flags & SHF_ALLOC) != 0)
248         return false;
249       if (&Sec == Obj.SectionNames)
250         return false;
251       switch (Sec.Type) {
252       case SHT_SYMTAB:
253       case SHT_REL:
254       case SHT_RELA:
255       case SHT_STRTAB:
256         return true;
257       }
258       return Sec.Name.startswith(".debug");
259     };
260 
261   if (Config.StripSections) {
262     RemovePred = [RemovePred](const SectionBase &Sec) {
263       return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
264     };
265   }
266 
267   if (Config.StripDebug) {
268     RemovePred = [RemovePred](const SectionBase &Sec) {
269       return RemovePred(Sec) || Sec.Name.startswith(".debug");
270     };
271   }
272 
273   if (Config.StripNonAlloc)
274     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
275       if (RemovePred(Sec))
276         return true;
277       if (&Sec == Obj.SectionNames)
278         return false;
279       return (Sec.Flags & SHF_ALLOC) == 0;
280     };
281 
282   if (Config.StripAll)
283     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
284       if (RemovePred(Sec))
285         return true;
286       if (&Sec == Obj.SectionNames)
287         return false;
288       if (Sec.Name.startswith(".gnu.warning"))
289         return false;
290       return (Sec.Flags & SHF_ALLOC) == 0;
291     };
292 
293   // Explicit copies:
294   if (!Config.OnlyKeep.empty()) {
295     RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
296       // Explicitly keep these sections regardless of previous removes.
297       if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
298                     Sec.Name) != std::end(Config.OnlyKeep))
299         return false;
300 
301       // Allow all implicit removes.
302       if (RemovePred(Sec))
303         return true;
304 
305       // Keep special sections.
306       if (Obj.SectionNames == &Sec)
307         return false;
308       if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
309         return false;
310 
311       // Remove everything else.
312       return true;
313     };
314   }
315 
316   if (!Config.Keep.empty()) {
317     RemovePred = [Config, RemovePred](const SectionBase &Sec) {
318       // Explicitly keep these sections regardless of previous removes.
319       if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
320           std::end(Config.Keep))
321         return false;
322       // Otherwise defer to RemovePred.
323       return RemovePred(Sec);
324     };
325   }
326 
327   Obj.removeSections(RemovePred);
328 
329   if (!Config.AddSection.empty()) {
330     for (const auto &Flag : Config.AddSection) {
331       auto SecPair = Flag.split("=");
332       auto SecName = SecPair.first;
333       auto File = SecPair.second;
334       auto BufOrErr = MemoryBuffer::getFile(File);
335       if (!BufOrErr)
336         reportError(File, BufOrErr.getError());
337       auto Buf = std::move(*BufOrErr);
338       auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
339       auto BufSize = Buf->getBufferSize();
340       Obj.addSection<OwnedDataSection>(SecName,
341                                        ArrayRef<uint8_t>(BufPtr, BufSize));
342     }
343   }
344 
345   if (!Config.AddGnuDebugLink.empty())
346     Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
347 
348   if (Obj.SymbolTable) {
349     Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
350       if ((Config.LocalizeHidden &&
351            (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
352           (!Config.SymbolsToLocalize.empty() &&
353            is_contained(Config.SymbolsToLocalize, Sym.Name)))
354         Sym.Binding = STB_LOCAL;
355 
356       if (!Config.SymbolsToGlobalize.empty() &&
357           is_contained(Config.SymbolsToGlobalize, Sym.Name))
358         Sym.Binding = STB_GLOBAL;
359 
360       if (!Config.SymbolsToWeaken.empty() &&
361           is_contained(Config.SymbolsToWeaken, Sym.Name) &&
362           Sym.Binding == STB_GLOBAL)
363         Sym.Binding = STB_WEAK;
364 
365       if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
366           Sym.getShndx() != SHN_UNDEF)
367         Sym.Binding = STB_WEAK;
368 
369       const auto I = Config.SymbolsToRename.find(Sym.Name);
370       if (I != Config.SymbolsToRename.end())
371         Sym.Name = I->getValue();
372     });
373 
374     Obj.SymbolTable->removeSymbols([&](const Symbol &Sym) {
375       if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
376           Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE &&
377           Sym.Type != STT_SECTION)
378         return true;
379       return false;
380     });
381   }
382 }
383 
384 std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
385                                      ElfType &OutputElfType) {
386   // Right now we can only read ELF files so there's only one reader;
387   auto Out = llvm::make_unique<ELFReader>(InputFilename);
388   // We need to set the default ElfType for output.
389   OutputElfType = Out->getElfType();
390   return std::move(Out);
391 }
392 
393 void ExecuteElfObjcopy(const CopyConfig &Config) {
394   ElfType OutputElfType;
395   auto Reader = CreateReader(Config.InputFilename, OutputElfType);
396   auto Obj = Reader->create();
397   auto Writer =
398       CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
399   HandleArgs(Config, *Obj, *Reader, OutputElfType);
400   Writer->finalize();
401   Writer->write();
402 }
403 
404 // ParseObjcopyOptions returns the config and sets the input arguments. If a
405 // help flag is set then ParseObjcopyOptions will print the help messege and
406 // exit.
407 CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
408   ObjcopyOptTable T;
409   unsigned MissingArgumentIndex, MissingArgumentCount;
410   llvm::opt::InputArgList InputArgs =
411       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
412 
413   if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {
414     T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
415     exit(0);
416   }
417 
418   SmallVector<const char *, 2> Positional;
419 
420   for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
421     error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
422 
423   for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
424     Positional.push_back(Arg->getValue());
425 
426   if (Positional.empty())
427     error("No input file specified");
428 
429   if (Positional.size() > 2)
430     error("Too many positional arguments");
431 
432   CopyConfig Config;
433   Config.InputFilename = Positional[0];
434   Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
435   Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
436   Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
437   Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
438 
439   Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
440   Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
441 
442   for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
443     if (!StringRef(Arg->getValue()).contains('='))
444       error("Bad format for --redefine-sym");
445     auto Old2New = StringRef(Arg->getValue()).split('=');
446     if (!Config.SymbolsToRename.insert(Old2New).second)
447       error("Multiple redefinition of symbol " + Old2New.first);
448   }
449 
450   for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
451     Config.ToRemove.push_back(Arg->getValue());
452   for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
453     Config.Keep.push_back(Arg->getValue());
454   for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
455     Config.OnlyKeep.push_back(Arg->getValue());
456   for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
457     Config.AddSection.push_back(Arg->getValue());
458   Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
459   Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
460   Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
461   Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
462   Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
463   Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
464   Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
465   Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
466   Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
467   Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all);
468   for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
469     Config.SymbolsToLocalize.push_back(Arg->getValue());
470   for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
471     Config.SymbolsToGlobalize.push_back(Arg->getValue());
472   for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
473     Config.SymbolsToWeaken.push_back(Arg->getValue());
474 
475   return Config;
476 }
477 
478 // ParseStripOptions returns the config and sets the input arguments. If a
479 // help flag is set then ParseStripOptions will print the help messege and
480 // exit.
481 CopyConfig ParseStripOptions(ArrayRef<const char *> ArgsArr) {
482   StripOptTable T;
483   unsigned MissingArgumentIndex, MissingArgumentCount;
484   llvm::opt::InputArgList InputArgs =
485       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
486 
487   if (InputArgs.size() == 0 || InputArgs.hasArg(STRIP_help)) {
488     T.PrintHelp(outs(), "llvm-strip <input> [ <output> ]", "strip tool");
489     exit(0);
490   }
491 
492   SmallVector<const char *, 2> Positional;
493   for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
494     error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
495   for (auto Arg : InputArgs.filtered(STRIP_INPUT))
496     Positional.push_back(Arg->getValue());
497 
498   if (Positional.empty())
499     error("No input file specified");
500 
501   if (Positional.size() > 2)
502     error("Support for multiple input files is not implemented yet");
503 
504   CopyConfig Config;
505   Config.InputFilename = Positional[0];
506   Config.OutputFilename = Positional[0];
507 
508   // Strip debug info only.
509   Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
510   if (!Config.StripDebug)
511     Config.StripAll = true;
512   return Config;
513 }
514 
515 int main(int argc, char **argv) {
516   InitLLVM X(argc, argv);
517   ToolName = argv[0];
518   CopyConfig Config;
519   if (sys::path::stem(ToolName).endswith_lower("strip"))
520     Config = ParseStripOptions(makeArrayRef(argv + 1, argc));
521   else
522     Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
523   ExecuteElfObjcopy(Config);
524 }
525