1 //===- ConfigManager.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ConfigManager.h"
10 #include "llvm/ADT/Optional.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/StringSet.h"
14 #include "llvm/Option/Arg.h"
15 #include "llvm/Option/ArgList.h"
16 #include "llvm/Support/CRC.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Compression.h"
19 #include "llvm/Support/Errc.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/StringSaver.h"
23 #include <memory>
24 
25 using namespace llvm;
26 using namespace llvm::objcopy;
27 
28 namespace {
29 enum ObjcopyID {
30   OBJCOPY_INVALID = 0, // This is not an option ID.
31 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
32                HELPTEXT, METAVAR, VALUES)                                      \
33   OBJCOPY_##ID,
34 #include "ObjcopyOpts.inc"
35 #undef OPTION
36 };
37 
38 #define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE;
39 #include "ObjcopyOpts.inc"
40 #undef PREFIX
41 
42 static const opt::OptTable::Info ObjcopyInfoTable[] = {
43 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
44                HELPTEXT, METAVAR, VALUES)                                      \
45   {OBJCOPY_##PREFIX,                                                           \
46    NAME,                                                                       \
47    HELPTEXT,                                                                   \
48    METAVAR,                                                                    \
49    OBJCOPY_##ID,                                                               \
50    opt::Option::KIND##Class,                                                   \
51    PARAM,                                                                      \
52    FLAGS,                                                                      \
53    OBJCOPY_##GROUP,                                                            \
54    OBJCOPY_##ALIAS,                                                            \
55    ALIASARGS,                                                                  \
56    VALUES},
57 #include "ObjcopyOpts.inc"
58 #undef OPTION
59 };
60 
61 class ObjcopyOptTable : public opt::OptTable {
62 public:
ObjcopyOptTable()63   ObjcopyOptTable() : OptTable(ObjcopyInfoTable) {
64     setGroupedShortOptions(true);
65   }
66 };
67 
68 enum InstallNameToolID {
69   INSTALL_NAME_TOOL_INVALID = 0, // This is not an option ID.
70 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
71                HELPTEXT, METAVAR, VALUES)                                      \
72   INSTALL_NAME_TOOL_##ID,
73 #include "InstallNameToolOpts.inc"
74 #undef OPTION
75 };
76 
77 #define PREFIX(NAME, VALUE)                                                    \
78   const char *const INSTALL_NAME_TOOL_##NAME[] = VALUE;
79 #include "InstallNameToolOpts.inc"
80 #undef PREFIX
81 
82 static const opt::OptTable::Info InstallNameToolInfoTable[] = {
83 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
84                HELPTEXT, METAVAR, VALUES)                                      \
85   {INSTALL_NAME_TOOL_##PREFIX,                                                 \
86    NAME,                                                                       \
87    HELPTEXT,                                                                   \
88    METAVAR,                                                                    \
89    INSTALL_NAME_TOOL_##ID,                                                     \
90    opt::Option::KIND##Class,                                                   \
91    PARAM,                                                                      \
92    FLAGS,                                                                      \
93    INSTALL_NAME_TOOL_##GROUP,                                                  \
94    INSTALL_NAME_TOOL_##ALIAS,                                                  \
95    ALIASARGS,                                                                  \
96    VALUES},
97 #include "InstallNameToolOpts.inc"
98 #undef OPTION
99 };
100 
101 class InstallNameToolOptTable : public opt::OptTable {
102 public:
InstallNameToolOptTable()103   InstallNameToolOptTable() : OptTable(InstallNameToolInfoTable) {}
104 };
105 
106 enum BitcodeStripID {
107   BITCODE_STRIP_INVALID = 0, // This is not an option ID.
108 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
109                HELPTEXT, METAVAR, VALUES)                                      \
110   BITCODE_STRIP_##ID,
111 #include "BitcodeStripOpts.inc"
112 #undef OPTION
113 };
114 
115 #define PREFIX(NAME, VALUE) const char *const BITCODE_STRIP_##NAME[] = VALUE;
116 #include "BitcodeStripOpts.inc"
117 #undef PREFIX
118 
119 static const opt::OptTable::Info BitcodeStripInfoTable[] = {
120 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
121                HELPTEXT, METAVAR, VALUES)                                      \
122   {BITCODE_STRIP_##PREFIX,                                                     \
123    NAME,                                                                       \
124    HELPTEXT,                                                                   \
125    METAVAR,                                                                    \
126    BITCODE_STRIP_##ID,                                                         \
127    opt::Option::KIND##Class,                                                   \
128    PARAM,                                                                      \
129    FLAGS,                                                                      \
130    BITCODE_STRIP_##GROUP,                                                      \
131    BITCODE_STRIP_##ALIAS,                                                      \
132    ALIASARGS,                                                                  \
133    VALUES},
134 #include "BitcodeStripOpts.inc"
135 #undef OPTION
136 };
137 
138 class BitcodeStripOptTable : public opt::OptTable {
139 public:
BitcodeStripOptTable()140   BitcodeStripOptTable() : OptTable(BitcodeStripInfoTable) {}
141 };
142 
143 enum StripID {
144   STRIP_INVALID = 0, // This is not an option ID.
145 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
146                HELPTEXT, METAVAR, VALUES)                                      \
147   STRIP_##ID,
148 #include "StripOpts.inc"
149 #undef OPTION
150 };
151 
152 #define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE;
153 #include "StripOpts.inc"
154 #undef PREFIX
155 
156 static const opt::OptTable::Info StripInfoTable[] = {
157 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
158                HELPTEXT, METAVAR, VALUES)                                      \
159   {STRIP_##PREFIX, NAME,       HELPTEXT,                                       \
160    METAVAR,        STRIP_##ID, opt::Option::KIND##Class,                       \
161    PARAM,          FLAGS,      STRIP_##GROUP,                                  \
162    STRIP_##ALIAS,  ALIASARGS,  VALUES},
163 #include "StripOpts.inc"
164 #undef OPTION
165 };
166 
167 class StripOptTable : public opt::OptTable {
168 public:
StripOptTable()169   StripOptTable() : OptTable(StripInfoTable) { setGroupedShortOptions(true); }
170 };
171 
172 } // namespace
173 
parseSectionRenameFlag(StringRef SectionName)174 static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
175   return llvm::StringSwitch<SectionFlag>(SectionName)
176       .CaseLower("alloc", SectionFlag::SecAlloc)
177       .CaseLower("load", SectionFlag::SecLoad)
178       .CaseLower("noload", SectionFlag::SecNoload)
179       .CaseLower("readonly", SectionFlag::SecReadonly)
180       .CaseLower("debug", SectionFlag::SecDebug)
181       .CaseLower("code", SectionFlag::SecCode)
182       .CaseLower("data", SectionFlag::SecData)
183       .CaseLower("rom", SectionFlag::SecRom)
184       .CaseLower("merge", SectionFlag::SecMerge)
185       .CaseLower("strings", SectionFlag::SecStrings)
186       .CaseLower("contents", SectionFlag::SecContents)
187       .CaseLower("share", SectionFlag::SecShare)
188       .CaseLower("exclude", SectionFlag::SecExclude)
189       .Default(SectionFlag::SecNone);
190 }
191 
192 static Expected<SectionFlag>
parseSectionFlagSet(ArrayRef<StringRef> SectionFlags)193 parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
194   SectionFlag ParsedFlags = SectionFlag::SecNone;
195   for (StringRef Flag : SectionFlags) {
196     SectionFlag ParsedFlag = parseSectionRenameFlag(Flag);
197     if (ParsedFlag == SectionFlag::SecNone)
198       return createStringError(
199           errc::invalid_argument,
200           "unrecognized section flag '%s'. Flags supported for GNU "
201           "compatibility: alloc, load, noload, readonly, exclude, debug, "
202           "code, data, rom, share, contents, merge, strings",
203           Flag.str().c_str());
204     ParsedFlags |= ParsedFlag;
205   }
206 
207   return ParsedFlags;
208 }
209 
parseRenameSectionValue(StringRef FlagValue)210 static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
211   if (!FlagValue.contains('='))
212     return createStringError(errc::invalid_argument,
213                              "bad format for --rename-section: missing '='");
214 
215   // Initial split: ".foo" = ".bar,f1,f2,..."
216   auto Old2New = FlagValue.split('=');
217   SectionRename SR;
218   SR.OriginalName = Old2New.first;
219 
220   // Flags split: ".bar" "f1" "f2" ...
221   SmallVector<StringRef, 6> NameAndFlags;
222   Old2New.second.split(NameAndFlags, ',');
223   SR.NewName = NameAndFlags[0];
224 
225   if (NameAndFlags.size() > 1) {
226     Expected<SectionFlag> ParsedFlagSet =
227         parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front());
228     if (!ParsedFlagSet)
229       return ParsedFlagSet.takeError();
230     SR.NewFlags = *ParsedFlagSet;
231   }
232 
233   return SR;
234 }
235 
236 static Expected<std::pair<StringRef, uint64_t>>
parseSetSectionAlignment(StringRef FlagValue)237 parseSetSectionAlignment(StringRef FlagValue) {
238   if (!FlagValue.contains('='))
239     return createStringError(
240         errc::invalid_argument,
241         "bad format for --set-section-alignment: missing '='");
242   auto Split = StringRef(FlagValue).split('=');
243   if (Split.first.empty())
244     return createStringError(
245         errc::invalid_argument,
246         "bad format for --set-section-alignment: missing section name");
247   uint64_t NewAlign;
248   if (Split.second.getAsInteger(0, NewAlign))
249     return createStringError(
250         errc::invalid_argument,
251         "invalid alignment for --set-section-alignment: '%s'",
252         Split.second.str().c_str());
253   return std::make_pair(Split.first, NewAlign);
254 }
255 
256 static Expected<SectionFlagsUpdate>
parseSetSectionFlagValue(StringRef FlagValue)257 parseSetSectionFlagValue(StringRef FlagValue) {
258   if (!StringRef(FlagValue).contains('='))
259     return createStringError(errc::invalid_argument,
260                              "bad format for --set-section-flags: missing '='");
261 
262   // Initial split: ".foo" = "f1,f2,..."
263   auto Section2Flags = StringRef(FlagValue).split('=');
264   SectionFlagsUpdate SFU;
265   SFU.Name = Section2Flags.first;
266 
267   // Flags split: "f1" "f2" ...
268   SmallVector<StringRef, 6> SectionFlags;
269   Section2Flags.second.split(SectionFlags, ',');
270   Expected<SectionFlag> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
271   if (!ParsedFlagSet)
272     return ParsedFlagSet.takeError();
273   SFU.NewFlags = *ParsedFlagSet;
274 
275   return SFU;
276 }
277 
278 namespace {
279 struct TargetInfo {
280   FileFormat Format;
281   MachineInfo Machine;
282 };
283 } // namespace
284 
285 // FIXME: consolidate with the bfd parsing used by lld.
286 static const StringMap<MachineInfo> TargetMap{
287     // Name, {EMachine, 64bit, LittleEndian}
288     // x86
289     {"elf32-i386", {ELF::EM_386, false, true}},
290     {"elf32-x86-64", {ELF::EM_X86_64, false, true}},
291     {"elf64-x86-64", {ELF::EM_X86_64, true, true}},
292     // Intel MCU
293     {"elf32-iamcu", {ELF::EM_IAMCU, false, true}},
294     // ARM
295     {"elf32-littlearm", {ELF::EM_ARM, false, true}},
296     // ARM AArch64
297     {"elf64-aarch64", {ELF::EM_AARCH64, true, true}},
298     {"elf64-littleaarch64", {ELF::EM_AARCH64, true, true}},
299     // RISC-V
300     {"elf32-littleriscv", {ELF::EM_RISCV, false, true}},
301     {"elf64-littleriscv", {ELF::EM_RISCV, true, true}},
302     // PowerPC
303     {"elf32-powerpc", {ELF::EM_PPC, false, false}},
304     {"elf32-powerpcle", {ELF::EM_PPC, false, true}},
305     {"elf64-powerpc", {ELF::EM_PPC64, true, false}},
306     {"elf64-powerpcle", {ELF::EM_PPC64, true, true}},
307     // MIPS
308     {"elf32-bigmips", {ELF::EM_MIPS, false, false}},
309     {"elf32-ntradbigmips", {ELF::EM_MIPS, false, false}},
310     {"elf32-ntradlittlemips", {ELF::EM_MIPS, false, true}},
311     {"elf32-tradbigmips", {ELF::EM_MIPS, false, false}},
312     {"elf32-tradlittlemips", {ELF::EM_MIPS, false, true}},
313     {"elf64-tradbigmips", {ELF::EM_MIPS, true, false}},
314     {"elf64-tradlittlemips", {ELF::EM_MIPS, true, true}},
315     // SPARC
316     {"elf32-sparc", {ELF::EM_SPARC, false, false}},
317     {"elf32-sparcel", {ELF::EM_SPARC, false, true}},
318     {"elf32-hexagon", {ELF::EM_HEXAGON, false, true}},
319 };
320 
321 static Expected<TargetInfo>
getOutputTargetInfoByTargetName(StringRef TargetName)322 getOutputTargetInfoByTargetName(StringRef TargetName) {
323   StringRef OriginalTargetName = TargetName;
324   bool IsFreeBSD = TargetName.consume_back("-freebsd");
325   auto Iter = TargetMap.find(TargetName);
326   if (Iter == std::end(TargetMap))
327     return createStringError(errc::invalid_argument,
328                              "invalid output format: '%s'",
329                              OriginalTargetName.str().c_str());
330   MachineInfo MI = Iter->getValue();
331   if (IsFreeBSD)
332     MI.OSABI = ELF::ELFOSABI_FREEBSD;
333 
334   FileFormat Format;
335   if (TargetName.startswith("elf"))
336     Format = FileFormat::ELF;
337   else
338     // This should never happen because `TargetName` is valid (it certainly
339     // exists in the TargetMap).
340     llvm_unreachable("unknown target prefix");
341 
342   return {TargetInfo{Format, MI}};
343 }
344 
addSymbolsFromFile(NameMatcher & Symbols,BumpPtrAllocator & Alloc,StringRef Filename,MatchStyle MS,function_ref<Error (Error)> ErrorCallback)345 static Error addSymbolsFromFile(NameMatcher &Symbols, BumpPtrAllocator &Alloc,
346                                 StringRef Filename, MatchStyle MS,
347                                 function_ref<Error(Error)> ErrorCallback) {
348   StringSaver Saver(Alloc);
349   SmallVector<StringRef, 16> Lines;
350   auto BufOrErr = MemoryBuffer::getFile(Filename);
351   if (!BufOrErr)
352     return createFileError(Filename, BufOrErr.getError());
353 
354   BufOrErr.get()->getBuffer().split(Lines, '\n');
355   for (StringRef Line : Lines) {
356     // Ignore everything after '#', trim whitespace, and only add the symbol if
357     // it's not empty.
358     auto TrimmedLine = Line.split('#').first.trim();
359     if (!TrimmedLine.empty())
360       if (Error E = Symbols.addMatcher(NameOrPattern::create(
361               Saver.save(TrimmedLine), MS, ErrorCallback)))
362         return E;
363   }
364 
365   return Error::success();
366 }
367 
368 Expected<NameOrPattern>
create(StringRef Pattern,MatchStyle MS,function_ref<Error (Error)> ErrorCallback)369 NameOrPattern::create(StringRef Pattern, MatchStyle MS,
370                       function_ref<Error(Error)> ErrorCallback) {
371   switch (MS) {
372   case MatchStyle::Literal:
373     return NameOrPattern(Pattern);
374   case MatchStyle::Wildcard: {
375     SmallVector<char, 32> Data;
376     bool IsPositiveMatch = true;
377     if (Pattern[0] == '!') {
378       IsPositiveMatch = false;
379       Pattern = Pattern.drop_front();
380     }
381     Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pattern);
382 
383     // If we couldn't create it as a glob, report the error, but try again with
384     // a literal if the error reporting is non-fatal.
385     if (!GlobOrErr) {
386       if (Error E = ErrorCallback(GlobOrErr.takeError()))
387         return std::move(E);
388       return create(Pattern, MatchStyle::Literal, ErrorCallback);
389     }
390 
391     return NameOrPattern(std::make_shared<GlobPattern>(*GlobOrErr),
392                          IsPositiveMatch);
393   }
394   case MatchStyle::Regex: {
395     SmallVector<char, 32> Data;
396     return NameOrPattern(std::make_shared<Regex>(
397         ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));
398   }
399   }
400   llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");
401 }
402 
addSymbolsToRenameFromFile(StringMap<StringRef> & SymbolsToRename,BumpPtrAllocator & Alloc,StringRef Filename)403 static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename,
404                                         BumpPtrAllocator &Alloc,
405                                         StringRef Filename) {
406   StringSaver Saver(Alloc);
407   SmallVector<StringRef, 16> Lines;
408   auto BufOrErr = MemoryBuffer::getFile(Filename);
409   if (!BufOrErr)
410     return createFileError(Filename, BufOrErr.getError());
411 
412   BufOrErr.get()->getBuffer().split(Lines, '\n');
413   size_t NumLines = Lines.size();
414   for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) {
415     StringRef TrimmedLine = Lines[LineNo].split('#').first.trim();
416     if (TrimmedLine.empty())
417       continue;
418 
419     std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' ');
420     StringRef NewName = Pair.second.trim();
421     if (NewName.empty())
422       return createStringError(errc::invalid_argument,
423                                "%s:%zu: missing new symbol name",
424                                Filename.str().c_str(), LineNo + 1);
425     SymbolsToRename.insert({Pair.first, NewName});
426   }
427   return Error::success();
428 }
429 
getAsInteger(StringRef Val)430 template <class T> static ErrorOr<T> getAsInteger(StringRef Val) {
431   T Result;
432   if (Val.getAsInteger(0, Result))
433     return errc::invalid_argument;
434   return Result;
435 }
436 
437 namespace {
438 
439 enum class ToolType { Objcopy, Strip, InstallNameTool, BitcodeStrip };
440 
441 } // anonymous namespace
442 
printHelp(const opt::OptTable & OptTable,raw_ostream & OS,ToolType Tool)443 static void printHelp(const opt::OptTable &OptTable, raw_ostream &OS,
444                       ToolType Tool) {
445   StringRef HelpText, ToolName;
446   switch (Tool) {
447   case ToolType::Objcopy:
448     ToolName = "llvm-objcopy";
449     HelpText = " [options] input [output]";
450     break;
451   case ToolType::Strip:
452     ToolName = "llvm-strip";
453     HelpText = " [options] inputs...";
454     break;
455   case ToolType::InstallNameTool:
456     ToolName = "llvm-install-name-tool";
457     HelpText = " [options] input";
458     break;
459   case ToolType::BitcodeStrip:
460     ToolName = "llvm-bitcode-strip";
461     HelpText = " [options] input";
462     break;
463   }
464   OptTable.printHelp(OS, (ToolName + HelpText).str().c_str(),
465                      (ToolName + " tool").str().c_str());
466   // TODO: Replace this with libOption call once it adds extrahelp support.
467   // The CommandLine library has a cl::extrahelp class to support this,
468   // but libOption does not have that yet.
469   OS << "\nPass @FILE as argument to read options from FILE.\n";
470 }
471 
parseNewSymbolInfo(StringRef FlagValue)472 static Expected<NewSymbolInfo> parseNewSymbolInfo(StringRef FlagValue) {
473   // Parse value given with --add-symbol option and create the
474   // new symbol if possible. The value format for --add-symbol is:
475   //
476   // <name>=[<section>:]<value>[,<flags>]
477   //
478   // where:
479   // <name> - symbol name, can be empty string
480   // <section> - optional section name. If not given ABS symbol is created
481   // <value> - symbol value, can be decimal or hexadecimal number prefixed
482   //           with 0x.
483   // <flags> - optional flags affecting symbol type, binding or visibility.
484   NewSymbolInfo SI;
485   StringRef Value;
486   std::tie(SI.SymbolName, Value) = FlagValue.split('=');
487   if (Value.empty())
488     return createStringError(
489         errc::invalid_argument,
490         "bad format for --add-symbol, missing '=' after '%s'",
491         SI.SymbolName.str().c_str());
492 
493   if (Value.contains(':')) {
494     std::tie(SI.SectionName, Value) = Value.split(':');
495     if (SI.SectionName.empty() || Value.empty())
496       return createStringError(
497           errc::invalid_argument,
498           "bad format for --add-symbol, missing section name or symbol value");
499   }
500 
501   SmallVector<StringRef, 6> Flags;
502   Value.split(Flags, ',');
503   if (Flags[0].getAsInteger(0, SI.Value))
504     return createStringError(errc::invalid_argument, "bad symbol value: '%s'",
505                              Flags[0].str().c_str());
506 
507   using Functor = std::function<void()>;
508   SmallVector<StringRef, 6> UnsupportedFlags;
509   for (size_t I = 1, NumFlags = Flags.size(); I < NumFlags; ++I)
510     static_cast<Functor>(
511         StringSwitch<Functor>(Flags[I])
512             .CaseLower("global",
513                        [&] { SI.Flags.push_back(SymbolFlag::Global); })
514             .CaseLower("local", [&] { SI.Flags.push_back(SymbolFlag::Local); })
515             .CaseLower("weak", [&] { SI.Flags.push_back(SymbolFlag::Weak); })
516             .CaseLower("default",
517                        [&] { SI.Flags.push_back(SymbolFlag::Default); })
518             .CaseLower("hidden",
519                        [&] { SI.Flags.push_back(SymbolFlag::Hidden); })
520             .CaseLower("protected",
521                        [&] { SI.Flags.push_back(SymbolFlag::Protected); })
522             .CaseLower("file", [&] { SI.Flags.push_back(SymbolFlag::File); })
523             .CaseLower("section",
524                        [&] { SI.Flags.push_back(SymbolFlag::Section); })
525             .CaseLower("object",
526                        [&] { SI.Flags.push_back(SymbolFlag::Object); })
527             .CaseLower("function",
528                        [&] { SI.Flags.push_back(SymbolFlag::Function); })
529             .CaseLower(
530                 "indirect-function",
531                 [&] { SI.Flags.push_back(SymbolFlag::IndirectFunction); })
532             .CaseLower("debug", [&] { SI.Flags.push_back(SymbolFlag::Debug); })
533             .CaseLower("constructor",
534                        [&] { SI.Flags.push_back(SymbolFlag::Constructor); })
535             .CaseLower("warning",
536                        [&] { SI.Flags.push_back(SymbolFlag::Warning); })
537             .CaseLower("indirect",
538                        [&] { SI.Flags.push_back(SymbolFlag::Indirect); })
539             .CaseLower("synthetic",
540                        [&] { SI.Flags.push_back(SymbolFlag::Synthetic); })
541             .CaseLower("unique-object",
542                        [&] { SI.Flags.push_back(SymbolFlag::UniqueObject); })
543             .StartsWithLower("before=",
544                              [&] {
545                                StringRef SymNamePart =
546                                    Flags[I].split('=').second;
547 
548                                if (!SymNamePart.empty())
549                                  SI.BeforeSyms.push_back(SymNamePart);
550                              })
551             .Default([&] { UnsupportedFlags.push_back(Flags[I]); }))();
552   if (!UnsupportedFlags.empty())
553     return createStringError(errc::invalid_argument,
554                              "unsupported flag%s for --add-symbol: '%s'",
555                              UnsupportedFlags.size() > 1 ? "s" : "",
556                              join(UnsupportedFlags, "', '").c_str());
557 
558   return SI;
559 }
560 
getELFConfig() const561 Expected<const ELFConfig &> ConfigManager::getELFConfig() const {
562   if (Common.StripSwiftSymbols || Common.KeepUndefined)
563     return createStringError(llvm::errc::invalid_argument,
564                              "option not supported by llvm-objcopy for ELF");
565 
566   return ELF;
567 }
568 
getCOFFConfig() const569 Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
570   if (Common.AllowBrokenLinks || !Common.SplitDWO.empty() ||
571       !Common.SymbolsPrefix.empty() || !Common.AllocSectionsPrefix.empty() ||
572       !Common.DumpSection.empty() || !Common.KeepSection.empty() ||
573       ELF.NewSymbolVisibility || !Common.SymbolsToGlobalize.empty() ||
574       !Common.SymbolsToKeep.empty() || !Common.SymbolsToLocalize.empty() ||
575       !Common.SymbolsToWeaken.empty() || !Common.SymbolsToKeepGlobal.empty() ||
576       !Common.SectionsToRename.empty() || !Common.SetSectionAlignment.empty() ||
577       Common.ExtractDWO || Common.LocalizeHidden || Common.PreserveDates ||
578       Common.StripDWO || Common.StripNonAlloc || Common.StripSections ||
579       Common.StripSwiftSymbols || Common.KeepUndefined || Common.Weaken ||
580       Common.DecompressDebugSections ||
581       Common.DiscardMode == DiscardType::Locals ||
582       !Common.SymbolsToAdd.empty() || Common.EntryExpr) {
583     return createStringError(llvm::errc::invalid_argument,
584                              "option not supported by llvm-objcopy for COFF");
585   }
586 
587   return COFF;
588 }
589 
getMachOConfig() const590 Expected<const MachOConfig &> ConfigManager::getMachOConfig() const {
591   if (Common.AllowBrokenLinks || !Common.SplitDWO.empty() ||
592       !Common.SymbolsPrefix.empty() || !Common.AllocSectionsPrefix.empty() ||
593       !Common.KeepSection.empty() || ELF.NewSymbolVisibility ||
594       !Common.SymbolsToGlobalize.empty() || !Common.SymbolsToKeep.empty() ||
595       !Common.SymbolsToLocalize.empty() || !Common.SymbolsToWeaken.empty() ||
596       !Common.SymbolsToKeepGlobal.empty() || !Common.SectionsToRename.empty() ||
597       !Common.UnneededSymbolsToRemove.empty() ||
598       !Common.SetSectionAlignment.empty() || !Common.SetSectionFlags.empty() ||
599       Common.ExtractDWO || Common.LocalizeHidden || Common.PreserveDates ||
600       Common.StripAllGNU || Common.StripDWO || Common.StripNonAlloc ||
601       Common.StripSections || Common.Weaken || Common.DecompressDebugSections ||
602       Common.StripUnneeded || Common.DiscardMode == DiscardType::Locals ||
603       !Common.SymbolsToAdd.empty() || Common.EntryExpr) {
604     return createStringError(llvm::errc::invalid_argument,
605                              "option not supported by llvm-objcopy for MachO");
606   }
607 
608   return MachO;
609 }
610 
getWasmConfig() const611 Expected<const WasmConfig &> ConfigManager::getWasmConfig() const {
612   if (!Common.AddGnuDebugLink.empty() || Common.ExtractPartition ||
613       !Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
614       !Common.AllocSectionsPrefix.empty() ||
615       Common.DiscardMode != DiscardType::None || ELF.NewSymbolVisibility ||
616       !Common.SymbolsToAdd.empty() || !Common.RPathToAdd.empty() ||
617       !Common.SymbolsToGlobalize.empty() || !Common.SymbolsToLocalize.empty() ||
618       !Common.SymbolsToKeep.empty() || !Common.SymbolsToRemove.empty() ||
619       !Common.UnneededSymbolsToRemove.empty() ||
620       !Common.SymbolsToWeaken.empty() || !Common.SymbolsToKeepGlobal.empty() ||
621       !Common.SectionsToRename.empty() || !Common.SetSectionAlignment.empty() ||
622       !Common.SetSectionFlags.empty() || !Common.SymbolsToRename.empty()) {
623     return createStringError(
624         llvm::errc::invalid_argument,
625         "only flags for section dumping, removal, and addition are supported");
626   }
627 
628   return Wasm;
629 }
630 
631 // ParseObjcopyOptions returns the config and sets the input arguments. If a
632 // help flag is set then ParseObjcopyOptions will print the help messege and
633 // exit.
634 Expected<DriverConfig>
parseObjcopyOptions(ArrayRef<const char * > RawArgsArr,function_ref<Error (Error)> ErrorCallback)635 objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
636                              function_ref<Error(Error)> ErrorCallback) {
637   DriverConfig DC;
638   ObjcopyOptTable T;
639 
640   const char *const *DashDash =
641       std::find_if(RawArgsArr.begin(), RawArgsArr.end(),
642                    [](StringRef Str) { return Str == "--"; });
643   ArrayRef<const char *> ArgsArr = makeArrayRef(RawArgsArr.begin(), DashDash);
644   if (DashDash != RawArgsArr.end())
645     DashDash = std::next(DashDash);
646 
647   unsigned MissingArgumentIndex, MissingArgumentCount;
648   llvm::opt::InputArgList InputArgs =
649       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
650 
651   if (InputArgs.size() == 0 && DashDash == RawArgsArr.end()) {
652     printHelp(T, errs(), ToolType::Objcopy);
653     exit(1);
654   }
655 
656   if (InputArgs.hasArg(OBJCOPY_help)) {
657     printHelp(T, outs(), ToolType::Objcopy);
658     exit(0);
659   }
660 
661   if (InputArgs.hasArg(OBJCOPY_version)) {
662     outs() << "llvm-objcopy, compatible with GNU objcopy\n";
663     cl::PrintVersionMessage();
664     exit(0);
665   }
666 
667   SmallVector<const char *, 2> Positional;
668 
669   for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
670     return createStringError(errc::invalid_argument, "unknown argument '%s'",
671                              Arg->getAsString(InputArgs).c_str());
672 
673   for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
674     Positional.push_back(Arg->getValue());
675   std::copy(DashDash, RawArgsArr.end(), std::back_inserter(Positional));
676 
677   if (Positional.empty())
678     return createStringError(errc::invalid_argument, "no input file specified");
679 
680   if (Positional.size() > 2)
681     return createStringError(errc::invalid_argument,
682                              "too many positional arguments");
683 
684   ConfigManager ConfigMgr;
685   CommonConfig &Config = ConfigMgr.Common;
686   ELFConfig &ELFConfig = ConfigMgr.ELF;
687   Config.InputFilename = Positional[0];
688   Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
689   if (InputArgs.hasArg(OBJCOPY_target) &&
690       (InputArgs.hasArg(OBJCOPY_input_target) ||
691        InputArgs.hasArg(OBJCOPY_output_target)))
692     return createStringError(
693         errc::invalid_argument,
694         "--target cannot be used with --input-target or --output-target");
695 
696   if (InputArgs.hasArg(OBJCOPY_regex) && InputArgs.hasArg(OBJCOPY_wildcard))
697     return createStringError(errc::invalid_argument,
698                              "--regex and --wildcard are incompatible");
699 
700   MatchStyle SectionMatchStyle = InputArgs.hasArg(OBJCOPY_regex)
701                                      ? MatchStyle::Regex
702                                      : MatchStyle::Wildcard;
703   MatchStyle SymbolMatchStyle = InputArgs.hasArg(OBJCOPY_regex)
704                                     ? MatchStyle::Regex
705                                     : InputArgs.hasArg(OBJCOPY_wildcard)
706                                           ? MatchStyle::Wildcard
707                                           : MatchStyle::Literal;
708   StringRef InputFormat, OutputFormat;
709   if (InputArgs.hasArg(OBJCOPY_target)) {
710     InputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
711     OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
712   } else {
713     InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
714     OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
715   }
716 
717   // FIXME:  Currently, we ignore the target for non-binary/ihex formats
718   // explicitly specified by -I option (e.g. -Ielf32-x86-64) and guess the
719   // format by llvm::object::createBinary regardless of the option value.
720   Config.InputFormat = StringSwitch<FileFormat>(InputFormat)
721                            .Case("binary", FileFormat::Binary)
722                            .Case("ihex", FileFormat::IHex)
723                            .Default(FileFormat::Unspecified);
724 
725   if (InputArgs.hasArg(OBJCOPY_new_symbol_visibility)) {
726     const uint8_t Invalid = 0xff;
727     StringRef VisibilityStr =
728         InputArgs.getLastArgValue(OBJCOPY_new_symbol_visibility);
729 
730     ELFConfig.NewSymbolVisibility = StringSwitch<uint8_t>(VisibilityStr)
731                                         .Case("default", ELF::STV_DEFAULT)
732                                         .Case("hidden", ELF::STV_HIDDEN)
733                                         .Case("internal", ELF::STV_INTERNAL)
734                                         .Case("protected", ELF::STV_PROTECTED)
735                                         .Default(Invalid);
736 
737     if (ELFConfig.NewSymbolVisibility == Invalid)
738       return createStringError(errc::invalid_argument,
739                                "'%s' is not a valid symbol visibility",
740                                VisibilityStr.str().c_str());
741   }
742 
743   Config.OutputFormat = StringSwitch<FileFormat>(OutputFormat)
744                             .Case("binary", FileFormat::Binary)
745                             .Case("ihex", FileFormat::IHex)
746                             .Default(FileFormat::Unspecified);
747   if (Config.OutputFormat == FileFormat::Unspecified) {
748     if (OutputFormat.empty()) {
749       Config.OutputFormat = Config.InputFormat;
750     } else {
751       Expected<TargetInfo> Target =
752           getOutputTargetInfoByTargetName(OutputFormat);
753       if (!Target)
754         return Target.takeError();
755       Config.OutputFormat = Target->Format;
756       Config.OutputArch = Target->Machine;
757     }
758   }
759 
760   if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections,
761                                       OBJCOPY_compress_debug_sections_eq)) {
762     Config.CompressionType = DebugCompressionType::Z;
763 
764     if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) {
765       Config.CompressionType =
766           StringSwitch<DebugCompressionType>(
767               InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq))
768               .Case("zlib-gnu", DebugCompressionType::GNU)
769               .Case("zlib", DebugCompressionType::Z)
770               .Default(DebugCompressionType::None);
771       if (Config.CompressionType == DebugCompressionType::None)
772         return createStringError(
773             errc::invalid_argument,
774             "invalid or unsupported --compress-debug-sections format: %s",
775             InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)
776                 .str()
777                 .c_str());
778     }
779     if (!zlib::isAvailable())
780       return createStringError(
781           errc::invalid_argument,
782           "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress");
783   }
784 
785   Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
786   // The gnu_debuglink's target is expected to not change or else its CRC would
787   // become invalidated and get rejected. We can avoid recalculating the
788   // checksum for every target file inside an archive by precomputing the CRC
789   // here. This prevents a significant amount of I/O.
790   if (!Config.AddGnuDebugLink.empty()) {
791     auto DebugOrErr = MemoryBuffer::getFile(Config.AddGnuDebugLink);
792     if (!DebugOrErr)
793       return createFileError(Config.AddGnuDebugLink, DebugOrErr.getError());
794     auto Debug = std::move(*DebugOrErr);
795     Config.GnuDebugLinkCRC32 =
796         llvm::crc32(arrayRefFromStringRef(Debug->getBuffer()));
797   }
798   Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
799   Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
800   Config.AllocSectionsPrefix =
801       InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections);
802   if (auto Arg = InputArgs.getLastArg(OBJCOPY_extract_partition))
803     Config.ExtractPartition = Arg->getValue();
804 
805   for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
806     if (!StringRef(Arg->getValue()).contains('='))
807       return createStringError(errc::invalid_argument,
808                                "bad format for --redefine-sym");
809     auto Old2New = StringRef(Arg->getValue()).split('=');
810     if (!Config.SymbolsToRename.insert(Old2New).second)
811       return createStringError(errc::invalid_argument,
812                                "multiple redefinition of symbol '%s'",
813                                Old2New.first.str().c_str());
814   }
815 
816   for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols))
817     if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc,
818                                              Arg->getValue()))
819       return std::move(E);
820 
821   for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) {
822     Expected<SectionRename> SR =
823         parseRenameSectionValue(StringRef(Arg->getValue()));
824     if (!SR)
825       return SR.takeError();
826     if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second)
827       return createStringError(errc::invalid_argument,
828                                "multiple renames of section '%s'",
829                                SR->OriginalName.str().c_str());
830   }
831   for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_alignment)) {
832     Expected<std::pair<StringRef, uint64_t>> NameAndAlign =
833         parseSetSectionAlignment(Arg->getValue());
834     if (!NameAndAlign)
835       return NameAndAlign.takeError();
836     Config.SetSectionAlignment[NameAndAlign->first] = NameAndAlign->second;
837   }
838   for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) {
839     Expected<SectionFlagsUpdate> SFU =
840         parseSetSectionFlagValue(Arg->getValue());
841     if (!SFU)
842       return SFU.takeError();
843     if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second)
844       return createStringError(
845           errc::invalid_argument,
846           "--set-section-flags set multiple times for section '%s'",
847           SFU->Name.str().c_str());
848   }
849   // Prohibit combinations of --set-section-flags when the section name is used
850   // by --rename-section, either as a source or a destination.
851   for (const auto &E : Config.SectionsToRename) {
852     const SectionRename &SR = E.second;
853     if (Config.SetSectionFlags.count(SR.OriginalName))
854       return createStringError(
855           errc::invalid_argument,
856           "--set-section-flags=%s conflicts with --rename-section=%s=%s",
857           SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(),
858           SR.NewName.str().c_str());
859     if (Config.SetSectionFlags.count(SR.NewName))
860       return createStringError(
861           errc::invalid_argument,
862           "--set-section-flags=%s conflicts with --rename-section=%s=%s",
863           SR.NewName.str().c_str(), SR.OriginalName.str().c_str(),
864           SR.NewName.str().c_str());
865   }
866 
867   for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
868     if (Error E = Config.ToRemove.addMatcher(NameOrPattern::create(
869             Arg->getValue(), SectionMatchStyle, ErrorCallback)))
870       return std::move(E);
871   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section))
872     if (Error E = Config.KeepSection.addMatcher(NameOrPattern::create(
873             Arg->getValue(), SectionMatchStyle, ErrorCallback)))
874       return std::move(E);
875   for (auto Arg : InputArgs.filtered(OBJCOPY_only_section))
876     if (Error E = Config.OnlySection.addMatcher(NameOrPattern::create(
877             Arg->getValue(), SectionMatchStyle, ErrorCallback)))
878       return std::move(E);
879   for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) {
880     StringRef ArgValue(Arg->getValue());
881     if (!ArgValue.contains('='))
882       return createStringError(errc::invalid_argument,
883                                "bad format for --add-section: missing '='");
884     if (ArgValue.split("=").second.empty())
885       return createStringError(
886           errc::invalid_argument,
887           "bad format for --add-section: missing file name");
888     Config.AddSection.push_back(ArgValue);
889   }
890   for (auto *Arg : InputArgs.filtered(OBJCOPY_dump_section)) {
891     StringRef Value(Arg->getValue());
892     if (Value.split('=').second.empty())
893       return createStringError(
894           errc::invalid_argument,
895           "bad format for --dump-section, expected section=file");
896     Config.DumpSection.push_back(Value);
897   }
898   Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
899   Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
900   Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
901   Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
902   Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
903   Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
904   Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded);
905   Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
906   Config.ExtractMainPartition =
907       InputArgs.hasArg(OBJCOPY_extract_main_partition);
908   Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
909   Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
910   if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals))
911     Config.DiscardMode =
912         InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals)
913             ? DiscardType::All
914             : DiscardType::Locals;
915   Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
916   Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
917   Config.KeepUndefined = InputArgs.hasArg(OBJCOPY_keep_undefined);
918   Config.DecompressDebugSections =
919       InputArgs.hasArg(OBJCOPY_decompress_debug_sections);
920   if (Config.DiscardMode == DiscardType::All) {
921     Config.StripDebug = true;
922     Config.KeepFileSymbols = true;
923   }
924   for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
925     if (Error E = Config.SymbolsToLocalize.addMatcher(NameOrPattern::create(
926             Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
927       return std::move(E);
928   for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols))
929     if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, DC.Alloc,
930                                      Arg->getValue(), SymbolMatchStyle,
931                                      ErrorCallback))
932       return std::move(E);
933   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol))
934     if (Error E = Config.SymbolsToKeepGlobal.addMatcher(NameOrPattern::create(
935             Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
936       return std::move(E);
937   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
938     if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc,
939                                      Arg->getValue(), SymbolMatchStyle,
940                                      ErrorCallback))
941       return std::move(E);
942   for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
943     if (Error E = Config.SymbolsToGlobalize.addMatcher(NameOrPattern::create(
944             Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
945       return std::move(E);
946   for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols))
947     if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc,
948                                      Arg->getValue(), SymbolMatchStyle,
949                                      ErrorCallback))
950       return std::move(E);
951   for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
952     if (Error E = Config.SymbolsToWeaken.addMatcher(NameOrPattern::create(
953             Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
954       return std::move(E);
955   for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols))
956     if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, DC.Alloc,
957                                      Arg->getValue(), SymbolMatchStyle,
958                                      ErrorCallback))
959       return std::move(E);
960   for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
961     if (Error E = Config.SymbolsToRemove.addMatcher(NameOrPattern::create(
962             Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
963       return std::move(E);
964   for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols))
965     if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc,
966                                      Arg->getValue(), SymbolMatchStyle,
967                                      ErrorCallback))
968       return std::move(E);
969   for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol))
970     if (Error E =
971             Config.UnneededSymbolsToRemove.addMatcher(NameOrPattern::create(
972                 Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
973       return std::move(E);
974   for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols))
975     if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc,
976                                      Arg->getValue(), SymbolMatchStyle,
977                                      ErrorCallback))
978       return std::move(E);
979   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
980     if (Error E = Config.SymbolsToKeep.addMatcher(NameOrPattern::create(
981             Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
982       return std::move(E);
983   for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbols))
984     if (Error E =
985             addSymbolsFromFile(Config.SymbolsToKeep, DC.Alloc, Arg->getValue(),
986                                SymbolMatchStyle, ErrorCallback))
987       return std::move(E);
988   for (auto *Arg : InputArgs.filtered(OBJCOPY_add_symbol)) {
989     Expected<NewSymbolInfo> SymInfo = parseNewSymbolInfo(Arg->getValue());
990     if (!SymInfo)
991       return SymInfo.takeError();
992 
993     Config.SymbolsToAdd.push_back(*SymInfo);
994   }
995 
996   Config.AllowBrokenLinks = InputArgs.hasArg(OBJCOPY_allow_broken_links);
997 
998   Config.DeterministicArchives = InputArgs.hasFlag(
999       OBJCOPY_enable_deterministic_archives,
1000       OBJCOPY_disable_deterministic_archives, /*default=*/true);
1001 
1002   Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
1003 
1004   if (Config.PreserveDates &&
1005       (Config.OutputFilename == "-" || Config.InputFilename == "-"))
1006     return createStringError(errc::invalid_argument,
1007                              "--preserve-dates requires a file");
1008 
1009   for (auto Arg : InputArgs)
1010     if (Arg->getOption().matches(OBJCOPY_set_start)) {
1011       auto EAddr = getAsInteger<uint64_t>(Arg->getValue());
1012       if (!EAddr)
1013         return createStringError(
1014             EAddr.getError(), "bad entry point address: '%s'", Arg->getValue());
1015 
1016       Config.EntryExpr = [EAddr](uint64_t) { return *EAddr; };
1017     } else if (Arg->getOption().matches(OBJCOPY_change_start)) {
1018       auto EIncr = getAsInteger<int64_t>(Arg->getValue());
1019       if (!EIncr)
1020         return createStringError(EIncr.getError(),
1021                                  "bad entry point increment: '%s'",
1022                                  Arg->getValue());
1023       auto Expr = Config.EntryExpr ? std::move(Config.EntryExpr)
1024                                    : [](uint64_t A) { return A; };
1025       Config.EntryExpr = [Expr, EIncr](uint64_t EAddr) {
1026         return Expr(EAddr) + *EIncr;
1027       };
1028     }
1029 
1030   if (Config.DecompressDebugSections &&
1031       Config.CompressionType != DebugCompressionType::None) {
1032     return createStringError(
1033         errc::invalid_argument,
1034         "cannot specify both --compress-debug-sections and "
1035         "--decompress-debug-sections");
1036   }
1037 
1038   if (Config.DecompressDebugSections && !zlib::isAvailable())
1039     return createStringError(
1040         errc::invalid_argument,
1041         "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress");
1042 
1043   if (Config.ExtractPartition && Config.ExtractMainPartition)
1044     return createStringError(errc::invalid_argument,
1045                              "cannot specify --extract-partition together with "
1046                              "--extract-main-partition");
1047 
1048   DC.CopyConfigs.push_back(std::move(ConfigMgr));
1049   return std::move(DC);
1050 }
1051 
1052 // ParseInstallNameToolOptions returns the config and sets the input arguments.
1053 // If a help flag is set then ParseInstallNameToolOptions will print the help
1054 // messege and exit.
1055 Expected<DriverConfig>
parseInstallNameToolOptions(ArrayRef<const char * > ArgsArr)1056 objcopy::parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) {
1057   DriverConfig DC;
1058   ConfigManager ConfigMgr;
1059   CommonConfig &Config = ConfigMgr.Common;
1060   InstallNameToolOptTable T;
1061   unsigned MissingArgumentIndex, MissingArgumentCount;
1062   llvm::opt::InputArgList InputArgs =
1063       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
1064 
1065   if (MissingArgumentCount)
1066     return createStringError(
1067         errc::invalid_argument,
1068         "missing argument to " +
1069             StringRef(InputArgs.getArgString(MissingArgumentIndex)) +
1070             " option");
1071 
1072   if (InputArgs.size() == 0) {
1073     printHelp(T, errs(), ToolType::InstallNameTool);
1074     exit(1);
1075   }
1076 
1077   if (InputArgs.hasArg(INSTALL_NAME_TOOL_help)) {
1078     printHelp(T, outs(), ToolType::InstallNameTool);
1079     exit(0);
1080   }
1081 
1082   if (InputArgs.hasArg(INSTALL_NAME_TOOL_version)) {
1083     outs() << "llvm-install-name-tool, compatible with cctools "
1084               "install_name_tool\n";
1085     cl::PrintVersionMessage();
1086     exit(0);
1087   }
1088 
1089   for (auto Arg : InputArgs.filtered(INSTALL_NAME_TOOL_add_rpath))
1090     Config.RPathToAdd.push_back(Arg->getValue());
1091 
1092   for (auto *Arg : InputArgs.filtered(INSTALL_NAME_TOOL_prepend_rpath))
1093     Config.RPathToPrepend.push_back(Arg->getValue());
1094 
1095   for (auto Arg : InputArgs.filtered(INSTALL_NAME_TOOL_delete_rpath)) {
1096     StringRef RPath = Arg->getValue();
1097 
1098     // Cannot add and delete the same rpath at the same time.
1099     if (is_contained(Config.RPathToAdd, RPath))
1100       return createStringError(
1101           errc::invalid_argument,
1102           "cannot specify both -add_rpath '%s' and -delete_rpath '%s'",
1103           RPath.str().c_str(), RPath.str().c_str());
1104     if (is_contained(Config.RPathToPrepend, RPath))
1105       return createStringError(
1106           errc::invalid_argument,
1107           "cannot specify both -prepend_rpath '%s' and -delete_rpath '%s'",
1108           RPath.str().c_str(), RPath.str().c_str());
1109 
1110     Config.RPathsToRemove.insert(RPath);
1111   }
1112 
1113   for (auto *Arg : InputArgs.filtered(INSTALL_NAME_TOOL_rpath)) {
1114     StringRef Old = Arg->getValue(0);
1115     StringRef New = Arg->getValue(1);
1116 
1117     auto Match = [=](StringRef RPath) { return RPath == Old || RPath == New; };
1118 
1119     // Cannot specify duplicate -rpath entries
1120     auto It1 = find_if(
1121         Config.RPathsToUpdate,
1122         [&Match](const DenseMap<StringRef, StringRef>::value_type &OldNew) {
1123           return Match(OldNew.getFirst()) || Match(OldNew.getSecond());
1124         });
1125     if (It1 != Config.RPathsToUpdate.end())
1126       return createStringError(errc::invalid_argument,
1127                                "cannot specify both -rpath '" +
1128                                    It1->getFirst() + "' '" + It1->getSecond() +
1129                                    "' and -rpath '" + Old + "' '" + New + "'");
1130 
1131     // Cannot specify the same rpath under both -delete_rpath and -rpath
1132     auto It2 = find_if(Config.RPathsToRemove, Match);
1133     if (It2 != Config.RPathsToRemove.end())
1134       return createStringError(errc::invalid_argument,
1135                                "cannot specify both -delete_rpath '" + *It2 +
1136                                    "' and -rpath '" + Old + "' '" + New + "'");
1137 
1138     // Cannot specify the same rpath under both -add_rpath and -rpath
1139     auto It3 = find_if(Config.RPathToAdd, Match);
1140     if (It3 != Config.RPathToAdd.end())
1141       return createStringError(errc::invalid_argument,
1142                                "cannot specify both -add_rpath '" + *It3 +
1143                                    "' and -rpath '" + Old + "' '" + New + "'");
1144 
1145     // Cannot specify the same rpath under both -prepend_rpath and -rpath.
1146     auto It4 = find_if(Config.RPathToPrepend, Match);
1147     if (It4 != Config.RPathToPrepend.end())
1148       return createStringError(errc::invalid_argument,
1149                                "cannot specify both -prepend_rpath '" + *It4 +
1150                                    "' and -rpath '" + Old + "' '" + New + "'");
1151 
1152     Config.RPathsToUpdate.insert({Old, New});
1153   }
1154 
1155   if (auto *Arg = InputArgs.getLastArg(INSTALL_NAME_TOOL_id)) {
1156     Config.SharedLibId = Arg->getValue();
1157     if (Config.SharedLibId->empty())
1158       return createStringError(errc::invalid_argument,
1159                                "cannot specify an empty id");
1160   }
1161 
1162   for (auto *Arg : InputArgs.filtered(INSTALL_NAME_TOOL_change))
1163     Config.InstallNamesToUpdate.insert({Arg->getValue(0), Arg->getValue(1)});
1164 
1165   Config.RemoveAllRpaths =
1166       InputArgs.hasArg(INSTALL_NAME_TOOL_delete_all_rpaths);
1167 
1168   SmallVector<StringRef, 2> Positional;
1169   for (auto Arg : InputArgs.filtered(INSTALL_NAME_TOOL_UNKNOWN))
1170     return createStringError(errc::invalid_argument, "unknown argument '%s'",
1171                              Arg->getAsString(InputArgs).c_str());
1172   for (auto Arg : InputArgs.filtered(INSTALL_NAME_TOOL_INPUT))
1173     Positional.push_back(Arg->getValue());
1174   if (Positional.empty())
1175     return createStringError(errc::invalid_argument, "no input file specified");
1176   if (Positional.size() > 1)
1177     return createStringError(
1178         errc::invalid_argument,
1179         "llvm-install-name-tool expects a single input file");
1180   Config.InputFilename = Positional[0];
1181   Config.OutputFilename = Positional[0];
1182 
1183   DC.CopyConfigs.push_back(std::move(ConfigMgr));
1184   return std::move(DC);
1185 }
1186 
1187 Expected<DriverConfig>
parseBitcodeStripOptions(ArrayRef<const char * > ArgsArr)1188 objcopy::parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr) {
1189   DriverConfig DC;
1190   ConfigManager ConfigMgr;
1191   CommonConfig &Config = ConfigMgr.Common;
1192   BitcodeStripOptTable T;
1193   unsigned MissingArgumentIndex, MissingArgumentCount;
1194   opt::InputArgList InputArgs =
1195       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
1196 
1197   if (InputArgs.size() == 0) {
1198     printHelp(T, errs(), ToolType::BitcodeStrip);
1199     exit(1);
1200   }
1201 
1202   if (InputArgs.hasArg(BITCODE_STRIP_help)) {
1203     printHelp(T, outs(), ToolType::BitcodeStrip);
1204     exit(0);
1205   }
1206 
1207   if (InputArgs.hasArg(BITCODE_STRIP_version)) {
1208     outs() << "llvm-bitcode-strip, compatible with cctools "
1209               "bitcode_strip\n";
1210     cl::PrintVersionMessage();
1211     exit(0);
1212   }
1213 
1214   for (auto *Arg : InputArgs.filtered(BITCODE_STRIP_UNKNOWN))
1215     return createStringError(errc::invalid_argument, "unknown argument '%s'",
1216                              Arg->getAsString(InputArgs).c_str());
1217 
1218   SmallVector<StringRef, 2> Positional;
1219   for (auto *Arg : InputArgs.filtered(BITCODE_STRIP_INPUT))
1220     Positional.push_back(Arg->getValue());
1221   if (Positional.size() > 1)
1222     return createStringError(errc::invalid_argument,
1223                              "llvm-bitcode-strip expects a single input file");
1224   assert(!Positional.empty());
1225   Config.InputFilename = Positional[0];
1226   Config.OutputFilename = Positional[0];
1227 
1228   DC.CopyConfigs.push_back(std::move(ConfigMgr));
1229   return std::move(DC);
1230 }
1231 
1232 // ParseStripOptions returns the config and sets the input arguments. If a
1233 // help flag is set then ParseStripOptions will print the help messege and
1234 // exit.
1235 Expected<DriverConfig>
parseStripOptions(ArrayRef<const char * > RawArgsArr,function_ref<Error (Error)> ErrorCallback)1236 objcopy::parseStripOptions(ArrayRef<const char *> RawArgsArr,
1237                            function_ref<Error(Error)> ErrorCallback) {
1238   const char *const *DashDash =
1239       std::find_if(RawArgsArr.begin(), RawArgsArr.end(),
1240                    [](StringRef Str) { return Str == "--"; });
1241   ArrayRef<const char *> ArgsArr = makeArrayRef(RawArgsArr.begin(), DashDash);
1242   if (DashDash != RawArgsArr.end())
1243     DashDash = std::next(DashDash);
1244 
1245   StripOptTable T;
1246   unsigned MissingArgumentIndex, MissingArgumentCount;
1247   llvm::opt::InputArgList InputArgs =
1248       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
1249 
1250   if (InputArgs.size() == 0 && DashDash == RawArgsArr.end()) {
1251     printHelp(T, errs(), ToolType::Strip);
1252     exit(1);
1253   }
1254 
1255   if (InputArgs.hasArg(STRIP_help)) {
1256     printHelp(T, outs(), ToolType::Strip);
1257     exit(0);
1258   }
1259 
1260   if (InputArgs.hasArg(STRIP_version)) {
1261     outs() << "llvm-strip, compatible with GNU strip\n";
1262     cl::PrintVersionMessage();
1263     exit(0);
1264   }
1265 
1266   SmallVector<StringRef, 2> Positional;
1267   for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
1268     return createStringError(errc::invalid_argument, "unknown argument '%s'",
1269                              Arg->getAsString(InputArgs).c_str());
1270   for (auto Arg : InputArgs.filtered(STRIP_INPUT))
1271     Positional.push_back(Arg->getValue());
1272   std::copy(DashDash, RawArgsArr.end(), std::back_inserter(Positional));
1273 
1274   if (Positional.empty())
1275     return createStringError(errc::invalid_argument, "no input file specified");
1276 
1277   if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output))
1278     return createStringError(
1279         errc::invalid_argument,
1280         "multiple input files cannot be used in combination with -o");
1281 
1282   ConfigManager ConfigMgr;
1283   CommonConfig &Config = ConfigMgr.Common;
1284 
1285   if (InputArgs.hasArg(STRIP_regex) && InputArgs.hasArg(STRIP_wildcard))
1286     return createStringError(errc::invalid_argument,
1287                              "--regex and --wildcard are incompatible");
1288   MatchStyle SectionMatchStyle =
1289       InputArgs.hasArg(STRIP_regex) ? MatchStyle::Regex : MatchStyle::Wildcard;
1290   MatchStyle SymbolMatchStyle = InputArgs.hasArg(STRIP_regex)
1291                                     ? MatchStyle::Regex
1292                                     : InputArgs.hasArg(STRIP_wildcard)
1293                                           ? MatchStyle::Wildcard
1294                                           : MatchStyle::Literal;
1295   Config.AllowBrokenLinks = InputArgs.hasArg(STRIP_allow_broken_links);
1296   Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
1297 
1298   if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals))
1299     Config.DiscardMode =
1300         InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals)
1301             ? DiscardType::All
1302             : DiscardType::Locals;
1303   Config.StripSections = InputArgs.hasArg(STRIP_strip_sections);
1304   Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded);
1305   if (auto Arg = InputArgs.getLastArg(STRIP_strip_all, STRIP_no_strip_all))
1306     Config.StripAll = Arg->getOption().getID() == STRIP_strip_all;
1307   Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu);
1308   Config.StripSwiftSymbols = InputArgs.hasArg(STRIP_strip_swift_symbols);
1309   Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug);
1310   Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols);
1311   Config.KeepUndefined = InputArgs.hasArg(STRIP_keep_undefined);
1312 
1313   for (auto Arg : InputArgs.filtered(STRIP_keep_section))
1314     if (Error E = Config.KeepSection.addMatcher(NameOrPattern::create(
1315             Arg->getValue(), SectionMatchStyle, ErrorCallback)))
1316       return std::move(E);
1317 
1318   for (auto Arg : InputArgs.filtered(STRIP_remove_section))
1319     if (Error E = Config.ToRemove.addMatcher(NameOrPattern::create(
1320             Arg->getValue(), SectionMatchStyle, ErrorCallback)))
1321       return std::move(E);
1322 
1323   for (auto Arg : InputArgs.filtered(STRIP_strip_symbol))
1324     if (Error E = Config.SymbolsToRemove.addMatcher(NameOrPattern::create(
1325             Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
1326       return std::move(E);
1327 
1328   for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
1329     if (Error E = Config.SymbolsToKeep.addMatcher(NameOrPattern::create(
1330             Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
1331       return std::move(E);
1332 
1333   if (!InputArgs.hasArg(STRIP_no_strip_all) && !Config.StripDebug &&
1334       !Config.StripUnneeded && Config.DiscardMode == DiscardType::None &&
1335       !Config.StripAllGNU && Config.SymbolsToRemove.empty())
1336     Config.StripAll = true;
1337 
1338   if (Config.DiscardMode == DiscardType::All) {
1339     Config.StripDebug = true;
1340     Config.KeepFileSymbols = true;
1341   }
1342 
1343   Config.DeterministicArchives =
1344       InputArgs.hasFlag(STRIP_enable_deterministic_archives,
1345                         STRIP_disable_deterministic_archives, /*default=*/true);
1346 
1347   Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates);
1348   Config.InputFormat = FileFormat::Unspecified;
1349   Config.OutputFormat = FileFormat::Unspecified;
1350 
1351   DriverConfig DC;
1352   if (Positional.size() == 1) {
1353     Config.InputFilename = Positional[0];
1354     Config.OutputFilename =
1355         InputArgs.getLastArgValue(STRIP_output, Positional[0]);
1356     DC.CopyConfigs.push_back(std::move(ConfigMgr));
1357   } else {
1358     StringMap<unsigned> InputFiles;
1359     for (StringRef Filename : Positional) {
1360       if (InputFiles[Filename]++ == 1) {
1361         if (Filename == "-")
1362           return createStringError(
1363               errc::invalid_argument,
1364               "cannot specify '-' as an input file more than once");
1365         if (Error E = ErrorCallback(createStringError(
1366                 errc::invalid_argument, "'%s' was already specified",
1367                 Filename.str().c_str())))
1368           return std::move(E);
1369       }
1370       Config.InputFilename = Filename;
1371       Config.OutputFilename = Filename;
1372       DC.CopyConfigs.push_back(ConfigMgr);
1373     }
1374   }
1375 
1376   if (Config.PreserveDates && (is_contained(Positional, "-") ||
1377                                InputArgs.getLastArgValue(STRIP_output) == "-"))
1378     return createStringError(errc::invalid_argument,
1379                              "--preserve-dates requires a file");
1380 
1381   return std::move(DC);
1382 }
1383