]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
x86: Emit label only for __mcount_loc section
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 3 Jul 2025 02:13:48 +0000 (10:13 +0800)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 3 Jul 2025 10:19:33 +0000 (18:19 +0800)
commit ecc81e33123d7ac9c11742161e128858d844b99d
Author: Andi Kleen <ak@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.tools@gmail.com>
15 files changed:
gcc/config/i386/i386.cc
gcc/testsuite/gcc.target/i386/pr120936-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-10.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-11.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-12.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-3.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-4.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-5.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-6.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-7.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-8.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr120936-9.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr93492-3.c
gcc/testsuite/gcc.target/i386/pr93492-5.c

index 24aedc136a699a468c9c82510852718bf2ba1bc0..b64175d6c93980a96ecadc3d0120824232f85999 100644 (file)
@@ -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 (file)
index 0000000..a20680d
--- /dev/null
@@ -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 (file)
index 0000000..ab95b08
--- /dev/null
@@ -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 (file)
index 0000000..3e39dfe
--- /dev/null
@@ -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 (file)
index 0000000..b5a2aac
--- /dev/null
@@ -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 (file)
index 0000000..0835658
--- /dev/null
@@ -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 (file)
index 0000000..dc0a8f1
--- /dev/null
@@ -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 (file)
index 0000000..2420f0b
--- /dev/null
@@ -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 (file)
index 0000000..20ecd37
--- /dev/null
@@ -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 (file)
index 0000000..6e2290f
--- /dev/null
@@ -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 (file)
index 0000000..0c86467
--- /dev/null
@@ -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 (file)
index 0000000..3f86781
--- /dev/null
@@ -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 (file)
index 0000000..3f4b387
--- /dev/null
@@ -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)
+{
+}
index b68da30bd36566465ac7ad805bfd639e5236f751..cdca595bd97003c67f2ecb4bed134b331866ad11 100644 (file)
@@ -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" } } */
index ee9849ae852ed460d2df7c8eb4f35d360c3f76f1..cc71f67c130a2a3f136477387ec2b11e7ca52c17 100644 (file)
@@ -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" } } */