Fix various issues (or do cleanups) found by enabling certain MSVC warnings.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyldMachO.cpp
index 81b2d17f885c97946c753d357d33c346f7fe840a..75e4f71ed8cee2094204a69a7f28328eb5db2d22 100644 (file)
@@ -1,4 +1,4 @@
-//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
+//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -15,7 +15,7 @@
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/STLExtras.h"
-#include "RuntimeDyldImpl.h"
+#include "RuntimeDyldMachO.h"
 using namespace llvm;
 using namespace llvm::object;
 
@@ -26,7 +26,7 @@ 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!");
+  default: llvm_unreachable("Unsupported CPU type!");
   case mach::CTM_x86_64:
     return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value,
                                    isPCRel, Type, Size, Addend);
@@ -50,8 +50,13 @@ resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
   switch(Type) {
   default:
     llvm_unreachable("Invalid relocation type!");
+  case macho::RIT_X86_64_Signed1:
+  case macho::RIT_X86_64_Signed2:
+  case macho::RIT_X86_64_Signed4:
+  case macho::RIT_X86_64_Signed:
   case macho::RIT_X86_64_Unsigned:
   case macho::RIT_X86_64_Branch: {
+    Value += Addend;
     // Mask in the target value a byte at a time (we don't have an alignment
     // guarantee for the target address, so this is safest).
     uint8_t *p = (uint8_t*)Address;
@@ -61,17 +66,12 @@ resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
     }
     return false;
   }
-  case macho::RIT_X86_64_Signed:
   case macho::RIT_X86_64_GOTLoad:
   case macho::RIT_X86_64_GOT:
   case macho::RIT_X86_64_Subtractor:
-  case macho::RIT_X86_64_Signed1:
-  case macho::RIT_X86_64_Signed2:
-  case macho::RIT_X86_64_Signed4:
   case macho::RIT_X86_64_TLV:
     return Error("Relocation type not implemented yet!");
   }
-  return false;
 }
 
 bool RuntimeDyldMachO::
@@ -91,7 +91,6 @@ resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
   default:
     llvm_unreachable("Invalid relocation type!");
   case macho::RIT_Vanilla: {
-    llvm_unreachable("Invalid relocation type!");
     // Mask in the target value a byte at a time (we don't have an alignment
     // guarantee for the target address, so this is safest).
     uint8_t *p = (uint8_t*)Address;
@@ -171,6 +170,7 @@ loadSegment32(const MachOObject *Obj,
 
     // Remember what got allocated for this SectionID.
     Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
+    SectionLocalMemToID[Buffer] = SectionID;
 
     // By default, the load address of a section is its memory buffer.
     SectionLoadAddress.push_back((uint64_t)Buffer);
@@ -271,16 +271,18 @@ loadSegment64(const MachOObject *Obj,
     // Allocate memory via the MM for the section.
     uint8_t *Buffer;
     uint32_t SectionID = Sections.size();
-    if (Sect->Flags != 0x80000400)
-      Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
+    unsigned Align = 1 << Sect->Align; // .o file has log2 alignment.
+    if (Sect->Flags == 0x80000400)
+      Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID);
     else
-      Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
+      Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID);
 
     DEBUG(dbgs() << "Loading "
                  << ((Sect->Flags == 0x80000400) ? "text" : "data")
                  << " (ID #" << SectionID << ")"
                  << " '" << Sect->SegmentName << ","
                  << Sect->Name << "' of size " << Sect->Size
+                 << " (align " << Align << ")"
                  << " to address " << Buffer << ".\n");
 
     // Copy the payload from the object file into the allocated buffer.
@@ -290,6 +292,7 @@ loadSegment64(const MachOObject *Obj,
 
     // Remember what got allocated for this SectionID.
     Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
+    SectionLocalMemToID[Buffer] = SectionID;
 
     // By default, the load address of a section is its memory buffer.
     SectionLoadAddress.push_back((uint64_t)Buffer);