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