Jim Grosbach | 2354f87 | 2011-06-22 20:14:52 +0000 | [diff] [blame] | 1 | //===-- ARMMachObjectWriter.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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/ARMMCTargetDesc.h" |
Evan Cheng | ad5f485 | 2011-07-23 00:00:19 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/ARMBaseInfo.h" |
| 12 | #include "MCTargetDesc/ARMFixupKinds.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Twine.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmLayout.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAssembler.h" |
Jim Grosbach | 5e5eabb | 2012-01-26 23:20:15 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCFixup.h" |
| 19 | #include "llvm/MC/MCFixupKindInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCMachObjectWriter.h" |
Rafael Espindola | 6e6820a | 2015-05-25 14:12:48 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSection.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCValue.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MachO.h" |
Jim Grosbach | 2354f87 | 2011-06-22 20:14:52 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | class ARMMachObjectWriter : public MCMachObjectTargetWriter { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 29 | void RecordARMScatteredRelocation(MachObjectWriter *Writer, |
| 30 | const MCAssembler &Asm, |
| 31 | const MCAsmLayout &Layout, |
| 32 | const MCFragment *Fragment, |
| 33 | const MCFixup &Fixup, |
| 34 | MCValue Target, |
Tim Northover | 1bc367a | 2014-07-04 10:58:05 +0000 | [diff] [blame] | 35 | unsigned Type, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 36 | unsigned Log2Size, |
| 37 | uint64_t &FixedValue); |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 38 | void RecordARMScatteredHalfRelocation(MachObjectWriter *Writer, |
| 39 | const MCAssembler &Asm, |
| 40 | const MCAsmLayout &Layout, |
| 41 | const MCFragment *Fragment, |
| 42 | const MCFixup &Fixup, MCValue Target, |
| 43 | uint64_t &FixedValue); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 44 | |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 45 | bool requiresExternRelocation(MachObjectWriter *Writer, |
| 46 | const MCAssembler &Asm, |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 47 | const MCFragment &Fragment, unsigned RelocType, |
| 48 | const MCSymbol &S, uint64_t FixedValue); |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 49 | |
Jim Grosbach | 2354f87 | 2011-06-22 20:14:52 +0000 | [diff] [blame] | 50 | public: |
Jim Grosbach | 7c76b4c | 2015-06-04 20:27:42 +0000 | [diff] [blame] | 51 | ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype) |
| 52 | : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {} |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 53 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 54 | void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm, |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 55 | const MCAsmLayout &Layout, const MCFragment *Fragment, |
| 56 | const MCFixup &Fixup, MCValue Target, |
| 57 | uint64_t &FixedValue) override; |
Jim Grosbach | 2354f87 | 2011-06-22 20:14:52 +0000 | [diff] [blame] | 58 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 59 | } |
Jim Grosbach | 2354f87 | 2011-06-22 20:14:52 +0000 | [diff] [blame] | 60 | |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 61 | static bool getARMFixupKindMachOInfo(unsigned Kind, unsigned &RelocType, |
| 62 | unsigned &Log2Size) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 63 | RelocType = unsigned(MachO::ARM_RELOC_VANILLA); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 64 | Log2Size = ~0U; |
| 65 | |
| 66 | switch (Kind) { |
| 67 | default: |
| 68 | return false; |
| 69 | |
| 70 | case FK_Data_1: |
| 71 | Log2Size = llvm::Log2_32(1); |
| 72 | return true; |
| 73 | case FK_Data_2: |
| 74 | Log2Size = llvm::Log2_32(2); |
| 75 | return true; |
| 76 | case FK_Data_4: |
| 77 | Log2Size = llvm::Log2_32(4); |
| 78 | return true; |
| 79 | case FK_Data_8: |
| 80 | Log2Size = llvm::Log2_32(8); |
| 81 | return true; |
| 82 | |
Kevin Enderby | 651898c | 2013-12-13 22:46:54 +0000 | [diff] [blame] | 83 | // These fixups are expected to always be resolvable at assembly time and |
| 84 | // have no relocations supported. |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 85 | case ARM::fixup_arm_ldst_pcrel_12: |
| 86 | case ARM::fixup_arm_pcrel_10: |
| 87 | case ARM::fixup_arm_adr_pcrel_12: |
Tim Northover | 4233557 | 2015-04-06 18:44:42 +0000 | [diff] [blame] | 88 | case ARM::fixup_arm_thumb_br: |
Kevin Enderby | 651898c | 2013-12-13 22:46:54 +0000 | [diff] [blame] | 89 | return false; |
| 90 | |
| 91 | // Handle 24-bit branch kinds. |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 92 | case ARM::fixup_arm_condbranch: |
| 93 | case ARM::fixup_arm_uncondbranch: |
James Molloy | fb5cd60 | 2012-03-30 09:15:32 +0000 | [diff] [blame] | 94 | case ARM::fixup_arm_uncondbl: |
| 95 | case ARM::fixup_arm_condbl: |
Jim Grosbach | 7b811d3 | 2012-02-27 21:36:23 +0000 | [diff] [blame] | 96 | case ARM::fixup_arm_blx: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 97 | RelocType = unsigned(MachO::ARM_RELOC_BR24); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 98 | // Report as 'long', even though that is not quite accurate. |
| 99 | Log2Size = llvm::Log2_32(4); |
| 100 | return true; |
| 101 | |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 102 | case ARM::fixup_t2_uncondbranch: |
| 103 | case ARM::fixup_arm_thumb_bl: |
| 104 | case ARM::fixup_arm_thumb_blx: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 105 | RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 106 | Log2Size = llvm::Log2_32(4); |
| 107 | return true; |
| 108 | |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 109 | // For movw/movt r_type relocations they always have a pair following them and |
| 110 | // the r_length bits are used differently. The encoding of the r_length is as |
| 111 | // follows: |
| 112 | // low bit of r_length: |
| 113 | // 0 - :lower16: for movw instructions |
| 114 | // 1 - :upper16: for movt instructions |
| 115 | // high bit of r_length: |
| 116 | // 0 - arm instructions |
| 117 | // 1 - thumb instructions |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 118 | case ARM::fixup_arm_movt_hi16: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 119 | RelocType = unsigned(MachO::ARM_RELOC_HALF); |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 120 | Log2Size = 1; |
| 121 | return true; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 122 | case ARM::fixup_t2_movt_hi16: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 123 | RelocType = unsigned(MachO::ARM_RELOC_HALF); |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 124 | Log2Size = 3; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 125 | return true; |
| 126 | |
| 127 | case ARM::fixup_arm_movw_lo16: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 128 | RelocType = unsigned(MachO::ARM_RELOC_HALF); |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 129 | Log2Size = 0; |
| 130 | return true; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 131 | case ARM::fixup_t2_movw_lo16: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 132 | RelocType = unsigned(MachO::ARM_RELOC_HALF); |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 133 | Log2Size = 2; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void ARMMachObjectWriter:: |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 139 | RecordARMScatteredHalfRelocation(MachObjectWriter *Writer, |
| 140 | const MCAssembler &Asm, |
| 141 | const MCAsmLayout &Layout, |
| 142 | const MCFragment *Fragment, |
| 143 | const MCFixup &Fixup, |
| 144 | MCValue Target, |
| 145 | uint64_t &FixedValue) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 146 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 147 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 148 | unsigned Type = MachO::ARM_RELOC_HALF; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 149 | |
| 150 | // See <reloc.h>. |
| 151 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 152 | |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 153 | if (!A->getFragment()) { |
| 154 | Asm.getContext().reportError(Fixup.getLoc(), |
Jim Grosbach | 20275a8 | 2012-01-27 00:37:12 +0000 | [diff] [blame] | 155 | "symbol '" + A->getName() + |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 156 | "' can not be undefined in a subtraction expression"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 157 | return; |
| 158 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 159 | |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 160 | uint32_t Value = Writer->getSymbolAddress(*A, Layout); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 161 | uint32_t Value2 = 0; |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 162 | uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 163 | FixedValue += SecAddr; |
| 164 | |
| 165 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 166 | const MCSymbol *SB = &B->getSymbol(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 167 | |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 168 | if (!SB->getFragment()) { |
| 169 | Asm.getContext().reportError(Fixup.getLoc(), |
Jim Grosbach | 20275a8 | 2012-01-27 00:37:12 +0000 | [diff] [blame] | 170 | "symbol '" + B->getSymbol().getName() + |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 171 | "' can not be undefined in a subtraction expression"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 172 | return; |
| 173 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 174 | |
| 175 | // Select the appropriate difference relocation type. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 176 | Type = MachO::ARM_RELOC_HALF_SECTDIFF; |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 177 | Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 178 | FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Relocations are written out in reverse order, so the PAIR comes first. |
| 182 | // ARM_RELOC_HALF and ARM_RELOC_HALF_SECTDIFF abuse the r_length field: |
| 183 | // |
| 184 | // For these two r_type relocations they always have a pair following them and |
| 185 | // the r_length bits are used differently. The encoding of the r_length is as |
| 186 | // follows: |
| 187 | // low bit of r_length: |
| 188 | // 0 - :lower16: for movw instructions |
| 189 | // 1 - :upper16: for movt instructions |
| 190 | // high bit of r_length: |
| 191 | // 0 - arm instructions |
| 192 | // 1 - thumb instructions |
| 193 | // the other half of the relocated expression is in the following pair |
Sylvestre Ledru | 35521e2 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 194 | // relocation entry in the low 16 bits of r_address field. |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 195 | unsigned ThumbBit = 0; |
| 196 | unsigned MovtBit = 0; |
| 197 | switch ((unsigned)Fixup.getKind()) { |
| 198 | default: break; |
| 199 | case ARM::fixup_arm_movt_hi16: |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 200 | MovtBit = 1; |
Jim Grosbach | ae91322 | 2011-11-29 01:15:25 +0000 | [diff] [blame] | 201 | // The thumb bit shouldn't be set in the 'other-half' bit of the |
| 202 | // relocation, but it will be set in FixedValue if the base symbol |
| 203 | // is a thumb function. Clear it out here. |
Rafael Espindola | b60c829 | 2014-04-29 12:46:50 +0000 | [diff] [blame] | 204 | if (Asm.isThumbFunc(A)) |
Jim Grosbach | ae91322 | 2011-11-29 01:15:25 +0000 | [diff] [blame] | 205 | FixedValue &= 0xfffffffe; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 206 | break; |
| 207 | case ARM::fixup_t2_movt_hi16: |
Rafael Espindola | b60c829 | 2014-04-29 12:46:50 +0000 | [diff] [blame] | 208 | if (Asm.isThumbFunc(A)) |
Jim Grosbach | ae91322 | 2011-11-29 01:15:25 +0000 | [diff] [blame] | 209 | FixedValue &= 0xfffffffe; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 210 | MovtBit = 1; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 211 | LLVM_FALLTHROUGH; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 212 | case ARM::fixup_t2_movw_lo16: |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 213 | ThumbBit = 1; |
| 214 | break; |
| 215 | } |
| 216 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 217 | if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 218 | uint32_t OtherHalf = MovtBit |
| 219 | ? (FixedValue & 0xffff) : ((FixedValue & 0xffff0000) >> 16); |
| 220 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 221 | MachO::any_relocation_info MRE; |
| 222 | MRE.r_word0 = ((OtherHalf << 0) | |
| 223 | (MachO::ARM_RELOC_PAIR << 24) | |
| 224 | (MovtBit << 28) | |
| 225 | (ThumbBit << 29) | |
| 226 | (IsPCRel << 30) | |
| 227 | MachO::R_SCATTERED); |
| 228 | MRE.r_word1 = Value2; |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 229 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 232 | MachO::any_relocation_info MRE; |
| 233 | MRE.r_word0 = ((FixupOffset << 0) | |
| 234 | (Type << 24) | |
| 235 | (MovtBit << 28) | |
| 236 | (ThumbBit << 29) | |
| 237 | (IsPCRel << 30) | |
| 238 | MachO::R_SCATTERED); |
| 239 | MRE.r_word1 = Value; |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 240 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void ARMMachObjectWriter::RecordARMScatteredRelocation(MachObjectWriter *Writer, |
| 244 | const MCAssembler &Asm, |
| 245 | const MCAsmLayout &Layout, |
| 246 | const MCFragment *Fragment, |
| 247 | const MCFixup &Fixup, |
| 248 | MCValue Target, |
Tim Northover | 1bc367a | 2014-07-04 10:58:05 +0000 | [diff] [blame] | 249 | unsigned Type, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 250 | unsigned Log2Size, |
| 251 | uint64_t &FixedValue) { |
| 252 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 253 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 254 | |
| 255 | // See <reloc.h>. |
| 256 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 257 | |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 258 | if (!A->getFragment()) { |
| 259 | Asm.getContext().reportError(Fixup.getLoc(), |
Jim Grosbach | 20275a8 | 2012-01-27 00:37:12 +0000 | [diff] [blame] | 260 | "symbol '" + A->getName() + |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 261 | "' can not be undefined in a subtraction expression"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 262 | return; |
| 263 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 264 | |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 265 | uint32_t Value = Writer->getSymbolAddress(*A, Layout); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 266 | uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 267 | FixedValue += SecAddr; |
| 268 | uint32_t Value2 = 0; |
| 269 | |
| 270 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
Tim Northover | 1bc367a | 2014-07-04 10:58:05 +0000 | [diff] [blame] | 271 | assert(Type == MachO::ARM_RELOC_VANILLA && "invalid reloc for 2 symbols"); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 272 | const MCSymbol *SB = &B->getSymbol(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 273 | |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 274 | if (!SB->getFragment()) { |
| 275 | Asm.getContext().reportError(Fixup.getLoc(), |
Jim Grosbach | 20275a8 | 2012-01-27 00:37:12 +0000 | [diff] [blame] | 276 | "symbol '" + B->getSymbol().getName() + |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 277 | "' can not be undefined in a subtraction expression"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 278 | return; |
| 279 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 280 | |
| 281 | // Select the appropriate difference relocation type. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 282 | Type = MachO::ARM_RELOC_SECTDIFF; |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 283 | Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 284 | FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // Relocations are written out in reverse order, so the PAIR comes first. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 288 | if (Type == MachO::ARM_RELOC_SECTDIFF || |
| 289 | Type == MachO::ARM_RELOC_LOCAL_SECTDIFF) { |
| 290 | MachO::any_relocation_info MRE; |
| 291 | MRE.r_word0 = ((0 << 0) | |
| 292 | (MachO::ARM_RELOC_PAIR << 24) | |
| 293 | (Log2Size << 28) | |
| 294 | (IsPCRel << 30) | |
| 295 | MachO::R_SCATTERED); |
| 296 | MRE.r_word1 = Value2; |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 297 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 300 | MachO::any_relocation_info MRE; |
| 301 | MRE.r_word0 = ((FixupOffset << 0) | |
| 302 | (Type << 24) | |
| 303 | (Log2Size << 28) | |
| 304 | (IsPCRel << 30) | |
| 305 | MachO::R_SCATTERED); |
| 306 | MRE.r_word1 = Value; |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 307 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 310 | bool ARMMachObjectWriter::requiresExternRelocation(MachObjectWriter *Writer, |
| 311 | const MCAssembler &Asm, |
| 312 | const MCFragment &Fragment, |
| 313 | unsigned RelocType, |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 314 | const MCSymbol &S, |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 315 | uint64_t FixedValue) { |
| 316 | // Most cases can be identified purely from the symbol. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 317 | if (Writer->doesSymbolRequireExternRelocation(S)) |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 318 | return true; |
| 319 | int64_t Value = (int64_t)FixedValue; // The displacement is signed. |
| 320 | int64_t Range; |
| 321 | switch (RelocType) { |
| 322 | default: |
| 323 | return false; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 324 | case MachO::ARM_RELOC_BR24: |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 325 | // PC pre-adjustment of 8 for these instructions. |
| 326 | Value -= 8; |
| 327 | // ARM BL/BLX has a 25-bit offset. |
| 328 | Range = 0x1ffffff; |
| 329 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 330 | case MachO::ARM_THUMB_RELOC_BR22: |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 331 | // PC pre-adjustment of 4 for these instructions. |
| 332 | Value -= 4; |
| 333 | // Thumb BL/BLX has a 24-bit offset. |
| 334 | Range = 0xffffff; |
| 335 | } |
| 336 | // BL/BLX also use external relocations when an internal relocation |
| 337 | // would result in the target being out of range. This gives the linker |
| 338 | // enough information to generate a branch island. |
Rafael Espindola | 079027e | 2015-05-26 00:52:18 +0000 | [diff] [blame] | 339 | Value += Writer->getSectionAddress(&S.getSection()); |
| 340 | Value -= Writer->getSectionAddress(Fragment.getParent()); |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 341 | // If the resultant value would be out of range for an internal relocation, |
| 342 | // use an external instead. |
| 343 | if (Value > Range || Value < -(Range + 1)) |
| 344 | return true; |
| 345 | return false; |
| 346 | } |
| 347 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 348 | void ARMMachObjectWriter::recordRelocation(MachObjectWriter *Writer, |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 349 | MCAssembler &Asm, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 350 | const MCAsmLayout &Layout, |
| 351 | const MCFragment *Fragment, |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 352 | const MCFixup &Fixup, MCValue Target, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 353 | uint64_t &FixedValue) { |
| 354 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
| 355 | unsigned Log2Size; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 356 | unsigned RelocType = MachO::ARM_RELOC_VANILLA; |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 357 | if (!getARMFixupKindMachOInfo(Fixup.getKind(), RelocType, Log2Size)) { |
Jim Grosbach | 5e5eabb | 2012-01-26 23:20:15 +0000 | [diff] [blame] | 358 | // If we failed to get fixup kind info, it's because there's no legal |
| 359 | // relocation type for the fixup kind. This happens when it's a fixup that's |
| 360 | // expected to always be resolvable at assembly time and not have any |
| 361 | // relocations needed. |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 362 | Asm.getContext().reportError(Fixup.getLoc(), |
| 363 | "unsupported relocation on symbol"); |
| 364 | return; |
| 365 | } |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 366 | |
| 367 | // If this is a difference or a defined symbol plus an offset, then we need a |
| 368 | // scattered relocation entry. Differences always require scattered |
| 369 | // relocations. |
| 370 | if (Target.getSymB()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 371 | if (RelocType == MachO::ARM_RELOC_HALF) |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 372 | return RecordARMScatteredHalfRelocation(Writer, Asm, Layout, Fragment, |
| 373 | Fixup, Target, FixedValue); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 374 | return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, |
Tim Northover | 1bc367a | 2014-07-04 10:58:05 +0000 | [diff] [blame] | 375 | Target, RelocType, Log2Size, |
| 376 | FixedValue); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // Get the symbol data, if any. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 380 | const MCSymbol *A = nullptr; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 381 | if (Target.getSymA()) |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 382 | A = &Target.getSymA()->getSymbol(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 383 | |
| 384 | // FIXME: For other platforms, we need to use scattered relocations for |
| 385 | // internal relocations with offsets. If this is an internal relocation with |
| 386 | // an offset, it also needs a scattered relocation entry. |
| 387 | // |
| 388 | // Is this right for ARM? |
| 389 | uint32_t Offset = Target.getConstant(); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 390 | if (IsPCRel && RelocType == MachO::ARM_RELOC_VANILLA) |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 391 | Offset += 1 << Log2Size; |
Tim Northover | b9edc5c | 2016-02-23 20:20:23 +0000 | [diff] [blame] | 392 | if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A) && |
| 393 | RelocType != MachO::ARM_RELOC_HALF) |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 394 | return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, |
Tim Northover | 1bc367a | 2014-07-04 10:58:05 +0000 | [diff] [blame] | 395 | Target, RelocType, Log2Size, |
| 396 | FixedValue); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 397 | |
| 398 | // See <reloc.h>. |
| 399 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
| 400 | unsigned Index = 0; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 401 | unsigned Type = 0; |
Duncan P. N. Exon Smith | 6e23e5a | 2015-05-16 01:14:19 +0000 | [diff] [blame] | 402 | const MCSymbol *RelSymbol = nullptr; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 403 | |
| 404 | if (Target.isAbsolute()) { // constant |
| 405 | // FIXME! |
| 406 | report_fatal_error("FIXME: relocations to absolute targets " |
| 407 | "not yet implemented"); |
| 408 | } else { |
| 409 | // Resolve constant variables. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 410 | if (A->isVariable()) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 411 | int64_t Res; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 412 | if (A->getVariableValue()->evaluateAsAbsolute( |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 413 | Res, Layout, Writer->getSectionAddressMap())) { |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 414 | FixedValue = Res; |
| 415 | return; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Check whether we need an external or internal relocation. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 420 | if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, *A, |
Jim Grosbach | df8ed71 | 2012-09-25 18:07:17 +0000 | [diff] [blame] | 421 | FixedValue)) { |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 422 | RelSymbol = A; |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 423 | |
| 424 | // For external relocations, make sure to offset the fixup value to |
| 425 | // compensate for the addend of the symbol address, if it was |
| 426 | // undefined. This occurs with weak definitions, for example. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 427 | if (!A->isUndefined()) |
| 428 | FixedValue -= Layout.getSymbolOffset(*A); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 429 | } else { |
| 430 | // The index is the section ordinal (1-based). |
Rafael Espindola | 6e6820a | 2015-05-25 14:12:48 +0000 | [diff] [blame] | 431 | const MCSection &Sec = A->getSection(); |
Rafael Espindola | 6e6820a | 2015-05-25 14:12:48 +0000 | [diff] [blame] | 432 | Index = Sec.getOrdinal() + 1; |
Rafael Espindola | 079027e | 2015-05-26 00:52:18 +0000 | [diff] [blame] | 433 | FixedValue += Writer->getSectionAddress(&Sec); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 434 | } |
| 435 | if (IsPCRel) |
Rafael Espindola | 079027e | 2015-05-26 00:52:18 +0000 | [diff] [blame] | 436 | FixedValue -= Writer->getSectionAddress(Fragment->getParent()); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 437 | |
| 438 | // The type is determined by the fixup kind. |
| 439 | Type = RelocType; |
| 440 | } |
| 441 | |
| 442 | // struct relocation_info (8 bytes) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 443 | MachO::any_relocation_info MRE; |
| 444 | MRE.r_word0 = FixupOffset; |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 445 | MRE.r_word1 = |
| 446 | (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28); |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 447 | |
| 448 | // Even when it's not a scattered relocation, movw/movt always uses |
| 449 | // a PAIR relocation. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 450 | if (Type == MachO::ARM_RELOC_HALF) { |
Tim Northover | b9edc5c | 2016-02-23 20:20:23 +0000 | [diff] [blame] | 451 | // The entire addend is needed to correctly apply a relocation. One half is |
| 452 | // extracted from the instruction itself, the other comes from this |
| 453 | // PAIR. I.e. it's correct that we insert the high bits of the addend in the |
| 454 | // MOVW case here. relocation entries. |
Dmitri Gribenko | ca1e27b | 2012-09-10 21:26:47 +0000 | [diff] [blame] | 455 | uint32_t Value = 0; |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 456 | switch ((unsigned)Fixup.getKind()) { |
| 457 | default: break; |
Kevin Enderby | 5c490f1 | 2012-07-30 18:46:15 +0000 | [diff] [blame] | 458 | case ARM::fixup_arm_movw_lo16: |
Kevin Enderby | 5c490f1 | 2012-07-30 18:46:15 +0000 | [diff] [blame] | 459 | case ARM::fixup_t2_movw_lo16: |
Kevin Enderby | 5c490f1 | 2012-07-30 18:46:15 +0000 | [diff] [blame] | 460 | Value = (FixedValue >> 16) & 0xffff; |
| 461 | break; |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 462 | case ARM::fixup_arm_movt_hi16: |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 463 | case ARM::fixup_t2_movt_hi16: |
Kevin Enderby | 5c490f1 | 2012-07-30 18:46:15 +0000 | [diff] [blame] | 464 | Value = FixedValue & 0xffff; |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 467 | MachO::any_relocation_info MREPair; |
| 468 | MREPair.r_word0 = Value; |
| 469 | MREPair.r_word1 = ((0xffffff << 0) | |
| 470 | (Log2Size << 25) | |
| 471 | (MachO::ARM_RELOC_PAIR << 28)); |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 472 | |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 473 | Writer->addRelocation(nullptr, Fragment->getParent(), MREPair); |
Jim Grosbach | 997614f | 2012-03-20 17:25:45 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 476 | Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 479 | MCObjectWriter *llvm::createARMMachObjectWriter(raw_pwrite_stream &OS, |
| 480 | bool Is64Bit, uint32_t CPUType, |
Jim Grosbach | 2354f87 | 2011-06-22 20:14:52 +0000 | [diff] [blame] | 481 | uint32_t CPUSubtype) { |
| 482 | return createMachObjectWriter(new ARMMachObjectWriter(Is64Bit, |
| 483 | CPUType, |
| 484 | CPUSubtype), |
| 485 | OS, /*IsLittleEndian=*/true); |
| 486 | } |