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