gas: ensure sections contents is zero for BFD_RELOC_PPC*_TLSM on XCOFF.

AIX ld expects the section contents for relocations BFD_RELOC_PPC_TLSM
or BFD_RELOC_PPC64_TLSM to be zero.
Actually, it seems to be the case for all the TLS relocations
generated by AIX assembly but only these two are mandatory.

	* config/tc-ppc.c (md_apply_fix): Adjust addend to nullify
	section contents for BFD_RELOC_PPC_TLSM or
	BFD_RELOC_PPC64_TLSM.
	* testsuite/gas/ppc/xcoff-tlsm-32.d: New test.
	* testsuite/gas/ppc/xcoff-tlsm-64.d: New test.
	* testsuite/gas/ppc/xcoff-tlsm.s: New test.
	* testsuite/gas/ppc/aix.exp: Run new tests.
diff --git a/gas/ChangeLog b/gas/ChangeLog
index 8fa1be8..ad20137 100644
--- a/gas/ChangeLog
+++ b/gas/ChangeLog
@@ -1,3 +1,13 @@
+2021-06-17  Clément Chigot  <clement.chigot@atos.net>
+
+	* config/tc-ppc.c (md_apply_fix): Adjust addend to nullify
+	section contents for BFD_RELOC_PPC_TLSM or
+	BFD_RELOC_PPC64_TLSM.
+	* testsuite/gas/ppc/xcoff-tlsm-32.d: New test.
+	* testsuite/gas/ppc/xcoff-tlsm-64.d: New test.
+	* testsuite/gas/ppc/xcoff-tlsm.s: New test.
+	* testsuite/gas/ppc/aix.exp: Run new tests.
+
 2021-06-16  Jan Beulich  <jbeulich@suse.com>
 
 	* read.c (parse_one_float): New.
diff --git a/gas/config/tc-ppc.c b/gas/config/tc-ppc.c
index fce2b15..72bf5ce 100644
--- a/gas/config/tc-ppc.c
+++ b/gas/config/tc-ppc.c
@@ -7411,11 +7411,9 @@
 	symbol_get_bfdsym (fixP->fx_addsy)->flags |= BSF_KEEP;
     }
 #else
-  if (fixP->fx_r_type != BFD_RELOC_PPC_TOC16
-      && fixP->fx_r_type != BFD_RELOC_PPC_TOC16_HI
-      && fixP->fx_r_type != BFD_RELOC_PPC_TOC16_LO)
-    fixP->fx_addnumber = 0;
-  else
+  if (fixP->fx_r_type == BFD_RELOC_PPC_TOC16
+      || fixP->fx_r_type == BFD_RELOC_PPC_TOC16_HI
+      || fixP->fx_r_type == BFD_RELOC_PPC_TOC16_LO)
     {
       /* We want to use the offset within the toc, not the actual VMA
 	 of the symbol.  */
@@ -7430,6 +7428,15 @@
       /* Set *valP to avoid errors.  */
       *valP = value;
     }
+  else if (fixP->fx_r_type == BFD_RELOC_PPC_TLSM
+	   || fixP->fx_r_type == BFD_RELOC_PPC64_TLSM)
+    /* AIX ld expects the section contents for these relocations
+       to be zero.  Arrange for that to occur when
+       bfd_install_relocation is called.  */
+    fixP->fx_addnumber = (- bfd_section_vma (S_GET_SEGMENT (fixP->fx_addsy))
+			  - S_GET_VALUE (fixP->fx_addsy));
+  else
+    fixP->fx_addnumber = 0;
 #endif
 }
 
diff --git a/gas/testsuite/gas/ppc/aix.exp b/gas/testsuite/gas/ppc/aix.exp
index efe27e4..38b7819 100644
--- a/gas/testsuite/gas/ppc/aix.exp
+++ b/gas/testsuite/gas/ppc/aix.exp
@@ -78,4 +78,7 @@
 
     run_dump_test "xcoff-function-1-32"
     run_dump_test "xcoff-function-1-64"
+
+    run_dump_test "xcoff-tlsm-32"
+    run_dump_test "xcoff-tlsm-64"
 }
diff --git a/gas/testsuite/gas/ppc/xcoff-tlsm-32.d b/gas/testsuite/gas/ppc/xcoff-tlsm-32.d
new file mode 100644
index 0000000..a09dd1a
--- /dev/null
+++ b/gas/testsuite/gas/ppc/xcoff-tlsm-32.d
@@ -0,0 +1,34 @@
+#as: -a32
+#source: xcoff-tlsm.s
+#objdump: -Dr
+#name: XCOFF TLSM relocation (32 bit)
+
+.*
+Disassembly of section .data:
+
+00000000 <TOC>:
+   0:	00 00 00 10.*
+			0: R_TLS	foo_tdata-0x10
+
+00000004 <.foo_tdata>:
+   4:	00 00 00 00.*
+			4: R_TLSM	foo_tdata-0x10
+
+00000008 <foo_tbss>:
+   8:	00 00 00 20.*
+			8: R_TLS	foo_tbss-0x20
+
+0000000c <.foo_tbss>:
+   c:	00 00 00 00.*
+			c: R_TLSM	foo_tbss-0x20
+
+Disassembly of section .tdata:
+
+00000010 <foo_tdata>:
+  10:	00 00 00 01.*
+	...
+
+Disassembly of section .tbss:
+
+00000020 <foo_tbss>:
+	...
diff --git a/gas/testsuite/gas/ppc/xcoff-tlsm-64.d b/gas/testsuite/gas/ppc/xcoff-tlsm-64.d
new file mode 100644
index 0000000..f447a51
--- /dev/null
+++ b/gas/testsuite/gas/ppc/xcoff-tlsm-64.d
@@ -0,0 +1,36 @@
+#as: -a64
+#source: xcoff-tlsm.s
+#objdump: -Dr
+#name: XCOFF TLSM relocation (64 bit)
+
+.*
+Disassembly of section .data:
+
+0000000000000000 <TOC>:
+   0:	00 00 00 00.*
+			0: R_TLS	foo_tdata-0x20
+   4:	00 00 00 20.*
+
+0000000000000008 <.foo_tdata>:
+	...
+			8: R_TLSM	foo_tdata-0x20
+
+0000000000000010 <foo_tbss>:
+  10:	00 00 00 00.*
+			10: R_TLS	foo_tbss-0x30
+  14:	00 00 00 30.*
+
+0000000000000018 <.foo_tbss>:
+	...
+			18: R_TLSM	foo_tbss-0x30
+
+Disassembly of section .tdata:
+
+0000000000000020 <foo_tdata>:
+  20:	00 00 00 01.*
+	...
+
+Disassembly of section .tbss:
+
+0000000000000030 <foo_tbss>:
+	...
diff --git a/gas/testsuite/gas/ppc/xcoff-tlsm.s b/gas/testsuite/gas/ppc/xcoff-tlsm.s
new file mode 100644
index 0000000..0c857bf
--- /dev/null
+++ b/gas/testsuite/gas/ppc/xcoff-tlsm.s
@@ -0,0 +1,12 @@
+  .csect foo_tdata[TL],4
+  .align 2
+foo_tdata:
+  .long	1
+
+  .comm foo_tbss[UL],8
+
+  .toc
+  .tc foo_tdata[TC],foo_tdata[TL]
+  .tc .foo_tdata[TC],foo_tdata[TL]@m
+  .tc foo_tbss[TC],foo_tbss[UL]
+  .tc .foo_tbss[TC],foo_tbss[UL]@m