@@ -781,29 +781,32 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
781781 return {Group, IsComdat, Flags};
782782}
783783
784- static MCSection *selectExplicitSectionGlobal (
785- const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
786- MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
787- bool Retain, bool ForceUnique) {
788- StringRef SectionName = GO->getSection ();
789-
784+ static StringRef handlePragmaClangSection (const GlobalObject *GO, SectionKind Kind) {
790785 // Check if '#pragma clang section' name is applicable.
791786 // Note that pragma directive overrides -ffunction-section, -fdata-section
792787 // and so section name is exactly as user specified and not uniqued.
793788 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
794789 if (GV && GV->hasImplicitSection ()) {
795790 auto Attrs = GV->getAttributes ();
796- if (Attrs.hasAttribute (" bss-section" ) && Kind.isBSS ()) {
797- SectionName = Attrs.getAttribute (" bss-section" ).getValueAsString ();
798- } else if (Attrs.hasAttribute (" rodata-section" ) && Kind.isReadOnly ()) {
799- SectionName = Attrs.getAttribute (" rodata-section" ).getValueAsString ();
800- } else if (Attrs.hasAttribute (" relro-section" ) && Kind.isReadOnlyWithRel ()) {
801- SectionName = Attrs.getAttribute (" relro-section" ).getValueAsString ();
802- } else if (Attrs.hasAttribute (" data-section" ) && Kind.isData ()) {
803- SectionName = Attrs.getAttribute (" data-section" ).getValueAsString ();
804- }
791+ if (Attrs.hasAttribute (" bss-section" ) && Kind.isBSS ())
792+ return Attrs.getAttribute (" bss-section" ).getValueAsString ();
793+ else if (Attrs.hasAttribute (" rodata-section" ) && Kind.isReadOnly ())
794+ return Attrs.getAttribute (" rodata-section" ).getValueAsString ();
795+ else if (Attrs.hasAttribute (" relro-section" ) && Kind.isReadOnlyWithRel ())
796+ return Attrs.getAttribute (" relro-section" ).getValueAsString ();
797+ else if (Attrs.hasAttribute (" data-section" ) && Kind.isData ())
798+ return Attrs.getAttribute (" data-section" ).getValueAsString ();
805799 }
806800
801+ return GO->getSection ();
802+ }
803+
804+ static MCSection *selectExplicitSectionGlobal (
805+ const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
806+ MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
807+ bool Retain, bool ForceUnique) {
808+ StringRef SectionName = handlePragmaClangSection (GO, Kind);
809+
807810 // Infer section flags from the section name if we can.
808811 Kind = getELFKindForNamedSection (SectionName, Kind);
809812
@@ -1284,21 +1287,7 @@ static void checkMachOComdat(const GlobalValue *GV) {
12841287MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal (
12851288 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
12861289
1287- StringRef SectionName = GO->getSection ();
1288-
1289- const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
1290- if (GV && GV->hasImplicitSection ()) {
1291- auto Attrs = GV->getAttributes ();
1292- if (Attrs.hasAttribute (" bss-section" ) && Kind.isBSS ()) {
1293- SectionName = Attrs.getAttribute (" bss-section" ).getValueAsString ();
1294- } else if (Attrs.hasAttribute (" rodata-section" ) && Kind.isReadOnly ()) {
1295- SectionName = Attrs.getAttribute (" rodata-section" ).getValueAsString ();
1296- } else if (Attrs.hasAttribute (" relro-section" ) && Kind.isReadOnlyWithRel ()) {
1297- SectionName = Attrs.getAttribute (" relro-section" ).getValueAsString ();
1298- } else if (Attrs.hasAttribute (" data-section" ) && Kind.isData ()) {
1299- SectionName = Attrs.getAttribute (" data-section" ).getValueAsString ();
1300- }
1301- }
1290+ StringRef SectionName = handlePragmaClangSection (GO, Kind);
13021291
13031292 // Parse the section specifier and create it if valid.
13041293 StringRef Segment, Section;
@@ -1667,7 +1656,7 @@ static int getSelectionForCOFF(const GlobalValue *GV) {
16671656
16681657MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal (
16691658 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1670- StringRef Name = GO-> getSection ( );
1659+ StringRef Name = handlePragmaClangSection (GO, Kind );
16711660 if (Name == getInstrProfSectionName (IPSK_covmap, Triple::COFF,
16721661 /* AddSegmentInfo=*/ false ) ||
16731662 Name == getInstrProfSectionName (IPSK_covfun, Triple::COFF,
@@ -1677,6 +1666,7 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
16771666 Name == getInstrProfSectionName (IPSK_covname, Triple::COFF,
16781667 /* AddSegmentInfo=*/ false ))
16791668 Kind = SectionKind::getMetadata ();
1669+
16801670 int Selection = 0 ;
16811671 unsigned Characteristics = getCOFFSectionFlags (Kind, TM);
16821672 StringRef COMDATSymName = " " ;
0 commit comments