Path: blob/main/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
39645 views
//===-- DWARFContext.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "DWARFContext.h"910#include "lldb/Core/Section.h"11#include <optional>1213using namespace lldb;14using namespace lldb_private;15using namespace lldb_private::plugin::dwarf;1617static DWARFDataExtractor LoadSection(SectionList *section_list,18SectionType section_type) {19if (!section_list)20return DWARFDataExtractor();2122auto section_sp = section_list->FindSectionByType(section_type, true);23if (!section_sp)24return DWARFDataExtractor();2526DWARFDataExtractor data;27section_sp->GetSectionData(data);28return data;29}3031const DWARFDataExtractor &32DWARFContext::LoadOrGetSection(std::optional<SectionType> main_section_type,33std::optional<SectionType> dwo_section_type,34SectionData &data) {35llvm::call_once(data.flag, [&] {36if (dwo_section_type && isDwo())37data.data = LoadSection(m_dwo_section_list, *dwo_section_type);38else if (main_section_type)39data.data = LoadSection(m_main_section_list, *main_section_type);40});41return data.data;42}4344const DWARFDataExtractor &DWARFContext::getOrLoadCuIndexData() {45return LoadOrGetSection(std::nullopt, eSectionTypeDWARFDebugCuIndex,46m_data_debug_cu_index);47}4849const DWARFDataExtractor &DWARFContext::getOrLoadTuIndexData() {50return LoadOrGetSection(std::nullopt, eSectionTypeDWARFDebugTuIndex,51m_data_debug_tu_index);52}5354const DWARFDataExtractor &DWARFContext::getOrLoadAbbrevData() {55return LoadOrGetSection(eSectionTypeDWARFDebugAbbrev,56eSectionTypeDWARFDebugAbbrevDwo, m_data_debug_abbrev);57}5859const DWARFDataExtractor &DWARFContext::getOrLoadArangesData() {60return LoadOrGetSection(eSectionTypeDWARFDebugAranges, std::nullopt,61m_data_debug_aranges);62}6364const DWARFDataExtractor &DWARFContext::getOrLoadAddrData() {65return LoadOrGetSection(eSectionTypeDWARFDebugAddr, std::nullopt,66m_data_debug_addr);67}6869const DWARFDataExtractor &DWARFContext::getOrLoadDebugInfoData() {70return LoadOrGetSection(eSectionTypeDWARFDebugInfo,71eSectionTypeDWARFDebugInfoDwo, m_data_debug_info);72}7374const DWARFDataExtractor &DWARFContext::getOrLoadLineData() {75return LoadOrGetSection(eSectionTypeDWARFDebugLine, std::nullopt,76m_data_debug_line);77}7879const DWARFDataExtractor &DWARFContext::getOrLoadLineStrData() {80return LoadOrGetSection(eSectionTypeDWARFDebugLineStr, std::nullopt,81m_data_debug_line_str);82}8384const DWARFDataExtractor &DWARFContext::getOrLoadLocData() {85return LoadOrGetSection(eSectionTypeDWARFDebugLoc,86eSectionTypeDWARFDebugLocDwo, m_data_debug_loc);87}8889const DWARFDataExtractor &DWARFContext::getOrLoadLocListsData() {90return LoadOrGetSection(eSectionTypeDWARFDebugLocLists,91eSectionTypeDWARFDebugLocListsDwo,92m_data_debug_loclists);93}9495const DWARFDataExtractor &DWARFContext::getOrLoadMacroData() {96return LoadOrGetSection(eSectionTypeDWARFDebugMacro, std::nullopt,97m_data_debug_macro);98}99100const DWARFDataExtractor &DWARFContext::getOrLoadRangesData() {101return LoadOrGetSection(eSectionTypeDWARFDebugRanges, std::nullopt,102m_data_debug_ranges);103}104105const DWARFDataExtractor &DWARFContext::getOrLoadRngListsData() {106return LoadOrGetSection(eSectionTypeDWARFDebugRngLists,107eSectionTypeDWARFDebugRngListsDwo,108m_data_debug_rnglists);109}110111const DWARFDataExtractor &DWARFContext::getOrLoadStrData() {112return LoadOrGetSection(eSectionTypeDWARFDebugStr,113eSectionTypeDWARFDebugStrDwo, m_data_debug_str);114}115116const DWARFDataExtractor &DWARFContext::getOrLoadStrOffsetsData() {117return LoadOrGetSection(eSectionTypeDWARFDebugStrOffsets,118eSectionTypeDWARFDebugStrOffsetsDwo,119m_data_debug_str_offsets);120}121122const DWARFDataExtractor &DWARFContext::getOrLoadDebugTypesData() {123return LoadOrGetSection(eSectionTypeDWARFDebugTypes,124eSectionTypeDWARFDebugTypesDwo, m_data_debug_types);125}126127llvm::DWARFContext &DWARFContext::GetAsLLVM() {128if (!m_llvm_context) {129llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> section_map;130uint8_t addr_size = 0;131auto AddSection = [&](llvm::StringRef name, DWARFDataExtractor data) {132// Set the address size the first time we see it.133if (addr_size == 0)134addr_size = data.GetAddressByteSize();135136section_map.try_emplace(137name, llvm::MemoryBuffer::getMemBuffer(toStringRef(data.GetData()),138name, false));139};140141AddSection("debug_line_str", getOrLoadLineStrData());142AddSection("debug_cu_index", getOrLoadCuIndexData());143AddSection("debug_tu_index", getOrLoadTuIndexData());144if (isDwo()) {145AddSection("debug_info.dwo", getOrLoadDebugInfoData());146AddSection("debug_types.dwo", getOrLoadDebugTypesData());147}148m_llvm_context = llvm::DWARFContext::create(section_map, addr_size);149}150return *m_llvm_context;151}152153154