1 //===-- PPCMachOWriter.cpp - Emit a Mach-O file for the PowerPC backend ---===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Nate Begeman and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a Mach-O writer for the PowerPC backend. The public
11 // interface to this file is the createPPCMachOObjectWriterPass function.
13 //===----------------------------------------------------------------------===//
15 #include "PPCRelocations.h"
16 #include "PPCTargetMachine.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/CodeGen/MachOWriter.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/OutputBuffer.h"
24 class VISIBILITY_HIDDEN PPCMachOWriter : public MachOWriter {
26 PPCMachOWriter(std::ostream &O, PPCTargetMachine &TM)
27 : MachOWriter(O, TM) {}
29 virtual void GetTargetRelocation(MachineRelocation &MR, MachOSection &From,
32 // Constants for the relocation r_type field.
33 // see <mach-o/ppc/reloc.h>
34 enum { PPC_RELOC_VANILLA, // generic relocation
35 PPC_RELOC_PAIR, // the second relocation entry of a pair
36 PPC_RELOC_BR14, // 14 bit branch displacement to word address
37 PPC_RELOC_BR24, // 24 bit branch displacement to word address
38 PPC_RELOC_HI16, // a PAIR follows with the low 16 bits
39 PPC_RELOC_LO16, // a PAIR follows with the high 16 bits
40 PPC_RELOC_HA16, // a PAIR follows, which is sign extended to 32b
41 PPC_RELOC_LO14 // LO16 with low 2 bits implicitly zero
46 /// addPPCMachOObjectWriterPass - Returns a pass that outputs the generated code
47 /// as a Mach-O object file.
49 void llvm::addPPCMachOObjectWriterPass(FunctionPassManager &FPM,
50 std::ostream &O, PPCTargetMachine &TM) {
51 PPCMachOWriter *MOW = new PPCMachOWriter(O, TM);
53 FPM.add(createPPCCodeEmitterPass(TM, MOW->getMachineCodeEmitter()));
56 /// GetTargetRelocation - For the MachineRelocation MR, convert it to one or
57 /// more PowerPC MachORelocation(s), add the new relocations to the
58 /// MachOSection, and rewrite the instruction at the section offset if required
59 /// by that relocation type.
60 void PPCMachOWriter::GetTargetRelocation(MachineRelocation &MR,
65 // Keep track of whether or not this is an externally defined relocation.
66 bool isExtern = false;
68 // Get the address of whatever it is we're relocating, if possible.
70 Addr = (uintptr_t)MR.getResultPointer() + To.addr;
72 switch ((PPC::RelocationType)MR.getRelocationType()) {
73 default: assert(0 && "Unknown PPC relocation type!");
74 case PPC::reloc_absolute_low_ix:
75 assert(0 && "Unhandled PPC relocation type!");
77 case PPC::reloc_vanilla:
79 // FIXME: need to handle 64 bit vanilla relocs
80 MachORelocation VANILLA(MR.getMachineCodeOffset(), To.Index, false, 2,
81 isExtern, PPC_RELOC_VANILLA);
84 OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
85 RelocOut.outword(VANILLA.r_address);
86 RelocOut.outword(VANILLA.getPackedFields());
88 OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
89 SecOut.fixword(Addr, MR.getMachineCodeOffset());
92 case PPC::reloc_pcrel_bx:
94 Addr -= MR.getMachineCodeOffset();
98 Addr |= (From.SectionData[MR.getMachineCodeOffset()] << 24);
100 OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
101 SecOut.fixword(Addr, MR.getMachineCodeOffset());
104 case PPC::reloc_pcrel_bcx:
106 Addr -= MR.getMachineCodeOffset();
109 OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
110 SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
113 case PPC::reloc_absolute_high:
115 MachORelocation HA16(MR.getMachineCodeOffset(), To.Index, false, 2,
116 isExtern, PPC_RELOC_HA16);
117 MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
122 OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
123 RelocOut.outword(HA16.r_address);
124 RelocOut.outword(HA16.getPackedFields());
125 RelocOut.outword(PAIR.r_address);
126 RelocOut.outword(PAIR.getPackedFields());
127 printf("ha16: %x\n", (unsigned)Addr);
130 OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
131 SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2);
134 case PPC::reloc_absolute_low:
136 MachORelocation LO16(MR.getMachineCodeOffset(), To.Index, false, 2,
137 isExtern, PPC_RELOC_LO16);
138 MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
143 OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
144 RelocOut.outword(LO16.r_address);
145 RelocOut.outword(LO16.getPackedFields());
146 RelocOut.outword(PAIR.r_address);
147 RelocOut.outword(PAIR.getPackedFields());
148 printf("lo16: %x\n", (unsigned)Addr);
150 OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
151 SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);