1 //===-- ARMELFObjectWriter.cpp - ARM ELF 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 10 #include "MCTargetDesc/ARMMCTargetDesc.h" 11 #include "MCTargetDesc/ARMFixupKinds.h" 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" 18 #include "llvm/Support/Debug.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/raw_ostream.h" 21 22 using namespace llvm; 23 24 namespace { 25 class ARMELFObjectWriter : public MCELFObjectTargetWriter { 26 enum { DefaultEABIVersion = 0x05000000U }; 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(); 36 37 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, 38 bool IsPCRel, bool IsRelocWithSymbol, 39 int64_t Addend) const override; 40 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm, 41 const MCValue &Target, const MCFragment &F, 42 const MCFixup &Fixup, 43 bool IsPCRel) const override; 44 }; 45 } 46 47 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI) 48 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, 49 ELF::EM_ARM, 50 /*HasRelocationAddend*/ false) {} 51 52 ARMELFObjectWriter::~ARMELFObjectWriter() {} 53 54 // In ARM, _MergedGlobals and other most symbols get emitted directly. 55 // I.e. not as an offset to a section symbol. 56 // This code is an approximation of what ARM/gcc does. 57 58 STATISTIC(PCRelCount, "Total number of PIC Relocations"); 59 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations"); 60 61 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm, 62 const MCValue &Target, 63 const MCFragment &F, 64 const MCFixup &Fixup, 65 bool IsPCRel) const { 66 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol(); 67 bool EmitThisSym = false; 68 69 const MCSectionELF &Section = 70 static_cast<const MCSectionELF&>(Symbol.getSection()); 71 bool InNormalSection = true; 72 unsigned RelocType = 0; 73 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel); 74 assert(!Target.getSymB() || 75 Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None); 76 77 DEBUG( 78 MCSymbolRefExpr::VariantKind Kind = Fixup.getAccessVariant(); 79 dbgs() << "considering symbol " 80 << Section.getSectionName() << "/" 81 << Symbol.getName() << "/" 82 << " Rel:" << (unsigned)RelocType 83 << " Kind: " << (int)Kind 84 << " Tmp:" 85 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/" 86 << Symbol.isVariable() << "/" << Symbol.isTemporary() 87 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n"); 88 89 if (IsPCRel) { ++PCRelCount; 90 switch (RelocType) { 91 default: 92 // Most relocation types are emitted as explicit symbols 93 InNormalSection = 94 StringSwitch<bool>(Section.getSectionName()) 95 .Case(".data.rel.ro.local", false) 96 .Case(".data.rel", false) 97 .Case(".bss", false) 98 .Default(true); 99 EmitThisSym = true; 100 break; 101 case ELF::R_ARM_ABS32: 102 // But things get strange with R_ARM_ABS32 103 // In this case, most things that go in .rodata show up 104 // as section relative relocations 105 InNormalSection = 106 StringSwitch<bool>(Section.getSectionName()) 107 .Case(".data.rel.ro.local", false) 108 .Case(".data.rel", false) 109 .Case(".rodata", false) 110 .Case(".bss", false) 111 .Default(true); 112 EmitThisSym = false; 113 break; 114 } 115 } else { 116 NonPCRelCount++; 117 InNormalSection = 118 StringSwitch<bool>(Section.getSectionName()) 119 .Case(".data.rel.ro.local", false) 120 .Case(".rodata", false) 121 .Case(".data.rel", false) 122 .Case(".bss", false) 123 .Default(true); 124 125 switch (RelocType) { 126 default: EmitThisSym = true; break; 127 case ELF::R_ARM_ABS32: EmitThisSym = false; break; 128 case ELF::R_ARM_PREL31: EmitThisSym = false; break; 129 } 130 } 131 132 if (EmitThisSym) 133 return &Symbol; 134 if (! Symbol.isTemporary() && InNormalSection) { 135 return &Symbol; 136 } 137 return NULL; 138 } 139 140 // Need to examine the Fixup when determining whether to 141 // emit the relocation as an explicit symbol or as a section relative 142 // offset 143 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, 144 const MCFixup &Fixup, 145 bool IsPCRel, 146 bool IsRelocWithSymbol, 147 int64_t Addend) const { 148 return GetRelocTypeInner(Target, Fixup, IsPCRel); 149 } 150 151 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target, 152 const MCFixup &Fixup, 153 bool IsPCRel) const { 154 MCSymbolRefExpr::VariantKind Modifier = Fixup.getAccessVariant(); 155 156 unsigned Type = 0; 157 if (IsPCRel) { 158 switch ((unsigned)Fixup.getKind()) { 159 default: llvm_unreachable("Unimplemented"); 160 case FK_Data_4: 161 switch (Modifier) { 162 default: llvm_unreachable("Unsupported Modifier"); 163 case MCSymbolRefExpr::VK_None: 164 Type = ELF::R_ARM_REL32; 165 break; 166 case MCSymbolRefExpr::VK_TLSGD: 167 llvm_unreachable("unimplemented"); 168 case MCSymbolRefExpr::VK_GOTTPOFF: 169 Type = ELF::R_ARM_TLS_IE32; 170 break; 171 } 172 break; 173 case ARM::fixup_arm_blx: 174 case ARM::fixup_arm_uncondbl: 175 switch (Modifier) { 176 case MCSymbolRefExpr::VK_PLT: 177 Type = ELF::R_ARM_PLT32; 178 break; 179 case MCSymbolRefExpr::VK_ARM_TLSCALL: 180 Type = ELF::R_ARM_TLS_CALL; 181 break; 182 default: 183 Type = ELF::R_ARM_CALL; 184 break; 185 } 186 break; 187 case ARM::fixup_arm_condbl: 188 case ARM::fixup_arm_condbranch: 189 case ARM::fixup_arm_uncondbranch: 190 Type = ELF::R_ARM_JUMP24; 191 break; 192 case ARM::fixup_t2_condbranch: 193 case ARM::fixup_t2_uncondbranch: 194 Type = ELF::R_ARM_THM_JUMP24; 195 break; 196 case ARM::fixup_arm_movt_hi16: 197 case ARM::fixup_arm_movt_hi16_pcrel: 198 Type = ELF::R_ARM_MOVT_PREL; 199 break; 200 case ARM::fixup_arm_movw_lo16: 201 case ARM::fixup_arm_movw_lo16_pcrel: 202 Type = ELF::R_ARM_MOVW_PREL_NC; 203 break; 204 case ARM::fixup_t2_movt_hi16: 205 case ARM::fixup_t2_movt_hi16_pcrel: 206 Type = ELF::R_ARM_THM_MOVT_PREL; 207 break; 208 case ARM::fixup_t2_movw_lo16: 209 case ARM::fixup_t2_movw_lo16_pcrel: 210 Type = ELF::R_ARM_THM_MOVW_PREL_NC; 211 break; 212 case ARM::fixup_arm_thumb_bl: 213 case ARM::fixup_arm_thumb_blx: 214 switch (Modifier) { 215 case MCSymbolRefExpr::VK_ARM_TLSCALL: 216 Type = ELF::R_ARM_THM_TLS_CALL; 217 break; 218 default: 219 Type = ELF::R_ARM_THM_CALL; 220 break; 221 } 222 break; 223 } 224 } else { 225 switch ((unsigned)Fixup.getKind()) { 226 default: llvm_unreachable("invalid fixup kind!"); 227 case FK_Data_4: 228 switch (Modifier) { 229 default: llvm_unreachable("Unsupported Modifier"); 230 case MCSymbolRefExpr::VK_ARM_NONE: 231 Type = ELF::R_ARM_NONE; 232 break; 233 case MCSymbolRefExpr::VK_GOT: 234 Type = ELF::R_ARM_GOT_BREL; 235 break; 236 case MCSymbolRefExpr::VK_TLSGD: 237 Type = ELF::R_ARM_TLS_GD32; 238 break; 239 case MCSymbolRefExpr::VK_TPOFF: 240 Type = ELF::R_ARM_TLS_LE32; 241 break; 242 case MCSymbolRefExpr::VK_GOTTPOFF: 243 Type = ELF::R_ARM_TLS_IE32; 244 break; 245 case MCSymbolRefExpr::VK_None: 246 Type = ELF::R_ARM_ABS32; 247 break; 248 case MCSymbolRefExpr::VK_GOTOFF: 249 Type = ELF::R_ARM_GOTOFF32; 250 break; 251 case MCSymbolRefExpr::VK_ARM_TARGET1: 252 Type = ELF::R_ARM_TARGET1; 253 break; 254 case MCSymbolRefExpr::VK_ARM_TARGET2: 255 Type = ELF::R_ARM_TARGET2; 256 break; 257 case MCSymbolRefExpr::VK_ARM_PREL31: 258 Type = ELF::R_ARM_PREL31; 259 break; 260 case MCSymbolRefExpr::VK_ARM_TLSLDO: 261 Type = ELF::R_ARM_TLS_LDO32; 262 break; 263 case MCSymbolRefExpr::VK_ARM_TLSCALL: 264 Type = ELF::R_ARM_TLS_CALL; 265 break; 266 case MCSymbolRefExpr::VK_ARM_TLSDESC: 267 Type = ELF::R_ARM_TLS_GOTDESC; 268 break; 269 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ: 270 Type = ELF::R_ARM_TLS_DESCSEQ; 271 break; 272 } 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: 281 llvm_unreachable("Unimplemented"); 282 case ARM::fixup_arm_condbranch: 283 case ARM::fixup_arm_uncondbranch: 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 } 309