Skip to content

Commit 29fe204

Browse files
jkshtjlhames
authored andcommitted
Re-apply "[JITLink] Introduce ELF/i386 backend " with correct authorship.
I (lhames) accidentally pushed 5f30039 on Kshitij Jain's behalf without updating the patch author first (my apologies Kshitij!). Re-applying with correct authorship. https://reviews.llvm.org/D131347
1 parent 73600b7 commit 29fe204

File tree

8 files changed

+251
-0
lines changed

8 files changed

+251
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- ELF_i386.h - JIT link functions for ELF/i386 --*- C++ -*----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// jit-link functions for ELF/i386.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_EXECUTIONENGINE_JITLINK_ELF_I386_H
16+
#define LLVM_EXECUTIONENGINE_JITLINK_ELF_I386_H
17+
18+
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
19+
20+
namespace llvm {
21+
namespace jitlink {
22+
23+
/// Create a LinkGraph from an ELF/i386 relocatable object
24+
///
25+
/// Note: The graph does not take ownership of the underlying buffer, nor copy
26+
/// its contents. The caller is responsible for ensuring that the object buffer
27+
/// outlives the graph.
28+
Expected<std::unique_ptr<LinkGraph>>
29+
createLinkGraphFromELFObject_i386(MemoryBufferRef ObjectBuffer);
30+
31+
/// jit-link the given object buffer, which must be a ELF i386 relocatable
32+
/// object file.
33+
void link_ELF_i386(std::unique_ptr<LinkGraph> G,
34+
std::unique_ptr<JITLinkContext> Ctx);
35+
36+
} // end namespace jitlink
37+
} // end namespace llvm
38+
39+
#endif // LLVM_EXECUTIONENGINE_JITLINK_ELF_I386_H
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//=== i386.h - Generic JITLink i386 edge kinds, utilities -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Generic utilities for graphs representing i386 objects.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_EXECUTIONENGINE_JITLINK_I386_H
14+
#define LLVM_EXECUTIONENGINE_JITLINK_I386_H
15+
16+
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
17+
18+
namespace llvm {
19+
namespace jitlink {
20+
namespace i386 {
21+
22+
/// Represets i386 fixups
23+
enum EdgeKind_i386 : Edge::Kind {
24+
25+
/// None
26+
None = Edge::FirstRelocation,
27+
28+
};
29+
30+
/// Returns a string name for the given i386 edge. For debugging purposes
31+
/// only
32+
const char *getEdgeKindName(Edge::Kind K);
33+
34+
} // namespace i386
35+
} // namespace jitlink
36+
} // namespace llvm
37+
38+
#endif // LLVM_EXECUTIONENGINE_JITLINK_I386_H

llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_llvm_component_library(LLVMJITLink
2222
ELF.cpp
2323
ELFLinkGraphBuilder.cpp
2424
ELF_aarch64.cpp
25+
ELF_i386.cpp
2526
ELF_riscv.cpp
2627
ELF_x86_64.cpp
2728

@@ -33,6 +34,7 @@ add_llvm_component_library(LLVMJITLink
3334

3435
# Architectures:
3536
aarch64.cpp
37+
i386.cpp
3638
riscv.cpp
3739
x86_64.cpp
3840

llvm/lib/ExecutionEngine/JITLink/ELF.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "llvm/BinaryFormat/ELF.h"
1616
#include "llvm/ExecutionEngine/JITLink/ELF_aarch64.h"
17+
#include "llvm/ExecutionEngine/JITLink/ELF_i386.h"
1718
#include "llvm/ExecutionEngine/JITLink/ELF_riscv.h"
1819
#include "llvm/ExecutionEngine/JITLink/ELF_x86_64.h"
1920
#include "llvm/Object/ELF.h"
@@ -71,6 +72,8 @@ createLinkGraphFromELFObject(MemoryBufferRef ObjectBuffer) {
7172
return createLinkGraphFromELFObject_riscv(ObjectBuffer);
7273
case ELF::EM_X86_64:
7374
return createLinkGraphFromELFObject_x86_64(ObjectBuffer);
75+
case ELF::EM_386:
76+
return createLinkGraphFromELFObject_i386(ObjectBuffer);
7477
default:
7578
return make_error<JITLinkError>(
7679
"Unsupported target machine architecture in ELF object " +
@@ -91,6 +94,9 @@ void link_ELF(std::unique_ptr<LinkGraph> G,
9194
case Triple::x86_64:
9295
link_ELF_x86_64(std::move(G), std::move(Ctx));
9396
return;
97+
case Triple::x86:
98+
link_ELF_i386(std::move(G), std::move(Ctx));
99+
return;
94100
default:
95101
Ctx->notifyFailed(make_error<JITLinkError>(
96102
"Unsupported target machine architecture in ELF link graph " +
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//===----- ELF_i386.cpp - JIT linker implementation for ELF/i386 ----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// ELF/i386 jit-link implementation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/ExecutionEngine/JITLink/ELF_i386.h"
14+
#include "ELFLinkGraphBuilder.h"
15+
#include "JITLinkGeneric.h"
16+
#include "llvm/BinaryFormat/ELF.h"
17+
#include "llvm/ExecutionEngine/JITLink/i386.h"
18+
#include "llvm/Object/ELFObjectFile.h"
19+
20+
#define DEBUG_TYPE "jitlink"
21+
22+
using namespace llvm;
23+
using namespace llvm::jitlink;
24+
25+
namespace llvm {
26+
namespace jitlink {
27+
28+
class ELFJITLinker_i386 : public JITLinker<ELFJITLinker_i386> {
29+
friend class JITLinker<ELFJITLinker_i386>;
30+
31+
public:
32+
ELFJITLinker_i386(std::unique_ptr<JITLinkContext> Ctx,
33+
std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig)
34+
: JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
35+
36+
private:
37+
Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
38+
using namespace i386;
39+
using namespace llvm::support;
40+
41+
switch (E.getKind()) {
42+
case i386::None: {
43+
break;
44+
}
45+
}
46+
return Error::success();
47+
}
48+
};
49+
50+
template <typename ELFT>
51+
class ELFLinkGraphBuilder_i386 : public ELFLinkGraphBuilder<ELFT> {
52+
private:
53+
static Expected<i386::EdgeKind_i386> getRelocationKind(const uint32_t Type) {
54+
using namespace i386;
55+
switch (Type) {
56+
case ELF::R_386_NONE:
57+
return EdgeKind_i386::None;
58+
}
59+
60+
return make_error<JITLinkError>("Unsupported i386 relocation:" +
61+
formatv("{0:d}", Type));
62+
}
63+
64+
Error addRelocations() override {
65+
LLVM_DEBUG(dbgs() << "Adding relocations\n");
66+
using Base = ELFLinkGraphBuilder<ELFT>;
67+
68+
return Error::success();
69+
}
70+
71+
public:
72+
ELFLinkGraphBuilder_i386(StringRef FileName, const object::ELFFile<ELFT> &Obj,
73+
const Triple T)
74+
: ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName,
75+
i386::getEdgeKindName) {}
76+
};
77+
78+
Expected<std::unique_ptr<LinkGraph>>
79+
createLinkGraphFromELFObject_i386(MemoryBufferRef ObjectBuffer) {
80+
LLVM_DEBUG({
81+
dbgs() << "Building jitlink graph for new input "
82+
<< ObjectBuffer.getBufferIdentifier() << "...\n";
83+
});
84+
85+
auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
86+
if (!ELFObj)
87+
return ELFObj.takeError();
88+
89+
assert((*ELFObj)->getArch() == Triple::x86 &&
90+
"Only i386 (little endian) is supported for now");
91+
92+
auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj);
93+
return ELFLinkGraphBuilder_i386<object::ELF32LE>((*ELFObj)->getFileName(),
94+
ELFObjFile.getELFFile(),
95+
(*ELFObj)->makeTriple())
96+
.buildGraph();
97+
}
98+
99+
void link_ELF_i386(std::unique_ptr<LinkGraph> G,
100+
std::unique_ptr<JITLinkContext> Ctx) {
101+
PassConfiguration Config;
102+
const Triple &TT = G->getTargetTriple();
103+
if (Ctx->shouldAddDefaultTargetPasses(TT)) {
104+
if (auto MarkLive = Ctx->getMarkLivePass(TT))
105+
Config.PrePrunePasses.push_back(std::move(MarkLive));
106+
else
107+
Config.PrePrunePasses.push_back(markAllSymbolsLive);
108+
}
109+
if (auto Err = Ctx->modifyPassConfig(*G, Config))
110+
return Ctx->notifyFailed(std::move(Err));
111+
112+
ELFJITLinker_i386::link(std::move(Ctx), std::move(G), std::move(Config));
113+
}
114+
115+
} // namespace jitlink
116+
} // namespace llvm
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===---- i386.cpp - Generic JITLink i386 edge kinds, utilities -----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Generic utilities for graphs representing i386 objects.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/ExecutionEngine/JITLink/i386.h"
14+
15+
#define DEBUG_TYPE "jitlink"
16+
17+
namespace llvm {
18+
namespace jitlink {
19+
namespace i386 {
20+
21+
const char *getEdgeKindName(Edge::Kind K) {
22+
switch (K) {
23+
case None:
24+
return "None";
25+
}
26+
return getGenericEdgeKindName(K);
27+
}
28+
} // namespace i386
29+
} // namespace jitlink
30+
} // namespace llvm
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RUN: llvm-mc -triple=i386-unknown-linux-gnu -position-independent -filetype=obj -o %t.o %s
2+
# RUN: llvm-jitlink -noexec %t.o
3+
4+
.text
5+
.globl main
6+
.p2align 4
7+
.type main,@function
8+
main:
9+
pushl %ebp
10+
movl %esp, %ebp
11+
pushl %eax
12+
movl $0, -4(%ebp)
13+
movl $42, %eax
14+
addl $4, %esp
15+
popl %ebp
16+
retl
17+
18+
.size main, .-main
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if not 'i386' in config.root.targets:
2+
config.unsupported = True

0 commit comments

Comments
 (0)