https://gcc.gnu.org/g:349da53f13de274864d01b6ccc466961c472dbe1

commit r16-1971-g349da53f13de274864d01b6ccc466961c472dbe1
Author: H.J. Lu <hjl.to...@gmail.com>
Date:   Thu Jul 3 10:13:48 2025 +0800

    x86: Emit label only for __mcount_loc section
    
    commit ecc81e33123d7ac9c11742161e128858d844b99d
    Author: Andi Kleen <a...@linux.intel.com>
    Date:   Fri Sep 26 04:06:40 2014 +0000
    
        Add direct support for Linux kernel __fentry__ patching
    
    emitted a label, 1, for __mcount_loc section:
    
    1:      call    mcount
            .section __mcount_loc, "a",@progbits
            .quad 1b
            .previous
    
    If __mcount_loc wasn't used, we got an unused label.  Update
    x86_function_profiler to emit label only when __mcount_loc section
    is used.
    
    gcc/
    
            PR target/120936
            * config/i386/i386.cc (x86_print_call_or_nop): Add a label
            argument and use it to print label.
            (x86_function_profiler): Emit label only when __mcount_loc
            section is used.
    
    gcc/testsuite/
    
            PR target/120936
            * gcc.target/i386/pr120936-1.c: New test
            * gcc.target/i386/pr120936-2.c: Likewise.
            * gcc.target/i386/pr120936-3.c: Likewise.
            * gcc.target/i386/pr120936-4.c: Likewise.
            * gcc.target/i386/pr120936-5.c: Likewise.
            * gcc.target/i386/pr120936-6.c: Likewise.
            * gcc.target/i386/pr120936-7.c: Likewise.
            * gcc.target/i386/pr120936-8.c: Likewise.
            * gcc.target/i386/pr120936-9.c: Likewise.
            * gcc.target/i386/pr120936-10.c: Likewise.
            * gcc.target/i386/pr120936-11.c: Likewise.
            * gcc.target/i386/pr120936-12.c: Likewise.
            * gcc.target/i386/pr93492-3.c: Updated.
            * gcc.target/i386/pr93492-5.c: Likewise.
    
    Signed-off-by: H.J. Lu <hjl.to...@gmail.com>

Diff:
---
 gcc/config/i386/i386.cc                     | 52 +++++++++++++++++------------
 gcc/testsuite/gcc.target/i386/pr120936-1.c  | 18 ++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-10.c | 23 +++++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-11.c | 19 +++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-12.c | 23 +++++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-2.c  | 18 ++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-3.c  | 18 ++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-4.c  | 18 ++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-5.c  | 18 ++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-6.c  | 18 ++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-7.c  | 18 ++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-8.c  | 18 ++++++++++
 gcc/testsuite/gcc.target/i386/pr120936-9.c  | 19 +++++++++++
 gcc/testsuite/gcc.target/i386/pr93492-3.c   |  2 +-
 gcc/testsuite/gcc.target/i386/pr93492-5.c   |  2 +-
 15 files changed, 261 insertions(+), 23 deletions(-)

diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
index 24aedc136a69..b64175d6c939 100644
--- a/gcc/config/i386/i386.cc
+++ b/gcc/config/i386/i386.cc
@@ -23686,19 +23686,21 @@ x86_field_alignment (tree type, int computed)
 /* Print call to TARGET to FILE.  */
 
 static void
-x86_print_call_or_nop (FILE *file, const char *target)
+x86_print_call_or_nop (FILE *file, const char *target,
+                      const char *label)
 {
   if (flag_nop_mcount || !strcmp (target, "nop"))
     /* 5 byte nop: nopl 0(%[re]ax,%[re]ax,1) */
-    fprintf (file, "1:" ASM_BYTE "0x0f, 0x1f, 0x44, 0x00, 0x00\n");
+    fprintf (file, "%s" ASM_BYTE "0x0f, 0x1f, 0x44, 0x00, 0x00\n",
+            label);
   else if (!TARGET_PECOFF && flag_pic)
     {
       gcc_assert (flag_plt);
 
-      fprintf (file, "1:\tcall\t%s@PLT\n", target);
+      fprintf (file, "%s\tcall\t%s@PLT\n", label, target);
     }
   else
-    fprintf (file, "1:\tcall\t%s\n", target);
+    fprintf (file, "%s\tcall\t%s\n", label, target);
 }
 
 static bool
