--- ./gcc/config/rs6000/rs6000.c_orig	2022-06-06 07:36:50.110802796 -0500
+++ ./gcc/config/rs6000/rs6000.c	2022-06-06 07:40:58.416813937 -0500
@@ -211,6 +211,13 @@
 static bool rs6000_passes_ieee128;
 #endif
 
+/* Programs with __thread might be able to link in 64bit on AIX,
+   even without -pthread when exec models are used.
+   In order to avoid that, a .ref to __tls_get_addr must be
+   added.  */
+static bool xcoff_tls_exec_model_detected = false;
+
+
 /* Generate the manged name (i.e. U10__float128) used in GCC 8.1, and not the
    name used in current releases (i.e. u9__ieee128).  */
 static bool ieee128_mangling_gcc_8_1;
@@ -9235,7 +9242,10 @@
       emit_insn (gen_tls_get_tpointer (tlsreg));
     }
   else
-    tlsreg = gen_rtx_REG (DImode, 13);
+   {
+      xcoff_tls_exec_model_detected = true;
+      tlsreg = gen_rtx_REG (DImode, 13);
+   }
 
   /* Load the TOC value into temporary register.  */
   tmpreg = gen_reg_rtx (Pmode);
@@ -34437,6 +34447,12 @@
 {
   switch_to_section (text_section);
   fputs ("_section_.text:\n", asm_out_file);
+  if (xcoff_tls_exec_model_detected)
+    {
+      /* Add a .ref to __tls_get_addr to ensure that link
+         fails if -pthread is provided.  */
+      fputs ("\t.ref __tls_get_addr\n", asm_out_file);
+    }
   switch_to_section (data_section);
   fputs (TARGET_32BIT
 	 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
