<rdar://problem/10103468>

I started work on being able to add symbol files after a debug session
had started with a new "target symfile add" command and quickly ran into
problems with stale Address objects in breakpoint locations that had 
lldb_private::Section pointers into modules that had been removed or 
replaced. This also let to grabbing stale modules from those sections. 
So I needed to thread harded the Address, Section and related objects.

To do this I modified the ModuleChild class to now require a ModuleSP
on initialization so that a weak reference can created. I also changed
all places that were handing out "Section *" to have them hand out SectionSP.
All ObjectFile, SymbolFile and SymbolVendors were inheriting from ModuleChild
so all of the find plug-in, static creation function and constructors now
require ModuleSP references instead of Module *. 

Address objects now have weak references to their sections which can
safely go stale when a module gets destructed. 

This checkin doesn't complete the "target symfile add" command, but it
does get us a lot clioser to being able to do such things without a high
risk of crashing or memory corruption.

llvm-svn: 151336
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index 56fc593..2f0d835 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -303,7 +303,11 @@
 SymbolFileDWARF::~SymbolFileDWARF()
 {
     if (m_is_external_ast_source)
-        m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource ();
+    {
+        ModuleSP module_sp (m_obj_file->GetModule());
+        if (module_sp)
+            module_sp->GetClangASTContext().RemoveExternalSource ();
+    }
 }
 
 static const ConstString &