@@ -23783,6 +23785,13 @@ x86_function_profiler (FILE *file, int labelno 
ATTRIBUTE_UNUSED)
 
   const char *mcount_name = MCOUNT_NAME;
 
+  bool fentry_section_p
+    = (flag_record_mcount
+       || lookup_attribute ("fentry_section",
+                           DECL_ATTRIBUTES (current_function_decl)));
+
+  const char *label = fentry_section_p ? "1:" : "";
+
   if (current_fentry_name (&mcount_name))
     ;
   else if (fentry_name)
@@ -23818,11 +23827,12 @@ x86_function_profiler (FILE *file, int labelno 
ATTRIBUTE_UNUSED)
                  reg = legacy_reg;
                }
              if (ASSEMBLER_DIALECT == ASM_INTEL)
-               fprintf (file, "1:\tmovabs\t%s, OFFSET FLAT:%s\n"
-                              "\tcall\t%s\n", reg, mcount_name, reg);
+               fprintf (file, "%s\tmovabs\t%s, OFFSET FLAT:%s\n"
+                              "\tcall\t%s\n", label, reg, mcount_name,
+                              reg);
              else
-               fprintf (file, "1:\tmovabsq\t$%s, %%%s\n\tcall\t*%%%s\n",
-                        mcount_name, reg, reg);
+               fprintf (file, "%s\tmovabsq\t$%s, %%%s\n\tcall\t*%%%s\n",
+                        label, mcount_name, reg, reg);
              break;
            case CM_LARGE_PIC:
 #ifdef NO_PROFILE_COUNTERS
@@ -23863,21 +23873,21 @@ x86_function_profiler (FILE *file, int labelno 
ATTRIBUTE_UNUSED)
              if (!flag_plt)
                {
                  if (ASSEMBLER_DIALECT == ASM_INTEL)
-                   fprintf (file, "1:\tcall\t[QWORD PTR %s@GOTPCREL[rip]]\n",
-                            mcount_name);
+                   fprintf (file, "%s\tcall\t[QWORD PTR %s@GOTPCREL[rip]]\n",
+                            label, mcount_name);
                  else
-                   fprintf (file, "1:\tcall\t*%s@GOTPCREL(%%rip)\n",
-                            mcount_name);
+                   fprintf (file, "%s\tcall\t*%s@GOTPCREL(%%rip)\n",
+                            label, mcount_name);
                  break;
                }
              /* fall through */
            default:
-             x86_print_call_or_nop (file, mcount_name);
+             x86_print_call_or_nop (file, mcount_name, label);
              break;
            }
        }
       else
-       x86_print_call_or_nop (file, mcount_name);
+       x86_print_call_or_nop (file, mcount_name, label);
     }
   else if (flag_pic)
     {
@@ -23892,11 +23902,13 @@ x86_function_profiler (FILE *file, int labelno 
ATTRIBUTE_UNUSED)
                 LPREFIX, labelno);
 #endif
       if (flag_plt)
-       x86_print_call_or_nop (file, mcount_name);
+       x86_print_call_or_nop (file, mcount_name, label);
       else if (ASSEMBLER_DIALECT == ASM_INTEL)
-       fprintf (file, "1:\tcall\t[DWORD PTR %s@GOT[ebx]]\n", mcount_name);
+       fprintf (file, "%s\tcall\t[DWORD PTR %s@GOT[ebx]]\n",
+                label, mcount_name);
       else
-       fprintf (file, "1:\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
+       fprintf (file, "%s\tcall\t*%s@GOT(%%ebx)\n",
+                label, mcount_name);
     }
   else
     {
@@ -23909,12 +23921,10 @@ x86_function_profiler (FILE *file, int labelno 
ATTRIBUTE_UNUSED)
        fprintf (file, "\tmovl\t$%sP%d, %%" PROFILE_COUNT_REGISTER "\n",
                 LPREFIX, labelno);
 #endif
-      x86_print_call_or_nop (file, mcount_name);
+      x86_print_call_or_nop (file, mcount_name, label);
     }
 
