Clang] Fix expansion of response files in -Wp after integrated-cc1 change
[llvm-project.git] / llvm / tools / dsymutil / DwarfLinkerForBinary.cpp
blobe6e7730651c8891a8f471875b742534a71e033f5
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 //===----------------------------------------------------------------------===//
9 #include "DwarfLinkerForBinary.h"
10 #include "BinaryHolder.h"
11 #include "DebugMap.h"
12 #include "DwarfStreamer.h"
13 #include "MachOUtils.h"
14 #include "dsymutil.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DenseMapInfo.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/Hashing.h"
22 #include "llvm/ADT/IntervalMap.h"
23 #include "llvm/ADT/None.h"
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/ADT/PointerIntPair.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/ADT/StringMap.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/Triple.h"
31 #include "llvm/ADT/Twine.h"
32 #include "llvm/BinaryFormat/Dwarf.h"
33 #include "llvm/BinaryFormat/MachO.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/Object/MachO.h"
64 #include "llvm/Object/ObjectFile.h"
65 #include "llvm/Object/SymbolicFile.h"
66 #include "llvm/Remarks/RemarkFormat.h"
67 #include "llvm/Remarks/RemarkLinker.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/TargetRegistry.h"
83 #include "llvm/Support/ThreadPool.h"
84 #include "llvm/Support/ToolOutputFile.h"
85 #include "llvm/Support/WithColor.h"
86 #include "llvm/Support/raw_ostream.h"
87 #include "llvm/Target/TargetMachine.h"
88 #include "llvm/Target/TargetOptions.h"
89 #include <algorithm>
90 #include <cassert>
91 #include <cinttypes>
92 #include <climits>
93 #include <cstdint>
94 #include <cstdlib>
95 #include <cstring>
96 #include <limits>
97 #include <map>
98 #include <memory>
99 #include <string>
100 #include <system_error>
101 #include <tuple>
102 #include <utility>
103 #include <vector>
105 namespace llvm {
106 namespace dsymutil {
108 /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
109 /// CompileUnit object instead.
110 static CompileUnit *getUnitForOffset(const UnitListTy &Units, uint64_t Offset) {
111 auto CU = std::upper_bound(
112 Units.begin(), Units.end(), Offset,
113 [](uint64_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
114 return LHS < RHS->getOrigUnit().getNextUnitOffset();
116 return CU != Units.end() ? CU->get() : nullptr;
119 /// Resolve the DIE attribute reference that has been extracted in \p RefValue.
120 /// The resulting DIE might be in another CompileUnit which is stored into \p
121 /// ReferencedCU. \returns null if resolving fails for any reason.
122 static DWARFDie resolveDIEReference(const DwarfLinkerForBinary &Linker,
123 const DebugMapObject &DMO,
124 const UnitListTy &Units,
125 const DWARFFormValue &RefValue,
126 const DWARFDie &DIE, CompileUnit *&RefCU) {
127 assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
128 uint64_t RefOffset = *RefValue.getAsReference();
129 if ((RefCU = getUnitForOffset(Units, RefOffset)))
130 if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
131 // In a file with broken references, an attribute might point to a NULL
132 // DIE.
133 if (!RefDie.isNULL())
134 return RefDie;
137 Linker.reportWarning("could not find referenced DIE", DMO, &DIE);
138 return DWARFDie();
141 /// \returns whether the passed \a Attr type might contain a DIE reference
142 /// suitable for ODR uniquing.
143 static bool isODRAttribute(uint16_t Attr) {
144 switch (Attr) {
145 default:
146 return false;
147 case dwarf::DW_AT_type:
148 case dwarf::DW_AT_containing_type:
149 case dwarf::DW_AT_specification:
150 case dwarf::DW_AT_abstract_origin:
151 case dwarf::DW_AT_import:
152 return true;
154 llvm_unreachable("Improper attribute.");
157 static bool isTypeTag(uint16_t Tag) {
158 switch (Tag) {
159 case dwarf::DW_TAG_array_type:
160 case dwarf::DW_TAG_class_type:
161 case dwarf::DW_TAG_enumeration_type:
162 case dwarf::DW_TAG_pointer_type:
163 case dwarf::DW_TAG_reference_type:
164 case dwarf::DW_TAG_string_type:
165 case dwarf::DW_TAG_structure_type:
166 case dwarf::DW_TAG_subroutine_type:
167 case dwarf::DW_TAG_typedef:
168 case dwarf::DW_TAG_union_type:
169 case dwarf::DW_TAG_ptr_to_member_type:
170 case dwarf::DW_TAG_set_type:
171 case dwarf::DW_TAG_subrange_type:
172 case dwarf::DW_TAG_base_type:
173 case dwarf::DW_TAG_const_type:
174 case dwarf::DW_TAG_constant:
175 case dwarf::DW_TAG_file_type:
176 case dwarf::DW_TAG_namelist:
177 case dwarf::DW_TAG_packed_type:
178 case dwarf::DW_TAG_volatile_type:
179 case dwarf::DW_TAG_restrict_type:
180 case dwarf::DW_TAG_atomic_type:
181 case dwarf::DW_TAG_interface_type:
182 case dwarf::DW_TAG_unspecified_type:
183 case dwarf::DW_TAG_shared_type:
184 return true;
185 default:
186 break;
188 return false;
191 static Error remarksErrorHandler(const DebugMapObject &DMO,
192 DwarfLinkerForBinary &Linker,
193 std::unique_ptr<FileError> FE) {
194 bool IsArchive = DMO.getObjectFilename().endswith(")");
195 // Don't report errors for missing remark files from static
196 // archives.
197 if (!IsArchive)
198 return Error(std::move(FE));
200 std::string Message = FE->message();
201 Error E = FE->takeError();
202 Error NewE = handleErrors(std::move(E), [&](std::unique_ptr<ECError> EC) {
203 if (EC->convertToErrorCode() != std::errc::no_such_file_or_directory)
204 return Error(std::move(EC));
206 Linker.reportWarning(Message, DMO);
207 return Error(Error::success());
210 if (!NewE)
211 return Error::success();
213 return createFileError(FE->getFileName(), std::move(NewE));
216 bool DwarfLinkerForBinary::DIECloner::getDIENames(const DWARFDie &Die,
217 AttributesInfo &Info,
218 OffsetsStringPool &StringPool,
219 bool StripTemplate) {
220 // This function will be called on DIEs having low_pcs and
221 // ranges. As getting the name might be more expansive, filter out
222 // blocks directly.
223 if (Die.getTag() == dwarf::DW_TAG_lexical_block)
224 return false;
226 // FIXME: a bit wasteful as the first getName might return the
227 // short name.
228 if (!Info.MangledName)
229 if (const char *MangledName = Die.getName(DINameKind::LinkageName))
230 Info.MangledName = StringPool.getEntry(MangledName);
232 if (!Info.Name)
233 if (const char *Name = Die.getName(DINameKind::ShortName))
234 Info.Name = StringPool.getEntry(Name);
236 if (StripTemplate && Info.Name && Info.MangledName != Info.Name) {
237 // FIXME: dsymutil compatibility. This is wrong for operator<
238 auto Split = Info.Name.getString().split('<');
239 if (!Split.second.empty())
240 Info.NameWithoutTemplate = StringPool.getEntry(Split.first);
243 return Info.Name || Info.MangledName;
246 /// Report a warning to the user, optionally including information about a
247 /// specific \p DIE related to the warning.
248 void DwarfLinkerForBinary::reportWarning(const Twine &Warning,
249 const DebugMapObject &DMO,
250 const DWARFDie *DIE) const {
251 StringRef Context = DMO.getObjectFilename();
252 warn(Warning, Context);
254 if (!Options.Verbose || !DIE)
255 return;
257 DIDumpOptions DumpOpts;
258 DumpOpts.ChildRecurseDepth = 0;
259 DumpOpts.Verbose = Options.Verbose;
261 WithColor::note() << " in DIE:\n";
262 DIE->dump(errs(), 6 /* Indent */, DumpOpts);
265 bool DwarfLinkerForBinary::createStreamer(const Triple &TheTriple,
266 raw_fd_ostream &OutFile) {
267 if (Options.NoOutput)
268 return true;
270 Streamer = std::make_unique<DwarfStreamer>(OutFile, Options);
271 return Streamer->init(TheTriple);
274 /// Resolve the relative path to a build artifact referenced by DWARF by
275 /// applying DW_AT_comp_dir.
276 static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
277 sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
280 /// Collect references to parseable Swift interfaces in imported
281 /// DW_TAG_module blocks.
282 static void analyzeImportedModule(
283 const DWARFDie &DIE, CompileUnit &CU,
284 std::map<std::string, std::string> &ParseableSwiftInterfaces,
285 std::function<void(const Twine &, const DWARFDie &)> ReportWarning) {
286 if (CU.getLanguage() != dwarf::DW_LANG_Swift)
287 return;
289 StringRef Path = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_include_path));
290 if (!Path.endswith(".swiftinterface"))
291 return;
292 if (Optional<DWARFFormValue> Val = DIE.find(dwarf::DW_AT_name))
293 if (Optional<const char *> Name = Val->getAsCString()) {
294 auto &Entry = ParseableSwiftInterfaces[*Name];
295 // The prepend path is applied later when copying.
296 DWARFDie CUDie = CU.getOrigUnit().getUnitDIE();
297 SmallString<128> ResolvedPath;
298 if (sys::path::is_relative(Path))
299 resolveRelativeObjectPath(ResolvedPath, CUDie);
300 sys::path::append(ResolvedPath, Path);
301 if (!Entry.empty() && Entry != ResolvedPath)
302 ReportWarning(
303 Twine("Conflicting parseable interfaces for Swift Module ") +
304 *Name + ": " + Entry + " and " + Path,
305 DIE);
306 Entry = ResolvedPath.str();
310 /// Recursive helper to build the global DeclContext information and
311 /// gather the child->parent relationships in the original compile unit.
313 /// \return true when this DIE and all of its children are only
314 /// forward declarations to types defined in external clang modules
315 /// (i.e., forward declarations that are children of a DW_TAG_module).
316 static bool analyzeContextInfo(
317 const DWARFDie &DIE, unsigned ParentIdx, CompileUnit &CU,
318 DeclContext *CurrentDeclContext, UniquingStringPool &StringPool,
319 DeclContextTree &Contexts, uint64_t ModulesEndOffset,
320 std::map<std::string, std::string> &ParseableSwiftInterfaces,
321 std::function<void(const Twine &, const DWARFDie &)> ReportWarning,
322 bool InImportedModule = false) {
323 unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
324 CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx);
326 // Clang imposes an ODR on modules(!) regardless of the language:
327 // "The module-id should consist of only a single identifier,
328 // which provides the name of the module being defined. Each
329 // module shall have a single definition."
331 // This does not extend to the types inside the modules:
332 // "[I]n C, this implies that if two structs are defined in
333 // different submodules with the same name, those two types are
334 // distinct types (but may be compatible types if their
335 // definitions match)."
337 // We treat non-C++ modules like namespaces for this reason.
338 if (DIE.getTag() == dwarf::DW_TAG_module && ParentIdx == 0 &&
339 dwarf::toString(DIE.find(dwarf::DW_AT_name), "") !=
340 CU.getClangModuleName()) {
341 InImportedModule = true;
342 analyzeImportedModule(DIE, CU, ParseableSwiftInterfaces, ReportWarning);
345 Info.ParentIdx = ParentIdx;
346 bool InClangModule = CU.isClangModule() || InImportedModule;
347 if (CU.hasODR() || InClangModule) {
348 if (CurrentDeclContext) {
349 auto PtrInvalidPair = Contexts.getChildDeclContext(
350 *CurrentDeclContext, DIE, CU, StringPool, InClangModule);
351 CurrentDeclContext = PtrInvalidPair.getPointer();
352 Info.Ctxt =
353 PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
354 if (Info.Ctxt)
355 Info.Ctxt->setDefinedInClangModule(InClangModule);
356 } else
357 Info.Ctxt = CurrentDeclContext = nullptr;
360 Info.Prune = InImportedModule;
361 if (DIE.hasChildren())
362 for (auto Child : DIE.children())
363 Info.Prune &= analyzeContextInfo(Child, MyIdx, CU, CurrentDeclContext,
364 StringPool, Contexts, ModulesEndOffset,
365 ParseableSwiftInterfaces, ReportWarning,
366 InImportedModule);
368 // Prune this DIE if it is either a forward declaration inside a
369 // DW_TAG_module or a DW_TAG_module that contains nothing but
370 // forward declarations.
371 Info.Prune &= (DIE.getTag() == dwarf::DW_TAG_module) ||
372 (isTypeTag(DIE.getTag()) &&
373 dwarf::toUnsigned(DIE.find(dwarf::DW_AT_declaration), 0));
375 // Only prune forward declarations inside a DW_TAG_module for which a
376 // definition exists elsewhere.
377 if (ModulesEndOffset == 0)
378 Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
379 else
380 Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() > 0 &&
381 Info.Ctxt->getCanonicalDIEOffset() <= ModulesEndOffset;
383 return Info.Prune;
384 } // namespace dsymutil
386 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
387 switch (Tag) {
388 default:
389 return false;
390 case dwarf::DW_TAG_class_type:
391 case dwarf::DW_TAG_common_block:
392 case dwarf::DW_TAG_lexical_block:
393 case dwarf::DW_TAG_structure_type:
394 case dwarf::DW_TAG_subprogram:
395 case dwarf::DW_TAG_subroutine_type:
396 case dwarf::DW_TAG_union_type:
397 return true;
399 llvm_unreachable("Invalid Tag");
402 void DwarfLinkerForBinary::startDebugObject(LinkContext &Context) {}
404 void DwarfLinkerForBinary::endDebugObject(LinkContext &Context) {
405 Context.Clear();
407 for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
408 (*I)->~DIEBlock();
409 for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
410 (*I)->~DIELoc();
412 DIEBlocks.clear();
413 DIELocs.clear();
414 DIEAlloc.Reset();
417 static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
418 switch (Arch) {
419 case Triple::x86:
420 return RelocType == MachO::GENERIC_RELOC_SECTDIFF ||
421 RelocType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
422 case Triple::x86_64:
423 return RelocType == MachO::X86_64_RELOC_SUBTRACTOR;
424 case Triple::arm:
425 case Triple::thumb:
426 return RelocType == MachO::ARM_RELOC_SECTDIFF ||
427 RelocType == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
428 RelocType == MachO::ARM_RELOC_HALF ||
429 RelocType == MachO::ARM_RELOC_HALF_SECTDIFF;
430 case Triple::aarch64:
431 return RelocType == MachO::ARM64_RELOC_SUBTRACTOR;
432 default:
433 return false;
437 /// Iterate over the relocations of the given \p Section and
438 /// store the ones that correspond to debug map entries into the
439 /// ValidRelocs array.
440 void DwarfLinkerForBinary::RelocationManager::findValidRelocsMachO(
441 const object::SectionRef &Section, const object::MachOObjectFile &Obj,
442 const DebugMapObject &DMO) {
443 Expected<StringRef> ContentsOrErr = Section.getContents();
444 if (!ContentsOrErr) {
445 consumeError(ContentsOrErr.takeError());
446 Linker.reportWarning("error reading section", DMO);
447 return;
449 DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian(), 0);
450 bool SkipNext = false;
452 for (const object::RelocationRef &Reloc : Section.relocations()) {
453 if (SkipNext) {
454 SkipNext = false;
455 continue;
458 object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
459 MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
461 if (isMachOPairedReloc(Obj.getAnyRelocationType(MachOReloc),
462 Obj.getArch())) {
463 SkipNext = true;
464 Linker.reportWarning("unsupported relocation in debug_info section.",
465 DMO);
466 continue;
469 unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
470 uint64_t Offset64 = Reloc.getOffset();
471 if ((RelocSize != 4 && RelocSize != 8)) {
472 Linker.reportWarning("unsupported relocation in debug_info section.",
473 DMO);
474 continue;
476 uint64_t OffsetCopy = Offset64;
477 // Mach-o uses REL relocations, the addend is at the relocation offset.
478 uint64_t Addend = Data.getUnsigned(&OffsetCopy, RelocSize);
479 uint64_t SymAddress;
480 int64_t SymOffset;
482 if (Obj.isRelocationScattered(MachOReloc)) {
483 // The address of the base symbol for scattered relocations is
484 // stored in the reloc itself. The actual addend will store the
485 // base address plus the offset.
486 SymAddress = Obj.getScatteredRelocationValue(MachOReloc);
487 SymOffset = int64_t(Addend) - SymAddress;
488 } else {
489 SymAddress = Addend;
490 SymOffset = 0;
493 auto Sym = Reloc.getSymbol();
494 if (Sym != Obj.symbol_end()) {
495 Expected<StringRef> SymbolName = Sym->getName();
496 if (!SymbolName) {
497 consumeError(SymbolName.takeError());
498 Linker.reportWarning("error getting relocation symbol name.", DMO);
499 continue;
501 if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
502 ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
503 } else if (const auto *Mapping = DMO.lookupObjectAddress(SymAddress)) {
504 // Do not store the addend. The addend was the address of the symbol in
505 // the object file, the address in the binary that is stored in the debug
506 // map doesn't need to be offset.
507 ValidRelocs.emplace_back(Offset64, RelocSize, SymOffset, Mapping);
512 /// Dispatch the valid relocation finding logic to the
513 /// appropriate handler depending on the object file format.
514 bool DwarfLinkerForBinary::RelocationManager::findValidRelocs(
515 const object::SectionRef &Section, const object::ObjectFile &Obj,
516 const DebugMapObject &DMO) {
517 // Dispatch to the right handler depending on the file type.
518 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
519 findValidRelocsMachO(Section, *MachOObj, DMO);
520 else
521 Linker.reportWarning(
522 Twine("unsupported object file type: ") + Obj.getFileName(), DMO);
524 if (ValidRelocs.empty())
525 return false;
527 // Sort the relocations by offset. We will walk the DIEs linearly in
528 // the file, this allows us to just keep an index in the relocation
529 // array that we advance during our walk, rather than resorting to
530 // some associative container. See DwarfLinker::NextValidReloc.
531 llvm::sort(ValidRelocs);
532 return true;
535 /// Look for relocations in the debug_info section that match
536 /// entries in the debug map. These relocations will drive the Dwarf
537 /// link by indicating which DIEs refer to symbols present in the
538 /// linked binary.
539 /// \returns whether there are any valid relocations in the debug info.
540 bool DwarfLinkerForBinary::RelocationManager::findValidRelocsInDebugInfo(
541 const object::ObjectFile &Obj, const DebugMapObject &DMO) {
542 // Find the debug_info section.
543 for (const object::SectionRef &Section : Obj.sections()) {
544 StringRef SectionName;
545 if (Expected<StringRef> NameOrErr = Section.getName())
546 SectionName = *NameOrErr;
547 else
548 consumeError(NameOrErr.takeError());
550 SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
551 if (SectionName != "debug_info")
552 continue;
553 return findValidRelocs(Section, Obj, DMO);
555 return false;
558 /// Checks that there is a relocation against an actual debug
559 /// map entry between \p StartOffset and \p NextOffset.
561 /// This function must be called with offsets in strictly ascending
562 /// order because it never looks back at relocations it already 'went past'.
563 /// \returns true and sets Info.InDebugMap if it is the case.
564 bool DwarfLinkerForBinary::RelocationManager::hasValidRelocationAt(
565 uint64_t StartOffset, uint64_t EndOffset, CompileUnit::DIEInfo &Info) {
566 assert(NextValidReloc == 0 ||
567 StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
568 if (NextValidReloc >= ValidRelocs.size())
569 return false;
571 uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
573 // We might need to skip some relocs that we didn't consider. For
574 // example the high_pc of a discarded DIE might contain a reloc that
575 // is in the list because it actually corresponds to the start of a
576 // function that is in the debug map.
577 while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
578 RelocOffset = ValidRelocs[++NextValidReloc].Offset;
580 if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
581 return false;
583 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
584 const auto &Mapping = ValidReloc.Mapping->getValue();
585 const uint64_t BinaryAddress = Mapping.BinaryAddress;
586 const uint64_t ObjectAddress = Mapping.ObjectAddress
587 ? uint64_t(*Mapping.ObjectAddress)
588 : std::numeric_limits<uint64_t>::max();
589 if (Linker.Options.Verbose)
590 outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
591 << "\t"
592 << format("0x%016" PRIx64 " => 0x%016" PRIx64 "\n", ObjectAddress,
593 BinaryAddress);
595 Info.AddrAdjust = BinaryAddress + ValidReloc.Addend;
596 if (Mapping.ObjectAddress)
597 Info.AddrAdjust -= ObjectAddress;
598 Info.InDebugMap = true;
599 return true;
602 /// Get the starting and ending (exclusive) offset for the
603 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
604 /// supposed to point to the position of the first attribute described
605 /// by \p Abbrev.
606 /// \return [StartOffset, EndOffset) as a pair.
607 static std::pair<uint64_t, uint64_t>
608 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
609 uint64_t Offset, const DWARFUnit &Unit) {
610 DataExtractor Data = Unit.getDebugInfoExtractor();
612 for (unsigned i = 0; i < Idx; ++i)
613 DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset,
614 Unit.getFormParams());
616 uint64_t End = Offset;
617 DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
618 Unit.getFormParams());
620 return std::make_pair(Offset, End);
623 /// Check if a variable describing DIE should be kept.
624 /// \returns updated TraversalFlags.
625 unsigned DwarfLinkerForBinary::shouldKeepVariableDIE(
626 RelocationManager &RelocMgr, const DWARFDie &DIE, CompileUnit &Unit,
627 CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
628 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
630 // Global variables with constant value can always be kept.
631 if (!(Flags & TF_InFunctionScope) &&
632 Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
633 MyInfo.InDebugMap = true;
634 return Flags | TF_Keep;
637 Optional<uint32_t> LocationIdx =
638 Abbrev->findAttributeIndex(dwarf::DW_AT_location);
639 if (!LocationIdx)
640 return Flags;
642 uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
643 const DWARFUnit &OrigUnit = Unit.getOrigUnit();
644 uint64_t LocationOffset, LocationEndOffset;
645 std::tie(LocationOffset, LocationEndOffset) =
646 getAttributeOffsets(Abbrev, *LocationIdx, Offset, OrigUnit);
648 // See if there is a relocation to a valid debug map entry inside
649 // this variable's location. The order is important here. We want to
650 // always check if the variable has a valid relocation, so that the
651 // DIEInfo is filled. However, we don't want a static variable in a
652 // function to force us to keep the enclosing function.
653 if (!RelocMgr.hasValidRelocationAt(LocationOffset, LocationEndOffset,
654 MyInfo) ||
655 (Flags & TF_InFunctionScope))
656 return Flags;
658 if (Options.Verbose) {
659 outs() << "Keeping variable DIE:";
660 DIDumpOptions DumpOpts;
661 DumpOpts.ChildRecurseDepth = 0;
662 DumpOpts.Verbose = Options.Verbose;
663 DIE.dump(outs(), 8 /* Indent */, DumpOpts);
666 return Flags | TF_Keep;
669 /// Check if a function describing DIE should be kept.
670 /// \returns updated TraversalFlags.
671 unsigned DwarfLinkerForBinary::shouldKeepSubprogramDIE(
672 RelocationManager &RelocMgr, RangesTy &Ranges, const DWARFDie &DIE,
673 const DebugMapObject &DMO, CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
674 unsigned Flags) {
675 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
677 Flags |= TF_InFunctionScope;
679 Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
680 if (!LowPcIdx)
681 return Flags;
683 uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
684 DWARFUnit &OrigUnit = Unit.getOrigUnit();
685 uint64_t LowPcOffset, LowPcEndOffset;
686 std::tie(LowPcOffset, LowPcEndOffset) =
687 getAttributeOffsets(Abbrev, *LowPcIdx, Offset, OrigUnit);
689 auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
690 assert(LowPc.hasValue() && "low_pc attribute is not an address.");
691 if (!LowPc ||
692 !RelocMgr.hasValidRelocationAt(LowPcOffset, LowPcEndOffset, MyInfo))
693 return Flags;
695 if (Options.Verbose) {
696 outs() << "Keeping subprogram DIE:";
697 DIDumpOptions DumpOpts;
698 DumpOpts.ChildRecurseDepth = 0;
699 DumpOpts.Verbose = Options.Verbose;
700 DIE.dump(outs(), 8 /* Indent */, DumpOpts);
703 if (DIE.getTag() == dwarf::DW_TAG_label) {
704 if (Unit.hasLabelAt(*LowPc))
705 return Flags;
706 // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
707 // that don't fall into the CU's aranges. This is wrong IMO. Debug info
708 // generation bugs aside, this is really wrong in the case of labels, where
709 // a label marking the end of a function will have a PC == CU's high_pc.
710 if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc))
711 .getValueOr(UINT64_MAX) <= LowPc)
712 return Flags;
713 Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust);
714 return Flags | TF_Keep;
717 Flags |= TF_Keep;
719 Optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
720 if (!HighPc) {
721 reportWarning("Function without high_pc. Range will be discarded.\n", DMO,
722 &DIE);
723 return Flags;
726 // Replace the debug map range with a more accurate one.
727 Ranges[*LowPc] = ObjFileAddressRange(*HighPc, MyInfo.AddrAdjust);
728 Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
729 return Flags;
732 /// Check if a DIE should be kept.
733 /// \returns updated TraversalFlags.
734 unsigned DwarfLinkerForBinary::shouldKeepDIE(
735 RelocationManager &RelocMgr, RangesTy &Ranges, const DWARFDie &DIE,
736 const DebugMapObject &DMO, CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
737 unsigned Flags) {
738 switch (DIE.getTag()) {
739 case dwarf::DW_TAG_constant:
740 case dwarf::DW_TAG_variable:
741 return shouldKeepVariableDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
742 case dwarf::DW_TAG_subprogram:
743 case dwarf::DW_TAG_label:
744 return shouldKeepSubprogramDIE(RelocMgr, Ranges, DIE, DMO, Unit, MyInfo,
745 Flags);
746 case dwarf::DW_TAG_base_type:
747 // DWARF Expressions may reference basic types, but scanning them
748 // is expensive. Basic types are tiny, so just keep all of them.
749 case dwarf::DW_TAG_imported_module:
750 case dwarf::DW_TAG_imported_declaration:
751 case dwarf::DW_TAG_imported_unit:
752 // We always want to keep these.
753 return Flags | TF_Keep;
754 default:
755 break;
758 return Flags;
761 namespace {
762 /// The distinct types of work performed by the work loop.
763 enum class WorklistItemType {
764 /// Given a DIE, look for DIEs to be kept.
765 LookForDIEsToKeep,
766 /// Given a DIE, look for children of this DIE to be kept.
767 LookForChildDIEsToKeep,
768 /// Given a DIE, look for DIEs referencing this DIE to be kept.
769 LookForRefDIEsToKeep,
770 /// Given a DIE, look for parent DIEs to be kept.
771 LookForParentDIEsToKeep,
772 /// Given a DIE, update its incompleteness based on whether its children are
773 /// incomplete.
774 UpdateChildIncompleteness,
775 /// Given a DIE, update its incompleteness based on whether the DIEs it
776 /// references are incomplete.
777 UpdateRefIncompleteness,
780 /// This class represents an item in the work list. The type defines what kind
781 /// of work needs to be performed when processing the current item. The flags
782 /// and info fields are optional based on the type.
783 struct WorklistItem {
784 WorklistItemType Type;
785 DWARFDie Die;
786 CompileUnit &CU;
787 unsigned Flags;
788 unsigned AncestorIdx = 0;
789 CompileUnit::DIEInfo *OtherInfo = nullptr;
791 WorklistItem(DWARFDie Die, CompileUnit &CU, unsigned Flags,
792 WorklistItemType T = WorklistItemType::LookForDIEsToKeep)
793 : Type(T), Die(Die), CU(CU), Flags(Flags){};
795 WorklistItem(DWARFDie Die, CompileUnit &CU, WorklistItemType T,
796 CompileUnit::DIEInfo *OtherInfo = nullptr)
797 : Type(T), Die(Die), CU(CU), OtherInfo(OtherInfo){};
799 WorklistItem(unsigned AncestorIdx, CompileUnit &CU, unsigned Flags)
800 : Type(WorklistItemType::LookForParentDIEsToKeep), CU(CU), Flags(Flags),
801 AncestorIdx(AncestorIdx){};
803 } // namespace
805 /// Helper that updates the completeness of the current DIE based on the
806 /// completeness of one of its children. It depends on the incompleteness of
807 /// the children already being computed.
808 static void updateChildIncompleteness(const DWARFDie &Die, CompileUnit &CU,
809 CompileUnit::DIEInfo &ChildInfo) {
810 switch (Die.getTag()) {
811 case dwarf::DW_TAG_structure_type:
812 case dwarf::DW_TAG_class_type:
813 break;
814 default:
815 return;
818 unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
819 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
821 if (ChildInfo.Incomplete || ChildInfo.Prune)
822 MyInfo.Incomplete = true;
825 /// Helper that updates the completeness of the current DIE based on the
826 /// completeness of the DIEs it references. It depends on the incompleteness of
827 /// the referenced DIE already being computed.
828 static void updateRefIncompleteness(const DWARFDie &Die, CompileUnit &CU,
829 CompileUnit::DIEInfo &RefInfo) {
830 switch (Die.getTag()) {
831 case dwarf::DW_TAG_typedef:
832 case dwarf::DW_TAG_member:
833 case dwarf::DW_TAG_reference_type:
834 case dwarf::DW_TAG_ptr_to_member_type:
835 case dwarf::DW_TAG_pointer_type:
836 break;
837 default:
838 return;
841 unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
842 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
844 if (MyInfo.Incomplete)
845 return;
847 if (RefInfo.Incomplete)
848 MyInfo.Incomplete = true;
851 /// Look at the children of the given DIE and decide whether they should be
852 /// kept.
853 static void lookForChildDIEsToKeep(const DWARFDie &Die, CompileUnit &CU,
854 unsigned Flags,
855 SmallVectorImpl<WorklistItem> &Worklist) {
856 // The TF_ParentWalk flag tells us that we are currently walking up the
857 // parent chain of a required DIE, and we don't want to mark all the children
858 // of the parents as kept (consider for example a DW_TAG_namespace node in
859 // the parent chain). There are however a set of DIE types for which we want
860 // to ignore that directive and still walk their children.
861 if (dieNeedsChildrenToBeMeaningful(Die.getTag()))
862 Flags &= ~DwarfLinkerForBinary::TF_ParentWalk;
864 // We're finished if this DIE has no children or we're walking the parent
865 // chain.
866 if (!Die.hasChildren() || (Flags & DwarfLinkerForBinary::TF_ParentWalk))
867 return;
869 // Add children in reverse order to the worklist to effectively process them
870 // in order.
871 for (auto Child : reverse(Die.children())) {
872 // Add a worklist item before every child to calculate incompleteness right
873 // after the current child is processed.
874 unsigned Idx = CU.getOrigUnit().getDIEIndex(Child);
875 CompileUnit::DIEInfo &ChildInfo = CU.getInfo(Idx);
876 Worklist.emplace_back(Die, CU, WorklistItemType::UpdateChildIncompleteness,
877 &ChildInfo);
878 Worklist.emplace_back(Child, CU, Flags);
882 /// Look at DIEs referenced by the given DIE and decide whether they should be
883 /// kept. All DIEs referenced though attributes should be kept.
884 static void lookForRefDIEsToKeep(const DWARFDie &Die, CompileUnit &CU,
885 unsigned Flags, DwarfLinkerForBinary &Linker,
886 const UnitListTy &Units,
887 const DebugMapObject &DMO,
888 SmallVectorImpl<WorklistItem> &Worklist) {
889 bool UseOdr = (Flags & DwarfLinkerForBinary::TF_DependencyWalk)
890 ? (Flags & DwarfLinkerForBinary::TF_ODR)
891 : CU.hasODR();
892 DWARFUnit &Unit = CU.getOrigUnit();
893 DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
894 const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
895 uint64_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
897 SmallVector<std::pair<DWARFDie, CompileUnit &>, 4> ReferencedDIEs;
898 for (const auto &AttrSpec : Abbrev->attributes()) {
899 DWARFFormValue Val(AttrSpec.Form);
900 if (!Val.isFormClass(DWARFFormValue::FC_Reference) ||
901 AttrSpec.Attr == dwarf::DW_AT_sibling) {
902 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
903 Unit.getFormParams());
904 continue;
907 Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit);
908 CompileUnit *ReferencedCU;
909 if (auto RefDie =
910 resolveDIEReference(Linker, DMO, Units, Val, Die, ReferencedCU)) {
911 uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDie);
912 CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
913 bool IsModuleRef = Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() &&
914 Info.Ctxt->isDefinedInClangModule();
915 // If the referenced DIE has a DeclContext that has already been
916 // emitted, then do not keep the one in this CU. We'll link to
917 // the canonical DIE in cloneDieReferenceAttribute.
919 // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
920 // be necessary and could be advantageously replaced by
921 // ReferencedCU->hasODR() && CU.hasODR().
923 // FIXME: compatibility with dsymutil-classic. There is no
924 // reason not to unique ref_addr references.
925 if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && (UseOdr || IsModuleRef) &&
926 Info.Ctxt &&
927 Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt &&
928 Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr))
929 continue;
931 // Keep a module forward declaration if there is no definition.
932 if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
933 Info.Ctxt->getCanonicalDIEOffset()))
934 Info.Prune = false;
935 ReferencedDIEs.emplace_back(RefDie, *ReferencedCU);
939 unsigned ODRFlag = UseOdr ? DwarfLinkerForBinary::TF_ODR : 0;
941 // Add referenced DIEs in reverse order to the worklist to effectively
942 // process them in order.
943 for (auto &P : reverse(ReferencedDIEs)) {
944 // Add a worklist item before every child to calculate incompleteness right
945 // after the current child is processed.
946 uint32_t RefIdx = P.second.getOrigUnit().getDIEIndex(P.first);
947 CompileUnit::DIEInfo &Info = P.second.getInfo(RefIdx);
948 Worklist.emplace_back(Die, CU, WorklistItemType::UpdateRefIncompleteness,
949 &Info);
950 Worklist.emplace_back(P.first, P.second,
951 DwarfLinkerForBinary::TF_Keep |
952 DwarfLinkerForBinary::TF_DependencyWalk |
953 ODRFlag);
957 /// Look at the parent of the given DIE and decide whether they should be kept.
958 static void lookForParentDIEsToKeep(unsigned AncestorIdx, CompileUnit &CU,
959 unsigned Flags,
960 SmallVectorImpl<WorklistItem> &Worklist) {
961 // Stop if we encounter an ancestor that's already marked as kept.
962 if (CU.getInfo(AncestorIdx).Keep)
963 return;
965 DWARFUnit &Unit = CU.getOrigUnit();
966 DWARFDie ParentDIE = Unit.getDIEAtIndex(AncestorIdx);
967 Worklist.emplace_back(CU.getInfo(AncestorIdx).ParentIdx, CU, Flags);
968 Worklist.emplace_back(ParentDIE, CU, Flags);
971 /// Recursively walk the \p DIE tree and look for DIEs to keep. Store that
972 /// information in \p CU's DIEInfo.
974 /// This function is the entry point of the DIE selection algorithm. It is
975 /// expected to walk the DIE tree in file order and (though the mediation of
976 /// its helper) call hasValidRelocation() on each DIE that might be a 'root
977 /// DIE' (See DwarfLinker class comment).
979 /// While walking the dependencies of root DIEs, this function is also called,
980 /// but during these dependency walks the file order is not respected. The
981 /// TF_DependencyWalk flag tells us which kind of traversal we are currently
982 /// doing.
984 /// The recursive algorithm is implemented iteratively as a work list because
985 /// very deep recursion could exhaust the stack for large projects. The work
986 /// list acts as a scheduler for different types of work that need to be
987 /// performed.
989 /// The recursive nature of the algorithm is simulated by running the "main"
990 /// algorithm (LookForDIEsToKeep) followed by either looking at more DIEs
991 /// (LookForChildDIEsToKeep, LookForRefDIEsToKeep, LookForParentDIEsToKeep) or
992 /// fixing up a computed property (UpdateChildIncompleteness,
993 /// UpdateRefIncompleteness).
995 /// The return value indicates whether the DIE is incomplete.
996 void DwarfLinkerForBinary::lookForDIEsToKeep(RelocationManager &RelocMgr,
997 RangesTy &Ranges,
998 const UnitListTy &Units,
999 const DWARFDie &Die,
1000 const DebugMapObject &DMO,
1001 CompileUnit &Cu, unsigned Flags) {
1002 // LIFO work list.
1003 SmallVector<WorklistItem, 4> Worklist;
1004 Worklist.emplace_back(Die, Cu, Flags);
1006 while (!Worklist.empty()) {
1007 WorklistItem Current = Worklist.back();
1008 Worklist.pop_back();
1010 // Look at the worklist type to decide what kind of work to perform.
1011 switch (Current.Type) {
1012 case WorklistItemType::UpdateChildIncompleteness:
1013 updateChildIncompleteness(Current.Die, Current.CU, *Current.OtherInfo);
1014 continue;
1015 case WorklistItemType::UpdateRefIncompleteness:
1016 updateRefIncompleteness(Current.Die, Current.CU, *Current.OtherInfo);
1017 continue;
1018 case WorklistItemType::LookForChildDIEsToKeep:
1019 lookForChildDIEsToKeep(Current.Die, Current.CU, Current.Flags, Worklist);
1020 continue;
1021 case WorklistItemType::LookForRefDIEsToKeep:
1022 lookForRefDIEsToKeep(Current.Die, Current.CU, Current.Flags, *this, Units,
1023 DMO, Worklist);
1024 continue;
1025 case WorklistItemType::LookForParentDIEsToKeep:
1026 lookForParentDIEsToKeep(Current.AncestorIdx, Current.CU, Current.Flags,
1027 Worklist);
1028 continue;
1029 case WorklistItemType::LookForDIEsToKeep:
1030 break;
1033 unsigned Idx = Current.CU.getOrigUnit().getDIEIndex(Current.Die);
1034 CompileUnit::DIEInfo &MyInfo = Current.CU.getInfo(Idx);
1036 if (MyInfo.Prune)
1037 continue;
1039 // If the Keep flag is set, we are marking a required DIE's dependencies.
1040 // If our target is already marked as kept, we're all set.
1041 bool AlreadyKept = MyInfo.Keep;
1042 if ((Current.Flags & TF_DependencyWalk) && AlreadyKept)
1043 continue;
1045 // We must not call shouldKeepDIE while called from keepDIEAndDependencies,
1046 // because it would screw up the relocation finding logic.
1047 if (!(Current.Flags & TF_DependencyWalk))
1048 Current.Flags = shouldKeepDIE(RelocMgr, Ranges, Current.Die, DMO,
1049 Current.CU, MyInfo, Current.Flags);
1051 // Finish by looking for child DIEs. Because of the LIFO worklist we need
1052 // to schedule that work before any subsequent items are added to the
1053 // worklist.
1054 Worklist.emplace_back(Current.Die, Current.CU, Current.Flags,
1055 WorklistItemType::LookForChildDIEsToKeep);
1057 if (AlreadyKept || !(Current.Flags & TF_Keep))
1058 continue;
1060 // If it is a newly kept DIE mark it as well as all its dependencies as
1061 // kept.
1062 MyInfo.Keep = true;
1064 // We're looking for incomplete types.
1065 MyInfo.Incomplete =
1066 Current.Die.getTag() != dwarf::DW_TAG_subprogram &&
1067 Current.Die.getTag() != dwarf::DW_TAG_member &&
1068 dwarf::toUnsigned(Current.Die.find(dwarf::DW_AT_declaration), 0);
1070 // After looking at the parent chain, look for referenced DIEs. Because of
1071 // the LIFO worklist we need to schedule that work before any subsequent
1072 // items are added to the worklist.
1073 Worklist.emplace_back(Current.Die, Current.CU, Current.Flags,
1074 WorklistItemType::LookForRefDIEsToKeep);
1076 bool UseOdr = (Current.Flags & TF_DependencyWalk) ? (Current.Flags & TF_ODR)
1077 : Current.CU.hasODR();
1078 unsigned ODRFlag = UseOdr ? TF_ODR : 0;
1079 unsigned ParFlags = TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag;
1081 // Now schedule the parent walk.
1082 Worklist.emplace_back(MyInfo.ParentIdx, Current.CU, ParFlags);
1086 /// Assign an abbreviation number to \p Abbrev.
1088 /// Our DIEs get freed after every DebugMapObject has been processed,
1089 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
1090 /// the instances hold by the DIEs. When we encounter an abbreviation
1091 /// that we don't know, we create a permanent copy of it.
1092 void DwarfLinkerForBinary::assignAbbrev(DIEAbbrev &Abbrev) {
1093 // Check the set for priors.
1094 FoldingSetNodeID ID;
1095 Abbrev.Profile(ID);
1096 void *InsertToken;
1097 DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
1099 // If it's newly added.
1100 if (InSet) {
1101 // Assign existing abbreviation number.
1102 Abbrev.setNumber(InSet->getNumber());
1103 } else {
1104 // Add to abbreviation list.
1105 Abbreviations.push_back(
1106 std::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
1107 for (const auto &Attr : Abbrev.getData())
1108 Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
1109 AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
1110 // Assign the unique abbreviation number.
1111 Abbrev.setNumber(Abbreviations.size());
1112 Abbreviations.back()->setNumber(Abbreviations.size());
1116 unsigned DwarfLinkerForBinary::DIECloner::cloneStringAttribute(
1117 DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1118 const DWARFUnit &U, OffsetsStringPool &StringPool, AttributesInfo &Info) {
1119 // Switch everything to out of line strings.
1120 const char *String = *Val.getAsCString();
1121 auto StringEntry = StringPool.getEntry(String);
1123 // Update attributes info.
1124 if (AttrSpec.Attr == dwarf::DW_AT_name)
1125 Info.Name = StringEntry;
1126 else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
1127 AttrSpec.Attr == dwarf::DW_AT_linkage_name)
1128 Info.MangledName = StringEntry;
1130 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
1131 DIEInteger(StringEntry.getOffset()));
1133 return 4;
1136 unsigned DwarfLinkerForBinary::DIECloner::cloneDieReferenceAttribute(
1137 DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
1138 unsigned AttrSize, const DWARFFormValue &Val, const DebugMapObject &DMO,
1139 CompileUnit &Unit) {
1140 const DWARFUnit &U = Unit.getOrigUnit();
1141 uint64_t Ref = *Val.getAsReference();
1142 DIE *NewRefDie = nullptr;
1143 CompileUnit *RefUnit = nullptr;
1144 DeclContext *Ctxt = nullptr;
1146 DWARFDie RefDie =
1147 resolveDIEReference(Linker, DMO, CompileUnits, Val, InputDIE, RefUnit);
1149 // If the referenced DIE is not found, drop the attribute.
1150 if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling)
1151 return 0;
1153 unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
1154 CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
1156 // If we already have emitted an equivalent DeclContext, just point
1157 // at it.
1158 if (isODRAttribute(AttrSpec.Attr)) {
1159 Ctxt = RefInfo.Ctxt;
1160 if (Ctxt && Ctxt->getCanonicalDIEOffset()) {
1161 DIEInteger Attr(Ctxt->getCanonicalDIEOffset());
1162 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1163 dwarf::DW_FORM_ref_addr, Attr);
1164 return U.getRefAddrByteSize();
1168 if (!RefInfo.Clone) {
1169 assert(Ref > InputDIE.getOffset());
1170 // We haven't cloned this DIE yet. Just create an empty one and
1171 // store it. It'll get really cloned when we process it.
1172 RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
1174 NewRefDie = RefInfo.Clone;
1176 if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
1177 (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
1178 // We cannot currently rely on a DIEEntry to emit ref_addr
1179 // references, because the implementation calls back to DwarfDebug
1180 // to find the unit offset. (We don't have a DwarfDebug)
1181 // FIXME: we should be able to design DIEEntry reliance on
1182 // DwarfDebug away.
1183 uint64_t Attr;
1184 if (Ref < InputDIE.getOffset()) {
1185 // We must have already cloned that DIE.
1186 uint32_t NewRefOffset =
1187 RefUnit->getStartOffset() + NewRefDie->getOffset();
1188 Attr = NewRefOffset;
1189 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1190 dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
1191 } else {
1192 // A forward reference. Note and fixup later.
1193 Attr = 0xBADDEF;
1194 Unit.noteForwardReference(
1195 NewRefDie, RefUnit, Ctxt,
1196 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1197 dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
1199 return U.getRefAddrByteSize();
1202 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1203 dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
1204 return AttrSize;
1207 void DwarfLinkerForBinary::DIECloner::cloneExpression(
1208 DataExtractor &Data, DWARFExpression Expression, const DebugMapObject &DMO,
1209 CompileUnit &Unit, SmallVectorImpl<uint8_t> &OutputBuffer) {
1210 using Encoding = DWARFExpression::Operation::Encoding;
1212 uint64_t OpOffset = 0;
1213 for (auto &Op : Expression) {
1214 auto Description = Op.getDescription();
1215 // DW_OP_const_type is variable-length and has 3
1216 // operands. DWARFExpression thus far only supports 2.
1217 auto Op0 = Description.Op[0];
1218 auto Op1 = Description.Op[1];
1219 if ((Op0 == Encoding::BaseTypeRef && Op1 != Encoding::SizeNA) ||
1220 (Op1 == Encoding::BaseTypeRef && Op0 != Encoding::Size1))
1221 Linker.reportWarning("Unsupported DW_OP encoding.", DMO);
1223 if ((Op0 == Encoding::BaseTypeRef && Op1 == Encoding::SizeNA) ||
1224 (Op1 == Encoding::BaseTypeRef && Op0 == Encoding::Size1)) {
1225 // This code assumes that the other non-typeref operand fits into 1 byte.
1226 assert(OpOffset < Op.getEndOffset());
1227 uint32_t ULEBsize = Op.getEndOffset() - OpOffset - 1;
1228 assert(ULEBsize <= 16);
1230 // Copy over the operation.
1231 OutputBuffer.push_back(Op.getCode());
1232 uint64_t RefOffset;
1233 if (Op1 == Encoding::SizeNA) {
1234 RefOffset = Op.getRawOperand(0);
1235 } else {
1236 OutputBuffer.push_back(Op.getRawOperand(0));
1237 RefOffset = Op.getRawOperand(1);
1239 auto RefDie = Unit.getOrigUnit().getDIEForOffset(RefOffset);
1240 uint32_t RefIdx = Unit.getOrigUnit().getDIEIndex(RefDie);
1241 CompileUnit::DIEInfo &Info = Unit.getInfo(RefIdx);
1242 uint32_t Offset = 0;
1243 if (DIE *Clone = Info.Clone)
1244 Offset = Clone->getOffset();
1245 else
1246 Linker.reportWarning("base type ref doesn't point to DW_TAG_base_type.",
1247 DMO);
1248 uint8_t ULEB[16];
1249 unsigned RealSize = encodeULEB128(Offset, ULEB, ULEBsize);
1250 if (RealSize > ULEBsize) {
1251 // Emit the generic type as a fallback.
1252 RealSize = encodeULEB128(0, ULEB, ULEBsize);
1253 Linker.reportWarning("base type ref doesn't fit.", DMO);
1255 assert(RealSize == ULEBsize && "padding failed");
1256 ArrayRef<uint8_t> ULEBbytes(ULEB, ULEBsize);
1257 OutputBuffer.append(ULEBbytes.begin(), ULEBbytes.end());
1258 } else {
1259 // Copy over everything else unmodified.
1260 StringRef Bytes = Data.getData().slice(OpOffset, Op.getEndOffset());
1261 OutputBuffer.append(Bytes.begin(), Bytes.end());
1263 OpOffset = Op.getEndOffset();
1267 unsigned DwarfLinkerForBinary::DIECloner::cloneBlockAttribute(
1268 DIE &Die, const DebugMapObject &DMO, CompileUnit &Unit,
1269 AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize,
1270 bool IsLittleEndian) {
1271 DIEValueList *Attr;
1272 DIEValue Value;
1273 DIELoc *Loc = nullptr;
1274 DIEBlock *Block = nullptr;
1275 if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
1276 Loc = new (DIEAlloc) DIELoc;
1277 Linker.DIELocs.push_back(Loc);
1278 } else {
1279 Block = new (DIEAlloc) DIEBlock;
1280 Linker.DIEBlocks.push_back(Block);
1282 Attr = Loc ? static_cast<DIEValueList *>(Loc)
1283 : static_cast<DIEValueList *>(Block);
1285 if (Loc)
1286 Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
1287 dwarf::Form(AttrSpec.Form), Loc);
1288 else
1289 Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
1290 dwarf::Form(AttrSpec.Form), Block);
1292 // If the block is a DWARF Expression, clone it into the temporary
1293 // buffer using cloneExpression(), otherwise copy the data directly.
1294 SmallVector<uint8_t, 32> Buffer;
1295 ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
1296 if (DWARFAttribute::mayHaveLocationDescription(AttrSpec.Attr) &&
1297 (Val.isFormClass(DWARFFormValue::FC_Block) ||
1298 Val.isFormClass(DWARFFormValue::FC_Exprloc))) {
1299 DWARFUnit &OrigUnit = Unit.getOrigUnit();
1300 DataExtractor Data(StringRef((const char *)Bytes.data(), Bytes.size()),
1301 IsLittleEndian, OrigUnit.getAddressByteSize());
1302 DWARFExpression Expr(Data, OrigUnit.getVersion(),
1303 OrigUnit.getAddressByteSize());
1304 cloneExpression(Data, Expr, DMO, Unit, Buffer);
1305 Bytes = Buffer;
1307 for (auto Byte : Bytes)
1308 Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
1309 dwarf::DW_FORM_data1, DIEInteger(Byte));
1311 // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1312 // the DIE class, this if could be replaced by
1313 // Attr->setSize(Bytes.size()).
1314 if (Linker.Streamer) {
1315 auto *AsmPrinter = &Linker.Streamer->getAsmPrinter();
1316 if (Loc)
1317 Loc->ComputeSize(AsmPrinter);
1318 else
1319 Block->ComputeSize(AsmPrinter);
1321 Die.addValue(DIEAlloc, Value);
1322 return AttrSize;
1325 unsigned DwarfLinkerForBinary::DIECloner::cloneAddressAttribute(
1326 DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1327 const CompileUnit &Unit, AttributesInfo &Info) {
1328 uint64_t Addr = *Val.getAsAddress();
1330 if (LLVM_UNLIKELY(Linker.Options.Update)) {
1331 if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
1332 Info.HasLowPc = true;
1333 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1334 dwarf::Form(AttrSpec.Form), DIEInteger(Addr));
1335 return Unit.getOrigUnit().getAddressByteSize();
1338 if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
1339 if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
1340 Die.getTag() == dwarf::DW_TAG_lexical_block)
1341 // The low_pc of a block or inline subroutine might get
1342 // relocated because it happens to match the low_pc of the
1343 // enclosing subprogram. To prevent issues with that, always use
1344 // the low_pc from the input DIE if relocations have been applied.
1345 Addr = (Info.OrigLowPc != std::numeric_limits<uint64_t>::max()
1346 ? Info.OrigLowPc
1347 : Addr) +
1348 Info.PCOffset;
1349 else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1350 Addr = Unit.getLowPc();
1351 if (Addr == std::numeric_limits<uint64_t>::max())
1352 return 0;
1354 Info.HasLowPc = true;
1355 } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
1356 if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1357 if (uint64_t HighPc = Unit.getHighPc())
1358 Addr = HighPc;
1359 else
1360 return 0;
1361 } else
1362 // If we have a high_pc recorded for the input DIE, use
1363 // it. Otherwise (when no relocations where applied) just use the
1364 // one we just decoded.
1365 Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
1366 } else if (AttrSpec.Attr == dwarf::DW_AT_call_return_pc) {
1367 // Relocate a return PC address within a call site entry.
1368 if (Die.getTag() == dwarf::DW_TAG_call_site)
1369 Addr += Info.PCOffset;
1372 Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
1373 static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
1374 return Unit.getOrigUnit().getAddressByteSize();
1377 unsigned DwarfLinkerForBinary::DIECloner::cloneScalarAttribute(
1378 DIE &Die, const DWARFDie &InputDIE, const DebugMapObject &DMO,
1379 CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1380 unsigned AttrSize, AttributesInfo &Info) {
1381 uint64_t Value;
1383 if (LLVM_UNLIKELY(Linker.Options.Update)) {
1384 if (auto OptionalValue = Val.getAsUnsignedConstant())
1385 Value = *OptionalValue;
1386 else if (auto OptionalValue = Val.getAsSignedConstant())
1387 Value = *OptionalValue;
1388 else if (auto OptionalValue = Val.getAsSectionOffset())
1389 Value = *OptionalValue;
1390 else {
1391 Linker.reportWarning(
1392 "Unsupported scalar attribute form. Dropping attribute.", DMO,
1393 &InputDIE);
1394 return 0;
1396 if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1397 Info.IsDeclaration = true;
1398 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1399 dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1400 return AttrSize;
1403 if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
1404 Die.getTag() == dwarf::DW_TAG_compile_unit) {
1405 if (Unit.getLowPc() == -1ULL)
1406 return 0;
1407 // Dwarf >= 4 high_pc is an size, not an address.
1408 Value = Unit.getHighPc() - Unit.getLowPc();
1409 } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
1410 Value = *Val.getAsSectionOffset();
1411 else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
1412 Value = *Val.getAsSignedConstant();
1413 else if (auto OptionalValue = Val.getAsUnsignedConstant())
1414 Value = *OptionalValue;
1415 else {
1416 Linker.reportWarning(
1417 "Unsupported scalar attribute form. Dropping attribute.", DMO,
1418 &InputDIE);
1419 return 0;
1421 PatchLocation Patch =
1422 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1423 dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1424 if (AttrSpec.Attr == dwarf::DW_AT_ranges) {
1425 Unit.noteRangeAttribute(Die, Patch);
1426 Info.HasRanges = true;
1429 // A more generic way to check for location attributes would be
1430 // nice, but it's very unlikely that any other attribute needs a
1431 // location list.
1432 // FIXME: use DWARFAttribute::mayHaveLocationDescription().
1433 else if (AttrSpec.Attr == dwarf::DW_AT_location ||
1434 AttrSpec.Attr == dwarf::DW_AT_frame_base)
1435 Unit.noteLocationAttribute(Patch, Info.PCOffset);
1436 else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1437 Info.IsDeclaration = true;
1439 return AttrSize;
1442 /// Clone \p InputDIE's attribute described by \p AttrSpec with
1443 /// value \p Val, and add it to \p Die.
1444 /// \returns the size of the cloned attribute.
1445 unsigned DwarfLinkerForBinary::DIECloner::cloneAttribute(
1446 DIE &Die, const DWARFDie &InputDIE, const DebugMapObject &DMO,
1447 CompileUnit &Unit, OffsetsStringPool &StringPool, const DWARFFormValue &Val,
1448 const AttributeSpec AttrSpec, unsigned AttrSize, AttributesInfo &Info,
1449 bool IsLittleEndian) {
1450 const DWARFUnit &U = Unit.getOrigUnit();
1452 switch (AttrSpec.Form) {
1453 case dwarf::DW_FORM_strp:
1454 case dwarf::DW_FORM_string:
1455 return cloneStringAttribute(Die, AttrSpec, Val, U, StringPool, Info);
1456 case dwarf::DW_FORM_ref_addr:
1457 case dwarf::DW_FORM_ref1:
1458 case dwarf::DW_FORM_ref2:
1459 case dwarf::DW_FORM_ref4:
1460 case dwarf::DW_FORM_ref8:
1461 return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
1462 DMO, Unit);
1463 case dwarf::DW_FORM_block:
1464 case dwarf::DW_FORM_block1:
1465 case dwarf::DW_FORM_block2:
1466 case dwarf::DW_FORM_block4:
1467 case dwarf::DW_FORM_exprloc:
1468 return cloneBlockAttribute(Die, DMO, Unit, AttrSpec, Val, AttrSize,
1469 IsLittleEndian);
1470 case dwarf::DW_FORM_addr:
1471 return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
1472 case dwarf::DW_FORM_data1:
1473 case dwarf::DW_FORM_data2:
1474 case dwarf::DW_FORM_data4:
1475 case dwarf::DW_FORM_data8:
1476 case dwarf::DW_FORM_udata:
1477 case dwarf::DW_FORM_sdata:
1478 case dwarf::DW_FORM_sec_offset:
1479 case dwarf::DW_FORM_flag:
1480 case dwarf::DW_FORM_flag_present:
1481 return cloneScalarAttribute(Die, InputDIE, DMO, Unit, AttrSpec, Val,
1482 AttrSize, Info);
1483 default:
1484 Linker.reportWarning(
1485 "Unsupported attribute form in cloneAttribute. Dropping.", DMO,
1486 &InputDIE);
1489 return 0;
1492 /// Apply the valid relocations found by findValidRelocs() to
1493 /// the buffer \p Data, taking into account that Data is at \p BaseOffset
1494 /// in the debug_info section.
1496 /// Like for findValidRelocs(), this function must be called with
1497 /// monotonic \p BaseOffset values.
1499 /// \returns whether any reloc has been applied.
1500 bool DwarfLinkerForBinary::RelocationManager::applyValidRelocs(
1501 MutableArrayRef<char> Data, uint64_t BaseOffset, bool IsLittleEndian) {
1502 assert((NextValidReloc == 0 ||
1503 BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
1504 "BaseOffset should only be increasing.");
1505 if (NextValidReloc >= ValidRelocs.size())
1506 return false;
1508 // Skip relocs that haven't been applied.
1509 while (NextValidReloc < ValidRelocs.size() &&
1510 ValidRelocs[NextValidReloc].Offset < BaseOffset)
1511 ++NextValidReloc;
1513 bool Applied = false;
1514 uint64_t EndOffset = BaseOffset + Data.size();
1515 while (NextValidReloc < ValidRelocs.size() &&
1516 ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
1517 ValidRelocs[NextValidReloc].Offset < EndOffset) {
1518 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
1519 assert(ValidReloc.Offset - BaseOffset < Data.size());
1520 assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
1521 char Buf[8];
1522 uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
1523 Value += ValidReloc.Addend;
1524 for (unsigned i = 0; i != ValidReloc.Size; ++i) {
1525 unsigned Index = IsLittleEndian ? i : (ValidReloc.Size - i - 1);
1526 Buf[i] = uint8_t(Value >> (Index * 8));
1528 assert(ValidReloc.Size <= sizeof(Buf));
1529 memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
1530 Applied = true;
1533 return Applied;
1536 static bool isObjCSelector(StringRef Name) {
1537 return Name.size() > 2 && (Name[0] == '-' || Name[0] == '+') &&
1538 (Name[1] == '[');
1541 void DwarfLinkerForBinary::DIECloner::addObjCAccelerator(
1542 CompileUnit &Unit, const DIE *Die, DwarfStringPoolEntryRef Name,
1543 OffsetsStringPool &StringPool, bool SkipPubSection) {
1544 assert(isObjCSelector(Name.getString()) && "not an objc selector");
1545 // Objective C method or class function.
1546 // "- [Class(Category) selector :withArg ...]"
1547 StringRef ClassNameStart(Name.getString().drop_front(2));
1548 size_t FirstSpace = ClassNameStart.find(' ');
1549 if (FirstSpace == StringRef::npos)
1550 return;
1552 StringRef SelectorStart(ClassNameStart.data() + FirstSpace + 1);
1553 if (!SelectorStart.size())
1554 return;
1556 StringRef Selector(SelectorStart.data(), SelectorStart.size() - 1);
1557 Unit.addNameAccelerator(Die, StringPool.getEntry(Selector), SkipPubSection);
1559 // Add an entry for the class name that points to this
1560 // method/class function.
1561 StringRef ClassName(ClassNameStart.data(), FirstSpace);
1562 Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassName), SkipPubSection);
1564 if (ClassName[ClassName.size() - 1] == ')') {
1565 size_t OpenParens = ClassName.find('(');
1566 if (OpenParens != StringRef::npos) {
1567 StringRef ClassNameNoCategory(ClassName.data(), OpenParens);
1568 Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassNameNoCategory),
1569 SkipPubSection);
1571 std::string MethodNameNoCategory(Name.getString().data(), OpenParens + 2);
1572 // FIXME: The missing space here may be a bug, but
1573 // dsymutil-classic also does it this way.
1574 MethodNameNoCategory.append(SelectorStart);
1575 Unit.addNameAccelerator(Die, StringPool.getEntry(MethodNameNoCategory),
1576 SkipPubSection);
1581 static bool
1582 shouldSkipAttribute(DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
1583 uint16_t Tag, bool InDebugMap, bool SkipPC,
1584 bool InFunctionScope) {
1585 switch (AttrSpec.Attr) {
1586 default:
1587 return false;
1588 case dwarf::DW_AT_low_pc:
1589 case dwarf::DW_AT_high_pc:
1590 case dwarf::DW_AT_ranges:
1591 return SkipPC;
1592 case dwarf::DW_AT_location:
1593 case dwarf::DW_AT_frame_base:
1594 // FIXME: for some reason dsymutil-classic keeps the location attributes
1595 // when they are of block type (i.e. not location lists). This is totally
1596 // wrong for globals where we will keep a wrong address. It is mostly
1597 // harmless for locals, but there is no point in keeping these anyway when
1598 // the function wasn't linked.
1599 return (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
1600 !InDebugMap)) &&
1601 !DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
1605 DIE *DwarfLinkerForBinary::DIECloner::cloneDIE(
1606 const DWARFDie &InputDIE, const DebugMapObject &DMO, CompileUnit &Unit,
1607 OffsetsStringPool &StringPool, int64_t PCOffset, uint32_t OutOffset,
1608 unsigned Flags, bool IsLittleEndian, DIE *Die) {
1609 DWARFUnit &U = Unit.getOrigUnit();
1610 unsigned Idx = U.getDIEIndex(InputDIE);
1611 CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
1613 // Should the DIE appear in the output?
1614 if (!Unit.getInfo(Idx).Keep)
1615 return nullptr;
1617 uint64_t Offset = InputDIE.getOffset();
1618 assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
1619 if (!Die) {
1620 // The DIE might have been already created by a forward reference
1621 // (see cloneDieReferenceAttribute()).
1622 if (!Info.Clone)
1623 Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
1624 Die = Info.Clone;
1627 assert(Die->getTag() == InputDIE.getTag());
1628 Die->setOffset(OutOffset);
1629 if ((Unit.hasODR() || Unit.isClangModule()) && !Info.Incomplete &&
1630 Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt &&
1631 Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt &&
1632 !Info.Ctxt->getCanonicalDIEOffset()) {
1633 // We are about to emit a DIE that is the root of its own valid
1634 // DeclContext tree. Make the current offset the canonical offset
1635 // for this context.
1636 Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
1639 // Extract and clone every attribute.
1640 DWARFDataExtractor Data = U.getDebugInfoExtractor();
1641 // Point to the next DIE (generally there is always at least a NULL
1642 // entry after the current one). If this is a lone
1643 // DW_TAG_compile_unit without any children, point to the next unit.
1644 uint64_t NextOffset = (Idx + 1 < U.getNumDIEs())
1645 ? U.getDIEAtIndex(Idx + 1).getOffset()
1646 : U.getNextUnitOffset();
1647 AttributesInfo AttrInfo;
1649 // We could copy the data only if we need to apply a relocation to it. After
1650 // testing, it seems there is no performance downside to doing the copy
1651 // unconditionally, and it makes the code simpler.
1652 SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
1653 Data =
1654 DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
1655 // Modify the copy with relocated addresses.
1656 if (RelocMgr.areRelocationsResolved() &&
1657 RelocMgr.applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
1658 // If we applied relocations, we store the value of high_pc that was
1659 // potentially stored in the input DIE. If high_pc is an address
1660 // (Dwarf version == 2), then it might have been relocated to a
1661 // totally unrelated value (because the end address in the object
1662 // file might be start address of another function which got moved
1663 // independently by the linker). The computation of the actual
1664 // high_pc value is done in cloneAddressAttribute().
1665 AttrInfo.OrigHighPc =
1666 dwarf::toAddress(InputDIE.find(dwarf::DW_AT_high_pc), 0);
1667 // Also store the low_pc. It might get relocated in an
1668 // inline_subprogram that happens at the beginning of its
1669 // inlining function.
1670 AttrInfo.OrigLowPc = dwarf::toAddress(InputDIE.find(dwarf::DW_AT_low_pc),
1671 std::numeric_limits<uint64_t>::max());
1674 // Reset the Offset to 0 as we will be working on the local copy of
1675 // the data.
1676 Offset = 0;
1678 const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
1679 Offset += getULEB128Size(Abbrev->getCode());
1681 // We are entering a subprogram. Get and propagate the PCOffset.
1682 if (Die->getTag() == dwarf::DW_TAG_subprogram)
1683 PCOffset = Info.AddrAdjust;
1684 AttrInfo.PCOffset = PCOffset;
1686 if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
1687 Flags |= TF_InFunctionScope;
1688 if (!Info.InDebugMap && LLVM_LIKELY(!Options.Update))
1689 Flags |= TF_SkipPC;
1692 bool Copied = false;
1693 for (const auto &AttrSpec : Abbrev->attributes()) {
1694 if (LLVM_LIKELY(!Options.Update) &&
1695 shouldSkipAttribute(AttrSpec, Die->getTag(), Info.InDebugMap,
1696 Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
1697 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
1698 U.getFormParams());
1699 // FIXME: dsymutil-classic keeps the old abbreviation around
1700 // even if it's not used. We can remove this (and the copyAbbrev
1701 // helper) as soon as bit-for-bit compatibility is not a goal anymore.
1702 if (!Copied) {
1703 copyAbbrev(*InputDIE.getAbbreviationDeclarationPtr(), Unit.hasODR());
1704 Copied = true;
1706 continue;
1709 DWARFFormValue Val(AttrSpec.Form);
1710 uint64_t AttrSize = Offset;
1711 Val.extractValue(Data, &Offset, U.getFormParams(), &U);
1712 AttrSize = Offset - AttrSize;
1714 OutOffset += cloneAttribute(*Die, InputDIE, DMO, Unit, StringPool, Val,
1715 AttrSpec, AttrSize, AttrInfo, IsLittleEndian);
1718 // Look for accelerator entries.
1719 uint16_t Tag = InputDIE.getTag();
1720 // FIXME: This is slightly wrong. An inline_subroutine without a
1721 // low_pc, but with AT_ranges might be interesting to get into the
1722 // accelerator tables too. For now stick with dsymutil's behavior.
1723 if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) &&
1724 Tag != dwarf::DW_TAG_compile_unit &&
1725 getDIENames(InputDIE, AttrInfo, StringPool,
1726 Tag != dwarf::DW_TAG_inlined_subroutine)) {
1727 if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
1728 Unit.addNameAccelerator(Die, AttrInfo.MangledName,
1729 Tag == dwarf::DW_TAG_inlined_subroutine);
1730 if (AttrInfo.Name) {
1731 if (AttrInfo.NameWithoutTemplate)
1732 Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate,
1733 /* SkipPubSection */ true);
1734 Unit.addNameAccelerator(Die, AttrInfo.Name,
1735 Tag == dwarf::DW_TAG_inlined_subroutine);
1737 if (AttrInfo.Name && isObjCSelector(AttrInfo.Name.getString()))
1738 addObjCAccelerator(Unit, Die, AttrInfo.Name, StringPool,
1739 /* SkipPubSection =*/true);
1741 } else if (Tag == dwarf::DW_TAG_namespace) {
1742 if (!AttrInfo.Name)
1743 AttrInfo.Name = StringPool.getEntry("(anonymous namespace)");
1744 Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
1745 } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
1746 getDIENames(InputDIE, AttrInfo, StringPool) && AttrInfo.Name &&
1747 AttrInfo.Name.getString()[0]) {
1748 uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, DMO);
1749 uint64_t RuntimeLang =
1750 dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
1751 .getValueOr(0);
1752 bool ObjCClassIsImplementation =
1753 (RuntimeLang == dwarf::DW_LANG_ObjC ||
1754 RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
1755 dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
1756 .getValueOr(0);
1757 Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1758 Hash);
1761 // Determine whether there are any children that we want to keep.
1762 bool HasChildren = false;
1763 for (auto Child : InputDIE.children()) {
1764 unsigned Idx = U.getDIEIndex(Child);
1765 if (Unit.getInfo(Idx).Keep) {
1766 HasChildren = true;
1767 break;
1771 DIEAbbrev NewAbbrev = Die->generateAbbrev();
1772 if (HasChildren)
1773 NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
1774 // Assign a permanent abbrev number
1775 Linker.assignAbbrev(NewAbbrev);
1776 Die->setAbbrevNumber(NewAbbrev.getNumber());
1778 // Add the size of the abbreviation number to the output offset.
1779 OutOffset += getULEB128Size(Die->getAbbrevNumber());
1781 if (!HasChildren) {
1782 // Update our size.
1783 Die->setSize(OutOffset - Die->getOffset());
1784 return Die;
1787 // Recursively clone children.
1788 for (auto Child : InputDIE.children()) {
1789 if (DIE *Clone = cloneDIE(Child, DMO, Unit, StringPool, PCOffset, OutOffset,
1790 Flags, IsLittleEndian)) {
1791 Die->addChild(Clone);
1792 OutOffset = Clone->getOffset() + Clone->getSize();
1796 // Account for the end of children marker.
1797 OutOffset += sizeof(int8_t);
1798 // Update our size.
1799 Die->setSize(OutOffset - Die->getOffset());
1800 return Die;
1803 /// Patch the input object file relevant debug_ranges entries
1804 /// and emit them in the output file. Update the relevant attributes
1805 /// to point at the new entries.
1806 void DwarfLinkerForBinary::patchRangesForUnit(const CompileUnit &Unit,
1807 DWARFContext &OrigDwarf,
1808 const DebugMapObject &DMO) const {
1809 DWARFDebugRangeList RangeList;
1810 const auto &FunctionRanges = Unit.getFunctionRanges();
1811 unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
1812 DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
1813 OrigDwarf.getDWARFObj().getRangesSection(),
1814 OrigDwarf.isLittleEndian(), AddressSize);
1815 auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
1816 DWARFUnit &OrigUnit = Unit.getOrigUnit();
1817 auto OrigUnitDie = OrigUnit.getUnitDIE(false);
1818 uint64_t OrigLowPc =
1819 dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc), -1ULL);
1820 // Ranges addresses are based on the unit's low_pc. Compute the
1821 // offset we need to apply to adapt to the new unit's low_pc.
1822 int64_t UnitPcOffset = 0;
1823 if (OrigLowPc != -1ULL)
1824 UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
1826 for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
1827 uint64_t Offset = RangeAttribute.get();
1828 RangeAttribute.set(Streamer->getRangesSectionSize());
1829 if (Error E = RangeList.extract(RangeExtractor, &Offset)) {
1830 llvm::consumeError(std::move(E));
1831 reportWarning("invalid range list ignored.", DMO);
1832 RangeList.clear();
1834 const auto &Entries = RangeList.getEntries();
1835 if (!Entries.empty()) {
1836 const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
1838 if (CurrRange == InvalidRange ||
1839 First.StartAddress + OrigLowPc < CurrRange.start() ||
1840 First.StartAddress + OrigLowPc >= CurrRange.stop()) {
1841 CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
1842 if (CurrRange == InvalidRange ||
1843 CurrRange.start() > First.StartAddress + OrigLowPc) {
1844 reportWarning("no mapping for range.", DMO);
1845 continue;
1850 Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
1851 AddressSize);
1855 /// Generate the debug_aranges entries for \p Unit and if the
1856 /// unit has a DW_AT_ranges attribute, also emit the debug_ranges
1857 /// contribution for this attribute.
1858 /// FIXME: this could actually be done right in patchRangesForUnit,
1859 /// but for the sake of initial bit-for-bit compatibility with legacy
1860 /// dsymutil, we have to do it in a delayed pass.
1861 void DwarfLinkerForBinary::generateUnitRanges(CompileUnit &Unit) const {
1862 auto Attr = Unit.getUnitRangesAttribute();
1863 if (Attr)
1864 Attr->set(Streamer->getRangesSectionSize());
1865 Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
1868 /// Insert the new line info sequence \p Seq into the current
1869 /// set of already linked line info \p Rows.
1870 static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
1871 std::vector<DWARFDebugLine::Row> &Rows) {
1872 if (Seq.empty())
1873 return;
1875 if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
1876 Rows.insert(Rows.end(), Seq.begin(), Seq.end());
1877 Seq.clear();
1878 return;
1881 object::SectionedAddress Front = Seq.front().Address;
1882 auto InsertPoint = partition_point(
1883 Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
1885 // FIXME: this only removes the unneeded end_sequence if the
1886 // sequences have been inserted in order. Using a global sort like
1887 // described in patchLineTableForUnit() and delaying the end_sequene
1888 // elimination to emitLineTableForUnit() we can get rid of all of them.
1889 if (InsertPoint != Rows.end() && InsertPoint->Address == Front &&
1890 InsertPoint->EndSequence) {
1891 *InsertPoint = Seq.front();
1892 Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
1893 } else {
1894 Rows.insert(InsertPoint, Seq.begin(), Seq.end());
1897 Seq.clear();
1900 static void patchStmtList(DIE &Die, DIEInteger Offset) {
1901 for (auto &V : Die.values())
1902 if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
1903 V = DIEValue(V.getAttribute(), V.getForm(), Offset);
1904 return;
1907 llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
1910 /// Extract the line table for \p Unit from \p OrigDwarf, and
1911 /// recreate a relocated version of these for the address ranges that
1912 /// are present in the binary.
1913 void DwarfLinkerForBinary::patchLineTableForUnit(CompileUnit &Unit,
1914 DWARFContext &OrigDwarf,
1915 RangesTy &Ranges,
1916 const DebugMapObject &DMO) {
1917 DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
1918 auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
1919 if (!StmtList)
1920 return;
1922 // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
1923 if (auto *OutputDIE = Unit.getOutputUnitDIE())
1924 patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize()));
1926 // Parse the original line info for the unit.
1927 DWARFDebugLine::LineTable LineTable;
1928 uint64_t StmtOffset = *StmtList;
1929 DWARFDataExtractor LineExtractor(
1930 OrigDwarf.getDWARFObj(), OrigDwarf.getDWARFObj().getLineSection(),
1931 OrigDwarf.isLittleEndian(), Unit.getOrigUnit().getAddressByteSize());
1932 if (Options.Translator)
1933 return Streamer->translateLineTable(LineExtractor, StmtOffset);
1935 Error Err = LineTable.parse(LineExtractor, &StmtOffset, OrigDwarf,
1936 &Unit.getOrigUnit(), DWARFContext::dumpWarning);
1937 DWARFContext::dumpWarning(std::move(Err));
1939 // This vector is the output line table.
1940 std::vector<DWARFDebugLine::Row> NewRows;
1941 NewRows.reserve(LineTable.Rows.size());
1943 // Current sequence of rows being extracted, before being inserted
1944 // in NewRows.
1945 std::vector<DWARFDebugLine::Row> Seq;
1946 const auto &FunctionRanges = Unit.getFunctionRanges();
1947 auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
1949 // FIXME: This logic is meant to generate exactly the same output as
1950 // Darwin's classic dsymutil. There is a nicer way to implement this
1951 // by simply putting all the relocated line info in NewRows and simply
1952 // sorting NewRows before passing it to emitLineTableForUnit. This
1953 // should be correct as sequences for a function should stay
1954 // together in the sorted output. There are a few corner cases that
1955 // look suspicious though, and that required to implement the logic
1956 // this way. Revisit that once initial validation is finished.
1958 // Iterate over the object file line info and extract the sequences
1959 // that correspond to linked functions.
1960 for (auto &Row : LineTable.Rows) {
1961 // Check whether we stepped out of the range. The range is
1962 // half-open, but consider accept the end address of the range if
1963 // it is marked as end_sequence in the input (because in that
1964 // case, the relocation offset is accurate and that entry won't
1965 // serve as the start of another function).
1966 if (CurrRange == InvalidRange || Row.Address.Address < CurrRange.start() ||
1967 Row.Address.Address > CurrRange.stop() ||
1968 (Row.Address.Address == CurrRange.stop() && !Row.EndSequence)) {
1969 // We just stepped out of a known range. Insert a end_sequence
1970 // corresponding to the end of the range.
1971 uint64_t StopAddress = CurrRange != InvalidRange
1972 ? CurrRange.stop() + CurrRange.value()
1973 : -1ULL;
1974 CurrRange = FunctionRanges.find(Row.Address.Address);
1975 bool CurrRangeValid =
1976 CurrRange != InvalidRange && CurrRange.start() <= Row.Address.Address;
1977 if (!CurrRangeValid) {
1978 CurrRange = InvalidRange;
1979 if (StopAddress != -1ULL) {
1980 // Try harder by looking in the DebugMapObject function
1981 // ranges map. There are corner cases where this finds a
1982 // valid entry. It's unclear if this is right or wrong, but
1983 // for now do as dsymutil.
1984 // FIXME: Understand exactly what cases this addresses and
1985 // potentially remove it along with the Ranges map.
1986 auto Range = Ranges.lower_bound(Row.Address.Address);
1987 if (Range != Ranges.begin() && Range != Ranges.end())
1988 --Range;
1990 if (Range != Ranges.end() && Range->first <= Row.Address.Address &&
1991 Range->second.HighPC >= Row.Address.Address) {
1992 StopAddress = Row.Address.Address + Range->second.Offset;
1996 if (StopAddress != -1ULL && !Seq.empty()) {
1997 // Insert end sequence row with the computed end address, but
1998 // the same line as the previous one.
1999 auto NextLine = Seq.back();
2000 NextLine.Address.Address = StopAddress;
2001 NextLine.EndSequence = 1;
2002 NextLine.PrologueEnd = 0;
2003 NextLine.BasicBlock = 0;
2004 NextLine.EpilogueBegin = 0;
2005 Seq.push_back(NextLine);
2006 insertLineSequence(Seq, NewRows);
2009 if (!CurrRangeValid)
2010 continue;
2013 // Ignore empty sequences.
2014 if (Row.EndSequence && Seq.empty())
2015 continue;
2017 // Relocate row address and add it to the current sequence.
2018 Row.Address.Address += CurrRange.value();
2019 Seq.emplace_back(Row);
2021 if (Row.EndSequence)
2022 insertLineSequence(Seq, NewRows);
2025 // Finished extracting, now emit the line tables.
2026 // FIXME: LLVM hard-codes its prologue values. We just copy the
2027 // prologue over and that works because we act as both producer and
2028 // consumer. It would be nicer to have a real configurable line
2029 // table emitter.
2030 if (LineTable.Prologue.getVersion() < 2 ||
2031 LineTable.Prologue.getVersion() > 5 ||
2032 LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
2033 LineTable.Prologue.OpcodeBase > 13)
2034 reportWarning("line table parameters mismatch. Cannot emit.", DMO);
2035 else {
2036 uint32_t PrologueEnd = *StmtList + 10 + LineTable.Prologue.PrologueLength;
2037 // DWARF v5 has an extra 2 bytes of information before the header_length
2038 // field.
2039 if (LineTable.Prologue.getVersion() == 5)
2040 PrologueEnd += 2;
2041 StringRef LineData = OrigDwarf.getDWARFObj().getLineSection().Data;
2042 MCDwarfLineTableParams Params;
2043 Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
2044 Params.DWARF2LineBase = LineTable.Prologue.LineBase;
2045 Params.DWARF2LineRange = LineTable.Prologue.LineRange;
2046 Streamer->emitLineTableForUnit(Params,
2047 LineData.slice(*StmtList + 4, PrologueEnd),
2048 LineTable.Prologue.MinInstLength, NewRows,
2049 Unit.getOrigUnit().getAddressByteSize());
2053 void DwarfLinkerForBinary::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
2054 switch (Options.TheAccelTableKind) {
2055 case AccelTableKind::Apple:
2056 emitAppleAcceleratorEntriesForUnit(Unit);
2057 break;
2058 case AccelTableKind::Dwarf:
2059 emitDwarfAcceleratorEntriesForUnit(Unit);
2060 break;
2061 case AccelTableKind::Default:
2062 llvm_unreachable("The default must be updated to a concrete value.");
2063 break;
2067 void DwarfLinkerForBinary::emitAppleAcceleratorEntriesForUnit(
2068 CompileUnit &Unit) {
2069 // Add namespaces.
2070 for (const auto &Namespace : Unit.getNamespaces())
2071 AppleNamespaces.addName(Namespace.Name,
2072 Namespace.Die->getOffset() + Unit.getStartOffset());
2074 /// Add names.
2075 if (!Options.Minimize)
2076 Streamer->emitPubNamesForUnit(Unit);
2077 for (const auto &Pubname : Unit.getPubnames())
2078 AppleNames.addName(Pubname.Name,
2079 Pubname.Die->getOffset() + Unit.getStartOffset());
2081 /// Add types.
2082 if (!Options.Minimize)
2083 Streamer->emitPubTypesForUnit(Unit);
2084 for (const auto &Pubtype : Unit.getPubtypes())
2085 AppleTypes.addName(
2086 Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(),
2087 Pubtype.Die->getTag(),
2088 Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
2089 : 0,
2090 Pubtype.QualifiedNameHash);
2092 /// Add ObjC names.
2093 for (const auto &ObjC : Unit.getObjC())
2094 AppleObjc.addName(ObjC.Name, ObjC.Die->getOffset() + Unit.getStartOffset());
2097 void DwarfLinkerForBinary::emitDwarfAcceleratorEntriesForUnit(
2098 CompileUnit &Unit) {
2099 for (const auto &Namespace : Unit.getNamespaces())
2100 DebugNames.addName(Namespace.Name, Namespace.Die->getOffset(),
2101 Namespace.Die->getTag(), Unit.getUniqueID());
2102 for (const auto &Pubname : Unit.getPubnames())
2103 DebugNames.addName(Pubname.Name, Pubname.Die->getOffset(),
2104 Pubname.Die->getTag(), Unit.getUniqueID());
2105 for (const auto &Pubtype : Unit.getPubtypes())
2106 DebugNames.addName(Pubtype.Name, Pubtype.Die->getOffset(),
2107 Pubtype.Die->getTag(), Unit.getUniqueID());
2110 /// Read the frame info stored in the object, and emit the
2111 /// patched frame descriptions for the linked binary.
2113 /// This is actually pretty easy as the data of the CIEs and FDEs can
2114 /// be considered as black boxes and moved as is. The only thing to do
2115 /// is to patch the addresses in the headers.
2116 void DwarfLinkerForBinary::patchFrameInfoForObject(const DebugMapObject &DMO,
2117 RangesTy &Ranges,
2118 DWARFContext &OrigDwarf,
2119 unsigned AddrSize) {
2120 StringRef FrameData = OrigDwarf.getDWARFObj().getFrameSection().Data;
2121 if (FrameData.empty())
2122 return;
2124 DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
2125 uint64_t InputOffset = 0;
2127 // Store the data of the CIEs defined in this object, keyed by their
2128 // offsets.
2129 DenseMap<uint64_t, StringRef> LocalCIES;
2131 while (Data.isValidOffset(InputOffset)) {
2132 uint64_t EntryOffset = InputOffset;
2133 uint32_t InitialLength = Data.getU32(&InputOffset);
2134 if (InitialLength == 0xFFFFFFFF)
2135 return reportWarning("Dwarf64 bits no supported", DMO);
2137 uint32_t CIEId = Data.getU32(&InputOffset);
2138 if (CIEId == 0xFFFFFFFF) {
2139 // This is a CIE, store it.
2140 StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
2141 LocalCIES[EntryOffset] = CIEData;
2142 // The -4 is to account for the CIEId we just read.
2143 InputOffset += InitialLength - 4;
2144 continue;
2147 uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
2149 // Some compilers seem to emit frame info that doesn't start at
2150 // the function entry point, thus we can't just lookup the address
2151 // in the debug map. Use the linker's range map to see if the FDE
2152 // describes something that we can relocate.
2153 auto Range = Ranges.upper_bound(Loc);
2154 if (Range != Ranges.begin())
2155 --Range;
2156 if (Range == Ranges.end() || Range->first > Loc ||
2157 Range->second.HighPC <= Loc) {
2158 // The +4 is to account for the size of the InitialLength field itself.
2159 InputOffset = EntryOffset + InitialLength + 4;
2160 continue;
2163 // This is an FDE, and we have a mapping.
2164 // Have we already emitted a corresponding CIE?
2165 StringRef CIEData = LocalCIES[CIEId];
2166 if (CIEData.empty())
2167 return reportWarning("Inconsistent debug_frame content. Dropping.", DMO);
2169 // Look if we already emitted a CIE that corresponds to the
2170 // referenced one (the CIE data is the key of that lookup).
2171 auto IteratorInserted = EmittedCIEs.insert(
2172 std::make_pair(CIEData, Streamer->getFrameSectionSize()));
2173 // If there is no CIE yet for this ID, emit it.
2174 if (IteratorInserted.second ||
2175 // FIXME: dsymutil-classic only caches the last used CIE for
2176 // reuse. Mimic that behavior for now. Just removing that
2177 // second half of the condition and the LastCIEOffset variable
2178 // makes the code DTRT.
2179 LastCIEOffset != IteratorInserted.first->getValue()) {
2180 LastCIEOffset = Streamer->getFrameSectionSize();
2181 IteratorInserted.first->getValue() = LastCIEOffset;
2182 Streamer->emitCIE(CIEData);
2185 // Emit the FDE with updated address and CIE pointer.
2186 // (4 + AddrSize) is the size of the CIEId + initial_location
2187 // fields that will get reconstructed by emitFDE().
2188 unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
2189 Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize,
2190 Loc + Range->second.Offset,
2191 FrameData.substr(InputOffset, FDERemainingBytes));
2192 InputOffset += FDERemainingBytes;
2196 void DwarfLinkerForBinary::DIECloner::copyAbbrev(
2197 const DWARFAbbreviationDeclaration &Abbrev, bool HasODR) {
2198 DIEAbbrev Copy(dwarf::Tag(Abbrev.getTag()),
2199 dwarf::Form(Abbrev.hasChildren()));
2201 for (const auto &Attr : Abbrev.attributes()) {
2202 uint16_t Form = Attr.Form;
2203 if (HasODR && isODRAttribute(Attr.Attr))
2204 Form = dwarf::DW_FORM_ref_addr;
2205 Copy.AddAttribute(dwarf::Attribute(Attr.Attr), dwarf::Form(Form));
2208 Linker.assignAbbrev(Copy);
2211 uint32_t DwarfLinkerForBinary::DIECloner::hashFullyQualifiedName(
2212 DWARFDie DIE, CompileUnit &U, const DebugMapObject &DMO,
2213 int ChildRecurseDepth) {
2214 const char *Name = nullptr;
2215 DWARFUnit *OrigUnit = &U.getOrigUnit();
2216 CompileUnit *CU = &U;
2217 Optional<DWARFFormValue> Ref;
2219 while (1) {
2220 if (const char *CurrentName = DIE.getName(DINameKind::ShortName))
2221 Name = CurrentName;
2223 if (!(Ref = DIE.find(dwarf::DW_AT_specification)) &&
2224 !(Ref = DIE.find(dwarf::DW_AT_abstract_origin)))
2225 break;
2227 if (!Ref->isFormClass(DWARFFormValue::FC_Reference))
2228 break;
2230 CompileUnit *RefCU;
2231 if (auto RefDIE =
2232 resolveDIEReference(Linker, DMO, CompileUnits, *Ref, DIE, RefCU)) {
2233 CU = RefCU;
2234 OrigUnit = &RefCU->getOrigUnit();
2235 DIE = RefDIE;
2239 unsigned Idx = OrigUnit->getDIEIndex(DIE);
2240 if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace)
2241 Name = "(anonymous namespace)";
2243 if (CU->getInfo(Idx).ParentIdx == 0 ||
2244 // FIXME: dsymutil-classic compatibility. Ignore modules.
2245 CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() ==
2246 dwarf::DW_TAG_module)
2247 return djbHash(Name ? Name : "", djbHash(ChildRecurseDepth ? "" : "::"));
2249 DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx);
2250 return djbHash(
2251 (Name ? Name : ""),
2252 djbHash((Name ? "::" : ""),
2253 hashFullyQualifiedName(Die, *CU, DMO, ++ChildRecurseDepth)));
2256 static uint64_t getDwoId(const DWARFDie &CUDie, const DWARFUnit &Unit) {
2257 auto DwoId = dwarf::toUnsigned(
2258 CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
2259 if (DwoId)
2260 return *DwoId;
2261 return 0;
2264 bool DwarfLinkerForBinary::registerModuleReference(
2265 DWARFDie CUDie, const DWARFUnit &Unit, DebugMap &ModuleMap,
2266 const DebugMapObject &DMO, RangesTy &Ranges, OffsetsStringPool &StringPool,
2267 UniquingStringPool &UniquingStringPool, DeclContextTree &ODRContexts,
2268 uint64_t ModulesEndOffset, unsigned &UnitID, bool IsLittleEndian,
2269 unsigned Indent, bool Quiet) {
2270 std::string PCMfile = dwarf::toString(
2271 CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
2272 if (PCMfile.empty())
2273 return false;
2275 // Clang module DWARF skeleton CUs abuse this for the path to the module.
2276 uint64_t DwoId = getDwoId(CUDie, Unit);
2278 std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
2279 if (Name.empty()) {
2280 if (!Quiet)
2281 reportWarning("Anonymous module skeleton CU for " + PCMfile, DMO);
2282 return true;
2285 if (!Quiet && Options.Verbose) {
2286 outs().indent(Indent);
2287 outs() << "Found clang module reference " << PCMfile;
2290 auto Cached = ClangModules.find(PCMfile);
2291 if (Cached != ClangModules.end()) {
2292 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2293 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2294 // ASTFileSignatures will change randomly when a module is rebuilt.
2295 if (!Quiet && Options.Verbose && (Cached->second != DwoId))
2296 reportWarning(Twine("hash mismatch: this object file was built against a "
2297 "different version of the module ") +
2298 PCMfile,
2299 DMO);
2300 if (!Quiet && Options.Verbose)
2301 outs() << " [cached].\n";
2302 return true;
2304 if (!Quiet && Options.Verbose)
2305 outs() << " ...\n";
2307 // Cyclic dependencies are disallowed by Clang, but we still
2308 // shouldn't run into an infinite loop, so mark it as processed now.
2309 ClangModules.insert({PCMfile, DwoId});
2311 if (Error E = loadClangModule(CUDie, PCMfile, Name, DwoId, ModuleMap, DMO,
2312 Ranges, StringPool, UniquingStringPool,
2313 ODRContexts, ModulesEndOffset, UnitID,
2314 IsLittleEndian, Indent + 2, Quiet)) {
2315 consumeError(std::move(E));
2316 return false;
2318 return true;
2321 ErrorOr<const object::ObjectFile &>
2322 DwarfLinkerForBinary::loadObject(const DebugMapObject &Obj,
2323 const DebugMap &Map) {
2324 auto ObjectEntry =
2325 BinHolder.getObjectEntry(Obj.getObjectFilename(), Obj.getTimestamp());
2326 if (!ObjectEntry) {
2327 auto Err = ObjectEntry.takeError();
2328 reportWarning(
2329 Twine(Obj.getObjectFilename()) + ": " + toString(std::move(Err)), Obj);
2330 return errorToErrorCode(std::move(Err));
2333 auto Object = ObjectEntry->getObject(Map.getTriple());
2334 if (!Object) {
2335 auto Err = Object.takeError();
2336 reportWarning(
2337 Twine(Obj.getObjectFilename()) + ": " + toString(std::move(Err)), Obj);
2338 return errorToErrorCode(std::move(Err));
2341 return *Object;
2344 Error DwarfLinkerForBinary::loadClangModule(
2345 DWARFDie CUDie, StringRef Filename, StringRef ModuleName, uint64_t DwoId,
2346 DebugMap &ModuleMap, const DebugMapObject &DMO, RangesTy &Ranges,
2347 OffsetsStringPool &StringPool, UniquingStringPool &UniquingStringPool,
2348 DeclContextTree &ODRContexts, uint64_t ModulesEndOffset, unsigned &UnitID,
2349 bool IsLittleEndian, unsigned Indent, bool Quiet) {
2350 /// Using a SmallString<0> because loadClangModule() is recursive.
2351 SmallString<0> Path(Options.PrependPath);
2352 if (sys::path::is_relative(Filename))
2353 resolveRelativeObjectPath(Path, CUDie);
2354 sys::path::append(Path, Filename);
2355 // Don't use the cached binary holder because we have no thread-safety
2356 // guarantee and the lifetime is limited.
2357 auto &Obj = ModuleMap.addDebugMapObject(
2358 Path, sys::TimePoint<std::chrono::seconds>(), MachO::N_OSO);
2359 auto ErrOrObj = loadObject(Obj, ModuleMap);
2360 if (!ErrOrObj) {
2361 // Try and emit more helpful warnings by applying some heuristics.
2362 StringRef ObjFile = DMO.getObjectFilename();
2363 bool isClangModule = sys::path::extension(Filename).equals(".pcm");
2364 bool isArchive = ObjFile.endswith(")");
2365 if (isClangModule) {
2366 StringRef ModuleCacheDir = sys::path::parent_path(Path);
2367 if (sys::fs::exists(ModuleCacheDir)) {
2368 // If the module's parent directory exists, we assume that the module
2369 // cache has expired and was pruned by clang. A more adventurous
2370 // dsymutil would invoke clang to rebuild the module now.
2371 if (!ModuleCacheHintDisplayed) {
2372 WithColor::note() << "The clang module cache may have expired since "
2373 "this object file was built. Rebuilding the "
2374 "object file will rebuild the module cache.\n";
2375 ModuleCacheHintDisplayed = true;
2377 } else if (isArchive) {
2378 // If the module cache directory doesn't exist at all and the object
2379 // file is inside a static library, we assume that the static library
2380 // was built on a different machine. We don't want to discourage module
2381 // debugging for convenience libraries within a project though.
2382 if (!ArchiveHintDisplayed) {
2383 WithColor::note()
2384 << "Linking a static library that was built with "
2385 "-gmodules, but the module cache was not found. "
2386 "Redistributable static libraries should never be "
2387 "built with module debugging enabled. The debug "
2388 "experience will be degraded due to incomplete "
2389 "debug information.\n";
2390 ArchiveHintDisplayed = true;
2394 return Error::success();
2397 std::unique_ptr<CompileUnit> Unit;
2399 // Setup access to the debug info.
2400 auto DwarfContext = DWARFContext::create(*ErrOrObj);
2401 RelocationManager RelocMgr(*this, *ErrOrObj, DMO);
2403 for (const auto &CU : DwarfContext->compile_units()) {
2404 updateDwarfVersion(CU->getVersion());
2405 // Recursively get all modules imported by this one.
2406 auto CUDie = CU->getUnitDIE(false);
2407 if (!CUDie)
2408 continue;
2409 if (!registerModuleReference(CUDie, *CU, ModuleMap, DMO, Ranges, StringPool,
2410 UniquingStringPool, ODRContexts,
2411 ModulesEndOffset, UnitID, IsLittleEndian,
2412 Indent, Quiet)) {
2413 if (Unit) {
2414 std::string Err =
2415 (Filename +
2416 ": Clang modules are expected to have exactly 1 compile unit.\n")
2417 .str();
2418 error(Err);
2419 return make_error<StringError>(Err, inconvertibleErrorCode());
2421 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2422 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2423 // ASTFileSignatures will change randomly when a module is rebuilt.
2424 uint64_t PCMDwoId = getDwoId(CUDie, *CU);
2425 if (PCMDwoId != DwoId) {
2426 if (!Quiet && Options.Verbose)
2427 reportWarning(
2428 Twine("hash mismatch: this object file was built against a "
2429 "different version of the module ") +
2430 Filename,
2431 DMO);
2432 // Update the cache entry with the DwoId of the module loaded from disk.
2433 ClangModules[Filename] = PCMDwoId;
2436 // Add this module.
2437 Unit = std::make_unique<CompileUnit>(*CU, UnitID++, !Options.NoODR,
2438 ModuleName);
2439 Unit->setHasInterestingContent();
2440 analyzeContextInfo(CUDie, 0, *Unit, &ODRContexts.getRoot(),
2441 UniquingStringPool, ODRContexts, ModulesEndOffset,
2442 ParseableSwiftInterfaces,
2443 [&](const Twine &Warning, const DWARFDie &DIE) {
2444 reportWarning(Warning, DMO, &DIE);
2446 // Keep everything.
2447 Unit->markEverythingAsKept();
2450 if (!Unit->getOrigUnit().getUnitDIE().hasChildren())
2451 return Error::success();
2452 if (!Quiet && Options.Verbose) {
2453 outs().indent(Indent);
2454 outs() << "cloning .debug_info from " << Filename << "\n";
2457 UnitListTy CompileUnits;
2458 CompileUnits.push_back(std::move(Unit));
2459 DIECloner(*this, RelocMgr, DIEAlloc, CompileUnits, Options)
2460 .cloneAllCompileUnits(*DwarfContext, DMO, Ranges, StringPool,
2461 IsLittleEndian);
2462 return Error::success();
2465 void DwarfLinkerForBinary::DIECloner::cloneAllCompileUnits(
2466 DWARFContext &DwarfContext, const DebugMapObject &DMO, RangesTy &Ranges,
2467 OffsetsStringPool &StringPool, bool IsLittleEndian) {
2468 if (!Linker.Streamer)
2469 return;
2471 uint64_t OutputDebugInfoSize = Linker.Streamer->getDebugInfoSectionSize();
2472 for (auto &CurrentUnit : CompileUnits) {
2473 auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
2474 CurrentUnit->setStartOffset(OutputDebugInfoSize);
2475 if (!InputDIE) {
2476 OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
2477 continue;
2479 if (CurrentUnit->getInfo(0).Keep) {
2480 // Clone the InputDIE into your Unit DIE in our compile unit since it
2481 // already has a DIE inside of it.
2482 CurrentUnit->createOutputDIE();
2483 cloneDIE(InputDIE, DMO, *CurrentUnit, StringPool, 0 /* PC offset */,
2484 11 /* Unit Header size */, 0, IsLittleEndian,
2485 CurrentUnit->getOutputUnitDIE());
2488 OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
2490 if (Linker.Options.NoOutput)
2491 continue;
2493 // FIXME: for compatibility with the classic dsymutil, we emit
2494 // an empty line table for the unit, even if the unit doesn't
2495 // actually exist in the DIE tree.
2496 if (LLVM_LIKELY(!Linker.Options.Update) || Linker.Options.Translator)
2497 Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext, Ranges, DMO);
2499 Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
2501 if (LLVM_UNLIKELY(Linker.Options.Update))
2502 continue;
2504 Linker.patchRangesForUnit(*CurrentUnit, DwarfContext, DMO);
2505 auto ProcessExpr = [&](StringRef Bytes, SmallVectorImpl<uint8_t> &Buffer) {
2506 DWARFUnit &OrigUnit = CurrentUnit->getOrigUnit();
2507 DataExtractor Data(Bytes, IsLittleEndian, OrigUnit.getAddressByteSize());
2508 cloneExpression(Data,
2509 DWARFExpression(Data, OrigUnit.getVersion(),
2510 OrigUnit.getAddressByteSize()),
2511 DMO, *CurrentUnit, Buffer);
2513 Linker.Streamer->emitLocationsForUnit(*CurrentUnit, DwarfContext,
2514 ProcessExpr);
2517 if (Linker.Options.NoOutput)
2518 return;
2520 // Emit all the compile unit's debug information.
2521 for (auto &CurrentUnit : CompileUnits) {
2522 if (LLVM_LIKELY(!Linker.Options.Update))
2523 Linker.generateUnitRanges(*CurrentUnit);
2525 CurrentUnit->fixupForwardReferences();
2527 if (!CurrentUnit->getOutputUnitDIE())
2528 continue;
2530 assert(Linker.Streamer->getDebugInfoSectionSize() ==
2531 CurrentUnit->getStartOffset());
2532 Linker.Streamer->emitCompileUnitHeader(*CurrentUnit);
2533 Linker.Streamer->emitDIE(*CurrentUnit->getOutputUnitDIE());
2534 assert(Linker.Streamer->getDebugInfoSectionSize() ==
2535 CurrentUnit->computeNextUnitOffset());
2539 void DwarfLinkerForBinary::updateAccelKind(DWARFContext &Dwarf) {
2540 if (Options.TheAccelTableKind != AccelTableKind::Default)
2541 return;
2543 auto &DwarfObj = Dwarf.getDWARFObj();
2545 if (!AtLeastOneDwarfAccelTable &&
2546 (!DwarfObj.getAppleNamesSection().Data.empty() ||
2547 !DwarfObj.getAppleTypesSection().Data.empty() ||
2548 !DwarfObj.getAppleNamespacesSection().Data.empty() ||
2549 !DwarfObj.getAppleObjCSection().Data.empty())) {
2550 AtLeastOneAppleAccelTable = true;
2553 if (!AtLeastOneDwarfAccelTable && !DwarfObj.getNamesSection().Data.empty()) {
2554 AtLeastOneDwarfAccelTable = true;
2558 bool DwarfLinkerForBinary::emitPaperTrailWarnings(
2559 const DebugMapObject &DMO, const DebugMap &Map,
2560 OffsetsStringPool &StringPool) {
2561 if (DMO.getWarnings().empty() || !DMO.empty())
2562 return false;
2564 Streamer->switchToDebugInfoSection(/* Version */ 2);
2565 DIE *CUDie = DIE::get(DIEAlloc, dwarf::DW_TAG_compile_unit);
2566 CUDie->setOffset(11);
2567 StringRef Producer = StringPool.internString("dsymutil");
2568 StringRef File = StringPool.internString(DMO.getObjectFilename());
2569 CUDie->addValue(DIEAlloc, dwarf::DW_AT_producer, dwarf::DW_FORM_strp,
2570 DIEInteger(StringPool.getStringOffset(Producer)));
2571 DIEBlock *String = new (DIEAlloc) DIEBlock();
2572 DIEBlocks.push_back(String);
2573 for (auto &C : File)
2574 String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
2575 DIEInteger(C));
2576 String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
2577 DIEInteger(0));
2579 CUDie->addValue(DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_string, String);
2580 for (const auto &Warning : DMO.getWarnings()) {
2581 DIE &ConstDie = CUDie->addChild(DIE::get(DIEAlloc, dwarf::DW_TAG_constant));
2582 ConstDie.addValue(
2583 DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp,
2584 DIEInteger(StringPool.getStringOffset("dsymutil_warning")));
2585 ConstDie.addValue(DIEAlloc, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag,
2586 DIEInteger(1));
2587 ConstDie.addValue(DIEAlloc, dwarf::DW_AT_const_value, dwarf::DW_FORM_strp,
2588 DIEInteger(StringPool.getStringOffset(Warning)));
2590 unsigned Size = 4 /* FORM_strp */ + File.size() + 1 +
2591 DMO.getWarnings().size() * (4 + 1 + 4) +
2592 1 /* End of children */;
2593 DIEAbbrev Abbrev = CUDie->generateAbbrev();
2594 assignAbbrev(Abbrev);
2595 CUDie->setAbbrevNumber(Abbrev.getNumber());
2596 Size += getULEB128Size(Abbrev.getNumber());
2597 // Abbreviation ordering needed for classic compatibility.
2598 for (auto &Child : CUDie->children()) {
2599 Abbrev = Child.generateAbbrev();
2600 assignAbbrev(Abbrev);
2601 Child.setAbbrevNumber(Abbrev.getNumber());
2602 Size += getULEB128Size(Abbrev.getNumber());
2604 CUDie->setSize(Size);
2605 Streamer->emitPaperTrailWarningsDie(Map.getTriple(), *CUDie);
2607 return true;
2610 static Error copySwiftInterfaces(
2611 const std::map<std::string, std::string> &ParseableSwiftInterfaces,
2612 StringRef Architecture, const LinkOptions &Options) {
2613 std::error_code EC;
2614 SmallString<128> InputPath;
2615 SmallString<128> Path;
2616 sys::path::append(Path, *Options.ResourceDir, "Swift", Architecture);
2617 if ((EC = sys::fs::create_directories(Path.str(), true,
2618 sys::fs::perms::all_all)))
2619 return make_error<StringError>(
2620 "cannot create directory: " + toString(errorCodeToError(EC)), EC);
2621 unsigned BaseLength = Path.size();
2623 for (auto &I : ParseableSwiftInterfaces) {
2624 StringRef ModuleName = I.first;
2625 StringRef InterfaceFile = I.second;
2626 if (!Options.PrependPath.empty()) {
2627 InputPath.clear();
2628 sys::path::append(InputPath, Options.PrependPath, InterfaceFile);
2629 InterfaceFile = InputPath;
2631 sys::path::append(Path, ModuleName);
2632 Path.append(".swiftinterface");
2633 if (Options.Verbose)
2634 outs() << "copy parseable Swift interface " << InterfaceFile << " -> "
2635 << Path.str() << '\n';
2637 // copy_file attempts an APFS clone first, so this should be cheap.
2638 if ((EC = sys::fs::copy_file(InterfaceFile, Path.str())))
2639 warn(Twine("cannot copy parseable Swift interface ") + InterfaceFile +
2640 ": " + toString(errorCodeToError(EC)));
2641 Path.resize(BaseLength);
2643 return Error::success();
2646 static Error emitRemarks(const LinkOptions &Options, StringRef BinaryPath,
2647 StringRef ArchName, const remarks::RemarkLinker &RL) {
2648 // Make sure we don't create the directories and the file if there is nothing
2649 // to serialize.
2650 if (RL.empty())
2651 return Error::success();
2653 SmallString<128> InputPath;
2654 SmallString<128> Path;
2655 // Create the "Remarks" directory in the "Resources" directory.
2656 sys::path::append(Path, *Options.ResourceDir, "Remarks");
2657 if (std::error_code EC = sys::fs::create_directories(Path.str(), true,
2658 sys::fs::perms::all_all))
2659 return errorCodeToError(EC);
2661 // Append the file name.
2662 // For fat binaries, also append a dash and the architecture name.
2663 sys::path::append(Path, sys::path::filename(BinaryPath));
2664 if (Options.NumDebugMaps > 1) {
2665 // More than one debug map means we have a fat binary.
2666 Path += '-';
2667 Path += ArchName;
2670 std::error_code EC;
2671 raw_fd_ostream OS(Options.NoOutput ? "-" : Path.str(), EC, sys::fs::OF_None);
2672 if (EC)
2673 return errorCodeToError(EC);
2675 if (Error E = RL.serialize(OS, Options.RemarksFormat))
2676 return E;
2678 return Error::success();
2681 bool DwarfLinkerForBinary::link(const DebugMap &Map) {
2682 if (!createStreamer(Map.getTriple(), OutFile))
2683 return false;
2685 // Size of the DIEs (and headers) generated for the linked output.
2686 // A unique ID that identifies each compile unit.
2687 unsigned UnitID = 0;
2688 DebugMap ModuleMap(Map.getTriple(), Map.getBinaryPath());
2690 // First populate the data structure we need for each iteration of the
2691 // parallel loop.
2692 unsigned NumObjects = Map.getNumberOfObjects();
2693 std::vector<LinkContext> ObjectContexts;
2694 ObjectContexts.reserve(NumObjects);
2695 for (const auto &Obj : Map.objects()) {
2696 ObjectContexts.emplace_back(Map, *this, *Obj.get());
2697 LinkContext &LC = ObjectContexts.back();
2698 if (LC.ObjectFile)
2699 updateAccelKind(*LC.DwarfContext);
2702 // This Dwarf string pool which is only used for uniquing. This one should
2703 // never be used for offsets as its not thread-safe or predictable.
2704 UniquingStringPool UniquingStringPool(nullptr, true);
2706 // This Dwarf string pool which is used for emission. It must be used
2707 // serially as the order of calling getStringOffset matters for
2708 // reproducibility.
2709 OffsetsStringPool OffsetsStringPool(Options.Translator, true);
2711 // ODR Contexts for the link.
2712 DeclContextTree ODRContexts;
2714 // If we haven't decided on an accelerator table kind yet, we base ourselves
2715 // on the DWARF we have seen so far. At this point we haven't pulled in debug
2716 // information from modules yet, so it is technically possible that they
2717 // would affect the decision. However, as they're built with the same
2718 // compiler and flags, it is safe to assume that they will follow the
2719 // decision made here.
2720 if (Options.TheAccelTableKind == AccelTableKind::Default) {
2721 if (AtLeastOneDwarfAccelTable && !AtLeastOneAppleAccelTable)
2722 Options.TheAccelTableKind = AccelTableKind::Dwarf;
2723 else
2724 Options.TheAccelTableKind = AccelTableKind::Apple;
2727 for (LinkContext &LinkContext : ObjectContexts) {
2728 if (Options.Verbose)
2729 outs() << "DEBUG MAP OBJECT: " << LinkContext.DMO.getObjectFilename()
2730 << "\n";
2732 // N_AST objects (swiftmodule files) should get dumped directly into the
2733 // appropriate DWARF section.
2734 if (LinkContext.DMO.getType() == MachO::N_AST) {
2735 StringRef File = LinkContext.DMO.getObjectFilename();
2736 auto ErrorOrMem = MemoryBuffer::getFile(File);
2737 if (!ErrorOrMem) {
2738 warn("Could not open '" + File + "'\n");
2739 continue;
2741 sys::fs::file_status Stat;
2742 if (auto Err = sys::fs::status(File, Stat)) {
2743 warn(Err.message());
2744 continue;
2746 if (!Options.NoTimestamp) {
2747 // The modification can have sub-second precision so we need to cast
2748 // away the extra precision that's not present in the debug map.
2749 auto ModificationTime =
2750 std::chrono::time_point_cast<std::chrono::seconds>(
2751 Stat.getLastModificationTime());
2752 if (ModificationTime != LinkContext.DMO.getTimestamp()) {
2753 // Not using the helper here as we can easily stream TimePoint<>.
2754 WithColor::warning()
2755 << "Timestamp mismatch for " << File << ": "
2756 << Stat.getLastModificationTime() << " and "
2757 << sys::TimePoint<>(LinkContext.DMO.getTimestamp()) << "\n";
2758 continue;
2762 // Copy the module into the .swift_ast section.
2763 if (!Options.NoOutput)
2764 Streamer->emitSwiftAST((*ErrorOrMem)->getBuffer());
2765 continue;
2768 if (emitPaperTrailWarnings(LinkContext.DMO, Map, OffsetsStringPool))
2769 continue;
2771 if (!LinkContext.ObjectFile)
2772 continue;
2774 // Look for relocations that correspond to debug map entries.
2776 if (LLVM_LIKELY(!Options.Update) &&
2777 !LinkContext.RelocMgr->hasValidRelocs()) {
2778 if (Options.Verbose)
2779 outs() << "No valid relocations found. Skipping.\n";
2781 // Clear this ObjFile entry as a signal to other loops that we should not
2782 // process this iteration.
2783 LinkContext.ObjectFile = nullptr;
2784 continue;
2787 // Setup access to the debug info.
2788 if (!LinkContext.DwarfContext)
2789 continue;
2791 startDebugObject(LinkContext);
2793 // In a first phase, just read in the debug info and load all clang modules.
2794 LinkContext.CompileUnits.reserve(
2795 LinkContext.DwarfContext->getNumCompileUnits());
2797 for (const auto &CU : LinkContext.DwarfContext->compile_units()) {
2798 updateDwarfVersion(CU->getVersion());
2799 auto CUDie = CU->getUnitDIE(false);
2800 if (Options.Verbose) {
2801 outs() << "Input compilation unit:";
2802 DIDumpOptions DumpOpts;
2803 DumpOpts.ChildRecurseDepth = 0;
2804 DumpOpts.Verbose = Options.Verbose;
2805 CUDie.dump(outs(), 0, DumpOpts);
2807 if (CUDie && !LLVM_UNLIKELY(Options.Update))
2808 registerModuleReference(CUDie, *CU, ModuleMap, LinkContext.DMO,
2809 LinkContext.Ranges, OffsetsStringPool,
2810 UniquingStringPool, ODRContexts, 0, UnitID,
2811 LinkContext.DwarfContext->isLittleEndian());
2815 // If we haven't seen any CUs, pick an arbitrary valid Dwarf version anyway.
2816 if (MaxDwarfVersion == 0)
2817 MaxDwarfVersion = 3;
2819 // At this point we know how much data we have emitted. We use this value to
2820 // compare canonical DIE offsets in analyzeContextInfo to see if a definition
2821 // is already emitted, without being affected by canonical die offsets set
2822 // later. This prevents undeterminism when analyze and clone execute
2823 // concurrently, as clone set the canonical DIE offset and analyze reads it.
2824 const uint64_t ModulesEndOffset =
2825 Options.NoOutput ? 0 : Streamer->getDebugInfoSectionSize();
2827 // These variables manage the list of processed object files.
2828 // The mutex and condition variable are to ensure that this is thread safe.
2829 std::mutex ProcessedFilesMutex;
2830 std::condition_variable ProcessedFilesConditionVariable;
2831 BitVector ProcessedFiles(NumObjects, false);
2833 // Analyzing the context info is particularly expensive so it is executed in
2834 // parallel with emitting the previous compile unit.
2835 auto AnalyzeLambda = [&](size_t i) {
2836 auto &LinkContext = ObjectContexts[i];
2838 if (!LinkContext.ObjectFile || !LinkContext.DwarfContext)
2839 return;
2841 for (const auto &CU : LinkContext.DwarfContext->compile_units()) {
2842 updateDwarfVersion(CU->getVersion());
2843 // The !registerModuleReference() condition effectively skips
2844 // over fully resolved skeleton units. This second pass of
2845 // registerModuleReferences doesn't do any new work, but it
2846 // will collect top-level errors, which are suppressed. Module
2847 // warnings were already displayed in the first iteration.
2848 bool Quiet = true;
2849 auto CUDie = CU->getUnitDIE(false);
2850 if (!CUDie || LLVM_UNLIKELY(Options.Update) ||
2851 !registerModuleReference(CUDie, *CU, ModuleMap, LinkContext.DMO,
2852 LinkContext.Ranges, OffsetsStringPool,
2853 UniquingStringPool, ODRContexts,
2854 ModulesEndOffset, UnitID, Quiet)) {
2855 LinkContext.CompileUnits.push_back(std::make_unique<CompileUnit>(
2856 *CU, UnitID++, !Options.NoODR && !Options.Update, ""));
2860 // Now build the DIE parent links that we will use during the next phase.
2861 for (auto &CurrentUnit : LinkContext.CompileUnits) {
2862 auto CUDie = CurrentUnit->getOrigUnit().getUnitDIE();
2863 if (!CUDie)
2864 continue;
2865 analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0,
2866 *CurrentUnit, &ODRContexts.getRoot(),
2867 UniquingStringPool, ODRContexts, ModulesEndOffset,
2868 ParseableSwiftInterfaces,
2869 [&](const Twine &Warning, const DWARFDie &DIE) {
2870 reportWarning(Warning, LinkContext.DMO, &DIE);
2875 // And then the remaining work in serial again.
2876 // Note, although this loop runs in serial, it can run in parallel with
2877 // the analyzeContextInfo loop so long as we process files with indices >=
2878 // than those processed by analyzeContextInfo.
2879 auto CloneLambda = [&](size_t i) {
2880 auto &LinkContext = ObjectContexts[i];
2881 if (!LinkContext.ObjectFile)
2882 return;
2884 // Then mark all the DIEs that need to be present in the linked output
2885 // and collect some information about them.
2886 // Note that this loop can not be merged with the previous one because
2887 // cross-cu references require the ParentIdx to be setup for every CU in
2888 // the object file before calling this.
2889 if (LLVM_UNLIKELY(Options.Update)) {
2890 for (auto &CurrentUnit : LinkContext.CompileUnits)
2891 CurrentUnit->markEverythingAsKept();
2892 Streamer->copyInvariantDebugSection(*LinkContext.ObjectFile);
2893 } else {
2894 for (auto &CurrentUnit : LinkContext.CompileUnits)
2895 lookForDIEsToKeep(*LinkContext.RelocMgr, LinkContext.Ranges,
2896 LinkContext.CompileUnits,
2897 CurrentUnit->getOrigUnit().getUnitDIE(),
2898 LinkContext.DMO, *CurrentUnit, 0);
2901 // The calls to applyValidRelocs inside cloneDIE will walk the reloc
2902 // array again (in the same way findValidRelocsInDebugInfo() did). We
2903 // need to reset the NextValidReloc index to the beginning.
2904 if (LinkContext.RelocMgr->hasValidRelocs() || LLVM_UNLIKELY(Options.Update))
2905 DIECloner(*this, *LinkContext.RelocMgr, DIEAlloc,
2906 LinkContext.CompileUnits, Options)
2907 .cloneAllCompileUnits(*LinkContext.DwarfContext, LinkContext.DMO,
2908 LinkContext.Ranges, OffsetsStringPool,
2909 LinkContext.DwarfContext->isLittleEndian());
2910 if (!Options.NoOutput && !LinkContext.CompileUnits.empty() &&
2911 LLVM_LIKELY(!Options.Update))
2912 patchFrameInfoForObject(
2913 LinkContext.DMO, LinkContext.Ranges, *LinkContext.DwarfContext,
2914 LinkContext.CompileUnits[0]->getOrigUnit().getAddressByteSize());
2916 // Clean-up before starting working on the next object.
2917 endDebugObject(LinkContext);
2920 auto EmitLambda = [&]() {
2921 // Emit everything that's global.
2922 if (!Options.NoOutput) {
2923 Streamer->emitAbbrevs(Abbreviations, MaxDwarfVersion);
2924 Streamer->emitStrings(OffsetsStringPool);
2925 switch (Options.TheAccelTableKind) {
2926 case AccelTableKind::Apple:
2927 Streamer->emitAppleNames(AppleNames);
2928 Streamer->emitAppleNamespaces(AppleNamespaces);
2929 Streamer->emitAppleTypes(AppleTypes);
2930 Streamer->emitAppleObjc(AppleObjc);
2931 break;
2932 case AccelTableKind::Dwarf:
2933 Streamer->emitDebugNames(DebugNames);
2934 break;
2935 case AccelTableKind::Default:
2936 llvm_unreachable("Default should have already been resolved.");
2937 break;
2942 remarks::RemarkLinker RL;
2943 if (!Options.RemarksPrependPath.empty())
2944 RL.setExternalFilePrependPath(Options.RemarksPrependPath);
2945 auto RemarkLinkLambda = [&](size_t i) {
2946 // Link remarks from one object file.
2947 auto &LinkContext = ObjectContexts[i];
2948 if (const object::ObjectFile *Obj = LinkContext.ObjectFile) {
2949 Error E = RL.link(*Obj);
2950 if (Error NewE = handleErrors(
2951 std::move(E), [&](std::unique_ptr<FileError> EC) -> Error {
2952 return remarksErrorHandler(LinkContext.DMO, *this,
2953 std::move(EC));
2955 return NewE;
2957 return Error(Error::success());
2960 auto AnalyzeAll = [&]() {
2961 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
2962 AnalyzeLambda(i);
2964 std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2965 ProcessedFiles.set(i);
2966 ProcessedFilesConditionVariable.notify_one();
2970 auto CloneAll = [&]() {
2971 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
2973 std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2974 if (!ProcessedFiles[i]) {
2975 ProcessedFilesConditionVariable.wait(
2976 LockGuard, [&]() { return ProcessedFiles[i]; });
2980 CloneLambda(i);
2982 EmitLambda();
2985 auto EmitRemarksLambda = [&]() {
2986 StringRef ArchName = Map.getTriple().getArchName();
2987 return emitRemarks(Options, Map.getBinaryPath(), ArchName, RL);
2990 // Instead of making error handling a lot more complicated using futures,
2991 // write to one llvm::Error instance if something went wrong.
2992 // We're assuming RemarkLinkAllError is alive longer than the thread
2993 // executing RemarkLinkAll.
2994 auto RemarkLinkAll = [&](Error &RemarkLinkAllError) {
2995 // Allow assigning to the error only within the lambda.
2996 ErrorAsOutParameter EAO(&RemarkLinkAllError);
2997 for (unsigned i = 0, e = NumObjects; i != e; ++i)
2998 if ((RemarkLinkAllError = RemarkLinkLambda(i)))
2999 return;
3001 if ((RemarkLinkAllError = EmitRemarksLambda()))
3002 return;
3005 // To limit memory usage in the single threaded case, analyze and clone are
3006 // run sequentially so the LinkContext is freed after processing each object
3007 // in endDebugObject.
3008 if (Options.Threads == 1) {
3009 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
3010 AnalyzeLambda(i);
3011 CloneLambda(i);
3013 if (Error E = RemarkLinkLambda(i))
3014 return error(toString(std::move(E)));
3016 EmitLambda();
3018 if (Error E = EmitRemarksLambda())
3019 return error(toString(std::move(E)));
3021 } else {
3022 // This should not be constructed on the single-threaded path to avoid fatal
3023 // errors from unchecked llvm::Error objects.
3024 Error RemarkLinkAllError = Error::success();
3026 ThreadPool pool(3);
3027 pool.async(AnalyzeAll);
3028 pool.async(CloneAll);
3029 pool.async(RemarkLinkAll, std::ref(RemarkLinkAllError));
3030 pool.wait();
3032 // Report errors from RemarkLinkAll, if any.
3033 if (Error E = std::move(RemarkLinkAllError))
3034 return error(toString(std::move(E)));
3037 if (Options.NoOutput)
3038 return true;
3040 if (Options.ResourceDir && !ParseableSwiftInterfaces.empty()) {
3041 StringRef ArchName = Triple::getArchTypeName(Map.getTriple().getArch());
3042 if (auto E =
3043 copySwiftInterfaces(ParseableSwiftInterfaces, ArchName, Options))
3044 return error(toString(std::move(E)));
3047 return Streamer->finish(Map, Options.Translator);
3048 } // namespace dsymutil
3050 bool linkDwarf(raw_fd_ostream &OutFile, BinaryHolder &BinHolder,
3051 const DebugMap &DM, LinkOptions Options) {
3052 DwarfLinkerForBinary Linker(OutFile, BinHolder, std::move(Options));
3053 return Linker.link(DM);
3056 } // namespace dsymutil
3057 } // namespace llvm