1 //===-- SymbolFileDWARFDebugMap.cpp ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "SymbolFileDWARFDebugMap.h"
10 #include "DWARFDebugAranges.h"
11
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h"
14 #include "lldb/Core/PluginManager.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Host/FileSystem.h"
17 #include "lldb/Utility/RangeMap.h"
18 #include "lldb/Utility/RegularExpression.h"
19 #include "lldb/Utility/Timer.h"
20
21 //#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT
22 #if defined(DEBUG_OSO_DMAP)
23 #include "lldb/Core/StreamFile.h"
24 #endif
25
26 #include "lldb/Symbol/CompileUnit.h"
27 #include "lldb/Symbol/LineTable.h"
28 #include "lldb/Symbol/ObjectFile.h"
29 #include "lldb/Symbol/SymbolVendor.h"
30 #include "lldb/Symbol/TypeMap.h"
31 #include "lldb/Symbol/VariableList.h"
32 #include "llvm/Support/ScopedPrinter.h"
33
34 #include "LogChannelDWARF.h"
35 #include "SymbolFileDWARF.h"
36
37 #include <memory>
38
39 using namespace lldb;
40 using namespace lldb_private;
41
42 char SymbolFileDWARFDebugMap::ID;
43
44 // Subclass lldb_private::Module so we can intercept the
45 // "Module::GetObjectFile()" (so we can fixup the object file sections) and
46 // also for "Module::GetSymbolFile()" (so we can fixup the symbol file id.
47
48 const SymbolFileDWARFDebugMap::FileRangeMap &
GetFileRangeMap(SymbolFileDWARFDebugMap * exe_symfile)49 SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap(
50 SymbolFileDWARFDebugMap *exe_symfile) {
51 if (file_range_map_valid)
52 return file_range_map;
53
54 file_range_map_valid = true;
55
56 Module *oso_module = exe_symfile->GetModuleByCompUnitInfo(this);
57 if (!oso_module)
58 return file_range_map;
59
60 ObjectFile *oso_objfile = oso_module->GetObjectFile();
61 if (!oso_objfile)
62 return file_range_map;
63
64 Log *log = GetLog(DWARFLog::DebugMap);
65 LLDB_LOGF(
66 log,
67 "%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')",
68 static_cast<void *>(this),
69 oso_module->GetSpecificationDescription().c_str());
70
71 std::vector<SymbolFileDWARFDebugMap::CompileUnitInfo *> cu_infos;
72 if (exe_symfile->GetCompUnitInfosForModule(oso_module, cu_infos)) {
73 for (auto comp_unit_info : cu_infos) {
74 Symtab *exe_symtab = exe_symfile->GetObjectFile()->GetSymtab();
75 ModuleSP oso_module_sp(oso_objfile->GetModule());
76 Symtab *oso_symtab = oso_objfile->GetSymtab();
77
78 /// const uint32_t fun_resolve_flags = SymbolContext::Module |
79 /// eSymbolContextCompUnit | eSymbolContextFunction;
80 // SectionList *oso_sections = oso_objfile->Sections();
81 // Now we need to make sections that map from zero based object file
82 // addresses to where things ended up in the main executable.
83
84 assert(comp_unit_info->first_symbol_index != UINT32_MAX);
85 // End index is one past the last valid symbol index
86 const uint32_t oso_end_idx = comp_unit_info->last_symbol_index + 1;
87 for (uint32_t idx = comp_unit_info->first_symbol_index +
88 2; // Skip the N_SO and N_OSO
89 idx < oso_end_idx; ++idx) {
90 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
91 if (exe_symbol) {
92 if (!exe_symbol->IsDebug())
93 continue;
94
95 switch (exe_symbol->GetType()) {
96 default:
97 break;
98
99 case eSymbolTypeCode: {
100 // For each N_FUN, or function that we run into in the debug map we
101 // make a new section that we add to the sections found in the .o
102 // file. This new section has the file address set to what the
103 // addresses are in the .o file, and the load address is adjusted
104 // to match where it ended up in the final executable! We do this
105 // before we parse any dwarf info so that when it goes get parsed
106 // all section/offset addresses that get registered will resolve
107 // correctly to the new addresses in the main executable.
108
109 // First we find the original symbol in the .o file's symbol table
110 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(
111 exe_symbol->GetMangled().GetName(Mangled::ePreferMangled),
112 eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
113 if (oso_fun_symbol) {
114 // Add the inverse OSO file address to debug map entry mapping
115 exe_symfile->AddOSOFileRange(
116 this, exe_symbol->GetAddressRef().GetFileAddress(),
117 exe_symbol->GetByteSize(),
118 oso_fun_symbol->GetAddressRef().GetFileAddress(),
119 oso_fun_symbol->GetByteSize());
120 }
121 } break;
122
123 case eSymbolTypeData: {
124 // For each N_GSYM we remap the address for the global by making a
125 // new section that we add to the sections found in the .o file.
126 // This new section has the file address set to what the addresses
127 // are in the .o file, and the load address is adjusted to match
128 // where it ended up in the final executable! We do this before we
129 // parse any dwarf info so that when it goes get parsed all
130 // section/offset addresses that get registered will resolve
131 // correctly to the new addresses in the main executable. We
132 // initially set the section size to be 1 byte, but will need to
133 // fix up these addresses further after all globals have been
134 // parsed to span the gaps, or we can find the global variable
135 // sizes from the DWARF info as we are parsing.
136
137 // Next we find the non-stab entry that corresponds to the N_GSYM
138 // in the .o file
139 Symbol *oso_gsym_symbol =
140 oso_symtab->FindFirstSymbolWithNameAndType(
141 exe_symbol->GetMangled().GetName(Mangled::ePreferMangled),
142 eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
143 if (exe_symbol && oso_gsym_symbol && exe_symbol->ValueIsAddress() &&
144 oso_gsym_symbol->ValueIsAddress()) {
145 // Add the inverse OSO file address to debug map entry mapping
146 exe_symfile->AddOSOFileRange(
147 this, exe_symbol->GetAddressRef().GetFileAddress(),
148 exe_symbol->GetByteSize(),
149 oso_gsym_symbol->GetAddressRef().GetFileAddress(),
150 oso_gsym_symbol->GetByteSize());
151 }
152 } break;
153 }
154 }
155 }
156
157 exe_symfile->FinalizeOSOFileRanges(this);
158 // We don't need the symbols anymore for the .o files
159 oso_objfile->ClearSymtab();
160 }
161 }
162 return file_range_map;
163 }
164
165 class DebugMapModule : public Module {
166 public:
DebugMapModule(const ModuleSP & exe_module_sp,uint32_t cu_idx,const FileSpec & file_spec,const ArchSpec & arch,const ConstString * object_name,off_t object_offset,const llvm::sys::TimePoint<> object_mod_time)167 DebugMapModule(const ModuleSP &exe_module_sp, uint32_t cu_idx,
168 const FileSpec &file_spec, const ArchSpec &arch,
169 const ConstString *object_name, off_t object_offset,
170 const llvm::sys::TimePoint<> object_mod_time)
171 : Module(file_spec, arch, object_name, object_offset, object_mod_time),
172 m_exe_module_wp(exe_module_sp), m_cu_idx(cu_idx) {}
173
174 ~DebugMapModule() override = default;
175
176 SymbolFile *
GetSymbolFile(bool can_create=true,lldb_private::Stream * feedback_strm=nullptr)177 GetSymbolFile(bool can_create = true,
178 lldb_private::Stream *feedback_strm = nullptr) override {
179 // Scope for locker
180 if (m_symfile_up.get() || !can_create)
181 return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
182
183 ModuleSP exe_module_sp(m_exe_module_wp.lock());
184 if (exe_module_sp) {
185 // Now get the object file outside of a locking scope
186 ObjectFile *oso_objfile = GetObjectFile();
187 if (oso_objfile) {
188 std::lock_guard<std::recursive_mutex> guard(m_mutex);
189 if (SymbolFile *symfile =
190 Module::GetSymbolFile(can_create, feedback_strm)) {
191 // Set a pointer to this class to set our OSO DWARF file know that
192 // the DWARF is being used along with a debug map and that it will
193 // have the remapped sections that we do below.
194 SymbolFileDWARF *oso_symfile =
195 SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(symfile);
196
197 if (!oso_symfile)
198 return nullptr;
199
200 ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
201 SymbolFile *exe_symfile = exe_module_sp->GetSymbolFile();
202
203 if (exe_objfile && exe_symfile) {
204 oso_symfile->SetDebugMapModule(exe_module_sp);
205 // Set the ID of the symbol file DWARF to the index of the OSO
206 // shifted left by 32 bits to provide a unique prefix for any
207 // UserID's that get created in the symbol file.
208 oso_symfile->SetID(((uint64_t)m_cu_idx + 1ull) << 32ull);
209 }
210 return symfile;
211 }
212 }
213 }
214 return nullptr;
215 }
216
217 protected:
218 ModuleWP m_exe_module_wp;
219 const uint32_t m_cu_idx;
220 };
221
Initialize()222 void SymbolFileDWARFDebugMap::Initialize() {
223 PluginManager::RegisterPlugin(GetPluginNameStatic(),
224 GetPluginDescriptionStatic(), CreateInstance);
225 }
226
Terminate()227 void SymbolFileDWARFDebugMap::Terminate() {
228 PluginManager::UnregisterPlugin(CreateInstance);
229 }
230
GetPluginDescriptionStatic()231 llvm::StringRef SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() {
232 return "DWARF and DWARF3 debug symbol file reader (debug map).";
233 }
234
CreateInstance(ObjectFileSP objfile_sp)235 SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFileSP objfile_sp) {
236 return new SymbolFileDWARFDebugMap(std::move(objfile_sp));
237 }
238
SymbolFileDWARFDebugMap(ObjectFileSP objfile_sp)239 SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap(ObjectFileSP objfile_sp)
240 : SymbolFileCommon(std::move(objfile_sp)), m_flags(), m_compile_unit_infos(),
241 m_func_indexes(), m_glob_indexes(),
242 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate) {}
243
244 SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() = default;
245
InitializeObject()246 void SymbolFileDWARFDebugMap::InitializeObject() {}
247
InitOSO()248 void SymbolFileDWARFDebugMap::InitOSO() {
249 if (m_flags.test(kHaveInitializedOSOs))
250 return;
251
252 m_flags.set(kHaveInitializedOSOs);
253
254 // If the object file has been stripped, there is no sense in looking further
255 // as all of the debug symbols for the debug map will not be available
256 if (m_objfile_sp->IsStripped())
257 return;
258
259 // Also make sure the file type is some sort of executable. Core files, debug
260 // info files (dSYM), object files (.o files), and stub libraries all can
261 switch (m_objfile_sp->GetType()) {
262 case ObjectFile::eTypeInvalid:
263 case ObjectFile::eTypeCoreFile:
264 case ObjectFile::eTypeDebugInfo:
265 case ObjectFile::eTypeObjectFile:
266 case ObjectFile::eTypeStubLibrary:
267 case ObjectFile::eTypeUnknown:
268 case ObjectFile::eTypeJIT:
269 return;
270
271 case ObjectFile::eTypeExecutable:
272 case ObjectFile::eTypeDynamicLinker:
273 case ObjectFile::eTypeSharedLibrary:
274 break;
275 }
276
277 // In order to get the abilities of this plug-in, we look at the list of
278 // N_OSO entries (object files) from the symbol table and make sure that
279 // these files exist and also contain valid DWARF. If we get any of that then
280 // we return the abilities of the first N_OSO's DWARF.
281
282 Symtab *symtab = m_objfile_sp->GetSymtab();
283 if (symtab) {
284 Log *log = GetLog(DWARFLog::DebugMap);
285
286 std::vector<uint32_t> oso_indexes;
287 // When a mach-o symbol is encoded, the n_type field is encoded in bits
288 // 23:16, and the n_desc field is encoded in bits 15:0.
289 //
290 // To find all N_OSO entries that are part of the DWARF + debug map we find
291 // only object file symbols with the flags value as follows: bits 23:16 ==
292 // 0x66 (N_OSO) bits 15: 0 == 0x0001 (specifies this is a debug map object
293 // file)
294 const uint32_t k_oso_symbol_flags_value = 0x660001u;
295
296 const uint32_t oso_index_count =
297 symtab->AppendSymbolIndexesWithTypeAndFlagsValue(
298 eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes);
299
300 if (oso_index_count > 0) {
301 symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes,
302 Symtab::eVisibilityAny,
303 m_func_indexes);
304 symtab->AppendSymbolIndexesWithType(eSymbolTypeData, Symtab::eDebugYes,
305 Symtab::eVisibilityAny,
306 m_glob_indexes);
307
308 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
309 symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
310
311 for (uint32_t sym_idx : m_func_indexes) {
312 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
313 lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
314 lldb::addr_t byte_size = symbol->GetByteSize();
315 DebugMap::Entry debug_map_entry(
316 file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
317 m_debug_map.Append(debug_map_entry);
318 }
319 for (uint32_t sym_idx : m_glob_indexes) {
320 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
321 lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
322 lldb::addr_t byte_size = symbol->GetByteSize();
323 DebugMap::Entry debug_map_entry(
324 file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
325 m_debug_map.Append(debug_map_entry);
326 }
327 m_debug_map.Sort();
328
329 m_compile_unit_infos.resize(oso_index_count);
330
331 for (uint32_t i = 0; i < oso_index_count; ++i) {
332 const uint32_t so_idx = oso_indexes[i] - 1;
333 const uint32_t oso_idx = oso_indexes[i];
334 const Symbol *so_symbol = symtab->SymbolAtIndex(so_idx);
335 const Symbol *oso_symbol = symtab->SymbolAtIndex(oso_idx);
336 if (so_symbol && oso_symbol &&
337 so_symbol->GetType() == eSymbolTypeSourceFile &&
338 oso_symbol->GetType() == eSymbolTypeObjectFile) {
339 m_compile_unit_infos[i].so_file.SetFile(
340 so_symbol->GetName().AsCString(), FileSpec::Style::native);
341 m_compile_unit_infos[i].oso_path = oso_symbol->GetName();
342 m_compile_unit_infos[i].oso_mod_time =
343 llvm::sys::toTimePoint(oso_symbol->GetIntegerValue(0));
344 uint32_t sibling_idx = so_symbol->GetSiblingIndex();
345 // The sibling index can't be less that or equal to the current index
346 // "i"
347 if (sibling_idx == UINT32_MAX) {
348 m_objfile_sp->GetModule()->ReportError(
349 "N_SO in symbol with UID %u has invalid sibling in debug map, "
350 "please file a bug and attach the binary listed in this error",
351 so_symbol->GetID());
352 } else {
353 const Symbol *last_symbol = symtab->SymbolAtIndex(sibling_idx - 1);
354 m_compile_unit_infos[i].first_symbol_index = so_idx;
355 m_compile_unit_infos[i].last_symbol_index = sibling_idx - 1;
356 m_compile_unit_infos[i].first_symbol_id = so_symbol->GetID();
357 m_compile_unit_infos[i].last_symbol_id = last_symbol->GetID();
358
359 LLDB_LOGF(log, "Initialized OSO 0x%8.8x: file=%s", i,
360 oso_symbol->GetName().GetCString());
361 }
362 } else {
363 if (oso_symbol == nullptr)
364 m_objfile_sp->GetModule()->ReportError(
365 "N_OSO symbol[%u] can't be found, please file a bug and attach "
366 "the binary listed in this error",
367 oso_idx);
368 else if (so_symbol == nullptr)
369 m_objfile_sp->GetModule()->ReportError(
370 "N_SO not found for N_OSO symbol[%u], please file a bug and "
371 "attach the binary listed in this error",
372 oso_idx);
373 else if (so_symbol->GetType() != eSymbolTypeSourceFile)
374 m_objfile_sp->GetModule()->ReportError(
375 "N_SO has incorrect symbol type (%u) for N_OSO symbol[%u], "
376 "please file a bug and attach the binary listed in this error",
377 so_symbol->GetType(), oso_idx);
378 else if (oso_symbol->GetType() != eSymbolTypeSourceFile)
379 m_objfile_sp->GetModule()->ReportError(
380 "N_OSO has incorrect symbol type (%u) for N_OSO symbol[%u], "
381 "please file a bug and attach the binary listed in this error",
382 oso_symbol->GetType(), oso_idx);
383 }
384 }
385 }
386 }
387 }
388
GetModuleByOSOIndex(uint32_t oso_idx)389 Module *SymbolFileDWARFDebugMap::GetModuleByOSOIndex(uint32_t oso_idx) {
390 const uint32_t cu_count = GetNumCompileUnits();
391 if (oso_idx < cu_count)
392 return GetModuleByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
393 return nullptr;
394 }
395
GetModuleByCompUnitInfo(CompileUnitInfo * comp_unit_info)396 Module *SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo(
397 CompileUnitInfo *comp_unit_info) {
398 if (!comp_unit_info->oso_sp) {
399 auto pos = m_oso_map.find(
400 {comp_unit_info->oso_path, comp_unit_info->oso_mod_time});
401 if (pos != m_oso_map.end()) {
402 comp_unit_info->oso_sp = pos->second;
403 } else {
404 ObjectFile *obj_file = GetObjectFile();
405 comp_unit_info->oso_sp = std::make_shared<OSOInfo>();
406 m_oso_map[{comp_unit_info->oso_path, comp_unit_info->oso_mod_time}] =
407 comp_unit_info->oso_sp;
408 const char *oso_path = comp_unit_info->oso_path.GetCString();
409 FileSpec oso_file(oso_path);
410 ConstString oso_object;
411 if (FileSystem::Instance().Exists(oso_file)) {
412 // The modification time returned by the FS can have a higher precision
413 // than the one from the CU.
414 auto oso_mod_time = std::chrono::time_point_cast<std::chrono::seconds>(
415 FileSystem::Instance().GetModificationTime(oso_file));
416 // A timestamp of 0 means that the linker was in deterministic mode. In
417 // that case, we should skip the check against the filesystem last
418 // modification timestamp, since it will never match.
419 if (comp_unit_info->oso_mod_time != llvm::sys::TimePoint<>() &&
420 oso_mod_time != comp_unit_info->oso_mod_time) {
421 obj_file->GetModule()->ReportError(
422 "debug map object file '%s' has changed (actual time is "
423 "%s, debug map time is %s"
424 ") since this executable was linked, file will be ignored",
425 oso_file.GetPath().c_str(), llvm::to_string(oso_mod_time).c_str(),
426 llvm::to_string(comp_unit_info->oso_mod_time).c_str());
427 return nullptr;
428 }
429
430 } else {
431 const bool must_exist = true;
432
433 if (!ObjectFile::SplitArchivePathWithObject(oso_path, oso_file,
434 oso_object, must_exist)) {
435 return nullptr;
436 }
437 }
438 // Always create a new module for .o files. Why? Because we use the debug
439 // map, to add new sections to each .o file and even though a .o file
440 // might not have changed, the sections that get added to the .o file can
441 // change.
442 ArchSpec oso_arch;
443 // Only adopt the architecture from the module (not the vendor or OS)
444 // since .o files for "i386-apple-ios" will historically show up as "i386
445 // -apple-macosx" due to the lack of a LC_VERSION_MIN_MACOSX or
446 // LC_VERSION_MIN_IPHONEOS load command...
447 oso_arch.SetTriple(m_objfile_sp->GetModule()
448 ->GetArchitecture()
449 .GetTriple()
450 .getArchName()
451 .str()
452 .c_str());
453 comp_unit_info->oso_sp->module_sp = std::make_shared<DebugMapModule>(
454 obj_file->GetModule(), GetCompUnitInfoIndex(comp_unit_info), oso_file,
455 oso_arch, oso_object ? &oso_object : nullptr, 0,
456 oso_object ? comp_unit_info->oso_mod_time : llvm::sys::TimePoint<>());
457 }
458 }
459 if (comp_unit_info->oso_sp)
460 return comp_unit_info->oso_sp->module_sp.get();
461 return nullptr;
462 }
463
GetFileSpecForSO(uint32_t oso_idx,FileSpec & file_spec)464 bool SymbolFileDWARFDebugMap::GetFileSpecForSO(uint32_t oso_idx,
465 FileSpec &file_spec) {
466 if (oso_idx < m_compile_unit_infos.size()) {
467 if (m_compile_unit_infos[oso_idx].so_file) {
468 file_spec = m_compile_unit_infos[oso_idx].so_file;
469 return true;
470 }
471 }
472 return false;
473 }
474
GetObjectFileByOSOIndex(uint32_t oso_idx)475 ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex(uint32_t oso_idx) {
476 Module *oso_module = GetModuleByOSOIndex(oso_idx);
477 if (oso_module)
478 return oso_module->GetObjectFile();
479 return nullptr;
480 }
481
482 SymbolFileDWARF *
GetSymbolFile(const SymbolContext & sc)483 SymbolFileDWARFDebugMap::GetSymbolFile(const SymbolContext &sc) {
484 return GetSymbolFile(*sc.comp_unit);
485 }
486
487 SymbolFileDWARF *
GetSymbolFile(const CompileUnit & comp_unit)488 SymbolFileDWARFDebugMap::GetSymbolFile(const CompileUnit &comp_unit) {
489 CompileUnitInfo *comp_unit_info = GetCompUnitInfo(comp_unit);
490 if (comp_unit_info)
491 return GetSymbolFileByCompUnitInfo(comp_unit_info);
492 return nullptr;
493 }
494
GetObjectFileByCompUnitInfo(CompileUnitInfo * comp_unit_info)495 ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo(
496 CompileUnitInfo *comp_unit_info) {
497 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
498 if (oso_module)
499 return oso_module->GetObjectFile();
500 return nullptr;
501 }
502
GetCompUnitInfoIndex(const CompileUnitInfo * comp_unit_info)503 uint32_t SymbolFileDWARFDebugMap::GetCompUnitInfoIndex(
504 const CompileUnitInfo *comp_unit_info) {
505 if (!m_compile_unit_infos.empty()) {
506 const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front();
507 const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back();
508 if (first_comp_unit_info <= comp_unit_info &&
509 comp_unit_info <= last_comp_unit_info)
510 return comp_unit_info - first_comp_unit_info;
511 }
512 return UINT32_MAX;
513 }
514
515 SymbolFileDWARF *
GetSymbolFileByOSOIndex(uint32_t oso_idx)516 SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex(uint32_t oso_idx) {
517 unsigned size = m_compile_unit_infos.size();
518 if (oso_idx < size)
519 return GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
520 return nullptr;
521 }
522
523 SymbolFileDWARF *
GetSymbolFileAsSymbolFileDWARF(SymbolFile * sym_file)524 SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file) {
525 if (sym_file &&
526 sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic())
527 return static_cast<SymbolFileDWARF *>(sym_file);
528 return nullptr;
529 }
530
GetSymbolFileByCompUnitInfo(CompileUnitInfo * comp_unit_info)531 SymbolFileDWARF *SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo(
532 CompileUnitInfo *comp_unit_info) {
533 if (Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info))
534 return GetSymbolFileAsSymbolFileDWARF(oso_module->GetSymbolFile());
535 return nullptr;
536 }
537
CalculateAbilities()538 uint32_t SymbolFileDWARFDebugMap::CalculateAbilities() {
539 // In order to get the abilities of this plug-in, we look at the list of
540 // N_OSO entries (object files) from the symbol table and make sure that
541 // these files exist and also contain valid DWARF. If we get any of that then
542 // we return the abilities of the first N_OSO's DWARF.
543
544 const uint32_t oso_index_count = GetNumCompileUnits();
545 if (oso_index_count > 0) {
546 InitOSO();
547 if (!m_compile_unit_infos.empty()) {
548 return SymbolFile::CompileUnits | SymbolFile::Functions |
549 SymbolFile::Blocks | SymbolFile::GlobalVariables |
550 SymbolFile::LocalVariables | SymbolFile::VariableTypes |
551 SymbolFile::LineTables;
552 }
553 }
554 return 0;
555 }
556
CalculateNumCompileUnits()557 uint32_t SymbolFileDWARFDebugMap::CalculateNumCompileUnits() {
558 InitOSO();
559 return m_compile_unit_infos.size();
560 }
561
ParseCompileUnitAtIndex(uint32_t cu_idx)562 CompUnitSP SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) {
563 CompUnitSP comp_unit_sp;
564 const uint32_t cu_count = GetNumCompileUnits();
565
566 if (cu_idx < cu_count) {
567 Module *oso_module = GetModuleByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
568 if (oso_module) {
569 FileSpec so_file_spec;
570 if (GetFileSpecForSO(cu_idx, so_file_spec)) {
571 // User zero as the ID to match the compile unit at offset zero in each
572 // .o file since each .o file can only have one compile unit for now.
573 lldb::user_id_t cu_id = 0;
574 m_compile_unit_infos[cu_idx].compile_unit_sp =
575 std::make_shared<CompileUnit>(
576 m_objfile_sp->GetModule(), nullptr, so_file_spec, cu_id,
577 eLanguageTypeUnknown, eLazyBoolCalculate);
578
579 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
580 SetCompileUnitAtIndex(cu_idx,
581 m_compile_unit_infos[cu_idx].compile_unit_sp);
582 }
583 }
584 }
585 comp_unit_sp = m_compile_unit_infos[cu_idx].compile_unit_sp;
586 }
587
588 return comp_unit_sp;
589 }
590
591 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompUnitInfo(const SymbolContext & sc)592 SymbolFileDWARFDebugMap::GetCompUnitInfo(const SymbolContext &sc) {
593 return GetCompUnitInfo(*sc.comp_unit);
594 }
595
596 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompUnitInfo(const CompileUnit & comp_unit)597 SymbolFileDWARFDebugMap::GetCompUnitInfo(const CompileUnit &comp_unit) {
598 const uint32_t cu_count = GetNumCompileUnits();
599 for (uint32_t i = 0; i < cu_count; ++i) {
600 if (&comp_unit == m_compile_unit_infos[i].compile_unit_sp.get())
601 return &m_compile_unit_infos[i];
602 }
603 return nullptr;
604 }
605
GetCompUnitInfosForModule(const lldb_private::Module * module,std::vector<CompileUnitInfo * > & cu_infos)606 size_t SymbolFileDWARFDebugMap::GetCompUnitInfosForModule(
607 const lldb_private::Module *module,
608 std::vector<CompileUnitInfo *> &cu_infos) {
609 const uint32_t cu_count = GetNumCompileUnits();
610 for (uint32_t i = 0; i < cu_count; ++i) {
611 if (module == GetModuleByCompUnitInfo(&m_compile_unit_infos[i]))
612 cu_infos.push_back(&m_compile_unit_infos[i]);
613 }
614 return cu_infos.size();
615 }
616
617 lldb::LanguageType
ParseLanguage(CompileUnit & comp_unit)618 SymbolFileDWARFDebugMap::ParseLanguage(CompileUnit &comp_unit) {
619 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
620 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
621 if (oso_dwarf)
622 return oso_dwarf->ParseLanguage(comp_unit);
623 return eLanguageTypeUnknown;
624 }
625
ParseXcodeSDK(CompileUnit & comp_unit)626 XcodeSDK SymbolFileDWARFDebugMap::ParseXcodeSDK(CompileUnit &comp_unit) {
627 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
628 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
629 if (oso_dwarf)
630 return oso_dwarf->ParseXcodeSDK(comp_unit);
631 return {};
632 }
633
ParseFunctions(CompileUnit & comp_unit)634 size_t SymbolFileDWARFDebugMap::ParseFunctions(CompileUnit &comp_unit) {
635 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
636 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
637 if (oso_dwarf)
638 return oso_dwarf->ParseFunctions(comp_unit);
639 return 0;
640 }
641
ParseLineTable(CompileUnit & comp_unit)642 bool SymbolFileDWARFDebugMap::ParseLineTable(CompileUnit &comp_unit) {
643 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
644 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
645 if (oso_dwarf)
646 return oso_dwarf->ParseLineTable(comp_unit);
647 return false;
648 }
649
ParseDebugMacros(CompileUnit & comp_unit)650 bool SymbolFileDWARFDebugMap::ParseDebugMacros(CompileUnit &comp_unit) {
651 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
652 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
653 if (oso_dwarf)
654 return oso_dwarf->ParseDebugMacros(comp_unit);
655 return false;
656 }
657
ForEachExternalModule(CompileUnit & comp_unit,llvm::DenseSet<lldb_private::SymbolFile * > & visited_symbol_files,llvm::function_ref<bool (Module &)> f)658 bool SymbolFileDWARFDebugMap::ForEachExternalModule(
659 CompileUnit &comp_unit,
660 llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,
661 llvm::function_ref<bool(Module &)> f) {
662 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
663 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
664 if (oso_dwarf)
665 return oso_dwarf->ForEachExternalModule(comp_unit, visited_symbol_files, f);
666 return false;
667 }
668
ParseSupportFiles(CompileUnit & comp_unit,FileSpecList & support_files)669 bool SymbolFileDWARFDebugMap::ParseSupportFiles(CompileUnit &comp_unit,
670 FileSpecList &support_files) {
671 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
672 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
673 if (oso_dwarf)
674 return oso_dwarf->ParseSupportFiles(comp_unit, support_files);
675 return false;
676 }
677
ParseIsOptimized(CompileUnit & comp_unit)678 bool SymbolFileDWARFDebugMap::ParseIsOptimized(CompileUnit &comp_unit) {
679 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
680 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
681 if (oso_dwarf)
682 return oso_dwarf->ParseIsOptimized(comp_unit);
683 return false;
684 }
685
ParseImportedModules(const SymbolContext & sc,std::vector<SourceModule> & imported_modules)686 bool SymbolFileDWARFDebugMap::ParseImportedModules(
687 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
688 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
689 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
690 if (oso_dwarf)
691 return oso_dwarf->ParseImportedModules(sc, imported_modules);
692 return false;
693 }
694
ParseBlocksRecursive(Function & func)695 size_t SymbolFileDWARFDebugMap::ParseBlocksRecursive(Function &func) {
696 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
697 CompileUnit *comp_unit = func.GetCompileUnit();
698 if (!comp_unit)
699 return 0;
700
701 SymbolFileDWARF *oso_dwarf = GetSymbolFile(*comp_unit);
702 if (oso_dwarf)
703 return oso_dwarf->ParseBlocksRecursive(func);
704 return 0;
705 }
706
ParseTypes(CompileUnit & comp_unit)707 size_t SymbolFileDWARFDebugMap::ParseTypes(CompileUnit &comp_unit) {
708 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
709 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
710 if (oso_dwarf)
711 return oso_dwarf->ParseTypes(comp_unit);
712 return 0;
713 }
714
715 size_t
ParseVariablesForContext(const SymbolContext & sc)716 SymbolFileDWARFDebugMap::ParseVariablesForContext(const SymbolContext &sc) {
717 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
718 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
719 if (oso_dwarf)
720 return oso_dwarf->ParseVariablesForContext(sc);
721 return 0;
722 }
723
ResolveTypeUID(lldb::user_id_t type_uid)724 Type *SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) {
725 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
726 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
727 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
728 if (oso_dwarf)
729 return oso_dwarf->ResolveTypeUID(type_uid);
730 return nullptr;
731 }
732
733 llvm::Optional<SymbolFile::ArrayInfo>
GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,const lldb_private::ExecutionContext * exe_ctx)734 SymbolFileDWARFDebugMap::GetDynamicArrayInfoForUID(
735 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
736 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
737 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
738 if (oso_dwarf)
739 return oso_dwarf->GetDynamicArrayInfoForUID(type_uid, exe_ctx);
740 return llvm::None;
741 }
742
CompleteType(CompilerType & compiler_type)743 bool SymbolFileDWARFDebugMap::CompleteType(CompilerType &compiler_type) {
744 bool success = false;
745 if (compiler_type) {
746 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
747 if (oso_dwarf->HasForwardDeclForClangType(compiler_type)) {
748 oso_dwarf->CompleteType(compiler_type);
749 success = true;
750 return true;
751 }
752 return false;
753 });
754 }
755 return success;
756 }
757
758 uint32_t
ResolveSymbolContext(const Address & exe_so_addr,SymbolContextItem resolve_scope,SymbolContext & sc)759 SymbolFileDWARFDebugMap::ResolveSymbolContext(const Address &exe_so_addr,
760 SymbolContextItem resolve_scope,
761 SymbolContext &sc) {
762 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
763 uint32_t resolved_flags = 0;
764 Symtab *symtab = m_objfile_sp->GetSymtab();
765 if (symtab) {
766 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
767
768 const DebugMap::Entry *debug_map_entry =
769 m_debug_map.FindEntryThatContains(exe_file_addr);
770 if (debug_map_entry) {
771
772 sc.symbol =
773 symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex());
774
775 if (sc.symbol != nullptr) {
776 resolved_flags |= eSymbolContextSymbol;
777
778 uint32_t oso_idx = 0;
779 CompileUnitInfo *comp_unit_info =
780 GetCompileUnitInfoForSymbolWithID(sc.symbol->GetID(), &oso_idx);
781 if (comp_unit_info) {
782 comp_unit_info->GetFileRangeMap(this);
783 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
784 if (oso_module) {
785 lldb::addr_t oso_file_addr =
786 exe_file_addr - debug_map_entry->GetRangeBase() +
787 debug_map_entry->data.GetOSOFileAddress();
788 Address oso_so_addr;
789 if (oso_module->ResolveFileAddress(oso_file_addr, oso_so_addr)) {
790 resolved_flags |=
791 oso_module->GetSymbolFile()->ResolveSymbolContext(
792 oso_so_addr, resolve_scope, sc);
793 }
794 }
795 }
796 }
797 }
798 }
799 return resolved_flags;
800 }
801
ResolveSymbolContext(const SourceLocationSpec & src_location_spec,SymbolContextItem resolve_scope,SymbolContextList & sc_list)802 uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext(
803 const SourceLocationSpec &src_location_spec,
804 SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
805 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
806 const uint32_t initial = sc_list.GetSize();
807 const uint32_t cu_count = GetNumCompileUnits();
808
809 for (uint32_t i = 0; i < cu_count; ++i) {
810 // If we are checking for inlines, then we need to look through all compile
811 // units no matter if "file_spec" matches.
812 bool resolve = src_location_spec.GetCheckInlines();
813
814 if (!resolve) {
815 FileSpec so_file_spec;
816 if (GetFileSpecForSO(i, so_file_spec))
817 resolve =
818 FileSpec::Match(src_location_spec.GetFileSpec(), so_file_spec);
819 }
820 if (resolve) {
821 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(i);
822 if (oso_dwarf)
823 oso_dwarf->ResolveSymbolContext(src_location_spec, resolve_scope,
824 sc_list);
825 }
826 }
827 return sc_list.GetSize() - initial;
828 }
829
PrivateFindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,const std::vector<uint32_t> & indexes,uint32_t max_matches,VariableList & variables)830 void SymbolFileDWARFDebugMap::PrivateFindGlobalVariables(
831 ConstString name, const CompilerDeclContext &parent_decl_ctx,
832 const std::vector<uint32_t>
833 &indexes, // Indexes into the symbol table that match "name"
834 uint32_t max_matches, VariableList &variables) {
835 const size_t match_count = indexes.size();
836 for (size_t i = 0; i < match_count; ++i) {
837 uint32_t oso_idx;
838 CompileUnitInfo *comp_unit_info =
839 GetCompileUnitInfoForSymbolWithIndex(indexes[i], &oso_idx);
840 if (comp_unit_info) {
841 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
842 if (oso_dwarf) {
843 oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
844 variables);
845 if (variables.GetSize() > max_matches)
846 break;
847 }
848 }
849 }
850 }
851
FindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,VariableList & variables)852 void SymbolFileDWARFDebugMap::FindGlobalVariables(
853 ConstString name, const CompilerDeclContext &parent_decl_ctx,
854 uint32_t max_matches, VariableList &variables) {
855 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
856 uint32_t total_matches = 0;
857
858 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
859 const uint32_t old_size = variables.GetSize();
860 oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
861 variables);
862 const uint32_t oso_matches = variables.GetSize() - old_size;
863 if (oso_matches > 0) {
864 total_matches += oso_matches;
865
866 // Are we getting all matches?
867 if (max_matches == UINT32_MAX)
868 return false; // Yep, continue getting everything
869
870 // If we have found enough matches, lets get out
871 if (max_matches >= total_matches)
872 return true;
873
874 // Update the max matches for any subsequent calls to find globals in any
875 // other object files with DWARF
876 max_matches -= oso_matches;
877 }
878
879 return false;
880 });
881 }
882
FindGlobalVariables(const RegularExpression & regex,uint32_t max_matches,VariableList & variables)883 void SymbolFileDWARFDebugMap::FindGlobalVariables(
884 const RegularExpression ®ex, uint32_t max_matches,
885 VariableList &variables) {
886 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
887 uint32_t total_matches = 0;
888 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
889 const uint32_t old_size = variables.GetSize();
890 oso_dwarf->FindGlobalVariables(regex, max_matches, variables);
891
892 const uint32_t oso_matches = variables.GetSize() - old_size;
893 if (oso_matches > 0) {
894 total_matches += oso_matches;
895
896 // Are we getting all matches?
897 if (max_matches == UINT32_MAX)
898 return false; // Yep, continue getting everything
899
900 // If we have found enough matches, lets get out
901 if (max_matches >= total_matches)
902 return true;
903
904 // Update the max matches for any subsequent calls to find globals in any
905 // other object files with DWARF
906 max_matches -= oso_matches;
907 }
908
909 return false;
910 });
911 }
912
SymbolContainsSymbolWithIndex(uint32_t * symbol_idx_ptr,const CompileUnitInfo * comp_unit_info)913 int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex(
914 uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
915 const uint32_t symbol_idx = *symbol_idx_ptr;
916
917 if (symbol_idx < comp_unit_info->first_symbol_index)
918 return -1;
919
920 if (symbol_idx <= comp_unit_info->last_symbol_index)
921 return 0;
922
923 return 1;
924 }
925
SymbolContainsSymbolWithID(user_id_t * symbol_idx_ptr,const CompileUnitInfo * comp_unit_info)926 int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID(
927 user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
928 const user_id_t symbol_id = *symbol_idx_ptr;
929
930 if (symbol_id < comp_unit_info->first_symbol_id)
931 return -1;
932
933 if (symbol_id <= comp_unit_info->last_symbol_id)
934 return 0;
935
936 return 1;
937 }
938
939 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfoForSymbolWithIndex(uint32_t symbol_idx,uint32_t * oso_idx_ptr)940 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex(
941 uint32_t symbol_idx, uint32_t *oso_idx_ptr) {
942 const uint32_t oso_index_count = m_compile_unit_infos.size();
943 CompileUnitInfo *comp_unit_info = nullptr;
944 if (oso_index_count) {
945 comp_unit_info = (CompileUnitInfo *)bsearch(
946 &symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
947 sizeof(CompileUnitInfo),
948 (ComparisonFunction)SymbolContainsSymbolWithIndex);
949 }
950
951 if (oso_idx_ptr) {
952 if (comp_unit_info != nullptr)
953 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
954 else
955 *oso_idx_ptr = UINT32_MAX;
956 }
957 return comp_unit_info;
958 }
959
960 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfoForSymbolWithID(user_id_t symbol_id,uint32_t * oso_idx_ptr)961 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID(
962 user_id_t symbol_id, uint32_t *oso_idx_ptr) {
963 const uint32_t oso_index_count = m_compile_unit_infos.size();
964 CompileUnitInfo *comp_unit_info = nullptr;
965 if (oso_index_count) {
966 comp_unit_info = (CompileUnitInfo *)::bsearch(
967 &symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
968 sizeof(CompileUnitInfo),
969 (ComparisonFunction)SymbolContainsSymbolWithID);
970 }
971
972 if (oso_idx_ptr) {
973 if (comp_unit_info != nullptr)
974 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
975 else
976 *oso_idx_ptr = UINT32_MAX;
977 }
978 return comp_unit_info;
979 }
980
RemoveFunctionsWithModuleNotEqualTo(const ModuleSP & module_sp,SymbolContextList & sc_list,uint32_t start_idx)981 static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp,
982 SymbolContextList &sc_list,
983 uint32_t start_idx) {
984 // We found functions in .o files. Not all functions in the .o files will
985 // have made it into the final output file. The ones that did make it into
986 // the final output file will have a section whose module matches the module
987 // from the ObjectFile for this SymbolFile. When the modules don't match,
988 // then we have something that was in a .o file, but doesn't map to anything
989 // in the final executable.
990 uint32_t i = start_idx;
991 while (i < sc_list.GetSize()) {
992 SymbolContext sc;
993 sc_list.GetContextAtIndex(i, sc);
994 if (sc.function) {
995 const SectionSP section_sp(
996 sc.function->GetAddressRange().GetBaseAddress().GetSection());
997 if (section_sp->GetModule() != module_sp) {
998 sc_list.RemoveContextAtIndex(i);
999 continue;
1000 }
1001 }
1002 ++i;
1003 }
1004 }
1005
FindFunctions(ConstString name,const CompilerDeclContext & parent_decl_ctx,FunctionNameType name_type_mask,bool include_inlines,SymbolContextList & sc_list)1006 void SymbolFileDWARFDebugMap::FindFunctions(
1007 ConstString name, const CompilerDeclContext &parent_decl_ctx,
1008 FunctionNameType name_type_mask, bool include_inlines,
1009 SymbolContextList &sc_list) {
1010 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1011 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
1012 name.GetCString());
1013
1014 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1015 uint32_t sc_idx = sc_list.GetSize();
1016 oso_dwarf->FindFunctions(name, parent_decl_ctx, name_type_mask,
1017 include_inlines, sc_list);
1018 if (!sc_list.IsEmpty()) {
1019 RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list,
1020 sc_idx);
1021 }
1022 return false;
1023 });
1024 }
1025
FindFunctions(const RegularExpression & regex,bool include_inlines,SymbolContextList & sc_list)1026 void SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression ®ex,
1027 bool include_inlines,
1028 SymbolContextList &sc_list) {
1029 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1030 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
1031 regex.GetText().str().c_str());
1032
1033 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1034 uint32_t sc_idx = sc_list.GetSize();
1035
1036 oso_dwarf->FindFunctions(regex, include_inlines, sc_list);
1037 if (!sc_list.IsEmpty()) {
1038 RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list,
1039 sc_idx);
1040 }
1041 return false;
1042 });
1043 }
1044
GetTypes(SymbolContextScope * sc_scope,lldb::TypeClass type_mask,TypeList & type_list)1045 void SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope,
1046 lldb::TypeClass type_mask,
1047 TypeList &type_list) {
1048 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1049 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)",
1050 type_mask);
1051
1052 SymbolFileDWARF *oso_dwarf = nullptr;
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 {
1064 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1065 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1066 return false;
1067 });
1068 }
1069 }
1070
1071 std::vector<std::unique_ptr<lldb_private::CallEdge>>
ParseCallEdgesInFunction(UserID func_id)1072 SymbolFileDWARFDebugMap::ParseCallEdgesInFunction(UserID func_id) {
1073 uint32_t oso_idx = GetOSOIndexFromUserID(func_id.GetID());
1074 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1075 if (oso_dwarf)
1076 return oso_dwarf->ParseCallEdgesInFunction(func_id);
1077 return {};
1078 }
1079
FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext & die_decl_ctx)1080 TypeSP SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext(
1081 const DWARFDeclContext &die_decl_ctx) {
1082 TypeSP type_sp;
1083 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1084 type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
1085 return ((bool)type_sp);
1086 });
1087 return type_sp;
1088 }
1089
Supports_DW_AT_APPLE_objc_complete_type(SymbolFileDWARF * skip_dwarf_oso)1090 bool SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type(
1091 SymbolFileDWARF *skip_dwarf_oso) {
1092 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
1093 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
1094 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1095 if (skip_dwarf_oso != oso_dwarf &&
1096 oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(nullptr)) {
1097 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
1098 return true;
1099 }
1100 return false;
1101 });
1102 }
1103 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
1104 }
1105
FindCompleteObjCDefinitionTypeForDIE(const DWARFDIE & die,ConstString type_name,bool must_be_implementation)1106 TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE(
1107 const DWARFDIE &die, ConstString type_name,
1108 bool must_be_implementation) {
1109 // If we have a debug map, we will have an Objective-C symbol whose name is
1110 // the type name and whose type is eSymbolTypeObjCClass. If we can find that
1111 // symbol and find its containing parent, we can locate the .o file that will
1112 // contain the implementation definition since it will be scoped inside the
1113 // N_SO and we can then locate the SymbolFileDWARF that corresponds to that
1114 // N_SO.
1115 SymbolFileDWARF *oso_dwarf = nullptr;
1116 TypeSP type_sp;
1117 ObjectFile *module_objfile = m_objfile_sp->GetModule()->GetObjectFile();
1118 if (module_objfile) {
1119 Symtab *symtab = module_objfile->GetSymtab();
1120 if (symtab) {
1121 Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
1122 type_name, eSymbolTypeObjCClass, Symtab::eDebugAny,
1123 Symtab::eVisibilityAny);
1124 if (objc_class_symbol) {
1125 // Get the N_SO symbol that contains the objective C class symbol as
1126 // this should be the .o file that contains the real definition...
1127 const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol);
1128
1129 if (source_file_symbol &&
1130 source_file_symbol->GetType() == eSymbolTypeSourceFile) {
1131 const uint32_t source_file_symbol_idx =
1132 symtab->GetIndexForSymbol(source_file_symbol);
1133 if (source_file_symbol_idx != UINT32_MAX) {
1134 CompileUnitInfo *compile_unit_info =
1135 GetCompileUnitInfoForSymbolWithIndex(source_file_symbol_idx,
1136 nullptr);
1137 if (compile_unit_info) {
1138 oso_dwarf = GetSymbolFileByCompUnitInfo(compile_unit_info);
1139 if (oso_dwarf) {
1140 TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1141 die, type_name, must_be_implementation));
1142 if (type_sp) {
1143 return type_sp;
1144 }
1145 }
1146 }
1147 }
1148 }
1149 }
1150 }
1151 }
1152
1153 // Only search all .o files for the definition if we don't need the
1154 // implementation because otherwise, with a valid debug map we should have
1155 // the ObjC class symbol and the code above should have found it.
1156 if (!must_be_implementation) {
1157 TypeSP type_sp;
1158
1159 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1160 type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1161 die, type_name, must_be_implementation);
1162 return (bool)type_sp;
1163 });
1164
1165 return type_sp;
1166 }
1167 return TypeSP();
1168 }
1169
FindTypes(ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,llvm::DenseSet<lldb_private::SymbolFile * > & searched_symbol_files,TypeMap & types)1170 void SymbolFileDWARFDebugMap::FindTypes(
1171 ConstString name, const CompilerDeclContext &parent_decl_ctx,
1172 uint32_t max_matches,
1173 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1174 TypeMap &types) {
1175 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1176 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1177 oso_dwarf->FindTypes(name, parent_decl_ctx, max_matches,
1178 searched_symbol_files, types);
1179 return types.GetSize() >= max_matches;
1180 });
1181 }
1182
FindTypes(llvm::ArrayRef<CompilerContext> context,LanguageSet languages,llvm::DenseSet<lldb_private::SymbolFile * > & searched_symbol_files,TypeMap & types)1183 void SymbolFileDWARFDebugMap::FindTypes(
1184 llvm::ArrayRef<CompilerContext> context, LanguageSet languages,
1185 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1186 TypeMap &types) {
1187 LLDB_SCOPED_TIMER();
1188 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1189 oso_dwarf->FindTypes(context, languages, searched_symbol_files, types);
1190 return false;
1191 });
1192 }
1193
FindNamespace(lldb_private::ConstString name,const CompilerDeclContext & parent_decl_ctx)1194 CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace(
1195 lldb_private::ConstString name,
1196 const CompilerDeclContext &parent_decl_ctx) {
1197 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1198 CompilerDeclContext matching_namespace;
1199
1200 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1201 matching_namespace = oso_dwarf->FindNamespace(name, parent_decl_ctx);
1202
1203 return (bool)matching_namespace;
1204 });
1205
1206 return matching_namespace;
1207 }
1208
DumpClangAST(Stream & s)1209 void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) {
1210 ForEachSymbolFile([&s](SymbolFileDWARF *oso_dwarf) -> bool {
1211 oso_dwarf->DumpClangAST(s);
1212 // The underlying assumption is that DumpClangAST(...) will obtain the
1213 // AST from the underlying TypeSystem and therefore we only need to do
1214 // this once and can stop after the first iteration hence we return true.
1215 return true;
1216 });
1217 }
1218
1219 lldb::CompUnitSP
GetCompileUnit(SymbolFileDWARF * oso_dwarf)1220 SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf) {
1221 if (oso_dwarf) {
1222 const uint32_t cu_count = GetNumCompileUnits();
1223 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1224 SymbolFileDWARF *oso_symfile =
1225 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1226 if (oso_symfile == oso_dwarf) {
1227 if (!m_compile_unit_infos[cu_idx].compile_unit_sp)
1228 m_compile_unit_infos[cu_idx].compile_unit_sp =
1229 ParseCompileUnitAtIndex(cu_idx);
1230
1231 return m_compile_unit_infos[cu_idx].compile_unit_sp;
1232 }
1233 }
1234 }
1235 llvm_unreachable("this shouldn't happen");
1236 }
1237
1238 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfo(SymbolFileDWARF * oso_dwarf)1239 SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) {
1240 if (oso_dwarf) {
1241 const uint32_t cu_count = GetNumCompileUnits();
1242 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1243 SymbolFileDWARF *oso_symfile =
1244 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1245 if (oso_symfile == oso_dwarf) {
1246 return &m_compile_unit_infos[cu_idx];
1247 }
1248 }
1249 }
1250 return nullptr;
1251 }
1252
SetCompileUnit(SymbolFileDWARF * oso_dwarf,const CompUnitSP & cu_sp)1253 void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
1254 const CompUnitSP &cu_sp) {
1255 if (oso_dwarf) {
1256 const uint32_t cu_count = GetNumCompileUnits();
1257 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1258 SymbolFileDWARF *oso_symfile =
1259 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1260 if (oso_symfile == oso_dwarf) {
1261 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
1262 assert(m_compile_unit_infos[cu_idx].compile_unit_sp.get() ==
1263 cu_sp.get());
1264 } else {
1265 m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp;
1266 SetCompileUnitAtIndex(cu_idx, cu_sp);
1267 }
1268 }
1269 }
1270 }
1271 }
1272
1273 CompilerDeclContext
GetDeclContextForUID(lldb::user_id_t type_uid)1274 SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
1275 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1276 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1277 if (oso_dwarf)
1278 return oso_dwarf->GetDeclContextForUID(type_uid);
1279 return CompilerDeclContext();
1280 }
1281
1282 CompilerDeclContext
GetDeclContextContainingUID(lldb::user_id_t type_uid)1283 SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1284 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1285 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1286 if (oso_dwarf)
1287 return oso_dwarf->GetDeclContextContainingUID(type_uid);
1288 return CompilerDeclContext();
1289 }
1290
ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx)1291 void SymbolFileDWARFDebugMap::ParseDeclsForContext(
1292 lldb_private::CompilerDeclContext decl_ctx) {
1293 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1294 oso_dwarf->ParseDeclsForContext(decl_ctx);
1295 return true; // Keep iterating
1296 });
1297 }
1298
AddOSOFileRange(CompileUnitInfo * cu_info,lldb::addr_t exe_file_addr,lldb::addr_t exe_byte_size,lldb::addr_t oso_file_addr,lldb::addr_t oso_byte_size)1299 bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info,
1300 lldb::addr_t exe_file_addr,
1301 lldb::addr_t exe_byte_size,
1302 lldb::addr_t oso_file_addr,
1303 lldb::addr_t oso_byte_size) {
1304 const uint32_t debug_map_idx =
1305 m_debug_map.FindEntryIndexThatContains(exe_file_addr);
1306 if (debug_map_idx != UINT32_MAX) {
1307 DebugMap::Entry *debug_map_entry =
1308 m_debug_map.FindEntryThatContains(exe_file_addr);
1309 debug_map_entry->data.SetOSOFileAddress(oso_file_addr);
1310 addr_t range_size = std::min<addr_t>(exe_byte_size, oso_byte_size);
1311 if (range_size == 0) {
1312 range_size = std::max<addr_t>(exe_byte_size, oso_byte_size);
1313 if (range_size == 0)
1314 range_size = 1;
1315 }
1316 cu_info->file_range_map.Append(
1317 FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr));
1318 return true;
1319 }
1320 return false;
1321 }
1322
FinalizeOSOFileRanges(CompileUnitInfo * cu_info)1323 void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) {
1324 cu_info->file_range_map.Sort();
1325 #if defined(DEBUG_OSO_DMAP)
1326 const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this);
1327 const size_t n = oso_file_range_map.GetSize();
1328 printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n",
1329 cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str());
1330 for (size_t i = 0; i < n; ++i) {
1331 const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i);
1332 printf("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
1333 ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n",
1334 entry.GetRangeBase(), entry.GetRangeEnd(), entry.data,
1335 entry.data + entry.GetByteSize());
1336 }
1337 #endif
1338 }
1339
1340 lldb::addr_t
LinkOSOFileAddress(SymbolFileDWARF * oso_symfile,lldb::addr_t oso_file_addr)1341 SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
1342 lldb::addr_t oso_file_addr) {
1343 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_symfile);
1344 if (cu_info) {
1345 const FileRangeMap::Entry *oso_range_entry =
1346 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1347 if (oso_range_entry) {
1348 const DebugMap::Entry *debug_map_entry =
1349 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1350 if (debug_map_entry) {
1351 const lldb::addr_t offset =
1352 oso_file_addr - oso_range_entry->GetRangeBase();
1353 const lldb::addr_t exe_file_addr =
1354 debug_map_entry->GetRangeBase() + offset;
1355 return exe_file_addr;
1356 }
1357 }
1358 }
1359 return LLDB_INVALID_ADDRESS;
1360 }
1361
LinkOSOAddress(Address & addr)1362 bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) {
1363 // Make sure this address hasn't been fixed already
1364 Module *exe_module = GetObjectFile()->GetModule().get();
1365 Module *addr_module = addr.GetModule().get();
1366 if (addr_module == exe_module)
1367 return true; // Address is already in terms of the main executable module
1368
1369 CompileUnitInfo *cu_info = GetCompileUnitInfo(
1370 GetSymbolFileAsSymbolFileDWARF(addr_module->GetSymbolFile()));
1371 if (cu_info) {
1372 const lldb::addr_t oso_file_addr = addr.GetFileAddress();
1373 const FileRangeMap::Entry *oso_range_entry =
1374 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1375 if (oso_range_entry) {
1376 const DebugMap::Entry *debug_map_entry =
1377 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1378 if (debug_map_entry) {
1379 const lldb::addr_t offset =
1380 oso_file_addr - oso_range_entry->GetRangeBase();
1381 const lldb::addr_t exe_file_addr =
1382 debug_map_entry->GetRangeBase() + offset;
1383 return exe_module->ResolveFileAddress(exe_file_addr, addr);
1384 }
1385 }
1386 }
1387 return true;
1388 }
1389
LinkOSOLineTable(SymbolFileDWARF * oso_dwarf,LineTable * line_table)1390 LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf,
1391 LineTable *line_table) {
1392 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf);
1393 if (cu_info)
1394 return line_table->LinkLineTable(cu_info->GetFileRangeMap(this));
1395 return nullptr;
1396 }
1397
1398 size_t
AddOSOARanges(SymbolFileDWARF * dwarf2Data,DWARFDebugAranges * debug_aranges)1399 SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data,
1400 DWARFDebugAranges *debug_aranges) {
1401 size_t num_line_entries_added = 0;
1402 if (debug_aranges && dwarf2Data) {
1403 CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data);
1404 if (compile_unit_info) {
1405 const FileRangeMap &file_range_map =
1406 compile_unit_info->GetFileRangeMap(this);
1407 for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) {
1408 const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(idx);
1409 if (entry) {
1410 debug_aranges->AppendRange(dwarf2Data->GetID(), entry->GetRangeBase(),
1411 entry->GetRangeEnd());
1412 num_line_entries_added++;
1413 }
1414 }
1415 }
1416 }
1417 return num_line_entries_added;
1418 }
1419
GetDebugInfoModules()1420 ModuleList SymbolFileDWARFDebugMap::GetDebugInfoModules() {
1421 ModuleList oso_modules;
1422 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1423 ObjectFile *oso_objfile = oso_dwarf->GetObjectFile();
1424 if (oso_objfile) {
1425 ModuleSP module_sp = oso_objfile->GetModule();
1426 if (module_sp)
1427 oso_modules.Append(module_sp);
1428 }
1429 return false; // Keep iterating
1430 });
1431 return oso_modules;
1432 }
1433