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