blob: ff4a2c847e777a75502f60a79a7716f396c187c9 [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
Jim Grosbach6e563312011-03-21 22:15:52 +00002//
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
Jim Grosbach8b54dca2011-03-23 19:52:00 +000014#define DEBUG_TYPE "dyld"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000015#include "RuntimeDyldImpl.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000016#include "RuntimeDyldELF.h"
17#include "RuntimeDyldMachO.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000018#include "llvm/Support/Path.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000019
Jim Grosbach6e563312011-03-21 22:15:52 +000020using namespace llvm;
21using namespace llvm::object;
22
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000023// Empty out-of-line virtual destructor as the key function.
24RTDyldMemoryManager::~RTDyldMemoryManager() {}
Danil Malyshevcf852dc2011-07-13 07:57:58 +000025RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000026
Jim Grosbach6e563312011-03-21 22:15:52 +000027namespace llvm {
Jim Grosbach6e563312011-03-21 22:15:52 +000028
Jim Grosbach61425c02012-01-16 22:26:39 +000029
Jim Grosbachc41ab782011-04-06 01:11:05 +000030
Jim Grosbachf8c1c842011-04-12 21:20:41 +000031// Resolve the relocations for all symbols we currently know about.
32void RuntimeDyldImpl::resolveRelocations() {
Danil Malyshev799184d2012-03-21 21:06:29 +000033 // First, resolve relocations assotiated with external symbols.
34 resolveSymbols();
35
Jim Grosbach61425c02012-01-16 22:26:39 +000036 // Just iterate over the sections we have and resolve all the relocations
37 // in them. Gross overkill, but it gets the job done.
38 for (int i = 0, e = Sections.size(); i != e; ++i) {
Danil Malyshev799184d2012-03-21 21:06:29 +000039 reassignSectionAddress(i, Sections[i].LoadAddress);
Jim Grosbach61425c02012-01-16 22:26:39 +000040 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000041}
42
Jim Grosbach020f4e82012-01-16 23:50:55 +000043void RuntimeDyldImpl::mapSectionAddress(void *LocalAddress,
44 uint64_t TargetAddress) {
Danil Malyshev799184d2012-03-21 21:06:29 +000045 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
46 if (Sections[i].Address == LocalAddress) {
47 reassignSectionAddress(i, TargetAddress);
48 return;
49 }
50 }
51 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach020f4e82012-01-16 23:50:55 +000052}
53
Danil Malyshev799184d2012-03-21 21:06:29 +000054bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) {
55 // FIXME: ObjectFile don't modify MemoryBuffer.
56 // It should use const MemoryBuffer as parameter.
57 ObjectFile *obj = ObjectFile::
58 createObjectFile(const_cast<MemoryBuffer*>(InputBuffer));
59
60 Arch = (Triple::ArchType)obj->getArch();
61
62 LocalSymbolMap LocalSymbols; // Functions and data symbols from the
63 // object file.
64 ObjSectionToIDMap LocalSections; // Used sections from the object file
65
66 error_code err;
67
68
69 // Parse symbols
70 DEBUG(dbgs() << "Parse symbols:\n");
71 for (symbol_iterator it = obj->begin_symbols(), itEnd = obj->end_symbols();
72 it != itEnd; it.increment(err)) {
73 if (err) break;
74 object::SymbolRef::Type SymType;
75 StringRef Name;
76 if ((bool)(err = it->getType(SymType))) break;
77 if ((bool)(err = it->getName(Name))) break;
78
79 if (SymType == object::SymbolRef::ST_Function ||
80 SymType == object::SymbolRef::ST_Data) {
81 uint64_t FileOffset;
82 uint32_t flags;
83 StringRef sData;
84 section_iterator sIt = obj->end_sections();
85 if ((bool)(err = it->getFileOffset(FileOffset))) break;
86 if ((bool)(err = it->getFlags(flags))) break;
87 if ((bool)(err = it->getSection(sIt))) break;
88 if (sIt == obj->end_sections()) continue;
89 if ((bool)(err = sIt->getContents(sData))) break;
90 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
91 (uintptr_t)FileOffset;
92 uintptr_t SectOffset = (uintptr_t)(SymPtr - (const uint8_t*)sData.begin());
93 unsigned SectionID =
94 findOrEmitSection(*sIt,
95 SymType == object::SymbolRef::ST_Function,
96 LocalSections);
97 bool isGlobal = flags & SymbolRef::SF_Global;
98 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
99 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
100 << " flags: " << flags
101 << " SID: " << SectionID
102 << " Offset: " << format("%p", SectOffset));
103 if (isGlobal)
104 SymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
105 }
106 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
107 }
108 if (err) {
109 report_fatal_error(err.message());
110 }
111
112 // Parse and proccess relocations
113 DEBUG(dbgs() << "Parse relocations:\n");
114 for (section_iterator sIt = obj->begin_sections(),
115 sItEnd = obj->end_sections(); sIt != sItEnd; sIt.increment(err)) {
116 if (err) break;
117 bool isFirstRelocation = true;
118 unsigned SectionID = 0;
119 StubMap Stubs;
120
121 for (relocation_iterator it = sIt->begin_relocations(),
122 itEnd = sIt->end_relocations(); it != itEnd; it.increment(err)) {
123 if (err) break;
124
125 // If it's first relocation in this section, find its SectionID
126 if (isFirstRelocation) {
127 SectionID = findOrEmitSection(*sIt, true, LocalSections);
128 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
129 isFirstRelocation = false;
130 }
131
132 ObjRelocationInfo RI;
133 RI.SectionID = SectionID;
134 if ((bool)(err = it->getAdditionalInfo(RI.AdditionalInfo))) break;
135 if ((bool)(err = it->getOffset(RI.Offset))) break;
136 if ((bool)(err = it->getSymbol(RI.Symbol))) break;
137 if ((bool)(err = it->getType(RI.Type))) break;
138
139 DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo
140 << " Offset: " << format("%p", (uintptr_t)RI.Offset)
141 << " Type: " << (uint32_t)(RI.Type & 0xffffffffL)
142 << "\n");
143 processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs);
144 }
145 if (err) {
146 report_fatal_error(err.message());
147 }
148 }
149 return false;
150}
151
152unsigned RuntimeDyldImpl::emitSection(const SectionRef &Section,
153 bool IsCode) {
154
155 unsigned StubBufSize = 0,
156 StubSize = getMaxStubSize();
157 error_code err;
158 if (StubSize > 0) {
159 for (relocation_iterator it = Section.begin_relocations(),
160 itEnd = Section.end_relocations(); it != itEnd; it.increment(err))
161 StubBufSize += StubSize;
162 }
163 StringRef data;
164 uint64_t Alignment64;
165 if ((bool)(err = Section.getContents(data))) report_fatal_error(err.message());
166 if ((bool)(err = Section.getAlignment(Alignment64)))
167 report_fatal_error(err.message());
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
190unsigned RuntimeDyldImpl::
191findOrEmitSection(const SectionRef &Section, bool IsCode,
192 ObjSectionToIDMap &LocalSections) {
193
194 unsigned SectionID = 0;
195 ObjSectionToIDMap::iterator sIDIt = LocalSections.find(Section);
196 if (sIDIt != LocalSections.end())
197 SectionID = sIDIt->second;
198 else {
199 SectionID = emitSection(Section, IsCode);
200 LocalSections[Section] = SectionID;
201 }
202 return SectionID;
203}
204
205void 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
229uint8_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.
243void 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
260void 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
273void 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.
282void RuntimeDyldImpl::resolveSymbols() {
283 StringMap<RelocationList>::iterator it = SymbolRelocations.begin(),
284 itEnd = SymbolRelocations.end();
285 for (; it != itEnd; it++) {
286 StringRef Name = it->first();
287 RelocationList &Relocs = it->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
Jim Grosbach6e563312011-03-21 22:15:52 +0000313//===----------------------------------------------------------------------===//
314// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000315RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
316 Dyld = 0;
317 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000318}
319
320RuntimeDyld::~RuntimeDyld() {
321 delete Dyld;
322}
323
324bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000325 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000326 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 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000354 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000355 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000356 report_fatal_error("Incompatible object format!");
357 }
358
Jim Grosbach6e563312011-03-21 22:15:52 +0000359 return Dyld->loadObject(InputBuffer);
360}
361
Jim Grosbachb0271052011-04-08 17:31:24 +0000362void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000363 return Dyld->getSymbolAddress(Name);
364}
365
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000366void RuntimeDyld::resolveRelocations() {
367 Dyld->resolveRelocations();
368}
369
Jim Grosbach61425c02012-01-16 22:26:39 +0000370void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
371 uint64_t Addr) {
372 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000373}
374
Jim Grosbach020f4e82012-01-16 23:50:55 +0000375void RuntimeDyld::mapSectionAddress(void *LocalAddress,
376 uint64_t TargetAddress) {
377 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
378}
379
Jim Grosbach91dde152011-03-22 18:22:27 +0000380StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000381 return Dyld->getErrorString();
382}
383
Jim Grosbach6e563312011-03-21 22:15:52 +0000384} // end namespace llvm