xref: /llvm-project/llvm/tools/llvm-objcopy/llvm-objcopy.cpp (revision 5be060e3419bd6920581949abb5abd6d85f5b324)
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 
12 #include "Object.h"
13 #include "llvm/ADT/BitmaskEnum.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/ArchiveWriter.h"
22 #include "llvm/Object/Binary.h"
23 #include "llvm/Object/ELFObjectFile.h"
24 #include "llvm/Object/ELFTypes.h"
25 #include "llvm/Object/Error.h"
26 #include "llvm/Option/Arg.h"
27 #include "llvm/Option/ArgList.h"
28 #include "llvm/Option/Option.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Error.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/ErrorOr.h"
35 #include "llvm/Support/FileOutputBuffer.h"
36 #include "llvm/Support/InitLLVM.h"
37 #include "llvm/Support/Memory.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Support/Process.h"
40 #include "llvm/Support/WithColor.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <algorithm>
43 #include <cassert>
44 #include <cstdlib>
45 #include <functional>
46 #include <iterator>
47 #include <memory>
48 #include <string>
49 #include <system_error>
50 #include <utility>
51 
52 using namespace llvm;
53 using namespace llvm::objcopy;
54 using namespace object;
55 using namespace ELF;
56 
57 namespace {
58 
59 enum ObjcopyID {
60   OBJCOPY_INVALID = 0, // This is not an option ID.
61 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
62                HELPTEXT, METAVAR, VALUES)                                      \
63   OBJCOPY_##ID,
64 #include "ObjcopyOpts.inc"
65 #undef OPTION
66 };
67 
68 #define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE;
69 #include "ObjcopyOpts.inc"
70 #undef PREFIX
71 
72 static const opt::OptTable::Info ObjcopyInfoTable[] = {
73 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
74                HELPTEXT, METAVAR, VALUES)                                      \
75   {OBJCOPY_##PREFIX,                                                           \
76    NAME,                                                                       \
77    HELPTEXT,                                                                   \
78    METAVAR,                                                                    \
79    OBJCOPY_##ID,                                                               \
80    opt::Option::KIND##Class,                                                   \
81    PARAM,                                                                      \
82    FLAGS,                                                                      \
83    OBJCOPY_##GROUP,                                                            \
84    OBJCOPY_##ALIAS,                                                            \
85    ALIASARGS,                                                                  \
86    VALUES},
87 #include "ObjcopyOpts.inc"
88 #undef OPTION
89 };
90 
91 class ObjcopyOptTable : public opt::OptTable {
92 public:
93   ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {}
94 };
95 
96 enum StripID {
97   STRIP_INVALID = 0, // This is not an option ID.
98 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
99                HELPTEXT, METAVAR, VALUES)                                      \
100   STRIP_##ID,
101 #include "StripOpts.inc"
102 #undef OPTION
103 };
104 
105 #define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE;
106 #include "StripOpts.inc"
107 #undef PREFIX
108 
109 static const opt::OptTable::Info StripInfoTable[] = {
110 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
111                HELPTEXT, METAVAR, VALUES)                                      \
112   {STRIP_##PREFIX, NAME,       HELPTEXT,                                       \
113    METAVAR,        STRIP_##ID, opt::Option::KIND##Class,                       \
114    PARAM,          FLAGS,      STRIP_##GROUP,                                  \
115    STRIP_##ALIAS,  ALIASARGS,  VALUES},
116 #include "StripOpts.inc"
117 #undef OPTION
118 };
119 
120 class StripOptTable : public opt::OptTable {
121 public:
122   StripOptTable() : OptTable(StripInfoTable, true) {}
123 };
124 
125 struct SectionRename {
126   StringRef OriginalName;
127   StringRef NewName;
128   Optional<uint64_t> NewFlags;
129 };
130 
131 // Configuration for copying/stripping a single file.
132 struct CopyConfig {
133   // Main input/output options
134   StringRef InputFilename;
135   StringRef InputFormat;
136   StringRef OutputFilename;
137   StringRef OutputFormat;
138 
139   // Only applicable for --input-format=Binary
140   MachineInfo BinaryArch;
141 
142   // Advanced options
143   StringRef AddGnuDebugLink;
144   StringRef SplitDWO;
145   StringRef SymbolsPrefix;
146 
147   // Repeated options
148   std::vector<StringRef> AddSection;
149   std::vector<StringRef> DumpSection;
150   std::vector<StringRef> Keep;
151   std::vector<StringRef> OnlyKeep;
152   std::vector<StringRef> SymbolsToGlobalize;
153   std::vector<StringRef> SymbolsToKeep;
154   std::vector<StringRef> SymbolsToLocalize;
155   std::vector<StringRef> SymbolsToRemove;
156   std::vector<StringRef> SymbolsToWeaken;
157   std::vector<StringRef> ToRemove;
158   std::vector<std::string> SymbolsToKeepGlobal;
159 
160   // Map options
161   StringMap<SectionRename> SectionsToRename;
162   StringMap<StringRef> SymbolsToRename;
163 
164   // Boolean options
165   bool DiscardAll = false;
166   bool ExtractDWO = false;
167   bool KeepFileSymbols = false;
168   bool LocalizeHidden = false;
169   bool OnlyKeepDebug = false;
170   bool PreserveDates = false;
171   bool StripAll = false;
172   bool StripAllGNU = false;
173   bool StripDWO = false;
174   bool StripDebug = false;
175   bool StripNonAlloc = false;
176   bool StripSections = false;
177   bool StripUnneeded = false;
178   bool Weaken = false;
179 };
180 
181 // Configuration for the overall invocation of this tool. When invoked as
182 // objcopy, will always contain exactly one CopyConfig. When invoked as strip,
183 // will contain one or more CopyConfigs.
184 struct DriverConfig {
185   SmallVector<CopyConfig, 1> CopyConfigs;
186 };
187 
188 using SectionPred = std::function<bool(const SectionBase &Sec)>;
189 
190 enum SectionFlag {
191   SecNone = 0,
192   SecAlloc = 1 << 0,
193   SecLoad = 1 << 1,
194   SecNoload = 1 << 2,
195   SecReadonly = 1 << 3,
196   SecDebug = 1 << 4,
197   SecCode = 1 << 5,
198   SecData = 1 << 6,
199   SecRom = 1 << 7,
200   SecMerge = 1 << 8,
201   SecStrings = 1 << 9,
202   SecContents = 1 << 10,
203   SecShare = 1 << 11,
204   LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
205 };
206 
207 } // namespace
208 
209 namespace llvm {
210 namespace objcopy {
211 
212 // The name this program was invoked as.
213 StringRef ToolName;
214 
215 LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
216   WithColor::error(errs(), ToolName) << Message << ".\n";
217   errs().flush();
218   exit(1);
219 }
220 
221 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
222   assert(EC);
223   WithColor::error(errs(), ToolName)
224       << "'" << File << "': " << EC.message() << ".\n";
225   exit(1);
226 }
227 
228 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
229   assert(E);
230   std::string Buf;
231   raw_string_ostream OS(Buf);
232   logAllUnhandledErrors(std::move(E), OS, "");
233   OS.flush();
234   WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf;
235   exit(1);
236 }
237 
238 } // end namespace objcopy
239 } // end namespace llvm
240 
241 static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
242   return llvm::StringSwitch<SectionFlag>(SectionName)
243       .Case("alloc", SectionFlag::SecAlloc)
244       .Case("load", SectionFlag::SecLoad)
245       .Case("noload", SectionFlag::SecNoload)
246       .Case("readonly", SectionFlag::SecReadonly)
247       .Case("debug", SectionFlag::SecDebug)
248       .Case("code", SectionFlag::SecCode)
249       .Case("data", SectionFlag::SecData)
250       .Case("rom", SectionFlag::SecRom)
251       .Case("merge", SectionFlag::SecMerge)
252       .Case("strings", SectionFlag::SecStrings)
253       .Case("contents", SectionFlag::SecContents)
254       .Case("share", SectionFlag::SecShare)
255       .Default(SectionFlag::SecNone);
256 }
257 
258 static SectionRename parseRenameSectionValue(StringRef FlagValue) {
259   if (!FlagValue.contains('='))
260     error("Bad format for --rename-section: missing '='");
261 
262   // Initial split: ".foo" = ".bar,f1,f2,..."
263   auto Old2New = FlagValue.split('=');
264   SectionRename SR;
265   SR.OriginalName = Old2New.first;
266 
267   // Flags split: ".bar" "f1" "f2" ...
268   SmallVector<StringRef, 6> NameAndFlags;
269   Old2New.second.split(NameAndFlags, ',');
270   SR.NewName = NameAndFlags[0];
271 
272   if (NameAndFlags.size() > 1) {
273     SectionFlag Flags = SectionFlag::SecNone;
274     for (size_t I = 1, Size = NameAndFlags.size(); I < Size; ++I) {
275       SectionFlag Flag = parseSectionRenameFlag(NameAndFlags[I]);
276       if (Flag == SectionFlag::SecNone)
277         error("Unrecognized section flag '" + NameAndFlags[I] +
278               "'. Flags supported for GNU compatibility: alloc, load, noload, "
279               "readonly, debug, code, data, rom, share, contents, merge, "
280               "strings.");
281       Flags |= Flag;
282     }
283 
284     SR.NewFlags = 0;
285     if (Flags & SectionFlag::SecAlloc)
286       *SR.NewFlags |= ELF::SHF_ALLOC;
287     if (!(Flags & SectionFlag::SecReadonly))
288       *SR.NewFlags |= ELF::SHF_WRITE;
289     if (Flags & SectionFlag::SecCode)
290       *SR.NewFlags |= ELF::SHF_EXECINSTR;
291     if (Flags & SectionFlag::SecMerge)
292       *SR.NewFlags |= ELF::SHF_MERGE;
293     if (Flags & SectionFlag::SecStrings)
294       *SR.NewFlags |= ELF::SHF_STRINGS;
295   }
296 
297   return SR;
298 }
299 
300 static bool isDebugSection(const SectionBase &Sec) {
301   return Sec.Name.startswith(".debug") || Sec.Name.startswith(".zdebug") ||
302          Sec.Name == ".gdb_index";
303 }
304 
305 static bool isDWOSection(const SectionBase &Sec) {
306   return Sec.Name.endswith(".dwo");
307 }
308 
309 static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
310   // We can't remove the section header string table.
311   if (&Sec == Obj.SectionNames)
312     return false;
313   // Short of keeping the string table we want to keep everything that is a DWO
314   // section and remove everything else.
315   return !isDWOSection(Sec);
316 }
317 
318 static const StringMap<MachineInfo> ArchMap{
319     // Name, {EMachine, 64bit, LittleEndian}
320     {"aarch64", {EM_AARCH64, true, true}},
321     {"arm", {EM_ARM, false, true}},
322     {"i386", {EM_386, false, true}},
323     {"i386:x86-64", {EM_X86_64, true, true}},
324     {"powerpc:common64", {EM_PPC64, true, true}},
325     {"sparc", {EM_SPARC, false, true}},
326     {"x86-64", {EM_X86_64, true, true}},
327 };
328 
329 static const MachineInfo &getMachineInfo(StringRef Arch) {
330   auto Iter = ArchMap.find(Arch);
331   if (Iter == std::end(ArchMap))
332     error("Invalid architecture: '" + Arch + "'");
333   return Iter->getValue();
334 }
335 
336 static ElfType getOutputElfType(const Binary &Bin) {
337   // Infer output ELF type from the input ELF object
338   if (isa<ELFObjectFile<ELF32LE>>(Bin))
339     return ELFT_ELF32LE;
340   if (isa<ELFObjectFile<ELF64LE>>(Bin))
341     return ELFT_ELF64LE;
342   if (isa<ELFObjectFile<ELF32BE>>(Bin))
343     return ELFT_ELF32BE;
344   if (isa<ELFObjectFile<ELF64BE>>(Bin))
345     return ELFT_ELF64BE;
346   llvm_unreachable("Invalid ELFType");
347 }
348 
349 static ElfType getOutputElfType(const MachineInfo &MI) {
350   // Infer output ELF type from the binary arch specified
351   if (MI.Is64Bit)
352     return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
353   else
354     return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
355 }
356 
357 static std::unique_ptr<Writer> createWriter(const CopyConfig &Config,
358                                             Object &Obj, Buffer &Buf,
359                                             ElfType OutputElfType) {
360   if (Config.OutputFormat == "binary") {
361     return llvm::make_unique<BinaryWriter>(Obj, Buf);
362   }
363   // Depending on the initial ELFT and OutputFormat we need a different Writer.
364   switch (OutputElfType) {
365   case ELFT_ELF32LE:
366     return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf,
367                                                  !Config.StripSections);
368   case ELFT_ELF64LE:
369     return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf,
370                                                  !Config.StripSections);
371   case ELFT_ELF32BE:
372     return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf,
373                                                  !Config.StripSections);
374   case ELFT_ELF64BE:
375     return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf,
376                                                  !Config.StripSections);
377   }
378   llvm_unreachable("Invalid output format");
379 }
380 
381 static void splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
382                            StringRef File, ElfType OutputElfType) {
383   auto DWOFile = Reader.create();
384   DWOFile->removeSections(
385       [&](const SectionBase &Sec) { return onlyKeepDWOPred(*DWOFile, Sec); });
386   FileBuffer FB(File);
387   auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType);
388   Writer->finalize();
389   Writer->write();
390 }
391 
392 static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
393                                Object &Obj) {
394   for (auto &Sec : Obj.sections()) {
395     if (Sec.Name == SecName) {
396       if (Sec.OriginalData.size() == 0)
397         return make_error<StringError>("Can't dump section \"" + SecName +
398                                            "\": it has no contents",
399                                        object_error::parse_failed);
400       Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
401           FileOutputBuffer::create(Filename, Sec.OriginalData.size());
402       if (!BufferOrErr)
403         return BufferOrErr.takeError();
404       std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
405       std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
406                 Buf->getBufferStart());
407       if (Error E = Buf->commit())
408         return E;
409       return Error::success();
410     }
411   }
412   return make_error<StringError>("Section not found",
413                                  object_error::parse_failed);
414 }
415 
416 // This function handles the high level operations of GNU objcopy including
417 // handling command line options. It's important to outline certain properties
418 // we expect to hold of the command line operations. Any operation that "keeps"
419 // should keep regardless of a remove. Additionally any removal should respect
420 // any previous removals. Lastly whether or not something is removed shouldn't
421 // depend a) on the order the options occur in or b) on some opaque priority
422 // system. The only priority is that keeps/copies overrule removes.
423 static void handleArgs(const CopyConfig &Config, Object &Obj,
424                        const Reader &Reader, ElfType OutputElfType) {
425 
426   if (!Config.SplitDWO.empty()) {
427     splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
428   }
429 
430   // TODO: update or remove symbols only if there is an option that affects
431   // them.
432   if (Obj.SymbolTable) {
433     Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
434       if ((Config.LocalizeHidden &&
435            (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
436           (!Config.SymbolsToLocalize.empty() &&
437            is_contained(Config.SymbolsToLocalize, Sym.Name)))
438         Sym.Binding = STB_LOCAL;
439 
440       // Note: these two globalize flags have very similar names but different
441       // meanings:
442       //
443       // --globalize-symbol: promote a symbol to global
444       // --keep-global-symbol: all symbols except for these should be made local
445       //
446       // If --globalize-symbol is specified for a given symbol, it will be
447       // global in the output file even if it is not included via
448       // --keep-global-symbol. Because of that, make sure to check
449       // --globalize-symbol second.
450       if (!Config.SymbolsToKeepGlobal.empty() &&
451           !is_contained(Config.SymbolsToKeepGlobal, Sym.Name))
452         Sym.Binding = STB_LOCAL;
453 
454       if (!Config.SymbolsToGlobalize.empty() &&
455           is_contained(Config.SymbolsToGlobalize, Sym.Name))
456         Sym.Binding = STB_GLOBAL;
457 
458       if (!Config.SymbolsToWeaken.empty() &&
459           is_contained(Config.SymbolsToWeaken, Sym.Name) &&
460           Sym.Binding == STB_GLOBAL)
461         Sym.Binding = STB_WEAK;
462 
463       if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
464           Sym.getShndx() != SHN_UNDEF)
465         Sym.Binding = STB_WEAK;
466 
467       const auto I = Config.SymbolsToRename.find(Sym.Name);
468       if (I != Config.SymbolsToRename.end())
469         Sym.Name = I->getValue();
470 
471       if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
472         Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
473     });
474 
475     // The purpose of this loop is to mark symbols referenced by sections
476     // (like GroupSection or RelocationSection). This way, we know which
477     // symbols are still 'needed' and wich are not.
478     if (Config.StripUnneeded) {
479       for (auto &Section : Obj.sections())
480         Section.markSymbols();
481     }
482 
483     Obj.removeSymbols([&](const Symbol &Sym) {
484       if ((!Config.SymbolsToKeep.empty() &&
485            is_contained(Config.SymbolsToKeep, Sym.Name)) ||
486           (Config.KeepFileSymbols && Sym.Type == STT_FILE))
487         return false;
488 
489       if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
490           Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE &&
491           Sym.Type != STT_SECTION)
492         return true;
493 
494       if (Config.StripAll || Config.StripAllGNU)
495         return true;
496 
497       if (!Config.SymbolsToRemove.empty() &&
498           is_contained(Config.SymbolsToRemove, Sym.Name)) {
499         return true;
500       }
501 
502       if (Config.StripUnneeded && !Sym.Referenced &&
503           (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
504           Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
505         return true;
506 
507       return false;
508     });
509   }
510 
511   SectionPred RemovePred = [](const SectionBase &) { return false; };
512 
513   // Removes:
514   if (!Config.ToRemove.empty()) {
515     RemovePred = [&Config](const SectionBase &Sec) {
516       return is_contained(Config.ToRemove, Sec.Name);
517     };
518   }
519 
520   if (Config.StripDWO || !Config.SplitDWO.empty())
521     RemovePred = [RemovePred](const SectionBase &Sec) {
522       return isDWOSection(Sec) || RemovePred(Sec);
523     };
524 
525   if (Config.ExtractDWO)
526     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
527       return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
528     };
529 
530   if (Config.StripAllGNU)
531     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
532       if (RemovePred(Sec))
533         return true;
534       if ((Sec.Flags & SHF_ALLOC) != 0)
535         return false;
536       if (&Sec == Obj.SectionNames)
537         return false;
538       switch (Sec.Type) {
539       case SHT_SYMTAB:
540       case SHT_REL:
541       case SHT_RELA:
542       case SHT_STRTAB:
543         return true;
544       }
545       return isDebugSection(Sec);
546     };
547 
548   if (Config.StripSections) {
549     RemovePred = [RemovePred](const SectionBase &Sec) {
550       return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
551     };
552   }
553 
554   if (Config.StripDebug) {
555     RemovePred = [RemovePred](const SectionBase &Sec) {
556       return RemovePred(Sec) || isDebugSection(Sec);
557     };
558   }
559 
560   if (Config.StripNonAlloc)
561     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
562       if (RemovePred(Sec))
563         return true;
564       if (&Sec == Obj.SectionNames)
565         return false;
566       return (Sec.Flags & SHF_ALLOC) == 0;
567     };
568 
569   if (Config.StripAll)
570     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
571       if (RemovePred(Sec))
572         return true;
573       if (&Sec == Obj.SectionNames)
574         return false;
575       if (Sec.Name.startswith(".gnu.warning"))
576         return false;
577       return (Sec.Flags & SHF_ALLOC) == 0;
578     };
579 
580   // Explicit copies:
581   if (!Config.OnlyKeep.empty()) {
582     RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
583       // Explicitly keep these sections regardless of previous removes.
584       if (is_contained(Config.OnlyKeep, Sec.Name))
585         return false;
586 
587       // Allow all implicit removes.
588       if (RemovePred(Sec))
589         return true;
590 
591       // Keep special sections.
592       if (Obj.SectionNames == &Sec)
593         return false;
594       if (Obj.SymbolTable == &Sec ||
595           (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
596         return false;
597 
598       // Remove everything else.
599       return true;
600     };
601   }
602 
603   if (!Config.Keep.empty()) {
604     RemovePred = [Config, RemovePred](const SectionBase &Sec) {
605       // Explicitly keep these sections regardless of previous removes.
606       if (is_contained(Config.Keep, Sec.Name))
607         return false;
608       // Otherwise defer to RemovePred.
609       return RemovePred(Sec);
610     };
611   }
612 
613   // This has to be the last predicate assignment.
614   // If the option --keep-symbol has been specified
615   // and at least one of those symbols is present
616   // (equivalently, the updated symbol table is not empty)
617   // the symbol table and the string table should not be removed.
618   if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
619       Obj.SymbolTable && !Obj.SymbolTable->empty()) {
620     RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
621       if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
622         return false;
623       return RemovePred(Sec);
624     };
625   }
626 
627   Obj.removeSections(RemovePred);
628 
629   if (!Config.SectionsToRename.empty()) {
630     for (auto &Sec : Obj.sections()) {
631       const auto Iter = Config.SectionsToRename.find(Sec.Name);
632       if (Iter != Config.SectionsToRename.end()) {
633         const SectionRename &SR = Iter->second;
634         Sec.Name = SR.NewName;
635         if (SR.NewFlags.hasValue()) {
636           // Preserve some flags which should not be dropped when setting flags.
637           // Also, preserve anything OS/processor dependant.
638           const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE |
639                                         ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
640                                         ELF::SHF_MASKOS | ELF::SHF_MASKPROC |
641                                         ELF::SHF_TLS | ELF::SHF_INFO_LINK;
642           Sec.Flags = (Sec.Flags & PreserveMask) |
643                       (SR.NewFlags.getValue() & ~PreserveMask);
644         }
645       }
646     }
647   }
648 
649   if (!Config.AddSection.empty()) {
650     for (const auto &Flag : Config.AddSection) {
651       auto SecPair = Flag.split("=");
652       auto SecName = SecPair.first;
653       auto File = SecPair.second;
654       auto BufOrErr = MemoryBuffer::getFile(File);
655       if (!BufOrErr)
656         reportError(File, BufOrErr.getError());
657       auto Buf = std::move(*BufOrErr);
658       auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
659       auto BufSize = Buf->getBufferSize();
660       Obj.addSection<OwnedDataSection>(SecName,
661                                        ArrayRef<uint8_t>(BufPtr, BufSize));
662     }
663   }
664 
665   if (!Config.DumpSection.empty()) {
666     for (const auto &Flag : Config.DumpSection) {
667       std::pair<StringRef, StringRef> SecPair = Flag.split("=");
668       StringRef SecName = SecPair.first;
669       StringRef File = SecPair.second;
670       if (Error E = dumpSectionToFile(SecName, File, Obj))
671         reportError(Config.InputFilename, std::move(E));
672     }
673   }
674 
675   if (!Config.AddGnuDebugLink.empty())
676     Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
677 }
678 
679 static void executeElfObjcopyOnBinary(const CopyConfig &Config, Reader &Reader,
680                                       Buffer &Out, ElfType OutputElfType) {
681   std::unique_ptr<Object> Obj = Reader.create();
682 
683   handleArgs(Config, *Obj, Reader, OutputElfType);
684 
685   std::unique_ptr<Writer> Writer =
686       createWriter(Config, *Obj, Out, OutputElfType);
687   Writer->finalize();
688   Writer->write();
689 }
690 
691 // For regular archives this function simply calls llvm::writeArchive,
692 // For thin archives it writes the archive file itself as well as its members.
693 static Error deepWriteArchive(StringRef ArcName,
694                               ArrayRef<NewArchiveMember> NewMembers,
695                               bool WriteSymtab, object::Archive::Kind Kind,
696                               bool Deterministic, bool Thin) {
697   Error E =
698       writeArchive(ArcName, NewMembers, WriteSymtab, Kind, Deterministic, Thin);
699   if (!Thin || E)
700     return E;
701   for (const NewArchiveMember &Member : NewMembers) {
702     // Internally, FileBuffer will use the buffer created by
703     // FileOutputBuffer::create, for regular files (that is the case for
704     // deepWriteArchive) FileOutputBuffer::create will return OnDiskBuffer.
705     // OnDiskBuffer uses a temporary file and then renames it. So in reality
706     // there is no inefficiency / duplicated in-memory buffers in this case. For
707     // now in-memory buffers can not be completely avoided since
708     // NewArchiveMember still requires them even though writeArchive does not
709     // write them on disk.
710     FileBuffer FB(Member.MemberName);
711     FB.allocate(Member.Buf->getBufferSize());
712     std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(),
713               FB.getBufferStart());
714     if (auto E = FB.commit())
715       return E;
716   }
717   return Error::success();
718 }
719 
720 static void executeElfObjcopyOnArchive(const CopyConfig &Config,
721                                        const Archive &Ar) {
722   std::vector<NewArchiveMember> NewArchiveMembers;
723   Error Err = Error::success();
724   for (const Archive::Child &Child : Ar.children(Err)) {
725     Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
726     if (!ChildOrErr)
727       reportError(Ar.getFileName(), ChildOrErr.takeError());
728     Binary *Bin = ChildOrErr->get();
729 
730     Expected<StringRef> ChildNameOrErr = Child.getName();
731     if (!ChildNameOrErr)
732       reportError(Ar.getFileName(), ChildNameOrErr.takeError());
733 
734     MemBuffer MB(ChildNameOrErr.get());
735     ELFReader Reader(Bin);
736     executeElfObjcopyOnBinary(Config, Reader, MB, getOutputElfType(*Bin));
737 
738     Expected<NewArchiveMember> Member =
739         NewArchiveMember::getOldMember(Child, true);
740     if (!Member)
741       reportError(Ar.getFileName(), Member.takeError());
742     Member->Buf = MB.releaseMemoryBuffer();
743     Member->MemberName = Member->Buf->getBufferIdentifier();
744     NewArchiveMembers.push_back(std::move(*Member));
745   }
746 
747   if (Err)
748     reportError(Config.InputFilename, std::move(Err));
749   if (Error E =
750           deepWriteArchive(Config.OutputFilename, NewArchiveMembers,
751                            Ar.hasSymbolTable(), Ar.kind(), true, Ar.isThin()))
752     reportError(Config.OutputFilename, std::move(E));
753 }
754 
755 static void restoreDateOnFile(StringRef Filename,
756                               const sys::fs::file_status &Stat) {
757   int FD;
758 
759   if (auto EC =
760           sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
761     reportError(Filename, EC);
762 
763   if (auto EC = sys::fs::setLastAccessAndModificationTime(
764           FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
765     reportError(Filename, EC);
766 
767   if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
768     reportError(Filename, EC);
769 }
770 
771 static void executeElfObjcopy(const CopyConfig &Config) {
772   sys::fs::file_status Stat;
773   if (Config.PreserveDates)
774     if (auto EC = sys::fs::status(Config.InputFilename, Stat))
775       reportError(Config.InputFilename, EC);
776 
777   if (Config.InputFormat == "binary") {
778     auto BufOrErr = MemoryBuffer::getFile(Config.InputFilename);
779     if (!BufOrErr)
780       reportError(Config.InputFilename, BufOrErr.getError());
781 
782     FileBuffer FB(Config.OutputFilename);
783     BinaryReader Reader(Config.BinaryArch, BufOrErr->get());
784     executeElfObjcopyOnBinary(Config, Reader, FB,
785                               getOutputElfType(Config.BinaryArch));
786   } else {
787     Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
788         createBinary(Config.InputFilename);
789     if (!BinaryOrErr)
790       reportError(Config.InputFilename, BinaryOrErr.takeError());
791 
792     if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) {
793       executeElfObjcopyOnArchive(Config, *Ar);
794     } else {
795       FileBuffer FB(Config.OutputFilename);
796       Binary *Bin = BinaryOrErr.get().getBinary();
797       ELFReader Reader(Bin);
798       executeElfObjcopyOnBinary(Config, Reader, FB, getOutputElfType(*Bin));
799     }
800   }
801 
802   if (Config.PreserveDates) {
803     restoreDateOnFile(Config.OutputFilename, Stat);
804     if (!Config.SplitDWO.empty())
805       restoreDateOnFile(Config.SplitDWO, Stat);
806   }
807 }
808 
809 static void addGlobalSymbolsFromFile(std::vector<std::string> &Symbols,
810                                      StringRef Filename) {
811   SmallVector<StringRef, 16> Lines;
812   auto BufOrErr = MemoryBuffer::getFile(Filename);
813   if (!BufOrErr)
814     reportError(Filename, BufOrErr.getError());
815 
816   BufOrErr.get()->getBuffer().split(Lines, '\n');
817   for (StringRef Line : Lines) {
818     // Ignore everything after '#', trim whitespace, and only add the symbol if
819     // it's not empty.
820     auto TrimmedLine = Line.split('#').first.trim();
821     if (!TrimmedLine.empty())
822       Symbols.push_back(TrimmedLine.str());
823   }
824 }
825 
826 // ParseObjcopyOptions returns the config and sets the input arguments. If a
827 // help flag is set then ParseObjcopyOptions will print the help messege and
828 // exit.
829 static DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
830   ObjcopyOptTable T;
831   unsigned MissingArgumentIndex, MissingArgumentCount;
832   llvm::opt::InputArgList InputArgs =
833       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
834 
835   if (InputArgs.size() == 0) {
836     T.PrintHelp(errs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
837     exit(1);
838   }
839 
840   if (InputArgs.hasArg(OBJCOPY_help)) {
841     T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
842     exit(0);
843   }
844 
845   SmallVector<const char *, 2> Positional;
846 
847   for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
848     error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
849 
850   for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
851     Positional.push_back(Arg->getValue());
852 
853   if (Positional.empty())
854     error("No input file specified");
855 
856   if (Positional.size() > 2)
857     error("Too many positional arguments");
858 
859   CopyConfig Config;
860   Config.InputFilename = Positional[0];
861   Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
862   Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
863   Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
864   if (Config.InputFormat == "binary") {
865     auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
866     if (BinaryArch.empty())
867       error("Specified binary input without specifiying an architecture");
868     Config.BinaryArch = getMachineInfo(BinaryArch);
869   }
870 
871   Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
872   Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
873   Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
874 
875   for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
876     if (!StringRef(Arg->getValue()).contains('='))
877       error("Bad format for --redefine-sym");
878     auto Old2New = StringRef(Arg->getValue()).split('=');
879     if (!Config.SymbolsToRename.insert(Old2New).second)
880       error("Multiple redefinition of symbol " + Old2New.first);
881   }
882 
883   for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) {
884     SectionRename SR = parseRenameSectionValue(StringRef(Arg->getValue()));
885     if (!Config.SectionsToRename.try_emplace(SR.OriginalName, SR).second)
886       error("Multiple renames of section " + SR.OriginalName);
887   }
888 
889   for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
890     Config.ToRemove.push_back(Arg->getValue());
891   for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
892     Config.Keep.push_back(Arg->getValue());
893   for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
894     Config.OnlyKeep.push_back(Arg->getValue());
895   for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
896     Config.AddSection.push_back(Arg->getValue());
897   for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section))
898     Config.DumpSection.push_back(Arg->getValue());
899   Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
900   Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
901   Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
902   Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
903   Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
904   Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
905   Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded);
906   Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
907   Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
908   Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
909   Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all);
910   Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
911   Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
912   for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
913     Config.SymbolsToLocalize.push_back(Arg->getValue());
914   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol))
915     Config.SymbolsToKeepGlobal.push_back(Arg->getValue());
916   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
917     addGlobalSymbolsFromFile(Config.SymbolsToKeepGlobal, Arg->getValue());
918   for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
919     Config.SymbolsToGlobalize.push_back(Arg->getValue());
920   for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
921     Config.SymbolsToWeaken.push_back(Arg->getValue());
922   for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
923     Config.SymbolsToRemove.push_back(Arg->getValue());
924   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
925     Config.SymbolsToKeep.push_back(Arg->getValue());
926 
927   Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
928 
929   DriverConfig DC;
930   DC.CopyConfigs.push_back(std::move(Config));
931   return DC;
932 }
933 
934 // ParseStripOptions returns the config and sets the input arguments. If a
935 // help flag is set then ParseStripOptions will print the help messege and
936 // exit.
937 static DriverConfig parseStripOptions(ArrayRef<const char *> ArgsArr) {
938   StripOptTable T;
939   unsigned MissingArgumentIndex, MissingArgumentCount;
940   llvm::opt::InputArgList InputArgs =
941       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
942 
943   if (InputArgs.size() == 0) {
944     T.PrintHelp(errs(), "llvm-strip", "strip tool");
945     exit(1);
946   }
947 
948   if (InputArgs.hasArg(STRIP_help)) {
949     T.PrintHelp(outs(), "llvm-strip", "strip tool");
950     exit(0);
951   }
952 
953   SmallVector<const char *, 2> Positional;
954   for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
955     error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
956   for (auto Arg : InputArgs.filtered(STRIP_INPUT))
957     Positional.push_back(Arg->getValue());
958 
959   if (Positional.empty())
960     error("No input file specified");
961 
962   if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output))
963     error("Multiple input files cannot be used in combination with -o");
964 
965   CopyConfig Config;
966   Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
967 
968   Config.DiscardAll = InputArgs.hasArg(STRIP_discard_all);
969   Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded);
970   Config.StripAll = InputArgs.hasArg(STRIP_strip_all);
971 
972   if (!Config.StripDebug && !Config.StripUnneeded && !Config.DiscardAll)
973     Config.StripAll = true;
974 
975   for (auto Arg : InputArgs.filtered(STRIP_remove_section))
976     Config.ToRemove.push_back(Arg->getValue());
977 
978   for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
979     Config.SymbolsToKeep.push_back(Arg->getValue());
980 
981   Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates);
982 
983   DriverConfig DC;
984   if (Positional.size() == 1) {
985     Config.InputFilename = Positional[0];
986     Config.OutputFilename =
987         InputArgs.getLastArgValue(STRIP_output, Positional[0]);
988     DC.CopyConfigs.push_back(std::move(Config));
989   } else {
990     for (const char *Filename : Positional) {
991       Config.InputFilename = Filename;
992       Config.OutputFilename = Filename;
993       DC.CopyConfigs.push_back(Config);
994     }
995   }
996 
997   return DC;
998 }
999 
1000 int main(int argc, char **argv) {
1001   InitLLVM X(argc, argv);
1002   ToolName = argv[0];
1003   DriverConfig DriverConfig;
1004   if (sys::path::stem(ToolName).endswith_lower("strip"))
1005     DriverConfig = parseStripOptions(makeArrayRef(argv + 1, argc));
1006   else
1007     DriverConfig = parseObjcopyOptions(makeArrayRef(argv + 1, argc));
1008   for (const CopyConfig &CopyConfig : DriverConfig.CopyConfigs)
1009     executeElfObjcopy(CopyConfig);
1010 }
1011