Factor some methods that were in DynamicLoaderPOSIXDYLD.

Move some code that was in DynamicLoaderPOSIXDLYD into the
base class DynamicLoader.  In the case of UpdateLoadedSections(),
the test to see whether a file is loadable (its address is zero)
is not generally applicable so that test is changed to a more
universally applicable check for the SHF_ALLOC flag on the section.

Also make it explicit that the reading of the module_id in
DynamicLoaderPOSIXDYLD::GetThreadLocalData() is using a hardcoded
size (of module_id) of 4, which might not be appropriate on
big-endian 64-bit systems, leaving a FIXME comment in place.

llvm-svn: 200939
diff --git a/lldb/source/Core/DynamicLoader.cpp b/lldb/source/Core/DynamicLoader.cpp
index 82f8404..0f53da8 100644
--- a/lldb/source/Core/DynamicLoader.cpp
+++ b/lldb/source/Core/DynamicLoader.cpp
@@ -10,7 +10,11 @@
 #include "lldb/lldb-private.h"
 #include "lldb/Target/DynamicLoader.h"
 #include "lldb/Target/Process.h"
+#include "lldb/Target/Target.h"
 #include "lldb/Core/PluginManager.h"
+#include "lldb/Core/Module.h"
+#include "lldb/Core/ModuleSpec.h"
+#include "lldb/Core/Section.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -74,3 +78,136 @@
     m_process->SetStopOnSharedLibraryEvents (stop);
 }
 
+ModuleSP
+DynamicLoader::GetTargetExecutable()
+{
+    Target &target = m_process->GetTarget();
+    ModuleSP executable = target.GetExecutableModule();
+
+    if (executable.get())
+    {
+        if (executable->GetFileSpec().Exists())
+        {
+            ModuleSpec module_spec (executable->GetFileSpec(), executable->GetArchitecture());
+            ModuleSP module_sp (new Module (module_spec));
+
+            // Check if the executable has changed and set it to the target executable if they differ.
+            if (module_sp.get() && module_sp->GetUUID().IsValid() && executable->GetUUID().IsValid())
+            {
+                if (module_sp->GetUUID() != executable->GetUUID())
+                    executable.reset();
+            }
+            else if (executable->FileHasChanged())
+            {
+                executable.reset();
+            }
+
+            if (!executable.get())
+            {
+                executable = target.GetSharedModule(module_spec);
+                if (executable.get() != target.GetExecutableModulePointer())
+                {
+                    // Don't load dependent images since we are in dyld where we will know
+                    // and find out about all images that are loaded
+                    const bool get_dependent_images = false;
+                    target.SetExecutableModule(executable, get_dependent_images);
+                }
+            }
+        }
+    }
+    return executable;
+}
+
+void
+DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, addr_t base_addr)
+{
+    UpdateLoadedSectionsCommon(module, base_addr);
+}
+
+void
+DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module, addr_t base_addr)
+{
+    bool changed;
+    module->SetLoadAddress(m_process->GetTarget(), base_addr, changed);
+}
+
+void
+DynamicLoader::UnloadSections(const ModuleSP module)
+{
+    UnloadSectionsCommon(module);
+}
+
+void
+DynamicLoader::UnloadSectionsCommon(const ModuleSP module)
+{
+    Target &target = m_process->GetTarget();
+    const SectionList *sections = GetSectionListFromModule(module);
+
+    assert(sections && "SectionList missing from unloaded module.");
+
+    const size_t num_sections = sections->GetSize();
+    for (size_t i = 0; i < num_sections; ++i)
+    {
+        SectionSP section_sp (sections->GetSectionAtIndex(i));
+        target.SetSectionUnloaded(section_sp);
+    }
+}
+
+
+const SectionList *
+DynamicLoader::GetSectionListFromModule(const ModuleSP module) const
+{
+    SectionList *sections = nullptr;
+    if (module.get())
+    {
+        ObjectFile *obj_file = module->GetObjectFile();
+        if (obj_file)
+        {
+            sections = obj_file->GetSectionList();
+        }
+    }
+    return sections;
+}
+
+ModuleSP
+DynamicLoader::LoadModuleAtAddress(const FileSpec &file, addr_t link_map_addr, addr_t base_addr)
+{
+    Target &target = m_process->GetTarget();
+    ModuleList &modules = target.GetImages();
+    ModuleSP module_sp;
+
+    ModuleSpec module_spec (file, target.GetArchitecture());
+    if ((module_sp = modules.FindFirstModule (module_spec)))
+    {
+        UpdateLoadedSections(module_sp, link_map_addr, base_addr);
+    }
+    else if ((module_sp = target.GetSharedModule(module_spec)))
+    {
+        UpdateLoadedSections(module_sp, link_map_addr, base_addr);
+    }
+
+    return module_sp;
+}
+
+int64_t
+DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, int size_in_bytes)
+{
+    Error error;
+
+    uint64_t value = m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
+    if (error.Fail())
+        return -1;
+    else
+        return (int64_t)value;
+}
+
+addr_t
+DynamicLoader::ReadPointer(addr_t addr)
+{
+    Error error;
+    addr_t value = m_process->ReadPointerFromMemory(addr, error);
+    if (error.Fail())
+        return LLDB_INVALID_ADDRESS;
+    else
+        return value;
+}
diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp
index 3815a2b..64a1679 100644
--- a/lldb/source/Core/Module.cpp
+++ b/lldb/source/Core/Module.cpp
@@ -1502,28 +1502,12 @@
 bool 
 Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed)
 {
-    size_t num_loaded_sections = 0;
-    SectionList *section_list = GetSectionList ();
-    if (section_list)
+    ObjectFile *object_file = GetObjectFile();
+    if (object_file)
     {
-        const size_t num_sections = section_list->GetSize();
-        size_t sect_idx = 0;
-        for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
-        {
-            // Iterate through the object file sections to find the
-            // first section that starts of file offset zero and that
-            // has bytes in the file...
-            SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
-            // Only load non-thread specific sections when given a slide
-            if (section_sp && !section_sp->IsThreadSpecific())
-            {
-                if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + offset))
-                    ++num_loaded_sections;
-            }
-        }
+        return object_file->SetLoadAddress(target, offset);
     }
-    changed = num_loaded_sections > 0;
-    return num_loaded_sections > 0;
+    return false;
 }