15ffd83dbSDimitry Andric //===- DwarfTransformer.cpp -----------------------------------------------===// 25ffd83dbSDimitry Andric // 35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65ffd83dbSDimitry Andric // 75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===// 85ffd83dbSDimitry Andric 95ffd83dbSDimitry Andric #include <thread> 105ffd83dbSDimitry Andric #include <unordered_set> 115ffd83dbSDimitry Andric 125ffd83dbSDimitry Andric #include "llvm/DebugInfo/DIContext.h" 1381ad6265SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 145ffd83dbSDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h" 155ffd83dbSDimitry Andric #include "llvm/Support/Error.h" 165ffd83dbSDimitry Andric #include "llvm/Support/ThreadPool.h" 175ffd83dbSDimitry Andric #include "llvm/Support/raw_ostream.h" 185ffd83dbSDimitry Andric 195ffd83dbSDimitry Andric #include "llvm/DebugInfo/GSYM/DwarfTransformer.h" 205ffd83dbSDimitry Andric #include "llvm/DebugInfo/GSYM/FunctionInfo.h" 215ffd83dbSDimitry Andric #include "llvm/DebugInfo/GSYM/GsymCreator.h" 225ffd83dbSDimitry Andric #include "llvm/DebugInfo/GSYM/GsymReader.h" 235ffd83dbSDimitry Andric #include "llvm/DebugInfo/GSYM/InlineInfo.h" 24*0fca6ea1SDimitry Andric #include "llvm/DebugInfo/GSYM/OutputAggregator.h" 25*0fca6ea1SDimitry Andric 26bdd1243dSDimitry Andric #include <optional> 275ffd83dbSDimitry Andric 285ffd83dbSDimitry Andric using namespace llvm; 295ffd83dbSDimitry Andric using namespace gsym; 305ffd83dbSDimitry Andric 315ffd83dbSDimitry Andric struct llvm::gsym::CUInfo { 325ffd83dbSDimitry Andric const DWARFDebugLine::LineTable *LineTable; 335ffd83dbSDimitry Andric const char *CompDir; 345ffd83dbSDimitry Andric std::vector<uint32_t> FileCache; 355ffd83dbSDimitry Andric uint64_t Language = 0; 365ffd83dbSDimitry Andric uint8_t AddrSize = 0; 375ffd83dbSDimitry Andric 385ffd83dbSDimitry Andric CUInfo(DWARFContext &DICtx, DWARFCompileUnit *CU) { 395ffd83dbSDimitry Andric LineTable = DICtx.getLineTableForUnit(CU); 405ffd83dbSDimitry Andric CompDir = CU->getCompilationDir(); 415ffd83dbSDimitry Andric FileCache.clear(); 425ffd83dbSDimitry Andric if (LineTable) 435ffd83dbSDimitry Andric FileCache.assign(LineTable->Prologue.FileNames.size() + 1, UINT32_MAX); 445ffd83dbSDimitry Andric DWARFDie Die = CU->getUnitDIE(); 455ffd83dbSDimitry Andric Language = dwarf::toUnsigned(Die.find(dwarf::DW_AT_language), 0); 465ffd83dbSDimitry Andric AddrSize = CU->getAddressByteSize(); 475ffd83dbSDimitry Andric } 485ffd83dbSDimitry Andric 495ffd83dbSDimitry Andric /// Return true if Addr is the highest address for a given compile unit. The 505ffd83dbSDimitry Andric /// highest address is encoded as -1, of all ones in the address. These high 515ffd83dbSDimitry Andric /// addresses are used by some linkers to indicate that a function has been 525ffd83dbSDimitry Andric /// dead stripped or didn't end up in the linked executable. 535ffd83dbSDimitry Andric bool isHighestAddress(uint64_t Addr) const { 545ffd83dbSDimitry Andric if (AddrSize == 4) 555ffd83dbSDimitry Andric return Addr == UINT32_MAX; 565ffd83dbSDimitry Andric else if (AddrSize == 8) 575ffd83dbSDimitry Andric return Addr == UINT64_MAX; 585ffd83dbSDimitry Andric return false; 595ffd83dbSDimitry Andric } 605ffd83dbSDimitry Andric 615ffd83dbSDimitry Andric /// Convert a DWARF compile unit file index into a GSYM global file index. 625ffd83dbSDimitry Andric /// 635ffd83dbSDimitry Andric /// Each compile unit in DWARF has its own file table in the line table 645ffd83dbSDimitry Andric /// prologue. GSYM has a single large file table that applies to all files 655ffd83dbSDimitry Andric /// from all of the info in a GSYM file. This function converts between the 665ffd83dbSDimitry Andric /// two and caches and DWARF CU file index that has already been converted so 675ffd83dbSDimitry Andric /// the first client that asks for a compile unit file index will end up 685ffd83dbSDimitry Andric /// doing the conversion, and subsequent clients will get the cached GSYM 695ffd83dbSDimitry Andric /// index. 705f757f3fSDimitry Andric std::optional<uint32_t> DWARFToGSYMFileIndex(GsymCreator &Gsym, 715f757f3fSDimitry Andric uint32_t DwarfFileIdx) { 725f757f3fSDimitry Andric if (!LineTable || DwarfFileIdx >= FileCache.size()) 735f757f3fSDimitry Andric return std::nullopt; 745ffd83dbSDimitry Andric uint32_t &GsymFileIdx = FileCache[DwarfFileIdx]; 755ffd83dbSDimitry Andric if (GsymFileIdx != UINT32_MAX) 765ffd83dbSDimitry Andric return GsymFileIdx; 775ffd83dbSDimitry Andric std::string File; 785ffd83dbSDimitry Andric if (LineTable->getFileNameByIndex( 795ffd83dbSDimitry Andric DwarfFileIdx, CompDir, 805ffd83dbSDimitry Andric DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) 815ffd83dbSDimitry Andric GsymFileIdx = Gsym.insertFile(File); 825ffd83dbSDimitry Andric else 835ffd83dbSDimitry Andric GsymFileIdx = 0; 845ffd83dbSDimitry Andric return GsymFileIdx; 855ffd83dbSDimitry Andric } 865ffd83dbSDimitry Andric }; 875ffd83dbSDimitry Andric 885ffd83dbSDimitry Andric 895ffd83dbSDimitry Andric static DWARFDie GetParentDeclContextDIE(DWARFDie &Die) { 905ffd83dbSDimitry Andric if (DWARFDie SpecDie = 915ffd83dbSDimitry Andric Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_specification)) { 925ffd83dbSDimitry Andric if (DWARFDie SpecParent = GetParentDeclContextDIE(SpecDie)) 935ffd83dbSDimitry Andric return SpecParent; 945ffd83dbSDimitry Andric } 955ffd83dbSDimitry Andric if (DWARFDie AbstDie = 965ffd83dbSDimitry Andric Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_abstract_origin)) { 975ffd83dbSDimitry Andric if (DWARFDie AbstParent = GetParentDeclContextDIE(AbstDie)) 985ffd83dbSDimitry Andric return AbstParent; 995ffd83dbSDimitry Andric } 1005ffd83dbSDimitry Andric 1015ffd83dbSDimitry Andric // We never want to follow parent for inlined subroutine - that would 1025ffd83dbSDimitry Andric // give us information about where the function is inlined, not what 1035ffd83dbSDimitry Andric // function is inlined 1045ffd83dbSDimitry Andric if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine) 1055ffd83dbSDimitry Andric return DWARFDie(); 1065ffd83dbSDimitry Andric 1075ffd83dbSDimitry Andric DWARFDie ParentDie = Die.getParent(); 1085ffd83dbSDimitry Andric if (!ParentDie) 1095ffd83dbSDimitry Andric return DWARFDie(); 1105ffd83dbSDimitry Andric 1115ffd83dbSDimitry Andric switch (ParentDie.getTag()) { 1125ffd83dbSDimitry Andric case dwarf::DW_TAG_namespace: 1135ffd83dbSDimitry Andric case dwarf::DW_TAG_structure_type: 1145ffd83dbSDimitry Andric case dwarf::DW_TAG_union_type: 1155ffd83dbSDimitry Andric case dwarf::DW_TAG_class_type: 1165ffd83dbSDimitry Andric case dwarf::DW_TAG_subprogram: 1175ffd83dbSDimitry Andric return ParentDie; // Found parent decl context DIE 1185ffd83dbSDimitry Andric case dwarf::DW_TAG_lexical_block: 1195ffd83dbSDimitry Andric return GetParentDeclContextDIE(ParentDie); 1205ffd83dbSDimitry Andric default: 1215ffd83dbSDimitry Andric break; 1225ffd83dbSDimitry Andric } 1235ffd83dbSDimitry Andric 1245ffd83dbSDimitry Andric return DWARFDie(); 1255ffd83dbSDimitry Andric } 1265ffd83dbSDimitry Andric 1275ffd83dbSDimitry Andric /// Get the GsymCreator string table offset for the qualified name for the 1285ffd83dbSDimitry Andric /// DIE passed in. This function will avoid making copies of any strings in 1295ffd83dbSDimitry Andric /// the GsymCreator when possible. We don't need to copy a string when the 1305ffd83dbSDimitry Andric /// string comes from our .debug_str section or is an inlined string in the 1315ffd83dbSDimitry Andric /// .debug_info. If we create a qualified name string in this function by 1325ffd83dbSDimitry Andric /// combining multiple strings in the DWARF string table or info, we will make 1335ffd83dbSDimitry Andric /// a copy of the string when we add it to the string table. 134bdd1243dSDimitry Andric static std::optional<uint32_t> 135bdd1243dSDimitry Andric getQualifiedNameIndex(DWARFDie &Die, uint64_t Language, GsymCreator &Gsym) { 1365ffd83dbSDimitry Andric // If the dwarf has mangled name, use mangled name 1375f757f3fSDimitry Andric if (auto LinkageName = Die.getLinkageName()) { 1385f757f3fSDimitry Andric // We have seen cases were linkage name is actually empty. 1395f757f3fSDimitry Andric if (strlen(LinkageName) > 0) 1405ffd83dbSDimitry Andric return Gsym.insertString(LinkageName, /* Copy */ false); 1415f757f3fSDimitry Andric } 1425ffd83dbSDimitry Andric 1435ffd83dbSDimitry Andric StringRef ShortName(Die.getName(DINameKind::ShortName)); 1445ffd83dbSDimitry Andric if (ShortName.empty()) 145bdd1243dSDimitry Andric return std::nullopt; 1465ffd83dbSDimitry Andric 1475ffd83dbSDimitry Andric // For C++ and ObjC, prepend names of all parent declaration contexts 1485ffd83dbSDimitry Andric if (!(Language == dwarf::DW_LANG_C_plus_plus || 1495ffd83dbSDimitry Andric Language == dwarf::DW_LANG_C_plus_plus_03 || 1505ffd83dbSDimitry Andric Language == dwarf::DW_LANG_C_plus_plus_11 || 1515ffd83dbSDimitry Andric Language == dwarf::DW_LANG_C_plus_plus_14 || 1525ffd83dbSDimitry Andric Language == dwarf::DW_LANG_ObjC_plus_plus || 1535ffd83dbSDimitry Andric // This should not be needed for C, but we see C++ code marked as C 1545ffd83dbSDimitry Andric // in some binaries. This should hurt, so let's do it for C as well 1555ffd83dbSDimitry Andric Language == dwarf::DW_LANG_C)) 1565ffd83dbSDimitry Andric return Gsym.insertString(ShortName, /* Copy */ false); 1575ffd83dbSDimitry Andric 1585ffd83dbSDimitry Andric // Some GCC optimizations create functions with names ending with .isra.<num> 1595ffd83dbSDimitry Andric // or .part.<num> and those names are just DW_AT_name, not DW_AT_linkage_name 1605ffd83dbSDimitry Andric // If it looks like it could be the case, don't add any prefix 1615f757f3fSDimitry Andric if (ShortName.starts_with("_Z") && 1625ffd83dbSDimitry Andric (ShortName.contains(".isra.") || ShortName.contains(".part."))) 1635ffd83dbSDimitry Andric return Gsym.insertString(ShortName, /* Copy */ false); 1645ffd83dbSDimitry Andric 1655ffd83dbSDimitry Andric DWARFDie ParentDeclCtxDie = GetParentDeclContextDIE(Die); 1665ffd83dbSDimitry Andric if (ParentDeclCtxDie) { 1675ffd83dbSDimitry Andric std::string Name = ShortName.str(); 1685ffd83dbSDimitry Andric while (ParentDeclCtxDie) { 1695ffd83dbSDimitry Andric StringRef ParentName(ParentDeclCtxDie.getName(DINameKind::ShortName)); 1705ffd83dbSDimitry Andric if (!ParentName.empty()) { 1715ffd83dbSDimitry Andric // "lambda" names are wrapped in < >. Replace with { } 1725ffd83dbSDimitry Andric // to be consistent with demangled names and not to confuse with 1735ffd83dbSDimitry Andric // templates 1745ffd83dbSDimitry Andric if (ParentName.front() == '<' && ParentName.back() == '>') 1755ffd83dbSDimitry Andric Name = "{" + ParentName.substr(1, ParentName.size() - 2).str() + "}" + 1765ffd83dbSDimitry Andric "::" + Name; 1775ffd83dbSDimitry Andric else 1785ffd83dbSDimitry Andric Name = ParentName.str() + "::" + Name; 1795ffd83dbSDimitry Andric } 1805ffd83dbSDimitry Andric ParentDeclCtxDie = GetParentDeclContextDIE(ParentDeclCtxDie); 1815ffd83dbSDimitry Andric } 1825ffd83dbSDimitry Andric // Copy the name since we created a new name in a std::string. 1835ffd83dbSDimitry Andric return Gsym.insertString(Name, /* Copy */ true); 1845ffd83dbSDimitry Andric } 1855ffd83dbSDimitry Andric // Don't copy the name since it exists in the DWARF object file. 1865ffd83dbSDimitry Andric return Gsym.insertString(ShortName, /* Copy */ false); 1875ffd83dbSDimitry Andric } 1885ffd83dbSDimitry Andric 1895ffd83dbSDimitry Andric static bool hasInlineInfo(DWARFDie Die, uint32_t Depth) { 1905ffd83dbSDimitry Andric bool CheckChildren = true; 1915ffd83dbSDimitry Andric switch (Die.getTag()) { 1925ffd83dbSDimitry Andric case dwarf::DW_TAG_subprogram: 1935ffd83dbSDimitry Andric // Don't look into functions within functions. 1945ffd83dbSDimitry Andric CheckChildren = Depth == 0; 1955ffd83dbSDimitry Andric break; 1965ffd83dbSDimitry Andric case dwarf::DW_TAG_inlined_subroutine: 1975ffd83dbSDimitry Andric return true; 1985ffd83dbSDimitry Andric default: 1995ffd83dbSDimitry Andric break; 2005ffd83dbSDimitry Andric } 2015ffd83dbSDimitry Andric if (!CheckChildren) 2025ffd83dbSDimitry Andric return false; 2035ffd83dbSDimitry Andric for (DWARFDie ChildDie : Die.children()) { 2045ffd83dbSDimitry Andric if (hasInlineInfo(ChildDie, Depth + 1)) 2055ffd83dbSDimitry Andric return true; 2065ffd83dbSDimitry Andric } 2075ffd83dbSDimitry Andric return false; 2085ffd83dbSDimitry Andric } 2095ffd83dbSDimitry Andric 2105f757f3fSDimitry Andric static AddressRanges 2115f757f3fSDimitry Andric ConvertDWARFRanges(const DWARFAddressRangesVector &DwarfRanges) { 2125f757f3fSDimitry Andric AddressRanges Ranges; 2135f757f3fSDimitry Andric for (const DWARFAddressRange &DwarfRange : DwarfRanges) { 2145f757f3fSDimitry Andric if (DwarfRange.LowPC < DwarfRange.HighPC) 2155f757f3fSDimitry Andric Ranges.insert({DwarfRange.LowPC, DwarfRange.HighPC}); 2165f757f3fSDimitry Andric } 2175f757f3fSDimitry Andric return Ranges; 2185f757f3fSDimitry Andric } 2195f757f3fSDimitry Andric 220*0fca6ea1SDimitry Andric static void parseInlineInfo(GsymCreator &Gsym, OutputAggregator &Out, 221*0fca6ea1SDimitry Andric CUInfo &CUI, DWARFDie Die, uint32_t Depth, 222*0fca6ea1SDimitry Andric FunctionInfo &FI, InlineInfo &Parent, 2235f757f3fSDimitry Andric const AddressRanges &AllParentRanges, 2245f757f3fSDimitry Andric bool &WarnIfEmpty) { 2255ffd83dbSDimitry Andric if (!hasInlineInfo(Die, Depth)) 2265ffd83dbSDimitry Andric return; 2275ffd83dbSDimitry Andric 2285ffd83dbSDimitry Andric dwarf::Tag Tag = Die.getTag(); 2295ffd83dbSDimitry Andric if (Tag == dwarf::DW_TAG_inlined_subroutine) { 2305ffd83dbSDimitry Andric // create new InlineInfo and append to parent.children 2315ffd83dbSDimitry Andric InlineInfo II; 2325f757f3fSDimitry Andric AddressRanges AllInlineRanges; 2335ffd83dbSDimitry Andric Expected<DWARFAddressRangesVector> RangesOrError = Die.getAddressRanges(); 2345ffd83dbSDimitry Andric if (RangesOrError) { 2355f757f3fSDimitry Andric AllInlineRanges = ConvertDWARFRanges(RangesOrError.get()); 2365f757f3fSDimitry Andric uint32_t EmptyCount = 0; 2375f757f3fSDimitry Andric for (const AddressRange &InlineRange : AllInlineRanges) { 2385f757f3fSDimitry Andric // Check for empty inline range in case inline function was outlined 2395f757f3fSDimitry Andric // or has not code 2405f757f3fSDimitry Andric if (InlineRange.empty()) { 2415f757f3fSDimitry Andric ++EmptyCount; 2425f757f3fSDimitry Andric } else { 2435f757f3fSDimitry Andric if (Parent.Ranges.contains(InlineRange)) { 2445f757f3fSDimitry Andric II.Ranges.insert(InlineRange); 2455f757f3fSDimitry Andric } else { 2465f757f3fSDimitry Andric // Only warn if the current inline range is not within any of all 2475f757f3fSDimitry Andric // of the parent ranges. If we have a DW_TAG_subpgram with multiple 2485f757f3fSDimitry Andric // ranges we will emit a FunctionInfo for each range of that 2495f757f3fSDimitry Andric // function that only emits information within the current range, 2505f757f3fSDimitry Andric // so we only want to emit an error if the DWARF has issues, not 2515f757f3fSDimitry Andric // when a range currently just isn't in the range we are currently 2525f757f3fSDimitry Andric // parsing for. 2535f757f3fSDimitry Andric if (AllParentRanges.contains(InlineRange)) { 2545f757f3fSDimitry Andric WarnIfEmpty = false; 255*0fca6ea1SDimitry Andric } else 256*0fca6ea1SDimitry Andric Out.Report("Function DIE has uncontained address range", 257*0fca6ea1SDimitry Andric [&](raw_ostream &OS) { 258*0fca6ea1SDimitry Andric OS << "error: inlined function DIE at " 2595f757f3fSDimitry Andric << HEX32(Die.getOffset()) << " has a range [" 2605f757f3fSDimitry Andric << HEX64(InlineRange.start()) << " - " 261*0fca6ea1SDimitry Andric << HEX64(InlineRange.end()) 262*0fca6ea1SDimitry Andric << ") that isn't contained in " 263*0fca6ea1SDimitry Andric << "any parent address ranges, this inline range " 264*0fca6ea1SDimitry Andric "will be " 2655f757f3fSDimitry Andric "removed.\n"; 266*0fca6ea1SDimitry Andric }); 2675ffd83dbSDimitry Andric } 2685f757f3fSDimitry Andric } 2695f757f3fSDimitry Andric } 2705f757f3fSDimitry Andric // If we have all empty ranges for the inlines, then don't warn if we 2715f757f3fSDimitry Andric // have an empty InlineInfo at the top level as all inline functions 2725f757f3fSDimitry Andric // were elided. 2735f757f3fSDimitry Andric if (EmptyCount == AllInlineRanges.size()) 2745f757f3fSDimitry Andric WarnIfEmpty = false; 2755f757f3fSDimitry Andric } 2765ffd83dbSDimitry Andric if (II.Ranges.empty()) 2775ffd83dbSDimitry Andric return; 2785ffd83dbSDimitry Andric 2795ffd83dbSDimitry Andric if (auto NameIndex = getQualifiedNameIndex(Die, CUI.Language, Gsym)) 2805ffd83dbSDimitry Andric II.Name = *NameIndex; 2815f757f3fSDimitry Andric const uint64_t DwarfFileIdx = dwarf::toUnsigned( 2825f757f3fSDimitry Andric Die.findRecursively(dwarf::DW_AT_call_file), UINT32_MAX); 2835f757f3fSDimitry Andric std::optional<uint32_t> OptGSymFileIdx = 2845f757f3fSDimitry Andric CUI.DWARFToGSYMFileIndex(Gsym, DwarfFileIdx); 2855f757f3fSDimitry Andric if (OptGSymFileIdx) { 2865f757f3fSDimitry Andric II.CallFile = OptGSymFileIdx.value(); 2875ffd83dbSDimitry Andric II.CallLine = dwarf::toUnsigned(Die.find(dwarf::DW_AT_call_line), 0); 2885ffd83dbSDimitry Andric // parse all children and append to parent 2895ffd83dbSDimitry Andric for (DWARFDie ChildDie : Die.children()) 290*0fca6ea1SDimitry Andric parseInlineInfo(Gsym, Out, CUI, ChildDie, Depth + 1, FI, II, 2915f757f3fSDimitry Andric AllInlineRanges, WarnIfEmpty); 2925f757f3fSDimitry Andric Parent.Children.emplace_back(std::move(II)); 293*0fca6ea1SDimitry Andric } else 294*0fca6ea1SDimitry Andric Out.Report( 295*0fca6ea1SDimitry Andric "Inlined function die has invlaid file index in DW_AT_call_file", 296*0fca6ea1SDimitry Andric [&](raw_ostream &OS) { 297*0fca6ea1SDimitry Andric OS << "error: inlined function DIE at " << HEX32(Die.getOffset()) 2985f757f3fSDimitry Andric << " has an invalid file index " << DwarfFileIdx 299*0fca6ea1SDimitry Andric << " in its DW_AT_call_file attribute, this inline entry and " 300*0fca6ea1SDimitry Andric "all " 3015f757f3fSDimitry Andric << "children will be removed.\n"; 302*0fca6ea1SDimitry Andric }); 3035ffd83dbSDimitry Andric return; 3045ffd83dbSDimitry Andric } 3055ffd83dbSDimitry Andric if (Tag == dwarf::DW_TAG_subprogram || Tag == dwarf::DW_TAG_lexical_block) { 3065ffd83dbSDimitry Andric // skip this Die and just recurse down 3075ffd83dbSDimitry Andric for (DWARFDie ChildDie : Die.children()) 308*0fca6ea1SDimitry Andric parseInlineInfo(Gsym, Out, CUI, ChildDie, Depth + 1, FI, Parent, 3095f757f3fSDimitry Andric AllParentRanges, WarnIfEmpty); 3105ffd83dbSDimitry Andric } 3115ffd83dbSDimitry Andric } 3125ffd83dbSDimitry Andric 313*0fca6ea1SDimitry Andric static void convertFunctionLineTable(OutputAggregator &Out, CUInfo &CUI, 3145ffd83dbSDimitry Andric DWARFDie Die, GsymCreator &Gsym, 3155ffd83dbSDimitry Andric FunctionInfo &FI) { 3165ffd83dbSDimitry Andric std::vector<uint32_t> RowVector; 3175ffd83dbSDimitry Andric const uint64_t StartAddress = FI.startAddress(); 3185ffd83dbSDimitry Andric const uint64_t EndAddress = FI.endAddress(); 3195ffd83dbSDimitry Andric const uint64_t RangeSize = EndAddress - StartAddress; 3205ffd83dbSDimitry Andric const object::SectionedAddress SecAddress{ 3215ffd83dbSDimitry Andric StartAddress, object::SectionedAddress::UndefSection}; 3225ffd83dbSDimitry Andric 3235ffd83dbSDimitry Andric 3245ffd83dbSDimitry Andric if (!CUI.LineTable->lookupAddressRange(SecAddress, RangeSize, RowVector)) { 3255ffd83dbSDimitry Andric // If we have a DW_TAG_subprogram but no line entries, fall back to using 3265ffd83dbSDimitry Andric // the DW_AT_decl_file an d DW_AT_decl_line if we have both attributes. 327349cc55cSDimitry Andric std::string FilePath = Die.getDeclFile( 328349cc55cSDimitry Andric DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath); 3295f757f3fSDimitry Andric if (FilePath.empty()) { 3305f757f3fSDimitry Andric // If we had a DW_AT_decl_file, but got no file then we need to emit a 3315f757f3fSDimitry Andric // warning. 332*0fca6ea1SDimitry Andric Out.Report("Invalid file index in DW_AT_decl_file", [&](raw_ostream &OS) { 3335f757f3fSDimitry Andric const uint64_t DwarfFileIdx = dwarf::toUnsigned( 3345f757f3fSDimitry Andric Die.findRecursively(dwarf::DW_AT_decl_file), UINT32_MAX); 335*0fca6ea1SDimitry Andric OS << "error: function DIE at " << HEX32(Die.getOffset()) 3365f757f3fSDimitry Andric << " has an invalid file index " << DwarfFileIdx 3375f757f3fSDimitry Andric << " in its DW_AT_decl_file attribute, unable to create a single " 3385f757f3fSDimitry Andric << "line entry from the DW_AT_decl_file/DW_AT_decl_line " 3395f757f3fSDimitry Andric << "attributes.\n"; 340*0fca6ea1SDimitry Andric }); 341349cc55cSDimitry Andric return; 3425f757f3fSDimitry Andric } 3435ffd83dbSDimitry Andric if (auto Line = 3445ffd83dbSDimitry Andric dwarf::toUnsigned(Die.findRecursively({dwarf::DW_AT_decl_line}))) { 345349cc55cSDimitry Andric LineEntry LE(StartAddress, Gsym.insertFile(FilePath), *Line); 3465ffd83dbSDimitry Andric FI.OptLineTable = LineTable(); 3475ffd83dbSDimitry Andric FI.OptLineTable->push(LE); 3485ffd83dbSDimitry Andric } 3495ffd83dbSDimitry Andric return; 3505ffd83dbSDimitry Andric } 3515ffd83dbSDimitry Andric 3525ffd83dbSDimitry Andric FI.OptLineTable = LineTable(); 3535ffd83dbSDimitry Andric DWARFDebugLine::Row PrevRow; 3545ffd83dbSDimitry Andric for (uint32_t RowIndex : RowVector) { 3555ffd83dbSDimitry Andric // Take file number and line/column from the row. 3565ffd83dbSDimitry Andric const DWARFDebugLine::Row &Row = CUI.LineTable->Rows[RowIndex]; 3575f757f3fSDimitry Andric std::optional<uint32_t> OptFileIdx = 3585f757f3fSDimitry Andric CUI.DWARFToGSYMFileIndex(Gsym, Row.File); 3595f757f3fSDimitry Andric if (!OptFileIdx) { 360*0fca6ea1SDimitry Andric Out.Report( 361*0fca6ea1SDimitry Andric "Invalid file index in DWARF line table", [&](raw_ostream &OS) { 362*0fca6ea1SDimitry Andric OS << "error: function DIE at " << HEX32(Die.getOffset()) << " has " 3635f757f3fSDimitry Andric << "a line entry with invalid DWARF file index, this entry will " 3645f757f3fSDimitry Andric << "be removed:\n"; 365*0fca6ea1SDimitry Andric Row.dumpTableHeader(OS, /*Indent=*/0); 366*0fca6ea1SDimitry Andric Row.dump(OS); 367*0fca6ea1SDimitry Andric OS << "\n"; 368*0fca6ea1SDimitry Andric }); 3695f757f3fSDimitry Andric continue; 3705f757f3fSDimitry Andric } 3715f757f3fSDimitry Andric const uint32_t FileIdx = OptFileIdx.value(); 3725ffd83dbSDimitry Andric uint64_t RowAddress = Row.Address.Address; 3735ffd83dbSDimitry Andric // Watch out for a RowAddress that is in the middle of a line table entry 3745ffd83dbSDimitry Andric // in the DWARF. If we pass an address in between two line table entries 3755ffd83dbSDimitry Andric // we will get a RowIndex for the previous valid line table row which won't 3765ffd83dbSDimitry Andric // be contained in our function. This is usually a bug in the DWARF due to 3775ffd83dbSDimitry Andric // linker problems or LTO or other DWARF re-linking so it is worth emitting 3785ffd83dbSDimitry Andric // an error, but not worth stopping the creation of the GSYM. 3795ffd83dbSDimitry Andric if (!FI.Range.contains(RowAddress)) { 38081ad6265SDimitry Andric if (RowAddress < FI.Range.start()) { 381*0fca6ea1SDimitry Andric Out.Report("Start address lies between valid Row table entries", 382*0fca6ea1SDimitry Andric [&](raw_ostream &OS) { 383*0fca6ea1SDimitry Andric OS << "error: DIE has a start address whose LowPC is " 384*0fca6ea1SDimitry Andric "between the " 385*0fca6ea1SDimitry Andric "line table Row[" 386*0fca6ea1SDimitry Andric << RowIndex << "] with address " << HEX64(RowAddress) 387*0fca6ea1SDimitry Andric << " and the next one.\n"; 388*0fca6ea1SDimitry Andric Die.dump(OS, 0, DIDumpOptions::getForSingleDIE()); 389*0fca6ea1SDimitry Andric }); 39081ad6265SDimitry Andric RowAddress = FI.Range.start(); 3915ffd83dbSDimitry Andric } else { 3925ffd83dbSDimitry Andric continue; 3935ffd83dbSDimitry Andric } 3945ffd83dbSDimitry Andric } 3955ffd83dbSDimitry Andric 3965ffd83dbSDimitry Andric LineEntry LE(RowAddress, FileIdx, Row.Line); 3975ffd83dbSDimitry Andric if (RowIndex != RowVector[0] && Row.Address < PrevRow.Address) { 3985ffd83dbSDimitry Andric // We have seen full duplicate line tables for functions in some 3995f757f3fSDimitry Andric // DWARF files. Watch for those here by checking the last 4005ffd83dbSDimitry Andric // row was the function's end address (HighPC) and that the 4015ffd83dbSDimitry Andric // current line table entry's address is the same as the first 4025ffd83dbSDimitry Andric // line entry we already have in our "function_info.Lines". If 4035ffd83dbSDimitry Andric // so break out after printing a warning. 4045ffd83dbSDimitry Andric auto FirstLE = FI.OptLineTable->first(); 405*0fca6ea1SDimitry Andric if (FirstLE && *FirstLE == LE) 406*0fca6ea1SDimitry Andric // if (Log && !Gsym.isQuiet()) { TODO <-- This looks weird 407*0fca6ea1SDimitry Andric Out.Report("Duplicate line table detected", [&](raw_ostream &OS) { 408*0fca6ea1SDimitry Andric OS << "warning: duplicate line table detected for DIE:\n"; 409*0fca6ea1SDimitry Andric Die.dump(OS, 0, DIDumpOptions::getForSingleDIE()); 410*0fca6ea1SDimitry Andric }); 411*0fca6ea1SDimitry Andric else 412*0fca6ea1SDimitry Andric Out.Report("Non-monotonically increasing addresses", 413*0fca6ea1SDimitry Andric [&](raw_ostream &OS) { 414*0fca6ea1SDimitry Andric OS << "error: line table has addresses that do not " 4155ffd83dbSDimitry Andric << "monotonically increase:\n"; 4165f757f3fSDimitry Andric for (uint32_t RowIndex2 : RowVector) 417*0fca6ea1SDimitry Andric CUI.LineTable->Rows[RowIndex2].dump(OS); 418*0fca6ea1SDimitry Andric Die.dump(OS, 0, DIDumpOptions::getForSingleDIE()); 419*0fca6ea1SDimitry Andric }); 4205ffd83dbSDimitry Andric break; 4215ffd83dbSDimitry Andric } 4225ffd83dbSDimitry Andric 4235ffd83dbSDimitry Andric // Skip multiple line entries for the same file and line. 4245ffd83dbSDimitry Andric auto LastLE = FI.OptLineTable->last(); 4255ffd83dbSDimitry Andric if (LastLE && LastLE->File == FileIdx && LastLE->Line == Row.Line) 4265ffd83dbSDimitry Andric continue; 4275ffd83dbSDimitry Andric // Only push a row if it isn't an end sequence. End sequence markers are 4285ffd83dbSDimitry Andric // included for the last address in a function or the last contiguous 4295ffd83dbSDimitry Andric // address in a sequence. 4305ffd83dbSDimitry Andric if (Row.EndSequence) { 4315ffd83dbSDimitry Andric // End sequence means that the next line entry could have a lower address 4325ffd83dbSDimitry Andric // that the previous entries. So we clear the previous row so we don't 4335ffd83dbSDimitry Andric // trigger the line table error about address that do not monotonically 4345ffd83dbSDimitry Andric // increase. 4355ffd83dbSDimitry Andric PrevRow = DWARFDebugLine::Row(); 4365ffd83dbSDimitry Andric } else { 4375ffd83dbSDimitry Andric FI.OptLineTable->push(LE); 4385ffd83dbSDimitry Andric PrevRow = Row; 4395ffd83dbSDimitry Andric } 4405ffd83dbSDimitry Andric } 4415ffd83dbSDimitry Andric // If not line table rows were added, clear the line table so we don't encode 4425ffd83dbSDimitry Andric // on in the GSYM file. 4435ffd83dbSDimitry Andric if (FI.OptLineTable->empty()) 444bdd1243dSDimitry Andric FI.OptLineTable = std::nullopt; 4455ffd83dbSDimitry Andric } 4465ffd83dbSDimitry Andric 447*0fca6ea1SDimitry Andric void DwarfTransformer::handleDie(OutputAggregator &Out, CUInfo &CUI, 448*0fca6ea1SDimitry Andric DWARFDie Die) { 4495ffd83dbSDimitry Andric switch (Die.getTag()) { 4505ffd83dbSDimitry Andric case dwarf::DW_TAG_subprogram: { 4515ffd83dbSDimitry Andric Expected<DWARFAddressRangesVector> RangesOrError = Die.getAddressRanges(); 4525ffd83dbSDimitry Andric if (!RangesOrError) { 4535ffd83dbSDimitry Andric consumeError(RangesOrError.takeError()); 4545ffd83dbSDimitry Andric break; 4555ffd83dbSDimitry Andric } 4565ffd83dbSDimitry Andric const DWARFAddressRangesVector &Ranges = RangesOrError.get(); 4575ffd83dbSDimitry Andric if (Ranges.empty()) 4585ffd83dbSDimitry Andric break; 4595ffd83dbSDimitry Andric auto NameIndex = getQualifiedNameIndex(Die, CUI.Language, Gsym); 4605ffd83dbSDimitry Andric if (!NameIndex) { 461*0fca6ea1SDimitry Andric Out.Report("Function has no name", [&](raw_ostream &OS) { 462*0fca6ea1SDimitry Andric OS << "error: function at " << HEX64(Die.getOffset()) 4635ffd83dbSDimitry Andric << " has no name\n "; 464*0fca6ea1SDimitry Andric Die.dump(OS, 0, DIDumpOptions::getForSingleDIE()); 465*0fca6ea1SDimitry Andric }); 4665ffd83dbSDimitry Andric break; 4675ffd83dbSDimitry Andric } 4685f757f3fSDimitry Andric // All ranges for the subprogram DIE in case it has multiple. We need to 4695f757f3fSDimitry Andric // pass this down into parseInlineInfo so we don't warn about inline 4705f757f3fSDimitry Andric // ranges that are not in the current subrange of a function when they 4715f757f3fSDimitry Andric // actually are in another subgrange. We do this because when a function 4725f757f3fSDimitry Andric // has discontiguos ranges, we create multiple function entries with only 4735f757f3fSDimitry Andric // the info for that range contained inside of it. 4745f757f3fSDimitry Andric AddressRanges AllSubprogramRanges = ConvertDWARFRanges(Ranges); 4755ffd83dbSDimitry Andric 4765ffd83dbSDimitry Andric // Create a function_info for each range 4775ffd83dbSDimitry Andric for (const DWARFAddressRange &Range : Ranges) { 4785ffd83dbSDimitry Andric // The low PC must be less than the high PC. Many linkers don't remove 4795ffd83dbSDimitry Andric // DWARF for functions that don't get linked into the final executable. 4805ffd83dbSDimitry Andric // If both the high and low pc have relocations, linkers will often set 4815ffd83dbSDimitry Andric // the address values for both to the same value to indicate the function 4825ffd83dbSDimitry Andric // has been remove. Other linkers have been known to set the one or both 4835ffd83dbSDimitry Andric // PC values to a UINT32_MAX for 4 byte addresses and UINT64_MAX for 8 4845ffd83dbSDimitry Andric // byte addresses to indicate the function isn't valid. The check below 4855ffd83dbSDimitry Andric // tries to watch for these cases and abort if it runs into them. 4865ffd83dbSDimitry Andric if (Range.LowPC >= Range.HighPC || CUI.isHighestAddress(Range.LowPC)) 4875ffd83dbSDimitry Andric break; 4885ffd83dbSDimitry Andric 4895ffd83dbSDimitry Andric // Many linkers can't remove DWARF and might set the LowPC to zero. Since 4905ffd83dbSDimitry Andric // high PC can be an offset from the low PC in more recent DWARF versions 491*0fca6ea1SDimitry Andric // we need to watch for a zero'ed low pc which we do using ValidTextRanges 492*0fca6ea1SDimitry Andric // below. 4935ffd83dbSDimitry Andric if (!Gsym.IsValidTextAddress(Range.LowPC)) { 4945ffd83dbSDimitry Andric // We expect zero and -1 to be invalid addresses in DWARF depending 4955ffd83dbSDimitry Andric // on the linker of the DWARF. This indicates a function was stripped 4965ffd83dbSDimitry Andric // and the debug info wasn't able to be stripped from the DWARF. If 4975ffd83dbSDimitry Andric // the LowPC isn't zero or -1, then we should emit an error. 4985ffd83dbSDimitry Andric if (Range.LowPC != 0) { 499fe6060f1SDimitry Andric if (!Gsym.isQuiet()) { 500fe6060f1SDimitry Andric // Unexpected invalid address, emit a warning 501*0fca6ea1SDimitry Andric Out.Report("Address range starts outside executable section", 502*0fca6ea1SDimitry Andric [&](raw_ostream &OS) { 503*0fca6ea1SDimitry Andric OS << "warning: DIE has an address range whose " 504*0fca6ea1SDimitry Andric "start address " 5055f757f3fSDimitry Andric "is not in any executable sections (" 506fe6060f1SDimitry Andric << *Gsym.GetValidTextRanges() 507fe6060f1SDimitry Andric << ") and will not be processed:\n"; 508*0fca6ea1SDimitry Andric Die.dump(OS, 0, DIDumpOptions::getForSingleDIE()); 509*0fca6ea1SDimitry Andric }); 5105ffd83dbSDimitry Andric } 511fe6060f1SDimitry Andric } 5125ffd83dbSDimitry Andric break; 5135ffd83dbSDimitry Andric } 5145ffd83dbSDimitry Andric 5155ffd83dbSDimitry Andric FunctionInfo FI; 51681ad6265SDimitry Andric FI.Range = {Range.LowPC, Range.HighPC}; 5175ffd83dbSDimitry Andric FI.Name = *NameIndex; 5185f757f3fSDimitry Andric if (CUI.LineTable) 519*0fca6ea1SDimitry Andric convertFunctionLineTable(Out, CUI, Die, Gsym, FI); 5205f757f3fSDimitry Andric 5215ffd83dbSDimitry Andric if (hasInlineInfo(Die, 0)) { 5225ffd83dbSDimitry Andric FI.Inline = InlineInfo(); 5235ffd83dbSDimitry Andric FI.Inline->Name = *NameIndex; 5245ffd83dbSDimitry Andric FI.Inline->Ranges.insert(FI.Range); 5255f757f3fSDimitry Andric bool WarnIfEmpty = true; 526*0fca6ea1SDimitry Andric parseInlineInfo(Gsym, Out, CUI, Die, 0, FI, *FI.Inline, 5275f757f3fSDimitry Andric AllSubprogramRanges, WarnIfEmpty); 5285f757f3fSDimitry Andric // Make sure we at least got some valid inline info other than just 5295f757f3fSDimitry Andric // the top level function. If we didn't then remove the inline info 5305f757f3fSDimitry Andric // from the function info. We have seen cases where LTO tries to modify 5315f757f3fSDimitry Andric // the DWARF for functions and it messes up the address ranges for 5325f757f3fSDimitry Andric // the inline functions so it is no longer valid. 5335f757f3fSDimitry Andric // 5345f757f3fSDimitry Andric // By checking if there are any valid children on the top level inline 5355f757f3fSDimitry Andric // information object, we will know if we got anything valid from the 5365f757f3fSDimitry Andric // debug info. 5375f757f3fSDimitry Andric if (FI.Inline->Children.empty()) { 538*0fca6ea1SDimitry Andric if (WarnIfEmpty && !Gsym.isQuiet()) 539*0fca6ea1SDimitry Andric Out.Report("DIE contains inline functions with no valid ranges", 540*0fca6ea1SDimitry Andric [&](raw_ostream &OS) { 541*0fca6ea1SDimitry Andric OS << "warning: DIE contains inline function " 542*0fca6ea1SDimitry Andric "information that has no valid ranges, removing " 543*0fca6ea1SDimitry Andric "inline information:\n"; 544*0fca6ea1SDimitry Andric Die.dump(OS, 0, DIDumpOptions::getForSingleDIE()); 545*0fca6ea1SDimitry Andric }); 5465f757f3fSDimitry Andric FI.Inline = std::nullopt; 5475f757f3fSDimitry Andric } 5485ffd83dbSDimitry Andric } 5495ffd83dbSDimitry Andric Gsym.addFunctionInfo(std::move(FI)); 5505ffd83dbSDimitry Andric } 5515ffd83dbSDimitry Andric } break; 5525ffd83dbSDimitry Andric default: 5535ffd83dbSDimitry Andric break; 5545ffd83dbSDimitry Andric } 5555ffd83dbSDimitry Andric for (DWARFDie ChildDie : Die.children()) 556*0fca6ea1SDimitry Andric handleDie(Out, CUI, ChildDie); 5575ffd83dbSDimitry Andric } 5585ffd83dbSDimitry Andric 559*0fca6ea1SDimitry Andric Error DwarfTransformer::convert(uint32_t NumThreads, OutputAggregator &Out) { 5605ffd83dbSDimitry Andric size_t NumBefore = Gsym.getNumFunctionInfos(); 56181ad6265SDimitry Andric auto getDie = [&](DWARFUnit &DwarfUnit) -> DWARFDie { 56281ad6265SDimitry Andric DWARFDie ReturnDie = DwarfUnit.getUnitDIE(false); 5635f757f3fSDimitry Andric if (DwarfUnit.getDWOId()) { 56481ad6265SDimitry Andric DWARFUnit *DWOCU = DwarfUnit.getNonSkeletonUnitDIE(false).getDwarfUnit(); 565*0fca6ea1SDimitry Andric if (!DWOCU->isDWOUnit()) 566*0fca6ea1SDimitry Andric Out.Report( 567*0fca6ea1SDimitry Andric "warning: Unable to retrieve DWO .debug_info section for some " 568*0fca6ea1SDimitry Andric "object files. (Remove the --quiet flag for full output)", 569*0fca6ea1SDimitry Andric [&](raw_ostream &OS) { 57081ad6265SDimitry Andric std::string DWOName = dwarf::toString( 57181ad6265SDimitry Andric DwarfUnit.getUnitDIE().find( 57281ad6265SDimitry Andric {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), 57381ad6265SDimitry Andric ""); 574*0fca6ea1SDimitry Andric OS << "warning: Unable to retrieve DWO .debug_info section for " 57581ad6265SDimitry Andric << DWOName << "\n"; 576*0fca6ea1SDimitry Andric }); 577*0fca6ea1SDimitry Andric else { 57881ad6265SDimitry Andric ReturnDie = DWOCU->getUnitDIE(false); 57981ad6265SDimitry Andric } 58081ad6265SDimitry Andric } 58181ad6265SDimitry Andric return ReturnDie; 58281ad6265SDimitry Andric }; 5835ffd83dbSDimitry Andric if (NumThreads == 1) { 5845ffd83dbSDimitry Andric // Parse all DWARF data from this thread, use the same string/file table 5855ffd83dbSDimitry Andric // for everything 5865ffd83dbSDimitry Andric for (const auto &CU : DICtx.compile_units()) { 58781ad6265SDimitry Andric DWARFDie Die = getDie(*CU); 5885ffd83dbSDimitry Andric CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get())); 589*0fca6ea1SDimitry Andric handleDie(Out, CUI, Die); 5905ffd83dbSDimitry Andric } 5915ffd83dbSDimitry Andric } else { 5925ffd83dbSDimitry Andric // LLVM Dwarf parser is not thread-safe and we need to parse all DWARF up 5935ffd83dbSDimitry Andric // front before we start accessing any DIEs since there might be 5945ffd83dbSDimitry Andric // cross compile unit references in the DWARF. If we don't do this we can 5955ffd83dbSDimitry Andric // end up crashing. 5965ffd83dbSDimitry Andric 5975ffd83dbSDimitry Andric // We need to call getAbbreviations sequentially first so that getUnitDIE() 5985ffd83dbSDimitry Andric // only works with its local data. 5995ffd83dbSDimitry Andric for (const auto &CU : DICtx.compile_units()) 6005ffd83dbSDimitry Andric CU->getAbbreviations(); 6015ffd83dbSDimitry Andric 6025ffd83dbSDimitry Andric // Now parse all DIEs in case we have cross compile unit references in a 6035ffd83dbSDimitry Andric // thread pool. 604*0fca6ea1SDimitry Andric DefaultThreadPool pool(hardware_concurrency(NumThreads)); 6055ffd83dbSDimitry Andric for (const auto &CU : DICtx.compile_units()) 6065ffd83dbSDimitry Andric pool.async([&CU]() { CU->getUnitDIE(false /*CUDieOnly*/); }); 6075ffd83dbSDimitry Andric pool.wait(); 6085ffd83dbSDimitry Andric 6095ffd83dbSDimitry Andric // Now convert all DWARF to GSYM in a thread pool. 6105ffd83dbSDimitry Andric std::mutex LogMutex; 6115ffd83dbSDimitry Andric for (const auto &CU : DICtx.compile_units()) { 61281ad6265SDimitry Andric DWARFDie Die = getDie(*CU); 6135ffd83dbSDimitry Andric if (Die) { 6145ffd83dbSDimitry Andric CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get())); 615*0fca6ea1SDimitry Andric pool.async([this, CUI, &LogMutex, &Out, Die]() mutable { 616*0fca6ea1SDimitry Andric std::string storage; 617*0fca6ea1SDimitry Andric raw_string_ostream StrStream(storage); 618*0fca6ea1SDimitry Andric OutputAggregator ThreadOut(Out.GetOS() ? &StrStream : nullptr); 619*0fca6ea1SDimitry Andric handleDie(ThreadOut, CUI, Die); 6205ffd83dbSDimitry Andric // Print ThreadLogStorage lines into an actual stream under a lock 6215ffd83dbSDimitry Andric std::lock_guard<std::mutex> guard(LogMutex); 622*0fca6ea1SDimitry Andric if (Out.GetOS()) { 623*0fca6ea1SDimitry Andric StrStream.flush(); 624*0fca6ea1SDimitry Andric Out << storage; 6255ffd83dbSDimitry Andric } 626*0fca6ea1SDimitry Andric Out.Merge(ThreadOut); 6275ffd83dbSDimitry Andric }); 6285ffd83dbSDimitry Andric } 6295ffd83dbSDimitry Andric } 6305ffd83dbSDimitry Andric pool.wait(); 6315ffd83dbSDimitry Andric } 6325ffd83dbSDimitry Andric size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore; 633*0fca6ea1SDimitry Andric Out << "Loaded " << FunctionsAddedCount << " functions from DWARF.\n"; 6345ffd83dbSDimitry Andric return Error::success(); 6355ffd83dbSDimitry Andric } 6365ffd83dbSDimitry Andric 637*0fca6ea1SDimitry Andric llvm::Error DwarfTransformer::verify(StringRef GsymPath, 638*0fca6ea1SDimitry Andric OutputAggregator &Out) { 639*0fca6ea1SDimitry Andric Out << "Verifying GSYM file \"" << GsymPath << "\":\n"; 6405ffd83dbSDimitry Andric 6415ffd83dbSDimitry Andric auto Gsym = GsymReader::openFile(GsymPath); 6425ffd83dbSDimitry Andric if (!Gsym) 6435ffd83dbSDimitry Andric return Gsym.takeError(); 6445ffd83dbSDimitry Andric 6455ffd83dbSDimitry Andric auto NumAddrs = Gsym->getNumAddresses(); 6465ffd83dbSDimitry Andric DILineInfoSpecifier DLIS( 6475ffd83dbSDimitry Andric DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, 6485ffd83dbSDimitry Andric DILineInfoSpecifier::FunctionNameKind::LinkageName); 6495ffd83dbSDimitry Andric std::string gsymFilename; 6505ffd83dbSDimitry Andric for (uint32_t I = 0; I < NumAddrs; ++I) { 6515ffd83dbSDimitry Andric auto FuncAddr = Gsym->getAddress(I); 6525ffd83dbSDimitry Andric if (!FuncAddr) 6535ffd83dbSDimitry Andric return createStringError(std::errc::invalid_argument, 6545ffd83dbSDimitry Andric "failed to extract address[%i]", I); 6555ffd83dbSDimitry Andric 6565ffd83dbSDimitry Andric auto FI = Gsym->getFunctionInfo(*FuncAddr); 6575ffd83dbSDimitry Andric if (!FI) 658*0fca6ea1SDimitry Andric return createStringError( 659*0fca6ea1SDimitry Andric std::errc::invalid_argument, 660*0fca6ea1SDimitry Andric "failed to extract function info for address 0x%" PRIu64, *FuncAddr); 6615ffd83dbSDimitry Andric 6625ffd83dbSDimitry Andric for (auto Addr = *FuncAddr; Addr < *FuncAddr + FI->size(); ++Addr) { 6635ffd83dbSDimitry Andric const object::SectionedAddress SectAddr{ 6645ffd83dbSDimitry Andric Addr, object::SectionedAddress::UndefSection}; 6655ffd83dbSDimitry Andric auto LR = Gsym->lookup(Addr); 6665ffd83dbSDimitry Andric if (!LR) 6675ffd83dbSDimitry Andric return LR.takeError(); 6685ffd83dbSDimitry Andric 6695ffd83dbSDimitry Andric auto DwarfInlineInfos = 6705ffd83dbSDimitry Andric DICtx.getInliningInfoForAddress(SectAddr, DLIS); 6715ffd83dbSDimitry Andric uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames(); 6725ffd83dbSDimitry Andric if (NumDwarfInlineInfos == 0) { 6735ffd83dbSDimitry Andric DwarfInlineInfos.addFrame( 6745ffd83dbSDimitry Andric DICtx.getLineInfoForAddress(SectAddr, DLIS)); 6755ffd83dbSDimitry Andric } 6765ffd83dbSDimitry Andric 6775ffd83dbSDimitry Andric // Check for 1 entry that has no file and line info 6785ffd83dbSDimitry Andric if (NumDwarfInlineInfos == 1 && 6795ffd83dbSDimitry Andric DwarfInlineInfos.getFrame(0).FileName == "<invalid>") { 6805ffd83dbSDimitry Andric DwarfInlineInfos = DIInliningInfo(); 6815ffd83dbSDimitry Andric NumDwarfInlineInfos = 0; 6825ffd83dbSDimitry Andric } 6835ffd83dbSDimitry Andric if (NumDwarfInlineInfos > 0 && 6845ffd83dbSDimitry Andric NumDwarfInlineInfos != LR->Locations.size()) { 685*0fca6ea1SDimitry Andric if (Out.GetOS()) { 686*0fca6ea1SDimitry Andric raw_ostream &Log = *Out.GetOS(); 6875ffd83dbSDimitry Andric Log << "error: address " << HEX64(Addr) << " has " 6885ffd83dbSDimitry Andric << NumDwarfInlineInfos << " DWARF inline frames and GSYM has " 6895ffd83dbSDimitry Andric << LR->Locations.size() << "\n"; 6905ffd83dbSDimitry Andric Log << " " << NumDwarfInlineInfos << " DWARF frames:\n"; 6915ffd83dbSDimitry Andric for (size_t Idx = 0; Idx < NumDwarfInlineInfos; ++Idx) { 6920eae32dcSDimitry Andric const auto &dii = DwarfInlineInfos.getFrame(Idx); 6935ffd83dbSDimitry Andric Log << " [" << Idx << "]: " << dii.FunctionName << " @ " 6945ffd83dbSDimitry Andric << dii.FileName << ':' << dii.Line << '\n'; 6955ffd83dbSDimitry Andric } 6965ffd83dbSDimitry Andric Log << " " << LR->Locations.size() << " GSYM frames:\n"; 697*0fca6ea1SDimitry Andric for (size_t Idx = 0, count = LR->Locations.size(); Idx < count; 698*0fca6ea1SDimitry Andric ++Idx) { 6995ffd83dbSDimitry Andric const auto &gii = LR->Locations[Idx]; 7005ffd83dbSDimitry Andric Log << " [" << Idx << "]: " << gii.Name << " @ " << gii.Dir 7015ffd83dbSDimitry Andric << '/' << gii.Base << ':' << gii.Line << '\n'; 7025ffd83dbSDimitry Andric } 7035ffd83dbSDimitry Andric DwarfInlineInfos = DICtx.getInliningInfoForAddress(SectAddr, DLIS); 7045ffd83dbSDimitry Andric Gsym->dump(Log, *FI); 705*0fca6ea1SDimitry Andric } 7065ffd83dbSDimitry Andric continue; 7075ffd83dbSDimitry Andric } 7085ffd83dbSDimitry Andric 7095ffd83dbSDimitry Andric for (size_t Idx = 0, count = LR->Locations.size(); Idx < count; 7105ffd83dbSDimitry Andric ++Idx) { 7115ffd83dbSDimitry Andric const auto &gii = LR->Locations[Idx]; 7125ffd83dbSDimitry Andric if (Idx < NumDwarfInlineInfos) { 7130eae32dcSDimitry Andric const auto &dii = DwarfInlineInfos.getFrame(Idx); 7145ffd83dbSDimitry Andric gsymFilename = LR->getSourceFile(Idx); 7155ffd83dbSDimitry Andric // Verify function name 7165ffd83dbSDimitry Andric if (dii.FunctionName.find(gii.Name.str()) != 0) 717*0fca6ea1SDimitry Andric Out << "error: address " << HEX64(Addr) << " DWARF function \"" 7185ffd83dbSDimitry Andric << dii.FunctionName.c_str() 7195ffd83dbSDimitry Andric << "\" doesn't match GSYM function \"" << gii.Name << "\"\n"; 720*0fca6ea1SDimitry Andric 7215ffd83dbSDimitry Andric // Verify source file path 7225ffd83dbSDimitry Andric if (dii.FileName != gsymFilename) 723*0fca6ea1SDimitry Andric Out << "error: address " << HEX64(Addr) << " DWARF path \"" 7245ffd83dbSDimitry Andric << dii.FileName.c_str() << "\" doesn't match GSYM path \"" 7255ffd83dbSDimitry Andric << gsymFilename.c_str() << "\"\n"; 7265ffd83dbSDimitry Andric // Verify source file line 7275ffd83dbSDimitry Andric if (dii.Line != gii.Line) 728*0fca6ea1SDimitry Andric Out << "error: address " << HEX64(Addr) << " DWARF line " 7295ffd83dbSDimitry Andric << dii.Line << " != GSYM line " << gii.Line << "\n"; 7305ffd83dbSDimitry Andric } 7315ffd83dbSDimitry Andric } 7325ffd83dbSDimitry Andric } 7335ffd83dbSDimitry Andric } 7345ffd83dbSDimitry Andric return Error::success(); 7355ffd83dbSDimitry Andric } 736