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