First pass at supporting relocations.  Relocations are written correctly to
the file now, however the relocated address is currently wrong.  Fixing
that will require some deep pondering.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30207 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/PowerPC/PPCJITInfo.cpp b/lib/Target/PowerPC/PPCJITInfo.cpp
index a3ca3c5..7c1d2a4 100644
--- a/lib/Target/PowerPC/PPCJITInfo.cpp
+++ b/lib/Target/PowerPC/PPCJITInfo.cpp
@@ -312,15 +312,12 @@
              "Relocation out of range!");
       *RelocPos |= (ResultPtr & ((1 << 14)-1))  << 2;
       break;
-    case PPC::reloc_absolute_ptr_high: // Pointer relocations.
-    case PPC::reloc_absolute_ptr_low:
     case PPC::reloc_absolute_high:     // high bits of ref -> low 16 of instr
     case PPC::reloc_absolute_low: {    // low bits of ref  -> low 16 of instr
       ResultPtr += MR->getConstantVal();
 
       // If this is a high-part access, get the high-part.
-      if (MR->getRelocationType() == PPC::reloc_absolute_high ||
-          MR->getRelocationType() == PPC::reloc_absolute_ptr_high) {
+      if (MR->getRelocationType() == PPC::reloc_absolute_high) {
         // If the low part will have a carry (really a borrow) from the low
         // 16-bits into the high 16, add a bit to borrow from.
         if (((int)ResultPtr << 16) < 0)
diff --git a/lib/Target/PowerPC/PPCMachOWriter.cpp b/lib/Target/PowerPC/PPCMachOWriter.cpp
index a7da371..f82ee34 100644
--- a/lib/Target/PowerPC/PPCMachOWriter.cpp
+++ b/lib/Target/PowerPC/PPCMachOWriter.cpp
@@ -12,6 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "PPCRelocations.h"
 #include "PPCTargetMachine.h"
 #include "llvm/PassManager.h"
 #include "llvm/CodeGen/MachOWriter.h"
@@ -22,11 +23,28 @@
   class VISIBILITY_HIDDEN PPCMachOWriter : public MachOWriter {
   public:
     PPCMachOWriter(std::ostream &O, PPCTargetMachine &TM) : MachOWriter(O, TM) {
-      // FIMXE: choose ppc64 when appropriate
-      Header.cputype = MachOHeader::CPU_TYPE_POWERPC;
+      if (TM.getTargetData()->getPointerSizeInBits() == 64) {
+        Header.cputype = MachOHeader::CPU_TYPE_POWERPC64;
+      } else {
+        Header.cputype = MachOHeader::CPU_TYPE_POWERPC;
+      }
       Header.cpusubtype = MachOHeader::CPU_SUBTYPE_POWERPC_ALL;
     }
 
+    virtual void GetTargetRelocation(MachOSection &MOS, MachineRelocation &MR,
+                                     uint64_t Addr);
+    
+    // Constants for the relocation r_type field.
+    // see <mach-o/ppc/reloc.h>
+    enum { PPC_RELOC_VANILLA, // generic relocation
+           PPC_RELOC_PAIR,    // the second relocation entry of a pair
+           PPC_RELOC_BR14,    // 14 bit branch displacement to word address
+           PPC_RELOC_BR24,    // 24 bit branch displacement to word address
+           PPC_RELOC_HI16,    // a PAIR follows with the low 16 bits
+           PPC_RELOC_LO16,    // a PAIR follows with the high 16 bits
+           PPC_RELOC_HA16,    // a PAIR follows, which is sign extended to 32b
+           PPC_RELOC_LO14     // LO16 with low 2 bits implicitly zero
+    };
   };
 }
 
@@ -39,3 +57,69 @@
   FPM.add(EW);
   FPM.add(createPPCCodeEmitterPass(TM, EW->getMachineCodeEmitter()));
 }
+
+/// GetTargetRelocation - For the MachineRelocation MR, convert it to one or
+/// more PowerPC MachORelocation(s), add the new relocations to the
+/// MachOSection, and rewrite the instruction at the section offset if required 
+/// by that relocation type.
+void PPCMachOWriter::GetTargetRelocation(MachOSection &MOS,
+                                         MachineRelocation &MR,
+                                         uint64_t Addr) {
+  // Keep track of whether or not this is an externally defined relocation.
+  uint32_t index = MOS.Index;
+  bool     isExtern = false;
+  
+  // Get the address of the instruction to rewrite
+  unsigned char *RelocPos = &MOS.SectionData[0] + MR.getMachineCodeOffset();
+  
+  // Get the address of whatever it is we're relocating, if possible.
+  if (MR.isGlobalValue()) {
+    // determine whether or not its external and then figure out what section
+    // we put it in if it's a locally defined symbol.
+  } else if (MR.isString()) {
+    // lookup in global values?
+  } else {
+    assert((MR.isConstantPoolIndex() || MR.isJumpTableIndex()) &&
+           "Unhandled MachineRelocation type!");
+  }
+  
+  switch ((PPC::RelocationType)MR.getRelocationType()) {
+  default: assert(0 && "Unknown PPC relocation type!");
+  case PPC::reloc_pcrel_bx:
+  case PPC::reloc_pcrel_bcx:
+  case PPC::reloc_absolute_low_ix:
+    assert(0 && "Unhandled PPC relocation type!");
+    break;
+  case PPC::reloc_absolute_high:
+    {
+      MachORelocation HA16(MR.getMachineCodeOffset(), index, false, 2, isExtern, 
+                           PPC_RELOC_HA16);
+      MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
+                           PPC_RELOC_PAIR);
+      outword(RelocBuffer, HA16.r_address);
+      outword(RelocBuffer, HA16.getPackedFields());
+      outword(RelocBuffer, PAIR.r_address);
+      outword(RelocBuffer, PAIR.getPackedFields());
+    }
+    MOS.nreloc += 2;
+    Addr += 0x8000;
+    *(unsigned *)RelocPos &= 0xFFFF0000;
+    *(unsigned *)RelocPos |= ((Addr >> 16) & 0xFFFF);
+    break;
+  case PPC::reloc_absolute_low:
+    {
+      MachORelocation LO16(MR.getMachineCodeOffset(), index, false, 2, isExtern, 
+                           PPC_RELOC_LO16);
+      MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
+                           PPC_RELOC_PAIR);
+      outword(RelocBuffer, LO16.r_address);
+      outword(RelocBuffer, LO16.getPackedFields());
+      outword(RelocBuffer, PAIR.r_address);
+      outword(RelocBuffer, PAIR.getPackedFields());
+    }
+    MOS.nreloc += 2;
+    *(unsigned *)RelocPos &= 0xFFFF0000;
+    *(unsigned *)RelocPos |= (Addr & 0xFFFF);
+    break;
+  }
+}
diff --git a/lib/Target/PowerPC/PPCRelocations.h b/lib/Target/PowerPC/PPCRelocations.h
index 8cb2859..21c688d 100644
--- a/lib/Target/PowerPC/PPCRelocations.h
+++ b/lib/Target/PowerPC/PPCRelocations.h
@@ -44,21 +44,7 @@
       
       // reloc_absolute_low_ix - Absolute relocation for the 64-bit load/store
       // instruction which have two implicit zero bits.
-      reloc_absolute_low_ix,
-
-      // reloc_absolute_ptr_high - Absolute relocation for references to lazy
-      // pointer stubs.  In this case, the relocated instruction should be
-      // relocated to point to a POINTER to the indicated global.  The low-16
-      // bits of the instruction are rewritten with the high 16-bits of the
-      // address of the pointer.
-      reloc_absolute_ptr_high,
-
-      // reloc_absolute_ptr_low - Absolute relocation for references to lazy
-      // pointer stubs.  In this case, the relocated instruction should be
-      // relocated to point to a POINTER to the indicated global.  The low-16
-      // bits of the instruction are rewritten with the low 16-bits of the
-      // address of the pointer.
-      reloc_absolute_ptr_low
+      reloc_absolute_low_ix
     };
   }
 }