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