1 //=== DWARFLinker.cpp -----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DWARFLinker/DWARFLinker.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/BitVector.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/CodeGen/NonRelocatableStringpool.h"
15 #include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
16 #include "llvm/DWARFLinker/DWARFStreamer.h"
17 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
18 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
19 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
23 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
24 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
25 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
26 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
27 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
28 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
29 #include "llvm/MC/MCDwarf.h"
30 #include "llvm/Support/DataExtractor.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/ErrorOr.h"
34 #include "llvm/Support/FormatVariadic.h"
35 #include "llvm/Support/LEB128.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Support/ThreadPool.h"
42 /// Hold the input and output of the debug info size in bytes.
43 struct DebugInfoSize
{
48 /// Compute the total size of the debug info.
49 static uint64_t getDebugInfoSize(DWARFContext
&Dwarf
) {
51 for (auto &Unit
: Dwarf
.compile_units()) {
52 Size
+= Unit
->getLength();
57 /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
58 /// CompileUnit object instead.
59 static CompileUnit
*getUnitForOffset(const UnitListTy
&Units
, uint64_t Offset
) {
60 auto CU
= llvm::upper_bound(
61 Units
, Offset
, [](uint64_t LHS
, const std::unique_ptr
<CompileUnit
> &RHS
) {
62 return LHS
< RHS
->getOrigUnit().getNextUnitOffset();
64 return CU
!= Units
.end() ? CU
->get() : nullptr;
67 /// Resolve the DIE attribute reference that has been extracted in \p RefValue.
68 /// The resulting DIE might be in another CompileUnit which is stored into \p
69 /// ReferencedCU. \returns null if resolving fails for any reason.
70 DWARFDie
DWARFLinker::resolveDIEReference(const DWARFFile
&File
,
71 const UnitListTy
&Units
,
72 const DWARFFormValue
&RefValue
,
74 CompileUnit
*&RefCU
) {
75 assert(RefValue
.isFormClass(DWARFFormValue::FC_Reference
));
76 uint64_t RefOffset
= *RefValue
.getAsReference();
77 if ((RefCU
= getUnitForOffset(Units
, RefOffset
)))
78 if (const auto RefDie
= RefCU
->getOrigUnit().getDIEForOffset(RefOffset
)) {
79 // In a file with broken references, an attribute might point to a NULL
85 reportWarning("could not find referenced DIE", File
, &DIE
);
89 /// \returns whether the passed \a Attr type might contain a DIE reference
90 /// suitable for ODR uniquing.
91 static bool isODRAttribute(uint16_t Attr
) {
95 case dwarf::DW_AT_type
:
96 case dwarf::DW_AT_containing_type
:
97 case dwarf::DW_AT_specification
:
98 case dwarf::DW_AT_abstract_origin
:
99 case dwarf::DW_AT_import
:
102 llvm_unreachable("Improper attribute.");
105 static bool isTypeTag(uint16_t Tag
) {
107 case dwarf::DW_TAG_array_type
:
108 case dwarf::DW_TAG_class_type
:
109 case dwarf::DW_TAG_enumeration_type
:
110 case dwarf::DW_TAG_pointer_type
:
111 case dwarf::DW_TAG_reference_type
:
112 case dwarf::DW_TAG_string_type
:
113 case dwarf::DW_TAG_structure_type
:
114 case dwarf::DW_TAG_subroutine_type
:
115 case dwarf::DW_TAG_typedef
:
116 case dwarf::DW_TAG_union_type
:
117 case dwarf::DW_TAG_ptr_to_member_type
:
118 case dwarf::DW_TAG_set_type
:
119 case dwarf::DW_TAG_subrange_type
:
120 case dwarf::DW_TAG_base_type
:
121 case dwarf::DW_TAG_const_type
:
122 case dwarf::DW_TAG_constant
:
123 case dwarf::DW_TAG_file_type
:
124 case dwarf::DW_TAG_namelist
:
125 case dwarf::DW_TAG_packed_type
:
126 case dwarf::DW_TAG_volatile_type
:
127 case dwarf::DW_TAG_restrict_type
:
128 case dwarf::DW_TAG_atomic_type
:
129 case dwarf::DW_TAG_interface_type
:
130 case dwarf::DW_TAG_unspecified_type
:
131 case dwarf::DW_TAG_shared_type
:
132 case dwarf::DW_TAG_immutable_type
:
140 AddressesMap::~AddressesMap() = default;
142 DwarfEmitter::~DwarfEmitter() = default;
144 bool DWARFLinker::DIECloner::getDIENames(const DWARFDie
&Die
,
145 AttributesInfo
&Info
,
146 OffsetsStringPool
&StringPool
,
147 bool StripTemplate
) {
148 // This function will be called on DIEs having low_pcs and
149 // ranges. As getting the name might be more expansive, filter out
151 if (Die
.getTag() == dwarf::DW_TAG_lexical_block
)
154 if (!Info
.MangledName
)
155 if (const char *MangledName
= Die
.getLinkageName())
156 Info
.MangledName
= StringPool
.getEntry(MangledName
);
159 if (const char *Name
= Die
.getShortName())
160 Info
.Name
= StringPool
.getEntry(Name
);
162 if (!Info
.MangledName
)
163 Info
.MangledName
= Info
.Name
;
165 if (StripTemplate
&& Info
.Name
&& Info
.MangledName
!= Info
.Name
) {
166 StringRef Name
= Info
.Name
.getString();
167 if (std::optional
<StringRef
> StrippedName
= StripTemplateParameters(Name
))
168 Info
.NameWithoutTemplate
= StringPool
.getEntry(*StrippedName
);
171 return Info
.Name
|| Info
.MangledName
;
174 /// Resolve the relative path to a build artifact referenced by DWARF by
175 /// applying DW_AT_comp_dir.
176 static void resolveRelativeObjectPath(SmallVectorImpl
<char> &Buf
, DWARFDie CU
) {
177 sys::path::append(Buf
, dwarf::toString(CU
.find(dwarf::DW_AT_comp_dir
), ""));
180 /// Collect references to parseable Swift interfaces in imported
181 /// DW_TAG_module blocks.
182 static void analyzeImportedModule(
183 const DWARFDie
&DIE
, CompileUnit
&CU
,
184 swiftInterfacesMap
*ParseableSwiftInterfaces
,
185 std::function
<void(const Twine
&, const DWARFDie
&)> ReportWarning
) {
186 if (CU
.getLanguage() != dwarf::DW_LANG_Swift
)
189 if (!ParseableSwiftInterfaces
)
192 StringRef Path
= dwarf::toStringRef(DIE
.find(dwarf::DW_AT_LLVM_include_path
));
193 if (!Path
.endswith(".swiftinterface"))
195 // Don't track interfaces that are part of the SDK.
196 StringRef SysRoot
= dwarf::toStringRef(DIE
.find(dwarf::DW_AT_LLVM_sysroot
));
198 SysRoot
= CU
.getSysRoot();
199 if (!SysRoot
.empty() && Path
.startswith(SysRoot
))
201 std::optional
<const char *> Name
=
202 dwarf::toString(DIE
.find(dwarf::DW_AT_name
));
205 auto &Entry
= (*ParseableSwiftInterfaces
)[*Name
];
206 // The prepend path is applied later when copying.
207 DWARFDie CUDie
= CU
.getOrigUnit().getUnitDIE();
208 SmallString
<128> ResolvedPath
;
209 if (sys::path::is_relative(Path
))
210 resolveRelativeObjectPath(ResolvedPath
, CUDie
);
211 sys::path::append(ResolvedPath
, Path
);
212 if (!Entry
.empty() && Entry
!= ResolvedPath
)
213 ReportWarning(Twine("Conflicting parseable interfaces for Swift Module ") +
214 *Name
+ ": " + Entry
+ " and " + Path
,
216 Entry
= std::string(ResolvedPath
.str());
219 /// The distinct types of work performed by the work loop in
220 /// analyzeContextInfo.
221 enum class ContextWorklistItemType
: uint8_t {
227 /// This class represents an item in the work list. The type defines what kind
228 /// of work needs to be performed when processing the current item. Everything
229 /// but the Type and Die fields are optional based on the type.
230 struct ContextWorklistItem
{
234 CompileUnit::DIEInfo
*OtherInfo
;
235 DeclContext
*Context
;
237 ContextWorklistItemType Type
;
238 bool InImportedModule
;
240 ContextWorklistItem(DWARFDie Die
, ContextWorklistItemType T
,
241 CompileUnit::DIEInfo
*OtherInfo
= nullptr)
242 : Die(Die
), ParentIdx(0), OtherInfo(OtherInfo
), Type(T
),
243 InImportedModule(false) {}
245 ContextWorklistItem(DWARFDie Die
, DeclContext
*Context
, unsigned ParentIdx
,
246 bool InImportedModule
)
247 : Die(Die
), ParentIdx(ParentIdx
), Context(Context
),
248 Type(ContextWorklistItemType::AnalyzeContextInfo
),
249 InImportedModule(InImportedModule
) {}
252 static bool updatePruning(const DWARFDie
&Die
, CompileUnit
&CU
,
253 uint64_t ModulesEndOffset
) {
254 CompileUnit::DIEInfo
&Info
= CU
.getInfo(Die
);
256 // Prune this DIE if it is either a forward declaration inside a
257 // DW_TAG_module or a DW_TAG_module that contains nothing but
258 // forward declarations.
259 Info
.Prune
&= (Die
.getTag() == dwarf::DW_TAG_module
) ||
260 (isTypeTag(Die
.getTag()) &&
261 dwarf::toUnsigned(Die
.find(dwarf::DW_AT_declaration
), 0));
263 // Only prune forward declarations inside a DW_TAG_module for which a
264 // definition exists elsewhere.
265 if (ModulesEndOffset
== 0)
266 Info
.Prune
&= Info
.Ctxt
&& Info
.Ctxt
->getCanonicalDIEOffset();
268 Info
.Prune
&= Info
.Ctxt
&& Info
.Ctxt
->getCanonicalDIEOffset() > 0 &&
269 Info
.Ctxt
->getCanonicalDIEOffset() <= ModulesEndOffset
;
274 static void updateChildPruning(const DWARFDie
&Die
, CompileUnit
&CU
,
275 CompileUnit::DIEInfo
&ChildInfo
) {
276 CompileUnit::DIEInfo
&Info
= CU
.getInfo(Die
);
277 Info
.Prune
&= ChildInfo
.Prune
;
280 /// Recursive helper to build the global DeclContext information and
281 /// gather the child->parent relationships in the original compile unit.
283 /// This function uses the same work list approach as lookForDIEsToKeep.
285 /// \return true when this DIE and all of its children are only
286 /// forward declarations to types defined in external clang modules
287 /// (i.e., forward declarations that are children of a DW_TAG_module).
288 static void analyzeContextInfo(
289 const DWARFDie
&DIE
, unsigned ParentIdx
, CompileUnit
&CU
,
290 DeclContext
*CurrentDeclContext
, DeclContextTree
&Contexts
,
291 uint64_t ModulesEndOffset
, swiftInterfacesMap
*ParseableSwiftInterfaces
,
292 std::function
<void(const Twine
&, const DWARFDie
&)> ReportWarning
) {
294 std::vector
<ContextWorklistItem
> Worklist
;
295 Worklist
.emplace_back(DIE
, CurrentDeclContext
, ParentIdx
, false);
297 while (!Worklist
.empty()) {
298 ContextWorklistItem Current
= Worklist
.back();
301 switch (Current
.Type
) {
302 case ContextWorklistItemType::UpdatePruning
:
303 updatePruning(Current
.Die
, CU
, ModulesEndOffset
);
305 case ContextWorklistItemType::UpdateChildPruning
:
306 updateChildPruning(Current
.Die
, CU
, *Current
.OtherInfo
);
308 case ContextWorklistItemType::AnalyzeContextInfo
:
312 unsigned Idx
= CU
.getOrigUnit().getDIEIndex(Current
.Die
);
313 CompileUnit::DIEInfo
&Info
= CU
.getInfo(Idx
);
315 // Clang imposes an ODR on modules(!) regardless of the language:
316 // "The module-id should consist of only a single identifier,
317 // which provides the name of the module being defined. Each
318 // module shall have a single definition."
320 // This does not extend to the types inside the modules:
321 // "[I]n C, this implies that if two structs are defined in
322 // different submodules with the same name, those two types are
323 // distinct types (but may be compatible types if their
324 // definitions match)."
326 // We treat non-C++ modules like namespaces for this reason.
327 if (Current
.Die
.getTag() == dwarf::DW_TAG_module
&&
328 Current
.ParentIdx
== 0 &&
329 dwarf::toString(Current
.Die
.find(dwarf::DW_AT_name
), "") !=
330 CU
.getClangModuleName()) {
331 Current
.InImportedModule
= true;
332 analyzeImportedModule(Current
.Die
, CU
, ParseableSwiftInterfaces
,
336 Info
.ParentIdx
= Current
.ParentIdx
;
337 Info
.InModuleScope
= CU
.isClangModule() || Current
.InImportedModule
;
338 if (CU
.hasODR() || Info
.InModuleScope
) {
339 if (Current
.Context
) {
340 auto PtrInvalidPair
= Contexts
.getChildDeclContext(
341 *Current
.Context
, Current
.Die
, CU
, Info
.InModuleScope
);
342 Current
.Context
= PtrInvalidPair
.getPointer();
344 PtrInvalidPair
.getInt() ? nullptr : PtrInvalidPair
.getPointer();
346 Info
.Ctxt
->setDefinedInClangModule(Info
.InModuleScope
);
348 Info
.Ctxt
= Current
.Context
= nullptr;
351 Info
.Prune
= Current
.InImportedModule
;
352 // Add children in reverse order to the worklist to effectively process
354 Worklist
.emplace_back(Current
.Die
, ContextWorklistItemType::UpdatePruning
);
355 for (auto Child
: reverse(Current
.Die
.children())) {
356 CompileUnit::DIEInfo
&ChildInfo
= CU
.getInfo(Child
);
357 Worklist
.emplace_back(
358 Current
.Die
, ContextWorklistItemType::UpdateChildPruning
, &ChildInfo
);
359 Worklist
.emplace_back(Child
, Current
.Context
, Idx
,
360 Current
.InImportedModule
);
365 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag
) {
369 case dwarf::DW_TAG_class_type
:
370 case dwarf::DW_TAG_common_block
:
371 case dwarf::DW_TAG_lexical_block
:
372 case dwarf::DW_TAG_structure_type
:
373 case dwarf::DW_TAG_subprogram
:
374 case dwarf::DW_TAG_subroutine_type
:
375 case dwarf::DW_TAG_union_type
:
378 llvm_unreachable("Invalid Tag");
381 void DWARFLinker::cleanupAuxiliarryData(LinkContext
&Context
) {
384 for (DIEBlock
*I
: DIEBlocks
)
386 for (DIELoc
*I
: DIELocs
)
394 static bool isTlsAddressCode(uint8_t DW_OP_Code
) {
395 return DW_OP_Code
== dwarf::DW_OP_form_tls_address
||
396 DW_OP_Code
== dwarf::DW_OP_GNU_push_tls_address
;
399 std::pair
<bool, std::optional
<int64_t>>
400 DWARFLinker::getVariableRelocAdjustment(AddressesMap
&RelocMgr
,
401 const DWARFDie
&DIE
) {
402 assert((DIE
.getTag() == dwarf::DW_TAG_variable
||
403 DIE
.getTag() == dwarf::DW_TAG_constant
) &&
404 "Wrong type of input die");
406 const auto *Abbrev
= DIE
.getAbbreviationDeclarationPtr();
408 // Check if DIE has DW_AT_location attribute.
409 DWARFUnit
*U
= DIE
.getDwarfUnit();
410 std::optional
<uint32_t> LocationIdx
=
411 Abbrev
->findAttributeIndex(dwarf::DW_AT_location
);
413 return std::make_pair(false, std::nullopt
);
415 // Get offset to the DW_AT_location attribute.
416 uint64_t AttrOffset
=
417 Abbrev
->getAttributeOffsetFromIndex(*LocationIdx
, DIE
.getOffset(), *U
);
419 // Get value of the DW_AT_location attribute.
420 std::optional
<DWARFFormValue
> LocationValue
=
421 Abbrev
->getAttributeValueFromOffset(*LocationIdx
, AttrOffset
, *U
);
423 return std::make_pair(false, std::nullopt
);
425 // Check that DW_AT_location attribute is of 'exprloc' class.
426 // Handling value of location expressions for attributes of 'loclist'
427 // class is not implemented yet.
428 std::optional
<ArrayRef
<uint8_t>> Expr
= LocationValue
->getAsBlock();
430 return std::make_pair(false, std::nullopt
);
432 // Parse 'exprloc' expression.
433 DataExtractor
Data(toStringRef(*Expr
), U
->getContext().isLittleEndian(),
434 U
->getAddressByteSize());
435 DWARFExpression
Expression(Data
, U
->getAddressByteSize(),
436 U
->getFormParams().Format
);
438 bool HasLocationAddress
= false;
439 uint64_t CurExprOffset
= 0;
440 for (DWARFExpression::iterator It
= Expression
.begin();
441 It
!= Expression
.end(); ++It
) {
442 DWARFExpression::iterator NextIt
= It
;
445 const DWARFExpression::Operation
&Op
= *It
;
446 switch (Op
.getCode()) {
447 case dwarf::DW_OP_const2u
:
448 case dwarf::DW_OP_const4u
:
449 case dwarf::DW_OP_const8u
:
450 case dwarf::DW_OP_const2s
:
451 case dwarf::DW_OP_const4s
:
452 case dwarf::DW_OP_const8s
:
453 if (NextIt
== Expression
.end() || !isTlsAddressCode(NextIt
->getCode()))
456 case dwarf::DW_OP_addr
: {
457 HasLocationAddress
= true;
458 // Check relocation for the address.
459 if (std::optional
<int64_t> RelocAdjustment
=
460 RelocMgr
.getExprOpAddressRelocAdjustment(
461 *U
, Op
, AttrOffset
+ CurExprOffset
,
462 AttrOffset
+ Op
.getEndOffset()))
463 return std::make_pair(HasLocationAddress
, *RelocAdjustment
);
465 case dwarf::DW_OP_constx
:
466 case dwarf::DW_OP_addrx
: {
467 HasLocationAddress
= true;
468 if (std::optional
<uint64_t> AddressOffset
=
469 DIE
.getDwarfUnit()->getIndexedAddressOffset(
470 Op
.getRawOperand(0))) {
471 // Check relocation for the address.
472 if (std::optional
<int64_t> RelocAdjustment
=
473 RelocMgr
.getExprOpAddressRelocAdjustment(
474 *U
, Op
, *AddressOffset
,
475 *AddressOffset
+ DIE
.getDwarfUnit()->getAddressByteSize()))
476 return std::make_pair(HasLocationAddress
, *RelocAdjustment
);
483 CurExprOffset
= Op
.getEndOffset();
486 return std::make_pair(HasLocationAddress
, std::nullopt
);
489 /// Check if a variable describing DIE should be kept.
490 /// \returns updated TraversalFlags.
491 unsigned DWARFLinker::shouldKeepVariableDIE(AddressesMap
&RelocMgr
,
493 CompileUnit::DIEInfo
&MyInfo
,
495 const auto *Abbrev
= DIE
.getAbbreviationDeclarationPtr();
497 // Global variables with constant value can always be kept.
498 if (!(Flags
& TF_InFunctionScope
) &&
499 Abbrev
->findAttributeIndex(dwarf::DW_AT_const_value
)) {
500 MyInfo
.InDebugMap
= true;
501 return Flags
| TF_Keep
;
504 // See if there is a relocation to a valid debug map entry inside this
505 // variable's location. The order is important here. We want to always check
506 // if the variable has a valid relocation, so that the DIEInfo is filled.
507 // However, we don't want a static variable in a function to force us to keep
508 // the enclosing function, unless requested explicitly.
509 std::pair
<bool, std::optional
<int64_t>> LocExprAddrAndRelocAdjustment
=
510 getVariableRelocAdjustment(RelocMgr
, DIE
);
512 if (LocExprAddrAndRelocAdjustment
.first
)
513 MyInfo
.HasLocationExpressionAddr
= true;
515 if (!LocExprAddrAndRelocAdjustment
.second
)
518 MyInfo
.AddrAdjust
= *LocExprAddrAndRelocAdjustment
.second
;
519 MyInfo
.InDebugMap
= true;
521 if (((Flags
& TF_InFunctionScope
) &&
522 !LLVM_UNLIKELY(Options
.KeepFunctionForStatic
)))
525 if (Options
.Verbose
) {
526 outs() << "Keeping variable DIE:";
527 DIDumpOptions DumpOpts
;
528 DumpOpts
.ChildRecurseDepth
= 0;
529 DumpOpts
.Verbose
= Options
.Verbose
;
530 DIE
.dump(outs(), 8 /* Indent */, DumpOpts
);
533 return Flags
| TF_Keep
;
536 /// Check if a function describing DIE should be kept.
537 /// \returns updated TraversalFlags.
538 unsigned DWARFLinker::shouldKeepSubprogramDIE(
539 AddressesMap
&RelocMgr
, const DWARFDie
&DIE
, const DWARFFile
&File
,
540 CompileUnit
&Unit
, CompileUnit::DIEInfo
&MyInfo
, unsigned Flags
) {
541 Flags
|= TF_InFunctionScope
;
543 auto LowPc
= dwarf::toAddress(DIE
.find(dwarf::DW_AT_low_pc
));
547 assert(LowPc
&& "low_pc attribute is not an address.");
548 std::optional
<int64_t> RelocAdjustment
=
549 RelocMgr
.getSubprogramRelocAdjustment(DIE
);
550 if (!RelocAdjustment
)
553 MyInfo
.AddrAdjust
= *RelocAdjustment
;
554 MyInfo
.InDebugMap
= true;
556 if (Options
.Verbose
) {
557 outs() << "Keeping subprogram DIE:";
558 DIDumpOptions DumpOpts
;
559 DumpOpts
.ChildRecurseDepth
= 0;
560 DumpOpts
.Verbose
= Options
.Verbose
;
561 DIE
.dump(outs(), 8 /* Indent */, DumpOpts
);
564 if (DIE
.getTag() == dwarf::DW_TAG_label
) {
565 if (Unit
.hasLabelAt(*LowPc
))
568 DWARFUnit
&OrigUnit
= Unit
.getOrigUnit();
569 // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
570 // that don't fall into the CU's aranges. This is wrong IMO. Debug info
571 // generation bugs aside, this is really wrong in the case of labels, where
572 // a label marking the end of a function will have a PC == CU's high_pc.
573 if (dwarf::toAddress(OrigUnit
.getUnitDIE().find(dwarf::DW_AT_high_pc
))
574 .value_or(UINT64_MAX
) <= LowPc
)
576 Unit
.addLabelLowPc(*LowPc
, MyInfo
.AddrAdjust
);
577 return Flags
| TF_Keep
;
582 std::optional
<uint64_t> HighPc
= DIE
.getHighPC(*LowPc
);
584 reportWarning("Function without high_pc. Range will be discarded.\n", File
,
588 if (*LowPc
> *HighPc
) {
589 reportWarning("low_pc greater than high_pc. Range will be discarded.\n",
594 // Replace the debug map range with a more accurate one.
595 Unit
.addFunctionRange(*LowPc
, *HighPc
, MyInfo
.AddrAdjust
);
599 /// Check if a DIE should be kept.
600 /// \returns updated TraversalFlags.
601 unsigned DWARFLinker::shouldKeepDIE(AddressesMap
&RelocMgr
, const DWARFDie
&DIE
,
602 const DWARFFile
&File
, CompileUnit
&Unit
,
603 CompileUnit::DIEInfo
&MyInfo
,
605 switch (DIE
.getTag()) {
606 case dwarf::DW_TAG_constant
:
607 case dwarf::DW_TAG_variable
:
608 return shouldKeepVariableDIE(RelocMgr
, DIE
, MyInfo
, Flags
);
609 case dwarf::DW_TAG_subprogram
:
610 case dwarf::DW_TAG_label
:
611 return shouldKeepSubprogramDIE(RelocMgr
, DIE
, File
, Unit
, MyInfo
, Flags
);
612 case dwarf::DW_TAG_base_type
:
613 // DWARF Expressions may reference basic types, but scanning them
614 // is expensive. Basic types are tiny, so just keep all of them.
615 case dwarf::DW_TAG_imported_module
:
616 case dwarf::DW_TAG_imported_declaration
:
617 case dwarf::DW_TAG_imported_unit
:
618 // We always want to keep these.
619 return Flags
| TF_Keep
;
627 /// Helper that updates the completeness of the current DIE based on the
628 /// completeness of one of its children. It depends on the incompleteness of
629 /// the children already being computed.
630 static void updateChildIncompleteness(const DWARFDie
&Die
, CompileUnit
&CU
,
631 CompileUnit::DIEInfo
&ChildInfo
) {
632 switch (Die
.getTag()) {
633 case dwarf::DW_TAG_structure_type
:
634 case dwarf::DW_TAG_class_type
:
635 case dwarf::DW_TAG_union_type
:
641 CompileUnit::DIEInfo
&MyInfo
= CU
.getInfo(Die
);
643 if (ChildInfo
.Incomplete
|| ChildInfo
.Prune
)
644 MyInfo
.Incomplete
= true;
647 /// Helper that updates the completeness of the current DIE based on the
648 /// completeness of the DIEs it references. It depends on the incompleteness of
649 /// the referenced DIE already being computed.
650 static void updateRefIncompleteness(const DWARFDie
&Die
, CompileUnit
&CU
,
651 CompileUnit::DIEInfo
&RefInfo
) {
652 switch (Die
.getTag()) {
653 case dwarf::DW_TAG_typedef
:
654 case dwarf::DW_TAG_member
:
655 case dwarf::DW_TAG_reference_type
:
656 case dwarf::DW_TAG_ptr_to_member_type
:
657 case dwarf::DW_TAG_pointer_type
:
663 CompileUnit::DIEInfo
&MyInfo
= CU
.getInfo(Die
);
665 if (MyInfo
.Incomplete
)
668 if (RefInfo
.Incomplete
)
669 MyInfo
.Incomplete
= true;
672 /// Look at the children of the given DIE and decide whether they should be
674 void DWARFLinker::lookForChildDIEsToKeep(
675 const DWARFDie
&Die
, CompileUnit
&CU
, unsigned Flags
,
676 SmallVectorImpl
<WorklistItem
> &Worklist
) {
677 // The TF_ParentWalk flag tells us that we are currently walking up the
678 // parent chain of a required DIE, and we don't want to mark all the children
679 // of the parents as kept (consider for example a DW_TAG_namespace node in
680 // the parent chain). There are however a set of DIE types for which we want
681 // to ignore that directive and still walk their children.
682 if (dieNeedsChildrenToBeMeaningful(Die
.getTag()))
683 Flags
&= ~DWARFLinker::TF_ParentWalk
;
685 // We're finished if this DIE has no children or we're walking the parent
687 if (!Die
.hasChildren() || (Flags
& DWARFLinker::TF_ParentWalk
))
690 // Add children in reverse order to the worklist to effectively process them
692 for (auto Child
: reverse(Die
.children())) {
693 // Add a worklist item before every child to calculate incompleteness right
694 // after the current child is processed.
695 CompileUnit::DIEInfo
&ChildInfo
= CU
.getInfo(Child
);
696 Worklist
.emplace_back(Die
, CU
, WorklistItemType::UpdateChildIncompleteness
,
698 Worklist
.emplace_back(Child
, CU
, Flags
);
702 static bool isODRCanonicalCandidate(const DWARFDie
&Die
, CompileUnit
&CU
) {
703 CompileUnit::DIEInfo
&Info
= CU
.getInfo(Die
);
705 if (!Info
.Ctxt
|| (Die
.getTag() == dwarf::DW_TAG_namespace
))
708 if (!CU
.hasODR() && !Info
.InModuleScope
)
711 return !Info
.Incomplete
&& Info
.Ctxt
!= CU
.getInfo(Info
.ParentIdx
).Ctxt
;
714 void DWARFLinker::markODRCanonicalDie(const DWARFDie
&Die
, CompileUnit
&CU
) {
715 CompileUnit::DIEInfo
&Info
= CU
.getInfo(Die
);
717 Info
.ODRMarkingDone
= true;
718 if (Info
.Keep
&& isODRCanonicalCandidate(Die
, CU
) &&
719 !Info
.Ctxt
->hasCanonicalDIE())
720 Info
.Ctxt
->setHasCanonicalDIE();
723 /// Look at DIEs referenced by the given DIE and decide whether they should be
724 /// kept. All DIEs referenced though attributes should be kept.
725 void DWARFLinker::lookForRefDIEsToKeep(
726 const DWARFDie
&Die
, CompileUnit
&CU
, unsigned Flags
,
727 const UnitListTy
&Units
, const DWARFFile
&File
,
728 SmallVectorImpl
<WorklistItem
> &Worklist
) {
729 bool UseOdr
= (Flags
& DWARFLinker::TF_DependencyWalk
)
730 ? (Flags
& DWARFLinker::TF_ODR
)
732 DWARFUnit
&Unit
= CU
.getOrigUnit();
733 DWARFDataExtractor Data
= Unit
.getDebugInfoExtractor();
734 const auto *Abbrev
= Die
.getAbbreviationDeclarationPtr();
735 uint64_t Offset
= Die
.getOffset() + getULEB128Size(Abbrev
->getCode());
737 SmallVector
<std::pair
<DWARFDie
, CompileUnit
&>, 4> ReferencedDIEs
;
738 for (const auto &AttrSpec
: Abbrev
->attributes()) {
739 DWARFFormValue
Val(AttrSpec
.Form
);
740 if (!Val
.isFormClass(DWARFFormValue::FC_Reference
) ||
741 AttrSpec
.Attr
== dwarf::DW_AT_sibling
) {
742 DWARFFormValue::skipValue(AttrSpec
.Form
, Data
, &Offset
,
743 Unit
.getFormParams());
747 Val
.extractValue(Data
, &Offset
, Unit
.getFormParams(), &Unit
);
748 CompileUnit
*ReferencedCU
;
750 resolveDIEReference(File
, Units
, Val
, Die
, ReferencedCU
)) {
751 CompileUnit::DIEInfo
&Info
= ReferencedCU
->getInfo(RefDie
);
752 // If the referenced DIE has a DeclContext that has already been
753 // emitted, then do not keep the one in this CU. We'll link to
754 // the canonical DIE in cloneDieReferenceAttribute.
756 // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
757 // be necessary and could be advantageously replaced by
758 // ReferencedCU->hasODR() && CU.hasODR().
760 // FIXME: compatibility with dsymutil-classic. There is no
761 // reason not to unique ref_addr references.
762 if (AttrSpec
.Form
!= dwarf::DW_FORM_ref_addr
&&
763 isODRAttribute(AttrSpec
.Attr
) && Info
.Ctxt
&&
764 Info
.Ctxt
->hasCanonicalDIE())
767 // Keep a module forward declaration if there is no definition.
768 if (!(isODRAttribute(AttrSpec
.Attr
) && Info
.Ctxt
&&
769 Info
.Ctxt
->hasCanonicalDIE()))
771 ReferencedDIEs
.emplace_back(RefDie
, *ReferencedCU
);
775 unsigned ODRFlag
= UseOdr
? DWARFLinker::TF_ODR
: 0;
777 // Add referenced DIEs in reverse order to the worklist to effectively
778 // process them in order.
779 for (auto &P
: reverse(ReferencedDIEs
)) {
780 // Add a worklist item before every child to calculate incompleteness right
781 // after the current child is processed.
782 CompileUnit::DIEInfo
&Info
= P
.second
.getInfo(P
.first
);
783 Worklist
.emplace_back(Die
, CU
, WorklistItemType::UpdateRefIncompleteness
,
785 Worklist
.emplace_back(P
.first
, P
.second
,
786 DWARFLinker::TF_Keep
|
787 DWARFLinker::TF_DependencyWalk
| ODRFlag
);
791 /// Look at the parent of the given DIE and decide whether they should be kept.
792 void DWARFLinker::lookForParentDIEsToKeep(
793 unsigned AncestorIdx
, CompileUnit
&CU
, unsigned Flags
,
794 SmallVectorImpl
<WorklistItem
> &Worklist
) {
795 // Stop if we encounter an ancestor that's already marked as kept.
796 if (CU
.getInfo(AncestorIdx
).Keep
)
799 DWARFUnit
&Unit
= CU
.getOrigUnit();
800 DWARFDie ParentDIE
= Unit
.getDIEAtIndex(AncestorIdx
);
801 Worklist
.emplace_back(CU
.getInfo(AncestorIdx
).ParentIdx
, CU
, Flags
);
802 Worklist
.emplace_back(ParentDIE
, CU
, Flags
);
805 /// Recursively walk the \p DIE tree and look for DIEs to keep. Store that
806 /// information in \p CU's DIEInfo.
808 /// This function is the entry point of the DIE selection algorithm. It is
809 /// expected to walk the DIE tree in file order and (though the mediation of
810 /// its helper) call hasValidRelocation() on each DIE that might be a 'root
811 /// DIE' (See DwarfLinker class comment).
813 /// While walking the dependencies of root DIEs, this function is also called,
814 /// but during these dependency walks the file order is not respected. The
815 /// TF_DependencyWalk flag tells us which kind of traversal we are currently
818 /// The recursive algorithm is implemented iteratively as a work list because
819 /// very deep recursion could exhaust the stack for large projects. The work
820 /// list acts as a scheduler for different types of work that need to be
823 /// The recursive nature of the algorithm is simulated by running the "main"
824 /// algorithm (LookForDIEsToKeep) followed by either looking at more DIEs
825 /// (LookForChildDIEsToKeep, LookForRefDIEsToKeep, LookForParentDIEsToKeep) or
826 /// fixing up a computed property (UpdateChildIncompleteness,
827 /// UpdateRefIncompleteness).
829 /// The return value indicates whether the DIE is incomplete.
830 void DWARFLinker::lookForDIEsToKeep(AddressesMap
&AddressesMap
,
831 const UnitListTy
&Units
,
832 const DWARFDie
&Die
, const DWARFFile
&File
,
833 CompileUnit
&Cu
, unsigned Flags
) {
835 SmallVector
<WorklistItem
, 4> Worklist
;
836 Worklist
.emplace_back(Die
, Cu
, Flags
);
838 while (!Worklist
.empty()) {
839 WorklistItem Current
= Worklist
.pop_back_val();
841 // Look at the worklist type to decide what kind of work to perform.
842 switch (Current
.Type
) {
843 case WorklistItemType::UpdateChildIncompleteness
:
844 updateChildIncompleteness(Current
.Die
, Current
.CU
, *Current
.OtherInfo
);
846 case WorklistItemType::UpdateRefIncompleteness
:
847 updateRefIncompleteness(Current
.Die
, Current
.CU
, *Current
.OtherInfo
);
849 case WorklistItemType::LookForChildDIEsToKeep
:
850 lookForChildDIEsToKeep(Current
.Die
, Current
.CU
, Current
.Flags
, Worklist
);
852 case WorklistItemType::LookForRefDIEsToKeep
:
853 lookForRefDIEsToKeep(Current
.Die
, Current
.CU
, Current
.Flags
, Units
, File
,
856 case WorklistItemType::LookForParentDIEsToKeep
:
857 lookForParentDIEsToKeep(Current
.AncestorIdx
, Current
.CU
, Current
.Flags
,
860 case WorklistItemType::MarkODRCanonicalDie
:
861 markODRCanonicalDie(Current
.Die
, Current
.CU
);
863 case WorklistItemType::LookForDIEsToKeep
:
867 unsigned Idx
= Current
.CU
.getOrigUnit().getDIEIndex(Current
.Die
);
868 CompileUnit::DIEInfo
&MyInfo
= Current
.CU
.getInfo(Idx
);
871 // We're walking the dependencies of a module forward declaration that was
872 // kept because there is no definition.
873 if (Current
.Flags
& TF_DependencyWalk
)
874 MyInfo
.Prune
= false;
879 // If the Keep flag is set, we are marking a required DIE's dependencies.
880 // If our target is already marked as kept, we're all set.
881 bool AlreadyKept
= MyInfo
.Keep
;
882 if ((Current
.Flags
& TF_DependencyWalk
) && AlreadyKept
)
885 if (!(Current
.Flags
& TF_DependencyWalk
))
886 Current
.Flags
= shouldKeepDIE(AddressesMap
, Current
.Die
, File
, Current
.CU
,
887 MyInfo
, Current
.Flags
);
889 // We need to mark context for the canonical die in the end of normal
890 // traversing(not TF_DependencyWalk) or after normal traversing if die
891 // was not marked as kept.
892 if (!(Current
.Flags
& TF_DependencyWalk
) ||
893 (MyInfo
.ODRMarkingDone
&& !MyInfo
.Keep
)) {
894 if (Current
.CU
.hasODR() || MyInfo
.InModuleScope
)
895 Worklist
.emplace_back(Current
.Die
, Current
.CU
,
896 WorklistItemType::MarkODRCanonicalDie
);
899 // Finish by looking for child DIEs. Because of the LIFO worklist we need
900 // to schedule that work before any subsequent items are added to the
902 Worklist
.emplace_back(Current
.Die
, Current
.CU
, Current
.Flags
,
903 WorklistItemType::LookForChildDIEsToKeep
);
905 if (AlreadyKept
|| !(Current
.Flags
& TF_Keep
))
908 // If it is a newly kept DIE mark it as well as all its dependencies as
912 // We're looking for incomplete types.
914 Current
.Die
.getTag() != dwarf::DW_TAG_subprogram
&&
915 Current
.Die
.getTag() != dwarf::DW_TAG_member
&&
916 dwarf::toUnsigned(Current
.Die
.find(dwarf::DW_AT_declaration
), 0);
918 // After looking at the parent chain, look for referenced DIEs. Because of
919 // the LIFO worklist we need to schedule that work before any subsequent
920 // items are added to the worklist.
921 Worklist
.emplace_back(Current
.Die
, Current
.CU
, Current
.Flags
,
922 WorklistItemType::LookForRefDIEsToKeep
);
924 bool UseOdr
= (Current
.Flags
& TF_DependencyWalk
) ? (Current
.Flags
& TF_ODR
)
925 : Current
.CU
.hasODR();
926 unsigned ODRFlag
= UseOdr
? TF_ODR
: 0;
927 unsigned ParFlags
= TF_ParentWalk
| TF_Keep
| TF_DependencyWalk
| ODRFlag
;
929 // Now schedule the parent walk.
930 Worklist
.emplace_back(MyInfo
.ParentIdx
, Current
.CU
, ParFlags
);
935 /// A broken link in the keep chain. By recording both the parent and the child
936 /// we can show only broken links for DIEs with multiple children.
938 BrokenLink(DWARFDie Parent
, DWARFDie Child
) : Parent(Parent
), Child(Child
) {}
943 /// Verify the keep chain by looking for DIEs that are kept but who's parent
945 static void verifyKeepChain(CompileUnit
&CU
) {
946 std::vector
<DWARFDie
> Worklist
;
947 Worklist
.push_back(CU
.getOrigUnit().getUnitDIE());
949 // List of broken links.
950 std::vector
<BrokenLink
> BrokenLinks
;
952 while (!Worklist
.empty()) {
953 const DWARFDie Current
= Worklist
.back();
956 const bool CurrentDieIsKept
= CU
.getInfo(Current
).Keep
;
958 for (DWARFDie Child
: reverse(Current
.children())) {
959 Worklist
.push_back(Child
);
961 const bool ChildDieIsKept
= CU
.getInfo(Child
).Keep
;
962 if (!CurrentDieIsKept
&& ChildDieIsKept
)
963 BrokenLinks
.emplace_back(Current
, Child
);
967 if (!BrokenLinks
.empty()) {
968 for (BrokenLink Link
: BrokenLinks
) {
969 WithColor::error() << formatv(
970 "Found invalid link in keep chain between {0:x} and {1:x}\n",
971 Link
.Parent
.getOffset(), Link
.Child
.getOffset());
974 Link
.Parent
.dump(errs(), 0, {});
975 CU
.getInfo(Link
.Parent
).dump();
978 Link
.Child
.dump(errs(), 2, {});
979 CU
.getInfo(Link
.Child
).dump();
981 report_fatal_error("invalid keep chain");
986 /// Assign an abbreviation number to \p Abbrev.
988 /// Our DIEs get freed after every DebugMapObject has been processed,
989 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
990 /// the instances hold by the DIEs. When we encounter an abbreviation
991 /// that we don't know, we create a permanent copy of it.
992 void DWARFLinker::assignAbbrev(DIEAbbrev
&Abbrev
) {
993 // Check the set for priors.
997 DIEAbbrev
*InSet
= AbbreviationsSet
.FindNodeOrInsertPos(ID
, InsertToken
);
999 // If it's newly added.
1001 // Assign existing abbreviation number.
1002 Abbrev
.setNumber(InSet
->getNumber());
1004 // Add to abbreviation list.
1005 Abbreviations
.push_back(
1006 std::make_unique
<DIEAbbrev
>(Abbrev
.getTag(), Abbrev
.hasChildren()));
1007 for (const auto &Attr
: Abbrev
.getData())
1008 Abbreviations
.back()->AddAttribute(Attr
);
1009 AbbreviationsSet
.InsertNode(Abbreviations
.back().get(), InsertToken
);
1010 // Assign the unique abbreviation number.
1011 Abbrev
.setNumber(Abbreviations
.size());
1012 Abbreviations
.back()->setNumber(Abbreviations
.size());
1016 unsigned DWARFLinker::DIECloner::cloneStringAttribute(DIE
&Die
,
1017 AttributeSpec AttrSpec
,
1018 const DWARFFormValue
&Val
,
1020 AttributesInfo
&Info
) {
1021 std::optional
<const char *> String
= dwarf::toString(Val
);
1024 DwarfStringPoolEntryRef StringEntry
;
1025 if (AttrSpec
.Form
== dwarf::DW_FORM_line_strp
) {
1026 StringEntry
= DebugLineStrPool
.getEntry(*String
);
1028 StringEntry
= DebugStrPool
.getEntry(*String
);
1030 if (AttrSpec
.Attr
== dwarf::DW_AT_APPLE_origin
) {
1031 Info
.HasAppleOrigin
= true;
1032 if (std::optional
<StringRef
> FileName
=
1033 ObjFile
.Addresses
->getLibraryInstallName()) {
1034 StringEntry
= DebugStrPool
.getEntry(*FileName
);
1038 // Update attributes info.
1039 if (AttrSpec
.Attr
== dwarf::DW_AT_name
)
1040 Info
.Name
= StringEntry
;
1041 else if (AttrSpec
.Attr
== dwarf::DW_AT_MIPS_linkage_name
||
1042 AttrSpec
.Attr
== dwarf::DW_AT_linkage_name
)
1043 Info
.MangledName
= StringEntry
;
1044 if (U
.getVersion() >= 5) {
1045 // Switch everything to DW_FORM_strx strings.
1046 auto StringOffsetIndex
=
1047 StringOffsetPool
.getValueIndex(StringEntry
.getOffset());
1049 .addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1050 dwarf::DW_FORM_strx
, DIEInteger(StringOffsetIndex
))
1051 ->sizeOf(U
.getFormParams());
1053 // Switch everything to out of line strings.
1054 AttrSpec
.Form
= dwarf::DW_FORM_strp
;
1056 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
), AttrSpec
.Form
,
1057 DIEInteger(StringEntry
.getOffset()));
1061 unsigned DWARFLinker::DIECloner::cloneDieReferenceAttribute(
1062 DIE
&Die
, const DWARFDie
&InputDIE
, AttributeSpec AttrSpec
,
1063 unsigned AttrSize
, const DWARFFormValue
&Val
, const DWARFFile
&File
,
1064 CompileUnit
&Unit
) {
1065 const DWARFUnit
&U
= Unit
.getOrigUnit();
1066 uint64_t Ref
= *Val
.getAsReference();
1068 DIE
*NewRefDie
= nullptr;
1069 CompileUnit
*RefUnit
= nullptr;
1072 Linker
.resolveDIEReference(File
, CompileUnits
, Val
, InputDIE
, RefUnit
);
1074 // If the referenced DIE is not found, drop the attribute.
1075 if (!RefDie
|| AttrSpec
.Attr
== dwarf::DW_AT_sibling
)
1078 CompileUnit::DIEInfo
&RefInfo
= RefUnit
->getInfo(RefDie
);
1080 // If we already have emitted an equivalent DeclContext, just point
1082 if (isODRAttribute(AttrSpec
.Attr
) && RefInfo
.Ctxt
&&
1083 RefInfo
.Ctxt
->getCanonicalDIEOffset()) {
1084 assert(RefInfo
.Ctxt
->hasCanonicalDIE() &&
1085 "Offset to canonical die is set, but context is not marked");
1086 DIEInteger
Attr(RefInfo
.Ctxt
->getCanonicalDIEOffset());
1087 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1088 dwarf::DW_FORM_ref_addr
, Attr
);
1089 return U
.getRefAddrByteSize();
1092 if (!RefInfo
.Clone
) {
1093 // We haven't cloned this DIE yet. Just create an empty one and
1094 // store it. It'll get really cloned when we process it.
1095 RefInfo
.UnclonedReference
= true;
1096 RefInfo
.Clone
= DIE::get(DIEAlloc
, dwarf::Tag(RefDie
.getTag()));
1098 NewRefDie
= RefInfo
.Clone
;
1100 if (AttrSpec
.Form
== dwarf::DW_FORM_ref_addr
||
1101 (Unit
.hasODR() && isODRAttribute(AttrSpec
.Attr
))) {
1102 // We cannot currently rely on a DIEEntry to emit ref_addr
1103 // references, because the implementation calls back to DwarfDebug
1104 // to find the unit offset. (We don't have a DwarfDebug)
1105 // FIXME: we should be able to design DIEEntry reliance on
1108 if (Ref
< InputDIE
.getOffset() && !RefInfo
.UnclonedReference
) {
1109 // We have already cloned that DIE.
1110 uint32_t NewRefOffset
=
1111 RefUnit
->getStartOffset() + NewRefDie
->getOffset();
1112 Attr
= NewRefOffset
;
1113 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1114 dwarf::DW_FORM_ref_addr
, DIEInteger(Attr
));
1116 // A forward reference. Note and fixup later.
1118 Unit
.noteForwardReference(
1119 NewRefDie
, RefUnit
, RefInfo
.Ctxt
,
1120 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1121 dwarf::DW_FORM_ref_addr
, DIEInteger(Attr
)));
1123 return U
.getRefAddrByteSize();
1126 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1127 dwarf::Form(AttrSpec
.Form
), DIEEntry(*NewRefDie
));
1132 void DWARFLinker::DIECloner::cloneExpression(
1133 DataExtractor
&Data
, DWARFExpression Expression
, const DWARFFile
&File
,
1134 CompileUnit
&Unit
, SmallVectorImpl
<uint8_t> &OutputBuffer
,
1135 int64_t AddrRelocAdjustment
, bool IsLittleEndian
) {
1136 using Encoding
= DWARFExpression::Operation::Encoding
;
1138 uint8_t OrigAddressByteSize
= Unit
.getOrigUnit().getAddressByteSize();
1140 uint64_t OpOffset
= 0;
1141 for (auto &Op
: Expression
) {
1142 auto Desc
= Op
.getDescription();
1143 // DW_OP_const_type is variable-length and has 3
1144 // operands. Thus far we only support 2.
1145 if ((Desc
.Op
.size() == 2 && Desc
.Op
[0] == Encoding::BaseTypeRef
) ||
1146 (Desc
.Op
.size() == 2 && Desc
.Op
[1] == Encoding::BaseTypeRef
&&
1147 Desc
.Op
[0] != Encoding::Size1
))
1148 Linker
.reportWarning("Unsupported DW_OP encoding.", File
);
1150 if ((Desc
.Op
.size() == 1 && Desc
.Op
[0] == Encoding::BaseTypeRef
) ||
1151 (Desc
.Op
.size() == 2 && Desc
.Op
[1] == Encoding::BaseTypeRef
&&
1152 Desc
.Op
[0] == Encoding::Size1
)) {
1153 // This code assumes that the other non-typeref operand fits into 1 byte.
1154 assert(OpOffset
< Op
.getEndOffset());
1155 uint32_t ULEBsize
= Op
.getEndOffset() - OpOffset
- 1;
1156 assert(ULEBsize
<= 16);
1158 // Copy over the operation.
1159 assert(!Op
.getSubCode() && "SubOps not yet supported");
1160 OutputBuffer
.push_back(Op
.getCode());
1162 if (Desc
.Op
.size() == 1) {
1163 RefOffset
= Op
.getRawOperand(0);
1165 OutputBuffer
.push_back(Op
.getRawOperand(0));
1166 RefOffset
= Op
.getRawOperand(1);
1168 uint32_t Offset
= 0;
1169 // Look up the base type. For DW_OP_convert, the operand may be 0 to
1170 // instead indicate the generic type. The same holds for
1171 // DW_OP_reinterpret, which is currently not supported.
1172 if (RefOffset
> 0 || Op
.getCode() != dwarf::DW_OP_convert
) {
1173 RefOffset
+= Unit
.getOrigUnit().getOffset();
1174 auto RefDie
= Unit
.getOrigUnit().getDIEForOffset(RefOffset
);
1175 CompileUnit::DIEInfo
&Info
= Unit
.getInfo(RefDie
);
1176 if (DIE
*Clone
= Info
.Clone
)
1177 Offset
= Clone
->getOffset();
1179 Linker
.reportWarning(
1180 "base type ref doesn't point to DW_TAG_base_type.", File
);
1183 unsigned RealSize
= encodeULEB128(Offset
, ULEB
, ULEBsize
);
1184 if (RealSize
> ULEBsize
) {
1185 // Emit the generic type as a fallback.
1186 RealSize
= encodeULEB128(0, ULEB
, ULEBsize
);
1187 Linker
.reportWarning("base type ref doesn't fit.", File
);
1189 assert(RealSize
== ULEBsize
&& "padding failed");
1190 ArrayRef
<uint8_t> ULEBbytes(ULEB
, ULEBsize
);
1191 OutputBuffer
.append(ULEBbytes
.begin(), ULEBbytes
.end());
1192 } else if (!Linker
.Options
.Update
&& Op
.getCode() == dwarf::DW_OP_addrx
) {
1193 if (std::optional
<object::SectionedAddress
> SA
=
1194 Unit
.getOrigUnit().getAddrOffsetSectionItem(
1195 Op
.getRawOperand(0))) {
1196 // DWARFLinker does not use addrx forms since it generates relocated
1197 // addresses. Replace DW_OP_addrx with DW_OP_addr here.
1198 // Argument of DW_OP_addrx should be relocated here as it is not
1199 // processed by applyValidRelocs.
1200 OutputBuffer
.push_back(dwarf::DW_OP_addr
);
1201 uint64_t LinkedAddress
= SA
->Address
+ AddrRelocAdjustment
;
1202 if (IsLittleEndian
!= sys::IsLittleEndianHost
)
1203 sys::swapByteOrder(LinkedAddress
);
1204 ArrayRef
<uint8_t> AddressBytes(
1205 reinterpret_cast<const uint8_t *>(&LinkedAddress
),
1206 OrigAddressByteSize
);
1207 OutputBuffer
.append(AddressBytes
.begin(), AddressBytes
.end());
1209 Linker
.reportWarning("cannot read DW_OP_addrx operand.", File
);
1210 } else if (!Linker
.Options
.Update
&& Op
.getCode() == dwarf::DW_OP_constx
) {
1211 if (std::optional
<object::SectionedAddress
> SA
=
1212 Unit
.getOrigUnit().getAddrOffsetSectionItem(
1213 Op
.getRawOperand(0))) {
1214 // DWARFLinker does not use constx forms since it generates relocated
1215 // addresses. Replace DW_OP_constx with DW_OP_const[*]u here.
1216 // Argument of DW_OP_constx should be relocated here as it is not
1217 // processed by applyValidRelocs.
1218 std::optional
<uint8_t> OutOperandKind
;
1219 switch (OrigAddressByteSize
) {
1221 OutOperandKind
= dwarf::DW_OP_const4u
;
1224 OutOperandKind
= dwarf::DW_OP_const8u
;
1227 Linker
.reportWarning(
1228 formatv(("unsupported address size: {0}."), OrigAddressByteSize
),
1233 if (OutOperandKind
) {
1234 OutputBuffer
.push_back(*OutOperandKind
);
1235 uint64_t LinkedAddress
= SA
->Address
+ AddrRelocAdjustment
;
1236 if (IsLittleEndian
!= sys::IsLittleEndianHost
)
1237 sys::swapByteOrder(LinkedAddress
);
1238 ArrayRef
<uint8_t> AddressBytes(
1239 reinterpret_cast<const uint8_t *>(&LinkedAddress
),
1240 OrigAddressByteSize
);
1241 OutputBuffer
.append(AddressBytes
.begin(), AddressBytes
.end());
1244 Linker
.reportWarning("cannot read DW_OP_constx operand.", File
);
1246 // Copy over everything else unmodified.
1247 StringRef Bytes
= Data
.getData().slice(OpOffset
, Op
.getEndOffset());
1248 OutputBuffer
.append(Bytes
.begin(), Bytes
.end());
1250 OpOffset
= Op
.getEndOffset();
1254 unsigned DWARFLinker::DIECloner::cloneBlockAttribute(
1255 DIE
&Die
, const DWARFDie
&InputDIE
, const DWARFFile
&File
,
1256 CompileUnit
&Unit
, AttributeSpec AttrSpec
, const DWARFFormValue
&Val
,
1257 bool IsLittleEndian
) {
1260 DIELoc
*Loc
= nullptr;
1261 DIEBlock
*Block
= nullptr;
1262 if (AttrSpec
.Form
== dwarf::DW_FORM_exprloc
) {
1263 Loc
= new (DIEAlloc
) DIELoc
;
1264 Linker
.DIELocs
.push_back(Loc
);
1266 Block
= new (DIEAlloc
) DIEBlock
;
1267 Linker
.DIEBlocks
.push_back(Block
);
1269 Attr
= Loc
? static_cast<DIEValueList
*>(Loc
)
1270 : static_cast<DIEValueList
*>(Block
);
1272 DWARFUnit
&OrigUnit
= Unit
.getOrigUnit();
1273 // If the block is a DWARF Expression, clone it into the temporary
1274 // buffer using cloneExpression(), otherwise copy the data directly.
1275 SmallVector
<uint8_t, 32> Buffer
;
1276 ArrayRef
<uint8_t> Bytes
= *Val
.getAsBlock();
1277 if (DWARFAttribute::mayHaveLocationExpr(AttrSpec
.Attr
) &&
1278 (Val
.isFormClass(DWARFFormValue::FC_Block
) ||
1279 Val
.isFormClass(DWARFFormValue::FC_Exprloc
))) {
1280 DataExtractor
Data(StringRef((const char *)Bytes
.data(), Bytes
.size()),
1281 IsLittleEndian
, OrigUnit
.getAddressByteSize());
1282 DWARFExpression
Expr(Data
, OrigUnit
.getAddressByteSize(),
1283 OrigUnit
.getFormParams().Format
);
1284 cloneExpression(Data
, Expr
, File
, Unit
, Buffer
,
1285 Unit
.getInfo(InputDIE
).AddrAdjust
, IsLittleEndian
);
1288 for (auto Byte
: Bytes
)
1289 Attr
->addValue(DIEAlloc
, static_cast<dwarf::Attribute
>(0),
1290 dwarf::DW_FORM_data1
, DIEInteger(Byte
));
1292 // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1293 // the DIE class, this "if" could be replaced by
1294 // Attr->setSize(Bytes.size()).
1296 Loc
->setSize(Bytes
.size());
1298 Block
->setSize(Bytes
.size());
1301 Value
= DIEValue(dwarf::Attribute(AttrSpec
.Attr
),
1302 dwarf::Form(AttrSpec
.Form
), Loc
);
1304 // The expression location data might be updated and exceed the original
1305 // size. Check whether the new data fits into the original form.
1306 if ((AttrSpec
.Form
== dwarf::DW_FORM_block1
&&
1307 (Bytes
.size() > UINT8_MAX
)) ||
1308 (AttrSpec
.Form
== dwarf::DW_FORM_block2
&&
1309 (Bytes
.size() > UINT16_MAX
)) ||
1310 (AttrSpec
.Form
== dwarf::DW_FORM_block4
&& (Bytes
.size() > UINT32_MAX
)))
1311 AttrSpec
.Form
= dwarf::DW_FORM_block
;
1313 Value
= DIEValue(dwarf::Attribute(AttrSpec
.Attr
),
1314 dwarf::Form(AttrSpec
.Form
), Block
);
1317 return Die
.addValue(DIEAlloc
, Value
)->sizeOf(OrigUnit
.getFormParams());
1320 unsigned DWARFLinker::DIECloner::cloneAddressAttribute(
1321 DIE
&Die
, const DWARFDie
&InputDIE
, AttributeSpec AttrSpec
,
1322 unsigned AttrSize
, const DWARFFormValue
&Val
, const CompileUnit
&Unit
,
1323 AttributesInfo
&Info
) {
1324 if (AttrSpec
.Attr
== dwarf::DW_AT_low_pc
)
1325 Info
.HasLowPc
= true;
1327 if (LLVM_UNLIKELY(Linker
.Options
.Update
)) {
1328 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1329 dwarf::Form(AttrSpec
.Form
), DIEInteger(Val
.getRawUValue()));
1333 // Cloned Die may have address attributes relocated to a
1334 // totally unrelated value. This can happen:
1335 // - If high_pc is an address (Dwarf version == 2), then it might have been
1336 // relocated to a totally unrelated value (because the end address in the
1337 // object file might be start address of another function which got moved
1338 // independently by the linker).
1339 // - If address relocated in an inline_subprogram that happens at the
1340 // beginning of its inlining function.
1341 // To avoid above cases and to not apply relocation twice (in applyValidRelocs
1342 // and here), read address attribute from InputDIE and apply Info.PCOffset
1345 std::optional
<DWARFFormValue
> AddrAttribute
= InputDIE
.find(AttrSpec
.Attr
);
1347 llvm_unreachable("Cann't find attribute.");
1349 std::optional
<uint64_t> Addr
= AddrAttribute
->getAsAddress();
1351 Linker
.reportWarning("Cann't read address attribute value.", ObjFile
);
1355 if (InputDIE
.getTag() == dwarf::DW_TAG_compile_unit
&&
1356 AttrSpec
.Attr
== dwarf::DW_AT_low_pc
) {
1357 if (std::optional
<uint64_t> LowPC
= Unit
.getLowPc())
1361 } else if (InputDIE
.getTag() == dwarf::DW_TAG_compile_unit
&&
1362 AttrSpec
.Attr
== dwarf::DW_AT_high_pc
) {
1363 if (uint64_t HighPc
= Unit
.getHighPc())
1368 *Addr
+= Info
.PCOffset
;
1371 if (AttrSpec
.Form
== dwarf::DW_FORM_addr
) {
1372 Die
.addValue(DIEAlloc
, static_cast<dwarf::Attribute
>(AttrSpec
.Attr
),
1373 AttrSpec
.Form
, DIEInteger(*Addr
));
1374 return Unit
.getOrigUnit().getAddressByteSize();
1377 auto AddrIndex
= AddrPool
.getValueIndex(*Addr
);
1380 .addValue(DIEAlloc
, static_cast<dwarf::Attribute
>(AttrSpec
.Attr
),
1381 dwarf::Form::DW_FORM_addrx
, DIEInteger(AddrIndex
))
1382 ->sizeOf(Unit
.getOrigUnit().getFormParams());
1385 unsigned DWARFLinker::DIECloner::cloneScalarAttribute(
1386 DIE
&Die
, const DWARFDie
&InputDIE
, const DWARFFile
&File
,
1387 CompileUnit
&Unit
, AttributeSpec AttrSpec
, const DWARFFormValue
&Val
,
1388 unsigned AttrSize
, AttributesInfo
&Info
) {
1391 // Check for the offset to the macro table. If offset is incorrect then we
1392 // need to remove the attribute.
1393 if (AttrSpec
.Attr
== dwarf::DW_AT_macro_info
) {
1394 if (std::optional
<uint64_t> Offset
= Val
.getAsSectionOffset()) {
1395 const DWARFDebugMacro
*Macro
= File
.Dwarf
->getDebugMacinfo();
1396 if (Macro
== nullptr || !Macro
->hasEntryForOffset(*Offset
))
1401 if (AttrSpec
.Attr
== dwarf::DW_AT_macros
) {
1402 if (std::optional
<uint64_t> Offset
= Val
.getAsSectionOffset()) {
1403 const DWARFDebugMacro
*Macro
= File
.Dwarf
->getDebugMacro();
1404 if (Macro
== nullptr || !Macro
->hasEntryForOffset(*Offset
))
1409 if (AttrSpec
.Attr
== dwarf::DW_AT_str_offsets_base
) {
1410 // DWARFLinker generates common .debug_str_offsets table used for all
1411 // compile units. The offset to the common .debug_str_offsets table is 8 on
1413 Info
.AttrStrOffsetBaseSeen
= true;
1415 .addValue(DIEAlloc
, dwarf::DW_AT_str_offsets_base
,
1416 dwarf::DW_FORM_sec_offset
, DIEInteger(8))
1417 ->sizeOf(Unit
.getOrigUnit().getFormParams());
1420 if (LLVM_UNLIKELY(Linker
.Options
.Update
)) {
1421 if (auto OptionalValue
= Val
.getAsUnsignedConstant())
1422 Value
= *OptionalValue
;
1423 else if (auto OptionalValue
= Val
.getAsSignedConstant())
1424 Value
= *OptionalValue
;
1425 else if (auto OptionalValue
= Val
.getAsSectionOffset())
1426 Value
= *OptionalValue
;
1428 Linker
.reportWarning(
1429 "Unsupported scalar attribute form. Dropping attribute.", File
,
1433 if (AttrSpec
.Attr
== dwarf::DW_AT_declaration
&& Value
)
1434 Info
.IsDeclaration
= true;
1436 if (AttrSpec
.Form
== dwarf::DW_FORM_loclistx
)
1437 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1438 dwarf::Form(AttrSpec
.Form
), DIELocList(Value
));
1440 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1441 dwarf::Form(AttrSpec
.Form
), DIEInteger(Value
));
1445 [[maybe_unused
]] dwarf::Form OriginalForm
= AttrSpec
.Form
;
1446 if (AttrSpec
.Form
== dwarf::DW_FORM_rnglistx
) {
1447 // DWARFLinker does not generate .debug_addr table. Thus we need to change
1448 // all "addrx" related forms to "addr" version. Change DW_FORM_rnglistx
1449 // to DW_FORM_sec_offset here.
1450 std::optional
<uint64_t> Index
= Val
.getAsSectionOffset();
1452 Linker
.reportWarning("Cannot read the attribute. Dropping.", File
,
1456 std::optional
<uint64_t> Offset
=
1457 Unit
.getOrigUnit().getRnglistOffset(*Index
);
1459 Linker
.reportWarning("Cannot read the attribute. Dropping.", File
,
1465 AttrSpec
.Form
= dwarf::DW_FORM_sec_offset
;
1466 AttrSize
= Unit
.getOrigUnit().getFormParams().getDwarfOffsetByteSize();
1467 } else if (AttrSpec
.Form
== dwarf::DW_FORM_loclistx
) {
1468 // DWARFLinker does not generate .debug_addr table. Thus we need to change
1469 // all "addrx" related forms to "addr" version. Change DW_FORM_loclistx
1470 // to DW_FORM_sec_offset here.
1471 std::optional
<uint64_t> Index
= Val
.getAsSectionOffset();
1473 Linker
.reportWarning("Cannot read the attribute. Dropping.", File
,
1477 std::optional
<uint64_t> Offset
=
1478 Unit
.getOrigUnit().getLoclistOffset(*Index
);
1480 Linker
.reportWarning("Cannot read the attribute. Dropping.", File
,
1486 AttrSpec
.Form
= dwarf::DW_FORM_sec_offset
;
1487 AttrSize
= Unit
.getOrigUnit().getFormParams().getDwarfOffsetByteSize();
1488 } else if (AttrSpec
.Attr
== dwarf::DW_AT_high_pc
&&
1489 Die
.getTag() == dwarf::DW_TAG_compile_unit
) {
1490 std::optional
<uint64_t> LowPC
= Unit
.getLowPc();
1493 // Dwarf >= 4 high_pc is an size, not an address.
1494 Value
= Unit
.getHighPc() - *LowPC
;
1495 } else if (AttrSpec
.Form
== dwarf::DW_FORM_sec_offset
)
1496 Value
= *Val
.getAsSectionOffset();
1497 else if (AttrSpec
.Form
== dwarf::DW_FORM_sdata
)
1498 Value
= *Val
.getAsSignedConstant();
1499 else if (auto OptionalValue
= Val
.getAsUnsignedConstant())
1500 Value
= *OptionalValue
;
1502 Linker
.reportWarning(
1503 "Unsupported scalar attribute form. Dropping attribute.", File
,
1508 DIE::value_iterator Patch
=
1509 Die
.addValue(DIEAlloc
, dwarf::Attribute(AttrSpec
.Attr
),
1510 dwarf::Form(AttrSpec
.Form
), DIEInteger(Value
));
1511 if (AttrSpec
.Attr
== dwarf::DW_AT_ranges
||
1512 AttrSpec
.Attr
== dwarf::DW_AT_start_scope
) {
1513 Unit
.noteRangeAttribute(Die
, Patch
);
1514 Info
.HasRanges
= true;
1515 } else if (DWARFAttribute::mayHaveLocationList(AttrSpec
.Attr
) &&
1516 dwarf::doesFormBelongToClass(AttrSpec
.Form
,
1517 DWARFFormValue::FC_SectionOffset
,
1518 Unit
.getOrigUnit().getVersion())) {
1520 CompileUnit::DIEInfo
&LocationDieInfo
= Unit
.getInfo(InputDIE
);
1521 Unit
.noteLocationAttribute({Patch
, LocationDieInfo
.InDebugMap
1522 ? LocationDieInfo
.AddrAdjust
1524 } else if (AttrSpec
.Attr
== dwarf::DW_AT_declaration
&& Value
)
1525 Info
.IsDeclaration
= true;
1527 // check that all dwarf::DW_FORM_rnglistx are handled previously.
1528 assert((Info
.HasRanges
|| (OriginalForm
!= dwarf::DW_FORM_rnglistx
)) &&
1529 "Unhandled DW_FORM_rnglistx attribute");
1534 /// Clone \p InputDIE's attribute described by \p AttrSpec with
1535 /// value \p Val, and add it to \p Die.
1536 /// \returns the size of the cloned attribute.
1537 unsigned DWARFLinker::DIECloner::cloneAttribute(
1538 DIE
&Die
, const DWARFDie
&InputDIE
, const DWARFFile
&File
,
1539 CompileUnit
&Unit
, const DWARFFormValue
&Val
, const AttributeSpec AttrSpec
,
1540 unsigned AttrSize
, AttributesInfo
&Info
, bool IsLittleEndian
) {
1541 const DWARFUnit
&U
= Unit
.getOrigUnit();
1543 switch (AttrSpec
.Form
) {
1544 case dwarf::DW_FORM_strp
:
1545 case dwarf::DW_FORM_line_strp
:
1546 case dwarf::DW_FORM_string
:
1547 case dwarf::DW_FORM_strx
:
1548 case dwarf::DW_FORM_strx1
:
1549 case dwarf::DW_FORM_strx2
:
1550 case dwarf::DW_FORM_strx3
:
1551 case dwarf::DW_FORM_strx4
:
1552 return cloneStringAttribute(Die
, AttrSpec
, Val
, U
, Info
);
1553 case dwarf::DW_FORM_ref_addr
:
1554 case dwarf::DW_FORM_ref1
:
1555 case dwarf::DW_FORM_ref2
:
1556 case dwarf::DW_FORM_ref4
:
1557 case dwarf::DW_FORM_ref8
:
1558 return cloneDieReferenceAttribute(Die
, InputDIE
, AttrSpec
, AttrSize
, Val
,
1560 case dwarf::DW_FORM_block
:
1561 case dwarf::DW_FORM_block1
:
1562 case dwarf::DW_FORM_block2
:
1563 case dwarf::DW_FORM_block4
:
1564 case dwarf::DW_FORM_exprloc
:
1565 return cloneBlockAttribute(Die
, InputDIE
, File
, Unit
, AttrSpec
, Val
,
1567 case dwarf::DW_FORM_addr
:
1568 case dwarf::DW_FORM_addrx
:
1569 case dwarf::DW_FORM_addrx1
:
1570 case dwarf::DW_FORM_addrx2
:
1571 case dwarf::DW_FORM_addrx3
:
1572 case dwarf::DW_FORM_addrx4
:
1573 return cloneAddressAttribute(Die
, InputDIE
, AttrSpec
, AttrSize
, Val
, Unit
,
1575 case dwarf::DW_FORM_data1
:
1576 case dwarf::DW_FORM_data2
:
1577 case dwarf::DW_FORM_data4
:
1578 case dwarf::DW_FORM_data8
:
1579 case dwarf::DW_FORM_udata
:
1580 case dwarf::DW_FORM_sdata
:
1581 case dwarf::DW_FORM_sec_offset
:
1582 case dwarf::DW_FORM_flag
:
1583 case dwarf::DW_FORM_flag_present
:
1584 case dwarf::DW_FORM_rnglistx
:
1585 case dwarf::DW_FORM_loclistx
:
1586 case dwarf::DW_FORM_implicit_const
:
1587 return cloneScalarAttribute(Die
, InputDIE
, File
, Unit
, AttrSpec
, Val
,
1590 Linker
.reportWarning("Unsupported attribute form " +
1591 dwarf::FormEncodingString(AttrSpec
.Form
) +
1592 " in cloneAttribute. Dropping.",
1599 void DWARFLinker::DIECloner::addObjCAccelerator(CompileUnit
&Unit
,
1601 DwarfStringPoolEntryRef Name
,
1602 OffsetsStringPool
&StringPool
,
1603 bool SkipPubSection
) {
1604 std::optional
<ObjCSelectorNames
> Names
=
1605 getObjCNamesIfSelector(Name
.getString());
1608 Unit
.addNameAccelerator(Die
, StringPool
.getEntry(Names
->Selector
),
1610 Unit
.addObjCAccelerator(Die
, StringPool
.getEntry(Names
->ClassName
),
1612 if (Names
->ClassNameNoCategory
)
1613 Unit
.addObjCAccelerator(
1614 Die
, StringPool
.getEntry(*Names
->ClassNameNoCategory
), SkipPubSection
);
1615 if (Names
->MethodNameNoCategory
)
1616 Unit
.addNameAccelerator(
1617 Die
, StringPool
.getEntry(*Names
->MethodNameNoCategory
), SkipPubSection
);
1621 shouldSkipAttribute(bool Update
,
1622 DWARFAbbreviationDeclaration::AttributeSpec AttrSpec
,
1624 switch (AttrSpec
.Attr
) {
1627 case dwarf::DW_AT_low_pc
:
1628 case dwarf::DW_AT_high_pc
:
1629 case dwarf::DW_AT_ranges
:
1630 return !Update
&& SkipPC
;
1631 case dwarf::DW_AT_rnglists_base
:
1632 // In case !Update the .debug_addr table is not generated/preserved.
1633 // Thus instead of DW_FORM_rnglistx the DW_FORM_sec_offset is used.
1634 // Since DW_AT_rnglists_base is used for only DW_FORM_rnglistx the
1635 // DW_AT_rnglists_base is removed.
1637 case dwarf::DW_AT_loclists_base
:
1638 // In case !Update the .debug_addr table is not generated/preserved.
1639 // Thus instead of DW_FORM_loclistx the DW_FORM_sec_offset is used.
1640 // Since DW_AT_loclists_base is used for only DW_FORM_loclistx the
1641 // DW_AT_loclists_base is removed.
1643 case dwarf::DW_AT_location
:
1644 case dwarf::DW_AT_frame_base
:
1645 return !Update
&& SkipPC
;
1649 struct AttributeLinkedOffsetFixup
{
1650 int64_t LinkedOffsetFixupVal
;
1651 uint64_t InputAttrStartOffset
;
1652 uint64_t InputAttrEndOffset
;
1655 DIE
*DWARFLinker::DIECloner::cloneDIE(const DWARFDie
&InputDIE
,
1656 const DWARFFile
&File
, CompileUnit
&Unit
,
1657 int64_t PCOffset
, uint32_t OutOffset
,
1658 unsigned Flags
, bool IsLittleEndian
,
1660 DWARFUnit
&U
= Unit
.getOrigUnit();
1661 unsigned Idx
= U
.getDIEIndex(InputDIE
);
1662 CompileUnit::DIEInfo
&Info
= Unit
.getInfo(Idx
);
1664 // Should the DIE appear in the output?
1665 if (!Unit
.getInfo(Idx
).Keep
)
1668 uint64_t Offset
= InputDIE
.getOffset();
1669 assert(!(Die
&& Info
.Clone
) && "Can't supply a DIE and a cloned DIE");
1671 // The DIE might have been already created by a forward reference
1672 // (see cloneDieReferenceAttribute()).
1674 Info
.Clone
= DIE::get(DIEAlloc
, dwarf::Tag(InputDIE
.getTag()));
1678 assert(Die
->getTag() == InputDIE
.getTag());
1679 Die
->setOffset(OutOffset
);
1680 if (isODRCanonicalCandidate(InputDIE
, Unit
) && Info
.Ctxt
&&
1681 (Info
.Ctxt
->getCanonicalDIEOffset() == 0)) {
1682 if (!Info
.Ctxt
->hasCanonicalDIE())
1683 Info
.Ctxt
->setHasCanonicalDIE();
1684 // We are about to emit a DIE that is the root of its own valid
1685 // DeclContext tree. Make the current offset the canonical offset
1686 // for this context.
1687 Info
.Ctxt
->setCanonicalDIEOffset(OutOffset
+ Unit
.getStartOffset());
1690 // Extract and clone every attribute.
1691 DWARFDataExtractor Data
= U
.getDebugInfoExtractor();
1692 // Point to the next DIE (generally there is always at least a NULL
1693 // entry after the current one). If this is a lone
1694 // DW_TAG_compile_unit without any children, point to the next unit.
1695 uint64_t NextOffset
= (Idx
+ 1 < U
.getNumDIEs())
1696 ? U
.getDIEAtIndex(Idx
+ 1).getOffset()
1697 : U
.getNextUnitOffset();
1698 AttributesInfo AttrInfo
;
1700 // We could copy the data only if we need to apply a relocation to it. After
1701 // testing, it seems there is no performance downside to doing the copy
1702 // unconditionally, and it makes the code simpler.
1703 SmallString
<40> DIECopy(Data
.getData().substr(Offset
, NextOffset
- Offset
));
1705 DWARFDataExtractor(DIECopy
, Data
.isLittleEndian(), Data
.getAddressSize());
1707 // Modify the copy with relocated addresses.
1708 ObjFile
.Addresses
->applyValidRelocs(DIECopy
, Offset
, Data
.isLittleEndian());
1710 // Reset the Offset to 0 as we will be working on the local copy of
1714 const auto *Abbrev
= InputDIE
.getAbbreviationDeclarationPtr();
1715 Offset
+= getULEB128Size(Abbrev
->getCode());
1717 // We are entering a subprogram. Get and propagate the PCOffset.
1718 if (Die
->getTag() == dwarf::DW_TAG_subprogram
)
1719 PCOffset
= Info
.AddrAdjust
;
1720 AttrInfo
.PCOffset
= PCOffset
;
1722 if (Abbrev
->getTag() == dwarf::DW_TAG_subprogram
) {
1723 Flags
|= TF_InFunctionScope
;
1724 if (!Info
.InDebugMap
&& LLVM_LIKELY(!Update
))
1726 } else if (Abbrev
->getTag() == dwarf::DW_TAG_variable
) {
1727 // Function-local globals could be in the debug map even when the function
1728 // is not, e.g., inlined functions.
1729 if ((Flags
& TF_InFunctionScope
) && Info
.InDebugMap
)
1730 Flags
&= ~TF_SkipPC
;
1731 // Location expressions referencing an address which is not in debug map
1732 // should be deleted.
1733 else if (!Info
.InDebugMap
&& Info
.HasLocationExpressionAddr
&&
1734 LLVM_LIKELY(!Update
))
1738 std::optional
<StringRef
> LibraryInstallName
=
1739 ObjFile
.Addresses
->getLibraryInstallName();
1740 SmallVector
<AttributeLinkedOffsetFixup
> AttributesFixups
;
1741 for (const auto &AttrSpec
: Abbrev
->attributes()) {
1742 if (shouldSkipAttribute(Update
, AttrSpec
, Flags
& TF_SkipPC
)) {
1743 DWARFFormValue::skipValue(AttrSpec
.Form
, Data
, &Offset
,
1748 AttributeLinkedOffsetFixup CurAttrFixup
;
1749 CurAttrFixup
.InputAttrStartOffset
= InputDIE
.getOffset() + Offset
;
1750 CurAttrFixup
.LinkedOffsetFixupVal
=
1751 Unit
.getStartOffset() + OutOffset
- CurAttrFixup
.InputAttrStartOffset
;
1753 DWARFFormValue Val
= AttrSpec
.getFormValue();
1754 uint64_t AttrSize
= Offset
;
1755 Val
.extractValue(Data
, &Offset
, U
.getFormParams(), &U
);
1756 CurAttrFixup
.InputAttrEndOffset
= InputDIE
.getOffset() + Offset
;
1757 AttrSize
= Offset
- AttrSize
;
1759 uint64_t FinalAttrSize
=
1760 cloneAttribute(*Die
, InputDIE
, File
, Unit
, Val
, AttrSpec
, AttrSize
,
1761 AttrInfo
, IsLittleEndian
);
1762 if (FinalAttrSize
!= 0 && ObjFile
.Addresses
->needToSaveValidRelocs())
1763 AttributesFixups
.push_back(CurAttrFixup
);
1765 OutOffset
+= FinalAttrSize
;
1768 uint16_t Tag
= InputDIE
.getTag();
1769 // Add the DW_AT_APPLE_origin attribute to Compile Unit die if we have
1770 // an install name and the DWARF doesn't have the attribute yet.
1771 const bool NeedsAppleOrigin
= (Tag
== dwarf::DW_TAG_compile_unit
) &&
1772 LibraryInstallName
.has_value() &&
1773 !AttrInfo
.HasAppleOrigin
;
1774 if (NeedsAppleOrigin
) {
1775 auto StringEntry
= DebugStrPool
.getEntry(LibraryInstallName
.value());
1776 Die
->addValue(DIEAlloc
, dwarf::Attribute(dwarf::DW_AT_APPLE_origin
),
1777 dwarf::DW_FORM_strp
, DIEInteger(StringEntry
.getOffset()));
1778 AttrInfo
.Name
= StringEntry
;
1782 // Look for accelerator entries.
1783 // FIXME: This is slightly wrong. An inline_subroutine without a
1784 // low_pc, but with AT_ranges might be interesting to get into the
1785 // accelerator tables too. For now stick with dsymutil's behavior.
1786 if ((Info
.InDebugMap
|| AttrInfo
.HasLowPc
|| AttrInfo
.HasRanges
) &&
1787 Tag
!= dwarf::DW_TAG_compile_unit
&&
1788 getDIENames(InputDIE
, AttrInfo
, DebugStrPool
,
1789 Tag
!= dwarf::DW_TAG_inlined_subroutine
)) {
1790 if (AttrInfo
.MangledName
&& AttrInfo
.MangledName
!= AttrInfo
.Name
)
1791 Unit
.addNameAccelerator(Die
, AttrInfo
.MangledName
,
1792 Tag
== dwarf::DW_TAG_inlined_subroutine
);
1793 if (AttrInfo
.Name
) {
1794 if (AttrInfo
.NameWithoutTemplate
)
1795 Unit
.addNameAccelerator(Die
, AttrInfo
.NameWithoutTemplate
,
1796 /* SkipPubSection */ true);
1797 Unit
.addNameAccelerator(Die
, AttrInfo
.Name
,
1798 Tag
== dwarf::DW_TAG_inlined_subroutine
);
1801 addObjCAccelerator(Unit
, Die
, AttrInfo
.Name
, DebugStrPool
,
1802 /* SkipPubSection =*/true);
1804 } else if (Tag
== dwarf::DW_TAG_namespace
) {
1806 AttrInfo
.Name
= DebugStrPool
.getEntry("(anonymous namespace)");
1807 Unit
.addNamespaceAccelerator(Die
, AttrInfo
.Name
);
1808 } else if (Tag
== dwarf::DW_TAG_imported_declaration
&& AttrInfo
.Name
) {
1809 Unit
.addNamespaceAccelerator(Die
, AttrInfo
.Name
);
1810 } else if (isTypeTag(Tag
) && !AttrInfo
.IsDeclaration
&&
1811 getDIENames(InputDIE
, AttrInfo
, DebugStrPool
) && AttrInfo
.Name
&&
1812 AttrInfo
.Name
.getString()[0]) {
1813 uint32_t Hash
= hashFullyQualifiedName(InputDIE
, Unit
, File
);
1814 uint64_t RuntimeLang
=
1815 dwarf::toUnsigned(InputDIE
.find(dwarf::DW_AT_APPLE_runtime_class
))
1817 bool ObjCClassIsImplementation
=
1818 (RuntimeLang
== dwarf::DW_LANG_ObjC
||
1819 RuntimeLang
== dwarf::DW_LANG_ObjC_plus_plus
) &&
1820 dwarf::toUnsigned(InputDIE
.find(dwarf::DW_AT_APPLE_objc_complete_type
))
1822 Unit
.addTypeAccelerator(Die
, AttrInfo
.Name
, ObjCClassIsImplementation
,
1826 // Determine whether there are any children that we want to keep.
1827 bool HasChildren
= false;
1828 for (auto Child
: InputDIE
.children()) {
1829 unsigned Idx
= U
.getDIEIndex(Child
);
1830 if (Unit
.getInfo(Idx
).Keep
) {
1836 if (Unit
.getOrigUnit().getVersion() >= 5 && !AttrInfo
.AttrStrOffsetBaseSeen
&&
1837 Die
->getTag() == dwarf::DW_TAG_compile_unit
) {
1838 // No DW_AT_str_offsets_base seen, add it to the DIE.
1839 Die
->addValue(DIEAlloc
, dwarf::DW_AT_str_offsets_base
,
1840 dwarf::DW_FORM_sec_offset
, DIEInteger(8));
1844 DIEAbbrev NewAbbrev
= Die
->generateAbbrev();
1846 NewAbbrev
.setChildrenFlag(dwarf::DW_CHILDREN_yes
);
1847 // Assign a permanent abbrev number
1848 Linker
.assignAbbrev(NewAbbrev
);
1849 Die
->setAbbrevNumber(NewAbbrev
.getNumber());
1851 uint64_t AbbrevNumberSize
= getULEB128Size(Die
->getAbbrevNumber());
1853 // Add the size of the abbreviation number to the output offset.
1854 OutOffset
+= AbbrevNumberSize
;
1856 // Update fixups with the size of the abbreviation number
1857 for (AttributeLinkedOffsetFixup
&F
: AttributesFixups
)
1858 F
.LinkedOffsetFixupVal
+= AbbrevNumberSize
;
1860 for (AttributeLinkedOffsetFixup
&F
: AttributesFixups
)
1861 ObjFile
.Addresses
->updateAndSaveValidRelocs(
1862 Unit
.getOrigUnit().getVersion() >= 5, Unit
.getOrigUnit().getOffset(),
1863 F
.LinkedOffsetFixupVal
, F
.InputAttrStartOffset
, F
.InputAttrEndOffset
);
1867 Die
->setSize(OutOffset
- Die
->getOffset());
1871 // Recursively clone children.
1872 for (auto Child
: InputDIE
.children()) {
1873 if (DIE
*Clone
= cloneDIE(Child
, File
, Unit
, PCOffset
, OutOffset
, Flags
,
1875 Die
->addChild(Clone
);
1876 OutOffset
= Clone
->getOffset() + Clone
->getSize();
1880 // Account for the end of children marker.
1881 OutOffset
+= sizeof(int8_t);
1883 Die
->setSize(OutOffset
- Die
->getOffset());
1887 /// Patch the input object file relevant debug_ranges or debug_rnglists
1888 /// entries and emit them in the output file. Update the relevant attributes
1889 /// to point at the new entries.
1890 void DWARFLinker::generateUnitRanges(CompileUnit
&Unit
, const DWARFFile
&File
,
1891 DebugDieValuePool
&AddrPool
) const {
1892 if (LLVM_UNLIKELY(Options
.Update
))
1895 const auto &FunctionRanges
= Unit
.getFunctionRanges();
1897 // Build set of linked address ranges for unit function ranges.
1898 AddressRanges LinkedFunctionRanges
;
1899 for (const AddressRangeValuePair
&Range
: FunctionRanges
)
1900 LinkedFunctionRanges
.insert(
1901 {Range
.Range
.start() + Range
.Value
, Range
.Range
.end() + Range
.Value
});
1903 // Emit LinkedFunctionRanges into .debug_aranges
1904 if (!LinkedFunctionRanges
.empty())
1905 TheDwarfEmitter
->emitDwarfDebugArangesTable(Unit
, LinkedFunctionRanges
);
1907 RngListAttributesTy AllRngListAttributes
= Unit
.getRangesAttributes();
1908 std::optional
<PatchLocation
> UnitRngListAttribute
=
1909 Unit
.getUnitRangesAttribute();
1911 if (!AllRngListAttributes
.empty() || UnitRngListAttribute
) {
1912 std::optional
<AddressRangeValuePair
> CachedRange
;
1913 MCSymbol
*EndLabel
= TheDwarfEmitter
->emitDwarfDebugRangeListHeader(Unit
);
1915 // Read original address ranges, apply relocation value, emit linked address
1917 for (PatchLocation
&AttributePatch
: AllRngListAttributes
) {
1918 // Get ranges from the source DWARF corresponding to the current
1920 AddressRanges LinkedRanges
;
1921 if (Expected
<DWARFAddressRangesVector
> OriginalRanges
=
1922 Unit
.getOrigUnit().findRnglistFromOffset(AttributePatch
.get())) {
1923 // Apply relocation adjustment.
1924 for (const auto &Range
: *OriginalRanges
) {
1925 if (!CachedRange
|| !CachedRange
->Range
.contains(Range
.LowPC
))
1926 CachedRange
= FunctionRanges
.getRangeThatContains(Range
.LowPC
);
1928 // All range entries should lie in the function range.
1930 reportWarning("inconsistent range data.", File
);
1934 // Store range for emiting.
1935 LinkedRanges
.insert({Range
.LowPC
+ CachedRange
->Value
,
1936 Range
.HighPC
+ CachedRange
->Value
});
1939 llvm::consumeError(OriginalRanges
.takeError());
1940 reportWarning("invalid range list ignored.", File
);
1943 // Emit linked ranges.
1944 TheDwarfEmitter
->emitDwarfDebugRangeListFragment(
1945 Unit
, LinkedRanges
, AttributePatch
, AddrPool
);
1948 // Emit ranges for Unit AT_ranges attribute.
1949 if (UnitRngListAttribute
.has_value())
1950 TheDwarfEmitter
->emitDwarfDebugRangeListFragment(
1951 Unit
, LinkedFunctionRanges
, *UnitRngListAttribute
, AddrPool
);
1953 // Emit ranges footer.
1954 TheDwarfEmitter
->emitDwarfDebugRangeListFooter(Unit
, EndLabel
);
1958 void DWARFLinker::DIECloner::generateUnitLocations(
1959 CompileUnit
&Unit
, const DWARFFile
&File
,
1960 ExpressionHandlerRef ExprHandler
) {
1961 if (LLVM_UNLIKELY(Linker
.Options
.Update
))
1964 const LocListAttributesTy
&AllLocListAttributes
=
1965 Unit
.getLocationAttributes();
1967 if (AllLocListAttributes
.empty())
1970 // Emit locations list table header.
1971 MCSymbol
*EndLabel
= Emitter
->emitDwarfDebugLocListHeader(Unit
);
1973 for (auto &CurLocAttr
: AllLocListAttributes
) {
1974 // Get location expressions vector corresponding to the current attribute
1975 // from the source DWARF.
1976 Expected
<DWARFLocationExpressionsVector
> OriginalLocations
=
1977 Unit
.getOrigUnit().findLoclistFromOffset(CurLocAttr
.get());
1979 if (!OriginalLocations
) {
1980 llvm::consumeError(OriginalLocations
.takeError());
1981 Linker
.reportWarning("Invalid location attribute ignored.", File
);
1985 DWARFLocationExpressionsVector LinkedLocationExpressions
;
1986 for (DWARFLocationExpression
&CurExpression
: *OriginalLocations
) {
1987 DWARFLocationExpression LinkedExpression
;
1989 if (CurExpression
.Range
) {
1990 // Relocate address range.
1991 LinkedExpression
.Range
= {
1992 CurExpression
.Range
->LowPC
+ CurLocAttr
.RelocAdjustment
,
1993 CurExpression
.Range
->HighPC
+ CurLocAttr
.RelocAdjustment
};
1996 // Clone expression.
1997 LinkedExpression
.Expr
.reserve(CurExpression
.Expr
.size());
1998 ExprHandler(CurExpression
.Expr
, LinkedExpression
.Expr
,
1999 CurLocAttr
.RelocAdjustment
);
2001 LinkedLocationExpressions
.push_back(LinkedExpression
);
2004 // Emit locations list table fragment corresponding to the CurLocAttr.
2005 Emitter
->emitDwarfDebugLocListFragment(Unit
, LinkedLocationExpressions
,
2006 CurLocAttr
, AddrPool
);
2009 // Emit locations list table footer.
2010 Emitter
->emitDwarfDebugLocListFooter(Unit
, EndLabel
);
2013 static void patchAddrBase(DIE
&Die
, DIEInteger Offset
) {
2014 for (auto &V
: Die
.values())
2015 if (V
.getAttribute() == dwarf::DW_AT_addr_base
) {
2016 V
= DIEValue(V
.getAttribute(), V
.getForm(), Offset
);
2020 llvm_unreachable("Didn't find a DW_AT_addr_base in cloned DIE!");
2023 void DWARFLinker::DIECloner::emitDebugAddrSection(
2025 const uint16_t DwarfVersion
) const {
2027 if (LLVM_UNLIKELY(Linker
.Options
.Update
))
2030 if (DwarfVersion
< 5)
2033 if (AddrPool
.DieValues
.empty())
2036 MCSymbol
*EndLabel
= Emitter
->emitDwarfDebugAddrsHeader(Unit
);
2037 patchAddrBase(*Unit
.getOutputUnitDIE(),
2038 DIEInteger(Emitter
->getDebugAddrSectionSize()));
2039 Emitter
->emitDwarfDebugAddrs(AddrPool
.DieValues
,
2040 Unit
.getOrigUnit().getAddressByteSize());
2041 Emitter
->emitDwarfDebugAddrsFooter(Unit
, EndLabel
);
2044 /// Insert the new line info sequence \p Seq into the current
2045 /// set of already linked line info \p Rows.
2046 static void insertLineSequence(std::vector
<DWARFDebugLine::Row
> &Seq
,
2047 std::vector
<DWARFDebugLine::Row
> &Rows
) {
2051 if (!Rows
.empty() && Rows
.back().Address
< Seq
.front().Address
) {
2052 llvm::append_range(Rows
, Seq
);
2057 object::SectionedAddress Front
= Seq
.front().Address
;
2058 auto InsertPoint
= partition_point(
2059 Rows
, [=](const DWARFDebugLine::Row
&O
) { return O
.Address
< Front
; });
2061 // FIXME: this only removes the unneeded end_sequence if the
2062 // sequences have been inserted in order. Using a global sort like
2063 // described in generateLineTableForUnit() and delaying the end_sequene
2064 // elimination to emitLineTableForUnit() we can get rid of all of them.
2065 if (InsertPoint
!= Rows
.end() && InsertPoint
->Address
== Front
&&
2066 InsertPoint
->EndSequence
) {
2067 *InsertPoint
= Seq
.front();
2068 Rows
.insert(InsertPoint
+ 1, Seq
.begin() + 1, Seq
.end());
2070 Rows
.insert(InsertPoint
, Seq
.begin(), Seq
.end());
2076 static void patchStmtList(DIE
&Die
, DIEInteger Offset
) {
2077 for (auto &V
: Die
.values())
2078 if (V
.getAttribute() == dwarf::DW_AT_stmt_list
) {
2079 V
= DIEValue(V
.getAttribute(), V
.getForm(), Offset
);
2083 llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
2086 void DWARFLinker::DIECloner::rememberUnitForMacroOffset(CompileUnit
&Unit
) {
2087 DWARFUnit
&OrigUnit
= Unit
.getOrigUnit();
2088 DWARFDie OrigUnitDie
= OrigUnit
.getUnitDIE();
2090 if (std::optional
<uint64_t> MacroAttr
=
2091 dwarf::toSectionOffset(OrigUnitDie
.find(dwarf::DW_AT_macros
))) {
2092 UnitMacroMap
.insert(std::make_pair(*MacroAttr
, &Unit
));
2096 if (std::optional
<uint64_t> MacroAttr
=
2097 dwarf::toSectionOffset(OrigUnitDie
.find(dwarf::DW_AT_macro_info
))) {
2098 UnitMacroMap
.insert(std::make_pair(*MacroAttr
, &Unit
));
2103 void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit
&Unit
) {
2104 if (LLVM_UNLIKELY(Emitter
== nullptr))
2107 // Check whether DW_AT_stmt_list attribute is presented.
2108 DWARFDie CUDie
= Unit
.getOrigUnit().getUnitDIE();
2109 auto StmtList
= dwarf::toSectionOffset(CUDie
.find(dwarf::DW_AT_stmt_list
));
2113 // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
2114 if (auto *OutputDIE
= Unit
.getOutputUnitDIE())
2115 patchStmtList(*OutputDIE
, DIEInteger(Emitter
->getLineSectionSize()));
2117 if (const DWARFDebugLine::LineTable
*LT
=
2118 ObjFile
.Dwarf
->getLineTableForUnit(&Unit
.getOrigUnit())) {
2120 DWARFDebugLine::LineTable LineTable
;
2122 // Set Line Table header.
2123 LineTable
.Prologue
= LT
->Prologue
;
2125 // Set Line Table Rows.
2126 if (Linker
.Options
.Update
) {
2127 LineTable
.Rows
= LT
->Rows
;
2128 // If all the line table contains is a DW_LNE_end_sequence, clear the line
2129 // table rows, it will be inserted again in the DWARFStreamer.
2130 if (LineTable
.Rows
.size() == 1 && LineTable
.Rows
[0].EndSequence
)
2131 LineTable
.Rows
.clear();
2133 LineTable
.Sequences
= LT
->Sequences
;
2135 // This vector is the output line table.
2136 std::vector
<DWARFDebugLine::Row
> NewRows
;
2137 NewRows
.reserve(LT
->Rows
.size());
2139 // Current sequence of rows being extracted, before being inserted
2141 std::vector
<DWARFDebugLine::Row
> Seq
;
2143 const auto &FunctionRanges
= Unit
.getFunctionRanges();
2144 std::optional
<AddressRangeValuePair
> CurrRange
;
2146 // FIXME: This logic is meant to generate exactly the same output as
2147 // Darwin's classic dsymutil. There is a nicer way to implement this
2148 // by simply putting all the relocated line info in NewRows and simply
2149 // sorting NewRows before passing it to emitLineTableForUnit. This
2150 // should be correct as sequences for a function should stay
2151 // together in the sorted output. There are a few corner cases that
2152 // look suspicious though, and that required to implement the logic
2153 // this way. Revisit that once initial validation is finished.
2155 // Iterate over the object file line info and extract the sequences
2156 // that correspond to linked functions.
2157 for (DWARFDebugLine::Row Row
: LT
->Rows
) {
2158 // Check whether we stepped out of the range. The range is
2159 // half-open, but consider accept the end address of the range if
2160 // it is marked as end_sequence in the input (because in that
2161 // case, the relocation offset is accurate and that entry won't
2162 // serve as the start of another function).
2163 if (!CurrRange
|| !CurrRange
->Range
.contains(Row
.Address
.Address
)) {
2164 // We just stepped out of a known range. Insert a end_sequence
2165 // corresponding to the end of the range.
2166 uint64_t StopAddress
=
2167 CurrRange
? CurrRange
->Range
.end() + CurrRange
->Value
: -1ULL;
2168 CurrRange
= FunctionRanges
.getRangeThatContains(Row
.Address
.Address
);
2169 if (StopAddress
!= -1ULL && !Seq
.empty()) {
2170 // Insert end sequence row with the computed end address, but
2171 // the same line as the previous one.
2172 auto NextLine
= Seq
.back();
2173 NextLine
.Address
.Address
= StopAddress
;
2174 NextLine
.EndSequence
= 1;
2175 NextLine
.PrologueEnd
= 0;
2176 NextLine
.BasicBlock
= 0;
2177 NextLine
.EpilogueBegin
= 0;
2178 Seq
.push_back(NextLine
);
2179 insertLineSequence(Seq
, NewRows
);
2186 // Ignore empty sequences.
2187 if (Row
.EndSequence
&& Seq
.empty())
2190 // Relocate row address and add it to the current sequence.
2191 Row
.Address
.Address
+= CurrRange
->Value
;
2192 Seq
.emplace_back(Row
);
2194 if (Row
.EndSequence
)
2195 insertLineSequence(Seq
, NewRows
);
2198 LineTable
.Rows
= std::move(NewRows
);
2201 Emitter
->emitLineTableForUnit(LineTable
, Unit
, DebugStrPool
,
2204 Linker
.reportWarning("Cann't load line table.", ObjFile
);
2207 void DWARFLinker::emitAcceleratorEntriesForUnit(CompileUnit
&Unit
) {
2208 for (AccelTableKind AccelTableKind
: Options
.AccelTables
) {
2209 switch (AccelTableKind
) {
2210 case AccelTableKind::Apple
: {
2212 for (const auto &Namespace
: Unit
.getNamespaces())
2213 AppleNamespaces
.addName(Namespace
.Name
, Namespace
.Die
->getOffset() +
2214 Unit
.getStartOffset());
2216 for (const auto &Pubname
: Unit
.getPubnames())
2217 AppleNames
.addName(Pubname
.Name
,
2218 Pubname
.Die
->getOffset() + Unit
.getStartOffset());
2220 for (const auto &Pubtype
: Unit
.getPubtypes())
2222 Pubtype
.Name
, Pubtype
.Die
->getOffset() + Unit
.getStartOffset(),
2223 Pubtype
.Die
->getTag(),
2224 Pubtype
.ObjcClassImplementation
? dwarf::DW_FLAG_type_implementation
2226 Pubtype
.QualifiedNameHash
);
2228 for (const auto &ObjC
: Unit
.getObjC())
2229 AppleObjc
.addName(ObjC
.Name
,
2230 ObjC
.Die
->getOffset() + Unit
.getStartOffset());
2232 case AccelTableKind::Pub
: {
2233 TheDwarfEmitter
->emitPubNamesForUnit(Unit
);
2234 TheDwarfEmitter
->emitPubTypesForUnit(Unit
);
2236 case AccelTableKind::DebugNames
: {
2237 for (const auto &Namespace
: Unit
.getNamespaces())
2238 DebugNames
.addName(Namespace
.Name
, Namespace
.Die
->getOffset(),
2239 Namespace
.Die
->getTag(), Unit
.getUniqueID());
2240 for (const auto &Pubname
: Unit
.getPubnames())
2241 DebugNames
.addName(Pubname
.Name
, Pubname
.Die
->getOffset(),
2242 Pubname
.Die
->getTag(), Unit
.getUniqueID());
2243 for (const auto &Pubtype
: Unit
.getPubtypes())
2244 DebugNames
.addName(Pubtype
.Name
, Pubtype
.Die
->getOffset(),
2245 Pubtype
.Die
->getTag(), Unit
.getUniqueID());
2251 /// Read the frame info stored in the object, and emit the
2252 /// patched frame descriptions for the resulting file.
2254 /// This is actually pretty easy as the data of the CIEs and FDEs can
2255 /// be considered as black boxes and moved as is. The only thing to do
2256 /// is to patch the addresses in the headers.
2257 void DWARFLinker::patchFrameInfoForObject(LinkContext
&Context
) {
2258 DWARFContext
&OrigDwarf
= *Context
.File
.Dwarf
;
2259 unsigned SrcAddrSize
= OrigDwarf
.getDWARFObj().getAddressSize();
2261 StringRef FrameData
= OrigDwarf
.getDWARFObj().getFrameSection().Data
;
2262 if (FrameData
.empty())
2265 RangesTy AllUnitsRanges
;
2266 for (std::unique_ptr
<CompileUnit
> &Unit
: Context
.CompileUnits
) {
2267 for (auto CurRange
: Unit
->getFunctionRanges())
2268 AllUnitsRanges
.insert(CurRange
.Range
, CurRange
.Value
);
2271 DataExtractor
Data(FrameData
, OrigDwarf
.isLittleEndian(), 0);
2272 uint64_t InputOffset
= 0;
2274 // Store the data of the CIEs defined in this object, keyed by their
2276 DenseMap
<uint64_t, StringRef
> LocalCIES
;
2278 while (Data
.isValidOffset(InputOffset
)) {
2279 uint64_t EntryOffset
= InputOffset
;
2280 uint32_t InitialLength
= Data
.getU32(&InputOffset
);
2281 if (InitialLength
== 0xFFFFFFFF)
2282 return reportWarning("Dwarf64 bits no supported", Context
.File
);
2284 uint32_t CIEId
= Data
.getU32(&InputOffset
);
2285 if (CIEId
== 0xFFFFFFFF) {
2286 // This is a CIE, store it.
2287 StringRef CIEData
= FrameData
.substr(EntryOffset
, InitialLength
+ 4);
2288 LocalCIES
[EntryOffset
] = CIEData
;
2289 // The -4 is to account for the CIEId we just read.
2290 InputOffset
+= InitialLength
- 4;
2294 uint64_t Loc
= Data
.getUnsigned(&InputOffset
, SrcAddrSize
);
2296 // Some compilers seem to emit frame info that doesn't start at
2297 // the function entry point, thus we can't just lookup the address
2298 // in the debug map. Use the AddressInfo's range map to see if the FDE
2299 // describes something that we can relocate.
2300 std::optional
<AddressRangeValuePair
> Range
=
2301 AllUnitsRanges
.getRangeThatContains(Loc
);
2303 // The +4 is to account for the size of the InitialLength field itself.
2304 InputOffset
= EntryOffset
+ InitialLength
+ 4;
2308 // This is an FDE, and we have a mapping.
2309 // Have we already emitted a corresponding CIE?
2310 StringRef CIEData
= LocalCIES
[CIEId
];
2311 if (CIEData
.empty())
2312 return reportWarning("Inconsistent debug_frame content. Dropping.",
2315 // Look if we already emitted a CIE that corresponds to the
2316 // referenced one (the CIE data is the key of that lookup).
2317 auto IteratorInserted
= EmittedCIEs
.insert(
2318 std::make_pair(CIEData
, TheDwarfEmitter
->getFrameSectionSize()));
2319 // If there is no CIE yet for this ID, emit it.
2320 if (IteratorInserted
.second
) {
2321 LastCIEOffset
= TheDwarfEmitter
->getFrameSectionSize();
2322 IteratorInserted
.first
->getValue() = LastCIEOffset
;
2323 TheDwarfEmitter
->emitCIE(CIEData
);
2326 // Emit the FDE with updated address and CIE pointer.
2327 // (4 + AddrSize) is the size of the CIEId + initial_location
2328 // fields that will get reconstructed by emitFDE().
2329 unsigned FDERemainingBytes
= InitialLength
- (4 + SrcAddrSize
);
2330 TheDwarfEmitter
->emitFDE(IteratorInserted
.first
->getValue(), SrcAddrSize
,
2332 FrameData
.substr(InputOffset
, FDERemainingBytes
));
2333 InputOffset
+= FDERemainingBytes
;
2337 uint32_t DWARFLinker::DIECloner::hashFullyQualifiedName(DWARFDie DIE
,
2339 const DWARFFile
&File
,
2340 int ChildRecurseDepth
) {
2341 const char *Name
= nullptr;
2342 DWARFUnit
*OrigUnit
= &U
.getOrigUnit();
2343 CompileUnit
*CU
= &U
;
2344 std::optional
<DWARFFormValue
> Ref
;
2347 if (const char *CurrentName
= DIE
.getName(DINameKind::ShortName
))
2350 if (!(Ref
= DIE
.find(dwarf::DW_AT_specification
)) &&
2351 !(Ref
= DIE
.find(dwarf::DW_AT_abstract_origin
)))
2354 if (!Ref
->isFormClass(DWARFFormValue::FC_Reference
))
2359 Linker
.resolveDIEReference(File
, CompileUnits
, *Ref
, DIE
, RefCU
)) {
2361 OrigUnit
= &RefCU
->getOrigUnit();
2366 unsigned Idx
= OrigUnit
->getDIEIndex(DIE
);
2367 if (!Name
&& DIE
.getTag() == dwarf::DW_TAG_namespace
)
2368 Name
= "(anonymous namespace)";
2370 if (CU
->getInfo(Idx
).ParentIdx
== 0 ||
2371 // FIXME: dsymutil-classic compatibility. Ignore modules.
2372 CU
->getOrigUnit().getDIEAtIndex(CU
->getInfo(Idx
).ParentIdx
).getTag() ==
2373 dwarf::DW_TAG_module
)
2374 return djbHash(Name
? Name
: "", djbHash(ChildRecurseDepth
? "" : "::"));
2376 DWARFDie Die
= OrigUnit
->getDIEAtIndex(CU
->getInfo(Idx
).ParentIdx
);
2379 djbHash((Name
? "::" : ""),
2380 hashFullyQualifiedName(Die
, *CU
, File
, ++ChildRecurseDepth
)));
2383 static uint64_t getDwoId(const DWARFDie
&CUDie
) {
2384 auto DwoId
= dwarf::toUnsigned(
2385 CUDie
.find({dwarf::DW_AT_dwo_id
, dwarf::DW_AT_GNU_dwo_id
}));
2391 static std::string
remapPath(StringRef Path
,
2392 const objectPrefixMap
&ObjectPrefixMap
) {
2393 if (ObjectPrefixMap
.empty())
2396 SmallString
<256> p
= Path
;
2397 for (const auto &Entry
: ObjectPrefixMap
)
2398 if (llvm::sys::path::replace_path_prefix(p
, Entry
.first
, Entry
.second
))
2400 return p
.str().str();
2403 static std::string
getPCMFile(const DWARFDie
&CUDie
,
2404 objectPrefixMap
*ObjectPrefixMap
) {
2405 std::string PCMFile
= dwarf::toString(
2406 CUDie
.find({dwarf::DW_AT_dwo_name
, dwarf::DW_AT_GNU_dwo_name
}), "");
2408 if (PCMFile
.empty())
2411 if (ObjectPrefixMap
)
2412 PCMFile
= remapPath(PCMFile
, *ObjectPrefixMap
);
2417 std::pair
<bool, bool> DWARFLinker::isClangModuleRef(const DWARFDie
&CUDie
,
2418 std::string
&PCMFile
,
2419 LinkContext
&Context
,
2422 if (PCMFile
.empty())
2423 return std::make_pair(false, false);
2425 // Clang module DWARF skeleton CUs abuse this for the path to the module.
2426 uint64_t DwoId
= getDwoId(CUDie
);
2428 std::string Name
= dwarf::toString(CUDie
.find(dwarf::DW_AT_name
), "");
2431 reportWarning("Anonymous module skeleton CU for " + PCMFile
,
2433 return std::make_pair(true, true);
2436 if (!Quiet
&& Options
.Verbose
) {
2437 outs().indent(Indent
);
2438 outs() << "Found clang module reference " << PCMFile
;
2441 auto Cached
= ClangModules
.find(PCMFile
);
2442 if (Cached
!= ClangModules
.end()) {
2443 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2444 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2445 // ASTFileSignatures will change randomly when a module is rebuilt.
2446 if (!Quiet
&& Options
.Verbose
&& (Cached
->second
!= DwoId
))
2447 reportWarning(Twine("hash mismatch: this object file was built against a "
2448 "different version of the module ") +
2451 if (!Quiet
&& Options
.Verbose
)
2452 outs() << " [cached].\n";
2453 return std::make_pair(true, true);
2456 return std::make_pair(true, false);
2459 bool DWARFLinker::registerModuleReference(const DWARFDie
&CUDie
,
2460 LinkContext
&Context
,
2461 objFileLoader Loader
,
2462 CompileUnitHandler OnCUDieLoaded
,
2464 std::string PCMFile
= getPCMFile(CUDie
, Options
.ObjectPrefixMap
);
2465 std::pair
<bool, bool> IsClangModuleRef
=
2466 isClangModuleRef(CUDie
, PCMFile
, Context
, Indent
, false);
2468 if (!IsClangModuleRef
.first
)
2471 if (IsClangModuleRef
.second
)
2474 if (Options
.Verbose
)
2477 // Cyclic dependencies are disallowed by Clang, but we still
2478 // shouldn't run into an infinite loop, so mark it as processed now.
2479 ClangModules
.insert({PCMFile
, getDwoId(CUDie
)});
2481 if (Error E
= loadClangModule(Loader
, CUDie
, PCMFile
, Context
, OnCUDieLoaded
,
2483 consumeError(std::move(E
));
2489 Error
DWARFLinker::loadClangModule(objFileLoader Loader
, const DWARFDie
&CUDie
,
2490 const std::string
&PCMFile
,
2491 LinkContext
&Context
,
2492 CompileUnitHandler OnCUDieLoaded
,
2495 uint64_t DwoId
= getDwoId(CUDie
);
2496 std::string ModuleName
= dwarf::toString(CUDie
.find(dwarf::DW_AT_name
), "");
2498 /// Using a SmallString<0> because loadClangModule() is recursive.
2499 SmallString
<0> Path(Options
.PrependPath
);
2500 if (sys::path::is_relative(PCMFile
))
2501 resolveRelativeObjectPath(Path
, CUDie
);
2502 sys::path::append(Path
, PCMFile
);
2503 // Don't use the cached binary holder because we have no thread-safety
2504 // guarantee and the lifetime is limited.
2506 if (Loader
== nullptr) {
2507 reportError("Could not load clang module: loader is not specified.\n",
2509 return Error::success();
2512 auto ErrOrObj
= Loader(Context
.File
.FileName
, Path
);
2514 return Error::success();
2516 std::unique_ptr
<CompileUnit
> Unit
;
2517 for (const auto &CU
: ErrOrObj
->Dwarf
->compile_units()) {
2519 // Recursively get all modules imported by this one.
2520 auto ChildCUDie
= CU
->getUnitDIE();
2523 if (!registerModuleReference(ChildCUDie
, Context
, Loader
, OnCUDieLoaded
,
2528 ": Clang modules are expected to have exactly 1 compile unit.\n");
2529 reportError(Err
, Context
.File
);
2530 return make_error
<StringError
>(Err
, inconvertibleErrorCode());
2532 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2533 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2534 // ASTFileSignatures will change randomly when a module is rebuilt.
2535 uint64_t PCMDwoId
= getDwoId(ChildCUDie
);
2536 if (PCMDwoId
!= DwoId
) {
2537 if (Options
.Verbose
)
2539 Twine("hash mismatch: this object file was built against a "
2540 "different version of the module ") +
2543 // Update the cache entry with the DwoId of the module loaded from disk.
2544 ClangModules
[PCMFile
] = PCMDwoId
;
2548 Unit
= std::make_unique
<CompileUnit
>(*CU
, UniqueUnitID
++, !Options
.NoODR
,
2554 Context
.ModuleUnits
.emplace_back(RefModuleUnit
{*ErrOrObj
, std::move(Unit
)});
2556 return Error::success();
2559 uint64_t DWARFLinker::DIECloner::cloneAllCompileUnits(
2560 DWARFContext
&DwarfContext
, const DWARFFile
&File
, bool IsLittleEndian
) {
2561 uint64_t OutputDebugInfoSize
=
2562 (Emitter
== nullptr) ? 0 : Emitter
->getDebugInfoSectionSize();
2563 const uint64_t StartOutputDebugInfoSize
= OutputDebugInfoSize
;
2565 for (auto &CurrentUnit
: CompileUnits
) {
2566 const uint16_t DwarfVersion
= CurrentUnit
->getOrigUnit().getVersion();
2567 const uint32_t UnitHeaderSize
= DwarfVersion
>= 5 ? 12 : 11;
2568 auto InputDIE
= CurrentUnit
->getOrigUnit().getUnitDIE();
2569 CurrentUnit
->setStartOffset(OutputDebugInfoSize
);
2571 OutputDebugInfoSize
= CurrentUnit
->computeNextUnitOffset(DwarfVersion
);
2574 if (CurrentUnit
->getInfo(0).Keep
) {
2575 // Clone the InputDIE into your Unit DIE in our compile unit since it
2576 // already has a DIE inside of it.
2577 CurrentUnit
->createOutputDIE();
2578 rememberUnitForMacroOffset(*CurrentUnit
);
2579 cloneDIE(InputDIE
, File
, *CurrentUnit
, 0 /* PC offset */, UnitHeaderSize
,
2580 0, IsLittleEndian
, CurrentUnit
->getOutputUnitDIE());
2583 OutputDebugInfoSize
= CurrentUnit
->computeNextUnitOffset(DwarfVersion
);
2585 if (Emitter
!= nullptr) {
2587 generateLineTableForUnit(*CurrentUnit
);
2589 Linker
.emitAcceleratorEntriesForUnit(*CurrentUnit
);
2591 if (LLVM_UNLIKELY(Linker
.Options
.Update
))
2594 Linker
.generateUnitRanges(*CurrentUnit
, File
, AddrPool
);
2596 auto ProcessExpr
= [&](SmallVectorImpl
<uint8_t> &SrcBytes
,
2597 SmallVectorImpl
<uint8_t> &OutBytes
,
2598 int64_t RelocAdjustment
) {
2599 DWARFUnit
&OrigUnit
= CurrentUnit
->getOrigUnit();
2600 DataExtractor
Data(SrcBytes
, IsLittleEndian
,
2601 OrigUnit
.getAddressByteSize());
2602 cloneExpression(Data
,
2603 DWARFExpression(Data
, OrigUnit
.getAddressByteSize(),
2604 OrigUnit
.getFormParams().Format
),
2605 File
, *CurrentUnit
, OutBytes
, RelocAdjustment
,
2608 generateUnitLocations(*CurrentUnit
, File
, ProcessExpr
);
2609 emitDebugAddrSection(*CurrentUnit
, DwarfVersion
);
2614 if (Emitter
!= nullptr) {
2616 // Emit macro tables.
2617 Emitter
->emitMacroTables(File
.Dwarf
.get(), UnitMacroMap
, DebugStrPool
);
2619 // Emit all the compile unit's debug information.
2620 for (auto &CurrentUnit
: CompileUnits
) {
2621 CurrentUnit
->fixupForwardReferences();
2623 if (!CurrentUnit
->getOutputUnitDIE())
2626 unsigned DwarfVersion
= CurrentUnit
->getOrigUnit().getVersion();
2628 assert(Emitter
->getDebugInfoSectionSize() ==
2629 CurrentUnit
->getStartOffset());
2630 Emitter
->emitCompileUnitHeader(*CurrentUnit
, DwarfVersion
);
2631 Emitter
->emitDIE(*CurrentUnit
->getOutputUnitDIE());
2632 assert(Emitter
->getDebugInfoSectionSize() ==
2633 CurrentUnit
->computeNextUnitOffset(DwarfVersion
));
2637 return OutputDebugInfoSize
- StartOutputDebugInfoSize
;
2640 void DWARFLinker::copyInvariantDebugSection(DWARFContext
&Dwarf
) {
2641 TheDwarfEmitter
->emitSectionContents(Dwarf
.getDWARFObj().getLocSection().Data
,
2643 TheDwarfEmitter
->emitSectionContents(
2644 Dwarf
.getDWARFObj().getRangesSection().Data
, "debug_ranges");
2645 TheDwarfEmitter
->emitSectionContents(
2646 Dwarf
.getDWARFObj().getFrameSection().Data
, "debug_frame");
2647 TheDwarfEmitter
->emitSectionContents(Dwarf
.getDWARFObj().getArangesSection(),
2649 TheDwarfEmitter
->emitSectionContents(
2650 Dwarf
.getDWARFObj().getAddrSection().Data
, "debug_addr");
2651 TheDwarfEmitter
->emitSectionContents(
2652 Dwarf
.getDWARFObj().getRnglistsSection().Data
, "debug_rnglists");
2653 TheDwarfEmitter
->emitSectionContents(
2654 Dwarf
.getDWARFObj().getLoclistsSection().Data
, "debug_loclists");
2657 void DWARFLinker::addObjectFile(DWARFFile
&File
, objFileLoader Loader
,
2658 CompileUnitHandler OnCUDieLoaded
) {
2659 ObjectContexts
.emplace_back(LinkContext(File
));
2661 if (ObjectContexts
.back().File
.Dwarf
) {
2662 for (const std::unique_ptr
<DWARFUnit
> &CU
:
2663 ObjectContexts
.back().File
.Dwarf
->compile_units()) {
2664 DWARFDie CUDie
= CU
->getUnitDIE();
2671 if (!LLVM_UNLIKELY(Options
.Update
))
2672 registerModuleReference(CUDie
, ObjectContexts
.back(), Loader
,
2678 Error
DWARFLinker::link() {
2679 assert((Options
.TargetDWARFVersion
!= 0) &&
2680 "TargetDWARFVersion should be set");
2682 // First populate the data structure we need for each iteration of the
2684 unsigned NumObjects
= ObjectContexts
.size();
2686 // This Dwarf string pool which is used for emission. It must be used
2687 // serially as the order of calling getStringOffset matters for
2689 OffsetsStringPool
DebugStrPool(StringsTranslator
, true);
2690 OffsetsStringPool
DebugLineStrPool(StringsTranslator
, false);
2691 DebugDieValuePool StringOffsetPool
;
2693 // ODR Contexts for the optimize.
2694 DeclContextTree ODRContexts
;
2696 for (LinkContext
&OptContext
: ObjectContexts
) {
2697 if (Options
.Verbose
) {
2698 if (DwarfLinkerClientID
== DwarfLinkerClient::Dsymutil
)
2699 outs() << "DEBUG MAP OBJECT: " << OptContext
.File
.FileName
<< "\n";
2701 outs() << "OBJECT FILE: " << OptContext
.File
.FileName
<< "\n";
2704 if (!OptContext
.File
.Dwarf
)
2707 if (Options
.VerifyInputDWARF
)
2708 verifyInput(OptContext
.File
);
2710 // Look for relocations that correspond to address map entries.
2712 // there was findvalidrelocations previously ... probably we need to gather
2714 if (LLVM_LIKELY(!Options
.Update
) &&
2715 !OptContext
.File
.Addresses
->hasValidRelocs()) {
2716 if (Options
.Verbose
)
2717 outs() << "No valid relocations found. Skipping.\n";
2719 // Set "Skip" flag as a signal to other loops that we should not
2720 // process this iteration.
2721 OptContext
.Skip
= true;
2725 // Setup access to the debug info.
2726 if (!OptContext
.File
.Dwarf
)
2729 // Check whether type units are presented.
2730 if (!OptContext
.File
.Dwarf
->types_section_units().empty()) {
2731 reportWarning("type units are not currently supported: file will "
2734 OptContext
.Skip
= true;
2738 // Clone all the clang modules with requires extracting the DIE units. We
2739 // don't need the full debug info until the Analyze phase.
2740 OptContext
.CompileUnits
.reserve(
2741 OptContext
.File
.Dwarf
->getNumCompileUnits());
2742 for (const auto &CU
: OptContext
.File
.Dwarf
->compile_units()) {
2743 auto CUDie
= CU
->getUnitDIE(/*ExtractUnitDIEOnly=*/true);
2744 if (Options
.Verbose
) {
2745 outs() << "Input compilation unit:";
2746 DIDumpOptions DumpOpts
;
2747 DumpOpts
.ChildRecurseDepth
= 0;
2748 DumpOpts
.Verbose
= Options
.Verbose
;
2749 CUDie
.dump(outs(), 0, DumpOpts
);
2753 for (auto &CU
: OptContext
.ModuleUnits
) {
2754 if (Error Err
= cloneModuleUnit(OptContext
, CU
, ODRContexts
, DebugStrPool
,
2755 DebugLineStrPool
, StringOffsetPool
))
2756 reportWarning(toString(std::move(Err
)), CU
.File
);
2760 // At this point we know how much data we have emitted. We use this value to
2761 // compare canonical DIE offsets in analyzeContextInfo to see if a definition
2762 // is already emitted, without being affected by canonical die offsets set
2763 // later. This prevents undeterminism when analyze and clone execute
2764 // concurrently, as clone set the canonical DIE offset and analyze reads it.
2765 const uint64_t ModulesEndOffset
=
2766 (TheDwarfEmitter
== nullptr) ? 0
2767 : TheDwarfEmitter
->getDebugInfoSectionSize();
2769 // These variables manage the list of processed object files.
2770 // The mutex and condition variable are to ensure that this is thread safe.
2771 std::mutex ProcessedFilesMutex
;
2772 std::condition_variable ProcessedFilesConditionVariable
;
2773 BitVector
ProcessedFiles(NumObjects
, false);
2775 // Analyzing the context info is particularly expensive so it is executed in
2776 // parallel with emitting the previous compile unit.
2777 auto AnalyzeLambda
= [&](size_t I
) {
2778 auto &Context
= ObjectContexts
[I
];
2780 if (Context
.Skip
|| !Context
.File
.Dwarf
)
2783 for (const auto &CU
: Context
.File
.Dwarf
->compile_units()) {
2784 // Previously we only extracted the unit DIEs. We need the full debug info
2786 auto CUDie
= CU
->getUnitDIE(/*ExtractUnitDIEOnly=*/false);
2787 std::string PCMFile
= getPCMFile(CUDie
, Options
.ObjectPrefixMap
);
2789 if (!CUDie
|| LLVM_UNLIKELY(Options
.Update
) ||
2790 !isClangModuleRef(CUDie
, PCMFile
, Context
, 0, true).first
) {
2791 Context
.CompileUnits
.push_back(std::make_unique
<CompileUnit
>(
2792 *CU
, UniqueUnitID
++, !Options
.NoODR
&& !Options
.Update
, ""));
2796 // Now build the DIE parent links that we will use during the next phase.
2797 for (auto &CurrentUnit
: Context
.CompileUnits
) {
2798 auto CUDie
= CurrentUnit
->getOrigUnit().getUnitDIE();
2801 analyzeContextInfo(CurrentUnit
->getOrigUnit().getUnitDIE(), 0,
2802 *CurrentUnit
, &ODRContexts
.getRoot(), ODRContexts
,
2803 ModulesEndOffset
, Options
.ParseableSwiftInterfaces
,
2804 [&](const Twine
&Warning
, const DWARFDie
&DIE
) {
2805 reportWarning(Warning
, Context
.File
, &DIE
);
2810 // For each object file map how many bytes were emitted.
2811 StringMap
<DebugInfoSize
> SizeByObject
;
2813 // And then the remaining work in serial again.
2814 // Note, although this loop runs in serial, it can run in parallel with
2815 // the analyzeContextInfo loop so long as we process files with indices >=
2816 // than those processed by analyzeContextInfo.
2817 auto CloneLambda
= [&](size_t I
) {
2818 auto &OptContext
= ObjectContexts
[I
];
2819 if (OptContext
.Skip
|| !OptContext
.File
.Dwarf
)
2822 // Then mark all the DIEs that need to be present in the generated output
2823 // and collect some information about them.
2824 // Note that this loop can not be merged with the previous one because
2825 // cross-cu references require the ParentIdx to be setup for every CU in
2826 // the object file before calling this.
2827 if (LLVM_UNLIKELY(Options
.Update
)) {
2828 for (auto &CurrentUnit
: OptContext
.CompileUnits
)
2829 CurrentUnit
->markEverythingAsKept();
2830 copyInvariantDebugSection(*OptContext
.File
.Dwarf
);
2832 for (auto &CurrentUnit
: OptContext
.CompileUnits
) {
2833 lookForDIEsToKeep(*OptContext
.File
.Addresses
, OptContext
.CompileUnits
,
2834 CurrentUnit
->getOrigUnit().getUnitDIE(),
2835 OptContext
.File
, *CurrentUnit
, 0);
2837 verifyKeepChain(*CurrentUnit
);
2842 // The calls to applyValidRelocs inside cloneDIE will walk the reloc
2843 // array again (in the same way findValidRelocsInDebugInfo() did). We
2844 // need to reset the NextValidReloc index to the beginning.
2845 if (OptContext
.File
.Addresses
->hasValidRelocs() ||
2846 LLVM_UNLIKELY(Options
.Update
)) {
2847 SizeByObject
[OptContext
.File
.FileName
].Input
=
2848 getDebugInfoSize(*OptContext
.File
.Dwarf
);
2849 SizeByObject
[OptContext
.File
.FileName
].Output
=
2850 DIECloner(*this, TheDwarfEmitter
.get(), OptContext
.File
, DIEAlloc
,
2851 OptContext
.CompileUnits
, Options
.Update
, DebugStrPool
,
2852 DebugLineStrPool
, StringOffsetPool
)
2853 .cloneAllCompileUnits(*OptContext
.File
.Dwarf
, OptContext
.File
,
2854 OptContext
.File
.Dwarf
->isLittleEndian());
2856 if ((TheDwarfEmitter
!= nullptr) && !OptContext
.CompileUnits
.empty() &&
2857 LLVM_LIKELY(!Options
.Update
))
2858 patchFrameInfoForObject(OptContext
);
2860 // Clean-up before starting working on the next object.
2861 cleanupAuxiliarryData(OptContext
);
2864 auto EmitLambda
= [&]() {
2865 // Emit everything that's global.
2866 if (TheDwarfEmitter
!= nullptr) {
2867 TheDwarfEmitter
->emitAbbrevs(Abbreviations
, Options
.TargetDWARFVersion
);
2868 TheDwarfEmitter
->emitStrings(DebugStrPool
);
2869 TheDwarfEmitter
->emitStringOffsets(StringOffsetPool
.DieValues
,
2870 Options
.TargetDWARFVersion
);
2871 TheDwarfEmitter
->emitLineStrings(DebugLineStrPool
);
2872 for (AccelTableKind TableKind
: Options
.AccelTables
) {
2873 switch (TableKind
) {
2874 case AccelTableKind::Apple
:
2875 TheDwarfEmitter
->emitAppleNamespaces(AppleNamespaces
);
2876 TheDwarfEmitter
->emitAppleNames(AppleNames
);
2877 TheDwarfEmitter
->emitAppleTypes(AppleTypes
);
2878 TheDwarfEmitter
->emitAppleObjc(AppleObjc
);
2880 case AccelTableKind::Pub
:
2881 // Already emitted by emitAcceleratorEntriesForUnit.
2882 // Already emitted by emitAcceleratorEntriesForUnit.
2884 case AccelTableKind::DebugNames
:
2885 TheDwarfEmitter
->emitDebugNames(DebugNames
);
2892 auto AnalyzeAll
= [&]() {
2893 for (unsigned I
= 0, E
= NumObjects
; I
!= E
; ++I
) {
2896 std::unique_lock
<std::mutex
> LockGuard(ProcessedFilesMutex
);
2897 ProcessedFiles
.set(I
);
2898 ProcessedFilesConditionVariable
.notify_one();
2902 auto CloneAll
= [&]() {
2903 for (unsigned I
= 0, E
= NumObjects
; I
!= E
; ++I
) {
2905 std::unique_lock
<std::mutex
> LockGuard(ProcessedFilesMutex
);
2906 if (!ProcessedFiles
[I
]) {
2907 ProcessedFilesConditionVariable
.wait(
2908 LockGuard
, [&]() { return ProcessedFiles
[I
]; });
2917 // To limit memory usage in the single threaded case, analyze and clone are
2918 // run sequentially so the OptContext is freed after processing each object
2919 // in endDebugObject.
2920 if (Options
.Threads
== 1) {
2921 for (unsigned I
= 0, E
= NumObjects
; I
!= E
; ++I
) {
2927 ThreadPool
Pool(hardware_concurrency(2));
2928 Pool
.async(AnalyzeAll
);
2929 Pool
.async(CloneAll
);
2933 if (Options
.Statistics
) {
2934 // Create a vector sorted in descending order by output size.
2935 std::vector
<std::pair
<StringRef
, DebugInfoSize
>> Sorted
;
2936 for (auto &E
: SizeByObject
)
2937 Sorted
.emplace_back(E
.first(), E
.second
);
2938 llvm::sort(Sorted
, [](auto &LHS
, auto &RHS
) {
2939 return LHS
.second
.Output
> RHS
.second
.Output
;
2942 auto ComputePercentange
= [](int64_t Input
, int64_t Output
) -> float {
2943 const float Difference
= Output
- Input
;
2944 const float Sum
= Input
+ Output
;
2947 return (Difference
/ (Sum
/ 2));
2950 int64_t InputTotal
= 0;
2951 int64_t OutputTotal
= 0;
2952 const char *FormatStr
= "{0,-45} {1,10}b {2,10}b {3,8:P}\n";
2955 outs() << ".debug_info section size (in bytes)\n";
2956 outs() << "----------------------------------------------------------------"
2957 "---------------\n";
2958 outs() << "Filename Object "
2960 outs() << "----------------------------------------------------------------"
2961 "---------------\n";
2964 for (auto &E
: Sorted
) {
2965 InputTotal
+= E
.second
.Input
;
2966 OutputTotal
+= E
.second
.Output
;
2967 llvm::outs() << formatv(
2968 FormatStr
, sys::path::filename(E
.first
).take_back(45), E
.second
.Input
,
2969 E
.second
.Output
, ComputePercentange(E
.second
.Input
, E
.second
.Output
));
2971 // Print total and footer.
2972 outs() << "----------------------------------------------------------------"
2973 "---------------\n";
2974 llvm::outs() << formatv(FormatStr
, "Total", InputTotal
, OutputTotal
,
2975 ComputePercentange(InputTotal
, OutputTotal
));
2976 outs() << "----------------------------------------------------------------"
2977 "---------------\n\n";
2980 return Error::success();
2983 Error
DWARFLinker::cloneModuleUnit(LinkContext
&Context
, RefModuleUnit
&Unit
,
2984 DeclContextTree
&ODRContexts
,
2985 OffsetsStringPool
&DebugStrPool
,
2986 OffsetsStringPool
&DebugLineStrPool
,
2987 DebugDieValuePool
&StringOffsetPool
,
2989 assert(Unit
.Unit
.get() != nullptr);
2991 if (!Unit
.Unit
->getOrigUnit().getUnitDIE().hasChildren())
2992 return Error::success();
2994 if (Options
.Verbose
) {
2995 outs().indent(Indent
);
2996 outs() << "cloning .debug_info from " << Unit
.File
.FileName
<< "\n";
2999 // Analyze context for the module.
3000 analyzeContextInfo(Unit
.Unit
->getOrigUnit().getUnitDIE(), 0, *(Unit
.Unit
),
3001 &ODRContexts
.getRoot(), ODRContexts
, 0,
3002 Options
.ParseableSwiftInterfaces
,
3003 [&](const Twine
&Warning
, const DWARFDie
&DIE
) {
3004 reportWarning(Warning
, Context
.File
, &DIE
);
3007 Unit
.Unit
->markEverythingAsKept();
3010 UnitListTy CompileUnits
;
3011 CompileUnits
.emplace_back(std::move(Unit
.Unit
));
3012 assert(TheDwarfEmitter
);
3013 DIECloner(*this, TheDwarfEmitter
.get(), Unit
.File
, DIEAlloc
, CompileUnits
,
3014 Options
.Update
, DebugStrPool
, DebugLineStrPool
, StringOffsetPool
)
3015 .cloneAllCompileUnits(*Unit
.File
.Dwarf
, Unit
.File
,
3016 Unit
.File
.Dwarf
->isLittleEndian());
3017 return Error::success();
3020 void DWARFLinker::verifyInput(const DWARFFile
&File
) {
3025 raw_string_ostream
OS(Buffer
);
3026 DIDumpOptions DumpOpts
;
3027 if (!File
.Dwarf
->verify(OS
, DumpOpts
.noImplicitRecursion())) {
3028 if (Options
.InputVerificationHandler
)
3029 Options
.InputVerificationHandler(File
, OS
.str());
3033 Error
DWARFLinker::createEmitter(const Triple
&TheTriple
,
3034 OutputFileType FileType
,
3035 raw_pwrite_stream
&OutFile
) {
3037 TheDwarfEmitter
= std::make_unique
<DwarfStreamer
>(
3038 FileType
, OutFile
, StringsTranslator
, WarningHandler
);
3040 return TheDwarfEmitter
->init(TheTriple
, "__DWARF");
3043 DwarfEmitter
*DWARFLinker::getEmitter() { return TheDwarfEmitter
.get(); }