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 75 DEBUG( 76 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind(); 77 MCSymbolRefExpr::VariantKind Kind2; 78 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() : 79 MCSymbolRefExpr::VK_None; 80 dbgs() << "considering symbol " 81 << Section.getSectionName() << "/" 82 << Symbol.getName() << "/" 83 << " Rel:" << (unsigned)RelocType 84 << " Kind: " << (int)Kind << "/" << (int)Kind2 85 << " Tmp:" 86 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/" 87 << Symbol.isVariable() << "/" << Symbol.isTemporary() 88 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n"); 89 90 if (IsPCRel) { ++PCRelCount; 91 switch (RelocType) { 92 default: 93 // Most relocation types are emitted as explicit symbols 94 InNormalSection = 95 StringSwitch<bool>(Section.getSectionName()) 96 .Case(".data.rel.ro.local", false) 97 .Case(".data.rel", false) 98 .Case(".bss", false) 99 .Default(true); 100 EmitThisSym = true; 101 break; 102 case ELF::R_ARM_ABS32: 103 // But things get strange with R_ARM_ABS32 104 // In this case, most things that go in .rodata show up 105 // as section relative relocations 106 InNormalSection = 107 StringSwitch<bool>(Section.getSectionName()) 108 .Case(".data.rel.ro.local", false) 109 .Case(".data.rel", false) 110 .Case(".rodata", false) 111 .Case(".bss", false) 112 .Default(true); 113 EmitThisSym = false; 114 break; 115 } 116 } else { 117 NonPCRelCount++; 118 InNormalSection = 119 StringSwitch<bool>(Section.getSectionName()) 120 .Case(".data.rel.ro.local", false) 121 .Case(".rodata", false) 122 .Case(".data.rel", false) 123 .Case(".bss", false) 124 .Default(true); 125 126 switch (RelocType) { 127 default: EmitThisSym = true; break; 128 case ELF::R_ARM_ABS32: EmitThisSym = false; break; 129 case ELF::R_ARM_PREL31: EmitThisSym = false; break; 130 } 131 } 132 133 if (EmitThisSym) 134 return &Symbol; 135 if (! Symbol.isTemporary() && InNormalSection) { 136 return &Symbol; 137 } 138 return NULL; 139 } 140 141 // Need to examine the Fixup when determining whether to 142 // emit the relocation as an explicit symbol or as a section relative 143 // offset 144 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, 145 const MCFixup &Fixup, 146 bool IsPCRel, 147 bool IsRelocWithSymbol, 148 int64_t Addend) const { 149 return GetRelocTypeInner(Target, Fixup, IsPCRel); 150 } 151 152 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target, 153 const MCFixup &Fixup, 154 bool IsPCRel) const { 155 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? 156 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); 157 158 unsigned Type = 0; 159 if (IsPCRel) { 160 switch ((unsigned)Fixup.getKind()) { 161 default: llvm_unreachable("Unimplemented"); 162 case FK_Data_4: 163 switch (Modifier) { 164 default: llvm_unreachable("Unsupported Modifier"); 165 case MCSymbolRefExpr::VK_None: 166 Type = ELF::R_ARM_REL32; 167 break; 168 case MCSymbolRefExpr::VK_TLSGD: 169 llvm_unreachable("unimplemented"); 170 case MCSymbolRefExpr::VK_GOTTPOFF: 171 Type = ELF::R_ARM_TLS_IE32; 172 break; 173 } 174 break; 175 case ARM::fixup_arm_blx: 176 case ARM::fixup_arm_uncondbl: 177 switch (Modifier) { 178 case MCSymbolRefExpr::VK_PLT: 179 Type = ELF::R_ARM_PLT32; 180 break; 181 case MCSymbolRefExpr::VK_ARM_TLSCALL: 182 Type = ELF::R_ARM_TLS_CALL; 183 break; 184 default: 185 Type = ELF::R_ARM_CALL; 186 break; 187 } 188 break; 189 case ARM::fixup_arm_condbl: 190 case ARM::fixup_arm_condbranch: 191 case ARM::fixup_arm_uncondbranch: 192 Type = ELF::R_ARM_JUMP24; 193 break; 194 case ARM::fixup_t2_condbranch: 195 case ARM::fixup_t2_uncondbranch: 196 Type = ELF::R_ARM_THM_JUMP24; 197 break; 198 case ARM::fixup_arm_movt_hi16: 199 case ARM::fixup_arm_movt_hi16_pcrel: 200 Type = ELF::R_ARM_MOVT_PREL; 201 break; 202 case ARM::fixup_arm_movw_lo16: 203 case ARM::fixup_arm_movw_lo16_pcrel: 204 Type = ELF::R_ARM_MOVW_PREL_NC; 205 break; 206 case ARM::fixup_t2_movt_hi16: 207 case ARM::fixup_t2_movt_hi16_pcrel: 208 Type = ELF::R_ARM_THM_MOVT_PREL; 209 break; 210 case ARM::fixup_t2_movw_lo16: 211 case ARM::fixup_t2_movw_lo16_pcrel: 212 Type = ELF::R_ARM_THM_MOVW_PREL_NC; 213 break; 214 case ARM::fixup_arm_thumb_bl: 215 case ARM::fixup_arm_thumb_blx: 216 switch (Modifier) { 217 case MCSymbolRefExpr::VK_ARM_TLSCALL: 218 Type = ELF::R_ARM_THM_TLS_CALL; 219 break; 220 default: 221 Type = ELF::R_ARM_THM_CALL; 222 break; 223 } 224 break; 225 } 226 } else { 227 switch ((unsigned)Fixup.getKind()) { 228 default: llvm_unreachable("invalid fixup kind!"); 229 case FK_Data_4: 230 switch (Modifier) { 231 default: llvm_unreachable("Unsupported Modifier"); 232 case MCSymbolRefExpr::VK_ARM_NONE: 233 Type = ELF::R_ARM_NONE; 234 break; 235 case MCSymbolRefExpr::VK_GOT: 236 Type = ELF::R_ARM_GOT_BREL; 237 break; 238 case MCSymbolRefExpr::VK_TLSGD: 239 Type = ELF::R_ARM_TLS_GD32; 240 break; 241 case MCSymbolRefExpr::VK_TPOFF: 242 Type = ELF::R_ARM_TLS_LE32; 243 break; 244 case MCSymbolRefExpr::VK_GOTTPOFF: 245 Type = ELF::R_ARM_TLS_IE32; 246 break; 247 case MCSymbolRefExpr::VK_None: 248 Type = ELF::R_ARM_ABS32; 249 break; 250 case MCSymbolRefExpr::VK_GOTOFF: 251 Type = ELF::R_ARM_GOTOFF32; 252 break; 253 case MCSymbolRefExpr::VK_ARM_TARGET1: 254 Type = ELF::R_ARM_TARGET1; 255 break; 256 case MCSymbolRefExpr::VK_ARM_TARGET2: 257 Type = ELF::R_ARM_TARGET2; 258 break; 259 case MCSymbolRefExpr::VK_ARM_PREL31: 260 Type = ELF::R_ARM_PREL31; 261 break; 262 case MCSymbolRefExpr::VK_ARM_TLSLDO: 263 Type = ELF::R_ARM_TLS_LDO32; 264 break; 265 case MCSymbolRefExpr::VK_ARM_TLSCALL: 266 Type = ELF::R_ARM_TLS_CALL; 267 break; 268 case MCSymbolRefExpr::VK_ARM_TLSDESC: 269 Type = ELF::R_ARM_TLS_GOTDESC; 270 break; 271 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ: 272 Type = ELF::R_ARM_TLS_DESCSEQ; 273 break; 274 } 275 break; 276 case ARM::fixup_arm_ldst_pcrel_12: 277 case ARM::fixup_arm_pcrel_10: 278 case ARM::fixup_arm_adr_pcrel_12: 279 case ARM::fixup_arm_thumb_bl: 280 case ARM::fixup_arm_thumb_cb: 281 case ARM::fixup_arm_thumb_cp: 282 case ARM::fixup_arm_thumb_br: 283 llvm_unreachable("Unimplemented"); 284 case ARM::fixup_arm_condbranch: 285 case ARM::fixup_arm_uncondbranch: 286 Type = ELF::R_ARM_JUMP24; 287 break; 288 case ARM::fixup_arm_movt_hi16: 289 Type = ELF::R_ARM_MOVT_ABS; 290 break; 291 case ARM::fixup_arm_movw_lo16: 292 Type = ELF::R_ARM_MOVW_ABS_NC; 293 break; 294 case ARM::fixup_t2_movt_hi16: 295 Type = ELF::R_ARM_THM_MOVT_ABS; 296 break; 297 case ARM::fixup_t2_movw_lo16: 298 Type = ELF::R_ARM_THM_MOVW_ABS_NC; 299 break; 300 } 301 } 302 303 return Type; 304 } 305 306 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS, 307 uint8_t OSABI) { 308 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI); 309 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true); 310 } 311