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;
19
20void 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
28void 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 contains ranges information.
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, /*check_hi_lo_pc=*/true);
44    if (num_ranges > 0) {
45      for (size_t i = 0; i < num_ranges; ++i) {
46        const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
47        debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
48                                   range.GetRangeEnd());
49      }
50
51      return;
52    }
53  }
54
55  if (debug_aranges->GetNumRanges() == num_debug_aranges) {
56    // We got nothing from the debug info, maybe we have a line tables only
57    // situation. Check the line tables and build the arange table from this.
58    SymbolContext sc;
59    sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(*this);
60    if (sc.comp_unit) {
61      SymbolFileDWARFDebugMap *debug_map_sym_file =
62          m_dwarf.GetDebugMapSymfile();
63      if (debug_map_sym_file == nullptr) {
64        if (LineTable *line_table = sc.comp_unit->GetLineTable()) {
65          LineTable::FileAddressRanges file_ranges;
66          const bool append = true;
67          const size_t num_ranges =
68              line_table->GetContiguousFileAddressRanges(file_ranges, append);
69          for (uint32_t idx = 0; idx < num_ranges; ++idx) {
70            const LineTable::FileAddressRanges::Entry &range =
71                file_ranges.GetEntryRef(idx);
72            debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
73                                       range.GetRangeEnd());
74          }
75        }
76      } else
77        debug_map_sym_file->AddOSOARanges(&m_dwarf, debug_aranges);
78    }
79  }
80
81  if (debug_aranges->GetNumRanges() == num_debug_aranges) {
82    // We got nothing from the functions, maybe we have a line tables only
83    // situation. Check the line tables and build the arange table from this.
84    SymbolContext sc;
85    sc.comp_unit = m_dwarf.GetCompUnitForDWARFCompUnit(*this);
86    if (sc.comp_unit) {
87      if (LineTable *line_table = sc.comp_unit->GetLineTable()) {
88        LineTable::FileAddressRanges file_ranges;
89        const bool append = true;
90        const size_t num_ranges =
91            line_table->GetContiguousFileAddressRanges(file_ranges, append);
92        for (uint32_t idx = 0; idx < num_ranges; ++idx) {
93          const LineTable::FileAddressRanges::Entry &range =
94              file_ranges.GetEntryRef(idx);
95          debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(),
96                                     range.GetRangeEnd());
97        }
98      }
99    }
100  }
101}
102