[llvm-commits] [llvm] r148258 - in /llvm/trunk: include/llvm/ExecutionEngine/JITMemoryManager.h include/llvm/ExecutionEngine/RuntimeDyld.h lib/ExecutionEngine/JIT/JITMemoryManager.cpp lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp tools/llvm-rtdyld/llvm-rtdyld.cpp unittests/ExecutionEngine/JIT/JITTest.cpp

Jim Grosbach grosbach at apple.com
Mon Jan 16 14:26:40 PST 2012


Author: grosbach
Date: Mon Jan 16 16:26:39 2012
New Revision: 148258

URL: http://llvm.org/viewvc/llvm-project?rev=148258&view=rev
Log:
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.

Modified:
    llvm/trunk/include/llvm/ExecutionEngine/JITMemoryManager.h
    llvm/trunk/include/llvm/ExecutionEngine/RuntimeDyld.h
    llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
    llvm/trunk/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h
    llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
    llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
    llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
    llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp
    llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp
    llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp

Modified: llvm/trunk/include/llvm/ExecutionEngine/JITMemoryManager.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/JITMemoryManager.h?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/JITMemoryManager.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/JITMemoryManager.h Mon Jan 16 16:26:39 2012
@@ -101,6 +101,22 @@
   virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
                                uint8_t *FunctionEnd) = 0;
 
+  /// allocateCodeSection - Allocate a memory block of (at least) the given
+  /// size suitable for executable code. The SectionID is a unique identifier
+  /// assigned by the JIT and passed through to the memory manager for
+  /// the instance class to use if it needs to communicate to the JIT about
+  /// a given section after the fact.
+  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+                                       unsigned SectionID) = 0;
+
+  /// allocateDataSection - Allocate a memory block of (at least) the given
+  /// size suitable for data. The SectionID is a unique identifier
+  /// assigned by the JIT and passed through to the memory manager for
+  /// the instance class to use if it needs to communicate to the JIT about
+  /// a given section after the fact.
+  virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+                                       unsigned SectionID) = 0;
+
   /// allocateSpace - Allocate a memory block of the given size.  This method
   /// cannot be called between calls to startFunctionBody and endFunctionBody.
   virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0;

Modified: llvm/trunk/include/llvm/ExecutionEngine/RuntimeDyld.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/RuntimeDyld.h?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/RuntimeDyld.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/RuntimeDyld.h Mon Jan 16 16:26:39 2012
@@ -35,6 +35,16 @@
   RTDyldMemoryManager() {}
   virtual ~RTDyldMemoryManager();
 
+  /// allocateCodeSection - Allocate a memory block of (at least) the given
+  /// size suitable for executable code.
+  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+                                       unsigned SectionID) = 0;
+
+  /// allocateDataSection - Allocate a memory block of (at least) the given
+  /// size suitable for data.
+  virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+                                       unsigned SectionID) = 0;
+
   // Allocate ActualSize bytes, or more, for the named function. Return
   // a pointer to the allocated memory and update Size to reflect how much
   // memory was acutally allocated.
@@ -65,9 +75,9 @@
   void *getSymbolAddress(StringRef Name);
   // Resolve the relocations for all symbols we currently know about.
   void resolveRelocations();
-  // Change the address associated with a symbol when resolving relocations.
+  // Change the address associated with a section when resolving relocations.
   // Any relocations already associated with the symbol will be re-resolved.
-  void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
+  void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
   StringRef getErrorString();
 };
 

Modified: llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp Mon Jan 16 16:26:39 2012
@@ -441,6 +441,50 @@
       return (uint8_t*)DataAllocator.Allocate(Size, Alignment);
     }
 
