From 8b5b814d51ff73bc739c0c037ae18df07acf2d96 Mon Sep 17 00:00:00 2001
From: David Edelsohn <dje.gcc@gmail.com>
Date: Tue, 4 May 2021 09:43:40 -0400
Subject: [PATCH] aix: encode function section

AIX XCOFF symbols can be labels or qualnames (names with an appended
mapping class).  CSECTs must be declared with a mapping class.
Within an assembler file, the symbol names with and without the mapping
class are unique.  An object file symbol table only presents the symbol
name without the mapping class, but the section of the symbol depends on
the mapping class.

The AIX XCOFF assembly language does not support first class aliases.
GCC implements symbol aliases by emitting additional labels for the function
or object.  When GCC encodes sections for a DECL, it must distinguish
between the primary definition and the aliases, which don't have a
mapping class encoding.

	.globl foo[DS]
	.globl .foo
	.globl foo1
	.globl .foo1
	.csect foo[DS]
foo:
foo1:
	.long .foo, TOC[tc0] 0
	.csect .foo[PR]
.foo:
.foo1:

The CSECT foo[DS] and label foo are distinct.  foo1 is another label (alias)
for foo, and .foo1 is another label (alias) for .foo.  foo is the function
descriptor and .foo is the code.

This patch adds the [DS] mapping class to the encoding of FUNCTION_DECL
but ensures that mapping class is not added to function aliases.

rs6000_output_mi_thunk is updated to emit the function name that matches
the behavior of GCC final.c for normal functions: get_fnname_from_decl based
on the RTL name, not the DECL name.

	* config/rs6000/rs6000-call.c (rs6000_output_mi_thunk): Use
	get_fnname_from_decl for name of thunk.
	* config/rs6000/rs6000.c (rs6000_declare_alias): Use assemble_name
	and ASM_OUTPUT_LABEL.
	(rs6000_xcoff_declare_function_name): Use assemble_name and
	ASM_OUTPUT_LABEL.
	(rs6000_xcoff_declare_object_name): Use ASM_OUTPUT_LABEL.
	(rs6000_xcoff_encode_section_info): Don't add mapping class
	for aliases.  Always add [DS] mapping class to primary
	FUNCTION_DECL.
	(rs6000_asm_weaken_decl): Don't explicitly add [DS].
---
 gcc/config/rs6000/rs6000-call.c |  2 +-
 gcc/config/rs6000/rs6000.c      | 41 ++++++++++++++++-----------------
 2 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/gcc/config/rs6000/rs6000-call.c b/gcc/config/rs6000/rs6000-call.c
index 6f6dc47f0aed..c4332a618629 100644
--- a/gcc/config/rs6000/rs6000-call.c
+++ b/gcc/config/rs6000/rs6000-call.c
@@ -15077,7 +15077,7 @@ rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
 			HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
 			tree function)
 {
-  const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
+  const char *fnname = get_fnname_from_decl (thunk_fndecl);
   rtx this_rtx, funexp;
   rtx_insn *insn;
 
diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c
index 0e9cf178245c..ee15af9efa43 100644
--- a/gcc/config/rs6000/rs6000.c
+++ b/gcc/config/rs6000/rs6000.c
@@ -21468,7 +21468,7 @@ rs6000_declare_alias (struct symtab_node *n, void *d)
 	      putc ('\n', data->file);
 	    }
 	  fputs ("\t.globl ", data->file);
-	  RS6000_OUTPUT_BASENAME (data->file, buffer);
+	  assemble_name (data->file, buffer);
 	  putc ('\n', data->file);
 	}
 #ifdef ASM_WEAKEN_DECL
@@ -21491,13 +21491,12 @@ rs6000_declare_alias (struct symtab_node *n, void *d)
 	  putc ('\n', data->file);
 	}
       fputs ("\t.lglobl ", data->file);
-      RS6000_OUTPUT_BASENAME (data->file, buffer);
+      assemble_name (data->file, buffer);
       putc ('\n', data->file);
     }
   if (data->function_descriptor)
-    fputs (".", data->file);
-  RS6000_OUTPUT_BASENAME (data->file, buffer);
-  fputs (":\n", data->file);
+    putc ('.', data->file);
+  ASM_OUTPUT_LABEL (data->file, buffer);
   return false;
 }
 
@@ -21574,21 +21573,24 @@ rs6000_xcoff_declare_function_name (FILE *file, const char *name, tree decl)
       RS6000_OUTPUT_BASENAME (file, buffer);
       putc ('\n', file);
     }
+
   fputs ("\t.csect ", file);
-  RS6000_OUTPUT_BASENAME (file, buffer);
-  fputs (TARGET_32BIT ? "[DS]\n" : "[DS],3\n", file);
-  RS6000_OUTPUT_BASENAME (file, buffer);
-  fputs (":\n", file);
+  assemble_name (file, buffer);
+  fputs (TARGET_32BIT ? "\n" : ",3\n", file);
+
+  ASM_OUTPUT_LABEL (file, buffer);
+
   symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
 							&data, true);
   fputs (TARGET_32BIT ? "\t.long ." : "\t.llong .", file);
   RS6000_OUTPUT_BASENAME (file, buffer);
   fputs (", TOC[tc0], 0\n", file);
+
   in_section = NULL;
   switch_to_section (function_section (decl));
   putc ('.', file);
-  RS6000_OUTPUT_BASENAME (file, buffer);
-  fputs (":\n", file);
+  ASM_OUTPUT_LABEL (file, buffer);
+
   data.function_descriptor = true;
   symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
 							&data, true);
@@ -21683,8 +21685,7 @@ void
 rs6000_xcoff_declare_object_name (FILE *file, const char *name, tree decl)
 {
   struct declare_alias_data data = {file, false};
-  RS6000_OUTPUT_BASENAME (file, name);
-  fputs (":\n", file);
+  ASM_OUTPUT_LABEL (file, name);
   symtab_node::get_create (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
 							       &data, true);
 }
@@ -21740,20 +21741,19 @@ rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
 
   symname = XSTR (symbol, 0);
 
-  /* Append CSECT mapping class, unless the symbol already is qualified.  */
+  /* Append CSECT mapping class, unless the symbol already is qualified.
+     Aliases are implemented as labels, so the symbol name should not add
+     a mapping class.  */
   if (decl
       && DECL_P (decl)
       && VAR_OR_FUNCTION_DECL_P (decl)
-      && lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) == NULL_TREE
+      && symtab_node::get (decl)->alias == 0
       && symname[strlen (symname) - 1] != ']')
     {
       const char *smclass = NULL;
 
       if (TREE_CODE (decl) == FUNCTION_DECL)
-	{
-	  if (DECL_EXTERNAL (decl))
-	    smclass = "[DS]";
-	}
+	smclass = "[DS]";
       else if (DECL_THREAD_LOCAL_P (decl))
 	{
 	  if (bss_initializer_p (decl))
@@ -21796,8 +21796,6 @@ rs6000_asm_weaken_decl (FILE *stream, tree decl,
   if (decl && TREE_CODE (decl) == FUNCTION_DECL
       && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
     {
-      if (TARGET_XCOFF && name[strlen (name) - 1] != ']')
-	fputs ("[DS]", stream);
 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
       if (TARGET_XCOFF)
 	fputs (rs6000_xcoff_visibility (decl), stream);
@@ -21810,6 +21808,7 @@ rs6000_asm_weaken_decl (FILE *stream, tree decl,
     fputs (rs6000_xcoff_visibility (decl), stream);
 #endif
   fputc ('\n', stream);
+
   if (val)
     {
 #ifdef ASM_OUTPUT_DEF
-- 
GitLab