1 //===----- ELF_aarch32.cpp - JIT linker implementation for arm/thumb ------===// 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 // ELF/aarch32 jit-link implementation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ExecutionEngine/JITLink/ELF_aarch32.h" 14 15 #include "llvm/BinaryFormat/ELF.h" 16 #include "llvm/ExecutionEngine/JITLink/JITLink.h" 17 #include "llvm/ExecutionEngine/JITLink/aarch32.h" 18 #include "llvm/Object/ELF.h" 19 #include "llvm/Object/ELFObjectFile.h" 20 #include "llvm/Support/Endian.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/TargetParser/ARMTargetParser.h" 23 24 #include "ELFLinkGraphBuilder.h" 25 #include "JITLinkGeneric.h" 26 27 #define DEBUG_TYPE "jitlink" 28 29 using namespace llvm::object; 30 31 namespace llvm { 32 namespace jitlink { 33 34 /// Translate from ELF relocation type to JITLink-internal edge kind. 35 Expected<aarch32::EdgeKind_aarch32> getJITLinkEdgeKind(uint32_t ELFType) { 36 switch (ELFType) { 37 case ELF::R_ARM_ABS32: 38 return aarch32::Data_Pointer32; 39 case ELF::R_ARM_REL32: 40 return aarch32::Data_Delta32; 41 case ELF::R_ARM_CALL: 42 return aarch32::Arm_Call; 43 case ELF::R_ARM_THM_CALL: 44 return aarch32::Thumb_Call; 45 case ELF::R_ARM_THM_JUMP24: 46 return aarch32::Thumb_Jump24; 47 case ELF::R_ARM_THM_MOVW_ABS_NC: 48 return aarch32::Thumb_MovwAbsNC; 49 case ELF::R_ARM_THM_MOVT_ABS: 50 return aarch32::Thumb_MovtAbs; 51 } 52 53 return make_error<JITLinkError>( 54 "Unsupported aarch32 relocation " + formatv("{0:d}: ", ELFType) + 55 object::getELFRelocationTypeName(ELF::EM_ARM, ELFType)); 56 } 57 58 /// Translate from JITLink-internal edge kind back to ELF relocation type. 59 Expected<uint32_t> getELFRelocationType(Edge::Kind Kind) { 60 switch (static_cast<aarch32::EdgeKind_aarch32>(Kind)) { 61 case aarch32::Data_Delta32: 62 return ELF::R_ARM_REL32; 63 case aarch32::Data_Pointer32: 64 return ELF::R_ARM_ABS32; 65 case aarch32::Arm_Call: 66 return ELF::R_ARM_CALL; 67 case aarch32::Thumb_Call: 68 return ELF::R_ARM_THM_CALL; 69 case aarch32::Thumb_Jump24: 70 return ELF::R_ARM_THM_JUMP24; 71 case aarch32::Thumb_MovwAbsNC: 72 return ELF::R_ARM_THM_MOVW_ABS_NC; 73 case aarch32::Thumb_MovtAbs: 74 return ELF::R_ARM_THM_MOVT_ABS; 75 } 76 77 return make_error<JITLinkError>(formatv("Invalid aarch32 edge {0:d}: ", 78 Kind)); 79 } 80 81 /// Get a human-readable name for the given ELF AArch32 edge kind. 82 const char *getELFAArch32EdgeKindName(Edge::Kind R) { 83 // No ELF-specific edge kinds yet 84 return aarch32::getEdgeKindName(R); 85 } 86 87 class ELFJITLinker_aarch32 : public JITLinker<ELFJITLinker_aarch32> { 88 friend class JITLinker<ELFJITLinker_aarch32>; 89 90 public: 91 ELFJITLinker_aarch32(std::unique_ptr<JITLinkContext> Ctx, 92 std::unique_ptr<LinkGraph> G, PassConfiguration PassCfg, 93 aarch32::ArmConfig ArmCfg) 94 : JITLinker(std::move(Ctx), std::move(G), std::move(PassCfg)), 95 ArmCfg(std::move(ArmCfg)) {} 96 97 private: 98 aarch32::ArmConfig ArmCfg; 99 100 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const { 101 return aarch32::applyFixup(G, B, E, ArmCfg); 102 } 103 }; 104 105 template <support::endianness DataEndianness> 106 class ELFLinkGraphBuilder_aarch32 107 : public ELFLinkGraphBuilder<ELFType<DataEndianness, false>> { 108 private: 109 using ELFT = ELFType<DataEndianness, false>; 110 using Base = ELFLinkGraphBuilder<ELFT>; 111 112 bool excludeSection(const typename ELFT::Shdr &Sect) const override { 113 // TODO: An .ARM.exidx (Exception Index table) entry is 8-bytes in size and 114 // consists of 2 words. It might be sufficient to process only relocations 115 // in the the second word (offset 4). Please find more details in: Exception 116 // Handling ABI for the Arm® Architecture -> Index table entries 117 if (Sect.sh_type == ELF::SHT_ARM_EXIDX) 118 return true; 119 return false; 120 } 121 122 Error addRelocations() override { 123 LLVM_DEBUG(dbgs() << "Processing relocations:\n"); 124 using Self = ELFLinkGraphBuilder_aarch32<DataEndianness>; 125 for (const auto &RelSect : Base::Sections) { 126 if (Error Err = Base::forEachRelRelocation(RelSect, this, 127 &Self::addSingleRelRelocation)) 128 return Err; 129 } 130 return Error::success(); 131 } 132 133 Error addSingleRelRelocation(const typename ELFT::Rel &Rel, 134 const typename ELFT::Shdr &FixupSect, 135 Block &BlockToFix) { 136 uint32_t SymbolIndex = Rel.getSymbol(false); 137 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec); 138 if (!ObjSymbol) 139 return ObjSymbol.takeError(); 140 141 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex); 142 if (!GraphSymbol) 143 return make_error<StringError>( 144 formatv("Could not find symbol at given index, did you add it to " 145 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}", 146 SymbolIndex, (*ObjSymbol)->st_shndx, 147 Base::GraphSymbols.size()), 148 inconvertibleErrorCode()); 149 150 uint32_t Type = Rel.getType(false); 151 Expected<aarch32::EdgeKind_aarch32> Kind = getJITLinkEdgeKind(Type); 152 if (!Kind) 153 return Kind.takeError(); 154 155 auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset; 156 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress(); 157 Edge E(*Kind, Offset, *GraphSymbol, 0); 158 159 Expected<int64_t> Addend = 160 aarch32::readAddend(*Base::G, BlockToFix, E, ArmCfg); 161 if (!Addend) 162 return Addend.takeError(); 163 164 E.setAddend(*Addend); 165 LLVM_DEBUG({ 166 dbgs() << " "; 167 printEdge(dbgs(), BlockToFix, E, getELFAArch32EdgeKindName(*Kind)); 168 dbgs() << "\n"; 169 }); 170 171 BlockToFix.addEdge(std::move(E)); 172 return Error::success(); 173 } 174 175 aarch32::ArmConfig ArmCfg; 176 177 protected: 178 TargetFlagsType makeTargetFlags(const typename ELFT::Sym &Sym) override { 179 if (Sym.getValue() & 0x01) 180 return aarch32::ThumbSymbol; 181 return TargetFlagsType{}; 182 } 183 184 orc::ExecutorAddrDiff getRawOffset(const typename ELFT::Sym &Sym, 185 TargetFlagsType Flags) override { 186 assert((makeTargetFlags(Sym) & Flags) == Flags); 187 static constexpr uint64_t ThumbBit = 0x01; 188 return Sym.getValue() & ~ThumbBit; 189 } 190 191 public: 192 ELFLinkGraphBuilder_aarch32(StringRef FileName, 193 const llvm::object::ELFFile<ELFT> &Obj, Triple TT, 194 SubtargetFeatures Features, 195 aarch32::ArmConfig ArmCfg) 196 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(TT), std::move(Features), 197 FileName, getELFAArch32EdgeKindName), 198 ArmCfg(std::move(ArmCfg)) {} 199 }; 200 201 template <aarch32::StubsFlavor Flavor> 202 Error buildTables_ELF_aarch32(LinkGraph &G) { 203 LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n"); 204 205 aarch32::StubsManager<Flavor> PLT; 206 visitExistingEdges(G, PLT); 207 return Error::success(); 208 } 209 210 Expected<std::unique_ptr<LinkGraph>> 211 createLinkGraphFromELFObject_aarch32(MemoryBufferRef ObjectBuffer) { 212 LLVM_DEBUG({ 213 dbgs() << "Building jitlink graph for new input " 214 << ObjectBuffer.getBufferIdentifier() << "...\n"; 215 }); 216 217 auto ELFObj = ObjectFile::createELFObjectFile(ObjectBuffer); 218 if (!ELFObj) 219 return ELFObj.takeError(); 220 221 auto Features = (*ELFObj)->getFeatures(); 222 if (!Features) 223 return Features.takeError(); 224 225 // Find out what exact AArch32 instruction set and features we target. 226 auto TT = (*ELFObj)->makeTriple(); 227 ARM::ArchKind AK = ARM::parseArch(TT.getArchName()); 228 if (AK == ARM::ArchKind::INVALID) 229 return make_error<JITLinkError>( 230 "Failed to build ELF link graph: Invalid ARM ArchKind"); 231 232 // Resolve our internal configuration for the target. If at some point the 233 // CPUArch alone becomes too unprecise, we can find more details in the 234 // Tag_CPU_arch_profile. 235 aarch32::ArmConfig ArmCfg; 236 using namespace ARMBuildAttrs; 237 auto Arch = static_cast<CPUArch>(ARM::getArchAttr(AK)); 238 switch (Arch) { 239 case v7: 240 case v8_A: 241 ArmCfg = aarch32::getArmConfigForCPUArch(Arch); 242 assert(ArmCfg.Stubs != aarch32::Unsupported && 243 "Provide a config for each supported CPU"); 244 break; 245 default: 246 return make_error<JITLinkError>( 247 "Failed to build ELF link graph: Unsupported CPU arch " + 248 StringRef(aarch32::getCPUArchName(Arch))); 249 } 250 251 // Populate the link-graph. 252 switch (TT.getArch()) { 253 case Triple::arm: 254 case Triple::thumb: { 255 auto &ELFFile = cast<ELFObjectFile<ELF32LE>>(**ELFObj).getELFFile(); 256 return ELFLinkGraphBuilder_aarch32<support::little>( 257 (*ELFObj)->getFileName(), ELFFile, TT, std::move(*Features), 258 ArmCfg) 259 .buildGraph(); 260 } 261 case Triple::armeb: 262 case Triple::thumbeb: { 263 auto &ELFFile = cast<ELFObjectFile<ELF32BE>>(**ELFObj).getELFFile(); 264 return ELFLinkGraphBuilder_aarch32<support::big>( 265 (*ELFObj)->getFileName(), ELFFile, TT, std::move(*Features), 266 ArmCfg) 267 .buildGraph(); 268 } 269 default: 270 return make_error<JITLinkError>( 271 "Failed to build ELF/aarch32 link graph: Invalid target triple " + 272 TT.getTriple()); 273 } 274 } 275 276 void link_ELF_aarch32(std::unique_ptr<LinkGraph> G, 277 std::unique_ptr<JITLinkContext> Ctx) { 278 const Triple &TT = G->getTargetTriple(); 279 280 using namespace ARMBuildAttrs; 281 ARM::ArchKind AK = ARM::parseArch(TT.getArchName()); 282 auto CPU = static_cast<CPUArch>(ARM::getArchAttr(AK)); 283 aarch32::ArmConfig ArmCfg = aarch32::getArmConfigForCPUArch(CPU); 284 285 PassConfiguration PassCfg; 286 if (Ctx->shouldAddDefaultTargetPasses(TT)) { 287 // Add a mark-live pass. 288 if (auto MarkLive = Ctx->getMarkLivePass(TT)) 289 PassCfg.PrePrunePasses.push_back(std::move(MarkLive)); 290 else 291 PassCfg.PrePrunePasses.push_back(markAllSymbolsLive); 292 293 switch (ArmCfg.Stubs) { 294 case aarch32::Thumbv7: 295 PassCfg.PostPrunePasses.push_back( 296 buildTables_ELF_aarch32<aarch32::Thumbv7>); 297 break; 298 case aarch32::Unsupported: 299 llvm_unreachable("Check before building graph"); 300 } 301 } 302 303 if (auto Err = Ctx->modifyPassConfig(*G, PassCfg)) 304 return Ctx->notifyFailed(std::move(Err)); 305 306 ELFJITLinker_aarch32::link(std::move(Ctx), std::move(G), std::move(PassCfg), 307 std::move(ArmCfg)); 308 } 309 310 } // namespace jitlink 311 } // namespace llvm 312