00db10
commit 58007e9e68913290b1f4f73afc1055f779a8ed5d
00db10
Author: H.J. Lu <hjl.tools@gmail.com>
00db10
Date:   Thu May 28 05:06:27 2015 -0700
00db10
00db10
    Make sure that calloc is called at least once
00db10
    
00db10
    PLT relocations aren't required when -z now used.
00db10
    
00db10
    
00db10
    	[BZ #18422]
00db10
    	* Makefile ($(objpfx)tst-audit2): Depend on $(libdl).
00db10
    	($(objpfx)tst-audit2.out): Also depend on
00db10
    	$(objpfx)tst-auditmod9b.so.
00db10
    	* elf/tst-audit2.c: Include <dlfcn.h>.
00db10
    	(calloc_called): New.
00db10
    	(calloc): Allow to be called more than once.
00db10
    	(do_test): dllopen/dlclose $ORIGIN/tst-auditmod9b.so.
00db10
00db10
diff -rup a/elf/Makefile b/elf/Makefile
00db10
--- a/elf/Makefile	2017-10-04 16:42:22.000000000 -0400
00db10
+++ b/elf/Makefile	2017-10-05 17:40:38.402451971 -0400
00db10
@@ -1020,7 +1020,8 @@ $(objpfx)tst-dlmopen3.out: $(objpfx)tst-
00db10
 $(objpfx)tst-audit1.out: $(objpfx)tst-auditmod1.so
00db10
 tst-audit1-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so
00db10
 
00db10
-$(objpfx)tst-audit2.out: $(objpfx)tst-auditmod1.so
00db10
+$(objpfx)tst-audit2: $(libdl)
00db10
+$(objpfx)tst-audit2.out: $(objpfx)tst-auditmod1.so $(objpfx)tst-auditmod9b.so
00db10
 tst-audit2-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so
00db10
 
00db10
 $(objpfx)tst-audit3: $(objpfx)tst-auditmod3a.so
00db10
diff -rup a/elf/tst-audit2.c b/elf/tst-audit2.c
00db10
--- a/elf/tst-audit2.c	2017-10-04 16:42:18.000000000 -0400
00db10
+++ b/elf/tst-audit2.c	2017-10-05 17:40:51.570518031 -0400
00db10
@@ -3,26 +3,35 @@
00db10
 #include <stdio.h>
00db10
 #include <stdlib.h>
00db10
 #include <string.h>
00db10
+#include <dlfcn.h>
00db10
 
00db10
 #define MAGIC1 0xabcdef72
00db10
 #define MAGIC2 0xd8675309
00db10
 static __thread unsigned int magic[] = { MAGIC1, MAGIC2 };
00db10
+static __thread int calloc_called;
00db10
 
00db10
 #undef calloc
00db10
 
00db10
 /* This calloc definition will be called by the dynamic linker itself.
00db10
-   We test that it has initialized our TLS block by the time it does so.  */
00db10
+   We test that interposed calloc is called by the dynamic loader, and
00db10
+   that TLS is fully initialized by then.  */
00db10
 
00db10
 void *
00db10
 calloc (size_t n, size_t m)
00db10
 {
00db10
-  if (magic[0] != MAGIC1 || magic[1] != MAGIC2)
00db10
+  if (!calloc_called)
00db10
     {
00db10
-      printf ("{%x, %x} != {%x, %x}\n", magic[0], magic[1], MAGIC1, MAGIC2);
00db10
-      abort ();
00db10
+      /* Allow our calloc to be called more than once.  */
00db10
+      calloc_called = 1;
00db10
+      if (magic[0] != MAGIC1 || magic[1] != MAGIC2)
00db10
+	{
00db10
+	  printf ("{%x, %x} != {%x, %x}\n",
00db10
+		  magic[0], magic[1], MAGIC1, MAGIC2);
00db10
+	  abort ();
00db10
+	}
00db10
+      magic[0] = MAGIC2;
00db10
+      magic[1] = MAGIC1;
00db10
     }
00db10
-  magic[0] = MAGIC2;
00db10
-  magic[1] = MAGIC1;
00db10
 
00db10
   n *= m;
00db10
   void *ptr = malloc (n);
00db10
@@ -34,6 +43,11 @@ calloc (size_t n, size_t m)
00db10
 static int
00db10
 do_test (void)
00db10
 {
00db10
+  /* Make sure that our calloc is called from the dynamic linker at least
00db10
+     once.  */
00db10
+  void *h = dlopen("$ORIGIN/tst-auditmod9b.so", RTLD_LAZY);
00db10
+  if (h != NULL)
00db10
+    dlclose (h);
00db10
   if (magic[1] != MAGIC1 || magic[0] != MAGIC2)
00db10
     {
00db10
       printf ("{%x, %x} != {%x, %x}\n", magic[0], magic[1], MAGIC2, MAGIC1);