+    /// allocateCodeSection - Allocate memory for a code section.
+    uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+                                 unsigned SectionID) {
+      // FIXME: Alignement handling.
+      FreeRangeHeader* candidateBlock = FreeMemoryList;
+      FreeRangeHeader* head = FreeMemoryList;
+      FreeRangeHeader* iter = head->Next;
+
+      uintptr_t largest = candidateBlock->BlockSize;
+
+      // Search for the largest free block.
+      while (iter != head) {
+        if (iter->BlockSize > largest) {
+          largest = iter->BlockSize;
+          candidateBlock = iter;
+        }
+        iter = iter->Next;
+      }
+
+      largest = largest - sizeof(MemoryRangeHeader);
+
+      // If this block isn't big enough for the allocation desired, allocate
+      // another block of memory and add it to the free list.
+      if (largest < Size || largest <= FreeRangeHeader::getMinBlockSize()) {
+        DEBUG(dbgs() << "JIT: Allocating another slab of memory for function.");
+        candidateBlock = allocateNewCodeSlab((size_t)Size);
+      }
+
+      // Select this candidate block for allocation
+      CurBlock = candidateBlock;
+
+      // Allocate the entire memory block.
+      FreeMemoryList = candidateBlock->AllocateBlock();
+      // Release the memory at the end of this block that isn't needed.
+      FreeMemoryList = CurBlock->TrimAllocationToSize(FreeMemoryList, Size);
+      return (uint8_t *)(CurBlock + 1);
+    }
+
+    /// allocateDataSection - Allocate memory for a data section.
+    uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+                                 unsigned SectionID) {
+      return (uint8_t*)DataAllocator.Allocate(Size, Alignment);
+    }
+
     /// startExceptionTable - Use startFunctionBody to allocate memory for the
     /// function's exception table.
     uint8_t* startExceptionTable(const Function* F, uintptr_t &ActualSize) {

Modified: llvm/trunk/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h (original)
+++ llvm/trunk/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h Mon Jan 16 16:26:39 2012
@@ -31,6 +31,16 @@
   // We own the JMM, so make sure to delete it.
   ~MCJITMemoryManager() { delete JMM; }
 
+  uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+                               unsigned SectionID) {
+    return JMM->allocateDataSection(Size, Alignment, SectionID);
+  }
+
+  uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+                               unsigned SectionID) {
+    return JMM->allocateCodeSection(Size, Alignment, SectionID);
+  }
+
   // Allocate ActualSize bytes, or more, for the named function. Return
   // a pointer to the allocated memory and update Size to reflect how much
   // memory was acutally allocated.

Modified: llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp Mon Jan 16 16:26:39 2012
@@ -25,6 +25,7 @@
 
 void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
                                       uint8_t *EndAddress) {
+  // FIXME: DEPRECATED in favor of by-section allocation.
   // Allocate memory for the function via the memory manager.
   uintptr_t Size = EndAddress - StartAddress + 1;
   uintptr_t AllocSize = Size;
@@ -35,21 +36,22 @@
   memcpy(Mem, StartAddress, Size);
   MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size);
   // Remember where we put it.
-  Functions[Name] = sys::MemoryBlock(Mem, Size);
+  unsigned SectionID = Sections.size();
+  Sections.push_back(sys::MemoryBlock(Mem, Size));
+
   // Default the assigned address for this symbol to wherever this
   // allocated it.
-  SymbolTable[Name] = Mem;
+  SymbolTable[Name] = SymbolLoc(SectionID, 0); 
   DEBUG(dbgs() << "    allocated to [" << Mem << ", " << Mem + Size << "]\n");
 }
 
 // Resolve the relocations for all symbols we currently know about.
 void RuntimeDyldImpl::resolveRelocations() {
-  // Just iterate over the symbols in our symbol table and assign their
-  // addresses.
-  StringMap<uint8_t*>::iterator i = SymbolTable.begin();
-  StringMap<uint8_t*>::iterator e = SymbolTable.end();
-  for (;i != e; ++i)
-    reassignSymbolAddress(i->getKey(), i->getValue());
+  // Just iterate over the sections we have and resolve all the relocations
+  // in them. Gross overkill, but it gets the job done.
+  for (int i = 0, e = Sections.size(); i != e; ++i) {
+    reassignSectionAddress(i, SectionLoadAddress[i]);
+  }
 }
 
 //===----------------------------------------------------------------------===//
@@ -109,8 +111,9 @@
   Dyld->resolveRelocations();
 }
 
-void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
-  Dyld->reassignSymbolAddress(Name, Addr);
+void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
+                                         uint64_t Addr) {
+  Dyld->reassignSectionAddress(SectionID, Addr);
 }
 
 StringRef RuntimeDyld::getErrorString() {

Modified: llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp Mon Jan 16 16:26:39 2012
@@ -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;

Modified: llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h (original)
+++ llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h Mon Jan 16 16:26:39 2012
@@ -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);
 

