Daniel Dunbar | 7da045e | 2010-12-20 15:07:39 +0000 | [diff] [blame] | 1 | //===-- X86MachObjectWriter.cpp - X86 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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/X86FixupKinds.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/X86MCTargetDesc.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 13 | #include "llvm/BinaryFormat/MachO.h" |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmInfo.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmLayout.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAssembler.h" |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 7da045e | 2010-12-20 15:07:39 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCMachObjectWriter.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSectionMachO.h" |
| 20 | #include "llvm/MC/MCValue.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 23 | |
Daniel Dunbar | 7da045e | 2010-12-20 15:07:39 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | class X86MachObjectWriter : public MCMachObjectTargetWriter { |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 28 | bool recordScatteredRelocation(MachObjectWriter *Writer, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 29 | const MCAssembler &Asm, |
| 30 | const MCAsmLayout &Layout, |
| 31 | const MCFragment *Fragment, |
| 32 | const MCFixup &Fixup, |
| 33 | MCValue Target, |
| 34 | unsigned Log2Size, |
| 35 | uint64_t &FixedValue); |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 36 | void recordTLVPRelocation(MachObjectWriter *Writer, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 37 | const MCAssembler &Asm, |
| 38 | const MCAsmLayout &Layout, |
| 39 | const MCFragment *Fragment, |
| 40 | const MCFixup &Fixup, |
| 41 | MCValue Target, |
| 42 | uint64_t &FixedValue); |
| 43 | |
| 44 | void RecordX86Relocation(MachObjectWriter *Writer, |
| 45 | const MCAssembler &Asm, |
| 46 | const MCAsmLayout &Layout, |
| 47 | const MCFragment *Fragment, |
| 48 | const MCFixup &Fixup, |
| 49 | MCValue Target, |
| 50 | uint64_t &FixedValue); |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 51 | void RecordX86_64Relocation(MachObjectWriter *Writer, MCAssembler &Asm, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 52 | const MCAsmLayout &Layout, |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 53 | const MCFragment *Fragment, const MCFixup &Fixup, |
| 54 | MCValue Target, uint64_t &FixedValue); |
| 55 | |
Daniel Dunbar | 7da045e | 2010-12-20 15:07:39 +0000 | [diff] [blame] | 56 | public: |
Jim Grosbach | 7c76b4c | 2015-06-04 20:27:42 +0000 | [diff] [blame] | 57 | X86MachObjectWriter(bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype) |
| 58 | : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {} |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 59 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 60 | void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm, |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 61 | const MCAsmLayout &Layout, const MCFragment *Fragment, |
| 62 | const MCFixup &Fixup, MCValue Target, |
| 63 | uint64_t &FixedValue) override { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 64 | if (Writer->is64Bit()) |
| 65 | RecordX86_64Relocation(Writer, Asm, Layout, Fragment, Fixup, Target, |
| 66 | FixedValue); |
| 67 | else |
| 68 | RecordX86Relocation(Writer, Asm, Layout, Fragment, Fixup, Target, |
| 69 | FixedValue); |
| 70 | } |
Daniel Dunbar | 7da045e | 2010-12-20 15:07:39 +0000 | [diff] [blame] | 71 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 72 | } |
Daniel Dunbar | 7da045e | 2010-12-20 15:07:39 +0000 | [diff] [blame] | 73 | |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 74 | static bool isFixupKindRIPRel(unsigned Kind) { |
| 75 | return Kind == X86::reloc_riprel_4byte || |
Rafael Espindola | 52bd330 | 2016-05-28 15:51:38 +0000 | [diff] [blame] | 76 | Kind == X86::reloc_riprel_4byte_movq_load || |
| 77 | Kind == X86::reloc_riprel_4byte_relax || |
| 78 | Kind == X86::reloc_riprel_4byte_relax_rex; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static unsigned getFixupKindLog2Size(unsigned Kind) { |
| 82 | switch (Kind) { |
| 83 | default: |
| 84 | llvm_unreachable("invalid fixup kind!"); |
| 85 | case FK_PCRel_1: |
| 86 | case FK_Data_1: return 0; |
| 87 | case FK_PCRel_2: |
| 88 | case FK_Data_2: return 1; |
| 89 | case FK_PCRel_4: |
| 90 | // FIXME: Remove these!!! |
| 91 | case X86::reloc_riprel_4byte: |
Rafael Espindola | 52bd330 | 2016-05-28 15:51:38 +0000 | [diff] [blame] | 92 | case X86::reloc_riprel_4byte_relax: |
| 93 | case X86::reloc_riprel_4byte_relax_rex: |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 94 | case X86::reloc_riprel_4byte_movq_load: |
| 95 | case X86::reloc_signed_4byte: |
Rafael Espindola | a29971f | 2016-07-06 21:19:11 +0000 | [diff] [blame] | 96 | case X86::reloc_signed_4byte_relax: |
George Rimar | da4f43a4 | 2018-02-20 10:17:57 +0000 | [diff] [blame] | 97 | case X86::reloc_branch_4byte_pcrel: |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 98 | case FK_Data_4: return 2; |
| 99 | case FK_Data_8: return 3; |
| 100 | } |
| 101 | } |
| 102 | |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 103 | void X86MachObjectWriter::RecordX86_64Relocation( |
| 104 | MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout, |
| 105 | const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, |
| 106 | uint64_t &FixedValue) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 107 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
| 108 | unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind()); |
| 109 | unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); |
| 110 | |
| 111 | // See <reloc.h>. |
| 112 | uint32_t FixupOffset = |
| 113 | Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 114 | uint32_t FixupAddress = |
| 115 | Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset(); |
| 116 | int64_t Value = 0; |
| 117 | unsigned Index = 0; |
| 118 | unsigned IsExtern = 0; |
| 119 | unsigned Type = 0; |
Duncan P. N. Exon Smith | 09bfa58 | 2015-05-16 00:48:58 +0000 | [diff] [blame] | 120 | const MCSymbol *RelSymbol = nullptr; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 121 | |
| 122 | Value = Target.getConstant(); |
| 123 | |
| 124 | if (IsPCRel) { |
| 125 | // Compensate for the relocation offset, Darwin x86_64 relocations only have |
| 126 | // the addend and appear to have attempted to define it to be the actual |
| 127 | // expression addend without the PCrel bias. However, instructions with data |
| 128 | // following the relocation are not accommodated for (see comment below |
| 129 | // regarding SIGNED{1,2,4}), so it isn't exactly that either. |
| 130 | Value += 1LL << Log2Size; |
| 131 | } |
| 132 | |
| 133 | if (Target.isAbsolute()) { // constant |
| 134 | // SymbolNum of 0 indicates the absolute section. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 135 | Type = MachO::X86_64_RELOC_UNSIGNED; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 136 | |
| 137 | // FIXME: I believe this is broken, I don't think the linker can understand |
| 138 | // it. I think it would require a local relocation, but I'm not sure if that |
| 139 | // would work either. The official way to get an absolute PCrel relocation |
| 140 | // is to use an absolute symbol (which we don't support yet). |
| 141 | if (IsPCRel) { |
| 142 | IsExtern = 1; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 143 | Type = MachO::X86_64_RELOC_BRANCH; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 144 | } |
| 145 | } else if (Target.getSymB()) { // A - B + constant |
| 146 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
Kevin Enderby | 09cdb43 | 2013-09-05 20:25:06 +0000 | [diff] [blame] | 147 | if (A->isTemporary()) |
Rafael Espindola | 7f4e07b | 2015-04-17 12:28:43 +0000 | [diff] [blame] | 148 | A = &Writer->findAliasedSymbol(*A); |
Duncan P. N. Exon Smith | fd27a1d | 2015-05-20 16:02:11 +0000 | [diff] [blame] | 149 | const MCSymbol *A_Base = Asm.getAtom(*A); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 150 | |
| 151 | const MCSymbol *B = &Target.getSymB()->getSymbol(); |
Kevin Enderby | 09cdb43 | 2013-09-05 20:25:06 +0000 | [diff] [blame] | 152 | if (B->isTemporary()) |
Rafael Espindola | 7f4e07b | 2015-04-17 12:28:43 +0000 | [diff] [blame] | 153 | B = &Writer->findAliasedSymbol(*B); |
Duncan P. N. Exon Smith | fd27a1d | 2015-05-20 16:02:11 +0000 | [diff] [blame] | 154 | const MCSymbol *B_Base = Asm.getAtom(*B); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 155 | |
| 156 | // Neither symbol can be modified. |
Rafael Espindola | 8a261c2 | 2017-06-22 17:25:35 +0000 | [diff] [blame] | 157 | if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None) { |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 158 | Asm.getContext().reportError(Fixup.getLoc(), |
| 159 | "unsupported relocation of modified symbol"); |
| 160 | return; |
| 161 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 162 | |
| 163 | // We don't support PCrel relocations of differences. Darwin 'as' doesn't |
| 164 | // implement most of these correctly. |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 165 | if (IsPCRel) { |
| 166 | Asm.getContext().reportError( |
| 167 | Fixup.getLoc(), "unsupported pc-relative relocation of difference"); |
| 168 | return; |
| 169 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 170 | |
| 171 | // The support for the situation where one or both of the symbols would |
| 172 | // require a local relocation is handled just like if the symbols were |
| 173 | // external. This is certainly used in the case of debug sections where the |
| 174 | // section has only temporary symbols and thus the symbols don't have base |
| 175 | // symbols. This is encoded using the section ordinal and non-extern |
| 176 | // relocation entries. |
| 177 | |
| 178 | // Darwin 'as' doesn't emit correct relocations for this (it ends up with a |
| 179 | // single SIGNED relocation); reject it for now. Except the case where both |
| 180 | // symbols don't have a base, equal but both NULL. |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 181 | if (A_Base == B_Base && A_Base) { |
| 182 | Asm.getContext().reportError( |
| 183 | Fixup.getLoc(), "unsupported relocation with identical base"); |
| 184 | return; |
| 185 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 186 | |
Kevin Enderby | 2813f49 | 2014-10-24 22:39:40 +0000 | [diff] [blame] | 187 | // A subtraction expression where either symbol is undefined is a |
Kevin Enderby | b03f3fe | 2013-08-12 22:45:44 +0000 | [diff] [blame] | 188 | // non-relocatable expression. |
Kevin Enderby | 2813f49 | 2014-10-24 22:39:40 +0000 | [diff] [blame] | 189 | if (A->isUndefined() || B->isUndefined()) { |
| 190 | StringRef Name = A->isUndefined() ? A->getName() : B->getName(); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 191 | Asm.getContext().reportError(Fixup.getLoc(), |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 192 | "unsupported relocation with subtraction expression, symbol '" + |
Kevin Enderby | 2813f49 | 2014-10-24 22:39:40 +0000 | [diff] [blame] | 193 | Name + "' can not be undefined in a subtraction expression"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 194 | return; |
Kevin Enderby | 2813f49 | 2014-10-24 22:39:40 +0000 | [diff] [blame] | 195 | } |
Kevin Enderby | b03f3fe | 2013-08-12 22:45:44 +0000 | [diff] [blame] | 196 | |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 197 | Value += Writer->getSymbolAddress(*A, Layout) - |
| 198 | (!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, Layout)); |
| 199 | Value -= Writer->getSymbolAddress(*B, Layout) - |
| 200 | (!B_Base ? 0 : Writer->getSymbolAddress(*B_Base, Layout)); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 201 | |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 202 | if (!A_Base) |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 203 | Index = A->getFragment()->getParent()->getOrdinal() + 1; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 204 | Type = MachO::X86_64_RELOC_UNSIGNED; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 205 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 206 | MachO::any_relocation_info MRE; |
| 207 | MRE.r_word0 = FixupOffset; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 208 | MRE.r_word1 = |
| 209 | (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 210 | Writer->addRelocation(A_Base, Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 211 | |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 212 | if (B_Base) |
| 213 | RelSymbol = B_Base; |
| 214 | else |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 215 | Index = B->getFragment()->getParent()->getOrdinal() + 1; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 216 | Type = MachO::X86_64_RELOC_SUBTRACTOR; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 217 | } else { |
| 218 | const MCSymbol *Symbol = &Target.getSymA()->getSymbol(); |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 219 | if (Symbol->isTemporary() && Value) { |
| 220 | const MCSection &Sec = Symbol->getSection(); |
| 221 | if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec)) |
Rafael Espindola | dfe2d35 | 2015-06-17 20:08:20 +0000 | [diff] [blame] | 222 | Symbol->setUsedInReloc(); |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 223 | } |
Duncan P. N. Exon Smith | fd27a1d | 2015-05-20 16:02:11 +0000 | [diff] [blame] | 224 | RelSymbol = Asm.getAtom(*Symbol); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 225 | |
| 226 | // Relocations inside debug sections always use local relocations when |
| 227 | // possible. This seems to be done because the debugger doesn't fully |
| 228 | // understand x86_64 relocation entries, and expects to find values that |
| 229 | // have already been fixed up. |
| 230 | if (Symbol->isInSection()) { |
Rafael Espindola | 7549f87 | 2015-05-26 00:36:57 +0000 | [diff] [blame] | 231 | const MCSectionMachO &Section = |
| 232 | static_cast<const MCSectionMachO &>(*Fragment->getParent()); |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 233 | if (Section.hasAttribute(MachO::S_ATTR_DEBUG)) |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 234 | RelSymbol = nullptr; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // x86_64 almost always uses external relocations, except when there is no |
| 238 | // symbol to use as a base address (a local symbol with no preceding |
| 239 | // non-local symbol). |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 240 | if (RelSymbol) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 241 | // Add the local offset, if needed. |
Duncan P. N. Exon Smith | 2a40483 | 2015-05-19 23:53:20 +0000 | [diff] [blame] | 242 | if (RelSymbol != Symbol) |
| 243 | Value += Layout.getSymbolOffset(*Symbol) - |
| 244 | Layout.getSymbolOffset(*RelSymbol); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 245 | } else if (Symbol->isInSection() && !Symbol->isVariable()) { |
| 246 | // The index is the section ordinal (1-based). |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 247 | Index = Symbol->getFragment()->getParent()->getOrdinal() + 1; |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 248 | Value += Writer->getSymbolAddress(*Symbol, Layout); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 249 | |
| 250 | if (IsPCRel) |
| 251 | Value -= FixupAddress + (1 << Log2Size); |
| 252 | } else if (Symbol->isVariable()) { |
| 253 | const MCExpr *Value = Symbol->getVariableValue(); |
| 254 | int64_t Res; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 255 | bool isAbs = Value->evaluateAsAbsolute(Res, Layout, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 256 | Writer->getSectionAddressMap()); |
| 257 | if (isAbs) { |
| 258 | FixedValue = Res; |
| 259 | return; |
| 260 | } else { |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 261 | Asm.getContext().reportError(Fixup.getLoc(), |
| 262 | "unsupported relocation of variable '" + |
| 263 | Symbol->getName() + "'"); |
| 264 | return; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 265 | } |
| 266 | } else { |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 267 | Asm.getContext().reportError( |
| 268 | Fixup.getLoc(), "unsupported relocation of undefined symbol '" + |
| 269 | Symbol->getName() + "'"); |
| 270 | return; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind(); |
| 274 | if (IsPCRel) { |
| 275 | if (IsRIPRel) { |
| 276 | if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) { |
| 277 | // x86_64 distinguishes movq foo@GOTPCREL so that the linker can |
| 278 | // rewrite the movq to an leaq at link time if the symbol ends up in |
| 279 | // the same linkage unit. |
| 280 | if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 281 | Type = MachO::X86_64_RELOC_GOT_LOAD; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 282 | else |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 283 | Type = MachO::X86_64_RELOC_GOT; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 284 | } else if (Modifier == MCSymbolRefExpr::VK_TLVP) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 285 | Type = MachO::X86_64_RELOC_TLV; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 286 | } else if (Modifier != MCSymbolRefExpr::VK_None) { |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 287 | Asm.getContext().reportError( |
| 288 | Fixup.getLoc(), "unsupported symbol modifier in relocation"); |
| 289 | return; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 290 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 291 | Type = MachO::X86_64_RELOC_SIGNED; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 292 | |
| 293 | // The Darwin x86_64 relocation format has a problem where it cannot |
| 294 | // encode an address (L<foo> + <constant>) which is outside the atom |
| 295 | // containing L<foo>. Generally, this shouldn't occur but it does |
| 296 | // happen when we have a RIPrel instruction with data following the |
| 297 | // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel |
| 298 | // adjustment Darwin x86_64 uses, the offset is still negative and the |
| 299 | // linker has no way to recognize this. |
| 300 | // |
| 301 | // To work around this, Darwin uses several special relocation types |
| 302 | // to indicate the offsets. However, the specification or |
| 303 | // implementation of these seems to also be incomplete; they should |
| 304 | // adjust the addend as well based on the actual encoded instruction |
| 305 | // (the additional bias), but instead appear to just look at the final |
| 306 | // offset. |
| 307 | switch (-(Target.getConstant() + (1LL << Log2Size))) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 308 | case 1: Type = MachO::X86_64_RELOC_SIGNED_1; break; |
| 309 | case 2: Type = MachO::X86_64_RELOC_SIGNED_2; break; |
| 310 | case 4: Type = MachO::X86_64_RELOC_SIGNED_4; break; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | } else { |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 314 | if (Modifier != MCSymbolRefExpr::VK_None) { |
| 315 | Asm.getContext().reportError( |
| 316 | Fixup.getLoc(), |
| 317 | "unsupported symbol modifier in branch relocation"); |
| 318 | return; |
| 319 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 320 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 321 | Type = MachO::X86_64_RELOC_BRANCH; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 322 | } |
| 323 | } else { |
| 324 | if (Modifier == MCSymbolRefExpr::VK_GOT) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 325 | Type = MachO::X86_64_RELOC_GOT; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 326 | } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) { |
| 327 | // GOTPCREL is allowed as a modifier on non-PCrel instructions, in which |
| 328 | // case all we do is set the PCrel bit in the relocation entry; this is |
| 329 | // used with exception handling, for example. The source is required to |
| 330 | // include any necessary offset directly. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 331 | Type = MachO::X86_64_RELOC_GOT; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 332 | IsPCRel = 1; |
| 333 | } else if (Modifier == MCSymbolRefExpr::VK_TLVP) { |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 334 | Asm.getContext().reportError( |
| 335 | Fixup.getLoc(), "TLVP symbol modifier should have been rip-rel"); |
| 336 | return; |
| 337 | } else if (Modifier != MCSymbolRefExpr::VK_None) { |
| 338 | Asm.getContext().reportError( |
| 339 | Fixup.getLoc(), "unsupported symbol modifier in relocation"); |
| 340 | return; |
| 341 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 342 | Type = MachO::X86_64_RELOC_UNSIGNED; |
Kevin Enderby | 7494675 | 2013-08-29 00:19:03 +0000 | [diff] [blame] | 343 | unsigned Kind = Fixup.getKind(); |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 344 | if (Kind == X86::reloc_signed_4byte) { |
| 345 | Asm.getContext().reportError( |
| 346 | Fixup.getLoc(), |
| 347 | "32-bit absolute addressing is not supported in 64-bit mode"); |
| 348 | return; |
| 349 | } |
Kevin Enderby | 7494675 | 2013-08-29 00:19:03 +0000 | [diff] [blame] | 350 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | // x86_64 always writes custom values into the fixups. |
| 355 | FixedValue = Value; |
| 356 | |
| 357 | // struct relocation_info (8 bytes) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 358 | MachO::any_relocation_info MRE; |
| 359 | MRE.r_word0 = FixupOffset; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 360 | MRE.r_word1 = (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | |
| 361 | (IsExtern << 27) | (Type << 28); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 362 | Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 365 | bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 366 | const MCAssembler &Asm, |
| 367 | const MCAsmLayout &Layout, |
| 368 | const MCFragment *Fragment, |
| 369 | const MCFixup &Fixup, |
| 370 | MCValue Target, |
| 371 | unsigned Log2Size, |
| 372 | uint64_t &FixedValue) { |
Kevin Enderby | 86496a4 | 2013-12-04 23:36:24 +0000 | [diff] [blame] | 373 | uint64_t OriginalFixedValue = FixedValue; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 374 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 375 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 376 | unsigned Type = MachO::GENERIC_RELOC_VANILLA; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 377 | |
| 378 | // See <reloc.h>. |
| 379 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 380 | |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 381 | if (!A->getFragment()) { |
| 382 | Asm.getContext().reportError( |
| 383 | Fixup.getLoc(), |
| 384 | "symbol '" + A->getName() + |
| 385 | "' can not be undefined in a subtraction expression"); |
| 386 | return false; |
| 387 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 388 | |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 389 | uint32_t Value = Writer->getSymbolAddress(*A, Layout); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 390 | uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 391 | FixedValue += SecAddr; |
| 392 | uint32_t Value2 = 0; |
| 393 | |
| 394 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 395 | const MCSymbol *SB = &B->getSymbol(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 396 | |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 397 | if (!SB->getFragment()) { |
| 398 | Asm.getContext().reportError( |
| 399 | Fixup.getLoc(), |
Rafael Espindola | d926ea2 | 2017-06-28 19:26:37 +0000 | [diff] [blame] | 400 | "symbol '" + SB->getName() + |
Tim Northover | 614e8ff | 2015-12-08 18:31:35 +0000 | [diff] [blame] | 401 | "' can not be undefined in a subtraction expression"); |
| 402 | return false; |
| 403 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 404 | |
| 405 | // Select the appropriate difference relocation type. |
| 406 | // |
| 407 | // Note that there is no longer any semantic difference between these two |
| 408 | // relocation types from the linkers point of view, this is done solely for |
| 409 | // pedantic compatibility with 'as'. |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 410 | Type = A->isExternal() ? (unsigned)MachO::GENERIC_RELOC_SECTDIFF |
| 411 | : (unsigned)MachO::GENERIC_RELOC_LOCAL_SECTDIFF; |
Rafael Espindola | d926ea2 | 2017-06-28 19:26:37 +0000 | [diff] [blame] | 412 | Value2 = Writer->getSymbolAddress(*SB, Layout); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 413 | FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // Relocations are written out in reverse order, so the PAIR comes first. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 417 | if (Type == MachO::GENERIC_RELOC_SECTDIFF || |
| 418 | Type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) { |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 419 | // If the offset is too large to fit in a scattered relocation, |
| 420 | // we're hosed. It's an unfortunate limitation of the MachO format. |
| 421 | if (FixupOffset > 0xffffff) { |
| 422 | char Buffer[32]; |
| 423 | format("0x%x", FixupOffset).print(Buffer, sizeof(Buffer)); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 424 | Asm.getContext().reportError(Fixup.getLoc(), |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 425 | Twine("Section too large, can't encode " |
| 426 | "r_address (") + Buffer + |
| 427 | ") into 24 bits of scattered " |
| 428 | "relocation entry."); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 429 | return false; |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 432 | MachO::any_relocation_info MRE; |
| 433 | MRE.r_word0 = ((0 << 0) | // r_address |
| 434 | (MachO::GENERIC_RELOC_PAIR << 24) | // r_type |
| 435 | (Log2Size << 28) | |
| 436 | (IsPCRel << 30) | |
| 437 | MachO::R_SCATTERED); |
| 438 | MRE.r_word1 = Value2; |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 439 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 440 | } else { |
| 441 | // If the offset is more than 24-bits, it won't fit in a scattered |
| 442 | // relocation offset field, so we fall back to using a non-scattered |
| 443 | // relocation. This is a bit risky, as if the offset reaches out of |
| 444 | // the block and the linker is doing scattered loading on this |
| 445 | // symbol, things can go badly. |
| 446 | // |
| 447 | // Required for 'as' compatibility. |
Kevin Enderby | 86496a4 | 2013-12-04 23:36:24 +0000 | [diff] [blame] | 448 | if (FixupOffset > 0xffffff) { |
| 449 | FixedValue = OriginalFixedValue; |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 450 | return false; |
Kevin Enderby | 86496a4 | 2013-12-04 23:36:24 +0000 | [diff] [blame] | 451 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 454 | MachO::any_relocation_info MRE; |
| 455 | MRE.r_word0 = ((FixupOffset << 0) | |
| 456 | (Type << 24) | |
| 457 | (Log2Size << 28) | |
| 458 | (IsPCRel << 30) | |
| 459 | MachO::R_SCATTERED); |
| 460 | MRE.r_word1 = Value; |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 461 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 462 | return true; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 465 | void X86MachObjectWriter::recordTLVPRelocation(MachObjectWriter *Writer, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 466 | const MCAssembler &Asm, |
| 467 | const MCAsmLayout &Layout, |
| 468 | const MCFragment *Fragment, |
| 469 | const MCFixup &Fixup, |
| 470 | MCValue Target, |
| 471 | uint64_t &FixedValue) { |
Rafael Espindola | d926ea2 | 2017-06-28 19:26:37 +0000 | [diff] [blame] | 472 | const MCSymbolRefExpr *SymA = Target.getSymA(); |
| 473 | assert(SymA->getKind() == MCSymbolRefExpr::VK_TLVP && !is64Bit() && |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 474 | "Should only be called with a 32-bit TLVP relocation!"); |
| 475 | |
| 476 | unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); |
| 477 | uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 478 | unsigned IsPCRel = 0; |
| 479 | |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 480 | // We're only going to have a second symbol in pic mode and it'll be a |
| 481 | // subtraction from the picbase. For 32-bit pic the addend is the difference |
| 482 | // between the picbase and the next address. For 32-bit static the addend is |
| 483 | // zero. |
Rafael Espindola | d926ea2 | 2017-06-28 19:26:37 +0000 | [diff] [blame] | 484 | if (auto *SymB = Target.getSymB()) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 485 | // If this is a subtraction then we're pcrel. |
| 486 | uint32_t FixupAddress = |
| 487 | Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 488 | IsPCRel = 1; |
Rafael Espindola | d926ea2 | 2017-06-28 19:26:37 +0000 | [diff] [blame] | 489 | FixedValue = FixupAddress - |
| 490 | Writer->getSymbolAddress(SymB->getSymbol(), Layout) + |
| 491 | Target.getConstant(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 492 | FixedValue += 1ULL << Log2Size; |
| 493 | } else { |
| 494 | FixedValue = 0; |
| 495 | } |
| 496 | |
| 497 | // struct relocation_info (8 bytes) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 498 | MachO::any_relocation_info MRE; |
| 499 | MRE.r_word0 = Value; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 500 | MRE.r_word1 = |
| 501 | (IsPCRel << 24) | (Log2Size << 25) | (MachO::GENERIC_RELOC_TLV << 28); |
Rafael Espindola | d926ea2 | 2017-06-28 19:26:37 +0000 | [diff] [blame] | 502 | Writer->addRelocation(&SymA->getSymbol(), Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer, |
| 506 | const MCAssembler &Asm, |
| 507 | const MCAsmLayout &Layout, |
| 508 | const MCFragment *Fragment, |
| 509 | const MCFixup &Fixup, |
| 510 | MCValue Target, |
| 511 | uint64_t &FixedValue) { |
| 512 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
| 513 | unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); |
| 514 | |
| 515 | // If this is a 32-bit TLVP reloc it's handled a bit differently. |
| 516 | if (Target.getSymA() && |
| 517 | Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) { |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 518 | recordTLVPRelocation(Writer, Asm, Layout, Fragment, Fixup, Target, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 519 | FixedValue); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | // If this is a difference or a defined symbol plus an offset, then we need a |
| 524 | // scattered relocation entry. Differences always require scattered |
| 525 | // relocations. |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 526 | if (Target.getSymB()) { |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 527 | recordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 528 | Target, Log2Size, FixedValue); |
| 529 | return; |
| 530 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 531 | |
| 532 | // Get the symbol data, if any. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 533 | const MCSymbol *A = nullptr; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 534 | if (Target.getSymA()) |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 535 | A = &Target.getSymA()->getSymbol(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 536 | |
| 537 | // If this is an internal relocation with an offset, it also needs a scattered |
| 538 | // relocation entry. |
| 539 | uint32_t Offset = Target.getConstant(); |
| 540 | if (IsPCRel) |
| 541 | Offset += 1 << Log2Size; |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 542 | // Try to record the scattered relocation if needed. Fall back to non |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 543 | // scattered if necessary (see comments in recordScatteredRelocation() |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 544 | // for details). |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 545 | if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A) && |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 546 | recordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, Target, |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 547 | Log2Size, FixedValue)) |
Jim Grosbach | c03a0c2 | 2012-09-26 21:27:45 +0000 | [diff] [blame] | 548 | return; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 549 | |
| 550 | // See <reloc.h>. |
| 551 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 552 | unsigned Index = 0; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 553 | unsigned Type = 0; |
Duncan P. N. Exon Smith | 6e23e5a | 2015-05-16 01:14:19 +0000 | [diff] [blame] | 554 | const MCSymbol *RelSymbol = nullptr; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 555 | |
| 556 | if (Target.isAbsolute()) { // constant |
| 557 | // SymbolNum of 0 indicates the absolute section. |
| 558 | // |
| 559 | // FIXME: Currently, these are never generated (see code below). I cannot |
| 560 | // find a case where they are actually emitted. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 561 | Type = MachO::GENERIC_RELOC_VANILLA; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 562 | } else { |
| 563 | // Resolve constant variables. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 564 | if (A->isVariable()) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 565 | int64_t Res; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 566 | if (A->getVariableValue()->evaluateAsAbsolute( |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 567 | Res, Layout, Writer->getSectionAddressMap())) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 568 | FixedValue = Res; |
| 569 | return; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // Check whether we need an external or internal relocation. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 574 | if (Writer->doesSymbolRequireExternRelocation(*A)) { |
| 575 | RelSymbol = A; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 576 | // For external relocations, make sure to offset the fixup value to |
| 577 | // compensate for the addend of the symbol address, if it was |
| 578 | // undefined. This occurs with weak definitions, for example. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 579 | if (!A->isUndefined()) |
| 580 | FixedValue -= Layout.getSymbolOffset(*A); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 581 | } else { |
| 582 | // The index is the section ordinal (1-based). |
Rafael Espindola | 6e6820a | 2015-05-25 14:12:48 +0000 | [diff] [blame] | 583 | const MCSection &Sec = A->getSection(); |
Rafael Espindola | 6e6820a | 2015-05-25 14:12:48 +0000 | [diff] [blame] | 584 | Index = Sec.getOrdinal() + 1; |
Rafael Espindola | 079027e | 2015-05-26 00:52:18 +0000 | [diff] [blame] | 585 | FixedValue += Writer->getSectionAddress(&Sec); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 586 | } |
| 587 | if (IsPCRel) |
Rafael Espindola | 079027e | 2015-05-26 00:52:18 +0000 | [diff] [blame] | 588 | FixedValue -= Writer->getSectionAddress(Fragment->getParent()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 589 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 590 | Type = MachO::GENERIC_RELOC_VANILLA; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // struct relocation_info (8 bytes) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 594 | MachO::any_relocation_info MRE; |
| 595 | MRE.r_word0 = FixupOffset; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 596 | MRE.r_word1 = |
| 597 | (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 598 | Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 601 | std::unique_ptr<MCObjectTargetWriter> |
| 602 | llvm::createX86MachObjectWriter(bool Is64Bit, uint32_t CPUType, |
| 603 | uint32_t CPUSubtype) { |
| 604 | return llvm::make_unique<X86MachObjectWriter>(Is64Bit, CPUType, CPUSubtype); |
Daniel Dunbar | 7da045e | 2010-12-20 15:07:39 +0000 | [diff] [blame] | 605 | } |