Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1 | //===-- AArch64MachObjectWriter.cpp - ARM Mach Object 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/AArch64FixupKinds.h" |
| 11 | #include "MCTargetDesc/AArch64MCTargetDesc.h" |
Benjamin Kramer | 1f8930e | 2014-07-25 11:42:14 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Twine.h" |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmLayout.h" |
Benjamin Kramer | 1f8930e | 2014-07-25 11:42:14 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAssembler.h" |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
| 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCFixup.h" |
| 19 | #include "llvm/MC/MCMachObjectWriter.h" |
| 20 | #include "llvm/MC/MCSectionMachO.h" |
| 21 | #include "llvm/MC/MCValue.h" |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | #include "llvm/Support/MachO.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | class AArch64MachObjectWriter : public MCMachObjectTargetWriter { |
| 28 | bool getAArch64FixupKindMachOInfo(const MCFixup &Fixup, unsigned &RelocType, |
| 29 | const MCSymbolRefExpr *Sym, |
| 30 | unsigned &Log2Size, const MCAssembler &Asm); |
| 31 | |
| 32 | public: |
| 33 | AArch64MachObjectWriter(uint32_t CPUType, uint32_t CPUSubtype) |
| 34 | : MCMachObjectTargetWriter(true /* is64Bit */, CPUType, CPUSubtype, |
| 35 | /*UseAggressiveSymbolFolding=*/true) {} |
| 36 | |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 37 | void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm, |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 38 | const MCAsmLayout &Layout, const MCFragment *Fragment, |
| 39 | const MCFixup &Fixup, MCValue Target, |
| 40 | uint64_t &FixedValue) override; |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | bool AArch64MachObjectWriter::getAArch64FixupKindMachOInfo( |
| 45 | const MCFixup &Fixup, unsigned &RelocType, const MCSymbolRefExpr *Sym, |
| 46 | unsigned &Log2Size, const MCAssembler &Asm) { |
| 47 | RelocType = unsigned(MachO::ARM64_RELOC_UNSIGNED); |
| 48 | Log2Size = ~0U; |
| 49 | |
| 50 | switch ((unsigned)Fixup.getKind()) { |
| 51 | default: |
| 52 | return false; |
| 53 | |
| 54 | case FK_Data_1: |
| 55 | Log2Size = llvm::Log2_32(1); |
| 56 | return true; |
| 57 | case FK_Data_2: |
| 58 | Log2Size = llvm::Log2_32(2); |
| 59 | return true; |
| 60 | case FK_Data_4: |
| 61 | Log2Size = llvm::Log2_32(4); |
| 62 | if (Sym->getKind() == MCSymbolRefExpr::VK_GOT) |
| 63 | RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT); |
| 64 | return true; |
| 65 | case FK_Data_8: |
| 66 | Log2Size = llvm::Log2_32(8); |
| 67 | if (Sym->getKind() == MCSymbolRefExpr::VK_GOT) |
| 68 | RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT); |
| 69 | return true; |
| 70 | case AArch64::fixup_aarch64_add_imm12: |
| 71 | case AArch64::fixup_aarch64_ldst_imm12_scale1: |
| 72 | case AArch64::fixup_aarch64_ldst_imm12_scale2: |
| 73 | case AArch64::fixup_aarch64_ldst_imm12_scale4: |
| 74 | case AArch64::fixup_aarch64_ldst_imm12_scale8: |
| 75 | case AArch64::fixup_aarch64_ldst_imm12_scale16: |
| 76 | Log2Size = llvm::Log2_32(4); |
| 77 | switch (Sym->getKind()) { |
| 78 | default: |
Craig Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 79 | llvm_unreachable("Unexpected symbol reference variant kind!"); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 80 | case MCSymbolRefExpr::VK_PAGEOFF: |
| 81 | RelocType = unsigned(MachO::ARM64_RELOC_PAGEOFF12); |
| 82 | return true; |
| 83 | case MCSymbolRefExpr::VK_GOTPAGEOFF: |
| 84 | RelocType = unsigned(MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12); |
| 85 | return true; |
| 86 | case MCSymbolRefExpr::VK_TLVPPAGEOFF: |
| 87 | RelocType = unsigned(MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12); |
| 88 | return true; |
| 89 | } |
| 90 | case AArch64::fixup_aarch64_pcrel_adrp_imm21: |
| 91 | Log2Size = llvm::Log2_32(4); |
| 92 | // This encompasses the relocation for the whole 21-bit value. |
| 93 | switch (Sym->getKind()) { |
| 94 | default: |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 95 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 96 | "ADR/ADRP relocations must be GOT relative"); |
| 97 | case MCSymbolRefExpr::VK_PAGE: |
| 98 | RelocType = unsigned(MachO::ARM64_RELOC_PAGE21); |
| 99 | return true; |
| 100 | case MCSymbolRefExpr::VK_GOTPAGE: |
| 101 | RelocType = unsigned(MachO::ARM64_RELOC_GOT_LOAD_PAGE21); |
| 102 | return true; |
| 103 | case MCSymbolRefExpr::VK_TLVPPAGE: |
| 104 | RelocType = unsigned(MachO::ARM64_RELOC_TLVP_LOAD_PAGE21); |
| 105 | return true; |
| 106 | } |
| 107 | return true; |
| 108 | case AArch64::fixup_aarch64_pcrel_branch26: |
| 109 | case AArch64::fixup_aarch64_pcrel_call26: |
| 110 | Log2Size = llvm::Log2_32(4); |
| 111 | RelocType = unsigned(MachO::ARM64_RELOC_BRANCH26); |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 116 | static bool canUseLocalRelocation(const MCSectionMachO &Section, |
| 117 | const MCSymbol &Symbol, unsigned Log2Size) { |
| 118 | // Debug info sections can use local relocations. |
| 119 | if (Section.hasAttribute(MachO::S_ATTR_DEBUG)) |
| 120 | return true; |
| 121 | |
| 122 | // Otherwise, only pointer sized relocations are supported. |
| 123 | if (Log2Size != 3) |
| 124 | return false; |
| 125 | |
| 126 | // But only if they don't point to a few forbidden sections. |
| 127 | if (!Symbol.isInSection()) |
| 128 | return true; |
| 129 | const MCSectionMachO &RefSec = cast<MCSectionMachO>(Symbol.getSection()); |
| 130 | if (RefSec.getType() == MachO::S_CSTRING_LITERALS) |
| 131 | return false; |
| 132 | |
| 133 | if (RefSec.getSegmentName() == "__DATA" && |
Rafael Espindola | e4bcad4 | 2015-02-12 23:11:59 +0000 | [diff] [blame] | 134 | RefSec.getSectionName() == "__objc_classrefs") |
| 135 | return false; |
| 136 | |
Tim Northover | d6223a2 | 2015-05-18 22:07:20 +0000 | [diff] [blame] | 137 | // FIXME: ld64 currently handles internal pointer-sized relocations |
| 138 | // incorrectly (applying the addend twice). We should be able to return true |
| 139 | // unconditionally by this point when that's fixed. |
| 140 | return false; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 143 | void AArch64MachObjectWriter::RecordRelocation( |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 144 | MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout, |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 145 | const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, |
| 146 | uint64_t &FixedValue) { |
| 147 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
| 148 | |
| 149 | // See <reloc.h>. |
| 150 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment); |
| 151 | unsigned Log2Size = 0; |
| 152 | int64_t Value = 0; |
| 153 | unsigned Index = 0; |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 154 | unsigned Type = 0; |
| 155 | unsigned Kind = Fixup.getKind(); |
Duncan P. N. Exon Smith | 09bfa58 | 2015-05-16 00:48:58 +0000 | [diff] [blame] | 156 | const MCSymbol *RelSymbol = nullptr; |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 157 | |
| 158 | FixupOffset += Fixup.getOffset(); |
| 159 | |
| 160 | // AArch64 pcrel relocation addends do not include the section offset. |
| 161 | if (IsPCRel) |
| 162 | FixedValue += FixupOffset; |
| 163 | |
| 164 | // ADRP fixups use relocations for the whole symbol value and only |
| 165 | // put the addend in the instruction itself. Clear out any value the |
| 166 | // generic code figured out from the sybmol definition. |
| 167 | if (Kind == AArch64::fixup_aarch64_pcrel_adrp_imm21) |
| 168 | FixedValue = 0; |
| 169 | |
| 170 | // imm19 relocations are for conditional branches, which require |
| 171 | // assembler local symbols. If we got here, that's not what we have, |
| 172 | // so complain loudly. |
| 173 | if (Kind == AArch64::fixup_aarch64_pcrel_branch19) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 174 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 175 | "conditional branch requires assembler-local" |
| 176 | " label. '" + |
| 177 | Target.getSymA()->getSymbol().getName() + |
| 178 | "' is external."); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | // 14-bit branch relocations should only target internal labels, and so |
| 183 | // should never get here. |
| 184 | if (Kind == AArch64::fixup_aarch64_pcrel_branch14) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 185 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 186 | "Invalid relocation on conditional branch!"); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (!getAArch64FixupKindMachOInfo(Fixup, Type, Target.getSymA(), Log2Size, |
| 191 | Asm)) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 192 | Asm.getContext().reportFatalError(Fixup.getLoc(), "unknown AArch64 fixup kind!"); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
| 196 | Value = Target.getConstant(); |
| 197 | |
| 198 | if (Target.isAbsolute()) { // constant |
| 199 | // FIXME: Should this always be extern? |
| 200 | // SymbolNum of 0 indicates the absolute section. |
| 201 | Type = MachO::ARM64_RELOC_UNSIGNED; |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 202 | |
| 203 | if (IsPCRel) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 204 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 205 | "PC relative absolute relocation!"); |
| 206 | |
| 207 | // FIXME: x86_64 sets the type to a branch reloc here. Should we do |
| 208 | // something similar? |
| 209 | } |
| 210 | } else if (Target.getSymB()) { // A - B + constant |
| 211 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
| 212 | const MCSymbolData &A_SD = Asm.getSymbolData(*A); |
Duncan P. N. Exon Smith | fd27a1d | 2015-05-20 16:02:11 +0000 | [diff] [blame] | 213 | const MCSymbol *A_Base = Asm.getAtom(*A); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 214 | |
| 215 | const MCSymbol *B = &Target.getSymB()->getSymbol(); |
| 216 | const MCSymbolData &B_SD = Asm.getSymbolData(*B); |
Duncan P. N. Exon Smith | fd27a1d | 2015-05-20 16:02:11 +0000 | [diff] [blame] | 217 | const MCSymbol *B_Base = Asm.getAtom(*B); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 218 | |
| 219 | // Check for "_foo@got - .", which comes through here as: |
| 220 | // Ltmp0: |
| 221 | // ... _foo@got - Ltmp0 |
| 222 | if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOT && |
| 223 | Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None && |
Duncan P. N. Exon Smith | 2a40483 | 2015-05-19 23:53:20 +0000 | [diff] [blame] | 224 | Layout.getSymbolOffset(*B) == |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 225 | Layout.getFragmentOffset(Fragment) + Fixup.getOffset()) { |
| 226 | // SymB is the PC, so use a PC-rel pointer-to-GOT relocation. |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 227 | Type = MachO::ARM64_RELOC_POINTER_TO_GOT; |
| 228 | IsPCRel = 1; |
| 229 | MachO::any_relocation_info MRE; |
| 230 | MRE.r_word0 = FixupOffset; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 231 | MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame^] | 232 | Writer->addRelocation(A_Base, Fragment->getParent(), MRE); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 233 | return; |
| 234 | } else if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None || |
| 235 | Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None) |
| 236 | // Otherwise, neither symbol can be modified. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 237 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 238 | "unsupported relocation of modified symbol"); |
| 239 | |
| 240 | // We don't support PCrel relocations of differences. |
| 241 | if (IsPCRel) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 242 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 243 | "unsupported pc-relative relocation of " |
| 244 | "difference"); |
| 245 | |
| 246 | // AArch64 always uses external relocations. If there is no symbol to use as |
| 247 | // a base address (a local symbol with no preceding non-local symbol), |
| 248 | // error out. |
| 249 | // |
| 250 | // FIXME: We should probably just synthesize an external symbol and use |
| 251 | // that. |
| 252 | if (!A_Base) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 253 | Asm.getContext().reportFatalError( |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 254 | Fixup.getLoc(), |
| 255 | "unsupported relocation of local symbol '" + A->getName() + |
| 256 | "'. Must have non-local symbol earlier in section."); |
| 257 | if (!B_Base) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 258 | Asm.getContext().reportFatalError( |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 259 | Fixup.getLoc(), |
| 260 | "unsupported relocation of local symbol '" + B->getName() + |
| 261 | "'. Must have non-local symbol earlier in section."); |
| 262 | |
| 263 | if (A_Base == B_Base && A_Base) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 264 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 265 | "unsupported relocation with identical base"); |
| 266 | |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 267 | Value += (!A_SD.getFragment() ? 0 : Writer->getSymbolAddress(*A, Layout)) - |
| 268 | (!A_Base || !A_Base->getData().getFragment() |
| 269 | ? 0 |
| 270 | : Writer->getSymbolAddress(*A_Base, Layout)); |
| 271 | Value -= (!B_SD.getFragment() ? 0 : Writer->getSymbolAddress(*B, Layout)) - |
| 272 | (!B_Base || !B_Base->getData().getFragment() |
| 273 | ? 0 |
| 274 | : Writer->getSymbolAddress(*B_Base, Layout)); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 275 | |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 276 | Type = MachO::ARM64_RELOC_UNSIGNED; |
| 277 | |
| 278 | MachO::any_relocation_info MRE; |
| 279 | MRE.r_word0 = FixupOffset; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 280 | MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame^] | 281 | Writer->addRelocation(A_Base, Fragment->getParent(), MRE); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 282 | |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 283 | RelSymbol = B_Base; |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 284 | Type = MachO::ARM64_RELOC_SUBTRACTOR; |
| 285 | } else { // A + constant |
| 286 | const MCSymbol *Symbol = &Target.getSymA()->getSymbol(); |
Rafael Espindola | 7549f87 | 2015-05-26 00:36:57 +0000 | [diff] [blame] | 287 | const MCSectionMachO &Section = |
| 288 | static_cast<const MCSectionMachO &>(*Fragment->getParent()); |
Rafael Espindola | d9c3e30 | 2015-01-12 18:13:07 +0000 | [diff] [blame] | 289 | |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 290 | bool CanUseLocalRelocation = |
| 291 | canUseLocalRelocation(Section, *Symbol, Log2Size); |
| 292 | if (Symbol->isTemporary() && (Value || !CanUseLocalRelocation)) { |
| 293 | const MCSection &Sec = Symbol->getSection(); |
| 294 | if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec)) |
| 295 | Asm.addLocalUsedInReloc(*Symbol); |
| 296 | } |
| 297 | |
Duncan P. N. Exon Smith | fd27a1d | 2015-05-20 16:02:11 +0000 | [diff] [blame] | 298 | const MCSymbol *Base = Asm.getAtom(*Symbol); |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 299 | |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 300 | // If the symbol is a variable and we weren't able to get a Base for it |
| 301 | // (i.e., it's not in the symbol table associated with a section) resolve |
| 302 | // the relocation based its expansion instead. |
| 303 | if (Symbol->isVariable() && !Base) { |
| 304 | // If the evaluation is an absolute value, just use that directly |
| 305 | // to keep things easy. |
| 306 | int64_t Res; |
Duncan P. N. Exon Smith | 92a699c | 2015-05-20 20:18:16 +0000 | [diff] [blame] | 307 | if (Symbol->getVariableValue()->EvaluateAsAbsolute( |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 308 | Res, Layout, Writer->getSectionAddressMap())) { |
| 309 | FixedValue = Res; |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | // FIXME: Will the Target we already have ever have any data in it |
| 314 | // we need to preserve and merge with the new Target? How about |
| 315 | // the FixedValue? |
Joerg Sonnenberger | 752b91b | 2014-08-10 11:35:12 +0000 | [diff] [blame] | 316 | if (!Symbol->getVariableValue()->EvaluateAsRelocatable(Target, &Layout, |
| 317 | &Fixup)) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 318 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 319 | "unable to resolve variable '" + |
| 320 | Symbol->getName() + "'"); |
| 321 | return RecordRelocation(Writer, Asm, Layout, Fragment, Fixup, Target, |
| 322 | FixedValue); |
| 323 | } |
| 324 | |
| 325 | // Relocations inside debug sections always use local relocations when |
| 326 | // possible. This seems to be done because the debugger doesn't fully |
| 327 | // understand relocation entries and expects to find values that |
| 328 | // have already been fixed up. |
| 329 | if (Symbol->isInSection()) { |
| 330 | if (Section.hasAttribute(MachO::S_ATTR_DEBUG)) |
| 331 | Base = nullptr; |
| 332 | } |
| 333 | |
| 334 | // AArch64 uses external relocations as much as possible. For debug |
| 335 | // sections, and for pointer-sized relocations (.quad), we allow section |
| 336 | // relocations. It's code sections that run into trouble. |
| 337 | if (Base) { |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 338 | RelSymbol = Base; |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 339 | |
| 340 | // Add the local offset, if needed. |
Duncan P. N. Exon Smith | 2a40483 | 2015-05-19 23:53:20 +0000 | [diff] [blame] | 341 | if (Base != Symbol) |
| 342 | Value += |
| 343 | Layout.getSymbolOffset(*Symbol) - Layout.getSymbolOffset(*Base); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 344 | } else if (Symbol->isInSection()) { |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 345 | if (!CanUseLocalRelocation) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 346 | Asm.getContext().reportFatalError( |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 347 | Fixup.getLoc(), |
| 348 | "unsupported relocation of local symbol '" + Symbol->getName() + |
| 349 | "'. Must have non-local symbol earlier in section."); |
| 350 | // Adjust the relocation to be section-relative. |
| 351 | // The index is the section ordinal (1-based). |
Rafael Espindola | 6e6820a | 2015-05-25 14:12:48 +0000 | [diff] [blame] | 352 | const MCSection &Sec = Symbol->getSection(); |
| 353 | Index = Sec.getOrdinal() + 1; |
Duncan P. N. Exon Smith | 92a699c | 2015-05-20 20:18:16 +0000 | [diff] [blame] | 354 | Value += Writer->getSymbolAddress(*Symbol, Layout); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 355 | |
| 356 | if (IsPCRel) |
| 357 | Value -= Writer->getFragmentAddress(Fragment, Layout) + |
| 358 | Fixup.getOffset() + (1ULL << Log2Size); |
| 359 | } else { |
| 360 | // Resolve constant variables. |
Duncan P. N. Exon Smith | 92a699c | 2015-05-20 20:18:16 +0000 | [diff] [blame] | 361 | if (Symbol->isVariable()) { |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 362 | int64_t Res; |
Duncan P. N. Exon Smith | 92a699c | 2015-05-20 20:18:16 +0000 | [diff] [blame] | 363 | if (Symbol->getVariableValue()->EvaluateAsAbsolute( |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 364 | Res, Layout, Writer->getSectionAddressMap())) { |
| 365 | FixedValue = Res; |
| 366 | return; |
| 367 | } |
| 368 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 369 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 370 | "unsupported relocation of variable '" + |
| 371 | Symbol->getName() + "'"); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // If the relocation kind is Branch26, Page21, or Pageoff12, any addend |
| 376 | // is represented via an Addend relocation, not encoded directly into |
| 377 | // the instruction. |
| 378 | if ((Type == MachO::ARM64_RELOC_BRANCH26 || |
| 379 | Type == MachO::ARM64_RELOC_PAGE21 || |
| 380 | Type == MachO::ARM64_RELOC_PAGEOFF12) && |
| 381 | Value) { |
| 382 | assert((Value & 0xff000000) == 0 && "Added relocation out of range!"); |
| 383 | |
| 384 | MachO::any_relocation_info MRE; |
| 385 | MRE.r_word0 = FixupOffset; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 386 | MRE.r_word1 = |
| 387 | (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame^] | 388 | Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 389 | |
| 390 | // Now set up the Addend relocation. |
| 391 | Type = MachO::ARM64_RELOC_ADDEND; |
| 392 | Index = Value; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 393 | RelSymbol = nullptr; |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 394 | IsPCRel = 0; |
| 395 | Log2Size = 2; |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 396 | |
| 397 | // Put zero into the instruction itself. The addend is in the relocation. |
| 398 | Value = 0; |
| 399 | } |
| 400 | |
| 401 | // If there's any addend left to handle, encode it in the instruction. |
| 402 | FixedValue = Value; |
| 403 | |
| 404 | // struct relocation_info (8 bytes) |
| 405 | MachO::any_relocation_info MRE; |
| 406 | MRE.r_word0 = FixupOffset; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 407 | MRE.r_word1 = |
| 408 | (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame^] | 409 | Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE); |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 412 | MCObjectWriter *llvm::createAArch64MachObjectWriter(raw_pwrite_stream &OS, |
Rafael Espindola | 49286e9 | 2015-04-09 18:32:58 +0000 | [diff] [blame] | 413 | uint32_t CPUType, |
| 414 | uint32_t CPUSubtype) { |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 415 | return createMachObjectWriter( |
| 416 | new AArch64MachObjectWriter(CPUType, CPUSubtype), OS, |
| 417 | /*IsLittleEndian=*/true); |
| 418 | } |