Modified: llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp Mon Jan 16 16:26:39 2012
@@ -22,25 +22,24 @@
 namespace llvm {
 
 bool RuntimeDyldMachO::
-resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel,
-                  unsigned Type, unsigned Size) {
+resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel,
+                  unsigned Type, unsigned Size, int64_t Addend) {
   // This just dispatches to the proper target specific routine.
   switch (CPUType) {
   default: assert(0 && "Unsupported CPU type!");
   case mach::CTM_x86_64:
     return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value,
-                                   isPCRel, Type, Size);
+                                   isPCRel, Type, Size, Addend);
   case mach::CTM_ARM:
     return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value,
-                                isPCRel, Type, Size);
+                                isPCRel, Type, Size, Addend);
   }
   llvm_unreachable("");
 }
 
 bool RuntimeDyldMachO::
-resolveX86_64Relocation(uintptr_t Address, uintptr_t Value,
-                        bool isPCRel, unsigned Type,
-                        unsigned Size) {
+resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
+                        unsigned Type, unsigned Size, int64_t Addend) {
   // If the relocation is PC-relative, the value to be encoded is the
   // pointer difference.
   if (isPCRel)
@@ -75,9 +74,9 @@
   return false;
 }
 
-bool RuntimeDyldMachO::resolveARMRelocation(uintptr_t Address, uintptr_t Value,
-                                         bool isPCRel, unsigned Type,
-                                         unsigned Size) {
+bool RuntimeDyldMachO::
+resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
+                     unsigned Type, unsigned Size, int64_t Addend) {
   // If the relocation is PC-relative, the value to be encoded is the
   // pointer difference.
   if (isPCRel) {
@@ -135,84 +134,63 @@
 loadSegment32(const MachOObject *Obj,
               const MachOObject::LoadCommandInfo *SegmentLCI,
               const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
+  // FIXME: This should really be combined w/ loadSegment64. Templatized
+  // function on the 32/64 datatypes maybe?
   InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
   Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC);
   if (!SegmentLC)
     return Error("unable to load segment load command");
 
+
+  SmallVector<unsigned, 16> SectionMap;
   for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
     InMemoryStruct<macho::Section> Sect;
     Obj->ReadSection(*SegmentLCI, SectNum, Sect);
     if (!Sect)
       return Error("unable to load section: '" + Twine(SectNum) + "'");
 
-    // FIXME: For the time being, we're only loading text segments.
+    // Allocate memory via the MM for the section.
+    uint8_t *Buffer;
+    uint32_t SectionID = Sections.size();
     if (Sect->Flags != 0x80000400)
-      continue;
+      Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
+    else
+      Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
+
+    DEBUG(dbgs() << "Loading "
+                 << ((Sect->Flags == 0x80000400) ? "text" : "data")
+                 << " (ID #" << SectionID << ")"
+                 << " '" << Sect->SegmentName << ","
+                 << Sect->Name << "' of size " << Sect->Size
+                 << " to address " << Buffer << ".\n");
 
-    // Address and names of symbols in the section.
-    typedef std::pair<uint64_t, StringRef> SymbolEntry;
-    SmallVector<SymbolEntry, 64> Symbols;
-    // Index of all the names, in this section or not. Used when we're
-    // dealing with relocation entries.
-    SmallVector<StringRef, 64> SymbolNames;
-    for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
-      InMemoryStruct<macho::SymbolTableEntry> STE;
-      Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
-      if (!STE)
-        return Error("unable to read symbol: '" + Twine(i) + "'");
-      if (STE->SectionIndex > SegmentLC->NumSections)
-        return Error("invalid section index for symbol: '" + Twine(i) + "'");
-      // Get the symbol name.
-      StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
-      SymbolNames.push_back(Name);
-
-      // Just skip symbols not defined in this section.
-      if ((unsigned)STE->SectionIndex - 1 != SectNum)
-        continue;
-
-      // FIXME: Check the symbol type and flags.
-      if (STE->Type != 0xF)  // external, defined in this section.
-        continue;
-      // Flags == 0x8 marks a thumb function for ARM, which is fine as it
-      // doesn't require any special handling here.
-      // Flags in the upper nibble we don't care about.
-      if ((STE->Flags & 0xf) != 0x0 && STE->Flags != 0x8)
-        continue;
+    // Copy the payload from the object file into the allocated buffer.
+    uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
+                                           SegmentLC->FileSize).data();
+    memcpy(Buffer, Base + Sect->Address, Sect->Size);
 
-      // Remember the symbol.
-      Symbols.push_back(SymbolEntry(STE->Value, Name));
+    // Remember what got allocated for this SectionID.
+    Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
 
-      DEBUG(dbgs() << "Function sym: '" << Name << "' @ " <<
-            (Sect->Address + STE->Value) << "\n");
-    }
-    // Sort the symbols by address, just in case they didn't come in that way.
-    array_pod_sort(Symbols.begin(), Symbols.end());
+    // By default, the load address of a section is its memory buffer.
+    SectionLoadAddress.push_back((uint64_t)Buffer);
 
