Adding support and tests for multiple module handling in lli
[oota-llvm.git] / tools / lli / RemoteMemoryManager.h
1 //===- 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
15 #ifndef REMOTEMEMORYMANAGER_H
16 #define REMOTEMEMORYMANAGER_H
17
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ExecutionEngine/JITMemoryManager.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Memory.h"
23 #include <utility>
24
25 #include "RemoteTarget.h"
26
27 namespace llvm {
28
29 class RemoteMemoryManager : public JITMemoryManager {
30 public:
31   // Notice that this structure takes ownership of the memory allocated.
32   struct Allocation {
33     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
41 private:
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.
51   SmallVector<const Allocation *, 2>  UnmappedSections;
52
53   // This map tracks the sections we have remapped for the remote target
54   // but have not yet copied to the target.
55   DenseMap<uint64_t, const Allocation *>  MappedSections;
56
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
65 public:
66   RemoteMemoryManager() : Target(NULL) {}
67   virtual ~RemoteMemoryManager();
68
69   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
70                                unsigned SectionID, StringRef SectionName);
71
72   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
73                                unsigned SectionID, StringRef SectionName,
74                                bool IsReadOnly);
75
76   void *getPointerToNamedFunction(const std::string &Name,
77                                   bool AbortOnFailure = true);
78
79   void notifyObjectLoaded(ExecutionEngine *EE, const ObjectImage *Obj);
80
81   bool finalizeMemory(std::string *ErrMsg);
82
83   // This is a non-interface function used by lli
84   void setRemoteTarget(RemoteTarget *T) { Target = T; }
85
86   // The following obsolete JITMemoryManager calls are stubbed out for
87   // this model.
88   void setMemoryWritable();
89   void setMemoryExecutable();
90   void setPoisonMemory(bool poison);
91   void AllocateGOT();
92   uint8_t *getGOTBase() const;
93   uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize);
94   uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
95                         unsigned Alignment);
96   void endFunctionBody(const Function *F, uint8_t *FunctionStart,
97                        uint8_t *FunctionEnd);
98   uint8_t *allocateSpace(intptr_t Size, unsigned Alignment);
99   uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
100   void deallocateFunctionBody(void *Body);
101 };
102
103 } // end namespace llvm
104
105 #endif