ce426f
# commit 122b66defdb9e4ded3ccc5c2b290f0520c6fa3cd
ce426f
# Author: Ulrich Weigand <Ulrich.Weigand@de.ibm.com>
ce426f
# Date:   Wed Dec 4 06:52:40 2013 -0600
ce426f
# 
ce426f
#     PowerPC64 ELFv2 ABI 3/6: PLT local entry point optimization
ce426f
#     
ce426f
#     This is a follow-on to the previous patch to support the ELFv2 ABI in the
ce426f
#     dynamic loader, split off into its own patch since it is just an optional
ce426f
#     optimization.
ce426f
#     
ce426f
#     In the ELFv2 ABI, most functions define both a global and a local entry
ce426f
#     point; the local entry requires r2 to be already set up by the caller
ce426f
#     to point to the callee's TOC; while the global entry does not require
ce426f
#     the caller to know about the callee's TOC, but it needs to set up r12
ce426f
#     to the callee's entry point address.
ce426f
#     
ce426f
#     Now, when setting up a PLT slot, the dynamic linker will usually need
ce426f
#     to enter the target function's global entry point.  However, if the
ce426f
#     linker can prove that the target function is in the same DSO as the
ce426f
#     PLT slot itself, and the whole DSO only uses a single TOC (which the
ce426f
#     linker will let ld.so know via a DT_PPC64_OPT entry), then it is
ce426f
#     possible to actually enter the local entry point address into the
ce426f
#     PLT slot, for a slight improvement in performance.
ce426f
#     
ce426f
#     Note that this uncovered a problem on the first call via _dl_runtime_resolve,
ce426f
#     because that routine neglected to restore the caller's TOC before calling
ce426f
#     the target function for the first time, since it assumed that function
ce426f
#     would always reload its own TOC anyway ...
ce426f
# 
ce426f
diff -urN glibc-2.17-c758a686/elf/elf.h glibc-2.17-c758a686/elf/elf.h
ce426f
--- glibc-2.17-c758a686/elf/elf.h	2014-05-29 14:08:44.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/elf/elf.h	2014-05-29 14:08:44.000000000 -0500
ce426f
@@ -2273,8 +2273,19 @@
ce426f
 #define DT_PPC64_GLINK  (DT_LOPROC + 0)
ce426f
 #define DT_PPC64_OPD	(DT_LOPROC + 1)
ce426f
 #define DT_PPC64_OPDSZ	(DT_LOPROC + 2)
ce426f
+#define DT_PPC64_OPT	(DT_LOPROC + 3)
ce426f
 #define DT_PPC64_NUM    3
ce426f
 
ce426f
+/* PowerPC64 specific values for the DT_PPC64_OPT Dyn entry.  */
ce426f
+#define PPC64_OPT_TLS		1
ce426f
+#define PPC64_OPT_MULTI_TOC	2
ce426f
+
ce426f
+/* PowerPC64 specific values for the Elf64_Sym st_other field.  */
ce426f
+#define STO_PPC64_LOCAL_BIT	5
ce426f
+#define STO_PPC64_LOCAL_MASK	(7 << STO_PPC64_LOCAL_BIT)
ce426f
+#define PPC64_LOCAL_ENTRY_OFFSET(other)				\
ce426f
+ (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
ce426f
+
ce426f
 
ce426f
 /* ARM specific declarations */
ce426f
 
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/dl-machine.h glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/dl-machine.h
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/dl-machine.h	2014-05-29 14:08:40.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/dl-machine.h	2014-05-29 14:08:44.000000000 -0500
ce426f
@@ -425,6 +425,42 @@
ce426f
   return lazy;
ce426f
 }
ce426f
 
