1 //===- tools/dsymutil/DwarfLinkerForBinary.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 "DwarfLinkerForBinary.h"
10 #include "BinaryHolder.h"
11 #include "DebugMap.h"
12 #include "MachOUtils.h"
13 #include "dsymutil.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/Hashing.h"
21 #include "llvm/ADT/IntervalMap.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/StringMap.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/ADT/Triple.h"
30 #include "llvm/ADT/Twine.h"
31 #include "llvm/BinaryFormat/Dwarf.h"
32 #include "llvm/BinaryFormat/MachO.h"
33 #include "llvm/BinaryFormat/Swift.h"
34 #include "llvm/CodeGen/AccelTable.h"
35 #include "llvm/CodeGen/AsmPrinter.h"
36 #include "llvm/CodeGen/DIE.h"
37 #include "llvm/CodeGen/NonRelocatableStringpool.h"
38 #include "llvm/Config/config.h"
39 #include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
40 #include "llvm/DebugInfo/DIContext.h"
41 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
42 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
43 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
44 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
45 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
46 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
47 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
48 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
49 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
50 #include "llvm/MC/MCAsmBackend.h"
51 #include "llvm/MC/MCAsmInfo.h"
52 #include "llvm/MC/MCCodeEmitter.h"
53 #include "llvm/MC/MCContext.h"
54 #include "llvm/MC/MCDwarf.h"
55 #include "llvm/MC/MCInstrInfo.h"
56 #include "llvm/MC/MCObjectFileInfo.h"
57 #include "llvm/MC/MCObjectWriter.h"
58 #include "llvm/MC/MCRegisterInfo.h"
59 #include "llvm/MC/MCSection.h"
60 #include "llvm/MC/MCStreamer.h"
61 #include "llvm/MC/MCSubtargetInfo.h"
62 #include "llvm/MC/MCTargetOptions.h"
63 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
64 #include "llvm/MC/TargetRegistry.h"
65 #include "llvm/Object/MachO.h"
66 #include "llvm/Object/ObjectFile.h"
67 #include "llvm/Object/SymbolicFile.h"
68 #include "llvm/Support/Allocator.h"
69 #include "llvm/Support/Casting.h"
70 #include "llvm/Support/Compiler.h"
71 #include "llvm/Support/DJB.h"
72 #include "llvm/Support/DataExtractor.h"
73 #include "llvm/Support/Error.h"
74 #include "llvm/Support/ErrorHandling.h"
75 #include "llvm/Support/ErrorOr.h"
76 #include "llvm/Support/FileSystem.h"
77 #include "llvm/Support/Format.h"
78 #include "llvm/Support/LEB128.h"
79 #include "llvm/Support/MathExtras.h"
80 #include "llvm/Support/MemoryBuffer.h"
81 #include "llvm/Support/Path.h"
82 #include "llvm/Support/ThreadPool.h"
83 #include "llvm/Support/ToolOutputFile.h"
84 #include "llvm/Support/WithColor.h"
85 #include "llvm/Support/raw_ostream.h"
86 #include "llvm/Target/TargetMachine.h"
87 #include "llvm/Target/TargetOptions.h"
88 #include <algorithm>
89 #include <cassert>
90 #include <cinttypes>
91 #include <climits>
92 #include <cstdint>
93 #include <cstdlib>
94 #include <cstring>
95 #include <limits>
96 #include <map>
97 #include <memory>
98 #include <string>
99 #include <system_error>
100 #include <tuple>
101 #include <utility>
102 #include <vector>
103 
104 namespace llvm {
105 
106 static mc::RegisterMCTargetOptionsFlags MOF;
107 
108 namespace dsymutil {
109 
110 static Error copySwiftInterfaces(
111     const std::map<std::string, std::string> &ParseableSwiftInterfaces,
112     StringRef Architecture, const LinkOptions &Options) {
113   std::error_code EC;
114   SmallString<128> InputPath;
115   SmallString<128> Path;
116   sys::path::append(Path, *Options.ResourceDir, "Swift", Architecture);
117   if ((EC = sys::fs::create_directories(Path.str(), true,
118                                         sys::fs::perms::all_all)))
119     return make_error<StringError>(
120         "cannot create directory: " + toString(errorCodeToError(EC)), EC);
121   unsigned BaseLength = Path.size();
122 
123   for (auto &I : ParseableSwiftInterfaces) {
124     StringRef ModuleName = I.first;
125     StringRef InterfaceFile = I.second;
126     if (!Options.PrependPath.empty()) {
127       InputPath.clear();
128       sys::path::append(InputPath, Options.PrependPath, InterfaceFile);
129       InterfaceFile = InputPath;
130     }
131     sys::path::append(Path, ModuleName);
132     Path.append(".swiftinterface");
133     if (Options.Verbose)
134       outs() << "copy parseable Swift interface " << InterfaceFile << " -> "
135              << Path.str() << '\n';
136 
137     // copy_file attempts an APFS clone first, so this should be cheap.
138     if ((EC = sys::fs::copy_file(InterfaceFile, Path.str())))
139       warn(Twine("cannot copy parseable Swift interface ") + InterfaceFile +
140            ": " + toString(errorCodeToError(EC)));
141     Path.resize(BaseLength);
142   }
143   return Error::success();
144 }
145 
146 /// Report a warning to the user, optionally including information about a
147 /// specific \p DIE related to the warning.
148 void DwarfLinkerForBinary::reportWarning(const Twine &Warning,
149                                          StringRef Context,
150                                          const DWARFDie *DIE) const {
151 
152   warn(Warning, Context);
153 
154   if (!Options.Verbose || !DIE)
155     return;
156 
157   DIDumpOptions DumpOpts;
158   DumpOpts.ChildRecurseDepth = 0;
159   DumpOpts.Verbose = Options.Verbose;
160 
161   WithColor::note() << "    in DIE:\n";
162   DIE->dump(errs(), 6 /* Indent */, DumpOpts);
163 }
164 
165 bool DwarfLinkerForBinary::createStreamer(const Triple &TheTriple,
166                                           raw_fd_ostream &OutFile) {
167   if (Options.NoOutput)
168     return true;
169 
170   Streamer = std::make_unique<DwarfStreamer>(
171       Options.FileType, OutFile, Options.Translator,
172       [&](const Twine &Error, StringRef Context, const DWARFDie *) {
173         error(Error, Context);
174       },
175       [&](const Twine &Warning, StringRef Context, const DWARFDie *) {
176         warn(Warning, Context);
177       });
178   return Streamer->init(TheTriple, "__DWARF");
179 }
180 
181 ErrorOr<const object::ObjectFile &>
182 DwarfLinkerForBinary::loadObject(const DebugMapObject &Obj,
183                                  const Triple &Triple) {
184   auto ObjectEntry =
185       BinHolder.getObjectEntry(Obj.getObjectFilename(), Obj.getTimestamp());
186   if (!ObjectEntry) {
187     auto Err = ObjectEntry.takeError();
188     reportWarning(Twine(Obj.getObjectFilename()) + ": " +
189                       toString(std::move(Err)),
190                   Obj.getObjectFilename());
191     return errorToErrorCode(std::move(Err));
192   }
193 
194   auto Object = ObjectEntry->getObject(Triple);
195   if (!Object) {
196     auto Err = Object.takeError();
197     reportWarning(Twine(Obj.getObjectFilename()) + ": " +
198                       toString(std::move(Err)),
199                   Obj.getObjectFilename());
200     return errorToErrorCode(std::move(Err));
201   }
202 
203   return *Object;
204 }
205 
206 static Error remarksErrorHandler(const DebugMapObject &DMO,
207                                  DwarfLinkerForBinary &Linker,
208                                  std::unique_ptr<FileError> FE) {
209   bool IsArchive = DMO.getObjectFilename().endswith(")");
210   // Don't report errors for missing remark files from static
211   // archives.
212   if (!IsArchive)
213     return Error(std::move(FE));
214 
215   std::string Message = FE->message();
216   Error E = FE->takeError();
217   Error NewE = handleErrors(std::move(E), [&](std::unique_ptr<ECError> EC) {
218     if (EC->convertToErrorCode() != std::errc::no_such_file_or_directory)
219       return Error(std::move(EC));
220 
221     Linker.reportWarning(Message, DMO.getObjectFilename());
222     return Error(Error::success());
223   });
224 
225   if (!NewE)
226     return Error::success();
227 
228   return createFileError(FE->getFileName(), std::move(NewE));
229 }
230 
231 static Error emitRemarks(const LinkOptions &Options, StringRef BinaryPath,
232                          StringRef ArchName, const remarks::RemarkLinker &RL) {
233   // Make sure we don't create the directories and the file if there is nothing
234   // to serialize.
235   if (RL.empty())
236     return Error::success();
237 
238   SmallString<128> InputPath;
239   SmallString<128> Path;
240   // Create the "Remarks" directory in the "Resources" directory.
241   sys::path::append(Path, *Options.ResourceDir, "Remarks");
242   if (std::error_code EC = sys::fs::create_directories(Path.str(), true,
243                                                        sys::fs::perms::all_all))
244     return errorCodeToError(EC);
245 
246   // Append the file name.
247   // For fat binaries, also append a dash and the architecture name.
248   sys::path::append(Path, sys::path::filename(BinaryPath));
249   if (Options.NumDebugMaps > 1) {
250     // More than one debug map means we have a fat binary.
251     Path += '-';
252     Path += ArchName;
253   }
254 
255   std::error_code EC;
256   raw_fd_ostream OS(Options.NoOutput ? "-" : Path.str(), EC,
257                     Options.RemarksFormat == remarks::Format::Bitstream
258                         ? sys::fs::OF_None
259                         : sys::fs::OF_Text);
260   if (EC)
261     return errorCodeToError(EC);
262 
263   if (Error E = RL.serialize(OS, Options.RemarksFormat))
264     return E;
265 
266   return Error::success();
267 }
268 
269 ErrorOr<DWARFFile &>
270 DwarfLinkerForBinary::loadObject(const DebugMapObject &Obj,
271                                  const DebugMap &DebugMap,
272                                  remarks::RemarkLinker &RL) {
273   auto ErrorOrObj = loadObject(Obj, DebugMap.getTriple());
274 
275   if (ErrorOrObj) {
276     ContextForLinking.push_back(
277         std::unique_ptr<DWARFContext>(DWARFContext::create(*ErrorOrObj)));
278     AddressMapForLinking.push_back(
279         std::make_unique<AddressManager>(*this, *ErrorOrObj, Obj));
280 
281     ObjectsForLinking.push_back(std::make_unique<DWARFFile>(
282         Obj.getObjectFilename(), ContextForLinking.back().get(),
283         AddressMapForLinking.back().get(),
284         Obj.empty() ? Obj.getWarnings() : EmptyWarnings));
285 
286     Error E = RL.link(*ErrorOrObj);
287     if (Error NewE = handleErrors(
288             std::move(E), [&](std::unique_ptr<FileError> EC) -> Error {
289               return remarksErrorHandler(Obj, *this, std::move(EC));
290             }))
291       return errorToErrorCode(std::move(NewE));
292 
293     return *ObjectsForLinking.back();
294   }
295 
296   return ErrorOrObj.getError();
297 }
298 
299 static bool binaryHasStrippableSwiftReflectionSections(
300     const DebugMap &Map, const LinkOptions &Options, BinaryHolder &BinHolder) {
301   // If the input binary has strippable swift5 reflection sections, there is no
302   // need to copy them to the .dSYM. Only copy them for binaries where the
303   // linker omitted the reflection metadata.
304   if (!Map.getBinaryPath().empty() &&
305       Options.FileType == OutputFileType::Object) {
306 
307     auto ObjectEntry = BinHolder.getObjectEntry(Map.getBinaryPath());
308     // If ObjectEntry or Object has an error, no binary exists, therefore no
309     // reflection sections exist.
310     if (!ObjectEntry) {
311       // Any errors will be diagnosed later in the main loop, ignore them here.
312       llvm::consumeError(ObjectEntry.takeError());
313       return false;
314     }
315 
316     auto Object =
317         ObjectEntry->getObjectAs<object::MachOObjectFile>(Map.getTriple());
318     if (!Object) {
319       // Any errors will be diagnosed later in the main loop, ignore them here.
320       llvm::consumeError(Object.takeError());
321       return false;
322     }
323 
324     for (auto &Section : Object->sections()) {
325       llvm::Expected<llvm::StringRef> NameOrErr =
326           Object->getSectionName(Section.getRawDataRefImpl());
327       if (!NameOrErr) {
328         llvm::consumeError(NameOrErr.takeError());
329         continue;
330       }
331       NameOrErr->consume_back("__TEXT");
332       auto ReflectionSectionKind =
333           Object->mapReflectionSectionNameToEnumValue(*NameOrErr);
334       if (Object->isReflectionSectionStrippable(ReflectionSectionKind)) {
335         return true;
336       }
337     }
338   }
339   return false;
340 }
341 
342 /// Calculate the start of the strippable swift reflection sections in Dwarf.
343 /// Note that there's an assumption that the reflection sections will appear
344 /// in alphabetic order.
345 static std::vector<uint64_t>
346 calculateStartOfStrippableReflectionSections(const DebugMap &Map) {
347   using llvm::binaryformat::Swift5ReflectionSectionKind;
348   uint64_t AssocTySize = 0;
349   uint64_t FieldMdSize = 0;
350   for (const auto &Obj : Map.objects()) {
351     auto OF =
352         llvm::object::ObjectFile::createObjectFile(Obj->getObjectFilename());
353     if (!OF) {
354       llvm::consumeError(OF.takeError());
355       continue;
356     }
357     if (auto *MO = dyn_cast<llvm::object::MachOObjectFile>(OF->getBinary())) {
358       for (auto &Section : MO->sections()) {
359         llvm::Expected<llvm::StringRef> NameOrErr =
360             MO->getSectionName(Section.getRawDataRefImpl());
361         if (!NameOrErr) {
362           llvm::consumeError(NameOrErr.takeError());
363           continue;
364         }
365         NameOrErr->consume_back("__TEXT");
366         auto ReflSectionKind =
367             MO->mapReflectionSectionNameToEnumValue(*NameOrErr);
368         switch (ReflSectionKind) {
369         case Swift5ReflectionSectionKind::assocty:
370           AssocTySize += Section.getSize();
371           break;
372         case Swift5ReflectionSectionKind::fieldmd:
373           FieldMdSize += Section.getSize();
374           break;
375         default:
376           break;
377         }
378       }
379     }
380   }
381   // Initialize the vector with enough space to fit every reflection section
382   // kind.
383   std::vector<uint64_t> SectionToOffset(Swift5ReflectionSectionKind::last, 0);
384   SectionToOffset[Swift5ReflectionSectionKind::assocty] = 0;
385   SectionToOffset[Swift5ReflectionSectionKind::fieldmd] =
386       llvm::alignTo(AssocTySize, 4);
387   SectionToOffset[Swift5ReflectionSectionKind::reflstr] = llvm::alignTo(
388       SectionToOffset[Swift5ReflectionSectionKind::fieldmd] + FieldMdSize, 4);
389 
390   return SectionToOffset;
391 }
392 
393 void DwarfLinkerForBinary::collectRelocationsToApplyToSwiftReflectionSections(
394     const object::SectionRef &Section, StringRef &Contents,
395     const llvm::object::MachOObjectFile *MO,
396     const std::vector<uint64_t> &SectionToOffsetInDwarf,
397     const llvm::dsymutil::DebugMapObject *Obj,
398     std::vector<MachOUtils::DwarfRelocationApplicationInfo> &RelocationsToApply)
399     const {
400   for (auto It = Section.relocation_begin(); It != Section.relocation_end();
401        ++It) {
402     object::DataRefImpl RelocDataRef = It->getRawDataRefImpl();
403     MachO::any_relocation_info MachOReloc = MO->getRelocation(RelocDataRef);
404 
405     if (!object::MachOObjectFile::isMachOPairedReloc(
406             MO->getAnyRelocationType(MachOReloc), MO->getArch())) {
407       reportWarning(
408           "Unimplemented relocation type in strippable reflection section ",
409           Obj->getObjectFilename());
410       continue;
411     }
412 
413     auto CalculateAddressOfSymbolInDwarfSegment =
414         [&]() -> llvm::Optional<int64_t> {
415       auto Symbol = It->getSymbol();
416       auto SymbolAbsoluteAddress = Symbol->getAddress();
417       if (!SymbolAbsoluteAddress)
418         return {};
419       auto Section = Symbol->getSection();
420       if (!Section) {
421         llvm::consumeError(Section.takeError());
422         return {};
423       }
424 
425       if ((*Section)->getObject()->section_end() == *Section)
426         return {};
427 
428       auto SectionStart = (*Section)->getAddress();
429       auto SymbolAddressInSection = *SymbolAbsoluteAddress - SectionStart;
430       auto SectionName = (*Section)->getName();
431       if (!SectionName)
432         return {};
433       auto ReflSectionKind =
434           MO->mapReflectionSectionNameToEnumValue(*SectionName);
435 
436       int64_t SectionStartInLinkedBinary =
437           SectionToOffsetInDwarf[ReflSectionKind];
438 
439       auto Addr = SectionStartInLinkedBinary + SymbolAddressInSection;
440       return Addr;
441     };
442 
443     // The first symbol should always be in the section we're currently
444     // iterating over.
445     auto FirstSymbolAddress = CalculateAddressOfSymbolInDwarfSegment();
446     ++It;
447 
448     bool ShouldSubtractDwarfVM = false;
449     // For the second symbol there are two possibilities.
450     llvm::Optional<int64_t> SecondSymbolAddress;
451     auto Sym = It->getSymbol();
452     if (Sym != MO->symbol_end()) {
453       Expected<StringRef> SymbolName = Sym->getName();
454       if (SymbolName) {
455         if (const auto *Mapping = Obj->lookupSymbol(*SymbolName)) {
456           // First possibility: the symbol exists in the binary, and exists in a
457           // non-strippable section (for example, typeref, or __TEXT,__const),
458           // in which case we look up its address in the  binary, which dsymutil
459           // will copy verbatim.
460           SecondSymbolAddress = Mapping->getValue().BinaryAddress;
461           // Since the symbols live in different segments, we have to substract
462           // the start of the Dwarf's vmaddr so the value calculated points to
463           // the correct place.
464           ShouldSubtractDwarfVM = true;
465         }
466       }
467     }
468 
469     if (!SecondSymbolAddress) {
470       // Second possibility, this symbol is not present in the main binary, and
471       // must be in one of the strippable sections (for example, reflstr).
472       // Calculate its address in the same way as we did the first one.
473       SecondSymbolAddress = CalculateAddressOfSymbolInDwarfSegment();
474     }
475 
476     if (!FirstSymbolAddress || !SecondSymbolAddress)
477       continue;
478 
479     auto SectionName = Section.getName();
480     if (!SectionName)
481       continue;
482 
483     int32_t Addend;
484     memcpy(&Addend, Contents.data() + It->getOffset(), sizeof(int32_t));
485     int32_t Value = (*SecondSymbolAddress + Addend) - *FirstSymbolAddress;
486     auto ReflSectionKind =
487         MO->mapReflectionSectionNameToEnumValue(*SectionName);
488     uint64_t AddressFromDwarfVM =
489         SectionToOffsetInDwarf[ReflSectionKind] + It->getOffset();
490     RelocationsToApply.emplace_back(AddressFromDwarfVM, Value,
491                                     ShouldSubtractDwarfVM);
492   }
493 }
494 
495 void DwarfLinkerForBinary::copySwiftReflectionMetadata(
496     const llvm::dsymutil::DebugMapObject *Obj, DwarfStreamer *Streamer,
497     std::vector<uint64_t> &SectionToOffsetInDwarf,
498     std::vector<MachOUtils::DwarfRelocationApplicationInfo>
499         &RelocationsToApply) {
500   using binaryformat::Swift5ReflectionSectionKind;
501   auto OF =
502       llvm::object::ObjectFile::createObjectFile(Obj->getObjectFilename());
503   if (!OF) {
504     llvm::consumeError(OF.takeError());
505     return;
506   }
507   if (auto *MO = dyn_cast<llvm::object::MachOObjectFile>(OF->getBinary())) {
508     // Collect the swift reflection sections before emitting them. This is
509     // done so we control the order they're emitted.
510     std::array<Optional<object::SectionRef>,
511                Swift5ReflectionSectionKind::last + 1>
512         SwiftSections;
513     for (auto &Section : MO->sections()) {
514       llvm::Expected<llvm::StringRef> NameOrErr =
515           MO->getSectionName(Section.getRawDataRefImpl());
516       if (!NameOrErr) {
517         llvm::consumeError(NameOrErr.takeError());
518         continue;
519       }
520       NameOrErr->consume_back("__TEXT");
521       auto ReflSectionKind =
522           MO->mapReflectionSectionNameToEnumValue(*NameOrErr);
523       if (MO->isReflectionSectionStrippable(ReflSectionKind))
524         SwiftSections[ReflSectionKind] = Section;
525     }
526     // Make sure we copy the sections in alphabetic order.
527     auto SectionKindsToEmit = {Swift5ReflectionSectionKind::assocty,
528                                Swift5ReflectionSectionKind::fieldmd,
529                                Swift5ReflectionSectionKind::reflstr};
530     for (auto SectionKind : SectionKindsToEmit) {
531       if (!SwiftSections[SectionKind])
532         continue;
533       auto &Section = *SwiftSections[SectionKind];
534       llvm::Expected<llvm::StringRef> SectionContents = Section.getContents();
535       if (!SectionContents)
536         continue;
537       const auto *MO =
538           llvm::cast<llvm::object::MachOObjectFile>(Section.getObject());
539       collectRelocationsToApplyToSwiftReflectionSections(
540           Section, *SectionContents, MO, SectionToOffsetInDwarf, Obj,
541           RelocationsToApply);
542       // Update the section start with the current section's contribution, so
543       // the next section we copy from a different .o file points to the correct
544       // place.
545       SectionToOffsetInDwarf[SectionKind] += Section.getSize();
546       Streamer->emitSwiftReflectionSection(SectionKind, *SectionContents,
547                                            Section.getAlignment(),
548                                            Section.getSize());
549     }
550   }
551 }
552 
553 bool DwarfLinkerForBinary::link(const DebugMap &Map) {
554   if (!createStreamer(Map.getTriple(), OutFile))
555     return false;
556 
557   ObjectsForLinking.clear();
558   ContextForLinking.clear();
559   AddressMapForLinking.clear();
560 
561   DebugMap DebugMap(Map.getTriple(), Map.getBinaryPath());
562 
563   DWARFLinker GeneralLinker(Streamer.get(), DwarfLinkerClient::Dsymutil);
564 
565   remarks::RemarkLinker RL;
566   if (!Options.RemarksPrependPath.empty())
567     RL.setExternalFilePrependPath(Options.RemarksPrependPath);
568   GeneralLinker.setObjectPrefixMap(&Options.ObjectPrefixMap);
569 
570   std::function<StringRef(StringRef)> TranslationLambda = [&](StringRef Input) {
571     assert(Options.Translator);
572     return Options.Translator(Input);
573   };
574 
575   GeneralLinker.setVerbosity(Options.Verbose);
576   GeneralLinker.setStatistics(Options.Statistics);
577   GeneralLinker.setVerifyInputDWARF(Options.VerifyInputDWARF);
578   GeneralLinker.setNoOutput(Options.NoOutput);
579   GeneralLinker.setNoODR(Options.NoODR);
580   GeneralLinker.setUpdate(Options.Update);
581   GeneralLinker.setNumThreads(Options.Threads);
582   GeneralLinker.setAccelTableKind(Options.TheAccelTableKind);
583   GeneralLinker.setPrependPath(Options.PrependPath);
584   GeneralLinker.setKeepFunctionForStatic(Options.KeepFunctionForStatic);
585   if (Options.Translator)
586     GeneralLinker.setStringsTranslator(TranslationLambda);
587   GeneralLinker.setWarningHandler(
588       [&](const Twine &Warning, StringRef Context, const DWARFDie *DIE) {
589         reportWarning(Warning, Context, DIE);
590       });
591   GeneralLinker.setErrorHandler(
592       [&](const Twine &Error, StringRef Context, const DWARFDie *) {
593         error(Error, Context);
594       });
595   GeneralLinker.setObjFileLoader(
596       [&DebugMap, &RL, this](StringRef ContainerName,
597                              StringRef Path) -> ErrorOr<DWARFFile &> {
598         auto &Obj = DebugMap.addDebugMapObject(
599             Path, sys::TimePoint<std::chrono::seconds>(), MachO::N_OSO);
600 
601         if (auto ErrorOrObj = loadObject(Obj, DebugMap, RL)) {
602           return *ErrorOrObj;
603         } else {
604           // Try and emit more helpful warnings by applying some heuristics.
605           StringRef ObjFile = ContainerName;
606           bool IsClangModule = sys::path::extension(Path).equals(".pcm");
607           bool IsArchive = ObjFile.endswith(")");
608 
609           if (IsClangModule) {
610             StringRef ModuleCacheDir = sys::path::parent_path(Path);
611             if (sys::fs::exists(ModuleCacheDir)) {
612               // If the module's parent directory exists, we assume that the
613               // module cache has expired and was pruned by clang.  A more
614               // adventurous dsymutil would invoke clang to rebuild the module
615               // now.
616               if (!ModuleCacheHintDisplayed) {
617                 WithColor::note()
618                     << "The clang module cache may have expired since "
619                        "this object file was built. Rebuilding the "
620                        "object file will rebuild the module cache.\n";
621                 ModuleCacheHintDisplayed = true;
622               }
623             } else if (IsArchive) {
624               // If the module cache directory doesn't exist at all and the
625               // object file is inside a static library, we assume that the
626               // static library was built on a different machine. We don't want
627               // to discourage module debugging for convenience libraries within
628               // a project though.
629               if (!ArchiveHintDisplayed) {
630                 WithColor::note()
631                     << "Linking a static library that was built with "
632                        "-gmodules, but the module cache was not found.  "
633                        "Redistributable static libraries should never be "
634                        "built with module debugging enabled.  The debug "
635                        "experience will be degraded due to incomplete "
636                        "debug information.\n";
637                 ArchiveHintDisplayed = true;
638               }
639             }
640           }
641 
642           return ErrorOrObj.getError();
643         }
644 
645         llvm_unreachable("Unhandled DebugMap object");
646       });
647   GeneralLinker.setSwiftInterfacesMap(&ParseableSwiftInterfaces);
648   bool ReflectionSectionsPresentInBinary = false;
649   // If there is no output specified, no point in checking the binary for swift5
650   // reflection sections.
651   if (!Options.NoOutput) {
652     ReflectionSectionsPresentInBinary =
653         binaryHasStrippableSwiftReflectionSections(Map, Options, BinHolder);
654   }
655 
656   std::vector<MachOUtils::DwarfRelocationApplicationInfo> RelocationsToApply;
657   if (!Options.NoOutput && !ReflectionSectionsPresentInBinary) {
658     auto SectionToOffsetInDwarf =
659         calculateStartOfStrippableReflectionSections(Map);
660     for (const auto &Obj : Map.objects())
661       copySwiftReflectionMetadata(Obj.get(), Streamer.get(),
662                                   SectionToOffsetInDwarf, RelocationsToApply);
663   }
664 
665   for (const auto &Obj : Map.objects()) {
666     // N_AST objects (swiftmodule files) should get dumped directly into the
667     // appropriate DWARF section.
668     if (Obj->getType() == MachO::N_AST) {
669       if (Options.Verbose)
670         outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
671 
672       StringRef File = Obj->getObjectFilename();
673       auto ErrorOrMem = MemoryBuffer::getFile(File);
674       if (!ErrorOrMem) {
675         warn("Could not open '" + File + "'\n");
676         continue;
677       }
678       sys::fs::file_status Stat;
679       if (auto Err = sys::fs::status(File, Stat)) {
680         warn(Err.message());
681         continue;
682       }
683       if (!Options.NoTimestamp) {
684         // The modification can have sub-second precision so we need to cast
685         // away the extra precision that's not present in the debug map.
686         auto ModificationTime =
687             std::chrono::time_point_cast<std::chrono::seconds>(
688                 Stat.getLastModificationTime());
689         if (Obj->getTimestamp() != sys::TimePoint<>() &&
690             ModificationTime != Obj->getTimestamp()) {
691           // Not using the helper here as we can easily stream TimePoint<>.
692           WithColor::warning()
693               << File << ": timestamp mismatch between swift interface file ("
694               << sys::TimePoint<>(ModificationTime) << ") and debug map ("
695               << sys::TimePoint<>(Obj->getTimestamp()) << ")\n";
696           continue;
697         }
698       }
699 
700       // Copy the module into the .swift_ast section.
701       if (!Options.NoOutput)
702         Streamer->emitSwiftAST((*ErrorOrMem)->getBuffer());
703 
704       continue;
705     }
706     if (auto ErrorOrObj = loadObject(*Obj, Map, RL))
707       GeneralLinker.addObjectFile(*ErrorOrObj);
708     else {
709       ObjectsForLinking.push_back(std::make_unique<DWARFFile>(
710           Obj->getObjectFilename(), nullptr, nullptr,
711           Obj->empty() ? Obj->getWarnings() : EmptyWarnings));
712       GeneralLinker.addObjectFile(*ObjectsForLinking.back());
713     }
714   }
715 
716   // link debug info for loaded object files.
717   GeneralLinker.link();
718 
719   StringRef ArchName = Map.getTriple().getArchName();
720   if (Error E = emitRemarks(Options, Map.getBinaryPath(), ArchName, RL))
721     return error(toString(std::move(E)));
722 
723   if (Options.NoOutput)
724     return true;
725 
726   if (Options.ResourceDir && !ParseableSwiftInterfaces.empty()) {
727     StringRef ArchName = Triple::getArchTypeName(Map.getTriple().getArch());
728     if (auto E =
729             copySwiftInterfaces(ParseableSwiftInterfaces, ArchName, Options))
730       return error(toString(std::move(E)));
731   }
732 
733   if (Map.getTriple().isOSDarwin() && !Map.getBinaryPath().empty() &&
734       Options.FileType == OutputFileType::Object)
735     return MachOUtils::generateDsymCompanion(
736         Options.VFS, Map, Options.Translator,
737         *Streamer->getAsmPrinter().OutStreamer, OutFile, RelocationsToApply);
738 
739   Streamer->finish();
740   return true;
741 }
742 
743 /// Iterate over the relocations of the given \p Section and
744 /// store the ones that correspond to debug map entries into the
745 /// ValidRelocs array.
746 void DwarfLinkerForBinary::AddressManager::findValidRelocsMachO(
747     const object::SectionRef &Section, const object::MachOObjectFile &Obj,
748     const DebugMapObject &DMO, std::vector<ValidReloc> &ValidRelocs) {
749   Expected<StringRef> ContentsOrErr = Section.getContents();
750   if (!ContentsOrErr) {
751     consumeError(ContentsOrErr.takeError());
752     Linker.reportWarning("error reading section", DMO.getObjectFilename());
753     return;
754   }
755   DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian(), 0);
756   bool SkipNext = false;
757 
758   for (const object::RelocationRef &Reloc : Section.relocations()) {
759     if (SkipNext) {
760       SkipNext = false;
761       continue;
762     }
763 
764     object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
765     MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
766 
767     if (object::MachOObjectFile::isMachOPairedReloc(Obj.getAnyRelocationType(MachOReloc),
768                            Obj.getArch())) {
769       SkipNext = true;
770       Linker.reportWarning("unsupported relocation in " + *Section.getName() +
771                                " section.",
772                            DMO.getObjectFilename());
773       continue;
774     }
775 
776     unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
777     uint64_t Offset64 = Reloc.getOffset();
778     if ((RelocSize != 4 && RelocSize != 8)) {
779       Linker.reportWarning("unsupported relocation in " + *Section.getName() +
780                                " section.",
781                            DMO.getObjectFilename());
782       continue;
783     }
784     uint64_t OffsetCopy = Offset64;
785     // Mach-o uses REL relocations, the addend is at the relocation offset.
786     uint64_t Addend = Data.getUnsigned(&OffsetCopy, RelocSize);
787     uint64_t SymAddress;
788     int64_t SymOffset;
789 
790     if (Obj.isRelocationScattered(MachOReloc)) {
791       // The address of the base symbol for scattered relocations is
792       // stored in the reloc itself. The actual addend will store the
793       // base address plus the offset.
794       SymAddress = Obj.getScatteredRelocationValue(MachOReloc);
795       SymOffset = int64_t(Addend) - SymAddress;
796     } else {
797       SymAddress = Addend;
798       SymOffset = 0;
799     }
800 
801     auto Sym = Reloc.getSymbol();
802     if (Sym != Obj.symbol_end()) {
803       Expected<StringRef> SymbolName = Sym->getName();
804       if (!SymbolName) {
805         consumeError(SymbolName.takeError());
806         Linker.reportWarning("error getting relocation symbol name.",
807                              DMO.getObjectFilename());
808         continue;
809       }
810       if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
811         ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
812     } else if (const auto *Mapping = DMO.lookupObjectAddress(SymAddress)) {
813       // Do not store the addend. The addend was the address of the symbol in
814       // the object file, the address in the binary that is stored in the debug
815       // map doesn't need to be offset.
816       ValidRelocs.emplace_back(Offset64, RelocSize, SymOffset, Mapping);
817     }
818   }
819 }
820 
821 /// Dispatch the valid relocation finding logic to the
822 /// appropriate handler depending on the object file format.
823 bool DwarfLinkerForBinary::AddressManager::findValidRelocs(
824     const object::SectionRef &Section, const object::ObjectFile &Obj,
825     const DebugMapObject &DMO, std::vector<ValidReloc> &Relocs) {
826   // Dispatch to the right handler depending on the file type.
827   if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
828     findValidRelocsMachO(Section, *MachOObj, DMO, Relocs);
829   else
830     Linker.reportWarning(Twine("unsupported object file type: ") +
831                              Obj.getFileName(),
832                          DMO.getObjectFilename());
833   if (Relocs.empty())
834     return false;
835 
836   // Sort the relocations by offset. We will walk the DIEs linearly in
837   // the file, this allows us to just keep an index in the relocation
838   // array that we advance during our walk, rather than resorting to
839   // some associative container. See DwarfLinkerForBinary::NextValidReloc.
840   llvm::sort(Relocs);
841   return true;
842 }
843 
844 /// Look for relocations in the debug_info and debug_addr section that match
845 /// entries in the debug map. These relocations will drive the Dwarf link by
846 /// indicating which DIEs refer to symbols present in the linked binary.
847 /// \returns whether there are any valid relocations in the debug info.
848 bool DwarfLinkerForBinary::AddressManager::findValidRelocsInDebugSections(
849     const object::ObjectFile &Obj, const DebugMapObject &DMO) {
850   // Find the debug_info section.
851   bool FoundValidRelocs = false;
852   for (const object::SectionRef &Section : Obj.sections()) {
853     StringRef SectionName;
854     if (Expected<StringRef> NameOrErr = Section.getName())
855       SectionName = *NameOrErr;
856     else
857       consumeError(NameOrErr.takeError());
858 
859     SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
860     if (SectionName == "debug_info")
861       FoundValidRelocs |=
862           findValidRelocs(Section, Obj, DMO, ValidDebugInfoRelocs);
863     if (SectionName == "debug_addr")
864       FoundValidRelocs |=
865           findValidRelocs(Section, Obj, DMO, ValidDebugAddrRelocs);
866   }
867   return FoundValidRelocs;
868 }
869 
870 std::vector<DwarfLinkerForBinary::AddressManager::ValidReloc>
871 DwarfLinkerForBinary::AddressManager::getRelocations(
872     const std::vector<ValidReloc> &Relocs, uint64_t StartPos, uint64_t EndPos) {
873   std::vector<DwarfLinkerForBinary::AddressManager::ValidReloc> Res;
874 
875   auto CurReloc = partition_point(Relocs, [StartPos](const ValidReloc &Reloc) {
876     return Reloc.Offset < StartPos;
877   });
878 
879   while (CurReloc != Relocs.end() && CurReloc->Offset >= StartPos &&
880          CurReloc->Offset < EndPos) {
881     Res.push_back(*CurReloc);
882     CurReloc++;
883   }
884 
885   return Res;
886 }
887 
888 void DwarfLinkerForBinary::AddressManager::printReloc(const ValidReloc &Reloc) {
889   const auto &Mapping = Reloc.Mapping->getValue();
890   const uint64_t ObjectAddress = Mapping.ObjectAddress
891                                      ? uint64_t(*Mapping.ObjectAddress)
892                                      : std::numeric_limits<uint64_t>::max();
893 
894   outs() << "Found valid debug map entry: " << Reloc.Mapping->getKey() << "\t"
895          << format("0x%016" PRIx64 " => 0x%016" PRIx64 "\n", ObjectAddress,
896                    uint64_t(Mapping.BinaryAddress));
897 }
898 
899 void DwarfLinkerForBinary::AddressManager::fillDieInfo(
900     const ValidReloc &Reloc, CompileUnit::DIEInfo &Info) {
901   Info.AddrAdjust = relocate(Reloc);
902   if (Reloc.Mapping->getValue().ObjectAddress)
903     Info.AddrAdjust -= uint64_t(*Reloc.Mapping->getValue().ObjectAddress);
904   Info.InDebugMap = true;
905 }
906 
907 bool DwarfLinkerForBinary::AddressManager::hasValidRelocationAt(
908     const std::vector<ValidReloc> &AllRelocs, uint64_t StartOffset,
909     uint64_t EndOffset, CompileUnit::DIEInfo &Info) {
910   std::vector<ValidReloc> Relocs =
911       getRelocations(AllRelocs, StartOffset, EndOffset);
912 
913   if (Relocs.size() == 0)
914     return false;
915 
916   if (Linker.Options.Verbose)
917     printReloc(Relocs[0]);
918   fillDieInfo(Relocs[0], Info);
919 
920   return true;
921 }
922 
923 /// Get the starting and ending (exclusive) offset for the
924 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
925 /// supposed to point to the position of the first attribute described
926 /// by \p Abbrev.
927 /// \return [StartOffset, EndOffset) as a pair.
928 static std::pair<uint64_t, uint64_t>
929 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
930                     uint64_t Offset, const DWARFUnit &Unit) {
931   DataExtractor Data = Unit.getDebugInfoExtractor();
932 
933   for (unsigned I = 0; I < Idx; ++I)
934     DWARFFormValue::skipValue(Abbrev->getFormByIndex(I), Data, &Offset,
935                               Unit.getFormParams());
936 
937   uint64_t End = Offset;
938   DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
939                             Unit.getFormParams());
940 
941   return std::make_pair(Offset, End);
942 }
943 
944 bool DwarfLinkerForBinary::AddressManager::hasLiveMemoryLocation(
945     const DWARFDie &DIE, CompileUnit::DIEInfo &MyInfo) {
946   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
947 
948   Optional<uint32_t> LocationIdx =
949       Abbrev->findAttributeIndex(dwarf::DW_AT_location);
950   if (!LocationIdx)
951     return false;
952 
953   uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
954   uint64_t LocationOffset, LocationEndOffset;
955   std::tie(LocationOffset, LocationEndOffset) =
956       getAttributeOffsets(Abbrev, *LocationIdx, Offset, *DIE.getDwarfUnit());
957 
958   // FIXME: Support relocations debug_addr.
959   return hasValidRelocationAt(ValidDebugInfoRelocs, LocationOffset,
960                               LocationEndOffset, MyInfo);
961 }
962 
963 bool DwarfLinkerForBinary::AddressManager::hasLiveAddressRange(
964     const DWARFDie &DIE, CompileUnit::DIEInfo &MyInfo) {
965   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
966 
967   Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
968   if (!LowPcIdx)
969     return false;
970 
971   dwarf::Form Form = Abbrev->getFormByIndex(*LowPcIdx);
972 
973   if (Form == dwarf::DW_FORM_addr) {
974     uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
975     uint64_t LowPcOffset, LowPcEndOffset;
976     std::tie(LowPcOffset, LowPcEndOffset) =
977         getAttributeOffsets(Abbrev, *LowPcIdx, Offset, *DIE.getDwarfUnit());
978     return hasValidRelocationAt(ValidDebugInfoRelocs, LowPcOffset,
979                                 LowPcEndOffset, MyInfo);
980   }
981 
982   if (Form == dwarf::DW_FORM_addrx) {
983     Optional<DWARFFormValue> AddrValue = DIE.find(dwarf::DW_AT_low_pc);
984     if (Optional<uint64_t> AddrOffsetSectionBase =
985             DIE.getDwarfUnit()->getAddrOffsetSectionBase()) {
986       uint64_t StartOffset = *AddrOffsetSectionBase + AddrValue->getRawUValue();
987       uint64_t EndOffset =
988           StartOffset + DIE.getDwarfUnit()->getAddressByteSize();
989       return hasValidRelocationAt(ValidDebugAddrRelocs, StartOffset, EndOffset,
990                                   MyInfo);
991     } else
992       Linker.reportWarning("no base offset for address table", SrcFileName);
993   }
994 
995   return false;
996 }
997 
998 uint64_t
999 DwarfLinkerForBinary::AddressManager::relocate(const ValidReloc &Reloc) const {
1000   return Reloc.Mapping->getValue().BinaryAddress + Reloc.Addend;
1001 }
1002 
1003 /// Apply the valid relocations found by findValidRelocs() to
1004 /// the buffer \p Data, taking into account that Data is at \p BaseOffset
1005 /// in the debug_info section.
1006 ///
1007 /// Like for findValidRelocs(), this function must be called with
1008 /// monotonic \p BaseOffset values.
1009 ///
1010 /// \returns whether any reloc has been applied.
1011 bool DwarfLinkerForBinary::AddressManager::applyValidRelocs(
1012     MutableArrayRef<char> Data, uint64_t BaseOffset, bool IsLittleEndian) {
1013   assert(areRelocationsResolved());
1014   std::vector<ValidReloc> Relocs = getRelocations(
1015       ValidDebugInfoRelocs, BaseOffset, BaseOffset + Data.size());
1016 
1017   for (const ValidReloc &CurReloc : Relocs) {
1018     assert(CurReloc.Offset - BaseOffset < Data.size());
1019     assert(CurReloc.Offset - BaseOffset + CurReloc.Size <= Data.size());
1020     char Buf[8];
1021     uint64_t Value = relocate(CurReloc);
1022     for (unsigned I = 0; I != CurReloc.Size; ++I) {
1023       unsigned Index = IsLittleEndian ? I : (CurReloc.Size - I - 1);
1024       Buf[I] = uint8_t(Value >> (Index * 8));
1025     }
1026     assert(CurReloc.Size <= sizeof(Buf));
1027     memcpy(&Data[CurReloc.Offset - BaseOffset], Buf, CurReloc.Size);
1028   }
1029 
1030   return Relocs.size() > 0;
1031 }
1032 
1033 llvm::Expected<uint64_t>
1034 DwarfLinkerForBinary::AddressManager::relocateIndexedAddr(uint64_t StartOffset,
1035                                                           uint64_t EndOffset) {
1036   std::vector<ValidReloc> Relocs =
1037       getRelocations(ValidDebugAddrRelocs, StartOffset, EndOffset);
1038   if (Relocs.size() == 0)
1039     return createStringError(
1040         std::make_error_code(std::errc::invalid_argument),
1041         "no relocation for offset %llu in debug_addr section", StartOffset);
1042 
1043   return relocate(Relocs[0]);
1044 }
1045 
1046 bool linkDwarf(raw_fd_ostream &OutFile, BinaryHolder &BinHolder,
1047                const DebugMap &DM, LinkOptions Options) {
1048   DwarfLinkerForBinary Linker(OutFile, BinHolder, std::move(Options));
1049   return Linker.link(DM);
1050 }
1051 
1052 } // namespace dsymutil
1053 } // namespace llvm
1054