LLVM API Documentation

PPCMachOWriterInfo.cpp

Go to the documentation of this file.
00001 //===-- PPCMachOWriterInfo.cpp - Mach-O Writer Info for the PowerPC -------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements Mach-O writer information for the PowerPC backend.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "PPCMachOWriterInfo.h"
00015 #include "PPCRelocations.h"
00016 #include "PPCTargetMachine.h"
00017 #include "llvm/CodeGen/MachORelocation.h"
00018 #include "llvm/Support/OutputBuffer.h"
00019 #include "llvm/Support/ErrorHandling.h"
00020 #include <cstdio>
00021 using namespace llvm;
00022 
00023 PPCMachOWriterInfo::PPCMachOWriterInfo(const PPCTargetMachine &TM)
00024   : TargetMachOWriterInfo(TM.getTargetData()->getPointerSizeInBits() == 64 ?
00025                           HDR_CPU_TYPE_POWERPC64 :
00026                           HDR_CPU_TYPE_POWERPC,
00027                           HDR_CPU_SUBTYPE_POWERPC_ALL) {}
00028 PPCMachOWriterInfo::~PPCMachOWriterInfo() {}
00029 
00030 /// GetTargetRelocation - For the MachineRelocation MR, convert it to one or
00031 /// more PowerPC MachORelocation(s), add the new relocations to the
00032 /// MachOSection, and rewrite the instruction at the section offset if required
00033 /// by that relocation type.
00034 unsigned PPCMachOWriterInfo::GetTargetRelocation(MachineRelocation &MR,
00035                                                  unsigned FromIdx,
00036                                                  unsigned ToAddr,
00037                                                  unsigned ToIdx,
00038                                                  OutputBuffer &RelocOut,
00039                                                  OutputBuffer &SecOut,
00040                                                  bool Scattered,
00041                                                  bool isExtern) const {
00042   unsigned NumRelocs = 0;
00043   uint64_t Addr = 0;
00044 
00045   // Get the address of whatever it is we're relocating, if possible.
00046   if (!isExtern)
00047     Addr = (uintptr_t)MR.getResultPointer() + ToAddr;
00048 
00049   switch ((PPC::RelocationType)MR.getRelocationType()) {
00050   default: llvm_unreachable("Unknown PPC relocation type!");
00051   case PPC::reloc_absolute_low_ix:
00052     llvm_unreachable("Unhandled PPC relocation type!");
00053     break;
00054   case PPC::reloc_vanilla:
00055     {
00056       // FIXME: need to handle 64 bit vanilla relocs
00057       MachORelocation VANILLA(MR.getMachineCodeOffset(), ToIdx,
00058                               false, 2, isExtern,
00059                               PPC_RELOC_VANILLA,
00060                               Scattered, (intptr_t)MR.getResultPointer());
00061       ++NumRelocs;
00062 
00063       if (Scattered) {
00064         RelocOut.outword(VANILLA.getPackedFields());
00065         RelocOut.outword(VANILLA.getAddress());
00066       } else {
00067         RelocOut.outword(VANILLA.getAddress());
00068         RelocOut.outword(VANILLA.getPackedFields());
00069       }
00070       
00071       intptr_t SymbolOffset;
00072 
00073       if (Scattered)
00074         SymbolOffset = Addr + MR.getConstantVal();
00075       else
00076         SymbolOffset = Addr;
00077 
00078       printf("vanilla fixup: sec_%x[%x] = %x\n", FromIdx,
00079              unsigned(MR.getMachineCodeOffset()),
00080              unsigned(SymbolOffset));
00081       SecOut.fixword(SymbolOffset, MR.getMachineCodeOffset());
00082     }
00083     break;
00084   case PPC::reloc_pcrel_bx:
00085     {
00086       // FIXME: Presumably someday we will need to branch to other, non-extern
00087       // functions too.  Need to figure out some way to distinguish between
00088       // target is BB and target is function.
00089       if (isExtern) {
00090         MachORelocation BR24(MR.getMachineCodeOffset(), ToIdx, true, 2, 
00091                              isExtern, PPC_RELOC_BR24, Scattered, 
00092                              (intptr_t)MR.getMachineCodeOffset());
00093         RelocOut.outword(BR24.getAddress());
00094         RelocOut.outword(BR24.getPackedFields());
00095         ++NumRelocs;
00096       }
00097 
00098       Addr -= MR.getMachineCodeOffset();
00099       Addr >>= 2;
00100       Addr &= 0xFFFFFF;
00101       Addr <<= 2;
00102       Addr |= (SecOut[MR.getMachineCodeOffset()] << 24);
00103       Addr |= (SecOut[MR.getMachineCodeOffset()+3] & 0x3);
00104       SecOut.fixword(Addr, MR.getMachineCodeOffset());
00105       break;
00106     }
00107   case PPC::reloc_pcrel_bcx:
00108     {
00109       Addr -= MR.getMachineCodeOffset();
00110       Addr &= 0xFFFC;
00111 
00112       SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
00113       break;
00114     }
00115   case PPC::reloc_absolute_high:
00116     {
00117       MachORelocation HA16(MR.getMachineCodeOffset(), ToIdx, false, 2,
00118                            isExtern, PPC_RELOC_HA16);
00119       MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
00120                            PPC_RELOC_PAIR);
00121       NumRelocs = 2;
00122 
00123       RelocOut.outword(HA16.getRawAddress());
00124       RelocOut.outword(HA16.getPackedFields());
00125       RelocOut.outword(PAIR.getRawAddress());
00126       RelocOut.outword(PAIR.getPackedFields());
00127 
00128       Addr += 0x8000;
00129 
00130       SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2);
00131       break;
00132     }
00133   case PPC::reloc_absolute_low:
00134     {
00135       MachORelocation LO16(MR.getMachineCodeOffset(), ToIdx, false, 2,
00136                            isExtern, PPC_RELOC_LO16);
00137       MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
00138                            PPC_RELOC_PAIR);
00139       NumRelocs = 2;
00140 
00141       RelocOut.outword(LO16.getRawAddress());
00142       RelocOut.outword(LO16.getPackedFields());
00143       RelocOut.outword(PAIR.getRawAddress());
00144       RelocOut.outword(PAIR.getPackedFields());
00145 
00146       SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
00147       break;
00148     }
00149   }
00150 
00151   return NumRelocs;
00152 }