From aff57ff24aca5074b427d6bbc2f3246aa97910c5 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Sat, 26 Jun 2021 21:27:14 +1000 Subject: [PATCH] [JITLink][ELF] Add generic ELFLinkGraphBuilder template. ELFLinkGraphBuilder will hold generic parsing and LinkGraph-building code that can be shared between JITLink ELF backends for different architectures. For now it's just a stub. The plan is to incrementally move functionality down from ELFLinkGraphBuilder_x86_64 into the new template. --- llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt | 1 + .../JITLink/ELFLinkGraphBuilder.cpp | 23 +++++++++ .../ExecutionEngine/JITLink/ELFLinkGraphBuilder.h | 56 ++++++++++++++++++++++ llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp | 11 ++--- 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.cpp create mode 100644 llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h diff --git a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt index bb3b622..ab4b24a 100644 --- a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt +++ b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt @@ -15,6 +15,7 @@ add_llvm_component_library(LLVMJITLink # ELF ELF.cpp + ELFLinkGraphBuilder.cpp ELF_x86_64.cpp # Architectures: diff --git a/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.cpp new file mode 100644 index 0000000..d1e221b --- /dev/null +++ b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.cpp @@ -0,0 +1,23 @@ +//=----------- ELFLinkGraphBuilder.cpp - ELF LinkGraph builder ------------===// +// +// 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 ELF LinkGraph buliding code. +// +//===----------------------------------------------------------------------===// + +#include "ELFLinkGraphBuilder.h" + +#define DEBUG_TYPE "jitlink" + +namespace llvm { +namespace jitlink { + +ELFLinkGraphBuilderBase::~ELFLinkGraphBuilderBase() {} + +} // end namespace jitlink +} // end namespace llvm diff --git a/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h new file mode 100644 index 0000000..0841bdb --- /dev/null +++ b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h @@ -0,0 +1,56 @@ +//===------- ELFLinkGraphBuilder.h - ELF LinkGraph builder ------*- 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 ELF LinkGraph building code. +// +//===----------------------------------------------------------------------===// + +#ifndef LIB_EXECUTIONENGINE_JITLINK_ELFLINKGRAPHBUILDER_H +#define LIB_EXECUTIONENGINE_JITLINK_ELFLINKGRAPHBUILDER_H + +#include "llvm/ExecutionEngine/JITLink/JITLink.h" +#include "llvm/Object/ELF.h" +#include "llvm/Support/Error.h" + +namespace llvm { +namespace jitlink { + +/// Common link-graph building code shared between all ELFFiles. +class ELFLinkGraphBuilderBase { +public: + virtual ~ELFLinkGraphBuilderBase(); +}; + +/// Ling-graph building code that's specific to the given ELFT, but common +/// across all architectures. +template +class ELFLinkGraphBuilder : public ELFLinkGraphBuilderBase { +public: + ELFLinkGraphBuilder(const object::ELFFile &Obj, Triple TT, + StringRef FileName, + LinkGraph::GetEdgeKindNameFunction GetEdgeKindName); + +protected: + std::unique_ptr G; + const object::ELFFile &Obj; +}; + +template +ELFLinkGraphBuilder::ELFLinkGraphBuilder( + const object::ELFFile &Obj, Triple TT, StringRef FileName, + LinkGraph::GetEdgeKindNameFunction GetEdgeKindName) + : G(std::make_unique(FileName.str(), Triple(std::move(TT)), + ELFT::Is64Bits ? 8 : 4, + support::endianness(ELFT::TargetEndianness), + std::move(GetEdgeKindName))), + Obj(Obj) {} + +} // end namespace jitlink +} // end namespace llvm + +#endif // LIB_EXECUTIONENGINE_JITLINK_ELFLINKGRAPHBUILDER_H diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp index 41d4583..80f814f 100644 --- a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp @@ -18,6 +18,7 @@ #include "DefineExternalSectionStartAndEndSymbols.h" #include "EHFrameSupportImpl.h" +#include "ELFLinkGraphBuilder.h" #include "JITLinkGeneric.h" #include "PerGraphGOTAndPLTStubsBuilder.h" @@ -237,7 +238,7 @@ namespace jitlink { // This should become a template as the ELFFile is so a lot of this could become // generic -class ELFLinkGraphBuilder_x86_64 { +class ELFLinkGraphBuilder_x86_64 : public ELFLinkGraphBuilder { private: Section *CommonSection = nullptr; @@ -285,9 +286,7 @@ private: formatv("{0:d}", Type)); } - std::unique_ptr G; // This could be a template - const object::ELFFile &Obj; object::ELFFile::Elf_Shdr_Range sections; SymbolTable SymTab; @@ -685,10 +684,8 @@ private: public: ELFLinkGraphBuilder_x86_64(StringRef FileName, const object::ELFFile &Obj) - : G(std::make_unique( - FileName.str(), Triple("x86_64-unknown-linux"), getPointerSize(Obj), - getEndianness(Obj), getELFX86RelocationKindName)), - Obj(Obj) {} + : ELFLinkGraphBuilder(Obj, Triple("x86_64-unknown-linux"), FileName, + getELFX86RelocationKindName) {} Expected> buildGraph() { // Sanity check: we only operate on relocatable objects. -- 2.7.4