ce426f
+#if _CALL_ELF == 2
ce426f
+/* If the PLT entry whose reloc is 'reloc' resolves to a function in
ce426f
+   the same object, return the target function's local entry point
ce426f
+   offset if usable.  */
ce426f
+static inline Elf64_Addr __attribute__ ((always_inline))
ce426f
+ppc64_local_entry_offset (struct link_map *map, lookup_t sym_map,
ce426f
+			  const Elf64_Rela *reloc)
ce426f
+{
ce426f
+  const Elf64_Sym *symtab;
ce426f
+  const Elf64_Sym *sym;
ce426f
+
ce426f
+  /* If the target function is in a different object, we cannot
ce426f
+     use the local entry point.  */
ce426f
+  if (sym_map != map)
ce426f
+    return 0;
ce426f
+
ce426f
+  /* If the linker inserted multiple TOCs, we cannot use the
ce426f
+     local entry point.  */
ce426f
+  if (map->l_info[DT_PPC64(OPT)]
ce426f
+      && (map->l_info[DT_PPC64(OPT)]->d_un.d_val & PPC64_OPT_MULTI_TOC))
ce426f
+    return 0;
ce426f
+
ce426f
+  /* Otherwise, we can use the local entry point.  Retrieve its offset
ce426f
+     from the symbol's ELF st_other field.  */
ce426f
+  symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
ce426f
+  sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
ce426f
+
ce426f
+  /* If the target function is an ifunc then the local entry offset is
ce426f
+     for the resolver, not the final destination.  */
ce426f
+  if (__builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
ce426f
+    return 0;
ce426f
+
ce426f
+  return PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
ce426f
+}
ce426f
+#endif
ce426f
+
ce426f
 /* Change the PLT entry whose reloc is 'reloc' to call the actual
ce426f
    routine.  */
ce426f
 static inline Elf64_Addr __attribute__ ((always_inline))
ce426f
@@ -471,6 +507,7 @@
ce426f
   PPC_DCBST (&plt->fd_func);
ce426f
   PPC_ISYNC;
ce426f
 #else
ce426f
+  finaladdr += ppc64_local_entry_offset (map, sym_map, reloc);
ce426f
   *reloc_addr = finaladdr;
ce426f
 #endif
ce426f
 
ce426f
@@ -478,7 +515,9 @@
ce426f
 }
ce426f
 
ce426f
 static inline void __attribute__ ((always_inline))
ce426f
-elf_machine_plt_conflict (Elf64_Addr *reloc_addr, Elf64_Addr finaladdr)
ce426f
+elf_machine_plt_conflict (struct link_map *map, lookup_t sym_map,
ce426f
+			  const Elf64_Rela *reloc,
ce426f
+			  Elf64_Addr *reloc_addr, Elf64_Addr finaladdr)
ce426f
 {
ce426f
 #if _CALL_ELF != 2
ce426f
   Elf64_FuncDesc *plt = (Elf64_FuncDesc *) reloc_addr;
ce426f
@@ -492,6 +531,7 @@
ce426f
   PPC_DCBST (&plt->fd_toc);
ce426f
   PPC_SYNC;
ce426f
 #else
ce426f
+  finaladdr += ppc64_local_entry_offset (map, sym_map, reloc);
ce426f
   *reloc_addr = finaladdr;
ce426f
 #endif
ce426f
 }
ce426f
@@ -641,7 +681,7 @@
ce426f
       /* Fall thru */
ce426f
     case R_PPC64_JMP_SLOT:
ce426f
 #ifdef RESOLVE_CONFLICT_FIND_MAP
ce426f
-      elf_machine_plt_conflict (reloc_addr, value);
ce426f
+      elf_machine_plt_conflict (map, sym_map, reloc, reloc_addr, value);
ce426f
 #else
ce426f
       elf_machine_fixup_plt (map, sym_map, reloc, reloc_addr, value);
ce426f
 #endif
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/dl-trampoline.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/dl-trampoline.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/dl-trampoline.S	2014-05-29 14:08:40.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/dl-trampoline.S	2014-05-29 14:08:44.000000000 -0500
ce426f
@@ -74,6 +74,10 @@
ce426f
 /* Prepare for calling the function returned by fixup.  */
ce426f
 	PPC64_LOAD_FUNCPTR r3
ce426f
 	ld	r3,INT_PARMS+0(r1)
ce426f
+#if _CALL_ELF == 2
ce426f
+/* Restore the caller's TOC in case we jump to a local entry point.  */
ce426f
+	ld	r2,FRAME_SIZE+40(r1)
ce426f
+#endif
ce426f
 /* Unwind the stack frame, and jump.  */
ce426f
 	addi	r1,r1,FRAME_SIZE
ce426f
 	bctr
ce426f
@@ -321,6 +325,10 @@
ce426f
 /* Prepare for calling the function returned by fixup.  */
ce426f
 	PPC64_LOAD_FUNCPTR r3
ce426f
 	ld	r3,INT_PARMS+0(r1)
ce426f
+#if _CALL_ELF == 2
ce426f
+/* Restore the caller's TOC in case we jump to a local entry point.  */
ce426f
+	ld	r2,FRAME_SIZE+40(r1)
ce426f
+#endif
ce426f
 /* Load the floating point registers.  */
ce426f
 	lfd	fp1,FPR_PARMS+0(r1)
ce426f
 	lfd	fp2,FPR_PARMS+8(r1)