RuntimeDyld.cpp revision 223017
1//===-- RuntimeDyld.h - 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 "llvm/ADT/OwningPtr.h" 16#include "llvm/ADT/SmallVector.h" 17#include "llvm/ADT/StringMap.h" 18#include "llvm/ADT/StringRef.h" 19#include "llvm/ADT/STLExtras.h" 20#include "llvm/ADT/Twine.h" 21#include "llvm/ExecutionEngine/RuntimeDyld.h" 22#include "llvm/Object/MachOObject.h" 23#include "llvm/Support/Debug.h" 24#include "llvm/Support/ErrorHandling.h" 25#include "llvm/Support/Format.h" 26#include "llvm/Support/Memory.h" 27#include "llvm/Support/MemoryBuffer.h" 28#include "llvm/Support/system_error.h" 29#include "llvm/Support/raw_ostream.h" 30using namespace llvm; 31using namespace llvm::object; 32 33// Empty out-of-line virtual destructor as the key function. 34RTDyldMemoryManager::~RTDyldMemoryManager() {} 35 36namespace llvm { 37class RuntimeDyldImpl { 38 unsigned CPUType; 39 unsigned CPUSubtype; 40 41 // The MemoryManager to load objects into. 42 RTDyldMemoryManager *MemMgr; 43 44 // FIXME: This all assumes we're dealing with external symbols for anything 45 // explicitly referenced. I.e., we can index by name and things 46 // will work out. In practice, this may not be the case, so we 47 // should find a way to effectively generalize. 48 49 // For each function, we have a MemoryBlock of it's instruction data. 50 StringMap<sys::MemoryBlock> Functions; 51 52 // Master symbol table. As modules are loaded and external symbols are 53 // resolved, their addresses are stored here. 54 StringMap<uint8_t*> SymbolTable; 55 56 // For each symbol, keep a list of relocations based on it. Anytime 57 // its address is reassigned (the JIT re-compiled the function, e.g.), 58 // the relocations get re-resolved. 59 struct RelocationEntry { 60 std::string Target; // Object this relocation is contained in. 61 uint64_t Offset; // Offset into the object for the relocation. 62 uint32_t Data; // Second word of the raw macho relocation entry. 63 int64_t Addend; // Addend encoded in the instruction itself, if any. 64 bool isResolved; // Has this relocation been resolved previously? 65 66 RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend) 67 : Target(t), Offset(offset), Data(data), Addend(addend), 68 isResolved(false) {} 69 }; 70 typedef SmallVector<RelocationEntry, 4> RelocationList; 71 StringMap<RelocationList> Relocations; 72 73 // FIXME: Also keep a map of all the relocations contained in an object. Use 74 // this to dynamically answer whether all of the relocations in it have 75 // been resolved or not. 76 77 bool HasError; 78 std::string ErrorStr; 79 80 // Set the error state and record an error string. 81 bool Error(const Twine &Msg) { 82 ErrorStr = Msg.str(); 83 HasError = true; 84 return true; 85 } 86 87 void extractFunction(StringRef Name, uint8_t *StartAddress, 88 uint8_t *EndAddress); 89 bool resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel, 90 unsigned Type, unsigned Size); 91 bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, 92 unsigned Type, unsigned Size); 93 bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, 94 unsigned Type, unsigned Size); 95 96 bool loadSegment32(const MachOObject *Obj, 97 const MachOObject::LoadCommandInfo *SegmentLCI, 98 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); 99 bool loadSegment64(const MachOObject *Obj, 100 const MachOObject::LoadCommandInfo *SegmentLCI, 101 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); 102 103public: 104 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {} 105 106 bool loadObject(MemoryBuffer *InputBuffer); 107 108 void *getSymbolAddress(StringRef Name) { 109 // FIXME: Just look up as a function for now. Overly simple of course. 110 // Work in progress. 111 return SymbolTable.lookup(Name); 112 } 113 114 void resolveRelocations(); 115 116 void reassignSymbolAddress(StringRef Name, uint8_t *Addr); 117 118 // Is the linker in an error state? 119 bool hasError() { return HasError; } 120 121 // Mark the error condition as handled and continue. 122 void clearError() { HasError = false; } 123 124 // Get the error message. 125 StringRef getErrorString() { return ErrorStr; } 126}; 127 128void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress, 129 uint8_t *EndAddress) { 130 // Allocate memory for the function via the memory manager. 131 uintptr_t Size = EndAddress - StartAddress + 1; 132 uintptr_t AllocSize = Size; 133 uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize); 134 assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) && 135 "Memory manager failed to allocate enough memory!"); 136 // Copy the function payload into the memory block. 137 memcpy(Mem, StartAddress, Size); 138 MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size); 139 // Remember where we put it. 140 Functions[Name] = sys::MemoryBlock(Mem, Size); 141 // Default the assigned address for this symbol to wherever this 142 // allocated it. 143 SymbolTable[Name] = Mem; 144 DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n"); 145} 146 147bool RuntimeDyldImpl:: 148resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel, 149 unsigned Type, unsigned Size) { 150 // This just dispatches to the proper target specific routine. 151 switch (CPUType) { 152 default: assert(0 && "Unsupported CPU type!"); 153 case mach::CTM_x86_64: 154 return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value, 155 isPCRel, Type, Size); 156 case mach::CTM_ARM: 157 return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value, 158 isPCRel, Type, Size); 159 } 160 llvm_unreachable(""); 161} 162 163bool RuntimeDyldImpl:: 164resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, 165 bool isPCRel, unsigned Type, 166 unsigned Size) { 167 // If the relocation is PC-relative, the value to be encoded is the 168 // pointer difference. 169 if (isPCRel) 170 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC 171 // address. Is that expected? Only for branches, perhaps? 172 Value -= Address + 4; 173 174 switch(Type) { 175 default: 176 llvm_unreachable("Invalid relocation type!"); 177 case macho::RIT_X86_64_Unsigned: 178 case macho::RIT_X86_64_Branch: { 179 // Mask in the target value a byte at a time (we don't have an alignment 180 // guarantee for the target address, so this is safest). 181 uint8_t *p = (uint8_t*)Address; 182 for (unsigned i = 0; i < Size; ++i) { 183 *p++ = (uint8_t)Value; 184 Value >>= 8; 185 } 186 return false; 187 } 188 case macho::RIT_X86_64_Signed: 189 case macho::RIT_X86_64_GOTLoad: 190 case macho::RIT_X86_64_GOT: 191 case macho::RIT_X86_64_Subtractor: 192 case macho::RIT_X86_64_Signed1: 193 case macho::RIT_X86_64_Signed2: 194 case macho::RIT_X86_64_Signed4: 195 case macho::RIT_X86_64_TLV: 196 return Error("Relocation type not implemented yet!"); 197 } 198 return false; 199} 200 201bool RuntimeDyldImpl::resolveARMRelocation(uintptr_t Address, uintptr_t Value, 202 bool isPCRel, unsigned Type, 203 unsigned Size) { 204 // If the relocation is PC-relative, the value to be encoded is the 205 // pointer difference. 206 if (isPCRel) { 207 Value -= Address; 208 // ARM PCRel relocations have an effective-PC offset of two instructions 209 // (four bytes in Thumb mode, 8 bytes in ARM mode). 210 // FIXME: For now, assume ARM mode. 211 Value -= 8; 212 } 213 214 switch(Type) { 215 default: 216 llvm_unreachable("Invalid relocation type!"); 217 case macho::RIT_Vanilla: { 218 llvm_unreachable("Invalid relocation type!"); 219 // Mask in the target value a byte at a time (we don't have an alignment 220 // guarantee for the target address, so this is safest). 221 uint8_t *p = (uint8_t*)Address; 222 for (unsigned i = 0; i < Size; ++i) { 223 *p++ = (uint8_t)Value; 224 Value >>= 8; 225 } 226 break; 227 } 228 case macho::RIT_ARM_Branch24Bit: { 229 // Mask the value into the target address. We know instructions are 230 // 32-bit aligned, so we can do it all at once. 231 uint32_t *p = (uint32_t*)Address; 232 // The low two bits of the value are not encoded. 233 Value >>= 2; 234 // Mask the value to 24 bits. 235 Value &= 0xffffff; 236 // FIXME: If the destination is a Thumb function (and the instruction 237 // is a non-predicated BL instruction), we need to change it to a BLX 238 // instruction instead. 239 240 // Insert the value into the instruction. 241 *p = (*p & ~0xffffff) | Value; 242 break; 243 } 244 case macho::RIT_ARM_ThumbBranch22Bit: 245 case macho::RIT_ARM_ThumbBranch32Bit: 246 case macho::RIT_ARM_Half: 247 case macho::RIT_ARM_HalfDifference: 248 case macho::RIT_Pair: 249 case macho::RIT_Difference: 250 case macho::RIT_ARM_LocalDifference: 251 case macho::RIT_ARM_PreboundLazyPointer: 252 return Error("Relocation type not implemented yet!"); 253 } 254 return false; 255} 256 257bool RuntimeDyldImpl:: 258loadSegment32(const MachOObject *Obj, 259 const MachOObject::LoadCommandInfo *SegmentLCI, 260 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { 261 InMemoryStruct<macho::SegmentLoadCommand> SegmentLC; 262 Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC); 263 if (!SegmentLC) 264 return Error("unable to load segment load command"); 265 266 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { 267 InMemoryStruct<macho::Section> Sect; 268 Obj->ReadSection(*SegmentLCI, SectNum, Sect); 269 if (!Sect) 270 return Error("unable to load section: '" + Twine(SectNum) + "'"); 271 272 // FIXME: For the time being, we're only loading text segments. 273 if (Sect->Flags != 0x80000400) 274 continue; 275 276 // Address and names of symbols in the section. 277 typedef std::pair<uint64_t, StringRef> SymbolEntry; 278 SmallVector<SymbolEntry, 64> Symbols; 279 // Index of all the names, in this section or not. Used when we're 280 // dealing with relocation entries. 281 SmallVector<StringRef, 64> SymbolNames; 282 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { 283 InMemoryStruct<macho::SymbolTableEntry> STE; 284 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE); 285 if (!STE) 286 return Error("unable to read symbol: '" + Twine(i) + "'"); 287 if (STE->SectionIndex > SegmentLC->NumSections) 288 return Error("invalid section index for symbol: '" + Twine(i) + "'"); 289 // Get the symbol name. 290 StringRef Name = Obj->getStringAtIndex(STE->StringIndex); 291 SymbolNames.push_back(Name); 292 293 // Just skip symbols not defined in this section. 294 if ((unsigned)STE->SectionIndex - 1 != SectNum) 295 continue; 296 297 // FIXME: Check the symbol type and flags. 298 if (STE->Type != 0xF) // external, defined in this section. 299 continue; 300 // Flags == 0x8 marks a thumb function for ARM, which is fine as it 301 // doesn't require any special handling here. 302 if (STE->Flags != 0x0 && STE->Flags != 0x8) 303 continue; 304 305 // Remember the symbol. 306 Symbols.push_back(SymbolEntry(STE->Value, Name)); 307 308 DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << 309 (Sect->Address + STE->Value) << "\n"); 310 } 311 // Sort the symbols by address, just in case they didn't come in that way. 312 array_pod_sort(Symbols.begin(), Symbols.end()); 313 314 // If there weren't any functions (odd, but just in case...) 315 if (!Symbols.size()) 316 continue; 317 318 // Extract the function data. 319 uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset, 320 SegmentLC->FileSize).data(); 321 for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) { 322 uint64_t StartOffset = Sect->Address + Symbols[i].first; 323 uint64_t EndOffset = Symbols[i + 1].first - 1; 324 DEBUG(dbgs() << "Extracting function: " << Symbols[i].second 325 << " from [" << StartOffset << ", " << EndOffset << "]\n"); 326 extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset); 327 } 328 // The last symbol we do after since the end address is calculated 329 // differently because there is no next symbol to reference. 330 uint64_t StartOffset = Symbols[Symbols.size() - 1].first; 331 uint64_t EndOffset = Sect->Size - 1; 332 DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second 333 << " from [" << StartOffset << ", " << EndOffset << "]\n"); 334 extractFunction(Symbols[Symbols.size()-1].second, 335 Base + StartOffset, Base + EndOffset); 336 337 // Now extract the relocation information for each function and process it. 338 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { 339 InMemoryStruct<macho::RelocationEntry> RE; 340 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); 341 if (RE->Word0 & macho::RF_Scattered) 342 return Error("NOT YET IMPLEMENTED: scattered relocations."); 343 // Word0 of the relocation is the offset into the section where the 344 // relocation should be applied. We need to translate that into an 345 // offset into a function since that's our atom. 346 uint32_t Offset = RE->Word0; 347 // Look for the function containing the address. This is used for JIT 348 // code, so the number of functions in section is almost always going 349 // to be very small (usually just one), so until we have use cases 350 // where that's not true, just use a trivial linear search. 351 unsigned SymbolNum; 352 unsigned NumSymbols = Symbols.size(); 353 assert(NumSymbols > 0 && Symbols[0].first <= Offset && 354 "No symbol containing relocation!"); 355 for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum) 356 if (Symbols[SymbolNum + 1].first > Offset) 357 break; 358 // Adjust the offset to be relative to the symbol. 359 Offset -= Symbols[SymbolNum].first; 360 // Get the name of the symbol containing the relocation. 361 StringRef TargetName = SymbolNames[SymbolNum]; 362 363 bool isExtern = (RE->Word1 >> 27) & 1; 364 // Figure out the source symbol of the relocation. If isExtern is true, 365 // this relocation references the symbol table, otherwise it references 366 // a section in the same object, numbered from 1 through NumSections 367 // (SectionBases is [0, NumSections-1]). 368 // FIXME: Some targets (ARM) use internal relocations even for 369 // externally visible symbols, if the definition is in the same 370 // file as the reference. We need to convert those back to by-name 371 // references. We can resolve the address based on the section 372 // offset and see if we have a symbol at that address. If we do, 373 // use that; otherwise, puke. 374 if (!isExtern) 375 return Error("Internal relocations not supported."); 376 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value 377 StringRef SourceName = SymbolNames[SourceNum]; 378 379 // FIXME: Get the relocation addend from the target address. 380 381 // Now store the relocation information. Associate it with the source 382 // symbol. 383 Relocations[SourceName].push_back(RelocationEntry(TargetName, 384 Offset, 385 RE->Word1, 386 0 /*Addend*/)); 387 DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset 388 << " from '" << SourceName << "(Word1: " 389 << format("0x%x", RE->Word1) << ")\n"); 390 } 391 } 392 return false; 393} 394 395 396bool RuntimeDyldImpl:: 397loadSegment64(const MachOObject *Obj, 398 const MachOObject::LoadCommandInfo *SegmentLCI, 399 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { 400 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC; 401 Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC); 402 if (!Segment64LC) 403 return Error("unable to load segment load command"); 404 405 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { 406 InMemoryStruct<macho::Section64> Sect; 407 Obj->ReadSection64(*SegmentLCI, SectNum, Sect); 408 if (!Sect) 409 return Error("unable to load section: '" + Twine(SectNum) + "'"); 410 411 // FIXME: For the time being, we're only loading text segments. 412 if (Sect->Flags != 0x80000400) 413 continue; 414 415 // Address and names of symbols in the section. 416 typedef std::pair<uint64_t, StringRef> SymbolEntry; 417 SmallVector<SymbolEntry, 64> Symbols; 418 // Index of all the names, in this section or not. Used when we're 419 // dealing with relocation entries. 420 SmallVector<StringRef, 64> SymbolNames; 421 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { 422 InMemoryStruct<macho::Symbol64TableEntry> STE; 423 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE); 424 if (!STE) 425 return Error("unable to read symbol: '" + Twine(i) + "'"); 426 if (STE->SectionIndex > Segment64LC->NumSections) 427 return Error("invalid section index for symbol: '" + Twine(i) + "'"); 428 // Get the symbol name. 429 StringRef Name = Obj->getStringAtIndex(STE->StringIndex); 430 SymbolNames.push_back(Name); 431 432 // Just skip symbols not defined in this section. 433 if ((unsigned)STE->SectionIndex - 1 != SectNum) 434 continue; 435 436 // FIXME: Check the symbol type and flags. 437 if (STE->Type != 0xF) // external, defined in this section. 438 continue; 439 if (STE->Flags != 0x0) 440 continue; 441 442 // Remember the symbol. 443 Symbols.push_back(SymbolEntry(STE->Value, Name)); 444 445 DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << 446 (Sect->Address + STE->Value) << "\n"); 447 } 448 // Sort the symbols by address, just in case they didn't come in that way. 449 array_pod_sort(Symbols.begin(), Symbols.end()); 450 451 // If there weren't any functions (odd, but just in case...) 452 if (!Symbols.size()) 453 continue; 454 455 // Extract the function data. 456 uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset, 457 Segment64LC->FileSize).data(); 458 for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) { 459 uint64_t StartOffset = Sect->Address + Symbols[i].first; 460 uint64_t EndOffset = Symbols[i + 1].first - 1; 461 DEBUG(dbgs() << "Extracting function: " << Symbols[i].second 462 << " from [" << StartOffset << ", " << EndOffset << "]\n"); 463 extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset); 464 } 465 // The last symbol we do after since the end address is calculated 466 // differently because there is no next symbol to reference. 467 uint64_t StartOffset = Symbols[Symbols.size() - 1].first; 468 uint64_t EndOffset = Sect->Size - 1; 469 DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second 470 << " from [" << StartOffset << ", " << EndOffset << "]\n"); 471 extractFunction(Symbols[Symbols.size()-1].second, 472 Base + StartOffset, Base + EndOffset); 473 474 // Now extract the relocation information for each function and process it. 475 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { 476 InMemoryStruct<macho::RelocationEntry> RE; 477 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); 478 if (RE->Word0 & macho::RF_Scattered) 479 return Error("NOT YET IMPLEMENTED: scattered relocations."); 480 // Word0 of the relocation is the offset into the section where the 481 // relocation should be applied. We need to translate that into an 482 // offset into a function since that's our atom. 483 uint32_t Offset = RE->Word0; 484 // Look for the function containing the address. This is used for JIT 485 // code, so the number of functions in section is almost always going 486 // to be very small (usually just one), so until we have use cases 487 // where that's not true, just use a trivial linear search. 488 unsigned SymbolNum; 489 unsigned NumSymbols = Symbols.size(); 490 assert(NumSymbols > 0 && Symbols[0].first <= Offset && 491 "No symbol containing relocation!"); 492 for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum) 493 if (Symbols[SymbolNum + 1].first > Offset) 494 break; 495 // Adjust the offset to be relative to the symbol. 496 Offset -= Symbols[SymbolNum].first; 497 // Get the name of the symbol containing the relocation. 498 StringRef TargetName = SymbolNames[SymbolNum]; 499 500 bool isExtern = (RE->Word1 >> 27) & 1; 501 // Figure out the source symbol of the relocation. If isExtern is true, 502 // this relocation references the symbol table, otherwise it references 503 // a section in the same object, numbered from 1 through NumSections 504 // (SectionBases is [0, NumSections-1]). 505 if (!isExtern) 506 return Error("Internal relocations not supported."); 507 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value 508 StringRef SourceName = SymbolNames[SourceNum]; 509 510 // FIXME: Get the relocation addend from the target address. 511 512 // Now store the relocation information. Associate it with the source 513 // symbol. 514 Relocations[SourceName].push_back(RelocationEntry(TargetName, 515 Offset, 516 RE->Word1, 517 0 /*Addend*/)); 518 DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset 519 << " from '" << SourceName << "(Word1: " 520 << format("0x%x", RE->Word1) << ")\n"); 521 } 522 } 523 return false; 524} 525 526bool RuntimeDyldImpl::loadObject(MemoryBuffer *InputBuffer) { 527 // If the linker is in an error state, don't do anything. 528 if (hasError()) 529 return true; 530 // Load the Mach-O wrapper object. 531 std::string ErrorStr; 532 OwningPtr<MachOObject> Obj( 533 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr)); 534 if (!Obj) 535 return Error("unable to load object: '" + ErrorStr + "'"); 536 537 // Get the CPU type information from the header. 538 const macho::Header &Header = Obj->getHeader(); 539 540 // FIXME: Error checking that the loaded object is compatible with 541 // the system we're running on. 542 CPUType = Header.CPUType; 543 CPUSubtype = Header.CPUSubtype; 544 545 // Validate that the load commands match what we expect. 546 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0, 547 *DysymtabLCI = 0; 548 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { 549 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i); 550 switch (LCI.Command.Type) { 551 case macho::LCT_Segment: 552 case macho::LCT_Segment64: 553 if (SegmentLCI) 554 return Error("unexpected input object (multiple segments)"); 555 SegmentLCI = &LCI; 556 break; 557 case macho::LCT_Symtab: 558 if (SymtabLCI) 559 return Error("unexpected input object (multiple symbol tables)"); 560 SymtabLCI = &LCI; 561 break; 562 case macho::LCT_Dysymtab: 563 if (DysymtabLCI) 564 return Error("unexpected input object (multiple symbol tables)"); 565 DysymtabLCI = &LCI; 566 break; 567 default: 568 return Error("unexpected input object (unexpected load command"); 569 } 570 } 571 572 if (!SymtabLCI) 573 return Error("no symbol table found in object"); 574 if (!SegmentLCI) 575 return Error("no symbol table found in object"); 576 577 // Read and register the symbol table data. 578 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC; 579 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC); 580 if (!SymtabLC) 581 return Error("unable to load symbol table load command"); 582 Obj->RegisterStringTable(*SymtabLC); 583 584 // Read the dynamic link-edit information, if present (not present in static 585 // objects). 586 if (DysymtabLCI) { 587 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC; 588 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC); 589 if (!DysymtabLC) 590 return Error("unable to load dynamic link-exit load command"); 591 592 // FIXME: We don't support anything interesting yet. 593// if (DysymtabLC->LocalSymbolsIndex != 0) 594// return Error("NOT YET IMPLEMENTED: local symbol entries"); 595// if (DysymtabLC->ExternalSymbolsIndex != 0) 596// return Error("NOT YET IMPLEMENTED: non-external symbol entries"); 597// if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries) 598// return Error("NOT YET IMPLEMENTED: undefined symbol entries"); 599 } 600 601 // Load the segment load command. 602 if (SegmentLCI->Command.Type == macho::LCT_Segment) { 603 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC)) 604 return true; 605 } else { 606 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC)) 607 return true; 608 } 609 610 return false; 611} 612 613// Resolve the relocations for all symbols we currently know about. 614void RuntimeDyldImpl::resolveRelocations() { 615 // Just iterate over the symbols in our symbol table and assign their 616 // addresses. 617 StringMap<uint8_t*>::iterator i = SymbolTable.begin(); 618 StringMap<uint8_t*>::iterator e = SymbolTable.end(); 619 for (;i != e; ++i) 620 reassignSymbolAddress(i->getKey(), i->getValue()); 621} 622 623// Assign an address to a symbol name and resolve all the relocations 624// associated with it. 625void RuntimeDyldImpl::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { 626 // Assign the address in our symbol table. 627 SymbolTable[Name] = Addr; 628 629 RelocationList &Relocs = Relocations[Name]; 630 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { 631 RelocationEntry &RE = Relocs[i]; 632 uint8_t *Target = SymbolTable[RE.Target] + RE.Offset; 633 bool isPCRel = (RE.Data >> 24) & 1; 634 unsigned Type = (RE.Data >> 28) & 0xf; 635 unsigned Size = 1 << ((RE.Data >> 25) & 3); 636 637 DEBUG(dbgs() << "Resolving relocation at '" << RE.Target 638 << "' + " << RE.Offset << " (" << format("%p", Target) << ")" 639 << " from '" << Name << " (" << format("%p", Addr) << ")" 640 << "(" << (isPCRel ? "pcrel" : "absolute") 641 << ", type: " << Type << ", Size: " << Size << ").\n"); 642 643 resolveRelocation(Target, Addr, isPCRel, Type, Size); 644 RE.isResolved = true; 645 } 646} 647 648//===----------------------------------------------------------------------===// 649// RuntimeDyld class implementation 650RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *MM) { 651 Dyld = new RuntimeDyldImpl(MM); 652} 653 654RuntimeDyld::~RuntimeDyld() { 655 delete Dyld; 656} 657 658bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { 659 return Dyld->loadObject(InputBuffer); 660} 661 662void *RuntimeDyld::getSymbolAddress(StringRef Name) { 663 return Dyld->getSymbolAddress(Name); 664} 665 666void RuntimeDyld::resolveRelocations() { 667 Dyld->resolveRelocations(); 668} 669 670void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { 671 Dyld->reassignSymbolAddress(Name, Addr); 672} 673 674StringRef RuntimeDyld::getErrorString() { 675 return Dyld->getErrorString(); 676} 677 678} // end namespace llvm 679