1*700637cbSDimitry Andric //===------- XCOFF_ppc64.cpp -JIT linker implementation for XCOFF/ppc64
2*700637cbSDimitry Andric //-------===//
3*700637cbSDimitry Andric //
4*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*700637cbSDimitry Andric //
8*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
9*700637cbSDimitry Andric //
10*700637cbSDimitry Andric // XCOFF/ppc64 jit-link implementation.
11*700637cbSDimitry Andric //
12*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
13*700637cbSDimitry Andric
14*700637cbSDimitry Andric #include "llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h"
15*700637cbSDimitry Andric #include "JITLinkGeneric.h"
16*700637cbSDimitry Andric #include "XCOFFLinkGraphBuilder.h"
17*700637cbSDimitry Andric #include "llvm/ADT/bit.h"
18*700637cbSDimitry Andric #include "llvm/ExecutionEngine/JITLink/JITLink.h"
19*700637cbSDimitry Andric #include "llvm/ExecutionEngine/JITLink/ppc64.h"
20*700637cbSDimitry Andric #include "llvm/Object/ObjectFile.h"
21*700637cbSDimitry Andric #include "llvm/Object/XCOFFObjectFile.h"
22*700637cbSDimitry Andric #include "llvm/Support/Error.h"
23*700637cbSDimitry Andric #include "llvm/Support/ErrorHandling.h"
24*700637cbSDimitry Andric #include <system_error>
25*700637cbSDimitry Andric
26*700637cbSDimitry Andric using namespace llvm;
27*700637cbSDimitry Andric
28*700637cbSDimitry Andric #define DEBUG_TYPE "jitlink"
29*700637cbSDimitry Andric
30*700637cbSDimitry Andric namespace llvm {
31*700637cbSDimitry Andric namespace jitlink {
32*700637cbSDimitry Andric
createLinkGraphFromXCOFFObject_ppc64(MemoryBufferRef ObjectBuffer,std::shared_ptr<orc::SymbolStringPool> SSP)33*700637cbSDimitry Andric Expected<std::unique_ptr<LinkGraph>> createLinkGraphFromXCOFFObject_ppc64(
34*700637cbSDimitry Andric MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP) {
35*700637cbSDimitry Andric LLVM_DEBUG({
36*700637cbSDimitry Andric dbgs() << "Building jitlink graph for new input "
37*700637cbSDimitry Andric << ObjectBuffer.getBufferIdentifier() << "...\n";
38*700637cbSDimitry Andric });
39*700637cbSDimitry Andric
40*700637cbSDimitry Andric auto Obj = object::ObjectFile::createObjectFile(ObjectBuffer);
41*700637cbSDimitry Andric if (!Obj)
42*700637cbSDimitry Andric return Obj.takeError();
43*700637cbSDimitry Andric assert((**Obj).isXCOFF() && "Expects and XCOFF Object");
44*700637cbSDimitry Andric
45*700637cbSDimitry Andric auto Features = (*Obj)->getFeatures();
46*700637cbSDimitry Andric if (!Features)
47*700637cbSDimitry Andric return Features.takeError();
48*700637cbSDimitry Andric LLVM_DEBUG({
49*700637cbSDimitry Andric dbgs() << " Features: ";
50*700637cbSDimitry Andric (*Features).print(dbgs());
51*700637cbSDimitry Andric });
52*700637cbSDimitry Andric
53*700637cbSDimitry Andric return XCOFFLinkGraphBuilder(cast<object::XCOFFObjectFile>(**Obj),
54*700637cbSDimitry Andric std::move(SSP), Triple("powerpc64-ibm-aix"),
55*700637cbSDimitry Andric std::move(*Features), ppc64::getEdgeKindName)
56*700637cbSDimitry Andric .buildGraph();
57*700637cbSDimitry Andric }
58*700637cbSDimitry Andric
59*700637cbSDimitry Andric class XCOFFJITLinker_ppc64 : public JITLinker<XCOFFJITLinker_ppc64> {
60*700637cbSDimitry Andric using JITLinkerBase = JITLinker<XCOFFJITLinker_ppc64>;
61*700637cbSDimitry Andric friend JITLinkerBase;
62*700637cbSDimitry Andric
63*700637cbSDimitry Andric public:
XCOFFJITLinker_ppc64(std::unique_ptr<JITLinkContext> Ctx,std::unique_ptr<LinkGraph> G,PassConfiguration PassConfig)64*700637cbSDimitry Andric XCOFFJITLinker_ppc64(std::unique_ptr<JITLinkContext> Ctx,
65*700637cbSDimitry Andric std::unique_ptr<LinkGraph> G,
66*700637cbSDimitry Andric PassConfiguration PassConfig)
67*700637cbSDimitry Andric : JITLinkerBase(std::move(Ctx), std::move(G), std::move(PassConfig)) {
68*700637cbSDimitry Andric // FIXME: Post allocation pass define TOC base, this is temporary to support
69*700637cbSDimitry Andric // building until we can build the required toc entries
70*700637cbSDimitry Andric defineTOCSymbol(getGraph());
71*700637cbSDimitry Andric }
72*700637cbSDimitry Andric
applyFixup(LinkGraph & G,Block & B,const Edge & E) const73*700637cbSDimitry Andric Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
74*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << " Applying fixup for " << G.getName()
75*700637cbSDimitry Andric << ", address = " << B.getAddress()
76*700637cbSDimitry Andric << ", target = " << E.getTarget().getName() << ", kind = "
77*700637cbSDimitry Andric << ppc64::getEdgeKindName(E.getKind()) << "\n");
78*700637cbSDimitry Andric switch (E.getKind()) {
79*700637cbSDimitry Andric case ppc64::Pointer64:
80*700637cbSDimitry Andric if (auto Err = ppc64::applyFixup<endianness::big>(G, B, E, TOCSymbol))
81*700637cbSDimitry Andric return Err;
82*700637cbSDimitry Andric break;
83*700637cbSDimitry Andric default:
84*700637cbSDimitry Andric return make_error<StringError>("Unsupported relocation type",
85*700637cbSDimitry Andric std::error_code());
86*700637cbSDimitry Andric }
87*700637cbSDimitry Andric return Error::success();
88*700637cbSDimitry Andric }
89*700637cbSDimitry Andric
90*700637cbSDimitry Andric private:
defineTOCSymbol(LinkGraph & G)91*700637cbSDimitry Andric void defineTOCSymbol(LinkGraph &G) {
92*700637cbSDimitry Andric for (Symbol *S : G.defined_symbols()) {
93*700637cbSDimitry Andric if (S->hasName() && *S->getName() == StringRef("TOC")) {
94*700637cbSDimitry Andric TOCSymbol = S;
95*700637cbSDimitry Andric return;
96*700637cbSDimitry Andric }
97*700637cbSDimitry Andric }
98*700637cbSDimitry Andric llvm_unreachable("LinkGraph does not contan an TOC Symbol");
99*700637cbSDimitry Andric }
100*700637cbSDimitry Andric
101*700637cbSDimitry Andric private:
102*700637cbSDimitry Andric Symbol *TOCSymbol = nullptr;
103*700637cbSDimitry Andric };
104*700637cbSDimitry Andric
link_XCOFF_ppc64(std::unique_ptr<LinkGraph> G,std::unique_ptr<JITLinkContext> Ctx)105*700637cbSDimitry Andric void link_XCOFF_ppc64(std::unique_ptr<LinkGraph> G,
106*700637cbSDimitry Andric std::unique_ptr<JITLinkContext> Ctx) {
107*700637cbSDimitry Andric // Ctx->notifyFailed(make_error<StringError>(
108*700637cbSDimitry Andric // "link_XCOFF_ppc64 is not implemented", std::error_code()));
109*700637cbSDimitry Andric
110*700637cbSDimitry Andric PassConfiguration Config;
111*700637cbSDimitry Andric
112*700637cbSDimitry Andric // Pass insertions
113*700637cbSDimitry Andric
114*700637cbSDimitry Andric if (auto Err = Ctx->modifyPassConfig(*G, Config))
115*700637cbSDimitry Andric return Ctx->notifyFailed(std::move(Err));
116*700637cbSDimitry Andric
117*700637cbSDimitry Andric XCOFFJITLinker_ppc64::link(std::move(Ctx), std::move(G), std::move(Config));
118*700637cbSDimitry Andric }
119*700637cbSDimitry Andric
120*700637cbSDimitry Andric } // namespace jitlink
121*700637cbSDimitry Andric } // namespace llvm
122