Rafael Espindola | 4449b21 | 2011-12-22 01:06:53 +0000 | [diff] [blame] | 1 | //===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===// |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/ARMMCTargetDesc.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/ARMFixupKinds.h" |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Statistic.h" |
| 13 | #include "llvm/ADT/StringSwitch.h" |
| 14 | #include "llvm/MC/MCELFObjectWriter.h" |
| 15 | #include "llvm/MC/MCExpr.h" |
| 16 | #include "llvm/MC/MCSectionELF.h" |
| 17 | #include "llvm/MC/MCValue.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | class ARMELFObjectWriter : public MCELFObjectTargetWriter { |
| 26 | enum { DefaultEABIVersion = 0x05000000U }; |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 27 | unsigned GetRelocTypeInner(const MCValue &Target, |
| 28 | const MCFixup &Fixup, |
| 29 | bool IsPCRel) const; |
| 30 | |
| 31 | |
| 32 | public: |
| 33 | ARMELFObjectWriter(uint8_t OSABI); |
| 34 | |
| 35 | virtual ~ARMELFObjectWriter(); |
Rafael Espindola | 84d00f1 | 2011-12-22 02:58:12 +0000 | [diff] [blame] | 36 | |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 37 | virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, |
| 38 | bool IsPCRel, bool IsRelocWithSymbol, |
| 39 | int64_t Addend) const; |
Rafael Espindola | 84d00f1 | 2011-12-22 02:58:12 +0000 | [diff] [blame] | 40 | virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm, |
| 41 | const MCValue &Target, |
| 42 | const MCFragment &F, |
| 43 | const MCFixup &Fixup, |
| 44 | bool IsPCRel) const; |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 45 | }; |
| 46 | } |
| 47 | |
| 48 | ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI) |
| 49 | : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, |
| 50 | ELF::EM_ARM, |
| 51 | /*HasRelocationAddend*/ false) {} |
| 52 | |
| 53 | ARMELFObjectWriter::~ARMELFObjectWriter() {} |
| 54 | |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 55 | // In ARM, _MergedGlobals and other most symbols get emitted directly. |
| 56 | // I.e. not as an offset to a section symbol. |
| 57 | // This code is an approximation of what ARM/gcc does. |
| 58 | |
| 59 | STATISTIC(PCRelCount, "Total number of PIC Relocations"); |
| 60 | STATISTIC(NonPCRelCount, "Total number of non-PIC relocations"); |
| 61 | |
| 62 | const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm, |
| 63 | const MCValue &Target, |
| 64 | const MCFragment &F, |
| 65 | const MCFixup &Fixup, |
| 66 | bool IsPCRel) const { |
James Molloy | b47489d | 2012-01-28 15:58:32 +0000 | [diff] [blame] | 67 | const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol(); |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 68 | bool EmitThisSym = false; |
| 69 | |
| 70 | const MCSectionELF &Section = |
| 71 | static_cast<const MCSectionELF&>(Symbol.getSection()); |
| 72 | bool InNormalSection = true; |
| 73 | unsigned RelocType = 0; |
| 74 | RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel); |
| 75 | |
| 76 | DEBUG( |
| 77 | const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind(); |
| 78 | MCSymbolRefExpr::VariantKind Kind2; |
| 79 | Kind2 = Target.getSymB() ? Target.getSymB()->getKind() : |
| 80 | MCSymbolRefExpr::VK_None; |
| 81 | dbgs() << "considering symbol " |
| 82 | << Section.getSectionName() << "/" |
| 83 | << Symbol.getName() << "/" |
| 84 | << " Rel:" << (unsigned)RelocType |
| 85 | << " Kind: " << (int)Kind << "/" << (int)Kind2 |
| 86 | << " Tmp:" |
| 87 | << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/" |
| 88 | << Symbol.isVariable() << "/" << Symbol.isTemporary() |
| 89 | << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n"); |
| 90 | |
| 91 | if (IsPCRel) { ++PCRelCount; |
| 92 | switch (RelocType) { |
| 93 | default: |
| 94 | // Most relocation types are emitted as explicit symbols |
| 95 | InNormalSection = |
| 96 | StringSwitch<bool>(Section.getSectionName()) |
| 97 | .Case(".data.rel.ro.local", false) |
| 98 | .Case(".data.rel", false) |
| 99 | .Case(".bss", false) |
| 100 | .Default(true); |
| 101 | EmitThisSym = true; |
| 102 | break; |
| 103 | case ELF::R_ARM_ABS32: |
| 104 | // But things get strange with R_ARM_ABS32 |
| 105 | // In this case, most things that go in .rodata show up |
| 106 | // as section relative relocations |
| 107 | InNormalSection = |
| 108 | StringSwitch<bool>(Section.getSectionName()) |
| 109 | .Case(".data.rel.ro.local", false) |
| 110 | .Case(".data.rel", false) |
| 111 | .Case(".rodata", false) |
| 112 | .Case(".bss", false) |
| 113 | .Default(true); |
| 114 | EmitThisSym = false; |
| 115 | break; |
| 116 | } |
| 117 | } else { |
| 118 | NonPCRelCount++; |
| 119 | InNormalSection = |
| 120 | StringSwitch<bool>(Section.getSectionName()) |
| 121 | .Case(".data.rel.ro.local", false) |
| 122 | .Case(".rodata", false) |
| 123 | .Case(".data.rel", false) |
| 124 | .Case(".bss", false) |
| 125 | .Default(true); |
| 126 | |
| 127 | switch (RelocType) { |
| 128 | default: EmitThisSym = true; break; |
| 129 | case ELF::R_ARM_ABS32: EmitThisSym = false; break; |
Logan Chien | 4dd14fb | 2012-12-12 07:14:46 +0000 | [diff] [blame] | 130 | case ELF::R_ARM_PREL31: EmitThisSym = false; break; |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | if (EmitThisSym) |
| 135 | return &Symbol; |
| 136 | if (! Symbol.isTemporary() && InNormalSection) { |
| 137 | return &Symbol; |
| 138 | } |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | // Need to examine the Fixup when determining whether to |
| 143 | // emit the relocation as an explicit symbol or as a section relative |
| 144 | // offset |
| 145 | unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, |
| 146 | const MCFixup &Fixup, |
| 147 | bool IsPCRel, |
| 148 | bool IsRelocWithSymbol, |
| 149 | int64_t Addend) const { |
| 150 | return GetRelocTypeInner(Target, Fixup, IsPCRel); |
| 151 | } |
| 152 | |
| 153 | unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target, |
| 154 | const MCFixup &Fixup, |
| 155 | bool IsPCRel) const { |
| 156 | MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? |
| 157 | MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); |
| 158 | |
| 159 | unsigned Type = 0; |
| 160 | if (IsPCRel) { |
| 161 | switch ((unsigned)Fixup.getKind()) { |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 162 | default: llvm_unreachable("Unimplemented"); |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 163 | case FK_Data_4: |
| 164 | switch (Modifier) { |
| 165 | default: llvm_unreachable("Unsupported Modifier"); |
| 166 | case MCSymbolRefExpr::VK_None: |
| 167 | Type = ELF::R_ARM_REL32; |
| 168 | break; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 169 | case MCSymbolRefExpr::VK_TLSGD: |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 170 | llvm_unreachable("unimplemented"); |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 171 | case MCSymbolRefExpr::VK_GOTTPOFF: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 172 | Type = ELF::R_ARM_TLS_IE32; |
| 173 | break; |
| 174 | } |
| 175 | break; |
Jim Grosbach | 7b811d3 | 2012-02-27 21:36:23 +0000 | [diff] [blame] | 176 | case ARM::fixup_arm_blx: |
Jan Wen Voung | 7f5d79f | 2012-06-19 16:03:02 +0000 | [diff] [blame] | 177 | case ARM::fixup_arm_uncondbl: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 178 | switch (Modifier) { |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 179 | case MCSymbolRefExpr::VK_PLT: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 180 | Type = ELF::R_ARM_PLT32; |
| 181 | break; |
Saleem Abdulrasool | 6e00ca8 | 2014-01-30 04:02:31 +0000 | [diff] [blame] | 182 | case MCSymbolRefExpr::VK_ARM_TLSCALL: |
| 183 | Type = ELF::R_ARM_TLS_CALL; |
| 184 | break; |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 185 | default: |
| 186 | Type = ELF::R_ARM_CALL; |
| 187 | break; |
| 188 | } |
| 189 | break; |
James Molloy | fb5cd60 | 2012-03-30 09:15:32 +0000 | [diff] [blame] | 190 | case ARM::fixup_arm_condbl: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 191 | case ARM::fixup_arm_condbranch: |
Jan Wen Voung | 7f5d79f | 2012-06-19 16:03:02 +0000 | [diff] [blame] | 192 | case ARM::fixup_arm_uncondbranch: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 193 | Type = ELF::R_ARM_JUMP24; |
| 194 | break; |
Logan Chien | cea0354 | 2012-09-01 15:06:36 +0000 | [diff] [blame] | 195 | case ARM::fixup_t2_condbranch: |
| 196 | case ARM::fixup_t2_uncondbranch: |
| 197 | Type = ELF::R_ARM_THM_JUMP24; |
| 198 | break; |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 199 | case ARM::fixup_arm_movt_hi16: |
| 200 | case ARM::fixup_arm_movt_hi16_pcrel: |
| 201 | Type = ELF::R_ARM_MOVT_PREL; |
| 202 | break; |
| 203 | case ARM::fixup_arm_movw_lo16: |
| 204 | case ARM::fixup_arm_movw_lo16_pcrel: |
| 205 | Type = ELF::R_ARM_MOVW_PREL_NC; |
| 206 | break; |
| 207 | case ARM::fixup_t2_movt_hi16: |
| 208 | case ARM::fixup_t2_movt_hi16_pcrel: |
| 209 | Type = ELF::R_ARM_THM_MOVT_PREL; |
| 210 | break; |
| 211 | case ARM::fixup_t2_movw_lo16: |
| 212 | case ARM::fixup_t2_movw_lo16_pcrel: |
| 213 | Type = ELF::R_ARM_THM_MOVW_PREL_NC; |
| 214 | break; |
| 215 | case ARM::fixup_arm_thumb_bl: |
| 216 | case ARM::fixup_arm_thumb_blx: |
Saleem Abdulrasool | 6e00ca8 | 2014-01-30 04:02:31 +0000 | [diff] [blame] | 217 | switch (Modifier) { |
| 218 | case MCSymbolRefExpr::VK_ARM_TLSCALL: |
| 219 | Type = ELF::R_ARM_THM_TLS_CALL; |
| 220 | break; |
| 221 | default: |
| 222 | Type = ELF::R_ARM_THM_CALL; |
| 223 | break; |
| 224 | } |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | } else { |
| 228 | switch ((unsigned)Fixup.getKind()) { |
| 229 | default: llvm_unreachable("invalid fixup kind!"); |
| 230 | case FK_Data_4: |
| 231 | switch (Modifier) { |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 232 | default: llvm_unreachable("Unsupported Modifier"); |
Logan Chien | 4dd14fb | 2012-12-12 07:14:46 +0000 | [diff] [blame] | 233 | case MCSymbolRefExpr::VK_ARM_NONE: |
| 234 | Type = ELF::R_ARM_NONE; |
| 235 | break; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 236 | case MCSymbolRefExpr::VK_GOT: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 237 | Type = ELF::R_ARM_GOT_BREL; |
| 238 | break; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 239 | case MCSymbolRefExpr::VK_TLSGD: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 240 | Type = ELF::R_ARM_TLS_GD32; |
| 241 | break; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 242 | case MCSymbolRefExpr::VK_TPOFF: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 243 | Type = ELF::R_ARM_TLS_LE32; |
| 244 | break; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 245 | case MCSymbolRefExpr::VK_GOTTPOFF: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 246 | Type = ELF::R_ARM_TLS_IE32; |
| 247 | break; |
| 248 | case MCSymbolRefExpr::VK_None: |
| 249 | Type = ELF::R_ARM_ABS32; |
| 250 | break; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 251 | case MCSymbolRefExpr::VK_GOTOFF: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 252 | Type = ELF::R_ARM_GOTOFF32; |
| 253 | break; |
James Molloy | 6685c08 | 2012-01-26 09:25:43 +0000 | [diff] [blame] | 254 | case MCSymbolRefExpr::VK_ARM_TARGET1: |
| 255 | Type = ELF::R_ARM_TARGET1; |
| 256 | break; |
Anton Korobeynikov | a305ea5 | 2012-11-09 20:20:12 +0000 | [diff] [blame] | 257 | case MCSymbolRefExpr::VK_ARM_TARGET2: |
| 258 | Type = ELF::R_ARM_TARGET2; |
| 259 | break; |
Logan Chien | 4dd14fb | 2012-12-12 07:14:46 +0000 | [diff] [blame] | 260 | case MCSymbolRefExpr::VK_ARM_PREL31: |
| 261 | Type = ELF::R_ARM_PREL31; |
| 262 | break; |
Kai Nacke | e51c813 | 2014-01-20 11:00:40 +0000 | [diff] [blame] | 263 | case MCSymbolRefExpr::VK_ARM_TLSLDO: |
| 264 | Type = ELF::R_ARM_TLS_LDO32; |
| 265 | break; |
Saleem Abdulrasool | 6e00ca8 | 2014-01-30 04:02:31 +0000 | [diff] [blame] | 266 | case MCSymbolRefExpr::VK_ARM_TLSCALL: |
| 267 | Type = ELF::R_ARM_TLS_CALL; |
| 268 | break; |
Saleem Abdulrasool | a3f12bd | 2014-01-30 04:02:38 +0000 | [diff] [blame^] | 269 | case MCSymbolRefExpr::VK_ARM_TLSDESC: |
| 270 | Type = ELF::R_ARM_TLS_GOTDESC; |
| 271 | break; |
Logan Chien | 4dd14fb | 2012-12-12 07:14:46 +0000 | [diff] [blame] | 272 | } |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 273 | break; |
| 274 | case ARM::fixup_arm_ldst_pcrel_12: |
| 275 | case ARM::fixup_arm_pcrel_10: |
| 276 | case ARM::fixup_arm_adr_pcrel_12: |
| 277 | case ARM::fixup_arm_thumb_bl: |
| 278 | case ARM::fixup_arm_thumb_cb: |
| 279 | case ARM::fixup_arm_thumb_cp: |
| 280 | case ARM::fixup_arm_thumb_br: |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 281 | llvm_unreachable("Unimplemented"); |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 282 | case ARM::fixup_arm_condbranch: |
Jan Wen Voung | 7f5d79f | 2012-06-19 16:03:02 +0000 | [diff] [blame] | 283 | case ARM::fixup_arm_uncondbranch: |
Rafael Espindola | a012405 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 284 | Type = ELF::R_ARM_JUMP24; |
| 285 | break; |
| 286 | case ARM::fixup_arm_movt_hi16: |
| 287 | Type = ELF::R_ARM_MOVT_ABS; |
| 288 | break; |
| 289 | case ARM::fixup_arm_movw_lo16: |
| 290 | Type = ELF::R_ARM_MOVW_ABS_NC; |
| 291 | break; |
| 292 | case ARM::fixup_t2_movt_hi16: |
| 293 | Type = ELF::R_ARM_THM_MOVT_ABS; |
| 294 | break; |
| 295 | case ARM::fixup_t2_movw_lo16: |
| 296 | Type = ELF::R_ARM_THM_MOVW_ABS_NC; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return Type; |
| 302 | } |
| 303 | |
| 304 | MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS, |
| 305 | uint8_t OSABI) { |
| 306 | MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI); |
| 307 | return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true); |
| 308 | } |