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