ce426f
#
ce426f
# commit 76a9b9986141b1a7d9fd290c349d27fcee780c7a
ce426f
# Author: Adhemerval Zanella <azanella@linux.vnet.ibm.com>
ce426f
# Date:   Thu Nov 7 05:34:22 2013 -0600
ce426f
# 
ce426f
#     PowerPC: Fix vDSO missing ODP entries
ce426f
#    
ce426f
#     This patch fixes the vDSO symbol used directed in IFUNC resolver where
ce426f
#     they do not have an associated ODP entry leading to undefined behavior
ce426f
#     in some cases. It adds an artificial OPD static entry to such cases
ce426f
#     and set its TOC to non 0 to avoid triggering lazy resolutions.
ce426f
#
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/bits/libc-vdso.h glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/bits/libc-vdso.h
ce426f
--- glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/bits/libc-vdso.h	2015-01-15 16:05:08.853681325 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/bits/libc-vdso.h	2015-01-15 16:06:11.451747716 -0500
ce426f
@@ -34,12 +34,32 @@
ce426f
 
ce426f
 extern void *__vdso_time;
ce426f
 
ce426f
-/* This macro is needed for PPC64 to return a skeleton OPD entry of a vDSO
ce426f
-   symbol.  This works because _dl_vdso_vsym always return the function
ce426f
-   address, and no vDSO symbols use the TOC or chain pointers from the OPD
ce426f
-   so we can allow them to be garbage.  */
ce426f
-#if defined(__PPC64__) || defined(__powerpc64__)
ce426f
-#define VDSO_IFUNC_RET(value)  ((void *) &(value))
ce426f
+/* The correct solution is for _dl_vdso_vsym to return the address of the OPD
ce426f
+   for the kernel VDSO function.  That address would then be stored in the
ce426f
+   __vdso_* variables and returned as the result of the IFUNC resolver function.
ce426f
+   Yet, the kernel does not contain any OPD entries for the VDSO functions
ce426f
+   (incomplete implementation).  However, PLT relocations for IFUNCs still expect
ce426f
+   the address of an OPD to be returned from the IFUNC resolver function (since
ce426f
+   PLT entries on PPC64 are just copies of OPDs).  The solution for now is to
ce426f
+   create an artificial static OPD for each VDSO function returned by a resolver
ce426f
+   function.  The TOC value is set to a non-zero value to avoid triggering lazy
ce426f
+   symbol resolution via .glink0/.plt0 for a zero TOC (requires thread-safe PLT
ce426f
+   sequences) when the dynamic linker isn't prepared for it e.g. RTLD_NOW.  None
ce426f
+   of the kernel VDSO routines use the TOC or AUX values so any non-zero value
ce426f
+   will work.  Note that function pointer comparisons will not use this artificial
ce426f
+   static OPD since those are resolved via ADDR64 relocations and will point at
ce426f
+   the non-IFUNC default OPD for the symbol.  Lastly, because the IFUNC relocations
ce426f
+   are processed immediately at startup the resolver functions and this code need
ce426f
+   not be thread-safe, but if the caller writes to a PLT slot it must do so in a
ce426f
+   thread-safe manner with all the required barriers.  */
ce426f
+#if (defined(__PPC64__) || defined(__powerpc64__)) && _CALL_ELF != 2
ce426f
+#define VDSO_IFUNC_RET(value)                            \
ce426f
+  ({                                                     \
ce426f
+    static Elf64_FuncDesc vdso_opd = { .fd_toc = ~0x0 }; \
ce426f
+    vdso_opd.fd_func = (Elf64_Addr)value;                \
ce426f
+    &vdso_opd;                                           \
ce426f
+  })
ce426f
+
ce426f
 #else
ce426f
 #define VDSO_IFUNC_RET(value)  ((void *) (value))
ce426f
 #endif
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
ce426f
--- glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c	2015-01-15 16:05:08.912679502 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c	2015-01-15 16:06:11.451747716 -0500
ce426f
@@ -21,6 +21,7 @@
ce426f
 
ce426f
 # include <dl-vdso.h>
ce426f
 # include <bits/libc-vdso.h>
ce426f
+# include <dl-machine.h>
ce426f
 
ce426f
 void *gettimeofday_ifunc (void) __asm__ ("__gettimeofday");
ce426f
 
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/time.c glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/time.c
ce426f
--- glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/time.c	2015-01-15 16:05:08.912679502 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/time.c	2015-01-15 16:06:11.451747716 -0500
ce426f
@@ -20,7 +20,9 @@
ce426f
 
ce426f
 # include <time.h>
ce426f
 # include <sysdep.h>
ce426f
+# include <dl-vdso.h>
ce426f
 # include <bits/libc-vdso.h>
ce426f
+# include <dl-machine.h>
ce426f
 
ce426f
 void *time_ifunc (void) asm ("time");
ce426f