Bill Wendling | 841056a | 2007-01-24 03:36:05 +0000 | [diff] [blame] | 1 | //===-- PPCMachOWriterInfo.cpp - Mach-O Writer Info for the PowerPC -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Bill Wendling | 2f5bcb5 | 2007-02-08 06:05:08 +0000 | [diff] [blame] | 5 | // This file was developed by Bill Wendling and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Bill Wendling | 841056a | 2007-01-24 03:36:05 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Mach-O writer information for the PowerPC backend. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PPCMachOWriterInfo.h" |
Bill Wendling | 736610c | 2007-02-03 02:41:58 +0000 | [diff] [blame] | 15 | #include "PPCRelocations.h" |
Bill Wendling | 841056a | 2007-01-24 03:36:05 +0000 | [diff] [blame] | 16 | #include "PPCTargetMachine.h" |
Bill Wendling | 736610c | 2007-02-03 02:41:58 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachORelocation.h" |
| 18 | #include "llvm/Support/OutputBuffer.h" |
Bill Wendling | 841056a | 2007-01-24 03:36:05 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | PPCMachOWriterInfo::PPCMachOWriterInfo(const PPCTargetMachine &TM) |
| 22 | : TargetMachOWriterInfo(TM.getTargetData()->getPointerSizeInBits() == 64 ? |
| 23 | HDR_CPU_TYPE_POWERPC64 : |
| 24 | HDR_CPU_TYPE_POWERPC, |
| 25 | HDR_CPU_SUBTYPE_POWERPC_ALL) {} |
Bill Wendling | 736610c | 2007-02-03 02:41:58 +0000 | [diff] [blame] | 26 | PPCMachOWriterInfo::~PPCMachOWriterInfo() {} |
| 27 | |
Bill Wendling | 736610c | 2007-02-03 02:41:58 +0000 | [diff] [blame] | 28 | /// GetTargetRelocation - For the MachineRelocation MR, convert it to one or |
| 29 | /// more PowerPC MachORelocation(s), add the new relocations to the |
| 30 | /// MachOSection, and rewrite the instruction at the section offset if required |
| 31 | /// by that relocation type. |
| 32 | unsigned PPCMachOWriterInfo::GetTargetRelocation(MachineRelocation &MR, |
| 33 | unsigned FromIdx, |
| 34 | unsigned ToAddr, |
| 35 | unsigned ToIdx, |
| 36 | OutputBuffer &RelocOut, |
| 37 | OutputBuffer &SecOut, |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame^] | 38 | bool Scattered, |
| 39 | bool isExtern) const { |
Bill Wendling | 736610c | 2007-02-03 02:41:58 +0000 | [diff] [blame] | 40 | unsigned NumRelocs = 0; |
| 41 | uint64_t Addr = 0; |
| 42 | |
Bill Wendling | 736610c | 2007-02-03 02:41:58 +0000 | [diff] [blame] | 43 | // Get the address of whatever it is we're relocating, if possible. |
| 44 | if (!isExtern) |
| 45 | Addr = (uintptr_t)MR.getResultPointer() + ToAddr; |
| 46 | |
| 47 | switch ((PPC::RelocationType)MR.getRelocationType()) { |
| 48 | default: assert(0 && "Unknown PPC relocation type!"); |
| 49 | case PPC::reloc_absolute_low_ix: |
| 50 | assert(0 && "Unhandled PPC relocation type!"); |
| 51 | break; |
| 52 | case PPC::reloc_vanilla: |
| 53 | { |
| 54 | // FIXME: need to handle 64 bit vanilla relocs |
| 55 | MachORelocation VANILLA(MR.getMachineCodeOffset(), ToIdx, |
| 56 | false, 2, isExtern, |
| 57 | PPC_RELOC_VANILLA, |
| 58 | Scattered, (intptr_t)MR.getResultPointer()); |
| 59 | ++NumRelocs; |
| 60 | |
| 61 | if (Scattered) { |
| 62 | RelocOut.outword(VANILLA.getPackedFields()); |
| 63 | RelocOut.outword(VANILLA.getAddress()); |
| 64 | } else { |
| 65 | RelocOut.outword(VANILLA.getAddress()); |
| 66 | RelocOut.outword(VANILLA.getPackedFields()); |
| 67 | } |
| 68 | |
| 69 | intptr_t SymbolOffset; |
| 70 | |
| 71 | if (Scattered) |
| 72 | SymbolOffset = Addr + MR.getConstantVal(); |
| 73 | else |
| 74 | SymbolOffset = Addr; |
| 75 | |
| 76 | printf("vanilla fixup: sec_%x[%x] = %x\n", FromIdx, |
| 77 | unsigned(MR.getMachineCodeOffset()), |
| 78 | unsigned(SymbolOffset)); |
| 79 | SecOut.fixword(SymbolOffset, MR.getMachineCodeOffset()); |
| 80 | } |
| 81 | break; |
| 82 | case PPC::reloc_pcrel_bx: |
| 83 | { |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame^] | 84 | // FIXME: Presumably someday we will need to branch to other, non-extern |
| 85 | // functions too. Need to figure out some way to distinguish between |
| 86 | // target is BB and target is function. |
| 87 | if (isExtern) { |
| 88 | MachORelocation BR24(MR.getMachineCodeOffset(), ToIdx, true, 2, |
| 89 | isExtern, PPC_RELOC_BR24, Scattered, |
| 90 | (intptr_t)MR.getMachineCodeOffset()); |
| 91 | RelocOut.outword(BR24.getAddress()); |
| 92 | RelocOut.outword(BR24.getPackedFields()); |
| 93 | ++NumRelocs; |
| 94 | } |
| 95 | |
Bill Wendling | 736610c | 2007-02-03 02:41:58 +0000 | [diff] [blame] | 96 | Addr -= MR.getMachineCodeOffset(); |
| 97 | Addr >>= 2; |
| 98 | Addr &= 0xFFFFFF; |
| 99 | Addr <<= 2; |
| 100 | Addr |= (SecOut[MR.getMachineCodeOffset()] << 24); |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame^] | 101 | Addr |= (SecOut[MR.getMachineCodeOffset()+3] & 0x3); |
Bill Wendling | 736610c | 2007-02-03 02:41:58 +0000 | [diff] [blame] | 102 | SecOut.fixword(Addr, MR.getMachineCodeOffset()); |
| 103 | break; |
| 104 | } |
| 105 | case PPC::reloc_pcrel_bcx: |
| 106 | { |
| 107 | Addr -= MR.getMachineCodeOffset(); |
| 108 | Addr &= 0xFFFC; |
| 109 | |
| 110 | SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2); |
| 111 | break; |
| 112 | } |
| 113 | case PPC::reloc_absolute_high: |
| 114 | { |
| 115 | MachORelocation HA16(MR.getMachineCodeOffset(), ToIdx, false, 2, |
| 116 | isExtern, PPC_RELOC_HA16); |
| 117 | MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern, |
| 118 | PPC_RELOC_PAIR); |
| 119 | NumRelocs = 2; |
| 120 | |
| 121 | RelocOut.outword(HA16.getRawAddress()); |
| 122 | RelocOut.outword(HA16.getPackedFields()); |
| 123 | RelocOut.outword(PAIR.getRawAddress()); |
| 124 | RelocOut.outword(PAIR.getPackedFields()); |
| 125 | |
| 126 | Addr += 0x8000; |
| 127 | |
| 128 | SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2); |
| 129 | break; |
| 130 | } |
| 131 | case PPC::reloc_absolute_low: |
| 132 | { |
| 133 | MachORelocation LO16(MR.getMachineCodeOffset(), ToIdx, false, 2, |
| 134 | isExtern, PPC_RELOC_LO16); |
| 135 | MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern, |
| 136 | PPC_RELOC_PAIR); |
| 137 | NumRelocs = 2; |
| 138 | |
| 139 | RelocOut.outword(LO16.getRawAddress()); |
| 140 | RelocOut.outword(LO16.getPackedFields()); |
| 141 | RelocOut.outword(PAIR.getRawAddress()); |
| 142 | RelocOut.outword(PAIR.getPackedFields()); |
| 143 | |
| 144 | SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2); |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return NumRelocs; |
| 150 | } |