-    // If there weren't any functions (odd, but just in case...)
-    if (!Symbols.size())
-      continue;
+    // Keep a map of object file section numbers to corresponding SectionIDs
+    // while processing the file.
+    SectionMap.push_back(SectionID);
+  }
 
-    // Extract the function data.
-    uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
-                                           SegmentLC->FileSize).data();
-    for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) {
-      uint64_t StartOffset = Sect->Address + Symbols[i].first;
-      uint64_t EndOffset = Symbols[i + 1].first - 1;
-      DEBUG(dbgs() << "Extracting function: " << Symbols[i].second
-                   << " from [" << StartOffset << ", " << EndOffset << "]\n");
-      extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset);
-    }
-    // The last symbol we do after since the end address is calculated
-    // differently because there is no next symbol to reference.
-    uint64_t StartOffset = Symbols[Symbols.size() - 1].first;
-    uint64_t EndOffset = Sect->Size - 1;
-    DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second
-                 << " from [" << StartOffset << ", " << EndOffset << "]\n");
-    extractFunction(Symbols[Symbols.size()-1].second,
-                    Base + StartOffset, Base + EndOffset);
+  // Process the symbol table.
+  SmallVector<StringRef, 64> SymbolNames;
+  processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC);
 
-    // Now extract the relocation information for each function and process it.
+  // Process the relocations for each section we're loading.
+  Relocations.grow(Relocations.size() + SegmentLC->NumSections);
+  for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
+    InMemoryStruct<macho::Section> Sect;
+    Obj->ReadSection(*SegmentLCI, SectNum, Sect);
+    if (!Sect)
+      return Error("unable to load section: '" + Twine(SectNum) + "'");
     for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
       InMemoryStruct<macho::RelocationEntry> RE;
       Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
@@ -222,51 +200,53 @@
       // relocation should be applied. We need to translate that into an
       // offset into a function since that's our atom.
       uint32_t Offset = RE->Word0;
-      // Look for the function containing the address. This is used for JIT
-      // code, so the number of functions in section is almost always going
-      // to be very small (usually just one), so until we have use cases
-      // where that's not true, just use a trivial linear search.
-      unsigned SymbolNum;
-      unsigned NumSymbols = Symbols.size();
-      assert(NumSymbols > 0 && Symbols[0].first <= Offset &&
-             "No symbol containing relocation!");
-      for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum)
-        if (Symbols[SymbolNum + 1].first > Offset)
-          break;
-      // Adjust the offset to be relative to the symbol.
-      Offset -= Symbols[SymbolNum].first;
-      // Get the name of the symbol containing the relocation.
-      StringRef TargetName = SymbolNames[SymbolNum];
-
       bool isExtern = (RE->Word1 >> 27) & 1;
+
+      // FIXME: Get the relocation addend from the target address.
+      // FIXME: VERY imporant for internal relocations.
+
       // Figure out the source symbol of the relocation. If isExtern is true,
       // this relocation references the symbol table, otherwise it references
       // a section in the same object, numbered from 1 through NumSections
       // (SectionBases is [0, NumSections-1]).
