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"
23 class VISIBILITY_HIDDEN PPCMachOWriter : public MachOWriter {
25 PPCMachOWriter(std::ostream &O, PPCTargetMachine &TM) : MachOWriter(O, TM) {
26 if (TM.getTargetData()->getPointerSizeInBits() == 64) {
27 Header.cputype = MachOHeader::CPU_TYPE_POWERPC64;
29 Header.cputype = MachOHeader::CPU_TYPE_POWERPC;
31 Header.cpusubtype = MachOHeader::CPU_SUBTYPE_POWERPC_ALL;
34 virtual void GetTargetRelocation(MachineRelocation &MR, MachOSection &MOS,
36 virtual MachineRelocation GetJTRelocation(unsigned Offset,
37 MachineBasicBlock *MBB);
39 virtual const char *getPassName() const {
40 return "PowerPC Mach-O Writer";
43 // Constants for the relocation r_type field.
44 // see <mach-o/ppc/reloc.h>
45 enum { PPC_RELOC_VANILLA, // generic relocation
46 PPC_RELOC_PAIR, // the second relocation entry of a pair
47 PPC_RELOC_BR14, // 14 bit branch displacement to word address
48 PPC_RELOC_BR24, // 24 bit branch displacement to word address
49 PPC_RELOC_HI16, // a PAIR follows with the low 16 bits
50 PPC_RELOC_LO16, // a PAIR follows with the high 16 bits
51 PPC_RELOC_HA16, // a PAIR follows, which is sign extended to 32b
52 PPC_RELOC_LO14 // LO16 with low 2 bits implicitly zero
57 /// addPPCMachOObjectWriterPass - Returns a pass that outputs the generated code
58 /// as a Mach-O object file.
60 void llvm::addPPCMachOObjectWriterPass(FunctionPassManager &FPM,
61 std::ostream &O, PPCTargetMachine &TM) {
62 PPCMachOWriter *MOW = new PPCMachOWriter(O, TM);
64 FPM.add(createPPCCodeEmitterPass(TM, MOW->getMachineCodeEmitter()));
67 /// GetTargetRelocation - For the MachineRelocation MR, convert it to one or
68 /// more PowerPC MachORelocation(s), add the new relocations to the
69 /// MachOSection, and rewrite the instruction at the section offset if required
70 /// by that relocation type.
71 void PPCMachOWriter::GetTargetRelocation(MachineRelocation &MR,
76 // Keep track of whether or not this is an externally defined relocation.
77 bool isExtern = false;
79 // Get the address of whatever it is we're relocating, if possible.
81 Addr = (uintptr_t)MR.getResultPointer();
83 switch ((PPC::RelocationType)MR.getRelocationType()) {
84 default: assert(0 && "Unknown PPC relocation type!");
85 case PPC::reloc_absolute_low_ix:
86 assert(0 && "Unhandled PPC relocation type!");
88 case PPC::reloc_vanilla:
90 // FIXME: need to handle 64 bit vanilla relocs
91 MachORelocation VANILLA(MR.getMachineCodeOffset(), ToIndex, false, 2,
92 isExtern, PPC_RELOC_VANILLA);
93 outword(MOS.RelocBuffer, VANILLA.r_address);
94 outword(MOS.RelocBuffer, VANILLA.getPackedFields());
97 fixword(MOS.SectionData, Addr, MR.getMachineCodeOffset());
99 case PPC::reloc_pcrel_bx:
100 Addr -= MR.getMachineCodeOffset();
104 Addr |= (MOS.SectionData[MR.getMachineCodeOffset()] << 24);
105 fixword(MOS.SectionData, Addr, MR.getMachineCodeOffset());
107 case PPC::reloc_pcrel_bcx:
108 Addr -= MR.getMachineCodeOffset();
110 fixhalf(MOS.SectionData, Addr, MR.getMachineCodeOffset() + 2);
112 case PPC::reloc_absolute_high:
114 MachORelocation HA16(MR.getMachineCodeOffset(), ToIndex, false, 2,
115 isExtern, PPC_RELOC_HA16);
116 MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
118 outword(MOS.RelocBuffer, HA16.r_address);
119 outword(MOS.RelocBuffer, HA16.getPackedFields());
120 outword(MOS.RelocBuffer, PAIR.r_address);
121 outword(MOS.RelocBuffer, PAIR.getPackedFields());
123 printf("ha16: %x\n", (unsigned)Addr);
126 fixhalf(MOS.SectionData, Addr >> 16, MR.getMachineCodeOffset() + 2);
128 case PPC::reloc_absolute_low:
130 MachORelocation LO16(MR.getMachineCodeOffset(), ToIndex, false, 2,
131 isExtern, PPC_RELOC_LO16);
132 MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
134 outword(MOS.RelocBuffer, LO16.r_address);
135 outword(MOS.RelocBuffer, LO16.getPackedFields());
136 outword(MOS.RelocBuffer, PAIR.r_address);
137 outword(MOS.RelocBuffer, PAIR.getPackedFields());
139 printf("lo16: %x\n", (unsigned)Addr);
141 fixhalf(MOS.SectionData, Addr, MR.getMachineCodeOffset() + 2);
146 MachineRelocation PPCMachOWriter::GetJTRelocation(unsigned Offset,
147 MachineBasicBlock *MBB) {
148 // FIXME: do something about PIC
149 return MachineRelocation::getBB(Offset, PPC::reloc_vanilla, MBB);