Re-apply "[JITLink] Introduce ELF/i386 backend " with correct authorship.
authorKshitij Jain <jkshtj@outlook.com>
Tue, 16 Aug 2022 01:39:34 +0000 (18:39 -0700)
committerLang Hames <lhames@gmail.com>
Tue, 16 Aug 2022 01:44:43 +0000 (18:44 -0700)
I (lhames) accidentally pushed 5f300397c6ae8fa7ca3547ec2b7a3cd844f3ed59 on
Kshitij Jain's behalf without updating the patch author first (my apologies
Kshitij!).

Re-applying with correct authorship.

https://reviews.llvm.org/D131347

llvm/include/llvm/ExecutionEngine/JITLink/ELF_i386.h [new file with mode: 0644]
llvm/include/llvm/ExecutionEngine/JITLink/i386.h [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
llvm/lib/ExecutionEngine/JITLink/ELF.cpp
llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp [new file with mode: 0644]
llvm/lib/ExecutionEngine/JITLink/i386.cpp [new file with mode: 0644]
llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_minimal.s [new file with mode: 0644]
llvm/test/ExecutionEngine/JITLink/i386/lit.local.cfg [new file with mode: 0644]

diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/ELF_i386.h b/llvm/include/llvm/ExecutionEngine/JITLink/ELF_i386.h
new file mode 100644 (file)
index 0000000..44ebd96
--- /dev/null
@@ -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 (file)
index 0000000..c43f32b
--- /dev/null
@@ -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
index a1dbb8a..8e165c8 100644 (file)
@@ -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
 
index eb98e4b..9d55e6e 100644 (file)
@@ -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 (file)
index 0000000..c6ddd32
--- /dev/null
@@ -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 (file)
index 0000000..ef5b7a0
--- /dev/null
@@ -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 (file)
index 0000000..5b628f8
--- /dev/null
@@ -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,@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 (file)
index 0000000..88cde62
--- /dev/null
@@ -0,0 +1,2 @@
+if not 'i386' in config.root.targets:
+  config.unsupported = True
\ No newline at end of file