diff --git a/include/nss_dns.h b/include/nss_dns.h
index 63b5853870..53205b27a6 100644
--- a/include/nss_dns.h
+++ b/include/nss_dns.h
@@ -24,13 +24,16 @@
 NSS_DECLARE_MODULE_FUNCTIONS (dns)
 
 libc_hidden_proto (_nss_dns_getcanonname_r)
-libc_hidden_proto (_nss_dns_gethostbyname3_r)
-libc_hidden_proto (_nss_dns_gethostbyname2_r)
-libc_hidden_proto (_nss_dns_gethostbyname_r)
-libc_hidden_proto (_nss_dns_gethostbyname4_r)
 libc_hidden_proto (_nss_dns_gethostbyaddr2_r)
 libc_hidden_proto (_nss_dns_gethostbyaddr_r)
-libc_hidden_proto (_nss_dns_getnetbyname_r)
+libc_hidden_proto (_nss_dns_gethostbyname2_r)
+libc_hidden_proto (_nss_dns_gethostbyname3_r)
+libc_hidden_proto (_nss_dns_gethostbyname4_r)
+libc_hidden_proto (_nss_dns_gethostbyname_r)
 libc_hidden_proto (_nss_dns_getnetbyaddr_r)
+libc_hidden_proto (_nss_dns_getnetbyname_r)
+
+void __nss_dns_functions (nss_module_functions_untyped pointers)
+  attribute_hidden;
 
 #endif
diff --git a/nss/nss_module.c b/nss/nss_module.c
index 60c070c851..cb91a16e3a 100644
--- a/nss/nss_module.c
+++ b/nss/nss_module.c
@@ -26,6 +26,7 @@
 #include <dlfcn.h>
 #include <gnu/lib-names.h>
 #include <libc-lock.h>
+#include <nss_dns.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -110,10 +111,36 @@ static const function_name nss_function_name_array[] =
 #include "function.def"
   };
 
+static bool
+module_load_nss_dns (struct nss_module *module)
+{
+  /* Initialize the function pointers, following the double-checked
+     locking idiom.  */
+  __libc_lock_lock (nss_module_list_lock);
+  switch ((enum nss_module_state) atomic_load_acquire (&module->state))
+    {
+    case nss_module_uninitialized:
+    case nss_module_failed:
+      __nss_dns_functions (module->functions.untyped);
+      module->handle = NULL;
+      /* Synchronizes with unlocked __nss_module_load atomic_load_acquire.  */
+      atomic_store_release (&module->state, nss_module_loaded);
+      break;
+    case nss_module_loaded:
+      /* Nothing to clean up.  */
+      break;
+    }
+  __libc_lock_unlock (nss_module_list_lock);
+  return true;
+}
+
 /* Internal implementation of __nss_module_load.  */
 static bool
 module_load (struct nss_module *module)
 {
+  if (strcmp (module->name, "dns") == 0)
+    return module_load_nss_dns (module);
+
   void *handle;
   {
     char *shlib_name;
@@ -360,7 +387,9 @@ __nss_module_freeres (void)
   struct nss_module *current = nss_module_list;
   while (current != NULL)
     {
-      if (current->state == nss_module_loaded)
+      /* Ignore built-in modules (which have a NULL handle).  */
+      if (current->state == nss_module_loaded
+	  && current->handle != NULL)
         __libc_dlclose (current->handle);
 
       struct nss_module *next = current->next;
diff --git a/nss/nss_module.h b/nss/nss_module.h
index 05c4791d11..908b2351d4 100644
--- a/nss/nss_module.h
+++ b/nss/nss_module.h
@@ -33,10 +33,16 @@ struct nss_module_functions
 #include "function.def"
 };
 
+/* Number of elements of the nss_module_functions_untyped array.  */
+enum
+  {
+    nss_module_functions_count = (sizeof (struct nss_module_functions)
+                                  / sizeof (void *))
+  };
+
 /* Untyped version of struct nss_module_functions, for consistent
    processing purposes.  */
-typedef void *nss_module_functions_untyped[sizeof (struct nss_module_functions)
-                                           / sizeof (void *)];
+typedef void *nss_module_functions_untyped[nss_module_functions_count];
 
 /* Initialization state of a NSS module.  */
 enum nss_module_state
diff --git a/resolv/Makefile b/resolv/Makefile
index dd0a98c74f..31d27454b4 100644
--- a/resolv/Makefile
+++ b/resolv/Makefile
@@ -48,6 +48,7 @@ routines := \
   ns_name_unpack \
   ns_samename \
   nsap_addr \
+  nss_dns_functions \
   res-close \
   res-name-checking \
   res-state \
diff --git a/resolv/nss_dns_functions.c b/resolv/nss_dns_functions.c
new file mode 100644
index 0000000000..684718d4a9
--- /dev/null
+++ b/resolv/nss_dns_functions.c
@@ -0,0 +1,46 @@
+/* Direct access for nss_dns functions for NSS module loading.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <nss/nss_module.h>
+#include <nss_dns.h>
+#include <string.h>
+#include <sysdep.h>
+
+void
+__nss_dns_functions (nss_module_functions_untyped pointers)
+{
+  struct nss_module_functions typed =
+    {
+      .getcanonname_r = &_nss_dns_getcanonname_r,
+      .gethostbyname3_r = &_nss_dns_gethostbyname3_r,
+      .gethostbyname2_r = &_nss_dns_gethostbyname2_r,
+      .gethostbyname_r = &_nss_dns_gethostbyname_r,
+      .gethostbyname4_r = &_nss_dns_gethostbyname4_r,
+      .gethostbyaddr2_r = &_nss_dns_gethostbyaddr2_r,
+      .gethostbyaddr_r = &_nss_dns_gethostbyaddr_r,
+      .getnetbyname_r = &_nss_dns_getnetbyname_r,
+      .getnetbyaddr_r = &_nss_dns_getnetbyaddr_r,
+    };
+
+  memcpy (pointers, &typed, sizeof (nss_module_functions_untyped));
+
+#ifdef PTR_MANGLE
+  for (int i = 0; i < nss_module_functions_count; ++i)
+    PTR_MANGLE (pointers[i]);
+#endif
+}
