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