David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 1 | //===-- PPCMachObjectWriter.cpp - PPC Mach-O Writer -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "MCTargetDesc/PPCMCTargetDesc.h" |
| 11 | #include "MCTargetDesc/PPCFixupKinds.h" |
| 12 | #include "llvm/ADT/Twine.h" |
| 13 | #include "llvm/MC/MCAsmLayout.h" |
| 14 | #include "llvm/MC/MCAssembler.h" |
| 15 | #include "llvm/MC/MCContext.h" |
| 16 | #include "llvm/MC/MCMachObjectWriter.h" |
| 17 | #include "llvm/MC/MCSectionMachO.h" |
| 18 | #include "llvm/MC/MCValue.h" |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/Format.h" |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MachO.h" |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | class PPCMachObjectWriter : public MCMachObjectTargetWriter { |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 27 | bool recordScatteredRelocation(MachObjectWriter *Writer, |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 28 | const MCAssembler &Asm, |
| 29 | const MCAsmLayout &Layout, |
| 30 | const MCFragment *Fragment, |
| 31 | const MCFixup &Fixup, MCValue Target, |
| 32 | unsigned Log2Size, uint64_t &FixedValue); |
| 33 | |
| 34 | void RecordPPCRelocation(MachObjectWriter *Writer, const MCAssembler &Asm, |
| 35 | const MCAsmLayout &Layout, |
| 36 | const MCFragment *Fragment, const MCFixup &Fixup, |
| 37 | MCValue Target, uint64_t &FixedValue); |
| 38 | |
| 39 | public: |
| 40 | PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype) |
Jim Grosbach | 7c76b4c | 2015-06-04 20:27:42 +0000 | [diff] [blame] | 41 | : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {} |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 42 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 43 | void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm, |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 44 | const MCAsmLayout &Layout, const MCFragment *Fragment, |
| 45 | const MCFixup &Fixup, MCValue Target, |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 46 | uint64_t &FixedValue) override { |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 47 | if (Writer->is64Bit()) { |
| 48 | report_fatal_error("Relocation emission for MachO/PPC64 unimplemented."); |
| 49 | } else |
| 50 | RecordPPCRelocation(Writer, Asm, Layout, Fragment, Fixup, Target, |
| 51 | FixedValue); |
| 52 | } |
| 53 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 54 | } |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 55 | |
| 56 | /// computes the log2 of the size of the relocation, |
| 57 | /// used for relocation_info::r_length. |
| 58 | static unsigned getFixupKindLog2Size(unsigned Kind) { |
| 59 | switch (Kind) { |
| 60 | default: |
| 61 | report_fatal_error("log2size(FixupKind): Unhandled fixup kind!"); |
| 62 | case FK_PCRel_1: |
| 63 | case FK_Data_1: |
| 64 | return 0; |
| 65 | case FK_PCRel_2: |
| 66 | case FK_Data_2: |
| 67 | return 1; |
| 68 | case FK_PCRel_4: |
| 69 | case PPC::fixup_ppc_brcond14: |
| 70 | case PPC::fixup_ppc_half16: |
| 71 | case PPC::fixup_ppc_br24: |
| 72 | case FK_Data_4: |
| 73 | return 2; |
| 74 | case FK_PCRel_8: |
| 75 | case FK_Data_8: |
| 76 | return 3; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /// Translates generic PPC fixup kind to Mach-O/PPC relocation type enum. |
JF Bastien | 664fd46 | 2016-01-13 23:36:00 +0000 | [diff] [blame^] | 82 | /// Outline based on PPCELFObjectWriter::getRelocType(). |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 83 | static unsigned getRelocType(const MCValue &Target, |
| 84 | const MCFixupKind FixupKind, // from |
| 85 | // Fixup.getKind() |
| 86 | const bool IsPCRel) { |
| 87 | const MCSymbolRefExpr::VariantKind Modifier = |
| 88 | Target.isAbsolute() ? MCSymbolRefExpr::VK_None |
| 89 | : Target.getSymA()->getKind(); |
| 90 | // determine the type of the relocation |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 91 | unsigned Type = MachO::GENERIC_RELOC_VANILLA; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 92 | if (IsPCRel) { // relative to PC |
| 93 | switch ((unsigned)FixupKind) { |
| 94 | default: |
| 95 | report_fatal_error("Unimplemented fixup kind (relative)"); |
| 96 | case PPC::fixup_ppc_br24: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 97 | Type = MachO::PPC_RELOC_BR24; // R_PPC_REL24 |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 98 | break; |
| 99 | case PPC::fixup_ppc_brcond14: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 100 | Type = MachO::PPC_RELOC_BR14; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 101 | break; |
| 102 | case PPC::fixup_ppc_half16: |
| 103 | switch (Modifier) { |
| 104 | default: |
| 105 | llvm_unreachable("Unsupported modifier for half16 fixup"); |
| 106 | case MCSymbolRefExpr::VK_PPC_HA: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 107 | Type = MachO::PPC_RELOC_HA16; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 108 | break; |
| 109 | case MCSymbolRefExpr::VK_PPC_LO: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 110 | Type = MachO::PPC_RELOC_LO16; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 111 | break; |
| 112 | case MCSymbolRefExpr::VK_PPC_HI: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 113 | Type = MachO::PPC_RELOC_HI16; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | } else { |
| 119 | switch ((unsigned)FixupKind) { |
| 120 | default: |
| 121 | report_fatal_error("Unimplemented fixup kind (absolute)!"); |
| 122 | case PPC::fixup_ppc_half16: |
| 123 | switch (Modifier) { |
| 124 | default: |
| 125 | llvm_unreachable("Unsupported modifier for half16 fixup"); |
| 126 | case MCSymbolRefExpr::VK_PPC_HA: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 127 | Type = MachO::PPC_RELOC_HA16_SECTDIFF; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 128 | break; |
| 129 | case MCSymbolRefExpr::VK_PPC_LO: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 130 | Type = MachO::PPC_RELOC_LO16_SECTDIFF; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 131 | break; |
| 132 | case MCSymbolRefExpr::VK_PPC_HI: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 133 | Type = MachO::PPC_RELOC_HI16_SECTDIFF; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | break; |
| 137 | case FK_Data_4: |
| 138 | break; |
| 139 | case FK_Data_2: |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | return Type; |
| 144 | } |
| 145 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 146 | static void makeRelocationInfo(MachO::any_relocation_info &MRE, |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 147 | const uint32_t FixupOffset, const uint32_t Index, |
| 148 | const unsigned IsPCRel, const unsigned Log2Size, |
| 149 | const unsigned IsExtern, const unsigned Type) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 150 | MRE.r_word0 = FixupOffset; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 151 | // The bitfield offsets that work (as determined by trial-and-error) |
| 152 | // are different than what is documented in the mach-o manuals. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 153 | // This appears to be an endianness issue; reversing the order of the |
| 154 | // documented bitfields in <llvm/Support/MachO.h> fixes this (but |
| 155 | // breaks x86/ARM assembly). |
| 156 | MRE.r_word1 = ((Index << 8) | // was << 0 |
| 157 | (IsPCRel << 7) | // was << 24 |
| 158 | (Log2Size << 5) | // was << 25 |
| 159 | (IsExtern << 4) | // was << 27 |
| 160 | (Type << 0)); // was << 28 |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 164 | makeScatteredRelocationInfo(MachO::any_relocation_info &MRE, |
| 165 | const uint32_t Addr, const unsigned Type, |
| 166 | const unsigned Log2Size, const unsigned IsPCRel, |
| 167 | const uint32_t Value2) { |
Charles Davis | 1827bd8 | 2013-08-27 05:38:30 +0000 | [diff] [blame] | 168 | // For notes on bitfield positions and endianness, see: |
| 169 | // https://developer.apple.com/library/mac/documentation/developertools/conceptual/MachORuntime/Reference/reference.html#//apple_ref/doc/uid/20001298-scattered_relocation_entry |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 170 | MRE.r_word0 = ((Addr << 0) | (Type << 24) | (Log2Size << 28) | |
| 171 | (IsPCRel << 30) | MachO::R_SCATTERED); |
| 172 | MRE.r_word1 = Value2; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /// Compute fixup offset (address). |
| 176 | static uint32_t getFixupOffset(const MCAsmLayout &Layout, |
| 177 | const MCFragment *Fragment, |
| 178 | const MCFixup &Fixup) { |
| 179 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 180 | // On Mach-O, ppc_fixup_half16 relocations must refer to the |
| 181 | // start of the instruction, not the second halfword, as ELF does |
David Fang | 2f1b0b5 | 2013-08-08 21:29:30 +0000 | [diff] [blame] | 182 | if (unsigned(Fixup.getKind()) == PPC::fixup_ppc_half16) |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 183 | FixupOffset &= ~uint32_t(3); |
| 184 | return FixupOffset; |
| 185 | } |
| 186 | |
| 187 | /// \return false if falling back to using non-scattered relocation, |
| 188 | /// otherwise true for normal scattered relocation. |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 189 | /// based on X86MachObjectWriter::recordScatteredRelocation |
| 190 | /// and ARMMachObjectWriter::recordScatteredRelocation |
| 191 | bool PPCMachObjectWriter::recordScatteredRelocation( |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 192 | MachObjectWriter *Writer, const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 193 | const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, |
| 194 | unsigned Log2Size, uint64_t &FixedValue) { |
| 195 | // caller already computes these, can we just pass and reuse? |
| 196 | const uint32_t FixupOffset = getFixupOffset(Layout, Fragment, Fixup); |
| 197 | const MCFixupKind FK = Fixup.getKind(); |
| 198 | const unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, FK); |
| 199 | const unsigned Type = getRelocType(Target, FK, IsPCRel); |
| 200 | |
| 201 | // Is this a local or SECTDIFF relocation entry? |
| 202 | // SECTDIFF relocation entries have symbol subtractions, |
| 203 | // and require two entries, the first for the add-symbol value, |
| 204 | // the second for the subtract-symbol value. |
| 205 | |
| 206 | // See <reloc.h>. |
| 207 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 208 | |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 209 | if (!A->getFragment()) |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 210 | report_fatal_error("symbol '" + A->getName() + |
| 211 | "' can not be undefined in a subtraction expression"); |
| 212 | |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 213 | uint32_t Value = Writer->getSymbolAddress(*A, Layout); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 214 | uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent()); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 215 | FixedValue += SecAddr; |
| 216 | uint32_t Value2 = 0; |
| 217 | |
| 218 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 219 | const MCSymbol *SB = &B->getSymbol(); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 220 | |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 221 | if (!SB->getFragment()) |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 222 | report_fatal_error("symbol '" + B->getSymbol().getName() + |
| 223 | "' can not be undefined in a subtraction expression"); |
| 224 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 225 | // FIXME: is Type correct? see include/llvm/Support/MachO.h |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 226 | Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 227 | FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent()); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 228 | } |
| 229 | // FIXME: does FixedValue get used?? |
| 230 | |
| 231 | // Relocations are written out in reverse order, so the PAIR comes first. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 232 | if (Type == MachO::PPC_RELOC_SECTDIFF || |
| 233 | Type == MachO::PPC_RELOC_HI16_SECTDIFF || |
| 234 | Type == MachO::PPC_RELOC_LO16_SECTDIFF || |
| 235 | Type == MachO::PPC_RELOC_HA16_SECTDIFF || |
| 236 | Type == MachO::PPC_RELOC_LO14_SECTDIFF || |
| 237 | Type == MachO::PPC_RELOC_LOCAL_SECTDIFF) { |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 238 | // X86 had this piece, but ARM does not |
| 239 | // If the offset is too large to fit in a scattered relocation, |
| 240 | // we're hosed. It's an unfortunate limitation of the MachO format. |
| 241 | if (FixupOffset > 0xffffff) { |
| 242 | char Buffer[32]; |
| 243 | format("0x%x", FixupOffset).print(Buffer, sizeof(Buffer)); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 244 | Asm.getContext().reportError(Fixup.getLoc(), |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 245 | Twine("Section too large, can't encode " |
| 246 | "r_address (") + |
| 247 | Buffer + ") into 24 bits of scattered " |
| 248 | "relocation entry."); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 249 | return false; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Is this supposed to follow MCTarget/PPCAsmBackend.cpp:adjustFixupValue()? |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 253 | // see PPCMCExpr::evaluateAsRelocatableImpl() |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 254 | uint32_t other_half = 0; |
| 255 | switch (Type) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 256 | case MachO::PPC_RELOC_LO16_SECTDIFF: |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 257 | other_half = (FixedValue >> 16) & 0xffff; |
| 258 | // applyFixupOffset longer extracts the high part because it now assumes |
| 259 | // this was already done. |
| 260 | // It looks like this is not true for the FixedValue needed with Mach-O |
| 261 | // relocs. |
| 262 | // So we need to adjust FixedValue again here. |
| 263 | FixedValue &= 0xffff; |
| 264 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 265 | case MachO::PPC_RELOC_HA16_SECTDIFF: |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 266 | other_half = FixedValue & 0xffff; |
| 267 | FixedValue = |
| 268 | ((FixedValue >> 16) + ((FixedValue & 0x8000) ? 1 : 0)) & 0xffff; |
| 269 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 270 | case MachO::PPC_RELOC_HI16_SECTDIFF: |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 271 | other_half = FixedValue & 0xffff; |
| 272 | FixedValue = (FixedValue >> 16) & 0xffff; |
| 273 | break; |
| 274 | default: |
| 275 | llvm_unreachable("Invalid PPC scattered relocation type."); |
| 276 | break; |
| 277 | } |
| 278 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 279 | MachO::any_relocation_info MRE; |
| 280 | makeScatteredRelocationInfo(MRE, other_half, MachO::GENERIC_RELOC_PAIR, |
| 281 | Log2Size, IsPCRel, Value2); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 282 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 283 | } else { |
| 284 | // If the offset is more than 24-bits, it won't fit in a scattered |
| 285 | // relocation offset field, so we fall back to using a non-scattered |
| 286 | // relocation. This is a bit risky, as if the offset reaches out of |
| 287 | // the block and the linker is doing scattered loading on this |
| 288 | // symbol, things can go badly. |
| 289 | // |
| 290 | // Required for 'as' compatibility. |
| 291 | if (FixupOffset > 0xffffff) |
| 292 | return false; |
| 293 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 294 | MachO::any_relocation_info MRE; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 295 | makeScatteredRelocationInfo(MRE, FixupOffset, Type, Log2Size, IsPCRel, Value); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 296 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | |
| 300 | // see PPCELFObjectWriter for a general outline of cases |
| 301 | void PPCMachObjectWriter::RecordPPCRelocation( |
| 302 | MachObjectWriter *Writer, const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 303 | const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, |
| 304 | uint64_t &FixedValue) { |
| 305 | const MCFixupKind FK = Fixup.getKind(); // unsigned |
| 306 | const unsigned Log2Size = getFixupKindLog2Size(FK); |
| 307 | const bool IsPCRel = Writer->isFixupKindPCRel(Asm, FK); |
| 308 | const unsigned RelocType = getRelocType(Target, FK, IsPCRel); |
| 309 | |
| 310 | // If this is a difference or a defined symbol plus an offset, then we need a |
| 311 | // scattered relocation entry. Differences always require scattered |
| 312 | // relocations. |
| 313 | if (Target.getSymB() && |
| 314 | // Q: are branch targets ever scattered? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 315 | RelocType != MachO::PPC_RELOC_BR24 && |
| 316 | RelocType != MachO::PPC_RELOC_BR14) { |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 317 | recordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, Target, |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 318 | Log2Size, FixedValue); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // this doesn't seem right for RIT_PPC_BR24 |
| 323 | // Get the symbol data, if any. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 324 | const MCSymbol *A = nullptr; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 325 | if (Target.getSymA()) |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 326 | A = &Target.getSymA()->getSymbol(); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 327 | |
| 328 | // See <reloc.h>. |
| 329 | const uint32_t FixupOffset = getFixupOffset(Layout, Fragment, Fixup); |
| 330 | unsigned Index = 0; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 331 | unsigned Type = RelocType; |
| 332 | |
Duncan P. N. Exon Smith | 6e23e5a | 2015-05-16 01:14:19 +0000 | [diff] [blame] | 333 | const MCSymbol *RelSymbol = nullptr; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 334 | if (Target.isAbsolute()) { // constant |
| 335 | // SymbolNum of 0 indicates the absolute section. |
| 336 | // |
| 337 | // FIXME: Currently, these are never generated (see code below). I cannot |
| 338 | // find a case where they are actually emitted. |
| 339 | report_fatal_error("FIXME: relocations to absolute targets " |
| 340 | "not yet implemented"); |
| 341 | // the above line stolen from ARM, not sure |
| 342 | } else { |
| 343 | // Resolve constant variables. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 344 | if (A->isVariable()) { |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 345 | int64_t Res; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 346 | if (A->getVariableValue()->evaluateAsAbsolute( |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 347 | Res, Layout, Writer->getSectionAddressMap())) { |
| 348 | FixedValue = Res; |
| 349 | return; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // Check whether we need an external or internal relocation. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 354 | if (Writer->doesSymbolRequireExternRelocation(*A)) { |
| 355 | RelSymbol = A; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 356 | // For external relocations, make sure to offset the fixup value to |
| 357 | // compensate for the addend of the symbol address, if it was |
| 358 | // undefined. This occurs with weak definitions, for example. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 359 | if (!A->isUndefined()) |
| 360 | FixedValue -= Layout.getSymbolOffset(*A); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 361 | } else { |
| 362 | // The index is the section ordinal (1-based). |
Rafael Espindola | 6e6820a | 2015-05-25 14:12:48 +0000 | [diff] [blame] | 363 | const MCSection &Sec = A->getSection(); |
Rafael Espindola | 6e6820a | 2015-05-25 14:12:48 +0000 | [diff] [blame] | 364 | Index = Sec.getOrdinal() + 1; |
Rafael Espindola | 079027e | 2015-05-26 00:52:18 +0000 | [diff] [blame] | 365 | FixedValue += Writer->getSectionAddress(&Sec); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 366 | } |
| 367 | if (IsPCRel) |
Rafael Espindola | 079027e | 2015-05-26 00:52:18 +0000 | [diff] [blame] | 368 | FixedValue -= Writer->getSectionAddress(Fragment->getParent()); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Charles Davis | 1827bd8 | 2013-08-27 05:38:30 +0000 | [diff] [blame] | 371 | // struct relocation_info (8 bytes) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 372 | MachO::any_relocation_info MRE; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 373 | makeRelocationInfo(MRE, FixupOffset, Index, IsPCRel, Log2Size, false, Type); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 374 | Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE); |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 377 | MCObjectWriter *llvm::createPPCMachObjectWriter(raw_pwrite_stream &OS, |
| 378 | bool Is64Bit, uint32_t CPUType, |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 379 | uint32_t CPUSubtype) { |
| 380 | return createMachObjectWriter( |
| 381 | new PPCMachObjectWriter(Is64Bit, CPUType, CPUSubtype), OS, |
| 382 | /*IsLittleEndian=*/false); |
| 383 | } |