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