| 1 | //===-- DWARFCompileUnit.cpp ----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "DWARFCompileUnit.h" |
| 10 | #include "DWARFDebugAranges.h" |
| 11 | #include "LogChannelDWARF.h" |
| 12 | #include "SymbolFileDWARFDebugMap.h" |
| 13 | |
| 14 | #include "lldb/Symbol/CompileUnit.h" |
| 15 | #include "lldb/Symbol/LineTable.h" |
| 16 | #include "lldb/Utility/Stream.h" |
| 17 | #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h" |
| 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | using namespace lldb_private::plugin::dwarf; |
| 22 | |
| 23 | void DWARFCompileUnit::Dump(Stream *s) const { |
| 24 | s->Format( |
| 25 | |
| 26 | format: "{0:x16}: Compile Unit: length = {1:x8}, version = {2:x}, " |
| 27 | "abbr_offset = {3:x8}, addr_size = {4:x2} (next CU at " |
| 28 | "[{5:x16}])\n" , |
| 29 | args: GetOffset(), args: GetLength(), args: GetVersion(), args: (uint32_t)GetAbbrevOffset(), |
| 30 | args: GetAddressByteSize(), args: GetNextUnitOffset()); |
| 31 | } |
| 32 | |
| 33 | void DWARFCompileUnit::BuildAddressRangeTable( |
| 34 | DWARFDebugAranges *debug_aranges) { |
| 35 | // This function is usually called if there in no .debug_aranges section in |
| 36 | // order to produce a compile unit level set of address ranges that is |
| 37 | // accurate. |
| 38 | |
| 39 | size_t num_debug_aranges = debug_aranges->GetNumRanges(); |
| 40 | |
| 41 | // First get the compile unit DIE only and check contains ranges information. |
| 42 | const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); |
| 43 | |
| 44 | const dw_offset_t cu_offset = GetOffset(); |
| 45 | if (die) { |
| 46 | llvm::Expected<llvm::DWARFAddressRangesVector> ranges = |
| 47 | die->GetAttributeAddressRanges(cu: this, /*check_hi_lo_pc=*/true); |
| 48 | if (ranges) { |
| 49 | for (const llvm::DWARFAddressRange &range : *ranges) |
| 50 | debug_aranges->AppendRange(cu_offset, low_pc: range.LowPC, high_pc: range.HighPC); |
| 51 | if (!ranges->empty()) |
| 52 | return; |
| 53 | } else { |
| 54 | LLDB_LOG_ERROR(GetLog(DWARFLog::DebugInfo), ranges.takeError(), |
| 55 | "{1:x}: {0}" , cu_offset); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (debug_aranges->GetNumRanges() == num_debug_aranges) { |
| 60 | // We got nothing from the debug info, try to build the arange table from |
| 61 | // the debug map OSO aranges. |
| 62 | SymbolContext sc; |
| 63 | sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(dwarf_cu&: *this); |
| 64 | if (sc.comp_unit) { |
| 65 | SymbolFileDWARFDebugMap *debug_map_sym_file = |
| 66 | m_dwarf.GetDebugMapSymfile(); |
| 67 | if (debug_map_sym_file) { |
| 68 | auto *cu_info = |
| 69 | debug_map_sym_file->GetCompileUnitInfo(oso_dwarf: &GetSymbolFileDWARF()); |
| 70 | // If there are extra compile units the OSO entries aren't a reliable |
| 71 | // source of information. |
| 72 | if (cu_info->compile_units_sps.empty()) |
| 73 | debug_map_sym_file->AddOSOARanges(dwarf2Data: &m_dwarf, debug_aranges); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (debug_aranges->GetNumRanges() == num_debug_aranges) { |
| 79 | // We got nothing from the functions, maybe we have a line tables only |
| 80 | // situation. Check the line tables and build the arange table from this. |
| 81 | SymbolContext sc; |
| 82 | sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(dwarf_cu&: *this); |
| 83 | if (sc.comp_unit) { |
| 84 | if (LineTable *line_table = sc.comp_unit->GetLineTable()) { |
| 85 | LineTable::FileAddressRanges file_ranges; |
| 86 | const bool append = true; |
| 87 | const size_t num_ranges = |
| 88 | line_table->GetContiguousFileAddressRanges(file_ranges, append); |
| 89 | for (uint32_t idx = 0; idx < num_ranges; ++idx) { |
| 90 | const LineTable::FileAddressRanges::Entry &range = |
| 91 | file_ranges.GetEntryRef(i: idx); |
| 92 | debug_aranges->AppendRange(cu_offset: GetOffset(), low_pc: range.GetRangeBase(), |
| 93 | high_pc: range.GetRangeEnd()); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | DWARFCompileUnit &DWARFCompileUnit::GetNonSkeletonUnit() { |
| 101 | return llvm::cast<DWARFCompileUnit>(Val&: DWARFUnit::GetNonSkeletonUnit()); |
| 102 | } |
| 103 | |
| 104 | DWARFDIE DWARFCompileUnit::LookupAddress(const dw_addr_t address) { |
| 105 | if (DIE()) { |
| 106 | const DWARFDebugAranges &func_aranges = GetFunctionAranges(); |
| 107 | |
| 108 | // Re-check the aranges auto pointer contents in case it was created above |
| 109 | if (!func_aranges.IsEmpty()) |
| 110 | return GetDIE(die_offset: func_aranges.FindAddress(address)); |
| 111 | } |
| 112 | return DWARFDIE(); |
| 113 | } |
| 114 | |