commit ecc81e33123d7ac9c11742161e128858d844b99d (HEAD) 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. OK for master? Thanks. -- H.J.
From dee88812fc0ca372107224fb3460e133efe8822d Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.to...@gmail.com> Date: Thu, 3 Jul 2025 10:13:48 +0800 Subject: [PATCH] x86: Emit label only for __mcount_loc section commit ecc81e33123d7ac9c11742161e128858d844b99d (HEAD) 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> --- gcc/config/i386/i386.cc | 55 +++++++++++++-------- 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, 264 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-1.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-10.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-11.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-12.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-2.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-3.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-4.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-5.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-6.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-7.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-8.c create mode 100644 gcc/testsuite/gcc.target/i386/pr120936-9.c diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index 9657c6ae31f..5c888b52c1c 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -23685,19 +23685,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 @@ -23782,6 +23784,16 @@ 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; + if (fentry_section_p) + label = "1:"; + else + label = ""; + if (current_fentry_name (&mcount_name)) ; else if (fentry_name) @@ -23817,11 +23829,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 @@ -23862,21 +23875,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) { @@ -23891,11 +23904,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 { @@ -23908,12 +23923,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 00000000000..a20680d8548 --- /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 00000000000..ab95b087ec9 --- /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 00000000000..3e39dfe0d37 --- /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 00000000000..b5a2aaced5d --- /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 00000000000..08356580165 --- /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 00000000000..dc0a8f1e424 --- /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 00000000000..2420f0ba0b4 --- /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 00000000000..20ecd37790d --- /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 00000000000..6e2290fda6e --- /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 00000000000..0c864671b06 --- /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 00000000000..3f86781bcbd --- /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 00000000000..3f4b38724e4 --- /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 b68da30bd36..cdca595bd97 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 ee9849ae852..cc71f67c130 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" } } */ -- 2.50.0