<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.




git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@151336 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index 09bdff8..930415a 100644
--- a/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -171,10 +171,11 @@
 }
 
 ObjectFile *
-ObjectFileELF::CreateInstance(Module *module,
-                              DataBufferSP &data_sp,
-                              const FileSpec *file, addr_t offset,
-                              addr_t length)
+ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
+                               DataBufferSP &data_sp,
+                               const FileSpec *file, 
+                               addr_t offset,
+                               addr_t length)
 {
     if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
     {
@@ -184,8 +185,7 @@
             unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
             if (address_size == 4 || address_size == 8)
             {
-                std::auto_ptr<ObjectFileELF> objfile_ap(
-                    new ObjectFileELF(module, data_sp, file, offset, length));
+                std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, file, offset, length));
                 ArchSpec spec;
                 if (objfile_ap->GetArchitecture(spec) &&
                     objfile_ap->SetModulesArchitecture(spec))
@@ -198,7 +198,10 @@
 
 
 ObjectFile*
-ObjectFileELF::CreateMemoryInstance(Module* module, DataBufferSP& data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
+ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp, 
+                                     DataBufferSP& data_sp, 
+                                     const lldb::ProcessSP &process_sp, 
+                                     lldb::addr_t header_addr)
 {
     return NULL;
 }
@@ -228,17 +231,19 @@
 // ObjectFile protocol
 //------------------------------------------------------------------
 
-ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP,
-                             const FileSpec* file, addr_t offset,
-                             addr_t length)
-    : ObjectFile(module, file, offset, length, dataSP),
-      m_header(),
-      m_program_headers(),
-      m_section_headers(),
-      m_sections_ap(),
-      m_symtab_ap(),
-      m_filespec_ap(),
-      m_shstr_data()
+ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, 
+                              DataBufferSP& dataSP,
+                              const FileSpec* file, 
+                              addr_t offset,
+                              addr_t length) : 
+    ObjectFile(module_sp, file, offset, length, dataSP),
+    m_header(),
+    m_program_headers(),
+    m_section_headers(),
+    m_sections_ap(),
+    m_symtab_ap(),
+    m_filespec_ap(),
+    m_shstr_data()
 {
     if (file)
         m_file = *file;
@@ -346,20 +351,20 @@
     if (!dynsym_hdr)
         return Address();
 
-    Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
-    if (!dynsym)
-        return Address();
-    
-    for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
+    SectionSP dynsym_section_sp (section_list->FindSectionByID(dynsym_id));
+    if (dynsym_section_sp)
     {
-        ELFDynamic &symbol = m_dynamic_symbols[i];
-
-        if (symbol.d_tag == DT_DEBUG)
+        for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
         {
-            // Compute the offset as the number of previous entries plus the
-            // size of d_tag.
-            addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
-            return Address(dynsym, offset);
+            ELFDynamic &symbol = m_dynamic_symbols[i];
+
+            if (symbol.d_tag == DT_DEBUG)
+            {
+                // Compute the offset as the number of previous entries plus the
+                // size of d_tag.
+                addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
+                return Address(dynsym_section_sp, offset);
+            }
         }
     }
 
@@ -648,7 +653,6 @@
             
             
             SectionSP section(new Section(
-                0,                  // Parent section.
                 GetModule(),        // Module to which this section belongs.
                 SectionIndex(I),    // Section ID.
                 name,               // Section name.
@@ -697,7 +701,7 @@
         if (symbol.Parse(symtab_data, &offset) == false)
             break;
 
-        Section *symbol_section = NULL;
+        SectionSP symbol_section_sp;
         SymbolType symbol_type = eSymbolTypeInvalid;
         Elf64_Half symbol_idx = symbol.st_shndx;
 
@@ -710,7 +714,7 @@
             symbol_type = eSymbolTypeUndefined;
             break;
         default:
-            symbol_section = section_list->GetSectionAtIndex(symbol_idx).get();
+            symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
             break;
         }
 
@@ -749,9 +753,9 @@
 
         if (symbol_type == eSymbolTypeInvalid)
         {
-            if (symbol_section)
+            if (symbol_section_sp)
             {
-                const ConstString &sect_name = symbol_section->GetName();
+                const ConstString &sect_name = symbol_section_sp->GetName();
                 if (sect_name == text_section_name ||
                     sect_name == init_section_name ||
                     sect_name == fini_section_name ||
@@ -772,25 +776,25 @@
         }
 
         uint64_t symbol_value = symbol.st_value;
-        if (symbol_section != NULL)
-            symbol_value -= symbol_section->GetFileAddress();
+        if (symbol_section_sp)
+            symbol_value -= symbol_section_sp->GetFileAddress();
         const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
         bool is_global = symbol.getBinding() == STB_GLOBAL;
         uint32_t flags = symbol.st_other << 8 | symbol.st_info;
 
         Symbol dc_symbol(
-            i + start_id,    // ID is the original symbol table index.
-            symbol_name,     // Symbol name.
-            false,           // Is the symbol name mangled?
-            symbol_type,     // Type of this symbol
-            is_global,       // Is this globally visible?
-            false,           // Is this symbol debug info?
-            false,           // Is this symbol a trampoline?
-            false,           // Is this symbol artificial?
-            symbol_section,  // Section in which this symbol is defined or null.
-            symbol_value,    // Offset in section or symbol value.
-            symbol.st_size,  // Size in bytes of this symbol.
-            flags);          // Symbol flags.
+            i + start_id,       // ID is the original symbol table index.
+            symbol_name,        // Symbol name.
+            false,              // Is the symbol name mangled?
+            symbol_type,        // Type of this symbol
+            is_global,          // Is this globally visible?
+            false,              // Is this symbol debug info?
+            false,              // Is this symbol a trampoline?
+            false,              // Is this symbol artificial?
+            symbol_section_sp,  // Section in which this symbol is defined or null.
+            symbol_value,       // Offset in section or symbol value.
+            symbol.st_size,     // Size in bytes of this symbol.
+            flags);             // Symbol flags.
         symtab->AddSymbol(dc_symbol);
     }
 
@@ -929,7 +933,7 @@
                     const ELFSectionHeader *rel_hdr,
                     const ELFSectionHeader *plt_hdr,
                     const ELFSectionHeader *sym_hdr,
-                    Section *plt_section,
+                    const lldb::SectionSP &plt_section_sp,
                     DataExtractor &rel_data,
                     DataExtractor &symtab_data,
                     DataExtractor &strtab_data)
@@ -982,7 +986,7 @@
             false,           // Is this symbol debug info?
             true,            // Is this symbol a trampoline?
             true,            // Is this symbol artificial?
-            plt_section,     // Section in which this symbol is defined or null.
+            plt_section_sp,  // Section in which this symbol is defined or null.
             plt_index,       // Offset in section or symbol value.
             plt_entsize,     // Size in bytes of this symbol.
             0);              // Symbol flags.
@@ -1029,8 +1033,8 @@
     if (!rel_section)
         return 0;
 
-    Section *plt_section = section_list->FindSectionByID(plt_id).get();
-    if (!plt_section)
+    SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
+    if (!plt_section_sp)
         return 0;
 
     Section *symtab = section_list->FindSectionByID(symtab_id).get();
@@ -1057,10 +1061,17 @@
     if (!rel_type)
         return 0;
 
-    return ParsePLTRelocations(symbol_table, start_id, rel_type,
-                               &m_header, rel_hdr, plt_hdr, sym_hdr,
-                               plt_section, 
-                               rel_data, symtab_data, strtab_data);
+    return ParsePLTRelocations (symbol_table, 
+                                start_id, 
+                                rel_type,
+                                &m_header, 
+                                rel_hdr, 
+                                plt_hdr, 
+                                sym_hdr,
+                                plt_section_sp, 
+                                rel_data, 
+                                symtab_data, 
+                                strtab_data);
 }
 
 Symtab *