1 //===-- DWARFContext.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "DWARFContext.h"
11 #include "lldb/Core/Section.h"
14 using namespace lldb_private;
16 static DWARFDataExtractor LoadSection(SectionList *section_list,
17 SectionType section_type) {
19 return DWARFDataExtractor();
21 auto section_sp = section_list->FindSectionByType(section_type, true);
23 return DWARFDataExtractor();
25 DWARFDataExtractor data;
26 section_sp->GetSectionData(data);
30 const DWARFDataExtractor &
31 DWARFContext::LoadOrGetSection(llvm::Optional<SectionType> main_section_type,
32 llvm::Optional<SectionType> dwo_section_type,
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);
43 const DWARFDataExtractor &DWARFContext::getOrLoadCuIndexData() {
44 return LoadOrGetSection(llvm::None, eSectionTypeDWARFDebugCuIndex,
45 m_data_debug_cu_index);
48 const DWARFDataExtractor &DWARFContext::getOrLoadTuIndexData() {
49 return LoadOrGetSection(llvm::None, eSectionTypeDWARFDebugTuIndex,
50 m_data_debug_tu_index);
53 const DWARFDataExtractor &DWARFContext::getOrLoadAbbrevData() {
54 return LoadOrGetSection(eSectionTypeDWARFDebugAbbrev,
55 eSectionTypeDWARFDebugAbbrevDwo, m_data_debug_abbrev);
58 const DWARFDataExtractor &DWARFContext::getOrLoadArangesData() {
59 return LoadOrGetSection(eSectionTypeDWARFDebugAranges, llvm::None,
60 m_data_debug_aranges);
63 const DWARFDataExtractor &DWARFContext::getOrLoadAddrData() {
64 return LoadOrGetSection(eSectionTypeDWARFDebugAddr, llvm::None,
68 const DWARFDataExtractor &DWARFContext::getOrLoadDebugInfoData() {
69 return LoadOrGetSection(eSectionTypeDWARFDebugInfo,
70 eSectionTypeDWARFDebugInfoDwo, m_data_debug_info);
73 const DWARFDataExtractor &DWARFContext::getOrLoadLineData() {
74 return LoadOrGetSection(eSectionTypeDWARFDebugLine, llvm::None,
78 const DWARFDataExtractor &DWARFContext::getOrLoadLineStrData() {
79 return LoadOrGetSection(eSectionTypeDWARFDebugLineStr, llvm::None,
80 m_data_debug_line_str);
83 const DWARFDataExtractor &DWARFContext::getOrLoadLocData() {
84 return LoadOrGetSection(eSectionTypeDWARFDebugLoc,
85 eSectionTypeDWARFDebugLocDwo, m_data_debug_loc);
88 const DWARFDataExtractor &DWARFContext::getOrLoadLocListsData() {
89 return LoadOrGetSection(eSectionTypeDWARFDebugLocLists,
90 eSectionTypeDWARFDebugLocListsDwo,
91 m_data_debug_loclists);
94 const DWARFDataExtractor &DWARFContext::getOrLoadMacroData() {
95 return LoadOrGetSection(eSectionTypeDWARFDebugMacro, llvm::None,
99 const DWARFDataExtractor &DWARFContext::getOrLoadRangesData() {
100 return LoadOrGetSection(eSectionTypeDWARFDebugRanges, llvm::None,
101 m_data_debug_ranges);
104 const DWARFDataExtractor &DWARFContext::getOrLoadRngListsData() {
105 return LoadOrGetSection(eSectionTypeDWARFDebugRngLists,
106 eSectionTypeDWARFDebugRngListsDwo,
107 m_data_debug_rnglists);
110 const DWARFDataExtractor &DWARFContext::getOrLoadStrData() {
111 return LoadOrGetSection(eSectionTypeDWARFDebugStr,
112 eSectionTypeDWARFDebugStrDwo, m_data_debug_str);
115 const DWARFDataExtractor &DWARFContext::getOrLoadStrOffsetsData() {
116 return LoadOrGetSection(eSectionTypeDWARFDebugStrOffsets,
117 eSectionTypeDWARFDebugStrOffsetsDwo,
118 m_data_debug_str_offsets);
121 const DWARFDataExtractor &DWARFContext::getOrLoadDebugTypesData() {
122 return LoadOrGetSection(eSectionTypeDWARFDebugTypes,
123 eSectionTypeDWARFDebugTypesDwo, m_data_debug_types);
126 llvm::DWARFContext &DWARFContext::GetAsLLVM() {
127 if (!m_llvm_context) {
128 llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> section_map;
129 uint8_t addr_size = 0;
130 auto AddSection = [&](llvm::StringRef name, DWARFDataExtractor data) {
131 // Set the address size the first time we see it.
133 addr_size = data.GetAddressByteSize();
135 section_map.try_emplace(
136 name, llvm::MemoryBuffer::getMemBuffer(toStringRef(data.GetData()),
140 AddSection("debug_line_str", getOrLoadLineStrData());
141 AddSection("debug_cu_index", getOrLoadCuIndexData());
142 AddSection("debug_tu_index", getOrLoadTuIndexData());
144 m_llvm_context = llvm::DWARFContext::create(section_map, addr_size);
146 return *m_llvm_context;