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   // Sanity check: 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())
createLinkGraphFromMachOObject(MemoryBufferRef ObjectBuffer)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   }
link_MachO(std::unique_ptr<LinkGraph> G,std::unique_ptr<JITLinkContext> Ctx)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     auto Prot = static_cast<sys::Memory::ProtectionFlags>(
112         sys::Memory::MF_READ | sys::Memory::MF_WRITE);
113     CommonSection = &G->createSection(CommonSectionName, Prot);
114   }
115   return *CommonSection;
116 }
117 
118 Error MachOLinkGraphBuilder::createNormalizedSections() {
119   // Build normalized sections. Verifies that section data is in-range (for
120   // sections with content) and that address ranges are non-overlapping.
121 
122   LLVM_DEBUG(dbgs() << "Creating normalized sections...\n");
123 
124   for (auto &SecRef : Obj.sections()) {
125     NormalizedSection NSec;
126     uint32_t DataOffset = 0;
127 
128     auto SecIndex = Obj.getSectionIndex(SecRef.getRawDataRefImpl());
129 
130     if (Obj.is64Bit()) {
131       const MachO::section_64 &Sec64 =
132           Obj.getSection64(SecRef.getRawDataRefImpl());
133 
134       memcpy(&NSec.SectName, &Sec64.sectname, 16);
135       NSec.SectName[16] = '\0';
136       memcpy(&NSec.SegName, Sec64.segname, 16);
137       NSec.SegName[16] = '\0';
138 
139       NSec.Address = Sec64.addr;
140       NSec.Size = Sec64.size;
141       NSec.Alignment = 1ULL << Sec64.align;
142       NSec.Flags = Sec64.flags;
143       DataOffset = Sec64.offset;
144     } else {
145       const MachO::section &Sec32 = Obj.getSection(SecRef.getRawDataRefImpl());
146 
147       memcpy(&NSec.SectName, &Sec32.sectname, 16);
148       NSec.SectName[16] = '\0';
149       memcpy(&NSec.SegName, Sec32.segname, 16);
150       NSec.SegName[16] = '\0';
151 
152       NSec.Address = Sec32.addr;
153       NSec.Size = Sec32.size;
154       NSec.Alignment = 1ULL << Sec32.align;
155       NSec.Flags = Sec32.flags;
156       DataOffset = Sec32.offset;
157     }
158 
159     LLVM_DEBUG({
160       dbgs() << "  " << NSec.SegName << "," << NSec.SectName << ": "
161              << formatv("{0:x16}", NSec.Address) << " -- "
162              << formatv("{0:x16}", NSec.Address + NSec.Size)
163              << ", align: " << NSec.Alignment << ", index: " << SecIndex
164              << "\n";
165     });
166 
167     // Get the section data if any.
168     if (!isZeroFillSection(NSec)) {
169       if (DataOffset + NSec.Size > Obj.getData().size())
170         return make_error<JITLinkError>(
171             "Section data extends past end of file");
172 
173       NSec.Data = Obj.getData().data() + DataOffset;
174     }
175 
176     // Get prot flags.
177     // FIXME: Make sure this test is correct (it's probably missing cases
178     // as-is).
179     sys::Memory::ProtectionFlags Prot;
180     if (NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS)
181       Prot = static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ |
182                                                        sys::Memory::MF_EXEC);
183     else
184       Prot = static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ |
185                                                        sys::Memory::MF_WRITE);
186 
187     if (!isDebugSection(NSec)) {
188       auto FullyQualifiedName =
189           G->allocateString(StringRef(NSec.SegName) + "," + NSec.SectName);
190       NSec.GraphSection = &G->createSection(
191           StringRef(FullyQualifiedName.data(), FullyQualifiedName.size()),
192           Prot);
193     } else
194       LLVM_DEBUG({
195         dbgs() << "    " << NSec.SegName << "," << NSec.SectName
196                << " is a debug section: No graph section will be created.\n";
197       });
198 
199     IndexToSection.insert(std::make_pair(SecIndex, std::move(NSec)));
200   }
201 
202   std::vector<NormalizedSection *> Sections;
203   Sections.reserve(IndexToSection.size());
204   for (auto &KV : IndexToSection)
205     Sections.push_back(&KV.second);
206 
207   // If we didn't end up creating any sections then bail out. The code below
208   // assumes that we have at least one section.
209   if (Sections.empty())
210     return Error::success();
211 
212   llvm::sort(Sections,
213              [](const NormalizedSection *LHS, const NormalizedSection *RHS) {
214                assert(LHS && RHS && "Null section?");
215                if (LHS->Address != RHS->Address)
216                  return LHS->Address < RHS->Address;
217                return LHS->Size < RHS->Size;
218              });
219 
220   for (unsigned I = 0, E = Sections.size() - 1; I != E; ++I) {
221     auto &Cur = *Sections[I];
222     auto &Next = *Sections[I + 1];
223     if (Next.Address < Cur.Address + Cur.Size)
224       return make_error<JITLinkError>(
225           "Address range for section " +
226           formatv("\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Cur.SegName,
227                   Cur.SectName, Cur.Address, Cur.Address + Cur.Size) +
228           "overlaps section \"" + Next.SegName + "/" + Next.SectName + "\"" +
229           formatv("\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Next.SegName,
230                   Next.SectName, Next.Address, Next.Address + Next.Size));
231   }
232 
233   return Error::success();
234 }
235 
236 Error MachOLinkGraphBuilder::createNormalizedSymbols() {
237   LLVM_DEBUG(dbgs() << "Creating normalized symbols...\n");
238 
239   for (auto &SymRef : Obj.symbols()) {
240 
241     unsigned SymbolIndex = Obj.getSymbolIndex(SymRef.getRawDataRefImpl());
242     uint64_t Value;
243     uint32_t NStrX;
244     uint8_t Type;
245     uint8_t Sect;
246     uint16_t Desc;
247 
248     if (Obj.is64Bit()) {
249       const MachO::nlist_64 &NL64 =
250           Obj.getSymbol64TableEntry(SymRef.getRawDataRefImpl());
251       Value = NL64.n_value;
252       NStrX = NL64.n_strx;
253       Type = NL64.n_type;
254       Sect = NL64.n_sect;
255       Desc = NL64.n_desc;
256     } else {
257       const MachO::nlist &NL32 =
258           Obj.getSymbolTableEntry(SymRef.getRawDataRefImpl());
259       Value = NL32.n_value;
260       NStrX = NL32.n_strx;
261       Type = NL32.n_type;
262       Sect = NL32.n_sect;
263       Desc = NL32.n_desc;
264     }
265 
266     // Skip stabs.
267     // FIXME: Are there other symbols we should be skipping?
268     if (Type & MachO::N_STAB)
269       continue;
270 
271     Optional<StringRef> Name;
272     if (NStrX) {
273       if (auto NameOrErr = SymRef.getName())
274         Name = *NameOrErr;
275       else
276         return NameOrErr.takeError();
277     }
278 
279     LLVM_DEBUG({
280       dbgs() << "  ";
281       if (!Name)
282         dbgs() << "<anonymous symbol>";
283       else
284         dbgs() << *Name;
285       dbgs() << ": value = " << formatv("{0:x16}", Value)
286              << ", type = " << formatv("{0:x2}", Type)
287              << ", desc = " << formatv("{0:x4}", Desc) << ", sect = ";
288       if (Sect)
289         dbgs() << static_cast<unsigned>(Sect - 1);
290       else
291         dbgs() << "none";
292       dbgs() << "\n";
293     });
294 
295     // If this symbol has a section, sanity check that the addresses line up.
296     if (Sect != 0) {
297       auto NSec = findSectionByIndex(Sect - 1);
298       if (!NSec)
299         return NSec.takeError();
300 
301       if (Value < NSec->Address || Value > NSec->Address + NSec->Size)
302         return make_error<JITLinkError>("Symbol address does not fall within "
303                                         "section");
304 
305       if (!NSec->GraphSection) {
306         LLVM_DEBUG({
307           dbgs() << "  Skipping: Symbol is in section " << NSec->SegName << "/"
308                  << NSec->SectName
309                  << " which has no associated graph section.\n";
310         });
311         continue;
312       }
313     }
314 
315     IndexToSymbol[SymbolIndex] =
316         &createNormalizedSymbol(*Name, Value, Type, Sect, Desc,
317                                 getLinkage(Desc), getScope(*Name, Type));
318   }
319 
320   return Error::success();
321 }
322 
323 void MachOLinkGraphBuilder::addSectionStartSymAndBlock(
324     Section &GraphSec, uint64_t Address, const char *Data, uint64_t Size,
325     uint32_t Alignment, bool IsLive) {
326   Block &B =
327       Data ? G->createContentBlock(GraphSec, ArrayRef<char>(Data, Size),
328                                    Address, Alignment, 0)
329            : G->createZeroFillBlock(GraphSec, Size, Address, Alignment, 0);
330   auto &Sym = G->addAnonymousSymbol(B, 0, Size, false, IsLive);
331   assert(!AddrToCanonicalSymbol.count(Sym.getAddress()) &&
332          "Anonymous block start symbol clashes with existing symbol address");
333   AddrToCanonicalSymbol[Sym.getAddress()] = &Sym;
334 }
335 
336 Error MachOLinkGraphBuilder::graphifyRegularSymbols() {
337 
338   LLVM_DEBUG(dbgs() << "Creating graph symbols...\n");
339 
340   /// We only have 256 section indexes: Use a vector rather than a map.
341   std::vector<std::vector<NormalizedSymbol *>> SecIndexToSymbols;
342   SecIndexToSymbols.resize(256);
343 
344   // Create commons, externs, and absolutes, and partition all other symbols by
345   // section.
346   for (auto &KV : IndexToSymbol) {
347     auto &NSym = *KV.second;
348 
349     switch (NSym.Type & MachO::N_TYPE) {
350     case MachO::N_UNDF:
351       if (NSym.Value) {
352         if (!NSym.Name)
353           return make_error<JITLinkError>("Anonymous common symbol at index " +
354                                           Twine(KV.first));
355         NSym.GraphSymbol = &G->addCommonSymbol(
356             *NSym.Name, NSym.S, getCommonSection(), 0, NSym.Value,
357             1ull << MachO::GET_COMM_ALIGN(NSym.Desc),
358             NSym.Desc & MachO::N_NO_DEAD_STRIP);
359       } else {
360         if (!NSym.Name)
361           return make_error<JITLinkError>("Anonymous external symbol at "
362                                           "index " +
363                                           Twine(KV.first));
364         NSym.GraphSymbol = &G->addExternalSymbol(
365             *NSym.Name, 0,
366             NSym.Desc & MachO::N_WEAK_REF ? Linkage::Weak : Linkage::Strong);
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, NSym.Value, 0, Linkage::Strong, Scope::Default,
375           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(*NSec.GraphSection, NSec.Address, NSec.Data,
448                                    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 (SecNSymStack.back()->Value != NSec.Address) {
479       auto AnonBlockSize = SecNSymStack.back()->Value - NSec.Address;
480       LLVM_DEBUG({
481         dbgs() << "    Section start not covered by symbol. "
482                << "Creating anonymous block to cover [ "
483                << formatv("{0:x16}", NSec.Address) << " -- "
484                << formatv("{0:x16}", NSec.Address + AnonBlockSize) << " ]\n";
485       });
486       addSectionStartSymAndBlock(*NSec.GraphSection, NSec.Address, NSec.Data,
487                                  AnonBlockSize, NSec.Alignment,
488                                  SectionIsNoDeadStrip);
489     }
490 
491     // Visit section symbols in order by popping off the reverse-sorted stack,
492     // building blocks for each alt-entry chain and creating symbols as we go.
493     while (!SecNSymStack.empty()) {
494       SmallVector<NormalizedSymbol *, 8> BlockSyms;
495 
496       BlockSyms.push_back(SecNSymStack.back());
497       SecNSymStack.pop_back();
498       while (!SecNSymStack.empty() &&
499              (isAltEntry(*SecNSymStack.back()) ||
500               SecNSymStack.back()->Value == BlockSyms.back()->Value)) {
501         BlockSyms.push_back(SecNSymStack.back());
502         SecNSymStack.pop_back();
503       }
504 
505       // BlockNSyms now contains the block symbols in reverse canonical order.
506       JITTargetAddress BlockStart = BlockSyms.front()->Value;
507       JITTargetAddress BlockEnd = SecNSymStack.empty()
508                                       ? NSec.Address + NSec.Size
509                                       : SecNSymStack.back()->Value;
510       JITTargetAddress BlockOffset = BlockStart - NSec.Address;
511       JITTargetAddress BlockSize = BlockEnd - BlockStart;
512 
513       LLVM_DEBUG({
514         dbgs() << "    Creating block for " << formatv("{0:x16}", BlockStart)
515                << " -- " << formatv("{0:x16}", BlockEnd) << ": "
516                << NSec.GraphSection->getName() << " + "
517                << formatv("{0:x16}", BlockOffset) << " with "
518                << BlockSyms.size() << " symbol(s)...\n";
519       });
520 
521       Block &B =
522           NSec.Data
523               ? G->createContentBlock(
524                     *NSec.GraphSection,
525                     ArrayRef<char>(NSec.Data + BlockOffset, BlockSize),
526                     BlockStart, NSec.Alignment, BlockStart % NSec.Alignment)
527               : G->createZeroFillBlock(*NSec.GraphSection, BlockSize,
528                                        BlockStart, NSec.Alignment,
529                                        BlockStart % NSec.Alignment);
530 
531       Optional<JITTargetAddress> LastCanonicalAddr;
532       JITTargetAddress SymEnd = BlockEnd;
533       while (!BlockSyms.empty()) {
534         auto &NSym = *BlockSyms.back();
535         BlockSyms.pop_back();
536 
537         bool SymLive =
538             (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip;
539 
540         auto &Sym = createStandardGraphSymbol(NSym, B, SymEnd - NSym.Value,
541                                               SectionIsText, SymLive,
542                                               LastCanonicalAddr != NSym.Value);
543 
544         if (LastCanonicalAddr != Sym.getAddress()) {
545           if (LastCanonicalAddr)
546             SymEnd = *LastCanonicalAddr;
547           LastCanonicalAddr = Sym.getAddress();
548         }
549       }
550     }
551   }
552 
553   return Error::success();
554 }
555 
556 Symbol &MachOLinkGraphBuilder::createStandardGraphSymbol(NormalizedSymbol &NSym,
557                                                          Block &B, size_t Size,
558                                                          bool IsText,
559                                                          bool IsNoDeadStrip,
560                                                          bool IsCanonical) {
561 
562   LLVM_DEBUG({
563     dbgs() << "      " << formatv("{0:x16}", NSym.Value) << " -- "
564            << formatv("{0:x16}", NSym.Value + Size) << ": ";
565     if (!NSym.Name)
566       dbgs() << "<anonymous symbol>";
567     else
568       dbgs() << NSym.Name;
569     if (IsText)
570       dbgs() << " [text]";
571     if (IsNoDeadStrip)
572       dbgs() << " [no-dead-strip]";
573     if (!IsCanonical)
574       dbgs() << " [non-canonical]";
575     dbgs() << "\n";
576   });
577 
578   auto &Sym = NSym.Name ? G->addDefinedSymbol(B, NSym.Value - B.getAddress(),
579                                               *NSym.Name, Size, NSym.L, NSym.S,
580                                               IsText, IsNoDeadStrip)
581                         : G->addAnonymousSymbol(B, NSym.Value - B.getAddress(),
582                                                 Size, IsText, IsNoDeadStrip);
583   NSym.GraphSymbol = &Sym;
584 
585   if (IsCanonical)
586     setCanonicalSymbol(Sym);
587 
588   return Sym;
589 }
590 
591 Error MachOLinkGraphBuilder::graphifySectionsWithCustomParsers() {
592   // Graphify special sections.
593   for (auto &KV : IndexToSection) {
594     auto &NSec = KV.second;
595 
596     // Skip non-graph sections.
597     if (!NSec.GraphSection)
598       continue;
599 
600     auto HI = CustomSectionParserFunctions.find(NSec.GraphSection->getName());
601     if (HI != CustomSectionParserFunctions.end()) {
602       auto &Parse = HI->second;
603       if (auto Err = Parse(NSec))
604         return Err;
605     }
606   }
607 
608   return Error::success();
609 }
610 
611 Error MachOLinkGraphBuilder::graphifyCStringSection(
612     NormalizedSection &NSec, std::vector<NormalizedSymbol *> NSyms) {
613 
614   assert(NSec.GraphSection && "C string literal section missing graph section");
615   assert(NSec.Data && "C string literal section has no data");
616 
617   LLVM_DEBUG({
618     dbgs() << "  Graphifying C-string literal section "
619            << NSec.GraphSection->getName() << "\n";
620   });
621 
622   if (NSec.Data[NSec.Size - 1] != '\0')
623     return make_error<JITLinkError>("C string literal section " +
624                                     NSec.GraphSection->getName() +
625                                     " does not end with null terminator");
626 
627   /// Sort into reverse order to use as a stack.
628   llvm::sort(NSyms,
629              [](const NormalizedSymbol *LHS, const NormalizedSymbol *RHS) {
630                if (LHS->Value != RHS->Value)
631                  return LHS->Value > RHS->Value;
632                if (LHS->L != RHS->L)
633                  return LHS->L > RHS->L;
634                if (LHS->S != RHS->S)
635                  return LHS->S > RHS->S;
636                if (RHS->Name) {
637                  if (!LHS->Name)
638                    return true;
639                  return *LHS->Name > *RHS->Name;
640                }
641                return false;
642              });
643 
644   bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP;
645   bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
646   JITTargetAddress BlockStart = 0;
647 
648   // Scan section for null characters.
649   for (size_t I = 0; I != NSec.Size; ++I)
650     if (NSec.Data[I] == '\0') {
651       JITTargetAddress BlockEnd = I + 1;
652       size_t BlockSize = BlockEnd - BlockStart;
653       // Create a block for this null terminated string.
654       auto &B = G->createContentBlock(*NSec.GraphSection,
655                                       {NSec.Data + BlockStart, BlockSize},
656                                       NSec.Address + BlockStart, 1, 0);
657 
658       LLVM_DEBUG({
659         dbgs() << "    Created block " << formatv("{0:x}", B.getAddress())
660                << " -- " << formatv("{0:x}", B.getAddress() + B.getSize())
661                << " for \"" << StringRef(B.getContent().data()) << "\"\n";
662       });
663 
664       // If there's no symbol at the start of this block then create one.
665       if (NSyms.empty() || NSyms.back()->Value != B.getAddress()) {
666         auto &S = G->addAnonymousSymbol(B, 0, BlockSize, false, false);
667         setCanonicalSymbol(S);
668         LLVM_DEBUG({
669           dbgs() << "      Adding anonymous symbol for c-string block "
670                  << formatv("{0:x16} -- {1:x16}", S.getAddress(),
671                             S.getAddress() + BlockSize)
672                  << "\n";
673         });
674       }
675 
676       // Process any remaining symbols that point into this block.
677       JITTargetAddress LastCanonicalAddr = B.getAddress() + BlockEnd;
678       while (!NSyms.empty() &&
679              NSyms.back()->Value < (B.getAddress() + BlockSize)) {
680         auto &NSym = *NSyms.back();
681         size_t SymSize = (B.getAddress() + BlockSize) - NSyms.back()->Value;
682         bool SymLive =
683             (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip;
684 
685         bool IsCanonical = false;
686         if (LastCanonicalAddr != NSym.Value) {
687           IsCanonical = true;
688           LastCanonicalAddr = NSym.Value;
689         }
690 
691         createStandardGraphSymbol(NSym, B, SymSize, SectionIsText, SymLive,
692                                   IsCanonical);
693 
694         NSyms.pop_back();
695       }
696 
697       BlockStart += BlockSize;
698     }
699 
700   return Error::success();
701 }
702 
703 } // end namespace jitlink
704 } // end namespace llvm
705