llvm.org GIT mirror llvm / 7cbf92d
Tidy up a bit now that we're using the MemoryManager interface. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@129328 91177308-0d34-0410-b5e6-96231b3b80d8 Jim Grosbach 8 years ago
3 changed file(s) with 22 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
6262 // and resolve relocatons based on where they put it).
6363 void *getSymbolAddress(StringRef Name);
6464 void reassignSymbolAddress(StringRef Name, uint64_t Addr);
65 // FIXME: Should be parameterized to get the memory block associated with
66 // a particular loaded object.
67 sys::MemoryBlock getMemoryBlock();
6865 StringRef getErrorString();
6966 };
7067
5050
5151 // FIXME: Should have multiple data blocks, one for each loaded chunk of
5252 // compiled code.
53 sys::MemoryBlock Data;
53 // sys::MemoryBlock Data;
5454
5555 bool HasError;
5656 std::string ErrorStr;
8989 // Work in progress.
9090 return Functions.lookup(Name).base();
9191 }
92
93 sys::MemoryBlock getMemoryBlock() { return Data; }
9492
9593 // Is the linker in an error state?
9694 bool hasError() { return HasError; }
527525 return Dyld->getSymbolAddress(Name);
528526 }
529527
530 sys::MemoryBlock RuntimeDyld::getMemoryBlock() {
531 return Dyld->getMemoryBlock();
532 }
533
534528 StringRef RuntimeDyld::getErrorString() {
535529 return Dyld->getErrorString();
536530 }
4343 // support library allocation routines directly.
4444 class TrivialMemoryManager : public RTDyldMemoryManager {
4545 public:
46 SmallVector FunctionMemory;
47
4648 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size);
4749 void endFunctionBody(const char *Name, uint8_t *FunctionStart,
48 uint8_t *FunctionEnd) {}
50 uint8_t *FunctionEnd);
4951 };
5052
5153 uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name,
5254 uintptr_t &Size) {
5355 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base();
56 }
57
58 void TrivialMemoryManager::endFunctionBody(const char *Name,
59 uint8_t *FunctionStart,
60 uint8_t *FunctionEnd) {
61 uintptr_t Size = FunctionEnd - FunctionStart + 1;
62 FunctionMemory.push_back(sys::MemoryBlock(FunctionStart, Size));
5463 }
5564
5665 static const char *ProgramName;
7382 return Error("unable to read input: '" + ec.message() + "'");
7483
7584 // Instantiate a dynamic linker.
76 RuntimeDyld Dyld(new TrivialMemoryManager);
85 TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
86 RuntimeDyld Dyld(MemMgr);
7787
7888 // Load the object file into it.
7989 if (Dyld.loadObject(InputBuffer.take())) {
8595 if (MainAddress == 0)
8696 return Error("no definition for '_main'");
8797
88 // Invalidate the instruction cache.
89 sys::MemoryBlock Data = Dyld.getMemoryBlock();
90 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
98 // Invalidate the instruction cache for each loaded function.
99 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
100 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
101 // Make sure the memory is executable.
102 std::string ErrorStr;
103 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
104 if (!sys::Memory::setExecutable(Data, &ErrorStr))
105 return Error("unable to mark function executable: '" + ErrorStr + "'");
106 }
91107
92 // Make sure the memory is executable.
93 std::string ErrorStr;
94 if (!sys::Memory::setExecutable(Data, &ErrorStr))
95 return Error("unable to mark function executable: '" + ErrorStr + "'");
96108
97109 // Dispatch to _main().
98110 errs() << "loaded '_main' at: " << (void*)MainAddress << "\n";