Index: binutils/gdb/testsuite/gdb.threads/tls-core.c
===================================================================
--- binutils.orig/gdb/testsuite/gdb.threads/tls-core.c	2017-11-06 21:58:53.369742438 +0000
+++ binutils/gdb/testsuite/gdb.threads/tls-core.c	2017-11-06 22:47:22.684061150 +0000
@@ -16,12 +16,21 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <pthread.h>
+#include <stdlib.h>
+
+#ifdef USE_RLIMIT
+# include <sys/resource.h>
+# ifndef RLIM_INFINITY
+#  define RLIM_INFINITY -1
+# endif
+#endif /* USE_RLIMIT */
 
 int __thread foo = 0xdeadbeef;
 
 static void *
 thread_proc (void *arg)
 {
+  abort ();
   return arg;
 }
 
@@ -30,6 +39,14 @@ main (void)
 {
   pthread_t thread;
 
+#ifdef USE_RLIMIT
+  {
+    struct rlimit rlim = { RLIM_INFINITY, RLIM_INFINITY };
+
+    setrlimit (RLIMIT_CORE, &rlim);
+  }
+#endif
+
   pthread_create (&thread, NULL, thread_proc, NULL);
   pthread_join (thread, NULL);
 
Index: binutils/gdb/testsuite/gdb.threads/tls-core.exp
===================================================================
--- binutils.orig/gdb/testsuite/gdb.threads/tls-core.exp	2017-11-06 21:58:53.384913472 +0000
+++ binutils/gdb/testsuite/gdb.threads/tls-core.exp	2017-11-06 22:50:40.419947495 +0000
@@ -16,41 +16,99 @@
 standard_testfile
 
 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
+	executable { debug additional_flags=-DUSE_RLIMIT }] != ""
+    && [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
 	executable { debug }] != "" } {
     return -1
 }
 
-
 clean_restart ${binfile}
 
+#
+# Use a fresh directory to confine the native core dumps.
+# Make it the working directory for the inferior.
+#
+set core_supported [expr ![use_gdb_stub]]
+if {$core_supported} {
+    set coredir [standard_output_file coredir.[getpid]]
+    file mkdir $coredir
+    if {[gdb_test_no_output "set cwd $coredir" \
+		"set cwd to temporary directory for core dumps"]} {
+        set core_supported 0
+    }
+}
+
 runto thread_proc
 
 #
 # Generate corefile.
 #
-set corefile [standard_output_file gcore.test]
-set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"]
+set gcorefile [standard_output_file gcore.test]
+set gcore_supported [gdb_gcore_cmd "$gcorefile" "save a corefile"]
+
+#
+# Let the program die making the OS dump core if possible too.
+#
+set corefile [standard_output_file core.test]
+set test "generate native core dump"
+if {$core_supported} {
+    gdb_test continue ".*Thread .* received signal SIGABRT.*" \
+	"continue to signal"
+    gdb_test continue ".*Program terminated with signal SIGABRT.*" \
+	"continue to termination"
+
+    # Find the core file produced.
+    set names [glob -nocomplain -directory $coredir *core*]
+    if {[llength $names] == 1} {
+        set file [file join $coredir [lindex $names 0]]
+        remote_exec build "mv $file $corefile"
+        pass $test
+    } else {
+        set core_supported 0
+        warning "can't generate a core file - core tests suppressed -\
+		check ulimit -c"
+    }
+
+    remote_exec build "rm -rf $coredir"
+}
 if {!$core_supported} {
-    return 0
+    unsupported $test
 }
 
 #
 # Restart gdb and load generated corefile.
 #
-clean_restart ${binfile}
+proc tls_core_test {supported corefile test1 test2} {
+    upvar target_triplet target_triplet
+    upvar host_triplet host_triplet
+    upvar binfile binfile
 
-set core_loaded [gdb_core_cmd "$corefile" "load generated corefile"]
-if { $core_loaded != 1 } {
-    # No use proceeding from here.
-    return 0
-}
+    clean_restart ${binfile}
 
-# This fails in cross-debugging due to the use of native `libthread_db'.
-if {![string match $host_triplet $target_triplet]} {
-    setup_kfail "threads/22381" "*-*-*"
+    if {$supported} {
+	set core_loaded [gdb_core_cmd $corefile $test1]
+    } else {
+	set core_loaded 0
+	unsupported $test1
+    }
+    if { $core_loaded == 1 } {
+	# This fails in cross-debugging due to the use of native
+	# `libthread_db'.
+	if {![string match $host_triplet $target_triplet]} {
+	    setup_kfail "threads/22381" "*-*-*"
+	}
+	gdb_test "p/x foo" "\\$\[0-9]+ = 0xdeadbeef" $test2
+    } else {
+	unsupported $test2
+    }
 }
-gdb_test "p/x foo" \
-	"\\$\[0-9]+ = 0xdeadbeef" \
-	"print thread-local storage variable"
+
+tls_core_test $gcore_supported $gcorefile "load generated corefile" \
+	"print thread-local storage variable from generated corefile"
+
+tls_core_test $core_supported $corefile "load native corefile" \
+	"print thread-local storage variable from native corefile"
+
+rename tls_core_test ""
 
 gdb_exit
