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 building code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MachOLinkGraphBuilder.h"
14#include "llvm/ADT/STLExtras.h"
15#include <optional>
16
17#define DEBUG_TYPE "jitlink"
18
19static const char *CommonSectionName = "__common";
20
21namespace llvm {
22namespace jitlink {
23
24MachOLinkGraphBuilder::~MachOLinkGraphBuilder() = default;
25
26Expected<std::unique_ptr<LinkGraph>> MachOLinkGraphBuilder::buildGraph() {
27
28 // We only operate on relocatable objects.
29 if (!Obj.isRelocatableObject())
30 return make_error<JITLinkError>(Args: "Object is not a relocatable MachO");
31
32 if (auto Err = createNormalizedSections())
33 return std::move(Err);
34
35 if (auto Err = createNormalizedSymbols())
36 return std::move(Err);
37
38 if (auto Err = graphifyRegularSymbols())
39 return std::move(Err);
40
41 if (auto Err = graphifySectionsWithCustomParsers())
42 return std::move(Err);
43
44 if (auto Err = addRelocations())
45 return std::move(Err);
46
47 return std::move(G);
48}
49
50MachOLinkGraphBuilder::MachOLinkGraphBuilder(
51 const object::MachOObjectFile &Obj,
52 std::shared_ptr<orc::SymbolStringPool> SSP, Triple TT,
53 SubtargetFeatures Features,
54 LinkGraph::GetEdgeKindNameFunction GetEdgeKindName)
55 : Obj(Obj),
56 G(std::make_unique<LinkGraph>(
57 args: std::string(Obj.getFileName()), args: std::move(SSP), args: std::move(TT),
58 args: std::move(Features), args: std::move(GetEdgeKindName))) {
59 auto &MachHeader = Obj.getHeader64();
60 SubsectionsViaSymbols = MachHeader.flags & MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
61}
62
63void MachOLinkGraphBuilder::addCustomSectionParser(
64 StringRef SectionName, SectionParserFunction Parser) {
65 assert(!CustomSectionParserFunctions.count(SectionName) &&
66 "Custom parser for this section already exists");
67 CustomSectionParserFunctions[SectionName] = std::move(Parser);
68}
69
70Linkage MachOLinkGraphBuilder::getLinkage(uint16_t Desc) {
71 if ((Desc & MachO::N_WEAK_DEF) || (Desc & MachO::N_WEAK_REF))
72 return Linkage::Weak;
73 return Linkage::Strong;
74}
75
76Scope MachOLinkGraphBuilder::getScope(StringRef Name, uint8_t Type) {
77 if (Type & MachO::N_EXT) {
78 if ((Type & MachO::N_PEXT) || Name.starts_with(Prefix: "l"))
79 return Scope::Hidden;
80 else
81 return Scope::Default;
82 }
83 return Scope::Local;
84}
85
86bool MachOLinkGraphBuilder::isAltEntry(const NormalizedSymbol &NSym) {
87 return NSym.Desc & MachO::N_ALT_ENTRY;
88}
89
90bool MachOLinkGraphBuilder::isDebugSection(const NormalizedSection &NSec) {
91 return (NSec.Flags & MachO::S_ATTR_DEBUG &&
92 strcmp(s1: NSec.SegName, s2: "__DWARF") == 0);
93}
94
95bool MachOLinkGraphBuilder::isZeroFillSection(const NormalizedSection &NSec) {
96 switch (NSec.Flags & MachO::SECTION_TYPE) {
97 case MachO::S_ZEROFILL:
98 case MachO::S_GB_ZEROFILL:
99 case MachO::S_THREAD_LOCAL_ZEROFILL:
100 return true;
101 default:
102 return false;
103 }
104}
105
106Section &MachOLinkGraphBuilder::getCommonSection() {
107 if (!CommonSection)
108 CommonSection = &G->createSection(Name: CommonSectionName,
109 Prot: orc::MemProt::Read | orc::MemProt::Write);
110 return *CommonSection;
111}
112
113Error MachOLinkGraphBuilder::createNormalizedSections() {
114 // Build normalized sections. Verifies that section data is in-range (for
115 // sections with content) and that address ranges are non-overlapping.
116
117 LLVM_DEBUG(dbgs() << "Creating normalized sections...\n");
118
119 for (auto &SecRef : Obj.sections()) {
120 NormalizedSection NSec;
121 uint32_t DataOffset = 0;
122
123 auto SecIndex = Obj.getSectionIndex(Sec: SecRef.getRawDataRefImpl());
124
125 if (Obj.is64Bit()) {
126 const MachO::section_64 &Sec64 =
127 Obj.getSection64(DRI: SecRef.getRawDataRefImpl());
128
129 memcpy(dest: &NSec.SectName, src: &Sec64.sectname, n: 16);
130 NSec.SectName[16] = '\0';
131 memcpy(dest: &NSec.SegName, src: Sec64.segname, n: 16);
132 NSec.SegName[16] = '\0';
133
134 NSec.Address = orc::ExecutorAddr(Sec64.addr);
135 NSec.Size = Sec64.size;
136 NSec.Alignment = 1ULL << Sec64.align;
137 NSec.Flags = Sec64.flags;
138 DataOffset = Sec64.offset;
139 } else {
140 const MachO::section &Sec32 = Obj.getSection(DRI: SecRef.getRawDataRefImpl());
141
142 memcpy(dest: &NSec.SectName, src: &Sec32.sectname, n: 16);
143 NSec.SectName[16] = '\0';
144 memcpy(dest: &NSec.SegName, src: Sec32.segname, n: 16);
145 NSec.SegName[16] = '\0';
146
147 NSec.Address = orc::ExecutorAddr(Sec32.addr);
148 NSec.Size = Sec32.size;
149 NSec.Alignment = 1ULL << Sec32.align;
150 NSec.Flags = Sec32.flags;
151 DataOffset = Sec32.offset;
152 }
153
154 LLVM_DEBUG({
155 dbgs() << " " << NSec.SegName << "," << NSec.SectName << ": "
156 << formatv("{0:x16}", NSec.Address) << " -- "
157 << formatv("{0:x16}", NSec.Address + NSec.Size)
158 << ", align: " << NSec.Alignment << ", index: " << SecIndex
159 << "\n";
160 });
161
162 // Get the section data if any.
163 if (!isZeroFillSection(NSec)) {
164 if (DataOffset + NSec.Size > Obj.getData().size())
165 return make_error<JITLinkError>(
166 Args: "Section data extends past end of file");
167
168 NSec.Data = Obj.getData().data() + DataOffset;
169 }
170
171 // Get prot flags.
172 // FIXME: Make sure this test is correct (it's probably missing cases
173 // as-is).
174 orc::MemProt Prot;
175 if (NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS)
176 Prot = orc::MemProt::Read | orc::MemProt::Exec;
177 else
178 Prot = orc::MemProt::Read | orc::MemProt::Write;
179
180 auto FullyQualifiedName =
181 G->allocateContent(Source: StringRef(NSec.SegName) + "," + NSec.SectName);
182 NSec.GraphSection = &G->createSection(
183 Name: StringRef(FullyQualifiedName.data(), FullyQualifiedName.size()), Prot);
184
185 // TODO: Are there any other criteria for NoAlloc lifetime?
186 if (NSec.Flags & MachO::S_ATTR_DEBUG)
187 NSec.GraphSection->setMemLifetime(orc::MemLifetime::NoAlloc);
188
189 IndexToSection.insert(KV: std::make_pair(x&: SecIndex, y: std::move(NSec)));
190 }
191
192 std::vector<NormalizedSection *> Sections;
193 Sections.reserve(n: IndexToSection.size());
194 for (auto &KV : IndexToSection)
195 Sections.push_back(x: &KV.second);
196
197 // If we didn't end up creating any sections then bail out. The code below
198 // assumes that we have at least one section.
199 if (Sections.empty())
200 return Error::success();
201
202 llvm::sort(C&: Sections,
203 Comp: [](const NormalizedSection *LHS, const NormalizedSection *RHS) {
204 assert(LHS && RHS && "Null section?");
205 return std::tie(args: LHS->Address, args: LHS->Size) <
206 std::tie(args: RHS->Address, args: 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 Args: "Address range for section " +
215 formatv(Fmt: "\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Vals&: Cur.SegName,
216 Vals&: Cur.SectName, Vals&: Cur.Address, Vals: Cur.Address + Cur.Size) +
217 "overlaps section \"" + Next.SegName + "/" + Next.SectName + "\"" +
218 formatv(Fmt: "\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Vals&: Next.SegName,
219 Vals&: Next.SectName, Vals&: Next.Address, Vals: Next.Address + Next.Size));
220 }
221
222 return Error::success();
223}
224
225Error MachOLinkGraphBuilder::createNormalizedSymbols() {
226 LLVM_DEBUG(dbgs() << "Creating normalized symbols...\n");
227
228 for (auto &SymRef : Obj.symbols()) {
229
230 unsigned SymbolIndex = Obj.getSymbolIndex(Symb: 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(DRI: 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(DRI: 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 std::optional<StringRef> Name;
261 if (NStrX) {
262 if (auto NameOrErr = SymRef.getName())
263 Name = *NameOrErr;
264 else
265 return NameOrErr.takeError();
266 } else if (Type & MachO::N_EXT)
267 return make_error<JITLinkError>(Args: "Symbol at index " +
268 formatv(Fmt: "{0}", Vals&: SymbolIndex) +
269 " has no name (string table index 0), "
270 "but N_EXT bit is set");
271
272 LLVM_DEBUG({
273 dbgs() << " ";
274 if (!Name)
275 dbgs() << "<anonymous symbol>";
276 else
277 dbgs() << *Name;
278 dbgs() << ": value = " << formatv("{0:x16}", Value)
279 << ", type = " << formatv("{0:x2}", Type)
280 << ", desc = " << formatv("{0:x4}", Desc) << ", sect = ";
281 if (Sect)
282 dbgs() << static_cast<unsigned>(Sect - 1);
283 else
284 dbgs() << "none";
285 dbgs() << "\n";
286 });
287
288 // If this symbol has a section, verify that the addresses line up.
289 if (Sect != 0) {
290 auto NSec = findSectionByIndex(Index: Sect - 1);
291 if (!NSec)
292 return NSec.takeError();
293
294 if (orc::ExecutorAddr(Value) < NSec->Address ||
295 orc::ExecutorAddr(Value) > NSec->Address + NSec->Size)
296 return make_error<JITLinkError>(Args: "Address " + formatv(Fmt: "{0:x}", Vals&: Value) +
297 " for symbol " + *Name +
298 " does not fall within section");
299
300 if (!NSec->GraphSection) {
301 LLVM_DEBUG({
302 dbgs() << " Skipping: Symbol is in section " << NSec->SegName << "/"
303 << NSec->SectName
304 << " which has no associated graph section.\n";
305 });
306 continue;
307 }
308 }
309
310 IndexToSymbol[SymbolIndex] = &createNormalizedSymbol(
311 Args&: Name, Args&: Value, Args&: Type, Args&: Sect, Args&: Desc, Args: getLinkage(Desc), Args: getScope(Name: *Name, Type));
312 }
313
314 return Error::success();
315}
316
317void 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(Parent&: GraphSec, Content: ArrayRef<char>(Data, Size),
323 Address, Alignment, AlignmentOffset: 0)
324 : G->createZeroFillBlock(Parent&: GraphSec, Size, Address, Alignment, AlignmentOffset: 0);
325 auto &Sym = G->addAnonymousSymbol(Content&: B, Offset: 0, Size, IsCallable: false, IsLive);
326 auto SecI = IndexToSection.find(Val: 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
334Error 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(new_size: 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>(Args: "Anonymous common symbol at index " +
352 Twine(KV.first));
353 NSym.GraphSymbol = &G->addDefinedSymbol(
354 Content&: G->createZeroFillBlock(Parent&: getCommonSection(),
355 Size: orc::ExecutorAddrDiff(NSym.Value),
356 Address: orc::ExecutorAddr(),
357 Alignment: 1ull << MachO::GET_COMM_ALIGN(n_desc: NSym.Desc), AlignmentOffset: 0),
358 Offset: 0, Name: *NSym.Name, Size: orc::ExecutorAddrDiff(NSym.Value), L: Linkage::Weak,
359 S: NSym.S, IsCallable: false, IsLive: NSym.Desc & MachO::N_NO_DEAD_STRIP);
360 } else {
361 if (!NSym.Name)
362 return make_error<JITLinkError>(Args: "Anonymous external symbol at "
363 "index " +
364 Twine(KV.first));
365 NSym.GraphSymbol = &G->addExternalSymbol(
366 Name: *NSym.Name, Size: 0, IsWeaklyReferenced: (NSym.Desc & MachO::N_WEAK_REF) != 0);
367 }
368 break;
369 case MachO::N_ABS:
370 if (!NSym.Name)
371 return make_error<JITLinkError>(Args: "Anonymous absolute symbol at index " +
372 Twine(KV.first));
373 NSym.GraphSymbol = &G->addAbsoluteSymbol(
374 Name: *NSym.Name, Address: orc::ExecutorAddr(NSym.Value), Size: 0, L: Linkage::Strong,
375 S: getScope(Name: *NSym.Name, Type: NSym.Type), IsLive: NSym.Desc & MachO::N_NO_DEAD_STRIP);
376 break;
377 case MachO::N_SECT:
378 SecIndexToSymbols[NSym.Sect - 1].push_back(x: &NSym);
379 break;
380 case MachO::N_PBUD:
381 return make_error<JITLinkError>(
382 Args: "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 Args: "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 Args: "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(Key: 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, NSyms: 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, GraphSec&: *NSec.GraphSection, Address: NSec.Address,
448 Data: NSec.Data, Size: NSec.Size, Alignment: NSec.Alignment,
449 IsLive: 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(C&: SecNSymStack, Comp: [](const NormalizedSymbol *LHS,
461 const NormalizedSymbol *RHS) {
462 if (LHS->Value != RHS->Value)
463 return LHS->Value > RHS->Value;
464 if (isAltEntry(NSym: *LHS) != isAltEntry(NSym: *RHS))
465 return isAltEntry(NSym: *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(NSym: *SecNSymStack.back()))
473 return make_error<JITLinkError>(
474 Args: "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, GraphSec&: *NSec.GraphSection, Address: NSec.Address,
487 Data: NSec.Data, Size: AnonBlockSize, Alignment: NSec.Alignment,
488 IsLive: 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(Elt: SecNSymStack.back());
505 SecNSymStack.pop_back();
506 while (!SecNSymStack.empty() &&
507 (isAltEntry(NSym: *SecNSymStack.back()) ||
508 SecNSymStack.back()->Value == BlockSyms.back()->Value ||
509 !SubsectionsViaSymbols)) {
510 BlockSyms.push_back(Elt: 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 Parent&: *NSec.GraphSection,
534 Content: ArrayRef<char>(NSec.Data + BlockOffset, BlockSize),
535 Address: BlockStart, Alignment: NSec.Alignment, AlignmentOffset: BlockStart % NSec.Alignment)
536 : G->createZeroFillBlock(Parent&: *NSec.GraphSection, Size: BlockSize,
537 Address: BlockStart, Alignment: NSec.Alignment,
538 AlignmentOffset: BlockStart % NSec.Alignment);
539
540 std::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 Sym&: NSym, B, Size: SymEnd - orc::ExecutorAddr(NSym.Value), IsText: SectionIsText,
551 IsNoDeadStrip: SymLive, IsCanonical: 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
565Symbol &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(Content&: B, Offset: SymOffset, Name: *NSym.Name, Size, L: NSym.L, S: NSym.S,
591 IsCallable: IsText, IsLive: IsNoDeadStrip)
592 : G->addAnonymousSymbol(Content&: B, Offset: SymOffset, Size, IsCallable: IsText, IsLive: IsNoDeadStrip);
593 NSym.GraphSymbol = &Sym;
594
595 if (IsCanonical)
596 setCanonicalSymbol(NSec&: getSectionByIndex(Index: NSym.Sect - 1), Sym);
597
598 return Sym;
599}
600
601Error 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(Key: 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
621Error 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>(Args: "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(C&: NSyms,
638 Comp: [](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(Parent&: *NSec.GraphSection,
663 Content: {NSec.Data + BlockStart, BlockSize},
664 Address: NSec.Address + BlockStart, Alignment: NSec.Alignment,
665 AlignmentOffset: 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(Content&: B, Offset: 0, Size: BlockSize, IsCallable: false, IsLive: false);
687 setCanonicalSymbol(NSec, Sym&: 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, Size: SymSize, IsText: SectionIsText,
711 IsNoDeadStrip: 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
727 assert(llvm::all_of(NSec.GraphSection->blocks(),
728 [](Block *B) { return isCStringBlock(*B); }) &&
729 "All blocks in section should hold single c-strings");
730
731 return Error::success();
732}
733
734} // end namespace jitlink
735} // end namespace llvm
736