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