diff mbox series

[v3,rs6000] Enable absolute jump table for PPC AIX and Linux

Message ID 3cfd526a-174b-5d09-6683-20a7fb002e04@linux.ibm.com
State New
Headers show
Series [v3,rs6000] Enable absolute jump table for PPC AIX and Linux | expand

Commit Message

HAO CHEN GUI March 1, 2022, 5:41 a.m. UTC
Hi,
   This patch enables absolute jump tables on PPC AIX and Linux. For AIX, the jump
table is placed in data section. For Linux, it is placed in RELRO section when
relocation is needed.

   Bootstrapped and tested on AIX,Linux BE and LE with no regressions. Is this okay for trunk?
Any recommendations? Thanks a lot.

ChangeLog
2022-03-01 Haochen Gui <guihaoc@linux.ibm.com>

gcc/
	* config/rs6000/aix.h (JUMP_TABLES_IN_TEXT_SECTION): Define.
	* config/rs6000/linux64.h (JUMP_TABLES_IN_TEXT_SECTION): Likewise.
	* config/rs6000/rs6000.cc (rs6000_option_override_internal): Enable
	absolute jump tables for AIX and Linux.
	(rs6000_xcoff_function_rodata_section): Implement.
	* config/rs6000/xcoff.h (TARGET_ASM_FUNCTION_RODATA_SECTION): Define.

gcc/testsuite
	* gcc.target/powerpc/absolute-jump-table-section.c: New.


patch.diff

Comments

David Edelsohn March 2, 2022, 12:38 a.m. UTC | #1
On Tue, Mar 1, 2022 at 12:41 AM HAO CHEN GUI <guihaoc@linux.ibm.com> wrote:
>
> Hi,
>    This patch enables absolute jump tables on PPC AIX and Linux. For AIX, the jump
> table is placed in data section. For Linux, it is placed in RELRO section when
> relocation is needed.
>
>    Bootstrapped and tested on AIX,Linux BE and LE with no regressions. Is this okay for trunk?
> Any recommendations? Thanks a lot.

Hi, Haochen

Thanks for working on this patch and for revising it.  The patch looks
okay now, but it is not appropriate for the current bug fixing stage
in GCC development -- it needs to wait for GCC Stage 1 later this
Spring.

Also the current code uses the default readonly data section for the
jump table.  The new rs6000_xcoff_function_rodata_section follows the
existing simple behavior, which is correct, but it should support
named data sections instead of placing everything in the default
".data" section, similar to the default ELF code.  I will work on
that.

Thanks, David

