RecordingMemoryManager.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "RecordingMemoryManager.h"
  15. using namespace llvm;
  16. RecordingMemoryManager::~RecordingMemoryManager() {
  17. for (SmallVectorImpl<Allocation>::iterator
  18. I = AllocatedCodeMem.begin(), E = AllocatedCodeMem.end();
  19. I != E; ++I)
  20. sys::Memory::releaseMappedMemory(I->first);
  21. for (SmallVectorImpl<Allocation>::iterator
  22. I = AllocatedDataMem.begin(), E = AllocatedDataMem.end();
  23. I != E; ++I)
  24. sys::Memory::releaseMappedMemory(I->first);
  25. }
  26. uint8_t *RecordingMemoryManager::
  27. allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
  28. StringRef SectionName) {
  29. // The recording memory manager is just a local copy of the remote target.
  30. // The alignment requirement is just stored here for later use. Regular
  31. // heap storage is sufficient here, but we're using mapped memory to work
  32. // around a bug in MCJIT.
  33. sys::MemoryBlock Block = allocateSection(Size);
  34. AllocatedCodeMem.push_back(Allocation(Block, Alignment));
  35. return (uint8_t*)Block.base();
  36. }
  37. uint8_t *RecordingMemoryManager::
  38. allocateDataSection(uintptr_t Size, unsigned Alignment,
  39. unsigned SectionID, StringRef SectionName,
  40. bool IsReadOnly) {
  41. // The recording memory manager is just a local copy of the remote target.
  42. // The alignment requirement is just stored here for later use. Regular
  43. // heap storage is sufficient here, but we're using mapped memory to work
  44. // around a bug in MCJIT.
  45. sys::MemoryBlock Block = allocateSection(Size);
  46. AllocatedDataMem.push_back(Allocation(Block, Alignment));
  47. return (uint8_t*)Block.base();
  48. }
  49. sys::MemoryBlock RecordingMemoryManager::allocateSection(uintptr_t Size) {
  50. error_code ec;
  51. sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(Size,
  52. &Near,
  53. sys::Memory::MF_READ |
  54. sys::Memory::MF_WRITE,
  55. ec);
  56. assert(!ec && MB.base());
  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. // Save this address as the basis for our next request
  61. Near = MB;
  62. return MB;
  63. }
  64. void RecordingMemoryManager::setMemoryWritable() { llvm_unreachable("Unexpected!"); }
  65. void RecordingMemoryManager::setMemoryExecutable() { llvm_unreachable("Unexpected!"); }
  66. void RecordingMemoryManager::setPoisonMemory(bool poison) { llvm_unreachable("Unexpected!"); }
  67. void RecordingMemoryManager::AllocateGOT() { llvm_unreachable("Unexpected!"); }
  68. uint8_t *RecordingMemoryManager::getGOTBase() const {
  69. llvm_unreachable("Unexpected!");
  70. return 0;
  71. }
  72. uint8_t *RecordingMemoryManager::startFunctionBody(const Function *F, uintptr_t &ActualSize){
  73. llvm_unreachable("Unexpected!");
  74. return 0;
  75. }
  76. uint8_t *RecordingMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
  77. unsigned Alignment) {
  78. llvm_unreachable("Unexpected!");
  79. return 0;
  80. }
  81. void RecordingMemoryManager::endFunctionBody(const Function *F, uint8_t *FunctionStart,
  82. uint8_t *FunctionEnd) {
  83. llvm_unreachable("Unexpected!");
  84. }
  85. uint8_t *RecordingMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment) {
  86. llvm_unreachable("Unexpected!");
  87. return 0;
  88. }
  89. uint8_t *RecordingMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment) {
  90. llvm_unreachable("Unexpected!");
  91. return 0;
  92. }
  93. void RecordingMemoryManager::deallocateFunctionBody(void *Body) {
  94. llvm_unreachable("Unexpected!");
  95. }
  96. static int jit_noop() {
  97. return 0;
  98. }
  99. void *RecordingMemoryManager::getPointerToNamedFunction(const std::string &Name,
  100. bool AbortOnFailure) {
  101. // We should not invoke parent's ctors/dtors from generated main()!
  102. // On Mingw and Cygwin, the symbol __main is resolved to
  103. // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
  104. // (and register wrong callee's dtors with atexit(3)).
  105. // We expect ExecutionEngine::runStaticConstructorsDestructors()
  106. // is called before ExecutionEngine::runFunctionAsMain() is called.
  107. if (Name == "__main") return (void*)(intptr_t)&jit_noop;
  108. // FIXME: Would it be responsible to provide GOT?
  109. if (AbortOnFailure) {
  110. if (Name == "_GLOBAL_OFFSET_TABLE_")
  111. report_fatal_error("Program used external function '" + Name +
  112. "' which could not be resolved!");
  113. }
  114. return NULL;
  115. }