blob: d5d60aed03eb5a851a80eb2203e6fed3044e1bdb [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARFDebugMap.cpp ----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARFDebugMap.h"
11
Enrico Granatac76e60b2013-06-27 01:43:09 +000012#include "DWARFDebugAranges.h"
13
14#include "lldb/Core/RangeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Module.h"
16#include "lldb/Core/ModuleList.h"
17#include "lldb/Core/PluginManager.h"
18#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Section.h"
Greg Clayton9422dd62013-03-04 21:46:16 +000020
21//#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT
Greg Clayton1f746072012-08-29 21:13:06 +000022#if defined(DEBUG_OSO_DMAP)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/StreamFile.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000026
27#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Greg Clayton1f746072012-08-29 21:13:06 +000028#include "lldb/Symbol/CompileUnit.h"
Greg Clayton9422dd62013-03-04 21:46:16 +000029#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Symbol/ObjectFile.h"
31#include "lldb/Symbol/SymbolVendor.h"
32#include "lldb/Symbol/VariableList.h"
33
Sean Callanan60217122012-04-13 00:10:03 +000034#include "LogChannelDWARF.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "SymbolFileDWARF.h"
36
37using namespace lldb;
38using namespace lldb_private;
39
Greg Clayton1f746072012-08-29 21:13:06 +000040// Subclass lldb_private::Module so we can intercept the "Module::GetObjectFile()"
41// (so we can fixup the object file sections) and also for "Module::GetSymbolVendor()"
42// (so we can fixup the symbol file id.
43
Greg Clayton9422dd62013-03-04 21:46:16 +000044
45
46
47const SymbolFileDWARFDebugMap::FileRangeMap &
48SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap(SymbolFileDWARFDebugMap *exe_symfile)
49{
50 if (file_range_map_valid)
51 return file_range_map;
52
53 file_range_map_valid = true;
54
55 Module *oso_module = exe_symfile->GetModuleByCompUnitInfo (this);
Jim Ingham199e4f72013-03-13 01:34:14 +000056 if (!oso_module)
57 return file_range_map;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000058
Greg Clayton9422dd62013-03-04 21:46:16 +000059 ObjectFile *oso_objfile = oso_module->GetObjectFile();
Greg Clayton8caea6d2013-05-22 00:10:28 +000060 if (!oso_objfile)
61 return file_range_map;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000062
Greg Clayton5160ce52013-03-27 23:08:40 +000063 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
Greg Clayton9422dd62013-03-04 21:46:16 +000064 if (log)
65 {
66 ConstString object_name (oso_module->GetObjectName());
Greg Claytonb5ad4ec2013-04-29 17:25:54 +000067 log->Printf("%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000068 static_cast<void*>(this),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +000069 oso_module->GetSpecificationDescription().c_str());
Greg Clayton9422dd62013-03-04 21:46:16 +000070 }
Greg Clayton9422dd62013-03-04 21:46:16 +000071
72 std::vector<SymbolFileDWARFDebugMap::CompileUnitInfo *> cu_infos;
73 if (exe_symfile->GetCompUnitInfosForModule(oso_module, cu_infos))
74 {
75 for (auto comp_unit_info : cu_infos)
76 {
Greg Clayton3046e662013-07-10 01:23:25 +000077 Symtab *exe_symtab = exe_symfile->GetObjectFile()->GetSymtab();
Greg Clayton9422dd62013-03-04 21:46:16 +000078 ModuleSP oso_module_sp (oso_objfile->GetModule());
Greg Clayton3046e662013-07-10 01:23:25 +000079 Symtab *oso_symtab = oso_objfile->GetSymtab();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000080
Greg Clayton9422dd62013-03-04 21:46:16 +000081 ///const uint32_t fun_resolve_flags = SymbolContext::Module | eSymbolContextCompUnit | eSymbolContextFunction;
82 //SectionList *oso_sections = oso_objfile->Sections();
83 // Now we need to make sections that map from zero based object
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +000084 // file addresses to where things ended up in the main executable.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000085
Greg Clayton9422dd62013-03-04 21:46:16 +000086 assert (comp_unit_info->first_symbol_index != UINT32_MAX);
87 // End index is one past the last valid symbol index
88 const uint32_t oso_end_idx = comp_unit_info->last_symbol_index + 1;
89 for (uint32_t idx = comp_unit_info->first_symbol_index + 2; // Skip the N_SO and N_OSO
90 idx < oso_end_idx;
91 ++idx)
92 {
93 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
94 if (exe_symbol)
95 {
96 if (exe_symbol->IsDebug() == false)
97 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000098
Greg Clayton9422dd62013-03-04 21:46:16 +000099 switch (exe_symbol->GetType())
100 {
101 default:
102 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000103
Greg Clayton9422dd62013-03-04 21:46:16 +0000104 case eSymbolTypeCode:
105 {
106 // For each N_FUN, or function that we run into in the debug map
107 // we make a new section that we add to the sections found in the
108 // .o file. This new section has the file address set to what the
109 // addresses are in the .o file, and the load address is adjusted
110 // to match where it ended up in the final executable! We do this
111 // before we parse any dwarf info so that when it goes get parsed
112 // all section/offset addresses that get registered will resolve
113 // correctly to the new addresses in the main executable.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000114
Greg Clayton9422dd62013-03-04 21:46:16 +0000115 // First we find the original symbol in the .o file's symbol table
116 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType (exe_symbol->GetMangled().GetName(Mangled::ePreferMangled),
117 eSymbolTypeCode,
118 Symtab::eDebugNo,
119 Symtab::eVisibilityAny);
120 if (oso_fun_symbol)
121 {
122 // Add the inverse OSO file address to debug map entry mapping
123 exe_symfile->AddOSOFileRange (this,
124 exe_symbol->GetAddress().GetFileAddress(),
125 oso_fun_symbol->GetAddress().GetFileAddress(),
126 std::min<addr_t>(exe_symbol->GetByteSize(), oso_fun_symbol->GetByteSize()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000127
Greg Clayton9422dd62013-03-04 21:46:16 +0000128 }
129 }
130 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000131
Greg Clayton9422dd62013-03-04 21:46:16 +0000132 case eSymbolTypeData:
133 {
134 // For each N_GSYM we remap the address for the global by making
135 // a new section that we add to the sections found in the .o file.
136 // This new section has the file address set to what the
137 // addresses are in the .o file, and the load address is adjusted
138 // to match where it ended up in the final executable! We do this
139 // before we parse any dwarf info so that when it goes get parsed
140 // all section/offset addresses that get registered will resolve
141 // correctly to the new addresses in the main executable. We
142 // initially set the section size to be 1 byte, but will need to
143 // fix up these addresses further after all globals have been
144 // parsed to span the gaps, or we can find the global variable
145 // sizes from the DWARF info as we are parsing.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000146
Greg Clayton9422dd62013-03-04 21:46:16 +0000147 // Next we find the non-stab entry that corresponds to the N_GSYM in the .o file
148 Symbol *oso_gsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType (exe_symbol->GetMangled().GetName(Mangled::ePreferMangled),
149 eSymbolTypeData,
150 Symtab::eDebugNo,
151 Symtab::eVisibilityAny);
Greg Clayton9422dd62013-03-04 21:46:16 +0000152 if (exe_symbol && oso_gsym_symbol &&
153 exe_symbol->ValueIsAddress() &&
154 oso_gsym_symbol->ValueIsAddress())
155 {
156 // Add the inverse OSO file address to debug map entry mapping
157 exe_symfile->AddOSOFileRange (this,
158 exe_symbol->GetAddress().GetFileAddress(),
159 oso_gsym_symbol->GetAddress().GetFileAddress(),
160 std::min<addr_t>(exe_symbol->GetByteSize(), oso_gsym_symbol->GetByteSize()));
161 }
162 }
163 break;
164 }
165 }
166 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000167
Greg Clayton9422dd62013-03-04 21:46:16 +0000168 exe_symfile->FinalizeOSOFileRanges (this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000169 // We don't need the symbols anymore for the .o files
Greg Clayton3046e662013-07-10 01:23:25 +0000170 oso_objfile->ClearSymtab();
Greg Clayton9422dd62013-03-04 21:46:16 +0000171 }
172 }
173 return file_range_map;
174}
175
176
Greg Clayton1f746072012-08-29 21:13:06 +0000177class DebugMapModule : public Module
178{
179public:
180 DebugMapModule (const ModuleSP &exe_module_sp,
181 uint32_t cu_idx,
182 const FileSpec& file_spec,
183 const ArchSpec& arch,
184 const ConstString *object_name,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000185 off_t object_offset,
186 const TimeValue *object_mod_time_ptr) :
187 Module (file_spec, arch, object_name, object_offset, object_mod_time_ptr),
Greg Clayton1f746072012-08-29 21:13:06 +0000188 m_exe_module_wp (exe_module_sp),
189 m_cu_idx (cu_idx)
190 {
191 }
192
Greg Clayton9ba42d12012-10-08 22:48:57 +0000193 virtual
194 ~DebugMapModule ()
195 {
196 }
197
Greg Clayton1f746072012-08-29 21:13:06 +0000198
199 virtual SymbolVendor*
Greg Clayton136dff82012-12-14 02:15:00 +0000200 GetSymbolVendor(bool can_create = true, lldb_private::Stream *feedback_strm = NULL)
Greg Clayton1f746072012-08-29 21:13:06 +0000201 {
202 // Scope for locker
203 if (m_symfile_ap.get() || can_create == false)
204 return m_symfile_ap.get();
205
206 ModuleSP exe_module_sp (m_exe_module_wp.lock());
207 if (exe_module_sp)
208 {
209 // Now get the object file outside of a locking scope
210 ObjectFile *oso_objfile = GetObjectFile ();
211 if (oso_objfile)
212 {
213 Mutex::Locker locker (m_mutex);
Greg Clayton136dff82012-12-14 02:15:00 +0000214 SymbolVendor* symbol_vendor = Module::GetSymbolVendor(can_create, feedback_strm);
Greg Clayton1f746072012-08-29 21:13:06 +0000215 if (symbol_vendor)
216 {
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000217 // Set a pointer to this class to set our OSO DWARF file know
Greg Clayton1f746072012-08-29 21:13:06 +0000218 // that the DWARF is being used along with a debug map and that
219 // it will have the remapped sections that we do below.
Greg Clayton57abc5d2013-05-10 21:47:16 +0000220 SymbolFileDWARF *oso_symfile = SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(symbol_vendor->GetSymbolFile());
Greg Clayton1f746072012-08-29 21:13:06 +0000221
222 if (!oso_symfile)
223 return NULL;
224
225 ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
226 SymbolVendor *exe_sym_vendor = exe_module_sp->GetSymbolVendor();
227
228 if (exe_objfile && exe_sym_vendor)
229 {
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000230 oso_symfile->SetDebugMapModule(exe_module_sp);
231 // Set the ID of the symbol file DWARF to the index of the OSO
232 // shifted left by 32 bits to provide a unique prefix for any
233 // UserID's that get created in the symbol file.
234 oso_symfile->SetID (((uint64_t)m_cu_idx + 1ull) << 32ull);
Greg Clayton1f746072012-08-29 21:13:06 +0000235 }
236 return symbol_vendor;
237 }
238 }
239 }
240 return NULL;
241 }
242
243protected:
244 ModuleWP m_exe_module_wp;
245 const uint32_t m_cu_idx;
246};
247
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248void
249SymbolFileDWARFDebugMap::Initialize()
250{
251 PluginManager::RegisterPlugin (GetPluginNameStatic(),
252 GetPluginDescriptionStatic(),
253 CreateInstance);
254}
255
256void
257SymbolFileDWARFDebugMap::Terminate()
258{
259 PluginManager::UnregisterPlugin (CreateInstance);
260}
261
262
Greg Clayton57abc5d2013-05-10 21:47:16 +0000263lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264SymbolFileDWARFDebugMap::GetPluginNameStatic()
265{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000266 static ConstString g_name("dwarf-debugmap");
267 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268}
269
270const char *
271SymbolFileDWARFDebugMap::GetPluginDescriptionStatic()
272{
273 return "DWARF and DWARF3 debug symbol file reader (debug map).";
274}
275
276SymbolFile*
277SymbolFileDWARFDebugMap::CreateInstance (ObjectFile* obj_file)
278{
279 return new SymbolFileDWARFDebugMap (obj_file);
280}
281
282
283SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap (ObjectFile* ofile) :
284 SymbolFile(ofile),
285 m_flags(),
286 m_compile_unit_infos(),
287 m_func_indexes(),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000288 m_glob_indexes(),
289 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290{
291}
292
293
294SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap()
295{
296}
297
Greg Clayton6beaaa62011-01-17 03:46:26 +0000298void
299SymbolFileDWARFDebugMap::InitializeObject()
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000300{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000301 // Install our external AST source callbacks so we can complete Clang types.
Todd Fiala955fe6f2014-02-27 17:18:23 +0000302 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (
Greg Clayton6beaaa62011-01-17 03:46:26 +0000303 new ClangExternalASTSourceCallbacks (SymbolFileDWARFDebugMap::CompleteTagDecl,
304 SymbolFileDWARFDebugMap::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000305 NULL,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000306 SymbolFileDWARFDebugMap::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000307 this));
308
309 GetClangASTContext().SetExternalSource (ast_source_ap);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000310}
311
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312void
Greg Clayton1f746072012-08-29 21:13:06 +0000313SymbolFileDWARFDebugMap::InitOSO()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314{
315 if (m_flags.test(kHaveInitializedOSOs))
316 return;
Greg Clayton9422dd62013-03-04 21:46:16 +0000317
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 m_flags.set(kHaveInitializedOSOs);
Greg Clayton3046e662013-07-10 01:23:25 +0000319
320 // If the object file has been stripped, there is no sense in looking further
321 // as all of the debug symbols for the debug map will not be available
322 if (m_obj_file->IsStripped())
323 return;
324
325 // Also make sure the file type is some sort of executable. Core files, debug
326 // info files (dSYM), object files (.o files), and stub libraries all can
327 switch (m_obj_file->GetType())
328 {
329 case ObjectFile::eTypeInvalid:
330 case ObjectFile::eTypeCoreFile:
331 case ObjectFile::eTypeDebugInfo:
332 case ObjectFile::eTypeObjectFile:
333 case ObjectFile::eTypeStubLibrary:
334 case ObjectFile::eTypeUnknown:
Greg Clayton23f8c952014-03-24 23:10:19 +0000335 case ObjectFile::eTypeJIT:
Greg Clayton3046e662013-07-10 01:23:25 +0000336 return;
337
338 case ObjectFile::eTypeExecutable:
339 case ObjectFile::eTypeDynamicLinker:
340 case ObjectFile::eTypeSharedLibrary:
341 break;
342 }
343
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 // In order to get the abilities of this plug-in, we look at the list of
345 // N_OSO entries (object files) from the symbol table and make sure that
346 // these files exist and also contain valid DWARF. If we get any of that
347 // then we return the abilities of the first N_OSO's DWARF.
348
Greg Clayton3046e662013-07-10 01:23:25 +0000349 Symtab* symtab = m_obj_file->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 if (symtab)
351 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000352 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
Sean Callanan60217122012-04-13 00:10:03 +0000353
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 std::vector<uint32_t> oso_indexes;
Greg Clayton16b2d2b2011-01-20 06:08:59 +0000355 // When a mach-o symbol is encoded, the n_type field is encoded in bits
356 // 23:16, and the n_desc field is encoded in bits 15:0.
357 //
358 // To find all N_OSO entries that are part of the DWARF + debug map
359 // we find only object file symbols with the flags value as follows:
360 // bits 23:16 == 0x66 (N_OSO)
361 // bits 15: 0 == 0x0001 (specifies this is a debug map object file)
362 const uint32_t k_oso_symbol_flags_value = 0x660001u;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363
Greg Clayton16b2d2b2011-01-20 06:08:59 +0000364 const uint32_t oso_index_count = symtab->AppendSymbolIndexesWithTypeAndFlagsValue(eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365
366 if (oso_index_count > 0)
367 {
Greg Clayton16b2d2b2011-01-20 06:08:59 +0000368 symtab->AppendSymbolIndexesWithType (eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes);
369 symtab->AppendSymbolIndexesWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, m_glob_indexes);
370
371 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
372 symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
373
Greg Clayton9422dd62013-03-04 21:46:16 +0000374 for (uint32_t sym_idx : m_func_indexes)
375 {
376 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
377 lldb::addr_t file_addr = symbol->GetAddress().GetFileAddress();
378 lldb::addr_t byte_size = symbol->GetByteSize();
379 DebugMap::Entry debug_map_entry(file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
380 m_debug_map.Append(debug_map_entry);
381 }
382 for (uint32_t sym_idx : m_glob_indexes)
383 {
384 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
385 lldb::addr_t file_addr = symbol->GetAddress().GetFileAddress();
Greg Clayton3046e662013-07-10 01:23:25 +0000386 lldb::addr_t byte_size = symbol->GetByteSize();
Greg Clayton9422dd62013-03-04 21:46:16 +0000387 DebugMap::Entry debug_map_entry(file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
388 m_debug_map.Append(debug_map_entry);
389 }
390 m_debug_map.Sort();
391
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 m_compile_unit_infos.resize(oso_index_count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393
394 for (uint32_t i=0; i<oso_index_count; ++i)
395 {
Greg Clayton906ba472013-02-06 00:38:25 +0000396 const uint32_t so_idx = oso_indexes[i] - 1;
Greg Clayton61f39ce2013-02-05 18:40:36 +0000397 const uint32_t oso_idx = oso_indexes[i];
Greg Clayton906ba472013-02-06 00:38:25 +0000398 const Symbol *so_symbol = symtab->SymbolAtIndex(so_idx);
Greg Clayton61f39ce2013-02-05 18:40:36 +0000399 const Symbol *oso_symbol = symtab->SymbolAtIndex(oso_idx);
400 if (so_symbol &&
401 oso_symbol &&
402 so_symbol->GetType() == eSymbolTypeSourceFile &&
403 oso_symbol->GetType() == eSymbolTypeObjectFile)
Greg Claytonb9cafbb2012-07-17 20:18:12 +0000404 {
Greg Clayton613641d2013-03-08 21:46:30 +0000405 m_compile_unit_infos[i].so_file.SetFile(so_symbol->GetName().AsCString(), false);
Greg Clayton906ba472013-02-06 00:38:25 +0000406 m_compile_unit_infos[i].oso_path = oso_symbol->GetName();
Greg Clayton57abc5d2013-05-10 21:47:16 +0000407 TimeValue oso_mod_time;
408 oso_mod_time.OffsetWithSeconds(oso_symbol->GetAddress().GetOffset());
409 m_compile_unit_infos[i].oso_mod_time = oso_mod_time;
Greg Clayton61f39ce2013-02-05 18:40:36 +0000410 uint32_t sibling_idx = so_symbol->GetSiblingIndex();
411 // The sibling index can't be less that or equal to the current index "i"
Greg Claytonbc63aac2015-02-25 22:41:34 +0000412 if (sibling_idx == UINT32_MAX)
Greg Clayton61f39ce2013-02-05 18:40:36 +0000413 {
414 m_obj_file->GetModule()->ReportError ("N_SO in symbol with UID %u has invalid sibling in debug map, please file a bug and attach the binary listed in this error", so_symbol->GetID());
415 }
416 else
417 {
418 const Symbol* last_symbol = symtab->SymbolAtIndex (sibling_idx - 1);
Greg Clayton906ba472013-02-06 00:38:25 +0000419 m_compile_unit_infos[i].first_symbol_index = so_idx;
420 m_compile_unit_infos[i].last_symbol_index = sibling_idx - 1;
Greg Clayton61f39ce2013-02-05 18:40:36 +0000421 m_compile_unit_infos[i].first_symbol_id = so_symbol->GetID();
422 m_compile_unit_infos[i].last_symbol_id = last_symbol->GetID();
423
424 if (log)
425 log->Printf("Initialized OSO 0x%8.8x: file=%s", i, oso_symbol->GetName().GetCString());
426 }
Greg Claytonb9cafbb2012-07-17 20:18:12 +0000427 }
428 else
429 {
Greg Clayton61f39ce2013-02-05 18:40:36 +0000430 if (oso_symbol == NULL)
431 m_obj_file->GetModule()->ReportError ("N_OSO symbol[%u] can't be found, please file a bug and attach the binary listed in this error", oso_idx);
432 else if (so_symbol == NULL)
433 m_obj_file->GetModule()->ReportError ("N_SO not found for N_OSO symbol[%u], please file a bug and attach the binary listed in this error", oso_idx);
434 else if (so_symbol->GetType() != eSymbolTypeSourceFile)
435 m_obj_file->GetModule()->ReportError ("N_SO has incorrect symbol type (%u) for N_OSO symbol[%u], please file a bug and attach the binary listed in this error", so_symbol->GetType(), oso_idx);
436 else if (oso_symbol->GetType() != eSymbolTypeSourceFile)
437 m_obj_file->GetModule()->ReportError ("N_OSO has incorrect symbol type (%u) for N_OSO symbol[%u], please file a bug and attach the binary listed in this error", oso_symbol->GetType(), oso_idx);
Greg Claytonb9cafbb2012-07-17 20:18:12 +0000438 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 }
440 }
441 }
442}
443
444Module *
445SymbolFileDWARFDebugMap::GetModuleByOSOIndex (uint32_t oso_idx)
446{
447 const uint32_t cu_count = GetNumCompileUnits();
448 if (oso_idx < cu_count)
449 return GetModuleByCompUnitInfo (&m_compile_unit_infos[oso_idx]);
450 return NULL;
451}
452
453Module *
454SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo (CompileUnitInfo *comp_unit_info)
455{
Greg Clayton906ba472013-02-06 00:38:25 +0000456 if (!comp_unit_info->oso_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 {
Greg Clayton906ba472013-02-06 00:38:25 +0000458 auto pos = m_oso_map.find (comp_unit_info->oso_path);
459 if (pos != m_oso_map.end())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 {
Greg Clayton906ba472013-02-06 00:38:25 +0000461 comp_unit_info->oso_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 }
Greg Clayton906ba472013-02-06 00:38:25 +0000463 else
464 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000465 ObjectFile *obj_file = GetObjectFile();
Greg Clayton906ba472013-02-06 00:38:25 +0000466 comp_unit_info->oso_sp.reset (new OSOInfo());
467 m_oso_map[comp_unit_info->oso_path] = comp_unit_info->oso_sp;
468 const char *oso_path = comp_unit_info->oso_path.GetCString();
Greg Clayton96b68662013-03-23 00:50:06 +0000469 FileSpec oso_file (oso_path, false);
Greg Clayton906ba472013-02-06 00:38:25 +0000470 ConstString oso_object;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000471 if (oso_file.Exists())
472 {
473 TimeValue oso_mod_time (oso_file.GetModificationTime());
474 if (oso_mod_time != comp_unit_info->oso_mod_time)
475 {
476 obj_file->GetModule()->ReportError ("debug map object file '%s' has changed (actual time is 0x%" PRIx64 ", debug map time is 0x%" PRIx64 ") since this executable was linked, file will be ignored",
477 oso_file.GetPath().c_str(),
478 oso_mod_time.GetAsSecondsSinceJan1_1970(),
479 comp_unit_info->oso_mod_time.GetAsSecondsSinceJan1_1970());
480 return NULL;
481 }
482
483 }
484 else
Greg Clayton906ba472013-02-06 00:38:25 +0000485 {
486 const bool must_exist = true;
487
488 if (!ObjectFile::SplitArchivePathWithObject (oso_path,
489 oso_file,
490 oso_object,
491 must_exist))
492 {
Greg Clayton906ba472013-02-06 00:38:25 +0000493 return NULL;
494 }
495 }
496 // Always create a new module for .o files. Why? Because we
497 // use the debug map, to add new sections to each .o file and
498 // even though a .o file might not have changed, the sections
499 // that get added to the .o file can change.
Greg Clayton3f19ada2014-07-10 23:33:37 +0000500 ArchSpec oso_arch;
501 // Only adopt the architecture from the module (not the vendor or OS)
502 // since .o files for "i386-apple-ios" will historically show up as "i386-apple-macosx"
503 // due to the lack of a LC_VERSION_MIN_MACOSX or LC_VERSION_MIN_IPHONEOS
504 // load command...
505 oso_arch.SetTriple(m_obj_file->GetModule()->GetArchitecture().GetTriple().getArchName().str().c_str());
Greg Clayton57abc5d2013-05-10 21:47:16 +0000506 comp_unit_info->oso_sp->module_sp.reset (new DebugMapModule (obj_file->GetModule(),
Greg Clayton906ba472013-02-06 00:38:25 +0000507 GetCompUnitInfoIndex(comp_unit_info),
508 oso_file,
Greg Clayton3f19ada2014-07-10 23:33:37 +0000509 oso_arch,
Greg Clayton906ba472013-02-06 00:38:25 +0000510 oso_object ? &oso_object : NULL,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000511 0,
512 oso_object ? &comp_unit_info->oso_mod_time : NULL));
Greg Clayton906ba472013-02-06 00:38:25 +0000513 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 }
Greg Clayton906ba472013-02-06 00:38:25 +0000515 if (comp_unit_info->oso_sp)
516 return comp_unit_info->oso_sp->module_sp.get();
517 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518}
519
520
521bool
522SymbolFileDWARFDebugMap::GetFileSpecForSO (uint32_t oso_idx, FileSpec &file_spec)
523{
524 if (oso_idx < m_compile_unit_infos.size())
525 {
Greg Clayton1f746072012-08-29 21:13:06 +0000526 if (m_compile_unit_infos[oso_idx].so_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527 {
Greg Clayton1f746072012-08-29 21:13:06 +0000528 file_spec = m_compile_unit_infos[oso_idx].so_file;
529 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 }
532 return false;
533}
534
535
536
537ObjectFile *
538SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex (uint32_t oso_idx)
539{
540 Module *oso_module = GetModuleByOSOIndex (oso_idx);
541 if (oso_module)
542 return oso_module->GetObjectFile();
543 return NULL;
544}
545
546SymbolFileDWARF *
547SymbolFileDWARFDebugMap::GetSymbolFile (const SymbolContext& sc)
548{
549 CompileUnitInfo *comp_unit_info = GetCompUnitInfo (sc);
550 if (comp_unit_info)
551 return GetSymbolFileByCompUnitInfo (comp_unit_info);
552 return NULL;
553}
554
555ObjectFile *
556SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo (CompileUnitInfo *comp_unit_info)
557{
558 Module *oso_module = GetModuleByCompUnitInfo (comp_unit_info);
559 if (oso_module)
560 return oso_module->GetObjectFile();
561 return NULL;
562}
563
Greg Clayton81c22f62011-10-19 18:09:39 +0000564
565uint32_t
566SymbolFileDWARFDebugMap::GetCompUnitInfoIndex (const CompileUnitInfo *comp_unit_info)
567{
568 if (!m_compile_unit_infos.empty())
569 {
570 const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front();
571 const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back();
572 if (first_comp_unit_info <= comp_unit_info && comp_unit_info <= last_comp_unit_info)
573 return comp_unit_info - first_comp_unit_info;
574 }
575 return UINT32_MAX;
576}
577
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578SymbolFileDWARF *
579SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex (uint32_t oso_idx)
580{
581 if (oso_idx < m_compile_unit_infos.size())
582 return GetSymbolFileByCompUnitInfo (&m_compile_unit_infos[oso_idx]);
583 return NULL;
584}
585
586SymbolFileDWARF *
Greg Clayton57abc5d2013-05-10 21:47:16 +0000587SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF (SymbolFile *sym_file)
588{
589 if (sym_file && sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic())
590 return (SymbolFileDWARF *)sym_file;
591 return NULL;
592}
593
594SymbolFileDWARF *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo (CompileUnitInfo *comp_unit_info)
596{
Greg Clayton1f746072012-08-29 21:13:06 +0000597 Module *oso_module = GetModuleByCompUnitInfo (comp_unit_info);
598 if (oso_module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 {
Greg Clayton1f746072012-08-29 21:13:06 +0000600 SymbolVendor *sym_vendor = oso_module->GetSymbolVendor();
601 if (sym_vendor)
Greg Clayton57abc5d2013-05-10 21:47:16 +0000602 return GetSymbolFileAsSymbolFileDWARF (sym_vendor->GetSymbolFile());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 return NULL;
605}
606
607uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000608SymbolFileDWARFDebugMap::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609{
610 // In order to get the abilities of this plug-in, we look at the list of
611 // N_OSO entries (object files) from the symbol table and make sure that
612 // these files exist and also contain valid DWARF. If we get any of that
613 // then we return the abilities of the first N_OSO's DWARF.
614
615 const uint32_t oso_index_count = GetNumCompileUnits();
616 if (oso_index_count > 0)
617 {
Greg Clayton1f746072012-08-29 21:13:06 +0000618 InitOSO();
619 if (!m_compile_unit_infos.empty())
Greg Clayton3046e662013-07-10 01:23:25 +0000620 {
621 return SymbolFile::CompileUnits |
622 SymbolFile::Functions |
623 SymbolFile::Blocks |
624 SymbolFile::GlobalVariables |
625 SymbolFile::LocalVariables |
626 SymbolFile::VariableTypes |
627 SymbolFile::LineTables ;
628 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 }
630 return 0;
631}
632
633uint32_t
634SymbolFileDWARFDebugMap::GetNumCompileUnits()
635{
636 InitOSO ();
637 return m_compile_unit_infos.size();
638}
639
640
641CompUnitSP
642SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx)
643{
644 CompUnitSP comp_unit_sp;
645 const uint32_t cu_count = GetNumCompileUnits();
646
647 if (cu_idx < cu_count)
648 {
Greg Clayton906ba472013-02-06 00:38:25 +0000649 Module *oso_module = GetModuleByCompUnitInfo (&m_compile_unit_infos[cu_idx]);
650 if (oso_module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 {
Greg Clayton1f746072012-08-29 21:13:06 +0000652 FileSpec so_file_spec;
653 if (GetFileSpecForSO (cu_idx, so_file_spec))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000655 // User zero as the ID to match the compile unit at offset
656 // zero in each .o file since each .o file can only have
657 // one compile unit for now.
658 lldb::user_id_t cu_id = 0;
659 m_compile_unit_infos[cu_idx].compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(),
660 NULL,
661 so_file_spec,
662 cu_id,
663 eLanguageTypeUnknown));
Greg Clayton1f746072012-08-29 21:13:06 +0000664
Greg Clayton906ba472013-02-06 00:38:25 +0000665 if (m_compile_unit_infos[cu_idx].compile_unit_sp)
Greg Clayton1f746072012-08-29 21:13:06 +0000666 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000667 // Let our symbol vendor know about this compile unit
Greg Clayton9422dd62013-03-04 21:46:16 +0000668 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex (cu_idx, m_compile_unit_infos[cu_idx].compile_unit_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 }
670 }
671 }
Greg Clayton906ba472013-02-06 00:38:25 +0000672 comp_unit_sp = m_compile_unit_infos[cu_idx].compile_unit_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 }
674
675 return comp_unit_sp;
676}
677
678SymbolFileDWARFDebugMap::CompileUnitInfo *
679SymbolFileDWARFDebugMap::GetCompUnitInfo (const SymbolContext& sc)
680{
681 const uint32_t cu_count = GetNumCompileUnits();
682 for (uint32_t i=0; i<cu_count; ++i)
683 {
Greg Clayton906ba472013-02-06 00:38:25 +0000684 if (sc.comp_unit == m_compile_unit_infos[i].compile_unit_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 return &m_compile_unit_infos[i];
686 }
687 return NULL;
688}
689
Greg Clayton1f746072012-08-29 21:13:06 +0000690
Greg Clayton906ba472013-02-06 00:38:25 +0000691size_t
692SymbolFileDWARFDebugMap::GetCompUnitInfosForModule (const lldb_private::Module *module, std::vector<CompileUnitInfo *>& cu_infos)
Greg Clayton1f746072012-08-29 21:13:06 +0000693{
694 const uint32_t cu_count = GetNumCompileUnits();
695 for (uint32_t i=0; i<cu_count; ++i)
696 {
Greg Clayton906ba472013-02-06 00:38:25 +0000697 if (module == GetModuleByCompUnitInfo (&m_compile_unit_infos[i]))
698 cu_infos.push_back (&m_compile_unit_infos[i]);
Greg Clayton1f746072012-08-29 21:13:06 +0000699 }
Greg Clayton906ba472013-02-06 00:38:25 +0000700 return cu_infos.size();
Greg Clayton1f746072012-08-29 21:13:06 +0000701}
702
703lldb::LanguageType
704SymbolFileDWARFDebugMap::ParseCompileUnitLanguage (const SymbolContext& sc)
705{
706 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
707 if (oso_dwarf)
708 return oso_dwarf->ParseCompileUnitLanguage (sc);
709 return eLanguageTypeUnknown;
710}
711
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712size_t
713SymbolFileDWARFDebugMap::ParseCompileUnitFunctions (const SymbolContext& sc)
714{
715 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
716 if (oso_dwarf)
717 return oso_dwarf->ParseCompileUnitFunctions (sc);
718 return 0;
719}
720
721bool
722SymbolFileDWARFDebugMap::ParseCompileUnitLineTable (const SymbolContext& sc)
723{
724 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
725 if (oso_dwarf)
726 return oso_dwarf->ParseCompileUnitLineTable (sc);
727 return false;
728}
729
730bool
731SymbolFileDWARFDebugMap::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
732{
733 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
734 if (oso_dwarf)
735 return oso_dwarf->ParseCompileUnitSupportFiles (sc, support_files);
736 return false;
737}
738
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000739bool
740SymbolFileDWARFDebugMap::ParseImportedModules (const SymbolContext &sc, std::vector<ConstString> &imported_modules)
741{
742 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
743 if (oso_dwarf)
744 return oso_dwarf->ParseImportedModules(sc, imported_modules);
745 return false;
746}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747
748size_t
749SymbolFileDWARFDebugMap::ParseFunctionBlocks (const SymbolContext& sc)
750{
751 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
752 if (oso_dwarf)
753 return oso_dwarf->ParseFunctionBlocks (sc);
754 return 0;
755}
756
757
758size_t
759SymbolFileDWARFDebugMap::ParseTypes (const SymbolContext& sc)
760{
761 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
762 if (oso_dwarf)
763 return oso_dwarf->ParseTypes (sc);
764 return 0;
765}
766
767
768size_t
769SymbolFileDWARFDebugMap::ParseVariablesForContext (const SymbolContext& sc)
770{
771 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
772 if (oso_dwarf)
Greg Clayton1f746072012-08-29 21:13:06 +0000773 return oso_dwarf->ParseVariablesForContext (sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774 return 0;
775}
776
777
778
779Type*
780SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid)
781{
Greg Clayton81c22f62011-10-19 18:09:39 +0000782 const uint64_t oso_idx = GetOSOIndexFromUserID (type_uid);
783 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
784 if (oso_dwarf)
Greg Clayton9422dd62013-03-04 21:46:16 +0000785 return oso_dwarf->ResolveTypeUID (type_uid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 return NULL;
787}
788
Greg Clayton57ee3062013-07-11 22:46:58 +0000789bool
790SymbolFileDWARFDebugMap::ResolveClangOpaqueTypeDefinition (ClangASTType& clang_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +0000791{
792 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton57ee3062013-07-11 22:46:58 +0000793 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000794}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795
796uint32_t
797SymbolFileDWARFDebugMap::ResolveSymbolContext (const Address& exe_so_addr, uint32_t resolve_scope, SymbolContext& sc)
798{
799 uint32_t resolved_flags = 0;
Greg Clayton3046e662013-07-10 01:23:25 +0000800 Symtab* symtab = m_obj_file->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 if (symtab)
802 {
803 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804
Greg Clayton9422dd62013-03-04 21:46:16 +0000805 const DebugMap::Entry *debug_map_entry = m_debug_map.FindEntryThatContains (exe_file_addr);
806 if (debug_map_entry)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808
Greg Clayton9422dd62013-03-04 21:46:16 +0000809 sc.symbol = symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex());
810
811 if (sc.symbol != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000813 resolved_flags |= eSymbolContextSymbol;
814
815 uint32_t oso_idx = 0;
816 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithID (sc.symbol->GetID(), &oso_idx);
817 if (comp_unit_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 {
Jim Ingham1b57d172013-03-13 00:07:18 +0000819 comp_unit_info->GetFileRangeMap(this);
Greg Clayton9422dd62013-03-04 21:46:16 +0000820 Module *oso_module = GetModuleByCompUnitInfo (comp_unit_info);
821 if (oso_module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000822 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000823 lldb::addr_t oso_file_addr = exe_file_addr - debug_map_entry->GetRangeBase() + debug_map_entry->data.GetOSOFileAddress();
824 Address oso_so_addr;
825 if (oso_module->ResolveFileAddress(oso_file_addr, oso_so_addr))
826 {
827 resolved_flags |= oso_module->GetSymbolVendor()->ResolveSymbolContext (oso_so_addr, resolve_scope, sc);
828 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000830 }
831 }
832 }
833 }
834 return resolved_flags;
835}
836
837
838uint32_t
839SymbolFileDWARFDebugMap::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
840{
Greg Clayton1f746072012-08-29 21:13:06 +0000841 const uint32_t initial = sc_list.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842 const uint32_t cu_count = GetNumCompileUnits();
843
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844 for (uint32_t i=0; i<cu_count; ++i)
845 {
Greg Clayton1f746072012-08-29 21:13:06 +0000846 // If we are checking for inlines, then we need to look through all
847 // compile units no matter if "file_spec" matches.
848 bool resolve = check_inlines;
849
850 if (!resolve)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851 {
Greg Clayton1f746072012-08-29 21:13:06 +0000852 FileSpec so_file_spec;
853 if (GetFileSpecForSO (i, so_file_spec))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854 {
Greg Clayton1f746072012-08-29 21:13:06 +0000855 // Match the full path if the incoming file_spec has a directory (not just a basename)
Sean Callananddd7a2a2013-10-03 22:27:29 +0000856 const bool full_match = (bool)file_spec.GetDirectory();
Greg Clayton1f746072012-08-29 21:13:06 +0000857 resolve = FileSpec::Equal (file_spec, so_file_spec, full_match);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858 }
859 }
Greg Clayton1f746072012-08-29 21:13:06 +0000860 if (resolve)
861 {
862 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (i);
863 if (oso_dwarf)
864 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list);
865 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 }
867 return sc_list.GetSize() - initial;
868}
869
870uint32_t
871SymbolFileDWARFDebugMap::PrivateFindGlobalVariables
872(
873 const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +0000874 const ClangNamespaceDecl *namespace_decl,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875 const std::vector<uint32_t> &indexes, // Indexes into the symbol table that match "name"
876 uint32_t max_matches,
877 VariableList& variables
878)
879{
880 const uint32_t original_size = variables.GetSize();
881 const size_t match_count = indexes.size();
882 for (size_t i=0; i<match_count; ++i)
883 {
884 uint32_t oso_idx;
885 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithIndex (indexes[i], &oso_idx);
886 if (comp_unit_info)
887 {
888 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
889 if (oso_dwarf)
890 {
Sean Callanan213fdb82011-10-13 01:49:10 +0000891 if (oso_dwarf->FindGlobalVariables(name, namespace_decl, true, max_matches, variables))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 if (variables.GetSize() > max_matches)
893 break;
894 }
895 }
896 }
897 return variables.GetSize() - original_size;
898}
899
900uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000901SymbolFileDWARFDebugMap::FindGlobalVariables (const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902{
903
904 // If we aren't appending the results to this list, then clear the list
905 if (!append)
906 variables.Clear();
907
908 // Remember how many variables are in the list before we search in case
909 // we are appending the results to a variable list.
910 const uint32_t original_size = variables.GetSize();
911
Greg Claytonba2d22d2010-11-13 22:57:37 +0000912 uint32_t total_matches = 0;
Sean Callanan0dc848c2015-04-01 20:43:23 +0000913
914 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Sean Callanan213fdb82011-10-13 01:49:10 +0000915 const uint32_t oso_matches = oso_dwarf->FindGlobalVariables (name,
916 namespace_decl,
Sean Callanan0dc848c2015-04-01 20:43:23 +0000917 true,
918 max_matches,
Greg Claytonba2d22d2010-11-13 22:57:37 +0000919 variables);
920 if (oso_matches > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921 {
Greg Claytonba2d22d2010-11-13 22:57:37 +0000922 total_matches += oso_matches;
Sean Callanan0dc848c2015-04-01 20:43:23 +0000923
Greg Claytonba2d22d2010-11-13 22:57:37 +0000924 // Are we getting all matches?
925 if (max_matches == UINT32_MAX)
Sean Callanan0dc848c2015-04-01 20:43:23 +0000926 return false; // Yep, continue getting everything
927
Greg Claytonba2d22d2010-11-13 22:57:37 +0000928 // If we have found enough matches, lets get out
929 if (max_matches >= total_matches)
Sean Callanan0dc848c2015-04-01 20:43:23 +0000930 return true;
931
Greg Claytonba2d22d2010-11-13 22:57:37 +0000932 // Update the max matches for any subsequent calls to find globals
933 // in any other object files with DWARF
934 max_matches -= oso_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935 }
Sean Callanan0dc848c2015-04-01 20:43:23 +0000936
937 return false;
938 });
939
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 // Return the number of variable that were appended to the list
941 return variables.GetSize() - original_size;
942}
943
944
945uint32_t
946SymbolFileDWARFDebugMap::FindGlobalVariables (const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
947{
Greg Claytonba2d22d2010-11-13 22:57:37 +0000948 // If we aren't appending the results to this list, then clear the list
949 if (!append)
950 variables.Clear();
951
952 // Remember how many variables are in the list before we search in case
953 // we are appending the results to a variable list.
954 const uint32_t original_size = variables.GetSize();
955
956 uint32_t total_matches = 0;
Sean Callanan0dc848c2015-04-01 20:43:23 +0000957 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
958 const uint32_t oso_matches = oso_dwarf->FindGlobalVariables (regex,
Greg Claytonba2d22d2010-11-13 22:57:37 +0000959 true,
960 max_matches,
961 variables);
962 if (oso_matches > 0)
963 {
964 total_matches += oso_matches;
965
966 // Are we getting all matches?
967 if (max_matches == UINT32_MAX)
Sean Callanan0dc848c2015-04-01 20:43:23 +0000968 return false; // Yep, continue getting everything
Greg Claytonba2d22d2010-11-13 22:57:37 +0000969
970 // If we have found enough matches, lets get out
971 if (max_matches >= total_matches)
Sean Callanan0dc848c2015-04-01 20:43:23 +0000972 return true;
Greg Claytonba2d22d2010-11-13 22:57:37 +0000973
974 // Update the max matches for any subsequent calls to find globals
975 // in any other object files with DWARF
976 max_matches -= oso_matches;
977 }
Sean Callanan0dc848c2015-04-01 20:43:23 +0000978
979 return false;
980 });
981
Greg Claytonba2d22d2010-11-13 22:57:37 +0000982 // Return the number of variable that were appended to the list
983 return variables.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984}
985
986
987int
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000988SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989{
990 const uint32_t symbol_idx = *symbol_idx_ptr;
991
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000992 if (symbol_idx < comp_unit_info->first_symbol_index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993 return -1;
994
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000995 if (symbol_idx <= comp_unit_info->last_symbol_index)
996 return 0;
997
998 return 1;
999}
1000
1001
1002int
1003SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID (user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
1004{
1005 const user_id_t symbol_id = *symbol_idx_ptr;
1006
Greg Clayton1f746072012-08-29 21:13:06 +00001007 if (symbol_id < comp_unit_info->first_symbol_id)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001008 return -1;
1009
Greg Clayton1f746072012-08-29 21:13:06 +00001010 if (symbol_id <= comp_unit_info->last_symbol_id)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 return 0;
1012
1013 return 1;
1014}
1015
1016
1017SymbolFileDWARFDebugMap::CompileUnitInfo*
1018SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex (uint32_t symbol_idx, uint32_t *oso_idx_ptr)
1019{
1020 const uint32_t oso_index_count = m_compile_unit_infos.size();
1021 CompileUnitInfo *comp_unit_info = NULL;
1022 if (oso_index_count)
1023 {
Greg Claytone0d378b2011-03-24 21:19:54 +00001024 comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_idx,
1025 &m_compile_unit_infos[0],
1026 m_compile_unit_infos.size(),
1027 sizeof(CompileUnitInfo),
1028 (ComparisonFunction)SymbolContainsSymbolWithIndex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029 }
1030
1031 if (oso_idx_ptr)
1032 {
1033 if (comp_unit_info != NULL)
1034 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
1035 else
1036 *oso_idx_ptr = UINT32_MAX;
1037 }
1038 return comp_unit_info;
1039}
1040
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001041SymbolFileDWARFDebugMap::CompileUnitInfo*
1042SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID (user_id_t symbol_id, uint32_t *oso_idx_ptr)
1043{
1044 const uint32_t oso_index_count = m_compile_unit_infos.size();
1045 CompileUnitInfo *comp_unit_info = NULL;
1046 if (oso_index_count)
1047 {
Greg Claytone0d378b2011-03-24 21:19:54 +00001048 comp_unit_info = (CompileUnitInfo*)::bsearch (&symbol_id,
1049 &m_compile_unit_infos[0],
1050 m_compile_unit_infos.size(),
1051 sizeof(CompileUnitInfo),
1052 (ComparisonFunction)SymbolContainsSymbolWithID);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001053 }
1054
1055 if (oso_idx_ptr)
1056 {
1057 if (comp_unit_info != NULL)
1058 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
1059 else
1060 *oso_idx_ptr = UINT32_MAX;
1061 }
1062 return comp_unit_info;
1063}
1064
1065
Greg Clayton3ef3fc62010-08-17 23:16:15 +00001066static void
Greg Claytone72dfb32012-02-24 01:59:29 +00001067RemoveFunctionsWithModuleNotEqualTo (const ModuleSP &module_sp, SymbolContextList &sc_list, uint32_t start_idx)
Greg Clayton3ef3fc62010-08-17 23:16:15 +00001068{
1069 // We found functions in .o files. Not all functions in the .o files
1070 // will have made it into the final output file. The ones that did
1071 // make it into the final output file will have a section whose module
1072 // matches the module from the ObjectFile for this SymbolFile. When
1073 // the modules don't match, then we have something that was in a
1074 // .o file, but doesn't map to anything in the final executable.
1075 uint32_t i=start_idx;
1076 while (i < sc_list.GetSize())
1077 {
1078 SymbolContext sc;
1079 sc_list.GetContextAtIndex(i, sc);
1080 if (sc.function)
1081 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001082 const SectionSP section_sp (sc.function->GetAddressRange().GetBaseAddress().GetSection());
1083 if (section_sp->GetModule() != module_sp)
Greg Clayton3ef3fc62010-08-17 23:16:15 +00001084 {
1085 sc_list.RemoveContextAtIndex(i);
1086 continue;
1087 }
1088 }
1089 ++i;
1090 }
1091}
1092
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00001094SymbolFileDWARFDebugMap::FindFunctions(const ConstString &name, const ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095{
1096 Timer scoped_timer (__PRETTY_FUNCTION__,
1097 "SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
1098 name.GetCString());
1099
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100 uint32_t initial_size = 0;
1101 if (append)
1102 initial_size = sc_list.GetSize();
1103 else
1104 sc_list.Clear();
1105
Sean Callanan0dc848c2015-04-01 20:43:23 +00001106 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Greg Clayton3ef3fc62010-08-17 23:16:15 +00001107 uint32_t sc_idx = sc_list.GetSize();
Sean Callanan9df05fb2012-02-10 22:52:19 +00001108 if (oso_dwarf->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, true, sc_list))
Greg Clayton3ef3fc62010-08-17 23:16:15 +00001109 {
1110 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx);
1111 }
Sean Callanan0dc848c2015-04-01 20:43:23 +00001112 return false;
1113 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114
1115 return sc_list.GetSize() - initial_size;
1116}
1117
1118
1119uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00001120SymbolFileDWARFDebugMap::FindFunctions (const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121{
1122 Timer scoped_timer (__PRETTY_FUNCTION__,
1123 "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
1124 regex.GetText());
1125
Greg Clayton57a6b992010-08-17 00:35:34 +00001126 uint32_t initial_size = 0;
1127 if (append)
1128 initial_size = sc_list.GetSize();
1129 else
1130 sc_list.Clear();
1131
Sean Callanan0dc848c2015-04-01 20:43:23 +00001132 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Greg Clayton3ef3fc62010-08-17 23:16:15 +00001133 uint32_t sc_idx = sc_list.GetSize();
1134
Sean Callanan9df05fb2012-02-10 22:52:19 +00001135 if (oso_dwarf->FindFunctions(regex, include_inlines, true, sc_list))
Greg Clayton3ef3fc62010-08-17 23:16:15 +00001136 {
1137 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx);
1138 }
Sean Callanan0dc848c2015-04-01 20:43:23 +00001139 return false;
1140 });
Greg Clayton57a6b992010-08-17 00:35:34 +00001141
1142 return sc_list.GetSize() - initial_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001143}
1144
Greg Claytonf02500c2013-06-18 22:51:05 +00001145size_t
1146SymbolFileDWARFDebugMap::GetTypes (SymbolContextScope *sc_scope,
1147 uint32_t type_mask,
1148 TypeList &type_list)
1149{
1150 Timer scoped_timer (__PRETTY_FUNCTION__,
1151 "SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)",
1152 type_mask);
1153
1154
1155 uint32_t initial_size = type_list.GetSize();
1156 SymbolFileDWARF *oso_dwarf = NULL;
1157 if (sc_scope)
1158 {
1159 SymbolContext sc;
1160 sc_scope->CalculateSymbolContext(&sc);
1161
1162 CompileUnitInfo *cu_info = GetCompUnitInfo (sc);
1163 if (cu_info)
1164 {
1165 oso_dwarf = GetSymbolFileByCompUnitInfo (cu_info);
1166 if (oso_dwarf)
1167 oso_dwarf->GetTypes (sc_scope, type_mask, type_list);
1168 }
1169 }
1170 else
1171 {
Sean Callanan0dc848c2015-04-01 20:43:23 +00001172 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Greg Claytonf02500c2013-06-18 22:51:05 +00001173 oso_dwarf->GetTypes (sc_scope, type_mask, type_list);
Sean Callanan0dc848c2015-04-01 20:43:23 +00001174 return false;
1175 });
Greg Claytonf02500c2013-06-18 22:51:05 +00001176 }
1177 return type_list.GetSize() - initial_size;
1178}
1179
1180
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00001181TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00001182SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &die_decl_ctx)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00001183{
1184 TypeSP type_sp;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001185 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Greg Claytona8022fa2012-04-24 21:22:41 +00001186 type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Sean Callanan0dc848c2015-04-01 20:43:23 +00001187 return ((bool)type_sp);
1188 });
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00001189 return type_sp;
1190}
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001191
Greg Clayton901c5ca2011-12-03 04:40:03 +00001192
Greg Claytonc7f03b62012-01-12 04:33:28 +00001193
1194bool
1195SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type (SymbolFileDWARF *skip_dwarf_oso)
1196{
1197 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
1198 {
1199 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001200 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Greg Claytonc7f03b62012-01-12 04:33:28 +00001201 if (skip_dwarf_oso != oso_dwarf && oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(NULL))
1202 {
1203 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001204 return true;
Greg Claytonc7f03b62012-01-12 04:33:28 +00001205 }
Sean Callanan0dc848c2015-04-01 20:43:23 +00001206 return false;
1207 });
Greg Claytonc7f03b62012-01-12 04:33:28 +00001208 }
1209 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
1210}
1211
Greg Clayton901c5ca2011-12-03 04:40:03 +00001212TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00001213SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
1214 const ConstString &type_name,
1215 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00001216{
Greg Claytonbc63aac2015-02-25 22:41:34 +00001217 // If we have a debug map, we will have an Objective C symbol whose name is
1218 // the type name and whose type is eSymbolTypeObjCClass. If we can find that
1219 // symbol and find its containing parent, we can locate the .o file that will
1220 // contain the implementation definition since it will be scoped inside the N_SO
1221 // and we can then locate the SymbolFileDWARF that corresponds to that N_SO.
1222 SymbolFileDWARF *oso_dwarf = NULL;
Greg Clayton901c5ca2011-12-03 04:40:03 +00001223 TypeSP type_sp;
Greg Claytonbc63aac2015-02-25 22:41:34 +00001224 ObjectFile *module_objfile = m_obj_file->GetModule()->GetObjectFile();
1225 if (module_objfile)
Greg Clayton901c5ca2011-12-03 04:40:03 +00001226 {
Greg Claytonbc63aac2015-02-25 22:41:34 +00001227 Symtab *symtab = module_objfile->GetSymtab();
1228 if (symtab)
1229 {
1230 Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(type_name, eSymbolTypeObjCClass, Symtab::eDebugAny, Symtab::eVisibilityAny);
1231 if (objc_class_symbol)
1232 {
1233 // Get the N_SO symbol that contains the objective C class symbol as this
1234 // should be the .o file that contains the real definition...
1235 const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol);
1236
1237 if (source_file_symbol && source_file_symbol->GetType() == eSymbolTypeSourceFile)
1238 {
1239 const uint32_t source_file_symbol_idx = symtab->GetIndexForSymbol(source_file_symbol);
1240 if (source_file_symbol_idx != UINT32_MAX)
1241 {
1242 CompileUnitInfo *compile_unit_info = GetCompileUnitInfoForSymbolWithIndex (source_file_symbol_idx, NULL);
1243 if (compile_unit_info)
1244 {
1245 oso_dwarf = GetSymbolFileByCompUnitInfo (compile_unit_info);
1246 if (oso_dwarf)
1247 {
1248 TypeSP type_sp (oso_dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name, must_be_implementation));
1249 if (type_sp)
1250 {
1251 return type_sp;
1252 }
1253 }
1254 }
1255 }
1256 }
1257 }
1258 }
Greg Clayton901c5ca2011-12-03 04:40:03 +00001259 }
Greg Claytonbc63aac2015-02-25 22:41:34 +00001260
1261 // Only search all .o files for the definition if we don't need the implementation
1262 // because otherwise, with a valid debug map we should have the ObjC class symbol and
1263 // the code above should have found it.
1264 if (must_be_implementation == false)
1265 {
Sean Callanan0dc848c2015-04-01 20:43:23 +00001266 TypeSP type_sp;
1267
1268 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1269 type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name, must_be_implementation);
1270 return (bool)type_sp;
1271 });
1272
1273 return type_sp;
Greg Claytonbc63aac2015-02-25 22:41:34 +00001274 }
1275 return TypeSP();
Greg Clayton901c5ca2011-12-03 04:40:03 +00001276}
1277
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001278uint32_t
Greg Clayton6dbd3982010-09-15 05:51:24 +00001279SymbolFileDWARFDebugMap::FindTypes
1280(
1281 const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00001282 const ConstString &name,
1283 const ClangNamespaceDecl *namespace_decl,
Greg Clayton6dbd3982010-09-15 05:51:24 +00001284 bool append,
1285 uint32_t max_matches,
1286 TypeList& types
1287)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001288{
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001289 if (!append)
1290 types.Clear();
Greg Clayton6dbd3982010-09-15 05:51:24 +00001291
1292 const uint32_t initial_types_size = types.GetSize();
1293 SymbolFileDWARF *oso_dwarf;
1294
1295 if (sc.comp_unit)
1296 {
1297 oso_dwarf = GetSymbolFile (sc);
1298 if (oso_dwarf)
Sean Callanan213fdb82011-10-13 01:49:10 +00001299 return oso_dwarf->FindTypes (sc, name, namespace_decl, append, max_matches, types);
Greg Clayton6dbd3982010-09-15 05:51:24 +00001300 }
1301 else
1302 {
Sean Callanan0dc848c2015-04-01 20:43:23 +00001303 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Sean Callanan213fdb82011-10-13 01:49:10 +00001304 oso_dwarf->FindTypes (sc, name, namespace_decl, append, max_matches, types);
Sean Callananbc021732015-04-01 22:12:57 +00001305 return false;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001306 });
Greg Clayton6dbd3982010-09-15 05:51:24 +00001307 }
1308
1309 return types.GetSize() - initial_types_size;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001310}
1311
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312//
1313//uint32_t
1314//SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, lldb::user_id_t udt_uid, TypeList& types)
1315//{
1316// SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
1317// if (oso_dwarf)
1318// return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, udt_uid, types);
1319// return 0;
1320//}
1321
Greg Clayton96d7d742010-11-10 23:42:09 +00001322
Greg Clayton526e5af2010-11-13 03:52:47 +00001323ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00001324SymbolFileDWARFDebugMap::FindNamespace (const lldb_private::SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00001325 const lldb_private::ConstString &name,
1326 const ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00001327{
Greg Clayton526e5af2010-11-13 03:52:47 +00001328 ClangNamespaceDecl matching_namespace;
Greg Clayton96d7d742010-11-10 23:42:09 +00001329 SymbolFileDWARF *oso_dwarf;
1330
1331 if (sc.comp_unit)
1332 {
1333 oso_dwarf = GetSymbolFile (sc);
1334 if (oso_dwarf)
Sean Callanan213fdb82011-10-13 01:49:10 +00001335 matching_namespace = oso_dwarf->FindNamespace (sc, name, parent_namespace_decl);
Greg Clayton96d7d742010-11-10 23:42:09 +00001336 }
1337 else
1338 {
Sean Callanan0dc848c2015-04-01 20:43:23 +00001339 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Sean Callanan213fdb82011-10-13 01:49:10 +00001340 matching_namespace = oso_dwarf->FindNamespace (sc, name, parent_namespace_decl);
Greg Clayton96d7d742010-11-10 23:42:09 +00001341
Sean Callanan0dc848c2015-04-01 20:43:23 +00001342 return (bool)matching_namespace;
1343 });
Greg Clayton96d7d742010-11-10 23:42:09 +00001344 }
1345
1346 return matching_namespace;
1347}
1348
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349//------------------------------------------------------------------
1350// PluginInterface protocol
1351//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001352lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001353SymbolFileDWARFDebugMap::GetPluginName()
1354{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355 return GetPluginNameStatic();
1356}
1357
1358uint32_t
1359SymbolFileDWARFDebugMap::GetPluginVersion()
1360{
1361 return 1;
1362}
1363
Greg Clayton1f746072012-08-29 21:13:06 +00001364lldb::CompUnitSP
1365SymbolFileDWARFDebugMap::GetCompileUnit (SymbolFileDWARF *oso_dwarf)
1366{
1367 if (oso_dwarf)
1368 {
1369 const uint32_t cu_count = GetNumCompileUnits();
1370 for (uint32_t cu_idx=0; cu_idx<cu_count; ++cu_idx)
1371 {
1372 SymbolFileDWARF *oso_symfile = GetSymbolFileByCompUnitInfo (&m_compile_unit_infos[cu_idx]);
1373 if (oso_symfile == oso_dwarf)
1374 {
Greg Clayton906ba472013-02-06 00:38:25 +00001375 if (!m_compile_unit_infos[cu_idx].compile_unit_sp)
1376 m_compile_unit_infos[cu_idx].compile_unit_sp = ParseCompileUnitAtIndex (cu_idx);
Greg Clayton1f746072012-08-29 21:13:06 +00001377
Greg Clayton906ba472013-02-06 00:38:25 +00001378 return m_compile_unit_infos[cu_idx].compile_unit_sp;
Greg Clayton1f746072012-08-29 21:13:06 +00001379 }
1380 }
1381 }
1382 assert(!"this shouldn't happen");
1383 return lldb::CompUnitSP();
1384}
1385
Greg Clayton9422dd62013-03-04 21:46:16 +00001386SymbolFileDWARFDebugMap::CompileUnitInfo *
1387SymbolFileDWARFDebugMap::GetCompileUnitInfo (SymbolFileDWARF *oso_dwarf)
1388{
1389 if (oso_dwarf)
1390 {
1391 const uint32_t cu_count = GetNumCompileUnits();
1392 for (uint32_t cu_idx=0; cu_idx<cu_count; ++cu_idx)
1393 {
1394 SymbolFileDWARF *oso_symfile = GetSymbolFileByCompUnitInfo (&m_compile_unit_infos[cu_idx]);
1395 if (oso_symfile == oso_dwarf)
1396 {
1397 return &m_compile_unit_infos[cu_idx];
1398 }
1399 }
1400 }
1401 return NULL;
1402}
1403
Greg Clayton1f746072012-08-29 21:13:06 +00001404
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405void
Greg Clayton450e3f32010-10-12 02:24:53 +00001406SymbolFileDWARFDebugMap::SetCompileUnit (SymbolFileDWARF *oso_dwarf, const CompUnitSP &cu_sp)
1407{
Greg Clayton1f746072012-08-29 21:13:06 +00001408 if (oso_dwarf)
Greg Clayton450e3f32010-10-12 02:24:53 +00001409 {
Greg Clayton1f746072012-08-29 21:13:06 +00001410 const uint32_t cu_count = GetNumCompileUnits();
1411 for (uint32_t cu_idx=0; cu_idx<cu_count; ++cu_idx)
Greg Clayton450e3f32010-10-12 02:24:53 +00001412 {
Greg Clayton1f746072012-08-29 21:13:06 +00001413 SymbolFileDWARF *oso_symfile = GetSymbolFileByCompUnitInfo (&m_compile_unit_infos[cu_idx]);
1414 if (oso_symfile == oso_dwarf)
Greg Clayton450e3f32010-10-12 02:24:53 +00001415 {
Greg Clayton906ba472013-02-06 00:38:25 +00001416 if (m_compile_unit_infos[cu_idx].compile_unit_sp)
Greg Clayton1f746072012-08-29 21:13:06 +00001417 {
Greg Clayton906ba472013-02-06 00:38:25 +00001418 assert (m_compile_unit_infos[cu_idx].compile_unit_sp.get() == cu_sp.get());
Greg Clayton1f746072012-08-29 21:13:06 +00001419 }
1420 else
1421 {
Greg Clayton906ba472013-02-06 00:38:25 +00001422 m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp;
Greg Clayton1f746072012-08-29 21:13:06 +00001423 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
1424 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001425 }
1426 }
1427 }
1428}
1429
Greg Clayton6beaaa62011-01-17 03:46:26 +00001430
1431void
1432SymbolFileDWARFDebugMap::CompleteTagDecl (void *baton, clang::TagDecl *decl)
1433{
1434 SymbolFileDWARFDebugMap *symbol_file_dwarf = (SymbolFileDWARFDebugMap *)baton;
Greg Clayton57ee3062013-07-11 22:46:58 +00001435 ClangASTType clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001436 if (clang_type)
1437 {
Sean Callanan0dc848c2015-04-01 20:43:23 +00001438 symbol_file_dwarf->ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001439 if (oso_dwarf->HasForwardDeclForClangType (clang_type))
1440 {
1441 oso_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
Sean Callanan0dc848c2015-04-01 20:43:23 +00001442 return true;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001443 }
Sean Callanan0dc848c2015-04-01 20:43:23 +00001444 return false;
1445 });
Greg Clayton6beaaa62011-01-17 03:46:26 +00001446 }
1447}
1448
1449void
1450SymbolFileDWARFDebugMap::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
1451{
1452 SymbolFileDWARFDebugMap *symbol_file_dwarf = (SymbolFileDWARFDebugMap *)baton;
Greg Clayton57ee3062013-07-11 22:46:58 +00001453 ClangASTType clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001454 if (clang_type)
1455 {
Sean Callanan0dc848c2015-04-01 20:43:23 +00001456 symbol_file_dwarf->ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001457 if (oso_dwarf->HasForwardDeclForClangType (clang_type))
1458 {
1459 oso_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
Sean Callanan0dc848c2015-04-01 20:43:23 +00001460 return true;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001461 }
Sean Callanan0dc848c2015-04-01 20:43:23 +00001462 return false;
1463 });
Greg Clayton6beaaa62011-01-17 03:46:26 +00001464 }
1465}
1466
Zachary Turner504f38d2015-03-24 16:24:50 +00001467bool
Zachary Turnera98fac22015-03-24 18:56:08 +00001468SymbolFileDWARFDebugMap::LayoutRecordType(void *baton, const clang::RecordDecl *record_decl, uint64_t &size,
1469 uint64_t &alignment,
1470 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
1471 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
1472 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
Greg Claytoncaab74e2012-01-28 00:48:57 +00001473{
1474 SymbolFileDWARFDebugMap *symbol_file_dwarf = (SymbolFileDWARFDebugMap *)baton;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001475 bool laid_out = false;
1476 symbol_file_dwarf->ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1477 return (laid_out = oso_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets));
1478 });
1479 return laid_out;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001480}
1481
1482
1483
Greg Clayton81c22f62011-10-19 18:09:39 +00001484clang::DeclContext*
1485SymbolFileDWARFDebugMap::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
1486{
1487 const uint64_t oso_idx = GetOSOIndexFromUserID (type_uid);
1488 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
1489 if (oso_dwarf)
1490 return oso_dwarf->GetClangDeclContextContainingTypeUID (type_uid);
1491 return NULL;
1492}
1493
1494clang::DeclContext*
1495SymbolFileDWARFDebugMap::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1496{
1497 const uint64_t oso_idx = GetOSOIndexFromUserID (type_uid);
1498 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
1499 if (oso_dwarf)
1500 return oso_dwarf->GetClangDeclContextForTypeUID (sc, type_uid);
1501 return NULL;
1502}
1503
Greg Clayton9422dd62013-03-04 21:46:16 +00001504bool
1505SymbolFileDWARFDebugMap::AddOSOFileRange (CompileUnitInfo *cu_info,
1506 lldb::addr_t exe_file_addr,
1507 lldb::addr_t oso_file_addr,
1508 lldb::addr_t oso_byte_size)
1509{
Greg Clayton9422dd62013-03-04 21:46:16 +00001510 const uint32_t debug_map_idx = m_debug_map.FindEntryIndexThatContains(exe_file_addr);
1511 if (debug_map_idx != UINT32_MAX)
1512 {
1513 DebugMap::Entry *debug_map_entry = m_debug_map.FindEntryThatContains(exe_file_addr);
Greg Clayton9422dd62013-03-04 21:46:16 +00001514 debug_map_entry->data.SetOSOFileAddress(oso_file_addr);
1515 cu_info->file_range_map.Append(FileRangeMap::Entry(oso_file_addr, oso_byte_size, exe_file_addr));
1516 return true;
1517 }
1518 return false;
1519}
1520
1521void
1522SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (CompileUnitInfo *cu_info)
1523{
1524 cu_info->file_range_map.Sort();
1525#if defined(DEBUG_OSO_DMAP)
1526 const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this);
1527 const size_t n = oso_file_range_map.GetSize();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001528 printf ("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n",
Greg Clayton084fad62013-03-06 23:23:27 +00001529 cu_info,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001530 cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton9422dd62013-03-04 21:46:16 +00001531 for (size_t i=0; i<n; ++i)
1532 {
1533 const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i);
1534 printf ("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n",
1535 entry.GetRangeBase(), entry.GetRangeEnd(),
1536 entry.data, entry.data + entry.GetByteSize());
1537 }
1538#endif
1539}
1540
1541lldb::addr_t
1542SymbolFileDWARFDebugMap::LinkOSOFileAddress (SymbolFileDWARF *oso_symfile, lldb::addr_t oso_file_addr)
1543{
1544 CompileUnitInfo *cu_info = GetCompileUnitInfo (oso_symfile);
1545 if (cu_info)
1546 {
1547 const FileRangeMap::Entry *oso_range_entry = cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1548 if (oso_range_entry)
1549 {
1550 const DebugMap::Entry *debug_map_entry = m_debug_map.FindEntryThatContains(oso_range_entry->data);
1551 if (debug_map_entry)
1552 {
1553 const lldb::addr_t offset = oso_file_addr - oso_range_entry->GetRangeBase();
1554 const lldb::addr_t exe_file_addr = debug_map_entry->GetRangeBase() + offset;
1555 return exe_file_addr;
1556 }
1557 }
1558 }
1559 return LLDB_INVALID_ADDRESS;
1560}
1561
1562bool
1563SymbolFileDWARFDebugMap::LinkOSOAddress (Address &addr)
1564{
1565 // Make sure this address hasn't been fixed already
1566 Module *exe_module = GetObjectFile()->GetModule().get();
1567 Module *addr_module = addr.GetModule().get();
1568 if (addr_module == exe_module)
1569 return true; // Address is already in terms of the main executable module
1570
Greg Clayton57abc5d2013-05-10 21:47:16 +00001571 CompileUnitInfo *cu_info = GetCompileUnitInfo (GetSymbolFileAsSymbolFileDWARF(addr_module->GetSymbolVendor()->GetSymbolFile()));
Greg Clayton9422dd62013-03-04 21:46:16 +00001572 if (cu_info)
1573 {
1574 const lldb::addr_t oso_file_addr = addr.GetFileAddress();
1575 const FileRangeMap::Entry *oso_range_entry = cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1576 if (oso_range_entry)
1577 {
1578 const DebugMap::Entry *debug_map_entry = m_debug_map.FindEntryThatContains(oso_range_entry->data);
1579 if (debug_map_entry)
1580 {
1581 const lldb::addr_t offset = oso_file_addr - oso_range_entry->GetRangeBase();
1582 const lldb::addr_t exe_file_addr = debug_map_entry->GetRangeBase() + offset;
1583 return exe_module->ResolveFileAddress(exe_file_addr, addr);
1584 }
1585 }
1586 }
1587 return true;
1588}
1589
1590LineTable *
1591SymbolFileDWARFDebugMap::LinkOSOLineTable (SymbolFileDWARF *oso_dwarf, LineTable *line_table)
1592{
1593 CompileUnitInfo *cu_info = GetCompileUnitInfo (oso_dwarf);
1594 if (cu_info)
1595 return line_table->LinkLineTable(cu_info->GetFileRangeMap(this));
1596 return NULL;
1597}
1598
Enrico Granatac76e60b2013-06-27 01:43:09 +00001599size_t
1600SymbolFileDWARFDebugMap::AddOSOARanges (SymbolFileDWARF* dwarf2Data, DWARFDebugAranges* debug_aranges)
1601{
1602 size_t num_line_entries_added = 0;
1603 if (debug_aranges && dwarf2Data)
1604 {
1605 CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data);
1606 if (compile_unit_info)
1607 {
1608 const FileRangeMap &file_range_map = compile_unit_info->GetFileRangeMap(this);
1609 for (size_t idx = 0;
1610 idx < file_range_map.GetSize();
1611 idx++)
1612 {
1613 const FileRangeMap::Entry* entry = file_range_map.GetEntryAtIndex(idx);
1614 if (entry)
1615 {
1616 printf ("[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n", entry->GetRangeBase(), entry->GetRangeEnd());
1617 debug_aranges->AppendRange(dwarf2Data->GetID(), entry->GetRangeBase(), entry->GetRangeEnd());
1618 num_line_entries_added++;
1619 }
1620 }
1621 }
1622 }
1623 return num_line_entries_added;
1624}
Greg Clayton81c22f62011-10-19 18:09:39 +00001625