On Sat, Nov 7, 2020 at 7:47 AM H.J. Lu <hjl.to...@gmail.com> wrote:
>
> On Sat, Oct 31, 2020 at 5:01 AM H.J. Lu <hjl.to...@gmail.com> wrote:
> >
> > On Fri, Oct 23, 2020 at 5:41 AM H.J. Lu <hjl.to...@gmail.com> wrote:
> > >
> > > On Fri, Oct 2, 2020 at 6:00 AM H.J. Lu <hjl.to...@gmail.com> wrote:
> > > >
> > > > On Thu, Feb 6, 2020 at 6:57 PM H.J. Lu <hjl.to...@gmail.com> wrote:
> > > > >
> > > > > This commit in GNU binutils 2.35:
> > > > >
> > > > > https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=b7d072167715829eed0622616f6ae0182900de3e
> > > > >
> > > > > added the section flag 'o' to .section directive:
> > > > >
> > > > > .section __patchable_function_entries,"awo",@progbits,foo
> > > > >
> > > > > which specifies the symbol name which the section references.  
> > > > > Assembler
> > > > > creates a unique __patchable_function_entries section with the 
> > > > > section,
> > > > > where foo is defined, as its linked-to section.  Linker keeps a 
> > > > > section
> > > > > if its linked-to section is kept during garbage collection.
> > > > >
> > > > > This patch checks assembler support for the section flag 'o' and uses
> > > > > it to implement __patchable_function_entries section.  Since Solaris 
> > > > > may
> > > > > use GNU assembler with Solairs ld.  Even if GNU assembler supports the
> > > > > section flag 'o', it doesn't mean that Solairs ld supports it.  This
> > > > > feature is disabled for Solairs targets.
> > > > >
> > > > > gcc/
> > > > >
> > > > >         PR middle-end/93195
> > > > >         PR middle-end/93197
> > > > >         * configure.ac (HAVE_GAS_SECTION_LINK_ORDER): New.  Define if
> > > > >         the assembler supports the section flag 'o' for specifying
> > > > >         section with link-order.
> > > > >         * dwarf2out.c (output_comdat_type_unit): Pass 0 as flags2
> > > > >         to targetm.asm_out.named_section.
> > > > >         * config/sol2.c (solaris_elf_asm_comdat_section): Likewise.
> > > > >         * output.h (SECTION2_LINK_ORDER): New.
> > > > >         (switch_to_section): Add an unsigned int argument.
> > > > >         (default_no_named_section): Likewise.
> > > > >         (default_elf_asm_named_section): Likewise.
> > > > >         * target.def (asm_out.named_section): Likewise.
> > > > >         * targhooks.c (default_print_patchable_function_entry): Pass
> > > > >         current_function_decl to get_section and SECTION2_LINK_ORDER
> > > > >         to switch_to_section.
> > > > >         * varasm.c (default_no_named_section): Add an unsigned int
> > > > >         argument.
> > > > >         (default_elf_asm_named_section): Add an unsigned int argument,
> > > > >         flags2.  Use 'o' flag for SECTION2_LINK_ORDER if assembler
> > > > >         supports it.
> > > > >         (switch_to_section): Add an unsigned int argument and pass it
> > > > >         to targetm.asm_out.named_section.
> > > > >         (handle_vtv_comdat_section): Pass 0 to
> > > > >         targetm.asm_out.named_section.
> > > > >         * config.in: Regenerated.
> > > > >         * configure: Likewise.
> > > > >         * doc/tm.texi: Likewise.
> > > > >
> > > > > gcc/testsuite/
> > > > >
> > > > >         PR middle-end/93195
> > > > >         * g++.dg/pr93195a.C: New test.
> > > > >         * g++.dg/pr93195b.C: Likewise.
> > > > >         * lib/target-supports.exp
> > > > >         (check_effective_target_o_flag_in_section): New proc.
> > > >
> > > > PING
> > > >
> > > > https://gcc.gnu.org/pipermail/gcc-patches/2020-February/539963.html
> > >
> > > PING.
> > >
> >
> > PING.
> >
>
> PING.

Here is a simpler patch.  OK for master?

This commit in GNU binutils 2.35:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=b7d072167715
829eed0622616f6ae0182900de3e

added the section flag 'o' to .section directive:

.section __patchable_function_entries,"awo",@progbits,foo

which specifies the symbol name which the section references.  Assembler
creates a unique __patchable_function_entries section with the section,
where foo is defined, as its linked-to section.  Linker keeps a section
if its linked-to section is kept during garbage collection.

This patch checks assembler support for the section flag 'o' and uses
it to implement __patchable_function_entries section.  Since Solaris may
use GNU assembler with Solairs ld.  Even if GNU assembler supports the
section flag 'o', it doesn't mean that Solairs ld supports it.  This
feature is disabled for Solairs targets.

