xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
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 = 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           Scope::Default, 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       orc::ExecutorAddrDiff BlockEnd = I + 1;
648       size_t BlockSize = BlockEnd - BlockStart;
649       // Create a block for this null terminated string.
650       auto &B = G->createContentBlock(*NSec.GraphSection,
651                                       {NSec.Data + BlockStart, BlockSize},
652                                       NSec.Address + BlockStart, 1, 0);
653 
654       LLVM_DEBUG({
655         dbgs() << "    Created block " << formatv("{0:x}", B.getAddress())
656                << " -- " << formatv("{0:x}", B.getAddress() + B.getSize())
657                << " for \"" << StringRef(B.getContent().data()) << "\"\n";
658       });
659 
660       // If there's no symbol at the start of this block then create one.
661       if (NSyms.empty() ||
662           orc::ExecutorAddr(NSyms.back()->Value) != B.getAddress()) {
663         auto &S = G->addAnonymousSymbol(B, 0, BlockSize, false, false);
664         setCanonicalSymbol(NSec, S);
665         LLVM_DEBUG({
666           dbgs() << "      Adding anonymous symbol for c-string block "
667                  << formatv("{0:x16} -- {1:x16}", S.getAddress(),
668                             S.getAddress() + BlockSize)
669                  << "\n";
670         });
671       }
672 
673       // Process any remaining symbols that point into this block.
674       auto LastCanonicalAddr = B.getAddress() + BlockEnd;
675       while (!NSyms.empty() && orc::ExecutorAddr(NSyms.back()->Value) <
676                                    B.getAddress() + BlockSize) {
677         auto &NSym = *NSyms.back();
678         size_t SymSize = (B.getAddress() + BlockSize) -
679                          orc::ExecutorAddr(NSyms.back()->Value);
680         bool SymLive =
681             (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip;
682 
683         bool IsCanonical = false;
684         if (LastCanonicalAddr != orc::ExecutorAddr(NSym.Value)) {
685           IsCanonical = true;
686           LastCanonicalAddr = orc::ExecutorAddr(NSym.Value);
687         }
688 
689         createStandardGraphSymbol(NSym, B, SymSize, SectionIsText, SymLive,
690                                   IsCanonical);
691 
692         NSyms.pop_back();
693       }
694 
695       BlockStart += BlockSize;
696     }
697 
698   return Error::success();
699 }
700 
701 Error CompactUnwindSplitter::operator()(LinkGraph &G) {
702   auto *CUSec = G.findSectionByName(CompactUnwindSectionName);
703   if (!CUSec)
704     return Error::success();
705 
706   if (!G.getTargetTriple().isOSBinFormatMachO())
707     return make_error<JITLinkError>(
708         "Error linking " + G.getName() +
709         ": compact unwind splitting not supported on non-macho target " +
710         G.getTargetTriple().str());
711 
712   unsigned CURecordSize = 0;
713   unsigned PersonalityEdgeOffset = 0;
714   unsigned LSDAEdgeOffset = 0;
715   switch (G.getTargetTriple().getArch()) {
716   case Triple::aarch64:
717   case Triple::x86_64:
718     // 64-bit compact-unwind record format:
719     // Range start: 8 bytes.
720     // Range size:  4 bytes.
721     // CU encoding: 4 bytes.
722     // Personality: 8 bytes.
723     // LSDA:        8 bytes.
724     CURecordSize = 32;
725     PersonalityEdgeOffset = 16;
726     LSDAEdgeOffset = 24;
727     break;
728   default:
729     return make_error<JITLinkError>(
730         "Error linking " + G.getName() +
731         ": compact unwind splitting not supported on " +
732         G.getTargetTriple().getArchName());
733   }
734 
735   std::vector<Block *> OriginalBlocks(CUSec->blocks().begin(),
736                                       CUSec->blocks().end());
737   LLVM_DEBUG({
738     dbgs() << "In " << G.getName() << " splitting compact unwind section "
739            << CompactUnwindSectionName << " containing "
740            << OriginalBlocks.size() << " initial blocks...\n";
741   });
742 
743   while (!OriginalBlocks.empty()) {
744     auto *B = OriginalBlocks.back();
745     OriginalBlocks.pop_back();
746 
747     if (B->getSize() == 0) {
748       LLVM_DEBUG({
749         dbgs() << "  Skipping empty block at "
750                << formatv("{0:x16}", B->getAddress()) << "\n";
751       });
752       continue;
753     }
754 
755     LLVM_DEBUG({
756       dbgs() << "  Splitting block at " << formatv("{0:x16}", B->getAddress())
757              << " into " << (B->getSize() / CURecordSize)
758              << " compact unwind record(s)\n";
759     });
760 
761     if (B->getSize() % CURecordSize)
762       return make_error<JITLinkError>(
763           "Error splitting compact unwind record in " + G.getName() +
764           ": block at " + formatv("{0:x}", B->getAddress()) + " has size " +
765           formatv("{0:x}", B->getSize()) +
766           " (not a multiple of CU record size of " +
767           formatv("{0:x}", CURecordSize) + ")");
768 
769     unsigned NumBlocks = B->getSize() / CURecordSize;
770     LinkGraph::SplitBlockCache C;
771 
772     for (unsigned I = 0; I != NumBlocks; ++I) {
773       auto &CURec = G.splitBlock(*B, CURecordSize, &C);
774       bool AddedKeepAlive = false;
775 
776       for (auto &E : CURec.edges()) {
777         if (E.getOffset() == 0) {
778           LLVM_DEBUG({
779             dbgs() << "    Updating compact unwind record at "
780                    << formatv("{0:x16}", CURec.getAddress()) << " to point to "
781                    << (E.getTarget().hasName() ? E.getTarget().getName()
782                                                : StringRef())
783                    << " (at " << formatv("{0:x16}", E.getTarget().getAddress())
784                    << ")\n";
785           });
786 
787           if (E.getTarget().isExternal())
788             return make_error<JITLinkError>(
789                 "Error adding keep-alive edge for compact unwind record at " +
790                 formatv("{0:x}", CURec.getAddress()) + ": target " +
791                 E.getTarget().getName() + " is an external symbol");
792           auto &TgtBlock = E.getTarget().getBlock();
793           auto &CURecSym =
794               G.addAnonymousSymbol(CURec, 0, CURecordSize, false, false);
795           TgtBlock.addEdge(Edge::KeepAlive, 0, CURecSym, 0);
796           AddedKeepAlive = true;
797         } else if (E.getOffset() != PersonalityEdgeOffset &&
798                    E.getOffset() != LSDAEdgeOffset)
799           return make_error<JITLinkError>("Unexpected edge at offset " +
800                                           formatv("{0:x}", E.getOffset()) +
801                                           " in compact unwind record at " +
802                                           formatv("{0:x}", CURec.getAddress()));
803       }
804 
805       if (!AddedKeepAlive)
806         return make_error<JITLinkError>(
807             "Error adding keep-alive edge for compact unwind record at " +
808             formatv("{0:x}", CURec.getAddress()) +
809             ": no outgoing target edge at offset 0");
810     }
811   }
812   return Error::success();
813 }
814 
815 } // end namespace jitlink
816 } // end namespace llvm
817