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