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() = default;
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 = orc::ExecutorAddr(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 = orc::ExecutorAddr(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 (orc::ExecutorAddr(Value) < NSec->Address ||
291           orc::ExecutorAddr(Value) > NSec->Address + NSec->Size)
292         return make_error<JITLinkError>("Address " + formatv("{0:x}", Value) +
293                                         " for symbol " + *Name +
294                                         " does not fall within section");
295 
296       if (!NSec->GraphSection) {
297         LLVM_DEBUG({
298           dbgs() << "  Skipping: Symbol is in section " << NSec->SegName << "/"
299                  << NSec->SectName
300                  << " which has no associated graph section.\n";
301         });
302         continue;
303       }
304     }
305 
306     IndexToSymbol[SymbolIndex] =
307         &createNormalizedSymbol(*Name, Value, Type, Sect, Desc,
308                                 getLinkage(Desc), getScope(*Name, Type));
309   }
310 
311   return Error::success();
312 }
313 
314 void MachOLinkGraphBuilder::addSectionStartSymAndBlock(
315     unsigned SecIndex, Section &GraphSec, orc::ExecutorAddr Address,
316     const char *Data, orc::ExecutorAddrDiff Size, uint32_t Alignment,
317     bool IsLive) {
318   Block &B =
319       Data ? G->createContentBlock(GraphSec, ArrayRef<char>(Data, Size),
320                                    Address, Alignment, 0)
321            : G->createZeroFillBlock(GraphSec, Size, Address, Alignment, 0);
322   auto &Sym = G->addAnonymousSymbol(B, 0, Size, false, IsLive);
323   auto SecI = IndexToSection.find(SecIndex);
324   assert(SecI != IndexToSection.end() && "SecIndex invalid");
325   auto &NSec = SecI->second;
326   assert(!NSec.CanonicalSymbols.count(Sym.getAddress()) &&
327          "Anonymous block start symbol clashes with existing symbol address");
328   NSec.CanonicalSymbols[Sym.getAddress()] = &Sym;
329 }
330 
331 Error MachOLinkGraphBuilder::graphifyRegularSymbols() {
332 
333   LLVM_DEBUG(dbgs() << "Creating graph symbols...\n");
334 
335   /// We only have 256 section indexes: Use a vector rather than a map.
336   std::vector<std::vector<NormalizedSymbol *>> SecIndexToSymbols;
337   SecIndexToSymbols.resize(256);
338 
339   // Create commons, externs, and absolutes, and partition all other symbols by
340   // section.
341   for (auto &KV : IndexToSymbol) {
342     auto &NSym = *KV.second;
343 
344     switch (NSym.Type & MachO::N_TYPE) {
345     case MachO::N_UNDF:
346       if (NSym.Value) {
347         if (!NSym.Name)
348           return make_error<JITLinkError>("Anonymous common symbol at index " +
349                                           Twine(KV.first));
350         NSym.GraphSymbol = &G->addCommonSymbol(
351             *NSym.Name, NSym.S, getCommonSection(), orc::ExecutorAddr(),
352             orc::ExecutorAddrDiff(NSym.Value),
353             1ull << MachO::GET_COMM_ALIGN(NSym.Desc),
354             NSym.Desc & MachO::N_NO_DEAD_STRIP);
355       } else {
356         if (!NSym.Name)
357           return make_error<JITLinkError>("Anonymous external symbol at "
358                                           "index " +
359                                           Twine(KV.first));
360         NSym.GraphSymbol = &G->addExternalSymbol(
361             *NSym.Name, 0,
362             NSym.Desc & MachO::N_WEAK_REF ? Linkage::Weak : Linkage::Strong);
363       }
364       break;
365     case MachO::N_ABS:
366       if (!NSym.Name)
367         return make_error<JITLinkError>("Anonymous absolute symbol at index " +
368                                         Twine(KV.first));
369       NSym.GraphSymbol = &G->addAbsoluteSymbol(
370           *NSym.Name, orc::ExecutorAddr(NSym.Value), 0, Linkage::Strong,
371           getScope(*NSym.Name, NSym.Type), NSym.Desc & MachO::N_NO_DEAD_STRIP);
372       break;
373     case MachO::N_SECT:
374       SecIndexToSymbols[NSym.Sect - 1].push_back(&NSym);
375       break;
376     case MachO::N_PBUD:
377       return make_error<JITLinkError>(
378           "Unupported N_PBUD symbol " +
379           (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) +
380           " at index " + Twine(KV.first));
381     case MachO::N_INDR:
382       return make_error<JITLinkError>(
383           "Unupported N_INDR symbol " +
384           (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) +
385           " at index " + Twine(KV.first));
386     default:
387       return make_error<JITLinkError>(
388           "Unrecognized symbol type " + Twine(NSym.Type & MachO::N_TYPE) +
389           " for symbol " +
390           (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) +
391           " at index " + Twine(KV.first));
392     }
393   }
394 
395   // Loop over sections performing regular graphification for those that
396   // don't have custom parsers.
397   for (auto &KV : IndexToSection) {
398     auto SecIndex = KV.first;
399     auto &NSec = KV.second;
400 
401     if (!NSec.GraphSection) {
402       LLVM_DEBUG({
403         dbgs() << "  " << NSec.SegName << "/" << NSec.SectName
404                << " has no graph section. Skipping.\n";
405       });
406       continue;
407     }
408 
409     // Skip sections with custom parsers.
410     if (CustomSectionParserFunctions.count(NSec.GraphSection->getName())) {
411       LLVM_DEBUG({
412         dbgs() << "  Skipping section " << NSec.GraphSection->getName()
413                << " as it has a custom parser.\n";
414       });
415       continue;
416     } else if ((NSec.Flags & MachO::SECTION_TYPE) ==
417                MachO::S_CSTRING_LITERALS) {
418       if (auto Err = graphifyCStringSection(
419               NSec, std::move(SecIndexToSymbols[SecIndex])))
420         return Err;
421       continue;
422     } else
423       LLVM_DEBUG({
424         dbgs() << "  Graphifying regular section "
425                << NSec.GraphSection->getName() << "...\n";
426       });
427 
428     bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP;
429     bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
430 
431     auto &SecNSymStack = SecIndexToSymbols[SecIndex];
432 
433     // If this section is non-empty but there are no symbols covering it then
434     // create one block and anonymous symbol to cover the entire section.
435     if (SecNSymStack.empty()) {
436       if (NSec.Size > 0) {
437         LLVM_DEBUG({
438           dbgs() << "    Section non-empty, but contains no symbols. "
439                     "Creating anonymous block to cover "
440                  << formatv("{0:x16}", NSec.Address) << " -- "
441                  << formatv("{0:x16}", NSec.Address + NSec.Size) << "\n";
442         });
443         addSectionStartSymAndBlock(SecIndex, *NSec.GraphSection, NSec.Address,
444                                    NSec.Data, NSec.Size, NSec.Alignment,
445                                    SectionIsNoDeadStrip);
446       } else
447         LLVM_DEBUG({
448           dbgs() << "    Section empty and contains no symbols. Skipping.\n";
449         });
450       continue;
451     }
452 
453     // Sort the symbol stack in by address, alt-entry status, scope, and name.
454     // We sort in reverse order so that symbols will be visited in the right
455     // order when we pop off the stack below.
456     llvm::sort(SecNSymStack, [](const NormalizedSymbol *LHS,
457                                 const NormalizedSymbol *RHS) {
458       if (LHS->Value != RHS->Value)
459         return LHS->Value > RHS->Value;
460       if (isAltEntry(*LHS) != isAltEntry(*RHS))
461         return isAltEntry(*RHS);
462       if (LHS->S != RHS->S)
463         return static_cast<uint8_t>(LHS->S) < static_cast<uint8_t>(RHS->S);
464       return LHS->Name < RHS->Name;
465     });
466 
467     // The first symbol in a section can not be an alt-entry symbol.
468     if (!SecNSymStack.empty() && isAltEntry(*SecNSymStack.back()))
469       return make_error<JITLinkError>(
470           "First symbol in " + NSec.GraphSection->getName() + " is alt-entry");
471 
472     // If the section is non-empty but there is no symbol covering the start
473     // address then add an anonymous one.
474     if (orc::ExecutorAddr(SecNSymStack.back()->Value) != NSec.Address) {
475       auto AnonBlockSize =
476           orc::ExecutorAddr(SecNSymStack.back()->Value) - NSec.Address;
477       LLVM_DEBUG({
478         dbgs() << "    Section start not covered by symbol. "
479                << "Creating anonymous block to cover [ " << NSec.Address
480                << " -- " << (NSec.Address + AnonBlockSize) << " ]\n";
481       });
482       addSectionStartSymAndBlock(SecIndex, *NSec.GraphSection, NSec.Address,
483                                  NSec.Data, AnonBlockSize, NSec.Alignment,
484                                  SectionIsNoDeadStrip);
485     }
486 
487     // Visit section symbols in order by popping off the reverse-sorted stack,
488     // building blocks for each alt-entry chain and creating symbols as we go.
489     while (!SecNSymStack.empty()) {
490       SmallVector<NormalizedSymbol *, 8> BlockSyms;
491 
492       BlockSyms.push_back(SecNSymStack.back());
493       SecNSymStack.pop_back();
494       while (!SecNSymStack.empty() &&
495              (isAltEntry(*SecNSymStack.back()) ||
496               SecNSymStack.back()->Value == BlockSyms.back()->Value)) {
497         BlockSyms.push_back(SecNSymStack.back());
498         SecNSymStack.pop_back();
499       }
500 
501       // BlockNSyms now contains the block symbols in reverse canonical order.
502       auto BlockStart = orc::ExecutorAddr(BlockSyms.front()->Value);
503       orc::ExecutorAddr BlockEnd =
504           SecNSymStack.empty() ? NSec.Address + NSec.Size
505                                : orc::ExecutorAddr(SecNSymStack.back()->Value);
506       orc::ExecutorAddrDiff BlockOffset = BlockStart - NSec.Address;
507       orc::ExecutorAddrDiff BlockSize = BlockEnd - BlockStart;
508 
509       LLVM_DEBUG({
510         dbgs() << "    Creating block for " << formatv("{0:x16}", BlockStart)
511                << " -- " << formatv("{0:x16}", BlockEnd) << ": "
512                << NSec.GraphSection->getName() << " + "
513                << formatv("{0:x16}", BlockOffset) << " with "
514                << BlockSyms.size() << " symbol(s)...\n";
515       });
516 
517       Block &B =
518           NSec.Data
519               ? G->createContentBlock(
520                     *NSec.GraphSection,
521                     ArrayRef<char>(NSec.Data + BlockOffset, BlockSize),
522                     BlockStart, NSec.Alignment, BlockStart % NSec.Alignment)
523               : G->createZeroFillBlock(*NSec.GraphSection, BlockSize,
524                                        BlockStart, NSec.Alignment,
525                                        BlockStart % NSec.Alignment);
526 
527       Optional<orc::ExecutorAddr> LastCanonicalAddr;
528       auto SymEnd = BlockEnd;
529       while (!BlockSyms.empty()) {
530         auto &NSym = *BlockSyms.back();
531         BlockSyms.pop_back();
532 
533         bool SymLive =
534             (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip;
535 
536         auto &Sym = createStandardGraphSymbol(
537             NSym, B, SymEnd - orc::ExecutorAddr(NSym.Value), SectionIsText,
538             SymLive, LastCanonicalAddr != orc::ExecutorAddr(NSym.Value));
539 
540         if (LastCanonicalAddr != Sym.getAddress()) {
541           if (LastCanonicalAddr)
542             SymEnd = *LastCanonicalAddr;
543           LastCanonicalAddr = Sym.getAddress();
544         }
545       }
546     }
547   }
548 
549   return Error::success();
550 }
551 
552 Symbol &MachOLinkGraphBuilder::createStandardGraphSymbol(NormalizedSymbol &NSym,
553                                                          Block &B, size_t Size,
554                                                          bool IsText,
555                                                          bool IsNoDeadStrip,
556                                                          bool IsCanonical) {
557 
558   LLVM_DEBUG({
559     dbgs() << "      " << formatv("{0:x16}", NSym.Value) << " -- "
560            << formatv("{0:x16}", NSym.Value + Size) << ": ";
561     if (!NSym.Name)
562       dbgs() << "<anonymous symbol>";
563     else
564       dbgs() << NSym.Name;
565     if (IsText)
566       dbgs() << " [text]";
567     if (IsNoDeadStrip)
568       dbgs() << " [no-dead-strip]";
569     if (!IsCanonical)
570       dbgs() << " [non-canonical]";
571     dbgs() << "\n";
572   });
573 
574   auto SymOffset = orc::ExecutorAddr(NSym.Value) - B.getAddress();
575   auto &Sym =
576       NSym.Name
577           ? G->addDefinedSymbol(B, SymOffset, *NSym.Name, Size, NSym.L, NSym.S,
578                                 IsText, IsNoDeadStrip)
579           : G->addAnonymousSymbol(B, SymOffset, Size, IsText, IsNoDeadStrip);
580   NSym.GraphSymbol = &Sym;
581 
582   if (IsCanonical)
583     setCanonicalSymbol(getSectionByIndex(NSym.Sect - 1), Sym);
584 
585   return Sym;
586 }
587 
588 Error MachOLinkGraphBuilder::graphifySectionsWithCustomParsers() {
589   // Graphify special sections.
590   for (auto &KV : IndexToSection) {
591     auto &NSec = KV.second;
592 
593     // Skip non-graph sections.
594     if (!NSec.GraphSection)
595       continue;
596 
597     auto HI = CustomSectionParserFunctions.find(NSec.GraphSection->getName());
598     if (HI != CustomSectionParserFunctions.end()) {
599       auto &Parse = HI->second;
600       if (auto Err = Parse(NSec))
601         return Err;
602     }
603   }
604 
605   return Error::success();
606 }
607 
608 Error MachOLinkGraphBuilder::graphifyCStringSection(
609     NormalizedSection &NSec, std::vector<NormalizedSymbol *> NSyms) {
610   assert(NSec.GraphSection && "C string literal section missing graph section");
611   assert(NSec.Data && "C string literal section has no data");
612 
613   LLVM_DEBUG({
614     dbgs() << "  Graphifying C-string literal section "
615            << NSec.GraphSection->getName() << "\n";
616   });
617 
618   if (NSec.Data[NSec.Size - 1] != '\0')
619     return make_error<JITLinkError>("C string literal section " +
620                                     NSec.GraphSection->getName() +
621                                     " does not end with null terminator");
622 
623   /// Sort into reverse order to use as a stack.
624   llvm::sort(NSyms,
625              [](const NormalizedSymbol *LHS, const NormalizedSymbol *RHS) {
626                if (LHS->Value != RHS->Value)
627                  return LHS->Value > RHS->Value;
628                if (LHS->L != RHS->L)
629                  return LHS->L > RHS->L;
630                if (LHS->S != RHS->S)
631                  return LHS->S > RHS->S;
632                if (RHS->Name) {
633                  if (!LHS->Name)
634                    return true;
635                  return *LHS->Name > *RHS->Name;
636                }
637                return false;
638              });
639 
640   bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP;
641   bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
642   orc::ExecutorAddrDiff BlockStart = 0;
643 
644   // Scan section for null characters.
645   for (size_t I = 0; I != NSec.Size; ++I)
646     if (NSec.Data[I] == '\0') {
647       size_t BlockSize = I + 1 - BlockStart;
648       // Create a block for this null terminated string.
649       auto &B = G->createContentBlock(*NSec.GraphSection,
650                                       {NSec.Data + BlockStart, BlockSize},
651                                       NSec.Address + BlockStart, NSec.Alignment,
652                                       BlockStart % NSec.Alignment);
653 
654       LLVM_DEBUG({
655         dbgs() << "    Created block " << B.getRange()
656                << ", align = " << B.getAlignment()
657                << ", align-ofs = " << B.getAlignmentOffset() << " for \"";
658         for (size_t J = 0; J != std::min(B.getSize(), size_t(16)); ++J)
659           switch (B.getContent()[J]) {
660           case '\0': break;
661           case '\n': dbgs() << "\\n"; break;
662           case '\t': dbgs() << "\\t"; break;
663           default:   dbgs() << B.getContent()[J]; break;
664           }
665         if (B.getSize() > 16)
666           dbgs() << "...";
667         dbgs() << "\"\n";
668       });
669 
670       // If there's no symbol at the start of this block then create one.
671       if (NSyms.empty() ||
672           orc::ExecutorAddr(NSyms.back()->Value) != B.getAddress()) {
673         auto &S = G->addAnonymousSymbol(B, 0, BlockSize, false, false);
674         setCanonicalSymbol(NSec, S);
675         LLVM_DEBUG({
676           dbgs() << "      Adding symbol for c-string block " << B.getRange()
677                  << ": <anonymous symbol> at offset 0\n";
678         });
679       }
680 
681       // Process any remaining symbols that point into this block.
682       auto LastCanonicalAddr = B.getAddress() + BlockSize;
683       while (!NSyms.empty() && orc::ExecutorAddr(NSyms.back()->Value) <
684                                    B.getAddress() + BlockSize) {
685         auto &NSym = *NSyms.back();
686         size_t SymSize = (B.getAddress() + BlockSize) -
687                          orc::ExecutorAddr(NSyms.back()->Value);
688         bool SymLive =
689             (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip;
690 
691         bool IsCanonical = false;
692         if (LastCanonicalAddr != orc::ExecutorAddr(NSym.Value)) {
693           IsCanonical = true;
694           LastCanonicalAddr = orc::ExecutorAddr(NSym.Value);
695         }
696 
697         auto &Sym = createStandardGraphSymbol(NSym, B, SymSize, SectionIsText,
698                                               SymLive, IsCanonical);
699         (void)Sym;
700         LLVM_DEBUG({
701           dbgs() << "      Adding symbol for c-string block " << B.getRange()
702                  << ": "
703                  << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>")
704                  << " at offset " << formatv("{0:x}", Sym.getOffset()) << "\n";
705         });
706 
707         NSyms.pop_back();
708       }
709 
710       BlockStart += BlockSize;
711     }
712 
713   return Error::success();
714 }
715 
716 Error CompactUnwindSplitter::operator()(LinkGraph &G) {
717   auto *CUSec = G.findSectionByName(CompactUnwindSectionName);
718   if (!CUSec)
719     return Error::success();
720 
721   if (!G.getTargetTriple().isOSBinFormatMachO())
722     return make_error<JITLinkError>(
723         "Error linking " + G.getName() +
724         ": compact unwind splitting not supported on non-macho target " +
725         G.getTargetTriple().str());
726 
727   unsigned CURecordSize = 0;
728   unsigned PersonalityEdgeOffset = 0;
729   unsigned LSDAEdgeOffset = 0;
730   switch (G.getTargetTriple().getArch()) {
731   case Triple::aarch64:
732   case Triple::x86_64:
733     // 64-bit compact-unwind record format:
734     // Range start: 8 bytes.
735     // Range size:  4 bytes.
736     // CU encoding: 4 bytes.
737     // Personality: 8 bytes.
738     // LSDA:        8 bytes.
739     CURecordSize = 32;
740     PersonalityEdgeOffset = 16;
741     LSDAEdgeOffset = 24;
742     break;
743   default:
744     return make_error<JITLinkError>(
745         "Error linking " + G.getName() +
746         ": compact unwind splitting not supported on " +
747         G.getTargetTriple().getArchName());
748   }
749 
750   std::vector<Block *> OriginalBlocks(CUSec->blocks().begin(),
751                                       CUSec->blocks().end());
752   LLVM_DEBUG({
753     dbgs() << "In " << G.getName() << " splitting compact unwind section "
754            << CompactUnwindSectionName << " containing "
755            << OriginalBlocks.size() << " initial blocks...\n";
756   });
757 
758   while (!OriginalBlocks.empty()) {
759     auto *B = OriginalBlocks.back();
760     OriginalBlocks.pop_back();
761 
762     if (B->getSize() == 0) {
763       LLVM_DEBUG({
764         dbgs() << "  Skipping empty block at "
765                << formatv("{0:x16}", B->getAddress()) << "\n";
766       });
767       continue;
768     }
769 
770     LLVM_DEBUG({
771       dbgs() << "  Splitting block at " << formatv("{0:x16}", B->getAddress())
772              << " into " << (B->getSize() / CURecordSize)
773              << " compact unwind record(s)\n";
774     });
775 
776     if (B->getSize() % CURecordSize)
777       return make_error<JITLinkError>(
778           "Error splitting compact unwind record in " + G.getName() +
779           ": block at " + formatv("{0:x}", B->getAddress()) + " has size " +
780           formatv("{0:x}", B->getSize()) +
781           " (not a multiple of CU record size of " +
782           formatv("{0:x}", CURecordSize) + ")");
783 
784     unsigned NumBlocks = B->getSize() / CURecordSize;
785     LinkGraph::SplitBlockCache C;
786 
787     for (unsigned I = 0; I != NumBlocks; ++I) {
788       auto &CURec = G.splitBlock(*B, CURecordSize, &C);
789       bool AddedKeepAlive = false;
790 
791       for (auto &E : CURec.edges()) {
792         if (E.getOffset() == 0) {
793           LLVM_DEBUG({
794             dbgs() << "    Updating compact unwind record at "
795                    << formatv("{0:x16}", CURec.getAddress()) << " to point to "
796                    << (E.getTarget().hasName() ? E.getTarget().getName()
797                                                : StringRef())
798                    << " (at " << formatv("{0:x16}", E.getTarget().getAddress())
799                    << ")\n";
800           });
801 
802           if (E.getTarget().isExternal())
803             return make_error<JITLinkError>(
804                 "Error adding keep-alive edge for compact unwind record at " +
805                 formatv("{0:x}", CURec.getAddress()) + ": target " +
806                 E.getTarget().getName() + " is an external symbol");
807           auto &TgtBlock = E.getTarget().getBlock();
808           auto &CURecSym =
809               G.addAnonymousSymbol(CURec, 0, CURecordSize, false, false);
810           TgtBlock.addEdge(Edge::KeepAlive, 0, CURecSym, 0);
811           AddedKeepAlive = true;
812         } else if (E.getOffset() != PersonalityEdgeOffset &&
813                    E.getOffset() != LSDAEdgeOffset)
814           return make_error<JITLinkError>("Unexpected edge at offset " +
815                                           formatv("{0:x}", E.getOffset()) +
816                                           " in compact unwind record at " +
817                                           formatv("{0:x}", CURec.getAddress()));
818       }
819 
820       if (!AddedKeepAlive)
821         return make_error<JITLinkError>(
822             "Error adding keep-alive edge for compact unwind record at " +
823             formatv("{0:x}", CURec.getAddress()) +
824             ": no outgoing target edge at offset 0");
825     }
826   }
827   return Error::success();
828 }
829 
830 } // end namespace jitlink
831 } // end namespace llvm
832