[ARM] Add support for MVE pre and post inc loads and stores
[llvm-core.git] / tools / dsymutil / DwarfLinker.cpp
blobe97e0e6489b24140d209542f3f10ac995f209689
1 //===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
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 "DwarfLinker.h"
10 #include "BinaryHolder.h"
11 #include "DebugMap.h"
12 #include "DeclContext.h"
13 #include "DwarfStreamer.h"
14 #include "MachOUtils.h"
15 #include "NonRelocatableStringpool.h"
16 #include "dsymutil.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/DenseMapInfo.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/Hashing.h"
24 #include "llvm/ADT/IntervalMap.h"
25 #include "llvm/ADT/None.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/ADT/PointerIntPair.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/SmallString.h"
30 #include "llvm/ADT/StringMap.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/ADT/Triple.h"
33 #include "llvm/ADT/Twine.h"
34 #include "llvm/BinaryFormat/Dwarf.h"
35 #include "llvm/BinaryFormat/MachO.h"
36 #include "llvm/CodeGen/AccelTable.h"
37 #include "llvm/CodeGen/AsmPrinter.h"
38 #include "llvm/CodeGen/DIE.h"
39 #include "llvm/Config/config.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/Support/Allocator.h"
67 #include "llvm/Support/Casting.h"
68 #include "llvm/Support/Compiler.h"
69 #include "llvm/Support/DJB.h"
70 #include "llvm/Support/DataExtractor.h"
71 #include "llvm/Support/Error.h"
72 #include "llvm/Support/ErrorHandling.h"
73 #include "llvm/Support/ErrorOr.h"
74 #include "llvm/Support/FileSystem.h"
75 #include "llvm/Support/Format.h"
76 #include "llvm/Support/LEB128.h"
77 #include "llvm/Support/MathExtras.h"
78 #include "llvm/Support/MemoryBuffer.h"
79 #include "llvm/Support/Path.h"
80 #include "llvm/Support/TargetRegistry.h"
81 #include "llvm/Support/ThreadPool.h"
82 #include "llvm/Support/ToolOutputFile.h"
83 #include "llvm/Support/WithColor.h"
84 #include "llvm/Support/raw_ostream.h"
85 #include "llvm/Target/TargetMachine.h"
86 #include "llvm/Target/TargetOptions.h"
87 #include <algorithm>
88 #include <cassert>
89 #include <cinttypes>
90 #include <climits>
91 #include <cstdint>
92 #include <cstdlib>
93 #include <cstring>
94 #include <limits>
95 #include <map>
96 #include <memory>
97 #include <string>
98 #include <system_error>
99 #include <tuple>
100 #include <utility>
101 #include <vector>
103 namespace llvm {
104 namespace dsymutil {
106 /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
107 /// CompileUnit object instead.
108 static CompileUnit *getUnitForOffset(const UnitListTy &Units, uint64_t Offset) {
109 auto CU = std::upper_bound(
110 Units.begin(), Units.end(), Offset,
111 [](uint64_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
112 return LHS < RHS->getOrigUnit().getNextUnitOffset();
114 return CU != Units.end() ? CU->get() : nullptr;
117 /// Resolve the DIE attribute reference that has been extracted in \p RefValue.
118 /// The resulting DIE might be in another CompileUnit which is stored into \p
119 /// ReferencedCU. \returns null if resolving fails for any reason.
120 static DWARFDie resolveDIEReference(const DwarfLinker &Linker,
121 const DebugMapObject &DMO,
122 const UnitListTy &Units,
123 const DWARFFormValue &RefValue,
124 const DWARFDie &DIE, CompileUnit *&RefCU) {
125 assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
126 uint64_t RefOffset = *RefValue.getAsReference();
127 if ((RefCU = getUnitForOffset(Units, RefOffset)))
128 if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
129 // In a file with broken references, an attribute might point to a NULL
130 // DIE.
131 if (!RefDie.isNULL())
132 return RefDie;
135 Linker.reportWarning("could not find referenced DIE", DMO, &DIE);
136 return DWARFDie();
139 /// \returns whether the passed \a Attr type might contain a DIE reference
140 /// suitable for ODR uniquing.
141 static bool isODRAttribute(uint16_t Attr) {
142 switch (Attr) {
143 default:
144 return false;
145 case dwarf::DW_AT_type:
146 case dwarf::DW_AT_containing_type:
147 case dwarf::DW_AT_specification:
148 case dwarf::DW_AT_abstract_origin:
149 case dwarf::DW_AT_import:
150 return true;
152 llvm_unreachable("Improper attribute.");
155 static bool isTypeTag(uint16_t Tag) {
156 switch (Tag) {
157 case dwarf::DW_TAG_array_type:
158 case dwarf::DW_TAG_class_type:
159 case dwarf::DW_TAG_enumeration_type:
160 case dwarf::DW_TAG_pointer_type:
161 case dwarf::DW_TAG_reference_type:
162 case dwarf::DW_TAG_string_type:
163 case dwarf::DW_TAG_structure_type:
164 case dwarf::DW_TAG_subroutine_type:
165 case dwarf::DW_TAG_typedef:
166 case dwarf::DW_TAG_union_type:
167 case dwarf::DW_TAG_ptr_to_member_type:
168 case dwarf::DW_TAG_set_type:
169 case dwarf::DW_TAG_subrange_type:
170 case dwarf::DW_TAG_base_type:
171 case dwarf::DW_TAG_const_type:
172 case dwarf::DW_TAG_constant:
173 case dwarf::DW_TAG_file_type:
174 case dwarf::DW_TAG_namelist:
175 case dwarf::DW_TAG_packed_type:
176 case dwarf::DW_TAG_volatile_type:
177 case dwarf::DW_TAG_restrict_type:
178 case dwarf::DW_TAG_atomic_type:
179 case dwarf::DW_TAG_interface_type:
180 case dwarf::DW_TAG_unspecified_type:
181 case dwarf::DW_TAG_shared_type:
182 return true;
183 default:
184 break;
186 return false;
189 bool DwarfLinker::DIECloner::getDIENames(const DWARFDie &Die,
190 AttributesInfo &Info,
191 OffsetsStringPool &StringPool,
192 bool StripTemplate) {
193 // This function will be called on DIEs having low_pcs and
194 // ranges. As getting the name might be more expansive, filter out
195 // blocks directly.
196 if (Die.getTag() == dwarf::DW_TAG_lexical_block)
197 return false;
199 // FIXME: a bit wasteful as the first getName might return the
200 // short name.
201 if (!Info.MangledName)
202 if (const char *MangledName = Die.getName(DINameKind::LinkageName))
203 Info.MangledName = StringPool.getEntry(MangledName);
205 if (!Info.Name)
206 if (const char *Name = Die.getName(DINameKind::ShortName))
207 Info.Name = StringPool.getEntry(Name);
209 if (StripTemplate && Info.Name && Info.MangledName != Info.Name) {
210 // FIXME: dsymutil compatibility. This is wrong for operator<
211 auto Split = Info.Name.getString().split('<');
212 if (!Split.second.empty())
213 Info.NameWithoutTemplate = StringPool.getEntry(Split.first);
216 return Info.Name || Info.MangledName;
219 /// Report a warning to the user, optionally including information about a
220 /// specific \p DIE related to the warning.
221 void DwarfLinker::reportWarning(const Twine &Warning, const DebugMapObject &DMO,
222 const DWARFDie *DIE) const {
223 StringRef Context = DMO.getObjectFilename();
224 warn(Warning, Context);
226 if (!Options.Verbose || !DIE)
227 return;
229 DIDumpOptions DumpOpts;
230 DumpOpts.ChildRecurseDepth = 0;
231 DumpOpts.Verbose = Options.Verbose;
233 WithColor::note() << " in DIE:\n";
234 DIE->dump(errs(), 6 /* Indent */, DumpOpts);
237 bool DwarfLinker::createStreamer(const Triple &TheTriple,
238 raw_fd_ostream &OutFile) {
239 if (Options.NoOutput)
240 return true;
242 Streamer = llvm::make_unique<DwarfStreamer>(OutFile, Options);
243 return Streamer->init(TheTriple);
246 /// Resolve the relative path to a build artifact referenced by DWARF by
247 /// applying DW_AT_comp_dir.
248 static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
249 sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
252 /// Collect references to parseable Swift interfaces in imported
253 /// DW_TAG_module blocks.
254 static void analyzeImportedModule(
255 const DWARFDie &DIE, CompileUnit &CU,
256 std::map<std::string, std::string> &ParseableSwiftInterfaces,
257 std::function<void(const Twine &, const DWARFDie &)> ReportWarning) {
258 if (CU.getLanguage() != dwarf::DW_LANG_Swift)
259 return;
261 StringRef Path = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_include_path));
262 if (!Path.endswith(".swiftinterface"))
263 return;
264 if (Optional<DWARFFormValue> Val = DIE.find(dwarf::DW_AT_name))
265 if (Optional<const char *> Name = Val->getAsCString()) {
266 auto &Entry = ParseableSwiftInterfaces[*Name];
267 // The prepend path is applied later when copying.
268 DWARFDie CUDie = CU.getOrigUnit().getUnitDIE();
269 SmallString<128> ResolvedPath;
270 if (sys::path::is_relative(Path))
271 resolveRelativeObjectPath(ResolvedPath, CUDie);
272 sys::path::append(ResolvedPath, Path);
273 if (!Entry.empty() && Entry != ResolvedPath)
274 ReportWarning(
275 Twine("Conflicting parseable interfaces for Swift Module ") +
276 *Name + ": " + Entry + " and " + Path,
277 DIE);
278 Entry = ResolvedPath.str();
282 /// Recursive helper to build the global DeclContext information and
283 /// gather the child->parent relationships in the original compile unit.
285 /// \return true when this DIE and all of its children are only
286 /// forward declarations to types defined in external clang modules
287 /// (i.e., forward declarations that are children of a DW_TAG_module).
288 static bool analyzeContextInfo(
289 const DWARFDie &DIE, unsigned ParentIdx, CompileUnit &CU,
290 DeclContext *CurrentDeclContext, UniquingStringPool &StringPool,
291 DeclContextTree &Contexts, uint64_t ModulesEndOffset,
292 std::map<std::string, std::string> &ParseableSwiftInterfaces,
293 std::function<void(const Twine &, const DWARFDie &)> ReportWarning,
294 bool InImportedModule = false) {
295 unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
296 CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx);
298 // Clang imposes an ODR on modules(!) regardless of the language:
299 // "The module-id should consist of only a single identifier,
300 // which provides the name of the module being defined. Each
301 // module shall have a single definition."
303 // This does not extend to the types inside the modules:
304 // "[I]n C, this implies that if two structs are defined in
305 // different submodules with the same name, those two types are
306 // distinct types (but may be compatible types if their
307 // definitions match)."
309 // We treat non-C++ modules like namespaces for this reason.
310 if (DIE.getTag() == dwarf::DW_TAG_module && ParentIdx == 0 &&
311 dwarf::toString(DIE.find(dwarf::DW_AT_name), "") !=
312 CU.getClangModuleName()) {
313 InImportedModule = true;
314 analyzeImportedModule(DIE, CU, ParseableSwiftInterfaces, ReportWarning);
317 Info.ParentIdx = ParentIdx;
318 bool InClangModule = CU.isClangModule() || InImportedModule;
319 if (CU.hasODR() || InClangModule) {
320 if (CurrentDeclContext) {
321 auto PtrInvalidPair = Contexts.getChildDeclContext(
322 *CurrentDeclContext, DIE, CU, StringPool, InClangModule);
323 CurrentDeclContext = PtrInvalidPair.getPointer();
324 Info.Ctxt =
325 PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
326 if (Info.Ctxt)
327 Info.Ctxt->setDefinedInClangModule(InClangModule);
328 } else
329 Info.Ctxt = CurrentDeclContext = nullptr;
332 Info.Prune = InImportedModule;
333 if (DIE.hasChildren())
334 for (auto Child : DIE.children())
335 Info.Prune &= analyzeContextInfo(Child, MyIdx, CU, CurrentDeclContext,
336 StringPool, Contexts, ModulesEndOffset,
337 ParseableSwiftInterfaces, ReportWarning,
338 InImportedModule);
340 // Prune this DIE if it is either a forward declaration inside a
341 // DW_TAG_module or a DW_TAG_module that contains nothing but
342 // forward declarations.
343 Info.Prune &= (DIE.getTag() == dwarf::DW_TAG_module) ||
344 (isTypeTag(DIE.getTag()) &&
345 dwarf::toUnsigned(DIE.find(dwarf::DW_AT_declaration), 0));
347 // Only prune forward declarations inside a DW_TAG_module for which a
348 // definition exists elsewhere.
349 if (ModulesEndOffset == 0)
350 Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
351 else
352 Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() > 0 &&
353 Info.Ctxt->getCanonicalDIEOffset() <= ModulesEndOffset;
355 return Info.Prune;
356 } // namespace dsymutil
358 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
359 switch (Tag) {
360 default:
361 return false;
362 case dwarf::DW_TAG_subprogram:
363 case dwarf::DW_TAG_lexical_block:
364 case dwarf::DW_TAG_subroutine_type:
365 case dwarf::DW_TAG_structure_type:
366 case dwarf::DW_TAG_class_type:
367 case dwarf::DW_TAG_union_type:
368 return true;
370 llvm_unreachable("Invalid Tag");
373 void DwarfLinker::startDebugObject(LinkContext &Context) {
374 // Iterate over the debug map entries and put all the ones that are
375 // functions (because they have a size) into the Ranges map. This map is
376 // very similar to the FunctionRanges that are stored in each unit, with 2
377 // notable differences:
379 // 1. Obviously this one is global, while the other ones are per-unit.
381 // 2. This one contains not only the functions described in the DIE
382 // tree, but also the ones that are only in the debug map.
384 // The latter information is required to reproduce dsymutil's logic while
385 // linking line tables. The cases where this information matters look like
386 // bugs that need to be investigated, but for now we need to reproduce
387 // dsymutil's behavior.
388 // FIXME: Once we understood exactly if that information is needed,
389 // maybe totally remove this (or try to use it to do a real
390 // -gline-tables-only on Darwin.
391 for (const auto &Entry : Context.DMO.symbols()) {
392 const auto &Mapping = Entry.getValue();
393 if (Mapping.Size && Mapping.ObjectAddress)
394 Context.Ranges[*Mapping.ObjectAddress] = DebugMapObjectRange(
395 *Mapping.ObjectAddress + Mapping.Size,
396 int64_t(Mapping.BinaryAddress) - *Mapping.ObjectAddress);
400 void DwarfLinker::endDebugObject(LinkContext &Context) {
401 Context.Clear();
403 for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
404 (*I)->~DIEBlock();
405 for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
406 (*I)->~DIELoc();
408 DIEBlocks.clear();
409 DIELocs.clear();
410 DIEAlloc.Reset();
413 static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
414 switch (Arch) {
415 case Triple::x86:
416 return RelocType == MachO::GENERIC_RELOC_SECTDIFF ||
417 RelocType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
418 case Triple::x86_64:
419 return RelocType == MachO::X86_64_RELOC_SUBTRACTOR;
420 case Triple::arm:
421 case Triple::thumb:
422 return RelocType == MachO::ARM_RELOC_SECTDIFF ||
423 RelocType == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
424 RelocType == MachO::ARM_RELOC_HALF ||
425 RelocType == MachO::ARM_RELOC_HALF_SECTDIFF;
426 case Triple::aarch64:
427 return RelocType == MachO::ARM64_RELOC_SUBTRACTOR;
428 default:
429 return false;
433 /// Iterate over the relocations of the given \p Section and
434 /// store the ones that correspond to debug map entries into the
435 /// ValidRelocs array.
436 void DwarfLinker::RelocationManager::findValidRelocsMachO(
437 const object::SectionRef &Section, const object::MachOObjectFile &Obj,
438 const DebugMapObject &DMO) {
439 Expected<StringRef> ContentsOrErr = Section.getContents();
440 if (!ContentsOrErr) {
441 consumeError(ContentsOrErr.takeError());
442 Linker.reportWarning("error reading section", DMO);
443 return;
445 DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian(), 0);
446 bool SkipNext = false;
448 for (const object::RelocationRef &Reloc : Section.relocations()) {
449 if (SkipNext) {
450 SkipNext = false;
451 continue;
454 object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
455 MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
457 if (isMachOPairedReloc(Obj.getAnyRelocationType(MachOReloc),
458 Obj.getArch())) {
459 SkipNext = true;
460 Linker.reportWarning("unsupported relocation in debug_info section.",
461 DMO);
462 continue;
465 unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
466 uint64_t Offset64 = Reloc.getOffset();
467 if ((RelocSize != 4 && RelocSize != 8)) {
468 Linker.reportWarning("unsupported relocation in debug_info section.",
469 DMO);
470 continue;
472 uint64_t OffsetCopy = Offset64;
473 // Mach-o uses REL relocations, the addend is at the relocation offset.
474 uint64_t Addend = Data.getUnsigned(&OffsetCopy, RelocSize);
475 uint64_t SymAddress;
476 int64_t SymOffset;
478 if (Obj.isRelocationScattered(MachOReloc)) {
479 // The address of the base symbol for scattered relocations is
480 // stored in the reloc itself. The actual addend will store the
481 // base address plus the offset.
482 SymAddress = Obj.getScatteredRelocationValue(MachOReloc);
483 SymOffset = int64_t(Addend) - SymAddress;
484 } else {
485 SymAddress = Addend;
486 SymOffset = 0;
489 auto Sym = Reloc.getSymbol();
490 if (Sym != Obj.symbol_end()) {
491 Expected<StringRef> SymbolName = Sym->getName();
492 if (!SymbolName) {
493 consumeError(SymbolName.takeError());
494 Linker.reportWarning("error getting relocation symbol name.", DMO);
495 continue;
497 if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
498 ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
499 } else if (const auto *Mapping = DMO.lookupObjectAddress(SymAddress)) {
500 // Do not store the addend. The addend was the address of the symbol in
501 // the object file, the address in the binary that is stored in the debug
502 // map doesn't need to be offset.
503 ValidRelocs.emplace_back(Offset64, RelocSize, SymOffset, Mapping);
508 /// Dispatch the valid relocation finding logic to the
509 /// appropriate handler depending on the object file format.
510 bool DwarfLinker::RelocationManager::findValidRelocs(
511 const object::SectionRef &Section, const object::ObjectFile &Obj,
512 const DebugMapObject &DMO) {
513 // Dispatch to the right handler depending on the file type.
514 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
515 findValidRelocsMachO(Section, *MachOObj, DMO);
516 else
517 Linker.reportWarning(
518 Twine("unsupported object file type: ") + Obj.getFileName(), DMO);
520 if (ValidRelocs.empty())
521 return false;
523 // Sort the relocations by offset. We will walk the DIEs linearly in
524 // the file, this allows us to just keep an index in the relocation
525 // array that we advance during our walk, rather than resorting to
526 // some associative container. See DwarfLinker::NextValidReloc.
527 llvm::sort(ValidRelocs);
528 return true;
531 /// Look for relocations in the debug_info section that match
532 /// entries in the debug map. These relocations will drive the Dwarf
533 /// link by indicating which DIEs refer to symbols present in the
534 /// linked binary.
535 /// \returns whether there are any valid relocations in the debug info.
536 bool DwarfLinker::RelocationManager::findValidRelocsInDebugInfo(
537 const object::ObjectFile &Obj, const DebugMapObject &DMO) {
538 // Find the debug_info section.
539 for (const object::SectionRef &Section : Obj.sections()) {
540 StringRef SectionName;
541 Section.getName(SectionName);
542 SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
543 if (SectionName != "debug_info")
544 continue;
545 return findValidRelocs(Section, Obj, DMO);
547 return false;
550 /// Checks that there is a relocation against an actual debug
551 /// map entry between \p StartOffset and \p NextOffset.
553 /// This function must be called with offsets in strictly ascending
554 /// order because it never looks back at relocations it already 'went past'.
555 /// \returns true and sets Info.InDebugMap if it is the case.
556 bool DwarfLinker::RelocationManager::hasValidRelocation(
557 uint64_t StartOffset, uint64_t EndOffset, CompileUnit::DIEInfo &Info) {
558 assert(NextValidReloc == 0 ||
559 StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
560 if (NextValidReloc >= ValidRelocs.size())
561 return false;
563 uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
565 // We might need to skip some relocs that we didn't consider. For
566 // example the high_pc of a discarded DIE might contain a reloc that
567 // is in the list because it actually corresponds to the start of a
568 // function that is in the debug map.
569 while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
570 RelocOffset = ValidRelocs[++NextValidReloc].Offset;
572 if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
573 return false;
575 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
576 const auto &Mapping = ValidReloc.Mapping->getValue();
577 uint64_t ObjectAddress = Mapping.ObjectAddress
578 ? uint64_t(*Mapping.ObjectAddress)
579 : std::numeric_limits<uint64_t>::max();
580 if (Linker.Options.Verbose)
581 outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
582 << " "
583 << format("\t%016" PRIx64 " => %016" PRIx64, ObjectAddress,
584 uint64_t(Mapping.BinaryAddress));
586 Info.AddrAdjust = int64_t(Mapping.BinaryAddress) + ValidReloc.Addend;
587 if (Mapping.ObjectAddress)
588 Info.AddrAdjust -= ObjectAddress;
589 Info.InDebugMap = true;
590 return true;
593 /// Get the starting and ending (exclusive) offset for the
594 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
595 /// supposed to point to the position of the first attribute described
596 /// by \p Abbrev.
597 /// \return [StartOffset, EndOffset) as a pair.
598 static std::pair<uint64_t, uint64_t>
599 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
600 uint64_t Offset, const DWARFUnit &Unit) {
601 DataExtractor Data = Unit.getDebugInfoExtractor();
603 for (unsigned i = 0; i < Idx; ++i)
604 DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset,
605 Unit.getFormParams());
607 uint64_t End = Offset;
608 DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
609 Unit.getFormParams());
611 return std::make_pair(Offset, End);
614 /// Check if a variable describing DIE should be kept.
615 /// \returns updated TraversalFlags.
616 unsigned DwarfLinker::shouldKeepVariableDIE(RelocationManager &RelocMgr,
617 const DWARFDie &DIE,
618 CompileUnit &Unit,
619 CompileUnit::DIEInfo &MyInfo,
620 unsigned Flags) {
621 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
623 // Global variables with constant value can always be kept.
624 if (!(Flags & TF_InFunctionScope) &&
625 Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
626 MyInfo.InDebugMap = true;
627 return Flags | TF_Keep;
630 Optional<uint32_t> LocationIdx =
631 Abbrev->findAttributeIndex(dwarf::DW_AT_location);
632 if (!LocationIdx)
633 return Flags;
635 uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
636 const DWARFUnit &OrigUnit = Unit.getOrigUnit();
637 uint64_t LocationOffset, LocationEndOffset;
638 std::tie(LocationOffset, LocationEndOffset) =
639 getAttributeOffsets(Abbrev, *LocationIdx, Offset, OrigUnit);
641 // See if there is a relocation to a valid debug map entry inside
642 // this variable's location. The order is important here. We want to
643 // always check in the variable has a valid relocation, so that the
644 // DIEInfo is filled. However, we don't want a static variable in a
645 // function to force us to keep the enclosing function.
646 if (!RelocMgr.hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
647 (Flags & TF_InFunctionScope))
648 return Flags;
650 if (Options.Verbose) {
651 DIDumpOptions DumpOpts;
652 DumpOpts.ChildRecurseDepth = 0;
653 DumpOpts.Verbose = Options.Verbose;
654 DIE.dump(outs(), 8 /* Indent */, DumpOpts);
657 return Flags | TF_Keep;
660 /// Check if a function describing DIE should be kept.
661 /// \returns updated TraversalFlags.
662 unsigned DwarfLinker::shouldKeepSubprogramDIE(
663 RelocationManager &RelocMgr, RangesTy &Ranges, const DWARFDie &DIE,
664 const DebugMapObject &DMO, CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
665 unsigned Flags) {
666 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
668 Flags |= TF_InFunctionScope;
670 Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
671 if (!LowPcIdx)
672 return Flags;
674 uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
675 DWARFUnit &OrigUnit = Unit.getOrigUnit();
676 uint64_t LowPcOffset, LowPcEndOffset;
677 std::tie(LowPcOffset, LowPcEndOffset) =
678 getAttributeOffsets(Abbrev, *LowPcIdx, Offset, OrigUnit);
680 auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
681 assert(LowPc.hasValue() && "low_pc attribute is not an address.");
682 if (!LowPc ||
683 !RelocMgr.hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
684 return Flags;
686 if (Options.Verbose) {
687 DIDumpOptions DumpOpts;
688 DumpOpts.ChildRecurseDepth = 0;
689 DumpOpts.Verbose = Options.Verbose;
690 DIE.dump(outs(), 8 /* Indent */, DumpOpts);
693 if (DIE.getTag() == dwarf::DW_TAG_label) {
694 if (Unit.hasLabelAt(*LowPc))
695 return Flags;
696 // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
697 // that don't fall into the CU's aranges. This is wrong IMO. Debug info
698 // generation bugs aside, this is really wrong in the case of labels, where
699 // a label marking the end of a function will have a PC == CU's high_pc.
700 if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc))
701 .getValueOr(UINT64_MAX) <= LowPc)
702 return Flags;
703 Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust);
704 return Flags | TF_Keep;
707 Flags |= TF_Keep;
709 Optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
710 if (!HighPc) {
711 reportWarning("Function without high_pc. Range will be discarded.\n", DMO,
712 &DIE);
713 return Flags;
716 // Replace the debug map range with a more accurate one.
717 Ranges[*LowPc] = DebugMapObjectRange(*HighPc, MyInfo.AddrAdjust);
718 Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
719 return Flags;
722 /// Check if a DIE should be kept.
723 /// \returns updated TraversalFlags.
724 unsigned DwarfLinker::shouldKeepDIE(RelocationManager &RelocMgr,
725 RangesTy &Ranges, const DWARFDie &DIE,
726 const DebugMapObject &DMO,
727 CompileUnit &Unit,
728 CompileUnit::DIEInfo &MyInfo,
729 unsigned Flags) {
730 switch (DIE.getTag()) {
731 case dwarf::DW_TAG_constant:
732 case dwarf::DW_TAG_variable:
733 return shouldKeepVariableDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
734 case dwarf::DW_TAG_subprogram:
735 case dwarf::DW_TAG_label:
736 return shouldKeepSubprogramDIE(RelocMgr, Ranges, DIE, DMO, Unit, MyInfo,
737 Flags);
738 case dwarf::DW_TAG_base_type:
739 // DWARF Expressions may reference basic types, but scanning them
740 // is expensive. Basic types are tiny, so just keep all of them.
741 case dwarf::DW_TAG_imported_module:
742 case dwarf::DW_TAG_imported_declaration:
743 case dwarf::DW_TAG_imported_unit:
744 // We always want to keep these.
745 return Flags | TF_Keep;
746 default:
747 break;
750 return Flags;
753 /// Mark the passed DIE as well as all the ones it depends on
754 /// as kept.
756 /// This function is called by lookForDIEsToKeep on DIEs that are
757 /// newly discovered to be needed in the link. It recursively calls
758 /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
759 /// TraversalFlags to inform it that it's not doing the primary DIE
760 /// tree walk.
761 void DwarfLinker::keepDIEAndDependencies(
762 RelocationManager &RelocMgr, RangesTy &Ranges, const UnitListTy &Units,
763 const DWARFDie &Die, CompileUnit::DIEInfo &MyInfo,
764 const DebugMapObject &DMO, CompileUnit &CU, bool UseODR) {
765 DWARFUnit &Unit = CU.getOrigUnit();
766 MyInfo.Keep = true;
768 // We're looking for incomplete types.
769 MyInfo.Incomplete = Die.getTag() != dwarf::DW_TAG_subprogram &&
770 Die.getTag() != dwarf::DW_TAG_member &&
771 dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0);
773 // First mark all the parent chain as kept.
774 unsigned AncestorIdx = MyInfo.ParentIdx;
775 while (!CU.getInfo(AncestorIdx).Keep) {
776 unsigned ODRFlag = UseODR ? TF_ODR : 0;
777 lookForDIEsToKeep(RelocMgr, Ranges, Units, Unit.getDIEAtIndex(AncestorIdx),
778 DMO, CU,
779 TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag);
780 AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
783 // Then we need to mark all the DIEs referenced by this DIE's
784 // attributes as kept.
785 DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
786 const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
787 uint64_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
789 // Mark all DIEs referenced through attributes as kept.
790 for (const auto &AttrSpec : Abbrev->attributes()) {
791 DWARFFormValue Val(AttrSpec.Form);
792 if (!Val.isFormClass(DWARFFormValue::FC_Reference) ||
793 AttrSpec.Attr == dwarf::DW_AT_sibling) {
794 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
795 Unit.getFormParams());
796 continue;
799 Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit);
800 CompileUnit *ReferencedCU;
801 if (auto RefDie =
802 resolveDIEReference(*this, DMO, Units, Val, Die, ReferencedCU)) {
803 uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDie);
804 CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
805 bool IsModuleRef = Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() &&
806 Info.Ctxt->isDefinedInClangModule();
807 // If the referenced DIE has a DeclContext that has already been
808 // emitted, then do not keep the one in this CU. We'll link to
809 // the canonical DIE in cloneDieReferenceAttribute.
810 // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
811 // be necessary and could be advantageously replaced by
812 // ReferencedCU->hasODR() && CU.hasODR().
813 // FIXME: compatibility with dsymutil-classic. There is no
814 // reason not to unique ref_addr references.
815 if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && (UseODR || IsModuleRef) &&
816 Info.Ctxt &&
817 Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt &&
818 Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr))
819 continue;
821 // Keep a module forward declaration if there is no definition.
822 if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
823 Info.Ctxt->getCanonicalDIEOffset()))
824 Info.Prune = false;
826 unsigned ODRFlag = UseODR ? TF_ODR : 0;
827 lookForDIEsToKeep(RelocMgr, Ranges, Units, RefDie, DMO, *ReferencedCU,
828 TF_Keep | TF_DependencyWalk | ODRFlag);
830 // The incomplete property is propagated if the current DIE is complete
831 // but references an incomplete DIE.
832 if (Info.Incomplete && !MyInfo.Incomplete &&
833 (Die.getTag() == dwarf::DW_TAG_typedef ||
834 Die.getTag() == dwarf::DW_TAG_member ||
835 Die.getTag() == dwarf::DW_TAG_reference_type ||
836 Die.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
837 Die.getTag() == dwarf::DW_TAG_pointer_type))
838 MyInfo.Incomplete = true;
843 namespace {
844 /// This class represents an item in the work list. In addition to it's obvious
845 /// purpose of representing the state associated with a particular run of the
846 /// work loop, it also serves as a marker to indicate that we should run the
847 /// "continuation" code.
849 /// Originally, the latter was lambda which allowed arbitrary code to be run.
850 /// Because we always need to run the exact same code, it made more sense to
851 /// use a boolean and repurpose the already existing DIE field.
852 struct WorklistItem {
853 DWARFDie Die;
854 unsigned Flags;
855 bool IsContinuation;
856 CompileUnit::DIEInfo *ChildInfo = nullptr;
858 /// Construct a classic worklist item.
859 WorklistItem(DWARFDie Die, unsigned Flags)
860 : Die(Die), Flags(Flags), IsContinuation(false){};
862 /// Creates a continuation marker.
863 WorklistItem(DWARFDie Die) : Die(Die), IsContinuation(true){};
865 } // namespace
867 // Helper that updates the completeness of the current DIE. It depends on the
868 // fact that the incompletness of its children is already computed.
869 static void updateIncompleteness(const DWARFDie &Die,
870 CompileUnit::DIEInfo &ChildInfo,
871 CompileUnit &CU) {
872 // Only propagate incomplete members.
873 if (Die.getTag() != dwarf::DW_TAG_structure_type &&
874 Die.getTag() != dwarf::DW_TAG_class_type)
875 return;
877 unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
878 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
880 if (MyInfo.Incomplete)
881 return;
883 if (ChildInfo.Incomplete || ChildInfo.Prune)
884 MyInfo.Incomplete = true;
887 /// Recursively walk the \p DIE tree and look for DIEs to
888 /// keep. Store that information in \p CU's DIEInfo.
890 /// This function is the entry point of the DIE selection
891 /// algorithm. It is expected to walk the DIE tree in file order and
892 /// (though the mediation of its helper) call hasValidRelocation() on
893 /// each DIE that might be a 'root DIE' (See DwarfLinker class
894 /// comment).
895 /// While walking the dependencies of root DIEs, this function is
896 /// also called, but during these dependency walks the file order is
897 /// not respected. The TF_DependencyWalk flag tells us which kind of
898 /// traversal we are currently doing.
900 /// The return value indicates whether the DIE is incomplete.
901 void DwarfLinker::lookForDIEsToKeep(RelocationManager &RelocMgr,
902 RangesTy &Ranges, const UnitListTy &Units,
903 const DWARFDie &Die,
904 const DebugMapObject &DMO, CompileUnit &CU,
905 unsigned Flags) {
906 // LIFO work list.
907 SmallVector<WorklistItem, 4> Worklist;
908 Worklist.emplace_back(Die, Flags);
910 while (!Worklist.empty()) {
911 WorklistItem Current = Worklist.back();
912 Worklist.pop_back();
914 if (Current.IsContinuation) {
915 updateIncompleteness(Current.Die, *Current.ChildInfo, CU);
916 continue;
919 unsigned Idx = CU.getOrigUnit().getDIEIndex(Current.Die);
920 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
922 // At this point we are guaranteed to have a continuation marker before us
923 // in the worklist, except for the last DIE.
924 if (!Worklist.empty())
925 Worklist.back().ChildInfo = &MyInfo;
927 if (MyInfo.Prune)
928 continue;
930 // If the Keep flag is set, we are marking a required DIE's dependencies.
931 // If our target is already marked as kept, we're all set.
932 bool AlreadyKept = MyInfo.Keep;
933 if ((Current.Flags & TF_DependencyWalk) && AlreadyKept)
934 continue;
936 // We must not call shouldKeepDIE while called from keepDIEAndDependencies,
937 // because it would screw up the relocation finding logic.
938 if (!(Current.Flags & TF_DependencyWalk))
939 Current.Flags = shouldKeepDIE(RelocMgr, Ranges, Current.Die, DMO, CU,
940 MyInfo, Current.Flags);
942 // If it is a newly kept DIE mark it as well as all its dependencies as
943 // kept.
944 if (!AlreadyKept && (Current.Flags & TF_Keep)) {
945 bool UseOdr = (Current.Flags & TF_DependencyWalk)
946 ? (Current.Flags & TF_ODR)
947 : CU.hasODR();
948 keepDIEAndDependencies(RelocMgr, Ranges, Units, Current.Die, MyInfo, DMO,
949 CU, UseOdr);
952 // The TF_ParentWalk flag tells us that we are currently walking up
953 // the parent chain of a required DIE, and we don't want to mark all
954 // the children of the parents as kept (consider for example a
955 // DW_TAG_namespace node in the parent chain). There are however a
956 // set of DIE types for which we want to ignore that directive and still
957 // walk their children.
958 if (dieNeedsChildrenToBeMeaningful(Current.Die.getTag()))
959 Current.Flags &= ~TF_ParentWalk;
961 if (!Current.Die.hasChildren() || (Current.Flags & TF_ParentWalk))
962 continue;
964 // Add children in reverse order to the worklist to effectively process
965 // them in order.
966 for (auto Child : reverse(Current.Die.children())) {
967 // Add continuation marker before every child to calculate incompleteness
968 // after the last child is processed. We can't store this information in
969 // the same item because we might have to process other continuations
970 // first.
971 Worklist.emplace_back(Current.Die);
972 Worklist.emplace_back(Child, Current.Flags);
977 /// Assign an abbreviation number to \p Abbrev.
979 /// Our DIEs get freed after every DebugMapObject has been processed,
980 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
981 /// the instances hold by the DIEs. When we encounter an abbreviation
982 /// that we don't know, we create a permanent copy of it.
983 void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
984 // Check the set for priors.
985 FoldingSetNodeID ID;
986 Abbrev.Profile(ID);
987 void *InsertToken;
988 DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
990 // If it's newly added.
991 if (InSet) {
992 // Assign existing abbreviation number.
993 Abbrev.setNumber(InSet->getNumber());
994 } else {
995 // Add to abbreviation list.
996 Abbreviations.push_back(
997 llvm::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
998 for (const auto &Attr : Abbrev.getData())
999 Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
1000 AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
1001 // Assign the unique abbreviation number.
1002 Abbrev.setNumber(Abbreviations.size());
1003 Abbreviations.back()->setNumber(Abbreviations.size());
1007 unsigned DwarfLinker::DIECloner::cloneStringAttribute(
1008 DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1009 const DWARFUnit &U, OffsetsStringPool &StringPool, AttributesInfo &Info) {
1010 // Switch everything to out of line strings.
1011 const char *String = *Val.getAsCString();
1012 auto StringEntry = StringPool.getEntry(String);
1014 // Update attributes info.
1015 if (AttrSpec.Attr == dwarf::DW_AT_name)
1016 Info.Name = StringEntry;
1017 else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
1018 AttrSpec.Attr == dwarf::DW_AT_linkage_name)
1019 Info.MangledName = StringEntry;
1021 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
1022 DIEInteger(StringEntry.getOffset()));
1024 return 4;
1027 unsigned DwarfLinker::DIECloner::cloneDieReferenceAttribute(
1028 DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
1029 unsigned AttrSize, const DWARFFormValue &Val, const DebugMapObject &DMO,
1030 CompileUnit &Unit) {
1031 const DWARFUnit &U = Unit.getOrigUnit();
1032 uint64_t Ref = *Val.getAsReference();
1033 DIE *NewRefDie = nullptr;
1034 CompileUnit *RefUnit = nullptr;
1035 DeclContext *Ctxt = nullptr;
1037 DWARFDie RefDie =
1038 resolveDIEReference(Linker, DMO, CompileUnits, Val, InputDIE, RefUnit);
1040 // If the referenced DIE is not found, drop the attribute.
1041 if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling)
1042 return 0;
1044 unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
1045 CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
1047 // If we already have emitted an equivalent DeclContext, just point
1048 // at it.
1049 if (isODRAttribute(AttrSpec.Attr)) {
1050 Ctxt = RefInfo.Ctxt;
1051 if (Ctxt && Ctxt->getCanonicalDIEOffset()) {
1052 DIEInteger Attr(Ctxt->getCanonicalDIEOffset());
1053 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1054 dwarf::DW_FORM_ref_addr, Attr);
1055 return U.getRefAddrByteSize();
1059 if (!RefInfo.Clone) {
1060 assert(Ref > InputDIE.getOffset());
1061 // We haven't cloned this DIE yet. Just create an empty one and
1062 // store it. It'll get really cloned when we process it.
1063 RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
1065 NewRefDie = RefInfo.Clone;
1067 if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
1068 (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
1069 // We cannot currently rely on a DIEEntry to emit ref_addr
1070 // references, because the implementation calls back to DwarfDebug
1071 // to find the unit offset. (We don't have a DwarfDebug)
1072 // FIXME: we should be able to design DIEEntry reliance on
1073 // DwarfDebug away.
1074 uint64_t Attr;
1075 if (Ref < InputDIE.getOffset()) {
1076 // We must have already cloned that DIE.
1077 uint32_t NewRefOffset =
1078 RefUnit->getStartOffset() + NewRefDie->getOffset();
1079 Attr = NewRefOffset;
1080 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1081 dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
1082 } else {
1083 // A forward reference. Note and fixup later.
1084 Attr = 0xBADDEF;
1085 Unit.noteForwardReference(
1086 NewRefDie, RefUnit, Ctxt,
1087 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1088 dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
1090 return U.getRefAddrByteSize();
1093 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1094 dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
1095 return AttrSize;
1098 void DwarfLinker::DIECloner::cloneExpression(
1099 DataExtractor &Data, DWARFExpression Expression, const DebugMapObject &DMO,
1100 CompileUnit &Unit, SmallVectorImpl<uint8_t> &OutputBuffer) {
1101 using Encoding = DWARFExpression::Operation::Encoding;
1103 uint64_t OpOffset = 0;
1104 for (auto &Op : Expression) {
1105 auto Description = Op.getDescription();
1106 // DW_OP_const_type is variable-length and has 3
1107 // operands. DWARFExpression thus far only supports 2.
1108 auto Op0 = Description.Op[0];
1109 auto Op1 = Description.Op[1];
1110 if ((Op0 == Encoding::BaseTypeRef && Op1 != Encoding::SizeNA) ||
1111 (Op1 == Encoding::BaseTypeRef && Op0 != Encoding::Size1))
1112 Linker.reportWarning("Unsupported DW_OP encoding.", DMO);
1114 if ((Op0 == Encoding::BaseTypeRef && Op1 == Encoding::SizeNA) ||
1115 (Op1 == Encoding::BaseTypeRef && Op0 == Encoding::Size1)) {
1116 // This code assumes that the other non-typeref operand fits into 1 byte.
1117 assert(OpOffset < Op.getEndOffset());
1118 uint32_t ULEBsize = Op.getEndOffset() - OpOffset - 1;
1119 assert(ULEBsize <= 16);
1121 // Copy over the operation.
1122 OutputBuffer.push_back(Op.getCode());
1123 uint64_t RefOffset;
1124 if (Op1 == Encoding::SizeNA) {
1125 RefOffset = Op.getRawOperand(0);
1126 } else {
1127 OutputBuffer.push_back(Op.getRawOperand(0));
1128 RefOffset = Op.getRawOperand(1);
1130 auto RefDie = Unit.getOrigUnit().getDIEForOffset(RefOffset);
1131 uint32_t RefIdx = Unit.getOrigUnit().getDIEIndex(RefDie);
1132 CompileUnit::DIEInfo &Info = Unit.getInfo(RefIdx);
1133 uint32_t Offset = 0;
1134 if (DIE *Clone = Info.Clone)
1135 Offset = Clone->getOffset();
1136 else
1137 Linker.reportWarning("base type ref doesn't point to DW_TAG_base_type.",
1138 DMO);
1139 uint8_t ULEB[16];
1140 unsigned RealSize = encodeULEB128(Offset, ULEB, ULEBsize);
1141 if (RealSize > ULEBsize) {
1142 // Emit the generic type as a fallback.
1143 RealSize = encodeULEB128(0, ULEB, ULEBsize);
1144 Linker.reportWarning("base type ref doesn't fit.", DMO);
1146 assert(RealSize == ULEBsize && "padding failed");
1147 ArrayRef<uint8_t> ULEBbytes(ULEB, ULEBsize);
1148 OutputBuffer.append(ULEBbytes.begin(), ULEBbytes.end());
1149 } else {
1150 // Copy over everything else unmodified.
1151 StringRef Bytes = Data.getData().slice(OpOffset, Op.getEndOffset());
1152 OutputBuffer.append(Bytes.begin(), Bytes.end());
1154 OpOffset = Op.getEndOffset();
1158 unsigned DwarfLinker::DIECloner::cloneBlockAttribute(
1159 DIE &Die, const DebugMapObject &DMO, CompileUnit &Unit,
1160 AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize,
1161 bool IsLittleEndian) {
1162 DIEValueList *Attr;
1163 DIEValue Value;
1164 DIELoc *Loc = nullptr;
1165 DIEBlock *Block = nullptr;
1166 if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
1167 Loc = new (DIEAlloc) DIELoc;
1168 Linker.DIELocs.push_back(Loc);
1169 } else {
1170 Block = new (DIEAlloc) DIEBlock;
1171 Linker.DIEBlocks.push_back(Block);
1173 Attr = Loc ? static_cast<DIEValueList *>(Loc)
1174 : static_cast<DIEValueList *>(Block);
1176 if (Loc)
1177 Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
1178 dwarf::Form(AttrSpec.Form), Loc);
1179 else
1180 Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
1181 dwarf::Form(AttrSpec.Form), Block);
1183 // If the block is a DWARF Expression, clone it into the temporary
1184 // buffer using cloneExpression(), otherwise copy the data directly.
1185 SmallVector<uint8_t, 32> Buffer;
1186 ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
1187 if (DWARFAttribute::mayHaveLocationDescription(AttrSpec.Attr) &&
1188 (Val.isFormClass(DWARFFormValue::FC_Block) ||
1189 Val.isFormClass(DWARFFormValue::FC_Exprloc))) {
1190 DWARFUnit &OrigUnit = Unit.getOrigUnit();
1191 DataExtractor Data(StringRef((const char *)Bytes.data(), Bytes.size()),
1192 IsLittleEndian, OrigUnit.getAddressByteSize());
1193 DWARFExpression Expr(Data, OrigUnit.getVersion(),
1194 OrigUnit.getAddressByteSize());
1195 cloneExpression(Data, Expr, DMO, Unit, Buffer);
1196 Bytes = Buffer;
1198 for (auto Byte : Bytes)
1199 Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
1200 dwarf::DW_FORM_data1, DIEInteger(Byte));
1202 // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1203 // the DIE class, this if could be replaced by
1204 // Attr->setSize(Bytes.size()).
1205 if (Linker.Streamer) {
1206 auto *AsmPrinter = &Linker.Streamer->getAsmPrinter();
1207 if (Loc)
1208 Loc->ComputeSize(AsmPrinter);
1209 else
1210 Block->ComputeSize(AsmPrinter);
1212 Die.addValue(DIEAlloc, Value);
1213 return AttrSize;
1216 unsigned DwarfLinker::DIECloner::cloneAddressAttribute(
1217 DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1218 const CompileUnit &Unit, AttributesInfo &Info) {
1219 uint64_t Addr = *Val.getAsAddress();
1221 if (LLVM_UNLIKELY(Linker.Options.Update)) {
1222 if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
1223 Info.HasLowPc = true;
1224 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1225 dwarf::Form(AttrSpec.Form), DIEInteger(Addr));
1226 return Unit.getOrigUnit().getAddressByteSize();
1229 if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
1230 if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
1231 Die.getTag() == dwarf::DW_TAG_lexical_block)
1232 // The low_pc of a block or inline subroutine might get
1233 // relocated because it happens to match the low_pc of the
1234 // enclosing subprogram. To prevent issues with that, always use
1235 // the low_pc from the input DIE if relocations have been applied.
1236 Addr = (Info.OrigLowPc != std::numeric_limits<uint64_t>::max()
1237 ? Info.OrigLowPc
1238 : Addr) +
1239 Info.PCOffset;
1240 else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1241 Addr = Unit.getLowPc();
1242 if (Addr == std::numeric_limits<uint64_t>::max())
1243 return 0;
1245 Info.HasLowPc = true;
1246 } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
1247 if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1248 if (uint64_t HighPc = Unit.getHighPc())
1249 Addr = HighPc;
1250 else
1251 return 0;
1252 } else
1253 // If we have a high_pc recorded for the input DIE, use
1254 // it. Otherwise (when no relocations where applied) just use the
1255 // one we just decoded.
1256 Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
1259 Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
1260 static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
1261 return Unit.getOrigUnit().getAddressByteSize();
1264 unsigned DwarfLinker::DIECloner::cloneScalarAttribute(
1265 DIE &Die, const DWARFDie &InputDIE, const DebugMapObject &DMO,
1266 CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1267 unsigned AttrSize, AttributesInfo &Info) {
1268 uint64_t Value;
1270 if (LLVM_UNLIKELY(Linker.Options.Update)) {
1271 if (auto OptionalValue = Val.getAsUnsignedConstant())
1272 Value = *OptionalValue;
1273 else if (auto OptionalValue = Val.getAsSignedConstant())
1274 Value = *OptionalValue;
1275 else if (auto OptionalValue = Val.getAsSectionOffset())
1276 Value = *OptionalValue;
1277 else {
1278 Linker.reportWarning(
1279 "Unsupported scalar attribute form. Dropping attribute.", DMO,
1280 &InputDIE);
1281 return 0;
1283 if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1284 Info.IsDeclaration = true;
1285 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1286 dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1287 return AttrSize;
1290 if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
1291 Die.getTag() == dwarf::DW_TAG_compile_unit) {
1292 if (Unit.getLowPc() == -1ULL)
1293 return 0;
1294 // Dwarf >= 4 high_pc is an size, not an address.
1295 Value = Unit.getHighPc() - Unit.getLowPc();
1296 } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
1297 Value = *Val.getAsSectionOffset();
1298 else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
1299 Value = *Val.getAsSignedConstant();
1300 else if (auto OptionalValue = Val.getAsUnsignedConstant())
1301 Value = *OptionalValue;
1302 else {
1303 Linker.reportWarning(
1304 "Unsupported scalar attribute form. Dropping attribute.", DMO,
1305 &InputDIE);
1306 return 0;
1308 PatchLocation Patch =
1309 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1310 dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1311 if (AttrSpec.Attr == dwarf::DW_AT_ranges) {
1312 Unit.noteRangeAttribute(Die, Patch);
1313 Info.HasRanges = true;
1316 // A more generic way to check for location attributes would be
1317 // nice, but it's very unlikely that any other attribute needs a
1318 // location list.
1319 // FIXME: use DWARFAttribute::mayHaveLocationDescription().
1320 else if (AttrSpec.Attr == dwarf::DW_AT_location ||
1321 AttrSpec.Attr == dwarf::DW_AT_frame_base)
1322 Unit.noteLocationAttribute(Patch, Info.PCOffset);
1323 else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1324 Info.IsDeclaration = true;
1326 return AttrSize;
1329 /// Clone \p InputDIE's attribute described by \p AttrSpec with
1330 /// value \p Val, and add it to \p Die.
1331 /// \returns the size of the cloned attribute.
1332 unsigned DwarfLinker::DIECloner::cloneAttribute(
1333 DIE &Die, const DWARFDie &InputDIE, const DebugMapObject &DMO,
1334 CompileUnit &Unit, OffsetsStringPool &StringPool, const DWARFFormValue &Val,
1335 const AttributeSpec AttrSpec, unsigned AttrSize, AttributesInfo &Info,
1336 bool IsLittleEndian) {
1337 const DWARFUnit &U = Unit.getOrigUnit();
1339 switch (AttrSpec.Form) {
1340 case dwarf::DW_FORM_strp:
1341 case dwarf::DW_FORM_string:
1342 return cloneStringAttribute(Die, AttrSpec, Val, U, StringPool, Info);
1343 case dwarf::DW_FORM_ref_addr:
1344 case dwarf::DW_FORM_ref1:
1345 case dwarf::DW_FORM_ref2:
1346 case dwarf::DW_FORM_ref4:
1347 case dwarf::DW_FORM_ref8:
1348 return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
1349 DMO, Unit);
1350 case dwarf::DW_FORM_block:
1351 case dwarf::DW_FORM_block1:
1352 case dwarf::DW_FORM_block2:
1353 case dwarf::DW_FORM_block4:
1354 case dwarf::DW_FORM_exprloc:
1355 return cloneBlockAttribute(Die, DMO, Unit, AttrSpec, Val, AttrSize,
1356 IsLittleEndian);
1357 case dwarf::DW_FORM_addr:
1358 return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
1359 case dwarf::DW_FORM_data1:
1360 case dwarf::DW_FORM_data2:
1361 case dwarf::DW_FORM_data4:
1362 case dwarf::DW_FORM_data8:
1363 case dwarf::DW_FORM_udata:
1364 case dwarf::DW_FORM_sdata:
1365 case dwarf::DW_FORM_sec_offset:
1366 case dwarf::DW_FORM_flag:
1367 case dwarf::DW_FORM_flag_present:
1368 return cloneScalarAttribute(Die, InputDIE, DMO, Unit, AttrSpec, Val,
1369 AttrSize, Info);
1370 default:
1371 Linker.reportWarning(
1372 "Unsupported attribute form in cloneAttribute. Dropping.", DMO,
1373 &InputDIE);
1376 return 0;
1379 /// Apply the valid relocations found by findValidRelocs() to
1380 /// the buffer \p Data, taking into account that Data is at \p BaseOffset
1381 /// in the debug_info section.
1383 /// Like for findValidRelocs(), this function must be called with
1384 /// monotonic \p BaseOffset values.
1386 /// \returns whether any reloc has been applied.
1387 bool DwarfLinker::RelocationManager::applyValidRelocs(
1388 MutableArrayRef<char> Data, uint64_t BaseOffset, bool IsLittleEndian) {
1389 assert((NextValidReloc == 0 ||
1390 BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
1391 "BaseOffset should only be increasing.");
1392 if (NextValidReloc >= ValidRelocs.size())
1393 return false;
1395 // Skip relocs that haven't been applied.
1396 while (NextValidReloc < ValidRelocs.size() &&
1397 ValidRelocs[NextValidReloc].Offset < BaseOffset)
1398 ++NextValidReloc;
1400 bool Applied = false;
1401 uint64_t EndOffset = BaseOffset + Data.size();
1402 while (NextValidReloc < ValidRelocs.size() &&
1403 ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
1404 ValidRelocs[NextValidReloc].Offset < EndOffset) {
1405 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
1406 assert(ValidReloc.Offset - BaseOffset < Data.size());
1407 assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
1408 char Buf[8];
1409 uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
1410 Value += ValidReloc.Addend;
1411 for (unsigned i = 0; i != ValidReloc.Size; ++i) {
1412 unsigned Index = IsLittleEndian ? i : (ValidReloc.Size - i - 1);
1413 Buf[i] = uint8_t(Value >> (Index * 8));
1415 assert(ValidReloc.Size <= sizeof(Buf));
1416 memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
1417 Applied = true;
1420 return Applied;
1423 static bool isObjCSelector(StringRef Name) {
1424 return Name.size() > 2 && (Name[0] == '-' || Name[0] == '+') &&
1425 (Name[1] == '[');
1428 void DwarfLinker::DIECloner::addObjCAccelerator(CompileUnit &Unit,
1429 const DIE *Die,
1430 DwarfStringPoolEntryRef Name,
1431 OffsetsStringPool &StringPool,
1432 bool SkipPubSection) {
1433 assert(isObjCSelector(Name.getString()) && "not an objc selector");
1434 // Objective C method or class function.
1435 // "- [Class(Category) selector :withArg ...]"
1436 StringRef ClassNameStart(Name.getString().drop_front(2));
1437 size_t FirstSpace = ClassNameStart.find(' ');
1438 if (FirstSpace == StringRef::npos)
1439 return;
1441 StringRef SelectorStart(ClassNameStart.data() + FirstSpace + 1);
1442 if (!SelectorStart.size())
1443 return;
1445 StringRef Selector(SelectorStart.data(), SelectorStart.size() - 1);
1446 Unit.addNameAccelerator(Die, StringPool.getEntry(Selector), SkipPubSection);
1448 // Add an entry for the class name that points to this
1449 // method/class function.
1450 StringRef ClassName(ClassNameStart.data(), FirstSpace);
1451 Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassName), SkipPubSection);
1453 if (ClassName[ClassName.size() - 1] == ')') {
1454 size_t OpenParens = ClassName.find('(');
1455 if (OpenParens != StringRef::npos) {
1456 StringRef ClassNameNoCategory(ClassName.data(), OpenParens);
1457 Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassNameNoCategory),
1458 SkipPubSection);
1460 std::string MethodNameNoCategory(Name.getString().data(), OpenParens + 2);
1461 // FIXME: The missing space here may be a bug, but
1462 // dsymutil-classic also does it this way.
1463 MethodNameNoCategory.append(SelectorStart);
1464 Unit.addNameAccelerator(Die, StringPool.getEntry(MethodNameNoCategory),
1465 SkipPubSection);
1470 static bool
1471 shouldSkipAttribute(DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
1472 uint16_t Tag, bool InDebugMap, bool SkipPC,
1473 bool InFunctionScope) {
1474 switch (AttrSpec.Attr) {
1475 default:
1476 return false;
1477 case dwarf::DW_AT_low_pc:
1478 case dwarf::DW_AT_high_pc:
1479 case dwarf::DW_AT_ranges:
1480 return SkipPC;
1481 case dwarf::DW_AT_location:
1482 case dwarf::DW_AT_frame_base:
1483 // FIXME: for some reason dsymutil-classic keeps the location attributes
1484 // when they are of block type (i.e. not location lists). This is totally
1485 // wrong for globals where we will keep a wrong address. It is mostly
1486 // harmless for locals, but there is no point in keeping these anyway when
1487 // the function wasn't linked.
1488 return (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
1489 !InDebugMap)) &&
1490 !DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
1494 DIE *DwarfLinker::DIECloner::cloneDIE(
1495 const DWARFDie &InputDIE, const DebugMapObject &DMO, CompileUnit &Unit,
1496 OffsetsStringPool &StringPool, int64_t PCOffset, uint32_t OutOffset,
1497 unsigned Flags, bool IsLittleEndian, DIE *Die) {
1498 DWARFUnit &U = Unit.getOrigUnit();
1499 unsigned Idx = U.getDIEIndex(InputDIE);
1500 CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
1502 // Should the DIE appear in the output?
1503 if (!Unit.getInfo(Idx).Keep)
1504 return nullptr;
1506 uint64_t Offset = InputDIE.getOffset();
1507 assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
1508 if (!Die) {
1509 // The DIE might have been already created by a forward reference
1510 // (see cloneDieReferenceAttribute()).
1511 if (!Info.Clone)
1512 Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
1513 Die = Info.Clone;
1516 assert(Die->getTag() == InputDIE.getTag());
1517 Die->setOffset(OutOffset);
1518 if ((Unit.hasODR() || Unit.isClangModule()) && !Info.Incomplete &&
1519 Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt &&
1520 Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt &&
1521 !Info.Ctxt->getCanonicalDIEOffset()) {
1522 // We are about to emit a DIE that is the root of its own valid
1523 // DeclContext tree. Make the current offset the canonical offset
1524 // for this context.
1525 Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
1528 // Extract and clone every attribute.
1529 DWARFDataExtractor Data = U.getDebugInfoExtractor();
1530 // Point to the next DIE (generally there is always at least a NULL
1531 // entry after the current one). If this is a lone
1532 // DW_TAG_compile_unit without any children, point to the next unit.
1533 uint64_t NextOffset = (Idx + 1 < U.getNumDIEs())
1534 ? U.getDIEAtIndex(Idx + 1).getOffset()
1535 : U.getNextUnitOffset();
1536 AttributesInfo AttrInfo;
1538 // We could copy the data only if we need to apply a relocation to it. After
1539 // testing, it seems there is no performance downside to doing the copy
1540 // unconditionally, and it makes the code simpler.
1541 SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
1542 Data =
1543 DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
1544 // Modify the copy with relocated addresses.
1545 if (RelocMgr.applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
1546 // If we applied relocations, we store the value of high_pc that was
1547 // potentially stored in the input DIE. If high_pc is an address
1548 // (Dwarf version == 2), then it might have been relocated to a
1549 // totally unrelated value (because the end address in the object
1550 // file might be start address of another function which got moved
1551 // independently by the linker). The computation of the actual
1552 // high_pc value is done in cloneAddressAttribute().
1553 AttrInfo.OrigHighPc =
1554 dwarf::toAddress(InputDIE.find(dwarf::DW_AT_high_pc), 0);
1555 // Also store the low_pc. It might get relocated in an
1556 // inline_subprogram that happens at the beginning of its
1557 // inlining function.
1558 AttrInfo.OrigLowPc = dwarf::toAddress(InputDIE.find(dwarf::DW_AT_low_pc),
1559 std::numeric_limits<uint64_t>::max());
1562 // Reset the Offset to 0 as we will be working on the local copy of
1563 // the data.
1564 Offset = 0;
1566 const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
1567 Offset += getULEB128Size(Abbrev->getCode());
1569 // We are entering a subprogram. Get and propagate the PCOffset.
1570 if (Die->getTag() == dwarf::DW_TAG_subprogram)
1571 PCOffset = Info.AddrAdjust;
1572 AttrInfo.PCOffset = PCOffset;
1574 if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
1575 Flags |= TF_InFunctionScope;
1576 if (!Info.InDebugMap && LLVM_LIKELY(!Options.Update))
1577 Flags |= TF_SkipPC;
1580 bool Copied = false;
1581 for (const auto &AttrSpec : Abbrev->attributes()) {
1582 if (LLVM_LIKELY(!Options.Update) &&
1583 shouldSkipAttribute(AttrSpec, Die->getTag(), Info.InDebugMap,
1584 Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
1585 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
1586 U.getFormParams());
1587 // FIXME: dsymutil-classic keeps the old abbreviation around
1588 // even if it's not used. We can remove this (and the copyAbbrev
1589 // helper) as soon as bit-for-bit compatibility is not a goal anymore.
1590 if (!Copied) {
1591 copyAbbrev(*InputDIE.getAbbreviationDeclarationPtr(), Unit.hasODR());
1592 Copied = true;
1594 continue;
1597 DWARFFormValue Val(AttrSpec.Form);
1598 uint64_t AttrSize = Offset;
1599 Val.extractValue(Data, &Offset, U.getFormParams(), &U);
1600 AttrSize = Offset - AttrSize;
1602 OutOffset += cloneAttribute(*Die, InputDIE, DMO, Unit, StringPool, Val,
1603 AttrSpec, AttrSize, AttrInfo, IsLittleEndian);
1606 // Look for accelerator entries.
1607 uint16_t Tag = InputDIE.getTag();
1608 // FIXME: This is slightly wrong. An inline_subroutine without a
1609 // low_pc, but with AT_ranges might be interesting to get into the
1610 // accelerator tables too. For now stick with dsymutil's behavior.
1611 if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) &&
1612 Tag != dwarf::DW_TAG_compile_unit &&
1613 getDIENames(InputDIE, AttrInfo, StringPool,
1614 Tag != dwarf::DW_TAG_inlined_subroutine)) {
1615 if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
1616 Unit.addNameAccelerator(Die, AttrInfo.MangledName,
1617 Tag == dwarf::DW_TAG_inlined_subroutine);
1618 if (AttrInfo.Name) {
1619 if (AttrInfo.NameWithoutTemplate)
1620 Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate,
1621 /* SkipPubSection */ true);
1622 Unit.addNameAccelerator(Die, AttrInfo.Name,
1623 Tag == dwarf::DW_TAG_inlined_subroutine);
1625 if (AttrInfo.Name && isObjCSelector(AttrInfo.Name.getString()))
1626 addObjCAccelerator(Unit, Die, AttrInfo.Name, StringPool,
1627 /* SkipPubSection =*/true);
1629 } else if (Tag == dwarf::DW_TAG_namespace) {
1630 if (!AttrInfo.Name)
1631 AttrInfo.Name = StringPool.getEntry("(anonymous namespace)");
1632 Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
1633 } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
1634 getDIENames(InputDIE, AttrInfo, StringPool) && AttrInfo.Name &&
1635 AttrInfo.Name.getString()[0]) {
1636 uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, DMO);
1637 uint64_t RuntimeLang =
1638 dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
1639 .getValueOr(0);
1640 bool ObjCClassIsImplementation =
1641 (RuntimeLang == dwarf::DW_LANG_ObjC ||
1642 RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
1643 dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
1644 .getValueOr(0);
1645 Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1646 Hash);
1649 // Determine whether there are any children that we want to keep.
1650 bool HasChildren = false;
1651 for (auto Child : InputDIE.children()) {
1652 unsigned Idx = U.getDIEIndex(Child);
1653 if (Unit.getInfo(Idx).Keep) {
1654 HasChildren = true;
1655 break;
1659 DIEAbbrev NewAbbrev = Die->generateAbbrev();
1660 if (HasChildren)
1661 NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
1662 // Assign a permanent abbrev number
1663 Linker.AssignAbbrev(NewAbbrev);
1664 Die->setAbbrevNumber(NewAbbrev.getNumber());
1666 // Add the size of the abbreviation number to the output offset.
1667 OutOffset += getULEB128Size(Die->getAbbrevNumber());
1669 if (!HasChildren) {
1670 // Update our size.
1671 Die->setSize(OutOffset - Die->getOffset());
1672 return Die;
1675 // Recursively clone children.
1676 for (auto Child : InputDIE.children()) {
1677 if (DIE *Clone = cloneDIE(Child, DMO, Unit, StringPool, PCOffset, OutOffset,
1678 Flags, IsLittleEndian)) {
1679 Die->addChild(Clone);
1680 OutOffset = Clone->getOffset() + Clone->getSize();
1684 // Account for the end of children marker.
1685 OutOffset += sizeof(int8_t);
1686 // Update our size.
1687 Die->setSize(OutOffset - Die->getOffset());
1688 return Die;
1691 /// Patch the input object file relevant debug_ranges entries
1692 /// and emit them in the output file. Update the relevant attributes
1693 /// to point at the new entries.
1694 void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
1695 DWARFContext &OrigDwarf,
1696 const DebugMapObject &DMO) const {
1697 DWARFDebugRangeList RangeList;
1698 const auto &FunctionRanges = Unit.getFunctionRanges();
1699 unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
1700 DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
1701 OrigDwarf.getDWARFObj().getRangesSection(),
1702 OrigDwarf.isLittleEndian(), AddressSize);
1703 auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
1704 DWARFUnit &OrigUnit = Unit.getOrigUnit();
1705 auto OrigUnitDie = OrigUnit.getUnitDIE(false);
1706 uint64_t OrigLowPc =
1707 dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc), -1ULL);
1708 // Ranges addresses are based on the unit's low_pc. Compute the
1709 // offset we need to apply to adapt to the new unit's low_pc.
1710 int64_t UnitPcOffset = 0;
1711 if (OrigLowPc != -1ULL)
1712 UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
1714 for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
1715 uint64_t Offset = RangeAttribute.get();
1716 RangeAttribute.set(Streamer->getRangesSectionSize());
1717 if (Error E = RangeList.extract(RangeExtractor, &Offset)) {
1718 llvm::consumeError(std::move(E));
1719 reportWarning("invalid range list ignored.", DMO);
1720 RangeList.clear();
1722 const auto &Entries = RangeList.getEntries();
1723 if (!Entries.empty()) {
1724 const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
1726 if (CurrRange == InvalidRange ||
1727 First.StartAddress + OrigLowPc < CurrRange.start() ||
1728 First.StartAddress + OrigLowPc >= CurrRange.stop()) {
1729 CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
1730 if (CurrRange == InvalidRange ||
1731 CurrRange.start() > First.StartAddress + OrigLowPc) {
1732 reportWarning("no mapping for range.", DMO);
1733 continue;
1738 Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
1739 AddressSize);
1743 /// Generate the debug_aranges entries for \p Unit and if the
1744 /// unit has a DW_AT_ranges attribute, also emit the debug_ranges
1745 /// contribution for this attribute.
1746 /// FIXME: this could actually be done right in patchRangesForUnit,
1747 /// but for the sake of initial bit-for-bit compatibility with legacy
1748 /// dsymutil, we have to do it in a delayed pass.
1749 void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
1750 auto Attr = Unit.getUnitRangesAttribute();
1751 if (Attr)
1752 Attr->set(Streamer->getRangesSectionSize());
1753 Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
1756 /// Insert the new line info sequence \p Seq into the current
1757 /// set of already linked line info \p Rows.
1758 static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
1759 std::vector<DWARFDebugLine::Row> &Rows) {
1760 if (Seq.empty())
1761 return;
1763 if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
1764 Rows.insert(Rows.end(), Seq.begin(), Seq.end());
1765 Seq.clear();
1766 return;
1769 object::SectionedAddress Front = Seq.front().Address;
1770 auto InsertPoint = partition_point(
1771 Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
1773 // FIXME: this only removes the unneeded end_sequence if the
1774 // sequences have been inserted in order. Using a global sort like
1775 // described in patchLineTableForUnit() and delaying the end_sequene
1776 // elimination to emitLineTableForUnit() we can get rid of all of them.
1777 if (InsertPoint != Rows.end() && InsertPoint->Address == Front &&
1778 InsertPoint->EndSequence) {
1779 *InsertPoint = Seq.front();
1780 Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
1781 } else {
1782 Rows.insert(InsertPoint, Seq.begin(), Seq.end());
1785 Seq.clear();
1788 static void patchStmtList(DIE &Die, DIEInteger Offset) {
1789 for (auto &V : Die.values())
1790 if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
1791 V = DIEValue(V.getAttribute(), V.getForm(), Offset);
1792 return;
1795 llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
1798 /// Extract the line table for \p Unit from \p OrigDwarf, and
1799 /// recreate a relocated version of these for the address ranges that
1800 /// are present in the binary.
1801 void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
1802 DWARFContext &OrigDwarf,
1803 RangesTy &Ranges,
1804 const DebugMapObject &DMO) {
1805 DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
1806 auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
1807 if (!StmtList)
1808 return;
1810 // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
1811 if (auto *OutputDIE = Unit.getOutputUnitDIE())
1812 patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize()));
1814 // Parse the original line info for the unit.
1815 DWARFDebugLine::LineTable LineTable;
1816 uint64_t StmtOffset = *StmtList;
1817 DWARFDataExtractor LineExtractor(
1818 OrigDwarf.getDWARFObj(), OrigDwarf.getDWARFObj().getLineSection(),
1819 OrigDwarf.isLittleEndian(), Unit.getOrigUnit().getAddressByteSize());
1820 if (Options.Translator)
1821 return Streamer->translateLineTable(LineExtractor, StmtOffset);
1823 Error Err = LineTable.parse(LineExtractor, &StmtOffset, OrigDwarf,
1824 &Unit.getOrigUnit(), DWARFContext::dumpWarning);
1825 DWARFContext::dumpWarning(std::move(Err));
1827 // This vector is the output line table.
1828 std::vector<DWARFDebugLine::Row> NewRows;
1829 NewRows.reserve(LineTable.Rows.size());
1831 // Current sequence of rows being extracted, before being inserted
1832 // in NewRows.
1833 std::vector<DWARFDebugLine::Row> Seq;
1834 const auto &FunctionRanges = Unit.getFunctionRanges();
1835 auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
1837 // FIXME: This logic is meant to generate exactly the same output as
1838 // Darwin's classic dsymutil. There is a nicer way to implement this
1839 // by simply putting all the relocated line info in NewRows and simply
1840 // sorting NewRows before passing it to emitLineTableForUnit. This
1841 // should be correct as sequences for a function should stay
1842 // together in the sorted output. There are a few corner cases that
1843 // look suspicious though, and that required to implement the logic
1844 // this way. Revisit that once initial validation is finished.
1846 // Iterate over the object file line info and extract the sequences
1847 // that correspond to linked functions.
1848 for (auto &Row : LineTable.Rows) {
1849 // Check whether we stepped out of the range. The range is
1850 // half-open, but consider accept the end address of the range if
1851 // it is marked as end_sequence in the input (because in that
1852 // case, the relocation offset is accurate and that entry won't
1853 // serve as the start of another function).
1854 if (CurrRange == InvalidRange || Row.Address.Address < CurrRange.start() ||
1855 Row.Address.Address > CurrRange.stop() ||
1856 (Row.Address.Address == CurrRange.stop() && !Row.EndSequence)) {
1857 // We just stepped out of a known range. Insert a end_sequence
1858 // corresponding to the end of the range.
1859 uint64_t StopAddress = CurrRange != InvalidRange
1860 ? CurrRange.stop() + CurrRange.value()
1861 : -1ULL;
1862 CurrRange = FunctionRanges.find(Row.Address.Address);
1863 bool CurrRangeValid =
1864 CurrRange != InvalidRange && CurrRange.start() <= Row.Address.Address;
1865 if (!CurrRangeValid) {
1866 CurrRange = InvalidRange;
1867 if (StopAddress != -1ULL) {
1868 // Try harder by looking in the DebugMapObject function
1869 // ranges map. There are corner cases where this finds a
1870 // valid entry. It's unclear if this is right or wrong, but
1871 // for now do as dsymutil.
1872 // FIXME: Understand exactly what cases this addresses and
1873 // potentially remove it along with the Ranges map.
1874 auto Range = Ranges.lower_bound(Row.Address.Address);
1875 if (Range != Ranges.begin() && Range != Ranges.end())
1876 --Range;
1878 if (Range != Ranges.end() && Range->first <= Row.Address.Address &&
1879 Range->second.HighPC >= Row.Address.Address) {
1880 StopAddress = Row.Address.Address + Range->second.Offset;
1884 if (StopAddress != -1ULL && !Seq.empty()) {
1885 // Insert end sequence row with the computed end address, but
1886 // the same line as the previous one.
1887 auto NextLine = Seq.back();
1888 NextLine.Address.Address = StopAddress;
1889 NextLine.EndSequence = 1;
1890 NextLine.PrologueEnd = 0;
1891 NextLine.BasicBlock = 0;
1892 NextLine.EpilogueBegin = 0;
1893 Seq.push_back(NextLine);
1894 insertLineSequence(Seq, NewRows);
1897 if (!CurrRangeValid)
1898 continue;
1901 // Ignore empty sequences.
1902 if (Row.EndSequence && Seq.empty())
1903 continue;
1905 // Relocate row address and add it to the current sequence.
1906 Row.Address.Address += CurrRange.value();
1907 Seq.emplace_back(Row);
1909 if (Row.EndSequence)
1910 insertLineSequence(Seq, NewRows);
1913 // Finished extracting, now emit the line tables.
1914 // FIXME: LLVM hard-codes its prologue values. We just copy the
1915 // prologue over and that works because we act as both producer and
1916 // consumer. It would be nicer to have a real configurable line
1917 // table emitter.
1918 if (LineTable.Prologue.getVersion() < 2 ||
1919 LineTable.Prologue.getVersion() > 5 ||
1920 LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
1921 LineTable.Prologue.OpcodeBase > 13)
1922 reportWarning("line table parameters mismatch. Cannot emit.", DMO);
1923 else {
1924 uint32_t PrologueEnd = *StmtList + 10 + LineTable.Prologue.PrologueLength;
1925 // DWARF v5 has an extra 2 bytes of information before the header_length
1926 // field.
1927 if (LineTable.Prologue.getVersion() == 5)
1928 PrologueEnd += 2;
1929 StringRef LineData = OrigDwarf.getDWARFObj().getLineSection().Data;
1930 MCDwarfLineTableParams Params;
1931 Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
1932 Params.DWARF2LineBase = LineTable.Prologue.LineBase;
1933 Params.DWARF2LineRange = LineTable.Prologue.LineRange;
1934 Streamer->emitLineTableForUnit(Params,
1935 LineData.slice(*StmtList + 4, PrologueEnd),
1936 LineTable.Prologue.MinInstLength, NewRows,
1937 Unit.getOrigUnit().getAddressByteSize());
1941 void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
1942 switch (Options.TheAccelTableKind) {
1943 case AccelTableKind::Apple:
1944 emitAppleAcceleratorEntriesForUnit(Unit);
1945 break;
1946 case AccelTableKind::Dwarf:
1947 emitDwarfAcceleratorEntriesForUnit(Unit);
1948 break;
1949 case AccelTableKind::Default:
1950 llvm_unreachable("The default must be updated to a concrete value.");
1951 break;
1955 void DwarfLinker::emitAppleAcceleratorEntriesForUnit(CompileUnit &Unit) {
1956 // Add namespaces.
1957 for (const auto &Namespace : Unit.getNamespaces())
1958 AppleNamespaces.addName(Namespace.Name,
1959 Namespace.Die->getOffset() + Unit.getStartOffset());
1961 /// Add names.
1962 if (!Options.Minimize)
1963 Streamer->emitPubNamesForUnit(Unit);
1964 for (const auto &Pubname : Unit.getPubnames())
1965 AppleNames.addName(Pubname.Name,
1966 Pubname.Die->getOffset() + Unit.getStartOffset());
1968 /// Add types.
1969 if (!Options.Minimize)
1970 Streamer->emitPubTypesForUnit(Unit);
1971 for (const auto &Pubtype : Unit.getPubtypes())
1972 AppleTypes.addName(
1973 Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(),
1974 Pubtype.Die->getTag(),
1975 Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
1976 : 0,
1977 Pubtype.QualifiedNameHash);
1979 /// Add ObjC names.
1980 for (const auto &ObjC : Unit.getObjC())
1981 AppleObjc.addName(ObjC.Name, ObjC.Die->getOffset() + Unit.getStartOffset());
1984 void DwarfLinker::emitDwarfAcceleratorEntriesForUnit(CompileUnit &Unit) {
1985 for (const auto &Namespace : Unit.getNamespaces())
1986 DebugNames.addName(Namespace.Name, Namespace.Die->getOffset(),
1987 Namespace.Die->getTag(), Unit.getUniqueID());
1988 for (const auto &Pubname : Unit.getPubnames())
1989 DebugNames.addName(Pubname.Name, Pubname.Die->getOffset(),
1990 Pubname.Die->getTag(), Unit.getUniqueID());
1991 for (const auto &Pubtype : Unit.getPubtypes())
1992 DebugNames.addName(Pubtype.Name, Pubtype.Die->getOffset(),
1993 Pubtype.Die->getTag(), Unit.getUniqueID());
1996 /// Read the frame info stored in the object, and emit the
1997 /// patched frame descriptions for the linked binary.
1999 /// This is actually pretty easy as the data of the CIEs and FDEs can
2000 /// be considered as black boxes and moved as is. The only thing to do
2001 /// is to patch the addresses in the headers.
2002 void DwarfLinker::patchFrameInfoForObject(const DebugMapObject &DMO,
2003 RangesTy &Ranges,
2004 DWARFContext &OrigDwarf,
2005 unsigned AddrSize) {
2006 StringRef FrameData = OrigDwarf.getDWARFObj().getFrameSection().Data;
2007 if (FrameData.empty())
2008 return;
2010 DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
2011 uint64_t InputOffset = 0;
2013 // Store the data of the CIEs defined in this object, keyed by their
2014 // offsets.
2015 DenseMap<uint64_t, StringRef> LocalCIES;
2017 while (Data.isValidOffset(InputOffset)) {
2018 uint64_t EntryOffset = InputOffset;
2019 uint32_t InitialLength = Data.getU32(&InputOffset);
2020 if (InitialLength == 0xFFFFFFFF)
2021 return reportWarning("Dwarf64 bits no supported", DMO);
2023 uint32_t CIEId = Data.getU32(&InputOffset);
2024 if (CIEId == 0xFFFFFFFF) {
2025 // This is a CIE, store it.
2026 StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
2027 LocalCIES[EntryOffset] = CIEData;
2028 // The -4 is to account for the CIEId we just read.
2029 InputOffset += InitialLength - 4;
2030 continue;
2033 uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
2035 // Some compilers seem to emit frame info that doesn't start at
2036 // the function entry point, thus we can't just lookup the address
2037 // in the debug map. Use the linker's range map to see if the FDE
2038 // describes something that we can relocate.
2039 auto Range = Ranges.upper_bound(Loc);
2040 if (Range != Ranges.begin())
2041 --Range;
2042 if (Range == Ranges.end() || Range->first > Loc ||
2043 Range->second.HighPC <= Loc) {
2044 // The +4 is to account for the size of the InitialLength field itself.
2045 InputOffset = EntryOffset + InitialLength + 4;
2046 continue;
2049 // This is an FDE, and we have a mapping.
2050 // Have we already emitted a corresponding CIE?
2051 StringRef CIEData = LocalCIES[CIEId];
2052 if (CIEData.empty())
2053 return reportWarning("Inconsistent debug_frame content. Dropping.", DMO);
2055 // Look if we already emitted a CIE that corresponds to the
2056 // referenced one (the CIE data is the key of that lookup).
2057 auto IteratorInserted = EmittedCIEs.insert(
2058 std::make_pair(CIEData, Streamer->getFrameSectionSize()));
2059 // If there is no CIE yet for this ID, emit it.
2060 if (IteratorInserted.second ||
2061 // FIXME: dsymutil-classic only caches the last used CIE for
2062 // reuse. Mimic that behavior for now. Just removing that
2063 // second half of the condition and the LastCIEOffset variable
2064 // makes the code DTRT.
2065 LastCIEOffset != IteratorInserted.first->getValue()) {
2066 LastCIEOffset = Streamer->getFrameSectionSize();
2067 IteratorInserted.first->getValue() = LastCIEOffset;
2068 Streamer->emitCIE(CIEData);
2071 // Emit the FDE with updated address and CIE pointer.
2072 // (4 + AddrSize) is the size of the CIEId + initial_location
2073 // fields that will get reconstructed by emitFDE().
2074 unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
2075 Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize,
2076 Loc + Range->second.Offset,
2077 FrameData.substr(InputOffset, FDERemainingBytes));
2078 InputOffset += FDERemainingBytes;
2082 void DwarfLinker::DIECloner::copyAbbrev(
2083 const DWARFAbbreviationDeclaration &Abbrev, bool hasODR) {
2084 DIEAbbrev Copy(dwarf::Tag(Abbrev.getTag()),
2085 dwarf::Form(Abbrev.hasChildren()));
2087 for (const auto &Attr : Abbrev.attributes()) {
2088 uint16_t Form = Attr.Form;
2089 if (hasODR && isODRAttribute(Attr.Attr))
2090 Form = dwarf::DW_FORM_ref_addr;
2091 Copy.AddAttribute(dwarf::Attribute(Attr.Attr), dwarf::Form(Form));
2094 Linker.AssignAbbrev(Copy);
2097 uint32_t
2098 DwarfLinker::DIECloner::hashFullyQualifiedName(DWARFDie DIE, CompileUnit &U,
2099 const DebugMapObject &DMO,
2100 int ChildRecurseDepth) {
2101 const char *Name = nullptr;
2102 DWARFUnit *OrigUnit = &U.getOrigUnit();
2103 CompileUnit *CU = &U;
2104 Optional<DWARFFormValue> Ref;
2106 while (1) {
2107 if (const char *CurrentName = DIE.getName(DINameKind::ShortName))
2108 Name = CurrentName;
2110 if (!(Ref = DIE.find(dwarf::DW_AT_specification)) &&
2111 !(Ref = DIE.find(dwarf::DW_AT_abstract_origin)))
2112 break;
2114 if (!Ref->isFormClass(DWARFFormValue::FC_Reference))
2115 break;
2117 CompileUnit *RefCU;
2118 if (auto RefDIE =
2119 resolveDIEReference(Linker, DMO, CompileUnits, *Ref, DIE, RefCU)) {
2120 CU = RefCU;
2121 OrigUnit = &RefCU->getOrigUnit();
2122 DIE = RefDIE;
2126 unsigned Idx = OrigUnit->getDIEIndex(DIE);
2127 if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace)
2128 Name = "(anonymous namespace)";
2130 if (CU->getInfo(Idx).ParentIdx == 0 ||
2131 // FIXME: dsymutil-classic compatibility. Ignore modules.
2132 CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() ==
2133 dwarf::DW_TAG_module)
2134 return djbHash(Name ? Name : "", djbHash(ChildRecurseDepth ? "" : "::"));
2136 DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx);
2137 return djbHash(
2138 (Name ? Name : ""),
2139 djbHash((Name ? "::" : ""),
2140 hashFullyQualifiedName(Die, *CU, DMO, ++ChildRecurseDepth)));
2143 static uint64_t getDwoId(const DWARFDie &CUDie, const DWARFUnit &Unit) {
2144 auto DwoId = dwarf::toUnsigned(
2145 CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
2146 if (DwoId)
2147 return *DwoId;
2148 return 0;
2151 bool DwarfLinker::registerModuleReference(
2152 DWARFDie CUDie, const DWARFUnit &Unit, DebugMap &ModuleMap,
2153 const DebugMapObject &DMO, RangesTy &Ranges, OffsetsStringPool &StringPool,
2154 UniquingStringPool &UniquingStringPool, DeclContextTree &ODRContexts,
2155 uint64_t ModulesEndOffset, unsigned &UnitID, bool IsLittleEndian,
2156 unsigned Indent, bool Quiet) {
2157 std::string PCMfile = dwarf::toString(
2158 CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
2159 if (PCMfile.empty())
2160 return false;
2162 // Clang module DWARF skeleton CUs abuse this for the path to the module.
2163 uint64_t DwoId = getDwoId(CUDie, Unit);
2165 std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
2166 if (Name.empty()) {
2167 if (!Quiet)
2168 reportWarning("Anonymous module skeleton CU for " + PCMfile, DMO);
2169 return true;
2172 if (!Quiet && Options.Verbose) {
2173 outs().indent(Indent);
2174 outs() << "Found clang module reference " << PCMfile;
2177 auto Cached = ClangModules.find(PCMfile);
2178 if (Cached != ClangModules.end()) {
2179 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2180 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2181 // ASTFileSignatures will change randomly when a module is rebuilt.
2182 if (!Quiet && Options.Verbose && (Cached->second != DwoId))
2183 reportWarning(Twine("hash mismatch: this object file was built against a "
2184 "different version of the module ") +
2185 PCMfile,
2186 DMO);
2187 if (!Quiet && Options.Verbose)
2188 outs() << " [cached].\n";
2189 return true;
2191 if (!Quiet && Options.Verbose)
2192 outs() << " ...\n";
2194 // Cyclic dependencies are disallowed by Clang, but we still
2195 // shouldn't run into an infinite loop, so mark it as processed now.
2196 ClangModules.insert({PCMfile, DwoId});
2198 if (Error E = loadClangModule(CUDie, PCMfile, Name, DwoId, ModuleMap, DMO,
2199 Ranges, StringPool, UniquingStringPool,
2200 ODRContexts, ModulesEndOffset, UnitID,
2201 IsLittleEndian, Indent + 2, Quiet)) {
2202 consumeError(std::move(E));
2203 return false;
2205 return true;
2208 ErrorOr<const object::ObjectFile &>
2209 DwarfLinker::loadObject(const DebugMapObject &Obj, const DebugMap &Map) {
2210 auto ObjectEntry =
2211 BinHolder.getObjectEntry(Obj.getObjectFilename(), Obj.getTimestamp());
2212 if (!ObjectEntry) {
2213 auto Err = ObjectEntry.takeError();
2214 reportWarning(
2215 Twine(Obj.getObjectFilename()) + ": " + toString(std::move(Err)), Obj);
2216 return errorToErrorCode(std::move(Err));
2219 auto Object = ObjectEntry->getObject(Map.getTriple());
2220 if (!Object) {
2221 auto Err = Object.takeError();
2222 reportWarning(
2223 Twine(Obj.getObjectFilename()) + ": " + toString(std::move(Err)), Obj);
2224 return errorToErrorCode(std::move(Err));
2227 return *Object;
2230 Error DwarfLinker::loadClangModule(
2231 DWARFDie CUDie, StringRef Filename, StringRef ModuleName, uint64_t DwoId,
2232 DebugMap &ModuleMap, const DebugMapObject &DMO, RangesTy &Ranges,
2233 OffsetsStringPool &StringPool, UniquingStringPool &UniquingStringPool,
2234 DeclContextTree &ODRContexts, uint64_t ModulesEndOffset, unsigned &UnitID,
2235 bool IsLittleEndian, unsigned Indent, bool Quiet) {
2236 /// Using a SmallString<0> because loadClangModule() is recursive.
2237 SmallString<0> Path(Options.PrependPath);
2238 if (sys::path::is_relative(Filename))
2239 resolveRelativeObjectPath(Path, CUDie);
2240 sys::path::append(Path, Filename);
2241 // Don't use the cached binary holder because we have no thread-safety
2242 // guarantee and the lifetime is limited.
2243 auto &Obj = ModuleMap.addDebugMapObject(
2244 Path, sys::TimePoint<std::chrono::seconds>(), MachO::N_OSO);
2245 auto ErrOrObj = loadObject(Obj, ModuleMap);
2246 if (!ErrOrObj) {
2247 // Try and emit more helpful warnings by applying some heuristics.
2248 StringRef ObjFile = DMO.getObjectFilename();
2249 bool isClangModule = sys::path::extension(Filename).equals(".pcm");
2250 bool isArchive = ObjFile.endswith(")");
2251 if (isClangModule) {
2252 StringRef ModuleCacheDir = sys::path::parent_path(Path);
2253 if (sys::fs::exists(ModuleCacheDir)) {
2254 // If the module's parent directory exists, we assume that the module
2255 // cache has expired and was pruned by clang. A more adventurous
2256 // dsymutil would invoke clang to rebuild the module now.
2257 if (!ModuleCacheHintDisplayed) {
2258 WithColor::note() << "The clang module cache may have expired since "
2259 "this object file was built. Rebuilding the "
2260 "object file will rebuild the module cache.\n";
2261 ModuleCacheHintDisplayed = true;
2263 } else if (isArchive) {
2264 // If the module cache directory doesn't exist at all and the object
2265 // file is inside a static library, we assume that the static library
2266 // was built on a different machine. We don't want to discourage module
2267 // debugging for convenience libraries within a project though.
2268 if (!ArchiveHintDisplayed) {
2269 WithColor::note()
2270 << "Linking a static library that was built with "
2271 "-gmodules, but the module cache was not found. "
2272 "Redistributable static libraries should never be "
2273 "built with module debugging enabled. The debug "
2274 "experience will be degraded due to incomplete "
2275 "debug information.\n";
2276 ArchiveHintDisplayed = true;
2280 return Error::success();
2283 std::unique_ptr<CompileUnit> Unit;
2285 // Setup access to the debug info.
2286 auto DwarfContext = DWARFContext::create(*ErrOrObj);
2287 RelocationManager RelocMgr(*this);
2289 for (const auto &CU : DwarfContext->compile_units()) {
2290 updateDwarfVersion(CU->getVersion());
2291 // Recursively get all modules imported by this one.
2292 auto CUDie = CU->getUnitDIE(false);
2293 if (!CUDie)
2294 continue;
2295 if (!registerModuleReference(CUDie, *CU, ModuleMap, DMO, Ranges, StringPool,
2296 UniquingStringPool, ODRContexts,
2297 ModulesEndOffset, UnitID, IsLittleEndian,
2298 Indent, Quiet)) {
2299 if (Unit) {
2300 std::string Err =
2301 (Filename +
2302 ": Clang modules are expected to have exactly 1 compile unit.\n")
2303 .str();
2304 error(Err);
2305 return make_error<StringError>(Err, inconvertibleErrorCode());
2307 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2308 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2309 // ASTFileSignatures will change randomly when a module is rebuilt.
2310 uint64_t PCMDwoId = getDwoId(CUDie, *CU);
2311 if (PCMDwoId != DwoId) {
2312 if (!Quiet && Options.Verbose)
2313 reportWarning(
2314 Twine("hash mismatch: this object file was built against a "
2315 "different version of the module ") +
2316 Filename,
2317 DMO);
2318 // Update the cache entry with the DwoId of the module loaded from disk.
2319 ClangModules[Filename] = PCMDwoId;
2322 // Add this module.
2323 Unit = llvm::make_unique<CompileUnit>(*CU, UnitID++, !Options.NoODR,
2324 ModuleName);
2325 Unit->setHasInterestingContent();
2326 analyzeContextInfo(CUDie, 0, *Unit, &ODRContexts.getRoot(),
2327 UniquingStringPool, ODRContexts, ModulesEndOffset,
2328 ParseableSwiftInterfaces,
2329 [&](const Twine &Warning, const DWARFDie &DIE) {
2330 reportWarning(Warning, DMO, &DIE);
2332 // Keep everything.
2333 Unit->markEverythingAsKept();
2336 if (!Unit->getOrigUnit().getUnitDIE().hasChildren())
2337 return Error::success();
2338 if (!Quiet && Options.Verbose) {
2339 outs().indent(Indent);
2340 outs() << "cloning .debug_info from " << Filename << "\n";
2343 UnitListTy CompileUnits;
2344 CompileUnits.push_back(std::move(Unit));
2345 DIECloner(*this, RelocMgr, DIEAlloc, CompileUnits, Options)
2346 .cloneAllCompileUnits(*DwarfContext, DMO, Ranges, StringPool,
2347 IsLittleEndian);
2348 return Error::success();
2351 void DwarfLinker::DIECloner::cloneAllCompileUnits(
2352 DWARFContext &DwarfContext, const DebugMapObject &DMO, RangesTy &Ranges,
2353 OffsetsStringPool &StringPool, bool IsLittleEndian) {
2354 if (!Linker.Streamer)
2355 return;
2357 for (auto &CurrentUnit : CompileUnits) {
2358 auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
2359 CurrentUnit->setStartOffset(Linker.OutputDebugInfoSize);
2360 if (!InputDIE) {
2361 Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
2362 continue;
2364 if (CurrentUnit->getInfo(0).Keep) {
2365 // Clone the InputDIE into your Unit DIE in our compile unit since it
2366 // already has a DIE inside of it.
2367 CurrentUnit->createOutputDIE();
2368 cloneDIE(InputDIE, DMO, *CurrentUnit, StringPool, 0 /* PC offset */,
2369 11 /* Unit Header size */, 0, IsLittleEndian,
2370 CurrentUnit->getOutputUnitDIE());
2373 Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
2375 if (Linker.Options.NoOutput)
2376 continue;
2378 // FIXME: for compatibility with the classic dsymutil, we emit
2379 // an empty line table for the unit, even if the unit doesn't
2380 // actually exist in the DIE tree.
2381 if (LLVM_LIKELY(!Linker.Options.Update) || Linker.Options.Translator)
2382 Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext, Ranges, DMO);
2384 Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
2386 if (LLVM_UNLIKELY(Linker.Options.Update))
2387 continue;
2389 Linker.patchRangesForUnit(*CurrentUnit, DwarfContext, DMO);
2390 auto ProcessExpr = [&](StringRef Bytes, SmallVectorImpl<uint8_t> &Buffer) {
2391 DWARFUnit &OrigUnit = CurrentUnit->getOrigUnit();
2392 DataExtractor Data(Bytes, IsLittleEndian, OrigUnit.getAddressByteSize());
2393 cloneExpression(Data,
2394 DWARFExpression(Data, OrigUnit.getVersion(),
2395 OrigUnit.getAddressByteSize()),
2396 DMO, *CurrentUnit, Buffer);
2398 Linker.Streamer->emitLocationsForUnit(*CurrentUnit, DwarfContext,
2399 ProcessExpr);
2402 if (Linker.Options.NoOutput)
2403 return;
2405 // Emit all the compile unit's debug information.
2406 for (auto &CurrentUnit : CompileUnits) {
2407 if (LLVM_LIKELY(!Linker.Options.Update))
2408 Linker.generateUnitRanges(*CurrentUnit);
2410 CurrentUnit->fixupForwardReferences();
2412 if (!CurrentUnit->getOutputUnitDIE())
2413 continue;
2415 Linker.Streamer->emitCompileUnitHeader(*CurrentUnit);
2416 Linker.Streamer->emitDIE(*CurrentUnit->getOutputUnitDIE());
2420 void DwarfLinker::updateAccelKind(DWARFContext &Dwarf) {
2421 if (Options.TheAccelTableKind != AccelTableKind::Default)
2422 return;
2424 auto &DwarfObj = Dwarf.getDWARFObj();
2426 if (!AtLeastOneDwarfAccelTable &&
2427 (!DwarfObj.getAppleNamesSection().Data.empty() ||
2428 !DwarfObj.getAppleTypesSection().Data.empty() ||
2429 !DwarfObj.getAppleNamespacesSection().Data.empty() ||
2430 !DwarfObj.getAppleObjCSection().Data.empty())) {
2431 AtLeastOneAppleAccelTable = true;
2434 if (!AtLeastOneDwarfAccelTable &&
2435 !DwarfObj.getNamesSection().Data.empty()) {
2436 AtLeastOneDwarfAccelTable = true;
2440 bool DwarfLinker::emitPaperTrailWarnings(const DebugMapObject &DMO,
2441 const DebugMap &Map,
2442 OffsetsStringPool &StringPool) {
2443 if (DMO.getWarnings().empty() || !DMO.empty())
2444 return false;
2446 Streamer->switchToDebugInfoSection(/* Version */ 2);
2447 DIE *CUDie = DIE::get(DIEAlloc, dwarf::DW_TAG_compile_unit);
2448 CUDie->setOffset(11);
2449 StringRef Producer = StringPool.internString("dsymutil");
2450 StringRef File = StringPool.internString(DMO.getObjectFilename());
2451 CUDie->addValue(DIEAlloc, dwarf::DW_AT_producer, dwarf::DW_FORM_strp,
2452 DIEInteger(StringPool.getStringOffset(Producer)));
2453 DIEBlock *String = new (DIEAlloc) DIEBlock();
2454 DIEBlocks.push_back(String);
2455 for (auto &C : File)
2456 String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
2457 DIEInteger(C));
2458 String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
2459 DIEInteger(0));
2461 CUDie->addValue(DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_string, String);
2462 for (const auto &Warning : DMO.getWarnings()) {
2463 DIE &ConstDie = CUDie->addChild(DIE::get(DIEAlloc, dwarf::DW_TAG_constant));
2464 ConstDie.addValue(
2465 DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp,
2466 DIEInteger(StringPool.getStringOffset("dsymutil_warning")));
2467 ConstDie.addValue(DIEAlloc, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag,
2468 DIEInteger(1));
2469 ConstDie.addValue(DIEAlloc, dwarf::DW_AT_const_value, dwarf::DW_FORM_strp,
2470 DIEInteger(StringPool.getStringOffset(Warning)));
2472 unsigned Size = 4 /* FORM_strp */ + File.size() + 1 +
2473 DMO.getWarnings().size() * (4 + 1 + 4) +
2474 1 /* End of children */;
2475 DIEAbbrev Abbrev = CUDie->generateAbbrev();
2476 AssignAbbrev(Abbrev);
2477 CUDie->setAbbrevNumber(Abbrev.getNumber());
2478 Size += getULEB128Size(Abbrev.getNumber());
2479 // Abbreviation ordering needed for classic compatibility.
2480 for (auto &Child : CUDie->children()) {
2481 Abbrev = Child.generateAbbrev();
2482 AssignAbbrev(Abbrev);
2483 Child.setAbbrevNumber(Abbrev.getNumber());
2484 Size += getULEB128Size(Abbrev.getNumber());
2486 CUDie->setSize(Size);
2487 auto &Asm = Streamer->getAsmPrinter();
2488 Asm.emitInt32(11 + CUDie->getSize() - 4);
2489 Asm.emitInt16(2);
2490 Asm.emitInt32(0);
2491 Asm.emitInt8(Map.getTriple().isArch64Bit() ? 8 : 4);
2492 Streamer->emitDIE(*CUDie);
2493 OutputDebugInfoSize += 11 /* Header */ + Size;
2495 return true;
2498 static Error copySwiftInterfaces(
2499 const std::map<std::string, std::string> &ParseableSwiftInterfaces,
2500 StringRef Architecture, const LinkOptions &Options) {
2501 std::error_code EC;
2502 SmallString<128> InputPath;
2503 SmallString<128> Path;
2504 sys::path::append(Path, *Options.ResourceDir, "Swift", Architecture);
2505 if ((EC = sys::fs::create_directories(Path.str(), true,
2506 sys::fs::perms::all_all)))
2507 return make_error<StringError>(
2508 "cannot create directory: " + toString(errorCodeToError(EC)), EC);
2509 unsigned BaseLength = Path.size();
2511 for (auto &I : ParseableSwiftInterfaces) {
2512 StringRef ModuleName = I.first;
2513 StringRef InterfaceFile = I.second;
2514 if (!Options.PrependPath.empty()) {
2515 InputPath.clear();
2516 sys::path::append(InputPath, Options.PrependPath, InterfaceFile);
2517 InterfaceFile = InputPath;
2519 sys::path::append(Path, ModuleName);
2520 Path.append(".swiftinterface");
2521 if (Options.Verbose)
2522 outs() << "copy parseable Swift interface " << InterfaceFile << " -> "
2523 << Path.str() << '\n';
2525 // copy_file attempts an APFS clone first, so this should be cheap.
2526 if ((EC = sys::fs::copy_file(InterfaceFile, Path.str())))
2527 warn(Twine("cannot copy parseable Swift interface ") +
2528 InterfaceFile + ": " +
2529 toString(errorCodeToError(EC)));
2530 Path.resize(BaseLength);
2532 return Error::success();
2535 bool DwarfLinker::link(const DebugMap &Map) {
2536 if (!createStreamer(Map.getTriple(), OutFile))
2537 return false;
2539 // Size of the DIEs (and headers) generated for the linked output.
2540 OutputDebugInfoSize = 0;
2541 // A unique ID that identifies each compile unit.
2542 unsigned UnitID = 0;
2543 DebugMap ModuleMap(Map.getTriple(), Map.getBinaryPath());
2545 // First populate the data structure we need for each iteration of the
2546 // parallel loop.
2547 unsigned NumObjects = Map.getNumberOfObjects();
2548 std::vector<LinkContext> ObjectContexts;
2549 ObjectContexts.reserve(NumObjects);
2550 for (const auto &Obj : Map.objects()) {
2551 ObjectContexts.emplace_back(Map, *this, *Obj.get());
2552 LinkContext &LC = ObjectContexts.back();
2553 if (LC.ObjectFile)
2554 updateAccelKind(*LC.DwarfContext);
2557 // This Dwarf string pool which is only used for uniquing. This one should
2558 // never be used for offsets as its not thread-safe or predictable.
2559 UniquingStringPool UniquingStringPool;
2561 // This Dwarf string pool which is used for emission. It must be used
2562 // serially as the order of calling getStringOffset matters for
2563 // reproducibility.
2564 OffsetsStringPool OffsetsStringPool(Options.Translator);
2566 // ODR Contexts for the link.
2567 DeclContextTree ODRContexts;
2569 // If we haven't decided on an accelerator table kind yet, we base ourselves
2570 // on the DWARF we have seen so far. At this point we haven't pulled in debug
2571 // information from modules yet, so it is technically possible that they
2572 // would affect the decision. However, as they're built with the same
2573 // compiler and flags, it is safe to assume that they will follow the
2574 // decision made here.
2575 if (Options.TheAccelTableKind == AccelTableKind::Default) {
2576 if (AtLeastOneDwarfAccelTable && !AtLeastOneAppleAccelTable)
2577 Options.TheAccelTableKind = AccelTableKind::Dwarf;
2578 else
2579 Options.TheAccelTableKind = AccelTableKind::Apple;
2582 for (LinkContext &LinkContext : ObjectContexts) {
2583 if (Options.Verbose)
2584 outs() << "DEBUG MAP OBJECT: " << LinkContext.DMO.getObjectFilename()
2585 << "\n";
2587 // N_AST objects (swiftmodule files) should get dumped directly into the
2588 // appropriate DWARF section.
2589 if (LinkContext.DMO.getType() == MachO::N_AST) {
2590 StringRef File = LinkContext.DMO.getObjectFilename();
2591 auto ErrorOrMem = MemoryBuffer::getFile(File);
2592 if (!ErrorOrMem) {
2593 warn("Could not open '" + File + "'\n");
2594 continue;
2596 sys::fs::file_status Stat;
2597 if (auto Err = sys::fs::status(File, Stat)) {
2598 warn(Err.message());
2599 continue;
2601 if (!Options.NoTimestamp) {
2602 // The modification can have sub-second precision so we need to cast
2603 // away the extra precision that's not present in the debug map.
2604 auto ModificationTime =
2605 std::chrono::time_point_cast<std::chrono::seconds>(
2606 Stat.getLastModificationTime());
2607 if (ModificationTime != LinkContext.DMO.getTimestamp()) {
2608 // Not using the helper here as we can easily stream TimePoint<>.
2609 WithColor::warning()
2610 << "Timestamp mismatch for " << File << ": "
2611 << Stat.getLastModificationTime() << " and "
2612 << sys::TimePoint<>(LinkContext.DMO.getTimestamp()) << "\n";
2613 continue;
2617 // Copy the module into the .swift_ast section.
2618 if (!Options.NoOutput)
2619 Streamer->emitSwiftAST((*ErrorOrMem)->getBuffer());
2620 continue;
2623 if (emitPaperTrailWarnings(LinkContext.DMO, Map, OffsetsStringPool))
2624 continue;
2626 if (!LinkContext.ObjectFile)
2627 continue;
2629 // Look for relocations that correspond to debug map entries.
2631 if (LLVM_LIKELY(!Options.Update) &&
2632 !LinkContext.RelocMgr.findValidRelocsInDebugInfo(
2633 *LinkContext.ObjectFile, LinkContext.DMO)) {
2634 if (Options.Verbose)
2635 outs() << "No valid relocations found. Skipping.\n";
2637 // Clear this ObjFile entry as a signal to other loops that we should not
2638 // process this iteration.
2639 LinkContext.ObjectFile = nullptr;
2640 continue;
2643 // Setup access to the debug info.
2644 if (!LinkContext.DwarfContext)
2645 continue;
2647 startDebugObject(LinkContext);
2649 // In a first phase, just read in the debug info and load all clang modules.
2650 LinkContext.CompileUnits.reserve(
2651 LinkContext.DwarfContext->getNumCompileUnits());
2653 for (const auto &CU : LinkContext.DwarfContext->compile_units()) {
2654 updateDwarfVersion(CU->getVersion());
2655 auto CUDie = CU->getUnitDIE(false);
2656 if (Options.Verbose) {
2657 outs() << "Input compilation unit:";
2658 DIDumpOptions DumpOpts;
2659 DumpOpts.ChildRecurseDepth = 0;
2660 DumpOpts.Verbose = Options.Verbose;
2661 CUDie.dump(outs(), 0, DumpOpts);
2663 if (CUDie && !LLVM_UNLIKELY(Options.Update))
2664 registerModuleReference(CUDie, *CU, ModuleMap, LinkContext.DMO,
2665 LinkContext.Ranges, OffsetsStringPool,
2666 UniquingStringPool, ODRContexts, 0, UnitID,
2667 LinkContext.DwarfContext->isLittleEndian());
2671 // If we haven't seen any CUs, pick an arbitrary valid Dwarf version anyway.
2672 if (MaxDwarfVersion == 0)
2673 MaxDwarfVersion = 3;
2675 // At this point we know how much data we have emitted. We use this value to
2676 // compare canonical DIE offsets in analyzeContextInfo to see if a definition
2677 // is already emitted, without being affected by canonical die offsets set
2678 // later. This prevents undeterminism when analyze and clone execute
2679 // concurrently, as clone set the canonical DIE offset and analyze reads it.
2680 const uint64_t ModulesEndOffset = OutputDebugInfoSize;
2682 // These variables manage the list of processed object files.
2683 // The mutex and condition variable are to ensure that this is thread safe.
2684 std::mutex ProcessedFilesMutex;
2685 std::condition_variable ProcessedFilesConditionVariable;
2686 BitVector ProcessedFiles(NumObjects, false);
2688 // Analyzing the context info is particularly expensive so it is executed in
2689 // parallel with emitting the previous compile unit.
2690 auto AnalyzeLambda = [&](size_t i) {
2691 auto &LinkContext = ObjectContexts[i];
2693 if (!LinkContext.ObjectFile || !LinkContext.DwarfContext)
2694 return;
2696 for (const auto &CU : LinkContext.DwarfContext->compile_units()) {
2697 updateDwarfVersion(CU->getVersion());
2698 // The !registerModuleReference() condition effectively skips
2699 // over fully resolved skeleton units. This second pass of
2700 // registerModuleReferences doesn't do any new work, but it
2701 // will collect top-level errors, which are suppressed. Module
2702 // warnings were already displayed in the first iteration.
2703 bool Quiet = true;
2704 auto CUDie = CU->getUnitDIE(false);
2705 if (!CUDie || LLVM_UNLIKELY(Options.Update) ||
2706 !registerModuleReference(CUDie, *CU, ModuleMap, LinkContext.DMO,
2707 LinkContext.Ranges, OffsetsStringPool,
2708 UniquingStringPool, ODRContexts,
2709 ModulesEndOffset, UnitID, Quiet)) {
2710 LinkContext.CompileUnits.push_back(llvm::make_unique<CompileUnit>(
2711 *CU, UnitID++, !Options.NoODR && !Options.Update, ""));
2715 // Now build the DIE parent links that we will use during the next phase.
2716 for (auto &CurrentUnit : LinkContext.CompileUnits) {
2717 auto CUDie = CurrentUnit->getOrigUnit().getUnitDIE();
2718 if (!CUDie)
2719 continue;
2720 analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0,
2721 *CurrentUnit, &ODRContexts.getRoot(),
2722 UniquingStringPool, ODRContexts, ModulesEndOffset,
2723 ParseableSwiftInterfaces,
2724 [&](const Twine &Warning, const DWARFDie &DIE) {
2725 reportWarning(Warning, LinkContext.DMO, &DIE);
2730 // And then the remaining work in serial again.
2731 // Note, although this loop runs in serial, it can run in parallel with
2732 // the analyzeContextInfo loop so long as we process files with indices >=
2733 // than those processed by analyzeContextInfo.
2734 auto CloneLambda = [&](size_t i) {
2735 auto &LinkContext = ObjectContexts[i];
2736 if (!LinkContext.ObjectFile)
2737 return;
2739 // Then mark all the DIEs that need to be present in the linked output
2740 // and collect some information about them.
2741 // Note that this loop can not be merged with the previous one because
2742 // cross-cu references require the ParentIdx to be setup for every CU in
2743 // the object file before calling this.
2744 if (LLVM_UNLIKELY(Options.Update)) {
2745 for (auto &CurrentUnit : LinkContext.CompileUnits)
2746 CurrentUnit->markEverythingAsKept();
2747 Streamer->copyInvariantDebugSection(*LinkContext.ObjectFile);
2748 } else {
2749 for (auto &CurrentUnit : LinkContext.CompileUnits)
2750 lookForDIEsToKeep(LinkContext.RelocMgr, LinkContext.Ranges,
2751 LinkContext.CompileUnits,
2752 CurrentUnit->getOrigUnit().getUnitDIE(),
2753 LinkContext.DMO, *CurrentUnit, 0);
2756 // The calls to applyValidRelocs inside cloneDIE will walk the reloc
2757 // array again (in the same way findValidRelocsInDebugInfo() did). We
2758 // need to reset the NextValidReloc index to the beginning.
2759 LinkContext.RelocMgr.resetValidRelocs();
2760 if (LinkContext.RelocMgr.hasValidRelocs() || LLVM_UNLIKELY(Options.Update))
2761 DIECloner(*this, LinkContext.RelocMgr, DIEAlloc, LinkContext.CompileUnits,
2762 Options)
2763 .cloneAllCompileUnits(*LinkContext.DwarfContext, LinkContext.DMO,
2764 LinkContext.Ranges, OffsetsStringPool,
2765 LinkContext.DwarfContext->isLittleEndian());
2766 if (!Options.NoOutput && !LinkContext.CompileUnits.empty() &&
2767 LLVM_LIKELY(!Options.Update))
2768 patchFrameInfoForObject(
2769 LinkContext.DMO, LinkContext.Ranges, *LinkContext.DwarfContext,
2770 LinkContext.CompileUnits[0]->getOrigUnit().getAddressByteSize());
2772 // Clean-up before starting working on the next object.
2773 endDebugObject(LinkContext);
2776 auto EmitLambda = [&]() {
2777 // Emit everything that's global.
2778 if (!Options.NoOutput) {
2779 Streamer->emitAbbrevs(Abbreviations, MaxDwarfVersion);
2780 Streamer->emitStrings(OffsetsStringPool);
2781 switch (Options.TheAccelTableKind) {
2782 case AccelTableKind::Apple:
2783 Streamer->emitAppleNames(AppleNames);
2784 Streamer->emitAppleNamespaces(AppleNamespaces);
2785 Streamer->emitAppleTypes(AppleTypes);
2786 Streamer->emitAppleObjc(AppleObjc);
2787 break;
2788 case AccelTableKind::Dwarf:
2789 Streamer->emitDebugNames(DebugNames);
2790 break;
2791 case AccelTableKind::Default:
2792 llvm_unreachable("Default should have already been resolved.");
2793 break;
2798 auto AnalyzeAll = [&]() {
2799 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
2800 AnalyzeLambda(i);
2802 std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2803 ProcessedFiles.set(i);
2804 ProcessedFilesConditionVariable.notify_one();
2808 auto CloneAll = [&]() {
2809 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
2811 std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2812 if (!ProcessedFiles[i]) {
2813 ProcessedFilesConditionVariable.wait(
2814 LockGuard, [&]() { return ProcessedFiles[i]; });
2818 CloneLambda(i);
2820 EmitLambda();
2823 // To limit memory usage in the single threaded case, analyze and clone are
2824 // run sequentially so the LinkContext is freed after processing each object
2825 // in endDebugObject.
2826 if (Options.Threads == 1) {
2827 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
2828 AnalyzeLambda(i);
2829 CloneLambda(i);
2831 EmitLambda();
2832 } else {
2833 ThreadPool pool(2);
2834 pool.async(AnalyzeAll);
2835 pool.async(CloneAll);
2836 pool.wait();
2839 if (Options.NoOutput)
2840 return true;
2842 if (Options.ResourceDir && !ParseableSwiftInterfaces.empty()) {
2843 StringRef ArchName = Triple::getArchTypeName(Map.getTriple().getArch());
2844 if (auto E =
2845 copySwiftInterfaces(ParseableSwiftInterfaces, ArchName, Options))
2846 return error(toString(std::move(E)));
2849 return Streamer->finish(Map, Options.Translator);
2850 } // namespace dsymutil
2852 bool linkDwarf(raw_fd_ostream &OutFile, BinaryHolder &BinHolder,
2853 const DebugMap &DM, LinkOptions Options) {
2854 DwarfLinker Linker(OutFile, BinHolder, std::move(Options));
2855 return Linker.link(DM);
2858 } // namespace dsymutil
2859 } // namespace llvm