@@ -603,21 +603,48 @@ void MachOPlatform::MachOPlatformPlugin::modifyPassConfig(
603
603
MaterializationResponsibility &MR, jitlink::LinkGraph &LG,
604
604
jitlink::PassConfiguration &Config) {
605
605
606
- // If the initializer symbol is the MachOHeader start symbol then just add
607
- // the macho header support passes.
608
- if (MR.getInitializerSymbol () == MP.MachOHeaderStartSymbol ) {
609
- addMachOHeaderSupportPasses (MR, Config);
610
- // The header materialization unit doesn't require any other support, so we
611
- // can bail out early.
612
- return ;
606
+ // --- Handle Initializers ---
607
+ if (auto InitSymbol = MR.getInitializerSymbol ()) {
608
+
609
+ // If the initializer symbol is the MachOHeader start symbol then just
610
+ // register it and then bail out -- the header materialization unit
611
+ // definitely doesn't need any other passes.
612
+ if (InitSymbol == MP.MachOHeaderStartSymbol ) {
613
+ Config.PostAllocationPasses .push_back ([this , &MR](jitlink::LinkGraph &G) {
614
+ return associateJITDylibHeaderSymbol (G, MR);
615
+ });
616
+ return ;
617
+ }
618
+
619
+ // If the object contains an init symbol other than the header start symbol
620
+ // then add passes to preserve, process and register the init
621
+ // sections/symbols.
622
+ Config.PrePrunePasses .push_back ([this , &MR](jitlink::LinkGraph &G) {
623
+ if (auto Err = preserveInitSections (G, MR))
624
+ return Err;
625
+ return processObjCImageInfo (G, MR);
626
+ });
627
+
628
+ Config.PostFixupPasses .push_back (
629
+ [this , &JD = MR.getTargetJITDylib ()](jitlink::LinkGraph &G) {
630
+ return registerInitSections (G, JD);
631
+ });
613
632
}
614
633
615
- // If the object contains initializers then add passes to record them.
616
- if (MR.getInitializerSymbol ())
617
- addInitializerSupportPasses (MR, Config);
634
+ // --- Add passes for eh-frame and TLV support ---
635
+
636
+ // Insert TLV lowering at the start of the PostPrunePasses, since we want
637
+ // it to run before GOT/PLT lowering.
638
+ Config.PostPrunePasses .insert (
639
+ Config.PostPrunePasses .begin (),
640
+ [this , &JD = MR.getTargetJITDylib ()](jitlink::LinkGraph &G) {
641
+ return fixTLVSectionsAndEdges (G, JD);
642
+ });
618
643
619
- // Add passes for eh-frame and TLV support.
620
- addEHAndTLVSupportPasses (MR, Config);
644
+ // Add a pass to register the final addresses of the eh-frame and TLV sections
645
+ // with the runtime.
646
+ Config.PostFixupPasses .push_back (
647
+ [this ](jitlink::LinkGraph &G) { return registerEHAndTLVSections (G); });
621
648
}
622
649
623
650
ObjectLinkingLayer::Plugin::SyntheticSymbolDependenciesMap
@@ -634,110 +661,22 @@ MachOPlatform::MachOPlatformPlugin::getSyntheticSymbolDependencies(
634
661
return SyntheticSymbolDependenciesMap ();
635
662
}
636
663
637
- void MachOPlatform::MachOPlatformPlugin::addInitializerSupportPasses (
638
- MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
639
-
640
- // / Preserve init sections.
641
- Config.PrePrunePasses .push_back ([this , &MR](jitlink::LinkGraph &G) {
642
- if (auto Err = preserveInitSections (G, MR))
643
- return Err;
644
- return processObjCImageInfo (G, MR);
645
- });
646
-
647
- Config.PostFixupPasses .push_back (
648
- [this , &JD = MR.getTargetJITDylib ()](jitlink::LinkGraph &G) {
649
- return registerInitSections (G, JD);
650
- });
651
- }
652
-
653
- void MachOPlatform::MachOPlatformPlugin::addMachOHeaderSupportPasses (
654
- MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
655
-
656
- Config.PostAllocationPasses .push_back ([this , &JD = MR.getTargetJITDylib ()](
657
- jitlink::LinkGraph &G) -> Error {
658
- auto I = llvm::find_if (G.defined_symbols (), [this ](jitlink::Symbol *Sym) {
659
- return Sym->getName () == *MP.MachOHeaderStartSymbol ;
660
- });
661
- assert (I != G.defined_symbols ().end () &&
662
- " Missing MachO header start symbol" );
663
- {
664
- std::lock_guard<std::mutex> Lock (MP.PlatformMutex );
665
- JITTargetAddress HeaderAddr = (*I)->getAddress ();
666
- MP.HeaderAddrToJITDylib [HeaderAddr] = &JD;
667
- assert (!MP.InitSeqs .count (&JD) && " InitSeq entry for JD already exists" );
668
- MP.InitSeqs .insert (std::make_pair (
669
- &JD,
670
- MachOJITDylibInitializers (JD.getName (), ExecutorAddr (HeaderAddr))));
671
- }
672
- return Error::success ();
673
- });
674
- }
675
-
676
- void MachOPlatform::MachOPlatformPlugin::addEHAndTLVSupportPasses (
677
- MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
678
-
679
- // Insert TLV lowering at the start of the PostPrunePasses, since we want
680
- // it to run before GOT/PLT lowering.
681
- Config.PostPrunePasses .insert (
682
- Config.PostPrunePasses .begin (),
683
- [this , &JD = MR.getTargetJITDylib ()](jitlink::LinkGraph &G) {
684
- return fixTLVSectionsAndEdges (G, JD);
685
- });
686
-
687
- // Add a pass to register the final addresses of the eh-frame and TLV sections
688
- // with the runtime.
689
- Config.PostFixupPasses .push_back ([this ](jitlink::LinkGraph &G) -> Error {
690
- MachOPerObjectSectionsToRegister POSR;
691
-
692
- if (auto *EHFrameSection = G.findSectionByName (EHFrameSectionName)) {
693
- jitlink::SectionRange R (*EHFrameSection);
694
- if (!R.empty ())
695
- POSR.EHFrameSection = {ExecutorAddr (R.getStart ()),
696
- ExecutorAddr (R.getEnd ())};
697
- }
698
-
699
- // Get a pointer to the thread data section if there is one. It will be used
700
- // below.
701
- jitlink::Section *ThreadDataSection =
702
- G.findSectionByName (ThreadDataSectionName);
703
-
704
- // Handle thread BSS section if there is one.
705
- if (auto *ThreadBSSSection = G.findSectionByName (ThreadBSSSectionName)) {
706
- // If there's already a thread data section in this graph then merge the
707
- // thread BSS section content into it, otherwise just treat the thread
708
- // BSS section as the thread data section.
709
- if (ThreadDataSection)
710
- G.mergeSections (*ThreadDataSection, *ThreadBSSSection);
711
- else
712
- ThreadDataSection = ThreadBSSSection;
713
- }
714
-
715
- // Having merged thread BSS (if present) and thread data (if present),
716
- // record the resulting section range.
717
- if (ThreadDataSection) {
718
- jitlink::SectionRange R (*ThreadDataSection);
719
- if (!R.empty ())
720
- POSR.ThreadDataSection = {ExecutorAddr (R.getStart ()),
721
- ExecutorAddr (R.getEnd ())};
722
- }
723
-
724
- if (POSR.EHFrameSection .Start || POSR.ThreadDataSection .Start ) {
725
-
726
- // If we're still bootstrapping the runtime then just record this
727
- // frame for now.
728
- if (!MP.RuntimeBootstrapped ) {
729
- std::lock_guard<std::mutex> Lock (MP.PlatformMutex );
730
- MP.BootstrapPOSRs .push_back (POSR);
731
- return Error::success ();
732
- }
733
-
734
- // Otherwise register it immediately.
735
- if (auto Err = MP.registerPerObjectSections (POSR))
736
- return Err;
737
- }
664
+ Error MachOPlatform::MachOPlatformPlugin::associateJITDylibHeaderSymbol (
665
+ jitlink::LinkGraph &G, MaterializationResponsibility &MR) {
738
666
739
- return Error::success ();
667
+ auto I = llvm::find_if (G.defined_symbols (), [this ](jitlink::Symbol *Sym) {
668
+ return Sym->getName () == *MP.MachOHeaderStartSymbol ;
740
669
});
670
+ assert (I != G.defined_symbols ().end () && " Missing MachO header start symbol" );
671
+
672
+ auto &JD = MR.getTargetJITDylib ();
673
+ std::lock_guard<std::mutex> Lock (MP.PlatformMutex );
674
+ JITTargetAddress HeaderAddr = (*I)->getAddress ();
675
+ MP.HeaderAddrToJITDylib [HeaderAddr] = &JD;
676
+ assert (!MP.InitSeqs .count (&JD) && " InitSeq entry for JD already exists" );
677
+ MP.InitSeqs .insert (std::make_pair (
678
+ &JD, MachOJITDylibInitializers (JD.getName (), ExecutorAddr (HeaderAddr))));
679
+ return Error::success ();
741
680
}
742
681
743
682
Error MachOPlatform::MachOPlatformPlugin::preserveInitSections (
@@ -942,5 +881,59 @@ Error MachOPlatform::MachOPlatformPlugin::fixTLVSectionsAndEdges(
942
881
return Error::success ();
943
882
}
944
883
884
+ Error MachOPlatform::MachOPlatformPlugin::registerEHAndTLVSections (
885
+ jitlink::LinkGraph &G) {
886
+ MachOPerObjectSectionsToRegister POSR;
887
+
888
+ if (auto *EHFrameSection = G.findSectionByName (EHFrameSectionName)) {
889
+ jitlink::SectionRange R (*EHFrameSection);
890
+ if (!R.empty ())
891
+ POSR.EHFrameSection = {ExecutorAddr (R.getStart ()),
892
+ ExecutorAddr (R.getEnd ())};
893
+ }
894
+
895
+ // Get a pointer to the thread data section if there is one. It will be used
896
+ // below.
897
+ jitlink::Section *ThreadDataSection =
898
+ G.findSectionByName (ThreadDataSectionName);
899
+
900
+ // Handle thread BSS section if there is one.
901
+ if (auto *ThreadBSSSection = G.findSectionByName (ThreadBSSSectionName)) {
902
+ // If there's already a thread data section in this graph then merge the
903
+ // thread BSS section content into it, otherwise just treat the thread
904
+ // BSS section as the thread data section.
905
+ if (ThreadDataSection)
906
+ G.mergeSections (*ThreadDataSection, *ThreadBSSSection);
907
+ else
908
+ ThreadDataSection = ThreadBSSSection;
909
+ }
910
+
911
+ // Having merged thread BSS (if present) and thread data (if present),
912
+ // record the resulting section range.
913
+ if (ThreadDataSection) {
914
+ jitlink::SectionRange R (*ThreadDataSection);
915
+ if (!R.empty ())
916
+ POSR.ThreadDataSection = {ExecutorAddr (R.getStart ()),
917
+ ExecutorAddr (R.getEnd ())};
918
+ }
919
+
920
+ if (POSR.EHFrameSection .Start || POSR.ThreadDataSection .Start ) {
921
+
922
+ // If we're still bootstrapping the runtime then just record this
923
+ // frame for now.
924
+ if (!MP.RuntimeBootstrapped ) {
925
+ std::lock_guard<std::mutex> Lock (MP.PlatformMutex );
926
+ MP.BootstrapPOSRs .push_back (POSR);
927
+ return Error::success ();
928
+ }
929
+
930
+ // Otherwise register it immediately.
931
+ if (auto Err = MP.registerPerObjectSections (POSR))
932
+ return Err;
933
+ }
934
+
935
+ return Error::success ();
936
+ }
937
+
945
938
} // End namespace orc.
946
939
} // End namespace llvm.
0 commit comments