diff --git a/Makerules b/Makerules
index 7f0eef8..2ebb8e0 100644
--- a/Makerules
+++ b/Makerules
@@ -105,6 +105,11 @@ $(common-objpfx)%.latest: $(common-objpfx)abi-versions.h
 	sed -n '/ VERSION_$*_/{s/^.*_\([A-Z0-9_]*\).*$$/\1/;h;};$${g;p;}' \
 	    $(common-objpfx)abi-versions.h > $@T
 	mv -f $@T $@
+
+# first-versions.h provides macros used in various symbol versioning
+# macro calls.
+before-compile := $(common-objpfx)first-versions.h $(before-compile)
+$(common-objpfx)first-versions.h: $(common-objpfx)versions.stmp
 endif # avoid-generated
 endif # $(build-shared) = yes
 
@@ -458,7 +463,8 @@ $(common-objpfx)Versions.all: $(..)scripts/firstversions.awk \
 $(common-objpfx)Versions.v.i: $(wildcard $(subdirs:%=$(..)%/Versions)) \
 			      $(wildcard $(sysdirs:%=%/Versions)) \
 			      $(sysd-versions-force)
-$(common-objpfx)sysd-versions: $(common-objpfx)Versions.all \
+$(common-objpfx)sysd-versions: $(common-objpfx)versions.stmp
+$(common-objpfx)versions.stmp: $(common-objpfx)Versions.all \
 			       $(common-objpfx)Versions.v \
 			       $(..)scripts/versions.awk
 	( echo 'sysd-versions-subdirs = $(subdirs) $(config-sysdirs)' ; \
@@ -466,8 +472,9 @@ $(common-objpfx)sysd-versions: $(common-objpfx)Versions.all \
 	  | LC_ALL=C $(AWK) -v buildroot=$(common-objpfx) -v defsfile=$< \
 			    -v move_if_change='$(move-if-change)' \
 			    -f $(word 3,$^); \
-	) > $@T
-	mv -f $@T $@
+	) > $(common-objpfx)sysd-versionsT
+	mv -f $(common-objpfx)sysd-versionsT $(common-objpfx)sysd-versions
+	touch $@
 endif # avoid-generated
 endif # $(build-shared) = yes
 endif # sysd-sorted-done
diff --git a/scripts/versions.awk b/scripts/versions.awk
index df33529..df08d06 100644
--- a/scripts/versions.awk
+++ b/scripts/versions.awk
@@ -101,6 +101,10 @@ END {
 
   oldlib = "";
   oldver = "";
+  real_first_ver_header = buildroot "first-versions.h"
+  first_ver_header = real_first_ver_header "T"
+  printf("#ifndef _FIRST_VERSIONS_H\n") > first_ver_header;
+  printf("#define _FIRST_VERSIONS_H\n") > first_ver_header;
   printf("version-maps =");
   while (getline < tmpfile) {
     if ($1 != oldlib) {
@@ -127,11 +131,24 @@ END {
     printf("   ") > outfile;
     for (n = 3; n <= NF; ++n) {
       printf(" %s", $n) > outfile;
+      sym = $n;
+      sub(";", "", sym);
+      first_ver_macro = "FIRST_VERSION_" oldlib "_" sym;
+      if (!(first_ver_macro in first_ver_seen) \
+	  && oldver ~ "^GLIBC_[0-9]" \
+	  && sym ~ "^[A-Za-z0-9_]*$") {
+	ver_val = oldver;
+	gsub("\\.", "_", ver_val);
+	printf("#define %s %s\n", first_ver_macro, ver_val) > first_ver_header;
+	first_ver_seen[first_ver_macro] = 1;
+      }
     }
     printf("\n") > outfile;
   }
   printf("\n");
+  printf("#endif /* first-versions.h */\n") > first_ver_header;
   closeversion(oldver, veryoldver);
   close_and_move(outfile, real_outfile);
+  close_and_move(first_ver_header, real_first_ver_header);
   #system("rm -f " tmpfile);
 }
diff --git a/sysdeps/ieee754/ldbl-opt/math-type-macros-double.h b/sysdeps/ieee754/ldbl-opt/math-type-macros-double.h
index a83c2b3..67b5268 100644
--- a/sysdeps/ieee754/ldbl-opt/math-type-macros-double.h
+++ b/sysdeps/ieee754/ldbl-opt/math-type-macros-double.h
@@ -19,48 +19,19 @@
 #ifndef _MATH_TYPE_MACROS_DOUBLE
 
 #include <math_ldbl_opt.h>
-
-#define LDOUBLE_cabsl_libm_version GLIBC_2_1
-#define LDOUBLE_cargl_libm_version GLIBC_2_1
-#define LDOUBLE_cimagl_libm_version GLIBC_2_1
-#define LDOUBLE_conjl_libm_version GLIBC_2_1
-#define LDOUBLE_creall_libm_version GLIBC_2_1
-#define LDOUBLE_cacosl_libm_version GLIBC_2_1
-#define LDOUBLE_cacoshl_libm_version GLIBC_2_1
-#define LDOUBLE_ccosl_libm_version GLIBC_2_1
-#define LDOUBLE_ccoshl_libm_version GLIBC_2_1
-#define LDOUBLE_casinl_libm_version GLIBC_2_1
-#define LDOUBLE_csinl_libm_version GLIBC_2_1
-#define LDOUBLE_casinhl_libm_version GLIBC_2_1
-#define LDOUBLE_csinhl_libm_version GLIBC_2_1
-#define LDOUBLE_catanl_libm_version GLIBC_2_1
-#define LDOUBLE_catanhl_libm_version GLIBC_2_1
-#define LDOUBLE_ctanl_libm_version GLIBC_2_1
-#define LDOUBLE_ctanhl_libm_version GLIBC_2_1
-#define LDOUBLE_cexpl_libm_version GLIBC_2_1
-#define LDOUBLE_clogl_libm_version GLIBC_2_1
-#define LDOUBLE_cprojl_libm_version GLIBC_2_1
-#define LDOUBLE_csqrtl_libm_version GLIBC_2_1
-#define LDOUBLE_cpowl_libm_version GLIBC_2_1
-#define LDOUBLE_clog10l_libm_version GLIBC_2_1
-#define LDOUBLE___clog10l_libm_version GLIBC_2_1
-#define LDOUBLE_fdiml_libm_version GLIBC_2_1
-#define LDOUBLE_fmaxl_libm_version GLIBC_2_1
-#define LDOUBLE_fminl_libm_version GLIBC_2_1
-#define LDOUBLE_ilogbl_libm_version GLIBC_2_0
-#define LDOUBLE_nanl_libm_version GLIBC_2_1
+#include <first-versions.h>
 
 /* Define compat symbols for long double on platforms
    where it was not always a distinct type.  */
 #if !defined M_LIBM_NEED_COMPAT
 # define M_LIBM_NEED_COMPAT(f) \
-  LONG_DOUBLE_COMPAT (libm, LDOUBLE_ ## f ## l_libm_version)
+  LONG_DOUBLE_COMPAT (libm, FIRST_VERSION_libm_ ## f ## l)
 #endif
 
 #if !defined declare_mgen_libm_compat
 # define declare_mgen_libm_compat(from, to)	      \
   compat_symbol (libm, from, to ## l,		      \
-		 LDOUBLE_ ## to ## l_libm_version);
+		 FIRST_VERSION_libm_ ## to ## l);
 #endif
 
 #include_next <math-type-macros-double.h>
