Rafael Espindola | 7609785 | 2011-12-22 01:06:53 +0000 | [diff] [blame] | 1 | //===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===// |
Rafael Espindola | 69bbda0 | 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 | |
| 10 | #include "MCTargetDesc/ARMFixupKinds.h" |
| 11 | #include "MCTargetDesc/ARMMCTargetDesc.h" |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "llvm/Support/ErrorHandling.h" |
| 14 | #include "llvm/ADT/Statistic.h" |
| 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | #include "llvm/MC/MCELFObjectWriter.h" |
| 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCSectionELF.h" |
| 19 | #include "llvm/MC/MCValue.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | class ARMELFObjectWriter : public MCELFObjectTargetWriter { |
| 25 | enum { DefaultEABIVersion = 0x05000000U }; |
Rafael Espindola | 69bbda0 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 26 | unsigned GetRelocTypeInner(const MCValue &Target, |
| 27 | const MCFixup &Fixup, |
| 28 | bool IsPCRel) const; |
| 29 | |
| 30 | |
| 31 | public: |
| 32 | ARMELFObjectWriter(uint8_t OSABI); |
| 33 | |
| 34 | virtual ~ARMELFObjectWriter(); |
Rafael Espindola | 6db2d926 | 2011-12-22 02:58:12 +0000 | [diff] [blame] | 35 | |
Rafael Espindola | 69bbda0 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 36 | virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, |
| 37 | bool IsPCRel, bool IsRelocWithSymbol, |
| 38 | int64_t Addend) const; |
| 39 | virtual unsigned getEFlags() const; |
Rafael Espindola | 6db2d926 | 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 | 69bbda0 | 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 | |
| 55 | // FIXME: get the real EABI Version from the Triple. |
| 56 | unsigned ARMELFObjectWriter::getEFlags() const { |
| 57 | return ELF::EF_ARM_EABIMASK & DefaultEABIVersion; |
| 58 | } |
| 59 | |
| 60 | // In ARM, _MergedGlobals and other most symbols get emitted directly. |
| 61 | // I.e. not as an offset to a section symbol. |
| 62 | // This code is an approximation of what ARM/gcc does. |
| 63 | |
| 64 | STATISTIC(PCRelCount, "Total number of PIC Relocations"); |
| 65 | STATISTIC(NonPCRelCount, "Total number of non-PIC relocations"); |
| 66 | |
| 67 | const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm, |
| 68 | const MCValue &Target, |
| 69 | const MCFragment &F, |
| 70 | const MCFixup &Fixup, |
| 71 | bool IsPCRel) const { |
| 72 | const MCSymbol &Symbol = Target.getSymA()->getSymbol(); |
| 73 | bool EmitThisSym = false; |
| 74 | |
| 75 | const MCSectionELF &Section = |
| 76 | static_cast<const MCSectionELF&>(Symbol.getSection()); |
| 77 | bool InNormalSection = true; |
| 78 | unsigned RelocType = 0; |
| 79 | RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel); |
| 80 | |
| 81 | DEBUG( |
| 82 | const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind(); |
| 83 | MCSymbolRefExpr::VariantKind Kind2; |
| 84 | Kind2 = Target.getSymB() ? Target.getSymB()->getKind() : |
| 85 | MCSymbolRefExpr::VK_None; |
| 86 | dbgs() << "considering symbol " |
| 87 | << Section.getSectionName() << "/" |
| 88 | << Symbol.getName() << "/" |
| 89 | << " Rel:" << (unsigned)RelocType |
| 90 | << " Kind: " << (int)Kind << "/" << (int)Kind2 |
| 91 | << " Tmp:" |
| 92 | << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/" |
| 93 | << Symbol.isVariable() << "/" << Symbol.isTemporary() |
| 94 | << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n"); |
| 95 | |
| 96 | if (IsPCRel) { ++PCRelCount; |
| 97 | switch (RelocType) { |
| 98 | default: |
| 99 | // Most relocation types are emitted as explicit symbols |
| 100 | InNormalSection = |
| 101 | StringSwitch<bool>(Section.getSectionName()) |
| 102 | .Case(".data.rel.ro.local", false) |
| 103 | .Case(".data.rel", false) |
| 104 | .Case(".bss", false) |
| 105 | .Default(true); |
| 106 | EmitThisSym = true; |
| 107 | break; |
| 108 | case ELF::R_ARM_ABS32: |
| 109 | // But things get strange with R_ARM_ABS32 |
| 110 | // In this case, most things that go in .rodata show up |
| 111 | // as section relative relocations |
| 112 | InNormalSection = |
| 113 | StringSwitch<bool>(Section.getSectionName()) |
| 114 | .Case(".data.rel.ro.local", false) |
| 115 | .Case(".data.rel", false) |
| 116 | .Case(".rodata", false) |
| 117 | .Case(".bss", false) |
| 118 | .Default(true); |
| 119 | EmitThisSym = false; |
| 120 | break; |
| 121 | } |
| 122 | } else { |
| 123 | NonPCRelCount++; |
| 124 | InNormalSection = |
| 125 | StringSwitch<bool>(Section.getSectionName()) |
| 126 | .Case(".data.rel.ro.local", false) |
| 127 | .Case(".rodata", false) |
| 128 | .Case(".data.rel", false) |
| 129 | .Case(".bss", false) |
| 130 | .Default(true); |
| 131 | |
| 132 | switch (RelocType) { |
| 133 | default: EmitThisSym = true; break; |
| 134 | case ELF::R_ARM_ABS32: EmitThisSym = false; break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (EmitThisSym) |
| 139 | return &Symbol; |
| 140 | if (! Symbol.isTemporary() && InNormalSection) { |
| 141 | return &Symbol; |
| 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | // Need to examine the Fixup when determining whether to |
| 147 | // emit the relocation as an explicit symbol or as a section relative |
| 148 | // offset |
| 149 | unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, |
| 150 | const MCFixup &Fixup, |
| 151 | bool IsPCRel, |
| 152 | bool IsRelocWithSymbol, |
| 153 | int64_t Addend) const { |
| 154 | return GetRelocTypeInner(Target, Fixup, IsPCRel); |
| 155 | } |
| 156 | |
| 157 | unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target, |
| 158 | const MCFixup &Fixup, |
| 159 | bool IsPCRel) const { |
| 160 | MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? |
| 161 | MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); |
| 162 | |
| 163 | unsigned Type = 0; |
| 164 | if (IsPCRel) { |
| 165 | switch ((unsigned)Fixup.getKind()) { |
| 166 | default: assert(0 && "Unimplemented"); |
| 167 | case FK_Data_4: |
| 168 | switch (Modifier) { |
| 169 | default: llvm_unreachable("Unsupported Modifier"); |
| 170 | case MCSymbolRefExpr::VK_None: |
| 171 | Type = ELF::R_ARM_REL32; |
| 172 | break; |
| 173 | case MCSymbolRefExpr::VK_ARM_TLSGD: |
| 174 | assert(0 && "unimplemented"); |
| 175 | break; |
| 176 | case MCSymbolRefExpr::VK_ARM_GOTTPOFF: |
| 177 | Type = ELF::R_ARM_TLS_IE32; |
| 178 | break; |
| 179 | } |
| 180 | break; |
| 181 | case ARM::fixup_arm_uncondbranch: |
| 182 | switch (Modifier) { |
| 183 | case MCSymbolRefExpr::VK_ARM_PLT: |
| 184 | Type = ELF::R_ARM_PLT32; |
| 185 | break; |
| 186 | default: |
| 187 | Type = ELF::R_ARM_CALL; |
| 188 | break; |
| 189 | } |
| 190 | break; |
| 191 | case ARM::fixup_arm_condbranch: |
| 192 | Type = ELF::R_ARM_JUMP24; |
| 193 | break; |
| 194 | case ARM::fixup_arm_movt_hi16: |
| 195 | case ARM::fixup_arm_movt_hi16_pcrel: |
| 196 | Type = ELF::R_ARM_MOVT_PREL; |
| 197 | break; |
| 198 | case ARM::fixup_arm_movw_lo16: |
| 199 | case ARM::fixup_arm_movw_lo16_pcrel: |
| 200 | Type = ELF::R_ARM_MOVW_PREL_NC; |
| 201 | break; |
| 202 | case ARM::fixup_t2_movt_hi16: |
| 203 | case ARM::fixup_t2_movt_hi16_pcrel: |
| 204 | Type = ELF::R_ARM_THM_MOVT_PREL; |
| 205 | break; |
| 206 | case ARM::fixup_t2_movw_lo16: |
| 207 | case ARM::fixup_t2_movw_lo16_pcrel: |
| 208 | Type = ELF::R_ARM_THM_MOVW_PREL_NC; |
| 209 | break; |
| 210 | case ARM::fixup_arm_thumb_bl: |
| 211 | case ARM::fixup_arm_thumb_blx: |
Rafael Espindola | b975c27 | 2011-12-22 21:36:43 +0000 | [diff] [blame^] | 212 | Type = ELF::R_ARM_THM_CALL; |
Rafael Espindola | 69bbda0 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | } else { |
| 216 | switch ((unsigned)Fixup.getKind()) { |
| 217 | default: llvm_unreachable("invalid fixup kind!"); |
| 218 | case FK_Data_4: |
| 219 | switch (Modifier) { |
| 220 | default: llvm_unreachable("Unsupported Modifier"); break; |
| 221 | case MCSymbolRefExpr::VK_ARM_GOT: |
| 222 | Type = ELF::R_ARM_GOT_BREL; |
| 223 | break; |
| 224 | case MCSymbolRefExpr::VK_ARM_TLSGD: |
| 225 | Type = ELF::R_ARM_TLS_GD32; |
| 226 | break; |
| 227 | case MCSymbolRefExpr::VK_ARM_TPOFF: |
| 228 | Type = ELF::R_ARM_TLS_LE32; |
| 229 | break; |
| 230 | case MCSymbolRefExpr::VK_ARM_GOTTPOFF: |
| 231 | Type = ELF::R_ARM_TLS_IE32; |
| 232 | break; |
| 233 | case MCSymbolRefExpr::VK_None: |
| 234 | Type = ELF::R_ARM_ABS32; |
| 235 | break; |
| 236 | case MCSymbolRefExpr::VK_ARM_GOTOFF: |
| 237 | Type = ELF::R_ARM_GOTOFF32; |
| 238 | break; |
| 239 | } |
| 240 | break; |
| 241 | case ARM::fixup_arm_ldst_pcrel_12: |
| 242 | case ARM::fixup_arm_pcrel_10: |
| 243 | case ARM::fixup_arm_adr_pcrel_12: |
| 244 | case ARM::fixup_arm_thumb_bl: |
| 245 | case ARM::fixup_arm_thumb_cb: |
| 246 | case ARM::fixup_arm_thumb_cp: |
| 247 | case ARM::fixup_arm_thumb_br: |
| 248 | assert(0 && "Unimplemented"); |
| 249 | break; |
| 250 | case ARM::fixup_arm_uncondbranch: |
| 251 | Type = ELF::R_ARM_CALL; |
| 252 | break; |
| 253 | case ARM::fixup_arm_condbranch: |
| 254 | Type = ELF::R_ARM_JUMP24; |
| 255 | break; |
| 256 | case ARM::fixup_arm_movt_hi16: |
| 257 | Type = ELF::R_ARM_MOVT_ABS; |
| 258 | break; |
| 259 | case ARM::fixup_arm_movw_lo16: |
| 260 | Type = ELF::R_ARM_MOVW_ABS_NC; |
| 261 | break; |
| 262 | case ARM::fixup_t2_movt_hi16: |
| 263 | Type = ELF::R_ARM_THM_MOVT_ABS; |
| 264 | break; |
| 265 | case ARM::fixup_t2_movw_lo16: |
| 266 | Type = ELF::R_ARM_THM_MOVW_ABS_NC; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return Type; |
| 272 | } |
| 273 | |
| 274 | MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS, |
| 275 | uint8_t OSABI) { |
| 276 | MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI); |
| 277 | return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true); |
| 278 | } |