gcc/

PR middle-end/93195
PR middle-end/93197
* configure.ac (HAVE_GAS_SECTION_LINK_ORDER): New.  Define 1 if
the assembler supports the section flag 'o' for specifying
section with link-order.
* output.h (SECTION_LINK_ORDER): New.  Defined to 0x4000000.
(SECTION_MACH_DEP): Changed from 0x4000000 to 0x8000000.
* targhooks.c (default_print_patchable_function_entry): Pass
SECTION_LINK_ORDER to switch_to_section if the section flag 'o'
works.  Pass current_function_decl to switch_to_section.
* varasm.c (default_elf_asm_named_section): Use 'o' flag for
SECTION_LINK_ORDER if assembler supports it.
* config.in: Regenerated.
* configure: Likewise.

gcc/testsuite/

PR middle-end/93195
* g++.dg/pr93195a.C: New test.
* g++.dg/pr93195b.C: Likewise.
* lib/target-supports.exp
(check_effective_target_o_flag_in_section): New proc.

-- 
H.J.
From 3d5e3d0bac782fcbb8ef6d6f65ebc857dd59e880 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 3 Feb 2020 11:55:43 -0800
Subject: [PATCH] Use the section flag 'o' for __patchable_function_entries

This commit in GNU binutils 2.35:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=b7d072167715829eed0622616f6ae0182900de3e

added the section flag 'o' to .section directive:

.section __patchable_function_entries,"awo",@progbits,foo

which specifies the symbol name which the section references.  Assembler
creates a unique __patchable_function_entries section with the section,
where foo is defined, as its linked-to section.  Linker keeps a section
if its linked-to section is kept during garbage collection.

This patch checks assembler support for the section flag 'o' and uses
it to implement __patchable_function_entries section.  Since Solaris may
use GNU assembler with Solairs ld.  Even if GNU assembler supports the
section flag 'o', it doesn't mean that Solairs ld supports it.  This
feature is disabled for Solairs targets.

gcc/

	PR middle-end/93195
	PR middle-end/93197
	* configure.ac (HAVE_GAS_SECTION_LINK_ORDER): New.  Define 1 if
	the assembler supports the section flag 'o' for specifying
	section with link-order.
	* output.h (SECTION_LINK_ORDER): New.  Defined to 0x4000000.
	(SECTION_MACH_DEP): Changed from 0x4000000 to 0x8000000.
	* targhooks.c (default_print_patchable_function_entry): Pass
	SECTION_LINK_ORDER to switch_to_section if the section flag 'o'
	works.  Pass current_function_decl to switch_to_section.
	* varasm.c (default_elf_asm_named_section): Use 'o' flag for
	SECTION_LINK_ORDER if assembler supports it.
	* config.in: Regenerated.
	* configure: Likewise.

gcc/testsuite/

	PR middle-end/93195
	* g++.dg/pr93195a.C: New test.
	* g++.dg/pr93195b.C: Likewise.
	* lib/target-supports.exp
	(check_effective_target_o_flag_in_section): New proc.
---
 gcc/config.in                         |  6 ++++
 gcc/configure                         | 52 +++++++++++++++++++++++++++
 gcc/configure.ac                      | 22 ++++++++++++
 gcc/output.h                          |  6 +++-
 gcc/targhooks.c                       |  6 +++-
 gcc/testsuite/g++.dg/pr93195a.C       | 27 ++++++++++++++
 gcc/testsuite/g++.dg/pr93195b.C       | 14 ++++++++
 gcc/testsuite/lib/target-supports.exp | 40 +++++++++++++++++++++
 gcc/varasm.c                          | 10 ++++++
 9 files changed, 181 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr93195a.C
 create mode 100644 gcc/testsuite/g++.dg/pr93195b.C

diff --git a/gcc/config.in b/gcc/config.in
index 6fae7b769b8..415efea4069 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -1364,6 +1364,12 @@
 #endif
 
 
+/* Define 0/1 if your assembler supports 'o' flag in .section directive. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_GAS_SECTION_LINK_ORDER
+#endif
+
+
 /* Define 0/1 if your assembler supports marking sections with SHF_MERGE flag.
    */
 #ifndef USED_FOR_TARGET
