blob: a8f376331aa523f2ee528d533984c13ca8907076 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARFDebugMap.cpp ----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARFDebugMap.h"
11
12#include "lldb/Core/Module.h"
13#include "lldb/Core/ModuleList.h"
14#include "lldb/Core/PluginManager.h"
15#include "lldb/Core/RegularExpression.h"
16#include "lldb/Core/StreamFile.h"
17#include "lldb/Core/Timer.h"
18#include "lldb/Symbol/ObjectFile.h"
19#include "lldb/Symbol/SymbolVendor.h"
20#include "lldb/Symbol/VariableList.h"
21
22#include "SymbolFileDWARF.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
27void
28SymbolFileDWARFDebugMap::Initialize()
29{
30 PluginManager::RegisterPlugin (GetPluginNameStatic(),
31 GetPluginDescriptionStatic(),
32 CreateInstance);
33}
34
35void
36SymbolFileDWARFDebugMap::Terminate()
37{
38 PluginManager::UnregisterPlugin (CreateInstance);
39}
40
41
42const char *
43SymbolFileDWARFDebugMap::GetPluginNameStatic()
44{
45 return "symbol-file.dwarf2-debugmap";
46}
47
48const char *
49SymbolFileDWARFDebugMap::GetPluginDescriptionStatic()
50{
51 return "DWARF and DWARF3 debug symbol file reader (debug map).";
52}
53
54SymbolFile*
55SymbolFileDWARFDebugMap::CreateInstance (ObjectFile* obj_file)
56{
57 return new SymbolFileDWARFDebugMap (obj_file);
58}
59
60
61SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap (ObjectFile* ofile) :
62 SymbolFile(ofile),
63 m_flags(),
64 m_compile_unit_infos(),
65 m_func_indexes(),
66 m_glob_indexes()
67{
68}
69
70
71SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap()
72{
73}
74
Greg Clayton2d95dc9b2010-11-10 04:57:04 +000075lldb_private::ClangASTContext &
76SymbolFileDWARFDebugMap::GetClangASTContext ()
77{
78 return GetTypeList()->GetClangASTContext();
79}
80
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081void
82SymbolFileDWARFDebugMap::InitOSO ()
83{
84 if (m_flags.test(kHaveInitializedOSOs))
85 return;
86
87 m_flags.set(kHaveInitializedOSOs);
88 // In order to get the abilities of this plug-in, we look at the list of
89 // N_OSO entries (object files) from the symbol table and make sure that
90 // these files exist and also contain valid DWARF. If we get any of that
91 // then we return the abilities of the first N_OSO's DWARF.
92
93 Symtab* symtab = m_obj_file->GetSymtab();
94 if (symtab)
95 {
96 //StreamFile s(0, 4, eByteOrderHost, stdout);
97 std::vector<uint32_t> oso_indexes;
98 const uint32_t oso_index_count = symtab->AppendSymbolIndexesWithType(eSymbolTypeObjectFile, oso_indexes);
99
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000100 symtab->AppendSymbolIndexesWithType (eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes);
101 symtab->AppendSymbolIndexesWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, m_glob_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
103 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
104 symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
105
106 if (oso_index_count > 0)
107 {
108 m_compile_unit_infos.resize(oso_index_count);
109// s.Printf("%s N_OSO symbols:\n", __PRETTY_FUNCTION__);
110// symtab->Dump(&s, oso_indexes);
111
112 for (uint32_t i=0; i<oso_index_count; ++i)
113 {
114 m_compile_unit_infos[i].so_symbol = symtab->SymbolAtIndex(oso_indexes[i] - 1);
115 if (m_compile_unit_infos[i].so_symbol->GetSiblingIndex() == 0)
116 m_compile_unit_infos[i].so_symbol = symtab->SymbolAtIndex(oso_indexes[i] - 2);
117 m_compile_unit_infos[i].oso_symbol = symtab->SymbolAtIndex(oso_indexes[i]);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000118 uint32_t sibling_idx = m_compile_unit_infos[i].so_symbol->GetSiblingIndex();
119 assert (sibling_idx != 0);
120 assert (sibling_idx > i + 1);
121 m_compile_unit_infos[i].last_symbol = symtab->SymbolAtIndex (sibling_idx - 1);
122 m_compile_unit_infos[i].first_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].so_symbol);
123 m_compile_unit_infos[i].last_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].last_symbol);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 }
125 }
126 }
127}
128
129Module *
130SymbolFileDWARFDebugMap::GetModuleByOSOIndex (uint32_t oso_idx)
131{
132 const uint32_t cu_count = GetNumCompileUnits();
133 if (oso_idx < cu_count)
134 return GetModuleByCompUnitInfo (&m_compile_unit_infos[oso_idx]);
135 return NULL;
136}
137
138Module *
139SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo (CompileUnitInfo *comp_unit_info)
140{
141 if (comp_unit_info->oso_module_sp.get() == NULL)
142 {
143 Symbol *oso_symbol = comp_unit_info->oso_symbol;
144 if (oso_symbol)
145 {
Greg Clayton274060b2010-10-20 20:54:39 +0000146 FileSpec oso_file_spec(oso_symbol->GetMangled().GetName().AsCString(), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
148 ModuleList::GetSharedModule (oso_file_spec,
149 m_obj_file->GetModule()->GetArchitecture(),
150 NULL, // UUID pointer
151 NULL, // object name
152 0, // object offset
153 comp_unit_info->oso_module_sp,
154 NULL,
155 NULL);
156 //comp_unit_info->oso_module_sp.reset(new Module (oso_file_spec, m_obj_file->GetModule()->GetArchitecture()));
157 }
158 }
159 return comp_unit_info->oso_module_sp.get();
160}
161
162
163bool
164SymbolFileDWARFDebugMap::GetFileSpecForSO (uint32_t oso_idx, FileSpec &file_spec)
165{
166 if (oso_idx < m_compile_unit_infos.size())
167 {
168 if (!m_compile_unit_infos[oso_idx].so_file)
169 {
170
171 if (m_compile_unit_infos[oso_idx].so_symbol == NULL)
172 return false;
173
174 std::string so_path (m_compile_unit_infos[oso_idx].so_symbol->GetMangled().GetName().AsCString());
175 if (m_compile_unit_infos[oso_idx].so_symbol[1].GetType() == eSymbolTypeSourceFile)
176 so_path += m_compile_unit_infos[oso_idx].so_symbol[1].GetMangled().GetName().AsCString();
Greg Clayton274060b2010-10-20 20:54:39 +0000177 m_compile_unit_infos[oso_idx].so_file.SetFile(so_path.c_str(), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 }
179 file_spec = m_compile_unit_infos[oso_idx].so_file;
180 return true;
181 }
182 return false;
183}
184
185
186
187ObjectFile *
188SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex (uint32_t oso_idx)
189{
190 Module *oso_module = GetModuleByOSOIndex (oso_idx);
191 if (oso_module)
192 return oso_module->GetObjectFile();
193 return NULL;
194}
195
196SymbolFileDWARF *
197SymbolFileDWARFDebugMap::GetSymbolFile (const SymbolContext& sc)
198{
199 CompileUnitInfo *comp_unit_info = GetCompUnitInfo (sc);
200 if (comp_unit_info)
201 return GetSymbolFileByCompUnitInfo (comp_unit_info);
202 return NULL;
203}
204
205ObjectFile *
206SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo (CompileUnitInfo *comp_unit_info)
207{
208 Module *oso_module = GetModuleByCompUnitInfo (comp_unit_info);
209 if (oso_module)
210 return oso_module->GetObjectFile();
211 return NULL;
212}
213
214SymbolFileDWARF *
215SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex (uint32_t oso_idx)
216{
217 if (oso_idx < m_compile_unit_infos.size())
218 return GetSymbolFileByCompUnitInfo (&m_compile_unit_infos[oso_idx]);
219 return NULL;
220}
221
222SymbolFileDWARF *
223SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo (CompileUnitInfo *comp_unit_info)
224{
225 if (comp_unit_info->oso_symbol_vendor == NULL)
226 {
227 ObjectFile *oso_objfile = GetObjectFileByCompUnitInfo (comp_unit_info);
228
229 if (oso_objfile)
230 {
231 comp_unit_info->oso_symbol_vendor = oso_objfile->GetModule()->GetSymbolVendor();
232// SymbolFileDWARF *oso_dwarf = new SymbolFileDWARF(oso_objfile);
233// comp_unit_info->oso_dwarf_sp.reset (oso_dwarf);
234 if (comp_unit_info->oso_symbol_vendor)
235 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000236 // Set a a pointer to this class to set our OSO DWARF file know
237 // that the DWARF is being used along with a debug map and that
238 // it will have the remapped sections that we do below.
239 ((SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile())->SetDebugMapSymfile(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 comp_unit_info->debug_map_sections_sp.reset(new SectionList);
241
242 Symtab *exe_symtab = m_obj_file->GetSymtab();
243 Module *oso_module = oso_objfile->GetModule();
244 Symtab *oso_symtab = oso_objfile->GetSymtab();
245//#define DEBUG_OSO_DMAP // Do not check in with this defined...
246#if defined(DEBUG_OSO_DMAP)
247 StreamFile s(stdout);
248 s << "OSO symtab:\n";
249 oso_symtab->Dump(&s, NULL);
250 s << "OSO sections before:\n";
251 oso_objfile->GetSectionList()->Dump(&s, NULL, true);
252#endif
253
254 ///const uint32_t fun_resolve_flags = SymbolContext::Module | eSymbolContextCompUnit | eSymbolContextFunction;
255 //SectionList *oso_sections = oso_objfile->Sections();
256 // Now we need to make sections that map from zero based object
257 // file addresses to where things eneded up in the main executable.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000258 uint32_t oso_start_idx = exe_symtab->GetIndexForSymbol (comp_unit_info->oso_symbol);
259 assert (oso_start_idx != UINT32_MAX);
260 oso_start_idx += 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 const uint32_t oso_end_idx = comp_unit_info->so_symbol->GetSiblingIndex();
262 uint32_t sect_id = 0x10000;
263 for (uint32_t idx = oso_start_idx; idx < oso_end_idx; ++idx)
264 {
265 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
266 if (exe_symbol)
267 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000268 if (exe_symbol->IsDebug() == false)
269 continue;
270
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271 switch (exe_symbol->GetType())
272 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000273 case eSymbolTypeCode:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274 {
275 // For each N_FUN, or function that we run into in the debug map
276 // we make a new section that we add to the sections found in the
277 // .o file. This new section has the file address set to what the
278 // addresses are in the .o file, and the load address is adjusted
279 // to match where it ended up in the final executable! We do this
280 // before we parse any dwarf info so that when it goes get parsed
281 // all section/offset addresses that get registered will resolve
282 // correctly to the new addresses in the main executable.
283
284 // First we find the original symbol in the .o file's symbol table
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000285 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 if (oso_fun_symbol)
287 {
288 // If we found the symbol, then we
289 Section* exe_fun_section = const_cast<Section *>(exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
290 Section* oso_fun_section = const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
291 if (oso_fun_section)
292 {
293 // Now we create a section that we will add as a child of the
294 // section in which the .o symbol (the N_FUN) exists.
295
296 // We use the exe_symbol size because the one in the .o file
297 // will just be a symbol with no size, and the exe_symbol
298 // size will reflect any size changes (ppc has been known to
299 // shrink function sizes when it gets rid of jump islands that
300 // aren't needed anymore).
301 SectionSP oso_fun_section_sp (new Section (const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()),
302 oso_module, // Module (the .o file)
303 sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
304 exe_symbol->GetMangled().GetName(), // Name the section the same as the symbol for which is was generated!
305 eSectionTypeDebug,
306 oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), // File VM address offset in the current section
307 exe_symbol->GetByteSize(), // File size (we need the size from the executable)
308 0, 0, 0));
309
310 oso_fun_section_sp->SetLinkedLocation (exe_fun_section,
311 exe_symbol->GetValue().GetFileAddress() - exe_fun_section->GetFileAddress());
312 oso_fun_section->GetChildren().AddSection(oso_fun_section_sp);
313 comp_unit_info->debug_map_sections_sp->AddSection(oso_fun_section_sp);
314 }
315 }
316 }
317 break;
318
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000319 case eSymbolTypeData:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 {
321 // For each N_GSYM we remap the address for the global by making
322 // a new section that we add to the sections found in the .o file.
323 // This new section has the file address set to what the
324 // addresses are in the .o file, and the load address is adjusted
325 // to match where it ended up in the final executable! We do this
326 // before we parse any dwarf info so that when it goes get parsed
327 // all section/offset addresses that get registered will resolve
328 // correctly to the new addresses in the main executable. We
329 // initially set the section size to be 1 byte, but will need to
330 // fix up these addresses further after all globals have been
331 // parsed to span the gaps, or we can find the global variable
332 // sizes from the DWARF info as we are parsing.
333
334#if 0
335 // First we find the non-stab entry that corresponds to the N_GSYM in the executable
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000336 Symbol *exe_gsym_symbol = exe_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337#else
338 // The mach-o object file parser already matches up the N_GSYM with with the non-stab
339 // entry, so we shouldn't have to do that. If this ever changes, enable the code above
340 // in the "#if 0" block. STSYM's always match the symbol as found below.
341 Symbol *exe_gsym_symbol = exe_symbol;
342#endif
343 // Next we find the non-stab entry that corresponds to the N_GSYM in the .o file
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000344 Symbol *oso_gsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 if (exe_gsym_symbol && oso_gsym_symbol)
346 {
347 // If we found the symbol, then we
348 Section* exe_gsym_section = const_cast<Section *>(exe_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
349 Section* oso_gsym_section = const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
350 if (oso_gsym_section)
351 {
352 SectionSP oso_gsym_section_sp (new Section (const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()),
353 oso_module, // Module (the .o file)
354 sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
355 exe_symbol->GetMangled().GetName(), // Name the section the same as the symbol for which is was generated!
356 eSectionTypeDebug,
357 oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), // File VM address offset in the current section
358 1, // We don't know the size of the global, just do the main address for now.
359 0, 0, 0));
360
361 oso_gsym_section_sp->SetLinkedLocation (exe_gsym_section,
362 exe_gsym_symbol->GetValue().GetFileAddress() - exe_gsym_section->GetFileAddress());
363 oso_gsym_section->GetChildren().AddSection(oso_gsym_section_sp);
364 comp_unit_info->debug_map_sections_sp->AddSection(oso_gsym_section_sp);
365 }
366 }
367 }
368 break;
369
370// case eSymbolTypeStatic:
371// {
372// // For each N_STSYM we remap the address for the global by making
373// // a new section that we add to the sections found in the .o file.
374// // This new section has the file address set to what the
375// // addresses are in the .o file, and the load address is adjusted
376// // to match where it ended up in the final executable! We do this
377// // before we parse any dwarf info so that when it goes get parsed
378// // all section/offset addresses that get registered will resolve
379// // correctly to the new addresses in the main executable. We
380// // initially set the section size to be 1 byte, but will need to
381// // fix up these addresses further after all globals have been
382// // parsed to span the gaps, or we can find the global variable
383// // sizes from the DWARF info as we are parsing.
384//
385//
386// Symbol *exe_stsym_symbol = exe_symbol;
387// // First we find the non-stab entry that corresponds to the N_STSYM in the .o file
388// Symbol *oso_stsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData);
389// if (exe_stsym_symbol && oso_stsym_symbol)
390// {
391// // If we found the symbol, then we
392// Section* exe_stsym_section = const_cast<Section *>(exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
393// Section* oso_stsym_section = const_cast<Section *>(oso_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
394// if (oso_stsym_section)
395// {
396// // The load address of the symbol will use the section in the
397// // executable that contains the debug map that corresponds to
398// // the N_FUN symbol. We set the offset to reflect the offset
399// // into that section since we are creating a new section.
400// AddressRange stsym_load_range(exe_stsym_section, exe_stsym_symbol->GetValue().GetFileAddress() - exe_stsym_section->GetFileAddress(), 1);
401// // We need the symbol's section offset address from the .o file, but
402// // we need a non-zero size.
403// AddressRange stsym_file_range(exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection(), exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), 1);
404//
405// // Now we create a section that we will add as a child of the
406// // section in which the .o symbol (the N_FUN) exists.
407//
408//// TODO: mimic what I did for N_FUN if that works...
409//// // We use the 1 byte for the size because we don't know the
410//// // size of the global symbol without seeing the DWARF.
411//// SectionSP oso_fun_section_sp (new Section ( NULL, oso_module, // Module (the .o file)
412//// sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
413//// exe_symbol->GetMangled().GetName(),// Name the section the same as the symbol for which is was generated!
414//// // &stsym_load_range, // Load offset is the offset into the executable section for the N_FUN from the debug map
415//// &stsym_file_range, // File section/offset is just the same os the symbol on the .o file
416//// 0, 0, 0));
417////
418//// // Now we add the new section to the .o file's sections as a child
419//// // of the section in which the N_SECT symbol exists.
420//// oso_stsym_section->GetChildren().AddSection(oso_fun_section_sp);
421//// comp_unit_info->debug_map_sections_sp->AddSection(oso_fun_section_sp);
422// }
423// }
424// }
425// break;
426 }
427 }
428 }
429#if defined(DEBUG_OSO_DMAP)
430 s << "OSO sections after:\n";
431 oso_objfile->GetSectionList()->Dump(&s, NULL, true);
432#endif
433 }
434 }
435 }
436 if (comp_unit_info->oso_symbol_vendor)
437 return (SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile();
438 return NULL;
439}
440
441uint32_t
442SymbolFileDWARFDebugMap::GetAbilities ()
443{
444 // In order to get the abilities of this plug-in, we look at the list of
445 // N_OSO entries (object files) from the symbol table and make sure that
446 // these files exist and also contain valid DWARF. If we get any of that
447 // then we return the abilities of the first N_OSO's DWARF.
448
449 const uint32_t oso_index_count = GetNumCompileUnits();
450 if (oso_index_count > 0)
451 {
452 const uint32_t dwarf_abilities = SymbolFile::CompileUnits |
453 SymbolFile::Functions |
454 SymbolFile::Blocks |
455 SymbolFile::GlobalVariables |
456 SymbolFile::LocalVariables |
457 SymbolFile::VariableTypes |
458 SymbolFile::LineTables;
459
460 for (uint32_t oso_idx=0; oso_idx<oso_index_count; ++oso_idx)
461 {
462 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
463 if (oso_dwarf)
464 {
465 uint32_t oso_abilities = oso_dwarf->GetAbilities();
466 if ((oso_abilities & dwarf_abilities) == dwarf_abilities)
467 return oso_abilities;
468 }
469 }
470 }
471 return 0;
472}
473
474uint32_t
475SymbolFileDWARFDebugMap::GetNumCompileUnits()
476{
477 InitOSO ();
478 return m_compile_unit_infos.size();
479}
480
481
482CompUnitSP
483SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx)
484{
485 CompUnitSP comp_unit_sp;
486 const uint32_t cu_count = GetNumCompileUnits();
487
488 if (cu_idx < cu_count)
489 {
490 if (m_compile_unit_infos[cu_idx].oso_compile_unit_sp.get() == NULL)
491 {
492 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (cu_idx);
493 if (oso_dwarf)
494 {
495 // There is only one compile unit for N_OSO entry right now, so
496 // it will always exist at index zero.
497 m_compile_unit_infos[cu_idx].oso_compile_unit_sp = m_compile_unit_infos[cu_idx].oso_symbol_vendor->GetCompileUnitAtIndex (0);
498 }
499
500 if (m_compile_unit_infos[cu_idx].oso_compile_unit_sp.get() == NULL)
501 {
502 // We weren't able to get the DWARF for this N_OSO entry (the
503 // .o file may be missing or not at the specified path), make
504 // one up as best we can from the debug map. We set the uid
505 // of the compile unit to the symbol index with the MSBit set
506 // so that it doesn't collide with any uid values from the DWARF
507 Symbol *so_symbol = m_compile_unit_infos[cu_idx].so_symbol;
508 if (so_symbol)
509 {
510 m_compile_unit_infos[cu_idx].oso_compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(),
511 NULL,
512 so_symbol->GetMangled().GetName().AsCString(),
513 cu_idx,
Greg Clayton9e409562010-07-28 02:04:09 +0000514 eLanguageTypeUnknown));
Greg Clayton450e3f32010-10-12 02:24:53 +0000515
516 // Let our symbol vendor know about this compile unit
517 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex (m_compile_unit_infos[cu_idx].oso_compile_unit_sp,
518 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519 }
520 }
521 }
522 comp_unit_sp = m_compile_unit_infos[cu_idx].oso_compile_unit_sp;
523 }
524
525 return comp_unit_sp;
526}
527
528SymbolFileDWARFDebugMap::CompileUnitInfo *
529SymbolFileDWARFDebugMap::GetCompUnitInfo (const SymbolContext& sc)
530{
531 const uint32_t cu_count = GetNumCompileUnits();
532 for (uint32_t i=0; i<cu_count; ++i)
533 {
534 if (sc.comp_unit == m_compile_unit_infos[i].oso_compile_unit_sp.get())
535 return &m_compile_unit_infos[i];
536 }
537 return NULL;
538}
539
540size_t
541SymbolFileDWARFDebugMap::ParseCompileUnitFunctions (const SymbolContext& sc)
542{
543 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
544 if (oso_dwarf)
545 return oso_dwarf->ParseCompileUnitFunctions (sc);
546 return 0;
547}
548
549bool
550SymbolFileDWARFDebugMap::ParseCompileUnitLineTable (const SymbolContext& sc)
551{
552 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
553 if (oso_dwarf)
554 return oso_dwarf->ParseCompileUnitLineTable (sc);
555 return false;
556}
557
558bool
559SymbolFileDWARFDebugMap::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
560{
561 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
562 if (oso_dwarf)
563 return oso_dwarf->ParseCompileUnitSupportFiles (sc, support_files);
564 return false;
565}
566
567
568size_t
569SymbolFileDWARFDebugMap::ParseFunctionBlocks (const SymbolContext& sc)
570{
571 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
572 if (oso_dwarf)
573 return oso_dwarf->ParseFunctionBlocks (sc);
574 return 0;
575}
576
577
578size_t
579SymbolFileDWARFDebugMap::ParseTypes (const SymbolContext& sc)
580{
581 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
582 if (oso_dwarf)
583 return oso_dwarf->ParseTypes (sc);
584 return 0;
585}
586
587
588size_t
589SymbolFileDWARFDebugMap::ParseVariablesForContext (const SymbolContext& sc)
590{
591 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
592 if (oso_dwarf)
593 return oso_dwarf->ParseTypes (sc);
594 return 0;
595}
596
597
598
599Type*
600SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid)
601{
602 return NULL;
603}
604
Greg Clayton1be10fc2010-09-29 01:12:09 +0000605lldb::clang_type_t
606SymbolFileDWARFDebugMap::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_Type)
607{
608 // We have a struct/union/class/enum that needs to be fully resolved.
609 return NULL;
610}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611
612uint32_t
613SymbolFileDWARFDebugMap::ResolveSymbolContext (const Address& exe_so_addr, uint32_t resolve_scope, SymbolContext& sc)
614{
615 uint32_t resolved_flags = 0;
616 Symtab* symtab = m_obj_file->GetSymtab();
617 if (symtab)
618 {
619 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
620 sc.symbol = symtab->FindSymbolContainingFileAddress (exe_file_addr, &m_func_indexes[0], m_func_indexes.size());
621
622 if (sc.symbol != NULL)
623 {
624 resolved_flags |= eSymbolContextSymbol;
625
626 uint32_t oso_idx = 0;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000627 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithID (sc.symbol->GetID(), &oso_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 if (comp_unit_info)
629 {
630 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
631 ObjectFile *oso_objfile = GetObjectFileByOSOIndex (oso_idx);
632 if (oso_dwarf && oso_objfile)
633 {
634 SectionList *oso_section_list = oso_objfile->GetSectionList();
635
Greg Clayton016a95e2010-09-14 02:20:48 +0000636 SectionSP oso_symbol_section_sp (oso_section_list->FindSectionContainingLinkedFileAddress (exe_file_addr, UINT32_MAX));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637
Greg Clayton016a95e2010-09-14 02:20:48 +0000638 if (oso_symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 {
Greg Clayton016a95e2010-09-14 02:20:48 +0000640 const addr_t linked_file_addr = oso_symbol_section_sp->GetLinkedFileAddress();
641 Address oso_so_addr (oso_symbol_section_sp.get(), exe_file_addr - linked_file_addr);
642 if (oso_so_addr.IsSectionOffset())
643 resolved_flags |= oso_dwarf->ResolveSymbolContext (oso_so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645 }
646 }
647 }
648 }
649 return resolved_flags;
650}
651
652
653uint32_t
654SymbolFileDWARFDebugMap::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
655{
656 uint32_t initial = sc_list.GetSize();
657 const uint32_t cu_count = GetNumCompileUnits();
658
659 FileSpec so_file_spec;
660 for (uint32_t i=0; i<cu_count; ++i)
661 {
662 if (GetFileSpecForSO (i, so_file_spec))
663 {
664 // By passing false to the comparison we will be able to match
665 // and files given a filename only. If both file_spec and
666 // so_file_spec have directories, we will still do a full match.
667 if (FileSpec::Compare (file_spec, so_file_spec, false) == 0)
668 {
669 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (i);
670
671 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list);
672 }
673 }
674 }
675 return sc_list.GetSize() - initial;
676}
677
678uint32_t
679SymbolFileDWARFDebugMap::PrivateFindGlobalVariables
680(
681 const ConstString &name,
682 const std::vector<uint32_t> &indexes, // Indexes into the symbol table that match "name"
683 uint32_t max_matches,
684 VariableList& variables
685)
686{
687 const uint32_t original_size = variables.GetSize();
688 const size_t match_count = indexes.size();
689 for (size_t i=0; i<match_count; ++i)
690 {
691 uint32_t oso_idx;
692 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithIndex (indexes[i], &oso_idx);
693 if (comp_unit_info)
694 {
695 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
696 if (oso_dwarf)
697 {
698 if (oso_dwarf->FindGlobalVariables(name, true, max_matches, variables))
699 if (variables.GetSize() > max_matches)
700 break;
701 }
702 }
703 }
704 return variables.GetSize() - original_size;
705}
706
707uint32_t
708SymbolFileDWARFDebugMap::FindGlobalVariables (const ConstString &name, bool append, uint32_t max_matches, VariableList& variables)
709{
710
711 // If we aren't appending the results to this list, then clear the list
712 if (!append)
713 variables.Clear();
714
715 // Remember how many variables are in the list before we search in case
716 // we are appending the results to a variable list.
717 const uint32_t original_size = variables.GetSize();
718
719 Symtab* symtab = m_obj_file->GetSymtab();
720 if (symtab)
721 {
722 std::vector<uint32_t> indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000723 const size_t match_count = m_obj_file->GetSymtab()->FindAllSymbolsWithNameAndType (name, eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 if (match_count)
725 {
726 PrivateFindGlobalVariables (name, indexes, max_matches, variables);
727 }
728 }
729 // Return the number of variable that were appended to the list
730 return variables.GetSize() - original_size;
731}
732
733
734uint32_t
735SymbolFileDWARFDebugMap::FindGlobalVariables (const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
736{
737 return 0;
738}
739
740
741int
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000742SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743{
744 const uint32_t symbol_idx = *symbol_idx_ptr;
745
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000746 if (symbol_idx < comp_unit_info->first_symbol_index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 return -1;
748
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000749 if (symbol_idx <= comp_unit_info->last_symbol_index)
750 return 0;
751
752 return 1;
753}
754
755
756int
757SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID (user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
758{
759 const user_id_t symbol_id = *symbol_idx_ptr;
760
761 if (symbol_id < comp_unit_info->so_symbol->GetID())
762 return -1;
763
764 if (symbol_id <= comp_unit_info->last_symbol->GetID())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 return 0;
766
767 return 1;
768}
769
770
771SymbolFileDWARFDebugMap::CompileUnitInfo*
772SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex (uint32_t symbol_idx, uint32_t *oso_idx_ptr)
773{
774 const uint32_t oso_index_count = m_compile_unit_infos.size();
775 CompileUnitInfo *comp_unit_info = NULL;
776 if (oso_index_count)
777 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000778 comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(), sizeof(CompileUnitInfo), (comparison_function)SymbolContainsSymbolWithIndex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779 }
780
781 if (oso_idx_ptr)
782 {
783 if (comp_unit_info != NULL)
784 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
785 else
786 *oso_idx_ptr = UINT32_MAX;
787 }
788 return comp_unit_info;
789}
790
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000791SymbolFileDWARFDebugMap::CompileUnitInfo*
792SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID (user_id_t symbol_id, uint32_t *oso_idx_ptr)
793{
794 const uint32_t oso_index_count = m_compile_unit_infos.size();
795 CompileUnitInfo *comp_unit_info = NULL;
796 if (oso_index_count)
797 {
798 comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(), sizeof(CompileUnitInfo), (comparison_function)SymbolContainsSymbolWithID);
799 }
800
801 if (oso_idx_ptr)
802 {
803 if (comp_unit_info != NULL)
804 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
805 else
806 *oso_idx_ptr = UINT32_MAX;
807 }
808 return comp_unit_info;
809}
810
811
Greg Clayton3ef3fc62010-08-17 23:16:15 +0000812static void
813RemoveFunctionsWithModuleNotEqualTo (Module *module, SymbolContextList &sc_list, uint32_t start_idx)
814{
815 // We found functions in .o files. Not all functions in the .o files
816 // will have made it into the final output file. The ones that did
817 // make it into the final output file will have a section whose module
818 // matches the module from the ObjectFile for this SymbolFile. When
819 // the modules don't match, then we have something that was in a
820 // .o file, but doesn't map to anything in the final executable.
821 uint32_t i=start_idx;
822 while (i < sc_list.GetSize())
823 {
824 SymbolContext sc;
825 sc_list.GetContextAtIndex(i, sc);
826 if (sc.function)
827 {
828 const Section *section = sc.function->GetAddressRange().GetBaseAddress().GetSection();
829 if (section->GetModule() != module)
830 {
831 sc_list.RemoveContextAtIndex(i);
832 continue;
833 }
834 }
835 ++i;
836 }
837}
838
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839uint32_t
Greg Clayton0c5cd902010-06-28 21:30:43 +0000840SymbolFileDWARFDebugMap::FindFunctions(const ConstString &name, uint32_t name_type_mask, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841{
842 Timer scoped_timer (__PRETTY_FUNCTION__,
843 "SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
844 name.GetCString());
845
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846 uint32_t initial_size = 0;
847 if (append)
848 initial_size = sc_list.GetSize();
849 else
850 sc_list.Clear();
851
Greg Clayton57a6b992010-08-17 00:35:34 +0000852 uint32_t oso_idx = 0;
853 SymbolFileDWARF *oso_dwarf;
854 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 {
Greg Clayton3ef3fc62010-08-17 23:16:15 +0000856 uint32_t sc_idx = sc_list.GetSize();
857 if (oso_dwarf->FindFunctions(name, name_type_mask, true, sc_list))
858 {
859 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx);
860 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 }
862
863 return sc_list.GetSize() - initial_size;
864}
865
866
867uint32_t
868SymbolFileDWARFDebugMap::FindFunctions (const RegularExpression& regex, bool append, SymbolContextList& sc_list)
869{
870 Timer scoped_timer (__PRETTY_FUNCTION__,
871 "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
872 regex.GetText());
873
Greg Clayton57a6b992010-08-17 00:35:34 +0000874 uint32_t initial_size = 0;
875 if (append)
876 initial_size = sc_list.GetSize();
877 else
878 sc_list.Clear();
879
880 uint32_t oso_idx = 0;
881 SymbolFileDWARF *oso_dwarf;
882 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
883 {
Greg Clayton3ef3fc62010-08-17 23:16:15 +0000884 uint32_t sc_idx = sc_list.GetSize();
885
886 if (oso_dwarf->FindFunctions(regex, true, sc_list))
887 {
888 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx);
889 }
Greg Clayton57a6b992010-08-17 00:35:34 +0000890 }
891
892 return sc_list.GetSize() - initial_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893}
894
Greg Clayton2ccf8cf2010-11-07 21:02:03 +0000895TypeSP
896SymbolFileDWARFDebugMap::FindDefinitionTypeForDIE (
897 DWARFCompileUnit* cu,
898 const DWARFDebugInfoEntry *die,
899 const ConstString &type_name
900)
901{
902 TypeSP type_sp;
903 SymbolFileDWARF *oso_dwarf;
904 for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx)
905 {
906 type_sp = oso_dwarf->FindDefinitionTypeForDIE (cu, die, type_name);
907 if (type_sp)
908 break;
909 }
910 return type_sp;
911}
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000912
913uint32_t
Greg Clayton6dbd3982010-09-15 05:51:24 +0000914SymbolFileDWARFDebugMap::FindTypes
915(
916 const SymbolContext& sc,
917 const ConstString &name,
918 bool append,
919 uint32_t max_matches,
920 TypeList& types
921)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000922{
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000923 if (!append)
924 types.Clear();
Greg Clayton6dbd3982010-09-15 05:51:24 +0000925
926 const uint32_t initial_types_size = types.GetSize();
927 SymbolFileDWARF *oso_dwarf;
928
929 if (sc.comp_unit)
930 {
931 oso_dwarf = GetSymbolFile (sc);
932 if (oso_dwarf)
933 return oso_dwarf->FindTypes (sc, name, append, max_matches, types);
934 }
935 else
936 {
937 uint32_t oso_idx = 0;
938 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
939 oso_dwarf->FindTypes (sc, name, append, max_matches, types);
940 }
941
942 return types.GetSize() - initial_types_size;
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000943}
944
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945//
946//uint32_t
947//SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, lldb::user_id_t udt_uid, TypeList& types)
948//{
949// SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
950// if (oso_dwarf)
951// return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, udt_uid, types);
952// return 0;
953//}
954
Greg Clayton96d7d742010-11-10 23:42:09 +0000955
Greg Clayton526e5af2010-11-13 03:52:47 +0000956ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +0000957SymbolFileDWARFDebugMap::FindNamespace (const lldb_private::SymbolContext& sc,
958 const lldb_private::ConstString &name)
959{
Greg Clayton526e5af2010-11-13 03:52:47 +0000960 ClangNamespaceDecl matching_namespace;
Greg Clayton96d7d742010-11-10 23:42:09 +0000961 SymbolFileDWARF *oso_dwarf;
962
963 if (sc.comp_unit)
964 {
965 oso_dwarf = GetSymbolFile (sc);
966 if (oso_dwarf)
967 matching_namespace = oso_dwarf->FindNamespace (sc, name);
968 }
969 else
970 {
971 for (uint32_t oso_idx = 0;
972 ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL);
973 ++oso_idx)
974 {
975 matching_namespace = oso_dwarf->FindNamespace (sc, name);
976
977 if (matching_namespace)
978 break;
979 }
980 }
981
982 return matching_namespace;
983}
984
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985//------------------------------------------------------------------
986// PluginInterface protocol
987//------------------------------------------------------------------
988const char *
989SymbolFileDWARFDebugMap::GetPluginName()
990{
991 return "SymbolFileDWARFDebugMap";
992}
993
994const char *
995SymbolFileDWARFDebugMap::GetShortPluginName()
996{
997 return GetPluginNameStatic();
998}
999
1000uint32_t
1001SymbolFileDWARFDebugMap::GetPluginVersion()
1002{
1003 return 1;
1004}
1005
1006void
1007SymbolFileDWARFDebugMap::GetPluginCommandHelp (const char *command, Stream *strm)
1008{
1009}
1010
1011Error
1012SymbolFileDWARFDebugMap::ExecutePluginCommand (Args &command, Stream *strm)
1013{
1014 Error error;
1015 error.SetErrorString("No plug-in command are currently supported.");
1016 return error;
1017}
1018
1019Log *
1020SymbolFileDWARFDebugMap::EnablePluginLogging (Stream *strm, Args &command)
1021{
1022 return NULL;
1023}
1024
1025
Greg Clayton450e3f32010-10-12 02:24:53 +00001026void
1027SymbolFileDWARFDebugMap::SetCompileUnit (SymbolFileDWARF *oso_dwarf, const CompUnitSP &cu_sp)
1028{
1029 const uint32_t cu_count = GetNumCompileUnits();
1030 for (uint32_t i=0; i<cu_count; ++i)
1031 {
1032 if (m_compile_unit_infos[i].oso_symbol_vendor &&
1033 m_compile_unit_infos[i].oso_symbol_vendor->GetSymbolFile() == oso_dwarf)
1034 {
1035 if (m_compile_unit_infos[i].oso_compile_unit_sp)
1036 {
1037 assert (m_compile_unit_infos[i].oso_compile_unit_sp.get() == cu_sp.get());
1038 }
1039 else
1040 {
1041 m_compile_unit_infos[i].oso_compile_unit_sp = cu_sp;
1042 }
1043 }
1044 }
1045}
1046