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