diff --git a/gcc/configure b/gcc/configure
index ba69f73ed9c..24e65b112f4 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -24272,6 +24272,58 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+# Test if the assembler supports the section flag 'o' for specifying
+# section with link-order.
+case "${target}" in
+  # Solaris may use GNU assembler with Solairs ld.  Even if GNU
+  # assembler supports the section flag 'o', it doesn't mean that
+  # Solairs ld supports it.
+  *-*-solaris2*)
+    gcc_cv_as_section_link_order=no
+    ;;
+  *)
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for section 'o' flag" >&5
+$as_echo_n "checking assembler for section 'o' flag... " >&6; }
+if ${gcc_cv_as_section_link_order+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  gcc_cv_as_section_link_order=no
+    if test $in_tree_gas = yes; then
+    if test $gcc_cv_gas_vers -ge `expr \( \( 2 \* 1000 \) + 35 \) \* 1000 + 0`
+  then gcc_cv_as_section_link_order=yes
+fi
+  elif test x$gcc_cv_as != x; then
+    $as_echo '.section .foo,"a"
+.byte 0
+.section __patchable_function_entries,"awo",%progbits,.foo
+.byte 0' > conftest.s
+    if { ac_try='$gcc_cv_as $gcc_cv_as_flags --fatal-warnings -o conftest.o conftest.s >&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+    then
+	gcc_cv_as_section_link_order=yes
+    else
+      echo "configure: failed program was" >&5
+      cat conftest.s >&5
+    fi
+    rm -f conftest.o conftest.s
+  fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_as_section_link_order" >&5
+$as_echo "$gcc_cv_as_section_link_order" >&6; }
+
+
+    ;;
+esac
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_GAS_SECTION_LINK_ORDER `if test $gcc_cv_as_section_link_order = yes; then echo 1; else echo 0; fi`
+_ACEOF
+
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for section merging support" >&5
 $as_echo_n "checking assembler for section merging support... " >&6; }
 if ${gcc_cv_as_shf_merge+:} false; then :
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 28d96d1cba3..7d66a87d553 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -3256,6 +3256,28 @@ AC_DEFINE_UNQUOTED(HAVE_GAS_SECTION_EXCLUDE,
   [`if test $gcc_cv_as_section_exclude_e = yes || test $gcc_cv_as_section_exclude_hash = yes; then echo 1; else echo 0; fi`],
 [Define if your assembler supports specifying the exclude section flag.])
 
+# Test if the assembler supports the section flag 'o' for specifying
+# section with link-order.
+case "${target}" in
+  # Solaris may use GNU assembler with Solairs ld.  Even if GNU
+  # assembler supports the section flag 'o', it doesn't mean that
+  # Solairs ld supports it.
+  *-*-solaris2*)
+    gcc_cv_as_section_link_order=no
+    ;;
+  *)
+    gcc_GAS_CHECK_FEATURE([section 'o' flag], gcc_cv_as_section_link_order,
+      [2,35,0], [--fatal-warnings],
+      [.section .foo,"a"
+.byte 0
+.section __patchable_function_entries,"awo",%progbits,.foo
+.byte 0])
+    ;;
+esac
+AC_DEFINE_UNQUOTED(HAVE_GAS_SECTION_LINK_ORDER,
+  [`if test $gcc_cv_as_section_link_order = yes; then echo 1; else echo 0; fi`],
+  [Define 0/1 if your assembler supports 'o' flag in .section directive.])
+
 gcc_GAS_CHECK_FEATURE(section merging support, gcc_cv_as_shf_merge,
  [elf,2,12,0], [--fatal-warnings],
  [.section .rodata.str, "aMS", @progbits, 1])
diff --git a/gcc/output.h b/gcc/output.h
index b44c1bd6fc7..3b4d450df19 100644
--- a/gcc/output.h
+++ b/gcc/output.h
@@ -381,7 +381,11 @@ extern void no_asm_to_stream (FILE *);
 #define SECTION_COMMON   0x800000	/* contains common data */
 #define SECTION_RELRO	 0x1000000	/* data is readonly after relocation processing */
 #define SECTION_EXCLUDE  0x2000000	/* discarded by the linker */
-#define SECTION_MACH_DEP 0x4000000	/* subsequent bits reserved for target */
+#define SECTION_LINK_ORDER 0x4000000	/* section needs link-order.  */
+
+/* NB: The maximum SECTION_MACH_DEP is 0x10000000 since AVR needs 4 bits
+   in SECTION_MACH_DEP.  */
+#define SECTION_MACH_DEP 0x8000000	/* subsequent bits reserved for target */
 
 /* This SECTION_STYLE is used for unnamed sections that we can switch
    to using a special assembler directive.  */
