1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dyld"
15 #include "RuntimeDyldImpl.h"
16 #include "RuntimeDyldELF.h"
17 #include "RuntimeDyldMachO.h"
18 #include "llvm/Support/Path.h"
21 using namespace llvm::object;
23 // Empty out-of-line virtual destructor as the key function.
24 RTDyldMemoryManager::~RTDyldMemoryManager() {}
25 RuntimeDyldImpl::~RuntimeDyldImpl() {}
30 // Helper for extensive error checking in debug builds.
31 error_code Check(error_code Err) {
33 report_fatal_error(Err.message());
37 } // end anonymous namespace
40 // Resolve the relocations for all symbols we currently know about.
41 void RuntimeDyldImpl::resolveRelocations() {
42 // First, resolve relocations assotiated with external symbols.
45 // Just iterate over the sections we have and resolve all the relocations
46 // in them. Gross overkill, but it gets the job done.
47 for (int i = 0, e = Sections.size(); i != e; ++i) {
48 reassignSectionAddress(i, Sections[i].LoadAddress);
52 void RuntimeDyldImpl::mapSectionAddress(void *LocalAddress,
53 uint64_t TargetAddress) {
54 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
55 if (Sections[i].Address == LocalAddress) {
56 reassignSectionAddress(i, TargetAddress);
60 llvm_unreachable("Attempting to remap address of unknown section!");
63 bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) {
64 // FIXME: ObjectFile don't modify MemoryBuffer.
65 // It should use const MemoryBuffer as parameter.
67 = ObjectFile::createObjectFile(const_cast<MemoryBuffer*>(InputBuffer));
69 Arch = (Triple::ArchType)obj->getArch();
71 LocalSymbolMap LocalSymbols; // Functions and data symbols from the
73 ObjSectionToIDMap LocalSections; // Used sections from the object file
77 DEBUG(dbgs() << "Parse symbols:\n");
78 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
79 i != e; i.increment(err)) {
81 object::SymbolRef::Type SymType;
83 Check(i->getType(SymType));
84 Check(i->getName(Name));
86 if (SymType == object::SymbolRef::ST_Function ||
87 SymType == object::SymbolRef::ST_Data) {
91 section_iterator si = obj->end_sections();
92 Check(i->getFileOffset(FileOffset));
93 Check(i->getFlags(flags));
94 Check(i->getSection(si));
95 if (si == obj->end_sections()) continue;
96 Check(si->getContents(sData));
97 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
98 (uintptr_t)FileOffset;
99 uintptr_t SectOffset = (uintptr_t)(SymPtr - (const uint8_t*)sData.begin());
101 = findOrEmitSection(*si,
102 SymType == object::SymbolRef::ST_Function,
104 bool isGlobal = flags & SymbolRef::SF_Global;
105 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
106 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
107 << " flags: " << flags
108 << " SID: " << SectionID
109 << " Offset: " << format("%p", SectOffset));
111 SymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
113 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
116 // Parse and proccess relocations
117 DEBUG(dbgs() << "Parse relocations:\n");
118 for (section_iterator si = obj->begin_sections(),
119 se = obj->end_sections(); si != se; si.increment(err)) {
121 bool isFirstRelocation = true;
122 unsigned SectionID = 0;
125 for (relocation_iterator i = si->begin_relocations(),
126 e = si->end_relocations(); i != e; i.increment(err)) {
129 // If it's first relocation in this section, find its SectionID
130 if (isFirstRelocation) {
131 SectionID = findOrEmitSection(*si, true, LocalSections);
132 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
133 isFirstRelocation = false;
136 ObjRelocationInfo RI;
137 RI.SectionID = SectionID;
138 Check(i->getAdditionalInfo(RI.AdditionalInfo));
139 Check(i->getOffset(RI.Offset));
140 Check(i->getSymbol(RI.Symbol));
141 Check(i->getType(RI.Type));
143 DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo
144 << " Offset: " << format("%p", (uintptr_t)RI.Offset)
145 << " Type: " << (uint32_t)(RI.Type & 0xffffffffL)
147 processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs);
153 unsigned RuntimeDyldImpl::emitSection(const SectionRef &Section,
156 unsigned StubBufSize = 0,
157 StubSize = getMaxStubSize();
160 for (relocation_iterator i = Section.begin_relocations(),
161 e = Section.end_relocations(); i != e; i.increment(err))
162 StubBufSize += StubSize;
165 uint64_t Alignment64;
166 Check(Section.getContents(data));
167 Check(Section.getAlignment(Alignment64));
169 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
170 unsigned DataSize = data.size();
171 unsigned Allocate = DataSize + StubBufSize;
172 unsigned SectionID = Sections.size();
173 const char *pData = data.data();
174 uint8_t *Addr = IsCode
175 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
176 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID);
178 memcpy(Addr, pData, DataSize);
179 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
180 << " obj addr: " << format("%p", pData)
181 << " new addr: " << format("%p", Addr)
182 << " DataSize: " << DataSize
183 << " StubBufSize: " << StubBufSize
184 << " Allocate: " << Allocate
186 Sections.push_back(SectionEntry(Addr, Allocate, DataSize,(uintptr_t)pData));
190 unsigned RuntimeDyldImpl::findOrEmitSection(const SectionRef &Section,
192 ObjSectionToIDMap &LocalSections) {
194 unsigned SectionID = 0;
195 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
196 if (i != LocalSections.end())
197 SectionID = i->second;
199 SectionID = emitSection(Section, IsCode);
200 LocalSections[Section] = SectionID;
205 void RuntimeDyldImpl::AddRelocation(const RelocationValueRef &Value,
206 unsigned SectionID, uintptr_t Offset,
208 DEBUG(dbgs() << "AddRelocation SymNamePtr: " << format("%p", Value.SymbolName)
209 << " SID: " << Value.SectionID
210 << " Addend: " << format("%p", Value.Addend)
211 << " Offset: " << format("%p", Offset)
212 << " RelType: " << format("%x", RelType)
215 if (Value.SymbolName == 0) {
216 Relocations[Value.SectionID].push_back(RelocationEntry(
222 SymbolRelocations[Value.SymbolName].push_back(RelocationEntry(
229 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
230 // TODO: There is only ARM far stub now. We should add the Thumb stub,
231 // and stubs for branches Thumb - ARM and ARM - Thumb.
232 if (Arch == Triple::arm) {
233 uint32_t *StubAddr = (uint32_t*)Addr;
234 *StubAddr = 0xe51ff004; // ldr pc,<label>
235 return (uint8_t*)++StubAddr;
241 // Assign an address to a symbol name and resolve all the relocations
242 // associated with it.
243 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
245 // The address to use for relocation resolution is not
246 // the address of the local section buffer. We must be doing
247 // a remote execution environment of some sort. Re-apply any
248 // relocations referencing this section with the given address.
250 // Addr is a uint64_t because we can't assume the pointer width
251 // of the target is the same as that of the host. Just use a generic
252 // "big enough" type.
253 Sections[SectionID].LoadAddress = Addr;
254 DEBUG(dbgs() << "Resolving relocations Section #" << SectionID
255 << "\t" << format("%p", (uint8_t *)Addr)
257 resolveRelocationList(Relocations[SectionID], Addr);
260 void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE,
262 uint8_t *Target = Sections[RE.SectionID].Address + RE.Offset;
263 DEBUG(dbgs() << "\tSectionID: " << RE.SectionID
264 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
265 << " Data: " << RE.Data
266 << " Addend: " << RE.Addend
269 resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset,
270 Value, RE.Data, RE.Addend);
273 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
275 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
276 resolveRelocationEntry(Relocs[i], Value);
280 // resolveSymbols - Resolve any relocations to the specified symbols if
281 // we know where it lives.
282 void RuntimeDyldImpl::resolveSymbols() {
283 StringMap<RelocationList>::iterator i = SymbolRelocations.begin(),
284 e = SymbolRelocations.end();
285 for (; i != e; i++) {
286 StringRef Name = i->first();
287 RelocationList &Relocs = i->second;
288 StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name);
289 if (Loc == SymbolTable.end()) {
290 // This is an external symbol, try to get it address from
292 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
294 DEBUG(dbgs() << "Resolving relocations Name: " << Name
295 << "\t" << format("%p", Addr)
297 resolveRelocationList(Relocs, (uintptr_t)Addr);
299 // Change the relocation to be section relative rather than symbol
300 // relative and move it to the resolved relocation list.
301 DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n");
302 for (int i = 0, e = Relocs.size(); i != e; ++i) {
303 RelocationEntry Entry = Relocs[i];
304 Entry.Addend += Loc->second.second;
305 Relocations[Loc->second.first].push_back(Entry);
313 //===----------------------------------------------------------------------===//
314 // RuntimeDyld class implementation
315 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
320 RuntimeDyld::~RuntimeDyld() {
324 bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
326 sys::LLVMFileType type = sys::IdentifyFileType(
327 InputBuffer->getBufferStart(),
328 static_cast<unsigned>(InputBuffer->getBufferSize()));
330 case sys::ELF_Relocatable_FileType:
331 case sys::ELF_Executable_FileType:
332 case sys::ELF_SharedObject_FileType:
333 case sys::ELF_Core_FileType:
334 Dyld = new RuntimeDyldELF(MM);
336 case sys::Mach_O_Object_FileType:
337 case sys::Mach_O_Executable_FileType:
338 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
339 case sys::Mach_O_Core_FileType:
340 case sys::Mach_O_PreloadExecutable_FileType:
341 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
342 case sys::Mach_O_DynamicLinker_FileType:
343 case sys::Mach_O_Bundle_FileType:
344 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
345 case sys::Mach_O_DSYMCompanion_FileType:
346 Dyld = new RuntimeDyldMachO(MM);
348 case sys::Unknown_FileType:
349 case sys::Bitcode_FileType:
350 case sys::Archive_FileType:
351 case sys::COFF_FileType:
352 report_fatal_error("Incompatible object format!");
355 if (!Dyld->isCompatibleFormat(InputBuffer))
356 report_fatal_error("Incompatible object format!");
359 return Dyld->loadObject(InputBuffer);
362 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
363 return Dyld->getSymbolAddress(Name);
366 void RuntimeDyld::resolveRelocations() {
367 Dyld->resolveRelocations();
370 void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
372 Dyld->reassignSectionAddress(SectionID, Addr);
375 void RuntimeDyld::mapSectionAddress(void *LocalAddress,
376 uint64_t TargetAddress) {
377 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
380 StringRef RuntimeDyld::getErrorString() {
381 return Dyld->getErrorString();
384 } // end namespace llvm