1 //=--------- MachOLinkGraphBuilder.cpp - MachO LinkGraph builder ----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Generic MachO LinkGraph buliding code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "MachOLinkGraphBuilder.h" 14 15 #define DEBUG_TYPE "jitlink" 16 17 static const char *CommonSectionName = "__common"; 18 19 namespace llvm { 20 namespace jitlink { 21 22 MachOLinkGraphBuilder::~MachOLinkGraphBuilder() {} 23 24 Expected<std::unique_ptr<LinkGraph>> MachOLinkGraphBuilder::buildGraph() { 25 26 // We only operate on relocatable objects. 27 if (!Obj.isRelocatableObject()) 28 return make_error<JITLinkError>("Object is not a relocatable MachO"); 29 30 if (auto Err = createNormalizedSections()) 31 return std::move(Err); 32 33 if (auto Err = createNormalizedSymbols()) 34 return std::move(Err); 35 36 if (auto Err = graphifyRegularSymbols()) 37 return std::move(Err); 38 39 if (auto Err = graphifySectionsWithCustomParsers()) 40 return std::move(Err); 41 42 if (auto Err = addRelocations()) 43 return std::move(Err); 44 45 return std::move(G); 46 } 47 48 MachOLinkGraphBuilder::MachOLinkGraphBuilder( 49 const object::MachOObjectFile &Obj, Triple TT, 50 LinkGraph::GetEdgeKindNameFunction GetEdgeKindName) 51 : Obj(Obj), 52 G(std::make_unique<LinkGraph>( 53 std::string(Obj.getFileName()), std::move(TT), getPointerSize(Obj), 54 getEndianness(Obj), std::move(GetEdgeKindName))) {} 55 56 void MachOLinkGraphBuilder::addCustomSectionParser( 57 StringRef SectionName, SectionParserFunction Parser) { 58 assert(!CustomSectionParserFunctions.count(SectionName) && 59 "Custom parser for this section already exists"); 60 CustomSectionParserFunctions[SectionName] = std::move(Parser); 61 } 62 63 Linkage MachOLinkGraphBuilder::getLinkage(uint16_t Desc) { 64 if ((Desc & MachO::N_WEAK_DEF) || (Desc & MachO::N_WEAK_REF)) 65 return Linkage::Weak; 66 return Linkage::Strong; 67 } 68 69 Scope MachOLinkGraphBuilder::getScope(StringRef Name, uint8_t Type) { 70 if (Type & MachO::N_EXT) { 71 if ((Type & MachO::N_PEXT) || Name.startswith("l")) 72 return Scope::Hidden; 73 else 74 return Scope::Default; 75 } 76 return Scope::Local; 77 } 78 79 bool MachOLinkGraphBuilder::isAltEntry(const NormalizedSymbol &NSym) { 80 return NSym.Desc & MachO::N_ALT_ENTRY; 81 } 82 83 bool MachOLinkGraphBuilder::isDebugSection(const NormalizedSection &NSec) { 84 return (NSec.Flags & MachO::S_ATTR_DEBUG && 85 strcmp(NSec.SegName, "__DWARF") == 0); 86 } 87 88 bool MachOLinkGraphBuilder::isZeroFillSection(const NormalizedSection &NSec) { 89 switch (NSec.Flags & MachO::SECTION_TYPE) { 90 case MachO::S_ZEROFILL: 91 case MachO::S_GB_ZEROFILL: 92 case MachO::S_THREAD_LOCAL_ZEROFILL: 93 return true; 94 default: 95 return false; 96 } 97 } 98 99 unsigned 100 MachOLinkGraphBuilder::getPointerSize(const object::MachOObjectFile &Obj) { 101 return Obj.is64Bit() ? 8 : 4; 102 } 103 104 support::endianness 105 MachOLinkGraphBuilder::getEndianness(const object::MachOObjectFile &Obj) { 106 return Obj.isLittleEndian() ? support::little : support::big; 107 } 108 109 Section &MachOLinkGraphBuilder::getCommonSection() { 110 if (!CommonSection) 111 CommonSection = 112 &G->createSection(CommonSectionName, MemProt::Read | MemProt::Write); 113 return *CommonSection; 114 } 115 116 Error MachOLinkGraphBuilder::createNormalizedSections() { 117 // Build normalized sections. Verifies that section data is in-range (for 118 // sections with content) and that address ranges are non-overlapping. 119 120 LLVM_DEBUG(dbgs() << "Creating normalized sections...\n"); 121 122 for (auto &SecRef : Obj.sections()) { 123 NormalizedSection NSec; 124 uint32_t DataOffset = 0; 125 126 auto SecIndex = Obj.getSectionIndex(SecRef.getRawDataRefImpl()); 127 128 if (Obj.is64Bit()) { 129 const MachO::section_64 &Sec64 = 130 Obj.getSection64(SecRef.getRawDataRefImpl()); 131 132 memcpy(&NSec.SectName, &Sec64.sectname, 16); 133 NSec.SectName[16] = '\0'; 134 memcpy(&NSec.SegName, Sec64.segname, 16); 135 NSec.SegName[16] = '\0'; 136 137 NSec.Address = Sec64.addr; 138 NSec.Size = Sec64.size; 139 NSec.Alignment = 1ULL << Sec64.align; 140 NSec.Flags = Sec64.flags; 141 DataOffset = Sec64.offset; 142 } else { 143 const MachO::section &Sec32 = Obj.getSection(SecRef.getRawDataRefImpl()); 144 145 memcpy(&NSec.SectName, &Sec32.sectname, 16); 146 NSec.SectName[16] = '\0'; 147 memcpy(&NSec.SegName, Sec32.segname, 16); 148 NSec.SegName[16] = '\0'; 149 150 NSec.Address = Sec32.addr; 151 NSec.Size = Sec32.size; 152 NSec.Alignment = 1ULL << Sec32.align; 153 NSec.Flags = Sec32.flags; 154 DataOffset = Sec32.offset; 155 } 156 157 LLVM_DEBUG({ 158 dbgs() << " " << NSec.SegName << "," << NSec.SectName << ": " 159 << formatv("{0:x16}", NSec.Address) << " -- " 160 << formatv("{0:x16}", NSec.Address + NSec.Size) 161 << ", align: " << NSec.Alignment << ", index: " << SecIndex 162 << "\n"; 163 }); 164 165 // Get the section data if any. 166 if (!isZeroFillSection(NSec)) { 167 if (DataOffset + NSec.Size > Obj.getData().size()) 168 return make_error<JITLinkError>( 169 "Section data extends past end of file"); 170 171 NSec.Data = Obj.getData().data() + DataOffset; 172 } 173 174 // Get prot flags. 175 // FIXME: Make sure this test is correct (it's probably missing cases 176 // as-is). 177 MemProt Prot; 178 if (NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) 179 Prot = MemProt::Read | MemProt::Exec; 180 else 181 Prot = MemProt::Read | MemProt::Write; 182 183 auto FullyQualifiedName = 184 G->allocateString(StringRef(NSec.SegName) + "," + NSec.SectName); 185 NSec.GraphSection = &G->createSection( 186 StringRef(FullyQualifiedName.data(), FullyQualifiedName.size()), Prot); 187 188 IndexToSection.insert(std::make_pair(SecIndex, std::move(NSec))); 189 } 190 191 std::vector<NormalizedSection *> Sections; 192 Sections.reserve(IndexToSection.size()); 193 for (auto &KV : IndexToSection) 194 Sections.push_back(&KV.second); 195 196 // If we didn't end up creating any sections then bail out. The code below 197 // assumes that we have at least one section. 198 if (Sections.empty()) 199 return Error::success(); 200 201 llvm::sort(Sections, 202 [](const NormalizedSection *LHS, const NormalizedSection *RHS) { 203 assert(LHS && RHS && "Null section?"); 204 if (LHS->Address != RHS->Address) 205 return LHS->Address < RHS->Address; 206 return LHS->Size < RHS->Size; 207 }); 208 209 for (unsigned I = 0, E = Sections.size() - 1; I != E; ++I) { 210 auto &Cur = *Sections[I]; 211 auto &Next = *Sections[I + 1]; 212 if (Next.Address < Cur.Address + Cur.Size) 213 return make_error<JITLinkError>( 214 "Address range for section " + 215 formatv("\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Cur.SegName, 216 Cur.SectName, Cur.Address, Cur.Address + Cur.Size) + 217 "overlaps section \"" + Next.SegName + "/" + Next.SectName + "\"" + 218 formatv("\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Next.SegName, 219 Next.SectName, Next.Address, Next.Address + Next.Size)); 220 } 221 222 return Error::success(); 223 } 224 225 Error MachOLinkGraphBuilder::createNormalizedSymbols() { 226 LLVM_DEBUG(dbgs() << "Creating normalized symbols...\n"); 227 228 for (auto &SymRef : Obj.symbols()) { 229 230 unsigned SymbolIndex = Obj.getSymbolIndex(SymRef.getRawDataRefImpl()); 231 uint64_t Value; 232 uint32_t NStrX; 233 uint8_t Type; 234 uint8_t Sect; 235 uint16_t Desc; 236 237 if (Obj.is64Bit()) { 238 const MachO::nlist_64 &NL64 = 239 Obj.getSymbol64TableEntry(SymRef.getRawDataRefImpl()); 240 Value = NL64.n_value; 241 NStrX = NL64.n_strx; 242 Type = NL64.n_type; 243 Sect = NL64.n_sect; 244 Desc = NL64.n_desc; 245 } else { 246 const MachO::nlist &NL32 = 247 Obj.getSymbolTableEntry(SymRef.getRawDataRefImpl()); 248 Value = NL32.n_value; 249 NStrX = NL32.n_strx; 250 Type = NL32.n_type; 251 Sect = NL32.n_sect; 252 Desc = NL32.n_desc; 253 } 254 255 // Skip stabs. 256 // FIXME: Are there other symbols we should be skipping? 257 if (Type & MachO::N_STAB) 258 continue; 259 260 Optional<StringRef> Name; 261 if (NStrX) { 262 if (auto NameOrErr = SymRef.getName()) 263 Name = *NameOrErr; 264 else 265 return NameOrErr.takeError(); 266 } 267 268 LLVM_DEBUG({ 269 dbgs() << " "; 270 if (!Name) 271 dbgs() << "<anonymous symbol>"; 272 else 273 dbgs() << *Name; 274 dbgs() << ": value = " << formatv("{0:x16}", Value) 275 << ", type = " << formatv("{0:x2}", Type) 276 << ", desc = " << formatv("{0:x4}", Desc) << ", sect = "; 277 if (Sect) 278 dbgs() << static_cast<unsigned>(Sect - 1); 279 else 280 dbgs() << "none"; 281 dbgs() << "\n"; 282 }); 283 284 // If this symbol has a section, verify that the addresses line up. 285 if (Sect != 0) { 286 auto NSec = findSectionByIndex(Sect - 1); 287 if (!NSec) 288 return NSec.takeError(); 289 290 if (Value < NSec->Address || Value > NSec->Address + NSec->Size) 291 return make_error<JITLinkError>("Address " + formatv("{0:x}", Value) + 292 " for symbol " + *Name + 293 " does not fall within section"); 294 295 if (!NSec->GraphSection) { 296 LLVM_DEBUG({ 297 dbgs() << " Skipping: Symbol is in section " << NSec->SegName << "/" 298 << NSec->SectName 299 << " which has no associated graph section.\n"; 300 }); 301 continue; 302 } 303 } 304 305 IndexToSymbol[SymbolIndex] = 306 &createNormalizedSymbol(*Name, Value, Type, Sect, Desc, 307 getLinkage(Desc), getScope(*Name, Type)); 308 } 309 310 return Error::success(); 311 } 312 313 void MachOLinkGraphBuilder::addSectionStartSymAndBlock( 314 unsigned SecIndex, Section &GraphSec, uint64_t Address, const char *Data, 315 uint64_t Size, uint32_t Alignment, bool IsLive) { 316 Block &B = 317 Data ? G->createContentBlock(GraphSec, ArrayRef<char>(Data, Size), 318 Address, Alignment, 0) 319 : G->createZeroFillBlock(GraphSec, Size, Address, Alignment, 0); 320 auto &Sym = G->addAnonymousSymbol(B, 0, Size, false, IsLive); 321 auto SecI = IndexToSection.find(SecIndex); 322 assert(SecI != IndexToSection.end() && "SecIndex invalid"); 323 auto &NSec = SecI->second; 324 assert(!NSec.CanonicalSymbols.count(Sym.getAddress()) && 325 "Anonymous block start symbol clashes with existing symbol address"); 326 NSec.CanonicalSymbols[Sym.getAddress()] = &Sym; 327 } 328 329 Error MachOLinkGraphBuilder::graphifyRegularSymbols() { 330 331 LLVM_DEBUG(dbgs() << "Creating graph symbols...\n"); 332 333 /// We only have 256 section indexes: Use a vector rather than a map. 334 std::vector<std::vector<NormalizedSymbol *>> SecIndexToSymbols; 335 SecIndexToSymbols.resize(256); 336 337 // Create commons, externs, and absolutes, and partition all other symbols by 338 // section. 339 for (auto &KV : IndexToSymbol) { 340 auto &NSym = *KV.second; 341 342 switch (NSym.Type & MachO::N_TYPE) { 343 case MachO::N_UNDF: 344 if (NSym.Value) { 345 if (!NSym.Name) 346 return make_error<JITLinkError>("Anonymous common symbol at index " + 347 Twine(KV.first)); 348 NSym.GraphSymbol = &G->addCommonSymbol( 349 *NSym.Name, NSym.S, getCommonSection(), 0, NSym.Value, 350 1ull << MachO::GET_COMM_ALIGN(NSym.Desc), 351 NSym.Desc & MachO::N_NO_DEAD_STRIP); 352 } else { 353 if (!NSym.Name) 354 return make_error<JITLinkError>("Anonymous external symbol at " 355 "index " + 356 Twine(KV.first)); 357 NSym.GraphSymbol = &G->addExternalSymbol( 358 *NSym.Name, 0, 359 NSym.Desc & MachO::N_WEAK_REF ? Linkage::Weak : Linkage::Strong); 360 } 361 break; 362 case MachO::N_ABS: 363 if (!NSym.Name) 364 return make_error<JITLinkError>("Anonymous absolute symbol at index " + 365 Twine(KV.first)); 366 NSym.GraphSymbol = &G->addAbsoluteSymbol( 367 *NSym.Name, NSym.Value, 0, Linkage::Strong, Scope::Default, 368 NSym.Desc & MachO::N_NO_DEAD_STRIP); 369 break; 370 case MachO::N_SECT: 371 SecIndexToSymbols[NSym.Sect - 1].push_back(&NSym); 372 break; 373 case MachO::N_PBUD: 374 return make_error<JITLinkError>( 375 "Unupported N_PBUD symbol " + 376 (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) + 377 " at index " + Twine(KV.first)); 378 case MachO::N_INDR: 379 return make_error<JITLinkError>( 380 "Unupported N_INDR symbol " + 381 (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) + 382 " at index " + Twine(KV.first)); 383 default: 384 return make_error<JITLinkError>( 385 "Unrecognized symbol type " + Twine(NSym.Type & MachO::N_TYPE) + 386 " for symbol " + 387 (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) + 388 " at index " + Twine(KV.first)); 389 } 390 } 391 392 // Loop over sections performing regular graphification for those that 393 // don't have custom parsers. 394 for (auto &KV : IndexToSection) { 395 auto SecIndex = KV.first; 396 auto &NSec = KV.second; 397 398 if (!NSec.GraphSection) { 399 LLVM_DEBUG({ 400 dbgs() << " " << NSec.SegName << "/" << NSec.SectName 401 << " has no graph section. Skipping.\n"; 402 }); 403 continue; 404 } 405 406 // Skip sections with custom parsers. 407 if (CustomSectionParserFunctions.count(NSec.GraphSection->getName())) { 408 LLVM_DEBUG({ 409 dbgs() << " Skipping section " << NSec.GraphSection->getName() 410 << " as it has a custom parser.\n"; 411 }); 412 continue; 413 } else if ((NSec.Flags & MachO::SECTION_TYPE) == 414 MachO::S_CSTRING_LITERALS) { 415 if (auto Err = graphifyCStringSection( 416 NSec, std::move(SecIndexToSymbols[SecIndex]))) 417 return Err; 418 continue; 419 } else 420 LLVM_DEBUG({ 421 dbgs() << " Graphifying regular section " 422 << NSec.GraphSection->getName() << "...\n"; 423 }); 424 425 bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP; 426 bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; 427 428 auto &SecNSymStack = SecIndexToSymbols[SecIndex]; 429 430 // If this section is non-empty but there are no symbols covering it then 431 // create one block and anonymous symbol to cover the entire section. 432 if (SecNSymStack.empty()) { 433 if (NSec.Size > 0) { 434 LLVM_DEBUG({ 435 dbgs() << " Section non-empty, but contains no symbols. " 436 "Creating anonymous block to cover " 437 << formatv("{0:x16}", NSec.Address) << " -- " 438 << formatv("{0:x16}", NSec.Address + NSec.Size) << "\n"; 439 }); 440 addSectionStartSymAndBlock(SecIndex, *NSec.GraphSection, NSec.Address, 441 NSec.Data, NSec.Size, NSec.Alignment, 442 SectionIsNoDeadStrip); 443 } else 444 LLVM_DEBUG({ 445 dbgs() << " Section empty and contains no symbols. Skipping.\n"; 446 }); 447 continue; 448 } 449 450 // Sort the symbol stack in by address, alt-entry status, scope, and name. 451 // We sort in reverse order so that symbols will be visited in the right 452 // order when we pop off the stack below. 453 llvm::sort(SecNSymStack, [](const NormalizedSymbol *LHS, 454 const NormalizedSymbol *RHS) { 455 if (LHS->Value != RHS->Value) 456 return LHS->Value > RHS->Value; 457 if (isAltEntry(*LHS) != isAltEntry(*RHS)) 458 return isAltEntry(*RHS); 459 if (LHS->S != RHS->S) 460 return static_cast<uint8_t>(LHS->S) < static_cast<uint8_t>(RHS->S); 461 return LHS->Name < RHS->Name; 462 }); 463 464 // The first symbol in a section can not be an alt-entry symbol. 465 if (!SecNSymStack.empty() && isAltEntry(*SecNSymStack.back())) 466 return make_error<JITLinkError>( 467 "First symbol in " + NSec.GraphSection->getName() + " is alt-entry"); 468 469 // If the section is non-empty but there is no symbol covering the start 470 // address then add an anonymous one. 471 if (SecNSymStack.back()->Value != NSec.Address) { 472 auto AnonBlockSize = SecNSymStack.back()->Value - NSec.Address; 473 LLVM_DEBUG({ 474 dbgs() << " Section start not covered by symbol. " 475 << "Creating anonymous block to cover [ " 476 << formatv("{0:x16}", NSec.Address) << " -- " 477 << formatv("{0:x16}", NSec.Address + AnonBlockSize) << " ]\n"; 478 }); 479 addSectionStartSymAndBlock(SecIndex, *NSec.GraphSection, NSec.Address, 480 NSec.Data, AnonBlockSize, NSec.Alignment, 481 SectionIsNoDeadStrip); 482 } 483 484 // Visit section symbols in order by popping off the reverse-sorted stack, 485 // building blocks for each alt-entry chain and creating symbols as we go. 486 while (!SecNSymStack.empty()) { 487 SmallVector<NormalizedSymbol *, 8> BlockSyms; 488 489 BlockSyms.push_back(SecNSymStack.back()); 490 SecNSymStack.pop_back(); 491 while (!SecNSymStack.empty() && 492 (isAltEntry(*SecNSymStack.back()) || 493 SecNSymStack.back()->Value == BlockSyms.back()->Value)) { 494 BlockSyms.push_back(SecNSymStack.back()); 495 SecNSymStack.pop_back(); 496 } 497 498 // BlockNSyms now contains the block symbols in reverse canonical order. 499 JITTargetAddress BlockStart = BlockSyms.front()->Value; 500 JITTargetAddress BlockEnd = SecNSymStack.empty() 501 ? NSec.Address + NSec.Size 502 : SecNSymStack.back()->Value; 503 JITTargetAddress BlockOffset = BlockStart - NSec.Address; 504 JITTargetAddress BlockSize = BlockEnd - BlockStart; 505 506 LLVM_DEBUG({ 507 dbgs() << " Creating block for " << formatv("{0:x16}", BlockStart) 508 << " -- " << formatv("{0:x16}", BlockEnd) << ": " 509 << NSec.GraphSection->getName() << " + " 510 << formatv("{0:x16}", BlockOffset) << " with " 511 << BlockSyms.size() << " symbol(s)...\n"; 512 }); 513 514 Block &B = 515 NSec.Data 516 ? G->createContentBlock( 517 *NSec.GraphSection, 518 ArrayRef<char>(NSec.Data + BlockOffset, BlockSize), 519 BlockStart, NSec.Alignment, BlockStart % NSec.Alignment) 520 : G->createZeroFillBlock(*NSec.GraphSection, BlockSize, 521 BlockStart, NSec.Alignment, 522 BlockStart % NSec.Alignment); 523 524 Optional<JITTargetAddress> LastCanonicalAddr; 525 JITTargetAddress SymEnd = BlockEnd; 526 while (!BlockSyms.empty()) { 527 auto &NSym = *BlockSyms.back(); 528 BlockSyms.pop_back(); 529 530 bool SymLive = 531 (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip; 532 533 auto &Sym = createStandardGraphSymbol(NSym, B, SymEnd - NSym.Value, 534 SectionIsText, SymLive, 535 LastCanonicalAddr != NSym.Value); 536 537 if (LastCanonicalAddr != Sym.getAddress()) { 538 if (LastCanonicalAddr) 539 SymEnd = *LastCanonicalAddr; 540 LastCanonicalAddr = Sym.getAddress(); 541 } 542 } 543 } 544 } 545 546 return Error::success(); 547 } 548 549 Symbol &MachOLinkGraphBuilder::createStandardGraphSymbol(NormalizedSymbol &NSym, 550 Block &B, size_t Size, 551 bool IsText, 552 bool IsNoDeadStrip, 553 bool IsCanonical) { 554 555 LLVM_DEBUG({ 556 dbgs() << " " << formatv("{0:x16}", NSym.Value) << " -- " 557 << formatv("{0:x16}", NSym.Value + Size) << ": "; 558 if (!NSym.Name) 559 dbgs() << "<anonymous symbol>"; 560 else 561 dbgs() << NSym.Name; 562 if (IsText) 563 dbgs() << " [text]"; 564 if (IsNoDeadStrip) 565 dbgs() << " [no-dead-strip]"; 566 if (!IsCanonical) 567 dbgs() << " [non-canonical]"; 568 dbgs() << "\n"; 569 }); 570 571 auto &Sym = NSym.Name ? G->addDefinedSymbol(B, NSym.Value - B.getAddress(), 572 *NSym.Name, Size, NSym.L, NSym.S, 573 IsText, IsNoDeadStrip) 574 : G->addAnonymousSymbol(B, NSym.Value - B.getAddress(), 575 Size, IsText, IsNoDeadStrip); 576 NSym.GraphSymbol = &Sym; 577 578 if (IsCanonical) 579 setCanonicalSymbol(getSectionByIndex(NSym.Sect - 1), Sym); 580 581 return Sym; 582 } 583 584 Error MachOLinkGraphBuilder::graphifySectionsWithCustomParsers() { 585 // Graphify special sections. 586 for (auto &KV : IndexToSection) { 587 auto &NSec = KV.second; 588 589 // Skip non-graph sections. 590 if (!NSec.GraphSection) 591 continue; 592 593 auto HI = CustomSectionParserFunctions.find(NSec.GraphSection->getName()); 594 if (HI != CustomSectionParserFunctions.end()) { 595 auto &Parse = HI->second; 596 if (auto Err = Parse(NSec)) 597 return Err; 598 } 599 } 600 601 return Error::success(); 602 } 603 604 Error MachOLinkGraphBuilder::graphifyCStringSection( 605 NormalizedSection &NSec, std::vector<NormalizedSymbol *> NSyms) { 606 assert(NSec.GraphSection && "C string literal section missing graph section"); 607 assert(NSec.Data && "C string literal section has no data"); 608 609 LLVM_DEBUG({ 610 dbgs() << " Graphifying C-string literal section " 611 << NSec.GraphSection->getName() << "\n"; 612 }); 613 614 if (NSec.Data[NSec.Size - 1] != '\0') 615 return make_error<JITLinkError>("C string literal section " + 616 NSec.GraphSection->getName() + 617 " does not end with null terminator"); 618 619 /// Sort into reverse order to use as a stack. 620 llvm::sort(NSyms, 621 [](const NormalizedSymbol *LHS, const NormalizedSymbol *RHS) { 622 if (LHS->Value != RHS->Value) 623 return LHS->Value > RHS->Value; 624 if (LHS->L != RHS->L) 625 return LHS->L > RHS->L; 626 if (LHS->S != RHS->S) 627 return LHS->S > RHS->S; 628 if (RHS->Name) { 629 if (!LHS->Name) 630 return true; 631 return *LHS->Name > *RHS->Name; 632 } 633 return false; 634 }); 635 636 bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP; 637 bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; 638 JITTargetAddress BlockStart = 0; 639 640 // Scan section for null characters. 641 for (size_t I = 0; I != NSec.Size; ++I) 642 if (NSec.Data[I] == '\0') { 643 JITTargetAddress BlockEnd = I + 1; 644 size_t BlockSize = BlockEnd - BlockStart; 645 // Create a block for this null terminated string. 646 auto &B = G->createContentBlock(*NSec.GraphSection, 647 {NSec.Data + BlockStart, BlockSize}, 648 NSec.Address + BlockStart, 1, 0); 649 650 LLVM_DEBUG({ 651 dbgs() << " Created block " << formatv("{0:x}", B.getAddress()) 652 << " -- " << formatv("{0:x}", B.getAddress() + B.getSize()) 653 << " for \"" << StringRef(B.getContent().data()) << "\"\n"; 654 }); 655 656 // If there's no symbol at the start of this block then create one. 657 if (NSyms.empty() || NSyms.back()->Value != B.getAddress()) { 658 auto &S = G->addAnonymousSymbol(B, 0, BlockSize, false, false); 659 setCanonicalSymbol(NSec, S); 660 LLVM_DEBUG({ 661 dbgs() << " Adding anonymous symbol for c-string block " 662 << formatv("{0:x16} -- {1:x16}", S.getAddress(), 663 S.getAddress() + BlockSize) 664 << "\n"; 665 }); 666 } 667 668 // Process any remaining symbols that point into this block. 669 JITTargetAddress LastCanonicalAddr = B.getAddress() + BlockEnd; 670 while (!NSyms.empty() && 671 NSyms.back()->Value < (B.getAddress() + BlockSize)) { 672 auto &NSym = *NSyms.back(); 673 size_t SymSize = (B.getAddress() + BlockSize) - NSyms.back()->Value; 674 bool SymLive = 675 (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip; 676 677 bool IsCanonical = false; 678 if (LastCanonicalAddr != NSym.Value) { 679 IsCanonical = true; 680 LastCanonicalAddr = NSym.Value; 681 } 682 683 createStandardGraphSymbol(NSym, B, SymSize, SectionIsText, SymLive, 684 IsCanonical); 685 686 NSyms.pop_back(); 687 } 688 689 BlockStart += BlockSize; 690 } 691 692 return Error::success(); 693 } 694 695 Error CompactUnwindSplitter::operator()(LinkGraph &G) { 696 auto *CUSec = G.findSectionByName(CompactUnwindSectionName); 697 if (!CUSec) 698 return Error::success(); 699 700 if (!G.getTargetTriple().isOSBinFormatMachO()) 701 return make_error<JITLinkError>( 702 "Error linking " + G.getName() + 703 ": compact unwind splitting not supported on non-macho target " + 704 G.getTargetTriple().str()); 705 706 unsigned CURecordSize = 0; 707 unsigned PersonalityEdgeOffset = 0; 708 unsigned LSDAEdgeOffset = 0; 709 switch (G.getTargetTriple().getArch()) { 710 case Triple::aarch64: 711 case Triple::x86_64: 712 // 64-bit compact-unwind record format: 713 // Range start: 8 bytes. 714 // Range size: 4 bytes. 715 // CU encoding: 4 bytes. 716 // Personality: 8 bytes. 717 // LSDA: 8 bytes. 718 CURecordSize = 32; 719 PersonalityEdgeOffset = 16; 720 LSDAEdgeOffset = 24; 721 break; 722 default: 723 return make_error<JITLinkError>( 724 "Error linking " + G.getName() + 725 ": compact unwind splitting not supported on " + 726 G.getTargetTriple().getArchName()); 727 } 728 729 std::vector<Block *> OriginalBlocks(CUSec->blocks().begin(), 730 CUSec->blocks().end()); 731 LLVM_DEBUG({ 732 dbgs() << "In " << G.getName() << " splitting compact unwind section " 733 << CompactUnwindSectionName << " containing " 734 << OriginalBlocks.size() << " initial blocks...\n"; 735 }); 736 737 while (!OriginalBlocks.empty()) { 738 auto *B = OriginalBlocks.back(); 739 OriginalBlocks.pop_back(); 740 741 if (B->getSize() == 0) { 742 LLVM_DEBUG({ 743 dbgs() << " Skipping empty block at " 744 << formatv("{0:x16}", B->getAddress()) << "\n"; 745 }); 746 continue; 747 } 748 749 LLVM_DEBUG({ 750 dbgs() << " Splitting block at " << formatv("{0:x16}", B->getAddress()) 751 << " into " << (B->getSize() / CURecordSize) 752 << " compact unwind record(s)\n"; 753 }); 754 755 if (B->getSize() % CURecordSize) 756 return make_error<JITLinkError>( 757 "Error splitting compact unwind record in " + G.getName() + 758 ": block at " + formatv("{0:x}", B->getAddress()) + " has size " + 759 formatv("{0:x}", B->getSize()) + 760 " (not a multiple of CU record size of " + 761 formatv("{0:x}", CURecordSize) + ")"); 762 763 unsigned NumBlocks = B->getSize() / CURecordSize; 764 LinkGraph::SplitBlockCache C; 765 766 for (unsigned I = 0; I != NumBlocks; ++I) { 767 auto &CURec = G.splitBlock(*B, CURecordSize, &C); 768 bool AddedKeepAlive = false; 769 770 for (auto &E : CURec.edges()) { 771 if (E.getOffset() == 0) { 772 LLVM_DEBUG({ 773 dbgs() << " Updating compact unwind record at " 774 << formatv("{0:x16}", CURec.getAddress()) << " to point to " 775 << (E.getTarget().hasName() ? E.getTarget().getName() 776 : StringRef()) 777 << " (at " << formatv("{0:x16}", E.getTarget().getAddress()) 778 << ")\n"; 779 }); 780 781 if (E.getTarget().isExternal()) 782 return make_error<JITLinkError>( 783 "Error adding keep-alive edge for compact unwind record at " + 784 formatv("{0:x}", CURec.getAddress()) + ": target " + 785 E.getTarget().getName() + " is an external symbol"); 786 auto &TgtBlock = E.getTarget().getBlock(); 787 auto &CURecSym = 788 G.addAnonymousSymbol(CURec, 0, CURecordSize, 0, false); 789 TgtBlock.addEdge(Edge::KeepAlive, 0, CURecSym, 0); 790 AddedKeepAlive = true; 791 } else if (E.getOffset() != PersonalityEdgeOffset && 792 E.getOffset() != LSDAEdgeOffset) 793 return make_error<JITLinkError>("Unexpected edge at offset " + 794 formatv("{0:x}", E.getOffset()) + 795 " in compact unwind record at " + 796 formatv("{0:x}", CURec.getAddress())); 797 } 798 799 if (!AddedKeepAlive) 800 return make_error<JITLinkError>( 801 "Error adding keep-alive edge for compact unwind record at " + 802 formatv("{0:x}", CURec.getAddress()) + 803 ": no outgoing target edge at offset 0"); 804 } 805 } 806 return Error::success(); 807 } 808 809 } // end namespace jitlink 810 } // end namespace llvm 811