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