blob: 7a851233b0289033871c1062b7a71bf1ea9462a1 [file] [log] [blame]
Chris Lattner24943d22010-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
75void
76SymbolFileDWARFDebugMap::InitOSO ()
77{
78 if (m_flags.test(kHaveInitializedOSOs))
79 return;
80
81 m_flags.set(kHaveInitializedOSOs);
82 // In order to get the abilities of this plug-in, we look at the list of
83 // N_OSO entries (object files) from the symbol table and make sure that
84 // these files exist and also contain valid DWARF. If we get any of that
85 // then we return the abilities of the first N_OSO's DWARF.
86
87 Symtab* symtab = m_obj_file->GetSymtab();
88 if (symtab)
89 {
90 //StreamFile s(0, 4, eByteOrderHost, stdout);
91 std::vector<uint32_t> oso_indexes;
92 const uint32_t oso_index_count = symtab->AppendSymbolIndexesWithType(eSymbolTypeObjectFile, oso_indexes);
93
Greg Clayton7c36fa02010-09-11 03:13:28 +000094 symtab->AppendSymbolIndexesWithType (eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes);
95 symtab->AppendSymbolIndexesWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, m_glob_indexes);
Chris Lattner24943d22010-06-08 16:52:24 +000096
97 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
98 symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
99
100 if (oso_index_count > 0)
101 {
102 m_compile_unit_infos.resize(oso_index_count);
103// s.Printf("%s N_OSO symbols:\n", __PRETTY_FUNCTION__);
104// symtab->Dump(&s, oso_indexes);
105
106 for (uint32_t i=0; i<oso_index_count; ++i)
107 {
108 m_compile_unit_infos[i].so_symbol = symtab->SymbolAtIndex(oso_indexes[i] - 1);
109 if (m_compile_unit_infos[i].so_symbol->GetSiblingIndex() == 0)
110 m_compile_unit_infos[i].so_symbol = symtab->SymbolAtIndex(oso_indexes[i] - 2);
111 m_compile_unit_infos[i].oso_symbol = symtab->SymbolAtIndex(oso_indexes[i]);
Greg Clayton7c36fa02010-09-11 03:13:28 +0000112 uint32_t sibling_idx = m_compile_unit_infos[i].so_symbol->GetSiblingIndex();
113 assert (sibling_idx != 0);
114 assert (sibling_idx > i + 1);
115 m_compile_unit_infos[i].last_symbol = symtab->SymbolAtIndex (sibling_idx - 1);
116 m_compile_unit_infos[i].first_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].so_symbol);
117 m_compile_unit_infos[i].last_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].last_symbol);
Chris Lattner24943d22010-06-08 16:52:24 +0000118 }
119 }
120 }
121}
122
123Module *
124SymbolFileDWARFDebugMap::GetModuleByOSOIndex (uint32_t oso_idx)
125{
126 const uint32_t cu_count = GetNumCompileUnits();
127 if (oso_idx < cu_count)
128 return GetModuleByCompUnitInfo (&m_compile_unit_infos[oso_idx]);
129 return NULL;
130}
131
132Module *
133SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo (CompileUnitInfo *comp_unit_info)
134{
135 if (comp_unit_info->oso_module_sp.get() == NULL)
136 {
137 Symbol *oso_symbol = comp_unit_info->oso_symbol;
138 if (oso_symbol)
139 {
140 FileSpec oso_file_spec(oso_symbol->GetMangled().GetName().AsCString());
141
142 ModuleList::GetSharedModule (oso_file_spec,
143 m_obj_file->GetModule()->GetArchitecture(),
144 NULL, // UUID pointer
145 NULL, // object name
146 0, // object offset
147 comp_unit_info->oso_module_sp,
148 NULL,
149 NULL);
150 //comp_unit_info->oso_module_sp.reset(new Module (oso_file_spec, m_obj_file->GetModule()->GetArchitecture()));
151 }
152 }
153 return comp_unit_info->oso_module_sp.get();
154}
155
156
157bool
158SymbolFileDWARFDebugMap::GetFileSpecForSO (uint32_t oso_idx, FileSpec &file_spec)
159{
160 if (oso_idx < m_compile_unit_infos.size())
161 {
162 if (!m_compile_unit_infos[oso_idx].so_file)
163 {
164
165 if (m_compile_unit_infos[oso_idx].so_symbol == NULL)
166 return false;
167
168 std::string so_path (m_compile_unit_infos[oso_idx].so_symbol->GetMangled().GetName().AsCString());
169 if (m_compile_unit_infos[oso_idx].so_symbol[1].GetType() == eSymbolTypeSourceFile)
170 so_path += m_compile_unit_infos[oso_idx].so_symbol[1].GetMangled().GetName().AsCString();
171 m_compile_unit_infos[oso_idx].so_file.SetFile(so_path.c_str());
172 }
173 file_spec = m_compile_unit_infos[oso_idx].so_file;
174 return true;
175 }
176 return false;
177}
178
179
180
181ObjectFile *
182SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex (uint32_t oso_idx)
183{
184 Module *oso_module = GetModuleByOSOIndex (oso_idx);
185 if (oso_module)
186 return oso_module->GetObjectFile();
187 return NULL;
188}
189
190SymbolFileDWARF *
191SymbolFileDWARFDebugMap::GetSymbolFile (const SymbolContext& sc)
192{
193 CompileUnitInfo *comp_unit_info = GetCompUnitInfo (sc);
194 if (comp_unit_info)
195 return GetSymbolFileByCompUnitInfo (comp_unit_info);
196 return NULL;
197}
198
199ObjectFile *
200SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo (CompileUnitInfo *comp_unit_info)
201{
202 Module *oso_module = GetModuleByCompUnitInfo (comp_unit_info);
203 if (oso_module)
204 return oso_module->GetObjectFile();
205 return NULL;
206}
207
208SymbolFileDWARF *
209SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex (uint32_t oso_idx)
210{
211 if (oso_idx < m_compile_unit_infos.size())
212 return GetSymbolFileByCompUnitInfo (&m_compile_unit_infos[oso_idx]);
213 return NULL;
214}
215
216SymbolFileDWARF *
217SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo (CompileUnitInfo *comp_unit_info)
218{
219 if (comp_unit_info->oso_symbol_vendor == NULL)
220 {
221 ObjectFile *oso_objfile = GetObjectFileByCompUnitInfo (comp_unit_info);
222
223 if (oso_objfile)
224 {
225 comp_unit_info->oso_symbol_vendor = oso_objfile->GetModule()->GetSymbolVendor();
226// SymbolFileDWARF *oso_dwarf = new SymbolFileDWARF(oso_objfile);
227// comp_unit_info->oso_dwarf_sp.reset (oso_dwarf);
228 if (comp_unit_info->oso_symbol_vendor)
229 {
230 // Set a bit that lets this DWARF file know that it is being
231 // used along with a debug map and that it will have the
232 // remapped sections that we do below.
233 ((SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile())->GetFlags().Set(SymbolFileDWARF::flagsDWARFIsOSOForDebugMap);
234 comp_unit_info->debug_map_sections_sp.reset(new SectionList);
235
236 Symtab *exe_symtab = m_obj_file->GetSymtab();
237 Module *oso_module = oso_objfile->GetModule();
238 Symtab *oso_symtab = oso_objfile->GetSymtab();
239//#define DEBUG_OSO_DMAP // Do not check in with this defined...
240#if defined(DEBUG_OSO_DMAP)
241 StreamFile s(stdout);
242 s << "OSO symtab:\n";
243 oso_symtab->Dump(&s, NULL);
244 s << "OSO sections before:\n";
245 oso_objfile->GetSectionList()->Dump(&s, NULL, true);
246#endif
247
248 ///const uint32_t fun_resolve_flags = SymbolContext::Module | eSymbolContextCompUnit | eSymbolContextFunction;
249 //SectionList *oso_sections = oso_objfile->Sections();
250 // Now we need to make sections that map from zero based object
251 // file addresses to where things eneded up in the main executable.
Greg Clayton7c36fa02010-09-11 03:13:28 +0000252 uint32_t oso_start_idx = exe_symtab->GetIndexForSymbol (comp_unit_info->oso_symbol);
253 assert (oso_start_idx != UINT32_MAX);
254 oso_start_idx += 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000255 const uint32_t oso_end_idx = comp_unit_info->so_symbol->GetSiblingIndex();
256 uint32_t sect_id = 0x10000;
257 for (uint32_t idx = oso_start_idx; idx < oso_end_idx; ++idx)
258 {
259 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
260 if (exe_symbol)
261 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000262 if (exe_symbol->IsDebug() == false)
263 continue;
264
Chris Lattner24943d22010-06-08 16:52:24 +0000265 switch (exe_symbol->GetType())
266 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000267 case eSymbolTypeCode:
Chris Lattner24943d22010-06-08 16:52:24 +0000268 {
269 // For each N_FUN, or function that we run into in the debug map
270 // we make a new section that we add to the sections found in the
271 // .o file. This new section has the file address set to what the
272 // addresses are in the .o file, and the load address is adjusted
273 // to match where it ended up in the final executable! We do this
274 // before we parse any dwarf info so that when it goes get parsed
275 // all section/offset addresses that get registered will resolve
276 // correctly to the new addresses in the main executable.
277
278 // First we find the original symbol in the .o file's symbol table
Greg Clayton7c36fa02010-09-11 03:13:28 +0000279 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner24943d22010-06-08 16:52:24 +0000280 if (oso_fun_symbol)
281 {
282 // If we found the symbol, then we
283 Section* exe_fun_section = const_cast<Section *>(exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
284 Section* oso_fun_section = const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
285 if (oso_fun_section)
286 {
287 // Now we create a section that we will add as a child of the
288 // section in which the .o symbol (the N_FUN) exists.
289
290 // We use the exe_symbol size because the one in the .o file
291 // will just be a symbol with no size, and the exe_symbol
292 // size will reflect any size changes (ppc has been known to
293 // shrink function sizes when it gets rid of jump islands that
294 // aren't needed anymore).
295 SectionSP oso_fun_section_sp (new Section (const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()),
296 oso_module, // Module (the .o file)
297 sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
298 exe_symbol->GetMangled().GetName(), // Name the section the same as the symbol for which is was generated!
299 eSectionTypeDebug,
300 oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), // File VM address offset in the current section
301 exe_symbol->GetByteSize(), // File size (we need the size from the executable)
302 0, 0, 0));
303
304 oso_fun_section_sp->SetLinkedLocation (exe_fun_section,
305 exe_symbol->GetValue().GetFileAddress() - exe_fun_section->GetFileAddress());
306 oso_fun_section->GetChildren().AddSection(oso_fun_section_sp);
307 comp_unit_info->debug_map_sections_sp->AddSection(oso_fun_section_sp);
308 }
309 }
310 }
311 break;
312
Greg Clayton7c36fa02010-09-11 03:13:28 +0000313 case eSymbolTypeData:
Chris Lattner24943d22010-06-08 16:52:24 +0000314 {
315 // For each N_GSYM we remap the address for the global by making
316 // a new section that we add to the sections found in the .o file.
317 // This new section has the file address set to what the
318 // addresses are in the .o file, and the load address is adjusted
319 // to match where it ended up in the final executable! We do this
320 // before we parse any dwarf info so that when it goes get parsed
321 // all section/offset addresses that get registered will resolve
322 // correctly to the new addresses in the main executable. We
323 // initially set the section size to be 1 byte, but will need to
324 // fix up these addresses further after all globals have been
325 // parsed to span the gaps, or we can find the global variable
326 // sizes from the DWARF info as we are parsing.
327
328#if 0
329 // First we find the non-stab entry that corresponds to the N_GSYM in the executable
Greg Clayton7c36fa02010-09-11 03:13:28 +0000330 Symbol *exe_gsym_symbol = exe_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner24943d22010-06-08 16:52:24 +0000331#else
332 // The mach-o object file parser already matches up the N_GSYM with with the non-stab
333 // entry, so we shouldn't have to do that. If this ever changes, enable the code above
334 // in the "#if 0" block. STSYM's always match the symbol as found below.
335 Symbol *exe_gsym_symbol = exe_symbol;
336#endif
337 // Next we find the non-stab entry that corresponds to the N_GSYM in the .o file
Greg Clayton7c36fa02010-09-11 03:13:28 +0000338 Symbol *oso_gsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner24943d22010-06-08 16:52:24 +0000339 if (exe_gsym_symbol && oso_gsym_symbol)
340 {
341 // If we found the symbol, then we
342 Section* exe_gsym_section = const_cast<Section *>(exe_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
343 Section* oso_gsym_section = const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
344 if (oso_gsym_section)
345 {
346 SectionSP oso_gsym_section_sp (new Section (const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()),
347 oso_module, // Module (the .o file)
348 sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
349 exe_symbol->GetMangled().GetName(), // Name the section the same as the symbol for which is was generated!
350 eSectionTypeDebug,
351 oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), // File VM address offset in the current section
352 1, // We don't know the size of the global, just do the main address for now.
353 0, 0, 0));
354
355 oso_gsym_section_sp->SetLinkedLocation (exe_gsym_section,
356 exe_gsym_symbol->GetValue().GetFileAddress() - exe_gsym_section->GetFileAddress());
357 oso_gsym_section->GetChildren().AddSection(oso_gsym_section_sp);
358 comp_unit_info->debug_map_sections_sp->AddSection(oso_gsym_section_sp);
359 }
360 }
361 }
362 break;
363
364// case eSymbolTypeStatic:
365// {
366// // For each N_STSYM we remap the address for the global by making
367// // a new section that we add to the sections found in the .o file.
368// // This new section has the file address set to what the
369// // addresses are in the .o file, and the load address is adjusted
370// // to match where it ended up in the final executable! We do this
371// // before we parse any dwarf info so that when it goes get parsed
372// // all section/offset addresses that get registered will resolve
373// // correctly to the new addresses in the main executable. We
374// // initially set the section size to be 1 byte, but will need to
375// // fix up these addresses further after all globals have been
376// // parsed to span the gaps, or we can find the global variable
377// // sizes from the DWARF info as we are parsing.
378//
379//
380// Symbol *exe_stsym_symbol = exe_symbol;
381// // First we find the non-stab entry that corresponds to the N_STSYM in the .o file
382// Symbol *oso_stsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData);
383// if (exe_stsym_symbol && oso_stsym_symbol)
384// {
385// // If we found the symbol, then we
386// Section* exe_stsym_section = const_cast<Section *>(exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
387// Section* oso_stsym_section = const_cast<Section *>(oso_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
388// if (oso_stsym_section)
389// {
390// // The load address of the symbol will use the section in the
391// // executable that contains the debug map that corresponds to
392// // the N_FUN symbol. We set the offset to reflect the offset
393// // into that section since we are creating a new section.
394// AddressRange stsym_load_range(exe_stsym_section, exe_stsym_symbol->GetValue().GetFileAddress() - exe_stsym_section->GetFileAddress(), 1);
395// // We need the symbol's section offset address from the .o file, but
396// // we need a non-zero size.
397// AddressRange stsym_file_range(exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection(), exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), 1);
398//
399// // Now we create a section that we will add as a child of the
400// // section in which the .o symbol (the N_FUN) exists.
401//
402//// TODO: mimic what I did for N_FUN if that works...
403//// // We use the 1 byte for the size because we don't know the
404//// // size of the global symbol without seeing the DWARF.
405//// SectionSP oso_fun_section_sp (new Section ( NULL, oso_module, // Module (the .o file)
406//// sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
407//// exe_symbol->GetMangled().GetName(),// Name the section the same as the symbol for which is was generated!
408//// // &stsym_load_range, // Load offset is the offset into the executable section for the N_FUN from the debug map
409//// &stsym_file_range, // File section/offset is just the same os the symbol on the .o file
410//// 0, 0, 0));
411////
412//// // Now we add the new section to the .o file's sections as a child
413//// // of the section in which the N_SECT symbol exists.
414//// oso_stsym_section->GetChildren().AddSection(oso_fun_section_sp);
415//// comp_unit_info->debug_map_sections_sp->AddSection(oso_fun_section_sp);
416// }
417// }
418// }
419// break;
420 }
421 }
422 }
423#if defined(DEBUG_OSO_DMAP)
424 s << "OSO sections after:\n";
425 oso_objfile->GetSectionList()->Dump(&s, NULL, true);
426#endif
427 }
428 }
429 }
430 if (comp_unit_info->oso_symbol_vendor)
431 return (SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile();
432 return NULL;
433}
434
435uint32_t
436SymbolFileDWARFDebugMap::GetAbilities ()
437{
438 // In order to get the abilities of this plug-in, we look at the list of
439 // N_OSO entries (object files) from the symbol table and make sure that
440 // these files exist and also contain valid DWARF. If we get any of that
441 // then we return the abilities of the first N_OSO's DWARF.
442
443 const uint32_t oso_index_count = GetNumCompileUnits();
444 if (oso_index_count > 0)
445 {
446 const uint32_t dwarf_abilities = SymbolFile::CompileUnits |
447 SymbolFile::Functions |
448 SymbolFile::Blocks |
449 SymbolFile::GlobalVariables |
450 SymbolFile::LocalVariables |
451 SymbolFile::VariableTypes |
452 SymbolFile::LineTables;
453
454 for (uint32_t oso_idx=0; oso_idx<oso_index_count; ++oso_idx)
455 {
456 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
457 if (oso_dwarf)
458 {
459 uint32_t oso_abilities = oso_dwarf->GetAbilities();
460 if ((oso_abilities & dwarf_abilities) == dwarf_abilities)
461 return oso_abilities;
462 }
463 }
464 }
465 return 0;
466}
467
468uint32_t
469SymbolFileDWARFDebugMap::GetNumCompileUnits()
470{
471 InitOSO ();
472 return m_compile_unit_infos.size();
473}
474
475
476CompUnitSP
477SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx)
478{
479 CompUnitSP comp_unit_sp;
480 const uint32_t cu_count = GetNumCompileUnits();
481
482 if (cu_idx < cu_count)
483 {
484 if (m_compile_unit_infos[cu_idx].oso_compile_unit_sp.get() == NULL)
485 {
486 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (cu_idx);
487 if (oso_dwarf)
488 {
489 // There is only one compile unit for N_OSO entry right now, so
490 // it will always exist at index zero.
491 m_compile_unit_infos[cu_idx].oso_compile_unit_sp = m_compile_unit_infos[cu_idx].oso_symbol_vendor->GetCompileUnitAtIndex (0);
492 }
493
494 if (m_compile_unit_infos[cu_idx].oso_compile_unit_sp.get() == NULL)
495 {
496 // We weren't able to get the DWARF for this N_OSO entry (the
497 // .o file may be missing or not at the specified path), make
498 // one up as best we can from the debug map. We set the uid
499 // of the compile unit to the symbol index with the MSBit set
500 // so that it doesn't collide with any uid values from the DWARF
501 Symbol *so_symbol = m_compile_unit_infos[cu_idx].so_symbol;
502 if (so_symbol)
503 {
504 m_compile_unit_infos[cu_idx].oso_compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(),
505 NULL,
506 so_symbol->GetMangled().GetName().AsCString(),
507 cu_idx,
Greg Clayton9488b742010-07-28 02:04:09 +0000508 eLanguageTypeUnknown));
Chris Lattner24943d22010-06-08 16:52:24 +0000509 }
510 }
511 }
512 comp_unit_sp = m_compile_unit_infos[cu_idx].oso_compile_unit_sp;
513 }
514
515 return comp_unit_sp;
516}
517
518SymbolFileDWARFDebugMap::CompileUnitInfo *
519SymbolFileDWARFDebugMap::GetCompUnitInfo (const SymbolContext& sc)
520{
521 const uint32_t cu_count = GetNumCompileUnits();
522 for (uint32_t i=0; i<cu_count; ++i)
523 {
524 if (sc.comp_unit == m_compile_unit_infos[i].oso_compile_unit_sp.get())
525 return &m_compile_unit_infos[i];
526 }
527 return NULL;
528}
529
530size_t
531SymbolFileDWARFDebugMap::ParseCompileUnitFunctions (const SymbolContext& sc)
532{
533 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
534 if (oso_dwarf)
535 return oso_dwarf->ParseCompileUnitFunctions (sc);
536 return 0;
537}
538
539bool
540SymbolFileDWARFDebugMap::ParseCompileUnitLineTable (const SymbolContext& sc)
541{
542 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
543 if (oso_dwarf)
544 return oso_dwarf->ParseCompileUnitLineTable (sc);
545 return false;
546}
547
548bool
549SymbolFileDWARFDebugMap::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
550{
551 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
552 if (oso_dwarf)
553 return oso_dwarf->ParseCompileUnitSupportFiles (sc, support_files);
554 return false;
555}
556
557
558size_t
559SymbolFileDWARFDebugMap::ParseFunctionBlocks (const SymbolContext& sc)
560{
561 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
562 if (oso_dwarf)
563 return oso_dwarf->ParseFunctionBlocks (sc);
564 return 0;
565}
566
567
568size_t
569SymbolFileDWARFDebugMap::ParseTypes (const SymbolContext& sc)
570{
571 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
572 if (oso_dwarf)
573 return oso_dwarf->ParseTypes (sc);
574 return 0;
575}
576
577
578size_t
579SymbolFileDWARFDebugMap::ParseVariablesForContext (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
588
589Type*
590SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid)
591{
592 return NULL;
593}
594
Greg Clayton462d4142010-09-29 01:12:09 +0000595lldb::clang_type_t
596SymbolFileDWARFDebugMap::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_Type)
597{
598 // We have a struct/union/class/enum that needs to be fully resolved.
599 return NULL;
600}
Chris Lattner24943d22010-06-08 16:52:24 +0000601
602uint32_t
603SymbolFileDWARFDebugMap::ResolveSymbolContext (const Address& exe_so_addr, uint32_t resolve_scope, SymbolContext& sc)
604{
605 uint32_t resolved_flags = 0;
606 Symtab* symtab = m_obj_file->GetSymtab();
607 if (symtab)
608 {
609 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
610 sc.symbol = symtab->FindSymbolContainingFileAddress (exe_file_addr, &m_func_indexes[0], m_func_indexes.size());
611
612 if (sc.symbol != NULL)
613 {
614 resolved_flags |= eSymbolContextSymbol;
615
616 uint32_t oso_idx = 0;
Greg Clayton7c36fa02010-09-11 03:13:28 +0000617 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithID (sc.symbol->GetID(), &oso_idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000618 if (comp_unit_info)
619 {
620 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
621 ObjectFile *oso_objfile = GetObjectFileByOSOIndex (oso_idx);
622 if (oso_dwarf && oso_objfile)
623 {
624 SectionList *oso_section_list = oso_objfile->GetSectionList();
625
Greg Clayton178710c2010-09-14 02:20:48 +0000626 SectionSP oso_symbol_section_sp (oso_section_list->FindSectionContainingLinkedFileAddress (exe_file_addr, UINT32_MAX));
Chris Lattner24943d22010-06-08 16:52:24 +0000627
Greg Clayton178710c2010-09-14 02:20:48 +0000628 if (oso_symbol_section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000629 {
Greg Clayton178710c2010-09-14 02:20:48 +0000630 const addr_t linked_file_addr = oso_symbol_section_sp->GetLinkedFileAddress();
631 Address oso_so_addr (oso_symbol_section_sp.get(), exe_file_addr - linked_file_addr);
632 if (oso_so_addr.IsSectionOffset())
633 resolved_flags |= oso_dwarf->ResolveSymbolContext (oso_so_addr, resolve_scope, sc);
Chris Lattner24943d22010-06-08 16:52:24 +0000634 }
Chris Lattner24943d22010-06-08 16:52:24 +0000635 }
636 }
637 }
638 }
639 return resolved_flags;
640}
641
642
643uint32_t
644SymbolFileDWARFDebugMap::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
645{
646 uint32_t initial = sc_list.GetSize();
647 const uint32_t cu_count = GetNumCompileUnits();
648
649 FileSpec so_file_spec;
650 for (uint32_t i=0; i<cu_count; ++i)
651 {
652 if (GetFileSpecForSO (i, so_file_spec))
653 {
654 // By passing false to the comparison we will be able to match
655 // and files given a filename only. If both file_spec and
656 // so_file_spec have directories, we will still do a full match.
657 if (FileSpec::Compare (file_spec, so_file_spec, false) == 0)
658 {
659 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (i);
660
661 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list);
662 }
663 }
664 }
665 return sc_list.GetSize() - initial;
666}
667
668uint32_t
669SymbolFileDWARFDebugMap::PrivateFindGlobalVariables
670(
671 const ConstString &name,
672 const std::vector<uint32_t> &indexes, // Indexes into the symbol table that match "name"
673 uint32_t max_matches,
674 VariableList& variables
675)
676{
677 const uint32_t original_size = variables.GetSize();
678 const size_t match_count = indexes.size();
679 for (size_t i=0; i<match_count; ++i)
680 {
681 uint32_t oso_idx;
682 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithIndex (indexes[i], &oso_idx);
683 if (comp_unit_info)
684 {
685 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
686 if (oso_dwarf)
687 {
688 if (oso_dwarf->FindGlobalVariables(name, true, max_matches, variables))
689 if (variables.GetSize() > max_matches)
690 break;
691 }
692 }
693 }
694 return variables.GetSize() - original_size;
695}
696
697uint32_t
698SymbolFileDWARFDebugMap::FindGlobalVariables (const ConstString &name, bool append, uint32_t max_matches, VariableList& variables)
699{
700
701 // If we aren't appending the results to this list, then clear the list
702 if (!append)
703 variables.Clear();
704
705 // Remember how many variables are in the list before we search in case
706 // we are appending the results to a variable list.
707 const uint32_t original_size = variables.GetSize();
708
709 Symtab* symtab = m_obj_file->GetSymtab();
710 if (symtab)
711 {
712 std::vector<uint32_t> indexes;
Greg Clayton7c36fa02010-09-11 03:13:28 +0000713 const size_t match_count = m_obj_file->GetSymtab()->FindAllSymbolsWithNameAndType (name, eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, indexes);
Chris Lattner24943d22010-06-08 16:52:24 +0000714 if (match_count)
715 {
716 PrivateFindGlobalVariables (name, indexes, max_matches, variables);
717 }
718 }
719 // Return the number of variable that were appended to the list
720 return variables.GetSize() - original_size;
721}
722
723
724uint32_t
725SymbolFileDWARFDebugMap::FindGlobalVariables (const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
726{
727 return 0;
728}
729
730
731int
Greg Clayton7c36fa02010-09-11 03:13:28 +0000732SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000733{
734 const uint32_t symbol_idx = *symbol_idx_ptr;
735
Greg Clayton7c36fa02010-09-11 03:13:28 +0000736 if (symbol_idx < comp_unit_info->first_symbol_index)
Chris Lattner24943d22010-06-08 16:52:24 +0000737 return -1;
738
Greg Clayton7c36fa02010-09-11 03:13:28 +0000739 if (symbol_idx <= comp_unit_info->last_symbol_index)
740 return 0;
741
742 return 1;
743}
744
745
746int
747SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID (user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
748{
749 const user_id_t symbol_id = *symbol_idx_ptr;
750
751 if (symbol_id < comp_unit_info->so_symbol->GetID())
752 return -1;
753
754 if (symbol_id <= comp_unit_info->last_symbol->GetID())
Chris Lattner24943d22010-06-08 16:52:24 +0000755 return 0;
756
757 return 1;
758}
759
760
761SymbolFileDWARFDebugMap::CompileUnitInfo*
762SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex (uint32_t symbol_idx, uint32_t *oso_idx_ptr)
763{
764 const uint32_t oso_index_count = m_compile_unit_infos.size();
765 CompileUnitInfo *comp_unit_info = NULL;
766 if (oso_index_count)
767 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000768 comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(), sizeof(CompileUnitInfo), (comparison_function)SymbolContainsSymbolWithIndex);
Chris Lattner24943d22010-06-08 16:52:24 +0000769 }
770
771 if (oso_idx_ptr)
772 {
773 if (comp_unit_info != NULL)
774 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
775 else
776 *oso_idx_ptr = UINT32_MAX;
777 }
778 return comp_unit_info;
779}
780
Greg Clayton7c36fa02010-09-11 03:13:28 +0000781SymbolFileDWARFDebugMap::CompileUnitInfo*
782SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID (user_id_t symbol_id, uint32_t *oso_idx_ptr)
783{
784 const uint32_t oso_index_count = m_compile_unit_infos.size();
785 CompileUnitInfo *comp_unit_info = NULL;
786 if (oso_index_count)
787 {
788 comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(), sizeof(CompileUnitInfo), (comparison_function)SymbolContainsSymbolWithID);
789 }
790
791 if (oso_idx_ptr)
792 {
793 if (comp_unit_info != NULL)
794 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
795 else
796 *oso_idx_ptr = UINT32_MAX;
797 }
798 return comp_unit_info;
799}
800
801
Greg Clayton88957ff2010-08-17 23:16:15 +0000802static void
803RemoveFunctionsWithModuleNotEqualTo (Module *module, SymbolContextList &sc_list, uint32_t start_idx)
804{
805 // We found functions in .o files. Not all functions in the .o files
806 // will have made it into the final output file. The ones that did
807 // make it into the final output file will have a section whose module
808 // matches the module from the ObjectFile for this SymbolFile. When
809 // the modules don't match, then we have something that was in a
810 // .o file, but doesn't map to anything in the final executable.
811 uint32_t i=start_idx;
812 while (i < sc_list.GetSize())
813 {
814 SymbolContext sc;
815 sc_list.GetContextAtIndex(i, sc);
816 if (sc.function)
817 {
818 const Section *section = sc.function->GetAddressRange().GetBaseAddress().GetSection();
819 if (section->GetModule() != module)
820 {
821 sc_list.RemoveContextAtIndex(i);
822 continue;
823 }
824 }
825 ++i;
826 }
827}
828
Chris Lattner24943d22010-06-08 16:52:24 +0000829uint32_t
Greg Clayton12bec712010-06-28 21:30:43 +0000830SymbolFileDWARFDebugMap::FindFunctions(const ConstString &name, uint32_t name_type_mask, bool append, SymbolContextList& sc_list)
Chris Lattner24943d22010-06-08 16:52:24 +0000831{
832 Timer scoped_timer (__PRETTY_FUNCTION__,
833 "SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
834 name.GetCString());
835
Chris Lattner24943d22010-06-08 16:52:24 +0000836 uint32_t initial_size = 0;
837 if (append)
838 initial_size = sc_list.GetSize();
839 else
840 sc_list.Clear();
841
Greg Clayton7241ba72010-08-17 00:35:34 +0000842 uint32_t oso_idx = 0;
843 SymbolFileDWARF *oso_dwarf;
844 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000845 {
Greg Clayton88957ff2010-08-17 23:16:15 +0000846 uint32_t sc_idx = sc_list.GetSize();
847 if (oso_dwarf->FindFunctions(name, name_type_mask, true, sc_list))
848 {
849 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx);
850 }
Chris Lattner24943d22010-06-08 16:52:24 +0000851 }
852
853 return sc_list.GetSize() - initial_size;
854}
855
856
857uint32_t
858SymbolFileDWARFDebugMap::FindFunctions (const RegularExpression& regex, bool append, SymbolContextList& sc_list)
859{
860 Timer scoped_timer (__PRETTY_FUNCTION__,
861 "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
862 regex.GetText());
863
Greg Clayton7241ba72010-08-17 00:35:34 +0000864 uint32_t initial_size = 0;
865 if (append)
866 initial_size = sc_list.GetSize();
867 else
868 sc_list.Clear();
869
870 uint32_t oso_idx = 0;
871 SymbolFileDWARF *oso_dwarf;
872 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
873 {
Greg Clayton88957ff2010-08-17 23:16:15 +0000874 uint32_t sc_idx = sc_list.GetSize();
875
876 if (oso_dwarf->FindFunctions(regex, true, sc_list))
877 {
878 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx);
879 }
Greg Clayton7241ba72010-08-17 00:35:34 +0000880 }
881
882 return sc_list.GetSize() - initial_size;
Chris Lattner24943d22010-06-08 16:52:24 +0000883}
884
Greg Clayton960d6a42010-08-03 00:35:52 +0000885
886uint32_t
Greg Clayton1924e242010-09-15 05:51:24 +0000887SymbolFileDWARFDebugMap::FindTypes
888(
889 const SymbolContext& sc,
890 const ConstString &name,
891 bool append,
892 uint32_t max_matches,
893 TypeList& types
894)
Greg Clayton960d6a42010-08-03 00:35:52 +0000895{
Greg Clayton960d6a42010-08-03 00:35:52 +0000896 if (!append)
897 types.Clear();
Greg Clayton1924e242010-09-15 05:51:24 +0000898
899 const uint32_t initial_types_size = types.GetSize();
900 SymbolFileDWARF *oso_dwarf;
901
902 if (sc.comp_unit)
903 {
904 oso_dwarf = GetSymbolFile (sc);
905 if (oso_dwarf)
906 return oso_dwarf->FindTypes (sc, name, append, max_matches, types);
907 }
908 else
909 {
910 uint32_t oso_idx = 0;
911 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
912 oso_dwarf->FindTypes (sc, name, append, max_matches, types);
913 }
914
915 return types.GetSize() - initial_types_size;
Greg Clayton960d6a42010-08-03 00:35:52 +0000916}
917
Chris Lattner24943d22010-06-08 16:52:24 +0000918//
919//uint32_t
920//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)
921//{
922// SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
923// if (oso_dwarf)
924// return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, udt_uid, types);
925// return 0;
926//}
927
928//------------------------------------------------------------------
929// PluginInterface protocol
930//------------------------------------------------------------------
931const char *
932SymbolFileDWARFDebugMap::GetPluginName()
933{
934 return "SymbolFileDWARFDebugMap";
935}
936
937const char *
938SymbolFileDWARFDebugMap::GetShortPluginName()
939{
940 return GetPluginNameStatic();
941}
942
943uint32_t
944SymbolFileDWARFDebugMap::GetPluginVersion()
945{
946 return 1;
947}
948
949void
950SymbolFileDWARFDebugMap::GetPluginCommandHelp (const char *command, Stream *strm)
951{
952}
953
954Error
955SymbolFileDWARFDebugMap::ExecutePluginCommand (Args &command, Stream *strm)
956{
957 Error error;
958 error.SetErrorString("No plug-in command are currently supported.");
959 return error;
960}
961
962Log *
963SymbolFileDWARFDebugMap::EnablePluginLogging (Stream *strm, Args &command)
964{
965 return NULL;
966}
967
968