LLDB  mainline
DWARFCompileUnit.cpp
Go to the documentation of this file.
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 =
56  debug_info.ValidOffset(cu_sp->GetNextCompileUnitOffset() - 1);
57  bool version_OK = SymbolFileDWARF::SupportedVersion(cu_sp->m_version);
58  bool abbr_offset_OK =
59  dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
60  bool addr_size_OK = (cu_sp->m_addr_size == 4) || (cu_sp->m_addr_size == 8);
61 
62  if (!length_OK)
63  return llvm::make_error<llvm::object::GenericBinaryError>(
64  "Invalid compile unit length");
65  if (!version_OK)
66  return llvm::make_error<llvm::object::GenericBinaryError>(
67  "Unsupported compile unit version");
68  if (!abbr_offset_OK)
69  return llvm::make_error<llvm::object::GenericBinaryError>(
70  "Abbreviation offset for compile unit is not valid");
71  if (!addr_size_OK)
72  return llvm::make_error<llvm::object::GenericBinaryError>(
73  "Invalid compile unit address size");
74 
75  cu_sp->m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
76  if (!cu_sp->m_abbrevs)
77  return llvm::make_error<llvm::object::GenericBinaryError>(
78  "No abbrev exists at the specified offset.");
79 
80  return cu_sp;
81 }
82 
84  s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, "
85  "abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at "
86  "{0x%8.8x})\n",
87  m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size,
88  GetNextCompileUnitOffset());
89 }
90 
92  if (m_version < 5)
93  return 11;
94 
95  switch (m_unit_type) {
96  case llvm::dwarf::DW_UT_compile:
97  case llvm::dwarf::DW_UT_partial:
98  return 12;
99  case llvm::dwarf::DW_UT_skeleton:
100  case llvm::dwarf::DW_UT_split_compile:
101  return 20;
102  case llvm::dwarf::DW_UT_type:
103  case llvm::dwarf::DW_UT_split_type:
104  return 24;
105  }
106  llvm_unreachable("invalid UnitType.");
107 }
108 
110  return m_dwarf->get_debug_info_data();
111 }
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
static bool SupportedVersion(uint16_t version)
uint32_t GetHeaderByteSize() const override
Get the size in bytes of the header.
uint64_t GetDWARFInitialLength(lldb::offset_t *offset_ptr) const
const lldb_private::DWARFDataExtractor & GetData() const override
Get the data that contains the DIE information for this unit.
uint64_t user_id_t
Definition: lldb-types.h:84
uint64_t offset_t
Definition: lldb-types.h:87
virtual const lldb_private::DWARFDataExtractor & get_debug_abbrev_data()
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
const DWARFAbbreviationDeclarationSet * GetAbbreviationDeclarationSet(dw_offset_t cu_abbr_offset) const
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
DWARFDebugAbbrev * DebugAbbrev()
Definition: SBAddress.h:15
dw_offset_t GetDWARFOffset(lldb::offset_t *offset_ptr) const
void Dump(lldb_private::Stream *s) const override
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
static llvm::Expected< DWARFUnitSP > extract(SymbolFileDWARF *dwarf2Data, lldb::user_id_t uid, const lldb_private::DWARFDataExtractor &debug_info, lldb::offset_t *offset_ptr)
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.