1 //===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "MCTargetDesc/ARMFixupKinds.h" 10 #include "MCTargetDesc/ARMMCTargetDesc.h" 11 #include "llvm/BinaryFormat/ELF.h" 12 #include "llvm/MC/MCContext.h" 13 #include "llvm/MC/MCELFObjectWriter.h" 14 #include "llvm/MC/MCExpr.h" 15 #include "llvm/MC/MCFixup.h" 16 #include "llvm/MC/MCObjectFileInfo.h" 17 #include "llvm/MC/MCObjectWriter.h" 18 #include "llvm/MC/MCValue.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include <cstdint> 22 23 using namespace llvm; 24 25 namespace { 26 27 class ARMELFObjectWriter : public MCELFObjectTargetWriter { 28 enum { DefaultEABIVersion = 0x05000000U }; 29 30 unsigned GetRelocTypeInner(const MCValue &Target, const MCFixup &Fixup, 31 bool IsPCRel, MCContext &Ctx) const; 32 33 public: 34 ARMELFObjectWriter(uint8_t OSABI); 35 36 ~ARMELFObjectWriter() override = default; 37 38 unsigned getRelocType(MCContext &Ctx, const MCValue &Target, 39 const MCFixup &Fixup, bool IsPCRel) const override; 40 41 bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym, 42 unsigned Type) const override; 43 44 void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec) override; 45 }; 46 47 } // end anonymous namespace 48 49 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI) 50 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, 51 ELF::EM_ARM, 52 /*HasRelocationAddend*/ false) {} 53 54 bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCValue &, 55 const MCSymbol &, 56 unsigned Type) const { 57 // FIXME: This is extremely conservative. This really needs to use an 58 // explicit list with a clear explanation for why each realocation needs to 59 // point to the symbol, not to the section. 60 switch (Type) { 61 default: 62 return true; 63 64 case ELF::R_ARM_PREL31: 65 case ELF::R_ARM_ABS32: 66 return false; 67 } 68 } 69 70 // Need to examine the Fixup when determining whether to 71 // emit the relocation as an explicit symbol or as a section relative 72 // offset 73 unsigned ARMELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, 74 const MCFixup &Fixup, 75 bool IsPCRel) const { 76 return GetRelocTypeInner(Target, Fixup, IsPCRel, Ctx); 77 } 78 79 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target, 80 const MCFixup &Fixup, 81 bool IsPCRel, 82 MCContext &Ctx) const { 83 unsigned Kind = Fixup.getTargetKind(); 84 if (Kind >= FirstLiteralRelocationKind) 85 return Kind - FirstLiteralRelocationKind; 86 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant(); 87 88 if (IsPCRel) { 89 switch (Fixup.getTargetKind()) { 90 default: 91 Ctx.reportError(Fixup.getLoc(), "unsupported relocation type"); 92 return ELF::R_ARM_NONE; 93 case FK_Data_4: 94 switch (Modifier) { 95 default: 96 Ctx.reportError(Fixup.getLoc(), 97 "invalid fixup for 4-byte pc-relative data relocation"); 98 return ELF::R_ARM_NONE; 99 case MCSymbolRefExpr::VK_None: { 100 if (const MCSymbolRefExpr *SymRef = Target.getSymA()) { 101 // For GNU AS compatibility expressions such as 102 // _GLOBAL_OFFSET_TABLE_ - label emit a R_ARM_BASE_PREL relocation. 103 if (SymRef->getSymbol().getName() == "_GLOBAL_OFFSET_TABLE_") 104 return ELF::R_ARM_BASE_PREL; 105 } 106 return ELF::R_ARM_REL32; 107 } 108 case MCSymbolRefExpr::VK_GOTTPOFF: 109 return ELF::R_ARM_TLS_IE32; 110 case MCSymbolRefExpr::VK_ARM_GOT_PREL: 111 return ELF::R_ARM_GOT_PREL; 112 case MCSymbolRefExpr::VK_ARM_PREL31: 113 return ELF::R_ARM_PREL31; 114 } 115 case ARM::fixup_arm_blx: 116 case ARM::fixup_arm_uncondbl: 117 switch (Modifier) { 118 case MCSymbolRefExpr::VK_PLT: 119 return ELF::R_ARM_CALL; 120 case MCSymbolRefExpr::VK_TLSCALL: 121 return ELF::R_ARM_TLS_CALL; 122 default: 123 return ELF::R_ARM_CALL; 124 } 125 case ARM::fixup_arm_condbl: 126 case ARM::fixup_arm_condbranch: 127 case ARM::fixup_arm_uncondbranch: 128 return ELF::R_ARM_JUMP24; 129 case ARM::fixup_t2_condbranch: 130 return ELF::R_ARM_THM_JUMP19; 131 case ARM::fixup_t2_uncondbranch: 132 return ELF::R_ARM_THM_JUMP24; 133 case ARM::fixup_arm_movt_hi16: 134 return ELF::R_ARM_MOVT_PREL; 135 case ARM::fixup_arm_movw_lo16: 136 return ELF::R_ARM_MOVW_PREL_NC; 137 case ARM::fixup_t2_movt_hi16: 138 return ELF::R_ARM_THM_MOVT_PREL; 139 case ARM::fixup_t2_movw_lo16: 140 return ELF::R_ARM_THM_MOVW_PREL_NC; 141 case ARM::fixup_arm_thumb_upper_8_15: 142 return ELF::R_ARM_THM_ALU_ABS_G3; 143 case ARM::fixup_arm_thumb_upper_0_7: 144 return ELF::R_ARM_THM_ALU_ABS_G2_NC; 145 case ARM::fixup_arm_thumb_lower_8_15: 146 return ELF::R_ARM_THM_ALU_ABS_G1_NC; 147 case ARM::fixup_arm_thumb_lower_0_7: 148 return ELF::R_ARM_THM_ALU_ABS_G0_NC; 149 case ARM::fixup_arm_thumb_br: 150 return ELF::R_ARM_THM_JUMP11; 151 case ARM::fixup_arm_thumb_bcc: 152 return ELF::R_ARM_THM_JUMP8; 153 case ARM::fixup_arm_thumb_bl: 154 case ARM::fixup_arm_thumb_blx: 155 switch (Modifier) { 156 case MCSymbolRefExpr::VK_TLSCALL: 157 return ELF::R_ARM_THM_TLS_CALL; 158 default: 159 return ELF::R_ARM_THM_CALL; 160 } 161 case ARM::fixup_bf_target: 162 return ELF::R_ARM_THM_BF16; 163 case ARM::fixup_bfc_target: 164 return ELF::R_ARM_THM_BF12; 165 case ARM::fixup_bfl_target: 166 return ELF::R_ARM_THM_BF18; 167 } 168 } 169 switch (Kind) { 170 default: 171 Ctx.reportError(Fixup.getLoc(), "unsupported relocation type"); 172 return ELF::R_ARM_NONE; 173 case FK_Data_1: 174 switch (Modifier) { 175 default: 176 Ctx.reportError(Fixup.getLoc(), 177 "invalid fixup for 1-byte data relocation"); 178 return ELF::R_ARM_NONE; 179 case MCSymbolRefExpr::VK_None: 180 return ELF::R_ARM_ABS8; 181 } 182 case FK_Data_2: 183 switch (Modifier) { 184 default: 185 Ctx.reportError(Fixup.getLoc(), 186 "invalid fixup for 2-byte data relocation"); 187 return ELF::R_ARM_NONE; 188 case MCSymbolRefExpr::VK_None: 189 return ELF::R_ARM_ABS16; 190 } 191 case FK_Data_4: 192 switch (Modifier) { 193 default: 194 Ctx.reportError(Fixup.getLoc(), 195 "invalid fixup for 4-byte data relocation"); 196 return ELF::R_ARM_NONE; 197 case MCSymbolRefExpr::VK_ARM_NONE: 198 return ELF::R_ARM_NONE; 199 case MCSymbolRefExpr::VK_GOT: 200 return ELF::R_ARM_GOT_BREL; 201 case MCSymbolRefExpr::VK_TLSGD: 202 return ELF::R_ARM_TLS_GD32; 203 case MCSymbolRefExpr::VK_TPOFF: 204 return ELF::R_ARM_TLS_LE32; 205 case MCSymbolRefExpr::VK_GOTTPOFF: 206 return ELF::R_ARM_TLS_IE32; 207 case MCSymbolRefExpr::VK_None: 208 return ELF::R_ARM_ABS32; 209 case MCSymbolRefExpr::VK_GOTOFF: 210 return ELF::R_ARM_GOTOFF32; 211 case MCSymbolRefExpr::VK_ARM_GOT_PREL: 212 return ELF::R_ARM_GOT_PREL; 213 case MCSymbolRefExpr::VK_ARM_TARGET1: 214 return ELF::R_ARM_TARGET1; 215 case MCSymbolRefExpr::VK_ARM_TARGET2: 216 return ELF::R_ARM_TARGET2; 217 case MCSymbolRefExpr::VK_ARM_PREL31: 218 return ELF::R_ARM_PREL31; 219 case MCSymbolRefExpr::VK_ARM_SBREL: 220 return ELF::R_ARM_SBREL32; 221 case MCSymbolRefExpr::VK_ARM_TLSLDO: 222 return ELF::R_ARM_TLS_LDO32; 223 case MCSymbolRefExpr::VK_TLSCALL: 224 return ELF::R_ARM_TLS_CALL; 225 case MCSymbolRefExpr::VK_TLSDESC: 226 return ELF::R_ARM_TLS_GOTDESC; 227 case MCSymbolRefExpr::VK_TLSLDM: 228 return ELF::R_ARM_TLS_LDM32; 229 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ: 230 return ELF::R_ARM_TLS_DESCSEQ; 231 } 232 case ARM::fixup_arm_condbranch: 233 case ARM::fixup_arm_uncondbranch: 234 return ELF::R_ARM_JUMP24; 235 case ARM::fixup_arm_movt_hi16: 236 switch (Modifier) { 237 default: 238 Ctx.reportError(Fixup.getLoc(), "invalid fixup for ARM MOVT instruction"); 239 return ELF::R_ARM_NONE; 240 case MCSymbolRefExpr::VK_None: 241 return ELF::R_ARM_MOVT_ABS; 242 case MCSymbolRefExpr::VK_ARM_SBREL: 243 return ELF::R_ARM_MOVT_BREL; 244 } 245 case ARM::fixup_arm_movw_lo16: 246 switch (Modifier) { 247 default: 248 Ctx.reportError(Fixup.getLoc(), "invalid fixup for ARM MOVW instruction"); 249 return ELF::R_ARM_NONE; 250 case MCSymbolRefExpr::VK_None: 251 return ELF::R_ARM_MOVW_ABS_NC; 252 case MCSymbolRefExpr::VK_ARM_SBREL: 253 return ELF::R_ARM_MOVW_BREL_NC; 254 } 255 case ARM::fixup_t2_movt_hi16: 256 switch (Modifier) { 257 default: 258 Ctx.reportError(Fixup.getLoc(), 259 "invalid fixup for Thumb MOVT instruction"); 260 return ELF::R_ARM_NONE; 261 case MCSymbolRefExpr::VK_None: 262 return ELF::R_ARM_THM_MOVT_ABS; 263 case MCSymbolRefExpr::VK_ARM_SBREL: 264 return ELF::R_ARM_THM_MOVT_BREL; 265 } 266 case ARM::fixup_t2_movw_lo16: 267 switch (Modifier) { 268 default: 269 Ctx.reportError(Fixup.getLoc(), 270 "invalid fixup for Thumb MOVW instruction"); 271 return ELF::R_ARM_NONE; 272 case MCSymbolRefExpr::VK_None: 273 return ELF::R_ARM_THM_MOVW_ABS_NC; 274 case MCSymbolRefExpr::VK_ARM_SBREL: 275 return ELF::R_ARM_THM_MOVW_BREL_NC; 276 } 277 278 case ARM::fixup_arm_thumb_upper_8_15: 279 return ELF::R_ARM_THM_ALU_ABS_G3; 280 case ARM::fixup_arm_thumb_upper_0_7: 281 return ELF::R_ARM_THM_ALU_ABS_G2_NC; 282 case ARM::fixup_arm_thumb_lower_8_15: 283 return ELF::R_ARM_THM_ALU_ABS_G1_NC; 284 case ARM::fixup_arm_thumb_lower_0_7: 285 return ELF::R_ARM_THM_ALU_ABS_G0_NC; 286 } 287 } 288 289 void ARMELFObjectWriter::addTargetSectionFlags(MCContext &Ctx, 290 MCSectionELF &Sec) { 291 // The mix of execute-only and non-execute-only at link time is 292 // non-execute-only. To avoid the empty implicitly created .text 293 // section from making the whole .text section non-execute-only, we 294 // mark it execute-only if it is empty and there is at least one 295 // execute-only section in the object. 296 MCSectionELF *TextSection = 297 static_cast<MCSectionELF *>(Ctx.getObjectFileInfo()->getTextSection()); 298 if (Sec.getKind().isExecuteOnly() && !TextSection->hasInstructions()) { 299 for (auto &F : TextSection->getFragmentList()) 300 if (auto *DF = dyn_cast<MCDataFragment>(&F)) 301 if (!DF->getContents().empty()) 302 return; 303 TextSection->setFlags(TextSection->getFlags() | ELF::SHF_ARM_PURECODE); 304 } 305 } 306 307 std::unique_ptr<MCObjectTargetWriter> 308 llvm::createARMELFObjectWriter(uint8_t OSABI) { 309 return std::make_unique<ARMELFObjectWriter>(OSABI); 310 } 311