1 //===-- DWARFContext.cpp --------------------------------------------------===// 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 "DWARFContext.h" 10 11 #include "lldb/Core/Section.h" 12 13 using namespace lldb; 14 using namespace lldb_private; 15 16 static DWARFDataExtractor LoadSection(SectionList *section_list, 17 SectionType section_type) { 18 if (!section_list) 19 return DWARFDataExtractor(); 20 21 auto section_sp = section_list->FindSectionByType(section_type, true); 22 if (!section_sp) 23 return DWARFDataExtractor(); 24 25 DWARFDataExtractor data; 26 section_sp->GetSectionData(data); 27 return data; 28 } 29 30 const DWARFDataExtractor & 31 DWARFContext::LoadOrGetSection(llvm::Optional<SectionType> main_section_type, 32 llvm::Optional<SectionType> dwo_section_type, 33 SectionData &data) { 34 llvm::call_once(data.flag, [&] { 35 if (dwo_section_type && isDwo()) 36 data.data = LoadSection(m_dwo_section_list, *dwo_section_type); 37 else if (main_section_type) 38 data.data = LoadSection(m_main_section_list, *main_section_type); 39 }); 40 return data.data; 41 } 42 43 const DWARFDataExtractor &DWARFContext::getOrLoadCuIndexData() { 44 return LoadOrGetSection(llvm::None, eSectionTypeDWARFDebugCuIndex, 45 m_data_debug_cu_index); 46 } 47 48 const DWARFDataExtractor &DWARFContext::getOrLoadAbbrevData() { 49 return LoadOrGetSection(eSectionTypeDWARFDebugAbbrev, 50 eSectionTypeDWARFDebugAbbrevDwo, m_data_debug_abbrev); 51 } 52 53 const DWARFDataExtractor &DWARFContext::getOrLoadArangesData() { 54 return LoadOrGetSection(eSectionTypeDWARFDebugAranges, llvm::None, 55 m_data_debug_aranges); 56 } 57 58 const DWARFDataExtractor &DWARFContext::getOrLoadAddrData() { 59 return LoadOrGetSection(eSectionTypeDWARFDebugAddr, llvm::None, 60 m_data_debug_addr); 61 } 62 63 const DWARFDataExtractor &DWARFContext::getOrLoadDebugInfoData() { 64 return LoadOrGetSection(eSectionTypeDWARFDebugInfo, 65 eSectionTypeDWARFDebugInfoDwo, m_data_debug_info); 66 } 67 68 const DWARFDataExtractor &DWARFContext::getOrLoadLineData() { 69 return LoadOrGetSection(eSectionTypeDWARFDebugLine, llvm::None, 70 m_data_debug_line); 71 } 72 73 const DWARFDataExtractor &DWARFContext::getOrLoadLineStrData() { 74 return LoadOrGetSection(eSectionTypeDWARFDebugLineStr, llvm::None, 75 m_data_debug_line_str); 76 } 77 78 const DWARFDataExtractor &DWARFContext::getOrLoadLocData() { 79 return LoadOrGetSection(eSectionTypeDWARFDebugLoc, 80 eSectionTypeDWARFDebugLocDwo, m_data_debug_loc); 81 } 82 83 const DWARFDataExtractor &DWARFContext::getOrLoadLocListsData() { 84 return LoadOrGetSection(eSectionTypeDWARFDebugLocLists, 85 eSectionTypeDWARFDebugLocListsDwo, 86 m_data_debug_loclists); 87 } 88 89 const DWARFDataExtractor &DWARFContext::getOrLoadMacroData() { 90 return LoadOrGetSection(eSectionTypeDWARFDebugMacro, llvm::None, 91 m_data_debug_macro); 92 } 93 94 const DWARFDataExtractor &DWARFContext::getOrLoadRangesData() { 95 return LoadOrGetSection(eSectionTypeDWARFDebugRanges, llvm::None, 96 m_data_debug_ranges); 97 } 98 99 const DWARFDataExtractor &DWARFContext::getOrLoadRngListsData() { 100 return LoadOrGetSection(eSectionTypeDWARFDebugRngLists, 101 eSectionTypeDWARFDebugRngListsDwo, 102 m_data_debug_rnglists); 103 } 104 105 const DWARFDataExtractor &DWARFContext::getOrLoadStrData() { 106 return LoadOrGetSection(eSectionTypeDWARFDebugStr, 107 eSectionTypeDWARFDebugStrDwo, m_data_debug_str); 108 } 109 110 const DWARFDataExtractor &DWARFContext::getOrLoadStrOffsetsData() { 111 return LoadOrGetSection(eSectionTypeDWARFDebugStrOffsets, 112 eSectionTypeDWARFDebugStrOffsetsDwo, 113 m_data_debug_str_offsets); 114 } 115 116 const DWARFDataExtractor &DWARFContext::getOrLoadDebugTypesData() { 117 return LoadOrGetSection(eSectionTypeDWARFDebugTypes, 118 eSectionTypeDWARFDebugTypesDwo, m_data_debug_types); 119 } 120 121 llvm::DWARFContext &DWARFContext::GetAsLLVM() { 122 if (!m_llvm_context) { 123 llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> section_map; 124 uint8_t addr_size = 0; 125 auto AddSection = [&](llvm::StringRef name, DWARFDataExtractor data) { 126 // Set the address size the first time we see it. 127 if (addr_size == 0) 128 addr_size = data.GetAddressByteSize(); 129 130 section_map.try_emplace( 131 name, llvm::MemoryBuffer::getMemBuffer(toStringRef(data.GetData()), 132 name, false)); 133 }; 134 135 AddSection("debug_line_str", getOrLoadLineStrData()); 136 AddSection("debug_cu_index", getOrLoadCuIndexData()); 137 138 m_llvm_context = llvm::DWARFContext::create(section_map, addr_size); 139 } 140 return *m_llvm_context; 141 } 142