[MCJIT] Replace memcpy with readBytesUnaligned in RuntimeDyldMachOI386.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / Targets / RuntimeDyldMachOI386.h
index a743ec69717185590143381c804074a2dc970a41..258b8476d42e2e1cd4d048fc299f4b03a6836f7a 100644 (file)
@@ -44,7 +44,7 @@ public:
           RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)
         return processSECTDIFFRelocation(SectionID, RelI, ObjImg,
                                          ObjSectionToID);
-      else if (Arch == Triple::x86 && RelType == MachO::GENERIC_RELOC_VANILLA)
+      else if (RelType == MachO::GENERIC_RELOC_VANILLA)
         return processI386ScatteredVANILLA(SectionID, RelI, ObjImg,
                                            ObjSectionToID);
       llvm_unreachable("Unhandled scattered relocation.");
@@ -119,8 +119,9 @@ public:
       populateJumpTable(cast<MachOObjectFile>(*ObjImg.getObjectFile()), Section,
                         SectionID);
     else if (Name == "__pointers")
-      populatePointersSection(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
-                              Section, SectionID);
+      populateIndirectSymbolPointersSection(
+                                 cast<MachOObjectFile>(*ObjImg.getObjectFile()),
+                                 Section, SectionID);
   }
 
 private:
@@ -141,8 +142,7 @@ private:
     RelI->getOffset(Offset);
     uint8_t *LocalAddress = Section.Address + Offset;
     unsigned NumBytes = 1 << Size;
-    int64_t Addend = 0;
-    memcpy(&Addend, LocalAddress, NumBytes);
+    uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
 
     ++RelI;
     MachO::any_relocation_info RE2 =
@@ -151,20 +151,17 @@ private:
     uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
     section_iterator SAI = getSectionByAddress(*MachO, AddrA);
     assert(SAI != MachO->section_end() && "Can't find section for address A");
-    uint64_t SectionABase;
-    SAI->getAddress(SectionABase);
+    uint64_t SectionABase = SAI->getAddress();
     uint64_t SectionAOffset = AddrA - SectionABase;
     SectionRef SectionA = *SAI;
-    bool IsCode;
-    SectionA.isText(IsCode);
+    bool IsCode = SectionA.isText();
     uint32_t SectionAID =
         findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
 
     uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
     section_iterator SBI = getSectionByAddress(*MachO, AddrB);
     assert(SBI != MachO->section_end() && "Can't find section for address B");
-    uint64_t SectionBBase;
-    SBI->getAddress(SectionBBase);
+    uint64_t SectionBBase = SBI->getAddress();
     uint64_t SectionBOffset = AddrB - SectionBBase;
     SectionRef SectionB = *SBI;
     uint32_t SectionBID =
@@ -204,17 +201,14 @@ private:
     RelI->getOffset(Offset);
     uint8_t *LocalAddress = Section.Address + Offset;
     unsigned NumBytes = 1 << Size;
-    int64_t Addend = 0;
-    memcpy(&Addend, LocalAddress, NumBytes);
+    int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
 
     unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE);
     section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);
     assert(TargetSI != MachO->section_end() && "Can't find section for symbol");
-    uint64_t SectionBaseAddr;
-    TargetSI->getAddress(SectionBaseAddr);
+    uint64_t SectionBaseAddr = TargetSI->getAddress();
     SectionRef TargetSection = *TargetSI;
-    bool IsCode;
-    TargetSection.isText(IsCode);
+    bool IsCode = TargetSection.isText();
     uint32_t TargetSectionID =
         findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID);
 
@@ -259,58 +253,6 @@ private:
     }
   }
 
-  // Populate __pointers section.
-  void populatePointersSection(MachOObjectFile &Obj,
-                               const SectionRef &PTSection,
-                               unsigned PTSectionID) {
-    assert(!Obj.is64Bit() &&
-           "__pointers section not supported in 64-bit MachO.");
-
-    MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
-    MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
-    uint32_t PTSectionSize = Sec32.size;
-    unsigned FirstIndirectSymbol = Sec32.reserved1;
-    const unsigned PTEntrySize = 4;
-    unsigned NumPTEntries = PTSectionSize / PTEntrySize;
-    unsigned PTEntryOffset = 0;
-
-    assert((PTSectionSize % PTEntrySize) == 0 &&
-           "Pointers section does not contain a whole number of stubs?");
-
-    DEBUG(dbgs() << "Populating __pointers, Section ID " << PTSectionID << ", "
-                 << NumPTEntries << " entries, " << PTEntrySize
-                 << " bytes each:\n");
-
-    for (unsigned i = 0; i < NumPTEntries; ++i) {
-      unsigned SymbolIndex =
-          Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
-      symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
-      StringRef IndirectSymbolName;
-      SI->getName(IndirectSymbolName);
-      DEBUG(dbgs() << "  " << IndirectSymbolName << ": index " << SymbolIndex
-                   << ", PT offset: " << PTEntryOffset << "\n");
-      RelocationEntry RE(PTSectionID, PTEntryOffset,
-                         MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
-      addRelocationForSymbol(RE, IndirectSymbolName);
-      PTEntryOffset += PTEntrySize;
-    }
-  }
-
-  static section_iterator getSectionByAddress(const MachOObjectFile &Obj,
-                                              uint64_t Addr) {
-    section_iterator SI = Obj.section_begin();
-    section_iterator SE = Obj.section_end();
-
-    for (; SI != SE; ++SI) {
-      uint64_t SAddr, SSize;
-      SI->getAddress(SAddr);
-      SI->getSize(SSize);
-      if ((Addr >= SAddr) && (Addr < SAddr + SSize))
-        return SI;
-    }
-
-    return SE;
-  }
 };
 }