1 //===-- DWARFContext.cpp ----------------------------------------*- C++ -*-===//
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(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);
38 data.data = LoadSection(m_main_section_list, main_section_type);
43 const DWARFDataExtractor &DWARFContext::getOrLoadAbbrevData() {
44 return LoadOrGetSection(eSectionTypeDWARFDebugAbbrev,
45 eSectionTypeDWARFDebugAbbrevDwo, m_data_debug_abbrev);
48 const DWARFDataExtractor &DWARFContext::getOrLoadArangesData() {
49 return LoadOrGetSection(eSectionTypeDWARFDebugAranges, llvm::None,
50 m_data_debug_aranges);
53 const DWARFDataExtractor &DWARFContext::getOrLoadAddrData() {
54 return LoadOrGetSection(eSectionTypeDWARFDebugAddr, llvm::None,
58 const DWARFDataExtractor &DWARFContext::getOrLoadDebugInfoData() {
59 return LoadOrGetSection(eSectionTypeDWARFDebugInfo,
60 eSectionTypeDWARFDebugInfoDwo, m_data_debug_info);
63 const DWARFDataExtractor &DWARFContext::getOrLoadLineData() {
64 return LoadOrGetSection(eSectionTypeDWARFDebugLine, llvm::None,
68 const DWARFDataExtractor &DWARFContext::getOrLoadLineStrData() {
69 return LoadOrGetSection(eSectionTypeDWARFDebugLineStr, llvm::None,
70 m_data_debug_line_str);
73 const DWARFDataExtractor &DWARFContext::getOrLoadLocData() {
74 return LoadOrGetSection(eSectionTypeDWARFDebugLoc,
75 eSectionTypeDWARFDebugLocDwo, m_data_debug_loc);
78 const DWARFDataExtractor &DWARFContext::getOrLoadLocListsData() {
79 return LoadOrGetSection(eSectionTypeDWARFDebugLocLists,
80 eSectionTypeDWARFDebugLocListsDwo,
81 m_data_debug_loclists);
84 const DWARFDataExtractor &DWARFContext::getOrLoadMacroData() {
85 return LoadOrGetSection(eSectionTypeDWARFDebugMacro, llvm::None,
89 const DWARFDataExtractor &DWARFContext::getOrLoadRangesData() {
90 return LoadOrGetSection(eSectionTypeDWARFDebugRanges, llvm::None,
94 const DWARFDataExtractor &DWARFContext::getOrLoadRngListsData() {
95 return LoadOrGetSection(eSectionTypeDWARFDebugRngLists,
96 eSectionTypeDWARFDebugRngListsDwo,
97 m_data_debug_rnglists);
100 const DWARFDataExtractor &DWARFContext::getOrLoadStrData() {
101 return LoadOrGetSection(eSectionTypeDWARFDebugStr,
102 eSectionTypeDWARFDebugStrDwo, m_data_debug_str);
105 const DWARFDataExtractor &DWARFContext::getOrLoadStrOffsetsData() {
106 return LoadOrGetSection(eSectionTypeDWARFDebugStrOffsets,
107 eSectionTypeDWARFDebugStrOffsetsDwo,
108 m_data_debug_str_offsets);
111 const DWARFDataExtractor &DWARFContext::getOrLoadDebugTypesData() {
112 return LoadOrGetSection(eSectionTypeDWARFDebugTypes,
113 eSectionTypeDWARFDebugTypesDwo, m_data_debug_types);
116 llvm::DWARFContext &DWARFContext::GetAsLLVM() {
117 if (!m_llvm_context) {
118 llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> section_map;
119 uint8_t addr_size = 0;
121 auto AddSection = [&](Section §ion) {
122 DataExtractor section_data;
123 section.GetSectionData(section_data);
125 // Set the address size the first time we see it.
127 addr_size = section_data.GetByteSize();
129 llvm::StringRef data = llvm::toStringRef(section_data.GetData());
130 llvm::StringRef name = section.GetName().GetStringRef();
131 if (name.startswith("."))
132 name = name.drop_front();
133 section_map.try_emplace(
134 name, llvm::MemoryBuffer::getMemBuffer(data, name, false));
137 if (m_main_section_list) {
138 for (auto §ion : *m_main_section_list)
139 AddSection(*section);
142 if (m_dwo_section_list) {
143 for (auto §ion : *m_dwo_section_list)
144 AddSection(*section);
147 m_llvm_context = llvm::DWARFContext::create(section_map, addr_size);
149 return *m_llvm_context;