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 const DWARFDataExtractor *
17 LoadOrGetSection(Module &module, SectionType section_type,
18                  llvm::Optional<DWARFDataExtractor> &extractor) {
19   if (extractor.hasValue())
20     return extractor->GetByteSize() > 0 ? extractor.getPointer() : nullptr;
21 
22   // Initialize to an empty extractor so that we always take the fast path going
23   // forward.
24   extractor.emplace();
25 
26   const SectionList *section_list = module.GetSectionList();
27   if (!section_list)
28     return nullptr;
29 
30   auto section_sp = section_list->FindSectionByType(section_type, true);
31   if (!section_sp)
32     return nullptr;
33 
34   section_sp->GetSectionData(*extractor);
35   return extractor.getPointer();
36 }
37 
38 DWARFContext::DWARFContext(Module &module) : m_module(module) {}
39 
40 const DWARFDataExtractor *DWARFContext::getOrLoadArangesData() {
41   return LoadOrGetSection(m_module, eSectionTypeDWARFDebugAranges,
42                           m_data_debug_aranges);
43 }
44