xref: /llvm-project/llvm/tools/llvm-objcopy/llvm-objcopy.cpp (revision 5d97c823a480f3b523fcd3f21d9e09ea601e3333)
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   std::vector<StringRef> SymbolsToRemove;
151   std::vector<StringRef> SymbolsToKeep;
152   StringMap<StringRef> SymbolsToRename;
153   bool StripAll = false;
154   bool StripAllGNU = false;
155   bool StripDebug = false;
156   bool StripSections = false;
157   bool StripNonAlloc = false;
158   bool StripDWO = false;
159   bool ExtractDWO = false;
160   bool LocalizeHidden = false;
161   bool Weaken = false;
162   bool DiscardAll = false;
163 };
164 
165 using SectionPred = std::function<bool(const SectionBase &Sec)>;
166 
167 bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
168 
169 bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
170   // We can't remove the section header string table.
171   if (&Sec == Obj.SectionNames)
172     return false;
173   // Short of keeping the string table we want to keep everything that is a DWO
174   // section and remove everything else.
175   return !IsDWOSection(Sec);
176 }
177 
178 std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
179                                      StringRef File, ElfType OutputElfType) {
180   if (Config.OutputFormat == "binary") {
181     return llvm::make_unique<BinaryWriter>(File, Obj);
182   }
183   // Depending on the initial ELFT and OutputFormat we need a different Writer.
184   switch (OutputElfType) {
185   case ELFT_ELF32LE:
186     return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
187                                                  !Config.StripSections);
188   case ELFT_ELF64LE:
189     return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
190                                                  !Config.StripSections);
191   case ELFT_ELF32BE:
192     return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
193                                                  !Config.StripSections);
194   case ELFT_ELF64BE:
195     return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
196                                                  !Config.StripSections);
197   }
198   llvm_unreachable("Invalid output format");
199 }
200 
201 void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
202                     StringRef File, ElfType OutputElfType) {
203   auto DWOFile = Reader.create();
204   DWOFile->removeSections(
205       [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
206   auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
207   Writer->finalize();
208   Writer->write();
209 }
210 
211 // This function handles the high level operations of GNU objcopy including
212 // handling command line options. It's important to outline certain properties
213 // we expect to hold of the command line operations. Any operation that "keeps"
214 // should keep regardless of a remove. Additionally any removal should respect
215 // any previous removals. Lastly whether or not something is removed shouldn't
216 // depend a) on the order the options occur in or b) on some opaque priority
217 // system. The only priority is that keeps/copies overrule removes.
218 void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
219                 ElfType OutputElfType) {
220 
221   if (!Config.SplitDWO.empty()) {
222     SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
223   }
224 
225   SectionPred RemovePred = [](const SectionBase &) { return false; };
226 
227   // Removes:
228   if (!Config.ToRemove.empty()) {
229     RemovePred = [&Config](const SectionBase &Sec) {
230       return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
231                        Sec.Name) != std::end(Config.ToRemove);
232     };
233   }
234 
235   if (Config.StripDWO || !Config.SplitDWO.empty())
236     RemovePred = [RemovePred](const SectionBase &Sec) {
237       return IsDWOSection(Sec) || RemovePred(Sec);
238     };
239 
240   if (Config.ExtractDWO)
241     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
242       return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
243     };
244 
245   if (Config.StripAllGNU)
246     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
247       if (RemovePred(Sec))
248         return true;
249       if ((Sec.Flags & SHF_ALLOC) != 0)
250         return false;
251       if (&Sec == Obj.SectionNames)
252         return false;
253       switch (Sec.Type) {
254       case SHT_SYMTAB:
255       case SHT_REL:
256       case SHT_RELA:
257       case SHT_STRTAB:
258         return true;
259       }
260       return Sec.Name.startswith(".debug");
261     };
262 
263   if (Config.StripSections) {
264     RemovePred = [RemovePred](const SectionBase &Sec) {
265       return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
266     };
267   }
268 
269   if (Config.StripDebug) {
270     RemovePred = [RemovePred](const SectionBase &Sec) {
271       return RemovePred(Sec) || Sec.Name.startswith(".debug");
272     };
273   }
274 
275   if (Config.StripNonAlloc)
276     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
277       if (RemovePred(Sec))
278         return true;
279       if (&Sec == Obj.SectionNames)
280         return false;
281       return (Sec.Flags & SHF_ALLOC) == 0;
282     };
283 
284   if (Config.StripAll)
285     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
286       if (RemovePred(Sec))
287         return true;
288       if (&Sec == Obj.SectionNames)
289         return false;
290       if (Sec.Name.startswith(".gnu.warning"))
291         return false;
292       return (Sec.Flags & SHF_ALLOC) == 0;
293     };
294 
295   // Explicit copies:
296   if (!Config.OnlyKeep.empty()) {
297     RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
298       // Explicitly keep these sections regardless of previous removes.
299       if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
300                     Sec.Name) != std::end(Config.OnlyKeep))
301         return false;
302 
303       // Allow all implicit removes.
304       if (RemovePred(Sec))
305         return true;
306 
307       // Keep special sections.
308       if (Obj.SectionNames == &Sec)
309         return false;
310       if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
311         return false;
312 
313       // Remove everything else.
314       return true;
315     };
316   }
317 
318   if (!Config.Keep.empty()) {
319     RemovePred = [Config, RemovePred](const SectionBase &Sec) {
320       // Explicitly keep these sections regardless of previous removes.
321       if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
322           std::end(Config.Keep))
323         return false;
324       // Otherwise defer to RemovePred.
325       return RemovePred(Sec);
326     };
327   }
328 
329   Obj.removeSections(RemovePred);
330 
331   if (!Config.AddSection.empty()) {
332     for (const auto &Flag : Config.AddSection) {
333       auto SecPair = Flag.split("=");
334       auto SecName = SecPair.first;
335       auto File = SecPair.second;
336       auto BufOrErr = MemoryBuffer::getFile(File);
337       if (!BufOrErr)
338         reportError(File, BufOrErr.getError());
339       auto Buf = std::move(*BufOrErr);
340       auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
341       auto BufSize = Buf->getBufferSize();
342       Obj.addSection<OwnedDataSection>(SecName,
343                                        ArrayRef<uint8_t>(BufPtr, BufSize));
344     }
345   }
346 
347   if (!Config.AddGnuDebugLink.empty())
348     Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
349 
350   if (Obj.SymbolTable) {
351     Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
352       if ((Config.LocalizeHidden &&
353            (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
354           (!Config.SymbolsToLocalize.empty() &&
355            is_contained(Config.SymbolsToLocalize, Sym.Name)))
356         Sym.Binding = STB_LOCAL;
357 
358       if (!Config.SymbolsToGlobalize.empty() &&
359           is_contained(Config.SymbolsToGlobalize, Sym.Name))
360         Sym.Binding = STB_GLOBAL;
361 
362       if (!Config.SymbolsToWeaken.empty() &&
363           is_contained(Config.SymbolsToWeaken, Sym.Name) &&
364           Sym.Binding == STB_GLOBAL)
365         Sym.Binding = STB_WEAK;
366 
367       if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
368           Sym.getShndx() != SHN_UNDEF)
369         Sym.Binding = STB_WEAK;
370 
371       const auto I = Config.SymbolsToRename.find(Sym.Name);
372       if (I != Config.SymbolsToRename.end())
373         Sym.Name = I->getValue();
374     });
375 
376     Obj.removeSymbols([&](const Symbol &Sym) {
377       if (!Config.SymbolsToKeep.empty() &&
378           is_contained(Config.SymbolsToKeep, Sym.Name))
379         return false;
380 
381       if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
382           Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE &&
383           Sym.Type != STT_SECTION)
384         return true;
385 
386       if (!Config.SymbolsToRemove.empty() &&
387           is_contained(Config.SymbolsToRemove, Sym.Name)) {
388         return true;
389       }
390 
391       return false;
392     });
393   }
394 }
395 
396 std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
397                                      ElfType &OutputElfType) {
398   // Right now we can only read ELF files so there's only one reader;
399   auto Out = llvm::make_unique<ELFReader>(InputFilename);
400   // We need to set the default ElfType for output.
401   OutputElfType = Out->getElfType();
402   return std::move(Out);
403 }
404 
405 void ExecuteElfObjcopy(const CopyConfig &Config) {
406   ElfType OutputElfType;
407   auto Reader = CreateReader(Config.InputFilename, OutputElfType);
408   auto Obj = Reader->create();
409   auto Writer =
410       CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
411   HandleArgs(Config, *Obj, *Reader, OutputElfType);
412   Writer->finalize();
413   Writer->write();
414 }
415 
416 // ParseObjcopyOptions returns the config and sets the input arguments. If a
417 // help flag is set then ParseObjcopyOptions will print the help messege and
418 // exit.
419 CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
420   ObjcopyOptTable T;
421   unsigned MissingArgumentIndex, MissingArgumentCount;
422   llvm::opt::InputArgList InputArgs =
423       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
424 
425   if (InputArgs.size() == 0) {
426     T.PrintHelp(errs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
427     exit(1);
428   }
429 
430   if (InputArgs.hasArg(OBJCOPY_help)) {
431     T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
432     exit(0);
433   }
434 
435   SmallVector<const char *, 2> Positional;
436 
437   for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
438     error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
439 
440   for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
441     Positional.push_back(Arg->getValue());
442 
443   if (Positional.empty())
444     error("No input file specified");
445 
446   if (Positional.size() > 2)
447     error("Too many positional arguments");
448 
449   CopyConfig Config;
450   Config.InputFilename = Positional[0];
451   Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
452   Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
453   Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
454   Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
455 
456   Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
457   Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
458 
459   for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
460     if (!StringRef(Arg->getValue()).contains('='))
461       error("Bad format for --redefine-sym");
462     auto Old2New = StringRef(Arg->getValue()).split('=');
463     if (!Config.SymbolsToRename.insert(Old2New).second)
464       error("Multiple redefinition of symbol " + Old2New.first);
465   }
466 
467   for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
468     Config.ToRemove.push_back(Arg->getValue());
469   for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
470     Config.Keep.push_back(Arg->getValue());
471   for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
472     Config.OnlyKeep.push_back(Arg->getValue());
473   for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
474     Config.AddSection.push_back(Arg->getValue());
475   Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
476   Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
477   Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
478   Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
479   Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
480   Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
481   Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
482   Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
483   Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
484   Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all);
485   for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
486     Config.SymbolsToLocalize.push_back(Arg->getValue());
487   for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
488     Config.SymbolsToGlobalize.push_back(Arg->getValue());
489   for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
490     Config.SymbolsToWeaken.push_back(Arg->getValue());
491   for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
492     Config.SymbolsToRemove.push_back(Arg->getValue());
493   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
494     Config.SymbolsToKeep.push_back(Arg->getValue());
495 
496   return Config;
497 }
498 
499 // ParseStripOptions returns the config and sets the input arguments. If a
500 // help flag is set then ParseStripOptions will print the help messege and
501 // exit.
502 CopyConfig ParseStripOptions(ArrayRef<const char *> ArgsArr) {
503   StripOptTable T;
504   unsigned MissingArgumentIndex, MissingArgumentCount;
505   llvm::opt::InputArgList InputArgs =
506       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
507 
508   if (InputArgs.size() == 0) {
509     T.PrintHelp(errs(), "llvm-strip <input> [ <output> ]", "strip tool");
510     exit(1);
511   }
512 
513   if (InputArgs.hasArg(STRIP_help)) {
514     T.PrintHelp(outs(), "llvm-strip <input> [ <output> ]", "strip tool");
515     exit(0);
516   }
517 
518   SmallVector<const char *, 2> Positional;
519   for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
520     error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
521   for (auto Arg : InputArgs.filtered(STRIP_INPUT))
522     Positional.push_back(Arg->getValue());
523 
524   if (Positional.empty())
525     error("No input file specified");
526 
527   if (Positional.size() > 2)
528     error("Support for multiple input files is not implemented yet");
529 
530   CopyConfig Config;
531   Config.InputFilename = Positional[0];
532   Config.OutputFilename = Positional[0];
533 
534   // Strip debug info only.
535   Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
536   if (!Config.StripDebug)
537     Config.StripAll = true;
538 
539   for (auto Arg : InputArgs.filtered(STRIP_remove_section))
540     Config.ToRemove.push_back(Arg->getValue());
541 
542   return Config;
543 }
544 
545 int main(int argc, char **argv) {
546   InitLLVM X(argc, argv);
547   ToolName = argv[0];
548   CopyConfig Config;
549   if (sys::path::stem(ToolName).endswith_lower("strip"))
550     Config = ParseStripOptions(makeArrayRef(argv + 1, argc));
551   else
552     Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
553   ExecuteElfObjcopy(Config);
554 }
555