Re-factored RuntimeDyLd:
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyld.cpp
1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
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 // Implementation of the MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "dyld"
15 #include "RuntimeDyldImpl.h"
16 #include "RuntimeDyldELF.h"
17 #include "RuntimeDyldMachO.h"
18 #include "llvm/Support/Path.h"
19
20 using namespace llvm;
21 using namespace llvm::object;
22
23 // Empty out-of-line virtual destructor as the key function.
24 RTDyldMemoryManager::~RTDyldMemoryManager() {}
25 RuntimeDyldImpl::~RuntimeDyldImpl() {}
26
27 namespace llvm {
28
29 namespace {
30   // Helper for extensive error checking in debug builds.
31   error_code Check(error_code Err) {
32     if (Err) {
33       report_fatal_error(Err.message());
34     }
35     return Err;
36   }
37 } // end anonymous namespace
38
39
40 // Resolve the relocations for all symbols we currently know about.
41 void RuntimeDyldImpl::resolveRelocations() {
42   // First, resolve relocations assotiated with external symbols.
43   resolveSymbols();
44
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);
49   }
50 }
51
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);
57       return;
58     }
59   }
60   llvm_unreachable("Attempting to remap address of unknown section!");
61 }
62
63 bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) {
64   // FIXME: ObjectFile don't modify MemoryBuffer.
65   //        It should use const MemoryBuffer as parameter.
66   ObjectFile *obj
67     = ObjectFile::createObjectFile(const_cast<MemoryBuffer*>(InputBuffer));
68
69   Arch = (Triple::ArchType)obj->getArch();
70
71   LocalSymbolMap LocalSymbols;     // Functions and data symbols from the
72                                    // object file.
73   ObjSectionToIDMap LocalSections; // Used sections from the object file
74
75   error_code err;
76   // Parse symbols
77   DEBUG(dbgs() << "Parse symbols:\n");
78   for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
79        i != e; i.increment(err)) {
80     Check(err);
81     object::SymbolRef::Type SymType;
82     StringRef Name;
83     Check(i->getType(SymType));
84     Check(i->getName(Name));
85
86     if (SymType == object::SymbolRef::ST_Function ||
87         SymType == object::SymbolRef::ST_Data) {
88       uint64_t FileOffset;
89       uint32_t flags;
90       StringRef sData;
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());
100       unsigned SectionID
101         = findOrEmitSection(*si,
102                           SymType == object::SymbolRef::ST_Function,
103                           LocalSections);
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));
110       if (isGlobal)
111         SymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
112     }
113     DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
114   }
115
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)) {
120     Check(err);
121     bool isFirstRelocation = true;
122     unsigned SectionID = 0;
123     StubMap Stubs;
124
125     for (relocation_iterator i = si->begin_relocations(),
126          e = si->end_relocations(); i != e; i.increment(err)) {
127       Check(err);
128
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;
134       }
135
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));
142
143       DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo
144                    << " Offset: " << format("%p", (uintptr_t)RI.Offset)
145                    << " Type: " << (uint32_t)(RI.Type & 0xffffffffL)
146                    << "\n");
147       processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs);
148     }
149   }
150   return false;
151 }
152
153 unsigned RuntimeDyldImpl::emitSection(const SectionRef &Section,
154                                       bool IsCode) {
155
156   unsigned StubBufSize = 0,
157            StubSize = getMaxStubSize();
158   error_code err;
159   if (StubSize > 0) {
160     for (relocation_iterator i = Section.begin_relocations(),
161          e = Section.end_relocations(); i != e; i.increment(err))
162       StubBufSize += StubSize;
163   }
164   StringRef data;
165   uint64_t Alignment64;
166   Check(Section.getContents(data));
167   Check(Section.getAlignment(Alignment64));
168
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);
177
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
185                << "\n");
186   Sections.push_back(SectionEntry(Addr, Allocate, DataSize,(uintptr_t)pData));
187   return SectionID;
188 }
189
190 unsigned RuntimeDyldImpl::findOrEmitSection(const SectionRef &Section,
191                                             bool IsCode,
192                                             ObjSectionToIDMap &LocalSections) {
193
194   unsigned SectionID = 0;
195   ObjSectionToIDMap::iterator i = LocalSections.find(Section);
196   if (i != LocalSections.end())
197     SectionID = i->second;
198   else {
199     SectionID = emitSection(Section, IsCode);
200     LocalSections[Section] = SectionID;
201   }
202   return SectionID;
203 }
204
205 void RuntimeDyldImpl::AddRelocation(const RelocationValueRef &Value,
206                                    unsigned SectionID, uintptr_t Offset,
207                                    uint32_t RelType) {
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)
213                << "\n");
214
215   if (Value.SymbolName == 0) {
216     Relocations[Value.SectionID].push_back(RelocationEntry(
217       SectionID,
218       Offset,
219       RelType,
220       Value.Addend));
221   } else
222     SymbolRelocations[Value.SymbolName].push_back(RelocationEntry(
223       SectionID,
224       Offset,
225       RelType,
226       Value.Addend));
227 }
228
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;
236   }
237   else
238     return Addr;
239 }
240
241 // Assign an address to a symbol name and resolve all the relocations
242 // associated with it.
243 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
244                                              uint64_t Addr) {
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.
249   //
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)
256           << "\n");
257   resolveRelocationList(Relocations[SectionID], Addr);
258 }
259
260 void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE,
261                                              uint64_t Value) {
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
267           << "\n");
268
269     resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset,
270                       Value, RE.Data, RE.Addend);
271 }
272
273 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
274                                             uint64_t Value) {
275   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
276     resolveRelocationEntry(Relocs[i], Value);
277   }
278 }
279
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
291       // MemoryManager.
292       uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
293                                                                    true);
294       DEBUG(dbgs() << "Resolving relocations Name: " << Name
295               << "\t" << format("%p", Addr)
296               << "\n");
297       resolveRelocationList(Relocs, (uintptr_t)Addr);
298     } else {
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);
306       }
307       Relocs.clear();
308     }
309   }
310 }
311
312
313 //===----------------------------------------------------------------------===//
314 // RuntimeDyld class implementation
315 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
316   Dyld = 0;
317   MM = mm;
318 }
319
320 RuntimeDyld::~RuntimeDyld() {
321   delete Dyld;
322 }
323
324 bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
325   if (!Dyld) {
326     sys::LLVMFileType type = sys::IdentifyFileType(
327             InputBuffer->getBufferStart(),
328             static_cast<unsigned>(InputBuffer->getBufferSize()));
329     switch (type) {
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);
335         break;
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);
347         break;
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!");
353     }
354   } else {
355     if (!Dyld->isCompatibleFormat(InputBuffer))
356       report_fatal_error("Incompatible object format!");
357   }
358
359   return Dyld->loadObject(InputBuffer);
360 }
361
362 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
363   return Dyld->getSymbolAddress(Name);
364 }
365
366 void RuntimeDyld::resolveRelocations() {
367   Dyld->resolveRelocations();
368 }
369
370 void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
371                                          uint64_t Addr) {
372   Dyld->reassignSectionAddress(SectionID, Addr);
373 }
374
375 void RuntimeDyld::mapSectionAddress(void *LocalAddress,
376                                     uint64_t TargetAddress) {
377   Dyld->mapSectionAddress(LocalAddress, TargetAddress);
378 }
379
380 StringRef RuntimeDyld::getErrorString() {
381   return Dyld->getErrorString();
382 }
383
384 } // end namespace llvm