1 //===- RecordingMemoryManager.cpp - Recording memory manager --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This memory manager allocates local storage and keeps a record of each
11 // allocation. Iterators are provided for all data and code allocations.
13 //===----------------------------------------------------------------------===//
15 #include "RecordingMemoryManager.h"
18 RecordingMemoryManager::~RecordingMemoryManager() {
19 for (SmallVectorImpl<Allocation>::iterator
20 I = AllocatedCodeMem.begin(), E = AllocatedCodeMem.end();
22 sys::Memory::releaseMappedMemory(I->first);
23 for (SmallVectorImpl<Allocation>::iterator
24 I = AllocatedDataMem.begin(), E = AllocatedDataMem.end();
26 sys::Memory::releaseMappedMemory(I->first);
29 uint8_t *RecordingMemoryManager::
30 allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
31 StringRef SectionName) {
32 // The recording memory manager is just a local copy of the remote target.
33 // The alignment requirement is just stored here for later use. Regular
34 // heap storage is sufficient here, but we're using mapped memory to work
35 // around a bug in MCJIT.
36 sys::MemoryBlock Block = allocateSection(Size);
37 AllocatedCodeMem.push_back(Allocation(Block, Alignment));
38 return (uint8_t*)Block.base();
41 uint8_t *RecordingMemoryManager::
42 allocateDataSection(uintptr_t Size, unsigned Alignment,
43 unsigned SectionID, StringRef SectionName,
45 // The recording memory manager is just a local copy of the remote target.
46 // The alignment requirement is just stored here for later use. Regular
47 // heap storage is sufficient here, but we're using mapped memory to work
48 // around a bug in MCJIT.
49 sys::MemoryBlock Block = allocateSection(Size);
50 AllocatedDataMem.push_back(Allocation(Block, Alignment));
51 return (uint8_t*)Block.base();
54 sys::MemoryBlock RecordingMemoryManager::allocateSection(uintptr_t Size) {
56 sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(Size,
58 sys::Memory::MF_READ |
59 sys::Memory::MF_WRITE,
61 assert(!ec && MB.base());
63 // FIXME: This is part of a work around to keep sections near one another
64 // when MCJIT performs relocations after code emission but before
65 // the generated code is moved to the remote target.
66 // Save this address as the basis for our next request
71 void RecordingMemoryManager::setMemoryWritable() { llvm_unreachable("Unexpected!"); }
72 void RecordingMemoryManager::setMemoryExecutable() { llvm_unreachable("Unexpected!"); }
73 void RecordingMemoryManager::setPoisonMemory(bool poison) { llvm_unreachable("Unexpected!"); }
74 void RecordingMemoryManager::AllocateGOT() { llvm_unreachable("Unexpected!"); }
75 uint8_t *RecordingMemoryManager::getGOTBase() const {
76 llvm_unreachable("Unexpected!");
79 uint8_t *RecordingMemoryManager::startFunctionBody(const Function *F, uintptr_t &ActualSize){
80 llvm_unreachable("Unexpected!");
83 uint8_t *RecordingMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
85 llvm_unreachable("Unexpected!");
88 void RecordingMemoryManager::endFunctionBody(const Function *F, uint8_t *FunctionStart,
89 uint8_t *FunctionEnd) {
90 llvm_unreachable("Unexpected!");
92 uint8_t *RecordingMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment) {
93 llvm_unreachable("Unexpected!");
96 uint8_t *RecordingMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment) {
97 llvm_unreachable("Unexpected!");
100 void RecordingMemoryManager::deallocateFunctionBody(void *Body) {
101 llvm_unreachable("Unexpected!");
104 static int jit_noop() {
108 void *RecordingMemoryManager::getPointerToNamedFunction(const std::string &Name,
109 bool AbortOnFailure) {
110 // We should not invoke parent's ctors/dtors from generated main()!
111 // On Mingw and Cygwin, the symbol __main is resolved to
112 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
113 // (and register wrong callee's dtors with atexit(3)).
114 // We expect ExecutionEngine::runStaticConstructorsDestructors()
115 // is called before ExecutionEngine::runFunctionAsMain() is called.
116 if (Name == "__main") return (void*)(intptr_t)&jit_noop;
118 // FIXME: Would it be responsible to provide GOT?
119 if (AbortOnFailure) {
120 if (Name == "_GLOBAL_OFFSET_TABLE_")
121 report_fatal_error("Program used external function '" + Name +
122 "' which could not be resolved!");