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 
20 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 
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         die->GetAttributeAddressRanges(this, /*check_hi_lo_pc=*/true);
45     for (const DWARFRangeList::Entry &range : ranges)
46       debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
47                                  range.GetRangeEnd());
48 
49     if (!ranges.IsEmpty())
50       return;
51   }
52 
53   if (debug_aranges->GetNumRanges() == num_debug_aranges) {
54     // We got nothing from the debug info, try to build the arange table from
55     // the debug map OSO aranges.
56     SymbolContext sc;
57     sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(*this);
58     if (sc.comp_unit) {
59       SymbolFileDWARFDebugMap *debug_map_sym_file =
60           m_dwarf.GetDebugMapSymfile();
61       if (debug_map_sym_file) {
62         auto *cu_info =
63             debug_map_sym_file->GetCompileUnitInfo(&GetSymbolFileDWARF());
64         // If there are extra compile units the OSO entries aren't a reliable
65         // source of information.
66         if (cu_info->compile_units_sps.empty())
67           debug_map_sym_file->AddOSOARanges(&m_dwarf, debug_aranges);
68       }
69     }
70   }
71 
72   if (debug_aranges->GetNumRanges() == num_debug_aranges) {
73     // We got nothing from the functions, maybe we have a line tables only
74     // situation. Check the line tables and build the arange table from this.
75     SymbolContext sc;
76     sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(*this);
77     if (sc.comp_unit) {
78       if (LineTable *line_table = sc.comp_unit->GetLineTable()) {
79         LineTable::FileAddressRanges file_ranges;
80         const bool append = true;
81         const size_t num_ranges =
82             line_table->GetContiguousFileAddressRanges(file_ranges, append);
83         for (uint32_t idx = 0; idx < num_ranges; ++idx) {
84           const LineTable::FileAddressRanges::Entry &range =
85               file_ranges.GetEntryRef(idx);
86           debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(),
87                                      range.GetRangeEnd());
88         }
89       }
90     }
91   }
92 }
93 
94 DWARFCompileUnit &DWARFCompileUnit::GetNonSkeletonUnit() {
95   return llvm::cast<DWARFCompileUnit>(DWARFUnit::GetNonSkeletonUnit());
96 }
97 
98 DWARFDIE DWARFCompileUnit::LookupAddress(const dw_addr_t address) {
99   if (DIE()) {
100     const DWARFDebugAranges &func_aranges = GetFunctionAranges();
101 
102     // Re-check the aranges auto pointer contents in case it was created above
103     if (!func_aranges.IsEmpty())
104       return GetDIE(func_aranges.FindAddress(address));
105   }
106   return DWARFDIE();
107 }
108