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 
11 #include "SymbolFileDWARF.h"
12 #include "lldb/Utility/Stream.h"
13 #include "llvm/Object/Error.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF *dwarf2Data,
19                                    lldb::user_id_t uid)
20     : DWARFUnit(dwarf2Data, uid) {}
21 
22 
23 llvm::Expected<DWARFUnitSP> DWARFCompileUnit::extract(
24     SymbolFileDWARF *dwarf2Data, user_id_t uid,
25     const DWARFDataExtractor &debug_info, lldb::offset_t *offset_ptr) {
26   assert(debug_info.ValidOffset(*offset_ptr));
27 
28   // std::make_shared would require the ctor to be public.
29   std::shared_ptr<DWARFCompileUnit> cu_sp(
30       new DWARFCompileUnit(dwarf2Data, uid));
31 
32   cu_sp->m_offset = *offset_ptr;
33 
34   dw_offset_t abbr_offset;
35   const DWARFDebugAbbrev *abbr = dwarf2Data->DebugAbbrev();
36   if (!abbr)
37     return llvm::make_error<llvm::object::GenericBinaryError>(
38         "No debug_abbrev data");
39 
40   cu_sp->m_length = debug_info.GetDWARFInitialLength(offset_ptr);
41   cu_sp->m_version = debug_info.GetU16(offset_ptr);
42 
43   if (cu_sp->m_version == 5) {
44     cu_sp->m_unit_type = debug_info.GetU8(offset_ptr);
45     cu_sp->m_addr_size = debug_info.GetU8(offset_ptr);
46     abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
47 
48     if (cu_sp->m_unit_type == llvm::dwarf::DW_UT_skeleton)
49       cu_sp->m_dwo_id = debug_info.GetU64(offset_ptr);
50   } else {
51     abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
52     cu_sp->m_addr_size = debug_info.GetU8(offset_ptr);
53   }
54 
55   bool length_OK = debug_info.ValidOffset(cu_sp->GetNextUnitOffset() - 1);
56   bool version_OK = SymbolFileDWARF::SupportedVersion(cu_sp->m_version);
57   bool abbr_offset_OK =
58       dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
59   bool addr_size_OK = (cu_sp->m_addr_size == 4) || (cu_sp->m_addr_size == 8);
60 
61   if (!length_OK)
62     return llvm::make_error<llvm::object::GenericBinaryError>(
63         "Invalid compile unit length");
64   if (!version_OK)
65     return llvm::make_error<llvm::object::GenericBinaryError>(
66         "Unsupported compile unit version");
67   if (!abbr_offset_OK)
68     return llvm::make_error<llvm::object::GenericBinaryError>(
69         "Abbreviation offset for compile unit is not valid");
70   if (!addr_size_OK)
71     return llvm::make_error<llvm::object::GenericBinaryError>(
72         "Invalid compile unit address size");
73 
74   cu_sp->m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
75   if (!cu_sp->m_abbrevs)
76     return llvm::make_error<llvm::object::GenericBinaryError>(
77         "No abbrev exists at the specified offset.");
78 
79   return cu_sp;
80 }
81 
82 void DWARFCompileUnit::Dump(Stream *s) const {
83   s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, "
84             "abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at "
85             "{0x%8.8x})\n",
86             m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size,
87             GetNextUnitOffset());
88 }
89 
90 uint32_t DWARFCompileUnit::GetHeaderByteSize() const {
91   if (m_version < 5)
92     return 11;
93 
94   switch (m_unit_type) {
95   case llvm::dwarf::DW_UT_compile:
96   case llvm::dwarf::DW_UT_partial:
97     return 12;
98   case llvm::dwarf::DW_UT_skeleton:
99   case llvm::dwarf::DW_UT_split_compile:
100     return 20;
101   case llvm::dwarf::DW_UT_type:
102   case llvm::dwarf::DW_UT_split_type:
103     return 24;
104   }
105   llvm_unreachable("invalid UnitType.");
106 }
107 
108 const lldb_private::DWARFDataExtractor &DWARFCompileUnit::GetData() const {
109   return m_dwarf->get_debug_info_data();
110 }
111