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/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
index cff7cbd..bbfef76 100644
--- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
@@ -16,6 +16,7 @@
 
 #include "llvm/ExecutionEngine/RuntimeDyld.h"
 #include "llvm/Object/MachOObject.h"
+#include "llvm/ADT/IndexedMap.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/ADT/SmallVector.h"
@@ -40,17 +41,18 @@
   // The MemoryManager to load objects into.
   RTDyldMemoryManager *MemMgr;
 
-  // FIXME: This all assumes we're dealing with external symbols for anything
-  //        explicitly referenced. I.e., we can index by name and things
-  //        will work out. In practice, this may not be the case, so we
-  //        should find a way to effectively generalize.
-
-  // For each function, we have a MemoryBlock of it's instruction data.
-  StringMap<sys::MemoryBlock> Functions;
+  // For each section, we have a MemoryBlock of it's data.
+  // Indexed by SectionID.
+  SmallVector<sys::MemoryBlock, 32> Sections;
+  // For each section, the address it will be considered to live at for
+  // relocations. The same as the pointer the above memory block for hosted
+  // JITs. Indexed by SectionID.
+  SmallVector<uint64_t, 32> SectionLoadAddress;
 
   // Master symbol table. As modules are loaded and external symbols are
-  // resolved, their addresses are stored here.
-  StringMap<uint8_t*> SymbolTable;
+  // resolved, their addresses are stored here as a SectionID/Offset pair.
+  typedef std::pair<unsigned, uint64_t> SymbolLoc;
+  StringMap<SymbolLoc> SymbolTable;
 
   bool HasError;
   std::string ErrorStr;
@@ -62,6 +64,9 @@
     return true;
   }
 
+  uint8_t *getSectionAddress(unsigned SectionID) {
+    return (uint8_t*)Sections[SectionID].base();
+  }
   void extractFunction(StringRef Name, uint8_t *StartAddress,
                        uint8_t *EndAddress);
 
@@ -75,12 +80,15 @@
   void *getSymbolAddress(StringRef Name) {
     // FIXME: Just look up as a function for now. Overly simple of course.
     // Work in progress.
-    return SymbolTable.lookup(Name);
+    if (SymbolTable.find(Name) == SymbolTable.end())
+      return 0;
+    SymbolLoc Loc = SymbolTable.lookup(Name);
+    return getSectionAddress(Loc.first) + Loc.second;
   }
 
-  void resolveRelocations();
+  virtual void resolveRelocations();
 
-  virtual void reassignSymbolAddress(StringRef Name, uint8_t *Addr) = 0;
+  virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0;
 
   // Is the linker in an error state?
   bool hasError() { return HasError; }
@@ -128,6 +136,8 @@
     StringMap<RelocationList> Relocations;
     unsigned Arch;
 
+    void resolveRelocations();
+
     void resolveX86_64Relocation(StringRef Name,
                                  uint8_t *Addr,
                                  const RelocationEntry &RE);
@@ -150,6 +160,7 @@
   bool loadObject(MemoryBuffer *InputBuffer);
 
   void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
+  void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
 
   bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const;
 };
@@ -160,30 +171,34 @@
   // For each symbol, keep a list of relocations based on it. Anytime
   // its address is reassigned (the JIT re-compiled the function, e.g.),
   // the relocations get re-resolved.
+  // The symbol (or section) the relocation is sourced from is the Key
+  // in the relocation list where it's stored.
   struct RelocationEntry {
-    std::string Target;     // Object this relocation is contained in.
-    uint64_t    Offset;     // Offset into the object for the relocation.
+    unsigned    SectionID;  // Section the relocation is contained in.
+    uint64_t    Offset;     // Offset into the section for the relocation.
     uint32_t    Data;       // Second word of the raw macho relocation entry.
-    int64_t     Addend;     // Addend encoded in the instruction itself, if any.
-    bool        isResolved; // Has this relocation been resolved previously?
+    int64_t     Addend;     // Addend encoded in the instruction itself, if any,
+                            // plus the offset into the source section for
+                            // the symbol once the relocation is resolvable.
 
-    RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend)
-      : Target(t), Offset(offset), Data(data), Addend(addend),
-        isResolved(false) {}
+    RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend)
+      : SectionID(id), Offset(offset), Data(data), Addend(addend) {}
   };
   typedef SmallVector<RelocationEntry, 4> RelocationList;
-  StringMap<RelocationList> Relocations;
+  // Relocations to sections already loaded. Indexed by SectionID which is the
+  // source of the address. The target where the address will be writen is
+  // SectionID/Offset in the relocation itself.
+  IndexedMap<RelocationList> Relocations;
+  // Relocations to symbols that are not yet resolved. Must be external
+  // relocations by definition. Indexed by symbol name.
+  StringMap<RelocationList> UnresolvedRelocations;
 
-  // FIXME: Also keep a map of all the relocations contained in an object. Use
-  // this to dynamically answer whether all of the relocations in it have
-  // been resolved or not.
-
-  bool resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel,
-                         unsigned Type, unsigned Size);
+  bool resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel,
+                         unsigned Type, unsigned Size, int64_t Addend);
   bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
-                               unsigned Type, unsigned Size);
+                               unsigned Type, unsigned Size, int64_t Addend);
   bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
-                            unsigned Type, unsigned Size);
+                            unsigned Type, unsigned Size, int64_t Addend);
 
   bool loadSegment32(const MachOObject *Obj,
                      const MachOObject::LoadCommandInfo *SegmentLCI,
@@ -191,13 +206,23 @@
   bool loadSegment64(const MachOObject *Obj,
                      const MachOObject::LoadCommandInfo *SegmentLCI,
                      const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
+  bool processSymbols32(const MachOObject *Obj,
+                      SmallVectorImpl<unsigned> &SectionMap,
+                      SmallVectorImpl<StringRef> &SymbolNames,
+                      const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
+  bool processSymbols64(const MachOObject *Obj,
+                      SmallVectorImpl<unsigned> &SectionMap,
+                      SmallVectorImpl<StringRef> &SymbolNames,
+                      const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
+
+  void resolveSymbol(StringRef Name);
 
 public:
   RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
 
   bool loadObject(MemoryBuffer *InputBuffer);
 
-  void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
+  void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
 
   static bool isKnownFormat(const MemoryBuffer *InputBuffer);