@@ -788,29 +788,35 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
788788 return {Group, IsComdat, Flags};
789789}
790790
791- static MCSection *selectExplicitSectionGlobal (
792- const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
793- MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
794- bool Retain, bool ForceUnique) {
795- StringRef SectionName = GO->getSection ();
796-
791+ static StringRef handlePragmaClangSection (const GlobalObject *GO,
792+ SectionKind Kind) {
797793 // Check if '#pragma clang section' name is applicable.
798794 // Note that pragma directive overrides -ffunction-section, -fdata-section
799795 // and so section name is exactly as user specified and not uniqued.
800796 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
801797 if (GV && GV->hasImplicitSection ()) {
802798 auto Attrs = GV->getAttributes ();
803- if (Attrs.hasAttribute (" bss-section" ) && Kind.isBSS ()) {
804- SectionName = Attrs.getAttribute (" bss-section" ).getValueAsString ();
805- } else if (Attrs.hasAttribute (" rodata-section" ) && Kind.isReadOnly ()) {
806- SectionName = Attrs.getAttribute (" rodata-section" ).getValueAsString ();
807- } else if (Attrs.hasAttribute (" relro-section" ) && Kind.isReadOnlyWithRel ()) {
808- SectionName = Attrs.getAttribute (" relro-section" ).getValueAsString ();
809- } else if (Attrs.hasAttribute (" data-section" ) && Kind.isData ()) {
810- SectionName = Attrs.getAttribute (" data-section" ).getValueAsString ();
811- }
799+ if (Attrs.hasAttribute (" bss-section" ) && Kind.isBSS ())
800+ return Attrs.getAttribute (" bss-section" ).getValueAsString ();
801+ else if (Attrs.hasAttribute (" rodata-section" ) && Kind.isReadOnly ())
802+ return Attrs.getAttribute (" rodata-section" ).getValueAsString ();
803+ else if (Attrs.hasAttribute (" relro-section" ) && Kind.isReadOnlyWithRel ())
804+ return Attrs.getAttribute (" relro-section" ).getValueAsString ();
805+ else if (Attrs.hasAttribute (" data-section" ) && Kind.isData ())
806+ return Attrs.getAttribute (" data-section" ).getValueAsString ();
812807 }
813808
809+ return GO->getSection ();
810+ }
811+
812+ static MCSection *selectExplicitSectionGlobal (const GlobalObject *GO,
813+ SectionKind Kind,
814+ const TargetMachine &TM,
815+ MCContext &Ctx, Mangler &Mang,
816+ unsigned &NextUniqueID,
817+ bool Retain, bool ForceUnique) {
818+ StringRef SectionName = handlePragmaClangSection (GO, Kind);
819+
814820 // Infer section flags from the section name if we can.
815821 Kind = getELFKindForNamedSection (SectionName, Kind);
816822
@@ -1291,21 +1297,7 @@ static void checkMachOComdat(const GlobalValue *GV) {
12911297MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal (
12921298 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
12931299
1294- StringRef SectionName = GO->getSection ();
1295-
1296- const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
1297- if (GV && GV->hasImplicitSection ()) {
1298- auto Attrs = GV->getAttributes ();
1299- if (Attrs.hasAttribute (" bss-section" ) && Kind.isBSS ()) {
1300- SectionName = Attrs.getAttribute (" bss-section" ).getValueAsString ();
1301- } else if (Attrs.hasAttribute (" rodata-section" ) && Kind.isReadOnly ()) {
1302- SectionName = Attrs.getAttribute (" rodata-section" ).getValueAsString ();
1303- } else if (Attrs.hasAttribute (" relro-section" ) && Kind.isReadOnlyWithRel ()) {
1304- SectionName = Attrs.getAttribute (" relro-section" ).getValueAsString ();
1305- } else if (Attrs.hasAttribute (" data-section" ) && Kind.isData ()) {
1306- SectionName = Attrs.getAttribute (" data-section" ).getValueAsString ();
1307- }
1308- }
1300+ StringRef SectionName = handlePragmaClangSection (GO, Kind);
13091301
13101302 // Parse the section specifier and create it if valid.
13111303 StringRef Segment, Section;
@@ -1674,7 +1666,7 @@ static int getSelectionForCOFF(const GlobalValue *GV) {
16741666
16751667MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal (
16761668 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1677- StringRef Name = GO-> getSection ( );
1669+ StringRef Name = handlePragmaClangSection (GO, Kind );
16781670 if (Name == getInstrProfSectionName (IPSK_covmap, Triple::COFF,
16791671 /* AddSegmentInfo=*/ false ) ||
16801672 Name == getInstrProfSectionName (IPSK_covfun, Triple::COFF,
@@ -1684,6 +1676,7 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
16841676 Name == getInstrProfSectionName (IPSK_covname, Triple::COFF,
16851677 /* AddSegmentInfo=*/ false ))
16861678 Kind = SectionKind::getMetadata ();
1679+
16871680 int Selection = 0 ;
16881681 unsigned Characteristics = getCOFFSectionFlags (Kind, TM);
16891682 StringRef COMDATSymName = " " ;
0 commit comments