-      // FIXME: Some targets (ARM) use internal relocations even for
-      // externally visible symbols, if the definition is in the same
-      // file as the reference. We need to convert those back to by-name
-      // references. We can resolve the address based on the section
-      // offset and see if we have a symbol at that address. If we do,
-      // use that; otherwise, puke.
-      if (!isExtern)
-        return Error("Internal relocations not supported.");
       uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
-      StringRef SourceName = SymbolNames[SourceNum];
-
-      // FIXME: Get the relocation addend from the target address.
-
-      // Now store the relocation information. Associate it with the source
-      // symbol.
-      Relocations[SourceName].push_back(RelocationEntry(TargetName,
-                                                        Offset,
-                                                        RE->Word1,
-                                                        0 /*Addend*/));
-      DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset
-                   << " from '" << SourceName << "(Word1: "
-                   << format("0x%x", RE->Word1) << ")\n");
+      if (!isExtern) {
+        assert(SourceNum > 0 && "Invalid relocation section number!");
+        unsigned SectionID = SectionMap[SourceNum - 1];
+        unsigned TargetID = SectionMap[SectNum];
+        DEBUG(dbgs() << "Internal relocation at Section #"
+                     << TargetID << " + " << Offset
+                     << " from Section #"
+                     << SectionID << " (Word1: "
+                     << format("0x%x", RE->Word1) << ")\n");
+
+        // Store the relocation information. It will get resolved when
+        // the section addresses are assigned.
+        Relocations[SectionID].push_back(RelocationEntry(TargetID,
+                                                         Offset,
+                                                         RE->Word1,
+                                                         0 /*Addend*/));
+      } else {
+        StringRef SourceName = SymbolNames[SourceNum];
+
+        // Now store the relocation information. Associate it with the source
+        // symbol. Just add it to the unresolved list and let the general
+        // path post-load resolve it if we know where the symbol is.
+        UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
+                                                                    Offset,
+                                                                    RE->Word1,
+                                                                 0 /*Addend*/));
+        DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
+              << " from '" << SourceName << "(Word1: "
+              << format("0x%x", RE->Word1) << ")\n");
+      }
     }
   }
+
+  // Resolve the addresses of any symbols that were defined in this segment.
+  for (int i = 0, e = SymbolNames.size(); i != e; ++i)
+    resolveSymbol(SymbolNames[i]);
+
   return false;
 }
 
@@ -280,77 +260,56 @@
   if (!Segment64LC)
     return Error("unable to load segment load command");
 
+
+  SmallVector<unsigned, 16> SectionMap;
   for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
     InMemoryStruct<macho::Section64> Sect;
     Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
     if (!Sect)
       return Error("unable to load section: '" + Twine(SectNum) + "'");
 
-    // FIXME: For the time being, we're only loading text segments.
+    // Allocate memory via the MM for the section.
+    uint8_t *Buffer;
+    uint32_t SectionID = Sections.size();
     if (Sect->Flags != 0x80000400)
-      continue;
+      Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
+    else
+      Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
+
+    DEBUG(dbgs() << "Loading "
+                 << ((Sect->Flags == 0x80000400) ? "text" : "data")
+                 << " (ID #" << SectionID << ")"
+                 << " '" << Sect->SegmentName << ","
+                 << Sect->Name << "' of size " << Sect->Size
+                 << " to address " << Buffer << ".\n");
 
-    // Address and names of symbols in the section.
-    typedef std::pair<uint64_t, StringRef> SymbolEntry;
-    SmallVector<SymbolEntry, 64> Symbols;
-    // Index of all the names, in this section or not. Used when we're
-    // dealing with relocation entries.
-    SmallVector<StringRef, 64> SymbolNames;
-    for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
-      InMemoryStruct<macho::Symbol64TableEntry> STE;
-      Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
-      if (!STE)
-        return Error("unable to read symbol: '" + Twine(i) + "'");
-      if (STE->SectionIndex > Segment64LC->NumSections)
-        return Error("invalid section index for symbol: '" + Twine(i) + "'");
-      // Get the symbol name.
-      StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
-      SymbolNames.push_back(Name);
-
-      // Just skip symbols not defined in this section.
-      if ((unsigned)STE->SectionIndex - 1 != SectNum)
-        continue;
-
-      // FIXME: Check the symbol type and flags.
-      if (STE->Type != 0xF)  // external, defined in this section.
-        continue;
-      // Flags in the upper nibble we don't care about.
-      if ((STE->Flags & 0xf) != 0x0)
-        continue;
+    // Copy the payload from the object file into the allocated buffer.
+    uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
+                                           Segment64LC->FileSize).data();
+    memcpy(Buffer, Base + Sect->Address, Sect->Size);
 
