180814287SRaphael Isemann //===-- SymbolFileDWARFDwo.cpp --------------------------------------------===//
2eb882fc1STamas Berghammer //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eb882fc1STamas Berghammer //
7eb882fc1STamas Berghammer //===----------------------------------------------------------------------===//
8eb882fc1STamas Berghammer 
9eb882fc1STamas Berghammer #include "SymbolFileDWARFDwo.h"
10eb882fc1STamas Berghammer 
11eb882fc1STamas Berghammer #include "lldb/Core/Section.h"
121f5e4483STamas Berghammer #include "lldb/Expression/DWARFExpression.h"
13eb882fc1STamas Berghammer #include "lldb/Symbol/ObjectFile.h"
14a5d09f64SPavel Labath #include "lldb/Utility/LLDBAssert.h"
15fcad3bc4SPavel Labath #include "llvm/Support/Casting.h"
16eb882fc1STamas Berghammer 
17ad17e289SPavel Labath #include "DWARFCompileUnit.h"
18eb882fc1STamas Berghammer #include "DWARFDebugInfo.h"
19ad17e289SPavel Labath #include "DWARFUnit.h"
20eb882fc1STamas Berghammer 
21eb882fc1STamas Berghammer using namespace lldb;
22eb882fc1STamas Berghammer using namespace lldb_private;
23eb882fc1STamas Berghammer 
247d71dd92SAdrian Prantl char SymbolFileDWARFDwo::ID;
257d71dd92SAdrian Prantl 
SymbolFileDWARFDwo(SymbolFileDWARF & base_symbol_file,ObjectFileSP objfile,uint32_t id)26e3aa062aSPavel Labath SymbolFileDWARFDwo::SymbolFileDWARFDwo(SymbolFileDWARF &base_symbol_file,
27e3aa062aSPavel Labath                                        ObjectFileSP objfile, uint32_t id)
28d2deeb44SPavel Labath     : SymbolFileDWARF(objfile, objfile->GetSectionList(
29ff9b4263SPavel Labath                                    /*update_module_section_list*/ false)),
30e3aa062aSPavel Labath       m_base_symbol_file(base_symbol_file) {
31e3aa062aSPavel Labath   SetID(user_id_t(id) << 32);
32eb882fc1STamas Berghammer 
3367f63f3fSPavel Labath   // Parsing of the dwarf unit index is not thread-safe, so we need to prime it
3467f63f3fSPavel Labath   // to enable subsequent concurrent lookups.
3567f63f3fSPavel Labath   m_context.GetAsLLVM().getCUIndex();
36eb882fc1STamas Berghammer }
37eb882fc1STamas Berghammer 
GetDWOCompileUnitForHash(uint64_t hash)389dc84e9bSPavel Labath DWARFCompileUnit *SymbolFileDWARFDwo::GetDWOCompileUnitForHash(uint64_t hash) {
3967f63f3fSPavel Labath   if (const llvm::DWARFUnitIndex &index = m_context.GetAsLLVM().getCUIndex()) {
4067f63f3fSPavel Labath     if (const llvm::DWARFUnitIndex::Entry *entry = index.getFromHash(hash)) {
41f13ce15dSIgor Kudrin       if (auto *unit_contrib = entry->getContribution())
4267f63f3fSPavel Labath         return llvm::dyn_cast_or_null<DWARFCompileUnit>(
43ddf60ba0SPavel Labath             DebugInfo().GetUnitAtOffset(DIERef::Section::DebugInfo,
4467f63f3fSPavel Labath                                         unit_contrib->Offset));
4567f63f3fSPavel Labath     }
4667f63f3fSPavel Labath     return nullptr;
4767f63f3fSPavel Labath   }
4867f63f3fSPavel Labath 
499dc84e9bSPavel Labath   DWARFCompileUnit *cu = FindSingleCompileUnit();
509dc84e9bSPavel Labath   if (!cu)
519dc84e9bSPavel Labath     return nullptr;
52*a39b1982SJorge Gorbe Moya   if (hash != cu->GetDWOId())
539dc84e9bSPavel Labath     return nullptr;
549dc84e9bSPavel Labath   return cu;
55fcad3bc4SPavel Labath }
56fcad3bc4SPavel Labath 
FindSingleCompileUnit()579dc84e9bSPavel Labath DWARFCompileUnit *SymbolFileDWARFDwo::FindSingleCompileUnit() {
58ddf60ba0SPavel Labath   DWARFDebugInfo &debug_info = DebugInfo();
59fcad3bc4SPavel Labath 
60fcad3bc4SPavel Labath   // Right now we only support dwo files with one compile unit. If we don't have
61fcad3bc4SPavel Labath   // type units, we can just check for the unit count.
62ddf60ba0SPavel Labath   if (!debug_info.ContainsTypeUnits() && debug_info.GetNumUnits() == 1)
63ddf60ba0SPavel Labath     return llvm::cast<DWARFCompileUnit>(debug_info.GetUnitAtIndex(0));
64fcad3bc4SPavel Labath 
65fcad3bc4SPavel Labath   // Otherwise, we have to run through all units, and find the compile unit that
66fcad3bc4SPavel Labath   // way.
67fcad3bc4SPavel Labath   DWARFCompileUnit *cu = nullptr;
68ddf60ba0SPavel Labath   for (size_t i = 0; i < debug_info.GetNumUnits(); ++i) {
69fcad3bc4SPavel Labath     if (auto *candidate =
70ddf60ba0SPavel Labath             llvm::dyn_cast<DWARFCompileUnit>(debug_info.GetUnitAtIndex(i))) {
71fcad3bc4SPavel Labath       if (cu)
72fcad3bc4SPavel Labath         return nullptr; // More that one CU found.
73fcad3bc4SPavel Labath       cu = candidate;
74fcad3bc4SPavel Labath     }
75fcad3bc4SPavel Labath   }
76fcad3bc4SPavel Labath   return cu;
77eb882fc1STamas Berghammer }
78eb882fc1STamas Berghammer 
GetDIEToType()79b9c1b51eSKate Stone SymbolFileDWARF::DIEToTypePtr &SymbolFileDWARFDwo::GetDIEToType() {
806a2eb367SPavel Labath   return GetBaseSymbolFile().GetDIEToType();
81eb882fc1STamas Berghammer }
82eb882fc1STamas Berghammer 
GetDIEToVariable()83b9c1b51eSKate Stone SymbolFileDWARF::DIEToVariableSP &SymbolFileDWARFDwo::GetDIEToVariable() {
846a2eb367SPavel Labath   return GetBaseSymbolFile().GetDIEToVariable();
85eb882fc1STamas Berghammer }
86eb882fc1STamas Berghammer 
87eb882fc1STamas Berghammer SymbolFileDWARF::DIEToClangType &
GetForwardDeclDieToClangType()88b9c1b51eSKate Stone SymbolFileDWARFDwo::GetForwardDeclDieToClangType() {
896a2eb367SPavel Labath   return GetBaseSymbolFile().GetForwardDeclDieToClangType();
90eb882fc1STamas Berghammer }
91eb882fc1STamas Berghammer 
92eb882fc1STamas Berghammer SymbolFileDWARF::ClangTypeToDIE &
GetForwardDeclClangTypeToDie()93b9c1b51eSKate Stone SymbolFileDWARFDwo::GetForwardDeclClangTypeToDie() {
946a2eb367SPavel Labath   return GetBaseSymbolFile().GetForwardDeclClangTypeToDie();
95d536a6d9STamas Berghammer }
96d536a6d9STamas Berghammer 
GetObjCMethods(lldb_private::ConstString class_name,llvm::function_ref<bool (DWARFDIE die)> callback)975e04b5f2SJan Kratochvil void SymbolFileDWARFDwo::GetObjCMethods(
985e04b5f2SJan Kratochvil     lldb_private::ConstString class_name,
99d482fe2aSJan Kratochvil     llvm::function_ref<bool(DWARFDIE die)> callback) {
1005e04b5f2SJan Kratochvil   GetBaseSymbolFile().GetObjCMethods(class_name, callback);
101f413c785SAlexander Shaposhnikov }
102f413c785SAlexander Shaposhnikov 
GetUniqueDWARFASTTypeMap()103b9c1b51eSKate Stone UniqueDWARFASTTypeMap &SymbolFileDWARFDwo::GetUniqueDWARFASTTypeMap() {
1046a2eb367SPavel Labath   return GetBaseSymbolFile().GetUniqueDWARFASTTypeMap();
10529932b01STamas Berghammer }
10629932b01STamas Berghammer 
FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext & die_decl_ctx)107b9c1b51eSKate Stone lldb::TypeSP SymbolFileDWARFDwo::FindDefinitionTypeForDWARFDeclContext(
108b9c1b51eSKate Stone     const DWARFDeclContext &die_decl_ctx) {
1096a2eb367SPavel Labath   return GetBaseSymbolFile().FindDefinitionTypeForDWARFDeclContext(
110b9c1b51eSKate Stone       die_decl_ctx);
111d536a6d9STamas Berghammer }
112d536a6d9STamas Berghammer 
FindCompleteObjCDefinitionTypeForDIE(const DWARFDIE & die,lldb_private::ConstString type_name,bool must_be_implementation)113f413c785SAlexander Shaposhnikov lldb::TypeSP SymbolFileDWARFDwo::FindCompleteObjCDefinitionTypeForDIE(
1140e4c4821SAdrian Prantl     const DWARFDIE &die, lldb_private::ConstString type_name,
115f413c785SAlexander Shaposhnikov     bool must_be_implementation) {
1166a2eb367SPavel Labath   return GetBaseSymbolFile().FindCompleteObjCDefinitionTypeForDIE(
117f413c785SAlexander Shaposhnikov       die, type_name, must_be_implementation);
118f413c785SAlexander Shaposhnikov }
119f413c785SAlexander Shaposhnikov 
1200e252e38SAlex Langford llvm::Expected<TypeSystem &>
GetTypeSystemForLanguage(LanguageType language)121b9c1b51eSKate Stone SymbolFileDWARFDwo::GetTypeSystemForLanguage(LanguageType language) {
1226a2eb367SPavel Labath   return GetBaseSymbolFile().GetTypeSystemForLanguage(language);
12369d0b330STamas Berghammer }
12463168e08SPavel Labath 
12563168e08SPavel Labath DWARFDIE
GetDIE(const DIERef & die_ref)126b9c1b51eSKate Stone SymbolFileDWARFDwo::GetDIE(const DIERef &die_ref) {
1275abfa322SPavel Labath   if (die_ref.dwo_num() == GetDwoNum())
128ddf60ba0SPavel Labath     return DebugInfo().GetDIE(die_ref);
1293b926988SPavel Labath   return GetBaseSymbolFile().GetDIE(die_ref);
13063168e08SPavel Labath }
131