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