@@ -601,7 +601,7 @@ class ReflectionContext
601
601
auto StrTab = reinterpret_cast <const char *>(StrTabBuf);
602
602
bool Error = false ;
603
603
auto findELFSectionByName =
604
- [&](llvm::StringRef Name) -> std::pair<RemoteRef<void >, uint64_t > {
604
+ [&](llvm::StringRef Name, bool Retained ) -> std::pair<RemoteRef<void >, uint64_t > {
605
605
if (Error)
606
606
return {nullptr , 0 };
607
607
// Now for all the sections, find their name.
@@ -616,6 +616,8 @@ class ReflectionContext
616
616
std::string SecName (Start, StringSize);
617
617
if (SecName != Name)
618
618
continue ;
619
+ if (Retained != !!(Hdr->sh_flags & llvm::ELF::SHF_GNU_RETAIN))
620
+ continue ;
619
621
RemoteAddress SecStart =
620
622
RemoteAddress (ImageStart.getAddressData () + Hdr->sh_addr );
621
623
auto SecSize = Hdr->sh_size ;
@@ -649,48 +651,96 @@ class ReflectionContext
649
651
650
652
SwiftObjectFileFormatELF ObjectFileFormat;
651
653
auto FieldMdSec = findELFSectionByName (
652
- ObjectFileFormat.getSectionName (ReflectionSectionKind::fieldmd));
654
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::fieldmd), true );
653
655
auto AssocTySec = findELFSectionByName (
654
- ObjectFileFormat.getSectionName (ReflectionSectionKind::assocty));
656
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::assocty), true );
655
657
auto BuiltinTySec = findELFSectionByName (
656
- ObjectFileFormat.getSectionName (ReflectionSectionKind::builtin));
658
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::builtin), true );
657
659
auto CaptureSec = findELFSectionByName (
658
- ObjectFileFormat.getSectionName (ReflectionSectionKind::capture));
660
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::capture), true );
659
661
auto TypeRefMdSec = findELFSectionByName (
660
- ObjectFileFormat.getSectionName (ReflectionSectionKind::typeref));
662
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::typeref), true );
661
663
auto ReflStrMdSec = findELFSectionByName (
662
- ObjectFileFormat.getSectionName (ReflectionSectionKind::reflstr));
664
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::reflstr), true );
663
665
auto ConformMdSec = findELFSectionByName (
664
- ObjectFileFormat.getSectionName (ReflectionSectionKind::conform));
666
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::conform), true );
665
667
auto MPEnumMdSec = findELFSectionByName (
666
- ObjectFileFormat.getSectionName (ReflectionSectionKind::mpenum));
668
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::mpenum), true );
667
669
668
670
if (Error)
669
671
return false ;
670
672
673
+ std::optional<uint32_t > result = false ;
674
+
671
675
// We succeed if at least one of the sections is present in the
672
676
// ELF executable.
673
- if (FieldMdSec.first == nullptr &&
674
- AssocTySec.first == nullptr &&
675
- BuiltinTySec.first == nullptr &&
676
- CaptureSec.first == nullptr &&
677
- TypeRefMdSec.first == nullptr &&
678
- ReflStrMdSec.first == nullptr &&
679
- ConformMdSec.first == nullptr &&
680
- MPEnumMdSec.first == nullptr )
677
+ if (FieldMdSec.first != nullptr ||
678
+ AssocTySec.first != nullptr ||
679
+ BuiltinTySec.first != nullptr ||
680
+ CaptureSec.first != nullptr ||
681
+ TypeRefMdSec.first != nullptr ||
682
+ ReflStrMdSec.first != nullptr ||
683
+ ConformMdSec.first != nullptr ||
684
+ MPEnumMdSec.first != nullptr ) {
685
+ ReflectionInfo info = {{FieldMdSec.first , FieldMdSec.second },
686
+ {AssocTySec.first , AssocTySec.second },
687
+ {BuiltinTySec.first , BuiltinTySec.second },
688
+ {CaptureSec.first , CaptureSec.second },
689
+ {TypeRefMdSec.first , TypeRefMdSec.second },
690
+ {ReflStrMdSec.first , ReflStrMdSec.second },
691
+ {ConformMdSec.first , ConformMdSec.second },
692
+ {MPEnumMdSec.first , MPEnumMdSec.second },
693
+ PotentialModuleNames};
694
+ result = this ->addReflectionInfo (info);
695
+ }
696
+
697
+ // Also check for the non-retained versions of the sections; we'll
698
+ // only return a single reflection info ID if both are found (and it'll
699
+ // be the one for the retained sections if we have them), but we'll
700
+ // still add all the reflection information.
701
+ FieldMdSec = findELFSectionByName (
702
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::fieldmd), false );
703
+ AssocTySec = findELFSectionByName (
704
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::assocty), false );
705
+ BuiltinTySec = findELFSectionByName (
706
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::builtin), false );
707
+ CaptureSec = findELFSectionByName (
708
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::capture), false );
709
+ TypeRefMdSec = findELFSectionByName (
710
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::typeref), false );
711
+ ReflStrMdSec = findELFSectionByName (
712
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::reflstr), false );
713
+ ConformMdSec = findELFSectionByName (
714
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::conform), false );
715
+ MPEnumMdSec = findELFSectionByName (
716
+ ObjectFileFormat.getSectionName (ReflectionSectionKind::mpenum), false );
717
+
718
+ if (Error)
681
719
return false ;
682
720
683
- ReflectionInfo info = {{FieldMdSec.first , FieldMdSec.second },
684
- {AssocTySec.first , AssocTySec.second },
685
- {BuiltinTySec.first , BuiltinTySec.second },
686
- {CaptureSec.first , CaptureSec.second },
687
- {TypeRefMdSec.first , TypeRefMdSec.second },
688
- {ReflStrMdSec.first , ReflStrMdSec.second },
689
- {ConformMdSec.first , ConformMdSec.second },
690
- {MPEnumMdSec.first , MPEnumMdSec.second },
691
- PotentialModuleNames};
721
+ if (FieldMdSec.first != nullptr ||
722
+ AssocTySec.first != nullptr ||
723
+ BuiltinTySec.first != nullptr ||
724
+ CaptureSec.first != nullptr ||
725
+ TypeRefMdSec.first != nullptr ||
726
+ ReflStrMdSec.first != nullptr ||
727
+ ConformMdSec.first != nullptr ||
728
+ MPEnumMdSec.first != nullptr ) {
729
+ ReflectionInfo info = {{FieldMdSec.first , FieldMdSec.second },
730
+ {AssocTySec.first , AssocTySec.second },
731
+ {BuiltinTySec.first , BuiltinTySec.second },
732
+ {CaptureSec.first , CaptureSec.second },
733
+ {TypeRefMdSec.first , TypeRefMdSec.second },
734
+ {ReflStrMdSec.first , ReflStrMdSec.second },
735
+ {ConformMdSec.first , ConformMdSec.second },
736
+ {MPEnumMdSec.first , MPEnumMdSec.second },
737
+ PotentialModuleNames};
738
+ auto rid = this ->addReflectionInfo (info);
739
+ if (!result)
740
+ result = rid;
741
+ }
692
742
693
- return this -> addReflectionInfo (info) ;
743
+ return result ;
694
744
}
695
745
696
746
// / Parses metadata information from an ELF image. Because the Section
0 commit comments