diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/ELF_aarch64.h b/llvm/include/llvm/ExecutionEngine/JITLink/ELF_aarch64.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ExecutionEngine/JITLink/ELF_aarch64.h @@ -0,0 +1,39 @@ +//===--- ELF_aarch64.h - JIT link functions for ELF/aarch64 --*- C++ -*----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +//===----------------------------------------------------------------------===// +// +// jit-link functions for ELF/aarch64. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_EXECUTIONENGINE_JITLINK_ELF_AARCH64_H +#define LLVM_EXECUTIONENGINE_JITLINK_ELF_AARCH64_H + +#include "llvm/ExecutionEngine/JITLink/JITLink.h" + +namespace llvm { +namespace jitlink { + +/// Create a LinkGraph from an ELF/aarch64 relocatable object +/// +/// Note: The graph does not take ownership of the underlying buffer, nor copy +/// its contents. The caller is responsible for ensuring that the object buffer +/// outlives the graph. +Expected> +createLinkGraphFromELFObject_aarch64(MemoryBufferRef ObjectBuffer); + +/// jit-link the given object buffer, which must be a ELF aarch64 relocatable +/// object file. +void link_ELF_aarch64(std::unique_ptr G, + std::unique_ptr Ctx); + +} // end namespace jitlink +} // end namespace llvm + +#endif // LLVM_EXECUTIONENGINE_JITLINK_ELF_AARCH64_H diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/aarch64.h b/llvm/include/llvm/ExecutionEngine/JITLink/aarch64.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ExecutionEngine/JITLink/aarch64.h @@ -0,0 +1,38 @@ +//=== aarch64.h - Generic JITLink aarch64 edge kinds, utilities -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Generic utilities for graphs representing aarch64 objects. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H +#define LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H + +#include "llvm/ExecutionEngine/JITLink/JITLink.h" + +namespace llvm { +namespace jitlink { +namespace aarch64 { + +/// Represets aarch64 fixups +enum EdgeKind_aarch64 : Edge::Kind { + + /// Set a CALL immediate field to bits [27:2] of X = Target - Fixup + Addend + R_AARCH64_CALL26 = Edge::FirstRelocation, + +}; + +/// Returns a string name for the given aarch64 edge. For debugging purposes +/// only +const char *getEdgeKindName(Edge::Kind K); + +} // namespace aarch64 +} // namespace jitlink +} // namespace llvm + +#endif // LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H diff --git a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt --- a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt +++ b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt @@ -13,13 +13,14 @@ MachOLinkGraphBuilder.cpp # ELF - ELF.cpp ELFLinkGraphBuilder.cpp + ELF_aarch64.cpp ELF_riscv.cpp ELF_x86_64.cpp # Architectures: + aarch64.cpp riscv.cpp x86_64.cpp @@ -35,7 +36,7 @@ OrcTargetProcess Support ) - + target_link_libraries(LLVMJITLink PRIVATE LLVMObject diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF.cpp --- a/llvm/lib/ExecutionEngine/JITLink/ELF.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/ELF.cpp @@ -13,6 +13,7 @@ #include "llvm/ExecutionEngine/JITLink/ELF.h" #include "llvm/BinaryFormat/ELF.h" +#include "llvm/ExecutionEngine/JITLink/ELF_aarch64.h" #include "llvm/ExecutionEngine/JITLink/ELF_riscv.h" #include "llvm/ExecutionEngine/JITLink/ELF_x86_64.h" #include "llvm/Object/ELF.h" @@ -64,6 +65,8 @@ return TargetMachineArch.takeError(); switch (*TargetMachineArch) { + case ELF::EM_AARCH64: + return createLinkGraphFromELFObject_aarch64(ObjectBuffer); case ELF::EM_RISCV: return createLinkGraphFromELFObject_riscv(ObjectBuffer); case ELF::EM_X86_64: @@ -78,6 +81,9 @@ void link_ELF(std::unique_ptr G, std::unique_ptr Ctx) { switch (G->getTargetTriple().getArch()) { + case Triple::aarch64: + link_ELF_aarch64(std::move(G), std::move(Ctx)); + return; case Triple::riscv32: case Triple::riscv64: link_ELF_riscv(std::move(G), std::move(Ctx)); diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp @@ -0,0 +1,220 @@ +//===----- ELF_aarch64.cpp - JIT linker implementation for ELF/aarch64 ----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// ELF/aarch64 jit-link implementation. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ExecutionEngine/JITLink/ELF_aarch64.h" +#include "ELFLinkGraphBuilder.h" +#include "JITLinkGeneric.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/ExecutionEngine/JITLink/aarch64.h" +#include "llvm/Object/ELFObjectFile.h" + +#define DEBUG_TYPE "jitlink" + +using namespace llvm; +using namespace llvm::jitlink; + +namespace llvm { +namespace jitlink { + +class ELFJITLinker_aarch64 : public JITLinker { + friend class JITLinker; + +public: + ELFJITLinker_aarch64(std::unique_ptr Ctx, + std::unique_ptr G, + PassConfiguration PassConfig) + : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {} + +private: + Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const { + using namespace aarch64; + using namespace llvm::support; + + char *BlockWorkingMem = B.getAlreadyMutableContent().data(); + char *FixupPtr = BlockWorkingMem + E.getOffset(); + JITTargetAddress FixupAddress = B.getAddress() + E.getOffset(); + switch (E.getKind()) { + case aarch64::R_AARCH64_CALL26: { + assert((FixupAddress & 0x3) == 0 && "Call-inst is not 32-bit aligned"); + int64_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend(); + + if (static_cast(Value) & 0x3) + return make_error("Call target is not 32-bit aligned"); + + if (!fitsRangeSignedInt<27>(Value)) + return makeTargetOutOfRangeError(G, B, E); + + uint32_t RawInstr = *(little32_t *)FixupPtr; + assert((RawInstr & 0x7fffffff) == 0x14000000 && + "RawInstr isn't a B or BR immediate instruction"); + uint32_t Imm = (static_cast(Value) & ((1 << 28) - 1)) >> 2; + uint32_t FixedInstr = RawInstr | Imm; + *(little32_t *)FixupPtr = FixedInstr; + break; + } + } + return Error::success(); + } + + template static bool fitsRangeSignedInt(int64_t Value) { + return Value >= -(1 << Bits) && Value < (1 << Bits); + } +}; + +template +class ELFLinkGraphBuilder_aarch64 : public ELFLinkGraphBuilder { +private: + static Expected + getRelocationKind(const uint32_t Type) { + using namespace aarch64; + switch (Type) { + case ELF::R_AARCH64_CALL26: + return EdgeKind_aarch64::R_AARCH64_CALL26; + } + + return make_error("Unsupported aarch64 relocation:" + + formatv("{0:d}", Type)); + } + + Error addRelocations() override { + using Base = ELFLinkGraphBuilder; + LLVM_DEBUG(dbgs() << "Adding relocations\n"); + + // Iterate sections and only process the interesting ones. + for (auto &SecRef : Base::Sections) { + if (SecRef.sh_type != ELF::SHT_RELA && SecRef.sh_type != ELF::SHT_REL) + continue; + auto RelSectName = Base::Obj.getSectionName(SecRef); + if (!RelSectName) + return RelSectName.takeError(); + + LLVM_DEBUG({ + dbgs() << "Adding relocations from section " << *RelSectName << "\n"; + }); + + auto UpdateSect = Base::Obj.getSection(SecRef.sh_info); + if (!UpdateSect) + return UpdateSect.takeError(); + + auto UpdateSectName = Base::Obj.getSectionName(**UpdateSect); + if (!UpdateSectName) + return UpdateSectName.takeError(); + + // Don't process relocations for debug sections. + if (Base::isDwarfSection(*UpdateSectName)) { + LLVM_DEBUG({ + dbgs() << " Target is dwarf section " << *UpdateSectName + << ". Skipping.\n"; + }); + continue; + } + LLVM_DEBUG(dbgs() << " For target section " << *UpdateSectName << "\n"); + + auto *JITSection = Base::G->findSectionByName(*UpdateSectName); + if (!JITSection) + return make_error( + "Refencing a section that wasn't added to graph" + *UpdateSectName, + llvm::inconvertibleErrorCode()); + + auto Relocations = Base::Obj.relas(SecRef); + if (!Relocations) + return Relocations.takeError(); + + for (const auto &Rela : *Relocations) { + auto Type = Rela.getType(false); + + LLVM_DEBUG({ + dbgs() << "Relocation Type: " << Type << "\n" + << "Name: " << Base::Obj.getRelocationTypeName(Type) << "\n"; + }); + + auto SymbolIndex = Rela.getSymbol(false); + auto Symbol = Base::Obj.getRelocationSymbol(Rela, Base::SymTabSec); + if (!Symbol) + return Symbol.takeError(); + + auto BlockToFix = *(JITSection->blocks().begin()); + auto *TargetSymbol = Base::getGraphSymbol(SymbolIndex); + + if (!TargetSymbol) { + return make_error( + "Could not find symbol at given index, did you add it to " + "JITSymbolTable? index: " + + std::to_string(SymbolIndex) + ", shndx: " + + std::to_string((*Symbol)->st_shndx) + " Size of table: " + + std::to_string(Base::GraphSymbols.size()), + llvm::inconvertibleErrorCode()); + } + int64_t Addend = Rela.r_addend; + JITTargetAddress FixupAddress = (*UpdateSect)->sh_addr + Rela.r_offset; + + LLVM_DEBUG({ + dbgs() << "Processing relocation at " + << format("0x%016" PRIx64, FixupAddress) << "\n"; + }); + auto Kind = getRelocationKind(Type); + if (!Kind) + return Kind.takeError(); + + BlockToFix->addEdge(*Kind, FixupAddress - BlockToFix->getAddress(), + *TargetSymbol, Addend); + } + } + return Error::success(); + } + +public: + ELFLinkGraphBuilder_aarch64(StringRef FileName, + const object::ELFFile &Obj, const Triple T) + : ELFLinkGraphBuilder(Obj, std::move(T), FileName, + aarch64::getEdgeKindName) {} +}; + +Expected> +createLinkGraphFromELFObject_aarch64(MemoryBufferRef ObjectBuffer) { + LLVM_DEBUG({ + dbgs() << "Building jitlink graph for new input " + << ObjectBuffer.getBufferIdentifier() << "...\n"; + }); + + auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer); + if (!ELFObj) + return ELFObj.takeError(); + + assert((*ELFObj)->getArch() == Triple::aarch64 && + "Only AArch64 (little endian) is supported for now"); + + auto &ELFObjFile = cast>(**ELFObj); + return ELFLinkGraphBuilder_aarch64((*ELFObj)->getFileName(), + ELFObjFile.getELFFile(), + (*ELFObj)->makeTriple()) + .buildGraph(); +} + +void link_ELF_aarch64(std::unique_ptr G, + std::unique_ptr Ctx) { + PassConfiguration Config; + const Triple &TT = G->getTargetTriple(); + if (Ctx->shouldAddDefaultTargetPasses(TT)) { + if (auto MarkLive = Ctx->getMarkLivePass(TT)) + Config.PrePrunePasses.push_back(std::move(MarkLive)); + else + Config.PrePrunePasses.push_back(markAllSymbolsLive); + } + if (auto Err = Ctx->modifyPassConfig(*G, Config)) + return Ctx->notifyFailed(std::move(Err)); + + ELFJITLinker_aarch64::link(std::move(Ctx), std::move(G), std::move(Config)); +} + +} // namespace jitlink +} // namespace llvm diff --git a/llvm/lib/ExecutionEngine/JITLink/aarch64.cpp b/llvm/lib/ExecutionEngine/JITLink/aarch64.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/ExecutionEngine/JITLink/aarch64.cpp @@ -0,0 +1,30 @@ +//===---- aarch64.cpp - Generic JITLink aarch64 edge kinds, utilities -----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Generic utilities for graphs representing aarch64 objects. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ExecutionEngine/JITLink/aarch64.h" + +#define DEBUG_TYPE "jitlink" + +namespace llvm { +namespace jitlink { +namespace aarch64 { + +const char *getEdgeKindName(Edge::Kind K) { + switch (K) { + case R_AARCH64_CALL26: + return "R_AARCH64_CALL26"; + } + return getGenericEdgeKindName(K); +} +} // namespace aarch64 +} // namespace jitlink +} // namespace llvm diff --git a/llvm/test/ExecutionEngine/JITLink/AArch64/ELF_aarch64_minimal.s b/llvm/test/ExecutionEngine/JITLink/AArch64/ELF_aarch64_minimal.s new file mode 100644 --- /dev/null +++ b/llvm/test/ExecutionEngine/JITLink/AArch64/ELF_aarch64_minimal.s @@ -0,0 +1,35 @@ +# RUN: rm -rf %t && mkdir -p %t +# RUN: llvm-mc -triple=aarch64-unknown-linux-gnu -relax-relocations=false -position-independent -filetype=obj -o %t/aarch64_reloc.o %s +# RUN: llvm-jitlink -noexec %t/aarch64_reloc.o + + .text + .globl sub1 + .p2align 2 + .type sub1,@function +sub1: + sub sp, sp, #16 + str w0, [sp, #12] + ldr w8, [sp, #12] + subs w0, w8, #1 + add sp, sp, #16 + ret + + .size sub1, .-sub1 + + .globl main + .p2align 2 + .type main,@function +main: + sub sp, sp, #32 + stp x29, x30, [sp, #16] + add x29, sp, #16 + stur wzr, [x29, #-4] + str w0, [sp, #8] + str x1, [sp] + ldr w0, [sp, #8] + bl sub1 + ldp x29, x30, [sp, #16] + add sp, sp, #32 + ret + + .size main, .-main