>
> ChangeLog
> 2022-03-01 Haochen Gui <guihaoc@linux.ibm.com>
>
> gcc/
>         * config/rs6000/aix.h (JUMP_TABLES_IN_TEXT_SECTION): Define.
>         * config/rs6000/linux64.h (JUMP_TABLES_IN_TEXT_SECTION): Likewise.
>         * config/rs6000/rs6000.cc (rs6000_option_override_internal): Enable
>         absolute jump tables for AIX and Linux.
>         (rs6000_xcoff_function_rodata_section): Implement.
>         * config/rs6000/xcoff.h (TARGET_ASM_FUNCTION_RODATA_SECTION): Define.
>
> gcc/testsuite
>         * gcc.target/powerpc/absolute-jump-table-section.c: New.
>
>
> patch.diff
> diff --git a/gcc/config/rs6000/aix.h b/gcc/config/rs6000/aix.h
> index ad3238bf09a..cf0708aa08b 100644
> --- a/gcc/config/rs6000/aix.h
> +++ b/gcc/config/rs6000/aix.h
> @@ -251,9 +251,9 @@
>  #define BLOCK_REG_PADDING(MODE, TYPE, FIRST) \
>    (!(FIRST) ? PAD_UPWARD : targetm.calls.function_arg_padding (MODE, TYPE))
>
> -/* Indicate that jump tables go in the text section.  */
> +/* Indicate that jump tables go in the data section.  */
>
> -#define JUMP_TABLES_IN_TEXT_SECTION 1
> +#define JUMP_TABLES_IN_TEXT_SECTION 0
>
>  /* Define any extra SPECS that the compiler needs to generate.  */
>  #undef  SUBTARGET_EXTRA_SPECS
> diff --git a/gcc/config/rs6000/linux64.h b/gcc/config/rs6000/linux64.h
> index b2a7afabc73..440e0fde52b 100644
> --- a/gcc/config/rs6000/linux64.h
> +++ b/gcc/config/rs6000/linux64.h
> @@ -237,9 +237,9 @@ extern int dot_symbols;
>  #define TARGET_ALIGN_NATURAL 1
>  #endif
>
> -/* Indicate that jump tables go in the text section.  */
> +/* Indicate that jump tables go in the rodata or RELRO section.  */
>  #undef  JUMP_TABLES_IN_TEXT_SECTION
> -#define JUMP_TABLES_IN_TEXT_SECTION TARGET_64BIT
> +#define JUMP_TABLES_IN_TEXT_SECTION 0
>
>  /* The linux ppc64 ABI isn't explicit on whether aggregates smaller
>     than a doubleword should be padded upward or downward.  You could
> diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
> index bc3ef0721a4..07f78d3a05b 100644
> --- a/gcc/config/rs6000/rs6000.cc
> +++ b/gcc/config/rs6000/rs6000.cc
> @@ -4954,6 +4954,10 @@ rs6000_option_override_internal (bool global_init_p)
>      warning (0, "%qs is deprecated and not recommended in any circumstances",
>              "-mno-speculate-indirect-jumps");
>
> +  /* Enable absolute jump tables for AIX and Linux.  */
> +  if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
> +    rs6000_relative_jumptables = 0;
> +
>    return ret;
>  }
>
> @@ -21419,6 +21423,16 @@ rs6000_xcoff_visibility (tree decl)
>    enum symbol_visibility vis = DECL_VISIBILITY (decl);
>    return visibility_types[vis];
>  }
> +
> +static section *
> +rs6000_xcoff_function_rodata_section (tree decl ATTRIBUTE_UNUSED,
> +                                     bool relocatable)
> +{
> +  if (relocatable)
> +    return data_section;
> +  else
> +    return readonly_data_section;
> +}
>  #endif
>
>
> diff --git a/gcc/config/rs6000/xcoff.h b/gcc/config/rs6000/xcoff.h
> index cd0f99cb9c6..0dacd86eed9 100644
> --- a/gcc/config/rs6000/xcoff.h
> +++ b/gcc/config/rs6000/xcoff.h
> @@ -98,7 +98,7 @@
>  #define TARGET_ASM_SELECT_SECTION  rs6000_xcoff_select_section
>  #define TARGET_ASM_SELECT_RTX_SECTION  rs6000_xcoff_select_rtx_section
>  #define TARGET_ASM_UNIQUE_SECTION  rs6000_xcoff_unique_section
> -#define TARGET_ASM_FUNCTION_RODATA_SECTION default_no_function_rodata_section
> +#define TARGET_ASM_FUNCTION_RODATA_SECTION rs6000_xcoff_function_rodata_section
>  #define TARGET_STRIP_NAME_ENCODING  rs6000_xcoff_strip_name_encoding
>  #define TARGET_SECTION_TYPE_FLAGS  rs6000_xcoff_section_type_flags
>  #ifdef HAVE_AS_TLS
> diff --git a/gcc/testsuite/gcc.target/powerpc/absolute-jump-table-section.c b/gcc/testsuite/gcc.target/powerpc/absolute-jump-table-section.c
> new file mode 100644
> index 00000000000..688a6f42836
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/powerpc/absolute-jump-table-section.c
> @@ -0,0 +1,29 @@
> +/* { dg-do compile { target { *-*-aix* || *-*-linux* } } } */
> +/* { dg-options "-O2 -fPIC" } */
> +
> +/* For Linux, the absolute jump tables are placed in .data.rel.ro.local.
> +   For AIX, they're placed in data section.  */
> +
> +int a;
> +
> +int foo (char c)
> +{
> +  switch (c) {
> +  case 'C':
> +    return a;
> +  case 'D':
> +    return 3;
> +  case 'A':
> +    return 1;
> +  case '%':
> +    return -2;
> +  case '#':
> +    return a+4;
> +  default:
> +    return 100;
> +  }
> +}
> +
> +/* { dg-final { scan-assembler "\\.section\[ \t\]\\.data\\.rel\\.ro\\.local" { target *-*-linux* } } } */
> +/* { dg-final { scan-assembler-times "\\.csect \\.data" 2 { target *-*-aix* } } } */
> +/* { dg-final { scan-assembler-times "\\.csect \\.text" 5 { target *-*-aix* } } } */
diff mbox series

Patch

diff --git a/gcc/config/rs6000/aix.h b/gcc/config/rs6000/aix.h
index ad3238bf09a..cf0708aa08b 100644
--- a/gcc/config/rs6000/aix.h
+++ b/gcc/config/rs6000/aix.h
@@ -251,9 +251,9 @@ 
 #define BLOCK_REG_PADDING(MODE, TYPE, FIRST) \
   (!(FIRST) ? PAD_UPWARD : targetm.calls.function_arg_padding (MODE, TYPE))

