1 //===-- PPCMachOWriterInfo.cpp - Mach-O Writer Info for the PowerPC -------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements Mach-O writer information for the PowerPC backend.
12 //===----------------------------------------------------------------------===//
14 #include "PPCMachOWriterInfo.h"
15 #include "PPCRelocations.h"
16 #include "PPCTargetMachine.h"
17 #include "llvm/CodeGen/MachORelocation.h"
18 #include "llvm/Support/OutputBuffer.h"
21 PPCMachOWriterInfo::PPCMachOWriterInfo(const PPCTargetMachine &TM)
22 : TargetMachOWriterInfo(TM.getTargetData()->getPointerSizeInBits() == 64 ?
23 HDR_CPU_TYPE_POWERPC64 :
25 HDR_CPU_SUBTYPE_POWERPC_ALL) {}
26 PPCMachOWriterInfo::~PPCMachOWriterInfo() {}
29 /// GetTargetRelocation - For the MachineRelocation MR, convert it to one or
30 /// more PowerPC MachORelocation(s), add the new relocations to the
31 /// MachOSection, and rewrite the instruction at the section offset if required
32 /// by that relocation type.
33 unsigned PPCMachOWriterInfo::GetTargetRelocation(MachineRelocation &MR,
37 OutputBuffer &RelocOut,
39 bool Scattered) const {
40 unsigned NumRelocs = 0;
43 // Keep track of whether or not this is an externally defined relocation.
44 bool isExtern = false;
46 // Get the address of whatever it is we're relocating, if possible.
48 Addr = (uintptr_t)MR.getResultPointer() + ToAddr;
50 switch ((PPC::RelocationType)MR.getRelocationType()) {
51 default: assert(0 && "Unknown PPC relocation type!");
52 case PPC::reloc_absolute_low_ix:
53 assert(0 && "Unhandled PPC relocation type!");
55 case PPC::reloc_vanilla:
57 // FIXME: need to handle 64 bit vanilla relocs
58 MachORelocation VANILLA(MR.getMachineCodeOffset(), ToIdx,
61 Scattered, (intptr_t)MR.getResultPointer());
65 RelocOut.outword(VANILLA.getPackedFields());
66 RelocOut.outword(VANILLA.getAddress());
68 RelocOut.outword(VANILLA.getAddress());
69 RelocOut.outword(VANILLA.getPackedFields());
72 intptr_t SymbolOffset;
75 SymbolOffset = Addr + MR.getConstantVal();
79 printf("vanilla fixup: sec_%x[%x] = %x\n", FromIdx,
80 unsigned(MR.getMachineCodeOffset()),
81 unsigned(SymbolOffset));
82 SecOut.fixword(SymbolOffset, MR.getMachineCodeOffset());
85 case PPC::reloc_pcrel_bx:
87 Addr -= MR.getMachineCodeOffset();
91 Addr |= (SecOut[MR.getMachineCodeOffset()] << 24);
93 SecOut.fixword(Addr, MR.getMachineCodeOffset());
96 case PPC::reloc_pcrel_bcx:
98 Addr -= MR.getMachineCodeOffset();
101 SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
104 case PPC::reloc_absolute_high:
106 MachORelocation HA16(MR.getMachineCodeOffset(), ToIdx, false, 2,
107 isExtern, PPC_RELOC_HA16);
108 MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
112 RelocOut.outword(HA16.getRawAddress());
113 RelocOut.outword(HA16.getPackedFields());
114 RelocOut.outword(PAIR.getRawAddress());
115 RelocOut.outword(PAIR.getPackedFields());
119 SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2);
122 case PPC::reloc_absolute_low:
124 MachORelocation LO16(MR.getMachineCodeOffset(), ToIdx, false, 2,
125 isExtern, PPC_RELOC_LO16);
126 MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
130 RelocOut.outword(LO16.getRawAddress());
131 RelocOut.outword(LO16.getPackedFields());
132 RelocOut.outword(PAIR.getRawAddress());
133 RelocOut.outword(PAIR.getPackedFields());
135 SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);