From a87f86a171d048ee78a9b92b5b03d86adaf5ef0a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Chigot?= <clement.chigot@atos.net>
Date: Wed, 17 Nov 2021 16:20:48 +0100
Subject: [PATCH 2/4] gas: add visibility support using GNU syntax on XCOFF

In order to ease port of GNU assembly code and especially ld testsuite,
this patch allows XCOFF to accept the usual GNU syntax for visibility.

PR 22085

gas/ChangeLog:

	* config/tc-ppc.c (ppc_GNU_visibility): New function.
	* testsuite/gas/ppc/aix.exp: Add new tests.
	* testsuite/gas/ppc/xcoff-visibility-2-32.d: New test.
	* testsuite/gas/ppc/xcoff-visibility-2-64.d: New test.
	* testsuite/gas/ppc/xcoff-visibility-2.s: New test.
---
 gas/config/tc-ppc.c                           | 38 +++++++++++++++++++
 gas/testsuite/gas/ppc/aix.exp                 |  2 +
 gas/testsuite/gas/ppc/xcoff-visibility-2-32.d | 18 +++++++++
 gas/testsuite/gas/ppc/xcoff-visibility-2-64.d | 18 +++++++++
 gas/testsuite/gas/ppc/xcoff-visibility-2.s    |  9 +++++
 5 files changed, 85 insertions(+)
 create mode 100644 gas/testsuite/gas/ppc/xcoff-visibility-2-32.d
 create mode 100644 gas/testsuite/gas/ppc/xcoff-visibility-2-64.d
 create mode 100644 gas/testsuite/gas/ppc/xcoff-visibility-2.s

diff --git a/gas/config/tc-ppc.c b/gas/config/tc-ppc.c
index 1660ba5030b..901f6dd6bb2 100644
--- a/gas/config/tc-ppc.c
+++ b/gas/config/tc-ppc.c
@@ -121,6 +121,7 @@ static void ppc_toc (int);
 static void ppc_xcoff_cons (int);
 static void ppc_vbyte (int);
 static void ppc_weak (int);
+static void ppc_GNU_visibility (int);
 #endif
 
 #ifdef OBJ_ELF
@@ -246,6 +247,11 @@ const pseudo_typeS md_pseudo_table[] =
   { "short",	ppc_xcoff_cons,	1 },
   { "vbyte",    ppc_vbyte,	0 },
   { "weak",     ppc_weak,	0 },
+
+  /* Enable GNU syntax for symbol visibility.  */
+  {"internal",  ppc_GNU_visibility, SYM_V_INTERNAL},
+  {"hidden",    ppc_GNU_visibility, SYM_V_HIDDEN},
+  {"protected", ppc_GNU_visibility, SYM_V_PROTECTED},
 #endif
 
 #ifdef OBJ_ELF
@@ -4321,6 +4327,38 @@ ppc_xcoff_get_visibility (void) {
   return 0;
 }
 
+/* Retrieve visiblity using GNU syntax.  */
+static void ppc_GNU_visibility (int visibility) {
+  int c;
+  char *name;
+  symbolS *symbolP;
+  coff_symbol_type *coffsym;
+
+  do
+    {
+      if ((name = read_symbol_name ()) == NULL)
+	break;
+      symbolP = symbol_find_or_make (name);
+      coffsym = coffsymbol (symbol_get_bfdsym (symbolP));
+
+      coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
+      coffsym->native->u.syment.n_type |= visibility;
+
+      c = *input_line_pointer;
+      if (c == ',')
+	{
+	  input_line_pointer ++;
+
+	  SKIP_WHITESPACE ();
+
+	  if (*input_line_pointer == '\n')
+	    c = '\n';
+	}
+    }
+  while (c == ',');
+
+  demand_empty_rest_of_line ();
+}
 
 /* The .comm and .lcomm pseudo-ops for XCOFF.  XCOFF puts common
    symbols in the .bss segment as though they were local common
diff --git a/gas/testsuite/gas/ppc/aix.exp b/gas/testsuite/gas/ppc/aix.exp
index 53806b3b4f2..070bc355e04 100644
--- a/gas/testsuite/gas/ppc/aix.exp
+++ b/gas/testsuite/gas/ppc/aix.exp
@@ -90,4 +90,6 @@ if { [istarget "powerpc*-*-aix*"] || [istarget "rs6000-*-aix*"] } then {
 
     run_dump_test "xcoff-visibility-1-32"
     run_dump_test "xcoff-visibility-1-64"
+    run_dump_test "xcoff-visibility-2-32"
+    run_dump_test "xcoff-visibility-2-64"
 }
diff --git a/gas/testsuite/gas/ppc/xcoff-visibility-2-32.d b/gas/testsuite/gas/ppc/xcoff-visibility-2-32.d
new file mode 100644
index 00000000000..ec2befbd85e
--- /dev/null
+++ b/gas/testsuite/gas/ppc/xcoff-visibility-2-32.d
@@ -0,0 +1,18 @@
+#as: -a32
+#source: xcoff-visibility-2.s
+#objdump: -t
+#name: XCOFF Visibility 2 (32 bit)
+
+.*
+
+SYMBOL TABLE:
+.*
+.*
+\[  2\].*\(ty 1000\).*internal
+.*
+\[  4\].*\(ty 2000\).*hidden
+.*
+\[  6\].*\(ty 3000\).*protected
+.*
+\[  8\].*\(ty 1000\).*dual
+.*
diff --git a/gas/testsuite/gas/ppc/xcoff-visibility-2-64.d b/gas/testsuite/gas/ppc/xcoff-visibility-2-64.d
new file mode 100644
index 00000000000..48d50a54d21
--- /dev/null
+++ b/gas/testsuite/gas/ppc/xcoff-visibility-2-64.d
@@ -0,0 +1,18 @@
+#as: -a64
+#source: xcoff-visibility-2.s
+#objdump: -t
+#name: XCOFF Visibility 2 (64 bit)
+
+.*
+
+SYMBOL TABLE:
+.*
+.*
+\[  2\].*\(ty 1000\).*internal
+.*
+\[  4\].*\(ty 2000\).*hidden
+.*
+\[  6\].*\(ty 3000\).*protected
+.*
+\[  8\].*\(ty 1000\).*dual
+.*
diff --git a/gas/testsuite/gas/ppc/xcoff-visibility-2.s b/gas/testsuite/gas/ppc/xcoff-visibility-2.s
new file mode 100644
index 00000000000..c5f0f7fce98
--- /dev/null
+++ b/gas/testsuite/gas/ppc/xcoff-visibility-2.s
@@ -0,0 +1,9 @@
+# Ensure that GNU syntax is accepted even for XCOFF.
+  .internal internal
+  .hidden hidden
+  .protected protected
+
+# Ensure that only the last visibility is taken into
+# account, when several are provided.
+  .protected dual
+  .internal dual
-- 
2.25.1

