MCJIT support for non-function sections.

Move to a by-section allocation and relocation scheme. This allows
better support for sections which do not contain externally visible
symbols.

Flesh out the relocation address vs. local storage address separation a
bit more as well. Remote process JITs use this to tell the relocation
resolution code where the code will live when it executes.

The startFunctionBody/endFunctionBody interfaces to the JIT and the
memory manager are deprecated. They'll stick around for as long as the
old JIT does, but the MCJIT doesn't use them anymore.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148258 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
index 54cb350..5c45cf3 100644
--- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
@@ -154,17 +154,31 @@
   return false;
 }
 
+void RuntimeDyldELF::resolveRelocations() {
+  // FIXME: deprecated. should be changed to use the by-section
+  // allocation and relocation scheme.
+
+  // Just iterate over the symbols in our symbol table and assign their
+  // addresses.
+  StringMap<SymbolLoc>::iterator i = SymbolTable.begin();
+  StringMap<SymbolLoc>::iterator e = SymbolTable.end();
+  for (;i != e; ++i) {
+    assert (i->getValue().second == 0 && "non-zero offset in by-function sym!");
+    reassignSymbolAddress(i->getKey(),
+                          (uint8_t*)Sections[i->getValue().first].base());
+  }
+}
+
 void RuntimeDyldELF::resolveX86_64Relocation(StringRef Name,
                                              uint8_t *Addr,
                                              const RelocationEntry &RE) {
   uint8_t *TargetAddr;
   if (RE.IsFunctionRelative) {
-    StringMap<sys::MemoryBlock>::iterator ContainingFunc
-      = Functions.find(RE.Target);
-    assert(ContainingFunc != Functions.end()
-           && "Function for relocation not found");
-    TargetAddr = reinterpret_cast<uint8_t*>(ContainingFunc->getValue().base()) +
-                 RE.Offset;
+    StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(RE.Target);
+    assert(Loc != SymbolTable.end() && "Function for relocation not found");
+    TargetAddr =
+      reinterpret_cast<uint8_t*>(Sections[Loc->second.first].base()) +
+      Loc->second.second + RE.Offset;
   } else {
     // FIXME: Get the address of the target section and add that to RE.Offset
     assert(0 && ("Non-function relocation not implemented yet!"));
@@ -209,12 +223,11 @@
                                           const RelocationEntry &RE) {
   uint8_t *TargetAddr;
   if (RE.IsFunctionRelative) {
-    StringMap<sys::MemoryBlock>::iterator ContainingFunc
-      = Functions.find(RE.Target);
-    assert(ContainingFunc != Functions.end()
-           && "Function for relocation not found");
-    TargetAddr = reinterpret_cast<uint8_t*>(
-      ContainingFunc->getValue().base()) + RE.Offset;
+    StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(RE.Target);
+    assert(Loc != SymbolTable.end() && "Function for relocation not found");
+    TargetAddr =
+      reinterpret_cast<uint8_t*>(Sections[Loc->second.first].base()) +
+      Loc->second.second + RE.Offset;
   } else {
     // FIXME: Get the address of the target section and add that to RE.Offset
     assert(0 && ("Non-function relocation not implemented yet!"));
@@ -266,7 +279,11 @@
 }
 
 void RuntimeDyldELF::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
-  SymbolTable[Name] = Addr;
+  // FIXME: deprecated. switch to reassignSectionAddress() instead.
+  //
+  // Actually moving the symbol address requires by-section mapping.
+  assert(Sections[SymbolTable.lookup(Name).first].base() == (void*)Addr &&
+         "Unable to relocate section in by-function JIT allocation model!");
 
   RelocationList &Relocs = Relocations[Name];
   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
@@ -275,6 +292,20 @@
   }
 }
 
+// Assign an address to a symbol name and resolve all the relocations
+// associated with it.
+void RuntimeDyldELF::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
+  // The address to use for relocation resolution is not
+  // the address of the local section buffer. We must be doing
+  // a remote execution environment of some sort. Re-apply any
+  // relocations referencing this section with the given address.
+  //
+  // Addr is a uint64_t because we can't assume the pointer width
+  // of the target is the same as that of the host. Just use a generic
+  // "big enough" type.
+  assert(0);
+}
+
 bool RuntimeDyldELF::isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
   StringRef Magic = InputBuffer->getBuffer().slice(0, ELF::EI_NIDENT);
   return (memcmp(Magic.data(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;