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 "llvm/ADT/Sequence.h" |
16 | #include <optional> |
17 | |
18 | #define DEBUG_TYPE "jitlink" |
19 | |
20 | static const char *CommonSectionName = "__common" ; |
21 | |
22 | namespace llvm { |
23 | namespace jitlink { |
24 | |
25 | MachOLinkGraphBuilder::~MachOLinkGraphBuilder() = default; |
26 | |
27 | Expected<std::unique_ptr<LinkGraph>> MachOLinkGraphBuilder::buildGraph() { |
28 | |
29 | // We only operate on relocatable objects. |
30 | if (!Obj.isRelocatableObject()) |
31 | return make_error<JITLinkError>(Args: "Object is not a relocatable MachO" ); |
32 | |
33 | if (auto Err = createNormalizedSections()) |
34 | return std::move(Err); |
35 | |
36 | if (auto Err = createNormalizedSymbols()) |
37 | return std::move(Err); |
38 | |
39 | if (auto Err = graphifyRegularSymbols()) |
40 | return std::move(Err); |
41 | |
42 | if (auto Err = graphifySectionsWithCustomParsers()) |
43 | return std::move(Err); |
44 | |
45 | if (auto Err = addRelocations()) |
46 | return std::move(Err); |
47 | |
48 | return std::move(G); |
49 | } |
50 | |
51 | MachOLinkGraphBuilder::MachOLinkGraphBuilder( |
52 | const object::MachOObjectFile &Obj, |
53 | std::shared_ptr<orc::SymbolStringPool> SSP, Triple TT, |
54 | SubtargetFeatures Features, |
55 | LinkGraph::GetEdgeKindNameFunction GetEdgeKindName) |
56 | : Obj(Obj), |
57 | G(std::make_unique<LinkGraph>( |
58 | args: std::string(Obj.getFileName()), args: std::move(SSP), args: std::move(TT), |
59 | args: std::move(Features), args: std::move(GetEdgeKindName))) { |
60 | auto & = Obj.getHeader64(); |
61 | SubsectionsViaSymbols = MachHeader.flags & MachO::MH_SUBSECTIONS_VIA_SYMBOLS; |
62 | } |
63 | |
64 | void MachOLinkGraphBuilder::addCustomSectionParser( |
65 | StringRef SectionName, SectionParserFunction Parser) { |
66 | assert(!CustomSectionParserFunctions.count(SectionName) && |
67 | "Custom parser for this section already exists" ); |
68 | CustomSectionParserFunctions[SectionName] = std::move(Parser); |
69 | } |
70 | |
71 | Linkage MachOLinkGraphBuilder::getLinkage(uint16_t Desc) { |
72 | if ((Desc & MachO::N_WEAK_DEF) || (Desc & MachO::N_WEAK_REF)) |
73 | return Linkage::Weak; |
74 | return Linkage::Strong; |
75 | } |
76 | |
77 | Scope MachOLinkGraphBuilder::getScope(StringRef Name, uint8_t Type) { |
78 | if (Type & MachO::N_EXT) { |
79 | if ((Type & MachO::N_PEXT) || Name.starts_with(Prefix: "l" )) |
80 | return Scope::Hidden; |
81 | else |
82 | return Scope::Default; |
83 | } |
84 | return Scope::Local; |
85 | } |
86 | |
87 | bool MachOLinkGraphBuilder::isAltEntry(const NormalizedSymbol &NSym) { |
88 | return NSym.Desc & MachO::N_ALT_ENTRY; |
89 | } |
90 | |
91 | bool MachOLinkGraphBuilder::isDebugSection(const NormalizedSection &NSec) { |
92 | return (NSec.Flags & MachO::S_ATTR_DEBUG && |
93 | strcmp(s1: NSec.SegName, s2: "__DWARF" ) == 0); |
94 | } |
95 | |
96 | bool MachOLinkGraphBuilder::isZeroFillSection(const NormalizedSection &NSec) { |
97 | switch (NSec.Flags & MachO::SECTION_TYPE) { |
98 | case MachO::S_ZEROFILL: |
99 | case MachO::S_GB_ZEROFILL: |
100 | case MachO::S_THREAD_LOCAL_ZEROFILL: |
101 | return true; |
102 | default: |
103 | return false; |
104 | } |
105 | } |
106 | |
107 | Section &MachOLinkGraphBuilder::getCommonSection() { |
108 | if (!CommonSection) |
109 | CommonSection = &G->createSection(Name: CommonSectionName, |
110 | Prot: orc::MemProt::Read | orc::MemProt::Write); |
111 | return *CommonSection; |
112 | } |
113 | |
114 | Error MachOLinkGraphBuilder::createNormalizedSections() { |
115 | // Build normalized sections. Verifies that section data is in-range (for |
116 | // sections with content) and that address ranges are non-overlapping. |
117 | |
118 | LLVM_DEBUG(dbgs() << "Creating normalized sections...\n" ); |
119 | |
120 | for (auto &SecRef : Obj.sections()) { |
121 | NormalizedSection NSec; |
122 | uint32_t DataOffset = 0; |
123 | |
124 | auto SecIndex = Obj.getSectionIndex(Sec: SecRef.getRawDataRefImpl()); |
125 | |
126 | if (Obj.is64Bit()) { |
127 | const MachO::section_64 &Sec64 = |
128 | Obj.getSection64(DRI: SecRef.getRawDataRefImpl()); |
129 | |
130 | memcpy(dest: &NSec.SectName, src: &Sec64.sectname, n: 16); |
131 | NSec.SectName[16] = '\0'; |
132 | memcpy(dest: &NSec.SegName, src: Sec64.segname, n: 16); |
133 | NSec.SegName[16] = '\0'; |
134 | |
135 | NSec.Address = orc::ExecutorAddr(Sec64.addr); |
136 | NSec.Size = Sec64.size; |
137 | NSec.Alignment = 1ULL << Sec64.align; |
138 | NSec.Flags = Sec64.flags; |
139 | DataOffset = Sec64.offset; |
140 | } else { |
141 | const MachO::section &Sec32 = Obj.getSection(DRI: SecRef.getRawDataRefImpl()); |
142 | |
143 | memcpy(dest: &NSec.SectName, src: &Sec32.sectname, n: 16); |
144 | NSec.SectName[16] = '\0'; |
145 | memcpy(dest: &NSec.SegName, src: Sec32.segname, n: 16); |
146 | NSec.SegName[16] = '\0'; |
147 | |
148 | NSec.Address = orc::ExecutorAddr(Sec32.addr); |
149 | NSec.Size = Sec32.size; |
150 | NSec.Alignment = 1ULL << Sec32.align; |
151 | NSec.Flags = Sec32.flags; |
152 | DataOffset = Sec32.offset; |
153 | } |
154 | |
155 | LLVM_DEBUG({ |
156 | dbgs() << " " << NSec.SegName << "," << NSec.SectName << ": " |
157 | << formatv("{0:x16}" , NSec.Address) << " -- " |
158 | << formatv("{0:x16}" , NSec.Address + NSec.Size) |
159 | << ", align: " << NSec.Alignment << ", index: " << SecIndex |
160 | << "\n" ; |
161 | }); |
162 | |
163 | // Get the section data if any. |
164 | if (!isZeroFillSection(NSec)) { |
165 | if (DataOffset + NSec.Size > Obj.getData().size()) |
166 | return make_error<JITLinkError>( |
167 | Args: "Section data extends past end of file" ); |
168 | |
169 | NSec.Data = Obj.getData().data() + DataOffset; |
170 | } |
171 | |
172 | // Get prot flags. |
173 | // FIXME: Make sure this test is correct (it's probably missing cases |
174 | // as-is). |
175 | orc::MemProt Prot; |
176 | if (NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) |
177 | Prot = orc::MemProt::Read | orc::MemProt::Exec; |
178 | else |
179 | Prot = orc::MemProt::Read | orc::MemProt::Write; |
180 | |
181 | auto FullyQualifiedName = |
182 | G->allocateContent(Source: StringRef(NSec.SegName) + "," + NSec.SectName); |
183 | NSec.GraphSection = &G->createSection( |
184 | Name: StringRef(FullyQualifiedName.data(), FullyQualifiedName.size()), Prot); |
185 | |
186 | // TODO: Are there any other criteria for NoAlloc lifetime? |
187 | if (NSec.Flags & MachO::S_ATTR_DEBUG) |
188 | NSec.GraphSection->setMemLifetime(orc::MemLifetime::NoAlloc); |
189 | |
190 | IndexToSection.insert(KV: std::make_pair(x&: SecIndex, y: std::move(NSec))); |
191 | } |
192 | |
193 | std::vector<NormalizedSection *> Sections; |
194 | Sections.reserve(n: IndexToSection.size()); |
195 | for (auto &KV : IndexToSection) |
196 | Sections.push_back(x: &KV.second); |
197 | |
198 | // If we didn't end up creating any sections then bail out. The code below |
199 | // assumes that we have at least one section. |
200 | if (Sections.empty()) |
201 | return Error::success(); |
202 | |
203 | llvm::sort(C&: Sections, |
204 | Comp: [](const NormalizedSection *LHS, const NormalizedSection *RHS) { |
205 | assert(LHS && RHS && "Null section?" ); |
206 | if (LHS->Address != RHS->Address) |
207 | return LHS->Address < RHS->Address; |
208 | return LHS->Size < RHS->Size; |
209 | }); |
210 | |
211 | for (unsigned I = 0, E = Sections.size() - 1; I != E; ++I) { |
212 | auto &Cur = *Sections[I]; |
213 | auto &Next = *Sections[I + 1]; |
214 | if (Next.Address < Cur.Address + Cur.Size) |
215 | return make_error<JITLinkError>( |
216 | Args: "Address range for section " + |
217 | formatv(Fmt: "\"{0}/{1}\" [ {2:x16} -- {3:x16} ] " , Vals&: Cur.SegName, |
218 | Vals&: Cur.SectName, Vals&: Cur.Address, Vals: Cur.Address + Cur.Size) + |
219 | "overlaps section \"" + Next.SegName + "/" + Next.SectName + "\"" + |
220 | formatv(Fmt: "\"{0}/{1}\" [ {2:x16} -- {3:x16} ] " , Vals&: Next.SegName, |
221 | Vals&: Next.SectName, Vals&: Next.Address, Vals: Next.Address + Next.Size)); |
222 | } |
223 | |
224 | return Error::success(); |
225 | } |
226 | |
227 | Error MachOLinkGraphBuilder::createNormalizedSymbols() { |
228 | LLVM_DEBUG(dbgs() << "Creating normalized symbols...\n" ); |
229 | |
230 | for (auto &SymRef : Obj.symbols()) { |
231 | |
232 | unsigned SymbolIndex = Obj.getSymbolIndex(Symb: SymRef.getRawDataRefImpl()); |
233 | uint64_t Value; |
234 | uint32_t NStrX; |
235 | uint8_t Type; |
236 | uint8_t Sect; |
237 | uint16_t Desc; |
238 | |
239 | if (Obj.is64Bit()) { |
240 | const MachO::nlist_64 &NL64 = |
241 | Obj.getSymbol64TableEntry(DRI: SymRef.getRawDataRefImpl()); |
242 | Value = NL64.n_value; |
243 | NStrX = NL64.n_strx; |
244 | Type = NL64.n_type; |
245 | Sect = NL64.n_sect; |
246 | Desc = NL64.n_desc; |
247 | } else { |
248 | const MachO::nlist &NL32 = |
249 | Obj.getSymbolTableEntry(DRI: SymRef.getRawDataRefImpl()); |
250 | Value = NL32.n_value; |
251 | NStrX = NL32.n_strx; |
252 | Type = NL32.n_type; |
253 | Sect = NL32.n_sect; |
254 | Desc = NL32.n_desc; |
255 | } |
256 | |
257 | // Skip stabs. |
258 | // FIXME: Are there other symbols we should be skipping? |
259 | if (Type & MachO::N_STAB) |
260 | continue; |
261 | |
262 | std::optional<StringRef> Name; |
263 | if (NStrX) { |
264 | if (auto NameOrErr = SymRef.getName()) |
265 | Name = *NameOrErr; |
266 | else |
267 | return NameOrErr.takeError(); |
268 | } else if (Type & MachO::N_EXT) |
269 | return make_error<JITLinkError>(Args: "Symbol at index " + |
270 | formatv(Fmt: "{0}" , Vals&: SymbolIndex) + |
271 | " has no name (string table index 0), " |
272 | "but N_EXT bit is set" ); |
273 | |
274 | LLVM_DEBUG({ |
275 | dbgs() << " " ; |
276 | if (!Name) |
277 | dbgs() << "<anonymous symbol>" ; |
278 | else |
279 | dbgs() << *Name; |
280 | dbgs() << ": value = " << formatv("{0:x16}" , Value) |
281 | << ", type = " << formatv("{0:x2}" , Type) |
282 | << ", desc = " << formatv("{0:x4}" , Desc) << ", sect = " ; |
283 | if (Sect) |
284 | dbgs() << static_cast<unsigned>(Sect - 1); |
285 | else |
286 | dbgs() << "none" ; |
287 | dbgs() << "\n" ; |
288 | }); |
289 | |
290 | // If this symbol has a section, verify that the addresses line up. |
291 | if (Sect != 0) { |
292 | auto NSec = findSectionByIndex(Index: Sect - 1); |
293 | if (!NSec) |
294 | return NSec.takeError(); |
295 | |
296 | if (orc::ExecutorAddr(Value) < NSec->Address || |
297 | orc::ExecutorAddr(Value) > NSec->Address + NSec->Size) |
298 | return make_error<JITLinkError>(Args: "Address " + formatv(Fmt: "{0:x}" , Vals&: Value) + |
299 | " for symbol " + *Name + |
300 | " does not fall within section" ); |
301 | |
302 | if (!NSec->GraphSection) { |
303 | LLVM_DEBUG({ |
304 | dbgs() << " Skipping: Symbol is in section " << NSec->SegName << "/" |
305 | << NSec->SectName |
306 | << " which has no associated graph section.\n" ; |
307 | }); |
308 | continue; |
309 | } |
310 | } |
311 | |
312 | IndexToSymbol[SymbolIndex] = &createNormalizedSymbol( |
313 | Args&: Name, Args&: Value, Args&: Type, Args&: Sect, Args&: Desc, Args: getLinkage(Desc), Args: getScope(Name: *Name, Type)); |
314 | } |
315 | |
316 | return Error::success(); |
317 | } |
318 | |
319 | void MachOLinkGraphBuilder::addSectionStartSymAndBlock( |
320 | unsigned SecIndex, Section &GraphSec, orc::ExecutorAddr Address, |
321 | const char *Data, orc::ExecutorAddrDiff Size, uint32_t Alignment, |
322 | bool IsLive) { |
323 | Block &B = |
324 | Data ? G->createContentBlock(Parent&: GraphSec, Content: ArrayRef<char>(Data, Size), |
325 | Address, Alignment, AlignmentOffset: 0) |
326 | : G->createZeroFillBlock(Parent&: GraphSec, Size, Address, Alignment, AlignmentOffset: 0); |
327 | auto &Sym = G->addAnonymousSymbol(Content&: B, Offset: 0, Size, IsCallable: false, IsLive); |
328 | auto SecI = IndexToSection.find(Val: SecIndex); |
329 | assert(SecI != IndexToSection.end() && "SecIndex invalid" ); |
330 | auto &NSec = SecI->second; |
331 | assert(!NSec.CanonicalSymbols.count(Sym.getAddress()) && |
332 | "Anonymous block start symbol clashes with existing symbol address" ); |
333 | NSec.CanonicalSymbols[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(new_size: 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>(Args: "Anonymous common symbol at index " + |
354 | Twine(KV.first)); |
355 | NSym.GraphSymbol = &G->addDefinedSymbol( |
356 | Content&: G->createZeroFillBlock(Parent&: getCommonSection(), |
357 | Size: orc::ExecutorAddrDiff(NSym.Value), |
358 | Address: orc::ExecutorAddr(), |
359 | Alignment: 1ull << MachO::GET_COMM_ALIGN(n_desc: NSym.Desc), AlignmentOffset: 0), |
360 | Offset: 0, Name: *NSym.Name, Size: orc::ExecutorAddrDiff(NSym.Value), L: Linkage::Weak, |
361 | S: NSym.S, IsCallable: false, IsLive: NSym.Desc & MachO::N_NO_DEAD_STRIP); |
362 | } else { |
363 | if (!NSym.Name) |
364 | return make_error<JITLinkError>(Args: "Anonymous external symbol at " |
365 | "index " + |
366 | Twine(KV.first)); |
367 | NSym.GraphSymbol = &G->addExternalSymbol( |
368 | Name: *NSym.Name, Size: 0, IsWeaklyReferenced: (NSym.Desc & MachO::N_WEAK_REF) != 0); |
369 | } |
370 | break; |
371 | case MachO::N_ABS: |
372 | if (!NSym.Name) |
373 | return make_error<JITLinkError>(Args: "Anonymous absolute symbol at index " + |
374 | Twine(KV.first)); |
375 | NSym.GraphSymbol = &G->addAbsoluteSymbol( |
376 | Name: *NSym.Name, Address: orc::ExecutorAddr(NSym.Value), Size: 0, L: Linkage::Strong, |
377 | S: getScope(Name: *NSym.Name, Type: NSym.Type), IsLive: NSym.Desc & MachO::N_NO_DEAD_STRIP); |
378 | break; |
379 | case MachO::N_SECT: |
380 | SecIndexToSymbols[NSym.Sect - 1].push_back(x: &NSym); |
381 | break; |
382 | case MachO::N_PBUD: |
383 | return make_error<JITLinkError>( |
384 | Args: "Unupported N_PBUD symbol " + |
385 | (NSym.Name ? ("\"" + *NSym.Name + "\"" ) : Twine("<anon>" )) + |
386 | " at index " + Twine(KV.first)); |
387 | case MachO::N_INDR: |
388 | return make_error<JITLinkError>( |
389 | Args: "Unupported N_INDR symbol " + |
390 | (NSym.Name ? ("\"" + *NSym.Name + "\"" ) : Twine("<anon>" )) + |
391 | " at index " + Twine(KV.first)); |
392 | default: |
393 | return make_error<JITLinkError>( |
394 | Args: "Unrecognized symbol type " + Twine(NSym.Type & MachO::N_TYPE) + |
395 | " for symbol " + |
396 | (NSym.Name ? ("\"" + *NSym.Name + "\"" ) : Twine("<anon>" )) + |
397 | " at index " + Twine(KV.first)); |
398 | } |
399 | } |
400 | |
401 | // Loop over sections performing regular graphification for those that |
402 | // don't have custom parsers. |
403 | for (auto &KV : IndexToSection) { |
404 | auto SecIndex = KV.first; |
405 | auto &NSec = KV.second; |
406 | |
407 | if (!NSec.GraphSection) { |
408 | LLVM_DEBUG({ |
409 | dbgs() << " " << NSec.SegName << "/" << NSec.SectName |
410 | << " has no graph section. Skipping.\n" ; |
411 | }); |
412 | continue; |
413 | } |
414 | |
415 | // Skip sections with custom parsers. |
416 | if (CustomSectionParserFunctions.count(Key: NSec.GraphSection->getName())) { |
417 | LLVM_DEBUG({ |
418 | dbgs() << " Skipping section " << NSec.GraphSection->getName() |
419 | << " as it has a custom parser.\n" ; |
420 | }); |
421 | continue; |
422 | } else if ((NSec.Flags & MachO::SECTION_TYPE) == |
423 | MachO::S_CSTRING_LITERALS) { |
424 | if (auto Err = graphifyCStringSection( |
425 | NSec, NSyms: std::move(SecIndexToSymbols[SecIndex]))) |
426 | return Err; |
427 | continue; |
428 | } else |
429 | LLVM_DEBUG({ |
430 | dbgs() << " Graphifying regular section " |
431 | << NSec.GraphSection->getName() << "...\n" ; |
432 | }); |
433 | |
434 | bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP; |
435 | bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; |
436 | |
437 | auto &SecNSymStack = SecIndexToSymbols[SecIndex]; |
438 | |
439 | // If this section is non-empty but there are no symbols covering it then |
440 | // create one block and anonymous symbol to cover the entire section. |
441 | if (SecNSymStack.empty()) { |
442 | if (NSec.Size > 0) { |
443 | LLVM_DEBUG({ |
444 | dbgs() << " Section non-empty, but contains no symbols. " |
445 | "Creating anonymous block to cover " |
446 | << formatv("{0:x16}" , NSec.Address) << " -- " |
447 | << formatv("{0:x16}" , NSec.Address + NSec.Size) << "\n" ; |
448 | }); |
449 | addSectionStartSymAndBlock(SecIndex, GraphSec&: *NSec.GraphSection, Address: NSec.Address, |
450 | Data: NSec.Data, Size: NSec.Size, Alignment: NSec.Alignment, |
451 | IsLive: SectionIsNoDeadStrip); |
452 | } else |
453 | LLVM_DEBUG({ |
454 | dbgs() << " Section empty and contains no symbols. Skipping.\n" ; |
455 | }); |
456 | continue; |
457 | } |
458 | |
459 | // Sort the symbol stack in by address, alt-entry status, scope, and name. |
460 | // We sort in reverse order so that symbols will be visited in the right |
461 | // order when we pop off the stack below. |
462 | llvm::sort(C&: SecNSymStack, Comp: [](const NormalizedSymbol *LHS, |
463 | const NormalizedSymbol *RHS) { |
464 | if (LHS->Value != RHS->Value) |
465 | return LHS->Value > RHS->Value; |
466 | if (isAltEntry(NSym: *LHS) != isAltEntry(NSym: *RHS)) |
467 | return isAltEntry(NSym: *RHS); |
468 | if (LHS->S != RHS->S) |
469 | return static_cast<uint8_t>(LHS->S) < static_cast<uint8_t>(RHS->S); |
470 | return LHS->Name < RHS->Name; |
471 | }); |
472 | |
473 | // The first symbol in a section can not be an alt-entry symbol. |
474 | if (!SecNSymStack.empty() && isAltEntry(NSym: *SecNSymStack.back())) |
475 | return make_error<JITLinkError>( |
476 | Args: "First symbol in " + NSec.GraphSection->getName() + " is alt-entry" ); |
477 | |
478 | // If the section is non-empty but there is no symbol covering the start |
479 | // address then add an anonymous one. |
480 | if (orc::ExecutorAddr(SecNSymStack.back()->Value) != NSec.Address) { |
481 | auto AnonBlockSize = |
482 | orc::ExecutorAddr(SecNSymStack.back()->Value) - NSec.Address; |
483 | LLVM_DEBUG({ |
484 | dbgs() << " Section start not covered by symbol. " |
485 | << "Creating anonymous block to cover [ " << NSec.Address |
486 | << " -- " << (NSec.Address + AnonBlockSize) << " ]\n" ; |
487 | }); |
488 | addSectionStartSymAndBlock(SecIndex, GraphSec&: *NSec.GraphSection, Address: NSec.Address, |
489 | Data: NSec.Data, Size: AnonBlockSize, Alignment: NSec.Alignment, |
490 | IsLive: SectionIsNoDeadStrip); |
491 | } |
492 | |
493 | // Visit section symbols in order by popping off the reverse-sorted stack, |
494 | // building graph symbols as we go. |
495 | // |
496 | // If MH_SUBSECTIONS_VIA_SYMBOLS is set we'll build a block for each |
497 | // alt-entry chain. |
498 | // |
499 | // If MH_SUBSECTIONS_VIA_SYMBOLS is not set then we'll just build one block |
500 | // for the whole section. |
501 | while (!SecNSymStack.empty()) { |
502 | SmallVector<NormalizedSymbol *, 8> BlockSyms; |
503 | |
504 | // Get the symbols in this alt-entry chain, or the whole section (if |
505 | // !SubsectionsViaSymbols). |
506 | BlockSyms.push_back(Elt: SecNSymStack.back()); |
507 | SecNSymStack.pop_back(); |
508 | while (!SecNSymStack.empty() && |
509 | (isAltEntry(NSym: *SecNSymStack.back()) || |
510 | SecNSymStack.back()->Value == BlockSyms.back()->Value || |
511 | !SubsectionsViaSymbols)) { |
512 | BlockSyms.push_back(Elt: SecNSymStack.back()); |
513 | SecNSymStack.pop_back(); |
514 | } |
515 | |
516 | // BlockNSyms now contains the block symbols in reverse canonical order. |
517 | auto BlockStart = orc::ExecutorAddr(BlockSyms.front()->Value); |
518 | orc::ExecutorAddr BlockEnd = |
519 | SecNSymStack.empty() ? NSec.Address + NSec.Size |
520 | : orc::ExecutorAddr(SecNSymStack.back()->Value); |
521 | orc::ExecutorAddrDiff BlockOffset = BlockStart - NSec.Address; |
522 | orc::ExecutorAddrDiff BlockSize = BlockEnd - BlockStart; |
523 | |
524 | LLVM_DEBUG({ |
525 | dbgs() << " Creating block for " << formatv("{0:x16}" , BlockStart) |
526 | << " -- " << formatv("{0:x16}" , BlockEnd) << ": " |
527 | << NSec.GraphSection->getName() << " + " |
528 | << formatv("{0:x16}" , BlockOffset) << " with " |
529 | << BlockSyms.size() << " symbol(s)...\n" ; |
530 | }); |
531 | |
532 | Block &B = |
533 | NSec.Data |
534 | ? G->createContentBlock( |
535 | Parent&: *NSec.GraphSection, |
536 | Content: ArrayRef<char>(NSec.Data + BlockOffset, BlockSize), |
537 | Address: BlockStart, Alignment: NSec.Alignment, AlignmentOffset: BlockStart % NSec.Alignment) |
538 | : G->createZeroFillBlock(Parent&: *NSec.GraphSection, Size: BlockSize, |
539 | Address: BlockStart, Alignment: NSec.Alignment, |
540 | AlignmentOffset: BlockStart % NSec.Alignment); |
541 | |
542 | std::optional<orc::ExecutorAddr> LastCanonicalAddr; |
543 | auto SymEnd = BlockEnd; |
544 | while (!BlockSyms.empty()) { |
545 | auto &NSym = *BlockSyms.back(); |
546 | BlockSyms.pop_back(); |
547 | |
548 | bool SymLive = |
549 | (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip; |
550 | |
551 | auto &Sym = createStandardGraphSymbol( |
552 | Sym&: NSym, B, Size: SymEnd - orc::ExecutorAddr(NSym.Value), IsText: SectionIsText, |
553 | IsNoDeadStrip: SymLive, IsCanonical: LastCanonicalAddr != orc::ExecutorAddr(NSym.Value)); |
554 | |
555 | if (LastCanonicalAddr != Sym.getAddress()) { |
556 | if (LastCanonicalAddr) |
557 | SymEnd = *LastCanonicalAddr; |
558 | LastCanonicalAddr = Sym.getAddress(); |
559 | } |
560 | } |
561 | } |
562 | } |
563 | |
564 | return Error::success(); |
565 | } |
566 | |
567 | Symbol &MachOLinkGraphBuilder::createStandardGraphSymbol(NormalizedSymbol &NSym, |
568 | Block &B, size_t Size, |
569 | bool IsText, |
570 | bool IsNoDeadStrip, |
571 | bool IsCanonical) { |
572 | |
573 | LLVM_DEBUG({ |
574 | dbgs() << " " << formatv("{0:x16}" , NSym.Value) << " -- " |
575 | << formatv("{0:x16}" , NSym.Value + Size) << ": " ; |
576 | if (!NSym.Name) |
577 | dbgs() << "<anonymous symbol>" ; |
578 | else |
579 | dbgs() << *NSym.Name; |
580 | if (IsText) |
581 | dbgs() << " [text]" ; |
582 | if (IsNoDeadStrip) |
583 | dbgs() << " [no-dead-strip]" ; |
584 | if (!IsCanonical) |
585 | dbgs() << " [non-canonical]" ; |
586 | dbgs() << "\n" ; |
587 | }); |
588 | |
589 | auto SymOffset = orc::ExecutorAddr(NSym.Value) - B.getAddress(); |
590 | auto &Sym = |
591 | NSym.Name |
592 | ? G->addDefinedSymbol(Content&: B, Offset: SymOffset, Name: *NSym.Name, Size, L: NSym.L, S: NSym.S, |
593 | IsCallable: IsText, IsLive: IsNoDeadStrip) |
594 | : G->addAnonymousSymbol(Content&: B, Offset: SymOffset, Size, IsCallable: IsText, IsLive: IsNoDeadStrip); |
595 | NSym.GraphSymbol = &Sym; |
596 | |
597 | if (IsCanonical) |
598 | setCanonicalSymbol(NSec&: getSectionByIndex(Index: NSym.Sect - 1), Sym); |
599 | |
600 | return Sym; |
601 | } |
602 | |
603 | Error MachOLinkGraphBuilder::graphifySectionsWithCustomParsers() { |
604 | // Graphify special sections. |
605 | for (auto &KV : IndexToSection) { |
606 | auto &NSec = KV.second; |
607 | |
608 | // Skip non-graph sections. |
609 | if (!NSec.GraphSection) |
610 | continue; |
611 | |
612 | auto HI = CustomSectionParserFunctions.find(Key: NSec.GraphSection->getName()); |
613 | if (HI != CustomSectionParserFunctions.end()) { |
614 | auto &Parse = HI->second; |
615 | if (auto Err = Parse(NSec)) |
616 | return Err; |
617 | } |
618 | } |
619 | |
620 | return Error::success(); |
621 | } |
622 | |
623 | Error MachOLinkGraphBuilder::graphifyCStringSection( |
624 | NormalizedSection &NSec, std::vector<NormalizedSymbol *> NSyms) { |
625 | assert(NSec.GraphSection && "C string literal section missing graph section" ); |
626 | assert(NSec.Data && "C string literal section has no data" ); |
627 | |
628 | LLVM_DEBUG({ |
629 | dbgs() << " Graphifying C-string literal section " |
630 | << NSec.GraphSection->getName() << "\n" ; |
631 | }); |
632 | |
633 | if (NSec.Data[NSec.Size - 1] != '\0') |
634 | return make_error<JITLinkError>(Args: "C string literal section " + |
635 | NSec.GraphSection->getName() + |
636 | " does not end with null terminator" ); |
637 | |
638 | /// Sort into reverse order to use as a stack. |
639 | llvm::sort(C&: NSyms, |
640 | Comp: [](const NormalizedSymbol *LHS, const NormalizedSymbol *RHS) { |
641 | if (LHS->Value != RHS->Value) |
642 | return LHS->Value > RHS->Value; |
643 | if (LHS->L != RHS->L) |
644 | return LHS->L > RHS->L; |
645 | if (LHS->S != RHS->S) |
646 | return LHS->S > RHS->S; |
647 | if (RHS->Name) { |
648 | if (!LHS->Name) |
649 | return true; |
650 | return *LHS->Name > *RHS->Name; |
651 | } |
652 | return false; |
653 | }); |
654 | |
655 | bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP; |
656 | bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; |
657 | orc::ExecutorAddrDiff BlockStart = 0; |
658 | |
659 | // Scan section for null characters. |
660 | for (size_t I = 0; I != NSec.Size; ++I) { |
661 | if (NSec.Data[I] == '\0') { |
662 | size_t BlockSize = I + 1 - BlockStart; |
663 | // Create a block for this null terminated string. |
664 | auto &B = G->createContentBlock(Parent&: *NSec.GraphSection, |
665 | Content: {NSec.Data + BlockStart, BlockSize}, |
666 | Address: NSec.Address + BlockStart, Alignment: NSec.Alignment, |
667 | AlignmentOffset: BlockStart % NSec.Alignment); |
668 | |
669 | LLVM_DEBUG({ |
670 | dbgs() << " Created block " << B.getRange() |
671 | << ", align = " << B.getAlignment() |
672 | << ", align-ofs = " << B.getAlignmentOffset() << " for \"" ; |
673 | for (size_t J = 0; J != std::min(B.getSize(), size_t(16)); ++J) |
674 | switch (B.getContent()[J]) { |
675 | case '\0': break; |
676 | case '\n': dbgs() << "\\n" ; break; |
677 | case '\t': dbgs() << "\\t" ; break; |
678 | default: dbgs() << B.getContent()[J]; break; |
679 | } |
680 | if (B.getSize() > 16) |
681 | dbgs() << "..." ; |
682 | dbgs() << "\"\n" ; |
683 | }); |
684 | |
685 | // If there's no symbol at the start of this block then create one. |
686 | if (NSyms.empty() || |
687 | orc::ExecutorAddr(NSyms.back()->Value) != B.getAddress()) { |
688 | auto &S = G->addAnonymousSymbol(Content&: B, Offset: 0, Size: BlockSize, IsCallable: false, IsLive: false); |
689 | setCanonicalSymbol(NSec, Sym&: S); |
690 | LLVM_DEBUG({ |
691 | dbgs() << " Adding symbol for c-string block " << B.getRange() |
692 | << ": <anonymous symbol> at offset 0\n" ; |
693 | }); |
694 | } |
695 | |
696 | // Process any remaining symbols that point into this block. |
697 | auto LastCanonicalAddr = B.getAddress() + BlockSize; |
698 | while (!NSyms.empty() && orc::ExecutorAddr(NSyms.back()->Value) < |
699 | B.getAddress() + BlockSize) { |
700 | auto &NSym = *NSyms.back(); |
701 | size_t SymSize = (B.getAddress() + BlockSize) - |
702 | orc::ExecutorAddr(NSyms.back()->Value); |
703 | bool SymLive = |
704 | (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip; |
705 | |
706 | bool IsCanonical = false; |
707 | if (LastCanonicalAddr != orc::ExecutorAddr(NSym.Value)) { |
708 | IsCanonical = true; |
709 | LastCanonicalAddr = orc::ExecutorAddr(NSym.Value); |
710 | } |
711 | |
712 | auto &Sym = createStandardGraphSymbol(NSym, B, Size: SymSize, IsText: SectionIsText, |
713 | IsNoDeadStrip: SymLive, IsCanonical); |
714 | (void)Sym; |
715 | LLVM_DEBUG({ |
716 | dbgs() << " Adding symbol for c-string block " << B.getRange() |
717 | << ": " |
718 | << (Sym.hasName() ? *Sym.getName() : "<anonymous symbol>" ) |
719 | << " at offset " << formatv("{0:x}" , Sym.getOffset()) << "\n" ; |
720 | }); |
721 | |
722 | NSyms.pop_back(); |
723 | } |
724 | |
725 | BlockStart += BlockSize; |
726 | } |
727 | } |
728 | |
729 | assert(llvm::all_of(NSec.GraphSection->blocks(), |
730 | [](Block *B) { return isCStringBlock(*B); }) && |
731 | "All blocks in section should hold single c-strings" ); |
732 | |
733 | return Error::success(); |
734 | } |
735 | |
736 | } // end namespace jitlink |
737 | } // end namespace llvm |
738 | |