blob: 3bfa6d71910575d6709fbcb490abdf2b2b5eaa31 [file] [log] [blame]
Bill Wendling841056a2007-01-24 03:36:05 +00001//===-- PPCMachOWriterInfo.cpp - Mach-O Writer Info for the PowerPC -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling841056a2007-01-24 03:36:05 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Mach-O writer information for the PowerPC backend.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPCMachOWriterInfo.h"
Bill Wendling736610c2007-02-03 02:41:58 +000015#include "PPCRelocations.h"
Bill Wendling841056a2007-01-24 03:36:05 +000016#include "PPCTargetMachine.h"
Bill Wendling736610c2007-02-03 02:41:58 +000017#include "llvm/CodeGen/MachORelocation.h"
18#include "llvm/Support/OutputBuffer.h"
Duncan Sands4520dd22008-10-08 07:23:46 +000019#include <cstdio>
Bill Wendling841056a2007-01-24 03:36:05 +000020using namespace llvm;
21
22PPCMachOWriterInfo::PPCMachOWriterInfo(const PPCTargetMachine &TM)
23 : TargetMachOWriterInfo(TM.getTargetData()->getPointerSizeInBits() == 64 ?
24 HDR_CPU_TYPE_POWERPC64 :
25 HDR_CPU_TYPE_POWERPC,
26 HDR_CPU_SUBTYPE_POWERPC_ALL) {}
Bill Wendling736610c2007-02-03 02:41:58 +000027PPCMachOWriterInfo::~PPCMachOWriterInfo() {}
28
Bill Wendling736610c2007-02-03 02:41:58 +000029/// 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.
33unsigned PPCMachOWriterInfo::GetTargetRelocation(MachineRelocation &MR,
34 unsigned FromIdx,
35 unsigned ToAddr,
36 unsigned ToIdx,
37 OutputBuffer &RelocOut,
38 OutputBuffer &SecOut,
Nate Begemanfec910c2007-02-28 07:40:50 +000039 bool Scattered,
40 bool isExtern) const {
Bill Wendling736610c2007-02-03 02:41:58 +000041 unsigned NumRelocs = 0;
42 uint64_t Addr = 0;
43
Bill Wendling736610c2007-02-03 02:41:58 +000044 // Get the address of whatever it is we're relocating, if possible.
45 if (!isExtern)
46 Addr = (uintptr_t)MR.getResultPointer() + ToAddr;
47
48 switch ((PPC::RelocationType)MR.getRelocationType()) {
49 default: assert(0 && "Unknown PPC relocation type!");
50 case PPC::reloc_absolute_low_ix:
51 assert(0 && "Unhandled PPC relocation type!");
52 break;
53 case PPC::reloc_vanilla:
54 {
55 // FIXME: need to handle 64 bit vanilla relocs
56 MachORelocation VANILLA(MR.getMachineCodeOffset(), ToIdx,
57 false, 2, isExtern,
58 PPC_RELOC_VANILLA,
59 Scattered, (intptr_t)MR.getResultPointer());
60 ++NumRelocs;
61
62 if (Scattered) {
63 RelocOut.outword(VANILLA.getPackedFields());
64 RelocOut.outword(VANILLA.getAddress());
65 } else {
66 RelocOut.outword(VANILLA.getAddress());
67 RelocOut.outword(VANILLA.getPackedFields());
68 }
69
70 intptr_t SymbolOffset;
71
72 if (Scattered)
73 SymbolOffset = Addr + MR.getConstantVal();
74 else
75 SymbolOffset = Addr;
76
77 printf("vanilla fixup: sec_%x[%x] = %x\n", FromIdx,
78 unsigned(MR.getMachineCodeOffset()),
79 unsigned(SymbolOffset));
80 SecOut.fixword(SymbolOffset, MR.getMachineCodeOffset());
81 }
82 break;
83 case PPC::reloc_pcrel_bx:
84 {
Nate Begemanfec910c2007-02-28 07:40:50 +000085 // FIXME: Presumably someday we will need to branch to other, non-extern
86 // functions too. Need to figure out some way to distinguish between
87 // target is BB and target is function.
88 if (isExtern) {
89 MachORelocation BR24(MR.getMachineCodeOffset(), ToIdx, true, 2,
90 isExtern, PPC_RELOC_BR24, Scattered,
91 (intptr_t)MR.getMachineCodeOffset());
92 RelocOut.outword(BR24.getAddress());
93 RelocOut.outword(BR24.getPackedFields());
94 ++NumRelocs;
95 }
96
Bill Wendling736610c2007-02-03 02:41:58 +000097 Addr -= MR.getMachineCodeOffset();
98 Addr >>= 2;
99 Addr &= 0xFFFFFF;
100 Addr <<= 2;
101 Addr |= (SecOut[MR.getMachineCodeOffset()] << 24);
Nate Begemanfec910c2007-02-28 07:40:50 +0000102 Addr |= (SecOut[MR.getMachineCodeOffset()+3] & 0x3);
Bill Wendling736610c2007-02-03 02:41:58 +0000103 SecOut.fixword(Addr, MR.getMachineCodeOffset());
104 break;
105 }
106 case PPC::reloc_pcrel_bcx:
107 {
108 Addr -= MR.getMachineCodeOffset();
109 Addr &= 0xFFFC;
110
111 SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
112 break;
113 }
114 case PPC::reloc_absolute_high:
115 {
116 MachORelocation HA16(MR.getMachineCodeOffset(), ToIdx, false, 2,
117 isExtern, PPC_RELOC_HA16);
118 MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
119 PPC_RELOC_PAIR);
120 NumRelocs = 2;
121
122 RelocOut.outword(HA16.getRawAddress());
123 RelocOut.outword(HA16.getPackedFields());
124 RelocOut.outword(PAIR.getRawAddress());
125 RelocOut.outword(PAIR.getPackedFields());
126
127 Addr += 0x8000;
128
129 SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2);
130 break;
131 }
132 case PPC::reloc_absolute_low:
133 {
134 MachORelocation LO16(MR.getMachineCodeOffset(), ToIdx, false, 2,
135 isExtern, PPC_RELOC_LO16);
136 MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
137 PPC_RELOC_PAIR);
138 NumRelocs = 2;
139
140 RelocOut.outword(LO16.getRawAddress());
141 RelocOut.outword(LO16.getPackedFields());
142 RelocOut.outword(PAIR.getRawAddress());
143 RelocOut.outword(PAIR.getPackedFields());
144
145 SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
146 break;
147 }
148 }
149
150 return NumRelocs;
151}