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 83 void DWARFCompileUnit::Dump(Stream *s) const { 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 91 uint32_t DWARFCompileUnit::GetHeaderByteSize() const { 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 109 const lldb_private::DWARFDataExtractor &DWARFCompileUnit::GetData() const { 110 return m_dwarf->get_debug_info_data(); 111 } 112