-      // Remember the symbol.
-      Symbols.push_back(SymbolEntry(STE->Value, Name));
+    // Remember what got allocated for this SectionID.
+    Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
 
-      DEBUG(dbgs() << "Function sym: '" << Name << "' @ " <<
-            (Sect->Address + STE->Value) << "\n");
-    }
-    // Sort the symbols by address, just in case they didn't come in that way.
-    array_pod_sort(Symbols.begin(), Symbols.end());
+    // By default, the load address of a section is its memory buffer.
+    SectionLoadAddress.push_back((uint64_t)Buffer);
 
-    // If there weren't any functions (odd, but just in case...)
-    if (!Symbols.size())
-      continue;
+    // Keep a map of object file section numbers to corresponding SectionIDs
+    // while processing the file.
+    SectionMap.push_back(SectionID);
+  }
 
-    // Extract the function data.
-    uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
-                                           Segment64LC->FileSize).data();
-    for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) {
-      uint64_t StartOffset = Sect->Address + Symbols[i].first;
-      uint64_t EndOffset = Symbols[i + 1].first - 1;
-      DEBUG(dbgs() << "Extracting function: " << Symbols[i].second
-                   << " from [" << StartOffset << ", " << EndOffset << "]\n");
-      extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset);
-    }
-    // The last symbol we do after since the end address is calculated
-    // differently because there is no next symbol to reference.
-    uint64_t StartOffset = Symbols[Symbols.size() - 1].first;
-    uint64_t EndOffset = Sect->Size - 1;
-    DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second
-                 << " from [" << StartOffset << ", " << EndOffset << "]\n");
-    extractFunction(Symbols[Symbols.size()-1].second,
-                    Base + StartOffset, Base + EndOffset);
+  // Process the symbol table.
+  SmallVector<StringRef, 64> SymbolNames;
+  processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC);
 
-    // Now extract the relocation information for each function and process it.
+  // Process the relocations for each section we're loading.
+  Relocations.grow(Relocations.size() + Segment64LC->NumSections);
+  for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
+    InMemoryStruct<macho::Section64> Sect;
+    Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
+    if (!Sect)
+      return Error("unable to load section: '" + Twine(SectNum) + "'");
     for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
       InMemoryStruct<macho::RelocationEntry> RE;
       Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
@@ -360,48 +319,142 @@
       // relocation should be applied. We need to translate that into an
       // offset into a function since that's our atom.
       uint32_t Offset = RE->Word0;
-      // Look for the function containing the address. This is used for JIT
-      // code, so the number of functions in section is almost always going
-      // to be very small (usually just one), so until we have use cases
-      // where that's not true, just use a trivial linear search.
-      unsigned SymbolNum;
-      unsigned NumSymbols = Symbols.size();
-      assert(NumSymbols > 0 && Symbols[0].first <= Offset &&
-             "No symbol containing relocation!");
-      for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum)
-        if (Symbols[SymbolNum + 1].first > Offset)
-          break;
-      // Adjust the offset to be relative to the symbol.
-      Offset -= Symbols[SymbolNum].first;
-      // Get the name of the symbol containing the relocation.
-      StringRef TargetName = SymbolNames[SymbolNum];
-
       bool isExtern = (RE->Word1 >> 27) & 1;
+
+      // FIXME: Get the relocation addend from the target address.
+      // FIXME: VERY imporant for internal relocations.
+
       // Figure out the source symbol of the relocation. If isExtern is true,
       // this relocation references the symbol table, otherwise it references
       // a section in the same object, numbered from 1 through NumSections
       // (SectionBases is [0, NumSections-1]).
-      if (!isExtern)
-        return Error("Internal relocations not supported.");
       uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
