11db9f3b2SDimitry Andric //=== DWARFLinker.cpp -----------------------------------------------------===// 21db9f3b2SDimitry Andric // 31db9f3b2SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 41db9f3b2SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 51db9f3b2SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61db9f3b2SDimitry Andric // 71db9f3b2SDimitry Andric //===----------------------------------------------------------------------===// 81db9f3b2SDimitry Andric 91db9f3b2SDimitry Andric #include "llvm/DWARFLinker/Classic/DWARFLinker.h" 101db9f3b2SDimitry Andric #include "llvm/ADT/ArrayRef.h" 111db9f3b2SDimitry Andric #include "llvm/ADT/BitVector.h" 121db9f3b2SDimitry Andric #include "llvm/ADT/STLExtras.h" 131db9f3b2SDimitry Andric #include "llvm/ADT/StringExtras.h" 141db9f3b2SDimitry Andric #include "llvm/CodeGen/NonRelocatableStringpool.h" 151db9f3b2SDimitry Andric #include "llvm/DWARFLinker/Classic/DWARFLinkerDeclContext.h" 161db9f3b2SDimitry Andric #include "llvm/DWARFLinker/Classic/DWARFStreamer.h" 17297eecfbSDimitry Andric #include "llvm/DWARFLinker/Utils.h" 181db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" 191db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" 201db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h" 211db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" 221db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" 231db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" 241db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" 251db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDie.h" 261db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 271db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 281db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFSection.h" 291db9f3b2SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 301db9f3b2SDimitry Andric #include "llvm/MC/MCDwarf.h" 311db9f3b2SDimitry Andric #include "llvm/Support/DataExtractor.h" 321db9f3b2SDimitry Andric #include "llvm/Support/Error.h" 331db9f3b2SDimitry Andric #include "llvm/Support/ErrorHandling.h" 341db9f3b2SDimitry Andric #include "llvm/Support/ErrorOr.h" 351db9f3b2SDimitry Andric #include "llvm/Support/FormatVariadic.h" 361db9f3b2SDimitry Andric #include "llvm/Support/LEB128.h" 371db9f3b2SDimitry Andric #include "llvm/Support/Path.h" 381db9f3b2SDimitry Andric #include "llvm/Support/ThreadPool.h" 391db9f3b2SDimitry Andric #include <vector> 401db9f3b2SDimitry Andric 411db9f3b2SDimitry Andric namespace llvm { 421db9f3b2SDimitry Andric 431db9f3b2SDimitry Andric using namespace dwarf_linker; 441db9f3b2SDimitry Andric using namespace dwarf_linker::classic; 451db9f3b2SDimitry Andric 461db9f3b2SDimitry Andric /// Hold the input and output of the debug info size in bytes. 471db9f3b2SDimitry Andric struct DebugInfoSize { 481db9f3b2SDimitry Andric uint64_t Input; 491db9f3b2SDimitry Andric uint64_t Output; 501db9f3b2SDimitry Andric }; 511db9f3b2SDimitry Andric 521db9f3b2SDimitry Andric /// Compute the total size of the debug info. 531db9f3b2SDimitry Andric static uint64_t getDebugInfoSize(DWARFContext &Dwarf) { 541db9f3b2SDimitry Andric uint64_t Size = 0; 551db9f3b2SDimitry Andric for (auto &Unit : Dwarf.compile_units()) { 561db9f3b2SDimitry Andric Size += Unit->getLength(); 571db9f3b2SDimitry Andric } 581db9f3b2SDimitry Andric return Size; 591db9f3b2SDimitry Andric } 601db9f3b2SDimitry Andric 611db9f3b2SDimitry Andric /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our 621db9f3b2SDimitry Andric /// CompileUnit object instead. 631db9f3b2SDimitry Andric static CompileUnit *getUnitForOffset(const UnitListTy &Units, uint64_t Offset) { 641db9f3b2SDimitry Andric auto CU = llvm::upper_bound( 651db9f3b2SDimitry Andric Units, Offset, [](uint64_t LHS, const std::unique_ptr<CompileUnit> &RHS) { 661db9f3b2SDimitry Andric return LHS < RHS->getOrigUnit().getNextUnitOffset(); 671db9f3b2SDimitry Andric }); 681db9f3b2SDimitry Andric return CU != Units.end() ? CU->get() : nullptr; 691db9f3b2SDimitry Andric } 701db9f3b2SDimitry Andric 711db9f3b2SDimitry Andric /// Resolve the DIE attribute reference that has been extracted in \p RefValue. 721db9f3b2SDimitry Andric /// The resulting DIE might be in another CompileUnit which is stored into \p 731db9f3b2SDimitry Andric /// ReferencedCU. \returns null if resolving fails for any reason. 741db9f3b2SDimitry Andric DWARFDie DWARFLinker::resolveDIEReference(const DWARFFile &File, 751db9f3b2SDimitry Andric const UnitListTy &Units, 761db9f3b2SDimitry Andric const DWARFFormValue &RefValue, 771db9f3b2SDimitry Andric const DWARFDie &DIE, 781db9f3b2SDimitry Andric CompileUnit *&RefCU) { 791db9f3b2SDimitry Andric assert(RefValue.isFormClass(DWARFFormValue::FC_Reference)); 80*0fca6ea1SDimitry Andric uint64_t RefOffset; 81*0fca6ea1SDimitry Andric if (std::optional<uint64_t> Off = RefValue.getAsRelativeReference()) { 82*0fca6ea1SDimitry Andric RefOffset = RefValue.getUnit()->getOffset() + *Off; 83*0fca6ea1SDimitry Andric } else if (Off = RefValue.getAsDebugInfoReference(); Off) { 84*0fca6ea1SDimitry Andric RefOffset = *Off; 85*0fca6ea1SDimitry Andric } else { 86*0fca6ea1SDimitry Andric reportWarning("Unsupported reference type", File, &DIE); 87*0fca6ea1SDimitry Andric return DWARFDie(); 88*0fca6ea1SDimitry Andric } 891db9f3b2SDimitry Andric if ((RefCU = getUnitForOffset(Units, RefOffset))) 901db9f3b2SDimitry Andric if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) { 911db9f3b2SDimitry Andric // In a file with broken references, an attribute might point to a NULL 921db9f3b2SDimitry Andric // DIE. 931db9f3b2SDimitry Andric if (!RefDie.isNULL()) 941db9f3b2SDimitry Andric return RefDie; 951db9f3b2SDimitry Andric } 961db9f3b2SDimitry Andric 971db9f3b2SDimitry Andric reportWarning("could not find referenced DIE", File, &DIE); 981db9f3b2SDimitry Andric return DWARFDie(); 991db9f3b2SDimitry Andric } 1001db9f3b2SDimitry Andric 1011db9f3b2SDimitry Andric /// \returns whether the passed \a Attr type might contain a DIE reference 1021db9f3b2SDimitry Andric /// suitable for ODR uniquing. 1031db9f3b2SDimitry Andric static bool isODRAttribute(uint16_t Attr) { 1041db9f3b2SDimitry Andric switch (Attr) { 1051db9f3b2SDimitry Andric default: 1061db9f3b2SDimitry Andric return false; 1071db9f3b2SDimitry Andric case dwarf::DW_AT_type: 1081db9f3b2SDimitry Andric case dwarf::DW_AT_containing_type: 1091db9f3b2SDimitry Andric case dwarf::DW_AT_specification: 1101db9f3b2SDimitry Andric case dwarf::DW_AT_abstract_origin: 1111db9f3b2SDimitry Andric case dwarf::DW_AT_import: 1121db9f3b2SDimitry Andric return true; 1131db9f3b2SDimitry Andric } 1141db9f3b2SDimitry Andric llvm_unreachable("Improper attribute."); 1151db9f3b2SDimitry Andric } 1161db9f3b2SDimitry Andric 1171db9f3b2SDimitry Andric static bool isTypeTag(uint16_t Tag) { 1181db9f3b2SDimitry Andric switch (Tag) { 1191db9f3b2SDimitry Andric case dwarf::DW_TAG_array_type: 1201db9f3b2SDimitry Andric case dwarf::DW_TAG_class_type: 1211db9f3b2SDimitry Andric case dwarf::DW_TAG_enumeration_type: 1221db9f3b2SDimitry Andric case dwarf::DW_TAG_pointer_type: 1231db9f3b2SDimitry Andric case dwarf::DW_TAG_reference_type: 1241db9f3b2SDimitry Andric case dwarf::DW_TAG_string_type: 1251db9f3b2SDimitry Andric case dwarf::DW_TAG_structure_type: 1261db9f3b2SDimitry Andric case dwarf::DW_TAG_subroutine_type: 127*0fca6ea1SDimitry Andric case dwarf::DW_TAG_template_alias: 1281db9f3b2SDimitry Andric case dwarf::DW_TAG_typedef: 1291db9f3b2SDimitry Andric case dwarf::DW_TAG_union_type: 1301db9f3b2SDimitry Andric case dwarf::DW_TAG_ptr_to_member_type: 1311db9f3b2SDimitry Andric case dwarf::DW_TAG_set_type: 1321db9f3b2SDimitry Andric case dwarf::DW_TAG_subrange_type: 1331db9f3b2SDimitry Andric case dwarf::DW_TAG_base_type: 1341db9f3b2SDimitry Andric case dwarf::DW_TAG_const_type: 1351db9f3b2SDimitry Andric case dwarf::DW_TAG_constant: 1361db9f3b2SDimitry Andric case dwarf::DW_TAG_file_type: 1371db9f3b2SDimitry Andric case dwarf::DW_TAG_namelist: 1381db9f3b2SDimitry Andric case dwarf::DW_TAG_packed_type: 1391db9f3b2SDimitry Andric case dwarf::DW_TAG_volatile_type: 1401db9f3b2SDimitry Andric case dwarf::DW_TAG_restrict_type: 1411db9f3b2SDimitry Andric case dwarf::DW_TAG_atomic_type: 1421db9f3b2SDimitry Andric case dwarf::DW_TAG_interface_type: 1431db9f3b2SDimitry Andric case dwarf::DW_TAG_unspecified_type: 1441db9f3b2SDimitry Andric case dwarf::DW_TAG_shared_type: 1451db9f3b2SDimitry Andric case dwarf::DW_TAG_immutable_type: 1461db9f3b2SDimitry Andric return true; 1471db9f3b2SDimitry Andric default: 1481db9f3b2SDimitry Andric break; 1491db9f3b2SDimitry Andric } 1501db9f3b2SDimitry Andric return false; 1511db9f3b2SDimitry Andric } 1521db9f3b2SDimitry Andric 1531db9f3b2SDimitry Andric bool DWARFLinker::DIECloner::getDIENames(const DWARFDie &Die, 1541db9f3b2SDimitry Andric AttributesInfo &Info, 1551db9f3b2SDimitry Andric OffsetsStringPool &StringPool, 1561db9f3b2SDimitry Andric bool StripTemplate) { 1571db9f3b2SDimitry Andric // This function will be called on DIEs having low_pcs and 1581db9f3b2SDimitry Andric // ranges. As getting the name might be more expansive, filter out 1591db9f3b2SDimitry Andric // blocks directly. 1601db9f3b2SDimitry Andric if (Die.getTag() == dwarf::DW_TAG_lexical_block) 1611db9f3b2SDimitry Andric return false; 1621db9f3b2SDimitry Andric 1631db9f3b2SDimitry Andric if (!Info.MangledName) 1641db9f3b2SDimitry Andric if (const char *MangledName = Die.getLinkageName()) 1651db9f3b2SDimitry Andric Info.MangledName = StringPool.getEntry(MangledName); 1661db9f3b2SDimitry Andric 1671db9f3b2SDimitry Andric if (!Info.Name) 1681db9f3b2SDimitry Andric if (const char *Name = Die.getShortName()) 1691db9f3b2SDimitry Andric Info.Name = StringPool.getEntry(Name); 1701db9f3b2SDimitry Andric 1711db9f3b2SDimitry Andric if (!Info.MangledName) 1721db9f3b2SDimitry Andric Info.MangledName = Info.Name; 1731db9f3b2SDimitry Andric 1741db9f3b2SDimitry Andric if (StripTemplate && Info.Name && Info.MangledName != Info.Name) { 1751db9f3b2SDimitry Andric StringRef Name = Info.Name.getString(); 1761db9f3b2SDimitry Andric if (std::optional<StringRef> StrippedName = StripTemplateParameters(Name)) 1771db9f3b2SDimitry Andric Info.NameWithoutTemplate = StringPool.getEntry(*StrippedName); 1781db9f3b2SDimitry Andric } 1791db9f3b2SDimitry Andric 1801db9f3b2SDimitry Andric return Info.Name || Info.MangledName; 1811db9f3b2SDimitry Andric } 1821db9f3b2SDimitry Andric 1831db9f3b2SDimitry Andric /// Resolve the relative path to a build artifact referenced by DWARF by 1841db9f3b2SDimitry Andric /// applying DW_AT_comp_dir. 1851db9f3b2SDimitry Andric static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) { 1861db9f3b2SDimitry Andric sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), "")); 1871db9f3b2SDimitry Andric } 1881db9f3b2SDimitry Andric 1891db9f3b2SDimitry Andric /// Collect references to parseable Swift interfaces in imported 1901db9f3b2SDimitry Andric /// DW_TAG_module blocks. 1911db9f3b2SDimitry Andric static void analyzeImportedModule( 1921db9f3b2SDimitry Andric const DWARFDie &DIE, CompileUnit &CU, 1931db9f3b2SDimitry Andric DWARFLinkerBase::SwiftInterfacesMapTy *ParseableSwiftInterfaces, 1941db9f3b2SDimitry Andric std::function<void(const Twine &, const DWARFDie &)> ReportWarning) { 1951db9f3b2SDimitry Andric if (CU.getLanguage() != dwarf::DW_LANG_Swift) 1961db9f3b2SDimitry Andric return; 1971db9f3b2SDimitry Andric 1981db9f3b2SDimitry Andric if (!ParseableSwiftInterfaces) 1991db9f3b2SDimitry Andric return; 2001db9f3b2SDimitry Andric 2011db9f3b2SDimitry Andric StringRef Path = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_include_path)); 2021db9f3b2SDimitry Andric if (!Path.ends_with(".swiftinterface")) 2031db9f3b2SDimitry Andric return; 2041db9f3b2SDimitry Andric // Don't track interfaces that are part of the SDK. 2051db9f3b2SDimitry Andric StringRef SysRoot = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_sysroot)); 2061db9f3b2SDimitry Andric if (SysRoot.empty()) 2071db9f3b2SDimitry Andric SysRoot = CU.getSysRoot(); 2081db9f3b2SDimitry Andric if (!SysRoot.empty() && Path.starts_with(SysRoot)) 2091db9f3b2SDimitry Andric return; 2101db9f3b2SDimitry Andric // Don't track interfaces that are part of the toolchain. 2111db9f3b2SDimitry Andric // For example: Swift, _Concurrency, ... 212*0fca6ea1SDimitry Andric StringRef DeveloperDir = guessDeveloperDir(SysRoot); 213*0fca6ea1SDimitry Andric if (!DeveloperDir.empty() && Path.starts_with(DeveloperDir)) 214*0fca6ea1SDimitry Andric return; 215*0fca6ea1SDimitry Andric if (isInToolchainDir(Path)) 2161db9f3b2SDimitry Andric return; 2171db9f3b2SDimitry Andric std::optional<const char *> Name = 2181db9f3b2SDimitry Andric dwarf::toString(DIE.find(dwarf::DW_AT_name)); 2191db9f3b2SDimitry Andric if (!Name) 2201db9f3b2SDimitry Andric return; 2211db9f3b2SDimitry Andric auto &Entry = (*ParseableSwiftInterfaces)[*Name]; 2221db9f3b2SDimitry Andric // The prepend path is applied later when copying. 2231db9f3b2SDimitry Andric DWARFDie CUDie = CU.getOrigUnit().getUnitDIE(); 2241db9f3b2SDimitry Andric SmallString<128> ResolvedPath; 2251db9f3b2SDimitry Andric if (sys::path::is_relative(Path)) 2261db9f3b2SDimitry Andric resolveRelativeObjectPath(ResolvedPath, CUDie); 2271db9f3b2SDimitry Andric sys::path::append(ResolvedPath, Path); 2281db9f3b2SDimitry Andric if (!Entry.empty() && Entry != ResolvedPath) 2291db9f3b2SDimitry Andric ReportWarning(Twine("Conflicting parseable interfaces for Swift Module ") + 2301db9f3b2SDimitry Andric *Name + ": " + Entry + " and " + Path, 2311db9f3b2SDimitry Andric DIE); 2327a6dacacSDimitry Andric Entry = std::string(ResolvedPath); 2331db9f3b2SDimitry Andric } 2341db9f3b2SDimitry Andric 2351db9f3b2SDimitry Andric /// The distinct types of work performed by the work loop in 2361db9f3b2SDimitry Andric /// analyzeContextInfo. 2371db9f3b2SDimitry Andric enum class ContextWorklistItemType : uint8_t { 2381db9f3b2SDimitry Andric AnalyzeContextInfo, 2391db9f3b2SDimitry Andric UpdateChildPruning, 2401db9f3b2SDimitry Andric UpdatePruning, 2411db9f3b2SDimitry Andric }; 2421db9f3b2SDimitry Andric 2431db9f3b2SDimitry Andric /// This class represents an item in the work list. The type defines what kind 2441db9f3b2SDimitry Andric /// of work needs to be performed when processing the current item. Everything 2451db9f3b2SDimitry Andric /// but the Type and Die fields are optional based on the type. 2461db9f3b2SDimitry Andric struct ContextWorklistItem { 2471db9f3b2SDimitry Andric DWARFDie Die; 2481db9f3b2SDimitry Andric unsigned ParentIdx; 2491db9f3b2SDimitry Andric union { 2501db9f3b2SDimitry Andric CompileUnit::DIEInfo *OtherInfo; 2511db9f3b2SDimitry Andric DeclContext *Context; 2521db9f3b2SDimitry Andric }; 2531db9f3b2SDimitry Andric ContextWorklistItemType Type; 2541db9f3b2SDimitry Andric bool InImportedModule; 2551db9f3b2SDimitry Andric 2561db9f3b2SDimitry Andric ContextWorklistItem(DWARFDie Die, ContextWorklistItemType T, 2571db9f3b2SDimitry Andric CompileUnit::DIEInfo *OtherInfo = nullptr) 2581db9f3b2SDimitry Andric : Die(Die), ParentIdx(0), OtherInfo(OtherInfo), Type(T), 2591db9f3b2SDimitry Andric InImportedModule(false) {} 2601db9f3b2SDimitry Andric 2611db9f3b2SDimitry Andric ContextWorklistItem(DWARFDie Die, DeclContext *Context, unsigned ParentIdx, 2621db9f3b2SDimitry Andric bool InImportedModule) 2631db9f3b2SDimitry Andric : Die(Die), ParentIdx(ParentIdx), Context(Context), 2641db9f3b2SDimitry Andric Type(ContextWorklistItemType::AnalyzeContextInfo), 2651db9f3b2SDimitry Andric InImportedModule(InImportedModule) {} 2661db9f3b2SDimitry Andric }; 2671db9f3b2SDimitry Andric 2681db9f3b2SDimitry Andric static bool updatePruning(const DWARFDie &Die, CompileUnit &CU, 2691db9f3b2SDimitry Andric uint64_t ModulesEndOffset) { 2701db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = CU.getInfo(Die); 2711db9f3b2SDimitry Andric 2721db9f3b2SDimitry Andric // Prune this DIE if it is either a forward declaration inside a 2731db9f3b2SDimitry Andric // DW_TAG_module or a DW_TAG_module that contains nothing but 2741db9f3b2SDimitry Andric // forward declarations. 2751db9f3b2SDimitry Andric Info.Prune &= (Die.getTag() == dwarf::DW_TAG_module) || 2761db9f3b2SDimitry Andric (isTypeTag(Die.getTag()) && 2771db9f3b2SDimitry Andric dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0)); 2781db9f3b2SDimitry Andric 2791db9f3b2SDimitry Andric // Only prune forward declarations inside a DW_TAG_module for which a 2801db9f3b2SDimitry Andric // definition exists elsewhere. 2811db9f3b2SDimitry Andric if (ModulesEndOffset == 0) 2821db9f3b2SDimitry Andric Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset(); 2831db9f3b2SDimitry Andric else 2841db9f3b2SDimitry Andric Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() > 0 && 2851db9f3b2SDimitry Andric Info.Ctxt->getCanonicalDIEOffset() <= ModulesEndOffset; 2861db9f3b2SDimitry Andric 2871db9f3b2SDimitry Andric return Info.Prune; 2881db9f3b2SDimitry Andric } 2891db9f3b2SDimitry Andric 2901db9f3b2SDimitry Andric static void updateChildPruning(const DWARFDie &Die, CompileUnit &CU, 2911db9f3b2SDimitry Andric CompileUnit::DIEInfo &ChildInfo) { 2921db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = CU.getInfo(Die); 2931db9f3b2SDimitry Andric Info.Prune &= ChildInfo.Prune; 2941db9f3b2SDimitry Andric } 2951db9f3b2SDimitry Andric 2961db9f3b2SDimitry Andric /// Recursive helper to build the global DeclContext information and 2971db9f3b2SDimitry Andric /// gather the child->parent relationships in the original compile unit. 2981db9f3b2SDimitry Andric /// 2991db9f3b2SDimitry Andric /// This function uses the same work list approach as lookForDIEsToKeep. 3001db9f3b2SDimitry Andric /// 3011db9f3b2SDimitry Andric /// \return true when this DIE and all of its children are only 3021db9f3b2SDimitry Andric /// forward declarations to types defined in external clang modules 3031db9f3b2SDimitry Andric /// (i.e., forward declarations that are children of a DW_TAG_module). 3041db9f3b2SDimitry Andric static void analyzeContextInfo( 3051db9f3b2SDimitry Andric const DWARFDie &DIE, unsigned ParentIdx, CompileUnit &CU, 3061db9f3b2SDimitry Andric DeclContext *CurrentDeclContext, DeclContextTree &Contexts, 3071db9f3b2SDimitry Andric uint64_t ModulesEndOffset, 3081db9f3b2SDimitry Andric DWARFLinkerBase::SwiftInterfacesMapTy *ParseableSwiftInterfaces, 3091db9f3b2SDimitry Andric std::function<void(const Twine &, const DWARFDie &)> ReportWarning) { 3101db9f3b2SDimitry Andric // LIFO work list. 3111db9f3b2SDimitry Andric std::vector<ContextWorklistItem> Worklist; 3121db9f3b2SDimitry Andric Worklist.emplace_back(DIE, CurrentDeclContext, ParentIdx, false); 3131db9f3b2SDimitry Andric 3141db9f3b2SDimitry Andric while (!Worklist.empty()) { 3151db9f3b2SDimitry Andric ContextWorklistItem Current = Worklist.back(); 3161db9f3b2SDimitry Andric Worklist.pop_back(); 3171db9f3b2SDimitry Andric 3181db9f3b2SDimitry Andric switch (Current.Type) { 3191db9f3b2SDimitry Andric case ContextWorklistItemType::UpdatePruning: 3201db9f3b2SDimitry Andric updatePruning(Current.Die, CU, ModulesEndOffset); 3211db9f3b2SDimitry Andric continue; 3221db9f3b2SDimitry Andric case ContextWorklistItemType::UpdateChildPruning: 3231db9f3b2SDimitry Andric updateChildPruning(Current.Die, CU, *Current.OtherInfo); 3241db9f3b2SDimitry Andric continue; 3251db9f3b2SDimitry Andric case ContextWorklistItemType::AnalyzeContextInfo: 3261db9f3b2SDimitry Andric break; 3271db9f3b2SDimitry Andric } 3281db9f3b2SDimitry Andric 3291db9f3b2SDimitry Andric unsigned Idx = CU.getOrigUnit().getDIEIndex(Current.Die); 3301db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = CU.getInfo(Idx); 3311db9f3b2SDimitry Andric 3321db9f3b2SDimitry Andric // Clang imposes an ODR on modules(!) regardless of the language: 3331db9f3b2SDimitry Andric // "The module-id should consist of only a single identifier, 3341db9f3b2SDimitry Andric // which provides the name of the module being defined. Each 3351db9f3b2SDimitry Andric // module shall have a single definition." 3361db9f3b2SDimitry Andric // 3371db9f3b2SDimitry Andric // This does not extend to the types inside the modules: 3381db9f3b2SDimitry Andric // "[I]n C, this implies that if two structs are defined in 3391db9f3b2SDimitry Andric // different submodules with the same name, those two types are 3401db9f3b2SDimitry Andric // distinct types (but may be compatible types if their 3411db9f3b2SDimitry Andric // definitions match)." 3421db9f3b2SDimitry Andric // 3431db9f3b2SDimitry Andric // We treat non-C++ modules like namespaces for this reason. 3441db9f3b2SDimitry Andric if (Current.Die.getTag() == dwarf::DW_TAG_module && 3451db9f3b2SDimitry Andric Current.ParentIdx == 0 && 3461db9f3b2SDimitry Andric dwarf::toString(Current.Die.find(dwarf::DW_AT_name), "") != 3471db9f3b2SDimitry Andric CU.getClangModuleName()) { 3481db9f3b2SDimitry Andric Current.InImportedModule = true; 3491db9f3b2SDimitry Andric analyzeImportedModule(Current.Die, CU, ParseableSwiftInterfaces, 3501db9f3b2SDimitry Andric ReportWarning); 3511db9f3b2SDimitry Andric } 3521db9f3b2SDimitry Andric 3531db9f3b2SDimitry Andric Info.ParentIdx = Current.ParentIdx; 3541db9f3b2SDimitry Andric Info.InModuleScope = CU.isClangModule() || Current.InImportedModule; 3551db9f3b2SDimitry Andric if (CU.hasODR() || Info.InModuleScope) { 3561db9f3b2SDimitry Andric if (Current.Context) { 3571db9f3b2SDimitry Andric auto PtrInvalidPair = Contexts.getChildDeclContext( 3581db9f3b2SDimitry Andric *Current.Context, Current.Die, CU, Info.InModuleScope); 3591db9f3b2SDimitry Andric Current.Context = PtrInvalidPair.getPointer(); 3601db9f3b2SDimitry Andric Info.Ctxt = 3611db9f3b2SDimitry Andric PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer(); 3621db9f3b2SDimitry Andric if (Info.Ctxt) 3631db9f3b2SDimitry Andric Info.Ctxt->setDefinedInClangModule(Info.InModuleScope); 3641db9f3b2SDimitry Andric } else 3651db9f3b2SDimitry Andric Info.Ctxt = Current.Context = nullptr; 3661db9f3b2SDimitry Andric } 3671db9f3b2SDimitry Andric 3681db9f3b2SDimitry Andric Info.Prune = Current.InImportedModule; 3691db9f3b2SDimitry Andric // Add children in reverse order to the worklist to effectively process 3701db9f3b2SDimitry Andric // them in order. 3711db9f3b2SDimitry Andric Worklist.emplace_back(Current.Die, ContextWorklistItemType::UpdatePruning); 3721db9f3b2SDimitry Andric for (auto Child : reverse(Current.Die.children())) { 3731db9f3b2SDimitry Andric CompileUnit::DIEInfo &ChildInfo = CU.getInfo(Child); 3741db9f3b2SDimitry Andric Worklist.emplace_back( 3751db9f3b2SDimitry Andric Current.Die, ContextWorklistItemType::UpdateChildPruning, &ChildInfo); 3761db9f3b2SDimitry Andric Worklist.emplace_back(Child, Current.Context, Idx, 3771db9f3b2SDimitry Andric Current.InImportedModule); 3781db9f3b2SDimitry Andric } 3791db9f3b2SDimitry Andric } 3801db9f3b2SDimitry Andric } 3811db9f3b2SDimitry Andric 3821db9f3b2SDimitry Andric static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) { 3831db9f3b2SDimitry Andric switch (Tag) { 3841db9f3b2SDimitry Andric default: 3851db9f3b2SDimitry Andric return false; 3861db9f3b2SDimitry Andric case dwarf::DW_TAG_class_type: 3871db9f3b2SDimitry Andric case dwarf::DW_TAG_common_block: 3881db9f3b2SDimitry Andric case dwarf::DW_TAG_lexical_block: 3891db9f3b2SDimitry Andric case dwarf::DW_TAG_structure_type: 3901db9f3b2SDimitry Andric case dwarf::DW_TAG_subprogram: 3911db9f3b2SDimitry Andric case dwarf::DW_TAG_subroutine_type: 3921db9f3b2SDimitry Andric case dwarf::DW_TAG_union_type: 3931db9f3b2SDimitry Andric return true; 3941db9f3b2SDimitry Andric } 3951db9f3b2SDimitry Andric llvm_unreachable("Invalid Tag"); 3961db9f3b2SDimitry Andric } 3971db9f3b2SDimitry Andric 3981db9f3b2SDimitry Andric void DWARFLinker::cleanupAuxiliarryData(LinkContext &Context) { 3991db9f3b2SDimitry Andric Context.clear(); 4001db9f3b2SDimitry Andric 4011db9f3b2SDimitry Andric for (DIEBlock *I : DIEBlocks) 4021db9f3b2SDimitry Andric I->~DIEBlock(); 4031db9f3b2SDimitry Andric for (DIELoc *I : DIELocs) 4041db9f3b2SDimitry Andric I->~DIELoc(); 4051db9f3b2SDimitry Andric 4061db9f3b2SDimitry Andric DIEBlocks.clear(); 4071db9f3b2SDimitry Andric DIELocs.clear(); 4081db9f3b2SDimitry Andric DIEAlloc.Reset(); 4091db9f3b2SDimitry Andric } 4101db9f3b2SDimitry Andric 4111db9f3b2SDimitry Andric static bool isTlsAddressCode(uint8_t DW_OP_Code) { 4121db9f3b2SDimitry Andric return DW_OP_Code == dwarf::DW_OP_form_tls_address || 4131db9f3b2SDimitry Andric DW_OP_Code == dwarf::DW_OP_GNU_push_tls_address; 4141db9f3b2SDimitry Andric } 4151db9f3b2SDimitry Andric 4161db9f3b2SDimitry Andric std::pair<bool, std::optional<int64_t>> 4171db9f3b2SDimitry Andric DWARFLinker::getVariableRelocAdjustment(AddressesMap &RelocMgr, 4181db9f3b2SDimitry Andric const DWARFDie &DIE) { 4191db9f3b2SDimitry Andric assert((DIE.getTag() == dwarf::DW_TAG_variable || 4201db9f3b2SDimitry Andric DIE.getTag() == dwarf::DW_TAG_constant) && 4211db9f3b2SDimitry Andric "Wrong type of input die"); 4221db9f3b2SDimitry Andric 4231db9f3b2SDimitry Andric const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); 4241db9f3b2SDimitry Andric 4251db9f3b2SDimitry Andric // Check if DIE has DW_AT_location attribute. 4261db9f3b2SDimitry Andric DWARFUnit *U = DIE.getDwarfUnit(); 4271db9f3b2SDimitry Andric std::optional<uint32_t> LocationIdx = 4281db9f3b2SDimitry Andric Abbrev->findAttributeIndex(dwarf::DW_AT_location); 4291db9f3b2SDimitry Andric if (!LocationIdx) 4301db9f3b2SDimitry Andric return std::make_pair(false, std::nullopt); 4311db9f3b2SDimitry Andric 4321db9f3b2SDimitry Andric // Get offset to the DW_AT_location attribute. 4331db9f3b2SDimitry Andric uint64_t AttrOffset = 4341db9f3b2SDimitry Andric Abbrev->getAttributeOffsetFromIndex(*LocationIdx, DIE.getOffset(), *U); 4351db9f3b2SDimitry Andric 4361db9f3b2SDimitry Andric // Get value of the DW_AT_location attribute. 4371db9f3b2SDimitry Andric std::optional<DWARFFormValue> LocationValue = 4381db9f3b2SDimitry Andric Abbrev->getAttributeValueFromOffset(*LocationIdx, AttrOffset, *U); 4391db9f3b2SDimitry Andric if (!LocationValue) 4401db9f3b2SDimitry Andric return std::make_pair(false, std::nullopt); 4411db9f3b2SDimitry Andric 4421db9f3b2SDimitry Andric // Check that DW_AT_location attribute is of 'exprloc' class. 4431db9f3b2SDimitry Andric // Handling value of location expressions for attributes of 'loclist' 4441db9f3b2SDimitry Andric // class is not implemented yet. 4451db9f3b2SDimitry Andric std::optional<ArrayRef<uint8_t>> Expr = LocationValue->getAsBlock(); 4461db9f3b2SDimitry Andric if (!Expr) 4471db9f3b2SDimitry Andric return std::make_pair(false, std::nullopt); 4481db9f3b2SDimitry Andric 4491db9f3b2SDimitry Andric // Parse 'exprloc' expression. 4501db9f3b2SDimitry Andric DataExtractor Data(toStringRef(*Expr), U->getContext().isLittleEndian(), 4511db9f3b2SDimitry Andric U->getAddressByteSize()); 4521db9f3b2SDimitry Andric DWARFExpression Expression(Data, U->getAddressByteSize(), 4531db9f3b2SDimitry Andric U->getFormParams().Format); 4541db9f3b2SDimitry Andric 4551db9f3b2SDimitry Andric bool HasLocationAddress = false; 4561db9f3b2SDimitry Andric uint64_t CurExprOffset = 0; 4571db9f3b2SDimitry Andric for (DWARFExpression::iterator It = Expression.begin(); 4581db9f3b2SDimitry Andric It != Expression.end(); ++It) { 4591db9f3b2SDimitry Andric DWARFExpression::iterator NextIt = It; 4601db9f3b2SDimitry Andric ++NextIt; 4611db9f3b2SDimitry Andric 4621db9f3b2SDimitry Andric const DWARFExpression::Operation &Op = *It; 4631db9f3b2SDimitry Andric switch (Op.getCode()) { 4641db9f3b2SDimitry Andric case dwarf::DW_OP_const2u: 4651db9f3b2SDimitry Andric case dwarf::DW_OP_const4u: 4661db9f3b2SDimitry Andric case dwarf::DW_OP_const8u: 4671db9f3b2SDimitry Andric case dwarf::DW_OP_const2s: 4681db9f3b2SDimitry Andric case dwarf::DW_OP_const4s: 4691db9f3b2SDimitry Andric case dwarf::DW_OP_const8s: 4701db9f3b2SDimitry Andric if (NextIt == Expression.end() || !isTlsAddressCode(NextIt->getCode())) 4711db9f3b2SDimitry Andric break; 4721db9f3b2SDimitry Andric [[fallthrough]]; 4731db9f3b2SDimitry Andric case dwarf::DW_OP_addr: { 4741db9f3b2SDimitry Andric HasLocationAddress = true; 4751db9f3b2SDimitry Andric // Check relocation for the address. 4761db9f3b2SDimitry Andric if (std::optional<int64_t> RelocAdjustment = 4771db9f3b2SDimitry Andric RelocMgr.getExprOpAddressRelocAdjustment( 4781db9f3b2SDimitry Andric *U, Op, AttrOffset + CurExprOffset, 4797a6dacacSDimitry Andric AttrOffset + Op.getEndOffset(), Options.Verbose)) 4801db9f3b2SDimitry Andric return std::make_pair(HasLocationAddress, *RelocAdjustment); 4811db9f3b2SDimitry Andric } break; 4821db9f3b2SDimitry Andric case dwarf::DW_OP_constx: 4831db9f3b2SDimitry Andric case dwarf::DW_OP_addrx: { 4841db9f3b2SDimitry Andric HasLocationAddress = true; 4851db9f3b2SDimitry Andric if (std::optional<uint64_t> AddressOffset = 4861db9f3b2SDimitry Andric DIE.getDwarfUnit()->getIndexedAddressOffset( 4871db9f3b2SDimitry Andric Op.getRawOperand(0))) { 4881db9f3b2SDimitry Andric // Check relocation for the address. 4891db9f3b2SDimitry Andric if (std::optional<int64_t> RelocAdjustment = 4901db9f3b2SDimitry Andric RelocMgr.getExprOpAddressRelocAdjustment( 4911db9f3b2SDimitry Andric *U, Op, *AddressOffset, 4927a6dacacSDimitry Andric *AddressOffset + DIE.getDwarfUnit()->getAddressByteSize(), 4937a6dacacSDimitry Andric Options.Verbose)) 4941db9f3b2SDimitry Andric return std::make_pair(HasLocationAddress, *RelocAdjustment); 4951db9f3b2SDimitry Andric } 4961db9f3b2SDimitry Andric } break; 4971db9f3b2SDimitry Andric default: { 4981db9f3b2SDimitry Andric // Nothing to do. 4991db9f3b2SDimitry Andric } break; 5001db9f3b2SDimitry Andric } 5011db9f3b2SDimitry Andric CurExprOffset = Op.getEndOffset(); 5021db9f3b2SDimitry Andric } 5031db9f3b2SDimitry Andric 5041db9f3b2SDimitry Andric return std::make_pair(HasLocationAddress, std::nullopt); 5051db9f3b2SDimitry Andric } 5061db9f3b2SDimitry Andric 5071db9f3b2SDimitry Andric /// Check if a variable describing DIE should be kept. 5081db9f3b2SDimitry Andric /// \returns updated TraversalFlags. 5091db9f3b2SDimitry Andric unsigned DWARFLinker::shouldKeepVariableDIE(AddressesMap &RelocMgr, 5101db9f3b2SDimitry Andric const DWARFDie &DIE, 5111db9f3b2SDimitry Andric CompileUnit::DIEInfo &MyInfo, 5121db9f3b2SDimitry Andric unsigned Flags) { 5131db9f3b2SDimitry Andric const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); 5141db9f3b2SDimitry Andric 5151db9f3b2SDimitry Andric // Global variables with constant value can always be kept. 5161db9f3b2SDimitry Andric if (!(Flags & TF_InFunctionScope) && 5171db9f3b2SDimitry Andric Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) { 5181db9f3b2SDimitry Andric MyInfo.InDebugMap = true; 5191db9f3b2SDimitry Andric return Flags | TF_Keep; 5201db9f3b2SDimitry Andric } 5211db9f3b2SDimitry Andric 5221db9f3b2SDimitry Andric // See if there is a relocation to a valid debug map entry inside this 5231db9f3b2SDimitry Andric // variable's location. The order is important here. We want to always check 5241db9f3b2SDimitry Andric // if the variable has a valid relocation, so that the DIEInfo is filled. 5251db9f3b2SDimitry Andric // However, we don't want a static variable in a function to force us to keep 5261db9f3b2SDimitry Andric // the enclosing function, unless requested explicitly. 5271db9f3b2SDimitry Andric std::pair<bool, std::optional<int64_t>> LocExprAddrAndRelocAdjustment = 5281db9f3b2SDimitry Andric getVariableRelocAdjustment(RelocMgr, DIE); 5291db9f3b2SDimitry Andric 5301db9f3b2SDimitry Andric if (LocExprAddrAndRelocAdjustment.first) 5311db9f3b2SDimitry Andric MyInfo.HasLocationExpressionAddr = true; 5321db9f3b2SDimitry Andric 5331db9f3b2SDimitry Andric if (!LocExprAddrAndRelocAdjustment.second) 5341db9f3b2SDimitry Andric return Flags; 5351db9f3b2SDimitry Andric 5361db9f3b2SDimitry Andric MyInfo.AddrAdjust = *LocExprAddrAndRelocAdjustment.second; 5371db9f3b2SDimitry Andric MyInfo.InDebugMap = true; 5381db9f3b2SDimitry Andric 5391db9f3b2SDimitry Andric if (((Flags & TF_InFunctionScope) && 5401db9f3b2SDimitry Andric !LLVM_UNLIKELY(Options.KeepFunctionForStatic))) 5411db9f3b2SDimitry Andric return Flags; 5421db9f3b2SDimitry Andric 5431db9f3b2SDimitry Andric if (Options.Verbose) { 5441db9f3b2SDimitry Andric outs() << "Keeping variable DIE:"; 5451db9f3b2SDimitry Andric DIDumpOptions DumpOpts; 5461db9f3b2SDimitry Andric DumpOpts.ChildRecurseDepth = 0; 5471db9f3b2SDimitry Andric DumpOpts.Verbose = Options.Verbose; 5481db9f3b2SDimitry Andric DIE.dump(outs(), 8 /* Indent */, DumpOpts); 5491db9f3b2SDimitry Andric } 5501db9f3b2SDimitry Andric 5511db9f3b2SDimitry Andric return Flags | TF_Keep; 5521db9f3b2SDimitry Andric } 5531db9f3b2SDimitry Andric 5541db9f3b2SDimitry Andric /// Check if a function describing DIE should be kept. 5551db9f3b2SDimitry Andric /// \returns updated TraversalFlags. 5561db9f3b2SDimitry Andric unsigned DWARFLinker::shouldKeepSubprogramDIE( 5571db9f3b2SDimitry Andric AddressesMap &RelocMgr, const DWARFDie &DIE, const DWARFFile &File, 5581db9f3b2SDimitry Andric CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo, unsigned Flags) { 5591db9f3b2SDimitry Andric Flags |= TF_InFunctionScope; 5601db9f3b2SDimitry Andric 5611db9f3b2SDimitry Andric auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc)); 5621db9f3b2SDimitry Andric if (!LowPc) 5631db9f3b2SDimitry Andric return Flags; 5641db9f3b2SDimitry Andric 5651db9f3b2SDimitry Andric assert(LowPc && "low_pc attribute is not an address."); 5661db9f3b2SDimitry Andric std::optional<int64_t> RelocAdjustment = 5677a6dacacSDimitry Andric RelocMgr.getSubprogramRelocAdjustment(DIE, Options.Verbose); 5681db9f3b2SDimitry Andric if (!RelocAdjustment) 5691db9f3b2SDimitry Andric return Flags; 5701db9f3b2SDimitry Andric 5711db9f3b2SDimitry Andric MyInfo.AddrAdjust = *RelocAdjustment; 5721db9f3b2SDimitry Andric MyInfo.InDebugMap = true; 5731db9f3b2SDimitry Andric 5741db9f3b2SDimitry Andric if (Options.Verbose) { 5751db9f3b2SDimitry Andric outs() << "Keeping subprogram DIE:"; 5761db9f3b2SDimitry Andric DIDumpOptions DumpOpts; 5771db9f3b2SDimitry Andric DumpOpts.ChildRecurseDepth = 0; 5781db9f3b2SDimitry Andric DumpOpts.Verbose = Options.Verbose; 5791db9f3b2SDimitry Andric DIE.dump(outs(), 8 /* Indent */, DumpOpts); 5801db9f3b2SDimitry Andric } 5811db9f3b2SDimitry Andric 5821db9f3b2SDimitry Andric if (DIE.getTag() == dwarf::DW_TAG_label) { 5831db9f3b2SDimitry Andric if (Unit.hasLabelAt(*LowPc)) 5841db9f3b2SDimitry Andric return Flags; 5851db9f3b2SDimitry Andric 5861db9f3b2SDimitry Andric DWARFUnit &OrigUnit = Unit.getOrigUnit(); 5871db9f3b2SDimitry Andric // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels 5881db9f3b2SDimitry Andric // that don't fall into the CU's aranges. This is wrong IMO. Debug info 5891db9f3b2SDimitry Andric // generation bugs aside, this is really wrong in the case of labels, where 5901db9f3b2SDimitry Andric // a label marking the end of a function will have a PC == CU's high_pc. 5911db9f3b2SDimitry Andric if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc)) 5921db9f3b2SDimitry Andric .value_or(UINT64_MAX) <= LowPc) 5931db9f3b2SDimitry Andric return Flags; 5941db9f3b2SDimitry Andric Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust); 5951db9f3b2SDimitry Andric return Flags | TF_Keep; 5961db9f3b2SDimitry Andric } 5971db9f3b2SDimitry Andric 5981db9f3b2SDimitry Andric Flags |= TF_Keep; 5991db9f3b2SDimitry Andric 6001db9f3b2SDimitry Andric std::optional<uint64_t> HighPc = DIE.getHighPC(*LowPc); 6011db9f3b2SDimitry Andric if (!HighPc) { 6021db9f3b2SDimitry Andric reportWarning("Function without high_pc. Range will be discarded.\n", File, 6031db9f3b2SDimitry Andric &DIE); 6041db9f3b2SDimitry Andric return Flags; 6051db9f3b2SDimitry Andric } 6061db9f3b2SDimitry Andric if (*LowPc > *HighPc) { 6071db9f3b2SDimitry Andric reportWarning("low_pc greater than high_pc. Range will be discarded.\n", 6081db9f3b2SDimitry Andric File, &DIE); 6091db9f3b2SDimitry Andric return Flags; 6101db9f3b2SDimitry Andric } 6111db9f3b2SDimitry Andric 6121db9f3b2SDimitry Andric // Replace the debug map range with a more accurate one. 6131db9f3b2SDimitry Andric Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust); 6141db9f3b2SDimitry Andric return Flags; 6151db9f3b2SDimitry Andric } 6161db9f3b2SDimitry Andric 6171db9f3b2SDimitry Andric /// Check if a DIE should be kept. 6181db9f3b2SDimitry Andric /// \returns updated TraversalFlags. 6191db9f3b2SDimitry Andric unsigned DWARFLinker::shouldKeepDIE(AddressesMap &RelocMgr, const DWARFDie &DIE, 6201db9f3b2SDimitry Andric const DWARFFile &File, CompileUnit &Unit, 6211db9f3b2SDimitry Andric CompileUnit::DIEInfo &MyInfo, 6221db9f3b2SDimitry Andric unsigned Flags) { 6231db9f3b2SDimitry Andric switch (DIE.getTag()) { 6241db9f3b2SDimitry Andric case dwarf::DW_TAG_constant: 6251db9f3b2SDimitry Andric case dwarf::DW_TAG_variable: 6261db9f3b2SDimitry Andric return shouldKeepVariableDIE(RelocMgr, DIE, MyInfo, Flags); 6271db9f3b2SDimitry Andric case dwarf::DW_TAG_subprogram: 6281db9f3b2SDimitry Andric case dwarf::DW_TAG_label: 6291db9f3b2SDimitry Andric return shouldKeepSubprogramDIE(RelocMgr, DIE, File, Unit, MyInfo, Flags); 6301db9f3b2SDimitry Andric case dwarf::DW_TAG_base_type: 6311db9f3b2SDimitry Andric // DWARF Expressions may reference basic types, but scanning them 6321db9f3b2SDimitry Andric // is expensive. Basic types are tiny, so just keep all of them. 6331db9f3b2SDimitry Andric case dwarf::DW_TAG_imported_module: 6341db9f3b2SDimitry Andric case dwarf::DW_TAG_imported_declaration: 6351db9f3b2SDimitry Andric case dwarf::DW_TAG_imported_unit: 6361db9f3b2SDimitry Andric // We always want to keep these. 6371db9f3b2SDimitry Andric return Flags | TF_Keep; 6381db9f3b2SDimitry Andric default: 6391db9f3b2SDimitry Andric break; 6401db9f3b2SDimitry Andric } 6411db9f3b2SDimitry Andric 6421db9f3b2SDimitry Andric return Flags; 6431db9f3b2SDimitry Andric } 6441db9f3b2SDimitry Andric 6451db9f3b2SDimitry Andric /// Helper that updates the completeness of the current DIE based on the 6461db9f3b2SDimitry Andric /// completeness of one of its children. It depends on the incompleteness of 6471db9f3b2SDimitry Andric /// the children already being computed. 6481db9f3b2SDimitry Andric static void updateChildIncompleteness(const DWARFDie &Die, CompileUnit &CU, 6491db9f3b2SDimitry Andric CompileUnit::DIEInfo &ChildInfo) { 6501db9f3b2SDimitry Andric switch (Die.getTag()) { 6511db9f3b2SDimitry Andric case dwarf::DW_TAG_structure_type: 6521db9f3b2SDimitry Andric case dwarf::DW_TAG_class_type: 6531db9f3b2SDimitry Andric case dwarf::DW_TAG_union_type: 6541db9f3b2SDimitry Andric break; 6551db9f3b2SDimitry Andric default: 6561db9f3b2SDimitry Andric return; 6571db9f3b2SDimitry Andric } 6581db9f3b2SDimitry Andric 6591db9f3b2SDimitry Andric CompileUnit::DIEInfo &MyInfo = CU.getInfo(Die); 6601db9f3b2SDimitry Andric 6611db9f3b2SDimitry Andric if (ChildInfo.Incomplete || ChildInfo.Prune) 6621db9f3b2SDimitry Andric MyInfo.Incomplete = true; 6631db9f3b2SDimitry Andric } 6641db9f3b2SDimitry Andric 6651db9f3b2SDimitry Andric /// Helper that updates the completeness of the current DIE based on the 6661db9f3b2SDimitry Andric /// completeness of the DIEs it references. It depends on the incompleteness of 6671db9f3b2SDimitry Andric /// the referenced DIE already being computed. 6681db9f3b2SDimitry Andric static void updateRefIncompleteness(const DWARFDie &Die, CompileUnit &CU, 6691db9f3b2SDimitry Andric CompileUnit::DIEInfo &RefInfo) { 6701db9f3b2SDimitry Andric switch (Die.getTag()) { 6711db9f3b2SDimitry Andric case dwarf::DW_TAG_typedef: 6721db9f3b2SDimitry Andric case dwarf::DW_TAG_member: 6731db9f3b2SDimitry Andric case dwarf::DW_TAG_reference_type: 6741db9f3b2SDimitry Andric case dwarf::DW_TAG_ptr_to_member_type: 6751db9f3b2SDimitry Andric case dwarf::DW_TAG_pointer_type: 6761db9f3b2SDimitry Andric break; 6771db9f3b2SDimitry Andric default: 6781db9f3b2SDimitry Andric return; 6791db9f3b2SDimitry Andric } 6801db9f3b2SDimitry Andric 6811db9f3b2SDimitry Andric CompileUnit::DIEInfo &MyInfo = CU.getInfo(Die); 6821db9f3b2SDimitry Andric 6831db9f3b2SDimitry Andric if (MyInfo.Incomplete) 6841db9f3b2SDimitry Andric return; 6851db9f3b2SDimitry Andric 6861db9f3b2SDimitry Andric if (RefInfo.Incomplete) 6871db9f3b2SDimitry Andric MyInfo.Incomplete = true; 6881db9f3b2SDimitry Andric } 6891db9f3b2SDimitry Andric 6901db9f3b2SDimitry Andric /// Look at the children of the given DIE and decide whether they should be 6911db9f3b2SDimitry Andric /// kept. 6921db9f3b2SDimitry Andric void DWARFLinker::lookForChildDIEsToKeep( 6931db9f3b2SDimitry Andric const DWARFDie &Die, CompileUnit &CU, unsigned Flags, 6941db9f3b2SDimitry Andric SmallVectorImpl<WorklistItem> &Worklist) { 6951db9f3b2SDimitry Andric // The TF_ParentWalk flag tells us that we are currently walking up the 6961db9f3b2SDimitry Andric // parent chain of a required DIE, and we don't want to mark all the children 6971db9f3b2SDimitry Andric // of the parents as kept (consider for example a DW_TAG_namespace node in 6981db9f3b2SDimitry Andric // the parent chain). There are however a set of DIE types for which we want 6991db9f3b2SDimitry Andric // to ignore that directive and still walk their children. 7001db9f3b2SDimitry Andric if (dieNeedsChildrenToBeMeaningful(Die.getTag())) 7011db9f3b2SDimitry Andric Flags &= ~DWARFLinker::TF_ParentWalk; 7021db9f3b2SDimitry Andric 7031db9f3b2SDimitry Andric // We're finished if this DIE has no children or we're walking the parent 7041db9f3b2SDimitry Andric // chain. 7051db9f3b2SDimitry Andric if (!Die.hasChildren() || (Flags & DWARFLinker::TF_ParentWalk)) 7061db9f3b2SDimitry Andric return; 7071db9f3b2SDimitry Andric 7081db9f3b2SDimitry Andric // Add children in reverse order to the worklist to effectively process them 7091db9f3b2SDimitry Andric // in order. 7101db9f3b2SDimitry Andric for (auto Child : reverse(Die.children())) { 7111db9f3b2SDimitry Andric // Add a worklist item before every child to calculate incompleteness right 7121db9f3b2SDimitry Andric // after the current child is processed. 7131db9f3b2SDimitry Andric CompileUnit::DIEInfo &ChildInfo = CU.getInfo(Child); 7141db9f3b2SDimitry Andric Worklist.emplace_back(Die, CU, WorklistItemType::UpdateChildIncompleteness, 7151db9f3b2SDimitry Andric &ChildInfo); 7161db9f3b2SDimitry Andric Worklist.emplace_back(Child, CU, Flags); 7171db9f3b2SDimitry Andric } 7181db9f3b2SDimitry Andric } 7191db9f3b2SDimitry Andric 7201db9f3b2SDimitry Andric static bool isODRCanonicalCandidate(const DWARFDie &Die, CompileUnit &CU) { 7211db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = CU.getInfo(Die); 7221db9f3b2SDimitry Andric 7231db9f3b2SDimitry Andric if (!Info.Ctxt || (Die.getTag() == dwarf::DW_TAG_namespace)) 7241db9f3b2SDimitry Andric return false; 7251db9f3b2SDimitry Andric 7261db9f3b2SDimitry Andric if (!CU.hasODR() && !Info.InModuleScope) 7271db9f3b2SDimitry Andric return false; 7281db9f3b2SDimitry Andric 7291db9f3b2SDimitry Andric return !Info.Incomplete && Info.Ctxt != CU.getInfo(Info.ParentIdx).Ctxt; 7301db9f3b2SDimitry Andric } 7311db9f3b2SDimitry Andric 7321db9f3b2SDimitry Andric void DWARFLinker::markODRCanonicalDie(const DWARFDie &Die, CompileUnit &CU) { 7331db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = CU.getInfo(Die); 7341db9f3b2SDimitry Andric 7351db9f3b2SDimitry Andric Info.ODRMarkingDone = true; 7361db9f3b2SDimitry Andric if (Info.Keep && isODRCanonicalCandidate(Die, CU) && 7371db9f3b2SDimitry Andric !Info.Ctxt->hasCanonicalDIE()) 7381db9f3b2SDimitry Andric Info.Ctxt->setHasCanonicalDIE(); 7391db9f3b2SDimitry Andric } 7401db9f3b2SDimitry Andric 7411db9f3b2SDimitry Andric /// Look at DIEs referenced by the given DIE and decide whether they should be 7421db9f3b2SDimitry Andric /// kept. All DIEs referenced though attributes should be kept. 7431db9f3b2SDimitry Andric void DWARFLinker::lookForRefDIEsToKeep( 7441db9f3b2SDimitry Andric const DWARFDie &Die, CompileUnit &CU, unsigned Flags, 7451db9f3b2SDimitry Andric const UnitListTy &Units, const DWARFFile &File, 7461db9f3b2SDimitry Andric SmallVectorImpl<WorklistItem> &Worklist) { 7471db9f3b2SDimitry Andric bool UseOdr = (Flags & DWARFLinker::TF_DependencyWalk) 7481db9f3b2SDimitry Andric ? (Flags & DWARFLinker::TF_ODR) 7491db9f3b2SDimitry Andric : CU.hasODR(); 7501db9f3b2SDimitry Andric DWARFUnit &Unit = CU.getOrigUnit(); 7511db9f3b2SDimitry Andric DWARFDataExtractor Data = Unit.getDebugInfoExtractor(); 7521db9f3b2SDimitry Andric const auto *Abbrev = Die.getAbbreviationDeclarationPtr(); 7531db9f3b2SDimitry Andric uint64_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode()); 7541db9f3b2SDimitry Andric 7551db9f3b2SDimitry Andric SmallVector<std::pair<DWARFDie, CompileUnit &>, 4> ReferencedDIEs; 7561db9f3b2SDimitry Andric for (const auto &AttrSpec : Abbrev->attributes()) { 7571db9f3b2SDimitry Andric DWARFFormValue Val(AttrSpec.Form); 7581db9f3b2SDimitry Andric if (!Val.isFormClass(DWARFFormValue::FC_Reference) || 7591db9f3b2SDimitry Andric AttrSpec.Attr == dwarf::DW_AT_sibling) { 7601db9f3b2SDimitry Andric DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, 7611db9f3b2SDimitry Andric Unit.getFormParams()); 7621db9f3b2SDimitry Andric continue; 7631db9f3b2SDimitry Andric } 7641db9f3b2SDimitry Andric 7651db9f3b2SDimitry Andric Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit); 7661db9f3b2SDimitry Andric CompileUnit *ReferencedCU; 7671db9f3b2SDimitry Andric if (auto RefDie = 7681db9f3b2SDimitry Andric resolveDIEReference(File, Units, Val, Die, ReferencedCU)) { 7691db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefDie); 7701db9f3b2SDimitry Andric // If the referenced DIE has a DeclContext that has already been 7711db9f3b2SDimitry Andric // emitted, then do not keep the one in this CU. We'll link to 7721db9f3b2SDimitry Andric // the canonical DIE in cloneDieReferenceAttribute. 7731db9f3b2SDimitry Andric // 7741db9f3b2SDimitry Andric // FIXME: compatibility with dsymutil-classic. UseODR shouldn't 7751db9f3b2SDimitry Andric // be necessary and could be advantageously replaced by 7761db9f3b2SDimitry Andric // ReferencedCU->hasODR() && CU.hasODR(). 7771db9f3b2SDimitry Andric // 7781db9f3b2SDimitry Andric // FIXME: compatibility with dsymutil-classic. There is no 7791db9f3b2SDimitry Andric // reason not to unique ref_addr references. 7801db9f3b2SDimitry Andric if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && 7811db9f3b2SDimitry Andric isODRAttribute(AttrSpec.Attr) && Info.Ctxt && 7821db9f3b2SDimitry Andric Info.Ctxt->hasCanonicalDIE()) 7831db9f3b2SDimitry Andric continue; 7841db9f3b2SDimitry Andric 7851db9f3b2SDimitry Andric // Keep a module forward declaration if there is no definition. 7861db9f3b2SDimitry Andric if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt && 7871db9f3b2SDimitry Andric Info.Ctxt->hasCanonicalDIE())) 7881db9f3b2SDimitry Andric Info.Prune = false; 7891db9f3b2SDimitry Andric ReferencedDIEs.emplace_back(RefDie, *ReferencedCU); 7901db9f3b2SDimitry Andric } 7911db9f3b2SDimitry Andric } 7921db9f3b2SDimitry Andric 7931db9f3b2SDimitry Andric unsigned ODRFlag = UseOdr ? DWARFLinker::TF_ODR : 0; 7941db9f3b2SDimitry Andric 7951db9f3b2SDimitry Andric // Add referenced DIEs in reverse order to the worklist to effectively 7961db9f3b2SDimitry Andric // process them in order. 7971db9f3b2SDimitry Andric for (auto &P : reverse(ReferencedDIEs)) { 7981db9f3b2SDimitry Andric // Add a worklist item before every child to calculate incompleteness right 7991db9f3b2SDimitry Andric // after the current child is processed. 8001db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = P.second.getInfo(P.first); 8011db9f3b2SDimitry Andric Worklist.emplace_back(Die, CU, WorklistItemType::UpdateRefIncompleteness, 8021db9f3b2SDimitry Andric &Info); 8031db9f3b2SDimitry Andric Worklist.emplace_back(P.first, P.second, 8041db9f3b2SDimitry Andric DWARFLinker::TF_Keep | 8051db9f3b2SDimitry Andric DWARFLinker::TF_DependencyWalk | ODRFlag); 8061db9f3b2SDimitry Andric } 8071db9f3b2SDimitry Andric } 8081db9f3b2SDimitry Andric 8091db9f3b2SDimitry Andric /// Look at the parent of the given DIE and decide whether they should be kept. 8101db9f3b2SDimitry Andric void DWARFLinker::lookForParentDIEsToKeep( 8111db9f3b2SDimitry Andric unsigned AncestorIdx, CompileUnit &CU, unsigned Flags, 8121db9f3b2SDimitry Andric SmallVectorImpl<WorklistItem> &Worklist) { 8131db9f3b2SDimitry Andric // Stop if we encounter an ancestor that's already marked as kept. 8141db9f3b2SDimitry Andric if (CU.getInfo(AncestorIdx).Keep) 8151db9f3b2SDimitry Andric return; 8161db9f3b2SDimitry Andric 8171db9f3b2SDimitry Andric DWARFUnit &Unit = CU.getOrigUnit(); 8181db9f3b2SDimitry Andric DWARFDie ParentDIE = Unit.getDIEAtIndex(AncestorIdx); 8191db9f3b2SDimitry Andric Worklist.emplace_back(CU.getInfo(AncestorIdx).ParentIdx, CU, Flags); 8201db9f3b2SDimitry Andric Worklist.emplace_back(ParentDIE, CU, Flags); 8211db9f3b2SDimitry Andric } 8221db9f3b2SDimitry Andric 8231db9f3b2SDimitry Andric /// Recursively walk the \p DIE tree and look for DIEs to keep. Store that 8241db9f3b2SDimitry Andric /// information in \p CU's DIEInfo. 8251db9f3b2SDimitry Andric /// 8261db9f3b2SDimitry Andric /// This function is the entry point of the DIE selection algorithm. It is 8271db9f3b2SDimitry Andric /// expected to walk the DIE tree in file order and (though the mediation of 8281db9f3b2SDimitry Andric /// its helper) call hasValidRelocation() on each DIE that might be a 'root 8291db9f3b2SDimitry Andric /// DIE' (See DwarfLinker class comment). 8301db9f3b2SDimitry Andric /// 8311db9f3b2SDimitry Andric /// While walking the dependencies of root DIEs, this function is also called, 8321db9f3b2SDimitry Andric /// but during these dependency walks the file order is not respected. The 8331db9f3b2SDimitry Andric /// TF_DependencyWalk flag tells us which kind of traversal we are currently 8341db9f3b2SDimitry Andric /// doing. 8351db9f3b2SDimitry Andric /// 8361db9f3b2SDimitry Andric /// The recursive algorithm is implemented iteratively as a work list because 8371db9f3b2SDimitry Andric /// very deep recursion could exhaust the stack for large projects. The work 8381db9f3b2SDimitry Andric /// list acts as a scheduler for different types of work that need to be 8391db9f3b2SDimitry Andric /// performed. 8401db9f3b2SDimitry Andric /// 8411db9f3b2SDimitry Andric /// The recursive nature of the algorithm is simulated by running the "main" 8421db9f3b2SDimitry Andric /// algorithm (LookForDIEsToKeep) followed by either looking at more DIEs 8431db9f3b2SDimitry Andric /// (LookForChildDIEsToKeep, LookForRefDIEsToKeep, LookForParentDIEsToKeep) or 8441db9f3b2SDimitry Andric /// fixing up a computed property (UpdateChildIncompleteness, 8451db9f3b2SDimitry Andric /// UpdateRefIncompleteness). 8461db9f3b2SDimitry Andric /// 8471db9f3b2SDimitry Andric /// The return value indicates whether the DIE is incomplete. 8481db9f3b2SDimitry Andric void DWARFLinker::lookForDIEsToKeep(AddressesMap &AddressesMap, 8491db9f3b2SDimitry Andric const UnitListTy &Units, 8501db9f3b2SDimitry Andric const DWARFDie &Die, const DWARFFile &File, 8511db9f3b2SDimitry Andric CompileUnit &Cu, unsigned Flags) { 8521db9f3b2SDimitry Andric // LIFO work list. 8531db9f3b2SDimitry Andric SmallVector<WorklistItem, 4> Worklist; 8541db9f3b2SDimitry Andric Worklist.emplace_back(Die, Cu, Flags); 8551db9f3b2SDimitry Andric 8561db9f3b2SDimitry Andric while (!Worklist.empty()) { 8571db9f3b2SDimitry Andric WorklistItem Current = Worklist.pop_back_val(); 8581db9f3b2SDimitry Andric 8591db9f3b2SDimitry Andric // Look at the worklist type to decide what kind of work to perform. 8601db9f3b2SDimitry Andric switch (Current.Type) { 8611db9f3b2SDimitry Andric case WorklistItemType::UpdateChildIncompleteness: 8621db9f3b2SDimitry Andric updateChildIncompleteness(Current.Die, Current.CU, *Current.OtherInfo); 8631db9f3b2SDimitry Andric continue; 8641db9f3b2SDimitry Andric case WorklistItemType::UpdateRefIncompleteness: 8651db9f3b2SDimitry Andric updateRefIncompleteness(Current.Die, Current.CU, *Current.OtherInfo); 8661db9f3b2SDimitry Andric continue; 8671db9f3b2SDimitry Andric case WorklistItemType::LookForChildDIEsToKeep: 8681db9f3b2SDimitry Andric lookForChildDIEsToKeep(Current.Die, Current.CU, Current.Flags, Worklist); 8691db9f3b2SDimitry Andric continue; 8701db9f3b2SDimitry Andric case WorklistItemType::LookForRefDIEsToKeep: 8711db9f3b2SDimitry Andric lookForRefDIEsToKeep(Current.Die, Current.CU, Current.Flags, Units, File, 8721db9f3b2SDimitry Andric Worklist); 8731db9f3b2SDimitry Andric continue; 8741db9f3b2SDimitry Andric case WorklistItemType::LookForParentDIEsToKeep: 8751db9f3b2SDimitry Andric lookForParentDIEsToKeep(Current.AncestorIdx, Current.CU, Current.Flags, 8761db9f3b2SDimitry Andric Worklist); 8771db9f3b2SDimitry Andric continue; 8781db9f3b2SDimitry Andric case WorklistItemType::MarkODRCanonicalDie: 8791db9f3b2SDimitry Andric markODRCanonicalDie(Current.Die, Current.CU); 8801db9f3b2SDimitry Andric continue; 8811db9f3b2SDimitry Andric case WorklistItemType::LookForDIEsToKeep: 8821db9f3b2SDimitry Andric break; 8831db9f3b2SDimitry Andric } 8841db9f3b2SDimitry Andric 8851db9f3b2SDimitry Andric unsigned Idx = Current.CU.getOrigUnit().getDIEIndex(Current.Die); 8861db9f3b2SDimitry Andric CompileUnit::DIEInfo &MyInfo = Current.CU.getInfo(Idx); 8871db9f3b2SDimitry Andric 8881db9f3b2SDimitry Andric if (MyInfo.Prune) { 8891db9f3b2SDimitry Andric // We're walking the dependencies of a module forward declaration that was 8901db9f3b2SDimitry Andric // kept because there is no definition. 8911db9f3b2SDimitry Andric if (Current.Flags & TF_DependencyWalk) 8921db9f3b2SDimitry Andric MyInfo.Prune = false; 8931db9f3b2SDimitry Andric else 8941db9f3b2SDimitry Andric continue; 8951db9f3b2SDimitry Andric } 8961db9f3b2SDimitry Andric 8971db9f3b2SDimitry Andric // If the Keep flag is set, we are marking a required DIE's dependencies. 8981db9f3b2SDimitry Andric // If our target is already marked as kept, we're all set. 8991db9f3b2SDimitry Andric bool AlreadyKept = MyInfo.Keep; 9001db9f3b2SDimitry Andric if ((Current.Flags & TF_DependencyWalk) && AlreadyKept) 9011db9f3b2SDimitry Andric continue; 9021db9f3b2SDimitry Andric 9031db9f3b2SDimitry Andric if (!(Current.Flags & TF_DependencyWalk)) 9041db9f3b2SDimitry Andric Current.Flags = shouldKeepDIE(AddressesMap, Current.Die, File, Current.CU, 9051db9f3b2SDimitry Andric MyInfo, Current.Flags); 9061db9f3b2SDimitry Andric 9071db9f3b2SDimitry Andric // We need to mark context for the canonical die in the end of normal 9081db9f3b2SDimitry Andric // traversing(not TF_DependencyWalk) or after normal traversing if die 9091db9f3b2SDimitry Andric // was not marked as kept. 9101db9f3b2SDimitry Andric if (!(Current.Flags & TF_DependencyWalk) || 9111db9f3b2SDimitry Andric (MyInfo.ODRMarkingDone && !MyInfo.Keep)) { 9121db9f3b2SDimitry Andric if (Current.CU.hasODR() || MyInfo.InModuleScope) 9131db9f3b2SDimitry Andric Worklist.emplace_back(Current.Die, Current.CU, 9141db9f3b2SDimitry Andric WorklistItemType::MarkODRCanonicalDie); 9151db9f3b2SDimitry Andric } 9161db9f3b2SDimitry Andric 9171db9f3b2SDimitry Andric // Finish by looking for child DIEs. Because of the LIFO worklist we need 9181db9f3b2SDimitry Andric // to schedule that work before any subsequent items are added to the 9191db9f3b2SDimitry Andric // worklist. 9201db9f3b2SDimitry Andric Worklist.emplace_back(Current.Die, Current.CU, Current.Flags, 9211db9f3b2SDimitry Andric WorklistItemType::LookForChildDIEsToKeep); 9221db9f3b2SDimitry Andric 9231db9f3b2SDimitry Andric if (AlreadyKept || !(Current.Flags & TF_Keep)) 9241db9f3b2SDimitry Andric continue; 9251db9f3b2SDimitry Andric 9261db9f3b2SDimitry Andric // If it is a newly kept DIE mark it as well as all its dependencies as 9271db9f3b2SDimitry Andric // kept. 9281db9f3b2SDimitry Andric MyInfo.Keep = true; 9291db9f3b2SDimitry Andric 9301db9f3b2SDimitry Andric // We're looking for incomplete types. 9311db9f3b2SDimitry Andric MyInfo.Incomplete = 9321db9f3b2SDimitry Andric Current.Die.getTag() != dwarf::DW_TAG_subprogram && 9331db9f3b2SDimitry Andric Current.Die.getTag() != dwarf::DW_TAG_member && 9341db9f3b2SDimitry Andric dwarf::toUnsigned(Current.Die.find(dwarf::DW_AT_declaration), 0); 9351db9f3b2SDimitry Andric 9361db9f3b2SDimitry Andric // After looking at the parent chain, look for referenced DIEs. Because of 9371db9f3b2SDimitry Andric // the LIFO worklist we need to schedule that work before any subsequent 9381db9f3b2SDimitry Andric // items are added to the worklist. 9391db9f3b2SDimitry Andric Worklist.emplace_back(Current.Die, Current.CU, Current.Flags, 9401db9f3b2SDimitry Andric WorklistItemType::LookForRefDIEsToKeep); 9411db9f3b2SDimitry Andric 9421db9f3b2SDimitry Andric bool UseOdr = (Current.Flags & TF_DependencyWalk) ? (Current.Flags & TF_ODR) 9431db9f3b2SDimitry Andric : Current.CU.hasODR(); 9441db9f3b2SDimitry Andric unsigned ODRFlag = UseOdr ? TF_ODR : 0; 9451db9f3b2SDimitry Andric unsigned ParFlags = TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag; 9461db9f3b2SDimitry Andric 9471db9f3b2SDimitry Andric // Now schedule the parent walk. 9481db9f3b2SDimitry Andric Worklist.emplace_back(MyInfo.ParentIdx, Current.CU, ParFlags); 9491db9f3b2SDimitry Andric } 9501db9f3b2SDimitry Andric } 9511db9f3b2SDimitry Andric 9521db9f3b2SDimitry Andric #ifndef NDEBUG 9531db9f3b2SDimitry Andric /// A broken link in the keep chain. By recording both the parent and the child 9541db9f3b2SDimitry Andric /// we can show only broken links for DIEs with multiple children. 9551db9f3b2SDimitry Andric struct BrokenLink { 9561db9f3b2SDimitry Andric BrokenLink(DWARFDie Parent, DWARFDie Child) : Parent(Parent), Child(Child) {} 9571db9f3b2SDimitry Andric DWARFDie Parent; 9581db9f3b2SDimitry Andric DWARFDie Child; 9591db9f3b2SDimitry Andric }; 9601db9f3b2SDimitry Andric 9611db9f3b2SDimitry Andric /// Verify the keep chain by looking for DIEs that are kept but who's parent 9621db9f3b2SDimitry Andric /// isn't. 9631db9f3b2SDimitry Andric static void verifyKeepChain(CompileUnit &CU) { 9641db9f3b2SDimitry Andric std::vector<DWARFDie> Worklist; 9651db9f3b2SDimitry Andric Worklist.push_back(CU.getOrigUnit().getUnitDIE()); 9661db9f3b2SDimitry Andric 9671db9f3b2SDimitry Andric // List of broken links. 9681db9f3b2SDimitry Andric std::vector<BrokenLink> BrokenLinks; 9691db9f3b2SDimitry Andric 9701db9f3b2SDimitry Andric while (!Worklist.empty()) { 9711db9f3b2SDimitry Andric const DWARFDie Current = Worklist.back(); 9721db9f3b2SDimitry Andric Worklist.pop_back(); 9731db9f3b2SDimitry Andric 9741db9f3b2SDimitry Andric const bool CurrentDieIsKept = CU.getInfo(Current).Keep; 9751db9f3b2SDimitry Andric 9761db9f3b2SDimitry Andric for (DWARFDie Child : reverse(Current.children())) { 9771db9f3b2SDimitry Andric Worklist.push_back(Child); 9781db9f3b2SDimitry Andric 9791db9f3b2SDimitry Andric const bool ChildDieIsKept = CU.getInfo(Child).Keep; 9801db9f3b2SDimitry Andric if (!CurrentDieIsKept && ChildDieIsKept) 9811db9f3b2SDimitry Andric BrokenLinks.emplace_back(Current, Child); 9821db9f3b2SDimitry Andric } 9831db9f3b2SDimitry Andric } 9841db9f3b2SDimitry Andric 9851db9f3b2SDimitry Andric if (!BrokenLinks.empty()) { 9861db9f3b2SDimitry Andric for (BrokenLink Link : BrokenLinks) { 9871db9f3b2SDimitry Andric WithColor::error() << formatv( 9881db9f3b2SDimitry Andric "Found invalid link in keep chain between {0:x} and {1:x}\n", 9891db9f3b2SDimitry Andric Link.Parent.getOffset(), Link.Child.getOffset()); 9901db9f3b2SDimitry Andric 9911db9f3b2SDimitry Andric errs() << "Parent:"; 9921db9f3b2SDimitry Andric Link.Parent.dump(errs(), 0, {}); 9931db9f3b2SDimitry Andric CU.getInfo(Link.Parent).dump(); 9941db9f3b2SDimitry Andric 9951db9f3b2SDimitry Andric errs() << "Child:"; 9961db9f3b2SDimitry Andric Link.Child.dump(errs(), 2, {}); 9971db9f3b2SDimitry Andric CU.getInfo(Link.Child).dump(); 9981db9f3b2SDimitry Andric } 9991db9f3b2SDimitry Andric report_fatal_error("invalid keep chain"); 10001db9f3b2SDimitry Andric } 10011db9f3b2SDimitry Andric } 10021db9f3b2SDimitry Andric #endif 10031db9f3b2SDimitry Andric 10041db9f3b2SDimitry Andric /// Assign an abbreviation number to \p Abbrev. 10051db9f3b2SDimitry Andric /// 10061db9f3b2SDimitry Andric /// Our DIEs get freed after every DebugMapObject has been processed, 10071db9f3b2SDimitry Andric /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to 10081db9f3b2SDimitry Andric /// the instances hold by the DIEs. When we encounter an abbreviation 10091db9f3b2SDimitry Andric /// that we don't know, we create a permanent copy of it. 10101db9f3b2SDimitry Andric void DWARFLinker::assignAbbrev(DIEAbbrev &Abbrev) { 10111db9f3b2SDimitry Andric // Check the set for priors. 10121db9f3b2SDimitry Andric FoldingSetNodeID ID; 10131db9f3b2SDimitry Andric Abbrev.Profile(ID); 10141db9f3b2SDimitry Andric void *InsertToken; 10151db9f3b2SDimitry Andric DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken); 10161db9f3b2SDimitry Andric 10171db9f3b2SDimitry Andric // If it's newly added. 10181db9f3b2SDimitry Andric if (InSet) { 10191db9f3b2SDimitry Andric // Assign existing abbreviation number. 10201db9f3b2SDimitry Andric Abbrev.setNumber(InSet->getNumber()); 10211db9f3b2SDimitry Andric } else { 10221db9f3b2SDimitry Andric // Add to abbreviation list. 10231db9f3b2SDimitry Andric Abbreviations.push_back( 10241db9f3b2SDimitry Andric std::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren())); 10251db9f3b2SDimitry Andric for (const auto &Attr : Abbrev.getData()) 10261db9f3b2SDimitry Andric Abbreviations.back()->AddAttribute(Attr); 10271db9f3b2SDimitry Andric AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken); 10281db9f3b2SDimitry Andric // Assign the unique abbreviation number. 10291db9f3b2SDimitry Andric Abbrev.setNumber(Abbreviations.size()); 10301db9f3b2SDimitry Andric Abbreviations.back()->setNumber(Abbreviations.size()); 10311db9f3b2SDimitry Andric } 10321db9f3b2SDimitry Andric } 10331db9f3b2SDimitry Andric 10341db9f3b2SDimitry Andric unsigned DWARFLinker::DIECloner::cloneStringAttribute(DIE &Die, 10351db9f3b2SDimitry Andric AttributeSpec AttrSpec, 10361db9f3b2SDimitry Andric const DWARFFormValue &Val, 10371db9f3b2SDimitry Andric const DWARFUnit &U, 10381db9f3b2SDimitry Andric AttributesInfo &Info) { 10391db9f3b2SDimitry Andric std::optional<const char *> String = dwarf::toString(Val); 10401db9f3b2SDimitry Andric if (!String) 10411db9f3b2SDimitry Andric return 0; 10421db9f3b2SDimitry Andric DwarfStringPoolEntryRef StringEntry; 10431db9f3b2SDimitry Andric if (AttrSpec.Form == dwarf::DW_FORM_line_strp) { 10441db9f3b2SDimitry Andric StringEntry = DebugLineStrPool.getEntry(*String); 10451db9f3b2SDimitry Andric } else { 10461db9f3b2SDimitry Andric StringEntry = DebugStrPool.getEntry(*String); 10471db9f3b2SDimitry Andric 10481db9f3b2SDimitry Andric if (AttrSpec.Attr == dwarf::DW_AT_APPLE_origin) { 10491db9f3b2SDimitry Andric Info.HasAppleOrigin = true; 10501db9f3b2SDimitry Andric if (std::optional<StringRef> FileName = 10511db9f3b2SDimitry Andric ObjFile.Addresses->getLibraryInstallName()) { 10521db9f3b2SDimitry Andric StringEntry = DebugStrPool.getEntry(*FileName); 10531db9f3b2SDimitry Andric } 10541db9f3b2SDimitry Andric } 10551db9f3b2SDimitry Andric 10561db9f3b2SDimitry Andric // Update attributes info. 10571db9f3b2SDimitry Andric if (AttrSpec.Attr == dwarf::DW_AT_name) 10581db9f3b2SDimitry Andric Info.Name = StringEntry; 10591db9f3b2SDimitry Andric else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name || 10601db9f3b2SDimitry Andric AttrSpec.Attr == dwarf::DW_AT_linkage_name) 10611db9f3b2SDimitry Andric Info.MangledName = StringEntry; 10621db9f3b2SDimitry Andric if (U.getVersion() >= 5) { 10631db9f3b2SDimitry Andric // Switch everything to DW_FORM_strx strings. 10641db9f3b2SDimitry Andric auto StringOffsetIndex = 10651db9f3b2SDimitry Andric StringOffsetPool.getValueIndex(StringEntry.getOffset()); 10661db9f3b2SDimitry Andric return Die 10671db9f3b2SDimitry Andric .addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 10681db9f3b2SDimitry Andric dwarf::DW_FORM_strx, DIEInteger(StringOffsetIndex)) 10691db9f3b2SDimitry Andric ->sizeOf(U.getFormParams()); 10701db9f3b2SDimitry Andric } 10711db9f3b2SDimitry Andric // Switch everything to out of line strings. 10721db9f3b2SDimitry Andric AttrSpec.Form = dwarf::DW_FORM_strp; 10731db9f3b2SDimitry Andric } 10741db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), AttrSpec.Form, 10751db9f3b2SDimitry Andric DIEInteger(StringEntry.getOffset())); 10761db9f3b2SDimitry Andric return 4; 10771db9f3b2SDimitry Andric } 10781db9f3b2SDimitry Andric 10791db9f3b2SDimitry Andric unsigned DWARFLinker::DIECloner::cloneDieReferenceAttribute( 10801db9f3b2SDimitry Andric DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec, 10811db9f3b2SDimitry Andric unsigned AttrSize, const DWARFFormValue &Val, const DWARFFile &File, 10821db9f3b2SDimitry Andric CompileUnit &Unit) { 10831db9f3b2SDimitry Andric const DWARFUnit &U = Unit.getOrigUnit(); 1084*0fca6ea1SDimitry Andric uint64_t Ref; 1085*0fca6ea1SDimitry Andric if (std::optional<uint64_t> Off = Val.getAsRelativeReference()) 1086*0fca6ea1SDimitry Andric Ref = Val.getUnit()->getOffset() + *Off; 1087*0fca6ea1SDimitry Andric else if (Off = Val.getAsDebugInfoReference(); Off) 1088*0fca6ea1SDimitry Andric Ref = *Off; 1089*0fca6ea1SDimitry Andric else 1090*0fca6ea1SDimitry Andric return 0; 10911db9f3b2SDimitry Andric 10921db9f3b2SDimitry Andric DIE *NewRefDie = nullptr; 10931db9f3b2SDimitry Andric CompileUnit *RefUnit = nullptr; 10941db9f3b2SDimitry Andric 10951db9f3b2SDimitry Andric DWARFDie RefDie = 10961db9f3b2SDimitry Andric Linker.resolveDIEReference(File, CompileUnits, Val, InputDIE, RefUnit); 10971db9f3b2SDimitry Andric 10981db9f3b2SDimitry Andric // If the referenced DIE is not found, drop the attribute. 10991db9f3b2SDimitry Andric if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling) 11001db9f3b2SDimitry Andric return 0; 11011db9f3b2SDimitry Andric 11021db9f3b2SDimitry Andric CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(RefDie); 11031db9f3b2SDimitry Andric 11041db9f3b2SDimitry Andric // If we already have emitted an equivalent DeclContext, just point 11051db9f3b2SDimitry Andric // at it. 11061db9f3b2SDimitry Andric if (isODRAttribute(AttrSpec.Attr) && RefInfo.Ctxt && 11071db9f3b2SDimitry Andric RefInfo.Ctxt->getCanonicalDIEOffset()) { 11081db9f3b2SDimitry Andric assert(RefInfo.Ctxt->hasCanonicalDIE() && 11091db9f3b2SDimitry Andric "Offset to canonical die is set, but context is not marked"); 11101db9f3b2SDimitry Andric DIEInteger Attr(RefInfo.Ctxt->getCanonicalDIEOffset()); 11111db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 11121db9f3b2SDimitry Andric dwarf::DW_FORM_ref_addr, Attr); 11131db9f3b2SDimitry Andric return U.getRefAddrByteSize(); 11141db9f3b2SDimitry Andric } 11151db9f3b2SDimitry Andric 11161db9f3b2SDimitry Andric if (!RefInfo.Clone) { 11171db9f3b2SDimitry Andric // We haven't cloned this DIE yet. Just create an empty one and 11181db9f3b2SDimitry Andric // store it. It'll get really cloned when we process it. 11191db9f3b2SDimitry Andric RefInfo.UnclonedReference = true; 11201db9f3b2SDimitry Andric RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag())); 11211db9f3b2SDimitry Andric } 11221db9f3b2SDimitry Andric NewRefDie = RefInfo.Clone; 11231db9f3b2SDimitry Andric 11241db9f3b2SDimitry Andric if (AttrSpec.Form == dwarf::DW_FORM_ref_addr || 11251db9f3b2SDimitry Andric (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) { 11261db9f3b2SDimitry Andric // We cannot currently rely on a DIEEntry to emit ref_addr 11271db9f3b2SDimitry Andric // references, because the implementation calls back to DwarfDebug 11281db9f3b2SDimitry Andric // to find the unit offset. (We don't have a DwarfDebug) 11291db9f3b2SDimitry Andric // FIXME: we should be able to design DIEEntry reliance on 11301db9f3b2SDimitry Andric // DwarfDebug away. 11311db9f3b2SDimitry Andric uint64_t Attr; 11321db9f3b2SDimitry Andric if (Ref < InputDIE.getOffset() && !RefInfo.UnclonedReference) { 11331db9f3b2SDimitry Andric // We have already cloned that DIE. 11341db9f3b2SDimitry Andric uint32_t NewRefOffset = 11351db9f3b2SDimitry Andric RefUnit->getStartOffset() + NewRefDie->getOffset(); 11361db9f3b2SDimitry Andric Attr = NewRefOffset; 11371db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 11381db9f3b2SDimitry Andric dwarf::DW_FORM_ref_addr, DIEInteger(Attr)); 11391db9f3b2SDimitry Andric } else { 11401db9f3b2SDimitry Andric // A forward reference. Note and fixup later. 11411db9f3b2SDimitry Andric Attr = 0xBADDEF; 11421db9f3b2SDimitry Andric Unit.noteForwardReference( 11431db9f3b2SDimitry Andric NewRefDie, RefUnit, RefInfo.Ctxt, 11441db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 11451db9f3b2SDimitry Andric dwarf::DW_FORM_ref_addr, DIEInteger(Attr))); 11461db9f3b2SDimitry Andric } 11471db9f3b2SDimitry Andric return U.getRefAddrByteSize(); 11481db9f3b2SDimitry Andric } 11491db9f3b2SDimitry Andric 11501db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 11511db9f3b2SDimitry Andric dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie)); 11521db9f3b2SDimitry Andric 11531db9f3b2SDimitry Andric return AttrSize; 11541db9f3b2SDimitry Andric } 11551db9f3b2SDimitry Andric 11561db9f3b2SDimitry Andric void DWARFLinker::DIECloner::cloneExpression( 11571db9f3b2SDimitry Andric DataExtractor &Data, DWARFExpression Expression, const DWARFFile &File, 11581db9f3b2SDimitry Andric CompileUnit &Unit, SmallVectorImpl<uint8_t> &OutputBuffer, 11591db9f3b2SDimitry Andric int64_t AddrRelocAdjustment, bool IsLittleEndian) { 11601db9f3b2SDimitry Andric using Encoding = DWARFExpression::Operation::Encoding; 11611db9f3b2SDimitry Andric 11621db9f3b2SDimitry Andric uint8_t OrigAddressByteSize = Unit.getOrigUnit().getAddressByteSize(); 11631db9f3b2SDimitry Andric 11641db9f3b2SDimitry Andric uint64_t OpOffset = 0; 11651db9f3b2SDimitry Andric for (auto &Op : Expression) { 11661db9f3b2SDimitry Andric auto Desc = Op.getDescription(); 11671db9f3b2SDimitry Andric // DW_OP_const_type is variable-length and has 3 11681db9f3b2SDimitry Andric // operands. Thus far we only support 2. 11691db9f3b2SDimitry Andric if ((Desc.Op.size() == 2 && Desc.Op[0] == Encoding::BaseTypeRef) || 11701db9f3b2SDimitry Andric (Desc.Op.size() == 2 && Desc.Op[1] == Encoding::BaseTypeRef && 11711db9f3b2SDimitry Andric Desc.Op[0] != Encoding::Size1)) 11721db9f3b2SDimitry Andric Linker.reportWarning("Unsupported DW_OP encoding.", File); 11731db9f3b2SDimitry Andric 11741db9f3b2SDimitry Andric if ((Desc.Op.size() == 1 && Desc.Op[0] == Encoding::BaseTypeRef) || 11751db9f3b2SDimitry Andric (Desc.Op.size() == 2 && Desc.Op[1] == Encoding::BaseTypeRef && 11761db9f3b2SDimitry Andric Desc.Op[0] == Encoding::Size1)) { 11771db9f3b2SDimitry Andric // This code assumes that the other non-typeref operand fits into 1 byte. 11781db9f3b2SDimitry Andric assert(OpOffset < Op.getEndOffset()); 11791db9f3b2SDimitry Andric uint32_t ULEBsize = Op.getEndOffset() - OpOffset - 1; 11801db9f3b2SDimitry Andric assert(ULEBsize <= 16); 11811db9f3b2SDimitry Andric 11821db9f3b2SDimitry Andric // Copy over the operation. 11831db9f3b2SDimitry Andric assert(!Op.getSubCode() && "SubOps not yet supported"); 11841db9f3b2SDimitry Andric OutputBuffer.push_back(Op.getCode()); 11851db9f3b2SDimitry Andric uint64_t RefOffset; 11861db9f3b2SDimitry Andric if (Desc.Op.size() == 1) { 11871db9f3b2SDimitry Andric RefOffset = Op.getRawOperand(0); 11881db9f3b2SDimitry Andric } else { 11891db9f3b2SDimitry Andric OutputBuffer.push_back(Op.getRawOperand(0)); 11901db9f3b2SDimitry Andric RefOffset = Op.getRawOperand(1); 11911db9f3b2SDimitry Andric } 11921db9f3b2SDimitry Andric uint32_t Offset = 0; 11931db9f3b2SDimitry Andric // Look up the base type. For DW_OP_convert, the operand may be 0 to 11941db9f3b2SDimitry Andric // instead indicate the generic type. The same holds for 11951db9f3b2SDimitry Andric // DW_OP_reinterpret, which is currently not supported. 11961db9f3b2SDimitry Andric if (RefOffset > 0 || Op.getCode() != dwarf::DW_OP_convert) { 11971db9f3b2SDimitry Andric RefOffset += Unit.getOrigUnit().getOffset(); 11981db9f3b2SDimitry Andric auto RefDie = Unit.getOrigUnit().getDIEForOffset(RefOffset); 11991db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = Unit.getInfo(RefDie); 12001db9f3b2SDimitry Andric if (DIE *Clone = Info.Clone) 12011db9f3b2SDimitry Andric Offset = Clone->getOffset(); 12021db9f3b2SDimitry Andric else 12031db9f3b2SDimitry Andric Linker.reportWarning( 12041db9f3b2SDimitry Andric "base type ref doesn't point to DW_TAG_base_type.", File); 12051db9f3b2SDimitry Andric } 12061db9f3b2SDimitry Andric uint8_t ULEB[16]; 12071db9f3b2SDimitry Andric unsigned RealSize = encodeULEB128(Offset, ULEB, ULEBsize); 12081db9f3b2SDimitry Andric if (RealSize > ULEBsize) { 12091db9f3b2SDimitry Andric // Emit the generic type as a fallback. 12101db9f3b2SDimitry Andric RealSize = encodeULEB128(0, ULEB, ULEBsize); 12111db9f3b2SDimitry Andric Linker.reportWarning("base type ref doesn't fit.", File); 12121db9f3b2SDimitry Andric } 12131db9f3b2SDimitry Andric assert(RealSize == ULEBsize && "padding failed"); 12141db9f3b2SDimitry Andric ArrayRef<uint8_t> ULEBbytes(ULEB, ULEBsize); 12151db9f3b2SDimitry Andric OutputBuffer.append(ULEBbytes.begin(), ULEBbytes.end()); 12161db9f3b2SDimitry Andric } else if (!Linker.Options.Update && Op.getCode() == dwarf::DW_OP_addrx) { 12171db9f3b2SDimitry Andric if (std::optional<object::SectionedAddress> SA = 12181db9f3b2SDimitry Andric Unit.getOrigUnit().getAddrOffsetSectionItem( 12191db9f3b2SDimitry Andric Op.getRawOperand(0))) { 12201db9f3b2SDimitry Andric // DWARFLinker does not use addrx forms since it generates relocated 12211db9f3b2SDimitry Andric // addresses. Replace DW_OP_addrx with DW_OP_addr here. 12221db9f3b2SDimitry Andric // Argument of DW_OP_addrx should be relocated here as it is not 12231db9f3b2SDimitry Andric // processed by applyValidRelocs. 12241db9f3b2SDimitry Andric OutputBuffer.push_back(dwarf::DW_OP_addr); 12251db9f3b2SDimitry Andric uint64_t LinkedAddress = SA->Address + AddrRelocAdjustment; 12261db9f3b2SDimitry Andric if (IsLittleEndian != sys::IsLittleEndianHost) 12271db9f3b2SDimitry Andric sys::swapByteOrder(LinkedAddress); 12281db9f3b2SDimitry Andric ArrayRef<uint8_t> AddressBytes( 12291db9f3b2SDimitry Andric reinterpret_cast<const uint8_t *>(&LinkedAddress), 12301db9f3b2SDimitry Andric OrigAddressByteSize); 12311db9f3b2SDimitry Andric OutputBuffer.append(AddressBytes.begin(), AddressBytes.end()); 12321db9f3b2SDimitry Andric } else 12331db9f3b2SDimitry Andric Linker.reportWarning("cannot read DW_OP_addrx operand.", File); 12341db9f3b2SDimitry Andric } else if (!Linker.Options.Update && Op.getCode() == dwarf::DW_OP_constx) { 12351db9f3b2SDimitry Andric if (std::optional<object::SectionedAddress> SA = 12361db9f3b2SDimitry Andric Unit.getOrigUnit().getAddrOffsetSectionItem( 12371db9f3b2SDimitry Andric Op.getRawOperand(0))) { 12381db9f3b2SDimitry Andric // DWARFLinker does not use constx forms since it generates relocated 12391db9f3b2SDimitry Andric // addresses. Replace DW_OP_constx with DW_OP_const[*]u here. 12401db9f3b2SDimitry Andric // Argument of DW_OP_constx should be relocated here as it is not 12411db9f3b2SDimitry Andric // processed by applyValidRelocs. 12421db9f3b2SDimitry Andric std::optional<uint8_t> OutOperandKind; 12431db9f3b2SDimitry Andric switch (OrigAddressByteSize) { 12441db9f3b2SDimitry Andric case 4: 12451db9f3b2SDimitry Andric OutOperandKind = dwarf::DW_OP_const4u; 12461db9f3b2SDimitry Andric break; 12471db9f3b2SDimitry Andric case 8: 12481db9f3b2SDimitry Andric OutOperandKind = dwarf::DW_OP_const8u; 12491db9f3b2SDimitry Andric break; 12501db9f3b2SDimitry Andric default: 12511db9f3b2SDimitry Andric Linker.reportWarning( 12521db9f3b2SDimitry Andric formatv(("unsupported address size: {0}."), OrigAddressByteSize), 12531db9f3b2SDimitry Andric File); 12541db9f3b2SDimitry Andric break; 12551db9f3b2SDimitry Andric } 12561db9f3b2SDimitry Andric 12571db9f3b2SDimitry Andric if (OutOperandKind) { 12581db9f3b2SDimitry Andric OutputBuffer.push_back(*OutOperandKind); 12591db9f3b2SDimitry Andric uint64_t LinkedAddress = SA->Address + AddrRelocAdjustment; 12601db9f3b2SDimitry Andric if (IsLittleEndian != sys::IsLittleEndianHost) 12611db9f3b2SDimitry Andric sys::swapByteOrder(LinkedAddress); 12621db9f3b2SDimitry Andric ArrayRef<uint8_t> AddressBytes( 12631db9f3b2SDimitry Andric reinterpret_cast<const uint8_t *>(&LinkedAddress), 12641db9f3b2SDimitry Andric OrigAddressByteSize); 12651db9f3b2SDimitry Andric OutputBuffer.append(AddressBytes.begin(), AddressBytes.end()); 12661db9f3b2SDimitry Andric } 12671db9f3b2SDimitry Andric } else 12681db9f3b2SDimitry Andric Linker.reportWarning("cannot read DW_OP_constx operand.", File); 12691db9f3b2SDimitry Andric } else { 12701db9f3b2SDimitry Andric // Copy over everything else unmodified. 12711db9f3b2SDimitry Andric StringRef Bytes = Data.getData().slice(OpOffset, Op.getEndOffset()); 12721db9f3b2SDimitry Andric OutputBuffer.append(Bytes.begin(), Bytes.end()); 12731db9f3b2SDimitry Andric } 12741db9f3b2SDimitry Andric OpOffset = Op.getEndOffset(); 12751db9f3b2SDimitry Andric } 12761db9f3b2SDimitry Andric } 12771db9f3b2SDimitry Andric 12781db9f3b2SDimitry Andric unsigned DWARFLinker::DIECloner::cloneBlockAttribute( 12791db9f3b2SDimitry Andric DIE &Die, const DWARFDie &InputDIE, const DWARFFile &File, 12801db9f3b2SDimitry Andric CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val, 12811db9f3b2SDimitry Andric bool IsLittleEndian) { 12821db9f3b2SDimitry Andric DIEValueList *Attr; 12831db9f3b2SDimitry Andric DIEValue Value; 12841db9f3b2SDimitry Andric DIELoc *Loc = nullptr; 12851db9f3b2SDimitry Andric DIEBlock *Block = nullptr; 12861db9f3b2SDimitry Andric if (AttrSpec.Form == dwarf::DW_FORM_exprloc) { 12871db9f3b2SDimitry Andric Loc = new (DIEAlloc) DIELoc; 12881db9f3b2SDimitry Andric Linker.DIELocs.push_back(Loc); 12891db9f3b2SDimitry Andric } else { 12901db9f3b2SDimitry Andric Block = new (DIEAlloc) DIEBlock; 12911db9f3b2SDimitry Andric Linker.DIEBlocks.push_back(Block); 12921db9f3b2SDimitry Andric } 12931db9f3b2SDimitry Andric Attr = Loc ? static_cast<DIEValueList *>(Loc) 12941db9f3b2SDimitry Andric : static_cast<DIEValueList *>(Block); 12951db9f3b2SDimitry Andric 12961db9f3b2SDimitry Andric DWARFUnit &OrigUnit = Unit.getOrigUnit(); 12971db9f3b2SDimitry Andric // If the block is a DWARF Expression, clone it into the temporary 12981db9f3b2SDimitry Andric // buffer using cloneExpression(), otherwise copy the data directly. 12991db9f3b2SDimitry Andric SmallVector<uint8_t, 32> Buffer; 13001db9f3b2SDimitry Andric ArrayRef<uint8_t> Bytes = *Val.getAsBlock(); 13011db9f3b2SDimitry Andric if (DWARFAttribute::mayHaveLocationExpr(AttrSpec.Attr) && 13021db9f3b2SDimitry Andric (Val.isFormClass(DWARFFormValue::FC_Block) || 13031db9f3b2SDimitry Andric Val.isFormClass(DWARFFormValue::FC_Exprloc))) { 13041db9f3b2SDimitry Andric DataExtractor Data(StringRef((const char *)Bytes.data(), Bytes.size()), 13051db9f3b2SDimitry Andric IsLittleEndian, OrigUnit.getAddressByteSize()); 13061db9f3b2SDimitry Andric DWARFExpression Expr(Data, OrigUnit.getAddressByteSize(), 13071db9f3b2SDimitry Andric OrigUnit.getFormParams().Format); 13081db9f3b2SDimitry Andric cloneExpression(Data, Expr, File, Unit, Buffer, 13091db9f3b2SDimitry Andric Unit.getInfo(InputDIE).AddrAdjust, IsLittleEndian); 13101db9f3b2SDimitry Andric Bytes = Buffer; 13111db9f3b2SDimitry Andric } 13121db9f3b2SDimitry Andric for (auto Byte : Bytes) 13131db9f3b2SDimitry Andric Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0), 13141db9f3b2SDimitry Andric dwarf::DW_FORM_data1, DIEInteger(Byte)); 13151db9f3b2SDimitry Andric 13161db9f3b2SDimitry Andric // FIXME: If DIEBlock and DIELoc just reuses the Size field of 13171db9f3b2SDimitry Andric // the DIE class, this "if" could be replaced by 13181db9f3b2SDimitry Andric // Attr->setSize(Bytes.size()). 13191db9f3b2SDimitry Andric if (Loc) 13201db9f3b2SDimitry Andric Loc->setSize(Bytes.size()); 13211db9f3b2SDimitry Andric else 13221db9f3b2SDimitry Andric Block->setSize(Bytes.size()); 13231db9f3b2SDimitry Andric 13241db9f3b2SDimitry Andric if (Loc) 13251db9f3b2SDimitry Andric Value = DIEValue(dwarf::Attribute(AttrSpec.Attr), 13261db9f3b2SDimitry Andric dwarf::Form(AttrSpec.Form), Loc); 13271db9f3b2SDimitry Andric else { 13281db9f3b2SDimitry Andric // The expression location data might be updated and exceed the original 13291db9f3b2SDimitry Andric // size. Check whether the new data fits into the original form. 13301db9f3b2SDimitry Andric if ((AttrSpec.Form == dwarf::DW_FORM_block1 && 13311db9f3b2SDimitry Andric (Bytes.size() > UINT8_MAX)) || 13321db9f3b2SDimitry Andric (AttrSpec.Form == dwarf::DW_FORM_block2 && 13331db9f3b2SDimitry Andric (Bytes.size() > UINT16_MAX)) || 13341db9f3b2SDimitry Andric (AttrSpec.Form == dwarf::DW_FORM_block4 && (Bytes.size() > UINT32_MAX))) 13351db9f3b2SDimitry Andric AttrSpec.Form = dwarf::DW_FORM_block; 13361db9f3b2SDimitry Andric 13371db9f3b2SDimitry Andric Value = DIEValue(dwarf::Attribute(AttrSpec.Attr), 13381db9f3b2SDimitry Andric dwarf::Form(AttrSpec.Form), Block); 13391db9f3b2SDimitry Andric } 13401db9f3b2SDimitry Andric 13411db9f3b2SDimitry Andric return Die.addValue(DIEAlloc, Value)->sizeOf(OrigUnit.getFormParams()); 13421db9f3b2SDimitry Andric } 13431db9f3b2SDimitry Andric 13441db9f3b2SDimitry Andric unsigned DWARFLinker::DIECloner::cloneAddressAttribute( 13451db9f3b2SDimitry Andric DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec, 13461db9f3b2SDimitry Andric unsigned AttrSize, const DWARFFormValue &Val, const CompileUnit &Unit, 13471db9f3b2SDimitry Andric AttributesInfo &Info) { 13481db9f3b2SDimitry Andric if (AttrSpec.Attr == dwarf::DW_AT_low_pc) 13491db9f3b2SDimitry Andric Info.HasLowPc = true; 13501db9f3b2SDimitry Andric 13511db9f3b2SDimitry Andric if (LLVM_UNLIKELY(Linker.Options.Update)) { 13521db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 13531db9f3b2SDimitry Andric dwarf::Form(AttrSpec.Form), DIEInteger(Val.getRawUValue())); 13541db9f3b2SDimitry Andric return AttrSize; 13551db9f3b2SDimitry Andric } 13561db9f3b2SDimitry Andric 13571db9f3b2SDimitry Andric // Cloned Die may have address attributes relocated to a 13581db9f3b2SDimitry Andric // totally unrelated value. This can happen: 13591db9f3b2SDimitry Andric // - If high_pc is an address (Dwarf version == 2), then it might have been 13601db9f3b2SDimitry Andric // relocated to a totally unrelated value (because the end address in the 13611db9f3b2SDimitry Andric // object file might be start address of another function which got moved 13621db9f3b2SDimitry Andric // independently by the linker). 13631db9f3b2SDimitry Andric // - If address relocated in an inline_subprogram that happens at the 13641db9f3b2SDimitry Andric // beginning of its inlining function. 13651db9f3b2SDimitry Andric // To avoid above cases and to not apply relocation twice (in 13661db9f3b2SDimitry Andric // applyValidRelocs and here), read address attribute from InputDIE and apply 13671db9f3b2SDimitry Andric // Info.PCOffset here. 13681db9f3b2SDimitry Andric 13691db9f3b2SDimitry Andric std::optional<DWARFFormValue> AddrAttribute = InputDIE.find(AttrSpec.Attr); 13701db9f3b2SDimitry Andric if (!AddrAttribute) 13711db9f3b2SDimitry Andric llvm_unreachable("Cann't find attribute."); 13721db9f3b2SDimitry Andric 13731db9f3b2SDimitry Andric std::optional<uint64_t> Addr = AddrAttribute->getAsAddress(); 13741db9f3b2SDimitry Andric if (!Addr) { 13751db9f3b2SDimitry Andric Linker.reportWarning("Cann't read address attribute value.", ObjFile); 13761db9f3b2SDimitry Andric return 0; 13771db9f3b2SDimitry Andric } 13781db9f3b2SDimitry Andric 13791db9f3b2SDimitry Andric if (InputDIE.getTag() == dwarf::DW_TAG_compile_unit && 13801db9f3b2SDimitry Andric AttrSpec.Attr == dwarf::DW_AT_low_pc) { 13811db9f3b2SDimitry Andric if (std::optional<uint64_t> LowPC = Unit.getLowPc()) 13821db9f3b2SDimitry Andric Addr = *LowPC; 13831db9f3b2SDimitry Andric else 13841db9f3b2SDimitry Andric return 0; 13851db9f3b2SDimitry Andric } else if (InputDIE.getTag() == dwarf::DW_TAG_compile_unit && 13861db9f3b2SDimitry Andric AttrSpec.Attr == dwarf::DW_AT_high_pc) { 13871db9f3b2SDimitry Andric if (uint64_t HighPc = Unit.getHighPc()) 13881db9f3b2SDimitry Andric Addr = HighPc; 13891db9f3b2SDimitry Andric else 13901db9f3b2SDimitry Andric return 0; 13911db9f3b2SDimitry Andric } else { 13921db9f3b2SDimitry Andric *Addr += Info.PCOffset; 13931db9f3b2SDimitry Andric } 13941db9f3b2SDimitry Andric 13951db9f3b2SDimitry Andric if (AttrSpec.Form == dwarf::DW_FORM_addr) { 13961db9f3b2SDimitry Andric Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr), 13971db9f3b2SDimitry Andric AttrSpec.Form, DIEInteger(*Addr)); 13981db9f3b2SDimitry Andric return Unit.getOrigUnit().getAddressByteSize(); 13991db9f3b2SDimitry Andric } 14001db9f3b2SDimitry Andric 14011db9f3b2SDimitry Andric auto AddrIndex = AddrPool.getValueIndex(*Addr); 14021db9f3b2SDimitry Andric 14031db9f3b2SDimitry Andric return Die 14041db9f3b2SDimitry Andric .addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr), 14051db9f3b2SDimitry Andric dwarf::Form::DW_FORM_addrx, DIEInteger(AddrIndex)) 14061db9f3b2SDimitry Andric ->sizeOf(Unit.getOrigUnit().getFormParams()); 14071db9f3b2SDimitry Andric } 14081db9f3b2SDimitry Andric 14091db9f3b2SDimitry Andric unsigned DWARFLinker::DIECloner::cloneScalarAttribute( 14101db9f3b2SDimitry Andric DIE &Die, const DWARFDie &InputDIE, const DWARFFile &File, 14111db9f3b2SDimitry Andric CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val, 14121db9f3b2SDimitry Andric unsigned AttrSize, AttributesInfo &Info) { 14131db9f3b2SDimitry Andric uint64_t Value; 14141db9f3b2SDimitry Andric 14151db9f3b2SDimitry Andric // Check for the offset to the macro table. If offset is incorrect then we 14161db9f3b2SDimitry Andric // need to remove the attribute. 14171db9f3b2SDimitry Andric if (AttrSpec.Attr == dwarf::DW_AT_macro_info) { 14181db9f3b2SDimitry Andric if (std::optional<uint64_t> Offset = Val.getAsSectionOffset()) { 14191db9f3b2SDimitry Andric const llvm::DWARFDebugMacro *Macro = File.Dwarf->getDebugMacinfo(); 14201db9f3b2SDimitry Andric if (Macro == nullptr || !Macro->hasEntryForOffset(*Offset)) 14211db9f3b2SDimitry Andric return 0; 14221db9f3b2SDimitry Andric } 14231db9f3b2SDimitry Andric } 14241db9f3b2SDimitry Andric 14251db9f3b2SDimitry Andric if (AttrSpec.Attr == dwarf::DW_AT_macros) { 14261db9f3b2SDimitry Andric if (std::optional<uint64_t> Offset = Val.getAsSectionOffset()) { 14271db9f3b2SDimitry Andric const llvm::DWARFDebugMacro *Macro = File.Dwarf->getDebugMacro(); 14281db9f3b2SDimitry Andric if (Macro == nullptr || !Macro->hasEntryForOffset(*Offset)) 14291db9f3b2SDimitry Andric return 0; 14301db9f3b2SDimitry Andric } 14311db9f3b2SDimitry Andric } 14321db9f3b2SDimitry Andric 14331db9f3b2SDimitry Andric if (AttrSpec.Attr == dwarf::DW_AT_str_offsets_base) { 14341db9f3b2SDimitry Andric // DWARFLinker generates common .debug_str_offsets table used for all 14351db9f3b2SDimitry Andric // compile units. The offset to the common .debug_str_offsets table is 8 on 14361db9f3b2SDimitry Andric // DWARF32. 14371db9f3b2SDimitry Andric Info.AttrStrOffsetBaseSeen = true; 14381db9f3b2SDimitry Andric return Die 14391db9f3b2SDimitry Andric .addValue(DIEAlloc, dwarf::DW_AT_str_offsets_base, 14401db9f3b2SDimitry Andric dwarf::DW_FORM_sec_offset, DIEInteger(8)) 14411db9f3b2SDimitry Andric ->sizeOf(Unit.getOrigUnit().getFormParams()); 14421db9f3b2SDimitry Andric } 14431db9f3b2SDimitry Andric 14441db9f3b2SDimitry Andric if (LLVM_UNLIKELY(Linker.Options.Update)) { 14451db9f3b2SDimitry Andric if (auto OptionalValue = Val.getAsUnsignedConstant()) 14461db9f3b2SDimitry Andric Value = *OptionalValue; 14471db9f3b2SDimitry Andric else if (auto OptionalValue = Val.getAsSignedConstant()) 14481db9f3b2SDimitry Andric Value = *OptionalValue; 14491db9f3b2SDimitry Andric else if (auto OptionalValue = Val.getAsSectionOffset()) 14501db9f3b2SDimitry Andric Value = *OptionalValue; 14511db9f3b2SDimitry Andric else { 14521db9f3b2SDimitry Andric Linker.reportWarning( 14531db9f3b2SDimitry Andric "Unsupported scalar attribute form. Dropping attribute.", File, 14541db9f3b2SDimitry Andric &InputDIE); 14551db9f3b2SDimitry Andric return 0; 14561db9f3b2SDimitry Andric } 14571db9f3b2SDimitry Andric if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value) 14581db9f3b2SDimitry Andric Info.IsDeclaration = true; 14591db9f3b2SDimitry Andric 14601db9f3b2SDimitry Andric if (AttrSpec.Form == dwarf::DW_FORM_loclistx) 14611db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 14621db9f3b2SDimitry Andric dwarf::Form(AttrSpec.Form), DIELocList(Value)); 14631db9f3b2SDimitry Andric else 14641db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 14651db9f3b2SDimitry Andric dwarf::Form(AttrSpec.Form), DIEInteger(Value)); 14661db9f3b2SDimitry Andric return AttrSize; 14671db9f3b2SDimitry Andric } 14681db9f3b2SDimitry Andric 14691db9f3b2SDimitry Andric [[maybe_unused]] dwarf::Form OriginalForm = AttrSpec.Form; 14701db9f3b2SDimitry Andric if (AttrSpec.Form == dwarf::DW_FORM_rnglistx) { 14711db9f3b2SDimitry Andric // DWARFLinker does not generate .debug_addr table. Thus we need to change 14721db9f3b2SDimitry Andric // all "addrx" related forms to "addr" version. Change DW_FORM_rnglistx 14731db9f3b2SDimitry Andric // to DW_FORM_sec_offset here. 14741db9f3b2SDimitry Andric std::optional<uint64_t> Index = Val.getAsSectionOffset(); 14751db9f3b2SDimitry Andric if (!Index) { 14761db9f3b2SDimitry Andric Linker.reportWarning("Cannot read the attribute. Dropping.", File, 14771db9f3b2SDimitry Andric &InputDIE); 14781db9f3b2SDimitry Andric return 0; 14791db9f3b2SDimitry Andric } 14801db9f3b2SDimitry Andric std::optional<uint64_t> Offset = 14811db9f3b2SDimitry Andric Unit.getOrigUnit().getRnglistOffset(*Index); 14821db9f3b2SDimitry Andric if (!Offset) { 14831db9f3b2SDimitry Andric Linker.reportWarning("Cannot read the attribute. Dropping.", File, 14841db9f3b2SDimitry Andric &InputDIE); 14851db9f3b2SDimitry Andric return 0; 14861db9f3b2SDimitry Andric } 14871db9f3b2SDimitry Andric 14881db9f3b2SDimitry Andric Value = *Offset; 14891db9f3b2SDimitry Andric AttrSpec.Form = dwarf::DW_FORM_sec_offset; 14901db9f3b2SDimitry Andric AttrSize = Unit.getOrigUnit().getFormParams().getDwarfOffsetByteSize(); 14911db9f3b2SDimitry Andric } else if (AttrSpec.Form == dwarf::DW_FORM_loclistx) { 14921db9f3b2SDimitry Andric // DWARFLinker does not generate .debug_addr table. Thus we need to change 14931db9f3b2SDimitry Andric // all "addrx" related forms to "addr" version. Change DW_FORM_loclistx 14941db9f3b2SDimitry Andric // to DW_FORM_sec_offset here. 14951db9f3b2SDimitry Andric std::optional<uint64_t> Index = Val.getAsSectionOffset(); 14961db9f3b2SDimitry Andric if (!Index) { 14971db9f3b2SDimitry Andric Linker.reportWarning("Cannot read the attribute. Dropping.", File, 14981db9f3b2SDimitry Andric &InputDIE); 14991db9f3b2SDimitry Andric return 0; 15001db9f3b2SDimitry Andric } 15011db9f3b2SDimitry Andric std::optional<uint64_t> Offset = 15021db9f3b2SDimitry Andric Unit.getOrigUnit().getLoclistOffset(*Index); 15031db9f3b2SDimitry Andric if (!Offset) { 15041db9f3b2SDimitry Andric Linker.reportWarning("Cannot read the attribute. Dropping.", File, 15051db9f3b2SDimitry Andric &InputDIE); 15061db9f3b2SDimitry Andric return 0; 15071db9f3b2SDimitry Andric } 15081db9f3b2SDimitry Andric 15091db9f3b2SDimitry Andric Value = *Offset; 15101db9f3b2SDimitry Andric AttrSpec.Form = dwarf::DW_FORM_sec_offset; 15111db9f3b2SDimitry Andric AttrSize = Unit.getOrigUnit().getFormParams().getDwarfOffsetByteSize(); 15121db9f3b2SDimitry Andric } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc && 15131db9f3b2SDimitry Andric Die.getTag() == dwarf::DW_TAG_compile_unit) { 15141db9f3b2SDimitry Andric std::optional<uint64_t> LowPC = Unit.getLowPc(); 15151db9f3b2SDimitry Andric if (!LowPC) 15161db9f3b2SDimitry Andric return 0; 15171db9f3b2SDimitry Andric // Dwarf >= 4 high_pc is an size, not an address. 15181db9f3b2SDimitry Andric Value = Unit.getHighPc() - *LowPC; 15191db9f3b2SDimitry Andric } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset) 15201db9f3b2SDimitry Andric Value = *Val.getAsSectionOffset(); 15211db9f3b2SDimitry Andric else if (AttrSpec.Form == dwarf::DW_FORM_sdata) 15221db9f3b2SDimitry Andric Value = *Val.getAsSignedConstant(); 15231db9f3b2SDimitry Andric else if (auto OptionalValue = Val.getAsUnsignedConstant()) 15241db9f3b2SDimitry Andric Value = *OptionalValue; 15251db9f3b2SDimitry Andric else { 15261db9f3b2SDimitry Andric Linker.reportWarning( 15271db9f3b2SDimitry Andric "Unsupported scalar attribute form. Dropping attribute.", File, 15281db9f3b2SDimitry Andric &InputDIE); 15291db9f3b2SDimitry Andric return 0; 15301db9f3b2SDimitry Andric } 15311db9f3b2SDimitry Andric 15321db9f3b2SDimitry Andric DIE::value_iterator Patch = 15331db9f3b2SDimitry Andric Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), 15341db9f3b2SDimitry Andric dwarf::Form(AttrSpec.Form), DIEInteger(Value)); 15351db9f3b2SDimitry Andric if (AttrSpec.Attr == dwarf::DW_AT_ranges || 15361db9f3b2SDimitry Andric AttrSpec.Attr == dwarf::DW_AT_start_scope) { 15371db9f3b2SDimitry Andric Unit.noteRangeAttribute(Die, Patch); 15381db9f3b2SDimitry Andric Info.HasRanges = true; 15391db9f3b2SDimitry Andric } else if (DWARFAttribute::mayHaveLocationList(AttrSpec.Attr) && 15401db9f3b2SDimitry Andric dwarf::doesFormBelongToClass(AttrSpec.Form, 15411db9f3b2SDimitry Andric DWARFFormValue::FC_SectionOffset, 15421db9f3b2SDimitry Andric Unit.getOrigUnit().getVersion())) { 15431db9f3b2SDimitry Andric 15441db9f3b2SDimitry Andric CompileUnit::DIEInfo &LocationDieInfo = Unit.getInfo(InputDIE); 15451db9f3b2SDimitry Andric Unit.noteLocationAttribute({Patch, LocationDieInfo.InDebugMap 15461db9f3b2SDimitry Andric ? LocationDieInfo.AddrAdjust 15471db9f3b2SDimitry Andric : Info.PCOffset}); 15481db9f3b2SDimitry Andric } else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value) 15491db9f3b2SDimitry Andric Info.IsDeclaration = true; 15501db9f3b2SDimitry Andric 15511db9f3b2SDimitry Andric // check that all dwarf::DW_FORM_rnglistx are handled previously. 15521db9f3b2SDimitry Andric assert((Info.HasRanges || (OriginalForm != dwarf::DW_FORM_rnglistx)) && 15531db9f3b2SDimitry Andric "Unhandled DW_FORM_rnglistx attribute"); 15541db9f3b2SDimitry Andric 15551db9f3b2SDimitry Andric return AttrSize; 15561db9f3b2SDimitry Andric } 15571db9f3b2SDimitry Andric 15581db9f3b2SDimitry Andric /// Clone \p InputDIE's attribute described by \p AttrSpec with 15591db9f3b2SDimitry Andric /// value \p Val, and add it to \p Die. 15601db9f3b2SDimitry Andric /// \returns the size of the cloned attribute. 15611db9f3b2SDimitry Andric unsigned DWARFLinker::DIECloner::cloneAttribute( 15621db9f3b2SDimitry Andric DIE &Die, const DWARFDie &InputDIE, const DWARFFile &File, 15631db9f3b2SDimitry Andric CompileUnit &Unit, const DWARFFormValue &Val, const AttributeSpec AttrSpec, 15641db9f3b2SDimitry Andric unsigned AttrSize, AttributesInfo &Info, bool IsLittleEndian) { 15651db9f3b2SDimitry Andric const DWARFUnit &U = Unit.getOrigUnit(); 15661db9f3b2SDimitry Andric 15671db9f3b2SDimitry Andric switch (AttrSpec.Form) { 15681db9f3b2SDimitry Andric case dwarf::DW_FORM_strp: 15691db9f3b2SDimitry Andric case dwarf::DW_FORM_line_strp: 15701db9f3b2SDimitry Andric case dwarf::DW_FORM_string: 15711db9f3b2SDimitry Andric case dwarf::DW_FORM_strx: 15721db9f3b2SDimitry Andric case dwarf::DW_FORM_strx1: 15731db9f3b2SDimitry Andric case dwarf::DW_FORM_strx2: 15741db9f3b2SDimitry Andric case dwarf::DW_FORM_strx3: 15751db9f3b2SDimitry Andric case dwarf::DW_FORM_strx4: 15761db9f3b2SDimitry Andric return cloneStringAttribute(Die, AttrSpec, Val, U, Info); 15771db9f3b2SDimitry Andric case dwarf::DW_FORM_ref_addr: 15781db9f3b2SDimitry Andric case dwarf::DW_FORM_ref1: 15791db9f3b2SDimitry Andric case dwarf::DW_FORM_ref2: 15801db9f3b2SDimitry Andric case dwarf::DW_FORM_ref4: 15811db9f3b2SDimitry Andric case dwarf::DW_FORM_ref8: 15821db9f3b2SDimitry Andric return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val, 15831db9f3b2SDimitry Andric File, Unit); 15841db9f3b2SDimitry Andric case dwarf::DW_FORM_block: 15851db9f3b2SDimitry Andric case dwarf::DW_FORM_block1: 15861db9f3b2SDimitry Andric case dwarf::DW_FORM_block2: 15871db9f3b2SDimitry Andric case dwarf::DW_FORM_block4: 15881db9f3b2SDimitry Andric case dwarf::DW_FORM_exprloc: 15891db9f3b2SDimitry Andric return cloneBlockAttribute(Die, InputDIE, File, Unit, AttrSpec, Val, 15901db9f3b2SDimitry Andric IsLittleEndian); 15911db9f3b2SDimitry Andric case dwarf::DW_FORM_addr: 15921db9f3b2SDimitry Andric case dwarf::DW_FORM_addrx: 15931db9f3b2SDimitry Andric case dwarf::DW_FORM_addrx1: 15941db9f3b2SDimitry Andric case dwarf::DW_FORM_addrx2: 15951db9f3b2SDimitry Andric case dwarf::DW_FORM_addrx3: 15961db9f3b2SDimitry Andric case dwarf::DW_FORM_addrx4: 15971db9f3b2SDimitry Andric return cloneAddressAttribute(Die, InputDIE, AttrSpec, AttrSize, Val, Unit, 15981db9f3b2SDimitry Andric Info); 15991db9f3b2SDimitry Andric case dwarf::DW_FORM_data1: 16001db9f3b2SDimitry Andric case dwarf::DW_FORM_data2: 16011db9f3b2SDimitry Andric case dwarf::DW_FORM_data4: 16021db9f3b2SDimitry Andric case dwarf::DW_FORM_data8: 16031db9f3b2SDimitry Andric case dwarf::DW_FORM_udata: 16041db9f3b2SDimitry Andric case dwarf::DW_FORM_sdata: 16051db9f3b2SDimitry Andric case dwarf::DW_FORM_sec_offset: 16061db9f3b2SDimitry Andric case dwarf::DW_FORM_flag: 16071db9f3b2SDimitry Andric case dwarf::DW_FORM_flag_present: 16081db9f3b2SDimitry Andric case dwarf::DW_FORM_rnglistx: 16091db9f3b2SDimitry Andric case dwarf::DW_FORM_loclistx: 16101db9f3b2SDimitry Andric case dwarf::DW_FORM_implicit_const: 16111db9f3b2SDimitry Andric return cloneScalarAttribute(Die, InputDIE, File, Unit, AttrSpec, Val, 16121db9f3b2SDimitry Andric AttrSize, Info); 16131db9f3b2SDimitry Andric default: 16141db9f3b2SDimitry Andric Linker.reportWarning("Unsupported attribute form " + 16151db9f3b2SDimitry Andric dwarf::FormEncodingString(AttrSpec.Form) + 16161db9f3b2SDimitry Andric " in cloneAttribute. Dropping.", 16171db9f3b2SDimitry Andric File, &InputDIE); 16181db9f3b2SDimitry Andric } 16191db9f3b2SDimitry Andric 16201db9f3b2SDimitry Andric return 0; 16211db9f3b2SDimitry Andric } 16221db9f3b2SDimitry Andric 16231db9f3b2SDimitry Andric void DWARFLinker::DIECloner::addObjCAccelerator(CompileUnit &Unit, 16241db9f3b2SDimitry Andric const DIE *Die, 16251db9f3b2SDimitry Andric DwarfStringPoolEntryRef Name, 16261db9f3b2SDimitry Andric OffsetsStringPool &StringPool, 16271db9f3b2SDimitry Andric bool SkipPubSection) { 16281db9f3b2SDimitry Andric std::optional<ObjCSelectorNames> Names = 16291db9f3b2SDimitry Andric getObjCNamesIfSelector(Name.getString()); 16301db9f3b2SDimitry Andric if (!Names) 16311db9f3b2SDimitry Andric return; 16321db9f3b2SDimitry Andric Unit.addNameAccelerator(Die, StringPool.getEntry(Names->Selector), 16331db9f3b2SDimitry Andric SkipPubSection); 16341db9f3b2SDimitry Andric Unit.addObjCAccelerator(Die, StringPool.getEntry(Names->ClassName), 16351db9f3b2SDimitry Andric SkipPubSection); 16361db9f3b2SDimitry Andric if (Names->ClassNameNoCategory) 16371db9f3b2SDimitry Andric Unit.addObjCAccelerator( 16381db9f3b2SDimitry Andric Die, StringPool.getEntry(*Names->ClassNameNoCategory), SkipPubSection); 16391db9f3b2SDimitry Andric if (Names->MethodNameNoCategory) 16401db9f3b2SDimitry Andric Unit.addNameAccelerator( 16411db9f3b2SDimitry Andric Die, StringPool.getEntry(*Names->MethodNameNoCategory), SkipPubSection); 16421db9f3b2SDimitry Andric } 16431db9f3b2SDimitry Andric 16441db9f3b2SDimitry Andric static bool 16451db9f3b2SDimitry Andric shouldSkipAttribute(bool Update, 16461db9f3b2SDimitry Andric DWARFAbbreviationDeclaration::AttributeSpec AttrSpec, 16471db9f3b2SDimitry Andric bool SkipPC) { 16481db9f3b2SDimitry Andric switch (AttrSpec.Attr) { 16491db9f3b2SDimitry Andric default: 16501db9f3b2SDimitry Andric return false; 16511db9f3b2SDimitry Andric case dwarf::DW_AT_low_pc: 16521db9f3b2SDimitry Andric case dwarf::DW_AT_high_pc: 16531db9f3b2SDimitry Andric case dwarf::DW_AT_ranges: 16541db9f3b2SDimitry Andric return !Update && SkipPC; 16551db9f3b2SDimitry Andric case dwarf::DW_AT_rnglists_base: 16561db9f3b2SDimitry Andric // In case !Update the .debug_addr table is not generated/preserved. 16571db9f3b2SDimitry Andric // Thus instead of DW_FORM_rnglistx the DW_FORM_sec_offset is used. 16581db9f3b2SDimitry Andric // Since DW_AT_rnglists_base is used for only DW_FORM_rnglistx the 16591db9f3b2SDimitry Andric // DW_AT_rnglists_base is removed. 16601db9f3b2SDimitry Andric return !Update; 16611db9f3b2SDimitry Andric case dwarf::DW_AT_loclists_base: 16621db9f3b2SDimitry Andric // In case !Update the .debug_addr table is not generated/preserved. 16631db9f3b2SDimitry Andric // Thus instead of DW_FORM_loclistx the DW_FORM_sec_offset is used. 16641db9f3b2SDimitry Andric // Since DW_AT_loclists_base is used for only DW_FORM_loclistx the 16651db9f3b2SDimitry Andric // DW_AT_loclists_base is removed. 16661db9f3b2SDimitry Andric return !Update; 16671db9f3b2SDimitry Andric case dwarf::DW_AT_location: 16681db9f3b2SDimitry Andric case dwarf::DW_AT_frame_base: 16691db9f3b2SDimitry Andric return !Update && SkipPC; 16701db9f3b2SDimitry Andric } 16711db9f3b2SDimitry Andric } 16721db9f3b2SDimitry Andric 16731db9f3b2SDimitry Andric struct AttributeLinkedOffsetFixup { 16741db9f3b2SDimitry Andric int64_t LinkedOffsetFixupVal; 16751db9f3b2SDimitry Andric uint64_t InputAttrStartOffset; 16761db9f3b2SDimitry Andric uint64_t InputAttrEndOffset; 16771db9f3b2SDimitry Andric }; 16781db9f3b2SDimitry Andric 16791db9f3b2SDimitry Andric DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE, 16801db9f3b2SDimitry Andric const DWARFFile &File, CompileUnit &Unit, 16811db9f3b2SDimitry Andric int64_t PCOffset, uint32_t OutOffset, 16821db9f3b2SDimitry Andric unsigned Flags, bool IsLittleEndian, 16831db9f3b2SDimitry Andric DIE *Die) { 16841db9f3b2SDimitry Andric DWARFUnit &U = Unit.getOrigUnit(); 16851db9f3b2SDimitry Andric unsigned Idx = U.getDIEIndex(InputDIE); 16861db9f3b2SDimitry Andric CompileUnit::DIEInfo &Info = Unit.getInfo(Idx); 16871db9f3b2SDimitry Andric 16881db9f3b2SDimitry Andric // Should the DIE appear in the output? 16891db9f3b2SDimitry Andric if (!Unit.getInfo(Idx).Keep) 16901db9f3b2SDimitry Andric return nullptr; 16911db9f3b2SDimitry Andric 16921db9f3b2SDimitry Andric uint64_t Offset = InputDIE.getOffset(); 16931db9f3b2SDimitry Andric assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE"); 16941db9f3b2SDimitry Andric if (!Die) { 16951db9f3b2SDimitry Andric // The DIE might have been already created by a forward reference 16961db9f3b2SDimitry Andric // (see cloneDieReferenceAttribute()). 16971db9f3b2SDimitry Andric if (!Info.Clone) 16981db9f3b2SDimitry Andric Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag())); 16991db9f3b2SDimitry Andric Die = Info.Clone; 17001db9f3b2SDimitry Andric } 17011db9f3b2SDimitry Andric 17021db9f3b2SDimitry Andric assert(Die->getTag() == InputDIE.getTag()); 17031db9f3b2SDimitry Andric Die->setOffset(OutOffset); 17041db9f3b2SDimitry Andric if (isODRCanonicalCandidate(InputDIE, Unit) && Info.Ctxt && 17051db9f3b2SDimitry Andric (Info.Ctxt->getCanonicalDIEOffset() == 0)) { 17061db9f3b2SDimitry Andric if (!Info.Ctxt->hasCanonicalDIE()) 17071db9f3b2SDimitry Andric Info.Ctxt->setHasCanonicalDIE(); 17081db9f3b2SDimitry Andric // We are about to emit a DIE that is the root of its own valid 17091db9f3b2SDimitry Andric // DeclContext tree. Make the current offset the canonical offset 17101db9f3b2SDimitry Andric // for this context. 17111db9f3b2SDimitry Andric Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset()); 17121db9f3b2SDimitry Andric } 17131db9f3b2SDimitry Andric 17141db9f3b2SDimitry Andric // Extract and clone every attribute. 17151db9f3b2SDimitry Andric DWARFDataExtractor Data = U.getDebugInfoExtractor(); 17161db9f3b2SDimitry Andric // Point to the next DIE (generally there is always at least a NULL 17171db9f3b2SDimitry Andric // entry after the current one). If this is a lone 17181db9f3b2SDimitry Andric // DW_TAG_compile_unit without any children, point to the next unit. 17191db9f3b2SDimitry Andric uint64_t NextOffset = (Idx + 1 < U.getNumDIEs()) 17201db9f3b2SDimitry Andric ? U.getDIEAtIndex(Idx + 1).getOffset() 17211db9f3b2SDimitry Andric : U.getNextUnitOffset(); 17221db9f3b2SDimitry Andric AttributesInfo AttrInfo; 17231db9f3b2SDimitry Andric 17241db9f3b2SDimitry Andric // We could copy the data only if we need to apply a relocation to it. After 17251db9f3b2SDimitry Andric // testing, it seems there is no performance downside to doing the copy 17261db9f3b2SDimitry Andric // unconditionally, and it makes the code simpler. 17271db9f3b2SDimitry Andric SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset)); 17281db9f3b2SDimitry Andric Data = 17291db9f3b2SDimitry Andric DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize()); 17301db9f3b2SDimitry Andric 17311db9f3b2SDimitry Andric // Modify the copy with relocated addresses. 17321db9f3b2SDimitry Andric ObjFile.Addresses->applyValidRelocs(DIECopy, Offset, Data.isLittleEndian()); 17331db9f3b2SDimitry Andric 17341db9f3b2SDimitry Andric // Reset the Offset to 0 as we will be working on the local copy of 17351db9f3b2SDimitry Andric // the data. 17361db9f3b2SDimitry Andric Offset = 0; 17371db9f3b2SDimitry Andric 17381db9f3b2SDimitry Andric const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr(); 17391db9f3b2SDimitry Andric Offset += getULEB128Size(Abbrev->getCode()); 17401db9f3b2SDimitry Andric 17411db9f3b2SDimitry Andric // We are entering a subprogram. Get and propagate the PCOffset. 17421db9f3b2SDimitry Andric if (Die->getTag() == dwarf::DW_TAG_subprogram) 17431db9f3b2SDimitry Andric PCOffset = Info.AddrAdjust; 17441db9f3b2SDimitry Andric AttrInfo.PCOffset = PCOffset; 17451db9f3b2SDimitry Andric 17461db9f3b2SDimitry Andric if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) { 17471db9f3b2SDimitry Andric Flags |= TF_InFunctionScope; 17481db9f3b2SDimitry Andric if (!Info.InDebugMap && LLVM_LIKELY(!Update)) 17491db9f3b2SDimitry Andric Flags |= TF_SkipPC; 17501db9f3b2SDimitry Andric } else if (Abbrev->getTag() == dwarf::DW_TAG_variable) { 17511db9f3b2SDimitry Andric // Function-local globals could be in the debug map even when the function 17521db9f3b2SDimitry Andric // is not, e.g., inlined functions. 17531db9f3b2SDimitry Andric if ((Flags & TF_InFunctionScope) && Info.InDebugMap) 17541db9f3b2SDimitry Andric Flags &= ~TF_SkipPC; 17551db9f3b2SDimitry Andric // Location expressions referencing an address which is not in debug map 17561db9f3b2SDimitry Andric // should be deleted. 17571db9f3b2SDimitry Andric else if (!Info.InDebugMap && Info.HasLocationExpressionAddr && 17581db9f3b2SDimitry Andric LLVM_LIKELY(!Update)) 17591db9f3b2SDimitry Andric Flags |= TF_SkipPC; 17601db9f3b2SDimitry Andric } 17611db9f3b2SDimitry Andric 17621db9f3b2SDimitry Andric std::optional<StringRef> LibraryInstallName = 17631db9f3b2SDimitry Andric ObjFile.Addresses->getLibraryInstallName(); 17641db9f3b2SDimitry Andric SmallVector<AttributeLinkedOffsetFixup> AttributesFixups; 17651db9f3b2SDimitry Andric for (const auto &AttrSpec : Abbrev->attributes()) { 17661db9f3b2SDimitry Andric if (shouldSkipAttribute(Update, AttrSpec, Flags & TF_SkipPC)) { 17671db9f3b2SDimitry Andric DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, 17681db9f3b2SDimitry Andric U.getFormParams()); 17691db9f3b2SDimitry Andric continue; 17701db9f3b2SDimitry Andric } 17711db9f3b2SDimitry Andric 17721db9f3b2SDimitry Andric AttributeLinkedOffsetFixup CurAttrFixup; 17731db9f3b2SDimitry Andric CurAttrFixup.InputAttrStartOffset = InputDIE.getOffset() + Offset; 17741db9f3b2SDimitry Andric CurAttrFixup.LinkedOffsetFixupVal = 17751db9f3b2SDimitry Andric Unit.getStartOffset() + OutOffset - CurAttrFixup.InputAttrStartOffset; 17761db9f3b2SDimitry Andric 17771db9f3b2SDimitry Andric DWARFFormValue Val = AttrSpec.getFormValue(); 17781db9f3b2SDimitry Andric uint64_t AttrSize = Offset; 17791db9f3b2SDimitry Andric Val.extractValue(Data, &Offset, U.getFormParams(), &U); 17801db9f3b2SDimitry Andric CurAttrFixup.InputAttrEndOffset = InputDIE.getOffset() + Offset; 17811db9f3b2SDimitry Andric AttrSize = Offset - AttrSize; 17821db9f3b2SDimitry Andric 17831db9f3b2SDimitry Andric uint64_t FinalAttrSize = 17841db9f3b2SDimitry Andric cloneAttribute(*Die, InputDIE, File, Unit, Val, AttrSpec, AttrSize, 17851db9f3b2SDimitry Andric AttrInfo, IsLittleEndian); 17861db9f3b2SDimitry Andric if (FinalAttrSize != 0 && ObjFile.Addresses->needToSaveValidRelocs()) 17871db9f3b2SDimitry Andric AttributesFixups.push_back(CurAttrFixup); 17881db9f3b2SDimitry Andric 17891db9f3b2SDimitry Andric OutOffset += FinalAttrSize; 17901db9f3b2SDimitry Andric } 17911db9f3b2SDimitry Andric 17921db9f3b2SDimitry Andric uint16_t Tag = InputDIE.getTag(); 17931db9f3b2SDimitry Andric // Add the DW_AT_APPLE_origin attribute to Compile Unit die if we have 17941db9f3b2SDimitry Andric // an install name and the DWARF doesn't have the attribute yet. 17951db9f3b2SDimitry Andric const bool NeedsAppleOrigin = (Tag == dwarf::DW_TAG_compile_unit) && 17961db9f3b2SDimitry Andric LibraryInstallName.has_value() && 17971db9f3b2SDimitry Andric !AttrInfo.HasAppleOrigin; 17981db9f3b2SDimitry Andric if (NeedsAppleOrigin) { 17991db9f3b2SDimitry Andric auto StringEntry = DebugStrPool.getEntry(LibraryInstallName.value()); 18001db9f3b2SDimitry Andric Die->addValue(DIEAlloc, dwarf::Attribute(dwarf::DW_AT_APPLE_origin), 18011db9f3b2SDimitry Andric dwarf::DW_FORM_strp, DIEInteger(StringEntry.getOffset())); 18021db9f3b2SDimitry Andric AttrInfo.Name = StringEntry; 18031db9f3b2SDimitry Andric OutOffset += 4; 18041db9f3b2SDimitry Andric } 18051db9f3b2SDimitry Andric 18061db9f3b2SDimitry Andric // Look for accelerator entries. 18071db9f3b2SDimitry Andric // FIXME: This is slightly wrong. An inline_subroutine without a 18081db9f3b2SDimitry Andric // low_pc, but with AT_ranges might be interesting to get into the 18091db9f3b2SDimitry Andric // accelerator tables too. For now stick with dsymutil's behavior. 18101db9f3b2SDimitry Andric if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) && 18111db9f3b2SDimitry Andric Tag != dwarf::DW_TAG_compile_unit && 18121db9f3b2SDimitry Andric getDIENames(InputDIE, AttrInfo, DebugStrPool, 18131db9f3b2SDimitry Andric Tag != dwarf::DW_TAG_inlined_subroutine)) { 18141db9f3b2SDimitry Andric if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name) 18151db9f3b2SDimitry Andric Unit.addNameAccelerator(Die, AttrInfo.MangledName, 18161db9f3b2SDimitry Andric Tag == dwarf::DW_TAG_inlined_subroutine); 18171db9f3b2SDimitry Andric if (AttrInfo.Name) { 18181db9f3b2SDimitry Andric if (AttrInfo.NameWithoutTemplate) 18191db9f3b2SDimitry Andric Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate, 18201db9f3b2SDimitry Andric /* SkipPubSection */ true); 18211db9f3b2SDimitry Andric Unit.addNameAccelerator(Die, AttrInfo.Name, 18221db9f3b2SDimitry Andric Tag == dwarf::DW_TAG_inlined_subroutine); 18231db9f3b2SDimitry Andric } 18241db9f3b2SDimitry Andric if (AttrInfo.Name) 18251db9f3b2SDimitry Andric addObjCAccelerator(Unit, Die, AttrInfo.Name, DebugStrPool, 18261db9f3b2SDimitry Andric /* SkipPubSection =*/true); 18271db9f3b2SDimitry Andric 18281db9f3b2SDimitry Andric } else if (Tag == dwarf::DW_TAG_namespace) { 18291db9f3b2SDimitry Andric if (!AttrInfo.Name) 18301db9f3b2SDimitry Andric AttrInfo.Name = DebugStrPool.getEntry("(anonymous namespace)"); 18311db9f3b2SDimitry Andric Unit.addNamespaceAccelerator(Die, AttrInfo.Name); 18321db9f3b2SDimitry Andric } else if (Tag == dwarf::DW_TAG_imported_declaration && AttrInfo.Name) { 18331db9f3b2SDimitry Andric Unit.addNamespaceAccelerator(Die, AttrInfo.Name); 18341db9f3b2SDimitry Andric } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration && 18351db9f3b2SDimitry Andric getDIENames(InputDIE, AttrInfo, DebugStrPool) && AttrInfo.Name && 18361db9f3b2SDimitry Andric AttrInfo.Name.getString()[0]) { 18371db9f3b2SDimitry Andric uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, File); 18381db9f3b2SDimitry Andric uint64_t RuntimeLang = 18391db9f3b2SDimitry Andric dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class)) 18401db9f3b2SDimitry Andric .value_or(0); 18411db9f3b2SDimitry Andric bool ObjCClassIsImplementation = 18421db9f3b2SDimitry Andric (RuntimeLang == dwarf::DW_LANG_ObjC || 18431db9f3b2SDimitry Andric RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) && 18441db9f3b2SDimitry Andric dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type)) 18451db9f3b2SDimitry Andric .value_or(0); 18461db9f3b2SDimitry Andric Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation, 18471db9f3b2SDimitry Andric Hash); 18481db9f3b2SDimitry Andric } 18491db9f3b2SDimitry Andric 18501db9f3b2SDimitry Andric // Determine whether there are any children that we want to keep. 18511db9f3b2SDimitry Andric bool HasChildren = false; 18521db9f3b2SDimitry Andric for (auto Child : InputDIE.children()) { 18531db9f3b2SDimitry Andric unsigned Idx = U.getDIEIndex(Child); 18541db9f3b2SDimitry Andric if (Unit.getInfo(Idx).Keep) { 18551db9f3b2SDimitry Andric HasChildren = true; 18561db9f3b2SDimitry Andric break; 18571db9f3b2SDimitry Andric } 18581db9f3b2SDimitry Andric } 18591db9f3b2SDimitry Andric 18601db9f3b2SDimitry Andric if (Unit.getOrigUnit().getVersion() >= 5 && !AttrInfo.AttrStrOffsetBaseSeen && 18611db9f3b2SDimitry Andric Die->getTag() == dwarf::DW_TAG_compile_unit) { 18621db9f3b2SDimitry Andric // No DW_AT_str_offsets_base seen, add it to the DIE. 18631db9f3b2SDimitry Andric Die->addValue(DIEAlloc, dwarf::DW_AT_str_offsets_base, 18641db9f3b2SDimitry Andric dwarf::DW_FORM_sec_offset, DIEInteger(8)); 18651db9f3b2SDimitry Andric OutOffset += 4; 18661db9f3b2SDimitry Andric } 18671db9f3b2SDimitry Andric 18681db9f3b2SDimitry Andric DIEAbbrev NewAbbrev = Die->generateAbbrev(); 18691db9f3b2SDimitry Andric if (HasChildren) 18701db9f3b2SDimitry Andric NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes); 18711db9f3b2SDimitry Andric // Assign a permanent abbrev number 18721db9f3b2SDimitry Andric Linker.assignAbbrev(NewAbbrev); 18731db9f3b2SDimitry Andric Die->setAbbrevNumber(NewAbbrev.getNumber()); 18741db9f3b2SDimitry Andric 18751db9f3b2SDimitry Andric uint64_t AbbrevNumberSize = getULEB128Size(Die->getAbbrevNumber()); 18761db9f3b2SDimitry Andric 18771db9f3b2SDimitry Andric // Add the size of the abbreviation number to the output offset. 18781db9f3b2SDimitry Andric OutOffset += AbbrevNumberSize; 18791db9f3b2SDimitry Andric 18801db9f3b2SDimitry Andric // Update fixups with the size of the abbreviation number 18811db9f3b2SDimitry Andric for (AttributeLinkedOffsetFixup &F : AttributesFixups) 18821db9f3b2SDimitry Andric F.LinkedOffsetFixupVal += AbbrevNumberSize; 18831db9f3b2SDimitry Andric 18841db9f3b2SDimitry Andric for (AttributeLinkedOffsetFixup &F : AttributesFixups) 18851db9f3b2SDimitry Andric ObjFile.Addresses->updateAndSaveValidRelocs( 18861db9f3b2SDimitry Andric Unit.getOrigUnit().getVersion() >= 5, Unit.getOrigUnit().getOffset(), 18871db9f3b2SDimitry Andric F.LinkedOffsetFixupVal, F.InputAttrStartOffset, F.InputAttrEndOffset); 18881db9f3b2SDimitry Andric 18891db9f3b2SDimitry Andric if (!HasChildren) { 18901db9f3b2SDimitry Andric // Update our size. 18911db9f3b2SDimitry Andric Die->setSize(OutOffset - Die->getOffset()); 18921db9f3b2SDimitry Andric return Die; 18931db9f3b2SDimitry Andric } 18941db9f3b2SDimitry Andric 18951db9f3b2SDimitry Andric // Recursively clone children. 18961db9f3b2SDimitry Andric for (auto Child : InputDIE.children()) { 18971db9f3b2SDimitry Andric if (DIE *Clone = cloneDIE(Child, File, Unit, PCOffset, OutOffset, Flags, 18981db9f3b2SDimitry Andric IsLittleEndian)) { 18991db9f3b2SDimitry Andric Die->addChild(Clone); 19001db9f3b2SDimitry Andric OutOffset = Clone->getOffset() + Clone->getSize(); 19011db9f3b2SDimitry Andric } 19021db9f3b2SDimitry Andric } 19031db9f3b2SDimitry Andric 19041db9f3b2SDimitry Andric // Account for the end of children marker. 19051db9f3b2SDimitry Andric OutOffset += sizeof(int8_t); 19061db9f3b2SDimitry Andric // Update our size. 19071db9f3b2SDimitry Andric Die->setSize(OutOffset - Die->getOffset()); 19081db9f3b2SDimitry Andric return Die; 19091db9f3b2SDimitry Andric } 19101db9f3b2SDimitry Andric 19111db9f3b2SDimitry Andric /// Patch the input object file relevant debug_ranges or debug_rnglists 19121db9f3b2SDimitry Andric /// entries and emit them in the output file. Update the relevant attributes 19131db9f3b2SDimitry Andric /// to point at the new entries. 19141db9f3b2SDimitry Andric void DWARFLinker::generateUnitRanges(CompileUnit &Unit, const DWARFFile &File, 19151db9f3b2SDimitry Andric DebugDieValuePool &AddrPool) const { 19161db9f3b2SDimitry Andric if (LLVM_UNLIKELY(Options.Update)) 19171db9f3b2SDimitry Andric return; 19181db9f3b2SDimitry Andric 19191db9f3b2SDimitry Andric const auto &FunctionRanges = Unit.getFunctionRanges(); 19201db9f3b2SDimitry Andric 19211db9f3b2SDimitry Andric // Build set of linked address ranges for unit function ranges. 19221db9f3b2SDimitry Andric AddressRanges LinkedFunctionRanges; 19231db9f3b2SDimitry Andric for (const AddressRangeValuePair &Range : FunctionRanges) 19241db9f3b2SDimitry Andric LinkedFunctionRanges.insert( 19251db9f3b2SDimitry Andric {Range.Range.start() + Range.Value, Range.Range.end() + Range.Value}); 19261db9f3b2SDimitry Andric 19271db9f3b2SDimitry Andric // Emit LinkedFunctionRanges into .debug_aranges 19281db9f3b2SDimitry Andric if (!LinkedFunctionRanges.empty()) 19291db9f3b2SDimitry Andric TheDwarfEmitter->emitDwarfDebugArangesTable(Unit, LinkedFunctionRanges); 19301db9f3b2SDimitry Andric 19311db9f3b2SDimitry Andric RngListAttributesTy AllRngListAttributes = Unit.getRangesAttributes(); 19321db9f3b2SDimitry Andric std::optional<PatchLocation> UnitRngListAttribute = 19331db9f3b2SDimitry Andric Unit.getUnitRangesAttribute(); 19341db9f3b2SDimitry Andric 19351db9f3b2SDimitry Andric if (!AllRngListAttributes.empty() || UnitRngListAttribute) { 19361db9f3b2SDimitry Andric std::optional<AddressRangeValuePair> CachedRange; 19371db9f3b2SDimitry Andric MCSymbol *EndLabel = TheDwarfEmitter->emitDwarfDebugRangeListHeader(Unit); 19381db9f3b2SDimitry Andric 19391db9f3b2SDimitry Andric // Read original address ranges, apply relocation value, emit linked address 19401db9f3b2SDimitry Andric // ranges. 19411db9f3b2SDimitry Andric for (PatchLocation &AttributePatch : AllRngListAttributes) { 19421db9f3b2SDimitry Andric // Get ranges from the source DWARF corresponding to the current 19431db9f3b2SDimitry Andric // attribute. 19441db9f3b2SDimitry Andric AddressRanges LinkedRanges; 19451db9f3b2SDimitry Andric if (Expected<DWARFAddressRangesVector> OriginalRanges = 19461db9f3b2SDimitry Andric Unit.getOrigUnit().findRnglistFromOffset(AttributePatch.get())) { 19471db9f3b2SDimitry Andric // Apply relocation adjustment. 19481db9f3b2SDimitry Andric for (const auto &Range : *OriginalRanges) { 19491db9f3b2SDimitry Andric if (!CachedRange || !CachedRange->Range.contains(Range.LowPC)) 19501db9f3b2SDimitry Andric CachedRange = FunctionRanges.getRangeThatContains(Range.LowPC); 19511db9f3b2SDimitry Andric 19521db9f3b2SDimitry Andric // All range entries should lie in the function range. 19531db9f3b2SDimitry Andric if (!CachedRange) { 19541db9f3b2SDimitry Andric reportWarning("inconsistent range data.", File); 19551db9f3b2SDimitry Andric continue; 19561db9f3b2SDimitry Andric } 19571db9f3b2SDimitry Andric 19581db9f3b2SDimitry Andric // Store range for emiting. 19591db9f3b2SDimitry Andric LinkedRanges.insert({Range.LowPC + CachedRange->Value, 19601db9f3b2SDimitry Andric Range.HighPC + CachedRange->Value}); 19611db9f3b2SDimitry Andric } 19621db9f3b2SDimitry Andric } else { 19631db9f3b2SDimitry Andric llvm::consumeError(OriginalRanges.takeError()); 19641db9f3b2SDimitry Andric reportWarning("invalid range list ignored.", File); 19651db9f3b2SDimitry Andric } 19661db9f3b2SDimitry Andric 19671db9f3b2SDimitry Andric // Emit linked ranges. 19681db9f3b2SDimitry Andric TheDwarfEmitter->emitDwarfDebugRangeListFragment( 19691db9f3b2SDimitry Andric Unit, LinkedRanges, AttributePatch, AddrPool); 19701db9f3b2SDimitry Andric } 19711db9f3b2SDimitry Andric 19721db9f3b2SDimitry Andric // Emit ranges for Unit AT_ranges attribute. 19731db9f3b2SDimitry Andric if (UnitRngListAttribute.has_value()) 19741db9f3b2SDimitry Andric TheDwarfEmitter->emitDwarfDebugRangeListFragment( 19751db9f3b2SDimitry Andric Unit, LinkedFunctionRanges, *UnitRngListAttribute, AddrPool); 19761db9f3b2SDimitry Andric 19771db9f3b2SDimitry Andric // Emit ranges footer. 19781db9f3b2SDimitry Andric TheDwarfEmitter->emitDwarfDebugRangeListFooter(Unit, EndLabel); 19791db9f3b2SDimitry Andric } 19801db9f3b2SDimitry Andric } 19811db9f3b2SDimitry Andric 19821db9f3b2SDimitry Andric void DWARFLinker::DIECloner::generateUnitLocations( 19831db9f3b2SDimitry Andric CompileUnit &Unit, const DWARFFile &File, 19841db9f3b2SDimitry Andric ExpressionHandlerRef ExprHandler) { 19851db9f3b2SDimitry Andric if (LLVM_UNLIKELY(Linker.Options.Update)) 19861db9f3b2SDimitry Andric return; 19871db9f3b2SDimitry Andric 19881db9f3b2SDimitry Andric const LocListAttributesTy &AllLocListAttributes = 19891db9f3b2SDimitry Andric Unit.getLocationAttributes(); 19901db9f3b2SDimitry Andric 19911db9f3b2SDimitry Andric if (AllLocListAttributes.empty()) 19921db9f3b2SDimitry Andric return; 19931db9f3b2SDimitry Andric 19941db9f3b2SDimitry Andric // Emit locations list table header. 19951db9f3b2SDimitry Andric MCSymbol *EndLabel = Emitter->emitDwarfDebugLocListHeader(Unit); 19961db9f3b2SDimitry Andric 19971db9f3b2SDimitry Andric for (auto &CurLocAttr : AllLocListAttributes) { 19981db9f3b2SDimitry Andric // Get location expressions vector corresponding to the current attribute 19991db9f3b2SDimitry Andric // from the source DWARF. 20001db9f3b2SDimitry Andric Expected<DWARFLocationExpressionsVector> OriginalLocations = 20011db9f3b2SDimitry Andric Unit.getOrigUnit().findLoclistFromOffset(CurLocAttr.get()); 20021db9f3b2SDimitry Andric 20031db9f3b2SDimitry Andric if (!OriginalLocations) { 20041db9f3b2SDimitry Andric llvm::consumeError(OriginalLocations.takeError()); 20051db9f3b2SDimitry Andric Linker.reportWarning("Invalid location attribute ignored.", File); 20061db9f3b2SDimitry Andric continue; 20071db9f3b2SDimitry Andric } 20081db9f3b2SDimitry Andric 20091db9f3b2SDimitry Andric DWARFLocationExpressionsVector LinkedLocationExpressions; 20101db9f3b2SDimitry Andric for (DWARFLocationExpression &CurExpression : *OriginalLocations) { 20111db9f3b2SDimitry Andric DWARFLocationExpression LinkedExpression; 20121db9f3b2SDimitry Andric 20131db9f3b2SDimitry Andric if (CurExpression.Range) { 20141db9f3b2SDimitry Andric // Relocate address range. 20151db9f3b2SDimitry Andric LinkedExpression.Range = { 20161db9f3b2SDimitry Andric CurExpression.Range->LowPC + CurLocAttr.RelocAdjustment, 20171db9f3b2SDimitry Andric CurExpression.Range->HighPC + CurLocAttr.RelocAdjustment}; 20181db9f3b2SDimitry Andric } 20191db9f3b2SDimitry Andric 20201db9f3b2SDimitry Andric // Clone expression. 20211db9f3b2SDimitry Andric LinkedExpression.Expr.reserve(CurExpression.Expr.size()); 20221db9f3b2SDimitry Andric ExprHandler(CurExpression.Expr, LinkedExpression.Expr, 20231db9f3b2SDimitry Andric CurLocAttr.RelocAdjustment); 20241db9f3b2SDimitry Andric 20251db9f3b2SDimitry Andric LinkedLocationExpressions.push_back(LinkedExpression); 20261db9f3b2SDimitry Andric } 20271db9f3b2SDimitry Andric 20281db9f3b2SDimitry Andric // Emit locations list table fragment corresponding to the CurLocAttr. 20291db9f3b2SDimitry Andric Emitter->emitDwarfDebugLocListFragment(Unit, LinkedLocationExpressions, 20301db9f3b2SDimitry Andric CurLocAttr, AddrPool); 20311db9f3b2SDimitry Andric } 20321db9f3b2SDimitry Andric 20331db9f3b2SDimitry Andric // Emit locations list table footer. 20341db9f3b2SDimitry Andric Emitter->emitDwarfDebugLocListFooter(Unit, EndLabel); 20351db9f3b2SDimitry Andric } 20361db9f3b2SDimitry Andric 20371db9f3b2SDimitry Andric static void patchAddrBase(DIE &Die, DIEInteger Offset) { 20381db9f3b2SDimitry Andric for (auto &V : Die.values()) 20391db9f3b2SDimitry Andric if (V.getAttribute() == dwarf::DW_AT_addr_base) { 20401db9f3b2SDimitry Andric V = DIEValue(V.getAttribute(), V.getForm(), Offset); 20411db9f3b2SDimitry Andric return; 20421db9f3b2SDimitry Andric } 20431db9f3b2SDimitry Andric 20441db9f3b2SDimitry Andric llvm_unreachable("Didn't find a DW_AT_addr_base in cloned DIE!"); 20451db9f3b2SDimitry Andric } 20461db9f3b2SDimitry Andric 20471db9f3b2SDimitry Andric void DWARFLinker::DIECloner::emitDebugAddrSection( 20481db9f3b2SDimitry Andric CompileUnit &Unit, const uint16_t DwarfVersion) const { 20491db9f3b2SDimitry Andric 20501db9f3b2SDimitry Andric if (LLVM_UNLIKELY(Linker.Options.Update)) 20511db9f3b2SDimitry Andric return; 20521db9f3b2SDimitry Andric 20531db9f3b2SDimitry Andric if (DwarfVersion < 5) 20541db9f3b2SDimitry Andric return; 20551db9f3b2SDimitry Andric 20567a6dacacSDimitry Andric if (AddrPool.getValues().empty()) 20571db9f3b2SDimitry Andric return; 20581db9f3b2SDimitry Andric 20591db9f3b2SDimitry Andric MCSymbol *EndLabel = Emitter->emitDwarfDebugAddrsHeader(Unit); 20601db9f3b2SDimitry Andric patchAddrBase(*Unit.getOutputUnitDIE(), 20611db9f3b2SDimitry Andric DIEInteger(Emitter->getDebugAddrSectionSize())); 20627a6dacacSDimitry Andric Emitter->emitDwarfDebugAddrs(AddrPool.getValues(), 20631db9f3b2SDimitry Andric Unit.getOrigUnit().getAddressByteSize()); 20641db9f3b2SDimitry Andric Emitter->emitDwarfDebugAddrsFooter(Unit, EndLabel); 20651db9f3b2SDimitry Andric } 20661db9f3b2SDimitry Andric 20671db9f3b2SDimitry Andric /// Insert the new line info sequence \p Seq into the current 20681db9f3b2SDimitry Andric /// set of already linked line info \p Rows. 20691db9f3b2SDimitry Andric static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq, 20701db9f3b2SDimitry Andric std::vector<DWARFDebugLine::Row> &Rows) { 20711db9f3b2SDimitry Andric if (Seq.empty()) 20721db9f3b2SDimitry Andric return; 20731db9f3b2SDimitry Andric 20741db9f3b2SDimitry Andric if (!Rows.empty() && Rows.back().Address < Seq.front().Address) { 20751db9f3b2SDimitry Andric llvm::append_range(Rows, Seq); 20761db9f3b2SDimitry Andric Seq.clear(); 20771db9f3b2SDimitry Andric return; 20781db9f3b2SDimitry Andric } 20791db9f3b2SDimitry Andric 20801db9f3b2SDimitry Andric object::SectionedAddress Front = Seq.front().Address; 20811db9f3b2SDimitry Andric auto InsertPoint = partition_point( 20821db9f3b2SDimitry Andric Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; }); 20831db9f3b2SDimitry Andric 20841db9f3b2SDimitry Andric // FIXME: this only removes the unneeded end_sequence if the 20851db9f3b2SDimitry Andric // sequences have been inserted in order. Using a global sort like 20861db9f3b2SDimitry Andric // described in generateLineTableForUnit() and delaying the end_sequene 20871db9f3b2SDimitry Andric // elimination to emitLineTableForUnit() we can get rid of all of them. 20881db9f3b2SDimitry Andric if (InsertPoint != Rows.end() && InsertPoint->Address == Front && 20891db9f3b2SDimitry Andric InsertPoint->EndSequence) { 20901db9f3b2SDimitry Andric *InsertPoint = Seq.front(); 20911db9f3b2SDimitry Andric Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end()); 20921db9f3b2SDimitry Andric } else { 20931db9f3b2SDimitry Andric Rows.insert(InsertPoint, Seq.begin(), Seq.end()); 20941db9f3b2SDimitry Andric } 20951db9f3b2SDimitry Andric 20961db9f3b2SDimitry Andric Seq.clear(); 20971db9f3b2SDimitry Andric } 20981db9f3b2SDimitry Andric 20991db9f3b2SDimitry Andric static void patchStmtList(DIE &Die, DIEInteger Offset) { 21001db9f3b2SDimitry Andric for (auto &V : Die.values()) 21011db9f3b2SDimitry Andric if (V.getAttribute() == dwarf::DW_AT_stmt_list) { 21021db9f3b2SDimitry Andric V = DIEValue(V.getAttribute(), V.getForm(), Offset); 21031db9f3b2SDimitry Andric return; 21041db9f3b2SDimitry Andric } 21051db9f3b2SDimitry Andric 21061db9f3b2SDimitry Andric llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!"); 21071db9f3b2SDimitry Andric } 21081db9f3b2SDimitry Andric 21091db9f3b2SDimitry Andric void DWARFLinker::DIECloner::rememberUnitForMacroOffset(CompileUnit &Unit) { 21101db9f3b2SDimitry Andric DWARFUnit &OrigUnit = Unit.getOrigUnit(); 21111db9f3b2SDimitry Andric DWARFDie OrigUnitDie = OrigUnit.getUnitDIE(); 21121db9f3b2SDimitry Andric 21131db9f3b2SDimitry Andric if (std::optional<uint64_t> MacroAttr = 21141db9f3b2SDimitry Andric dwarf::toSectionOffset(OrigUnitDie.find(dwarf::DW_AT_macros))) { 21151db9f3b2SDimitry Andric UnitMacroMap.insert(std::make_pair(*MacroAttr, &Unit)); 21161db9f3b2SDimitry Andric return; 21171db9f3b2SDimitry Andric } 21181db9f3b2SDimitry Andric 21191db9f3b2SDimitry Andric if (std::optional<uint64_t> MacroAttr = 21201db9f3b2SDimitry Andric dwarf::toSectionOffset(OrigUnitDie.find(dwarf::DW_AT_macro_info))) { 21211db9f3b2SDimitry Andric UnitMacroMap.insert(std::make_pair(*MacroAttr, &Unit)); 21221db9f3b2SDimitry Andric return; 21231db9f3b2SDimitry Andric } 21241db9f3b2SDimitry Andric } 21251db9f3b2SDimitry Andric 21261db9f3b2SDimitry Andric void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) { 21271db9f3b2SDimitry Andric if (LLVM_UNLIKELY(Emitter == nullptr)) 21281db9f3b2SDimitry Andric return; 21291db9f3b2SDimitry Andric 21301db9f3b2SDimitry Andric // Check whether DW_AT_stmt_list attribute is presented. 21311db9f3b2SDimitry Andric DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE(); 21321db9f3b2SDimitry Andric auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list)); 21331db9f3b2SDimitry Andric if (!StmtList) 21341db9f3b2SDimitry Andric return; 21351db9f3b2SDimitry Andric 21361db9f3b2SDimitry Andric // Update the cloned DW_AT_stmt_list with the correct debug_line offset. 21371db9f3b2SDimitry Andric if (auto *OutputDIE = Unit.getOutputUnitDIE()) 21381db9f3b2SDimitry Andric patchStmtList(*OutputDIE, DIEInteger(Emitter->getLineSectionSize())); 21391db9f3b2SDimitry Andric 21401db9f3b2SDimitry Andric if (const DWARFDebugLine::LineTable *LT = 21411db9f3b2SDimitry Andric ObjFile.Dwarf->getLineTableForUnit(&Unit.getOrigUnit())) { 21421db9f3b2SDimitry Andric 21431db9f3b2SDimitry Andric DWARFDebugLine::LineTable LineTable; 21441db9f3b2SDimitry Andric 21451db9f3b2SDimitry Andric // Set Line Table header. 21461db9f3b2SDimitry Andric LineTable.Prologue = LT->Prologue; 21471db9f3b2SDimitry Andric 21481db9f3b2SDimitry Andric // Set Line Table Rows. 21491db9f3b2SDimitry Andric if (Linker.Options.Update) { 21501db9f3b2SDimitry Andric LineTable.Rows = LT->Rows; 21511db9f3b2SDimitry Andric // If all the line table contains is a DW_LNE_end_sequence, clear the line 21521db9f3b2SDimitry Andric // table rows, it will be inserted again in the DWARFStreamer. 21531db9f3b2SDimitry Andric if (LineTable.Rows.size() == 1 && LineTable.Rows[0].EndSequence) 21541db9f3b2SDimitry Andric LineTable.Rows.clear(); 21551db9f3b2SDimitry Andric 21561db9f3b2SDimitry Andric LineTable.Sequences = LT->Sequences; 21571db9f3b2SDimitry Andric } else { 21581db9f3b2SDimitry Andric // This vector is the output line table. 21591db9f3b2SDimitry Andric std::vector<DWARFDebugLine::Row> NewRows; 21601db9f3b2SDimitry Andric NewRows.reserve(LT->Rows.size()); 21611db9f3b2SDimitry Andric 21621db9f3b2SDimitry Andric // Current sequence of rows being extracted, before being inserted 21631db9f3b2SDimitry Andric // in NewRows. 21641db9f3b2SDimitry Andric std::vector<DWARFDebugLine::Row> Seq; 21651db9f3b2SDimitry Andric 21661db9f3b2SDimitry Andric const auto &FunctionRanges = Unit.getFunctionRanges(); 21671db9f3b2SDimitry Andric std::optional<AddressRangeValuePair> CurrRange; 21681db9f3b2SDimitry Andric 21691db9f3b2SDimitry Andric // FIXME: This logic is meant to generate exactly the same output as 21701db9f3b2SDimitry Andric // Darwin's classic dsymutil. There is a nicer way to implement this 21711db9f3b2SDimitry Andric // by simply putting all the relocated line info in NewRows and simply 21721db9f3b2SDimitry Andric // sorting NewRows before passing it to emitLineTableForUnit. This 21731db9f3b2SDimitry Andric // should be correct as sequences for a function should stay 21741db9f3b2SDimitry Andric // together in the sorted output. There are a few corner cases that 21751db9f3b2SDimitry Andric // look suspicious though, and that required to implement the logic 21761db9f3b2SDimitry Andric // this way. Revisit that once initial validation is finished. 21771db9f3b2SDimitry Andric 21781db9f3b2SDimitry Andric // Iterate over the object file line info and extract the sequences 21791db9f3b2SDimitry Andric // that correspond to linked functions. 21801db9f3b2SDimitry Andric for (DWARFDebugLine::Row Row : LT->Rows) { 21811db9f3b2SDimitry Andric // Check whether we stepped out of the range. The range is 21821db9f3b2SDimitry Andric // half-open, but consider accept the end address of the range if 21831db9f3b2SDimitry Andric // it is marked as end_sequence in the input (because in that 21841db9f3b2SDimitry Andric // case, the relocation offset is accurate and that entry won't 21851db9f3b2SDimitry Andric // serve as the start of another function). 21861db9f3b2SDimitry Andric if (!CurrRange || !CurrRange->Range.contains(Row.Address.Address)) { 21871db9f3b2SDimitry Andric // We just stepped out of a known range. Insert a end_sequence 21881db9f3b2SDimitry Andric // corresponding to the end of the range. 21891db9f3b2SDimitry Andric uint64_t StopAddress = 21901db9f3b2SDimitry Andric CurrRange ? CurrRange->Range.end() + CurrRange->Value : -1ULL; 21911db9f3b2SDimitry Andric CurrRange = FunctionRanges.getRangeThatContains(Row.Address.Address); 21921db9f3b2SDimitry Andric if (StopAddress != -1ULL && !Seq.empty()) { 21931db9f3b2SDimitry Andric // Insert end sequence row with the computed end address, but 21941db9f3b2SDimitry Andric // the same line as the previous one. 21951db9f3b2SDimitry Andric auto NextLine = Seq.back(); 21961db9f3b2SDimitry Andric NextLine.Address.Address = StopAddress; 21971db9f3b2SDimitry Andric NextLine.EndSequence = 1; 21981db9f3b2SDimitry Andric NextLine.PrologueEnd = 0; 21991db9f3b2SDimitry Andric NextLine.BasicBlock = 0; 22001db9f3b2SDimitry Andric NextLine.EpilogueBegin = 0; 22011db9f3b2SDimitry Andric Seq.push_back(NextLine); 22021db9f3b2SDimitry Andric insertLineSequence(Seq, NewRows); 22031db9f3b2SDimitry Andric } 22041db9f3b2SDimitry Andric 22051db9f3b2SDimitry Andric if (!CurrRange) 22061db9f3b2SDimitry Andric continue; 22071db9f3b2SDimitry Andric } 22081db9f3b2SDimitry Andric 22091db9f3b2SDimitry Andric // Ignore empty sequences. 22101db9f3b2SDimitry Andric if (Row.EndSequence && Seq.empty()) 22111db9f3b2SDimitry Andric continue; 22121db9f3b2SDimitry Andric 22131db9f3b2SDimitry Andric // Relocate row address and add it to the current sequence. 22141db9f3b2SDimitry Andric Row.Address.Address += CurrRange->Value; 22151db9f3b2SDimitry Andric Seq.emplace_back(Row); 22161db9f3b2SDimitry Andric 22171db9f3b2SDimitry Andric if (Row.EndSequence) 22181db9f3b2SDimitry Andric insertLineSequence(Seq, NewRows); 22191db9f3b2SDimitry Andric } 22201db9f3b2SDimitry Andric 22211db9f3b2SDimitry Andric LineTable.Rows = std::move(NewRows); 22221db9f3b2SDimitry Andric } 22231db9f3b2SDimitry Andric 22241db9f3b2SDimitry Andric Emitter->emitLineTableForUnit(LineTable, Unit, DebugStrPool, 22251db9f3b2SDimitry Andric DebugLineStrPool); 22261db9f3b2SDimitry Andric } else 22271db9f3b2SDimitry Andric Linker.reportWarning("Cann't load line table.", ObjFile); 22281db9f3b2SDimitry Andric } 22291db9f3b2SDimitry Andric 22301db9f3b2SDimitry Andric void DWARFLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) { 22311db9f3b2SDimitry Andric for (AccelTableKind AccelTableKind : Options.AccelTables) { 22321db9f3b2SDimitry Andric switch (AccelTableKind) { 22331db9f3b2SDimitry Andric case AccelTableKind::Apple: { 22341db9f3b2SDimitry Andric // Add namespaces. 22351db9f3b2SDimitry Andric for (const auto &Namespace : Unit.getNamespaces()) 22361db9f3b2SDimitry Andric AppleNamespaces.addName(Namespace.Name, Namespace.Die->getOffset() + 22371db9f3b2SDimitry Andric Unit.getStartOffset()); 22381db9f3b2SDimitry Andric // Add names. 22391db9f3b2SDimitry Andric for (const auto &Pubname : Unit.getPubnames()) 22401db9f3b2SDimitry Andric AppleNames.addName(Pubname.Name, 22411db9f3b2SDimitry Andric Pubname.Die->getOffset() + Unit.getStartOffset()); 22421db9f3b2SDimitry Andric // Add types. 22431db9f3b2SDimitry Andric for (const auto &Pubtype : Unit.getPubtypes()) 22441db9f3b2SDimitry Andric AppleTypes.addName( 22451db9f3b2SDimitry Andric Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(), 22461db9f3b2SDimitry Andric Pubtype.Die->getTag(), 22471db9f3b2SDimitry Andric Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation 22481db9f3b2SDimitry Andric : 0, 22491db9f3b2SDimitry Andric Pubtype.QualifiedNameHash); 22501db9f3b2SDimitry Andric // Add ObjC names. 22511db9f3b2SDimitry Andric for (const auto &ObjC : Unit.getObjC()) 22521db9f3b2SDimitry Andric AppleObjc.addName(ObjC.Name, 22531db9f3b2SDimitry Andric ObjC.Die->getOffset() + Unit.getStartOffset()); 22541db9f3b2SDimitry Andric } break; 22551db9f3b2SDimitry Andric case AccelTableKind::Pub: { 22561db9f3b2SDimitry Andric TheDwarfEmitter->emitPubNamesForUnit(Unit); 22571db9f3b2SDimitry Andric TheDwarfEmitter->emitPubTypesForUnit(Unit); 22581db9f3b2SDimitry Andric } break; 22591db9f3b2SDimitry Andric case AccelTableKind::DebugNames: { 22601db9f3b2SDimitry Andric for (const auto &Namespace : Unit.getNamespaces()) 22617a6dacacSDimitry Andric DebugNames.addName( 22627a6dacacSDimitry Andric Namespace.Name, Namespace.Die->getOffset(), 22637a6dacacSDimitry Andric DWARF5AccelTableData::getDefiningParentDieOffset(*Namespace.Die), 2264*0fca6ea1SDimitry Andric Namespace.Die->getTag(), Unit.getUniqueID(), 2265*0fca6ea1SDimitry Andric Unit.getTag() == dwarf::DW_TAG_type_unit); 22661db9f3b2SDimitry Andric for (const auto &Pubname : Unit.getPubnames()) 22677a6dacacSDimitry Andric DebugNames.addName( 22687a6dacacSDimitry Andric Pubname.Name, Pubname.Die->getOffset(), 22697a6dacacSDimitry Andric DWARF5AccelTableData::getDefiningParentDieOffset(*Pubname.Die), 2270*0fca6ea1SDimitry Andric Pubname.Die->getTag(), Unit.getUniqueID(), 2271*0fca6ea1SDimitry Andric Unit.getTag() == dwarf::DW_TAG_type_unit); 22721db9f3b2SDimitry Andric for (const auto &Pubtype : Unit.getPubtypes()) 22737a6dacacSDimitry Andric DebugNames.addName( 22747a6dacacSDimitry Andric Pubtype.Name, Pubtype.Die->getOffset(), 22757a6dacacSDimitry Andric DWARF5AccelTableData::getDefiningParentDieOffset(*Pubtype.Die), 2276*0fca6ea1SDimitry Andric Pubtype.Die->getTag(), Unit.getUniqueID(), 2277*0fca6ea1SDimitry Andric Unit.getTag() == dwarf::DW_TAG_type_unit); 22781db9f3b2SDimitry Andric } break; 22791db9f3b2SDimitry Andric } 22801db9f3b2SDimitry Andric } 22811db9f3b2SDimitry Andric } 22821db9f3b2SDimitry Andric 22831db9f3b2SDimitry Andric /// Read the frame info stored in the object, and emit the 22841db9f3b2SDimitry Andric /// patched frame descriptions for the resulting file. 22851db9f3b2SDimitry Andric /// 22861db9f3b2SDimitry Andric /// This is actually pretty easy as the data of the CIEs and FDEs can 22871db9f3b2SDimitry Andric /// be considered as black boxes and moved as is. The only thing to do 22881db9f3b2SDimitry Andric /// is to patch the addresses in the headers. 22891db9f3b2SDimitry Andric void DWARFLinker::patchFrameInfoForObject(LinkContext &Context) { 22901db9f3b2SDimitry Andric DWARFContext &OrigDwarf = *Context.File.Dwarf; 22911db9f3b2SDimitry Andric unsigned SrcAddrSize = OrigDwarf.getDWARFObj().getAddressSize(); 22921db9f3b2SDimitry Andric 22931db9f3b2SDimitry Andric StringRef FrameData = OrigDwarf.getDWARFObj().getFrameSection().Data; 22941db9f3b2SDimitry Andric if (FrameData.empty()) 22951db9f3b2SDimitry Andric return; 22961db9f3b2SDimitry Andric 22971db9f3b2SDimitry Andric RangesTy AllUnitsRanges; 22981db9f3b2SDimitry Andric for (std::unique_ptr<CompileUnit> &Unit : Context.CompileUnits) { 22991db9f3b2SDimitry Andric for (auto CurRange : Unit->getFunctionRanges()) 23001db9f3b2SDimitry Andric AllUnitsRanges.insert(CurRange.Range, CurRange.Value); 23011db9f3b2SDimitry Andric } 23021db9f3b2SDimitry Andric 23031db9f3b2SDimitry Andric DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0); 23041db9f3b2SDimitry Andric uint64_t InputOffset = 0; 23051db9f3b2SDimitry Andric 23061db9f3b2SDimitry Andric // Store the data of the CIEs defined in this object, keyed by their 23071db9f3b2SDimitry Andric // offsets. 23081db9f3b2SDimitry Andric DenseMap<uint64_t, StringRef> LocalCIES; 23091db9f3b2SDimitry Andric 23101db9f3b2SDimitry Andric while (Data.isValidOffset(InputOffset)) { 23111db9f3b2SDimitry Andric uint64_t EntryOffset = InputOffset; 23121db9f3b2SDimitry Andric uint32_t InitialLength = Data.getU32(&InputOffset); 23131db9f3b2SDimitry Andric if (InitialLength == 0xFFFFFFFF) 23141db9f3b2SDimitry Andric return reportWarning("Dwarf64 bits no supported", Context.File); 23151db9f3b2SDimitry Andric 23161db9f3b2SDimitry Andric uint32_t CIEId = Data.getU32(&InputOffset); 23171db9f3b2SDimitry Andric if (CIEId == 0xFFFFFFFF) { 23181db9f3b2SDimitry Andric // This is a CIE, store it. 23191db9f3b2SDimitry Andric StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4); 23201db9f3b2SDimitry Andric LocalCIES[EntryOffset] = CIEData; 23211db9f3b2SDimitry Andric // The -4 is to account for the CIEId we just read. 23221db9f3b2SDimitry Andric InputOffset += InitialLength - 4; 23231db9f3b2SDimitry Andric continue; 23241db9f3b2SDimitry Andric } 23251db9f3b2SDimitry Andric 23261db9f3b2SDimitry Andric uint64_t Loc = Data.getUnsigned(&InputOffset, SrcAddrSize); 23271db9f3b2SDimitry Andric 23281db9f3b2SDimitry Andric // Some compilers seem to emit frame info that doesn't start at 23291db9f3b2SDimitry Andric // the function entry point, thus we can't just lookup the address 23301db9f3b2SDimitry Andric // in the debug map. Use the AddressInfo's range map to see if the FDE 23311db9f3b2SDimitry Andric // describes something that we can relocate. 23321db9f3b2SDimitry Andric std::optional<AddressRangeValuePair> Range = 23331db9f3b2SDimitry Andric AllUnitsRanges.getRangeThatContains(Loc); 23341db9f3b2SDimitry Andric if (!Range) { 23351db9f3b2SDimitry Andric // The +4 is to account for the size of the InitialLength field itself. 23361db9f3b2SDimitry Andric InputOffset = EntryOffset + InitialLength + 4; 23371db9f3b2SDimitry Andric continue; 23381db9f3b2SDimitry Andric } 23391db9f3b2SDimitry Andric 23401db9f3b2SDimitry Andric // This is an FDE, and we have a mapping. 23411db9f3b2SDimitry Andric // Have we already emitted a corresponding CIE? 23421db9f3b2SDimitry Andric StringRef CIEData = LocalCIES[CIEId]; 23431db9f3b2SDimitry Andric if (CIEData.empty()) 23441db9f3b2SDimitry Andric return reportWarning("Inconsistent debug_frame content. Dropping.", 23451db9f3b2SDimitry Andric Context.File); 23461db9f3b2SDimitry Andric 23471db9f3b2SDimitry Andric // Look if we already emitted a CIE that corresponds to the 23481db9f3b2SDimitry Andric // referenced one (the CIE data is the key of that lookup). 23491db9f3b2SDimitry Andric auto IteratorInserted = EmittedCIEs.insert( 23501db9f3b2SDimitry Andric std::make_pair(CIEData, TheDwarfEmitter->getFrameSectionSize())); 23511db9f3b2SDimitry Andric // If there is no CIE yet for this ID, emit it. 23521db9f3b2SDimitry Andric if (IteratorInserted.second) { 23531db9f3b2SDimitry Andric LastCIEOffset = TheDwarfEmitter->getFrameSectionSize(); 23541db9f3b2SDimitry Andric IteratorInserted.first->getValue() = LastCIEOffset; 23551db9f3b2SDimitry Andric TheDwarfEmitter->emitCIE(CIEData); 23561db9f3b2SDimitry Andric } 23571db9f3b2SDimitry Andric 23581db9f3b2SDimitry Andric // Emit the FDE with updated address and CIE pointer. 23591db9f3b2SDimitry Andric // (4 + AddrSize) is the size of the CIEId + initial_location 23601db9f3b2SDimitry Andric // fields that will get reconstructed by emitFDE(). 23611db9f3b2SDimitry Andric unsigned FDERemainingBytes = InitialLength - (4 + SrcAddrSize); 23621db9f3b2SDimitry Andric TheDwarfEmitter->emitFDE(IteratorInserted.first->getValue(), SrcAddrSize, 23631db9f3b2SDimitry Andric Loc + Range->Value, 23641db9f3b2SDimitry Andric FrameData.substr(InputOffset, FDERemainingBytes)); 23651db9f3b2SDimitry Andric InputOffset += FDERemainingBytes; 23661db9f3b2SDimitry Andric } 23671db9f3b2SDimitry Andric } 23681db9f3b2SDimitry Andric 23691db9f3b2SDimitry Andric uint32_t DWARFLinker::DIECloner::hashFullyQualifiedName(DWARFDie DIE, 23701db9f3b2SDimitry Andric CompileUnit &U, 23711db9f3b2SDimitry Andric const DWARFFile &File, 23721db9f3b2SDimitry Andric int ChildRecurseDepth) { 23731db9f3b2SDimitry Andric const char *Name = nullptr; 23741db9f3b2SDimitry Andric DWARFUnit *OrigUnit = &U.getOrigUnit(); 23751db9f3b2SDimitry Andric CompileUnit *CU = &U; 23761db9f3b2SDimitry Andric std::optional<DWARFFormValue> Ref; 23771db9f3b2SDimitry Andric 23781db9f3b2SDimitry Andric while (true) { 23791db9f3b2SDimitry Andric if (const char *CurrentName = DIE.getName(DINameKind::ShortName)) 23801db9f3b2SDimitry Andric Name = CurrentName; 23811db9f3b2SDimitry Andric 23821db9f3b2SDimitry Andric if (!(Ref = DIE.find(dwarf::DW_AT_specification)) && 23831db9f3b2SDimitry Andric !(Ref = DIE.find(dwarf::DW_AT_abstract_origin))) 23841db9f3b2SDimitry Andric break; 23851db9f3b2SDimitry Andric 23861db9f3b2SDimitry Andric if (!Ref->isFormClass(DWARFFormValue::FC_Reference)) 23871db9f3b2SDimitry Andric break; 23881db9f3b2SDimitry Andric 23891db9f3b2SDimitry Andric CompileUnit *RefCU; 23901db9f3b2SDimitry Andric if (auto RefDIE = 23911db9f3b2SDimitry Andric Linker.resolveDIEReference(File, CompileUnits, *Ref, DIE, RefCU)) { 23921db9f3b2SDimitry Andric CU = RefCU; 23931db9f3b2SDimitry Andric OrigUnit = &RefCU->getOrigUnit(); 23941db9f3b2SDimitry Andric DIE = RefDIE; 23951db9f3b2SDimitry Andric } 23961db9f3b2SDimitry Andric } 23971db9f3b2SDimitry Andric 23981db9f3b2SDimitry Andric unsigned Idx = OrigUnit->getDIEIndex(DIE); 23991db9f3b2SDimitry Andric if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace) 24001db9f3b2SDimitry Andric Name = "(anonymous namespace)"; 24011db9f3b2SDimitry Andric 24021db9f3b2SDimitry Andric if (CU->getInfo(Idx).ParentIdx == 0 || 24031db9f3b2SDimitry Andric // FIXME: dsymutil-classic compatibility. Ignore modules. 24041db9f3b2SDimitry Andric CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() == 24051db9f3b2SDimitry Andric dwarf::DW_TAG_module) 24061db9f3b2SDimitry Andric return djbHash(Name ? Name : "", djbHash(ChildRecurseDepth ? "" : "::")); 24071db9f3b2SDimitry Andric 24081db9f3b2SDimitry Andric DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx); 24091db9f3b2SDimitry Andric return djbHash( 24101db9f3b2SDimitry Andric (Name ? Name : ""), 24111db9f3b2SDimitry Andric djbHash((Name ? "::" : ""), 24121db9f3b2SDimitry Andric hashFullyQualifiedName(Die, *CU, File, ++ChildRecurseDepth))); 24131db9f3b2SDimitry Andric } 24141db9f3b2SDimitry Andric 24151db9f3b2SDimitry Andric static uint64_t getDwoId(const DWARFDie &CUDie) { 24161db9f3b2SDimitry Andric auto DwoId = dwarf::toUnsigned( 24171db9f3b2SDimitry Andric CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id})); 24181db9f3b2SDimitry Andric if (DwoId) 24191db9f3b2SDimitry Andric return *DwoId; 24201db9f3b2SDimitry Andric return 0; 24211db9f3b2SDimitry Andric } 24221db9f3b2SDimitry Andric 24231db9f3b2SDimitry Andric static std::string 24241db9f3b2SDimitry Andric remapPath(StringRef Path, 24251db9f3b2SDimitry Andric const DWARFLinkerBase::ObjectPrefixMapTy &ObjectPrefixMap) { 24261db9f3b2SDimitry Andric if (ObjectPrefixMap.empty()) 24271db9f3b2SDimitry Andric return Path.str(); 24281db9f3b2SDimitry Andric 24291db9f3b2SDimitry Andric SmallString<256> p = Path; 24301db9f3b2SDimitry Andric for (const auto &Entry : ObjectPrefixMap) 24311db9f3b2SDimitry Andric if (llvm::sys::path::replace_path_prefix(p, Entry.first, Entry.second)) 24321db9f3b2SDimitry Andric break; 24331db9f3b2SDimitry Andric return p.str().str(); 24341db9f3b2SDimitry Andric } 24351db9f3b2SDimitry Andric 24361db9f3b2SDimitry Andric static std::string 24371db9f3b2SDimitry Andric getPCMFile(const DWARFDie &CUDie, 24381db9f3b2SDimitry Andric const DWARFLinkerBase::ObjectPrefixMapTy *ObjectPrefixMap) { 24391db9f3b2SDimitry Andric std::string PCMFile = dwarf::toString( 24401db9f3b2SDimitry Andric CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), ""); 24411db9f3b2SDimitry Andric 24421db9f3b2SDimitry Andric if (PCMFile.empty()) 24431db9f3b2SDimitry Andric return PCMFile; 24441db9f3b2SDimitry Andric 24451db9f3b2SDimitry Andric if (ObjectPrefixMap) 24461db9f3b2SDimitry Andric PCMFile = remapPath(PCMFile, *ObjectPrefixMap); 24471db9f3b2SDimitry Andric 24481db9f3b2SDimitry Andric return PCMFile; 24491db9f3b2SDimitry Andric } 24501db9f3b2SDimitry Andric 24511db9f3b2SDimitry Andric std::pair<bool, bool> DWARFLinker::isClangModuleRef(const DWARFDie &CUDie, 24521db9f3b2SDimitry Andric std::string &PCMFile, 24531db9f3b2SDimitry Andric LinkContext &Context, 24541db9f3b2SDimitry Andric unsigned Indent, 24551db9f3b2SDimitry Andric bool Quiet) { 24561db9f3b2SDimitry Andric if (PCMFile.empty()) 24571db9f3b2SDimitry Andric return std::make_pair(false, false); 24581db9f3b2SDimitry Andric 24591db9f3b2SDimitry Andric // Clang module DWARF skeleton CUs abuse this for the path to the module. 24601db9f3b2SDimitry Andric uint64_t DwoId = getDwoId(CUDie); 24611db9f3b2SDimitry Andric 24621db9f3b2SDimitry Andric std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), ""); 24631db9f3b2SDimitry Andric if (Name.empty()) { 24641db9f3b2SDimitry Andric if (!Quiet) 24651db9f3b2SDimitry Andric reportWarning("Anonymous module skeleton CU for " + PCMFile, 24661db9f3b2SDimitry Andric Context.File); 24671db9f3b2SDimitry Andric return std::make_pair(true, true); 24681db9f3b2SDimitry Andric } 24691db9f3b2SDimitry Andric 24701db9f3b2SDimitry Andric if (!Quiet && Options.Verbose) { 24711db9f3b2SDimitry Andric outs().indent(Indent); 24721db9f3b2SDimitry Andric outs() << "Found clang module reference " << PCMFile; 24731db9f3b2SDimitry Andric } 24741db9f3b2SDimitry Andric 24751db9f3b2SDimitry Andric auto Cached = ClangModules.find(PCMFile); 24761db9f3b2SDimitry Andric if (Cached != ClangModules.end()) { 24771db9f3b2SDimitry Andric // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is 24781db9f3b2SDimitry Andric // fixed in clang, only warn about DWO_id mismatches in verbose mode. 24791db9f3b2SDimitry Andric // ASTFileSignatures will change randomly when a module is rebuilt. 24801db9f3b2SDimitry Andric if (!Quiet && Options.Verbose && (Cached->second != DwoId)) 24811db9f3b2SDimitry Andric reportWarning(Twine("hash mismatch: this object file was built against a " 24821db9f3b2SDimitry Andric "different version of the module ") + 24831db9f3b2SDimitry Andric PCMFile, 24841db9f3b2SDimitry Andric Context.File); 24851db9f3b2SDimitry Andric if (!Quiet && Options.Verbose) 24861db9f3b2SDimitry Andric outs() << " [cached].\n"; 24871db9f3b2SDimitry Andric return std::make_pair(true, true); 24881db9f3b2SDimitry Andric } 24891db9f3b2SDimitry Andric 24901db9f3b2SDimitry Andric return std::make_pair(true, false); 24911db9f3b2SDimitry Andric } 24921db9f3b2SDimitry Andric 24931db9f3b2SDimitry Andric bool DWARFLinker::registerModuleReference(const DWARFDie &CUDie, 24941db9f3b2SDimitry Andric LinkContext &Context, 24951db9f3b2SDimitry Andric ObjFileLoaderTy Loader, 24961db9f3b2SDimitry Andric CompileUnitHandlerTy OnCUDieLoaded, 24971db9f3b2SDimitry Andric unsigned Indent) { 24981db9f3b2SDimitry Andric std::string PCMFile = getPCMFile(CUDie, Options.ObjectPrefixMap); 24991db9f3b2SDimitry Andric std::pair<bool, bool> IsClangModuleRef = 25001db9f3b2SDimitry Andric isClangModuleRef(CUDie, PCMFile, Context, Indent, false); 25011db9f3b2SDimitry Andric 25021db9f3b2SDimitry Andric if (!IsClangModuleRef.first) 25031db9f3b2SDimitry Andric return false; 25041db9f3b2SDimitry Andric 25051db9f3b2SDimitry Andric if (IsClangModuleRef.second) 25061db9f3b2SDimitry Andric return true; 25071db9f3b2SDimitry Andric 25081db9f3b2SDimitry Andric if (Options.Verbose) 25091db9f3b2SDimitry Andric outs() << " ...\n"; 25101db9f3b2SDimitry Andric 25111db9f3b2SDimitry Andric // Cyclic dependencies are disallowed by Clang, but we still 25121db9f3b2SDimitry Andric // shouldn't run into an infinite loop, so mark it as processed now. 25131db9f3b2SDimitry Andric ClangModules.insert({PCMFile, getDwoId(CUDie)}); 25141db9f3b2SDimitry Andric 25151db9f3b2SDimitry Andric if (Error E = loadClangModule(Loader, CUDie, PCMFile, Context, OnCUDieLoaded, 25161db9f3b2SDimitry Andric Indent + 2)) { 25171db9f3b2SDimitry Andric consumeError(std::move(E)); 25181db9f3b2SDimitry Andric return false; 25191db9f3b2SDimitry Andric } 25201db9f3b2SDimitry Andric return true; 25211db9f3b2SDimitry Andric } 25221db9f3b2SDimitry Andric 25231db9f3b2SDimitry Andric Error DWARFLinker::loadClangModule( 25241db9f3b2SDimitry Andric ObjFileLoaderTy Loader, const DWARFDie &CUDie, const std::string &PCMFile, 25251db9f3b2SDimitry Andric LinkContext &Context, CompileUnitHandlerTy OnCUDieLoaded, unsigned Indent) { 25261db9f3b2SDimitry Andric 25271db9f3b2SDimitry Andric uint64_t DwoId = getDwoId(CUDie); 25281db9f3b2SDimitry Andric std::string ModuleName = dwarf::toString(CUDie.find(dwarf::DW_AT_name), ""); 25291db9f3b2SDimitry Andric 25301db9f3b2SDimitry Andric /// Using a SmallString<0> because loadClangModule() is recursive. 25311db9f3b2SDimitry Andric SmallString<0> Path(Options.PrependPath); 25321db9f3b2SDimitry Andric if (sys::path::is_relative(PCMFile)) 25331db9f3b2SDimitry Andric resolveRelativeObjectPath(Path, CUDie); 25341db9f3b2SDimitry Andric sys::path::append(Path, PCMFile); 25351db9f3b2SDimitry Andric // Don't use the cached binary holder because we have no thread-safety 25361db9f3b2SDimitry Andric // guarantee and the lifetime is limited. 25371db9f3b2SDimitry Andric 25381db9f3b2SDimitry Andric if (Loader == nullptr) { 25391db9f3b2SDimitry Andric reportError("Could not load clang module: loader is not specified.\n", 25401db9f3b2SDimitry Andric Context.File); 25411db9f3b2SDimitry Andric return Error::success(); 25421db9f3b2SDimitry Andric } 25431db9f3b2SDimitry Andric 25441db9f3b2SDimitry Andric auto ErrOrObj = Loader(Context.File.FileName, Path); 25451db9f3b2SDimitry Andric if (!ErrOrObj) 25461db9f3b2SDimitry Andric return Error::success(); 25471db9f3b2SDimitry Andric 25481db9f3b2SDimitry Andric std::unique_ptr<CompileUnit> Unit; 25491db9f3b2SDimitry Andric for (const auto &CU : ErrOrObj->Dwarf->compile_units()) { 25501db9f3b2SDimitry Andric OnCUDieLoaded(*CU); 25511db9f3b2SDimitry Andric // Recursively get all modules imported by this one. 25521db9f3b2SDimitry Andric auto ChildCUDie = CU->getUnitDIE(); 25531db9f3b2SDimitry Andric if (!ChildCUDie) 25541db9f3b2SDimitry Andric continue; 25551db9f3b2SDimitry Andric if (!registerModuleReference(ChildCUDie, Context, Loader, OnCUDieLoaded, 25561db9f3b2SDimitry Andric Indent)) { 25571db9f3b2SDimitry Andric if (Unit) { 25581db9f3b2SDimitry Andric std::string Err = 25591db9f3b2SDimitry Andric (PCMFile + 25601db9f3b2SDimitry Andric ": Clang modules are expected to have exactly 1 compile unit.\n"); 25611db9f3b2SDimitry Andric reportError(Err, Context.File); 25621db9f3b2SDimitry Andric return make_error<StringError>(Err, inconvertibleErrorCode()); 25631db9f3b2SDimitry Andric } 25641db9f3b2SDimitry Andric // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is 25651db9f3b2SDimitry Andric // fixed in clang, only warn about DWO_id mismatches in verbose mode. 25661db9f3b2SDimitry Andric // ASTFileSignatures will change randomly when a module is rebuilt. 25671db9f3b2SDimitry Andric uint64_t PCMDwoId = getDwoId(ChildCUDie); 25681db9f3b2SDimitry Andric if (PCMDwoId != DwoId) { 25691db9f3b2SDimitry Andric if (Options.Verbose) 25701db9f3b2SDimitry Andric reportWarning( 25711db9f3b2SDimitry Andric Twine("hash mismatch: this object file was built against a " 25721db9f3b2SDimitry Andric "different version of the module ") + 25731db9f3b2SDimitry Andric PCMFile, 25741db9f3b2SDimitry Andric Context.File); 25751db9f3b2SDimitry Andric // Update the cache entry with the DwoId of the module loaded from disk. 25761db9f3b2SDimitry Andric ClangModules[PCMFile] = PCMDwoId; 25771db9f3b2SDimitry Andric } 25781db9f3b2SDimitry Andric 25791db9f3b2SDimitry Andric // Add this module. 25801db9f3b2SDimitry Andric Unit = std::make_unique<CompileUnit>(*CU, UniqueUnitID++, !Options.NoODR, 25811db9f3b2SDimitry Andric ModuleName); 25821db9f3b2SDimitry Andric } 25831db9f3b2SDimitry Andric } 25841db9f3b2SDimitry Andric 25851db9f3b2SDimitry Andric if (Unit) 25861db9f3b2SDimitry Andric Context.ModuleUnits.emplace_back(RefModuleUnit{*ErrOrObj, std::move(Unit)}); 25871db9f3b2SDimitry Andric 25881db9f3b2SDimitry Andric return Error::success(); 25891db9f3b2SDimitry Andric } 25901db9f3b2SDimitry Andric 25911db9f3b2SDimitry Andric uint64_t DWARFLinker::DIECloner::cloneAllCompileUnits( 25921db9f3b2SDimitry Andric DWARFContext &DwarfContext, const DWARFFile &File, bool IsLittleEndian) { 25931db9f3b2SDimitry Andric uint64_t OutputDebugInfoSize = 25941db9f3b2SDimitry Andric (Emitter == nullptr) ? 0 : Emitter->getDebugInfoSectionSize(); 25951db9f3b2SDimitry Andric const uint64_t StartOutputDebugInfoSize = OutputDebugInfoSize; 25961db9f3b2SDimitry Andric 25971db9f3b2SDimitry Andric for (auto &CurrentUnit : CompileUnits) { 25981db9f3b2SDimitry Andric const uint16_t DwarfVersion = CurrentUnit->getOrigUnit().getVersion(); 25991db9f3b2SDimitry Andric const uint32_t UnitHeaderSize = DwarfVersion >= 5 ? 12 : 11; 26001db9f3b2SDimitry Andric auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE(); 26011db9f3b2SDimitry Andric CurrentUnit->setStartOffset(OutputDebugInfoSize); 26021db9f3b2SDimitry Andric if (!InputDIE) { 26031db9f3b2SDimitry Andric OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset(DwarfVersion); 26041db9f3b2SDimitry Andric continue; 26051db9f3b2SDimitry Andric } 26061db9f3b2SDimitry Andric if (CurrentUnit->getInfo(0).Keep) { 26071db9f3b2SDimitry Andric // Clone the InputDIE into your Unit DIE in our compile unit since it 26081db9f3b2SDimitry Andric // already has a DIE inside of it. 26091db9f3b2SDimitry Andric CurrentUnit->createOutputDIE(); 26101db9f3b2SDimitry Andric rememberUnitForMacroOffset(*CurrentUnit); 26111db9f3b2SDimitry Andric cloneDIE(InputDIE, File, *CurrentUnit, 0 /* PC offset */, UnitHeaderSize, 26121db9f3b2SDimitry Andric 0, IsLittleEndian, CurrentUnit->getOutputUnitDIE()); 26131db9f3b2SDimitry Andric } 26141db9f3b2SDimitry Andric 26151db9f3b2SDimitry Andric OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset(DwarfVersion); 26161db9f3b2SDimitry Andric 26171db9f3b2SDimitry Andric if (Emitter != nullptr) { 26181db9f3b2SDimitry Andric 26191db9f3b2SDimitry Andric generateLineTableForUnit(*CurrentUnit); 26201db9f3b2SDimitry Andric 26211db9f3b2SDimitry Andric Linker.emitAcceleratorEntriesForUnit(*CurrentUnit); 26221db9f3b2SDimitry Andric 26231db9f3b2SDimitry Andric if (LLVM_UNLIKELY(Linker.Options.Update)) 26241db9f3b2SDimitry Andric continue; 26251db9f3b2SDimitry Andric 26261db9f3b2SDimitry Andric Linker.generateUnitRanges(*CurrentUnit, File, AddrPool); 26271db9f3b2SDimitry Andric 26281db9f3b2SDimitry Andric auto ProcessExpr = [&](SmallVectorImpl<uint8_t> &SrcBytes, 26291db9f3b2SDimitry Andric SmallVectorImpl<uint8_t> &OutBytes, 26301db9f3b2SDimitry Andric int64_t RelocAdjustment) { 26311db9f3b2SDimitry Andric DWARFUnit &OrigUnit = CurrentUnit->getOrigUnit(); 26321db9f3b2SDimitry Andric DataExtractor Data(SrcBytes, IsLittleEndian, 26331db9f3b2SDimitry Andric OrigUnit.getAddressByteSize()); 26341db9f3b2SDimitry Andric cloneExpression(Data, 26351db9f3b2SDimitry Andric DWARFExpression(Data, OrigUnit.getAddressByteSize(), 26361db9f3b2SDimitry Andric OrigUnit.getFormParams().Format), 26371db9f3b2SDimitry Andric File, *CurrentUnit, OutBytes, RelocAdjustment, 26381db9f3b2SDimitry Andric IsLittleEndian); 26391db9f3b2SDimitry Andric }; 26401db9f3b2SDimitry Andric generateUnitLocations(*CurrentUnit, File, ProcessExpr); 26411db9f3b2SDimitry Andric emitDebugAddrSection(*CurrentUnit, DwarfVersion); 26421db9f3b2SDimitry Andric } 26431db9f3b2SDimitry Andric AddrPool.clear(); 26441db9f3b2SDimitry Andric } 26451db9f3b2SDimitry Andric 26461db9f3b2SDimitry Andric if (Emitter != nullptr) { 26471db9f3b2SDimitry Andric assert(Emitter); 26481db9f3b2SDimitry Andric // Emit macro tables. 26491db9f3b2SDimitry Andric Emitter->emitMacroTables(File.Dwarf.get(), UnitMacroMap, DebugStrPool); 26501db9f3b2SDimitry Andric 26511db9f3b2SDimitry Andric // Emit all the compile unit's debug information. 26521db9f3b2SDimitry Andric for (auto &CurrentUnit : CompileUnits) { 26531db9f3b2SDimitry Andric CurrentUnit->fixupForwardReferences(); 26541db9f3b2SDimitry Andric 26551db9f3b2SDimitry Andric if (!CurrentUnit->getOutputUnitDIE()) 26561db9f3b2SDimitry Andric continue; 26571db9f3b2SDimitry Andric 26581db9f3b2SDimitry Andric unsigned DwarfVersion = CurrentUnit->getOrigUnit().getVersion(); 26591db9f3b2SDimitry Andric 26601db9f3b2SDimitry Andric assert(Emitter->getDebugInfoSectionSize() == 26611db9f3b2SDimitry Andric CurrentUnit->getStartOffset()); 26621db9f3b2SDimitry Andric Emitter->emitCompileUnitHeader(*CurrentUnit, DwarfVersion); 26631db9f3b2SDimitry Andric Emitter->emitDIE(*CurrentUnit->getOutputUnitDIE()); 26641db9f3b2SDimitry Andric assert(Emitter->getDebugInfoSectionSize() == 26651db9f3b2SDimitry Andric CurrentUnit->computeNextUnitOffset(DwarfVersion)); 26661db9f3b2SDimitry Andric } 26671db9f3b2SDimitry Andric } 26681db9f3b2SDimitry Andric 26691db9f3b2SDimitry Andric return OutputDebugInfoSize - StartOutputDebugInfoSize; 26701db9f3b2SDimitry Andric } 26711db9f3b2SDimitry Andric 26721db9f3b2SDimitry Andric void DWARFLinker::copyInvariantDebugSection(DWARFContext &Dwarf) { 26731db9f3b2SDimitry Andric TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getLocSection().Data, 26747a6dacacSDimitry Andric DebugSectionKind::DebugLoc); 26751db9f3b2SDimitry Andric TheDwarfEmitter->emitSectionContents( 26767a6dacacSDimitry Andric Dwarf.getDWARFObj().getRangesSection().Data, 26777a6dacacSDimitry Andric DebugSectionKind::DebugRange); 26781db9f3b2SDimitry Andric TheDwarfEmitter->emitSectionContents( 26797a6dacacSDimitry Andric Dwarf.getDWARFObj().getFrameSection().Data, DebugSectionKind::DebugFrame); 26801db9f3b2SDimitry Andric TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getArangesSection(), 26817a6dacacSDimitry Andric DebugSectionKind::DebugARanges); 26821db9f3b2SDimitry Andric TheDwarfEmitter->emitSectionContents( 26837a6dacacSDimitry Andric Dwarf.getDWARFObj().getAddrSection().Data, DebugSectionKind::DebugAddr); 26841db9f3b2SDimitry Andric TheDwarfEmitter->emitSectionContents( 26857a6dacacSDimitry Andric Dwarf.getDWARFObj().getRnglistsSection().Data, 26867a6dacacSDimitry Andric DebugSectionKind::DebugRngLists); 26871db9f3b2SDimitry Andric TheDwarfEmitter->emitSectionContents( 26887a6dacacSDimitry Andric Dwarf.getDWARFObj().getLoclistsSection().Data, 26897a6dacacSDimitry Andric DebugSectionKind::DebugLocLists); 26901db9f3b2SDimitry Andric } 26911db9f3b2SDimitry Andric 26921db9f3b2SDimitry Andric void DWARFLinker::addObjectFile(DWARFFile &File, ObjFileLoaderTy Loader, 26931db9f3b2SDimitry Andric CompileUnitHandlerTy OnCUDieLoaded) { 26941db9f3b2SDimitry Andric ObjectContexts.emplace_back(LinkContext(File)); 26951db9f3b2SDimitry Andric 26961db9f3b2SDimitry Andric if (ObjectContexts.back().File.Dwarf) { 26971db9f3b2SDimitry Andric for (const std::unique_ptr<DWARFUnit> &CU : 26981db9f3b2SDimitry Andric ObjectContexts.back().File.Dwarf->compile_units()) { 26991db9f3b2SDimitry Andric DWARFDie CUDie = CU->getUnitDIE(); 27001db9f3b2SDimitry Andric 27011db9f3b2SDimitry Andric if (!CUDie) 27021db9f3b2SDimitry Andric continue; 27031db9f3b2SDimitry Andric 27041db9f3b2SDimitry Andric OnCUDieLoaded(*CU); 27051db9f3b2SDimitry Andric 27061db9f3b2SDimitry Andric if (!LLVM_UNLIKELY(Options.Update)) 27071db9f3b2SDimitry Andric registerModuleReference(CUDie, ObjectContexts.back(), Loader, 27081db9f3b2SDimitry Andric OnCUDieLoaded); 27091db9f3b2SDimitry Andric } 27101db9f3b2SDimitry Andric } 27111db9f3b2SDimitry Andric } 27121db9f3b2SDimitry Andric 27131db9f3b2SDimitry Andric Error DWARFLinker::link() { 27141db9f3b2SDimitry Andric assert((Options.TargetDWARFVersion != 0) && 27151db9f3b2SDimitry Andric "TargetDWARFVersion should be set"); 27161db9f3b2SDimitry Andric 27171db9f3b2SDimitry Andric // First populate the data structure we need for each iteration of the 27181db9f3b2SDimitry Andric // parallel loop. 27191db9f3b2SDimitry Andric unsigned NumObjects = ObjectContexts.size(); 27201db9f3b2SDimitry Andric 27211db9f3b2SDimitry Andric // This Dwarf string pool which is used for emission. It must be used 27221db9f3b2SDimitry Andric // serially as the order of calling getStringOffset matters for 27231db9f3b2SDimitry Andric // reproducibility. 2724*0fca6ea1SDimitry Andric OffsetsStringPool DebugStrPool(true); 2725*0fca6ea1SDimitry Andric OffsetsStringPool DebugLineStrPool(false); 27261db9f3b2SDimitry Andric DebugDieValuePool StringOffsetPool; 27271db9f3b2SDimitry Andric 27281db9f3b2SDimitry Andric // ODR Contexts for the optimize. 27291db9f3b2SDimitry Andric DeclContextTree ODRContexts; 27301db9f3b2SDimitry Andric 27311db9f3b2SDimitry Andric for (LinkContext &OptContext : ObjectContexts) { 27321db9f3b2SDimitry Andric if (Options.Verbose) 27331db9f3b2SDimitry Andric outs() << "DEBUG MAP OBJECT: " << OptContext.File.FileName << "\n"; 27341db9f3b2SDimitry Andric 27351db9f3b2SDimitry Andric if (!OptContext.File.Dwarf) 27361db9f3b2SDimitry Andric continue; 27371db9f3b2SDimitry Andric 27381db9f3b2SDimitry Andric if (Options.VerifyInputDWARF) 27391db9f3b2SDimitry Andric verifyInput(OptContext.File); 27401db9f3b2SDimitry Andric 27411db9f3b2SDimitry Andric // Look for relocations that correspond to address map entries. 27421db9f3b2SDimitry Andric 27431db9f3b2SDimitry Andric // there was findvalidrelocations previously ... probably we need to gather 27441db9f3b2SDimitry Andric // info here 27451db9f3b2SDimitry Andric if (LLVM_LIKELY(!Options.Update) && 27461db9f3b2SDimitry Andric !OptContext.File.Addresses->hasValidRelocs()) { 27471db9f3b2SDimitry Andric if (Options.Verbose) 27481db9f3b2SDimitry Andric outs() << "No valid relocations found. Skipping.\n"; 27491db9f3b2SDimitry Andric 27501db9f3b2SDimitry Andric // Set "Skip" flag as a signal to other loops that we should not 27511db9f3b2SDimitry Andric // process this iteration. 27521db9f3b2SDimitry Andric OptContext.Skip = true; 27531db9f3b2SDimitry Andric continue; 27541db9f3b2SDimitry Andric } 27551db9f3b2SDimitry Andric 27561db9f3b2SDimitry Andric // Setup access to the debug info. 27571db9f3b2SDimitry Andric if (!OptContext.File.Dwarf) 27581db9f3b2SDimitry Andric continue; 27591db9f3b2SDimitry Andric 27601db9f3b2SDimitry Andric // Check whether type units are presented. 27611db9f3b2SDimitry Andric if (!OptContext.File.Dwarf->types_section_units().empty()) { 27621db9f3b2SDimitry Andric reportWarning("type units are not currently supported: file will " 27631db9f3b2SDimitry Andric "be skipped", 27641db9f3b2SDimitry Andric OptContext.File); 27651db9f3b2SDimitry Andric OptContext.Skip = true; 27661db9f3b2SDimitry Andric continue; 27671db9f3b2SDimitry Andric } 27681db9f3b2SDimitry Andric 27691db9f3b2SDimitry Andric // Clone all the clang modules with requires extracting the DIE units. We 27701db9f3b2SDimitry Andric // don't need the full debug info until the Analyze phase. 27711db9f3b2SDimitry Andric OptContext.CompileUnits.reserve( 27721db9f3b2SDimitry Andric OptContext.File.Dwarf->getNumCompileUnits()); 27731db9f3b2SDimitry Andric for (const auto &CU : OptContext.File.Dwarf->compile_units()) { 27741db9f3b2SDimitry Andric auto CUDie = CU->getUnitDIE(/*ExtractUnitDIEOnly=*/true); 27751db9f3b2SDimitry Andric if (Options.Verbose) { 27761db9f3b2SDimitry Andric outs() << "Input compilation unit:"; 27771db9f3b2SDimitry Andric DIDumpOptions DumpOpts; 27781db9f3b2SDimitry Andric DumpOpts.ChildRecurseDepth = 0; 27791db9f3b2SDimitry Andric DumpOpts.Verbose = Options.Verbose; 27801db9f3b2SDimitry Andric CUDie.dump(outs(), 0, DumpOpts); 27811db9f3b2SDimitry Andric } 27821db9f3b2SDimitry Andric } 27831db9f3b2SDimitry Andric 27841db9f3b2SDimitry Andric for (auto &CU : OptContext.ModuleUnits) { 27851db9f3b2SDimitry Andric if (Error Err = cloneModuleUnit(OptContext, CU, ODRContexts, DebugStrPool, 27861db9f3b2SDimitry Andric DebugLineStrPool, StringOffsetPool)) 27871db9f3b2SDimitry Andric reportWarning(toString(std::move(Err)), CU.File); 27881db9f3b2SDimitry Andric } 27891db9f3b2SDimitry Andric } 27901db9f3b2SDimitry Andric 27911db9f3b2SDimitry Andric // At this point we know how much data we have emitted. We use this value to 27921db9f3b2SDimitry Andric // compare canonical DIE offsets in analyzeContextInfo to see if a definition 27931db9f3b2SDimitry Andric // is already emitted, without being affected by canonical die offsets set 27941db9f3b2SDimitry Andric // later. This prevents undeterminism when analyze and clone execute 27951db9f3b2SDimitry Andric // concurrently, as clone set the canonical DIE offset and analyze reads it. 27961db9f3b2SDimitry Andric const uint64_t ModulesEndOffset = 27971db9f3b2SDimitry Andric (TheDwarfEmitter == nullptr) ? 0 27981db9f3b2SDimitry Andric : TheDwarfEmitter->getDebugInfoSectionSize(); 27991db9f3b2SDimitry Andric 28001db9f3b2SDimitry Andric // These variables manage the list of processed object files. 28011db9f3b2SDimitry Andric // The mutex and condition variable are to ensure that this is thread safe. 28021db9f3b2SDimitry Andric std::mutex ProcessedFilesMutex; 28031db9f3b2SDimitry Andric std::condition_variable ProcessedFilesConditionVariable; 28041db9f3b2SDimitry Andric BitVector ProcessedFiles(NumObjects, false); 28051db9f3b2SDimitry Andric 28061db9f3b2SDimitry Andric // Analyzing the context info is particularly expensive so it is executed in 28071db9f3b2SDimitry Andric // parallel with emitting the previous compile unit. 28081db9f3b2SDimitry Andric auto AnalyzeLambda = [&](size_t I) { 28091db9f3b2SDimitry Andric auto &Context = ObjectContexts[I]; 28101db9f3b2SDimitry Andric 28111db9f3b2SDimitry Andric if (Context.Skip || !Context.File.Dwarf) 28121db9f3b2SDimitry Andric return; 28131db9f3b2SDimitry Andric 28141db9f3b2SDimitry Andric for (const auto &CU : Context.File.Dwarf->compile_units()) { 28151db9f3b2SDimitry Andric // Previously we only extracted the unit DIEs. We need the full debug info 28161db9f3b2SDimitry Andric // now. 28171db9f3b2SDimitry Andric auto CUDie = CU->getUnitDIE(/*ExtractUnitDIEOnly=*/false); 28181db9f3b2SDimitry Andric std::string PCMFile = getPCMFile(CUDie, Options.ObjectPrefixMap); 28191db9f3b2SDimitry Andric 28201db9f3b2SDimitry Andric if (!CUDie || LLVM_UNLIKELY(Options.Update) || 28211db9f3b2SDimitry Andric !isClangModuleRef(CUDie, PCMFile, Context, 0, true).first) { 28221db9f3b2SDimitry Andric Context.CompileUnits.push_back(std::make_unique<CompileUnit>( 28231db9f3b2SDimitry Andric *CU, UniqueUnitID++, !Options.NoODR && !Options.Update, "")); 28241db9f3b2SDimitry Andric } 28251db9f3b2SDimitry Andric } 28261db9f3b2SDimitry Andric 28271db9f3b2SDimitry Andric // Now build the DIE parent links that we will use during the next phase. 28281db9f3b2SDimitry Andric for (auto &CurrentUnit : Context.CompileUnits) { 28291db9f3b2SDimitry Andric auto CUDie = CurrentUnit->getOrigUnit().getUnitDIE(); 28301db9f3b2SDimitry Andric if (!CUDie) 28311db9f3b2SDimitry Andric continue; 28321db9f3b2SDimitry Andric analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0, 28331db9f3b2SDimitry Andric *CurrentUnit, &ODRContexts.getRoot(), ODRContexts, 28341db9f3b2SDimitry Andric ModulesEndOffset, Options.ParseableSwiftInterfaces, 28351db9f3b2SDimitry Andric [&](const Twine &Warning, const DWARFDie &DIE) { 28361db9f3b2SDimitry Andric reportWarning(Warning, Context.File, &DIE); 28371db9f3b2SDimitry Andric }); 28381db9f3b2SDimitry Andric } 28391db9f3b2SDimitry Andric }; 28401db9f3b2SDimitry Andric 28411db9f3b2SDimitry Andric // For each object file map how many bytes were emitted. 28421db9f3b2SDimitry Andric StringMap<DebugInfoSize> SizeByObject; 28431db9f3b2SDimitry Andric 28441db9f3b2SDimitry Andric // And then the remaining work in serial again. 28451db9f3b2SDimitry Andric // Note, although this loop runs in serial, it can run in parallel with 28461db9f3b2SDimitry Andric // the analyzeContextInfo loop so long as we process files with indices >= 28471db9f3b2SDimitry Andric // than those processed by analyzeContextInfo. 28481db9f3b2SDimitry Andric auto CloneLambda = [&](size_t I) { 28491db9f3b2SDimitry Andric auto &OptContext = ObjectContexts[I]; 28501db9f3b2SDimitry Andric if (OptContext.Skip || !OptContext.File.Dwarf) 28511db9f3b2SDimitry Andric return; 28521db9f3b2SDimitry Andric 28531db9f3b2SDimitry Andric // Then mark all the DIEs that need to be present in the generated output 28541db9f3b2SDimitry Andric // and collect some information about them. 28551db9f3b2SDimitry Andric // Note that this loop can not be merged with the previous one because 28561db9f3b2SDimitry Andric // cross-cu references require the ParentIdx to be setup for every CU in 28571db9f3b2SDimitry Andric // the object file before calling this. 28581db9f3b2SDimitry Andric if (LLVM_UNLIKELY(Options.Update)) { 28591db9f3b2SDimitry Andric for (auto &CurrentUnit : OptContext.CompileUnits) 28601db9f3b2SDimitry Andric CurrentUnit->markEverythingAsKept(); 28611db9f3b2SDimitry Andric copyInvariantDebugSection(*OptContext.File.Dwarf); 28621db9f3b2SDimitry Andric } else { 28631db9f3b2SDimitry Andric for (auto &CurrentUnit : OptContext.CompileUnits) { 28641db9f3b2SDimitry Andric lookForDIEsToKeep(*OptContext.File.Addresses, OptContext.CompileUnits, 28651db9f3b2SDimitry Andric CurrentUnit->getOrigUnit().getUnitDIE(), 28661db9f3b2SDimitry Andric OptContext.File, *CurrentUnit, 0); 28671db9f3b2SDimitry Andric #ifndef NDEBUG 28681db9f3b2SDimitry Andric verifyKeepChain(*CurrentUnit); 28691db9f3b2SDimitry Andric #endif 28701db9f3b2SDimitry Andric } 28711db9f3b2SDimitry Andric } 28721db9f3b2SDimitry Andric 28731db9f3b2SDimitry Andric // The calls to applyValidRelocs inside cloneDIE will walk the reloc 28741db9f3b2SDimitry Andric // array again (in the same way findValidRelocsInDebugInfo() did). We 28751db9f3b2SDimitry Andric // need to reset the NextValidReloc index to the beginning. 28761db9f3b2SDimitry Andric if (OptContext.File.Addresses->hasValidRelocs() || 28771db9f3b2SDimitry Andric LLVM_UNLIKELY(Options.Update)) { 28781db9f3b2SDimitry Andric SizeByObject[OptContext.File.FileName].Input = 28791db9f3b2SDimitry Andric getDebugInfoSize(*OptContext.File.Dwarf); 28801db9f3b2SDimitry Andric SizeByObject[OptContext.File.FileName].Output = 28817a6dacacSDimitry Andric DIECloner(*this, TheDwarfEmitter, OptContext.File, DIEAlloc, 28821db9f3b2SDimitry Andric OptContext.CompileUnits, Options.Update, DebugStrPool, 28831db9f3b2SDimitry Andric DebugLineStrPool, StringOffsetPool) 28841db9f3b2SDimitry Andric .cloneAllCompileUnits(*OptContext.File.Dwarf, OptContext.File, 28851db9f3b2SDimitry Andric OptContext.File.Dwarf->isLittleEndian()); 28861db9f3b2SDimitry Andric } 28871db9f3b2SDimitry Andric if ((TheDwarfEmitter != nullptr) && !OptContext.CompileUnits.empty() && 28881db9f3b2SDimitry Andric LLVM_LIKELY(!Options.Update)) 28891db9f3b2SDimitry Andric patchFrameInfoForObject(OptContext); 28901db9f3b2SDimitry Andric 28911db9f3b2SDimitry Andric // Clean-up before starting working on the next object. 28921db9f3b2SDimitry Andric cleanupAuxiliarryData(OptContext); 28931db9f3b2SDimitry Andric }; 28941db9f3b2SDimitry Andric 28951db9f3b2SDimitry Andric auto EmitLambda = [&]() { 28961db9f3b2SDimitry Andric // Emit everything that's global. 28971db9f3b2SDimitry Andric if (TheDwarfEmitter != nullptr) { 28981db9f3b2SDimitry Andric TheDwarfEmitter->emitAbbrevs(Abbreviations, Options.TargetDWARFVersion); 28991db9f3b2SDimitry Andric TheDwarfEmitter->emitStrings(DebugStrPool); 29007a6dacacSDimitry Andric TheDwarfEmitter->emitStringOffsets(StringOffsetPool.getValues(), 29011db9f3b2SDimitry Andric Options.TargetDWARFVersion); 29021db9f3b2SDimitry Andric TheDwarfEmitter->emitLineStrings(DebugLineStrPool); 29031db9f3b2SDimitry Andric for (AccelTableKind TableKind : Options.AccelTables) { 29041db9f3b2SDimitry Andric switch (TableKind) { 29051db9f3b2SDimitry Andric case AccelTableKind::Apple: 29061db9f3b2SDimitry Andric TheDwarfEmitter->emitAppleNamespaces(AppleNamespaces); 29071db9f3b2SDimitry Andric TheDwarfEmitter->emitAppleNames(AppleNames); 29081db9f3b2SDimitry Andric TheDwarfEmitter->emitAppleTypes(AppleTypes); 29091db9f3b2SDimitry Andric TheDwarfEmitter->emitAppleObjc(AppleObjc); 29101db9f3b2SDimitry Andric break; 29111db9f3b2SDimitry Andric case AccelTableKind::Pub: 29121db9f3b2SDimitry Andric // Already emitted by emitAcceleratorEntriesForUnit. 29131db9f3b2SDimitry Andric // Already emitted by emitAcceleratorEntriesForUnit. 29141db9f3b2SDimitry Andric break; 29151db9f3b2SDimitry Andric case AccelTableKind::DebugNames: 29161db9f3b2SDimitry Andric TheDwarfEmitter->emitDebugNames(DebugNames); 29171db9f3b2SDimitry Andric break; 29181db9f3b2SDimitry Andric } 29191db9f3b2SDimitry Andric } 29201db9f3b2SDimitry Andric } 29211db9f3b2SDimitry Andric }; 29221db9f3b2SDimitry Andric 29231db9f3b2SDimitry Andric auto AnalyzeAll = [&]() { 29241db9f3b2SDimitry Andric for (unsigned I = 0, E = NumObjects; I != E; ++I) { 29251db9f3b2SDimitry Andric AnalyzeLambda(I); 29261db9f3b2SDimitry Andric 29271db9f3b2SDimitry Andric std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex); 29281db9f3b2SDimitry Andric ProcessedFiles.set(I); 29291db9f3b2SDimitry Andric ProcessedFilesConditionVariable.notify_one(); 29301db9f3b2SDimitry Andric } 29311db9f3b2SDimitry Andric }; 29321db9f3b2SDimitry Andric 29331db9f3b2SDimitry Andric auto CloneAll = [&]() { 29341db9f3b2SDimitry Andric for (unsigned I = 0, E = NumObjects; I != E; ++I) { 29351db9f3b2SDimitry Andric { 29361db9f3b2SDimitry Andric std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex); 29371db9f3b2SDimitry Andric if (!ProcessedFiles[I]) { 29381db9f3b2SDimitry Andric ProcessedFilesConditionVariable.wait( 29391db9f3b2SDimitry Andric LockGuard, [&]() { return ProcessedFiles[I]; }); 29401db9f3b2SDimitry Andric } 29411db9f3b2SDimitry Andric } 29421db9f3b2SDimitry Andric 29431db9f3b2SDimitry Andric CloneLambda(I); 29441db9f3b2SDimitry Andric } 29451db9f3b2SDimitry Andric EmitLambda(); 29461db9f3b2SDimitry Andric }; 29471db9f3b2SDimitry Andric 29481db9f3b2SDimitry Andric // To limit memory usage in the single threaded case, analyze and clone are 29491db9f3b2SDimitry Andric // run sequentially so the OptContext is freed after processing each object 29501db9f3b2SDimitry Andric // in endDebugObject. 29511db9f3b2SDimitry Andric if (Options.Threads == 1) { 29521db9f3b2SDimitry Andric for (unsigned I = 0, E = NumObjects; I != E; ++I) { 29531db9f3b2SDimitry Andric AnalyzeLambda(I); 29541db9f3b2SDimitry Andric CloneLambda(I); 29551db9f3b2SDimitry Andric } 29561db9f3b2SDimitry Andric EmitLambda(); 29571db9f3b2SDimitry Andric } else { 2958*0fca6ea1SDimitry Andric DefaultThreadPool Pool(hardware_concurrency(2)); 29591db9f3b2SDimitry Andric Pool.async(AnalyzeAll); 29601db9f3b2SDimitry Andric Pool.async(CloneAll); 29611db9f3b2SDimitry Andric Pool.wait(); 29621db9f3b2SDimitry Andric } 29631db9f3b2SDimitry Andric 29641db9f3b2SDimitry Andric if (Options.Statistics) { 29651db9f3b2SDimitry Andric // Create a vector sorted in descending order by output size. 29661db9f3b2SDimitry Andric std::vector<std::pair<StringRef, DebugInfoSize>> Sorted; 29671db9f3b2SDimitry Andric for (auto &E : SizeByObject) 29681db9f3b2SDimitry Andric Sorted.emplace_back(E.first(), E.second); 29691db9f3b2SDimitry Andric llvm::sort(Sorted, [](auto &LHS, auto &RHS) { 29701db9f3b2SDimitry Andric return LHS.second.Output > RHS.second.Output; 29711db9f3b2SDimitry Andric }); 29721db9f3b2SDimitry Andric 29731db9f3b2SDimitry Andric auto ComputePercentange = [](int64_t Input, int64_t Output) -> float { 29741db9f3b2SDimitry Andric const float Difference = Output - Input; 29751db9f3b2SDimitry Andric const float Sum = Input + Output; 29761db9f3b2SDimitry Andric if (Sum == 0) 29771db9f3b2SDimitry Andric return 0; 29781db9f3b2SDimitry Andric return (Difference / (Sum / 2)); 29791db9f3b2SDimitry Andric }; 29801db9f3b2SDimitry Andric 29811db9f3b2SDimitry Andric int64_t InputTotal = 0; 29821db9f3b2SDimitry Andric int64_t OutputTotal = 0; 29831db9f3b2SDimitry Andric const char *FormatStr = "{0,-45} {1,10}b {2,10}b {3,8:P}\n"; 29841db9f3b2SDimitry Andric 29851db9f3b2SDimitry Andric // Print header. 29861db9f3b2SDimitry Andric outs() << ".debug_info section size (in bytes)\n"; 29871db9f3b2SDimitry Andric outs() << "----------------------------------------------------------------" 29881db9f3b2SDimitry Andric "---------------\n"; 29891db9f3b2SDimitry Andric outs() << "Filename Object " 29901db9f3b2SDimitry Andric " dSYM Change\n"; 29911db9f3b2SDimitry Andric outs() << "----------------------------------------------------------------" 29921db9f3b2SDimitry Andric "---------------\n"; 29931db9f3b2SDimitry Andric 29941db9f3b2SDimitry Andric // Print body. 29951db9f3b2SDimitry Andric for (auto &E : Sorted) { 29961db9f3b2SDimitry Andric InputTotal += E.second.Input; 29971db9f3b2SDimitry Andric OutputTotal += E.second.Output; 29981db9f3b2SDimitry Andric llvm::outs() << formatv( 29991db9f3b2SDimitry Andric FormatStr, sys::path::filename(E.first).take_back(45), E.second.Input, 30001db9f3b2SDimitry Andric E.second.Output, ComputePercentange(E.second.Input, E.second.Output)); 30011db9f3b2SDimitry Andric } 30021db9f3b2SDimitry Andric // Print total and footer. 30031db9f3b2SDimitry Andric outs() << "----------------------------------------------------------------" 30041db9f3b2SDimitry Andric "---------------\n"; 30051db9f3b2SDimitry Andric llvm::outs() << formatv(FormatStr, "Total", InputTotal, OutputTotal, 30061db9f3b2SDimitry Andric ComputePercentange(InputTotal, OutputTotal)); 30071db9f3b2SDimitry Andric outs() << "----------------------------------------------------------------" 30081db9f3b2SDimitry Andric "---------------\n\n"; 30091db9f3b2SDimitry Andric } 30101db9f3b2SDimitry Andric 30111db9f3b2SDimitry Andric return Error::success(); 30121db9f3b2SDimitry Andric } 30131db9f3b2SDimitry Andric 30141db9f3b2SDimitry Andric Error DWARFLinker::cloneModuleUnit(LinkContext &Context, RefModuleUnit &Unit, 30151db9f3b2SDimitry Andric DeclContextTree &ODRContexts, 30161db9f3b2SDimitry Andric OffsetsStringPool &DebugStrPool, 30171db9f3b2SDimitry Andric OffsetsStringPool &DebugLineStrPool, 30181db9f3b2SDimitry Andric DebugDieValuePool &StringOffsetPool, 30191db9f3b2SDimitry Andric unsigned Indent) { 30201db9f3b2SDimitry Andric assert(Unit.Unit.get() != nullptr); 30211db9f3b2SDimitry Andric 30221db9f3b2SDimitry Andric if (!Unit.Unit->getOrigUnit().getUnitDIE().hasChildren()) 30231db9f3b2SDimitry Andric return Error::success(); 30241db9f3b2SDimitry Andric 30251db9f3b2SDimitry Andric if (Options.Verbose) { 30261db9f3b2SDimitry Andric outs().indent(Indent); 30271db9f3b2SDimitry Andric outs() << "cloning .debug_info from " << Unit.File.FileName << "\n"; 30281db9f3b2SDimitry Andric } 30291db9f3b2SDimitry Andric 30301db9f3b2SDimitry Andric // Analyze context for the module. 30311db9f3b2SDimitry Andric analyzeContextInfo(Unit.Unit->getOrigUnit().getUnitDIE(), 0, *(Unit.Unit), 30321db9f3b2SDimitry Andric &ODRContexts.getRoot(), ODRContexts, 0, 30331db9f3b2SDimitry Andric Options.ParseableSwiftInterfaces, 30341db9f3b2SDimitry Andric [&](const Twine &Warning, const DWARFDie &DIE) { 30351db9f3b2SDimitry Andric reportWarning(Warning, Context.File, &DIE); 30361db9f3b2SDimitry Andric }); 30371db9f3b2SDimitry Andric // Keep everything. 30381db9f3b2SDimitry Andric Unit.Unit->markEverythingAsKept(); 30391db9f3b2SDimitry Andric 30401db9f3b2SDimitry Andric // Clone unit. 30411db9f3b2SDimitry Andric UnitListTy CompileUnits; 30421db9f3b2SDimitry Andric CompileUnits.emplace_back(std::move(Unit.Unit)); 30431db9f3b2SDimitry Andric assert(TheDwarfEmitter); 30447a6dacacSDimitry Andric DIECloner(*this, TheDwarfEmitter, Unit.File, DIEAlloc, CompileUnits, 30451db9f3b2SDimitry Andric Options.Update, DebugStrPool, DebugLineStrPool, StringOffsetPool) 30461db9f3b2SDimitry Andric .cloneAllCompileUnits(*Unit.File.Dwarf, Unit.File, 30471db9f3b2SDimitry Andric Unit.File.Dwarf->isLittleEndian()); 30481db9f3b2SDimitry Andric return Error::success(); 30491db9f3b2SDimitry Andric } 30501db9f3b2SDimitry Andric 30511db9f3b2SDimitry Andric void DWARFLinker::verifyInput(const DWARFFile &File) { 30521db9f3b2SDimitry Andric assert(File.Dwarf); 30531db9f3b2SDimitry Andric 30541db9f3b2SDimitry Andric std::string Buffer; 30551db9f3b2SDimitry Andric raw_string_ostream OS(Buffer); 30561db9f3b2SDimitry Andric DIDumpOptions DumpOpts; 30571db9f3b2SDimitry Andric if (!File.Dwarf->verify(OS, DumpOpts.noImplicitRecursion())) { 30581db9f3b2SDimitry Andric if (Options.InputVerificationHandler) 30591db9f3b2SDimitry Andric Options.InputVerificationHandler(File, OS.str()); 30601db9f3b2SDimitry Andric } 30611db9f3b2SDimitry Andric } 30621db9f3b2SDimitry Andric 30631db9f3b2SDimitry Andric } // namespace llvm 3064