Blob Blame History Raw
commit 58007e9e68913290b1f4f73afc1055f779a8ed5d
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu May 28 05:06:27 2015 -0700

    Make sure that calloc is called at least once
    
    PLT relocations aren't required when -z now used.
    
    
    	[BZ #18422]
    	* Makefile ($(objpfx)tst-audit2): Depend on $(libdl).
    	($(objpfx)tst-audit2.out): Also depend on
    	$(objpfx)tst-auditmod9b.so.
    	* elf/tst-audit2.c: Include <dlfcn.h>.
    	(calloc_called): New.
    	(calloc): Allow to be called more than once.
    	(do_test): dllopen/dlclose $ORIGIN/tst-auditmod9b.so.

diff -rup a/elf/Makefile b/elf/Makefile
--- a/elf/Makefile	2017-10-04 16:42:22.000000000 -0400
+++ b/elf/Makefile	2017-10-05 17:40:38.402451971 -0400
@@ -1020,7 +1020,8 @@ $(objpfx)tst-dlmopen3.out: $(objpfx)tst-
 $(objpfx)tst-audit1.out: $(objpfx)tst-auditmod1.so
 tst-audit1-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so
 
-$(objpfx)tst-audit2.out: $(objpfx)tst-auditmod1.so
+$(objpfx)tst-audit2: $(libdl)
+$(objpfx)tst-audit2.out: $(objpfx)tst-auditmod1.so $(objpfx)tst-auditmod9b.so
 tst-audit2-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so
 
 $(objpfx)tst-audit3: $(objpfx)tst-auditmod3a.so
diff -rup a/elf/tst-audit2.c b/elf/tst-audit2.c
--- a/elf/tst-audit2.c	2017-10-04 16:42:18.000000000 -0400
+++ b/elf/tst-audit2.c	2017-10-05 17:40:51.570518031 -0400
@@ -3,26 +3,35 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <dlfcn.h>
 
 #define MAGIC1 0xabcdef72
 #define MAGIC2 0xd8675309
 static __thread unsigned int magic[] = { MAGIC1, MAGIC2 };
+static __thread int calloc_called;
 
 #undef calloc
 
 /* This calloc definition will be called by the dynamic linker itself.
-   We test that it has initialized our TLS block by the time it does so.  */
+   We test that interposed calloc is called by the dynamic loader, and
+   that TLS is fully initialized by then.  */
 
 void *
 calloc (size_t n, size_t m)
 {
-  if (magic[0] != MAGIC1 || magic[1] != MAGIC2)
+  if (!calloc_called)
     {
-      printf ("{%x, %x} != {%x, %x}\n", magic[0], magic[1], MAGIC1, MAGIC2);
-      abort ();
+      /* Allow our calloc to be called more than once.  */
+      calloc_called = 1;
+      if (magic[0] != MAGIC1 || magic[1] != MAGIC2)
+	{
+	  printf ("{%x, %x} != {%x, %x}\n",
+		  magic[0], magic[1], MAGIC1, MAGIC2);
+	  abort ();
+	}
+      magic[0] = MAGIC2;
+      magic[1] = MAGIC1;
     }
-  magic[0] = MAGIC2;
-  magic[1] = MAGIC1;
 
   n *= m;
   void *ptr = malloc (n);
@@ -34,6 +43,11 @@ calloc (size_t n, size_t m)
 static int
 do_test (void)
 {
+  /* Make sure that our calloc is called from the dynamic linker at least
+     once.  */
+  void *h = dlopen("$ORIGIN/tst-auditmod9b.so", RTLD_LAZY);
+  if (h != NULL)
+    dlclose (h);
   if (magic[1] != MAGIC1 || magic[0] != MAGIC2)
     {
       printf ("{%x, %x} != {%x, %x}\n", magic[0], magic[1], MAGIC2, MAGIC1);