Blame SOURCES/glibc-rh1639524.patch

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