RuntimeDyldMachO.cpp revision 5069b21b45d8ec99a1b34b6483d800ba5c2ea598
1//===-- RuntimeDyldMachO.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 "llvm/ADT/OwningPtr.h" 16#include "llvm/ADT/StringRef.h" 17#include "llvm/ADT/STLExtras.h" 18#include "RuntimeDyldMachO.h" 19using namespace llvm; 20using namespace llvm::object; 21 22namespace llvm { 23 24bool RuntimeDyldMachO:: 25resolveRelocation(uint8_t *LocalAddress, 26 uint64_t FinalAddress, 27 uint64_t Value, 28 bool isPCRel, 29 unsigned Type, 30 unsigned Size, 31 int64_t Addend) { 32 // This just dispatches to the proper target specific routine. 33 switch (CPUType) { 34 default: llvm_unreachable("Unsupported CPU type!"); 35 case mach::CTM_x86_64: 36 return resolveX86_64Relocation(LocalAddress, 37 FinalAddress, 38 (uintptr_t)Value, 39 isPCRel, 40 Type, 41 Size, 42 Addend); 43 case mach::CTM_ARM: 44 return resolveARMRelocation(LocalAddress, 45 FinalAddress, 46 (uintptr_t)Value, 47 isPCRel, 48 Type, 49 Size, 50 Addend); 51 } 52} 53 54bool RuntimeDyldMachO:: 55resolveX86_64Relocation(uint8_t *LocalAddress, 56 uint64_t FinalAddress, 57 uint64_t Value, 58 bool isPCRel, 59 unsigned Type, 60 unsigned Size, 61 int64_t Addend) { 62 // If the relocation is PC-relative, the value to be encoded is the 63 // pointer difference. 64 if (isPCRel) 65 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC 66 // address. Is that expected? Only for branches, perhaps? 67 Value -= FinalAddress + 4; 68 69 switch(Type) { 70 default: 71 llvm_unreachable("Invalid relocation type!"); 72 case macho::RIT_X86_64_Signed1: 73 case macho::RIT_X86_64_Signed2: 74 case macho::RIT_X86_64_Signed4: 75 case macho::RIT_X86_64_Signed: 76 case macho::RIT_X86_64_Unsigned: 77 case macho::RIT_X86_64_Branch: { 78 Value += Addend; 79 // Mask in the target value a byte at a time (we don't have an alignment 80 // guarantee for the target address, so this is safest). 81 uint8_t *p = (uint8_t*)LocalAddress; 82 for (unsigned i = 0; i < Size; ++i) { 83 *p++ = (uint8_t)Value; 84 Value >>= 8; 85 } 86 return false; 87 } 88 case macho::RIT_X86_64_GOTLoad: 89 case macho::RIT_X86_64_GOT: 90 case macho::RIT_X86_64_Subtractor: 91 case macho::RIT_X86_64_TLV: 92 return Error("Relocation type not implemented yet!"); 93 } 94} 95 96bool RuntimeDyldMachO:: 97resolveARMRelocation(uint8_t *LocalAddress, 98 uint64_t FinalAddress, 99 uint64_t Value, 100 bool isPCRel, 101 unsigned Type, 102 unsigned Size, 103 int64_t Addend) { 104 // If the relocation is PC-relative, the value to be encoded is the 105 // pointer difference. 106 if (isPCRel) { 107 Value -= FinalAddress; 108 // ARM PCRel relocations have an effective-PC offset of two instructions 109 // (four bytes in Thumb mode, 8 bytes in ARM mode). 110 // FIXME: For now, assume ARM mode. 111 Value -= 8; 112 } 113 114 switch(Type) { 115 default: 116 llvm_unreachable("Invalid relocation type!"); 117 case macho::RIT_Vanilla: { 118 // Mask in the target value a byte at a time (we don't have an alignment 119 // guarantee for the target address, so this is safest). 120 uint8_t *p = (uint8_t*)LocalAddress; 121 for (unsigned i = 0; i < Size; ++i) { 122 *p++ = (uint8_t)Value; 123 Value >>= 8; 124 } 125 break; 126 } 127 case macho::RIT_ARM_Branch24Bit: { 128 // Mask the value into the target address. We know instructions are 129 // 32-bit aligned, so we can do it all at once. 130 uint32_t *p = (uint32_t*)LocalAddress; 131 // The low two bits of the value are not encoded. 132 Value >>= 2; 133 // Mask the value to 24 bits. 134 Value &= 0xffffff; 135 // FIXME: If the destination is a Thumb function (and the instruction 136 // is a non-predicated BL instruction), we need to change it to a BLX 137 // instruction instead. 138 139 // Insert the value into the instruction. 140 *p = (*p & ~0xffffff) | Value; 141 break; 142 } 143 case macho::RIT_ARM_ThumbBranch22Bit: 144 case macho::RIT_ARM_ThumbBranch32Bit: 145 case macho::RIT_ARM_Half: 146 case macho::RIT_ARM_HalfDifference: 147 case macho::RIT_Pair: 148 case macho::RIT_Difference: 149 case macho::RIT_ARM_LocalDifference: 150 case macho::RIT_ARM_PreboundLazyPointer: 151 return Error("Relocation type not implemented yet!"); 152 } 153 return false; 154} 155 156bool RuntimeDyldMachO:: 157loadSegment32(const MachOObject *Obj, 158 const MachOObject::LoadCommandInfo *SegmentLCI, 159 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { 160 // FIXME: This should really be combined w/ loadSegment64. Templatized 161 // function on the 32/64 datatypes maybe? 162 InMemoryStruct<macho::SegmentLoadCommand> SegmentLC; 163 Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC); 164 if (!SegmentLC) 165 return Error("unable to load segment load command"); 166 167 168 SmallVector<unsigned, 16> SectionMap; 169 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { 170 InMemoryStruct<macho::Section> Sect; 171 Obj->ReadSection(*SegmentLCI, SectNum, Sect); 172 if (!Sect) 173 return Error("unable to load section: '" + Twine(SectNum) + "'"); 174 175 // Allocate memory via the MM for the section. 176 uint8_t *Buffer; 177 uint32_t SectionID = Sections.size(); 178 if (Sect->Flags == 0x80000400) 179 Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID); 180 else 181 Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID); 182 183 DEBUG(dbgs() << "Loading " 184 << ((Sect->Flags == 0x80000400) ? "text" : "data") 185 << " (ID #" << SectionID << ")" 186 << " '" << Sect->SegmentName << "," 187 << Sect->Name << "' of size " << Sect->Size 188 << " to address " << Buffer << ".\n"); 189 190 // Copy the payload from the object file into the allocated buffer. 191 uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset, 192 SegmentLC->FileSize).data(); 193 memcpy(Buffer, Base + Sect->Address, Sect->Size); 194 195 // Remember what got allocated for this SectionID. 196 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size)); 197 SectionLocalMemToID[Buffer] = SectionID; 198 199 // By default, the load address of a section is its memory buffer. 200 SectionLoadAddress.push_back((uint64_t)Buffer); 201 202 // Keep a map of object file section numbers to corresponding SectionIDs 203 // while processing the file. 204 SectionMap.push_back(SectionID); 205 } 206 207 // Process the symbol table. 208 SmallVector<StringRef, 64> SymbolNames; 209 processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC); 210 211 // Process the relocations for each section we're loading. 212 Relocations.grow(Relocations.size() + SegmentLC->NumSections); 213 Referrers.grow(Referrers.size() + SegmentLC->NumSections); 214 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { 215 InMemoryStruct<macho::Section> Sect; 216 Obj->ReadSection(*SegmentLCI, SectNum, Sect); 217 if (!Sect) 218 return Error("unable to load section: '" + Twine(SectNum) + "'"); 219 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { 220 InMemoryStruct<macho::RelocationEntry> RE; 221 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); 222 if (RE->Word0 & macho::RF_Scattered) 223 return Error("NOT YET IMPLEMENTED: scattered relocations."); 224 // Word0 of the relocation is the offset into the section where the 225 // relocation should be applied. We need to translate that into an 226 // offset into a function since that's our atom. 227 uint32_t Offset = RE->Word0; 228 bool isExtern = (RE->Word1 >> 27) & 1; 229 230 // FIXME: Get the relocation addend from the target address. 231 // FIXME: VERY imporant for internal relocations. 232 233 // Figure out the source symbol of the relocation. If isExtern is true, 234 // this relocation references the symbol table, otherwise it references 235 // a section in the same object, numbered from 1 through NumSections 236 // (SectionBases is [0, NumSections-1]). 237 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value 238 if (!isExtern) { 239 assert(SourceNum > 0 && "Invalid relocation section number!"); 240 unsigned SectionID = SectionMap[SourceNum - 1]; 241 unsigned TargetID = SectionMap[SectNum]; 242 DEBUG(dbgs() << "Internal relocation at Section #" 243 << TargetID << " + " << Offset 244 << " from Section #" 245 << SectionID << " (Word1: " 246 << format("0x%x", RE->Word1) << ")\n"); 247 248 // Store the relocation information. It will get resolved when 249 // the section addresses are assigned. 250 uint32_t RelocationIndex = Relocations[SectionID].size(); 251 Relocations[SectionID].push_back(RelocationEntry(TargetID, 252 Offset, 253 RE->Word1, 254 0 /*Addend*/)); 255 Referrers[TargetID].push_back(Referrer(SectionID, RelocationIndex)); 256 } else { 257 StringRef SourceName = SymbolNames[SourceNum]; 258 259 // Now store the relocation information. Associate it with the source 260 // symbol. Just add it to the unresolved list and let the general 261 // path post-load resolve it if we know where the symbol is. 262 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum, 263 Offset, 264 RE->Word1, 265 0 /*Addend*/)); 266 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset 267 << " from '" << SourceName << "(Word1: " 268 << format("0x%x", RE->Word1) << ")\n"); 269 } 270 } 271 } 272 273 // Resolve the addresses of any symbols that were defined in this segment. 274 for (int i = 0, e = SymbolNames.size(); i != e; ++i) 275 resolveSymbol(SymbolNames[i]); 276 277 return false; 278} 279 280 281bool RuntimeDyldMachO:: 282loadSegment64(const MachOObject *Obj, 283 const MachOObject::LoadCommandInfo *SegmentLCI, 284 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { 285 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC; 286 Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC); 287 if (!Segment64LC) 288 return Error("unable to load segment load command"); 289 290 291 SmallVector<unsigned, 16> SectionMap; 292 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { 293 InMemoryStruct<macho::Section64> Sect; 294 Obj->ReadSection64(*SegmentLCI, SectNum, Sect); 295 if (!Sect) 296 return Error("unable to load section: '" + Twine(SectNum) + "'"); 297 298 // Allocate memory via the MM for the section. 299 uint8_t *Buffer; 300 uint32_t SectionID = Sections.size(); 301 unsigned Align = 1 << Sect->Align; // .o file has log2 alignment. 302 if (Sect->Flags == 0x80000400) 303 Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID); 304 else 305 Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID); 306 307 DEBUG(dbgs() << "Loading " 308 << ((Sect->Flags == 0x80000400) ? "text" : "data") 309 << " (ID #" << SectionID << ")" 310 << " '" << Sect->SegmentName << "," 311 << Sect->Name << "' of size " << Sect->Size 312 << " (align " << Align << ")" 313 << " to address " << Buffer << ".\n"); 314 315 // Copy the payload from the object file into the allocated buffer. 316 uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset, 317 Segment64LC->FileSize).data(); 318 memcpy(Buffer, Base + Sect->Address, Sect->Size); 319 320 // Remember what got allocated for this SectionID. 321 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size)); 322 SectionLocalMemToID[Buffer] = SectionID; 323 324 // By default, the load address of a section is its memory buffer. 325 SectionLoadAddress.push_back((uint64_t)Buffer); 326 327 // Keep a map of object file section numbers to corresponding SectionIDs 328 // while processing the file. 329 SectionMap.push_back(SectionID); 330 } 331 332 // Process the symbol table. 333 SmallVector<StringRef, 64> SymbolNames; 334 processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC); 335 336 // Process the relocations for each section we're loading. 337 Relocations.grow(Relocations.size() + Segment64LC->NumSections); 338 Referrers.grow(Referrers.size() + Segment64LC->NumSections); 339 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { 340 InMemoryStruct<macho::Section64> Sect; 341 Obj->ReadSection64(*SegmentLCI, SectNum, Sect); 342 if (!Sect) 343 return Error("unable to load section: '" + Twine(SectNum) + "'"); 344 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { 345 InMemoryStruct<macho::RelocationEntry> RE; 346 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); 347 if (RE->Word0 & macho::RF_Scattered) 348 return Error("NOT YET IMPLEMENTED: scattered relocations."); 349 // Word0 of the relocation is the offset into the section where the 350 // relocation should be applied. We need to translate that into an 351 // offset into a function since that's our atom. 352 uint32_t Offset = RE->Word0; 353 bool isExtern = (RE->Word1 >> 27) & 1; 354 355 // FIXME: Get the relocation addend from the target address. 356 // FIXME: VERY imporant for internal relocations. 357 358 // Figure out the source symbol of the relocation. If isExtern is true, 359 // this relocation references the symbol table, otherwise it references 360 // a section in the same object, numbered from 1 through NumSections 361 // (SectionBases is [0, NumSections-1]). 362 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value 363 if (!isExtern) { 364 assert(SourceNum > 0 && "Invalid relocation section number!"); 365 unsigned SectionID = SectionMap[SourceNum - 1]; 366 unsigned TargetID = SectionMap[SectNum]; 367 DEBUG(dbgs() << "Internal relocation at Section #" 368 << TargetID << " + " << Offset 369 << " from Section #" 370 << SectionID << " (Word1: " 371 << format("0x%x", RE->Word1) << ")\n"); 372 373 // Store the relocation information. It will get resolved when 374 // the section addresses are assigned. 375 uint32_t RelocationIndex = Relocations[SectionID].size(); 376 Relocations[SectionID].push_back(RelocationEntry(TargetID, 377 Offset, 378 RE->Word1, 379 0 /*Addend*/)); 380 Referrers[TargetID].push_back(Referrer(SectionID, RelocationIndex)); 381 } else { 382 StringRef SourceName = SymbolNames[SourceNum]; 383 384 // Now store the relocation information. Associate it with the source 385 // symbol. Just add it to the unresolved list and let the general 386 // path post-load resolve it if we know where the symbol is. 387 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum, 388 Offset, 389 RE->Word1, 390 0 /*Addend*/)); 391 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset 392 << " from '" << SourceName << "(Word1: " 393 << format("0x%x", RE->Word1) << ")\n"); 394 } 395 } 396 } 397 398 // Resolve the addresses of any symbols that were defined in this segment. 399 for (int i = 0, e = SymbolNames.size(); i != e; ++i) 400 resolveSymbol(SymbolNames[i]); 401 402 return false; 403} 404 405bool RuntimeDyldMachO:: 406processSymbols32(const MachOObject *Obj, 407 SmallVectorImpl<unsigned> &SectionMap, 408 SmallVectorImpl<StringRef> &SymbolNames, 409 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { 410 // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such. 411 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { 412 InMemoryStruct<macho::SymbolTableEntry> STE; 413 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE); 414 if (!STE) 415 return Error("unable to read symbol: '" + Twine(i) + "'"); 416 // Get the symbol name. 417 StringRef Name = Obj->getStringAtIndex(STE->StringIndex); 418 SymbolNames.push_back(Name); 419 420 // FIXME: Check the symbol type and flags. 421 if (STE->Type != 0xF) // external, defined in this segment. 422 continue; 423 // Flags in the upper nibble we don't care about. 424 if ((STE->Flags & 0xf) != 0x0) 425 continue; 426 427 // Remember the symbol. 428 uint32_t SectionID = SectionMap[STE->SectionIndex - 1]; 429 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value); 430 431 DEBUG(dbgs() << "Symbol: '" << Name << "' @ " 432 << (getSectionAddress(SectionID) + STE->Value) 433 << "\n"); 434 } 435 return false; 436} 437 438bool RuntimeDyldMachO:: 439processSymbols64(const MachOObject *Obj, 440 SmallVectorImpl<unsigned> &SectionMap, 441 SmallVectorImpl<StringRef> &SymbolNames, 442 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { 443 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { 444 InMemoryStruct<macho::Symbol64TableEntry> STE; 445 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE); 446 if (!STE) 447 return Error("unable to read symbol: '" + Twine(i) + "'"); 448 // Get the symbol name. 449 StringRef Name = Obj->getStringAtIndex(STE->StringIndex); 450 SymbolNames.push_back(Name); 451 452 // FIXME: Check the symbol type and flags. 453 if (STE->Type != 0xF) // external, defined in this segment. 454 continue; 455 // Flags in the upper nibble we don't care about. 456 if ((STE->Flags & 0xf) != 0x0) 457 continue; 458 459 // Remember the symbol. 460 uint32_t SectionID = SectionMap[STE->SectionIndex - 1]; 461 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value); 462 463 DEBUG(dbgs() << "Symbol: '" << Name << "' @ " 464 << (getSectionAddress(SectionID) + STE->Value) 465 << "\n"); 466 } 467 return false; 468} 469 470// resolveSymbol - Resolve any relocations to the specified symbol if 471// we know where it lives. 472void RuntimeDyldMachO::resolveSymbol(StringRef Name) { 473 StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name); 474 if (Loc == SymbolTable.end()) 475 return; 476 477 RelocationList &Relocs = UnresolvedRelocations[Name]; 478 DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n"); 479 for (int i = 0, e = Relocs.size(); i != e; ++i) { 480 // Change the relocation to be section relative rather than symbol 481 // relative and move it to the resolved relocation list. 482 RelocationEntry Entry = Relocs[i]; 483 Entry.Addend += Loc->second.second; 484 uint32_t RelocationIndex = Relocations[Loc->second.first].size(); 485 Relocations[Loc->second.first].push_back(Entry); 486 Referrers[Entry.SectionID].push_back(Referrer(Loc->second.first, RelocationIndex)); 487 } 488 // FIXME: Keep a worklist of the relocations we've added so that we can 489 // resolve more selectively later. 490 Relocs.clear(); 491} 492 493bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) { 494 // If the linker is in an error state, don't do anything. 495 if (hasError()) 496 return true; 497 // Load the Mach-O wrapper object. 498 std::string ErrorStr; 499 OwningPtr<MachOObject> Obj( 500 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr)); 501 if (!Obj) 502 return Error("unable to load object: '" + ErrorStr + "'"); 503 504 // Get the CPU type information from the header. 505 const macho::Header &Header = Obj->getHeader(); 506 507 // FIXME: Error checking that the loaded object is compatible with 508 // the system we're running on. 509 CPUType = Header.CPUType; 510 CPUSubtype = Header.CPUSubtype; 511 512 // Validate that the load commands match what we expect. 513 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0, 514 *DysymtabLCI = 0; 515 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { 516 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i); 517 switch (LCI.Command.Type) { 518 case macho::LCT_Segment: 519 case macho::LCT_Segment64: 520 if (SegmentLCI) 521 return Error("unexpected input object (multiple segments)"); 522 SegmentLCI = &LCI; 523 break; 524 case macho::LCT_Symtab: 525 if (SymtabLCI) 526 return Error("unexpected input object (multiple symbol tables)"); 527 SymtabLCI = &LCI; 528 break; 529 case macho::LCT_Dysymtab: 530 if (DysymtabLCI) 531 return Error("unexpected input object (multiple symbol tables)"); 532 DysymtabLCI = &LCI; 533 break; 534 default: 535 return Error("unexpected input object (unexpected load command"); 536 } 537 } 538 539 if (!SymtabLCI) 540 return Error("no symbol table found in object"); 541 if (!SegmentLCI) 542 return Error("no segments found in object"); 543 544 // Read and register the symbol table data. 545 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC; 546 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC); 547 if (!SymtabLC) 548 return Error("unable to load symbol table load command"); 549 Obj->RegisterStringTable(*SymtabLC); 550 551 // Read the dynamic link-edit information, if present (not present in static 552 // objects). 553 if (DysymtabLCI) { 554 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC; 555 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC); 556 if (!DysymtabLC) 557 return Error("unable to load dynamic link-exit load command"); 558 559 // FIXME: We don't support anything interesting yet. 560// if (DysymtabLC->LocalSymbolsIndex != 0) 561// return Error("NOT YET IMPLEMENTED: local symbol entries"); 562// if (DysymtabLC->ExternalSymbolsIndex != 0) 563// return Error("NOT YET IMPLEMENTED: non-external symbol entries"); 564// if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries) 565// return Error("NOT YET IMPLEMENTED: undefined symbol entries"); 566 } 567 568 // Load the segment load command. 569 if (SegmentLCI->Command.Type == macho::LCT_Segment) { 570 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC)) 571 return true; 572 } else { 573 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC)) 574 return true; 575 } 576 577 // Assign the addresses of the sections from the object so that any 578 // relocations to them get set properly. 579 // FIXME: This is done directly from the client at the moment. We should 580 // default the values to the local storage, at least when the target arch 581 // is the same as the host arch. 582 583 return false; 584} 585 586// Assign an address to a symbol name and resolve all the relocations 587// associated with it. 588void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID, 589 uint64_t Addr) { 590 // The address to use for relocation resolution is not 591 // the address of the local section buffer. We must be doing 592 // a remote execution environment of some sort. Re-apply any 593 // relocations referencing this section with the given address. 594 // 595 // Addr is a uint64_t because we can't assume the pointer width 596 // of the target is the same as that of the host. Just use a generic 597 // "big enough" type. 598 599 SectionLoadAddress[SectionID] = Addr; 600 601 RelocationList &Relocs = Relocations[SectionID]; 602 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { 603 RelocationEntry &RE = Relocs[i]; 604 uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset; 605 uint64_t FinalTarget = (uint64_t)SectionLoadAddress[RE.SectionID] + RE.Offset; 606 bool isPCRel = (RE.Data >> 24) & 1; 607 unsigned Type = (RE.Data >> 28) & 0xf; 608 unsigned Size = 1 << ((RE.Data >> 25) & 3); 609 610 DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID 611 << " + " << RE.Offset << " (" << format("%p", Target) << ")" 612 << " from Section #" << SectionID << " (" << format("%p", Addr) << ")" 613 << "(" << (isPCRel ? "pcrel" : "absolute") 614 << ", type: " << Type << ", Size: " << Size << ", Addend: " 615 << RE.Addend << ").\n"); 616 617 resolveRelocation(Target, 618 FinalTarget, 619 Addr, 620 isPCRel, 621 Type, 622 Size, 623 RE.Addend); 624 } 625 ReferrerList &Refers = Referrers[SectionID]; 626 for (unsigned i = 0, e = Refers.size(); i != e; ++i) { 627 Referrer &R = Refers[i]; 628 RelocationEntry &RE = Relocations[R.SectionID][R.Index]; 629 uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset; 630 uint64_t FinalTarget = (uint64_t)SectionLoadAddress[RE.SectionID] + RE.Offset; 631 bool isPCRel = (RE.Data >> 24) & 1; 632 unsigned Type = (RE.Data >> 28) & 0xf; 633 unsigned Size = 1 << ((RE.Data >> 25) & 3); 634 635 DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID 636 << " + " << RE.Offset << " (" << format("%p", Target) << ")" 637 << " from Section #" << SectionID << " (" << format("%p", Addr) << ")" 638 << "(" << (isPCRel ? "pcrel" : "absolute") 639 << ", type: " << Type << ", Size: " << Size << ", Addend: " 640 << RE.Addend << ").\n"); 641 642 resolveRelocation(Target, 643 FinalTarget, 644 Addr, 645 isPCRel, 646 Type, 647 Size, 648 RE.Addend); 649 } 650} 651 652bool RuntimeDyldMachO::isKnownFormat(const MemoryBuffer *InputBuffer) { 653 StringRef Magic = InputBuffer->getBuffer().slice(0, 4); 654 if (Magic == "\xFE\xED\xFA\xCE") return true; 655 if (Magic == "\xCE\xFA\xED\xFE") return true; 656 if (Magic == "\xFE\xED\xFA\xCF") return true; 657 if (Magic == "\xCF\xFA\xED\xFE") return true; 658 return false; 659} 660 661} // end namespace llvm 662