diff --git a/gcc/targhooks.c b/gcc/targhooks.c
index 5b68a2ad7d4..ecb7d43129b 100644
--- a/gcc/targhooks.c
+++ b/gcc/targhooks.c
@@ -1860,8 +1860,12 @@ default_print_patchable_function_entry (FILE *file,
       patch_area_number++;
       ASM_GENERATE_INTERNAL_LABEL (buf, "LPFE", patch_area_number);
 
+      unsigned int flags = SECTION_WRITE | SECTION_RELRO;
+#if HAVE_GAS_SECTION_LINK_ORDER
+      flags |= SECTION_LINK_ORDER;
+#endif
       switch_to_section (get_section ("__patchable_function_entries",
-				      SECTION_WRITE | SECTION_RELRO, NULL));
+				      flags, current_function_decl));
       assemble_align (POINTER_SIZE);
       fputs (asm_op, file);
       assemble_name_raw (file, buf);
diff --git a/gcc/testsuite/g++.dg/pr93195a.C b/gcc/testsuite/g++.dg/pr93195a.C
new file mode 100644
index 00000000000..26d265da74e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr93195a.C
@@ -0,0 +1,27 @@
+/* { dg-do link { target { ! { nvptx*-*-* visium-*-* } } } } */
+// { dg-require-effective-target o_flag_in_section }
+/* { dg-options "-O0 -fpatchable-function-entry=1" } */
+/* { dg-additional-options "-fno-pie" { target sparc*-*-* } } */
+/* { dg-additional-sources pr93195b.C } */
+
+extern void bar1 (void);
+
+inline void
+foo (void)
+{
+}
+
+void
+bar (void)
+{
+  foo ();
+  bar1 ();
+}
+
+int
+main ()
+{
+  bar ();
+  return 0;
+}
+
diff --git a/gcc/testsuite/g++.dg/pr93195b.C b/gcc/testsuite/g++.dg/pr93195b.C
new file mode 100644
index 00000000000..303d8588c0f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr93195b.C
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { ! { nvptx*-*-* visium-*-* } } } } */
+/* { dg-options "-O0 -fpatchable-function-entry=1" } */
+/* { dg-additional-options "-fno-pie" { target sparc*-*-* } } */
+
+inline void
+foo (void)
+{
+}
+
+void
+bar1 (void)
+{
+  foo ();
+}
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
index 22acda2a74f..0b2b38adc2c 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -10650,3 +10650,43 @@ proc check_effective_target_no_fsanitize_address {} {
     }
     return 0;
 }
+
+# Return 1 if this target supports 'o' flag in .section directive, 0
+# otherwise.  Cache the result.
+
+proc check_effective_target_o_flag_in_section { } {
+    global tool
+    global GCC_UNDER_TEST
+
+    # Need auto-host.h to check linker support.
+    if { ![file exists ../../auto-host.h ] } {
+	return 0
+    }
+
+    return [check_cached_effective_target o_flag_in_section {
+
+	set src pie[pid].c
+	set obj pie[pid].o
+
+	set f [open $src "w"]
+	puts $f "#include \"../../auto-host.h\""
+	puts $f "#if HAVE_GAS_SECTION_LINK_ORDER == 0"
+	puts $f "# error Assembler does not support 'o' flag in .section directive."
+	puts $f "#endif"
+	close $f
+
+	verbose "check_effective_target_o_flag_in_section compiling testfile $src" 2
+	set lines [${tool}_target_compile $src $obj object ""]
+
+	file delete $src
+	file delete $obj
+
+	if [string match "" $lines] then {
+	    verbose "check_effective_target_o_flag_in_section testfile compilation passed" 2
+	    return 1
+	} else {
+	    verbose "check_effective_target_o_flag_in_section testfile compilation failed" 2
+	    return 0
+	}
+    }]
+}
diff --git a/gcc/varasm.c b/gcc/varasm.c
index ada99940f65..edd6a465ab0 100644
--- a/gcc/varasm.c
+++ b/gcc/varasm.c
@@ -6772,6 +6772,8 @@ default_elf_asm_named_section (const char *name, unsigned int flags,
 	*f++ = TLS_SECTION_ASM_FLAG;
       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
 	*f++ = 'G';
+      if (flags & SECTION_LINK_ORDER)
+	*f++ = 'o';
 #ifdef MACH_DEP_SECTION_ASM_FLAG
       if (flags & SECTION_MACH_DEP)
 	*f++ = MACH_DEP_SECTION_ASM_FLAG;
@@ -6804,6 +6806,14 @@ default_elf_asm_named_section (const char *name, unsigned int flags,
 
       if (flags & SECTION_ENTSIZE)
 	fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
+      if (flags & SECTION_LINK_ORDER)
+	{
+	  tree id = DECL_ASSEMBLER_NAME (decl);
+	  ultimate_transparent_alias_target (&id);
+	  const char *name = IDENTIFIER_POINTER (id);
+	  name = targetm.strip_name_encoding (name);
+	  fprintf (asm_out_file, ",%s", name);
+	}
       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
 	{
 	  if (TREE_CODE (decl) == IDENTIFIER_NODE)
-- 
2.28.0

Reply via email to