[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
This commit is contained in:
Lang Hames 2022-08-14 21:13:37 -07:00
parent f45d89d73d
commit 5f300397c6
8 changed files with 251 additions and 0 deletions

View File

@ -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

View File

@ -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

View 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

View 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 " +

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,2 @@
if not 'i386' in config.root.targets:
config.unsupported = True