[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / llvm / tools / dsymutil / dsymutil.cpp
blob104895b1a90bdaa134d2d188db7e2fd4648ad59b
1 //===- dsymutil.cpp - Debug info dumping utility for llvm -----------------===//
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 // This program is a utility that aims to be a dropin replacement for Darwin's
10 // dsymutil.
11 //===----------------------------------------------------------------------===//
13 #include "dsymutil.h"
14 #include "BinaryHolder.h"
15 #include "CFBundle.h"
16 #include "DebugMap.h"
17 #include "DwarfLinkerForBinary.h"
18 #include "LinkUtils.h"
19 #include "MachOUtils.h"
20 #include "Reproducer.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/DebugInfo/DIContext.h"
27 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
28 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/Object/Binary.h"
31 #include "llvm/Object/MachO.h"
32 #include "llvm/Option/Arg.h"
33 #include "llvm/Option/ArgList.h"
34 #include "llvm/Option/Option.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/CrashRecoveryContext.h"
37 #include "llvm/Support/FileCollector.h"
38 #include "llvm/Support/FileSystem.h"
39 #include "llvm/Support/FormatVariadic.h"
40 #include "llvm/Support/InitLLVM.h"
41 #include "llvm/Support/LLVMDriver.h"
42 #include "llvm/Support/Path.h"
43 #include "llvm/Support/TargetSelect.h"
44 #include "llvm/Support/ThreadPool.h"
45 #include "llvm/Support/WithColor.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/thread.h"
48 #include "llvm/TargetParser/Triple.h"
49 #include <algorithm>
50 #include <cstdint>
51 #include <cstdlib>
52 #include <string>
53 #include <system_error>
55 using namespace llvm;
56 using namespace llvm::dsymutil;
57 using namespace object;
59 namespace {
60 enum ID {
61 OPT_INVALID = 0, // This is not an option ID.
62 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
63 #include "Options.inc"
64 #undef OPTION
67 #define PREFIX(NAME, VALUE) \
68 static constexpr StringLiteral NAME##_init[] = VALUE; \
69 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
70 std::size(NAME##_init) - 1);
71 #include "Options.inc"
72 #undef PREFIX
74 using namespace llvm::opt;
75 static constexpr opt::OptTable::Info InfoTable[] = {
76 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
77 #include "Options.inc"
78 #undef OPTION
81 class DsymutilOptTable : public opt::GenericOptTable {
82 public:
83 DsymutilOptTable() : opt::GenericOptTable(InfoTable) {}
85 } // namespace
87 enum class DWARFVerify : uint8_t {
88 None = 0,
89 Input = 1 << 0,
90 Output = 1 << 1,
91 OutputOnValidInput = 1 << 2,
92 All = Input | Output,
93 Auto = Input | OutputOnValidInput,
94 #if !defined(NDEBUG) || defined(EXPENSIVE_CHECKS)
95 Default = Auto
96 #else
97 Default = None
98 #endif
101 inline bool flagIsSet(DWARFVerify Flags, DWARFVerify SingleFlag) {
102 return static_cast<uint8_t>(Flags) & static_cast<uint8_t>(SingleFlag);
105 struct DsymutilOptions {
106 bool DumpDebugMap = false;
107 bool DumpStab = false;
108 bool Flat = false;
109 bool InputIsYAMLDebugMap = false;
110 bool ForceKeepFunctionForStatic = false;
111 std::string SymbolMap;
112 std::string OutputFile;
113 std::string Toolchain;
114 std::string ReproducerPath;
115 std::vector<std::string> Archs;
116 std::vector<std::string> InputFiles;
117 unsigned NumThreads;
118 DWARFVerify Verify = DWARFVerify::Default;
119 ReproducerMode ReproMode = ReproducerMode::GenerateOnCrash;
120 dsymutil::LinkOptions LinkOpts;
123 /// Return a list of input files. This function has logic for dealing with the
124 /// special case where we might have dSYM bundles as input. The function
125 /// returns an error when the directory structure doesn't match that of a dSYM
126 /// bundle.
127 static Expected<std::vector<std::string>> getInputs(opt::InputArgList &Args,
128 bool DsymAsInput) {
129 std::vector<std::string> InputFiles;
130 for (auto *File : Args.filtered(OPT_INPUT))
131 InputFiles.push_back(File->getValue());
133 if (!DsymAsInput)
134 return InputFiles;
136 // If we are updating, we might get dSYM bundles as input.
137 std::vector<std::string> Inputs;
138 for (const auto &Input : InputFiles) {
139 if (!sys::fs::is_directory(Input)) {
140 Inputs.push_back(Input);
141 continue;
144 // Make sure that we're dealing with a dSYM bundle.
145 SmallString<256> BundlePath(Input);
146 sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
147 if (!sys::fs::is_directory(BundlePath))
148 return make_error<StringError>(
149 Input + " is a directory, but doesn't look like a dSYM bundle.",
150 inconvertibleErrorCode());
152 // Create a directory iterator to iterate over all the entries in the
153 // bundle.
154 std::error_code EC;
155 sys::fs::directory_iterator DirIt(BundlePath, EC);
156 sys::fs::directory_iterator DirEnd;
157 if (EC)
158 return errorCodeToError(EC);
160 // Add each entry to the list of inputs.
161 while (DirIt != DirEnd) {
162 Inputs.push_back(DirIt->path());
163 DirIt.increment(EC);
164 if (EC)
165 return errorCodeToError(EC);
168 return Inputs;
171 // Verify that the given combination of options makes sense.
172 static Error verifyOptions(const DsymutilOptions &Options) {
173 if (Options.InputFiles.empty()) {
174 return make_error<StringError>("no input files specified",
175 errc::invalid_argument);
178 if (Options.LinkOpts.Update && llvm::is_contained(Options.InputFiles, "-")) {
179 // FIXME: We cannot use stdin for an update because stdin will be
180 // consumed by the BinaryHolder during the debugmap parsing, and
181 // then we will want to consume it again in DwarfLinker. If we
182 // used a unique BinaryHolder object that could cache multiple
183 // binaries this restriction would go away.
184 return make_error<StringError>(
185 "standard input cannot be used as input for a dSYM update.",
186 errc::invalid_argument);
189 if (!Options.Flat && Options.OutputFile == "-")
190 return make_error<StringError>(
191 "cannot emit to standard output without --flat.",
192 errc::invalid_argument);
194 if (Options.InputFiles.size() > 1 && Options.Flat &&
195 !Options.OutputFile.empty())
196 return make_error<StringError>(
197 "cannot use -o with multiple inputs in flat mode.",
198 errc::invalid_argument);
200 if (!Options.ReproducerPath.empty() &&
201 Options.ReproMode != ReproducerMode::Use)
202 return make_error<StringError>(
203 "cannot combine --gen-reproducer and --use-reproducer.",
204 errc::invalid_argument);
206 return Error::success();
209 static Expected<DsymutilAccelTableKind>
210 getAccelTableKind(opt::InputArgList &Args) {
211 if (opt::Arg *Accelerator = Args.getLastArg(OPT_accelerator)) {
212 StringRef S = Accelerator->getValue();
213 if (S == "Apple")
214 return DsymutilAccelTableKind::Apple;
215 if (S == "Dwarf")
216 return DsymutilAccelTableKind::Dwarf;
217 if (S == "Pub")
218 return DsymutilAccelTableKind::Pub;
219 if (S == "Default")
220 return DsymutilAccelTableKind::Default;
221 if (S == "None")
222 return DsymutilAccelTableKind::None;
223 return make_error<StringError>("invalid accelerator type specified: '" + S +
224 "'. Supported values are 'Apple', "
225 "'Dwarf', 'Pub', 'Default' and 'None'.",
226 inconvertibleErrorCode());
228 return DsymutilAccelTableKind::Default;
231 static Expected<DsymutilDWARFLinkerType>
232 getDWARFLinkerType(opt::InputArgList &Args) {
233 if (opt::Arg *LinkerType = Args.getLastArg(OPT_linker)) {
234 StringRef S = LinkerType->getValue();
235 if (S == "apple")
236 return DsymutilDWARFLinkerType::Apple;
237 if (S == "llvm")
238 return DsymutilDWARFLinkerType::LLVM;
239 return make_error<StringError>("invalid DWARF linker type specified: '" +
241 "'. Supported values are 'apple', "
242 "'llvm'.",
243 inconvertibleErrorCode());
246 return DsymutilDWARFLinkerType::Apple;
249 static Expected<ReproducerMode> getReproducerMode(opt::InputArgList &Args) {
250 if (Args.hasArg(OPT_gen_reproducer))
251 return ReproducerMode::GenerateOnExit;
252 if (opt::Arg *Reproducer = Args.getLastArg(OPT_reproducer)) {
253 StringRef S = Reproducer->getValue();
254 if (S == "GenerateOnExit")
255 return ReproducerMode::GenerateOnExit;
256 if (S == "GenerateOnCrash")
257 return ReproducerMode::GenerateOnCrash;
258 if (S == "Off")
259 return ReproducerMode::Off;
260 return make_error<StringError>(
261 "invalid reproducer mode: '" + S +
262 "'. Supported values are 'GenerateOnExit', 'GenerateOnCrash', "
263 "'Off'.",
264 inconvertibleErrorCode());
266 return ReproducerMode::GenerateOnCrash;
269 static Expected<DWARFVerify> getVerifyKind(opt::InputArgList &Args) {
270 if (Args.hasArg(OPT_verify))
271 return DWARFVerify::Output;
272 if (opt::Arg *Verify = Args.getLastArg(OPT_verify_dwarf)) {
273 StringRef S = Verify->getValue();
274 if (S == "input")
275 return DWARFVerify::Input;
276 if (S == "output")
277 return DWARFVerify::Output;
278 if (S == "all")
279 return DWARFVerify::All;
280 if (S == "auto")
281 return DWARFVerify::Auto;
282 if (S == "none")
283 return DWARFVerify::None;
284 return make_error<StringError>("invalid verify type specified: '" + S +
285 "'. Supported values are 'none', "
286 "'input', 'output', 'all' and 'auto'.",
287 inconvertibleErrorCode());
289 return DWARFVerify::Default;
292 /// Parses the command line options into the LinkOptions struct and performs
293 /// some sanity checking. Returns an error in case the latter fails.
294 static Expected<DsymutilOptions> getOptions(opt::InputArgList &Args) {
295 DsymutilOptions Options;
297 Options.DumpDebugMap = Args.hasArg(OPT_dump_debug_map);
298 Options.DumpStab = Args.hasArg(OPT_symtab);
299 Options.Flat = Args.hasArg(OPT_flat);
300 Options.InputIsYAMLDebugMap = Args.hasArg(OPT_yaml_input);
302 if (Expected<DWARFVerify> Verify = getVerifyKind(Args)) {
303 Options.Verify = *Verify;
304 } else {
305 return Verify.takeError();
308 Options.LinkOpts.NoODR = Args.hasArg(OPT_no_odr);
309 Options.LinkOpts.VerifyInputDWARF =
310 flagIsSet(Options.Verify, DWARFVerify::Input);
311 Options.LinkOpts.NoOutput = Args.hasArg(OPT_no_output);
312 Options.LinkOpts.NoTimestamp = Args.hasArg(OPT_no_swiftmodule_timestamp);
313 Options.LinkOpts.Update = Args.hasArg(OPT_update);
314 Options.LinkOpts.Verbose = Args.hasArg(OPT_verbose);
315 Options.LinkOpts.Statistics = Args.hasArg(OPT_statistics);
316 Options.LinkOpts.Fat64 = Args.hasArg(OPT_fat64);
317 Options.LinkOpts.KeepFunctionForStatic =
318 Args.hasArg(OPT_keep_func_for_static);
320 if (opt::Arg *ReproducerPath = Args.getLastArg(OPT_use_reproducer)) {
321 Options.ReproMode = ReproducerMode::Use;
322 Options.ReproducerPath = ReproducerPath->getValue();
323 } else {
324 if (Expected<ReproducerMode> ReproMode = getReproducerMode(Args)) {
325 Options.ReproMode = *ReproMode;
326 } else {
327 return ReproMode.takeError();
331 if (Expected<DsymutilAccelTableKind> AccelKind = getAccelTableKind(Args)) {
332 Options.LinkOpts.TheAccelTableKind = *AccelKind;
333 } else {
334 return AccelKind.takeError();
337 if (Expected<DsymutilDWARFLinkerType> DWARFLinkerType =
338 getDWARFLinkerType(Args)) {
339 Options.LinkOpts.DWARFLinkerType = *DWARFLinkerType;
340 } else {
341 return DWARFLinkerType.takeError();
344 if (opt::Arg *SymbolMap = Args.getLastArg(OPT_symbolmap))
345 Options.SymbolMap = SymbolMap->getValue();
347 if (Args.hasArg(OPT_symbolmap))
348 Options.LinkOpts.Update = true;
350 if (Expected<std::vector<std::string>> InputFiles =
351 getInputs(Args, Options.LinkOpts.Update)) {
352 Options.InputFiles = std::move(*InputFiles);
353 } else {
354 return InputFiles.takeError();
357 for (auto *Arch : Args.filtered(OPT_arch))
358 Options.Archs.push_back(Arch->getValue());
360 if (opt::Arg *OsoPrependPath = Args.getLastArg(OPT_oso_prepend_path))
361 Options.LinkOpts.PrependPath = OsoPrependPath->getValue();
363 for (const auto &Arg : Args.getAllArgValues(OPT_object_prefix_map)) {
364 auto Split = StringRef(Arg).split('=');
365 Options.LinkOpts.ObjectPrefixMap.insert(
366 {std::string(Split.first), std::string(Split.second)});
369 if (opt::Arg *OutputFile = Args.getLastArg(OPT_output))
370 Options.OutputFile = OutputFile->getValue();
372 if (opt::Arg *Toolchain = Args.getLastArg(OPT_toolchain))
373 Options.Toolchain = Toolchain->getValue();
375 if (Args.hasArg(OPT_assembly))
376 Options.LinkOpts.FileType = DWARFLinker::OutputFileType::Assembly;
378 if (opt::Arg *NumThreads = Args.getLastArg(OPT_threads))
379 Options.LinkOpts.Threads = atoi(NumThreads->getValue());
380 else
381 Options.LinkOpts.Threads = 0; // Use all available hardware threads
383 if (Options.DumpDebugMap || Options.LinkOpts.Verbose)
384 Options.LinkOpts.Threads = 1;
386 if (opt::Arg *RemarksPrependPath = Args.getLastArg(OPT_remarks_prepend_path))
387 Options.LinkOpts.RemarksPrependPath = RemarksPrependPath->getValue();
389 if (opt::Arg *RemarksOutputFormat =
390 Args.getLastArg(OPT_remarks_output_format)) {
391 if (Expected<remarks::Format> FormatOrErr =
392 remarks::parseFormat(RemarksOutputFormat->getValue()))
393 Options.LinkOpts.RemarksFormat = *FormatOrErr;
394 else
395 return FormatOrErr.takeError();
398 Options.LinkOpts.RemarksKeepAll =
399 !Args.hasArg(OPT_remarks_drop_without_debug);
401 if (Error E = verifyOptions(Options))
402 return std::move(E);
403 return Options;
406 static Error createPlistFile(StringRef Bin, StringRef BundleRoot,
407 StringRef Toolchain) {
408 // Create plist file to write to.
409 SmallString<128> InfoPlist(BundleRoot);
410 sys::path::append(InfoPlist, "Contents/Info.plist");
411 std::error_code EC;
412 raw_fd_ostream PL(InfoPlist, EC, sys::fs::OF_TextWithCRLF);
413 if (EC)
414 return make_error<StringError>(
415 "cannot create Plist: " + toString(errorCodeToError(EC)), EC);
417 CFBundleInfo BI = getBundleInfo(Bin);
419 if (BI.IDStr.empty()) {
420 StringRef BundleID = *sys::path::rbegin(BundleRoot);
421 if (sys::path::extension(BundleRoot) == ".dSYM")
422 BI.IDStr = std::string(sys::path::stem(BundleID));
423 else
424 BI.IDStr = std::string(BundleID);
427 // Print out information to the plist file.
428 PL << "<?xml version=\"1.0\" encoding=\"UTF-8\"\?>\n"
429 << "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
430 << "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
431 << "<plist version=\"1.0\">\n"
432 << "\t<dict>\n"
433 << "\t\t<key>CFBundleDevelopmentRegion</key>\n"
434 << "\t\t<string>English</string>\n"
435 << "\t\t<key>CFBundleIdentifier</key>\n"
436 << "\t\t<string>com.apple.xcode.dsym.";
437 printHTMLEscaped(BI.IDStr, PL);
438 PL << "</string>\n"
439 << "\t\t<key>CFBundleInfoDictionaryVersion</key>\n"
440 << "\t\t<string>6.0</string>\n"
441 << "\t\t<key>CFBundlePackageType</key>\n"
442 << "\t\t<string>dSYM</string>\n"
443 << "\t\t<key>CFBundleSignature</key>\n"
444 << "\t\t<string>\?\?\?\?</string>\n";
446 if (!BI.OmitShortVersion()) {
447 PL << "\t\t<key>CFBundleShortVersionString</key>\n";
448 PL << "\t\t<string>";
449 printHTMLEscaped(BI.ShortVersionStr, PL);
450 PL << "</string>\n";
453 PL << "\t\t<key>CFBundleVersion</key>\n";
454 PL << "\t\t<string>";
455 printHTMLEscaped(BI.VersionStr, PL);
456 PL << "</string>\n";
458 if (!Toolchain.empty()) {
459 PL << "\t\t<key>Toolchain</key>\n";
460 PL << "\t\t<string>";
461 printHTMLEscaped(Toolchain, PL);
462 PL << "</string>\n";
465 PL << "\t</dict>\n"
466 << "</plist>\n";
468 PL.close();
469 return Error::success();
472 static Error createBundleDir(StringRef BundleBase) {
473 SmallString<128> Bundle(BundleBase);
474 sys::path::append(Bundle, "Contents", "Resources", "DWARF");
475 if (std::error_code EC =
476 create_directories(Bundle.str(), true, sys::fs::perms::all_all))
477 return make_error<StringError>(
478 "cannot create bundle: " + toString(errorCodeToError(EC)), EC);
480 return Error::success();
483 static bool verifyOutput(StringRef OutputFile, StringRef Arch,
484 DsymutilOptions Options, std::mutex &Mutex) {
486 if (OutputFile == "-") {
487 std::lock_guard<std::mutex> Guard(Mutex);
488 WithColor::warning() << "verification skipped for " << Arch
489 << " because writing to stdout.\n";
490 return true;
493 if (Options.LinkOpts.NoOutput) {
494 std::lock_guard<std::mutex> Guard(Mutex);
495 WithColor::warning() << "verification skipped for " << Arch
496 << " because --no-output was passed.\n";
497 return true;
500 Expected<OwningBinary<Binary>> BinOrErr = createBinary(OutputFile);
501 if (!BinOrErr) {
502 std::lock_guard<std::mutex> Guard(Mutex);
503 WithColor::error() << OutputFile << ": " << toString(BinOrErr.takeError());
504 return false;
507 Binary &Binary = *BinOrErr.get().getBinary();
508 if (auto *Obj = dyn_cast<MachOObjectFile>(&Binary)) {
509 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*Obj);
510 if (DICtx->getMaxVersion() > 5) {
511 std::lock_guard<std::mutex> Guard(Mutex);
512 WithColor::warning()
513 << "verification skipped for " << Arch
514 << " because DWARF standard greater than v5 is not supported yet.\n";
515 return true;
518 if (Options.LinkOpts.Verbose) {
519 std::lock_guard<std::mutex> Guard(Mutex);
520 errs() << "Verifying DWARF for architecture: " << Arch << "\n";
523 std::string Buffer;
524 raw_string_ostream OS(Buffer);
526 DIDumpOptions DumpOpts;
527 bool success = DICtx->verify(OS, DumpOpts.noImplicitRecursion());
528 if (!success) {
529 std::lock_guard<std::mutex> Guard(Mutex);
530 errs() << OS.str();
531 WithColor::error() << "output verification failed for " << Arch << '\n';
533 return success;
536 return false;
539 namespace {
540 struct OutputLocation {
541 OutputLocation(std::string DWARFFile,
542 std::optional<std::string> ResourceDir = {})
543 : DWARFFile(DWARFFile), ResourceDir(ResourceDir) {}
544 /// This method is a workaround for older compilers.
545 std::optional<std::string> getResourceDir() const { return ResourceDir; }
546 std::string DWARFFile;
547 std::optional<std::string> ResourceDir;
549 } // namespace
551 static Expected<OutputLocation>
552 getOutputFileName(StringRef InputFile, const DsymutilOptions &Options) {
553 if (Options.OutputFile == "-")
554 return OutputLocation(Options.OutputFile);
556 // When updating, do in place replacement.
557 if (Options.OutputFile.empty() &&
558 (Options.LinkOpts.Update || !Options.SymbolMap.empty()))
559 return OutputLocation(std::string(InputFile));
561 // When dumping the debug map, just return an empty output location. This
562 // allows us to compute the output location once.
563 if (Options.DumpDebugMap)
564 return OutputLocation("");
566 // If a flat dSYM has been requested, things are pretty simple.
567 if (Options.Flat) {
568 if (Options.OutputFile.empty()) {
569 if (InputFile == "-")
570 return OutputLocation{"a.out.dwarf", {}};
571 return OutputLocation((InputFile + ".dwarf").str());
574 return OutputLocation(Options.OutputFile);
577 // We need to create/update a dSYM bundle.
578 // A bundle hierarchy looks like this:
579 // <bundle name>.dSYM/
580 // Contents/
581 // Info.plist
582 // Resources/
583 // DWARF/
584 // <DWARF file(s)>
585 std::string DwarfFile =
586 std::string(InputFile == "-" ? StringRef("a.out") : InputFile);
587 SmallString<128> Path(Options.OutputFile);
588 if (Path.empty())
589 Path = DwarfFile + ".dSYM";
590 if (!Options.LinkOpts.NoOutput) {
591 if (auto E = createBundleDir(Path))
592 return std::move(E);
593 if (auto E = createPlistFile(DwarfFile, Path, Options.Toolchain))
594 return std::move(E);
597 sys::path::append(Path, "Contents", "Resources");
598 std::string ResourceDir = std::string(Path.str());
599 sys::path::append(Path, "DWARF", sys::path::filename(DwarfFile));
600 return OutputLocation(std::string(Path.str()), ResourceDir);
603 int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
604 InitLLVM X(argc, argv);
606 // Parse arguments.
607 DsymutilOptTable T;
608 unsigned MAI;
609 unsigned MAC;
610 ArrayRef<const char *> ArgsArr = ArrayRef(argv + 1, argc - 1);
611 opt::InputArgList Args = T.ParseArgs(ArgsArr, MAI, MAC);
613 void *P = (void *)(intptr_t)getOutputFileName;
614 std::string SDKPath = sys::fs::getMainExecutable(argv[0], P);
615 SDKPath = std::string(sys::path::parent_path(SDKPath));
617 for (auto *Arg : Args.filtered(OPT_UNKNOWN)) {
618 WithColor::warning() << "ignoring unknown option: " << Arg->getSpelling()
619 << '\n';
622 if (Args.hasArg(OPT_help)) {
623 T.printHelp(
624 outs(), (std::string(argv[0]) + " [options] <input files>").c_str(),
625 "manipulate archived DWARF debug symbol files.\n\n"
626 "dsymutil links the DWARF debug information found in the object files\n"
627 "for the executable <input file> by using debug symbols information\n"
628 "contained in its symbol table.\n",
629 false);
630 return EXIT_SUCCESS;
633 if (Args.hasArg(OPT_version)) {
634 cl::PrintVersionMessage();
635 return EXIT_SUCCESS;
638 auto OptionsOrErr = getOptions(Args);
639 if (!OptionsOrErr) {
640 WithColor::error() << toString(OptionsOrErr.takeError()) << '\n';
641 return EXIT_FAILURE;
644 auto &Options = *OptionsOrErr;
646 InitializeAllTargetInfos();
647 InitializeAllTargetMCs();
648 InitializeAllTargets();
649 InitializeAllAsmPrinters();
651 auto Repro = Reproducer::createReproducer(Options.ReproMode,
652 Options.ReproducerPath, argc, argv);
653 if (!Repro) {
654 WithColor::error() << toString(Repro.takeError()) << '\n';
655 return EXIT_FAILURE;
658 Options.LinkOpts.VFS = (*Repro)->getVFS();
660 for (const auto &Arch : Options.Archs)
661 if (Arch != "*" && Arch != "all" &&
662 !object::MachOObjectFile::isValidArch(Arch)) {
663 WithColor::error() << "unsupported cpu architecture: '" << Arch << "'\n";
664 return EXIT_FAILURE;
667 SymbolMapLoader SymMapLoader(Options.SymbolMap);
669 for (auto &InputFile : Options.InputFiles) {
670 // Dump the symbol table for each input file and requested arch
671 if (Options.DumpStab) {
672 if (!dumpStab(Options.LinkOpts.VFS, InputFile, Options.Archs,
673 Options.LinkOpts.PrependPath))
674 return EXIT_FAILURE;
675 continue;
678 auto DebugMapPtrsOrErr =
679 parseDebugMap(Options.LinkOpts.VFS, InputFile, Options.Archs,
680 Options.LinkOpts.PrependPath, Options.LinkOpts.Verbose,
681 Options.InputIsYAMLDebugMap);
683 if (auto EC = DebugMapPtrsOrErr.getError()) {
684 WithColor::error() << "cannot parse the debug map for '" << InputFile
685 << "': " << EC.message() << '\n';
686 return EXIT_FAILURE;
689 // Remember the number of debug maps that are being processed to decide how
690 // to name the remark files.
691 Options.LinkOpts.NumDebugMaps = DebugMapPtrsOrErr->size();
693 if (Options.LinkOpts.Update) {
694 // The debug map should be empty. Add one object file corresponding to
695 // the input file.
696 for (auto &Map : *DebugMapPtrsOrErr)
697 Map->addDebugMapObject(InputFile,
698 sys::TimePoint<std::chrono::seconds>());
701 // Ensure that the debug map is not empty (anymore).
702 if (DebugMapPtrsOrErr->empty()) {
703 WithColor::error() << "no architecture to link\n";
704 return EXIT_FAILURE;
707 // Shared a single binary holder for all the link steps.
708 BinaryHolder BinHolder(Options.LinkOpts.VFS);
710 // Compute the output location and update the resource directory.
711 Expected<OutputLocation> OutputLocationOrErr =
712 getOutputFileName(InputFile, Options);
713 if (!OutputLocationOrErr) {
714 WithColor::error() << toString(OutputLocationOrErr.takeError());
715 return EXIT_FAILURE;
717 Options.LinkOpts.ResourceDir = OutputLocationOrErr->getResourceDir();
719 // Statistics only require different architectures to be processed
720 // sequentially, the link itself can still happen in parallel. Change the
721 // thread pool strategy here instead of modifying LinkOpts.Threads.
722 ThreadPoolStrategy S = hardware_concurrency(
723 Options.LinkOpts.Statistics ? 1 : Options.LinkOpts.Threads);
724 if (Options.LinkOpts.Threads == 0) {
725 // If NumThreads is not specified, create one thread for each input, up to
726 // the number of hardware threads.
727 S.ThreadsRequested = DebugMapPtrsOrErr->size();
728 S.Limit = true;
730 ThreadPool Threads(S);
732 // If there is more than one link to execute, we need to generate
733 // temporary files.
734 const bool NeedsTempFiles =
735 !Options.DumpDebugMap && (Options.OutputFile != "-") &&
736 (DebugMapPtrsOrErr->size() != 1 || Options.LinkOpts.Update);
738 std::atomic_char AllOK(1);
739 SmallVector<MachOUtils::ArchAndFile, 4> TempFiles;
741 std::mutex ErrorHandlerMutex;
743 // Set up a crash recovery context.
744 CrashRecoveryContext::Enable();
745 CrashRecoveryContext CRC;
746 CRC.DumpStackAndCleanupOnFailure = true;
748 const bool Crashed = !CRC.RunSafely([&]() {
749 for (auto &Map : *DebugMapPtrsOrErr) {
750 if (Options.LinkOpts.Verbose || Options.DumpDebugMap)
751 Map->print(outs());
753 if (Options.DumpDebugMap)
754 continue;
756 if (!Options.SymbolMap.empty())
757 Options.LinkOpts.Translator = SymMapLoader.Load(InputFile, *Map);
759 if (Map->begin() == Map->end()) {
760 std::lock_guard<std::mutex> Guard(ErrorHandlerMutex);
761 WithColor::warning()
762 << "no debug symbols in executable (-arch "
763 << MachOUtils::getArchName(Map->getTriple().getArchName())
764 << ")\n";
767 // Using a std::shared_ptr rather than std::unique_ptr because move-only
768 // types don't work with std::bind in the ThreadPool implementation.
769 std::shared_ptr<raw_fd_ostream> OS;
771 std::string OutputFile = OutputLocationOrErr->DWARFFile;
772 if (NeedsTempFiles) {
773 TempFiles.emplace_back(Map->getTriple().getArchName().str());
775 auto E = TempFiles.back().createTempFile();
776 if (E) {
777 std::lock_guard<std::mutex> Guard(ErrorHandlerMutex);
778 WithColor::error() << toString(std::move(E));
779 AllOK.fetch_and(false);
780 return;
783 MachOUtils::ArchAndFile &AF = TempFiles.back();
784 OS = std::make_shared<raw_fd_ostream>(AF.getFD(),
785 /*shouldClose*/ false);
786 OutputFile = AF.getPath();
787 } else {
788 std::error_code EC;
789 OS = std::make_shared<raw_fd_ostream>(
790 Options.LinkOpts.NoOutput ? "-" : OutputFile, EC,
791 sys::fs::OF_None);
792 if (EC) {
793 WithColor::error() << OutputFile << ": " << EC.message();
794 AllOK.fetch_and(false);
795 return;
799 auto LinkLambda = [&,
800 OutputFile](std::shared_ptr<raw_fd_ostream> Stream) {
801 DwarfLinkerForBinary Linker(*Stream, BinHolder, Options.LinkOpts,
802 ErrorHandlerMutex);
803 AllOK.fetch_and(Linker.link(*Map));
804 Stream->flush();
805 if (flagIsSet(Options.Verify, DWARFVerify::Output) ||
806 (flagIsSet(Options.Verify, DWARFVerify::OutputOnValidInput) &&
807 !Linker.InputVerificationFailed())) {
808 AllOK.fetch_and(verifyOutput(OutputFile,
809 Map->getTriple().getArchName(),
810 Options, ErrorHandlerMutex));
814 // FIXME: The DwarfLinker can have some very deep recursion that can max
815 // out the (significantly smaller) stack when using threads. We don't
816 // want this limitation when we only have a single thread.
817 if (S.ThreadsRequested == 1)
818 LinkLambda(OS);
819 else
820 Threads.async(LinkLambda, OS);
823 Threads.wait();
826 if (Crashed)
827 (*Repro)->generate();
829 if (!AllOK)
830 return EXIT_FAILURE;
832 if (NeedsTempFiles) {
833 const bool Fat64 = Options.LinkOpts.Fat64;
834 if (!Fat64) {
835 // Universal Mach-O files can't have an archicture slice that starts
836 // beyond the 4GB boundary. "lipo" can create a 64 bit universal
837 // header, but not all tools can parse these files so we want to return
838 // an error if the file can't be encoded as a file with a 32 bit
839 // universal header. To detect this, we check the size of each
840 // architecture's skinny Mach-O file and add up the offsets. If they
841 // exceed 4GB, then we return an error.
843 // First we compute the right offset where the first architecture will
844 // fit followin the 32 bit universal header. The 32 bit universal header
845 // starts with a uint32_t magic and a uint32_t number of architecture
846 // infos. Then it is followed by 5 uint32_t values for each
847 // architecture. So we set the start offset to the right value so we can
848 // calculate the exact offset that the first architecture slice can
849 // start at.
850 constexpr uint64_t MagicAndCountSize = 2 * 4;
851 constexpr uint64_t UniversalArchInfoSize = 5 * 4;
852 uint64_t FileOffset =
853 MagicAndCountSize + UniversalArchInfoSize * TempFiles.size();
854 for (const auto &File : TempFiles) {
855 ErrorOr<vfs::Status> stat =
856 Options.LinkOpts.VFS->status(File.getPath());
857 if (!stat)
858 break;
859 if (FileOffset > UINT32_MAX) {
860 WithColor::error()
861 << formatv("the universal binary has a slice with a starting "
862 "offset ({0:x}) that exceeds 4GB and will produce "
863 "an invalid Mach-O file. Use the -fat64 flag to "
864 "generate a universal binary with a 64-bit header "
865 "but note that not all tools support this format.",
866 FileOffset);
867 return EXIT_FAILURE;
869 FileOffset += stat->getSize();
872 if (!MachOUtils::generateUniversalBinary(
873 TempFiles, OutputLocationOrErr->DWARFFile, Options.LinkOpts,
874 SDKPath, Fat64))
875 return EXIT_FAILURE;
879 return EXIT_SUCCESS;