Provide a substitute to load command of gdb.

For bare-metal targets, lldb was missing a command like 'load' in gdb
which can be used to create executable image on the target. This was
discussed in
http://lists.llvm.org/pipermail/lldb-dev/2016-December/011752.html

This commits adds an option to "target module load" command to provide
that functionality. It does not set the PC to entry address which will
be done separately.

Reviewed in https://reviews.llvm.org/D28804

llvm-svn: 292499
diff --git a/lldb/source/Symbol/ObjectFile.cpp b/lldb/source/Symbol/ObjectFile.cpp
index 0b1d6b2..ecae78e 100644
--- a/lldb/source/Symbol/ObjectFile.cpp
+++ b/lldb/source/Symbol/ObjectFile.cpp
@@ -20,6 +20,9 @@
 #include "lldb/Core/Timer.h"
 #include "lldb/Symbol/ObjectContainer.h"
 #include "lldb/Symbol/SymbolFile.h"
+#include "lldb/Target/Target.h"
+#include "lldb/Target/RegisterContext.h"
+#include "lldb/Target/SectionLoadList.h"
 #include "lldb/Target/Process.h"
 #include "lldb/lldb-private.h"
 
@@ -648,3 +651,31 @@
             file_name.GetCString());
   return ConstString(ss.GetString());
 }
+
+Error ObjectFile::LoadInMemory(Target &target) {
+  Error error;
+  ProcessSP process = target.CalculateProcess();
+  if (!process)
+    return Error("No Process");
+
+  SectionList *section_list = GetSectionList();
+  if (!section_list)
+      return Error("No section in object file");
+  size_t section_count = section_list->GetNumSections(0);
+  for (size_t i = 0; i < section_count; ++i) {
+    SectionSP section_sp = section_list->GetSectionAtIndex(i);
+    addr_t addr = target.GetSectionLoadList().GetSectionLoadAddress(section_sp);
+    if (addr != LLDB_INVALID_ADDRESS) {
+      DataExtractor section_data;
+      // We can skip sections like bss
+      if (section_sp->GetFileSize() == 0)
+        continue;
+      section_sp->GetSectionData(section_data);
+      lldb::offset_t written = process->WriteMemory(
+          addr, section_data.GetDataStart(), section_data.GetByteSize(), error);
+      if (written != section_data.GetByteSize())
+        return error;
+    }
+  }
+  return error;
+}