84f64e
Note: the context of this patch differs from upstream slightly,
84f64e
to accomodate the lack of ILP32 in RHEL.
84f64e
84f64e
commit b7cf203b5c17dd6d9878537d41e0c7cc3d270a67
84f64e
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
84f64e
Date:   Wed Sep 27 16:55:14 2017 +0100
84f64e
84f64e
    aarch64: Disable lazy symbol binding of TLSDESC
84f64e
    
84f64e
    Always do TLS descriptor initialization at load time during relocation
84f64e
    processing to avoid barriers at every TLS access. In non-dlopened shared
84f64e
    libraries the overhead of tls access vs static global access is > 3x
84f64e
    bigger when lazy initialization is used (_dl_tlsdesc_return_lazy)
84f64e
    compared to bind-now (_dl_tlsdesc_return) so the barriers dominate tls
84f64e
    access performance.
84f64e
    
84f64e
    TLSDESC relocs are in DT_JMPREL which are processed at load time using
84f64e
    elf_machine_lazy_rel which is only supposed to do lightweight
84f64e
    initialization using the DT_TLSDESC_PLT trampoline (the trampoline code
84f64e
    jumps to the entry point in DT_TLSDESC_GOT which does the lazy tlsdesc
84f64e
    initialization at runtime).  This patch changes elf_machine_lazy_rel
84f64e
    in aarch64 to do the symbol binding and initialization as if DF_BIND_NOW
84f64e
    was set, so the non-lazy code path of elf/do-rel.h was replicated.
84f64e
    
84f64e
    The static linker could be changed to emit TLSDESC relocs in DT_REL*,
84f64e
    which are processed non-lazily, but the goal of this patch is to always
84f64e
    guarantee bind-now semantics, even if the binary was produced with an
84f64e
    old linker, so the barriers can be dropped in tls descriptor functions.
84f64e
    
84f64e
    After this change the synchronizing ldar instructions can be dropped
84f64e
    as well as the lazy initialization machinery including the DT_TLSDESC_GOT
84f64e
    setup.
84f64e
    
84f64e
    I believe this should be done on all targets, including ones where no
84f64e
    barrier is needed for lazy initialization.  There is very little gain in
84f64e
    optimizing for large number of symbolic tlsdesc relocations which is an
84f64e
    extremely uncommon case.  And currently the tlsdesc entries are only
84f64e
    readonly protected with -z now and some hardennings against writable
84f64e
    JUMPSLOT relocs don't work for TLSDESC so they are a security hazard.
84f64e
    (But to fix that the static linker has to be changed.)
84f64e
    
84f64e
    	* sysdeps/aarch64/dl-machine.h (elf_machine_lazy_rel): Do symbol
84f64e
    	binding and initialization non-lazily for R_AARCH64_TLSDESC.
84f64e
84f64e
diff --git a/sysdeps/aarch64/dl-machine.h b/sysdeps/aarch64/dl-machine.h
84f64e
index 4faf818b710d6ed7..185402f6dc550dc1 100644
84f64e
--- a/sysdeps/aarch64/dl-machine.h
84f64e
+++ b/sysdeps/aarch64/dl-machine.h
84f64e
@@ -376,12 +376,21 @@ elf_machine_lazy_rel (struct link_map *map,
84f64e
     }
84f64e
   else if (__builtin_expect (r_type == R_AARCH64_TLSDESC, 1))
84f64e
     {
84f64e
-      struct tlsdesc volatile *td =
84f64e
-	(struct tlsdesc volatile *)reloc_addr;
84f64e
+      const Elf_Symndx symndx = ELFW (R_SYM) (reloc->r_info);
84f64e
+      const ElfW (Sym) *symtab = (const void *)D_PTR (map, l_info[DT_SYMTAB]);
84f64e
+      const ElfW (Sym) *sym = &symtab[symndx];
84f64e
+      const struct r_found_version *version = NULL;
84f64e
 
84f64e
-      td->arg = (void*)reloc;
84f64e
-      td->entry = (void*)(D_PTR (map, l_info[ADDRIDX (DT_TLSDESC_PLT)])
84f64e
-			  + map->l_addr);
84f64e
+      if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
84f64e
+	{
84f64e
+	  const ElfW (Half) *vernum =
84f64e
+	    (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
84f64e
+	  version = &map->l_versions[vernum[symndx] & 0x7fff];
84f64e
+	}
84f64e
+
84f64e
+      /* Always initialize TLS descriptors completely, because lazy
84f64e
+	 initialization requires synchronization at every TLS access.  */
84f64e
+      elf_machine_rela (map, reloc, sym, version, reloc_addr, skip_ifunc);
84f64e
     }
84f64e
   else if (__glibc_unlikely (r_type == R_AARCH64_IRELATIVE))
84f64e
     {