1 //===-- 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 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 void
28 SymbolFileDWARFDebugMap::Initialize()
29 {
30     PluginManager::RegisterPlugin (GetPluginNameStatic(),
31                                    GetPluginDescriptionStatic(),
32                                    CreateInstance);
33 }
34 
35 void
36 SymbolFileDWARFDebugMap::Terminate()
37 {
38     PluginManager::UnregisterPlugin (CreateInstance);
39 }
40 
41 
42 const char *
43 SymbolFileDWARFDebugMap::GetPluginNameStatic()
44 {
45     return "symbol-file.dwarf2-debugmap";
46 }
47 
48 const char *
49 SymbolFileDWARFDebugMap::GetPluginDescriptionStatic()
50 {
51     return "DWARF and DWARF3 debug symbol file reader (debug map).";
52 }
53 
54 SymbolFile*
55 SymbolFileDWARFDebugMap::CreateInstance (ObjectFile* obj_file)
56 {
57     return new SymbolFileDWARFDebugMap (obj_file);
58 }
59 
60 
61 SymbolFileDWARFDebugMap::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 
71 SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap()
72 {
73 }
74 
75 void
76 SymbolFileDWARFDebugMap::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 
94         symtab->AppendSymbolIndexesWithType (eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes);
95         symtab->AppendSymbolIndexesWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, m_glob_indexes);
96 
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]);
112                 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);
118             }
119         }
120     }
121 }
122 
123 Module *
124 SymbolFileDWARFDebugMap::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 
132 Module *
133 SymbolFileDWARFDebugMap::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(), true);
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 
157 bool
158 SymbolFileDWARFDebugMap::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(), true);
172         }
173         file_spec = m_compile_unit_infos[oso_idx].so_file;
174         return true;
175     }
176     return false;
177 }
178 
179 
180 
181 ObjectFile *
182 SymbolFileDWARFDebugMap::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 
190 SymbolFileDWARF *
191 SymbolFileDWARFDebugMap::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 
199 ObjectFile *
200 SymbolFileDWARFDebugMap::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 
208 SymbolFileDWARF *
209 SymbolFileDWARFDebugMap::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 
216 SymbolFileDWARF *
217 SymbolFileDWARFDebugMap::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 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);
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.
252                 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;
255                 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                     {
262                         if (exe_symbol->IsDebug() == false)
263                             continue;
264 
265                         switch (exe_symbol->GetType())
266                         {
267                         case eSymbolTypeCode:
268                             {
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
279                                 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
280                                 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 
313                         case eSymbolTypeData:
314                             {
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
330                                 Symbol *exe_gsym_symbol = exe_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
331 #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
338                                 Symbol *oso_gsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
339                                 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 
435 uint32_t
436 SymbolFileDWARFDebugMap::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 
468 uint32_t
469 SymbolFileDWARFDebugMap::GetNumCompileUnits()
470 {
471     InitOSO ();
472     return m_compile_unit_infos.size();
473 }
474 
475 
476 CompUnitSP
477 SymbolFileDWARFDebugMap::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,
508                                                                                             eLanguageTypeUnknown));
509 
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);
513                 }
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 
522 SymbolFileDWARFDebugMap::CompileUnitInfo *
523 SymbolFileDWARFDebugMap::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 
534 size_t
535 SymbolFileDWARFDebugMap::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 
543 bool
544 SymbolFileDWARFDebugMap::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 
552 bool
553 SymbolFileDWARFDebugMap::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 
562 size_t
563 SymbolFileDWARFDebugMap::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 
572 size_t
573 SymbolFileDWARFDebugMap::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 
582 size_t
583 SymbolFileDWARFDebugMap::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 
593 Type*
594 SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid)
595 {
596     return NULL;
597 }
598 
599 lldb::clang_type_t
600 SymbolFileDWARFDebugMap::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 }
605 
606 uint32_t
607 SymbolFileDWARFDebugMap::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;
621             CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithID (sc.symbol->GetID(), &oso_idx);
622             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 
630                     SectionSP oso_symbol_section_sp (oso_section_list->FindSectionContainingLinkedFileAddress (exe_file_addr, UINT32_MAX));
631 
632                     if (oso_symbol_section_sp)
633                     {
634                         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);
638                     }
639                 }
640             }
641         }
642     }
643     return resolved_flags;
644 }
645 
646 
647 uint32_t
648 SymbolFileDWARFDebugMap::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 
672 uint32_t
673 SymbolFileDWARFDebugMap::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 
701 uint32_t
702 SymbolFileDWARFDebugMap::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;
717         const size_t match_count = m_obj_file->GetSymtab()->FindAllSymbolsWithNameAndType (name, eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, indexes);
718         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 
728 uint32_t
729 SymbolFileDWARFDebugMap::FindGlobalVariables (const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
730 {
731     return 0;
732 }
733 
734 
735 int
736 SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info)
737 {
738     const uint32_t symbol_idx = *symbol_idx_ptr;
739 
740     if (symbol_idx < comp_unit_info->first_symbol_index)
741         return -1;
742 
743     if (symbol_idx <= comp_unit_info->last_symbol_index)
744         return 0;
745 
746     return 1;
747 }
748 
749 
750 int
751 SymbolFileDWARFDebugMap::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())
759         return 0;
760 
761     return 1;
762 }
763 
764 
765 SymbolFileDWARFDebugMap::CompileUnitInfo*
766 SymbolFileDWARFDebugMap::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     {
772         comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(), sizeof(CompileUnitInfo), (comparison_function)SymbolContainsSymbolWithIndex);
773     }
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 
785 SymbolFileDWARFDebugMap::CompileUnitInfo*
786 SymbolFileDWARFDebugMap::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 
806 static void
807 RemoveFunctionsWithModuleNotEqualTo (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 
833 uint32_t
834 SymbolFileDWARFDebugMap::FindFunctions(const ConstString &name, uint32_t name_type_mask, bool append, SymbolContextList& sc_list)
835 {
836     Timer scoped_timer (__PRETTY_FUNCTION__,
837                         "SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
838                         name.GetCString());
839 
840     uint32_t initial_size = 0;
841     if (append)
842         initial_size = sc_list.GetSize();
843     else
844         sc_list.Clear();
845 
846     uint32_t oso_idx = 0;
847     SymbolFileDWARF *oso_dwarf;
848     while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
849     {
850         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         }
855     }
856 
857     return sc_list.GetSize() - initial_size;
858 }
859 
860 
861 uint32_t
862 SymbolFileDWARFDebugMap::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 
868     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     {
878         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         }
884     }
885 
886     return sc_list.GetSize() - initial_size;
887 }
888 
889 
890 uint32_t
891 SymbolFileDWARFDebugMap::FindTypes
892 (
893     const SymbolContext& sc,
894     const ConstString &name,
895     bool append,
896     uint32_t max_matches,
897     TypeList& types
898 )
899 {
900     if (!append)
901         types.Clear();
902 
903     const uint32_t initial_types_size = types.GetSize();
904     SymbolFileDWARF *oso_dwarf;
905 
906     if (sc.comp_unit)
907     {
908         oso_dwarf = GetSymbolFile (sc);
909         if (oso_dwarf)
910             return oso_dwarf->FindTypes (sc, name, append, max_matches, types);
911     }
912     else
913     {
914         uint32_t oso_idx = 0;
915         while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL)
916             oso_dwarf->FindTypes (sc, name, append, max_matches, types);
917     }
918 
919     return types.GetSize() - initial_types_size;
920 }
921 
922 //
923 //uint32_t
924 //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)
925 //{
926 //  SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
927 //  if (oso_dwarf)
928 //      return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, udt_uid, types);
929 //  return 0;
930 //}
931 
932 //------------------------------------------------------------------
933 // PluginInterface protocol
934 //------------------------------------------------------------------
935 const char *
936 SymbolFileDWARFDebugMap::GetPluginName()
937 {
938     return "SymbolFileDWARFDebugMap";
939 }
940 
941 const char *
942 SymbolFileDWARFDebugMap::GetShortPluginName()
943 {
944     return GetPluginNameStatic();
945 }
946 
947 uint32_t
948 SymbolFileDWARFDebugMap::GetPluginVersion()
949 {
950     return 1;
951 }
952 
953 void
954 SymbolFileDWARFDebugMap::GetPluginCommandHelp (const char *command, Stream *strm)
955 {
956 }
957 
958 Error
959 SymbolFileDWARFDebugMap::ExecutePluginCommand (Args &command, Stream *strm)
960 {
961     Error error;
962     error.SetErrorString("No plug-in command are currently supported.");
963     return error;
964 }
965 
966 Log *
967 SymbolFileDWARFDebugMap::EnablePluginLogging (Stream *strm, Args &command)
968 {
969     return NULL;
970 }
971 
972 
973 void
974 SymbolFileDWARFDebugMap::SetCompileUnit (SymbolFileDWARF *oso_dwarf, const CompUnitSP &cu_sp)
975 {
976     const uint32_t cu_count = GetNumCompileUnits();
977     for (uint32_t i=0; i<cu_count; ++i)
978     {
979         if (m_compile_unit_infos[i].oso_symbol_vendor &&
980             m_compile_unit_infos[i].oso_symbol_vendor->GetSymbolFile() == oso_dwarf)
981         {
982             if (m_compile_unit_infos[i].oso_compile_unit_sp)
983             {
984                 assert (m_compile_unit_infos[i].oso_compile_unit_sp.get() == cu_sp.get());
985             }
986             else
987             {
988                 m_compile_unit_infos[i].oso_compile_unit_sp = cu_sp;
989             }
990         }
991     }
992 }
993 
994