Fixed a bug with section names containing special characters.
[oota-llvm.git] / tools / lli / RecordingMemoryManager.cpp
1 //===- RecordingMemoryManager.cpp - 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 #include "RecordingMemoryManager.h"
16 using namespace llvm;
17
18 RecordingMemoryManager::~RecordingMemoryManager() {
19   for (SmallVectorImpl<Allocation>::iterator
20          I = AllocatedCodeMem.begin(), E = AllocatedCodeMem.end();
21        I != E; ++I)
22     sys::Memory::releaseMappedMemory(I->first);
23   for (SmallVectorImpl<Allocation>::iterator
24          I = AllocatedDataMem.begin(), E = AllocatedDataMem.end();
25        I != E; ++I)
26     sys::Memory::releaseMappedMemory(I->first);
27 }
28
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();
39 }
40
41 uint8_t *RecordingMemoryManager::
42 allocateDataSection(uintptr_t Size, unsigned Alignment,
43                     unsigned SectionID, StringRef SectionName,
44                     bool IsReadOnly) {
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();
52 }
53
54 sys::MemoryBlock RecordingMemoryManager::allocateSection(uintptr_t Size) {
55   error_code ec;
56   sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(Size,
57                                                           &Near,
58                                                           sys::Memory::MF_READ |
59                                                           sys::Memory::MF_WRITE,
60                                                           ec);
61   assert(!ec && MB.base());
62
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
67   Near = MB;
68   return MB;
69 }
70
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!");
77   return 0;
78 }
79 uint8_t *RecordingMemoryManager::startFunctionBody(const Function *F, uintptr_t &ActualSize){
80   llvm_unreachable("Unexpected!");
81   return 0;
82 }
83 uint8_t *RecordingMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
84                                               unsigned Alignment) {
85   llvm_unreachable("Unexpected!");
86   return 0;
87 }
88 void RecordingMemoryManager::endFunctionBody(const Function *F, uint8_t *FunctionStart,
89                                              uint8_t *FunctionEnd) {
90   llvm_unreachable("Unexpected!");
91 }
92 uint8_t *RecordingMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment) {
93   llvm_unreachable("Unexpected!");
94   return 0;
95 }
96 uint8_t *RecordingMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment) {
97   llvm_unreachable("Unexpected!");
98   return 0;
99 }
100 void RecordingMemoryManager::deallocateFunctionBody(void *Body) {
101   llvm_unreachable("Unexpected!");
102 }
103
104 static int jit_noop() {
105   return 0;
106 }
107
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;
117
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!");
123   }
124
125   return NULL;
126 }