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