1 //===-- DWARFCompileUnit.cpp ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "DWARFCompileUnit.h"
10 #include "DWARFDebugAranges.h"
11 #include "SymbolFileDWARFDebugMap.h"
13 #include "lldb/Symbol/CompileUnit.h"
14 #include "lldb/Symbol/LineTable.h"
15 #include "lldb/Utility/Stream.h"
18 using namespace lldb_private;
20 void DWARFCompileUnit::Dump(Stream *s) const {
21 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, "
22 "abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at "
24 GetOffset(), GetLength(), GetVersion(), GetAbbrevOffset(),
25 GetAddressByteSize(), GetNextUnitOffset());
28 void DWARFCompileUnit::BuildAddressRangeTable(
29 DWARFDebugAranges *debug_aranges) {
30 // This function is usually called if there in no .debug_aranges section in
31 // order to produce a compile unit level set of address ranges that is
34 size_t num_debug_aranges = debug_aranges->GetNumRanges();
36 // First get the compile unit DIE only and check if it has a DW_AT_ranges
37 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
39 const dw_offset_t cu_offset = GetOffset();
41 DWARFRangeList ranges;
42 const size_t num_ranges =
43 die->GetAttributeAddressRanges(this, ranges, false);
45 // This compile unit has DW_AT_ranges, assume this is correct if it is
46 // present since clang no longer makes .debug_aranges by default and it
47 // emits DW_AT_ranges for DW_TAG_compile_units. GCC also does this with
49 for (size_t i = 0; i < num_ranges; ++i) {
50 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
51 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
55 return; // We got all of our ranges from the DW_AT_ranges attribute
58 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF
60 // If the DIEs weren't parsed, then we don't want all dies for all compile
61 // units to stay loaded when they weren't needed. So we can end up parsing
62 // the DWARF and then throwing them all away to keep memory usage down.
63 ScopedExtractDIEs clear_dies(ExtractDIEsScoped());
67 die->BuildAddressRangeTable(this, debug_aranges);
69 if (debug_aranges->GetNumRanges() == num_debug_aranges) {
70 // We got nothing from the functions, maybe we have a line tables only
71 // situation. Check the line tables and build the arange table from this.
73 sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(*this);
75 SymbolFileDWARFDebugMap *debug_map_sym_file =
76 m_dwarf.GetDebugMapSymfile();
77 if (debug_map_sym_file == nullptr) {
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(cu_offset, range.GetRangeBase(),
91 debug_map_sym_file->AddOSOARanges(&m_dwarf, debug_aranges);
95 if (debug_aranges->GetNumRanges() == num_debug_aranges) {
96 // We got nothing from the functions, maybe we have a line tables only
97 // situation. Check the line tables and build the arange table from this.
99 sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(*this);
101 if (LineTable *line_table = sc.comp_unit->GetLineTable()) {
102 LineTable::FileAddressRanges file_ranges;
103 const bool append = true;
104 const size_t num_ranges =
105 line_table->GetContiguousFileAddressRanges(file_ranges, append);
106 for (uint32_t idx = 0; idx < num_ranges; ++idx) {
107 const LineTable::FileAddressRanges::Entry &range =
108 file_ranges.GetEntryRef(idx);
109 debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(),
110 range.GetRangeEnd());