@@ -346,8 +350,8 @@
 SymbolFileDWARF::InitializeObject()
 {
     // Install our external AST source callbacks so we can complete Clang types.
-    Module *module = m_obj_file->GetModule();
-    if (module)
+    ModuleSP module_sp (m_obj_file->GetModule());
+    if (module_sp)
     {
         const SectionList *section_list = m_obj_file->GetSectionList();
 
@@ -531,17 +535,17 @@
         const SectionList *section_list = m_obj_file->GetSectionList();
         if (section_list)
         {
-            Section *section = section_list->FindSectionByType(sect_type, true).get();
-            if (section)
+            SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
+            if (section_sp)
             {
                 // See if we memory mapped the DWARF segment?
                 if (m_dwarf_data.GetByteSize())
                 {
-                    data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
+                    data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetByteSize());
                 }
                 else
                 {
-                    if (m_obj_file->ReadSectionData (section, data) == 0)
+                    if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
                         data.Clear();
                 }
             }
@@ -2183,7 +2187,7 @@
         
     if (sc.function)
     {        
-        sc.module_sp = sc.function->CalculateSymbolContextModule()->shared_from_this();
+        sc.module_sp = sc.function->CalculateSymbolContextModule();
         return true;
     }
     
@@ -2195,7 +2199,7 @@
 {
     Timer scoped_timer(__PRETTY_FUNCTION__,
                        "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
-                       so_addr.GetSection(),
+                       so_addr.GetSection().get(),
                        so_addr.GetOffset(),
                        resolve_scope);
     uint32_t resolved = 0;
@@ -2601,7 +2605,7 @@
     if (num_matches)
     {
         SymbolContext sc;
-        sc.module_sp = m_obj_file->GetModule()->shared_from_this();
+        sc.module_sp = m_obj_file->GetModule();
         assert (sc.module_sp);
         
         DWARFDebugInfo* debug_info = DebugInfo();
@@ -2687,7 +2691,7 @@
     }
 
     SymbolContext sc;
-    sc.module_sp = m_obj_file->GetModule()->shared_from_this();
+    sc.module_sp = m_obj_file->GetModule();
     assert (sc.module_sp);
     
     DWARFCompileUnit* dwarf_cu = NULL;
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
index 9584a50..7e72048 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
@@ -281,7 +281,7 @@
                 comp_unit_info->debug_map_sections_sp.reset(new SectionList);
 
                 Symtab *exe_symtab = m_obj_file->GetSymtab();
-                Module *oso_module = oso_objfile->GetModule();
+                ModuleSP oso_module_sp (oso_objfile->GetModule());
                 Symtab *oso_symtab = oso_objfile->GetSymtab();
 //#define DEBUG_OSO_DMAP    // Do not check in with this defined...
 #if defined(DEBUG_OSO_DMAP)
@@ -330,8 +330,8 @@
                                 if (oso_fun_symbol)
                                 {
                                     // If we found the symbol, then we
-                                    Section* exe_fun_section = const_cast<Section *>(exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
-                                    Section* oso_fun_section = const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
+                                    SectionSP exe_fun_section (exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
+                                    SectionSP oso_fun_section (oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
                                     if (oso_fun_section)
                                     {
                                         // Now we create a section that we will add as a child of the
@@ -342,14 +342,14 @@
                                         // size will reflect any size changes (ppc has been known to
                                         // shrink function sizes when it gets rid of jump islands that
                                         // aren't needed anymore).
-                                        SectionSP oso_fun_section_sp (new Section (const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()),
-                                                                                   oso_module,                         // Module (the .o file)
-                                                                                   sect_id++,                          // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
-                                                                                   exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), // Name the section the same as the symbol for which is was generated!
-                                                                                   eSectionTypeDebug,
-                                                                                   oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(),  // File VM address offset in the current section
-                                                                                   exe_symbol->GetByteSize(),          // File size (we need the size from the executable)
-                                                                                   0, 0, 0));
+                                        SectionSP oso_fun_section_sp (new Section (oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection(),
+                                                                                        oso_module_sp,                         // Module (the .o file)
+                                                                                        sect_id++,                          // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
+                                                                                        exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), // Name the section the same as the symbol for which is was generated!
+                                                                                        eSectionTypeDebug,
+                                                                                        oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(),  // File VM address offset in the current section
+                                                                                        exe_symbol->GetByteSize(),          // File size (we need the size from the executable)
+                                                                                        0, 0, 0));
 
                                         oso_fun_section_sp->SetLinkedLocation (exe_fun_section,
                                                                                exe_symbol->GetValue().GetFileAddress() - exe_fun_section->GetFileAddress());
@@ -384,12 +384,12 @@
                                 if (exe_symbol && oso_gsym_symbol && exe_symbol->GetAddressRangePtr() && oso_gsym_symbol->GetAddressRangePtr())
                                 {
                                     // If we found the symbol, then we
-                                    Section* exe_gsym_section = const_cast<Section *>(exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
-                                    Section* oso_gsym_section = const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
+                                    SectionSP exe_gsym_section (exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
+                                    SectionSP oso_gsym_section (oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection());
                                     if (oso_gsym_section)
                                     {
-                                        SectionSP oso_gsym_section_sp (new Section (const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()),
-                                                                                    oso_module,                         // Module (the .o file)
+                                        SectionSP oso_gsym_section_sp (new Section (oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection(),
+                                                                                    oso_module_sp,                         // Module (the .o file)
                                                                                     sect_id++,                          // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs
                                                                                     exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), // Name the section the same as the symbol for which is was generated!
                                                                                     eSectionTypeDebug,
@@ -624,7 +624,7 @@
                     if (oso_symbol_section_sp)
                     {
                         const addr_t linked_file_addr = oso_symbol_section_sp->GetLinkedFileAddress();
-                        Address oso_so_addr (oso_symbol_section_sp.get(), exe_file_addr - linked_file_addr);
+                        Address oso_so_addr (oso_symbol_section_sp, exe_file_addr - linked_file_addr);
                         if (oso_so_addr.IsSectionOffset())
                             resolved_flags |= oso_dwarf->ResolveSymbolContext (oso_so_addr, resolve_scope, sc);
                     }
@@ -855,7 +855,7 @@
 
 
 static void
-RemoveFunctionsWithModuleNotEqualTo (Module *module, SymbolContextList &sc_list, uint32_t start_idx)
+RemoveFunctionsWithModuleNotEqualTo (const ModuleSP &module_sp, SymbolContextList &sc_list, uint32_t start_idx)
 {
     // We found functions in .o files. Not all functions in the .o files
     // will have made it into the final output file. The ones that did
@@ -870,8 +870,8 @@
         sc_list.GetContextAtIndex(i, sc);
         if (sc.function)
         {
-            const Section *section = sc.function->GetAddressRange().GetBaseAddress().GetSection();
-            if (section->GetModule() != module)
+            const SectionSP section_sp (sc.function->GetAddressRange().GetBaseAddress().GetSection());
+            if (section_sp->GetModule() != module_sp)
             {
                 sc_list.RemoveContextAtIndex(i);
                 continue;
diff --git a/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp b/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp
index f1e0c4f..d4164cd 100644
--- a/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp
+++ b/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp
@@ -161,7 +161,7 @@
     {
         const Symbol *cu_symbol = m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
         if (cu_symbol)
-            cu_sp.reset(new CompileUnit(m_obj_file->GetModule(), NULL, cu_symbol->GetMangled().GetName().AsCString(), 0, eLanguageTypeUnknown));
+            cu_sp.reset(new CompileUnit (m_obj_file->GetModule(), NULL, cu_symbol->GetMangled().GetName().AsCString(), 0, eLanguageTypeUnknown));
     }
     return cu_sp;
 }