1 //===-- DWARFCompileUnit.cpp ------------------------------------*- C++ -*-===// 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->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 " 23 "{0x%8.8x})\n", 24 GetOffset(), GetLength(), GetVersion(), GetAbbrevOffset(), 25 GetAddressByteSize(), GetNextUnitOffset()); 26 } 27 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 32 // accurate. 33 34 size_t num_debug_aranges = debug_aranges->GetNumRanges(); 35 36 // First get the compile unit DIE only and check if it has a DW_AT_ranges 37 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly(); 38 39 const dw_offset_t cu_offset = GetOffset(); 40 if (die) { 41 DWARFRangeList ranges; 42 const size_t num_ranges = 43 die->GetAttributeAddressRanges(this, ranges, false); 44 if (num_ranges > 0) { 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 48 // recent GCC builds. 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(), 52 range.GetRangeEnd()); 53 } 54 55 return; // We got all of our ranges from the DW_AT_ranges attribute 56 } 57 } 58 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF 59 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()); 64 65 die = DIEPtr(); 66 if (die) 67 die->BuildAddressRangeTable(this, debug_aranges); 68 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. 72 SymbolContext sc; 73 sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(*this); 74 if (sc.comp_unit) { 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(), 87 range.GetRangeEnd()); 88 } 89 } 90 } else 91 debug_map_sym_file->AddOSOARanges(&m_dwarf, debug_aranges); 92 } 93 } 94 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. 98 SymbolContext sc; 99 sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(*this); 100 if (sc.comp_unit) { 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()); 111 } 112 } 113 } 114 } 115 } 116