blob: 3e2d7b76e328614bb6ceb8a6524684a90501e977 [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
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 Claytonbcf2cfb2010-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 Lattner30fdc8d2010-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 Claytonbcf2cfb2010-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 Lattner30fdc8d2010-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 {
Greg Clayton274060b2010-10-20 20:54:39 +0000140 FileSpec oso_file_spec(oso_symbol->GetMangled().GetName().AsCString(), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141
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();
Greg Clayton274060b2010-10-20 20:54:39 +0000171 m_compile_unit_infos[oso_idx].so_file.SetFile(so_path.c_str(), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172 }
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 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000230 // Set a a pointer to this class to set our OSO DWARF file know
231 // that the DWARF is being used along with a debug map and that
232 // it will have the remapped sections that we do below.
233 ((SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile())->SetDebugMapSymfile(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 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 Claytonbcf2cfb2010-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 Lattner30fdc8d2010-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 Claytonbcf2cfb2010-09-11 03:13:28 +0000262 if (exe_symbol->IsDebug() == false)
263 continue;
264
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 switch (exe_symbol->GetType())
266 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000267 case eSymbolTypeCode:
Chris Lattner30fdc8d2010-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 Claytonbcf2cfb2010-09-11 03:13:28 +0000279 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-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 Claytonbcf2cfb2010-09-11 03:13:28 +0000313 case eSymbolTypeData:
Chris Lattner30fdc8d2010-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 Claytonbcf2cfb2010-09-11 03:13:28 +0000330 Symbol *exe_gsym_symbol = exe_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-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 Claytonbcf2cfb2010-09-11 03:13:28 +0000338 Symbol *oso_gsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
Chris Lattner30fdc8d2010-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 Clayton9e409562010-07-28 02:04:09 +0000508 eLanguageTypeUnknown));
Greg Clayton450e3f32010-10-12 02:24:53 +0000509
510 // Let our symbol vendor know about this compile unit
511 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex (m_compile_unit_infos[cu_idx].oso_compile_unit_sp,
512 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 }
514 }
515 }
516 comp_unit_sp = m_compile_unit_infos[cu_idx].oso_compile_unit_sp;
517 }
518
519 return comp_unit_sp;
520}
521
522SymbolFileDWARFDebugMap::CompileUnitInfo *
523SymbolFileDWARFDebugMap::GetCompUnitInfo (const SymbolContext& sc)
524{
525 const uint32_t cu_count = GetNumCompileUnits();
526 for (uint32_t i=0; i<cu_count; ++i)
527 {
528 if (sc.comp_unit == m_compile_unit_infos[i].oso_compile_unit_sp.get())
529 return &m_compile_unit_infos[i];
530 }
531 return NULL;
532}
533
534size_t
535SymbolFileDWARFDebugMap::ParseCompileUnitFunctions (const SymbolContext& sc)
536{
537 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
538 if (oso_dwarf)
539 return oso_dwarf->ParseCompileUnitFunctions (sc);
540 return 0;
541}
542
543bool
544SymbolFileDWARFDebugMap::ParseCompileUnitLineTable (const SymbolContext& sc)
545{
546 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
547 if (oso_dwarf)
548 return oso_dwarf->ParseCompileUnitLineTable (sc);
549 return false;
550}
551
552bool
553SymbolFileDWARFDebugMap::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
554{
555 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
556 if (oso_dwarf)
557 return oso_dwarf->ParseCompileUnitSupportFiles (sc, support_files);
558 return false;
559}
560
561
562size_t
563SymbolFileDWARFDebugMap::ParseFunctionBlocks (const SymbolContext& sc)
564{
565 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
566 if (oso_dwarf)
567 return oso_dwarf->ParseFunctionBlocks (sc);
568 return 0;
569}
570
571
572size_t
573SymbolFileDWARFDebugMap::ParseTypes (const SymbolContext& sc)
574{
575 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
576 if (oso_dwarf)
577 return oso_dwarf->ParseTypes (sc);
578 return 0;
579}
580
581
582size_t
583SymbolFileDWARFDebugMap::ParseVariablesForContext (const SymbolContext& sc)
584{
585 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
586 if (oso_dwarf)
587 return oso_dwarf->ParseTypes (sc);
588 return 0;
589}
590
591
592
593Type*
594SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid)
595{
596 return NULL;
597}
598
Greg Clayton1be10fc2010-09-29 01:12:09 +0000599lldb::clang_type_t
600SymbolFileDWARFDebugMap::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_Type)
601{
602 // We have a struct/union/class/enum that needs to be fully resolved.
603 return NULL;
604}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605
606uint32_t
607SymbolFileDWARFDebugMap::ResolveSymbolContext (const Address& exe_so_addr, uint32_t resolve_scope, SymbolContext& sc)
608{
609 uint32_t resolved_flags = 0;
610 Symtab* symtab = m_obj_file->GetSymtab();
611 if (symtab)
612 {
613 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
614 sc.symbol = symtab->FindSymbolContainingFileAddress (exe_file_addr, &m_func_indexes[0], m_func_indexes.size());
615
616 if (sc.symbol != NULL)
617 {
618 resolved_flags |= eSymbolContextSymbol;
619
620 uint32_t oso_idx = 0;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000621 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithID (sc.symbol->GetID(), &oso_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 if (comp_unit_info)
623 {
624 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
625 ObjectFile *oso_objfile = GetObjectFileByOSOIndex (oso_idx);
626 if (oso_dwarf && oso_objfile)
627 {
628 SectionList *oso_section_list = oso_objfile->GetSectionList();
629
Greg Clayton016a95e2010-09-14 02:20:48 +0000630 SectionSP oso_symbol_section_sp (oso_section_list->FindSectionContainingLinkedFileAddress (exe_file_addr, UINT32_MAX));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631
Greg Clayton016a95e2010-09-14 02:20:48 +0000632 if (oso_symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 {
Greg Clayton016a95e2010-09-14 02:20:48 +0000634 const addr_t linked_file_addr = oso_symbol_section_sp->GetLinkedFileAddress();
635 Address oso_so_addr (oso_symbol_section_sp.get(), exe_file_addr - linked_file_addr);
636 if (oso_so_addr.IsSectionOffset())
637 resolved_flags |= oso_dwarf->ResolveSymbolContext (oso_so_addr, resolve_scope, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 }
640 }
641 }
642 }
643 return resolved_flags;
644}
645
646
647uint32_t
648SymbolFileDWARFDebugMap::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
649{
650 uint32_t initial = sc_list.GetSize();
651 const uint32_t cu_count = GetNumCompileUnits();
652
653 FileSpec so_file_spec;
654 for (uint32_t i=0; i<cu_count; ++i)
655 {
656 if (GetFileSpecForSO (i, so_file_spec))
657 {
658 // By passing false to the comparison we will be able to match
659 // and files given a filename only. If both file_spec and
660 // so_file_spec have directories, we will still do a full match.
661 if (FileSpec::Compare (file_spec, so_file_spec, false) == 0)
662 {
663 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (i);
664
665 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list);
666 }
667 }
668 }
669 return sc_list.GetSize() - initial;
670}
671
672uint32_t
673SymbolFileDWARFDebugMap::PrivateFindGlobalVariables
674(
675 const ConstString &name,
676 const std::vector<uint32_t> &indexes, // Indexes into the symbol table that match "name"
677 uint32_t max_matches,
678 VariableList& variables
679)
680{
681 const uint32_t original_size = variables.GetSize();
682 const size_t match_count = indexes.size();
683 for (size_t i=0; i<match_count; ++i)
684 {
685 uint32_t oso_idx;
686 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithIndex (indexes[i], &oso_idx);
687 if (comp_unit_info)
688 {
689 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx);
690 if (oso_dwarf)
691 {
692 if (oso_dwarf->FindGlobalVariables(name, true, max_matches, variables))
693 if (variables.GetSize() > max_matches)
694 break;
695 }
696 }
697 }
698 return variables.GetSize() - original_size;
699}
700
701uint32_t
702SymbolFileDWARFDebugMap::FindGlobalVariables (const ConstString &name, bool append, uint32_t max_matches, VariableList& variables)
703{
704
705 // If we aren't appending the results to this list, then clear the list
706 if (!append)
707 variables.Clear();
708
709 // Remember how many variables are in the list before we search in case
710 // we are appending the results to a variable list.
711 const uint32_t original_size = variables.GetSize();
712
713 Symtab* symtab = m_obj_file->GetSymtab();
714 if (symtab)
715 {
716 std::vector<uint32_t> indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000717 const size_t match_count = m_obj_file->GetSymtab()->FindAllSymbolsWithNameAndType (name, eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 if (match_count)
719 {
720 PrivateFindGlobalVariables (name, indexes, max_matches, variables);
721 }
722 }
723 // Return the number of variable that were appended to the list
724 return variables.GetSize() - original_size;
725}
726
727
728uint32_t
729SymbolFileDWARFDebugMap::FindGlobalVariables (const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
730{
731 return 0;
732}
733
734
735int
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000736SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737{
738 const uint32_t symbol_idx = *symbol_idx_ptr;
739
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000740 if (symbol_idx < comp_unit_info->first_symbol_index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 return -1;
742
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000743 if (symbol_idx <= comp_unit_info->last_symbol_index)
744 return 0;
745
746 return 1;
747}
748
749
750int
751SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID (user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
752{
753 const user_id_t symbol_id = *symbol_idx_ptr;
754
755 if (symbol_id < comp_unit_info->so_symbol->GetID())
756 return -1;
757
758 if (symbol_id <= comp_unit_info->last_symbol->GetID())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 return 0;
760
761 return 1;
762}
763
764
765SymbolFileDWARFDebugMap::CompileUnitInfo*
766SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex (uint32_t symbol_idx, uint32_t *oso_idx_ptr)
767{
768 const uint32_t oso_index_count = m_compile_unit_infos.size();
769 CompileUnitInfo *comp_unit_info = NULL;
770 if (oso_index_count)
771 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000772 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 +0000773 }
774
775 if (oso_idx_ptr)
776 {
777 if (comp_unit_info != NULL)
778 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
779 else
780 *oso_idx_ptr = UINT32_MAX;
781 }
782 return comp_unit_info;
783}
784
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000785SymbolFileDWARFDebugMap::CompileUnitInfo*
786SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID (user_id_t symbol_id, uint32_t *oso_idx_ptr)
787{
788 const uint32_t oso_index_count = m_compile_unit_infos.size();
789 CompileUnitInfo *comp_unit_info = NULL;
790 if (oso_index_count)
791 {
792 comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(), sizeof(CompileUnitInfo), (comparison_function)SymbolContainsSymbolWithID);
793 }
794
795 if (oso_idx_ptr)
796 {
797 if (comp_unit_info != NULL)
798 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
799 else
800 *oso_idx_ptr = UINT32_MAX;
801 }
802 return comp_unit_info;
803}
804
805
Greg Clayton3ef3fc62010-08-17 23:16:15 +0000806static void
807RemoveFunctionsWithModuleNotEqualTo (Module *module, SymbolContextList &sc_list, uint32_t start_idx)
808{
809 // We found functions in .o files. Not all functions in the .o files
810 // will have made it into the final output file. The ones that did
811 // make it into the final output file will have a section whose module
812 // matches the module from the ObjectFile for this SymbolFile. When
813 // the modules don't match, then we have something that was in a
814 // .o file, but doesn't map to anything in the final executable.
815 uint32_t i=start_idx;
816 while (i < sc_list.GetSize())
817 {
818 SymbolContext sc;
819 sc_list.GetContextAtIndex(i, sc);
820 if (sc.function)
821 {
822 const Section *section = sc.function->GetAddressRange().GetBaseAddress().GetSection();
823 if (section->GetModule() != module)
824 {
825 sc_list.RemoveContextAtIndex(i);
826 continue;
827 }
828 }
829 ++i;
830 }
831}
832
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833uint32_t
Greg Clayton0c5cd902010-06-28 21:30:43 +0000834SymbolFileDWARFDebugMap::FindFunctions(const ConstString &name, uint32_t name_type_mask, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835{
836 Timer scoped_timer (__PRETTY_FUNCTION__,
837 "SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
838 name.GetCString());
839
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840 uint32_t initial_size = 0;
841 if (append)
842 initial_size = sc_list.GetSize();
843 else
844 sc_list.Clear();
845
Greg Clayton57a6b992010-08-17 00:35:34 +0000846 uint32_t oso_idx = 0;
847 SymbolFileDWARF *oso_dwarf;
848 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849 {
Greg Clayton3ef3fc62010-08-17 23:16:15 +0000850 uint32_t sc_idx = sc_list.GetSize();
851 if (oso_dwarf->FindFunctions(name, name_type_mask, true, sc_list))
852 {
853 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx);
854 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 }
856
857 return sc_list.GetSize() - initial_size;
858}
859
860
861uint32_t
862SymbolFileDWARFDebugMap::FindFunctions (const RegularExpression& regex, bool append, SymbolContextList& sc_list)
863{
864 Timer scoped_timer (__PRETTY_FUNCTION__,
865 "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
866 regex.GetText());
867
Greg Clayton57a6b992010-08-17 00:35:34 +0000868 uint32_t initial_size = 0;
869 if (append)
870 initial_size = sc_list.GetSize();
871 else
872 sc_list.Clear();
873
874 uint32_t oso_idx = 0;
875 SymbolFileDWARF *oso_dwarf;
876 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
877 {
Greg Clayton3ef3fc62010-08-17 23:16:15 +0000878 uint32_t sc_idx = sc_list.GetSize();
879
880 if (oso_dwarf->FindFunctions(regex, true, sc_list))
881 {
882 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx);
883 }
Greg Clayton57a6b992010-08-17 00:35:34 +0000884 }
885
886 return sc_list.GetSize() - initial_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887}
888
Greg Clayton2ccf8cf2010-11-07 21:02:03 +0000889TypeSP
890SymbolFileDWARFDebugMap::FindDefinitionTypeForDIE (
891 DWARFCompileUnit* cu,
892 const DWARFDebugInfoEntry *die,
893 const ConstString &type_name
894)
895{
896 TypeSP type_sp;
897 SymbolFileDWARF *oso_dwarf;
898 for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx)
899 {
900 type_sp = oso_dwarf->FindDefinitionTypeForDIE (cu, die, type_name);
901 if (type_sp)
902 break;
903 }
904 return type_sp;
905}
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000906
907uint32_t
Greg Clayton6dbd3982010-09-15 05:51:24 +0000908SymbolFileDWARFDebugMap::FindTypes
909(
910 const SymbolContext& sc,
911 const ConstString &name,
912 bool append,
913 uint32_t max_matches,
914 TypeList& types
915)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000916{
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000917 if (!append)
918 types.Clear();
Greg Clayton6dbd3982010-09-15 05:51:24 +0000919
920 const uint32_t initial_types_size = types.GetSize();
921 SymbolFileDWARF *oso_dwarf;
922
923 if (sc.comp_unit)
924 {
925 oso_dwarf = GetSymbolFile (sc);
926 if (oso_dwarf)
927 return oso_dwarf->FindTypes (sc, name, append, max_matches, types);
928 }
929 else
930 {
931 uint32_t oso_idx = 0;
932 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
933 oso_dwarf->FindTypes (sc, name, append, max_matches, types);
934 }
935
936 return types.GetSize() - initial_types_size;
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000937}
938
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939//
940//uint32_t
941//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)
942//{
943// SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
944// if (oso_dwarf)
945// return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, udt_uid, types);
946// return 0;
947//}
948
949//------------------------------------------------------------------
950// PluginInterface protocol
951//------------------------------------------------------------------
952const char *
953SymbolFileDWARFDebugMap::GetPluginName()
954{
955 return "SymbolFileDWARFDebugMap";
956}
957
958const char *
959SymbolFileDWARFDebugMap::GetShortPluginName()
960{
961 return GetPluginNameStatic();
962}
963
964uint32_t
965SymbolFileDWARFDebugMap::GetPluginVersion()
966{
967 return 1;
968}
969
970void
971SymbolFileDWARFDebugMap::GetPluginCommandHelp (const char *command, Stream *strm)
972{
973}
974
975Error
976SymbolFileDWARFDebugMap::ExecutePluginCommand (Args &command, Stream *strm)
977{
978 Error error;
979 error.SetErrorString("No plug-in command are currently supported.");
980 return error;
981}
982
983Log *
984SymbolFileDWARFDebugMap::EnablePluginLogging (Stream *strm, Args &command)
985{
986 return NULL;
987}
988
989
Greg Clayton450e3f32010-10-12 02:24:53 +0000990void
991SymbolFileDWARFDebugMap::SetCompileUnit (SymbolFileDWARF *oso_dwarf, const CompUnitSP &cu_sp)
992{
993 const uint32_t cu_count = GetNumCompileUnits();
994 for (uint32_t i=0; i<cu_count; ++i)
995 {
996 if (m_compile_unit_infos[i].oso_symbol_vendor &&
997 m_compile_unit_infos[i].oso_symbol_vendor->GetSymbolFile() == oso_dwarf)
998 {
999 if (m_compile_unit_infos[i].oso_compile_unit_sp)
1000 {
1001 assert (m_compile_unit_infos[i].oso_compile_unit_sp.get() == cu_sp.get());
1002 }
1003 else
1004 {
1005 m_compile_unit_infos[i].oso_compile_unit_sp = cu_sp;
1006 }
1007 }
1008 }
1009}
1010