2011-07-13 07:57:58 +00:00
|
|
|
//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
|
2011-03-21 22:15:52 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implementation of the MC-JIT runtime dynamic linker.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-03-23 19:52:00 +00:00
|
|
|
#define DEBUG_TYPE "dyld"
|
2011-07-13 07:57:58 +00:00
|
|
|
#include "RuntimeDyldImpl.h"
|
2011-03-21 22:15:52 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2011-04-05 23:54:31 +00:00
|
|
|
// Empty out-of-line virtual destructor as the key function.
|
|
|
|
RTDyldMemoryManager::~RTDyldMemoryManager() {}
|
2011-07-13 07:57:58 +00:00
|
|
|
RuntimeDyldImpl::~RuntimeDyldImpl() {}
|
2011-04-05 23:54:31 +00:00
|
|
|
|
2011-03-21 22:15:52 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2011-04-06 01:11:05 +00:00
|
|
|
void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
|
2011-04-06 22:13:52 +00:00
|
|
|
uint8_t *EndAddress) {
|
2011-04-06 01:11:05 +00:00
|
|
|
// Allocate memory for the function via the memory manager.
|
|
|
|
uintptr_t Size = EndAddress - StartAddress + 1;
|
2011-05-13 20:12:14 +00:00
|
|
|
uintptr_t AllocSize = Size;
|
|
|
|
uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize);
|
2011-04-06 01:11:05 +00:00
|
|
|
assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) &&
|
|
|
|
"Memory manager failed to allocate enough memory!");
|
|
|
|
// Copy the function payload into the memory block.
|
2011-05-13 20:12:14 +00:00
|
|
|
memcpy(Mem, StartAddress, Size);
|
2011-04-06 01:11:05 +00:00
|
|
|
MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size);
|
|
|
|
// Remember where we put it.
|
|
|
|
Functions[Name] = sys::MemoryBlock(Mem, Size);
|
MCJIT lazy relocation resolution and symbol address re-assignment.
Add handling for tracking the relocations on symbols and resolving them.
Keep track of the relocations even after they are resolved so that if
the RuntimeDyld client moves the object, it can update the address and any
relocations to that object will be updated.
For our trival object file load/run test harness (llvm-rtdyld), this enables
relocations between functions located in the same object module. It should
be trivially extendable to load multiple objects with mutual references.
As a simple example, the following now works (running on x86_64 Darwin 10.6):
$ cat t.c
int bar() {
return 65;
}
int main() {
return bar();
}
$ clang t.c -fno-asynchronous-unwind-tables -o t.o -c
$ otool -vt t.o
t.o:
(__TEXT,__text) section
_bar:
0000000000000000 pushq %rbp
0000000000000001 movq %rsp,%rbp
0000000000000004 movl $0x00000041,%eax
0000000000000009 popq %rbp
000000000000000a ret
000000000000000b nopl 0x00(%rax,%rax)
_main:
0000000000000010 pushq %rbp
0000000000000011 movq %rsp,%rbp
0000000000000014 subq $0x10,%rsp
0000000000000018 movl $0x00000000,0xfc(%rbp)
000000000000001f callq 0x00000024
0000000000000024 addq $0x10,%rsp
0000000000000028 popq %rbp
0000000000000029 ret
$ llvm-rtdyld t.o -debug-only=dyld ; echo $?
Function sym: '_bar' @ 0
Function sym: '_main' @ 16
Extracting function: _bar from [0, 15]
allocated to 0x100153000
Extracting function: _main from [16, 41]
allocated to 0x100154000
Relocation at '_main' + 16 from '_bar(Word1: 0x2d000000)
Resolving relocation at '_main' + 16 (0x100154010) from '_bar (0x100153000)(pcrel, type: 2, Size: 4).
loaded '_main' at: 0x100154000
65
$
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@129388 91177308-0d34-0410-b5e6-96231b3b80d8
2011-04-12 21:20:41 +00:00
|
|
|
// Default the assigned address for this symbol to wherever this
|
|
|
|
// allocated it.
|
|
|
|
SymbolTable[Name] = Mem;
|
2011-05-13 20:12:14 +00:00
|
|
|
DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n");
|
2011-04-06 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
MCJIT lazy relocation resolution and symbol address re-assignment.
Add handling for tracking the relocations on symbols and resolving them.
Keep track of the relocations even after they are resolved so that if
the RuntimeDyld client moves the object, it can update the address and any
relocations to that object will be updated.
For our trival object file load/run test harness (llvm-rtdyld), this enables
relocations between functions located in the same object module. It should
be trivially extendable to load multiple objects with mutual references.
As a simple example, the following now works (running on x86_64 Darwin 10.6):
$ cat t.c
int bar() {
return 65;
}
int main() {
return bar();
}
$ clang t.c -fno-asynchronous-unwind-tables -o t.o -c
$ otool -vt t.o
t.o:
(__TEXT,__text) section
_bar:
0000000000000000 pushq %rbp
0000000000000001 movq %rsp,%rbp
0000000000000004 movl $0x00000041,%eax
0000000000000009 popq %rbp
000000000000000a ret
000000000000000b nopl 0x00(%rax,%rax)
_main:
0000000000000010 pushq %rbp
0000000000000011 movq %rsp,%rbp
0000000000000014 subq $0x10,%rsp
0000000000000018 movl $0x00000000,0xfc(%rbp)
000000000000001f callq 0x00000024
0000000000000024 addq $0x10,%rsp
0000000000000028 popq %rbp
0000000000000029 ret
$ llvm-rtdyld t.o -debug-only=dyld ; echo $?
Function sym: '_bar' @ 0
Function sym: '_main' @ 16
Extracting function: _bar from [0, 15]
allocated to 0x100153000
Extracting function: _main from [16, 41]
allocated to 0x100154000
Relocation at '_main' + 16 from '_bar(Word1: 0x2d000000)
Resolving relocation at '_main' + 16 (0x100154010) from '_bar (0x100153000)(pcrel, type: 2, Size: 4).
loaded '_main' at: 0x100154000
65
$
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@129388 91177308-0d34-0410-b5e6-96231b3b80d8
2011-04-12 21:20:41 +00:00
|
|
|
// Resolve the relocations for all symbols we currently know about.
|
|
|
|
void RuntimeDyldImpl::resolveRelocations() {
|
|
|
|
// Just iterate over the symbols in our symbol table and assign their
|
|
|
|
// addresses.
|
|
|
|
StringMap<uint8_t*>::iterator i = SymbolTable.begin();
|
|
|
|
StringMap<uint8_t*>::iterator e = SymbolTable.end();
|
|
|
|
for (;i != e; ++i)
|
|
|
|
reassignSymbolAddress(i->getKey(), i->getValue());
|
|
|
|
}
|
|
|
|
|
2011-03-21 22:15:52 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RuntimeDyld class implementation
|
2011-07-13 07:57:58 +00:00
|
|
|
RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
|
|
|
|
Dyld = 0;
|
|
|
|
MM = mm;
|
2011-03-21 22:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeDyld::~RuntimeDyld() {
|
|
|
|
delete Dyld;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
|
2011-07-13 07:57:58 +00:00
|
|
|
if (!Dyld) {
|
|
|
|
if (RuntimeDyldMachO::isKnownFormat(InputBuffer))
|
|
|
|
Dyld = new RuntimeDyldMachO(MM);
|
|
|
|
else
|
|
|
|
report_fatal_error("Unknown object format!");
|
|
|
|
} else {
|
|
|
|
if(!Dyld->isCompatibleFormat(InputBuffer))
|
|
|
|
report_fatal_error("Incompatible object format!");
|
|
|
|
}
|
|
|
|
|
2011-03-21 22:15:52 +00:00
|
|
|
return Dyld->loadObject(InputBuffer);
|
|
|
|
}
|
|
|
|
|
2011-04-08 17:31:24 +00:00
|
|
|
void *RuntimeDyld::getSymbolAddress(StringRef Name) {
|
2011-03-21 22:15:52 +00:00
|
|
|
return Dyld->getSymbolAddress(Name);
|
|
|
|
}
|
|
|
|
|
MCJIT lazy relocation resolution and symbol address re-assignment.
Add handling for tracking the relocations on symbols and resolving them.
Keep track of the relocations even after they are resolved so that if
the RuntimeDyld client moves the object, it can update the address and any
relocations to that object will be updated.
For our trival object file load/run test harness (llvm-rtdyld), this enables
relocations between functions located in the same object module. It should
be trivially extendable to load multiple objects with mutual references.
As a simple example, the following now works (running on x86_64 Darwin 10.6):
$ cat t.c
int bar() {
return 65;
}
int main() {
return bar();
}
$ clang t.c -fno-asynchronous-unwind-tables -o t.o -c
$ otool -vt t.o
t.o:
(__TEXT,__text) section
_bar:
0000000000000000 pushq %rbp
0000000000000001 movq %rsp,%rbp
0000000000000004 movl $0x00000041,%eax
0000000000000009 popq %rbp
000000000000000a ret
000000000000000b nopl 0x00(%rax,%rax)
_main:
0000000000000010 pushq %rbp
0000000000000011 movq %rsp,%rbp
0000000000000014 subq $0x10,%rsp
0000000000000018 movl $0x00000000,0xfc(%rbp)
000000000000001f callq 0x00000024
0000000000000024 addq $0x10,%rsp
0000000000000028 popq %rbp
0000000000000029 ret
$ llvm-rtdyld t.o -debug-only=dyld ; echo $?
Function sym: '_bar' @ 0
Function sym: '_main' @ 16
Extracting function: _bar from [0, 15]
allocated to 0x100153000
Extracting function: _main from [16, 41]
allocated to 0x100154000
Relocation at '_main' + 16 from '_bar(Word1: 0x2d000000)
Resolving relocation at '_main' + 16 (0x100154010) from '_bar (0x100153000)(pcrel, type: 2, Size: 4).
loaded '_main' at: 0x100154000
65
$
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@129388 91177308-0d34-0410-b5e6-96231b3b80d8
2011-04-12 21:20:41 +00:00
|
|
|
void RuntimeDyld::resolveRelocations() {
|
|
|
|
Dyld->resolveRelocations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
|
|
|
|
Dyld->reassignSymbolAddress(Name, Addr);
|
|
|
|
}
|
|
|
|
|
2011-03-22 18:22:27 +00:00
|
|
|
StringRef RuntimeDyld::getErrorString() {
|
2011-03-22 18:19:42 +00:00
|
|
|
return Dyld->getErrorString();
|
|
|
|
}
|
|
|
|
|
2011-03-21 22:15:52 +00:00
|
|
|
} // end namespace llvm
|