1 //===-- RuntimeDyldMachO.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 "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "RuntimeDyldImpl.h"
20 using namespace llvm::object;
24 bool RuntimeDyldMachO::
25 resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel,
26 unsigned Type, unsigned Size, int64_t Addend) {
27 // This just dispatches to the proper target specific routine.
29 default: assert(0 && "Unsupported CPU type!");
30 case mach::CTM_x86_64:
31 return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value,
32 isPCRel, Type, Size, Addend);
34 return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value,
35 isPCRel, Type, Size, Addend);
40 bool RuntimeDyldMachO::
41 resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
42 unsigned Type, unsigned Size, int64_t Addend) {
43 // If the relocation is PC-relative, the value to be encoded is the
44 // pointer difference.
46 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
47 // address. Is that expected? Only for branches, perhaps?
52 llvm_unreachable("Invalid relocation type!");
53 case macho::RIT_X86_64_Signed1:
54 case macho::RIT_X86_64_Signed2:
55 case macho::RIT_X86_64_Signed4:
56 case macho::RIT_X86_64_Signed:
57 case macho::RIT_X86_64_Unsigned:
58 case macho::RIT_X86_64_Branch: {
60 // Mask in the target value a byte at a time (we don't have an alignment
61 // guarantee for the target address, so this is safest).
62 uint8_t *p = (uint8_t*)Address;
63 for (unsigned i = 0; i < Size; ++i) {
64 *p++ = (uint8_t)Value;
69 case macho::RIT_X86_64_GOTLoad:
70 case macho::RIT_X86_64_GOT:
71 case macho::RIT_X86_64_Subtractor:
72 case macho::RIT_X86_64_TLV:
73 return Error("Relocation type not implemented yet!");
77 bool RuntimeDyldMachO::
78 resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
79 unsigned Type, unsigned Size, int64_t Addend) {
80 // If the relocation is PC-relative, the value to be encoded is the
81 // pointer difference.
84 // ARM PCRel relocations have an effective-PC offset of two instructions
85 // (four bytes in Thumb mode, 8 bytes in ARM mode).
86 // FIXME: For now, assume ARM mode.
92 llvm_unreachable("Invalid relocation type!");
93 case macho::RIT_Vanilla: {
94 llvm_unreachable("Invalid relocation type!");
95 // Mask in the target value a byte at a time (we don't have an alignment
96 // guarantee for the target address, so this is safest).
97 uint8_t *p = (uint8_t*)Address;
98 for (unsigned i = 0; i < Size; ++i) {
99 *p++ = (uint8_t)Value;
104 case macho::RIT_ARM_Branch24Bit: {
105 // Mask the value into the target address. We know instructions are
106 // 32-bit aligned, so we can do it all at once.
107 uint32_t *p = (uint32_t*)Address;
108 // The low two bits of the value are not encoded.
110 // Mask the value to 24 bits.
112 // FIXME: If the destination is a Thumb function (and the instruction
113 // is a non-predicated BL instruction), we need to change it to a BLX
114 // instruction instead.
116 // Insert the value into the instruction.
117 *p = (*p & ~0xffffff) | Value;
120 case macho::RIT_ARM_ThumbBranch22Bit:
121 case macho::RIT_ARM_ThumbBranch32Bit:
122 case macho::RIT_ARM_Half:
123 case macho::RIT_ARM_HalfDifference:
124 case macho::RIT_Pair:
125 case macho::RIT_Difference:
126 case macho::RIT_ARM_LocalDifference:
127 case macho::RIT_ARM_PreboundLazyPointer:
128 return Error("Relocation type not implemented yet!");
133 bool RuntimeDyldMachO::
134 loadSegment32(const MachOObject *Obj,
135 const MachOObject::LoadCommandInfo *SegmentLCI,
136 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
137 // FIXME: This should really be combined w/ loadSegment64. Templatized
138 // function on the 32/64 datatypes maybe?
139 InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
140 Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC);
142 return Error("unable to load segment load command");
145 SmallVector<unsigned, 16> SectionMap;
146 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
147 InMemoryStruct<macho::Section> Sect;
148 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
150 return Error("unable to load section: '" + Twine(SectNum) + "'");
152 // Allocate memory via the MM for the section.
154 uint32_t SectionID = Sections.size();
155 if (Sect->Flags != 0x80000400)
156 Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
158 Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
160 DEBUG(dbgs() << "Loading "
161 << ((Sect->Flags == 0x80000400) ? "text" : "data")
162 << " (ID #" << SectionID << ")"
163 << " '" << Sect->SegmentName << ","
164 << Sect->Name << "' of size " << Sect->Size
165 << " to address " << Buffer << ".\n");
167 // Copy the payload from the object file into the allocated buffer.
168 uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
169 SegmentLC->FileSize).data();
170 memcpy(Buffer, Base + Sect->Address, Sect->Size);
172 // Remember what got allocated for this SectionID.
173 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
174 SectionLocalMemToID[Buffer] = SectionID;
176 // By default, the load address of a section is its memory buffer.
177 SectionLoadAddress.push_back((uint64_t)Buffer);
179 // Keep a map of object file section numbers to corresponding SectionIDs
180 // while processing the file.
181 SectionMap.push_back(SectionID);
184 // Process the symbol table.
185 SmallVector<StringRef, 64> SymbolNames;
186 processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC);
188 // Process the relocations for each section we're loading.
189 Relocations.grow(Relocations.size() + SegmentLC->NumSections);
190 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
191 InMemoryStruct<macho::Section> Sect;
192 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
194 return Error("unable to load section: '" + Twine(SectNum) + "'");
195 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
196 InMemoryStruct<macho::RelocationEntry> RE;
197 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
198 if (RE->Word0 & macho::RF_Scattered)
199 return Error("NOT YET IMPLEMENTED: scattered relocations.");
200 // Word0 of the relocation is the offset into the section where the
201 // relocation should be applied. We need to translate that into an
202 // offset into a function since that's our atom.
203 uint32_t Offset = RE->Word0;
204 bool isExtern = (RE->Word1 >> 27) & 1;
206 // FIXME: Get the relocation addend from the target address.
207 // FIXME: VERY imporant for internal relocations.
209 // Figure out the source symbol of the relocation. If isExtern is true,
210 // this relocation references the symbol table, otherwise it references
211 // a section in the same object, numbered from 1 through NumSections
212 // (SectionBases is [0, NumSections-1]).
213 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
215 assert(SourceNum > 0 && "Invalid relocation section number!");
216 unsigned SectionID = SectionMap[SourceNum - 1];
217 unsigned TargetID = SectionMap[SectNum];
218 DEBUG(dbgs() << "Internal relocation at Section #"
219 << TargetID << " + " << Offset
221 << SectionID << " (Word1: "
222 << format("0x%x", RE->Word1) << ")\n");
224 // Store the relocation information. It will get resolved when
225 // the section addresses are assigned.
226 Relocations[SectionID].push_back(RelocationEntry(TargetID,
231 StringRef SourceName = SymbolNames[SourceNum];
233 // Now store the relocation information. Associate it with the source
234 // symbol. Just add it to the unresolved list and let the general
235 // path post-load resolve it if we know where the symbol is.
236 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
240 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
241 << " from '" << SourceName << "(Word1: "
242 << format("0x%x", RE->Word1) << ")\n");
247 // Resolve the addresses of any symbols that were defined in this segment.
248 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
249 resolveSymbol(SymbolNames[i]);
255 bool RuntimeDyldMachO::
256 loadSegment64(const MachOObject *Obj,
257 const MachOObject::LoadCommandInfo *SegmentLCI,
258 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
259 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
260 Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
262 return Error("unable to load segment load command");
265 SmallVector<unsigned, 16> SectionMap;
266 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
267 InMemoryStruct<macho::Section64> Sect;
268 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
270 return Error("unable to load section: '" + Twine(SectNum) + "'");
272 // Allocate memory via the MM for the section.
274 uint32_t SectionID = Sections.size();
275 unsigned Align = 1 << Sect->Align; // .o file has log2 alignment.
276 if (Sect->Flags == 0x80000400)
277 Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID);
279 Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID);
281 DEBUG(dbgs() << "Loading "
282 << ((Sect->Flags == 0x80000400) ? "text" : "data")
283 << " (ID #" << SectionID << ")"
284 << " '" << Sect->SegmentName << ","
285 << Sect->Name << "' of size " << Sect->Size
286 << " (align " << Align << ")"
287 << " to address " << Buffer << ".\n");
289 // Copy the payload from the object file into the allocated buffer.
290 uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
291 Segment64LC->FileSize).data();
292 memcpy(Buffer, Base + Sect->Address, Sect->Size);
294 // Remember what got allocated for this SectionID.
295 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
296 SectionLocalMemToID[Buffer] = SectionID;
298 // By default, the load address of a section is its memory buffer.
299 SectionLoadAddress.push_back((uint64_t)Buffer);
301 // Keep a map of object file section numbers to corresponding SectionIDs
302 // while processing the file.
303 SectionMap.push_back(SectionID);
306 // Process the symbol table.
307 SmallVector<StringRef, 64> SymbolNames;
308 processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC);
310 // Process the relocations for each section we're loading.
311 Relocations.grow(Relocations.size() + Segment64LC->NumSections);
312 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
313 InMemoryStruct<macho::Section64> Sect;
314 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
316 return Error("unable to load section: '" + Twine(SectNum) + "'");
317 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
318 InMemoryStruct<macho::RelocationEntry> RE;
319 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
320 if (RE->Word0 & macho::RF_Scattered)
321 return Error("NOT YET IMPLEMENTED: scattered relocations.");
322 // Word0 of the relocation is the offset into the section where the
323 // relocation should be applied. We need to translate that into an
324 // offset into a function since that's our atom.
325 uint32_t Offset = RE->Word0;
326 bool isExtern = (RE->Word1 >> 27) & 1;
328 // FIXME: Get the relocation addend from the target address.
329 // FIXME: VERY imporant for internal relocations.
331 // Figure out the source symbol of the relocation. If isExtern is true,
332 // this relocation references the symbol table, otherwise it references
333 // a section in the same object, numbered from 1 through NumSections
334 // (SectionBases is [0, NumSections-1]).
335 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
337 assert(SourceNum > 0 && "Invalid relocation section number!");
338 unsigned SectionID = SectionMap[SourceNum - 1];
339 unsigned TargetID = SectionMap[SectNum];
340 DEBUG(dbgs() << "Internal relocation at Section #"
341 << TargetID << " + " << Offset
343 << SectionID << " (Word1: "
344 << format("0x%x", RE->Word1) << ")\n");
346 // Store the relocation information. It will get resolved when
347 // the section addresses are assigned.
348 Relocations[SectionID].push_back(RelocationEntry(TargetID,
353 StringRef SourceName = SymbolNames[SourceNum];
355 // Now store the relocation information. Associate it with the source
356 // symbol. Just add it to the unresolved list and let the general
357 // path post-load resolve it if we know where the symbol is.
358 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
362 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
363 << " from '" << SourceName << "(Word1: "
364 << format("0x%x", RE->Word1) << ")\n");
369 // Resolve the addresses of any symbols that were defined in this segment.
370 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
371 resolveSymbol(SymbolNames[i]);
376 bool RuntimeDyldMachO::
377 processSymbols32(const MachOObject *Obj,
378 SmallVectorImpl<unsigned> &SectionMap,
379 SmallVectorImpl<StringRef> &SymbolNames,
380 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
381 // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such.
382 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
383 InMemoryStruct<macho::SymbolTableEntry> STE;
384 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
386 return Error("unable to read symbol: '" + Twine(i) + "'");
387 // Get the symbol name.
388 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
389 SymbolNames.push_back(Name);
391 // FIXME: Check the symbol type and flags.
392 if (STE->Type != 0xF) // external, defined in this segment.
394 // Flags in the upper nibble we don't care about.
395 if ((STE->Flags & 0xf) != 0x0)
398 // Remember the symbol.
399 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
400 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
402 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
403 << (getSectionAddress(SectionID) + STE->Value)
409 bool RuntimeDyldMachO::
410 processSymbols64(const MachOObject *Obj,
411 SmallVectorImpl<unsigned> &SectionMap,
412 SmallVectorImpl<StringRef> &SymbolNames,
413 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
414 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
415 InMemoryStruct<macho::Symbol64TableEntry> STE;
416 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
418 return Error("unable to read symbol: '" + Twine(i) + "'");
419 // Get the symbol name.
420 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
421 SymbolNames.push_back(Name);
423 // FIXME: Check the symbol type and flags.
424 if (STE->Type != 0xF) // external, defined in this segment.
426 // Flags in the upper nibble we don't care about.
427 if ((STE->Flags & 0xf) != 0x0)
430 // Remember the symbol.
431 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
432 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
434 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
435 << (getSectionAddress(SectionID) + STE->Value)
441 // resolveSymbol - Resolve any relocations to the specified symbol if
442 // we know where it lives.
443 void RuntimeDyldMachO::resolveSymbol(StringRef Name) {
444 StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name);
445 if (Loc == SymbolTable.end())
448 RelocationList &Relocs = UnresolvedRelocations[Name];
449 DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n");
450 for (int i = 0, e = Relocs.size(); i != e; ++i) {
451 // Change the relocation to be section relative rather than symbol
452 // relative and move it to the resolved relocation list.
453 RelocationEntry Entry = Relocs[i];
454 Entry.Addend += Loc->second.second;
455 Relocations[Loc->second.first].push_back(Entry);
457 // FIXME: Keep a worklist of the relocations we've added so that we can
458 // resolve more selectively later.
462 bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
463 // If the linker is in an error state, don't do anything.
466 // Load the Mach-O wrapper object.
467 std::string ErrorStr;
468 OwningPtr<MachOObject> Obj(
469 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr));
471 return Error("unable to load object: '" + ErrorStr + "'");
473 // Get the CPU type information from the header.
474 const macho::Header &Header = Obj->getHeader();
476 // FIXME: Error checking that the loaded object is compatible with
477 // the system we're running on.
478 CPUType = Header.CPUType;
479 CPUSubtype = Header.CPUSubtype;
481 // Validate that the load commands match what we expect.
482 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
484 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
485 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i);
486 switch (LCI.Command.Type) {
487 case macho::LCT_Segment:
488 case macho::LCT_Segment64:
490 return Error("unexpected input object (multiple segments)");
493 case macho::LCT_Symtab:
495 return Error("unexpected input object (multiple symbol tables)");
498 case macho::LCT_Dysymtab:
500 return Error("unexpected input object (multiple symbol tables)");
504 return Error("unexpected input object (unexpected load command");
509 return Error("no symbol table found in object");
511 return Error("no segments found in object");
513 // Read and register the symbol table data.
514 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
515 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
517 return Error("unable to load symbol table load command");
518 Obj->RegisterStringTable(*SymtabLC);
520 // Read the dynamic link-edit information, if present (not present in static
523 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
524 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
526 return Error("unable to load dynamic link-exit load command");
528 // FIXME: We don't support anything interesting yet.
529 // if (DysymtabLC->LocalSymbolsIndex != 0)
530 // return Error("NOT YET IMPLEMENTED: local symbol entries");
531 // if (DysymtabLC->ExternalSymbolsIndex != 0)
532 // return Error("NOT YET IMPLEMENTED: non-external symbol entries");
533 // if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries)
534 // return Error("NOT YET IMPLEMENTED: undefined symbol entries");
537 // Load the segment load command.
538 if (SegmentLCI->Command.Type == macho::LCT_Segment) {
539 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC))
542 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC))
546 // Assign the addresses of the sections from the object so that any
547 // relocations to them get set properly.
548 // FIXME: This is done directly from the client at the moment. We should
549 // default the values to the local storage, at least when the target arch
550 // is the same as the host arch.
555 // Assign an address to a symbol name and resolve all the relocations
556 // associated with it.
557 void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID,
559 // The address to use for relocation resolution is not
560 // the address of the local section buffer. We must be doing
561 // a remote execution environment of some sort. Re-apply any
562 // relocations referencing this section with the given address.
564 // Addr is a uint64_t because we can't assume the pointer width
565 // of the target is the same as that of the host. Just use a generic
566 // "big enough" type.
568 SectionLoadAddress[SectionID] = Addr;
570 RelocationList &Relocs = Relocations[SectionID];
571 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
572 RelocationEntry &RE = Relocs[i];
573 uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset;
574 bool isPCRel = (RE.Data >> 24) & 1;
575 unsigned Type = (RE.Data >> 28) & 0xf;
576 unsigned Size = 1 << ((RE.Data >> 25) & 3);
578 DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID
579 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
580 << " from Section #" << SectionID << " (" << format("%p", Addr) << ")"
581 << "(" << (isPCRel ? "pcrel" : "absolute")
582 << ", type: " << Type << ", Size: " << Size << ", Addend: "
583 << RE.Addend << ").\n");
585 resolveRelocation(Target, Addr, isPCRel, Type, Size, RE.Addend);
589 bool RuntimeDyldMachO::isKnownFormat(const MemoryBuffer *InputBuffer) {
590 StringRef Magic = InputBuffer->getBuffer().slice(0, 4);
591 if (Magic == "\xFE\xED\xFA\xCE") return true;
592 if (Magic == "\xCE\xFA\xED\xFE") return true;
593 if (Magic == "\xFE\xED\xFA\xCF") return true;
594 if (Magic == "\xCF\xFA\xED\xFE") return true;
598 } // end namespace llvm