18#define DEBUG_TYPE "jitlink"
30 if (!Obj.isRelocatableObject())
33 if (
auto Err = createNormalizedSections())
34 return std::move(Err);
36 if (
auto Err = createNormalizedSymbols())
37 return std::move(Err);
39 if (
auto Err = graphifyRegularSymbols())
40 return std::move(Err);
42 if (
auto Err = graphifySectionsWithCustomParsers())
43 return std::move(Err);
46 return std::move(Err);
53 std::shared_ptr<orc::SymbolStringPool> SSP,
Triple TT,
60 auto &MachHeader = Obj.getHeader64();
67 "Custom parser for this section already exists");
68 CustomSectionParserFunctions[
SectionName] = std::move(Parser);
93 strcmp(NSec.
SegName,
"__DWARF") == 0);
107Section &MachOLinkGraphBuilder::getCommonSection() {
111 return *CommonSection;
114Error MachOLinkGraphBuilder::createNormalizedSections() {
120 for (
auto &SecRef : Obj.
sections()) {
121 NormalizedSection NSec;
130 memcpy(&NSec.SectName, &Sec64.
sectname, 16);
131 NSec.SectName[16] =
'\0';
132 memcpy(&NSec.SegName, Sec64.
segname, 16);
133 NSec.SegName[16] =
'\0';
136 NSec.Size = Sec64.
size;
137 NSec.Alignment = 1ULL << Sec64.
align;
138 NSec.Flags = Sec64.
flags;
139 DataOffset = Sec64.
offset;
141 const MachO::section &Sec32 = Obj.
getSection(SecRef.getRawDataRefImpl());
143 memcpy(&NSec.SectName, &Sec32.sectname, 16);
144 NSec.SectName[16] =
'\0';
145 memcpy(&NSec.SegName, Sec32.segname, 16);
146 NSec.SegName[16] =
'\0';
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;
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
165 if (DataOffset + NSec.Size > Obj.getData().size())
167 "Section data extends past end of file");
169 NSec.Data = Obj.getData().data() + DataOffset;
181 auto FullyQualifiedName =
182 G->allocateContent(StringRef(NSec.SegName) +
"," + NSec.SectName);
183 NSec.GraphSection = &G->createSection(
184 StringRef(FullyQualifiedName.data(), FullyQualifiedName.size()), Prot);
190 IndexToSection.insert(std::make_pair(SecIndex, std::move(NSec)));
193 std::vector<NormalizedSection *> Sections;
194 Sections.reserve(IndexToSection.size());
195 for (
auto &KV : IndexToSection)
196 Sections.push_back(&KV.second);
200 if (Sections.empty())
206 return std::tie(
LHS->Address,
LHS->Size) <
207 std::tie(
RHS->Address,
RHS->Size);
210 for (
unsigned I = 0,
E = Sections.size() - 1;
I !=
E; ++
I) {
211 auto &Cur = *Sections[
I];
212 auto &
Next = *Sections[
I + 1];
213 if (
Next.Address < Cur.Address + Cur.Size)
215 "Address range for section " +
216 formatv(
"\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Cur.SegName,
217 Cur.SectName, Cur.Address, Cur.Address + Cur.Size) +
218 "overlaps section \"" +
Next.SegName +
"/" +
Next.SectName +
"\"" +
219 formatv(
"\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ",
Next.SegName,
226Error MachOLinkGraphBuilder::createNormalizedSymbols() {
229 for (
auto &SymRef : Obj.symbols()) {
231 unsigned SymbolIndex = Obj.getSymbolIndex(SymRef.getRawDataRefImpl());
239 const MachO::nlist_64 &NL64 =
240 Obj.getSymbol64TableEntry(SymRef.getRawDataRefImpl());
241 Value = NL64.n_value;
247 const MachO::nlist &NL32 =
248 Obj.getSymbolTableEntry(SymRef.getRawDataRefImpl());
249 Value = NL32.n_value;
261 std::optional<StringRef>
Name;
263 if (
auto NameOrErr = SymRef.getName())
266 return NameOrErr.takeError();
270 " has no name (string table index 0), "
271 "but N_EXT bit is set");
276 dbgs() <<
"<anonymous symbol>";
281 <<
", desc = " <<
formatv(
"{0:x4}",
Desc) <<
", sect = ";
283 dbgs() <<
static_cast<unsigned>(Sect - 1);
293 return NSec.takeError();
295 if (orc::ExecutorAddr(
Value) < NSec->Address ||
296 orc::ExecutorAddr(
Value) > NSec->Address + NSec->Size)
298 " for symbol " + *Name +
299 " does not fall within section");
301 if (!NSec->GraphSection) {
303 dbgs() <<
" Skipping: Symbol is in section " << NSec->SegName <<
"/"
305 <<
" which has no associated graph section.\n";
318void MachOLinkGraphBuilder::addSectionStartSymAndBlock(
323 Data ? G->createContentBlock(GraphSec, ArrayRef<char>(
Data,
Size),
325 : G->createZeroFillBlock(GraphSec,
Size,
Address, Alignment, 0);
326 auto &Sym = G->addAnonymousSymbol(
B, 0,
Size,
false, IsLive);
327 auto SecI = IndexToSection.find(SecIndex);
328 assert(SecI != IndexToSection.end() &&
"SecIndex invalid");
329 auto &NSec = SecI->second;
330 assert(!NSec.CanonicalSymbols.count(Sym.getAddress()) &&
331 "Anonymous block start symbol clashes with existing symbol address");
332 NSec.CanonicalSymbols[Sym.getAddress()] = &Sym;
335Error MachOLinkGraphBuilder::graphifyRegularSymbols() {
340 std::vector<std::vector<NormalizedSymbol *>> SecIndexToSymbols;
341 SecIndexToSymbols.resize(256);
345 for (
auto &KV : IndexToSymbol) {
346 auto &NSym = *KV.second;
354 NSym.GraphSymbol = &G->addDefinedSymbol(
355 G->createZeroFillBlock(getCommonSection(),
366 NSym.GraphSymbol = &G->addExternalSymbol(
374 NSym.GraphSymbol = &G->addAbsoluteSymbol(
379 SecIndexToSymbols[NSym.Sect - 1].push_back(&NSym);
383 "Unupported N_PBUD symbol " +
384 (NSym.Name ? (
"\"" + *NSym.Name +
"\"") : Twine(
"<anon>")) +
385 " at index " + Twine(KV.first));
388 "Unupported N_INDR symbol " +
389 (NSym.Name ? (
"\"" + *NSym.Name +
"\"") : Twine(
"<anon>")) +
390 " at index " + Twine(KV.first));
393 "Unrecognized symbol type " + Twine(NSym.Type &
MachO::N_TYPE) +
395 (NSym.Name ? (
"\"" + *NSym.Name +
"\"") : Twine(
"<anon>")) +
396 " at index " + Twine(KV.first));
402 for (
auto &KV : IndexToSection) {
403 auto SecIndex = KV.first;
404 auto &NSec = KV.second;
406 if (!NSec.GraphSection) {
408 dbgs() <<
" " << NSec.SegName <<
"/" << NSec.SectName
409 <<
" has no graph section. Skipping.\n";
415 if (CustomSectionParserFunctions.count(NSec.GraphSection->getName())) {
417 dbgs() <<
" Skipping section " << NSec.GraphSection->getName()
418 <<
" as it has a custom parser.\n";
423 if (
auto Err = graphifyCStringSection(
424 NSec, std::move(SecIndexToSymbols[SecIndex])))
429 dbgs() <<
" Graphifying regular section "
430 << NSec.GraphSection->getName() <<
"...\n";
436 auto &SecNSymStack = SecIndexToSymbols[SecIndex];
440 if (SecNSymStack.empty()) {
443 dbgs() <<
" Section non-empty, but contains no symbols. "
444 "Creating anonymous block to cover "
445 <<
formatv(
"{0:x16}", NSec.Address) <<
" -- "
446 <<
formatv(
"{0:x16}", NSec.Address + NSec.Size) <<
"\n";
448 addSectionStartSymAndBlock(SecIndex, *NSec.GraphSection, NSec.Address,
449 NSec.Data, NSec.Size, NSec.Alignment,
450 SectionIsNoDeadStrip);
453 dbgs() <<
" Section empty and contains no symbols. Skipping.\n";
463 if (
LHS->Value !=
RHS->Value)
464 return LHS->Value >
RHS->Value;
468 return static_cast<uint8_t
>(
LHS->S) <
static_cast<uint8_t
>(
RHS->S);
469 return LHS->Name <
RHS->Name;
473 if (!SecNSymStack.empty() &&
isAltEntry(*SecNSymStack.back()))
475 "First symbol in " + NSec.GraphSection->getName() +
" is alt-entry");
479 if (orc::ExecutorAddr(SecNSymStack.back()->Value) != NSec.Address) {
481 orc::ExecutorAddr(SecNSymStack.back()->Value) - NSec.Address;
483 dbgs() <<
" Section start not covered by symbol. "
484 <<
"Creating anonymous block to cover [ " << NSec.Address
485 <<
" -- " << (NSec.Address + AnonBlockSize) <<
" ]\n";
487 addSectionStartSymAndBlock(SecIndex, *NSec.GraphSection, NSec.Address,
488 NSec.Data, AnonBlockSize, NSec.Alignment,
489 SectionIsNoDeadStrip);
500 while (!SecNSymStack.empty()) {
505 BlockSyms.push_back(SecNSymStack.back());
506 SecNSymStack.pop_back();
507 while (!SecNSymStack.empty() &&
509 SecNSymStack.back()->Value == BlockSyms.back()->Value ||
510 !SubsectionsViaSymbols)) {
511 BlockSyms.push_back(SecNSymStack.back());
512 SecNSymStack.pop_back();
516 auto BlockStart = orc::ExecutorAddr(BlockSyms.front()->Value);
517 orc::ExecutorAddr BlockEnd =
518 SecNSymStack.empty() ? NSec.Address + NSec.Size
519 : orc::ExecutorAddr(SecNSymStack.back()->Value);
524 dbgs() <<
" Creating block for " <<
formatv(
"{0:x16}", BlockStart)
525 <<
" -- " <<
formatv(
"{0:x16}", BlockEnd) <<
": "
526 << NSec.GraphSection->getName() <<
" + "
527 <<
formatv(
"{0:x16}", BlockOffset) <<
" with "
528 << BlockSyms.size() <<
" symbol(s)...\n";
533 ? G->createContentBlock(
535 ArrayRef<char>(NSec.Data + BlockOffset,
BlockSize),
536 BlockStart, NSec.Alignment, BlockStart % NSec.Alignment)
537 : G->createZeroFillBlock(*NSec.GraphSection,
BlockSize,
538 BlockStart, NSec.Alignment,
539 BlockStart % NSec.Alignment);
541 std::optional<orc::ExecutorAddr> LastCanonicalAddr;
542 auto SymEnd = BlockEnd;
543 while (!BlockSyms.empty()) {
544 auto &NSym = *BlockSyms.back();
545 BlockSyms.pop_back();
550 auto &Sym = createStandardGraphSymbol(
551 NSym,
B, SymEnd - orc::ExecutorAddr(NSym.Value), SectionIsText,
552 SymLive, LastCanonicalAddr != orc::ExecutorAddr(NSym.Value));
554 if (LastCanonicalAddr != Sym.getAddress()) {
555 if (LastCanonicalAddr)
556 SymEnd = *LastCanonicalAddr;
557 LastCanonicalAddr = Sym.getAddress();
566Symbol &MachOLinkGraphBuilder::createStandardGraphSymbol(NormalizedSymbol &NSym,
573 dbgs() <<
" " <<
formatv(
"{0:x16}", NSym.Value) <<
" -- "
576 dbgs() <<
"<anonymous symbol>";
578 dbgs() << *NSym.Name;
582 dbgs() <<
" [no-dead-strip]";
584 dbgs() <<
" [non-canonical]";
588 auto SymOffset = orc::ExecutorAddr(NSym.Value) -
B.
getAddress();
591 ? G->addDefinedSymbol(
B, SymOffset, *NSym.Name,
Size, NSym.L, NSym.S,
592 IsText, IsNoDeadStrip)
593 : G->addAnonymousSymbol(
B, SymOffset,
Size, IsText, IsNoDeadStrip);
594 NSym.GraphSymbol = &Sym;
602Error MachOLinkGraphBuilder::graphifySectionsWithCustomParsers() {
604 for (
auto &KV : IndexToSection) {
605 auto &NSec = KV.second;
608 if (!NSec.GraphSection)
611 auto HI = CustomSectionParserFunctions.find(NSec.GraphSection->getName());
612 if (HI != CustomSectionParserFunctions.end()) {
613 auto &Parse =
HI->second;
614 if (
auto Err = Parse(NSec))
622Error MachOLinkGraphBuilder::graphifyCStringSection(
623 NormalizedSection &NSec, std::vector<NormalizedSymbol *> NSyms) {
624 assert(NSec.GraphSection &&
"C string literal section missing graph section");
625 assert(NSec.Data &&
"C string literal section has no data");
628 dbgs() <<
" Graphifying C-string literal section "
629 << NSec.GraphSection->getName() <<
"\n";
632 if (NSec.Data[NSec.Size - 1] !=
'\0')
634 NSec.GraphSection->getName() +
635 " does not end with null terminator");
640 if (
LHS->Value !=
RHS->Value)
641 return LHS->Value >
RHS->Value;
649 return *
LHS->Name > *
RHS->Name;
659 for (
size_t I = 0;
I != NSec.Size; ++
I) {
660 if (NSec.Data[
I] ==
'\0') {
663 auto &
B = G->createContentBlock(*NSec.GraphSection,
664 {NSec.Data + BlockStart, BlockSize},
665 NSec.Address + BlockStart, NSec.Alignment,
666 BlockStart % NSec.Alignment);
669 dbgs() <<
" Created block " <<
B.getRange()
670 <<
", align = " <<
B.getAlignment()
671 <<
", align-ofs = " <<
B.getAlignmentOffset() <<
" for \"";
672 for (
size_t J = 0; J != std::min(
B.getSize(),
size_t(16)); ++J)
673 switch (
B.getContent()[J]) {
675 case '\n':
dbgs() <<
"\\n";
break;
676 case '\t':
dbgs() <<
"\\t";
break;
677 default:
dbgs() <<
B.getContent()[J];
break;
679 if (
B.getSize() > 16)
686 orc::ExecutorAddr(NSyms.back()->Value) !=
B.getAddress()) {
687 auto &S = G->addAnonymousSymbol(
B, 0,
BlockSize,
false,
false);
688 setCanonicalSymbol(NSec, S);
690 dbgs() <<
" Adding symbol for c-string block " <<
B.getRange()
691 <<
": <anonymous symbol> at offset 0\n";
696 auto LastCanonicalAddr =
B.getAddress() +
BlockSize;
697 while (!NSyms.empty() && orc::ExecutorAddr(NSyms.back()->Value) <
699 auto &NSym = *NSyms.back();
700 size_t SymSize = (
B.getAddress() +
BlockSize) -
701 orc::ExecutorAddr(NSyms.back()->Value);
705 bool IsCanonical =
false;
706 if (LastCanonicalAddr != orc::ExecutorAddr(NSym.Value)) {
708 LastCanonicalAddr = orc::ExecutorAddr(NSym.Value);
711 auto &Sym = createStandardGraphSymbol(NSym,
B, SymSize, SectionIsText,
712 SymLive, IsCanonical);
715 dbgs() <<
" Adding symbol for c-string block " <<
B.getRange()
717 << (Sym.hasName() ? *Sym.getName() :
"<anonymous symbol>")
718 <<
" at offset " <<
formatv(
"{0:x}", Sym.getOffset()) <<
"\n";
729 [](
Block *
B) { return isCStringBlock(*B); }) &&
730 "All blocks in section should hold single c-strings");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static const char * CommonSectionName
static Expected< StringRef > getFileName(const DebugStringTableSubsectionRef &Strings, const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID)
Provides some synthesis utilities to produce sequences of values.
static const int BlockSize
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
StringRef - Represent a constant reference to a string, i.e.
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
The instances of the Type class are immutable: once they are created, they are never changed.
An Addressable with content and edges.
const char *(*)(Edge::Kind) GetEdgeKindNameFunction
static bool isDebugSection(const NormalizedSection &NSec)
void addCustomSectionParser(StringRef SectionName, SectionParserFunction Parse)
virtual ~MachOLinkGraphBuilder()
virtual Error addRelocations()=0
std::function< Error(NormalizedSection &S)> SectionParserFunction
static Scope getScope(StringRef Name, uint8_t Type)
static bool isZeroFillSection(const NormalizedSection &NSec)
Expected< std::unique_ptr< LinkGraph > > buildGraph()
NormalizedSection & getSectionByIndex(unsigned Index)
Index is zero-based (MachO section indexes are usually one-based) and assumed to be in-range.
MachOLinkGraphBuilder(const object::MachOObjectFile &Obj, std::shared_ptr< orc::SymbolStringPool > SSP, Triple TT, SubtargetFeatures Features, LinkGraph::GetEdgeKindNameFunction GetEdgeKindName)
NormalizedSymbol & createNormalizedSymbol(ArgTs &&... Args)
Create a symbol.
static Linkage getLinkage(uint16_t Desc)
static bool isAltEntry(const NormalizedSymbol &NSym)
Expected< NormalizedSection & > findSectionByIndex(unsigned Index)
Try to get the section at the given index.
Represents an object file section.
orc::ExecutorAddr getAddress() const
Returns the address of this symbol.
Expected< SectionRef > getSection(unsigned SectionIndex) const
MachO::section_64 getSection64(DataRefImpl DRI) const
bool is64Bit() const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
section_iterator_range sections() const
Represents an address in the executor process.
@ S_GB_ZEROFILL
S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4 gigabytes).
@ S_THREAD_LOCAL_ZEROFILL
S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
@ S_CSTRING_LITERALS
S_CSTRING_LITERALS - Section with literal C strings.
@ S_ZEROFILL
S_ZEROFILL - Zero fill on demand section.
@ MH_SUBSECTIONS_VIA_SYMBOLS
uint8_t GET_COMM_ALIGN(uint16_t n_desc)
@ S_ATTR_DEBUG
S_ATTR_DEBUG - A debug section.
@ S_ATTR_NO_DEAD_STRIP
S_ATTR_NO_DEAD_STRIP - No dead stripping.
@ S_ATTR_PURE_INSTRUCTIONS
S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine instructions.
Linkage
Describes symbol linkage. This can be used to resolve definition clashes.
Scope
Defines the scope in which this symbol should be visible: Default – Visible in the public interface o...
MemProt
Describes Read/Write/Exec permissions for memory.
uint64_t ExecutorAddrDiff
@ NoAlloc
NoAlloc memory should not be allocated by the JITLinkMemoryManager at all.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
FunctionAddr VTableAddr Next
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.