-/* Indicate that jump tables go in the text section.  */
+/* Indicate that jump tables go in the data section.  */

-#define JUMP_TABLES_IN_TEXT_SECTION 1
+#define JUMP_TABLES_IN_TEXT_SECTION 0

 /* Define any extra SPECS that the compiler needs to generate.  */
 #undef  SUBTARGET_EXTRA_SPECS
diff --git a/gcc/config/rs6000/linux64.h b/gcc/config/rs6000/linux64.h
index b2a7afabc73..440e0fde52b 100644
--- a/gcc/config/rs6000/linux64.h
+++ b/gcc/config/rs6000/linux64.h
@@ -237,9 +237,9 @@  extern int dot_symbols;
 #define TARGET_ALIGN_NATURAL 1
 #endif

-/* Indicate that jump tables go in the text section.  */
+/* Indicate that jump tables go in the rodata or RELRO section.  */
 #undef  JUMP_TABLES_IN_TEXT_SECTION
-#define JUMP_TABLES_IN_TEXT_SECTION TARGET_64BIT
+#define JUMP_TABLES_IN_TEXT_SECTION 0

 /* The linux ppc64 ABI isn't explicit on whether aggregates smaller
    than a doubleword should be padded upward or downward.  You could
diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index bc3ef0721a4..07f78d3a05b 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -4954,6 +4954,10 @@  rs6000_option_override_internal (bool global_init_p)
     warning (0, "%qs is deprecated and not recommended in any circumstances",
 	     "-mno-speculate-indirect-jumps");

+  /* Enable absolute jump tables for AIX and Linux.  */
+  if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
+    rs6000_relative_jumptables = 0;
+
   return ret;
 }

@@ -21419,6 +21423,16 @@  rs6000_xcoff_visibility (tree decl)
   enum symbol_visibility vis = DECL_VISIBILITY (decl);
   return visibility_types[vis];
 }
+
+static section *
+rs6000_xcoff_function_rodata_section (tree decl ATTRIBUTE_UNUSED,
+				      bool relocatable)
+{
+  if (relocatable)
+    return data_section;
+  else
+    return readonly_data_section;
+}
 #endif


diff --git a/gcc/config/rs6000/xcoff.h b/gcc/config/rs6000/xcoff.h
index cd0f99cb9c6..0dacd86eed9 100644
--- a/gcc/config/rs6000/xcoff.h
+++ b/gcc/config/rs6000/xcoff.h
@@ -98,7 +98,7 @@ 
 #define TARGET_ASM_SELECT_SECTION  rs6000_xcoff_select_section
 #define TARGET_ASM_SELECT_RTX_SECTION  rs6000_xcoff_select_rtx_section
 #define TARGET_ASM_UNIQUE_SECTION  rs6000_xcoff_unique_section
-#define TARGET_ASM_FUNCTION_RODATA_SECTION default_no_function_rodata_section
+#define TARGET_ASM_FUNCTION_RODATA_SECTION rs6000_xcoff_function_rodata_section
 #define TARGET_STRIP_NAME_ENCODING  rs6000_xcoff_strip_name_encoding
 #define TARGET_SECTION_TYPE_FLAGS  rs6000_xcoff_section_type_flags
 #ifdef HAVE_AS_TLS
diff --git a/gcc/testsuite/gcc.target/powerpc/absolute-jump-table-section.c b/gcc/testsuite/gcc.target/powerpc/absolute-jump-table-section.c
new file mode 100644
index 00000000000..688a6f42836
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/absolute-jump-table-section.c
@@ -0,0 +1,29 @@ 
+/* { dg-do compile { target { *-*-aix* || *-*-linux* } } } */
+/* { dg-options "-O2 -fPIC" } */
+
+/* For Linux, the absolute jump tables are placed in .data.rel.ro.local.
+   For AIX, they're placed in data section.  */
+
+int a;
+
+int foo (char c)
+{
+  switch (c) {
+  case 'C':
+    return a;
+  case 'D':
+    return 3;
+  case 'A':
+    return 1;
+  case '%':
+    return -2;
+  case '#':
+    return a+4;
+  default:
+    return 100;
+  }
+}
+
+/* { dg-final { scan-assembler "\\.section\[ \t\]\\.data\\.rel\\.ro\\.local" { target *-*-linux* } } } */
+/* { dg-final { scan-assembler-times "\\.csect \\.data" 2 { target *-*-aix* } } } */
+/* { dg-final { scan-assembler-times "\\.csect \\.text" 5 { target *-*-aix* } } } */