blob: 6a1b6b423fbef698fae7c0de8981eaca1cb6a444 [file] [log] [blame]
Eugene Zelenko0af149a2015-10-19 18:52:10 +00001//===-- SymbolFileDWARFDebugMap.cpp -----------------------------*- C++ -*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
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
Eugene Zelenko0af149a2015-10-19 18:52:10 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "SymbolFileDWARFDebugMap.h"
15
Enrico Granatac76e60b2013-06-27 01:43:09 +000016#include "DWARFDebugAranges.h"
17
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Module.h"
19#include "lldb/Core/ModuleList.h"
20#include "lldb/Core/PluginManager.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/Core/RangeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Core/Section.h"
Greg Clayton9422dd62013-03-04 21:46:16 +000024
25//#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT
Greg Clayton1f746072012-08-29 21:13:06 +000026#if defined(DEBUG_OSO_DMAP)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/StreamFile.h"
Greg Clayton1f746072012-08-29 21:13:06 +000028#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Timer.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000030
Greg Clayton1f746072012-08-29 21:13:06 +000031#include "lldb/Symbol/CompileUnit.h"
Greg Clayton9422dd62013-03-04 21:46:16 +000032#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Symbol/ObjectFile.h"
34#include "lldb/Symbol/SymbolVendor.h"
Ravitheja Addepally40697302015-10-08 09:45:41 +000035#include "lldb/Symbol/TypeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Symbol/VariableList.h"
37
Sean Callanan60217122012-04-13 00:10:03 +000038#include "LogChannelDWARF.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "SymbolFileDWARF.h"
40
41using namespace lldb;
42using namespace lldb_private;
43
Kate Stoneb9c1b512016-09-06 20:57:50 +000044// Subclass lldb_private::Module so we can intercept the
45// "Module::GetObjectFile()"
46// (so we can fixup the object file sections) and also for
47// "Module::GetSymbolVendor()"
Greg Clayton1f746072012-08-29 21:13:06 +000048// (so we can fixup the symbol file id.
49
Greg Clayton9422dd62013-03-04 21:46:16 +000050const SymbolFileDWARFDebugMap::FileRangeMap &
Kate Stoneb9c1b512016-09-06 20:57:50 +000051SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap(
52 SymbolFileDWARFDebugMap *exe_symfile) {
53 if (file_range_map_valid)
Greg Clayton9422dd62013-03-04 21:46:16 +000054 return file_range_map;
Kate Stoneb9c1b512016-09-06 20:57:50 +000055
56 file_range_map_valid = true;
57
58 Module *oso_module = exe_symfile->GetModuleByCompUnitInfo(this);
59 if (!oso_module)
60 return file_range_map;
61
62 ObjectFile *oso_objfile = oso_module->GetObjectFile();
63 if (!oso_objfile)
64 return file_range_map;
65
66 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
67 if (log) {
68 ConstString object_name(oso_module->GetObjectName());
69 log->Printf(
70 "%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')",
71 static_cast<void *>(this),
72 oso_module->GetSpecificationDescription().c_str());
73 }
74
75 std::vector<SymbolFileDWARFDebugMap::CompileUnitInfo *> cu_infos;
76 if (exe_symfile->GetCompUnitInfosForModule(oso_module, cu_infos)) {
77 for (auto comp_unit_info : cu_infos) {
78 Symtab *exe_symtab = exe_symfile->GetObjectFile()->GetSymtab();
79 ModuleSP oso_module_sp(oso_objfile->GetModule());
80 Symtab *oso_symtab = oso_objfile->GetSymtab();
81
82 /// const uint32_t fun_resolve_flags = SymbolContext::Module |
83 /// eSymbolContextCompUnit | eSymbolContextFunction;
84 // SectionList *oso_sections = oso_objfile->Sections();
85 // Now we need to make sections that map from zero based object
86 // file addresses to where things ended up in the main executable.
87
88 assert(comp_unit_info->first_symbol_index != UINT32_MAX);
89 // End index is one past the last valid symbol index
90 const uint32_t oso_end_idx = comp_unit_info->last_symbol_index + 1;
91 for (uint32_t idx = comp_unit_info->first_symbol_index +
92 2; // Skip the N_SO and N_OSO
93 idx < oso_end_idx;
94 ++idx) {
95 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
96 if (exe_symbol) {
97 if (exe_symbol->IsDebug() == false)
98 continue;
99
100 switch (exe_symbol->GetType()) {
101 default:
102 break;
103
104 case eSymbolTypeCode: {
105 // For each N_FUN, or function that we run into in the debug map
106 // we make a new section that we add to the sections found in the
107 // .o file. This new section has the file address set to what the
108 // addresses are in the .o file, and the load address is adjusted
109 // to match where it ended up in the final executable! We do this
110 // before we parse any dwarf info so that when it goes get parsed
111 // all section/offset addresses that get registered will resolve
112 // correctly to the new addresses in the main executable.
113
114 // First we find the original symbol in the .o file's symbol table
115 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(
116 exe_symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown,
117 Mangled::ePreferMangled),
118 eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
119 if (oso_fun_symbol) {
120 // Add the inverse OSO file address to debug map entry mapping
121 exe_symfile->AddOSOFileRange(
122 this, exe_symbol->GetAddressRef().GetFileAddress(),
123 exe_symbol->GetByteSize(),
124 oso_fun_symbol->GetAddressRef().GetFileAddress(),
125 oso_fun_symbol->GetByteSize());
126 }
127 } break;
128
129 case eSymbolTypeData: {
130 // For each N_GSYM we remap the address for the global by making
131 // a new section that we add to the sections found in the .o file.
132 // This new section has the file address set to what the
133 // addresses are in the .o file, and the load address is adjusted
134 // to match where it ended up in the final executable! We do this
135 // before we parse any dwarf info so that when it goes get parsed
136 // all section/offset addresses that get registered will resolve
137 // correctly to the new addresses in the main executable. We
138 // initially set the section size to be 1 byte, but will need to
139 // fix up these addresses further after all globals have been
140 // parsed to span the gaps, or we can find the global variable
141 // sizes from the DWARF info as we are parsing.
142
143 // Next we find the non-stab entry that corresponds to the N_GSYM in
144 // the .o file
145 Symbol *oso_gsym_symbol =
146 oso_symtab->FindFirstSymbolWithNameAndType(
147 exe_symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown,
148 Mangled::ePreferMangled),
149 eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
150 if (exe_symbol && oso_gsym_symbol && exe_symbol->ValueIsAddress() &&
151 oso_gsym_symbol->ValueIsAddress()) {
152 // Add the inverse OSO file address to debug map entry mapping
153 exe_symfile->AddOSOFileRange(
154 this, exe_symbol->GetAddressRef().GetFileAddress(),
155 exe_symbol->GetByteSize(),
156 oso_gsym_symbol->GetAddressRef().GetFileAddress(),
157 oso_gsym_symbol->GetByteSize());
158 }
159 } break;
160 }
161 }
162 }
163
164 exe_symfile->FinalizeOSOFileRanges(this);
165 // We don't need the symbols anymore for the .o files
166 oso_objfile->ClearSymtab();
167 }
168 }
169 return file_range_map;
Greg Clayton9422dd62013-03-04 21:46:16 +0000170}
171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172class DebugMapModule : public Module {
Greg Clayton1f746072012-08-29 21:13:06 +0000173public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 DebugMapModule(const ModuleSP &exe_module_sp, uint32_t cu_idx,
175 const FileSpec &file_spec, const ArchSpec &arch,
176 const ConstString *object_name, off_t object_offset,
177 const TimeValue *object_mod_time_ptr)
178 : Module(file_spec, arch, object_name, object_offset,
179 object_mod_time_ptr),
180 m_exe_module_wp(exe_module_sp), m_cu_idx(cu_idx) {}
Greg Clayton1f746072012-08-29 21:13:06 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 ~DebugMapModule() override = default;
Greg Clayton9ba42d12012-10-08 22:48:57 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 SymbolVendor *
185 GetSymbolVendor(bool can_create = true,
186 lldb_private::Stream *feedback_strm = NULL) override {
187 // Scope for locker
188 if (m_symfile_ap.get() || can_create == false)
189 return m_symfile_ap.get();
Greg Clayton1f746072012-08-29 21:13:06 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 ModuleSP exe_module_sp(m_exe_module_wp.lock());
192 if (exe_module_sp) {
193 // Now get the object file outside of a locking scope
194 ObjectFile *oso_objfile = GetObjectFile();
195 if (oso_objfile) {
196 std::lock_guard<std::recursive_mutex> guard(m_mutex);
197 SymbolVendor *symbol_vendor =
198 Module::GetSymbolVendor(can_create, feedback_strm);
199 if (symbol_vendor) {
200 // Set a pointer to this class to set our OSO DWARF file know
201 // that the DWARF is being used along with a debug map and that
202 // it will have the remapped sections that we do below.
203 SymbolFileDWARF *oso_symfile =
204 SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(
205 symbol_vendor->GetSymbolFile());
Greg Clayton1f746072012-08-29 21:13:06 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 if (!oso_symfile)
208 return NULL;
209
210 ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
211 SymbolVendor *exe_sym_vendor = exe_module_sp->GetSymbolVendor();
212
213 if (exe_objfile && exe_sym_vendor) {
214 oso_symfile->SetDebugMapModule(exe_module_sp);
215 // Set the ID of the symbol file DWARF to the index of the OSO
216 // shifted left by 32 bits to provide a unique prefix for any
217 // UserID's that get created in the symbol file.
218 oso_symfile->SetID(((uint64_t)m_cu_idx + 1ull) << 32ull);
219 }
220 return symbol_vendor;
Greg Clayton1f746072012-08-29 21:13:06 +0000221 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 }
Greg Clayton1f746072012-08-29 21:13:06 +0000223 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 return NULL;
225 }
Greg Clayton1f746072012-08-29 21:13:06 +0000226
227protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 ModuleWP m_exe_module_wp;
229 const uint32_t m_cu_idx;
Greg Clayton1f746072012-08-29 21:13:06 +0000230};
231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232void SymbolFileDWARFDebugMap::Initialize() {
233 PluginManager::RegisterPlugin(GetPluginNameStatic(),
234 GetPluginDescriptionStatic(), CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235}
236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237void SymbolFileDWARFDebugMap::Terminate() {
238 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginNameStatic() {
242 static ConstString g_name("dwarf-debugmap");
243 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244}
245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246const char *SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() {
247 return "DWARF and DWARF3 debug symbol file reader (debug map).";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248}
249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFile *obj_file) {
251 return new SymbolFileDWARFDebugMap(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap(ObjectFile *ofile)
255 : SymbolFile(ofile), m_flags(), m_compile_unit_infos(), m_func_indexes(),
256 m_glob_indexes(),
257 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261void SymbolFileDWARFDebugMap::InitializeObject() {}
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263void SymbolFileDWARFDebugMap::InitOSO() {
264 if (m_flags.test(kHaveInitializedOSOs))
265 return;
Greg Clayton3046e662013-07-10 01:23:25 +0000266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 m_flags.set(kHaveInitializedOSOs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 // If the object file has been stripped, there is no sense in looking further
270 // as all of the debug symbols for the debug map will not be available
271 if (m_obj_file->IsStripped())
272 return;
Sean Callanan60217122012-04-13 00:10:03 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 // Also make sure the file type is some sort of executable. Core files, debug
275 // info files (dSYM), object files (.o files), and stub libraries all can
276 switch (m_obj_file->GetType()) {
277 case ObjectFile::eTypeInvalid:
278 case ObjectFile::eTypeCoreFile:
279 case ObjectFile::eTypeDebugInfo:
280 case ObjectFile::eTypeObjectFile:
281 case ObjectFile::eTypeStubLibrary:
282 case ObjectFile::eTypeUnknown:
283 case ObjectFile::eTypeJIT:
284 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 case ObjectFile::eTypeExecutable:
287 case ObjectFile::eTypeDynamicLinker:
288 case ObjectFile::eTypeSharedLibrary:
289 break;
290 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 // In order to get the abilities of this plug-in, we look at the list of
293 // N_OSO entries (object files) from the symbol table and make sure that
294 // these files exist and also contain valid DWARF. If we get any of that
295 // then we return the abilities of the first N_OSO's DWARF.
Greg Clayton16b2d2b2011-01-20 06:08:59 +0000296
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 Symtab *symtab = m_obj_file->GetSymtab();
298 if (symtab) {
299 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
Greg Clayton16b2d2b2011-01-20 06:08:59 +0000300
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 std::vector<uint32_t> oso_indexes;
302 // When a mach-o symbol is encoded, the n_type field is encoded in bits
303 // 23:16, and the n_desc field is encoded in bits 15:0.
304 //
305 // To find all N_OSO entries that are part of the DWARF + debug map
306 // we find only object file symbols with the flags value as follows:
307 // bits 23:16 == 0x66 (N_OSO)
308 // bits 15: 0 == 0x0001 (specifies this is a debug map object file)
309 const uint32_t k_oso_symbol_flags_value = 0x660001u;
Greg Clayton9422dd62013-03-04 21:46:16 +0000310
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311 const uint32_t oso_index_count =
312 symtab->AppendSymbolIndexesWithTypeAndFlagsValue(
313 eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 if (oso_index_count > 0) {
316 symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes,
317 Symtab::eVisibilityAny,
318 m_func_indexes);
319 symtab->AppendSymbolIndexesWithType(eSymbolTypeData, Symtab::eDebugYes,
320 Symtab::eVisibilityAny,
321 m_glob_indexes);
322
323 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
324 symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
325
326 for (uint32_t sym_idx : m_func_indexes) {
327 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
328 lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
329 lldb::addr_t byte_size = symbol->GetByteSize();
330 DebugMap::Entry debug_map_entry(
331 file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
332 m_debug_map.Append(debug_map_entry);
333 }
334 for (uint32_t sym_idx : m_glob_indexes) {
335 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
336 lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
337 lldb::addr_t byte_size = symbol->GetByteSize();
338 DebugMap::Entry debug_map_entry(
339 file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
340 m_debug_map.Append(debug_map_entry);
341 }
342 m_debug_map.Sort();
343
344 m_compile_unit_infos.resize(oso_index_count);
345
346 for (uint32_t i = 0; i < oso_index_count; ++i) {
347 const uint32_t so_idx = oso_indexes[i] - 1;
348 const uint32_t oso_idx = oso_indexes[i];
349 const Symbol *so_symbol = symtab->SymbolAtIndex(so_idx);
350 const Symbol *oso_symbol = symtab->SymbolAtIndex(oso_idx);
351 if (so_symbol && oso_symbol &&
352 so_symbol->GetType() == eSymbolTypeSourceFile &&
353 oso_symbol->GetType() == eSymbolTypeObjectFile) {
354 m_compile_unit_infos[i].so_file.SetFile(
355 so_symbol->GetName().AsCString(), false);
356 m_compile_unit_infos[i].oso_path = oso_symbol->GetName();
357 TimeValue oso_mod_time;
358 oso_mod_time.OffsetWithSeconds(oso_symbol->GetIntegerValue(0));
359 m_compile_unit_infos[i].oso_mod_time = oso_mod_time;
360 uint32_t sibling_idx = so_symbol->GetSiblingIndex();
361 // The sibling index can't be less that or equal to the current index
362 // "i"
363 if (sibling_idx == UINT32_MAX) {
364 m_obj_file->GetModule()->ReportError(
365 "N_SO in symbol with UID %u has invalid sibling in debug map, "
366 "please file a bug and attach the binary listed in this error",
367 so_symbol->GetID());
368 } else {
369 const Symbol *last_symbol = symtab->SymbolAtIndex(sibling_idx - 1);
370 m_compile_unit_infos[i].first_symbol_index = so_idx;
371 m_compile_unit_infos[i].last_symbol_index = sibling_idx - 1;
372 m_compile_unit_infos[i].first_symbol_id = so_symbol->GetID();
373 m_compile_unit_infos[i].last_symbol_id = last_symbol->GetID();
374
375 if (log)
376 log->Printf("Initialized OSO 0x%8.8x: file=%s", i,
377 oso_symbol->GetName().GetCString());
378 }
379 } else {
380 if (oso_symbol == NULL)
381 m_obj_file->GetModule()->ReportError(
382 "N_OSO symbol[%u] can't be found, please file a bug and attach "
383 "the binary listed in this error",
384 oso_idx);
385 else if (so_symbol == NULL)
386 m_obj_file->GetModule()->ReportError(
387 "N_SO not found for N_OSO symbol[%u], please file a bug and "
388 "attach the binary listed in this error",
389 oso_idx);
390 else if (so_symbol->GetType() != eSymbolTypeSourceFile)
391 m_obj_file->GetModule()->ReportError(
392 "N_SO has incorrect symbol type (%u) for N_OSO symbol[%u], "
393 "please file a bug and attach the binary listed in this error",
394 so_symbol->GetType(), oso_idx);
395 else if (oso_symbol->GetType() != eSymbolTypeSourceFile)
396 m_obj_file->GetModule()->ReportError(
397 "N_OSO has incorrect symbol type (%u) for N_OSO symbol[%u], "
398 "please file a bug and attach the binary listed in this error",
399 oso_symbol->GetType(), oso_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404}
405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406Module *SymbolFileDWARFDebugMap::GetModuleByOSOIndex(uint32_t oso_idx) {
407 const uint32_t cu_count = GetNumCompileUnits();
408 if (oso_idx < cu_count)
409 return GetModuleByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
410 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411}
412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413Module *SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo(
414 CompileUnitInfo *comp_unit_info) {
415 if (!comp_unit_info->oso_sp) {
416 auto pos = m_oso_map.find(comp_unit_info->oso_path);
417 if (pos != m_oso_map.end()) {
418 comp_unit_info->oso_sp = pos->second;
419 } else {
420 ObjectFile *obj_file = GetObjectFile();
421 comp_unit_info->oso_sp.reset(new OSOInfo());
422 m_oso_map[comp_unit_info->oso_path] = comp_unit_info->oso_sp;
423 const char *oso_path = comp_unit_info->oso_path.GetCString();
424 FileSpec oso_file(oso_path, false);
425 ConstString oso_object;
426 if (oso_file.Exists()) {
427 TimeValue oso_mod_time(oso_file.GetModificationTime());
428 if (oso_mod_time != comp_unit_info->oso_mod_time) {
429 obj_file->GetModule()->ReportError(
430 "debug map object file '%s' has changed (actual time is "
431 "0x%" PRIx64 ", debug map time is 0x%" PRIx64
432 ") since this executable was linked, file will be ignored",
433 oso_file.GetPath().c_str(),
434 oso_mod_time.GetAsSecondsSinceJan1_1970(),
435 comp_unit_info->oso_mod_time.GetAsSecondsSinceJan1_1970());
436 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 }
Greg Clayton57abc5d2013-05-10 21:47:16 +0000438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 } else {
440 const bool must_exist = true;
Greg Clayton906ba472013-02-06 00:38:25 +0000441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 if (!ObjectFile::SplitArchivePathWithObject(oso_path, oso_file,
443 oso_object, must_exist)) {
444 return NULL;
Greg Clayton906ba472013-02-06 00:38:25 +0000445 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 }
447 // Always create a new module for .o files. Why? Because we
448 // use the debug map, to add new sections to each .o file and
449 // even though a .o file might not have changed, the sections
450 // that get added to the .o file can change.
451 ArchSpec oso_arch;
452 // Only adopt the architecture from the module (not the vendor or OS)
453 // since .o files for "i386-apple-ios" will historically show up as
454 // "i386-apple-macosx"
455 // due to the lack of a LC_VERSION_MIN_MACOSX or LC_VERSION_MIN_IPHONEOS
456 // load command...
457 oso_arch.SetTriple(m_obj_file->GetModule()
458 ->GetArchitecture()
459 .GetTriple()
460 .getArchName()
461 .str()
462 .c_str());
463 comp_unit_info->oso_sp->module_sp.reset(new DebugMapModule(
464 obj_file->GetModule(), GetCompUnitInfoIndex(comp_unit_info), oso_file,
465 oso_arch, oso_object ? &oso_object : NULL, 0,
466 oso_object ? &comp_unit_info->oso_mod_time : NULL));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 }
469 if (comp_unit_info->oso_sp)
470 return comp_unit_info->oso_sp->module_sp.get();
471 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472}
473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474bool SymbolFileDWARFDebugMap::GetFileSpecForSO(uint32_t oso_idx,
475 FileSpec &file_spec) {
476 if (oso_idx < m_compile_unit_infos.size()) {
477 if (m_compile_unit_infos[oso_idx].so_file) {
478 file_spec = m_compile_unit_infos[oso_idx].so_file;
479 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481 }
482 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483}
484
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex(uint32_t oso_idx) {
486 Module *oso_module = GetModuleByOSOIndex(oso_idx);
487 if (oso_module)
488 return oso_module->GetObjectFile();
489 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490}
491
492SymbolFileDWARF *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493SymbolFileDWARFDebugMap::GetSymbolFile(const SymbolContext &sc) {
494 CompileUnitInfo *comp_unit_info = GetCompUnitInfo(sc);
495 if (comp_unit_info)
496 return GetSymbolFileByCompUnitInfo(comp_unit_info);
497 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498}
499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo(
501 CompileUnitInfo *comp_unit_info) {
502 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
503 if (oso_module)
504 return oso_module->GetObjectFile();
505 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506}
507
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508uint32_t SymbolFileDWARFDebugMap::GetCompUnitInfoIndex(
509 const CompileUnitInfo *comp_unit_info) {
510 if (!m_compile_unit_infos.empty()) {
511 const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front();
512 const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back();
513 if (first_comp_unit_info <= comp_unit_info &&
514 comp_unit_info <= last_comp_unit_info)
515 return comp_unit_info - first_comp_unit_info;
516 }
517 return UINT32_MAX;
518}
519
520SymbolFileDWARF *
521SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex(uint32_t oso_idx) {
522 if (oso_idx < m_compile_unit_infos.size())
523 return GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
524 return NULL;
525}
526
527SymbolFileDWARF *
528SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file) {
529 if (sym_file &&
530 sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic())
531 return (SymbolFileDWARF *)sym_file;
532 return NULL;
533}
534
535SymbolFileDWARF *SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo(
536 CompileUnitInfo *comp_unit_info) {
537 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
538 if (oso_module) {
539 SymbolVendor *sym_vendor = oso_module->GetSymbolVendor();
540 if (sym_vendor)
541 return GetSymbolFileAsSymbolFileDWARF(sym_vendor->GetSymbolFile());
542 }
543 return NULL;
544}
545
546uint32_t SymbolFileDWARFDebugMap::CalculateAbilities() {
547 // In order to get the abilities of this plug-in, we look at the list of
548 // N_OSO entries (object files) from the symbol table and make sure that
549 // these files exist and also contain valid DWARF. If we get any of that
550 // then we return the abilities of the first N_OSO's DWARF.
551
552 const uint32_t oso_index_count = GetNumCompileUnits();
553 if (oso_index_count > 0) {
554 InitOSO();
555 if (!m_compile_unit_infos.empty()) {
556 return SymbolFile::CompileUnits | SymbolFile::Functions |
557 SymbolFile::Blocks | SymbolFile::GlobalVariables |
558 SymbolFile::LocalVariables | SymbolFile::VariableTypes |
559 SymbolFile::LineTables;
Greg Clayton81c22f62011-10-19 18:09:39 +0000560 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 }
562 return 0;
Greg Clayton81c22f62011-10-19 18:09:39 +0000563}
564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565uint32_t SymbolFileDWARFDebugMap::GetNumCompileUnits() {
566 InitOSO();
567 return m_compile_unit_infos.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568}
569
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570CompUnitSP SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) {
571 CompUnitSP comp_unit_sp;
572 const uint32_t cu_count = GetNumCompileUnits();
Greg Clayton57abc5d2013-05-10 21:47:16 +0000573
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 if (cu_idx < cu_count) {
575 Module *oso_module = GetModuleByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
576 if (oso_module) {
577 FileSpec so_file_spec;
578 if (GetFileSpecForSO(cu_idx, so_file_spec)) {
579 // User zero as the ID to match the compile unit at offset
580 // zero in each .o file since each .o file can only have
581 // one compile unit for now.
582 lldb::user_id_t cu_id = 0;
583 m_compile_unit_infos[cu_idx].compile_unit_sp.reset(
584 new CompileUnit(m_obj_file->GetModule(), NULL, so_file_spec, cu_id,
585 eLanguageTypeUnknown, eLazyBoolCalculate));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
588 // Let our symbol vendor know about this compile unit
589 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
590 cu_idx, m_compile_unit_infos[cu_idx].compile_unit_sp);
Greg Clayton3046e662013-07-10 01:23:25 +0000591 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 comp_unit_sp = m_compile_unit_infos[cu_idx].compile_unit_sp;
595 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 return comp_unit_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598}
599
600SymbolFileDWARFDebugMap::CompileUnitInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601SymbolFileDWARFDebugMap::GetCompUnitInfo(const SymbolContext &sc) {
602 const uint32_t cu_count = GetNumCompileUnits();
603 for (uint32_t i = 0; i < cu_count; ++i) {
604 if (sc.comp_unit == m_compile_unit_infos[i].compile_unit_sp.get())
605 return &m_compile_unit_infos[i];
606 }
607 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608}
609
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610size_t SymbolFileDWARFDebugMap::GetCompUnitInfosForModule(
611 const lldb_private::Module *module,
612 std::vector<CompileUnitInfo *> &cu_infos) {
613 const uint32_t cu_count = GetNumCompileUnits();
614 for (uint32_t i = 0; i < cu_count; ++i) {
615 if (module == GetModuleByCompUnitInfo(&m_compile_unit_infos[i]))
616 cu_infos.push_back(&m_compile_unit_infos[i]);
617 }
618 return cu_infos.size();
Greg Clayton1f746072012-08-29 21:13:06 +0000619}
620
621lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622SymbolFileDWARFDebugMap::ParseCompileUnitLanguage(const SymbolContext &sc) {
623 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
624 if (oso_dwarf)
625 return oso_dwarf->ParseCompileUnitLanguage(sc);
626 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000627}
628
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630SymbolFileDWARFDebugMap::ParseCompileUnitFunctions(const SymbolContext &sc) {
631 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
632 if (oso_dwarf)
633 return oso_dwarf->ParseCompileUnitFunctions(sc);
634 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635}
636
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637bool SymbolFileDWARFDebugMap::ParseCompileUnitLineTable(
638 const SymbolContext &sc) {
639 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
640 if (oso_dwarf)
641 return oso_dwarf->ParseCompileUnitLineTable(sc);
642 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643}
644
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645bool SymbolFileDWARFDebugMap::ParseCompileUnitDebugMacros(
646 const SymbolContext &sc) {
647 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
648 if (oso_dwarf)
649 return oso_dwarf->ParseCompileUnitDebugMacros(sc);
650 return false;
Siva Chandrad8335e92015-12-16 00:22:08 +0000651}
652
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653bool SymbolFileDWARFDebugMap::ParseCompileUnitSupportFiles(
654 const SymbolContext &sc, FileSpecList &support_files) {
655 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
656 if (oso_dwarf)
657 return oso_dwarf->ParseCompileUnitSupportFiles(sc, support_files);
658 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659}
660
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661bool SymbolFileDWARFDebugMap::ParseCompileUnitIsOptimized(
662 const lldb_private::SymbolContext &sc) {
663 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
664 if (oso_dwarf)
665 return oso_dwarf->ParseCompileUnitIsOptimized(sc);
666 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000667}
668
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669bool SymbolFileDWARFDebugMap::ParseImportedModules(
670 const SymbolContext &sc, std::vector<ConstString> &imported_modules) {
671 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
672 if (oso_dwarf)
673 return oso_dwarf->ParseImportedModules(sc, imported_modules);
674 return false;
675}
676
677size_t SymbolFileDWARFDebugMap::ParseFunctionBlocks(const SymbolContext &sc) {
678 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
679 if (oso_dwarf)
680 return oso_dwarf->ParseFunctionBlocks(sc);
681 return 0;
682}
683
684size_t SymbolFileDWARFDebugMap::ParseTypes(const SymbolContext &sc) {
685 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
686 if (oso_dwarf)
687 return oso_dwarf->ParseTypes(sc);
688 return 0;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000689}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690
691size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692SymbolFileDWARFDebugMap::ParseVariablesForContext(const SymbolContext &sc) {
693 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
694 if (oso_dwarf)
695 return oso_dwarf->ParseVariablesForContext(sc);
696 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697}
698
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699Type *SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) {
700 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
701 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
702 if (oso_dwarf)
703 return oso_dwarf->ResolveTypeUID(type_uid);
704 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705}
706
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707bool SymbolFileDWARFDebugMap::CompleteType(CompilerType &compiler_type) {
708 bool success = false;
709 if (compiler_type) {
710 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
711 if (oso_dwarf->HasForwardDeclForClangType(compiler_type)) {
712 oso_dwarf->CompleteType(compiler_type);
713 success = true;
714 return true;
715 }
716 return false;
717 });
718 }
719 return success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720}
721
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext(
723 const Address &exe_so_addr, uint32_t resolve_scope, SymbolContext &sc) {
724 uint32_t resolved_flags = 0;
725 Symtab *symtab = m_obj_file->GetSymtab();
726 if (symtab) {
727 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 const DebugMap::Entry *debug_map_entry =
730 m_debug_map.FindEntryThatContains(exe_file_addr);
731 if (debug_map_entry) {
732
733 sc.symbol =
734 symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex());
735
736 if (sc.symbol != NULL) {
737 resolved_flags |= eSymbolContextSymbol;
738
739 uint32_t oso_idx = 0;
740 CompileUnitInfo *comp_unit_info =
741 GetCompileUnitInfoForSymbolWithID(sc.symbol->GetID(), &oso_idx);
742 if (comp_unit_info) {
743 comp_unit_info->GetFileRangeMap(this);
744 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
745 if (oso_module) {
746 lldb::addr_t oso_file_addr =
747 exe_file_addr - debug_map_entry->GetRangeBase() +
748 debug_map_entry->data.GetOSOFileAddress();
749 Address oso_so_addr;
750 if (oso_module->ResolveFileAddress(oso_file_addr, oso_so_addr)) {
751 resolved_flags |=
752 oso_module->GetSymbolVendor()->ResolveSymbolContext(
753 oso_so_addr, resolve_scope, sc);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000754 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 }
756 }
757 }
Greg Clayton6dc8d582015-08-18 22:32:36 +0000758 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 }
760 return resolved_flags;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000761}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext(
764 const FileSpec &file_spec, uint32_t line, bool check_inlines,
765 uint32_t resolve_scope, SymbolContextList &sc_list) {
766 const uint32_t initial = sc_list.GetSize();
767 const uint32_t cu_count = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 for (uint32_t i = 0; i < cu_count; ++i) {
770 // If we are checking for inlines, then we need to look through all
771 // compile units no matter if "file_spec" matches.
772 bool resolve = check_inlines;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 if (!resolve) {
775 FileSpec so_file_spec;
776 if (GetFileSpecForSO(i, so_file_spec)) {
777 // Match the full path if the incoming file_spec has a directory (not
778 // just a basename)
779 const bool full_match = (bool)file_spec.GetDirectory();
780 resolve = FileSpec::Equal(file_spec, so_file_spec, full_match);
781 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 if (resolve) {
784 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(i);
785 if (oso_dwarf)
786 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines,
787 resolve_scope, sc_list);
788 }
789 }
790 return sc_list.GetSize() - initial;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791}
792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793uint32_t SymbolFileDWARFDebugMap::PrivateFindGlobalVariables(
794 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
795 const std::vector<uint32_t>
796 &indexes, // Indexes into the symbol table that match "name"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000798 VariableList &variables) {
799 const uint32_t original_size = variables.GetSize();
800 const size_t match_count = indexes.size();
801 for (size_t i = 0; i < match_count; ++i) {
802 uint32_t oso_idx;
803 CompileUnitInfo *comp_unit_info =
804 GetCompileUnitInfoForSymbolWithIndex(indexes[i], &oso_idx);
805 if (comp_unit_info) {
806 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
807 if (oso_dwarf) {
808 if (oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, true,
809 max_matches, variables))
810 if (variables.GetSize() > max_matches)
811 break;
812 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 }
815 return variables.GetSize() - original_size;
816}
817
818uint32_t SymbolFileDWARFDebugMap::FindGlobalVariables(
819 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
820 bool append, uint32_t max_matches, VariableList &variables) {
821
822 // If we aren't appending the results to this list, then clear the list
823 if (!append)
824 variables.Clear();
825
826 // Remember how many variables are in the list before we search in case
827 // we are appending the results to a variable list.
828 const uint32_t original_size = variables.GetSize();
829
830 uint32_t total_matches = 0;
831
832 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
833 const uint32_t oso_matches = oso_dwarf->FindGlobalVariables(
834 name, parent_decl_ctx, true, max_matches, variables);
835 if (oso_matches > 0) {
836 total_matches += oso_matches;
837
838 // Are we getting all matches?
839 if (max_matches == UINT32_MAX)
840 return false; // Yep, continue getting everything
841
842 // If we have found enough matches, lets get out
843 if (max_matches >= total_matches)
844 return true;
845
846 // Update the max matches for any subsequent calls to find globals
847 // in any other object files with DWARF
848 max_matches -= oso_matches;
849 }
850
851 return false;
852 });
853
854 // Return the number of variable that were appended to the list
855 return variables.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856}
857
858uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859SymbolFileDWARFDebugMap::FindGlobalVariables(const RegularExpression &regex,
860 bool append, uint32_t max_matches,
861 VariableList &variables) {
862 // If we aren't appending the results to this list, then clear the list
863 if (!append)
864 variables.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866 // Remember how many variables are in the list before we search in case
867 // we are appending the results to a variable list.
868 const uint32_t original_size = variables.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870 uint32_t total_matches = 0;
871 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
872 const uint32_t oso_matches =
873 oso_dwarf->FindGlobalVariables(regex, true, max_matches, variables);
874 if (oso_matches > 0) {
875 total_matches += oso_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877 // Are we getting all matches?
878 if (max_matches == UINT32_MAX)
879 return false; // Yep, continue getting everything
Sean Callanan0dc848c2015-04-01 20:43:23 +0000880
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881 // If we have found enough matches, lets get out
882 if (max_matches >= total_matches)
883 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885 // Update the max matches for any subsequent calls to find globals
886 // in any other object files with DWARF
887 max_matches -= oso_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888 }
889
Kate Stoneb9c1b512016-09-06 20:57:50 +0000890 return false;
891 });
892
893 // Return the number of variable that were appended to the list
894 return variables.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex(
898 uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
899 const uint32_t symbol_idx = *symbol_idx_ptr;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 if (symbol_idx < comp_unit_info->first_symbol_index)
902 return -1;
903
904 if (symbol_idx <= comp_unit_info->last_symbol_index)
905 return 0;
906
907 return 1;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000908}
909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID(
911 user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
912 const user_id_t symbol_id = *symbol_idx_ptr;
913
914 if (symbol_id < comp_unit_info->first_symbol_id)
915 return -1;
916
917 if (symbol_id <= comp_unit_info->last_symbol_id)
918 return 0;
919
920 return 1;
Greg Clayton3ef3fc62010-08-17 23:16:15 +0000921}
922
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923SymbolFileDWARFDebugMap::CompileUnitInfo *
924SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex(
925 uint32_t symbol_idx, uint32_t *oso_idx_ptr) {
926 const uint32_t oso_index_count = m_compile_unit_infos.size();
927 CompileUnitInfo *comp_unit_info = NULL;
928 if (oso_index_count) {
929 comp_unit_info = (CompileUnitInfo *)bsearch(
930 &symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
931 sizeof(CompileUnitInfo),
932 (ComparisonFunction)SymbolContainsSymbolWithIndex);
933 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934
Kate Stoneb9c1b512016-09-06 20:57:50 +0000935 if (oso_idx_ptr) {
936 if (comp_unit_info != NULL)
937 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939 *oso_idx_ptr = UINT32_MAX;
940 }
941 return comp_unit_info;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942}
943
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944SymbolFileDWARFDebugMap::CompileUnitInfo *
945SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID(
946 user_id_t symbol_id, uint32_t *oso_idx_ptr) {
947 const uint32_t oso_index_count = m_compile_unit_infos.size();
948 CompileUnitInfo *comp_unit_info = NULL;
949 if (oso_index_count) {
950 comp_unit_info = (CompileUnitInfo *)::bsearch(
951 &symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
952 sizeof(CompileUnitInfo),
953 (ComparisonFunction)SymbolContainsSymbolWithID);
954 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 if (oso_idx_ptr) {
957 if (comp_unit_info != NULL)
958 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
Greg Clayton57a6b992010-08-17 00:35:34 +0000959 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960 *oso_idx_ptr = UINT32_MAX;
961 }
962 return comp_unit_info;
963}
Greg Clayton57a6b992010-08-17 00:35:34 +0000964
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp,
966 SymbolContextList &sc_list,
967 uint32_t start_idx) {
968 // We found functions in .o files. Not all functions in the .o files
969 // will have made it into the final output file. The ones that did
970 // make it into the final output file will have a section whose module
971 // matches the module from the ObjectFile for this SymbolFile. When
972 // the modules don't match, then we have something that was in a
973 // .o file, but doesn't map to anything in the final executable.
974 uint32_t i = start_idx;
975 while (i < sc_list.GetSize()) {
976 SymbolContext sc;
977 sc_list.GetContextAtIndex(i, sc);
978 if (sc.function) {
979 const SectionSP section_sp(
980 sc.function->GetAddressRange().GetBaseAddress().GetSection());
981 if (section_sp->GetModule() != module_sp) {
982 sc_list.RemoveContextAtIndex(i);
983 continue;
984 }
985 }
986 ++i;
987 }
988}
989
990uint32_t SymbolFileDWARFDebugMap::FindFunctions(
991 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
992 uint32_t name_type_mask, bool include_inlines, bool append,
993 SymbolContextList &sc_list) {
994 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
995 "SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
996 name.GetCString());
997
998 uint32_t initial_size = 0;
999 if (append)
1000 initial_size = sc_list.GetSize();
1001 else
1002 sc_list.Clear();
1003
1004 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1005 uint32_t sc_idx = sc_list.GetSize();
1006 if (oso_dwarf->FindFunctions(name, parent_decl_ctx, name_type_mask,
1007 include_inlines, true, sc_list)) {
1008 RemoveFunctionsWithModuleNotEqualTo(m_obj_file->GetModule(), sc_list,
1009 sc_idx);
1010 }
1011 return false;
1012 });
1013
1014 return sc_list.GetSize() - initial_size;
1015}
1016
1017uint32_t SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression &regex,
1018 bool include_inlines,
1019 bool append,
1020 SymbolContextList &sc_list) {
1021 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
1022 "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00001023 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024
1025 uint32_t initial_size = 0;
1026 if (append)
1027 initial_size = sc_list.GetSize();
1028 else
1029 sc_list.Clear();
1030
1031 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1032 uint32_t sc_idx = sc_list.GetSize();
1033
1034 if (oso_dwarf->FindFunctions(regex, include_inlines, true, sc_list)) {
1035 RemoveFunctionsWithModuleNotEqualTo(m_obj_file->GetModule(), sc_list,
1036 sc_idx);
1037 }
1038 return false;
1039 });
1040
1041 return sc_list.GetSize() - initial_size;
1042}
1043
1044size_t SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope,
1045 uint32_t type_mask,
1046 TypeList &type_list) {
1047 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
1048 "SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)",
1049 type_mask);
1050
1051 uint32_t initial_size = type_list.GetSize();
1052 SymbolFileDWARF *oso_dwarf = NULL;
1053 if (sc_scope) {
1054 SymbolContext sc;
1055 sc_scope->CalculateSymbolContext(&sc);
1056
1057 CompileUnitInfo *cu_info = GetCompUnitInfo(sc);
1058 if (cu_info) {
1059 oso_dwarf = GetSymbolFileByCompUnitInfo(cu_info);
1060 if (oso_dwarf)
1061 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1062 }
1063 } else {
Sean Callanan0dc848c2015-04-01 20:43:23 +00001064 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1066 return false;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001067 });
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068 }
1069 return type_list.GetSize() - initial_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070}
1071
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072TypeSP SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext(
1073 const DWARFDeclContext &die_decl_ctx) {
1074 TypeSP type_sp;
1075 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1076 type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
1077 return ((bool)type_sp);
1078 });
1079 return type_sp;
Greg Claytonf02500c2013-06-18 22:51:05 +00001080}
1081
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082bool SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type(
1083 SymbolFileDWARF *skip_dwarf_oso) {
1084 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
1085 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001086 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 if (skip_dwarf_oso != oso_dwarf &&
1088 oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(NULL)) {
1089 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
1090 return true;
1091 }
1092 return false;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001093 });
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 }
1095 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00001096}
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001097
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE(
1099 const DWARFDIE &die, const ConstString &type_name,
1100 bool must_be_implementation) {
1101 // If we have a debug map, we will have an Objective C symbol whose name is
1102 // the type name and whose type is eSymbolTypeObjCClass. If we can find that
1103 // symbol and find its containing parent, we can locate the .o file that will
1104 // contain the implementation definition since it will be scoped inside the
1105 // N_SO
1106 // and we can then locate the SymbolFileDWARF that corresponds to that N_SO.
1107 SymbolFileDWARF *oso_dwarf = NULL;
1108 TypeSP type_sp;
1109 ObjectFile *module_objfile = m_obj_file->GetModule()->GetObjectFile();
1110 if (module_objfile) {
1111 Symtab *symtab = module_objfile->GetSymtab();
1112 if (symtab) {
1113 Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
1114 type_name, eSymbolTypeObjCClass, Symtab::eDebugAny,
1115 Symtab::eVisibilityAny);
1116 if (objc_class_symbol) {
1117 // Get the N_SO symbol that contains the objective C class symbol as
1118 // this
1119 // should be the .o file that contains the real definition...
1120 const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol);
Greg Claytonc7f03b62012-01-12 04:33:28 +00001121
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 if (source_file_symbol &&
1123 source_file_symbol->GetType() == eSymbolTypeSourceFile) {
1124 const uint32_t source_file_symbol_idx =
1125 symtab->GetIndexForSymbol(source_file_symbol);
1126 if (source_file_symbol_idx != UINT32_MAX) {
1127 CompileUnitInfo *compile_unit_info =
1128 GetCompileUnitInfoForSymbolWithIndex(source_file_symbol_idx,
1129 NULL);
1130 if (compile_unit_info) {
1131 oso_dwarf = GetSymbolFileByCompUnitInfo(compile_unit_info);
1132 if (oso_dwarf) {
1133 TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1134 die, type_name, must_be_implementation));
1135 if (type_sp) {
1136 return type_sp;
Greg Claytonbc63aac2015-02-25 22:41:34 +00001137 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 }
Greg Claytonbc63aac2015-02-25 22:41:34 +00001139 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 }
Greg Claytonbc63aac2015-02-25 22:41:34 +00001141 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 }
Greg Clayton901c5ca2011-12-03 04:40:03 +00001143 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144 }
Greg Claytonbc63aac2015-02-25 22:41:34 +00001145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146 // Only search all .o files for the definition if we don't need the
1147 // implementation
1148 // because otherwise, with a valid debug map we should have the ObjC class
1149 // symbol and
1150 // the code above should have found it.
1151 if (must_be_implementation == false) {
1152 TypeSP type_sp;
1153
1154 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1155 type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1156 die, type_name, must_be_implementation);
1157 return (bool)type_sp;
1158 });
1159
1160 return type_sp;
1161 }
1162 return TypeSP();
Greg Clayton901c5ca2011-12-03 04:40:03 +00001163}
1164
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165uint32_t SymbolFileDWARFDebugMap::FindTypes(
1166 const SymbolContext &sc, const ConstString &name,
1167 const CompilerDeclContext *parent_decl_ctx, bool append,
Greg Claytonae088e52016-02-10 21:28:13 +00001168 uint32_t max_matches,
1169 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170 TypeMap &types) {
1171 if (!append)
1172 types.Clear();
Greg Clayton6dbd3982010-09-15 05:51:24 +00001173
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174 const uint32_t initial_types_size = types.GetSize();
1175 SymbolFileDWARF *oso_dwarf;
Greg Clayton6dbd3982010-09-15 05:51:24 +00001176
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 if (sc.comp_unit) {
1178 oso_dwarf = GetSymbolFile(sc);
1179 if (oso_dwarf)
1180 return oso_dwarf->FindTypes(sc, name, parent_decl_ctx, append,
1181 max_matches, searched_symbol_files, types);
1182 } else {
1183 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1184 oso_dwarf->FindTypes(sc, name, parent_decl_ctx, append, max_matches,
1185 searched_symbol_files, types);
1186 if (types.GetSize() >= max_matches)
1187 return true;
1188 else
1189 return false;
1190 });
1191 }
Greg Clayton6dbd3982010-09-15 05:51:24 +00001192
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 return types.GetSize() - initial_types_size;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001194}
1195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196//
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197// uint32_t
1198// SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const
1199// RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding
1200// encoding, lldb::user_id_t udt_uid, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201//{
1202// SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
1203// if (oso_dwarf)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204// return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding,
1205// udt_uid, types);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206// return 0;
1207//}
1208
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace(
1210 const lldb_private::SymbolContext &sc,
1211 const lldb_private::ConstString &name,
1212 const CompilerDeclContext *parent_decl_ctx) {
1213 CompilerDeclContext matching_namespace;
1214 SymbolFileDWARF *oso_dwarf;
Greg Clayton96d7d742010-11-10 23:42:09 +00001215
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 if (sc.comp_unit) {
1217 oso_dwarf = GetSymbolFile(sc);
1218 if (oso_dwarf)
1219 matching_namespace = oso_dwarf->FindNamespace(sc, name, parent_decl_ctx);
1220 } else {
1221 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1222 matching_namespace = oso_dwarf->FindNamespace(sc, name, parent_decl_ctx);
Greg Clayton96d7d742010-11-10 23:42:09 +00001223
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224 return (bool)matching_namespace;
1225 });
1226 }
Greg Clayton96d7d742010-11-10 23:42:09 +00001227
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228 return matching_namespace;
Greg Clayton96d7d742010-11-10 23:42:09 +00001229}
1230
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231//------------------------------------------------------------------
1232// PluginInterface protocol
1233//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginName() {
1235 return GetPluginNameStatic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236}
1237
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238uint32_t SymbolFileDWARFDebugMap::GetPluginVersion() { return 1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239
Greg Clayton1f746072012-08-29 21:13:06 +00001240lldb::CompUnitSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001241SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf) {
1242 if (oso_dwarf) {
1243 const uint32_t cu_count = GetNumCompileUnits();
1244 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1245 SymbolFileDWARF *oso_symfile =
1246 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1247 if (oso_symfile == oso_dwarf) {
1248 if (!m_compile_unit_infos[cu_idx].compile_unit_sp)
1249 m_compile_unit_infos[cu_idx].compile_unit_sp =
1250 ParseCompileUnitAtIndex(cu_idx);
Greg Clayton1f746072012-08-29 21:13:06 +00001251
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 return m_compile_unit_infos[cu_idx].compile_unit_sp;
1253 }
Greg Clayton1f746072012-08-29 21:13:06 +00001254 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 }
1256 assert(!"this shouldn't happen");
1257 return lldb::CompUnitSP();
Greg Clayton1f746072012-08-29 21:13:06 +00001258}
1259
Greg Clayton9422dd62013-03-04 21:46:16 +00001260SymbolFileDWARFDebugMap::CompileUnitInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) {
1262 if (oso_dwarf) {
1263 const uint32_t cu_count = GetNumCompileUnits();
1264 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1265 SymbolFileDWARF *oso_symfile =
1266 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1267 if (oso_symfile == oso_dwarf) {
1268 return &m_compile_unit_infos[cu_idx];
1269 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 }
1272 return NULL;
Greg Clayton9422dd62013-03-04 21:46:16 +00001273}
1274
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
1276 const CompUnitSP &cu_sp) {
1277 if (oso_dwarf) {
1278 const uint32_t cu_count = GetNumCompileUnits();
1279 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1280 SymbolFileDWARF *oso_symfile =
1281 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1282 if (oso_symfile == oso_dwarf) {
1283 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
1284 assert(m_compile_unit_infos[cu_idx].compile_unit_sp.get() ==
1285 cu_sp.get());
1286 } else {
1287 m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp;
1288 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
1289 cu_idx, cu_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00001290 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001292 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001294}
1295
Greg Clayton99558cc42015-08-24 23:46:31 +00001296CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001297SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
1298 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1299 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1300 if (oso_dwarf)
1301 return oso_dwarf->GetDeclContextForUID(type_uid);
1302 return CompilerDeclContext();
Greg Clayton81c22f62011-10-19 18:09:39 +00001303}
1304
Greg Clayton99558cc42015-08-24 23:46:31 +00001305CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1307 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1308 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1309 if (oso_dwarf)
1310 return oso_dwarf->GetDeclContextContainingUID(type_uid);
1311 return CompilerDeclContext();
Greg Clayton81c22f62011-10-19 18:09:39 +00001312}
1313
Kate Stoneb9c1b512016-09-06 20:57:50 +00001314void SymbolFileDWARFDebugMap::ParseDeclsForContext(
1315 lldb_private::CompilerDeclContext decl_ctx) {
1316 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1317 oso_dwarf->ParseDeclsForContext(decl_ctx);
1318 return true; // Keep iterating
1319 });
Greg Clayton2bd8e7b2015-10-07 22:07:33 +00001320}
1321
Kate Stoneb9c1b512016-09-06 20:57:50 +00001322bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info,
1323 lldb::addr_t exe_file_addr,
1324 lldb::addr_t exe_byte_size,
1325 lldb::addr_t oso_file_addr,
1326 lldb::addr_t oso_byte_size) {
1327 const uint32_t debug_map_idx =
1328 m_debug_map.FindEntryIndexThatContains(exe_file_addr);
1329 if (debug_map_idx != UINT32_MAX) {
1330 DebugMap::Entry *debug_map_entry =
1331 m_debug_map.FindEntryThatContains(exe_file_addr);
1332 debug_map_entry->data.SetOSOFileAddress(oso_file_addr);
1333 addr_t range_size = std::min<addr_t>(exe_byte_size, oso_byte_size);
1334 if (range_size == 0) {
1335 range_size = std::max<addr_t>(exe_byte_size, oso_byte_size);
1336 if (range_size == 0)
1337 range_size = 1;
Greg Clayton9422dd62013-03-04 21:46:16 +00001338 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 cu_info->file_range_map.Append(
1340 FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr));
1341 return true;
1342 }
1343 return false;
Greg Clayton9422dd62013-03-04 21:46:16 +00001344}
1345
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) {
1347 cu_info->file_range_map.Sort();
Greg Clayton9422dd62013-03-04 21:46:16 +00001348#if defined(DEBUG_OSO_DMAP)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349 const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this);
1350 const size_t n = oso_file_range_map.GetSize();
1351 printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n",
1352 cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str());
1353 for (size_t i = 0; i < n; ++i) {
1354 const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i);
1355 printf("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
1356 ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n",
1357 entry.GetRangeBase(), entry.GetRangeEnd(), entry.data,
1358 entry.data + entry.GetByteSize());
1359 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001360#endif
1361}
1362
1363lldb::addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
1365 lldb::addr_t oso_file_addr) {
1366 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_symfile);
1367 if (cu_info) {
1368 const FileRangeMap::Entry *oso_range_entry =
1369 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1370 if (oso_range_entry) {
1371 const DebugMap::Entry *debug_map_entry =
1372 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1373 if (debug_map_entry) {
1374 const lldb::addr_t offset =
1375 oso_file_addr - oso_range_entry->GetRangeBase();
1376 const lldb::addr_t exe_file_addr =
1377 debug_map_entry->GetRangeBase() + offset;
1378 return exe_file_addr;
1379 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001380 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 }
1382 return LLDB_INVALID_ADDRESS;
Greg Clayton9422dd62013-03-04 21:46:16 +00001383}
1384
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) {
1386 // Make sure this address hasn't been fixed already
1387 Module *exe_module = GetObjectFile()->GetModule().get();
1388 Module *addr_module = addr.GetModule().get();
1389 if (addr_module == exe_module)
1390 return true; // Address is already in terms of the main executable module
Greg Clayton9422dd62013-03-04 21:46:16 +00001391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392 CompileUnitInfo *cu_info = GetCompileUnitInfo(GetSymbolFileAsSymbolFileDWARF(
1393 addr_module->GetSymbolVendor()->GetSymbolFile()));
1394 if (cu_info) {
1395 const lldb::addr_t oso_file_addr = addr.GetFileAddress();
1396 const FileRangeMap::Entry *oso_range_entry =
1397 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1398 if (oso_range_entry) {
1399 const DebugMap::Entry *debug_map_entry =
1400 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1401 if (debug_map_entry) {
1402 const lldb::addr_t offset =
1403 oso_file_addr - oso_range_entry->GetRangeBase();
1404 const lldb::addr_t exe_file_addr =
1405 debug_map_entry->GetRangeBase() + offset;
1406 return exe_module->ResolveFileAddress(exe_file_addr, addr);
1407 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001408 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409 }
1410 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +00001411}
1412
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf,
1414 LineTable *line_table) {
1415 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf);
1416 if (cu_info)
1417 return line_table->LinkLineTable(cu_info->GetFileRangeMap(this));
1418 return NULL;
Greg Clayton9422dd62013-03-04 21:46:16 +00001419}
1420
Enrico Granatac76e60b2013-06-27 01:43:09 +00001421size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data,
1423 DWARFDebugAranges *debug_aranges) {
1424 size_t num_line_entries_added = 0;
1425 if (debug_aranges && dwarf2Data) {
1426 CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data);
1427 if (compile_unit_info) {
1428 const FileRangeMap &file_range_map =
1429 compile_unit_info->GetFileRangeMap(this);
1430 for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) {
1431 const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(idx);
1432 if (entry) {
1433 debug_aranges->AppendRange(dwarf2Data->GetID(), entry->GetRangeBase(),
1434 entry->GetRangeEnd());
1435 num_line_entries_added++;
Enrico Granatac76e60b2013-06-27 01:43:09 +00001436 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 }
Enrico Granatac76e60b2013-06-27 01:43:09 +00001438 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439 }
1440 return num_line_entries_added;
Enrico Granatac76e60b2013-06-27 01:43:09 +00001441}