diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index 766266942f9..ffe69d6fcb9 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -2666,6 +2666,9 @@ Target can compile using @code{pthread.h} with no errors or warnings.
 @item pthread_h
 Target has @code{pthread.h}.
 
+@item sockets
+Target can compile using @code{sys/socket.h} with no errors or warnings.
+
 @item run_expensive_tests
 Expensive testcases (usually those that consume excessive amounts of CPU
 time) should be run on this target.  This can be enabled by setting the
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-accept.c b/gcc/testsuite/gcc.dg/analyzer/fd-accept.c
index 36cc7af7184..5426063f31d 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-accept.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-accept.c
@@ -1,3 +1,5 @@
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-bind.c b/gcc/testsuite/gcc.dg/analyzer/fd-bind.c
index 6f91bc4b794..c34803f1380 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-bind.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-bind.c
@@ -1,3 +1,5 @@
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-connect.c b/gcc/testsuite/gcc.dg/analyzer/fd-connect.c
index 1ab54d01f36..7bf687e2570 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-connect.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-connect.c
@@ -1,3 +1,5 @@
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-datagram-socket.c b/gcc/testsuite/gcc.dg/analyzer/fd-datagram-socket.c
index 045bdfa32d3..58508570a25 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-datagram-socket.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-datagram-socket.c
@@ -1,3 +1,5 @@
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-glibc-byte-stream-connection-server.c b/gcc/testsuite/gcc.dg/analyzer/fd-glibc-byte-stream-connection-server.c
index 1ff902894af..5a1a6f5f14f 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-glibc-byte-stream-connection-server.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-glibc-byte-stream-connection-server.c
@@ -1,4 +1,5 @@
 /* Example from glibc manual (16.9.7).  */
+/* { dg-require-effective-target sockets } */
 /* { dg-additional-options "-Wno-analyzer-too-complex" } */
 
 #include <stdio.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-glibc-byte-stream-socket.c b/gcc/testsuite/gcc.dg/analyzer/fd-glibc-byte-stream-socket.c
index f96da8101cc..806bb24d9eb 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-glibc-byte-stream-socket.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-glibc-byte-stream-socket.c
@@ -1,4 +1,5 @@
 /* Example from glibc manual (16.9.6).  */
+/* { dg-require-effective-target sockets } */
 
 #include <stdio.h>
 #include <string.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-client.c b/gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-client.c
index 888c751e88d..072ac4cd0d3 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-client.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-client.c
@@ -1,4 +1,5 @@
 /* Example from the glibc manual (16.10.4).  */
+/* { dg-require-effective-target sockets } */
 
 #include <stdio.h>
 #include <errno.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-socket.c b/gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-socket.c
index b8b68768c34..d417828f6ee 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-socket.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-socket.c
@@ -1,4 +1,5 @@
 /* Example from glibc manual (16.10.3).  */
+/* { dg-require-effective-target sockets } */
 
 #include <stdio.h>
 #include <errno.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-listen.c b/gcc/testsuite/gcc.dg/analyzer/fd-listen.c
index 1f54a8f2953..becf4690293 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-listen.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-listen.c
@@ -1,3 +1,5 @@
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-manpage-getaddrinfo-client.c b/gcc/testsuite/gcc.dg/analyzer/fd-manpage-getaddrinfo-client.c
index d9c3ff05de8..ee627de9643 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-manpage-getaddrinfo-client.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-manpage-getaddrinfo-client.c
@@ -26,6 +26,7 @@ Formatted or processed versions of this manual, if unaccompanied by
 the source, must acknowledge the copyright and authors of this work.
 */
 
+/* { dg-require-effective-target sockets } */
 /* { dg-additional-options "-Wno-analyzer-too-complex" } */
 
 #include <sys/types.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-mappage-getaddrinfo-server.c b/gcc/testsuite/gcc.dg/analyzer/fd-mappage-getaddrinfo-server.c
index 66398e834cc..d2e39eb6976 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-mappage-getaddrinfo-server.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-mappage-getaddrinfo-server.c
@@ -26,6 +26,8 @@ Formatted or processed versions of this manual, if unaccompanied by
 the source, must acknowledge the copyright and authors of this work.
 */
 
+/* { dg-require-effective-target sockets } */
+
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c b/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
index 6a9ec921fd3..384dbd08158 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
@@ -34,4 +34,4 @@ void test_3 (const char* path)
         close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
         close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
     }
-}
\ No newline at end of file
+}
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-socket-meaning.c b/gcc/testsuite/gcc.dg/analyzer/fd-socket-meaning.c
index 5bfb57f68fb..82a199f83b0 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-socket-meaning.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-socket-meaning.c
@@ -1,3 +1,4 @@
+/* { dg-require-effective-target sockets } */
 /* { dg-additional-options "-fanalyzer-verbose-state-changes" } */
 
 #include <sys/socket.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-socket-misuse.c b/gcc/testsuite/gcc.dg/analyzer/fd-socket-misuse.c
index 4ff08d5ec19..628f5e4a65c 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-socket-misuse.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-socket-misuse.c
@@ -1,5 +1,7 @@
 /* Various operations done on sockets in the wrong phase.  */
 
+/* { dg-require-effective-target sockets } */
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket-active-open.c b/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket-active-open.c
index 7fde0ef6285..841894cc1bd 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket-active-open.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket-active-open.c
@@ -1,3 +1,5 @@
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket-passive-open.c b/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket-passive-open.c
index c31e5b5eefb..a61091101ed 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket-passive-open.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket-passive-open.c
@@ -2,6 +2,8 @@
    either from scratch, or when various phases are assumed to already
    be done.  */
 
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket.c b/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket.c
index 3a292d0e2d2..e51f0f08c34 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-stream-socket.c
@@ -1,3 +1,5 @@
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-symbolic-socket.c b/gcc/testsuite/gcc.dg/analyzer/fd-symbolic-socket.c
index 83400c18f50..ad1f989cf09 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-symbolic-socket.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-symbolic-socket.c
@@ -1,3 +1,5 @@
+/* { dg-require-effective-target sockets } */
+
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
index 01cf17ea44a..65ba33b9d94 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -1176,6 +1176,20 @@ proc check_effective_target_pthread {} {
     } "-pthread"]
 }
 
+# Return 1 if the target supports both Unix and internet sockets, 0 otherwise.
+proc check_effective_target_sockets {} {
+    return [check_no_compiler_messages socket executable {
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+int main (void) {
+    socket(AF_UNIX, SOCK_STREAM, 0);
+    socket(AF_INET, SOCK_DGRAM, 0);
+    return 0;
+}
+    } ""]
+}
+
 # Return 1 if compilation with -mpe-aligned-commons is error-free
 # for trivial code, 0 otherwise.
 
