blob: 2ce00e45586c4eb406b9f14df80fb534d8d215d9 [file] [log] [blame]
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +00001//===- RemoteMemoryManager.h - LLI MCJIT recording memory manager ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This memory manager allocates local storage and keeps a record of each
11// allocation. Iterators are provided for all data and code allocations.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_TOOLS_LLI_REMOTEMEMORYMANAGER_H
16#define LLVM_TOOLS_LLI_REMOTEMEMORYMANAGER_H
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000017
Chandler Carruth07baed52014-01-13 08:04:33 +000018#include "RemoteTarget.h"
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ExecutionEngine/JITMemoryManager.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/Memory.h"
24#include <utility>
25
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000026namespace llvm {
27
28class RemoteMemoryManager : public JITMemoryManager {
29public:
30 // Notice that this structure takes ownership of the memory allocated.
31 struct Allocation {
Andrew Kaylor97231762013-10-04 20:09:36 +000032 Allocation() {}
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000033 Allocation(sys::MemoryBlock mb, unsigned a, bool code)
34 : MB(mb), Alignment(a), IsCode(code) {}
35
36 sys::MemoryBlock MB;
37 unsigned Alignment;
38 bool IsCode;
39 };
40
41private:
42 // This vector contains Allocation objects for all sections which we have
43 // allocated. This vector effectively owns the memory associated with the
44 // allocations.
45 SmallVector<Allocation, 2> AllocatedSections;
46
47 // This vector contains pointers to Allocation objects for any sections we
48 // have allocated locally but have not yet remapped for the remote target.
49 // When we receive notification of a completed module load, we will map
50 // these sections into the remote target.
Andrew Kaylor97231762013-10-04 20:09:36 +000051 SmallVector<Allocation, 2> UnmappedSections;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000052
53 // This map tracks the sections we have remapped for the remote target
54 // but have not yet copied to the target.
Andrew Kaylor97231762013-10-04 20:09:36 +000055 DenseMap<uint64_t, Allocation> MappedSections;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000056
57 // FIXME: This is part of a work around to keep sections near one another
58 // when MCJIT performs relocations after code emission but before
59 // the generated code is moved to the remote target.
60 sys::MemoryBlock Near;
61 sys::MemoryBlock allocateSection(uintptr_t Size);
62
63 RemoteTarget *Target;
64
65public:
Craig Toppere73658d2014-04-28 04:05:08 +000066 RemoteMemoryManager() : Target(nullptr) {}
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000067 virtual ~RemoteMemoryManager();
68
69 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
Craig Toppere56917c2014-03-08 08:27:28 +000070 unsigned SectionID,
71 StringRef SectionName) override;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000072
73 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
74 unsigned SectionID, StringRef SectionName,
Craig Toppere56917c2014-03-08 08:27:28 +000075 bool IsReadOnly) override;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000076
Andrew Kaylor7bb13442013-10-11 21:25:48 +000077 // For now, remote symbol resolution is not support in lli. The MCJIT
78 // interface does support this, but clients must provide their own
79 // mechanism for finding remote symbol addresses. MCJIT will resolve
80 // symbols from Modules it contains.
Craig Toppere56917c2014-03-08 08:27:28 +000081 uint64_t getSymbolAddress(const std::string &Name) override { return 0; }
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000082
Craig Toppere56917c2014-03-08 08:27:28 +000083 void notifyObjectLoaded(ExecutionEngine *EE, const ObjectImage *Obj) override;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000084
Craig Toppere56917c2014-03-08 08:27:28 +000085 bool finalizeMemory(std::string *ErrMsg) override;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000086
Andrew Kaylor7bb13442013-10-11 21:25:48 +000087 // For now, remote EH frame registration isn't supported. Remote symbol
88 // resolution is a prerequisite to supporting remote EH frame registration.
Craig Toppere56917c2014-03-08 08:27:28 +000089 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
90 size_t Size) override {}
91 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
92 size_t Size) override {}
Andrew Kaylor7bb13442013-10-11 21:25:48 +000093
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000094 // This is a non-interface function used by lli
95 void setRemoteTarget(RemoteTarget *T) { Target = T; }
96
97 // The following obsolete JITMemoryManager calls are stubbed out for
98 // this model.
Craig Toppere56917c2014-03-08 08:27:28 +000099 void setMemoryWritable() override;
100 void setMemoryExecutable() override;
101 void setPoisonMemory(bool poison) override;
102 void AllocateGOT() override;
103 uint8_t *getGOTBase() const override;
104 uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize) override;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000105 uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
Craig Toppere56917c2014-03-08 08:27:28 +0000106 unsigned Alignment) override;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000107 void endFunctionBody(const Function *F, uint8_t *FunctionStart,
Craig Toppere56917c2014-03-08 08:27:28 +0000108 uint8_t *FunctionEnd) override;
109 uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) override;
110 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) override;
111 void deallocateFunctionBody(void *Body) override;
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000112};
113
114} // end namespace llvm
115
116#endif