[llvm] 5f30039 - [JITLink] Introduce ELF/i386 backend support for JITLink.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 15 18:35:56 PDT 2022


Author: Lang Hames
Date: 2022-08-15T18:35:51-07:00
New Revision: 5f300397c6ae8fa7ca3547ec2b7a3cd844f3ed59

URL: https://github.com/llvm/llvm-project/commit/5f300397c6ae8fa7ca3547ec2b7a3cd844f3ed59
DIFF: https://github.com/llvm/llvm-project/commit/5f300397c6ae8fa7ca3547ec2b7a3cd844f3ed59.diff

LOG: [JITLink] Introduce ELF/i386 backend support for JITLink.

This initial ELF/i386 JITLink backend enables JIT-linking of minimal ELF i386
object files. No relocations are supported yet.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D131347

Added: 
    llvm/include/llvm/ExecutionEngine/JITLink/ELF_i386.h
    llvm/include/llvm/ExecutionEngine/JITLink/i386.h
    llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp
    llvm/lib/ExecutionEngine/JITLink/i386.cpp
    llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_minimal.s
    llvm/test/ExecutionEngine/JITLink/i386/lit.local.cfg

Modified: 
    llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
    llvm/lib/ExecutionEngine/JITLink/ELF.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/JITLink/ELF_i386.h b/llvm/include/llvm/ExecutionEngine/JITLink/ELF_i386.h
new file mode 100644
index 0000000000000..44ebd96994611
--- /dev/null
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/ELF_i386.h
@@ -0,0 +1,39 @@
+//===--- ELF_i386.h - JIT link functions for ELF/i386 --*- 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/i386.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_JITLINK_ELF_I386_H
+#define LLVM_EXECUTIONENGINE_JITLINK_ELF_I386_H
+
+#include "llvm/ExecutionEngine/JITLink/JITLink.h"
+
+namespace llvm {
+namespace jitlink {
+
+/// Create a LinkGraph from an ELF/i386 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<std::unique_ptr<LinkGraph>>
+createLinkGraphFromELFObject_i386(MemoryBufferRef ObjectBuffer);
+
+/// jit-link the given object buffer, which must be a ELF i386 relocatable
+/// object file.
+void link_ELF_i386(std::unique_ptr<LinkGraph> G,
+                   std::unique_ptr<JITLinkContext> Ctx);
+
+} // end namespace jitlink
+} // end namespace llvm
+
+#endif // LLVM_EXECUTIONENGINE_JITLINK_ELF_I386_H

diff  --git a/llvm/include/llvm/ExecutionEngine/JITLink/i386.h b/llvm/include/llvm/ExecutionEngine/JITLink/i386.h
new file mode 100644
index 0000000000000..c43f32b9016fd
--- /dev/null
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/i386.h
@@ -0,0 +1,38 @@
+//=== i386.h - Generic JITLink i386 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 i386 objects.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_JITLINK_I386_H
+#define LLVM_EXECUTIONENGINE_JITLINK_I386_H
+
+#include "llvm/ExecutionEngine/JITLink/JITLink.h"
+
+namespace llvm {
+namespace jitlink {
+namespace i386 {
+
+/// Represets i386 fixups
+enum EdgeKind_i386 : Edge::Kind {
+
+  /// None
+  None = Edge::FirstRelocation,
+
+};
+
+/// Returns a string name for the given i386 edge. For debugging purposes
+/// only
+const char *getEdgeKindName(Edge::Kind K);
+
+} // namespace i386
+} // namespace jitlink
+} // namespace llvm
+
+#endif // LLVM_EXECUTIONENGINE_JITLINK_I386_H
\ No newline at end of file