-      StringRef SourceName = SymbolNames[SourceNum];
+      if (!isExtern) {
+        assert(SourceNum > 0 && "Invalid relocation section number!");
+        unsigned SectionID = SectionMap[SourceNum - 1];
+        unsigned TargetID = SectionMap[SectNum];
+        DEBUG(dbgs() << "Internal relocation at Section #"
+                     << TargetID << " + " << Offset
+                     << " from Section #"
+                     << SectionID << " (Word1: "
+                     << format("0x%x", RE->Word1) << ")\n");
+
+        // Store the relocation information. It will get resolved when
+        // the section addresses are assigned.
+        Relocations[SectionID].push_back(RelocationEntry(TargetID,
+                                                         Offset,
+                                                         RE->Word1,
+                                                         0 /*Addend*/));
+      } else {
+        StringRef SourceName = SymbolNames[SourceNum];
+
+        // Now store the relocation information. Associate it with the source
+        // symbol. Just add it to the unresolved list and let the general
+        // path post-load resolve it if we know where the symbol is.
+        UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
+                                                                    Offset,
+                                                                    RE->Word1,
+                                                                 0 /*Addend*/));
+        DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
+              << " from '" << SourceName << "(Word1: "
+              << format("0x%x", RE->Word1) << ")\n");
+      }
+    }
+  }
 
-      // FIXME: Get the relocation addend from the target address.
+  // Resolve the addresses of any symbols that were defined in this segment.
+  for (int i = 0, e = SymbolNames.size(); i != e; ++i)
+    resolveSymbol(SymbolNames[i]);
 
-      // Now store the relocation information. Associate it with the source
-      // symbol.
-      Relocations[SourceName].push_back(RelocationEntry(TargetName,
-                                                        Offset,
-                                                        RE->Word1,
-                                                        0 /*Addend*/));
-      DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset
-                   << " from '" << SourceName << "(Word1: "
-                   << format("0x%x", RE->Word1) << ")\n");
-    }
+  return false;
+}
+
+bool RuntimeDyldMachO::
+processSymbols32(const MachOObject *Obj,
+                 SmallVectorImpl<unsigned> &SectionMap,
+                 SmallVectorImpl<StringRef> &SymbolNames,
+                 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
+  // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such.
+  for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
+    InMemoryStruct<macho::SymbolTableEntry> STE;
+    Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
+    if (!STE)
+      return Error("unable to read symbol: '" + Twine(i) + "'");
+    // Get the symbol name.
+    StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
+    SymbolNames.push_back(Name);
+
+    // FIXME: Check the symbol type and flags.
+    if (STE->Type != 0xF)  // external, defined in this segment.
+      continue;
+    // Flags in the upper nibble we don't care about.
+    if ((STE->Flags & 0xf) != 0x0)
+      continue;
+
+    // Remember the symbol.
+    uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
+    SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
+
+    DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
+                 << (getSectionAddress(SectionID) + STE->Value)
+                 << "\n");
   }
   return false;
 }
 
+bool RuntimeDyldMachO::
+processSymbols64(const MachOObject *Obj,
+                 SmallVectorImpl<unsigned> &SectionMap,
+                 SmallVectorImpl<StringRef> &SymbolNames,
+                 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
+  for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
+    InMemoryStruct<macho::Symbol64TableEntry> STE;
+    Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
+    if (!STE)
+      return Error("unable to read symbol: '" + Twine(i) + "'");
+    // Get the symbol name.
+    StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
+    SymbolNames.push_back(Name);
+
+    // FIXME: Check the symbol type and flags.
+    if (STE->Type != 0xF)  // external, defined in this segment.
+      continue;
+    // Flags in the upper nibble we don't care about.
+    if ((STE->Flags & 0xf) != 0x0)
+      continue;
+
+    // Remember the symbol.
+    uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
+    SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
+
+    DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
+                 << (getSectionAddress(SectionID) + STE->Value)
+                 << "\n");
+  }
+  return false;
+}
+
+// resolveSymbol - Resolve any relocations to the specified symbol if
+// we know where it lives.
+void RuntimeDyldMachO::resolveSymbol(StringRef Name) {
+  StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name);
+  if (Loc == SymbolTable.end())
+    return;
+
+  RelocationList &Relocs = UnresolvedRelocations[Name];
+  DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n");
+  for (int i = 0, e = Relocs.size(); i != e; ++i) {
+    // Change the relocation to be section relative rather than symbol
+    // relative and move it to the resolved relocation list.
+    RelocationEntry Entry = Relocs[i];
+    Entry.Addend += Loc->second.second;
+    Relocations[Loc->second.first].push_back(Entry);
+  }
+  // FIXME: Keep a worklist of the relocations we've added so that we can
+  // resolve more selectively later.
+  Relocs.clear();
+}
+
 bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
   // If the linker is in an error state, don't do anything.
   if (hasError())