-  if (flag_record_mcount
-      || lookup_attribute ("fentry_section",
-                          DECL_ATTRIBUTES (current_function_decl)))
+  if (fentry_section_p)
     {
       const char *sname = "__mcount_loc";
 
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-1.c 
b/gcc/testsuite/gcc.target/i386/pr120936-1.c
new file mode 100644
index 000000000000..a20680d85483
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-1.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target fpic } } */
+/* { dg-options "-O2 -pg -mno-fentry -fno-pic -fno-shrink-wrap" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } 
} */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**     call    mcount
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-10.c 
b/gcc/testsuite/gcc.target/i386/pr120936-10.c
new file mode 100644
index 000000000000..ab95b087ec9b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-10.c
@@ -0,0 +1,23 @@
+/* { dg-do compile { target { fpic && lp64 } } } */
+/* { dg-options "-O2 -mcmodel=large -pg -mno-fentry -fpic -fno-shrink-wrap" } 
*/
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } 
{^(1|\t?\.)} } } */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**1:   movabsq \$_GLOBAL_OFFSET_TABLE_-1b, %r11
+**     leaq    1b\(%rip\), %r10
+**     addq    %r11, %r10
+**     movabsq \$mcount@PLTOFF, %r11
+**     addq    %r11, %r10
+**     call    \*%r10
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-11.c 
b/gcc/testsuite/gcc.target/i386/pr120936-11.c
new file mode 100644
index 000000000000..3e39dfe0d374
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-11.c
@@ -0,0 +1,19 @@
+/* { dg-do compile { target { fpic && lp64 } } } */
+/* { dg-options "-O2 -mrecord-mcount -mcmodel=large -pg -mno-fentry -fno-pic 
-fno-shrink-wrap" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } 
{^(1|\t?\.)} } } */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**1:   movabsq \$mcount, %r10
+**     call    \*%r10
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-12.c 
b/gcc/testsuite/gcc.target/i386/pr120936-12.c
new file mode 100644
index 000000000000..b5a2aaced5d8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-12.c
@@ -0,0 +1,23 @@
+/* { dg-do compile { target { fpic && lp64 } } } */
+/* { dg-options "-O2 -mcmodel=large -mrecord-mcount -pg -mno-fentry -fpic 
-fno-shrink-wrap" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } 
{^(1|\t?\.)} } } */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**1:   movabsq \$_GLOBAL_OFFSET_TABLE_-1b, %r11
+**     leaq    1b\(%rip\), %r10
+**     addq    %r11, %r10
+**     movabsq \$mcount@PLTOFF, %r11
+**     addq    %r11, %r10
+**     call    \*%r10
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-2.c 
b/gcc/testsuite/gcc.target/i386/pr120936-2.c
new file mode 100644
index 000000000000..083565801651
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-2.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target fpic } } */
+/* { dg-options "-O2 -pg -mno-fentry -fpic -fno-shrink-wrap" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } 
} */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**     call    mcount@PLT
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-3.c 
b/gcc/testsuite/gcc.target/i386/pr120936-3.c
new file mode 100644
index 000000000000..dc0a8f1e4243
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-3.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target fpic } } */
+/* { dg-options "-O2 -mnop-mcount -pg -mno-fentry -fno-pic -fno-shrink-wrap" } 
*/
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } 
} */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**     .byte   0x0f, 0x1f, 0x44, 0x00, 0x00
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-4.c 
b/gcc/testsuite/gcc.target/i386/pr120936-4.c
new file mode 100644
index 000000000000..2420f0ba0b40
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-4.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target fpic } } */
+/* { dg-options "-O2 -pg -mno-fentry -mrecord-mcount -fno-pic 
-fno-shrink-wrap" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } 
{^(1|\t?\.)} } } */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**1:   call    mcount
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-5.c 
b/gcc/testsuite/gcc.target/i386/pr120936-5.c
new file mode 100644
index 000000000000..20ecd37790d5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-5.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target fpic } } */
+/* { dg-options "-O2 -pg -mrecord-mcount -mno-fentry -fpic -fno-shrink-wrap" } 
*/
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } 
{^(1|\t?\.)} } } */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**1:   call    mcount@PLT
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-6.c 
b/gcc/testsuite/gcc.target/i386/pr120936-6.c
new file mode 100644
index 000000000000..6e2290fda6e8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-6.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target fpic } } */
+/* { dg-options "-O2 -mrecord-mcount -mnop-mcount -pg -mno-fentry -fno-pic 
-fno-shrink-wrap" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } 
{^(1|\t?\.)} } } */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**1:   .byte   0x0f, 0x1f, 0x44, 0x00, 0x00
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-7.c 
b/gcc/testsuite/gcc.target/i386/pr120936-7.c
new file mode 100644
index 000000000000..0c864671b060
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-7.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target { *-*-linux* && { ! ia32 } } } } */
+/* { dg-options "-O2 -pg -mno-fentry -fpic -fno-plt -fno-shrink-wrap" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } 
} */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**     call    \*mcount@GOTPCREL\(%rip\)
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-8.c 
b/gcc/testsuite/gcc.target/i386/pr120936-8.c
new file mode 100644
index 000000000000..3f86781bcbda
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-8.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target { *-*-linux* && { ! ia32 } } } } */
+/* { dg-options "-O2 -pg -mrecord-mcount -mno-fentry -fpic -fno-plt 
-fno-shrink-wrap" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } 
{^(1|\t?\.)} } } */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**1:   call    \*mcount@GOTPCREL\(%rip\)
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr120936-9.c 
b/gcc/testsuite/gcc.target/i386/pr120936-9.c
new file mode 100644
index 000000000000..3f4b38724e44
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr120936-9.c
@@ -0,0 +1,19 @@
+/* { dg-do compile { target { fpic && lp64 } } } */
+/* { dg-options "-O2 -mcmodel=large -pg -mno-fentry -fno-pic -fno-shrink-wrap" 
} */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc').  */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } 
} */
+
+/*
+**foo:
+**.LFB[0-9]+:
+**...
+**     .cfi_.*
+**     movabsq \$mcount, %r10
+**     call    \*%r10
+**...
+*/
+
+void
+foo (void)
+{
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr93492-3.c 
b/gcc/testsuite/gcc.target/i386/pr93492-3.c
index b68da30bd365..cdca595bd970 100644
--- a/gcc/testsuite/gcc.target/i386/pr93492-3.c
+++ b/gcc/testsuite/gcc.target/i386/pr93492-3.c
@@ -10,4 +10,4 @@ f10_endbr (void)
 {
 }
 
-/* { dg-final { scan-assembler 
"\t\.cfi_startproc\n\tendbr(32|64)\n.*\.LPFE0:\n\tnop\n1:\tcall\t\[^\n\]*__fentry__\[^\n\]*\n\tret\n"
 } } */
+/* { dg-final { scan-assembler 
"\t\.cfi_startproc\n\tendbr(32|64)\n.*\.LPFE0:\n\tnop\n\tcall\t\[^\n\]*__fentry__\[^\n\]*\n\tret\n"
 } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr93492-5.c 
b/gcc/testsuite/gcc.target/i386/pr93492-5.c
index ee9849ae852e..cc71f67c130a 100644
--- a/gcc/testsuite/gcc.target/i386/pr93492-5.c
+++ b/gcc/testsuite/gcc.target/i386/pr93492-5.c
@@ -9,4 +9,4 @@ foo (void)
 {
 }
 
-/* { dg-final { scan-assembler 
"\t\.cfi_startproc\n.*\.LPFE0:\n\tnop\n1:\tcall\t\[^\n\]*__fentry__\[^\n\]*\n\tret\n"
 } } */
+/* { dg-final { scan-assembler 
"\t\.cfi_startproc\n.*\.LPFE0:\n\tnop\n\tcall\t\[^\n\]*__fentry__\[^\n\]*\n\tret\n"
 } } */

Reply via email to