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(MachOSection &MOS, MachineRelocation &MR,
37 // Constants for the relocation r_type field.
38 // see <mach-o/ppc/reloc.h>
39 enum { PPC_RELOC_VANILLA, // generic relocation
40 PPC_RELOC_PAIR, // the second relocation entry of a pair
41 PPC_RELOC_BR14, // 14 bit branch displacement to word address
42 PPC_RELOC_BR24, // 24 bit branch displacement to word address
43 PPC_RELOC_HI16, // a PAIR follows with the low 16 bits
44 PPC_RELOC_LO16, // a PAIR follows with the high 16 bits
45 PPC_RELOC_HA16, // a PAIR follows, which is sign extended to 32b
46 PPC_RELOC_LO14 // LO16 with low 2 bits implicitly zero
51 /// addPPCMachOObjectWriterPass - Returns a pass that outputs the generated code
52 /// as a Mach-O object file.
54 void llvm::addPPCMachOObjectWriterPass(FunctionPassManager &FPM,
55 std::ostream &O, PPCTargetMachine &TM) {
56 PPCMachOWriter *EW = new PPCMachOWriter(O, TM);
58 FPM.add(createPPCCodeEmitterPass(TM, EW->getMachineCodeEmitter()));
61 /// GetTargetRelocation - For the MachineRelocation MR, convert it to one or
62 /// more PowerPC MachORelocation(s), add the new relocations to the
63 /// MachOSection, and rewrite the instruction at the section offset if required
64 /// by that relocation type.
65 void PPCMachOWriter::GetTargetRelocation(MachOSection &MOS,
66 MachineRelocation &MR,
68 // Keep track of whether or not this is an externally defined relocation.
69 uint32_t index = MOS.Index;
70 bool isExtern = false;
72 // Get the address of the instruction to rewrite
73 unsigned char *RelocPos = &MOS.SectionData[0] + MR.getMachineCodeOffset();
75 // Get the address of whatever it is we're relocating, if possible.
76 if (MR.isGlobalValue()) {
77 // determine whether or not its external and then figure out what section
78 // we put it in if it's a locally defined symbol.
79 } else if (MR.isString()) {
80 // lookup in global values?
82 assert((MR.isConstantPoolIndex() || MR.isJumpTableIndex()) &&
83 "Unhandled MachineRelocation type!");
86 switch ((PPC::RelocationType)MR.getRelocationType()) {
87 default: assert(0 && "Unknown PPC relocation type!");
88 case PPC::reloc_pcrel_bx:
89 case PPC::reloc_pcrel_bcx:
90 case PPC::reloc_absolute_low_ix:
91 assert(0 && "Unhandled PPC relocation type!");
93 case PPC::reloc_absolute_high:
95 MachORelocation HA16(MR.getMachineCodeOffset(), index, false, 2, isExtern,
97 MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
99 outword(RelocBuffer, HA16.r_address);
100 outword(RelocBuffer, HA16.getPackedFields());
101 outword(RelocBuffer, PAIR.r_address);
102 outword(RelocBuffer, PAIR.getPackedFields());
106 *(unsigned *)RelocPos &= 0xFFFF0000;
107 *(unsigned *)RelocPos |= ((Addr >> 16) & 0xFFFF);
109 case PPC::reloc_absolute_low:
111 MachORelocation LO16(MR.getMachineCodeOffset(), index, false, 2, isExtern,
113 MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
115 outword(RelocBuffer, LO16.r_address);
116 outword(RelocBuffer, LO16.getPackedFields());
117 outword(RelocBuffer, PAIR.r_address);
118 outword(RelocBuffer, PAIR.getPackedFields());
121 *(unsigned *)RelocPos &= 0xFFFF0000;
122 *(unsigned *)RelocPos |= (Addr & 0xFFFF);