@@ -486,31 +539,46 @@
       return true;
   }
 
+  // Assign the addresses of the sections from the object so that any
+  // relocations to them get set properly.
+  // FIXME: This is done directly from the client at the moment. We should
+  // default the values to the local storage, at least when the target arch
+  // is the same as the host arch.
+
   return false;
 }
 
 // Assign an address to a symbol name and resolve all the relocations
 // associated with it.
-void RuntimeDyldMachO::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
-  // Assign the address in our symbol table.
-  SymbolTable[Name] = Addr;
+void RuntimeDyldMachO::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.
+
+  SectionLoadAddress[SectionID] = Addr;
 
-  RelocationList &Relocs = Relocations[Name];
+  RelocationList &Relocs = Relocations[SectionID];
   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
     RelocationEntry &RE = Relocs[i];
-    uint8_t *Target = SymbolTable[RE.Target] + RE.Offset;
+    uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset;
     bool isPCRel = (RE.Data >> 24) & 1;
     unsigned Type = (RE.Data >> 28) & 0xf;
     unsigned Size = 1 << ((RE.Data >> 25) & 3);
 
-    DEBUG(dbgs() << "Resolving relocation at '" << RE.Target
-          << "' + " << RE.Offset << " (" << format("%p", Target) << ")"
-          << " from '" << Name << " (" << format("%p", Addr) << ")"
+    DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID
+          << " + " << RE.Offset << " (" << format("%p", Target) << ")"
+          << " from Section #" << SectionID << " (" << format("%p", Addr) << ")"
           << "(" << (isPCRel ? "pcrel" : "absolute")
-          << ", type: " << Type << ", Size: " << Size << ").\n");
+          << ", type: " << Type << ", Size: " << Size << ", Addend: "
+          << RE.Addend << ").\n");
 
-    resolveRelocation(Target, Addr, isPCRel, Type, Size);
-    RE.isResolved = true;
+    resolveRelocation(Target, Addr, isPCRel, Type, Size, RE.Addend);
   }
 }
 

Modified: llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp (original)
+++ llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp Mon Jan 16 16:26:39 2012
@@ -51,12 +51,30 @@
 class TrivialMemoryManager : public RTDyldMemoryManager {
 public:
   SmallVector<sys::MemoryBlock, 16> FunctionMemory;
+  SmallVector<sys::MemoryBlock, 16> DataMemory;
+
+  uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+                               unsigned SectionID);
+  uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+                               unsigned SectionID);
 
   uint8_t *startFunctionBody(const char *Name, uintptr_t &Size);
   void endFunctionBody(const char *Name, uint8_t *FunctionStart,
                        uint8_t *FunctionEnd);
 };
 
+uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
+                                                   unsigned Alignment,
+                                                   unsigned SectionID) {
+  return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
+}
+
+uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
+                                                   unsigned Alignment,
+                                                   unsigned SectionID) {
+  return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
+}
+
 uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name,
                                                  uintptr_t &Size) {
   return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();

Modified: llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp?rev=148258&r1=148257&r2=148258&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp Mon Jan 16 16:26:39 2012
@@ -113,6 +113,14 @@
       EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
     Base->endFunctionBody(F, FunctionStart, FunctionEnd);
   }
+  virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+                                       unsigned SectionID) {
+    return Base->allocateDataSection(Size, Alignment, SectionID);
+  }
+  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+                                       unsigned SectionID) {
+    return Base->allocateCodeSection(Size, Alignment, SectionID);
+  }
   virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
     return Base->allocateSpace(Size, Alignment);
   }





More information about the llvm-commits mailing list