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