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 binaryHasSwiftReflectionSections(const DebugMap &Map,
300                                              const LinkOptions &Options,
301                                              BinaryHolder &BinHolder) {
302   // If the input binary has swift5 reflection sections, there is no need to
303   // copy them to the .dSYM. Only copy them for binaries where the linker
304   // omitted the reflection metadata.
305   if (!Map.getBinaryPath().empty() &&
306       Options.FileType == OutputFileType::Object) {
307 
308     auto ObjectEntry = BinHolder.getObjectEntry(Map.getBinaryPath());
309     // If ObjectEntry or Object has an error, no binary exists, therefore no
310     // reflection sections exist.
311     if (!ObjectEntry) {
312       // Any errors will be diagnosed later in the main loop, ignore them here.
313       llvm::consumeError(ObjectEntry.takeError());
314       return false;
315     }
316 
317     auto Object =
318         ObjectEntry->getObjectAs<object::MachOObjectFile>(Map.getTriple());
319     if (!Object) {
320       // Any errors will be diagnosed later in the main loop, ignore them here.
321       llvm::consumeError(Object.takeError());
322       return false;
323     }
324 
325     for (auto &Section : Object->sections()) {
326       llvm::Expected<llvm::StringRef> NameOrErr =
327           Object->getSectionName(Section.getRawDataRefImpl());
328       if (!NameOrErr) {
329         llvm::consumeError(NameOrErr.takeError());
330         continue;
331       }
332       NameOrErr->consume_back("__TEXT");
333       if (Object->mapReflectionSectionNameToEnumValue(*NameOrErr) !=
334           llvm::binaryformat::Swift5ReflectionSectionKind::unknown) {
335         return true;
336       }
337     }
338   }
339   return false;
340 }
341 
342 static void
343 copySwiftReflectionMetadata(const llvm::dsymutil::DebugMapObject *Obj,
344                             DwarfStreamer *Streamer) {
345   auto OF =
346       llvm::object::ObjectFile::createObjectFile(Obj->getObjectFilename());
347   if (!OF) {
348     llvm::consumeError(OF.takeError());
349     return;
350   } else if (auto *MO =
351                  dyn_cast<llvm::object::MachOObjectFile>(OF->getBinary())) {
352     for (auto &Section : OF->getBinary()->sections()) {
353       llvm::Expected<llvm::StringRef> NameOrErr =
354           MO->getSectionName(Section.getRawDataRefImpl());
355       if (!NameOrErr) {
356         llvm::consumeError(NameOrErr.takeError());
357         continue;
358       }
359       llvm::Expected<llvm::StringRef> SectionContents = Section.getContents();
360       if (SectionContents) {
361         NameOrErr->consume_back("__TEXT");
362         Streamer->emitSwiftReflectionSection(
363             MO->mapReflectionSectionNameToEnumValue(*NameOrErr),
364             *SectionContents, Section.getAlignment(), Section.getSize());
365       }
366     }
367   }
368 }
369 
370 bool DwarfLinkerForBinary::link(const DebugMap &Map) {
371   if (!createStreamer(Map.getTriple(), OutFile))
372     return false;
373 
374   ObjectsForLinking.clear();
375   ContextForLinking.clear();
376   AddressMapForLinking.clear();
377 
378   DebugMap DebugMap(Map.getTriple(), Map.getBinaryPath());
379 
380   DWARFLinker GeneralLinker(Streamer.get(), DwarfLinkerClient::Dsymutil);
381 
382   remarks::RemarkLinker RL;
383   if (!Options.RemarksPrependPath.empty())
384     RL.setExternalFilePrependPath(Options.RemarksPrependPath);
385   GeneralLinker.setObjectPrefixMap(&Options.ObjectPrefixMap);
386 
387   std::function<StringRef(StringRef)> TranslationLambda = [&](StringRef Input) {
388     assert(Options.Translator);
389     return Options.Translator(Input);
390   };
391 
392   GeneralLinker.setVerbosity(Options.Verbose);
393   GeneralLinker.setStatistics(Options.Statistics);
394   GeneralLinker.setVerifyInputDWARF(Options.VerifyInputDWARF);
395   GeneralLinker.setNoOutput(Options.NoOutput);
396   GeneralLinker.setNoODR(Options.NoODR);
397   GeneralLinker.setUpdate(Options.Update);
398   GeneralLinker.setNumThreads(Options.Threads);
399   GeneralLinker.setAccelTableKind(Options.TheAccelTableKind);
400   GeneralLinker.setPrependPath(Options.PrependPath);
401   GeneralLinker.setKeepFunctionForStatic(Options.KeepFunctionForStatic);
402   if (Options.Translator)
403     GeneralLinker.setStringsTranslator(TranslationLambda);
404   GeneralLinker.setWarningHandler(
405       [&](const Twine &Warning, StringRef Context, const DWARFDie *DIE) {
406         reportWarning(Warning, Context, DIE);
407       });
408   GeneralLinker.setErrorHandler(
409       [&](const Twine &Error, StringRef Context, const DWARFDie *) {
410         error(Error, Context);
411       });
412   GeneralLinker.setObjFileLoader(
413       [&DebugMap, &RL, this](StringRef ContainerName,
414                              StringRef Path) -> ErrorOr<DWARFFile &> {
415         auto &Obj = DebugMap.addDebugMapObject(
416             Path, sys::TimePoint<std::chrono::seconds>(), MachO::N_OSO);
417 
418         if (auto ErrorOrObj = loadObject(Obj, DebugMap, RL)) {
419           return *ErrorOrObj;
420         } else {
421           // Try and emit more helpful warnings by applying some heuristics.
422           StringRef ObjFile = ContainerName;
423           bool IsClangModule = sys::path::extension(Path).equals(".pcm");
424           bool IsArchive = ObjFile.endswith(")");
425 
426           if (IsClangModule) {
427             StringRef ModuleCacheDir = sys::path::parent_path(Path);
428             if (sys::fs::exists(ModuleCacheDir)) {
429               // If the module's parent directory exists, we assume that the
430               // module cache has expired and was pruned by clang.  A more
431               // adventurous dsymutil would invoke clang to rebuild the module
432               // now.
433               if (!ModuleCacheHintDisplayed) {
434                 WithColor::note()
435                     << "The clang module cache may have expired since "
436                        "this object file was built. Rebuilding the "
437                        "object file will rebuild the module cache.\n";
438                 ModuleCacheHintDisplayed = true;
439               }
440             } else if (IsArchive) {
441               // If the module cache directory doesn't exist at all and the
442               // object file is inside a static library, we assume that the
443               // static library was built on a different machine. We don't want
444               // to discourage module debugging for convenience libraries within
445               // a project though.
446               if (!ArchiveHintDisplayed) {
447                 WithColor::note()
448                     << "Linking a static library that was built with "
449                        "-gmodules, but the module cache was not found.  "
450                        "Redistributable static libraries should never be "
451                        "built with module debugging enabled.  The debug "
452                        "experience will be degraded due to incomplete "
453                        "debug information.\n";
454                 ArchiveHintDisplayed = true;
455               }
456             }
457           }
458 
459           return ErrorOrObj.getError();
460         }
461 
462         llvm_unreachable("Unhandled DebugMap object");
463       });
464   GeneralLinker.setSwiftInterfacesMap(&ParseableSwiftInterfaces);
465   bool ReflectionSectionsPresentInBinary = false;
466   // If there is no output specified, no point in checking the binary for swift5
467   // reflection sections.
468   if (!Options.NoOutput) {
469     ReflectionSectionsPresentInBinary =
470         binaryHasSwiftReflectionSections(Map, Options, BinHolder);
471   }
472 
473   for (const auto &Obj : Map.objects()) {
474     // If there is no output specified or the reflection sections are present in
475     // the Input binary, there is no need to copy the Swift Reflection Metadata
476     if (!Options.NoOutput && !ReflectionSectionsPresentInBinary)
477       copySwiftReflectionMetadata(Obj.get(), Streamer.get());
478 
479     // N_AST objects (swiftmodule files) should get dumped directly into the
480     // appropriate DWARF section.
481     if (Obj->getType() == MachO::N_AST) {
482       if (Options.Verbose)
483         outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
484 
485       StringRef File = Obj->getObjectFilename();
486       auto ErrorOrMem = MemoryBuffer::getFile(File);
487       if (!ErrorOrMem) {
488         warn("Could not open '" + File + "'\n");
489         continue;
490       }
491       sys::fs::file_status Stat;
492       if (auto Err = sys::fs::status(File, Stat)) {
493         warn(Err.message());
494         continue;
495       }
496       if (!Options.NoTimestamp) {
497         // The modification can have sub-second precision so we need to cast
498         // away the extra precision that's not present in the debug map.
499         auto ModificationTime =
500             std::chrono::time_point_cast<std::chrono::seconds>(
501                 Stat.getLastModificationTime());
502         if (Obj->getTimestamp() != sys::TimePoint<>() &&
503             ModificationTime != Obj->getTimestamp()) {
504           // Not using the helper here as we can easily stream TimePoint<>.
505           WithColor::warning()
506               << File << ": timestamp mismatch between swift interface file ("
507               << sys::TimePoint<>(ModificationTime) << ") and debug map ("
508               << sys::TimePoint<>(Obj->getTimestamp()) << ")\n";
509           continue;
510         }
511       }
512 
513       // Copy the module into the .swift_ast section.
514       if (!Options.NoOutput)
515         Streamer->emitSwiftAST((*ErrorOrMem)->getBuffer());
516 
517       continue;
518     }
519     if (auto ErrorOrObj = loadObject(*Obj, Map, RL))
520       GeneralLinker.addObjectFile(*ErrorOrObj);
521     else {
522       ObjectsForLinking.push_back(std::make_unique<DWARFFile>(
523           Obj->getObjectFilename(), nullptr, nullptr,
524           Obj->empty() ? Obj->getWarnings() : EmptyWarnings));
525       GeneralLinker.addObjectFile(*ObjectsForLinking.back());
526     }
527   }
528 
529   // link debug info for loaded object files.
530   GeneralLinker.link();
531 
532   StringRef ArchName = Map.getTriple().getArchName();
533   if (Error E = emitRemarks(Options, Map.getBinaryPath(), ArchName, RL))
534     return error(toString(std::move(E)));
535 
536   if (Options.NoOutput)
537     return true;
538 
539   if (Options.ResourceDir && !ParseableSwiftInterfaces.empty()) {
540     StringRef ArchName = Triple::getArchTypeName(Map.getTriple().getArch());
541     if (auto E =
542             copySwiftInterfaces(ParseableSwiftInterfaces, ArchName, Options))
543       return error(toString(std::move(E)));
544   }
545 
546   if (Map.getTriple().isOSDarwin() && !Map.getBinaryPath().empty() &&
547       Options.FileType == OutputFileType::Object)
548     return MachOUtils::generateDsymCompanion(
549         Options.VFS, Map, Options.Translator,
550         *Streamer->getAsmPrinter().OutStreamer, OutFile);
551 
552   Streamer->finish();
553   return true;
554 }
555 
556 static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
557   switch (Arch) {
558   case Triple::x86:
559     return RelocType == MachO::GENERIC_RELOC_SECTDIFF ||
560            RelocType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
561   case Triple::x86_64:
562     return RelocType == MachO::X86_64_RELOC_SUBTRACTOR;
563   case Triple::arm:
564   case Triple::thumb:
565     return RelocType == MachO::ARM_RELOC_SECTDIFF ||
566            RelocType == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
567            RelocType == MachO::ARM_RELOC_HALF ||
568            RelocType == MachO::ARM_RELOC_HALF_SECTDIFF;
569   case Triple::aarch64:
570     return RelocType == MachO::ARM64_RELOC_SUBTRACTOR;
571   default:
572     return false;
573   }
574 }
575 
576 /// Iterate over the relocations of the given \p Section and
577 /// store the ones that correspond to debug map entries into the
578 /// ValidRelocs array.
579 void DwarfLinkerForBinary::AddressManager::findValidRelocsMachO(
580     const object::SectionRef &Section, const object::MachOObjectFile &Obj,
581     const DebugMapObject &DMO, std::vector<ValidReloc> &ValidRelocs) {
582   Expected<StringRef> ContentsOrErr = Section.getContents();
583   if (!ContentsOrErr) {
584     consumeError(ContentsOrErr.takeError());
585     Linker.reportWarning("error reading section", DMO.getObjectFilename());
586     return;
587   }
588   DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian(), 0);
589   bool SkipNext = false;
590 
591   for (const object::RelocationRef &Reloc : Section.relocations()) {
592     if (SkipNext) {
593       SkipNext = false;
594       continue;
595     }
596 
597     object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
598     MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
599 
600     if (isMachOPairedReloc(Obj.getAnyRelocationType(MachOReloc),
601                            Obj.getArch())) {
602       SkipNext = true;
603       Linker.reportWarning("unsupported relocation in " + *Section.getName() +
604                                " section.",
605                            DMO.getObjectFilename());
606       continue;
607     }
608 
609     unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
610     uint64_t Offset64 = Reloc.getOffset();
611     if ((RelocSize != 4 && RelocSize != 8)) {
612       Linker.reportWarning("unsupported relocation in " + *Section.getName() +
613                                " section.",
614                            DMO.getObjectFilename());
615       continue;
616     }
617     uint64_t OffsetCopy = Offset64;
618     // Mach-o uses REL relocations, the addend is at the relocation offset.
619     uint64_t Addend = Data.getUnsigned(&OffsetCopy, RelocSize);
620     uint64_t SymAddress;
621     int64_t SymOffset;
622 
623     if (Obj.isRelocationScattered(MachOReloc)) {
624       // The address of the base symbol for scattered relocations is
625       // stored in the reloc itself. The actual addend will store the
626       // base address plus the offset.
627       SymAddress = Obj.getScatteredRelocationValue(MachOReloc);
628       SymOffset = int64_t(Addend) - SymAddress;
629     } else {
630       SymAddress = Addend;
631       SymOffset = 0;
632     }
633 
634     auto Sym = Reloc.getSymbol();
635     if (Sym != Obj.symbol_end()) {
636       Expected<StringRef> SymbolName = Sym->getName();
637       if (!SymbolName) {
638         consumeError(SymbolName.takeError());
639         Linker.reportWarning("error getting relocation symbol name.",
640                              DMO.getObjectFilename());
641         continue;
642       }
643       if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
644         ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
645     } else if (const auto *Mapping = DMO.lookupObjectAddress(SymAddress)) {
646       // Do not store the addend. The addend was the address of the symbol in
647       // the object file, the address in the binary that is stored in the debug
648       // map doesn't need to be offset.
649       ValidRelocs.emplace_back(Offset64, RelocSize, SymOffset, Mapping);
650     }
651   }
652 }
653 
654 /// Dispatch the valid relocation finding logic to the
655 /// appropriate handler depending on the object file format.
656 bool DwarfLinkerForBinary::AddressManager::findValidRelocs(
657     const object::SectionRef &Section, const object::ObjectFile &Obj,
658     const DebugMapObject &DMO, std::vector<ValidReloc> &Relocs) {
659   // Dispatch to the right handler depending on the file type.
660   if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
661     findValidRelocsMachO(Section, *MachOObj, DMO, Relocs);
662   else
663     Linker.reportWarning(Twine("unsupported object file type: ") +
664                              Obj.getFileName(),
665                          DMO.getObjectFilename());
666   if (Relocs.empty())
667     return false;
668 
669   // Sort the relocations by offset. We will walk the DIEs linearly in
670   // the file, this allows us to just keep an index in the relocation
671   // array that we advance during our walk, rather than resorting to
672   // some associative container. See DwarfLinkerForBinary::NextValidReloc.
673   llvm::sort(Relocs);
674   return true;
675 }
676 
677 /// Look for relocations in the debug_info and debug_addr section that match
678 /// entries in the debug map. These relocations will drive the Dwarf link by
679 /// indicating which DIEs refer to symbols present in the linked binary.
680 /// \returns whether there are any valid relocations in the debug info.
681 bool DwarfLinkerForBinary::AddressManager::findValidRelocsInDebugSections(
682     const object::ObjectFile &Obj, const DebugMapObject &DMO) {
683   // Find the debug_info section.
684   bool FoundValidRelocs = false;
685   for (const object::SectionRef &Section : Obj.sections()) {
686     StringRef SectionName;
687     if (Expected<StringRef> NameOrErr = Section.getName())
688       SectionName = *NameOrErr;
689     else
690       consumeError(NameOrErr.takeError());
691 
692     SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
693     if (SectionName == "debug_info")
694       FoundValidRelocs |=
695           findValidRelocs(Section, Obj, DMO, ValidDebugInfoRelocs);
696     if (SectionName == "debug_addr")
697       FoundValidRelocs |=
698           findValidRelocs(Section, Obj, DMO, ValidDebugAddrRelocs);
699   }
700   return FoundValidRelocs;
701 }
702 
703 std::vector<DwarfLinkerForBinary::AddressManager::ValidReloc>
704 DwarfLinkerForBinary::AddressManager::getRelocations(
705     const std::vector<ValidReloc> &Relocs, uint64_t StartPos, uint64_t EndPos) {
706   std::vector<DwarfLinkerForBinary::AddressManager::ValidReloc> Res;
707 
708   auto CurReloc = partition_point(Relocs, [StartPos](const ValidReloc &Reloc) {
709     return Reloc.Offset < StartPos;
710   });
711 
712   while (CurReloc != Relocs.end() && CurReloc->Offset >= StartPos &&
713          CurReloc->Offset < EndPos) {
714     Res.push_back(*CurReloc);
715     CurReloc++;
716   }
717 
718   return Res;
719 }
720 
721 void DwarfLinkerForBinary::AddressManager::printReloc(const ValidReloc &Reloc) {
722   const auto &Mapping = Reloc.Mapping->getValue();
723   const uint64_t ObjectAddress = Mapping.ObjectAddress
724                                      ? uint64_t(*Mapping.ObjectAddress)
725                                      : std::numeric_limits<uint64_t>::max();
726 
727   outs() << "Found valid debug map entry: " << Reloc.Mapping->getKey() << "\t"
728          << format("0x%016" PRIx64 " => 0x%016" PRIx64 "\n", ObjectAddress,
729                    uint64_t(Mapping.BinaryAddress));
730 }
731 
732 void DwarfLinkerForBinary::AddressManager::fillDieInfo(
733     const ValidReloc &Reloc, CompileUnit::DIEInfo &Info) {
734   Info.AddrAdjust = relocate(Reloc);
735   if (Reloc.Mapping->getValue().ObjectAddress)
736     Info.AddrAdjust -= uint64_t(*Reloc.Mapping->getValue().ObjectAddress);
737   Info.InDebugMap = true;
738 }
739 
740 bool DwarfLinkerForBinary::AddressManager::hasValidRelocationAt(
741     const std::vector<ValidReloc> &AllRelocs, uint64_t StartOffset,
742     uint64_t EndOffset, CompileUnit::DIEInfo &Info) {
743   std::vector<ValidReloc> Relocs =
744       getRelocations(AllRelocs, StartOffset, EndOffset);
745 
746   if (Relocs.size() == 0)
747     return false;
748 
749   if (Linker.Options.Verbose)
750     printReloc(Relocs[0]);
751   fillDieInfo(Relocs[0], Info);
752 
753   return true;
754 }
755 
756 /// Get the starting and ending (exclusive) offset for the
757 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
758 /// supposed to point to the position of the first attribute described
759 /// by \p Abbrev.
760 /// \return [StartOffset, EndOffset) as a pair.
761 static std::pair<uint64_t, uint64_t>
762 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
763                     uint64_t Offset, const DWARFUnit &Unit) {
764   DataExtractor Data = Unit.getDebugInfoExtractor();
765 
766   for (unsigned I = 0; I < Idx; ++I)
767     DWARFFormValue::skipValue(Abbrev->getFormByIndex(I), Data, &Offset,
768                               Unit.getFormParams());
769 
770   uint64_t End = Offset;
771   DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
772                             Unit.getFormParams());
773 
774   return std::make_pair(Offset, End);
775 }
776 
777 bool DwarfLinkerForBinary::AddressManager::hasLiveMemoryLocation(
778     const DWARFDie &DIE, CompileUnit::DIEInfo &MyInfo) {
779   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
780 
781   Optional<uint32_t> LocationIdx =
782       Abbrev->findAttributeIndex(dwarf::DW_AT_location);
783   if (!LocationIdx)
784     return false;
785 
786   uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
787   uint64_t LocationOffset, LocationEndOffset;
788   std::tie(LocationOffset, LocationEndOffset) =
789       getAttributeOffsets(Abbrev, *LocationIdx, Offset, *DIE.getDwarfUnit());
790 
791   // FIXME: Support relocations debug_addr.
792   return hasValidRelocationAt(ValidDebugInfoRelocs, LocationOffset,
793                               LocationEndOffset, MyInfo);
794 }
795 
796 bool DwarfLinkerForBinary::AddressManager::hasLiveAddressRange(
797     const DWARFDie &DIE, CompileUnit::DIEInfo &MyInfo) {
798   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
799 
800   Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
801   if (!LowPcIdx)
802     return false;
803 
804   dwarf::Form Form = Abbrev->getFormByIndex(*LowPcIdx);
805 
806   if (Form == dwarf::DW_FORM_addr) {
807     uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
808     uint64_t LowPcOffset, LowPcEndOffset;
809     std::tie(LowPcOffset, LowPcEndOffset) =
810         getAttributeOffsets(Abbrev, *LowPcIdx, Offset, *DIE.getDwarfUnit());
811     return hasValidRelocationAt(ValidDebugInfoRelocs, LowPcOffset,
812                                 LowPcEndOffset, MyInfo);
813   }
814 
815   if (Form == dwarf::DW_FORM_addrx) {
816     Optional<DWARFFormValue> AddrValue = DIE.find(dwarf::DW_AT_low_pc);
817     if (Optional<uint64_t> AddrOffsetSectionBase =
818             DIE.getDwarfUnit()->getAddrOffsetSectionBase()) {
819       uint64_t StartOffset = *AddrOffsetSectionBase + AddrValue->getRawUValue();
820       uint64_t EndOffset =
821           StartOffset + DIE.getDwarfUnit()->getAddressByteSize();
822       return hasValidRelocationAt(ValidDebugAddrRelocs, StartOffset, EndOffset,
823                                   MyInfo);
824     } else
825       Linker.reportWarning("no base offset for address table", SrcFileName);
826   }
827 
828   return false;
829 }
830 
831 uint64_t
832 DwarfLinkerForBinary::AddressManager::relocate(const ValidReloc &Reloc) const {
833   return Reloc.Mapping->getValue().BinaryAddress + Reloc.Addend;
834 }
835 
836 /// Apply the valid relocations found by findValidRelocs() to
837 /// the buffer \p Data, taking into account that Data is at \p BaseOffset
838 /// in the debug_info section.
839 ///
840 /// Like for findValidRelocs(), this function must be called with
841 /// monotonic \p BaseOffset values.
842 ///
843 /// \returns whether any reloc has been applied.
844 bool DwarfLinkerForBinary::AddressManager::applyValidRelocs(
845     MutableArrayRef<char> Data, uint64_t BaseOffset, bool IsLittleEndian) {
846   assert(areRelocationsResolved());
847   std::vector<ValidReloc> Relocs = getRelocations(
848       ValidDebugInfoRelocs, BaseOffset, BaseOffset + Data.size());
849 
850   for (const ValidReloc &CurReloc : Relocs) {
851     assert(CurReloc.Offset - BaseOffset < Data.size());
852     assert(CurReloc.Offset - BaseOffset + CurReloc.Size <= Data.size());
853     char Buf[8];
854     uint64_t Value = relocate(CurReloc);
855     for (unsigned I = 0; I != CurReloc.Size; ++I) {
856       unsigned Index = IsLittleEndian ? I : (CurReloc.Size - I - 1);
857       Buf[I] = uint8_t(Value >> (Index * 8));
858     }
859     assert(CurReloc.Size <= sizeof(Buf));
860     memcpy(&Data[CurReloc.Offset - BaseOffset], Buf, CurReloc.Size);
861   }
862 
863   return Relocs.size() > 0;
864 }
865 
866 llvm::Expected<uint64_t>
867 DwarfLinkerForBinary::AddressManager::relocateIndexedAddr(uint64_t StartOffset,
868                                                           uint64_t EndOffset) {
869   std::vector<ValidReloc> Relocs =
870       getRelocations(ValidDebugAddrRelocs, StartOffset, EndOffset);
871   if (Relocs.size() == 0)
872     return createStringError(
873         std::make_error_code(std::errc::invalid_argument),
874         "no relocation for offset %llu in debug_addr section", StartOffset);
875 
876   return relocate(Relocs[0]);
877 }
878 
879 bool linkDwarf(raw_fd_ostream &OutFile, BinaryHolder &BinHolder,
880                const DebugMap &DM, LinkOptions Options) {
881   DwarfLinkerForBinary Linker(OutFile, BinHolder, std::move(Options));
882   return Linker.link(DM);
883 }
884 
885 } // namespace dsymutil
886 } // namespace llvm
887