LLDB  mainline
SymbolFileDWARFDwp.cpp
Go to the documentation of this file.
1 //===-- SymbolFileDWARFDwp.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 "SymbolFileDWARFDwp.h"
10 
11 #include "lldb/Core/Section.h"
12 #include "lldb/Symbol/ObjectFile.h"
13 
14 #include "SymbolFileDWARFDwoDwp.h"
15 
16 static llvm::DWARFSectionKind
18  switch (type) {
20  return llvm::DW_SECT_INFO;
21  // case lldb::eSectionTypeDWARFDebugTypes:
22  // return llvm::DW_SECT_TYPES;
24  return llvm::DW_SECT_ABBREV;
26  return llvm::DW_SECT_LINE;
28  return llvm::DW_SECT_LOC;
30  return llvm::DW_SECT_STR_OFFSETS;
31  // case lldb::eSectionTypeDWARFDebugMacinfo:
32  // return llvm::DW_SECT_MACINFO;
34  return llvm::DW_SECT_MACRO;
35  default:
36  // Note: 0 is an invalid dwarf section kind.
37  return llvm::DWARFSectionKind(0);
38  }
39 }
40 
41 std::unique_ptr<SymbolFileDWARFDwp>
42 SymbolFileDWARFDwp::Create(lldb::ModuleSP module_sp,
43  const lldb_private::FileSpec &file_spec) {
44  const lldb::offset_t file_offset = 0;
45  lldb::DataBufferSP file_data_sp;
46  lldb::offset_t file_data_offset = 0;
47  lldb::ObjectFileSP obj_file = lldb_private::ObjectFile::FindPlugin(
48  module_sp, &file_spec, file_offset,
49  lldb_private::FileSystem::Instance().GetByteSize(file_spec), file_data_sp,
50  file_data_offset);
51  if (obj_file == nullptr)
52  return nullptr;
53 
54  std::unique_ptr<SymbolFileDWARFDwp> dwp_symfile(
55  new SymbolFileDWARFDwp(module_sp, obj_file));
56 
57  lldb_private::DWARFDataExtractor debug_cu_index;
58  if (!dwp_symfile->LoadRawSectionData(lldb::eSectionTypeDWARFDebugCuIndex,
59  debug_cu_index))
60  return nullptr;
61 
62  llvm::DataExtractor llvm_debug_cu_index(
63  llvm::StringRef(debug_cu_index.PeekCStr(0), debug_cu_index.GetByteSize()),
64  debug_cu_index.GetByteOrder() == lldb::eByteOrderLittle,
65  debug_cu_index.GetAddressByteSize());
66  if (!dwp_symfile->m_debug_cu_index.parse(llvm_debug_cu_index))
67  return nullptr;
68  dwp_symfile->InitDebugCUIndexMap();
69  return dwp_symfile;
70 }
71 
72 void SymbolFileDWARFDwp::InitDebugCUIndexMap() {
73  m_debug_cu_index_map.clear();
74  for (const auto &entry : m_debug_cu_index.getRows())
75  m_debug_cu_index_map.emplace(entry.getSignature(), &entry);
76 }
77 
78 SymbolFileDWARFDwp::SymbolFileDWARFDwp(lldb::ModuleSP module_sp,
79  lldb::ObjectFileSP obj_file)
80  : m_obj_file(std::move(obj_file)), m_debug_cu_index(llvm::DW_SECT_INFO)
81 {}
82 
83 std::unique_ptr<SymbolFileDWARFDwo>
85  uint64_t dwo_id) {
86  return std::unique_ptr<SymbolFileDWARFDwo>(
87  new SymbolFileDWARFDwoDwp(this, m_obj_file, dwarf_cu, dwo_id));
88 }
89 
91  uint64_t dwo_id, lldb::SectionType sect_type,
94  if (!LoadRawSectionData(sect_type, section_data))
95  return false;
96 
97  auto it = m_debug_cu_index_map.find(dwo_id);
98  if (it == m_debug_cu_index_map.end())
99  return false;
100 
101  auto *offsets =
102  it->second->getOffset(lldbSectTypeToLlvmSectionKind(sect_type));
103  if (offsets) {
104  data.SetData(section_data, offsets->Offset, offsets->Length);
105  } else {
106  data.SetData(section_data, 0, section_data.GetByteSize());
107  }
108  return true;
109 }
110 
111 bool SymbolFileDWARFDwp::LoadRawSectionData(
113  std::lock_guard<std::mutex> lock(m_sections_mutex);
114 
115  auto it = m_sections.find(sect_type);
116  if (it != m_sections.end()) {
117  if (it->second.GetByteSize() == 0)
118  return false;
119 
120  data = it->second;
121  return true;
122  }
123 
124  const lldb_private::SectionList *section_list =
125  m_obj_file->GetSectionList(false /* update_module_section_list */);
126  if (section_list) {
127  lldb::SectionSP section_sp(
128  section_list->FindSectionByType(sect_type, true));
129  if (section_sp) {
130  if (m_obj_file->ReadSectionData(section_sp.get(), data) != 0) {
131  m_sections[sect_type] = data;
132  return true;
133  }
134  }
135  }
136  m_sections[sect_type].Clear();
137  return false;
138 }
A file utility class.
Definition: FileSpec.h:55
const char * PeekCStr(lldb::offset_t offset) const
Peek at a C string at offset.
lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, bool check_children, size_t start_idx=0) const
Definition: Section.cpp:545
static FileSystem & Instance()
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
uint64_t offset_t
Definition: lldb-types.h:87
static llvm::DWARFSectionKind lldbSectTypeToLlvmSectionKind(lldb::SectionType type)
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
bool LoadSectionData(uint64_t dwo_id, lldb::SectionType sect_type, lldb_private::DWARFDataExtractor &data)
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
std::unique_ptr< SymbolFileDWARFDwo > GetSymbolFileForDwoId(DWARFUnit *dwarf_cu, uint64_t dwo_id)
static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataBufferSP &data_sp, lldb::offset_t &data_offset)
Find a ObjectFile plug-in that can parse file_spec.
static std::unique_ptr< SymbolFileDWARFDwp > Create(lldb::ModuleSP module_sp, const lldb_private::FileSpec &file_spec)
uint32_t GetAddressByteSize() const
Get the current address size.