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
17using namespace lldb;
18using namespace lldb_private;
19using namespace lldb_private::plugin::dwarf;
20
21void DWARFCompileUnit::Dump(Stream *s) const {
22 s->Format(
23
24 format: "{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 args: GetOffset(), args: GetLength(), args: GetVersion(), args: (uint32_t)GetAbbrevOffset(),
28 args: GetAddressByteSize(), args: GetNextUnitOffset());
29}
30
31void 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(cu: this, /*check_hi_lo_pc=*/true);
46 for (const DWARFRangeList::Entry &range : ranges)
47 debug_aranges->AppendRange(cu_offset, low_pc: range.GetRangeBase(),
48 high_pc: 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(dwarf_cu&: *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(oso_dwarf: &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(dwarf2Data: &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(dwarf_cu&: *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(i: idx);
87 debug_aranges->AppendRange(cu_offset: GetOffset(), low_pc: range.GetRangeBase(),
88 high_pc: range.GetRangeEnd());
89 }
90 }
91 }
92 }
93}
94
95DWARFCompileUnit &DWARFCompileUnit::GetNonSkeletonUnit() {
96 return llvm::cast<DWARFCompileUnit>(Val&: DWARFUnit::GetNonSkeletonUnit());
97}
98
99DWARFDIE 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(die_offset: func_aranges.FindAddress(address));
106 }
107 return DWARFDIE();
108}
109

source code of lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp