2012-01-16 23:50: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"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
2013-11-19 00:57:56 +00:00
|
|
|
#include "JITRegistrar.h"
|
2012-10-02 21:18:39 +00:00
|
|
|
#include "ObjectImageCommon.h"
|
2012-01-22 07:05:02 +00:00
|
|
|
#include "RuntimeDyldELF.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "RuntimeDyldImpl.h"
|
2012-01-22 07:05:02 +00:00
|
|
|
#include "RuntimeDyldMachO.h"
|
2013-06-11 18:01:14 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-10-29 10:47:04 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2013-10-21 17:42:06 +00:00
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2013-05-30 03:05:14 +00:00
|
|
|
#include "llvm/Object/ELF.h"
|
2012-01-22 07:05:02 +00:00
|
|
|
|
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.
|
2011-07-13 07:57:58 +00:00
|
|
|
RuntimeDyldImpl::~RuntimeDyldImpl() {}
|
2011-04-05 23:54:31 +00:00
|
|
|
|
2013-11-19 00:57:56 +00:00
|
|
|
// Pin the JITRegistrar's and ObjectImage*'s vtables to this file.
|
|
|
|
void JITRegistrar::anchor() {}
|
|
|
|
void ObjectImage::anchor() {}
|
|
|
|
void ObjectImageCommon::anchor() {}
|
|
|
|
|
2011-03-21 22:15:52 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2013-10-11 21:25:48 +00:00
|
|
|
void RuntimeDyldImpl::registerEHFrames() {
|
2013-05-05 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 00:14:21 +00:00
|
|
|
void RuntimeDyldImpl::deregisterEHFrames() {
|
|
|
|
}
|
|
|
|
|
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() {
|
2013-10-21 17:42:06 +00:00
|
|
|
MutexGuard locked(lock);
|
|
|
|
|
2012-04-12 20:13:57 +00:00
|
|
|
// First, resolve relocations associated with external symbols.
|
2012-04-30 12:15:58 +00:00
|
|
|
resolveExternalSymbols();
|
2012-03-30 16:45:19 +00:00
|
|
|
|
2012-01-16 22:26:39 +00:00
|
|
|
// Just iterate over the sections we have and resolve all the relocations
|
|
|
|
// in them. Gross overkill, but it gets the job done.
|
|
|
|
for (int i = 0, e = Sections.size(); i != e; ++i) {
|
2013-08-19 19:38:06 +00:00
|
|
|
// The Section here (Sections[i]) refers to the section in which the
|
|
|
|
// symbol for the relocation is located. The SectionID in the relocation
|
|
|
|
// entry provides the section to which the relocation will be applied.
|
2012-11-05 20:57:16 +00:00
|
|
|
uint64_t Addr = Sections[i].LoadAddress;
|
|
|
|
DEBUG(dbgs() << "Resolving relocations Section #" << i
|
|
|
|
<< "\t" << format("%p", (uint8_t *)Addr)
|
|
|
|
<< "\n");
|
|
|
|
resolveRelocationList(Relocations[i], Addr);
|
2013-10-01 01:47:35 +00:00
|
|
|
Relocations.erase(i);
|
2012-01-16 22:26:39 +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
|
|
|
}
|
|
|
|
|
2012-09-13 21:50:06 +00:00
|
|
|
void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
|
2012-01-16 23:50:55 +00:00
|
|
|
uint64_t TargetAddress) {
|
2013-10-21 17:42:06 +00:00
|
|
|
MutexGuard locked(lock);
|
2012-03-30 16:45:19 +00:00
|
|
|
for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
|
|
|
|
if (Sections[i].Address == LocalAddress) {
|
|
|
|
reassignSectionAddress(i, TargetAddress);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("Attempting to remap address of unknown section!");
|
|
|
|
}
|
|
|
|
|
2012-04-29 12:40:47 +00:00
|
|
|
// Subclasses can implement this method to create specialized image instances.
|
2012-07-23 08:51:15 +00:00
|
|
|
// The caller owns the pointer that is returned.
|
2012-10-02 21:18:39 +00:00
|
|
|
ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
|
|
|
|
return new ObjectImageCommon(InputBuffer);
|
2012-04-16 22:12:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 21:18:39 +00:00
|
|
|
ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
|
2013-10-21 17:42:06 +00:00
|
|
|
MutexGuard locked(lock);
|
|
|
|
|
2013-12-07 04:25:19 +00:00
|
|
|
OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
|
2012-04-12 20:13:57 +00:00
|
|
|
if (!obj)
|
2013-12-07 04:25:19 +00:00
|
|
|
report_fatal_error("Unable to create object image from memory buffer!");
|
2012-03-30 16:45:19 +00:00
|
|
|
|
2013-10-15 20:44:55 +00:00
|
|
|
// Save information about our target
|
2012-03-30 16:45:19 +00:00
|
|
|
Arch = (Triple::ArchType)obj->getArch();
|
2013-10-15 20:44:55 +00:00
|
|
|
IsTargetLittleEndian = obj->getObjectFile()->isLittleEndian();
|
2012-03-30 16:45:19 +00:00
|
|
|
|
2012-05-01 06:58:59 +00:00
|
|
|
// Symbols found in this object
|
|
|
|
StringMap<SymbolLoc> LocalSymbols;
|
|
|
|
// Used sections from the object file
|
|
|
|
ObjSectionToIDMap LocalSections;
|
|
|
|
|
2012-10-29 10:47:04 +00:00
|
|
|
// Common symbols requiring allocation, with their sizes and alignments
|
2012-05-01 06:58:59 +00:00
|
|
|
CommonSymbolMap CommonSymbols;
|
2012-10-29 10:47:04 +00:00
|
|
|
// Maximum required total memory to allocate all common symbols
|
2012-05-01 06:58:59 +00:00
|
|
|
uint64_t CommonSize = 0;
|
2012-03-30 16:45:19 +00:00
|
|
|
|
|
|
|
error_code err;
|
|
|
|
// Parse symbols
|
|
|
|
DEBUG(dbgs() << "Parse symbols:\n");
|
|
|
|
for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
|
|
|
|
i != e; i.increment(err)) {
|
|
|
|
Check(err);
|
|
|
|
object::SymbolRef::Type SymType;
|
|
|
|
StringRef Name;
|
|
|
|
Check(i->getType(SymType));
|
|
|
|
Check(i->getName(Name));
|
|
|
|
|
2012-04-12 20:13:57 +00:00
|
|
|
uint32_t flags;
|
|
|
|
Check(i->getFlags(flags));
|
|
|
|
|
|
|
|
bool isCommon = flags & SymbolRef::SF_Common;
|
|
|
|
if (isCommon) {
|
|
|
|
// Add the common symbols to a list. We'll allocate them all below.
|
2013-04-29 22:24:22 +00:00
|
|
|
uint32_t Align;
|
|
|
|
Check(i->getAlignment(Align));
|
2012-04-12 20:13:57 +00:00
|
|
|
uint64_t Size = 0;
|
|
|
|
Check(i->getSize(Size));
|
2012-10-29 10:47:04 +00:00
|
|
|
CommonSize += Size + Align;
|
|
|
|
CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
|
2012-04-12 20:13:57 +00:00
|
|
|
} else {
|
|
|
|
if (SymType == object::SymbolRef::ST_Function ||
|
2012-08-17 21:28:04 +00:00
|
|
|
SymType == object::SymbolRef::ST_Data ||
|
|
|
|
SymType == object::SymbolRef::ST_Unknown) {
|
2012-04-12 20:13:57 +00:00
|
|
|
uint64_t FileOffset;
|
2012-04-29 12:40:47 +00:00
|
|
|
StringRef SectionData;
|
2012-12-17 17:59:35 +00:00
|
|
|
bool IsCode;
|
2012-04-12 20:13:57 +00:00
|
|
|
section_iterator si = obj->end_sections();
|
|
|
|
Check(i->getFileOffset(FileOffset));
|
|
|
|
Check(i->getSection(si));
|
|
|
|
if (si == obj->end_sections()) continue;
|
2012-04-29 12:40:47 +00:00
|
|
|
Check(si->getContents(SectionData));
|
2012-12-17 17:59:35 +00:00
|
|
|
Check(si->isText(IsCode));
|
2013-12-07 04:25:19 +00:00
|
|
|
const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
|
2012-04-12 20:13:57 +00:00
|
|
|
(uintptr_t)FileOffset;
|
2012-04-29 12:40:47 +00:00
|
|
|
uintptr_t SectOffset = (uintptr_t)(SymPtr -
|
|
|
|
(const uint8_t*)SectionData.begin());
|
2012-12-17 17:59:35 +00:00
|
|
|
unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
|
2012-04-12 20:13:57 +00:00
|
|
|
LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
|
|
|
|
DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
|
|
|
|
<< " flags: " << flags
|
|
|
|
<< " SID: " << SectionID
|
|
|
|
<< " Offset: " << format("%p", SectOffset));
|
2012-11-16 11:11:59 +00:00
|
|
|
GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
|
2012-04-12 20:13:57 +00:00
|
|
|
}
|
2012-03-30 16:45:19 +00:00
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
|
|
|
|
}
|
|
|
|
|
2012-04-12 20:13:57 +00:00
|
|
|
// Allocate common symbols
|
|
|
|
if (CommonSize != 0)
|
2012-04-16 22:12:58 +00:00
|
|
|
emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
|
2012-04-12 20:13:57 +00:00
|
|
|
|
2012-04-29 12:40:47 +00:00
|
|
|
// Parse and process relocations
|
2012-03-30 16:45:19 +00:00
|
|
|
DEBUG(dbgs() << "Parse relocations:\n");
|
|
|
|
for (section_iterator si = obj->begin_sections(),
|
|
|
|
se = obj->end_sections(); si != se; si.increment(err)) {
|
|
|
|
Check(err);
|
|
|
|
bool isFirstRelocation = true;
|
|
|
|
unsigned SectionID = 0;
|
|
|
|
StubMap Stubs;
|
2013-05-30 03:05:14 +00:00
|
|
|
section_iterator RelocatedSection = si->getRelocatedSection();
|
2012-03-30 16:45:19 +00:00
|
|
|
|
|
|
|
for (relocation_iterator i = si->begin_relocations(),
|
|
|
|
e = si->end_relocations(); i != e; i.increment(err)) {
|
|
|
|
Check(err);
|
|
|
|
|
2012-04-29 12:40:47 +00:00
|
|
|
// If it's the first relocation in this section, find its SectionID
|
2012-03-30 16:45:19 +00:00
|
|
|
if (isFirstRelocation) {
|
2013-05-30 03:05:14 +00:00
|
|
|
SectionID =
|
|
|
|
findOrEmitSection(*obj, *RelocatedSection, true, LocalSections);
|
2012-03-30 16:45:19 +00:00
|
|
|
DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
|
|
|
|
isFirstRelocation = false;
|
|
|
|
}
|
|
|
|
|
2013-04-29 19:03:21 +00:00
|
|
|
processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
|
2013-04-29 14:44:23 +00:00
|
|
|
Stubs);
|
2012-03-30 16:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-16 22:12:58 +00:00
|
|
|
|
2013-08-19 23:27:43 +00:00
|
|
|
// Give the subclasses a chance to tie-up any loose ends.
|
2013-10-11 21:25:48 +00:00
|
|
|
finalizeLoad(LocalSections);
|
2013-08-19 23:27:43 +00:00
|
|
|
|
2012-10-02 21:18:39 +00:00
|
|
|
return obj.take();
|
2012-01-16 23:50:55 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 10:41:12 +00:00
|
|
|
void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
|
|
|
|
const CommonSymbolMap &CommonSymbols,
|
|
|
|
uint64_t TotalSize,
|
|
|
|
SymbolTableMap &SymbolTable) {
|
2012-04-12 20:13:57 +00:00
|
|
|
// Allocate memory for the section
|
|
|
|
unsigned SectionID = Sections.size();
|
2013-10-02 00:59:25 +00:00
|
|
|
uint8_t *Addr = MemMgr->allocateDataSection(
|
|
|
|
TotalSize, sizeof(void*), SectionID, StringRef(), false);
|
2012-04-12 20:13:57 +00:00
|
|
|
if (!Addr)
|
|
|
|
report_fatal_error("Unable to allocate memory for common symbols!");
|
|
|
|
uint64_t Offset = 0;
|
2013-05-05 20:43:10 +00:00
|
|
|
Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
|
2012-04-12 20:13:57 +00:00
|
|
|
memset(Addr, 0, TotalSize);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
|
|
|
|
<< " new addr: " << format("%p", Addr)
|
|
|
|
<< " DataSize: " << TotalSize
|
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
// Assign the address of each symbol
|
2012-05-01 10:41:12 +00:00
|
|
|
for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
|
|
|
|
itEnd = CommonSymbols.end(); it != itEnd; it++) {
|
2012-10-29 10:47:04 +00:00
|
|
|
uint64_t Size = it->second.first;
|
|
|
|
uint64_t Align = it->second.second;
|
2012-04-12 20:13:57 +00:00
|
|
|
StringRef Name;
|
|
|
|
it->first.getName(Name);
|
2012-10-29 10:47:04 +00:00
|
|
|
if (Align) {
|
|
|
|
// This symbol has an alignment requirement.
|
|
|
|
uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
|
|
|
|
Addr += AlignOffset;
|
|
|
|
Offset += AlignOffset;
|
|
|
|
DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
|
2012-11-01 19:49:21 +00:00
|
|
|
format("%p\n", Addr));
|
2012-10-29 10:47:04 +00:00
|
|
|
}
|
2012-04-16 22:12:58 +00:00
|
|
|
Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
|
2012-05-01 10:41:12 +00:00
|
|
|
SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
|
2012-04-12 20:13:57 +00:00
|
|
|
Offset += Size;
|
|
|
|
Addr += Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-16 22:12:58 +00:00
|
|
|
unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
|
|
|
|
const SectionRef &Section,
|
2012-03-30 16:45:19 +00:00
|
|
|
bool IsCode) {
|
|
|
|
|
|
|
|
unsigned StubBufSize = 0,
|
|
|
|
StubSize = getMaxStubSize();
|
|
|
|
error_code err;
|
2013-05-30 03:05:14 +00:00
|
|
|
const ObjectFile *ObjFile = Obj.getObjectFile();
|
|
|
|
// FIXME: this is an inefficient way to handle this. We should computed the
|
|
|
|
// necessary section allocation size in loadObject by walking all the sections
|
|
|
|
// once.
|
2012-03-30 16:45:19 +00:00
|
|
|
if (StubSize > 0) {
|
2013-05-30 03:05:14 +00:00
|
|
|
for (section_iterator SI = ObjFile->begin_sections(),
|
|
|
|
SE = ObjFile->end_sections();
|
|
|
|
SI != SE; SI.increment(err), Check(err)) {
|
|
|
|
section_iterator RelSecI = SI->getRelocatedSection();
|
|
|
|
if (!(RelSecI == Section))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (relocation_iterator I = SI->begin_relocations(),
|
|
|
|
E = SI->end_relocations(); I != E; I.increment(err), Check(err)) {
|
|
|
|
StubBufSize += StubSize;
|
|
|
|
}
|
|
|
|
}
|
2012-03-30 16:45:19 +00:00
|
|
|
}
|
2013-05-30 03:05:14 +00:00
|
|
|
|
2012-03-30 16:45:19 +00:00
|
|
|
StringRef data;
|
|
|
|
uint64_t Alignment64;
|
|
|
|
Check(Section.getContents(data));
|
|
|
|
Check(Section.getAlignment(Alignment64));
|
|
|
|
|
|
|
|
unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
|
2012-04-12 20:13:57 +00:00
|
|
|
bool IsRequired;
|
|
|
|
bool IsVirtual;
|
|
|
|
bool IsZeroInit;
|
2012-11-15 23:50:01 +00:00
|
|
|
bool IsReadOnly;
|
2012-04-12 20:13:57 +00:00
|
|
|
uint64_t DataSize;
|
2013-10-16 00:32:24 +00:00
|
|
|
unsigned PaddingSize = 0;
|
2012-10-25 13:13:48 +00:00
|
|
|
StringRef Name;
|
2012-04-12 20:13:57 +00:00
|
|
|
Check(Section.isRequiredForExecution(IsRequired));
|
|
|
|
Check(Section.isVirtual(IsVirtual));
|
|
|
|
Check(Section.isZeroInit(IsZeroInit));
|
2012-11-15 23:50:01 +00:00
|
|
|
Check(Section.isReadOnlyData(IsReadOnly));
|
2012-04-12 20:13:57 +00:00
|
|
|
Check(Section.getSize(DataSize));
|
2012-10-25 13:13:48 +00:00
|
|
|
Check(Section.getName(Name));
|
2013-05-03 14:15:35 +00:00
|
|
|
if (StubSize > 0) {
|
|
|
|
unsigned StubAlignment = getStubAlignment();
|
|
|
|
unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
|
|
|
|
if (StubAlignment > EndAlignment)
|
|
|
|
StubBufSize += StubAlignment - EndAlignment;
|
|
|
|
}
|
2012-04-12 20:13:57 +00:00
|
|
|
|
2013-10-16 00:32:24 +00:00
|
|
|
// The .eh_frame section (at least on Linux) needs an extra four bytes padded
|
|
|
|
// with zeroes added at the end. For MachO objects, this section has a
|
|
|
|
// slightly different name, so this won't have any effect for MachO objects.
|
|
|
|
if (Name == ".eh_frame")
|
|
|
|
PaddingSize = 4;
|
|
|
|
|
2012-04-12 20:13:57 +00:00
|
|
|
unsigned Allocate;
|
2012-03-30 16:45:19 +00:00
|
|
|
unsigned SectionID = Sections.size();
|
2012-04-12 20:13:57 +00:00
|
|
|
uint8_t *Addr;
|
|
|
|
const char *pData = 0;
|
|
|
|
|
|
|
|
// Some sections, such as debug info, don't need to be loaded for execution.
|
|
|
|
// Leave those where they are.
|
|
|
|
if (IsRequired) {
|
2013-10-16 00:32:24 +00:00
|
|
|
Allocate = DataSize + PaddingSize + StubBufSize;
|
2012-04-12 20:13:57 +00:00
|
|
|
Addr = IsCode
|
2013-10-02 00:59:25 +00:00
|
|
|
? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID, Name)
|
|
|
|
: MemMgr->allocateDataSection(Allocate, Alignment, SectionID, Name,
|
|
|
|
IsReadOnly);
|
2012-04-12 20:13:57 +00:00
|
|
|
if (!Addr)
|
|
|
|
report_fatal_error("Unable to allocate section memory!");
|
|
|
|
|
|
|
|
// Virtual sections have no data in the object image, so leave pData = 0
|
|
|
|
if (!IsVirtual)
|
|
|
|
pData = data.data();
|
|
|
|
|
|
|
|
// Zero-initialize or copy the data from the image
|
|
|
|
if (IsZeroInit || IsVirtual)
|
|
|
|
memset(Addr, 0, DataSize);
|
|
|
|
else
|
|
|
|
memcpy(Addr, pData, DataSize);
|
|
|
|
|
2013-10-16 00:32:24 +00:00
|
|
|
// Fill in any extra bytes we allocated for padding
|
|
|
|
if (PaddingSize != 0) {
|
|
|
|
memset(Addr + DataSize, 0, PaddingSize);
|
|
|
|
// Update the DataSize variable so that the stub offset is set correctly.
|
|
|
|
DataSize += PaddingSize;
|
|
|
|
}
|
|
|
|
|
2012-04-12 20:13:57 +00:00
|
|
|
DEBUG(dbgs() << "emitSection SectionID: " << SectionID
|
2012-10-25 13:13:48 +00:00
|
|
|
<< " Name: " << Name
|
2012-04-12 20:13:57 +00:00
|
|
|
<< " obj addr: " << format("%p", pData)
|
|
|
|
<< " new addr: " << format("%p", Addr)
|
|
|
|
<< " DataSize: " << DataSize
|
|
|
|
<< " StubBufSize: " << StubBufSize
|
|
|
|
<< " Allocate: " << Allocate
|
|
|
|
<< "\n");
|
2012-04-16 22:12:58 +00:00
|
|
|
Obj.updateSectionAddress(Section, (uint64_t)Addr);
|
2012-04-12 20:13:57 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Even if we didn't load the section, we need to record an entry for it
|
2012-04-29 12:40:47 +00:00
|
|
|
// to handle later processing (and by 'handle' I mean don't do anything
|
|
|
|
// with these sections).
|
2012-04-12 20:13:57 +00:00
|
|
|
Allocate = 0;
|
|
|
|
Addr = 0;
|
|
|
|
DEBUG(dbgs() << "emitSection SectionID: " << SectionID
|
2012-10-25 13:13:48 +00:00
|
|
|
<< " Name: " << Name
|
2012-04-12 20:13:57 +00:00
|
|
|
<< " obj addr: " << format("%p", data.data())
|
|
|
|
<< " new addr: 0"
|
|
|
|
<< " DataSize: " << DataSize
|
|
|
|
<< " StubBufSize: " << StubBufSize
|
|
|
|
<< " Allocate: " << Allocate
|
|
|
|
<< "\n");
|
|
|
|
}
|
|
|
|
|
2013-05-05 20:43:10 +00:00
|
|
|
Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
|
2012-03-30 16:45:19 +00:00
|
|
|
return SectionID;
|
|
|
|
}
|
|
|
|
|
2012-04-16 22:12:58 +00:00
|
|
|
unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
|
|
|
|
const SectionRef &Section,
|
2012-03-30 16:45:19 +00:00
|
|
|
bool IsCode,
|
|
|
|
ObjSectionToIDMap &LocalSections) {
|
|
|
|
|
|
|
|
unsigned SectionID = 0;
|
|
|
|
ObjSectionToIDMap::iterator i = LocalSections.find(Section);
|
|
|
|
if (i != LocalSections.end())
|
|
|
|
SectionID = i->second;
|
|
|
|
else {
|
2012-04-16 22:12:58 +00:00
|
|
|
SectionID = emitSection(Obj, Section, IsCode);
|
2012-03-30 16:45:19 +00:00
|
|
|
LocalSections[Section] = SectionID;
|
|
|
|
}
|
|
|
|
return SectionID;
|
|
|
|
}
|
|
|
|
|
2012-05-01 10:41:12 +00:00
|
|
|
void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
|
|
|
|
unsigned SectionID) {
|
|
|
|
Relocations[SectionID].push_back(RE);
|
|
|
|
}
|
2012-03-30 16:45:19 +00:00
|
|
|
|
2012-05-01 10:41:12 +00:00
|
|
|
void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
|
|
|
|
StringRef SymbolName) {
|
|
|
|
// Relocation by symbol. If the symbol is found in the global symbol table,
|
|
|
|
// create an appropriate section relocation. Otherwise, add it to
|
|
|
|
// ExternalSymbolRelocations.
|
|
|
|
SymbolTableMap::const_iterator Loc =
|
|
|
|
GlobalSymbolTable.find(SymbolName);
|
|
|
|
if (Loc == GlobalSymbolTable.end()) {
|
|
|
|
ExternalSymbolRelocations[SymbolName].push_back(RE);
|
2012-04-30 12:15:58 +00:00
|
|
|
} else {
|
2012-05-01 10:41:12 +00:00
|
|
|
// Copy the RE since we want to modify its addend.
|
|
|
|
RelocationEntry RECopy = RE;
|
|
|
|
RECopy.Addend += Loc->second.second;
|
|
|
|
Relocations[Loc->second.first].push_back(RECopy);
|
2012-04-30 12:15:58 +00:00
|
|
|
}
|
2012-03-30 16:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
|
2013-05-04 20:14:09 +00:00
|
|
|
if (Arch == Triple::aarch64) {
|
|
|
|
// This stub has to be able to access the full address space,
|
|
|
|
// since symbol lookup won't necessarily find a handy, in-range,
|
|
|
|
// PLT stub for functions which could be anywhere.
|
|
|
|
uint32_t *StubAddr = (uint32_t*)Addr;
|
|
|
|
|
|
|
|
// Stub can use ip0 (== x16) to calculate address
|
|
|
|
*StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
|
|
|
|
StubAddr++;
|
|
|
|
*StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
|
|
|
|
StubAddr++;
|
|
|
|
*StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
|
|
|
|
StubAddr++;
|
|
|
|
*StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
|
|
|
|
StubAddr++;
|
|
|
|
*StubAddr = 0xd61f0200; // br ip0
|
|
|
|
|
|
|
|
return Addr;
|
|
|
|
} else if (Arch == Triple::arm) {
|
2012-08-17 21:28:04 +00:00
|
|
|
// TODO: There is only ARM far stub now. We should add the Thumb stub,
|
|
|
|
// and stubs for branches Thumb - ARM and ARM - Thumb.
|
2012-03-30 16:45:19 +00:00
|
|
|
uint32_t *StubAddr = (uint32_t*)Addr;
|
|
|
|
*StubAddr = 0xe51ff004; // ldr pc,<label>
|
|
|
|
return (uint8_t*)++StubAddr;
|
2012-12-04 00:08:14 +00:00
|
|
|
} else if (Arch == Triple::mipsel || Arch == Triple::mips) {
|
2012-08-17 21:28:04 +00:00
|
|
|
uint32_t *StubAddr = (uint32_t*)Addr;
|
|
|
|
// 0: 3c190000 lui t9,%hi(addr).
|
|
|
|
// 4: 27390000 addiu t9,t9,%lo(addr).
|
|
|
|
// 8: 03200008 jr t9.
|
|
|
|
// c: 00000000 nop.
|
|
|
|
const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
|
|
|
|
const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
|
|
|
|
|
|
|
|
*StubAddr = LuiT9Instr;
|
|
|
|
StubAddr++;
|
|
|
|
*StubAddr = AdduiT9Instr;
|
|
|
|
StubAddr++;
|
|
|
|
*StubAddr = JrT9Instr;
|
|
|
|
StubAddr++;
|
|
|
|
*StubAddr = NopInstr;
|
2012-10-25 13:13:48 +00:00
|
|
|
return Addr;
|
2013-07-26 01:35:43 +00:00
|
|
|
} else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
|
2012-10-25 13:13:48 +00:00
|
|
|
// PowerPC64 stub: the address points to a function descriptor
|
|
|
|
// instead of the function itself. Load the function address
|
|
|
|
// on r11 and sets it to control register. Also loads the function
|
|
|
|
// TOC in r2 and environment pointer to r11.
|
|
|
|
writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
|
|
|
|
writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
|
|
|
|
writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
|
|
|
|
writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
|
|
|
|
writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
|
|
|
|
writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
|
|
|
|
writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
|
|
|
|
writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
|
|
|
|
writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
|
|
|
|
writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
|
|
|
|
writeInt32BE(Addr+40, 0x4E800420); // bctr
|
2012-11-01 19:49:21 +00:00
|
|
|
|
2013-05-03 14:15:35 +00:00
|
|
|
return Addr;
|
|
|
|
} else if (Arch == Triple::systemz) {
|
|
|
|
writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
|
|
|
|
writeInt16BE(Addr+2, 0x0000);
|
|
|
|
writeInt16BE(Addr+4, 0x0004);
|
|
|
|
writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
|
|
|
|
// 8-byte address stored at Addr + 8
|
2012-03-30 16:45:19 +00:00
|
|
|
return Addr;
|
2013-08-19 23:27:43 +00:00
|
|
|
} else if (Arch == Triple::x86_64) {
|
|
|
|
*Addr = 0xFF; // jmp
|
|
|
|
*(Addr+1) = 0x25; // rip
|
|
|
|
// 32-bit PC-relative address of the GOT entry will be stored at Addr+2
|
2012-08-17 21:28:04 +00:00
|
|
|
}
|
|
|
|
return Addr;
|
2012-03-30 16:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign an address to a symbol name and resolve all the relocations
|
|
|
|
// associated with it.
|
|
|
|
void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
|
|
|
|
uint64_t Addr) {
|
|
|
|
// The address to use for relocation resolution is not
|
|
|
|
// the address of the local section buffer. We must be doing
|
2012-11-05 20:57:16 +00:00
|
|
|
// a remote execution environment of some sort. Relocations can't
|
|
|
|
// be applied until all the sections have been moved. The client must
|
|
|
|
// trigger this with a call to MCJIT::finalize() or
|
|
|
|
// RuntimeDyld::resolveRelocations().
|
2012-03-30 16:45:19 +00:00
|
|
|
//
|
|
|
|
// Addr is a uint64_t because we can't assume the pointer width
|
|
|
|
// of the target is the same as that of the host. Just use a generic
|
|
|
|
// "big enough" type.
|
|
|
|
Sections[SectionID].LoadAddress = Addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
|
|
|
|
uint64_t Value) {
|
|
|
|
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
|
2013-04-29 17:24:34 +00:00
|
|
|
const RelocationEntry &RE = Relocs[i];
|
|
|
|
// Ignore relocations for sections that were not loaded
|
|
|
|
if (Sections[RE.SectionID].Address == 0)
|
|
|
|
continue;
|
|
|
|
resolveRelocation(RE, Value);
|
2012-03-30 16:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:15:58 +00:00
|
|
|
void RuntimeDyldImpl::resolveExternalSymbols() {
|
2013-10-01 01:47:35 +00:00
|
|
|
while(!ExternalSymbolRelocations.empty()) {
|
|
|
|
StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
|
|
|
|
|
2012-03-30 16:45:19 +00:00
|
|
|
StringRef Name = i->first();
|
2013-10-01 01:47:35 +00:00
|
|
|
if (Name.size() == 0) {
|
|
|
|
// This is an absolute symbol, use an address of zero.
|
|
|
|
DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
|
2013-11-11 19:55:10 +00:00
|
|
|
RelocationList &Relocs = i->second;
|
2013-10-01 01:47:35 +00:00
|
|
|
resolveRelocationList(Relocs, 0);
|
|
|
|
} else {
|
|
|
|
uint64_t Addr = 0;
|
|
|
|
SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
|
|
|
|
if (Loc == GlobalSymbolTable.end()) {
|
|
|
|
// This is an external symbol, try to get its address from
|
|
|
|
// MemoryManager.
|
|
|
|
Addr = MemMgr->getSymbolAddress(Name.data());
|
2013-11-11 19:55:10 +00:00
|
|
|
// The call to getSymbolAddress may have caused additional modules to
|
|
|
|
// be loaded, which may have added new entries to the
|
|
|
|
// ExternalSymbolRelocations map. Consquently, we need to update our
|
|
|
|
// iterator. This is also why retrieval of the relocation list
|
|
|
|
// associated with this symbol is deferred until below this point.
|
|
|
|
// New entries may have been added to the relocation list.
|
|
|
|
i = ExternalSymbolRelocations.find(Name);
|
2013-02-20 18:24:34 +00:00
|
|
|
} else {
|
2013-10-01 01:47:35 +00:00
|
|
|
// We found the symbol in our global table. It was probably in a
|
|
|
|
// Module that we loaded previously.
|
2013-10-19 09:04:26 +00:00
|
|
|
SymbolLoc SymLoc = Loc->second;
|
2013-10-01 01:47:35 +00:00
|
|
|
Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
|
2013-02-20 18:09:21 +00:00
|
|
|
}
|
2013-10-01 01:47:35 +00:00
|
|
|
|
|
|
|
// FIXME: Implement error handling that doesn't kill the host program!
|
|
|
|
if (!Addr)
|
|
|
|
report_fatal_error("Program used external function '" + Name +
|
|
|
|
"' which could not be resolved!");
|
|
|
|
|
|
|
|
updateGOTEntries(Name, Addr);
|
|
|
|
DEBUG(dbgs() << "Resolving relocations Name: " << Name
|
|
|
|
<< "\t" << format("0x%lx", Addr)
|
|
|
|
<< "\n");
|
2013-11-11 19:55:10 +00:00
|
|
|
// This list may have been updated when we called getSymbolAddress, so
|
|
|
|
// don't change this code to get the list earlier.
|
|
|
|
RelocationList &Relocs = i->second;
|
2013-10-01 01:47:35 +00:00
|
|
|
resolveRelocationList(Relocs, Addr);
|
2012-03-30 16:45:19 +00:00
|
|
|
}
|
2013-10-01 01:47:35 +00:00
|
|
|
|
2013-11-11 19:55:10 +00:00
|
|
|
ExternalSymbolRelocations.erase(i);
|
2012-03-30 16:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-21 22:15:52 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RuntimeDyld class implementation
|
2011-07-13 07:57:58 +00:00
|
|
|
RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
|
2012-11-15 23:50:01 +00:00
|
|
|
// FIXME: There's a potential issue lurking here if a single instance of
|
|
|
|
// RuntimeDyld is used to load multiple objects. The current implementation
|
|
|
|
// associates a single memory manager with a RuntimeDyld instance. Even
|
|
|
|
// though the public class spawns a new 'impl' instance for each load,
|
|
|
|
// they share a single memory manager. This can become a problem when page
|
|
|
|
// permissions are applied.
|
2011-07-13 07:57:58 +00:00
|
|
|
Dyld = 0;
|
|
|
|
MM = mm;
|
2011-03-21 22:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeDyld::~RuntimeDyld() {
|
|
|
|
delete Dyld;
|
|
|
|
}
|
|
|
|
|
2012-10-02 21:18:39 +00:00
|
|
|
ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
|
2011-07-13 07:57:58 +00:00
|
|
|
if (!Dyld) {
|
2013-06-11 18:01:14 +00:00
|
|
|
sys::fs::file_magic Type =
|
|
|
|
sys::fs::identify_magic(InputBuffer->getBuffer());
|
|
|
|
switch (Type) {
|
|
|
|
case sys::fs::file_magic::elf_relocatable:
|
|
|
|
case sys::fs::file_magic::elf_executable:
|
|
|
|
case sys::fs::file_magic::elf_shared_object:
|
|
|
|
case sys::fs::file_magic::elf_core:
|
|
|
|
Dyld = new RuntimeDyldELF(MM);
|
|
|
|
break;
|
|
|
|
case sys::fs::file_magic::macho_object:
|
|
|
|
case sys::fs::file_magic::macho_executable:
|
|
|
|
case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
|
|
|
|
case sys::fs::file_magic::macho_core:
|
|
|
|
case sys::fs::file_magic::macho_preload_executable:
|
|
|
|
case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
|
|
|
|
case sys::fs::file_magic::macho_dynamic_linker:
|
|
|
|
case sys::fs::file_magic::macho_bundle:
|
|
|
|
case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
|
|
|
|
case sys::fs::file_magic::macho_dsym_companion:
|
|
|
|
Dyld = new RuntimeDyldMachO(MM);
|
|
|
|
break;
|
|
|
|
case sys::fs::file_magic::unknown:
|
|
|
|
case sys::fs::file_magic::bitcode:
|
|
|
|
case sys::fs::file_magic::archive:
|
|
|
|
case sys::fs::file_magic::coff_object:
|
2013-11-15 21:22:02 +00:00
|
|
|
case sys::fs::file_magic::coff_import_library:
|
2013-06-11 18:01:14 +00:00
|
|
|
case sys::fs::file_magic::pecoff_executable:
|
2013-06-18 15:03:28 +00:00
|
|
|
case sys::fs::file_magic::macho_universal_binary:
|
2013-10-15 22:45:38 +00:00
|
|
|
case sys::fs::file_magic::windows_resource:
|
2013-06-11 18:01:14 +00:00
|
|
|
report_fatal_error("Incompatible object format!");
|
2012-01-16 08:56:09 +00:00
|
|
|
}
|
2011-07-13 07:57:58 +00:00
|
|
|
} else {
|
2012-01-16 08:56:09 +00:00
|
|
|
if (!Dyld->isCompatibleFormat(InputBuffer))
|
2011-07-13 07:57:58 +00:00
|
|
|
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) {
|
2013-10-01 01:47:35 +00:00
|
|
|
if (!Dyld)
|
|
|
|
return NULL;
|
2011-03-21 22:15:52 +00:00
|
|
|
return Dyld->getSymbolAddress(Name);
|
|
|
|
}
|
|
|
|
|
2012-09-05 16:50:40 +00:00
|
|
|
uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
|
2013-10-01 01:47:35 +00:00
|
|
|
if (!Dyld)
|
|
|
|
return 0;
|
2012-09-05 16:50:40 +00:00
|
|
|
return Dyld->getSymbolLoadAddress(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();
|
|
|
|
}
|
|
|
|
|
2012-01-16 22:26:39 +00:00
|
|
|
void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
|
|
|
|
uint64_t Addr) {
|
|
|
|
Dyld->reassignSectionAddress(SectionID, Addr);
|
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
|
|
|
}
|
|
|
|
|
2012-09-13 21:50:06 +00:00
|
|
|
void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
|
2012-01-16 23:50:55 +00:00
|
|
|
uint64_t TargetAddress) {
|
|
|
|
Dyld->mapSectionAddress(LocalAddress, TargetAddress);
|
|
|
|
}
|
|
|
|
|
2011-03-22 18:22:27 +00:00
|
|
|
StringRef RuntimeDyld::getErrorString() {
|
2011-03-22 18:19:42 +00:00
|
|
|
return Dyld->getErrorString();
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:25:48 +00:00
|
|
|
void RuntimeDyld::registerEHFrames() {
|
2013-10-16 00:14:21 +00:00
|
|
|
if (Dyld)
|
|
|
|
Dyld->registerEHFrames();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeDyld::deregisterEHFrames() {
|
|
|
|
if (Dyld)
|
|
|
|
Dyld->deregisterEHFrames();
|
2013-05-05 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 22:15:52 +00:00
|
|
|
} // end namespace llvm
|