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() {}
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,
36 OutputBuffer &RelocOut,
38 bool Scattered) const {
39 unsigned NumRelocs = 0;
42 // Keep track of whether or not this is an externally defined relocation.
43 bool isExtern = false;
45 // Get the address of whatever it is we're relocating, if possible.
47 Addr = (uintptr_t)MR.getResultPointer() + ToAddr;
49 switch ((PPC::RelocationType)MR.getRelocationType()) {
50 default: assert(0 && "Unknown PPC relocation type!");
51 case PPC::reloc_absolute_low_ix:
52 assert(0 && "Unhandled PPC relocation type!");
54 case PPC::reloc_vanilla:
56 // FIXME: need to handle 64 bit vanilla relocs
57 MachORelocation VANILLA(MR.getMachineCodeOffset(), ToIdx,
60 Scattered, (intptr_t)MR.getResultPointer());
64 RelocOut.outword(VANILLA.getPackedFields());
65 RelocOut.outword(VANILLA.getAddress());
67 RelocOut.outword(VANILLA.getAddress());
68 RelocOut.outword(VANILLA.getPackedFields());
71 intptr_t SymbolOffset;
74 SymbolOffset = Addr + MR.getConstantVal();
78 printf("vanilla fixup: sec_%x[%x] = %x\n", FromIdx,
79 unsigned(MR.getMachineCodeOffset()),
80 unsigned(SymbolOffset));
81 SecOut.fixword(SymbolOffset, MR.getMachineCodeOffset());
84 case PPC::reloc_pcrel_bx:
86 Addr -= MR.getMachineCodeOffset();
90 Addr |= (SecOut[MR.getMachineCodeOffset()] << 24);
92 SecOut.fixword(Addr, MR.getMachineCodeOffset());
95 case PPC::reloc_pcrel_bcx:
97 Addr -= MR.getMachineCodeOffset();
100 SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
103 case PPC::reloc_absolute_high:
105 MachORelocation HA16(MR.getMachineCodeOffset(), ToIdx, false, 2,
106 isExtern, PPC_RELOC_HA16);
107 MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
111 RelocOut.outword(HA16.getRawAddress());
112 RelocOut.outword(HA16.getPackedFields());
113 RelocOut.outword(PAIR.getRawAddress());
114 RelocOut.outword(PAIR.getPackedFields());
118 SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2);
121 case PPC::reloc_absolute_low:
123 MachORelocation LO16(MR.getMachineCodeOffset(), ToIdx, false, 2,
124 isExtern, PPC_RELOC_LO16);
125 MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
129 RelocOut.outword(LO16.getRawAddress());
130 RelocOut.outword(LO16.getPackedFields());
131 RelocOut.outword(PAIR.getRawAddress());
132 RelocOut.outword(PAIR.getPackedFields());
134 SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);