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 14 using namespace lldb; 15 using namespace lldb_private; 16 17 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF *dwarf2Data) 18 : DWARFUnit(dwarf2Data) {} 19 20 DWARFUnitSP DWARFCompileUnit::Extract(SymbolFileDWARF *dwarf2Data, 21 const DWARFDataExtractor &debug_info, 22 lldb::offset_t *offset_ptr) { 23 // std::make_shared would require the ctor to be public. 24 std::shared_ptr<DWARFCompileUnit> cu_sp(new DWARFCompileUnit(dwarf2Data)); 25 26 cu_sp->m_offset = *offset_ptr; 27 28 if (debug_info.ValidOffset(*offset_ptr)) { 29 dw_offset_t abbr_offset; 30 const DWARFDebugAbbrev *abbr = dwarf2Data->DebugAbbrev(); 31 cu_sp->m_length = debug_info.GetDWARFInitialLength(offset_ptr); 32 cu_sp->m_version = debug_info.GetU16(offset_ptr); 33 34 if (cu_sp->m_version == 5) { 35 cu_sp->m_unit_type = debug_info.GetU8(offset_ptr); 36 cu_sp->m_addr_size = debug_info.GetU8(offset_ptr); 37 abbr_offset = debug_info.GetDWARFOffset(offset_ptr); 38 39 if (cu_sp->m_unit_type == llvm::dwarf::DW_UT_skeleton) 40 cu_sp->m_dwo_id = debug_info.GetU64(offset_ptr); 41 } else { 42 abbr_offset = debug_info.GetDWARFOffset(offset_ptr); 43 cu_sp->m_addr_size = debug_info.GetU8(offset_ptr); 44 } 45 46 bool length_OK = 47 debug_info.ValidOffset(cu_sp->GetNextCompileUnitOffset() - 1); 48 bool version_OK = SymbolFileDWARF::SupportedVersion(cu_sp->m_version); 49 bool abbr_offset_OK = 50 dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset); 51 bool addr_size_OK = (cu_sp->m_addr_size == 4) || (cu_sp->m_addr_size == 8); 52 53 if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && 54 abbr != NULL) { 55 cu_sp->m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset); 56 return cu_sp; 57 } 58 59 // reset the offset to where we tried to parse from if anything went wrong 60 *offset_ptr = cu_sp->m_offset; 61 } 62 63 return nullptr; 64 } 65 66 void DWARFCompileUnit::Dump(Stream *s) const { 67 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, " 68 "abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at " 69 "{0x%8.8x})\n", 70 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, 71 GetNextCompileUnitOffset()); 72 } 73 74 uint32_t DWARFCompileUnit::GetHeaderByteSize() const { 75 if (m_version < 5) 76 return 11; 77 78 switch (m_unit_type) { 79 case llvm::dwarf::DW_UT_compile: 80 case llvm::dwarf::DW_UT_partial: 81 return 12; 82 case llvm::dwarf::DW_UT_skeleton: 83 case llvm::dwarf::DW_UT_split_compile: 84 return 20; 85 case llvm::dwarf::DW_UT_type: 86 case llvm::dwarf::DW_UT_split_type: 87 return 24; 88 } 89 llvm_unreachable("invalid UnitType."); 90 } 91 92 const lldb_private::DWARFDataExtractor &DWARFCompileUnit::GetData() const { 93 return m_dwarf->get_debug_info_data(); 94 } 95