diff  --git a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
index a1dbb8a908752..8e165c8c64fc1 100644
--- a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
@@ -22,6 +22,7 @@ add_llvm_component_library(LLVMJITLink
   ELF.cpp
   ELFLinkGraphBuilder.cpp
   ELF_aarch64.cpp
+  ELF_i386.cpp
   ELF_riscv.cpp
   ELF_x86_64.cpp
 
@@ -33,6 +34,7 @@ add_llvm_component_library(LLVMJITLink
 
   # Architectures:
   aarch64.cpp
+  i386.cpp
   riscv.cpp
   x86_64.cpp
 

diff  --git a/llvm/lib/ExecutionEngine/JITLink/ELF.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF.cpp
index eb98e4ba40418..9d55e6e2bf965 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF.cpp
@@ -14,6 +14,7 @@
 
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/ExecutionEngine/JITLink/ELF_aarch64.h"
+#include "llvm/ExecutionEngine/JITLink/ELF_i386.h"
 #include "llvm/ExecutionEngine/JITLink/ELF_riscv.h"
 #include "llvm/ExecutionEngine/JITLink/ELF_x86_64.h"
 #include "llvm/Object/ELF.h"
@@ -71,6 +72,8 @@ createLinkGraphFromELFObject(MemoryBufferRef ObjectBuffer) {
     return createLinkGraphFromELFObject_riscv(ObjectBuffer);
   case ELF::EM_X86_64:
     return createLinkGraphFromELFObject_x86_64(ObjectBuffer);
+  case ELF::EM_386:
+    return createLinkGraphFromELFObject_i386(ObjectBuffer);
   default:
     return make_error<JITLinkError>(
         "Unsupported target machine architecture in ELF object " +
@@ -91,6 +94,9 @@ void link_ELF(std::unique_ptr<LinkGraph> G,
   case Triple::x86_64:
     link_ELF_x86_64(std::move(G), std::move(Ctx));
     return;
+  case Triple::x86:
+    link_ELF_i386(std::move(G), std::move(Ctx));
+    return;
   default:
     Ctx->notifyFailed(make_error<JITLinkError>(
         "Unsupported target machine architecture in ELF link graph " +

diff  --git a/llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp
new file mode 100644
index 0000000000000..c6ddd32d2561d
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp
@@ -0,0 +1,116 @@
+//===----- ELF_i386.cpp - JIT linker implementation for ELF/i386 ----===//
+//
+// 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/i386 jit-link implementation.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/JITLink/ELF_i386.h"
+#include "ELFLinkGraphBuilder.h"
+#include "JITLinkGeneric.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/ExecutionEngine/JITLink/i386.h"
+#include "llvm/Object/ELFObjectFile.h"
+
+#define DEBUG_TYPE "jitlink"
+
+using namespace llvm;
+using namespace llvm::jitlink;
+
+namespace llvm {
+namespace jitlink {
+
+class ELFJITLinker_i386 : public JITLinker<ELFJITLinker_i386> {
+  friend class JITLinker<ELFJITLinker_i386>;
+
+public:
+  ELFJITLinker_i386(std::unique_ptr<JITLinkContext> Ctx,
+                    std::unique_ptr<LinkGraph> 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 i386;
+    using namespace llvm::support;
+
+    switch (E.getKind()) {
+    case i386::None: {
+      break;
+    }
+    }
+    return Error::success();
+  }
+};
+
+template <typename ELFT>
+class ELFLinkGraphBuilder_i386 : public ELFLinkGraphBuilder<ELFT> {
+private:
+  static Expected<i386::EdgeKind_i386> getRelocationKind(const uint32_t Type) {
+    using namespace i386;
+    switch (Type) {
+    case ELF::R_386_NONE:
+      return EdgeKind_i386::None;
+    }
+
+    return make_error<JITLinkError>("Unsupported i386 relocation:" +
+                                    formatv("{0:d}", Type));
+  }
+
+  Error addRelocations() override {
+    LLVM_DEBUG(dbgs() << "Adding relocations\n");
+    using Base = ELFLinkGraphBuilder<ELFT>;
+
+    return Error::success();
+  }
+
+public:
+  ELFLinkGraphBuilder_i386(StringRef FileName, const object::ELFFile<ELFT> &Obj,
+                           const Triple T)
+      : ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName,
+                                  i386::getEdgeKindName) {}
+};
+
+Expected<std::unique_ptr<LinkGraph>>
+createLinkGraphFromELFObject_i386(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::x86 &&
+         "Only i386 (little endian) is supported for now");
+
+  auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj);
+  return ELFLinkGraphBuilder_i386<object::ELF32LE>((*ELFObj)->getFileName(),
+                                                   ELFObjFile.getELFFile(),
+                                                   (*ELFObj)->makeTriple())
+      .buildGraph();
+}
+
+void link_ELF_i386(std::unique_ptr<LinkGraph> G,
+                   std::unique_ptr<JITLinkContext> 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_i386::link(std::move(Ctx), std::move(G), std::move(Config));
+}
+
+} // namespace jitlink
+} // namespace llvm
\ No newline at end of file

diff  --git a/llvm/lib/ExecutionEngine/JITLink/i386.cpp b/llvm/lib/ExecutionEngine/JITLink/i386.cpp
new file mode 100644
index 0000000000000..ef5b7a0ba8fc7
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/JITLink/i386.cpp
@@ -0,0 +1,30 @@
+//===---- i386.cpp - Generic JITLink i386 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 i386 objects.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/JITLink/i386.h"
+
+#define DEBUG_TYPE "jitlink"
+
+namespace llvm {
+namespace jitlink {
+namespace i386 {
+
+const char *getEdgeKindName(Edge::Kind K) {
+  switch (K) {
+  case None:
+    return "None";
+  }
+  return getGenericEdgeKindName(K);
+}
+} // namespace i386
+} // namespace jitlink
+} // namespace llvm
\ No newline at end of file

diff  --git a/llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_minimal.s b/llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_minimal.s
new file mode 100644
index 0000000000000..5b628f875423c
--- /dev/null
+++ b/llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_minimal.s
@@ -0,0 +1,18 @@
+# RUN: llvm-mc -triple=i386-unknown-linux-gnu -position-independent -filetype=obj -o %t.o %s
+# RUN: llvm-jitlink -noexec %t.o
+
+	.text
+	.globl	main
+	.p2align	4
+	.type	main, at function
+main:
+    pushl   %ebp
+    movl    %esp, %ebp
+    pushl   %eax
+    movl    $0, -4(%ebp)
+    movl    $42, %eax
+    addl    $4, %esp
+    popl    %ebp
+    retl
+
+	.size	main, .-main
\ No newline at end of file

diff  --git a/llvm/test/ExecutionEngine/JITLink/i386/lit.local.cfg b/llvm/test/ExecutionEngine/JITLink/i386/lit.local.cfg
new file mode 100644
index 0000000000000..88cde621da28c
--- /dev/null
+++ b/llvm/test/ExecutionEngine/JITLink/i386/lit.local.cfg
@@ -0,0 +1,2 @@
+if not 'i386' in config.root.targets:
+  config.unsupported = True
\ No newline at end of file


        


More information about the llvm-commits mailing list