1 //===-- DWARFContext.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 "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 static const DWARFDataExtractor &
31 LoadOrGetSection(SectionList *section_list, SectionType section_type,
32                  llvm::Optional<DWARFDataExtractor> &extractor) {
33   if (!extractor)
34     extractor = LoadSection(section_list, section_type);
35   return *extractor;
36 }
37 
38 const DWARFDataExtractor &DWARFContext::getOrLoadAbbrevData() {
39   if (isDwo())
40     return LoadOrGetSection(m_dwo_section_list, eSectionTypeDWARFDebugAbbrevDwo,
41                             m_data_debug_abbrev);
42   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugAbbrev,
43                           m_data_debug_abbrev);
44 }
45 
46 const DWARFDataExtractor &DWARFContext::getOrLoadArangesData() {
47   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugAranges,
48                           m_data_debug_aranges);
49 }
50 
51 const DWARFDataExtractor &DWARFContext::getOrLoadAddrData() {
52   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugAddr,
53                           m_data_debug_addr);
54 }
55 
56 const DWARFDataExtractor &DWARFContext::getOrLoadDebugInfoData() {
57   if (isDwo())
58     return LoadOrGetSection(m_dwo_section_list, eSectionTypeDWARFDebugInfoDwo,
59                             m_data_debug_info);
60   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugInfo,
61                           m_data_debug_info);
62 }
63 
64 const DWARFDataExtractor &DWARFContext::getOrLoadLineData() {
65   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugLine,
66                           m_data_debug_line);
67 }
68 
69 const DWARFDataExtractor &DWARFContext::getOrLoadLineStrData() {
70   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugLineStr,
71                           m_data_debug_line_str);
72 }
73 
74 const DWARFDataExtractor &DWARFContext::getOrLoadMacroData() {
75   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugMacro,
76                           m_data_debug_macro);
77 }
78 
79 const DWARFDataExtractor &DWARFContext::getOrLoadStrData() {
80   if (isDwo())
81     return LoadOrGetSection(m_dwo_section_list, eSectionTypeDWARFDebugStrDwo,
82                             m_data_debug_str);
83   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugStr,
84                           m_data_debug_str);
85 }
86 
87 const DWARFDataExtractor &DWARFContext::getOrLoadStrOffsetsData() {
88   if (isDwo())
89     return LoadOrGetSection(m_dwo_section_list, eSectionTypeDWARFDebugStrOffsetsDwo,
90                             m_data_debug_str_offsets);
91   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugStrOffsets,
92                           m_data_debug_str_offsets);
93 }
94 
95 const DWARFDataExtractor &DWARFContext::getOrLoadDebugTypesData() {
96   return LoadOrGetSection(m_main_section_list, eSectionTypeDWARFDebugTypes,
97                           m_data_debug_types);
98 }
99