unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Introduce __libc_early_init (for rseq support)
@ 2019-11-28 13:49 Florian Weimer
  2019-11-28 13:50 ` [PATCH 1/2] Introduce <elf_machine_sym_no_match.h> Florian Weimer
  2019-11-28 13:50 ` [PATCH 2/2] Implement __libc_early_init Florian Weimer
  0 siblings, 2 replies; 8+ messages in thread
From: Florian Weimer @ 2019-11-28 13:49 UTC (permalink / raw
  To: libc-alpha

I plan to use this functionality to fix __libc_multiple_libcs and to
implement __libc_signal_threaded as well.

The rseq initialization will be called from __libc_early_init.

Florian Weimer (2):
  Introduce <elf_machine_sym_no_match.h>
  Implement __libc_early_init

 ChangeLog.old/ChangeLog.19              |  40 ++++++++
 csu/init-first.c                        |   4 -
 csu/libc-start.c                        |   5 +
 elf/Makefile                            |   5 +-
 elf/Versions                            |   1 +
 elf/dl-call-libc-early-init.c           |  41 +++++++++
 elf/dl-load.c                           |   9 ++
 elf/dl-lookup-direct.c                  | 116 ++++++++++++++++++++++++
 elf/dl-lookup.c                         |  10 +-
 elf/dl-open.c                           |  24 +++++
 elf/elf_machine_sym_no_match.h          |  34 +++++++
 elf/libc-early-init.h                   |  35 +++++++
 elf/libc_early_init.c                   |  27 ++++++
 elf/rtld.c                              |   4 +
 sysdeps/generic/ldsodefs.h              |  17 ++++
 sysdeps/mach/hurd/i386/init-first.c     |   4 -
 sysdeps/mips/dl-machine.h               |  15 ---
 sysdeps/mips/elf_machine_sym_no_match.h |  43 +++++++++
 18 files changed, 401 insertions(+), 33 deletions(-)
 create mode 100644 elf/dl-call-libc-early-init.c
 create mode 100644 elf/dl-lookup-direct.c
 create mode 100644 elf/elf_machine_sym_no_match.h
 create mode 100644 elf/libc-early-init.h
 create mode 100644 elf/libc_early_init.c
 create mode 100644 sysdeps/mips/elf_machine_sym_no_match.h

-- 
2.23.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/2] Introduce <elf_machine_sym_no_match.h>
  2019-11-28 13:49 [PATCH 0/2] Introduce __libc_early_init (for rseq support) Florian Weimer
@ 2019-11-28 13:50 ` Florian Weimer
  2019-12-17 16:42   ` Florian Weimer
  2019-11-28 13:50 ` [PATCH 2/2] Implement __libc_early_init Florian Weimer
  1 sibling, 1 reply; 8+ messages in thread
From: Florian Weimer @ 2019-11-28 13:50 UTC (permalink / raw
  To: libc-alpha

MIPS needs to ignore certain existing symbols during symbol lookup.
The old scheme uses the ELF_MACHINE_SYM_NO_MATCH macro, with an
inline function, within its own header, with a sysdeps override for
MIPS.  This allows re-use of the function from another file (without
having to include <dl-machine.h> or providing the default definition
for ELF_MACHINE_SYM_NO_MATCH).

Built with build-many-glibcs.py, with manual verification that
sysdeps/mips/elf_machine_sym_no_match.h is picked up on MIPS.  Tested
on aarch64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
s390x-linux-gnu, x86_64-linux-gnu.
---
 ChangeLog.old/ChangeLog.19              | 11 +++++++
 elf/dl-lookup.c                         | 10 ++----
 elf/elf_machine_sym_no_match.h          | 34 +++++++++++++++++++
 sysdeps/mips/dl-machine.h               | 15 ---------
 sysdeps/mips/elf_machine_sym_no_match.h | 43 +++++++++++++++++++++++++
 5 files changed, 90 insertions(+), 23 deletions(-)
 create mode 100644 elf/elf_machine_sym_no_match.h
 create mode 100644 sysdeps/mips/elf_machine_sym_no_match.h

diff --git a/ChangeLog.old/ChangeLog.19 b/ChangeLog.old/ChangeLog.19
index 84d56ed5e8..6804d33c3e 100644
--- a/ChangeLog.old/ChangeLog.19
+++ b/ChangeLog.old/ChangeLog.19
@@ -1,3 +1,14 @@
+2019-10-11  Florian Weimer  <fweimer@redhat.com>
+
+	* elf/elf_machine_sym_no_match.h: New file.
+	* elf/dl-lookip.c (ELF_MACHINE_SYM_NO_MATCH): Do not define.
+	* elf/dl-lookup.c (check_match) Call elf_machine_sym_no_match
+	instead of ELF_MACHINE_SYM_NO_MATCH.
+	* sysdeps/mips/dl-machine.h (ELF_MACHINE_SYM_NO_MATCH): Remove
+	definition.
+	* sysdeps/mips/elf_machine_sym_no_match.h: New file.  Extracted
+	from sysdeps/mips/dl-machine.h.
+
 2019-10-11  Florian Weimer  <fweimer@redhat.com>
 
 	* manual/startup.texi (Aborting a Program): Remove warning.
diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index a2e85a5568..34b39a72f9 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -28,18 +28,12 @@
 #include <libc-lock.h>
 #include <tls.h>
 #include <atomic.h>
+#include <elf_machine_sym_no_match.h>
 
 #include <assert.h>
 
-/* Return nonzero if check_match should consider SYM to fail to match a
-   symbol reference for some machine-specific reason.  */
-#ifndef ELF_MACHINE_SYM_NO_MATCH
-# define ELF_MACHINE_SYM_NO_MATCH(sym) 0
-#endif
-
 #define VERSTAG(tag)	(DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
 
-
 struct sym_val
   {
     const ElfW(Sym) *s;
@@ -78,7 +72,7 @@ check_match (const char *const undef_name,
   if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
 			 && sym->st_shndx != SHN_ABS
 			 && stt != STT_TLS)
-			|| ELF_MACHINE_SYM_NO_MATCH (sym)
+			|| elf_machine_sym_no_match (sym)
 			|| (type_class & (sym->st_shndx == SHN_UNDEF))))
     return NULL;
 
diff --git a/elf/elf_machine_sym_no_match.h b/elf/elf_machine_sym_no_match.h
new file mode 100644
index 0000000000..6e299e5ee8
--- /dev/null
+++ b/elf/elf_machine_sym_no_match.h
@@ -0,0 +1,34 @@
+/* Function to ignore certain symbol matches for machine-specific reasons.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _ELF_MACHINE_SYM_NO_MATCH_H
+#define _ELF_MACHINE_SYM_NO_MATCH_H
+
+#include <link.h>
+#include <stdbool.h>
+
+/* This can be customized to ignore certain symbols during lookup in
+   case there are machine-specific rules to disregard some
+   symbols.  */
+static inline bool
+elf_machine_sym_no_match (const ElfW(Sym) *sym)
+{
+  return false;
+}
+
+#endif /* _ELF_MACHINE_SYM_NO_MATCH_H */
diff --git a/sysdeps/mips/dl-machine.h b/sysdeps/mips/dl-machine.h
index c036c7a905..b238ab6476 100644
--- a/sysdeps/mips/dl-machine.h
+++ b/sysdeps/mips/dl-machine.h
@@ -467,21 +467,6 @@ elf_machine_plt_value (struct link_map *map, const ElfW(Rel) *reloc,
   return value;
 }
 
-/* The semantics of zero/non-zero values of undefined symbols differs
-   depending on whether the non-PIC ABI is in use.  Under the non-PIC
-   ABI, a non-zero value indicates that there is an address reference
-   to the symbol and thus it must always be resolved (except when
-   resolving a jump slot relocation) to the PLT entry whose address is
-   provided as the symbol's value; a zero value indicates that this
-   canonical-address behaviour is not required.  Yet under the classic
-   MIPS psABI, a zero value indicates that there is an address
-   reference to the function and the dynamic linker must resolve the
-   symbol immediately upon loading.  To avoid conflict, symbols for
-   which the dynamic linker must assume the non-PIC ABI semantics are
-   marked with the STO_MIPS_PLT flag.  */
-#define ELF_MACHINE_SYM_NO_MATCH(sym) \
-  ((sym)->st_shndx == SHN_UNDEF && !((sym)->st_other & STO_MIPS_PLT))
-
 #endif /* !dl_machine_h */
 
 #ifdef RESOLVE_MAP
diff --git a/sysdeps/mips/elf_machine_sym_no_match.h b/sysdeps/mips/elf_machine_sym_no_match.h
new file mode 100644
index 0000000000..f2be74caaf
--- /dev/null
+++ b/sysdeps/mips/elf_machine_sym_no_match.h
@@ -0,0 +1,43 @@
+/* MIPS-specific handling of undefined symbols.
+   Copyright (C) 2008-2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _ELF_MACHINE_SYM_NO_MATCH_H
+#define _ELF_MACHINE_SYM_NO_MATCH_H
+
+#include <link.h>
+#include <stdbool.h>
+
+/* The semantics of zero/non-zero values of undefined symbols differs
+   depending on whether the non-PIC ABI is in use.  Under the non-PIC
+   ABI, a non-zero value indicates that there is an address reference
+   to the symbol and thus it must always be resolved (except when
+   resolving a jump slot relocation) to the PLT entry whose address is
+   provided as the symbol's value; a zero value indicates that this
+   canonical-address behaviour is not required.  Yet under the classic
+   MIPS psABI, a zero value indicates that there is an address
+   reference to the function and the dynamic linker must resolve the
+   symbol immediately upon loading.  To avoid conflict, symbols for
+   which the dynamic linker must assume the non-PIC ABI semantics are
+   marked with the STO_MIPS_PLT flag.  */
+static inline bool
+elf_machine_sym_no_match (const ElfW(Sym) *sym)
+{
+  return sym->st_shndx == SHN_UNDEF && !(sym->st_other & STO_MIPS_PLT);
+}
+
+#endif /* _ELF_MACHINE_SYM_NO_MATCH_H */
-- 
2.23.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/2] Implement __libc_early_init
  2019-11-28 13:49 [PATCH 0/2] Introduce __libc_early_init (for rseq support) Florian Weimer
  2019-11-28 13:50 ` [PATCH 1/2] Introduce <elf_machine_sym_no_match.h> Florian Weimer
@ 2019-11-28 13:50 ` Florian Weimer
  2019-12-17 16:50   ` Florian Weimer
  1 sibling, 1 reply; 8+ messages in thread
From: Florian Weimer @ 2019-11-28 13:50 UTC (permalink / raw
  To: libc-alpha

This function is defined in libc.so, and the dynamic loader calls
right after relocation has been finished, before any ELF constructors
or the preinit function is invoked.  It is also used in the static
build for initializing parts of the static libc.

To locate __libc_early_init, a direct symbol lookup function is used,
_dl_lookup_direct.  It does not search the entire symbol scope and
consults merely a single link map.  This function could also be used
to implement lookups in the vDSO (as an optimization).

A per-namespace variable (libc_map) is added for locating libc.so,
to avoid repeated traversals of the search scope.  It is similar to
GL(dl_initfirst).  An alternative would have been to thread a context
argument from _dl_open down to _dl_map_object_from_fd (where libc.so
is identified).  This could have avoided the global variable, but
the change would be larger as a result.  It would not have been
possible to use this to replace GL(dl_initfirst) because that global
variable is used to pass the function pointer past the stack switch
from dl_main to the main program.  Replacing that requires adding
a new argument to _dl_init, which in turn needs changes to the
architecture-specific libc.so startup code written in assembler.

__libc_early_init should not be used to replace _dl_var_init (as
it exists today on some architectures).  Instead, _dl_lookup_direct
should be used to look up a new variable symbol in libc.so, and
that should then be initialized from the dynamic loader, immediately
after the object has been loaded in _dl_map_object_from_fd (before
relocation is run).  This way, more IFUNC resolvers which depend on
these variables will work.

This version was tested on x86_64-linux-gnu.
---
 ChangeLog.old/ChangeLog.19          |  29 +++++++
 csu/init-first.c                    |   4 -
 csu/libc-start.c                    |   5 ++
 elf/Makefile                        |   5 +-
 elf/Versions                        |   1 +
 elf/dl-call-libc-early-init.c       |  41 ++++++++++
 elf/dl-load.c                       |   9 +++
 elf/dl-lookup-direct.c              | 116 ++++++++++++++++++++++++++++
 elf/dl-open.c                       |  24 ++++++
 elf/libc-early-init.h               |  35 +++++++++
 elf/libc_early_init.c               |  27 +++++++
 elf/rtld.c                          |   4 +
 sysdeps/generic/ldsodefs.h          |  17 ++++
 sysdeps/mach/hurd/i386/init-first.c |   4 -
 14 files changed, 311 insertions(+), 10 deletions(-)
 create mode 100644 elf/dl-call-libc-early-init.c
 create mode 100644 elf/dl-lookup-direct.c
 create mode 100644 elf/libc-early-init.h
 create mode 100644 elf/libc_early_init.c

diff --git a/ChangeLog.old/ChangeLog.19 b/ChangeLog.old/ChangeLog.19
index 6804d33c3e..9f06c9ce0c 100644
--- a/ChangeLog.old/ChangeLog.19
+++ b/ChangeLog.old/ChangeLog.19
@@ -1,3 +1,32 @@
+2019-10-11  Florian Weimer  <fweimer@redhat.com>
+
+	* csu/init-first.c (_init): Remove call to __ctype_init.  Moved to
+	__libc_early_init.
+	* csu/libc-start.c [!SHARED] (LIBC_START_MAIN): Call
+	__libc_early_init.
+	* elf/Makefile (routines): Add libc_early_init.
+	(dl-routines): Add dl-call-libc-early-init.
+	* elf/Versions (libc): Export __libc_early_init under
+	GLIBC_PRIVATE.
+	* elf/dl-call-libc-early-init.c: New file.
+	* elf/dl-load.c (_dl_map_object_from_fd): Record in the namespace
+	description if libc.so has been loaded.
+	* elf/dl-lookup-direct.c: New file.  Extracted from
+	elf/dl-lookup.c.
+	* elf/dl-open.c (struct dl_open_args): Add libc_already_loaded.
+	(dl_open_worker): Set libc_already_loaded.  Call
+	_dl_call_libc_early_init if libc.so has been loaded.
+	(_dl_open): Initialize libc_already_loaded.  Reset libc_map on
+	failure if necessary.
+	* elf/libc-early-init.h: New file.
+	* elf/libc_early_init.c: Likewise.
+	* elf/rtld.c (dl_main): Call _dl_call_libc_early_init.
+	* sysdeps/generic/ldsodefs.h (struct rtld_global): Add libc_map
+	member to the namespace description.
+	(_dl_lookup_direct): Declare.
+	* sysdeps/mach/hurd/i386/init-first.c (posixland_init): Do not
+	call __ctype_init.
+
 2019-10-11  Florian Weimer  <fweimer@redhat.com>
 
 	* elf/elf_machine_sym_no_match.h: New file.
diff --git a/csu/init-first.c b/csu/init-first.c
index e0f489ee6d..8caec305b7 100644
--- a/csu/init-first.c
+++ b/csu/init-first.c
@@ -16,7 +16,6 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
@@ -80,9 +79,6 @@ _init (int argc, char **argv, char **envp)
 
   __init_misc (argc, argv, envp);
 
-  /* Initialize ctype data.  */
-  __ctype_init ();
-
 #if defined SHARED && !defined NO_CTORS_DTORS_SECTIONS
   __libc_global_ctors ();
 #endif
diff --git a/csu/libc-start.c b/csu/libc-start.c
index e9e8e44fe5..b2d83a503a 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -22,6 +22,7 @@
 #include <ldsodefs.h>
 #include <exit-thread.h>
 #include <libc-internal.h>
+#include <elf/libc-early-init.h>
 
 #include <elf/dl-tunables.h>
 
@@ -238,6 +239,10 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
     __cxa_atexit ((void (*) (void *)) rtld_fini, NULL, NULL);
 
 #ifndef SHARED
+  /* Perform early initialization.  In the shared case, this function
+     is called from the dynamic loader as early as possible.  */
+  __libc_early_init ();
+
   /* Call the initializer of the libc.  This is only needed here if we
      are compiling for the static library in which case we haven't
      run the constructors in `_dl_start_user'.  */
diff --git a/elf/Makefile b/elf/Makefile
index 305bed2d38..60ed80cdbe 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -25,7 +25,7 @@ headers		= elf.h bits/elfclass.h link.h bits/link.h
 routines	= $(all-dl-routines) dl-support dl-iteratephdr \
 		  dl-addr dl-addr-obj enbl-secure dl-profstub \
 		  dl-origin dl-libc dl-sym dl-sysdep dl-error \
-		  dl-reloc-static-pie
+		  dl-reloc-static-pie libc_early_init
 
 # The core dynamic linking functions are in libc for the static and
 # profiled libraries.
@@ -33,7 +33,8 @@ dl-routines	= $(addprefix dl-,load lookup object reloc deps hwcaps \
 				  runtime init fini debug misc \
 				  version profile tls origin scope \
 				  execstack open close trampoline \
-				  exception sort-maps)
+				  exception sort-maps lookup-direct \
+				  call-libc-early-init)
 ifeq (yes,$(use-ldconfig))
 dl-routines += dl-cache
 endif
diff --git a/elf/Versions b/elf/Versions
index 3b09901f6c..f26d2817c3 100644
--- a/elf/Versions
+++ b/elf/Versions
@@ -26,6 +26,7 @@ libc {
     _dl_open_hook; _dl_open_hook2;
     _dl_sym; _dl_vsym;
     __libc_dlclose; __libc_dlopen_mode; __libc_dlsym; __libc_dlvsym;
+    __libc_early_init;
 
     # Internal error handling support.  Interposes the functions in ld.so.
     _dl_signal_exception; _dl_catch_exception;
diff --git a/elf/dl-call-libc-early-init.c b/elf/dl-call-libc-early-init.c
new file mode 100644
index 0000000000..6c3ac5bfe7
--- /dev/null
+++ b/elf/dl-call-libc-early-init.c
@@ -0,0 +1,41 @@
+/* Invoke the early initialization function in libc.so.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <assert.h>
+#include <ldsodefs.h>
+#include <libc-early-init.h>
+#include <link.h>
+#include <stddef.h>
+
+void
+_dl_call_libc_early_init (struct link_map *libc_map)
+{
+  /* There is nothing to do if we did not actually load libc.so.  */
+  if (libc_map == NULL)
+    return;
+
+  const ElfW(Sym) *sym
+    = _dl_lookup_direct (libc_map, "__libc_early_init",
+                         0x69682ac, /* dl_new_hash output.  */
+                         "GLIBC_PRIVATE",
+                         0x0963cf85); /* _dl_elf_hash output.  */
+  assert (sym != NULL);
+  __typeof (__libc_early_init) *early_init
+    = DL_SYMBOL_ADDRESS (libc_map, sym);
+  early_init ();
+}
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 6cdd11e6b0..dcaa5a8a53 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -30,6 +30,7 @@
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <gnu/lib-names.h>
 
 /* Type for the buffer we put the ELF header and hopefully the program
    header.  This buffer does not really have to be too large.  In most
@@ -1374,6 +1375,14 @@ cannot enable executable stack as shared object requires");
     add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
 			    + l->l_info[DT_SONAME]->d_un.d_val));
 
+  /* If we have newly loaded libc.so, update the namespace
+     description.  */
+  if (GL(dl_ns)[nsid].libc_map == NULL
+      && l->l_info[DT_SONAME] != NULL
+      && strcmp (((const char *) D_PTR (l, l_info[DT_STRTAB])
+		  + l->l_info[DT_SONAME]->d_un.d_val), LIBC_SO) == 0)
+    GL(dl_ns)[nsid].libc_map = l;
+
   /* _dl_close can only eventually undo the module ID assignment (via
      remove_slotinfo) if this function returns a pointer to a link
      map.  Therefore, delay this step until all possibilities for
diff --git a/elf/dl-lookup-direct.c b/elf/dl-lookup-direct.c
new file mode 100644
index 0000000000..190b826e1e
--- /dev/null
+++ b/elf/dl-lookup-direct.c
@@ -0,0 +1,116 @@
+/* Look up a symbol in a single specified object.
+   Copyright (C) 1995-2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <ldsodefs.h>
+#include <string.h>
+#include <elf_machine_sym_no_match.h>
+#include <dl-hash.h>
+
+/* This function corresponds to do_lookup_x in elf/dl-lookup.c.  The
+   variant here is simplified because it requires symbol
+   versioning. */
+static const ElfW(Sym) *
+check_match (const struct link_map *const map, const char *const undef_name,
+             const char *version, uint32_t version_hash,
+             const Elf_Symndx symidx)
+{
+  const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
+  const ElfW(Sym) *sym = &symtab[symidx];
+
+  unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
+  if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
+                         && sym->st_shndx != SHN_ABS
+                         && stt != STT_TLS)
+                        || elf_machine_sym_no_match (sym)))
+    return NULL;
+
+  /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
+     STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
+     code/data definitions.  */
+#define ALLOWED_STT \
+  ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
+   | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
+  if (__glibc_unlikely (((1 << stt) & ALLOWED_STT) == 0))
+    return NULL;
+
+  const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
+
+  if (strcmp (strtab + sym->st_name, undef_name) != 0)
+    /* Not the symbol we are looking for.  */
+    return NULL;
+
+  ElfW(Half) ndx = map->l_versyms[symidx] & 0x7fff;
+  if (map->l_versions[ndx].hash != version_hash
+      || strcmp (map->l_versions[ndx].name, version) != 0)
+    /* It's not the version we want.  */
+    return NULL;
+
+  return sym;
+}
+
+
+/* This function corresponds to do_lookup_x in elf/dl-lookup.c.  The
+   variant here is simplified because it does not search object
+   dependencies.  It is optimized for a successful lookup.  */
+const ElfW(Sym) *
+_dl_lookup_direct (struct link_map *map,
+                   const char *undef_name, uint32_t new_hash,
+                   const char *version, uint32_t version_hash)
+{
+  const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
+  if (__glibc_likely (bitmask != NULL))
+    {
+      Elf32_Word bucket = map->l_gnu_buckets[new_hash % map->l_nbuckets];
+      if (bucket != 0)
+        {
+          const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
+
+          do
+            if (((*hasharr ^ new_hash) >> 1) == 0)
+              {
+                Elf_Symndx symidx = ELF_MACHINE_HASH_SYMIDX (map, hasharr);
+                const ElfW(Sym) *sym = check_match (map, undef_name,
+                                                    version, version_hash,
+                                                    symidx);
+                if (sym != NULL)
+                  return sym;
+              }
+          while ((*hasharr++ & 1u) == 0);
+        }
+    }
+  else
+    {
+      /* Fallback code for lack of GNU_HASH support.  */
+      uint32_t old_hash = _dl_elf_hash (undef_name);
+
+      /* Use the old SysV-style hash table.  Search the appropriate
+         hash bucket in this object's symbol table for a definition
+         for the same symbol name.  */
+      for (Elf_Symndx symidx = map->l_buckets[old_hash % map->l_nbuckets];
+           symidx != STN_UNDEF;
+           symidx = map->l_chain[symidx])
+        {
+          const ElfW(Sym) *sym = check_match (map, undef_name,
+                                              version, version_hash, symidx);
+          if (sym != NULL)
+            return sym;
+        }
+    }
+
+  return NULL;
+}
diff --git a/elf/dl-open.c b/elf/dl-open.c
index df9f29a5e5..9819b49e4f 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -35,6 +35,7 @@
 #include <libc-internal.h>
 #include <array_length.h>
 #include <internal-signals.h>
+#include <libc-early-init.h>
 
 #include <dl-dst.h>
 #include <dl-prop.h>
@@ -53,6 +54,13 @@ struct dl_open_args
   /* Namespace ID.  */
   Lmid_t nsid;
 
+  /* Set to true if libc.so was already loaded into the namespace at
+     the time dl_open_worker was called.  This is used to determine
+     whether libc.so early initialization needs to before, and whether
+     to roll back the cached libc_map value in the namespace in case
+     of a dlopen failure.  */
+  bool libc_already_loaded;
+
   /* Original signal mask.  Used for unblocking signal handlers before
      running ELF constructors.  */
   sigset_t original_signal_mask;
@@ -511,6 +519,11 @@ dl_open_worker (void *a)
 	args->nsid = call_map->l_ns;
     }
 
+  /* The namespace ID is now known.  Keep track of whether libc.so was
+     already loaded, to determine whether it is necessary to call the
+     early initialization routine (or clear libc_map on error).  */
+  args->libc_already_loaded = GL(dl_ns)[args->nsid].libc_map != NULL;
+
   /* Retain the old value, so that it can be restored.  */
   args->original_global_scope_pending_adds
     = GL (dl_ns)[args->nsid]._ns_global_scope_pending_adds;
@@ -745,6 +758,11 @@ dl_open_worker (void *a)
   if (relocation_in_progress)
     LIBC_PROBE (reloc_complete, 3, args->nsid, r, new);
 
+  /* If libc.so was not there before, attempt to call its early
+     initialization routine.  */
+  if (!args->libc_already_loaded)
+    _dl_call_libc_early_init (GL(dl_ns)[args->nsid].libc_map);
+
 #ifndef SHARED
   DL_STATIC_INIT (new);
 #endif
@@ -843,6 +861,7 @@ no more namespaces available for dlmopen()"));
   args.caller_dlopen = caller_dlopen;
   args.map = NULL;
   args.nsid = nsid;
+  args.libc_already_loaded = true; /* No reset below with early failure.  */
   args.argc = argc;
   args.argv = argv;
   args.env = env;
@@ -875,6 +894,11 @@ no more namespaces available for dlmopen()"));
   /* See if an error occurred during loading.  */
   if (__glibc_unlikely (exception.errstring != NULL))
     {
+      /* Avoid keeping around a dangling reference to the libc.so link
+	 map in case it has been cached in libc_map.  */
+      if (!args.libc_already_loaded)
+	GL(dl_ns)[nsid].libc_map = NULL;
+
       /* Remove the object from memory.  It may be in an inconsistent
 	 state if relocation failed, for example.  */
       if (args.map)
diff --git a/elf/libc-early-init.h b/elf/libc-early-init.h
new file mode 100644
index 0000000000..02b855754e
--- /dev/null
+++ b/elf/libc-early-init.h
@@ -0,0 +1,35 @@
+/* Early initialization of libc.so.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _LIBC_EARLY_INIT_H
+#define _LIBC_EARLY_INIT_H
+
+struct link_map;
+
+/* If LIBC_MAP is not NULL, look up the __libc_early_init symbol in it
+   and call this function.  */
+void _dl_call_libc_early_init (struct link_map *libc_map) attribute_hidden;
+
+/* In the shared case, this function is defined in libc.so and invoked
+   from ld.so (or on the fist static dlopen) after complete relocation
+   of a new loaded libc.so, but before user-defined ELF constructors
+   run.  In the static case, this function is called directly from the
+   startup code.  */
+void __libc_early_init (void);
+
+#endif /* _LIBC_EARLY_INIT_H */
diff --git a/elf/libc_early_init.c b/elf/libc_early_init.c
new file mode 100644
index 0000000000..1ac66d895d
--- /dev/null
+++ b/elf/libc_early_init.c
@@ -0,0 +1,27 @@
+/* Early initialization of libc.so, libc.so side.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <ctype.h>
+#include <libc-early-init.h>
+
+void
+__libc_early_init (void)
+{
+  /* Initialize ctype data.  */
+  __ctype_init ();
+}
diff --git a/elf/rtld.c b/elf/rtld.c
index dd8fc5e6c6..8c27c62aa0 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -43,6 +43,7 @@
 #include <stap-probe.h>
 #include <stackinfo.h>
 #include <not-cancel.h>
+#include <libc-early-init.h>
 
 #include <assert.h>
 
@@ -2315,6 +2316,9 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
       rtld_timer_accum (&relocate_time, start);
     }
 
+  /* Relocation is complete.  Perform early libc initialization.  */
+  _dl_call_libc_early_init (GL(dl_ns)[LM_ID_BASE].libc_map);
+
   /* Do any necessary cleanups for the startup OS interface code.
      We do these now so that no calls are made after rtld re-relocation
      which might be resolved to different functions than we expect.
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index fc25a81e1c..f767baea83 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -336,6 +336,10 @@ struct rtld_global
        recursive dlopen calls from ELF constructors.  */
     unsigned int _ns_global_scope_pending_adds;
 
+    /* Once libc.so has been loaded into the namespace, this points to
+       its link map.  */
+    struct link_map *libc_map;
+
     /* Search table for unique objects.  */
     struct unique_sym_table
     {
@@ -940,6 +944,19 @@ extern lookup_t _dl_lookup_symbol_x (const char *undef,
      attribute_hidden;
 
 
+/* Restricted version of _dl_lookup_symbol_x.  Searches MAP (and only
+   MAP) for the symbol UNDEF_NAME, with GNU hash NEW_HASH (computed
+   with dl_new_hash), symbol version VERSION, and symbol version hash
+   VERSION_HASH (computed with _dl_elf_hash).  Returns a pointer to
+   the symbol table entry in MAP on success, or NULL on failure.  MAP
+   must have symbol versioning information, or otherwise the result is
+   undefined.  */
+const ElfW(Sym) *_dl_lookup_direct (struct link_map *map,
+				    const char *undef_name,
+				    uint32_t new_hash,
+				    const char *version,
+				    uint32_t version_hash) attribute_hidden;
+
 /* Add the new link_map NEW to the end of the namespace list.  */
 extern void _dl_add_to_namespace_list (struct link_map *new, Lmid_t nsid)
      attribute_hidden;
diff --git a/sysdeps/mach/hurd/i386/init-first.c b/sysdeps/mach/hurd/i386/init-first.c
index 5c21ebb958..6c18e83195 100644
--- a/sysdeps/mach/hurd/i386/init-first.c
+++ b/sysdeps/mach/hurd/i386/init-first.c
@@ -17,7 +17,6 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
-#include <ctype.h>
 #include <hurd.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -85,9 +84,6 @@ posixland_init (int argc, char **argv, char **envp)
 #endif
   __init_misc (argc, argv, envp);
 
-  /* Initialize ctype data.  */
-  __ctype_init ();
-
 #if defined SHARED && !defined NO_CTORS_DTORS_SECTIONS
   __libc_global_ctors ();
 #endif
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] Introduce <elf_machine_sym_no_match.h>
  2019-11-28 13:50 ` [PATCH 1/2] Introduce <elf_machine_sym_no_match.h> Florian Weimer
@ 2019-12-17 16:42   ` Florian Weimer
  2019-12-17 16:54     ` Andreas Schwab
  2020-03-26 16:39     ` H.J. Lu via Libc-alpha
  0 siblings, 2 replies; 8+ messages in thread
From: Florian Weimer @ 2019-12-17 16:42 UTC (permalink / raw
  To: libc-alpha

Rebased, and dropped the false ChangeLog update.

Thanks,
Florian

8<------------------------------------------------------------------8<
MIPS needs to ignore certain existing symbols during symbol lookup.
The old scheme uses the ELF_MACHINE_SYM_NO_MATCH macro, with an
inline function, within its own header, with a sysdeps override for
MIPS.  This allows re-use of the function from another file (without
having to include <dl-machine.h> or providing the default definition
for ELF_MACHINE_SYM_NO_MATCH).

Built with build-many-glibcs.py, with manual verification that
sysdeps/mips/elf_machine_sym_no_match.h is picked up on MIPS.  Tested
on aarch64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
s390x-linux-gnu, x86_64-linux-gnu.

-----
 elf/dl-lookup.c                         | 10 ++------
 elf/elf_machine_sym_no_match.h          | 34 ++++++++++++++++++++++++++
 sysdeps/mips/dl-machine.h               | 15 ------------
 sysdeps/mips/elf_machine_sym_no_match.h | 43 +++++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+), 23 deletions(-)

diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index 759b45a2c9..55e521988e 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -28,18 +28,12 @@
 #include <libc-lock.h>
 #include <tls.h>
 #include <atomic.h>
+#include <elf_machine_sym_no_match.h>
 
 #include <assert.h>
 
-/* Return nonzero if check_match should consider SYM to fail to match a
-   symbol reference for some machine-specific reason.  */
-#ifndef ELF_MACHINE_SYM_NO_MATCH
-# define ELF_MACHINE_SYM_NO_MATCH(sym) 0
-#endif
-
 #define VERSTAG(tag)	(DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
 
-
 struct sym_val
   {
     const ElfW(Sym) *s;
@@ -78,7 +72,7 @@ check_match (const char *const undef_name,
   if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
 			 && sym->st_shndx != SHN_ABS
 			 && stt != STT_TLS)
-			|| ELF_MACHINE_SYM_NO_MATCH (sym)
+			|| elf_machine_sym_no_match (sym)
 			|| (type_class & (sym->st_shndx == SHN_UNDEF))))
     return NULL;
 
diff --git a/elf/elf_machine_sym_no_match.h b/elf/elf_machine_sym_no_match.h
new file mode 100644
index 0000000000..6e299e5ee8
--- /dev/null
+++ b/elf/elf_machine_sym_no_match.h
@@ -0,0 +1,34 @@
+/* Function to ignore certain symbol matches for machine-specific reasons.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _ELF_MACHINE_SYM_NO_MATCH_H
+#define _ELF_MACHINE_SYM_NO_MATCH_H
+
+#include <link.h>
+#include <stdbool.h>
+
+/* This can be customized to ignore certain symbols during lookup in
+   case there are machine-specific rules to disregard some
+   symbols.  */
+static inline bool
+elf_machine_sym_no_match (const ElfW(Sym) *sym)
+{
+  return false;
+}
+
+#endif /* _ELF_MACHINE_SYM_NO_MATCH_H */
diff --git a/sysdeps/mips/dl-machine.h b/sysdeps/mips/dl-machine.h
index c036c7a905..b238ab6476 100644
--- a/sysdeps/mips/dl-machine.h
+++ b/sysdeps/mips/dl-machine.h
@@ -467,21 +467,6 @@ elf_machine_plt_value (struct link_map *map, const ElfW(Rel) *reloc,
   return value;
 }
 
-/* The semantics of zero/non-zero values of undefined symbols differs
-   depending on whether the non-PIC ABI is in use.  Under the non-PIC
-   ABI, a non-zero value indicates that there is an address reference
-   to the symbol and thus it must always be resolved (except when
-   resolving a jump slot relocation) to the PLT entry whose address is
-   provided as the symbol's value; a zero value indicates that this
-   canonical-address behaviour is not required.  Yet under the classic
-   MIPS psABI, a zero value indicates that there is an address
-   reference to the function and the dynamic linker must resolve the
-   symbol immediately upon loading.  To avoid conflict, symbols for
-   which the dynamic linker must assume the non-PIC ABI semantics are
-   marked with the STO_MIPS_PLT flag.  */
-#define ELF_MACHINE_SYM_NO_MATCH(sym) \
-  ((sym)->st_shndx == SHN_UNDEF && !((sym)->st_other & STO_MIPS_PLT))
-
 #endif /* !dl_machine_h */
 
 #ifdef RESOLVE_MAP
diff --git a/sysdeps/mips/elf_machine_sym_no_match.h b/sysdeps/mips/elf_machine_sym_no_match.h
new file mode 100644
index 0000000000..f2be74caaf
--- /dev/null
+++ b/sysdeps/mips/elf_machine_sym_no_match.h
@@ -0,0 +1,43 @@
+/* MIPS-specific handling of undefined symbols.
+   Copyright (C) 2008-2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _ELF_MACHINE_SYM_NO_MATCH_H
+#define _ELF_MACHINE_SYM_NO_MATCH_H
+
+#include <link.h>
+#include <stdbool.h>
+
+/* The semantics of zero/non-zero values of undefined symbols differs
+   depending on whether the non-PIC ABI is in use.  Under the non-PIC
+   ABI, a non-zero value indicates that there is an address reference
+   to the symbol and thus it must always be resolved (except when
+   resolving a jump slot relocation) to the PLT entry whose address is
+   provided as the symbol's value; a zero value indicates that this
+   canonical-address behaviour is not required.  Yet under the classic
+   MIPS psABI, a zero value indicates that there is an address
+   reference to the function and the dynamic linker must resolve the
+   symbol immediately upon loading.  To avoid conflict, symbols for
+   which the dynamic linker must assume the non-PIC ABI semantics are
+   marked with the STO_MIPS_PLT flag.  */
+static inline bool
+elf_machine_sym_no_match (const ElfW(Sym) *sym)
+{
+  return sym->st_shndx == SHN_UNDEF && !(sym->st_other & STO_MIPS_PLT);
+}
+
+#endif /* _ELF_MACHINE_SYM_NO_MATCH_H */


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] Implement __libc_early_init
  2019-11-28 13:50 ` [PATCH 2/2] Implement __libc_early_init Florian Weimer
@ 2019-12-17 16:50   ` Florian Weimer
  0 siblings, 0 replies; 8+ messages in thread
From: Florian Weimer @ 2019-12-17 16:50 UTC (permalink / raw
  To: libc-alpha

New version, rebased on top of the recent dlopen fixes.

Thanks,
Florian

8<------------------------------------------------------------------8<
This function is defined in libc.so, and the dynamic loader calls
right after relocation has been finished, before any ELF constructors
or the preinit function is invoked.  It is also used in the static
build for initializing parts of the static libc.

To locate __libc_early_init, a direct symbol lookup function is used,
_dl_lookup_direct.  It does not search the entire symbol scope and
consults merely a single link map.  This function could also be used
to implement lookups in the vDSO (as an optimization).

A per-namespace variable (libc_map) is added for locating libc.so,
to avoid repeated traversals of the search scope.  It is similar to
GL(dl_initfirst).  An alternative would have been to thread a context
argument from _dl_open down to _dl_map_object_from_fd (where libc.so
is identified).  This could have avoided the global variable, but
the change would be larger as a result.  It would not have been
possible to use this to replace GL(dl_initfirst) because that global
variable is used to pass the function pointer past the stack switch
from dl_main to the main program.  Replacing that requires adding
a new argument to _dl_init, which in turn needs changes to the
architecture-specific libc.so startup code written in assembler.

__libc_early_init should not be used to replace _dl_var_init (as
it exists today on some architectures).  Instead, _dl_lookup_direct
should be used to look up a new variable symbol in libc.so, and
that should then be initialized from the dynamic loader, immediately
after the object has been loaded in _dl_map_object_from_fd (before
relocation is run).  This way, more IFUNC resolvers which depend on
these variables will work.

-----
 csu/init-first.c                    |   4 --
 csu/libc-start.c                    |   5 ++
 elf/Makefile                        |   5 +-
 elf/Versions                        |   1 +
 elf/dl-call-libc-early-init.c       |  41 +++++++++++++
 elf/dl-load.c                       |   9 +++
 elf/dl-lookup-direct.c              | 116 ++++++++++++++++++++++++++++++++++++
 elf/dl-open.c                       |  24 ++++++++
 elf/libc-early-init.h               |  35 +++++++++++
 elf/libc_early_init.c               |  27 +++++++++
 elf/rtld.c                          |   4 ++
 sysdeps/generic/ldsodefs.h          |  17 ++++++
 sysdeps/mach/hurd/i386/init-first.c |   4 --
 13 files changed, 282 insertions(+), 10 deletions(-)

diff --git a/csu/init-first.c b/csu/init-first.c
index e0f489ee6d..8caec305b7 100644
--- a/csu/init-first.c
+++ b/csu/init-first.c
@@ -16,7 +16,6 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
@@ -80,9 +79,6 @@ _init (int argc, char **argv, char **envp)
 
   __init_misc (argc, argv, envp);
 
-  /* Initialize ctype data.  */
-  __ctype_init ();
-
 #if defined SHARED && !defined NO_CTORS_DTORS_SECTIONS
   __libc_global_ctors ();
 #endif
diff --git a/csu/libc-start.c b/csu/libc-start.c
index e9e8e44fe5..b2d83a503a 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -22,6 +22,7 @@
 #include <ldsodefs.h>
 #include <exit-thread.h>
 #include <libc-internal.h>
+#include <elf/libc-early-init.h>
 
 #include <elf/dl-tunables.h>
 
@@ -238,6 +239,10 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
     __cxa_atexit ((void (*) (void *)) rtld_fini, NULL, NULL);
 
 #ifndef SHARED
+  /* Perform early initialization.  In the shared case, this function
+     is called from the dynamic loader as early as possible.  */
+  __libc_early_init ();
+
   /* Call the initializer of the libc.  This is only needed here if we
      are compiling for the static library in which case we haven't
      run the constructors in `_dl_start_user'.  */
diff --git a/elf/Makefile b/elf/Makefile
index 72a5aa88b1..5d4fbb639f 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -25,7 +25,7 @@ headers		= elf.h bits/elfclass.h link.h bits/link.h
 routines	= $(all-dl-routines) dl-support dl-iteratephdr \
 		  dl-addr dl-addr-obj enbl-secure dl-profstub \
 		  dl-origin dl-libc dl-sym dl-sysdep dl-error \
-		  dl-reloc-static-pie
+		  dl-reloc-static-pie libc_early_init
 
 # The core dynamic linking functions are in libc for the static and
 # profiled libraries.
@@ -33,7 +33,8 @@ dl-routines	= $(addprefix dl-,load lookup object reloc deps hwcaps \
 				  runtime init fini debug misc \
 				  version profile tls origin scope \
 				  execstack open close trampoline \
-				  exception sort-maps)
+				  exception sort-maps lookup-direct \
+				  call-libc-early-init)
 ifeq (yes,$(use-ldconfig))
 dl-routines += dl-cache
 endif
diff --git a/elf/Versions b/elf/Versions
index 3b09901f6c..f26d2817c3 100644
--- a/elf/Versions
+++ b/elf/Versions
@@ -26,6 +26,7 @@ libc {
     _dl_open_hook; _dl_open_hook2;
     _dl_sym; _dl_vsym;
     __libc_dlclose; __libc_dlopen_mode; __libc_dlsym; __libc_dlvsym;
+    __libc_early_init;
 
     # Internal error handling support.  Interposes the functions in ld.so.
     _dl_signal_exception; _dl_catch_exception;
diff --git a/elf/dl-call-libc-early-init.c b/elf/dl-call-libc-early-init.c
new file mode 100644
index 0000000000..6c3ac5bfe7
--- /dev/null
+++ b/elf/dl-call-libc-early-init.c
@@ -0,0 +1,41 @@
+/* Invoke the early initialization function in libc.so.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <assert.h>
+#include <ldsodefs.h>
+#include <libc-early-init.h>
+#include <link.h>
+#include <stddef.h>
+
+void
+_dl_call_libc_early_init (struct link_map *libc_map)
+{
+  /* There is nothing to do if we did not actually load libc.so.  */
+  if (libc_map == NULL)
+    return;
+
+  const ElfW(Sym) *sym
+    = _dl_lookup_direct (libc_map, "__libc_early_init",
+                         0x69682ac, /* dl_new_hash output.  */
+                         "GLIBC_PRIVATE",
+                         0x0963cf85); /* _dl_elf_hash output.  */
+  assert (sym != NULL);
+  __typeof (__libc_early_init) *early_init
+    = DL_SYMBOL_ADDRESS (libc_map, sym);
+  early_init ();
+}
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 6cdd11e6b0..dcaa5a8a53 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -30,6 +30,7 @@
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <gnu/lib-names.h>
 
 /* Type for the buffer we put the ELF header and hopefully the program
    header.  This buffer does not really have to be too large.  In most
@@ -1374,6 +1375,14 @@ cannot enable executable stack as shared object requires");
     add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
 			    + l->l_info[DT_SONAME]->d_un.d_val));
 
+  /* If we have newly loaded libc.so, update the namespace
+     description.  */
+  if (GL(dl_ns)[nsid].libc_map == NULL
+      && l->l_info[DT_SONAME] != NULL
+      && strcmp (((const char *) D_PTR (l, l_info[DT_STRTAB])
+		  + l->l_info[DT_SONAME]->d_un.d_val), LIBC_SO) == 0)
+    GL(dl_ns)[nsid].libc_map = l;
+
   /* _dl_close can only eventually undo the module ID assignment (via
      remove_slotinfo) if this function returns a pointer to a link
      map.  Therefore, delay this step until all possibilities for
diff --git a/elf/dl-lookup-direct.c b/elf/dl-lookup-direct.c
new file mode 100644
index 0000000000..190b826e1e
--- /dev/null
+++ b/elf/dl-lookup-direct.c
@@ -0,0 +1,116 @@
+/* Look up a symbol in a single specified object.
+   Copyright (C) 1995-2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <ldsodefs.h>
+#include <string.h>
+#include <elf_machine_sym_no_match.h>
+#include <dl-hash.h>
+
+/* This function corresponds to do_lookup_x in elf/dl-lookup.c.  The
+   variant here is simplified because it requires symbol
+   versioning. */
+static const ElfW(Sym) *
+check_match (const struct link_map *const map, const char *const undef_name,
+             const char *version, uint32_t version_hash,
+             const Elf_Symndx symidx)
+{
+  const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
+  const ElfW(Sym) *sym = &symtab[symidx];
+
+  unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
+  if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
+                         && sym->st_shndx != SHN_ABS
+                         && stt != STT_TLS)
+                        || elf_machine_sym_no_match (sym)))
+    return NULL;
+
+  /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
+     STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
+     code/data definitions.  */
+#define ALLOWED_STT \
+  ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
+   | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
+  if (__glibc_unlikely (((1 << stt) & ALLOWED_STT) == 0))
+    return NULL;
+
+  const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
+
+  if (strcmp (strtab + sym->st_name, undef_name) != 0)
+    /* Not the symbol we are looking for.  */
+    return NULL;
+
+  ElfW(Half) ndx = map->l_versyms[symidx] & 0x7fff;
+  if (map->l_versions[ndx].hash != version_hash
+      || strcmp (map->l_versions[ndx].name, version) != 0)
+    /* It's not the version we want.  */
+    return NULL;
+
+  return sym;
+}
+
+
+/* This function corresponds to do_lookup_x in elf/dl-lookup.c.  The
+   variant here is simplified because it does not search object
+   dependencies.  It is optimized for a successful lookup.  */
+const ElfW(Sym) *
+_dl_lookup_direct (struct link_map *map,
+                   const char *undef_name, uint32_t new_hash,
+                   const char *version, uint32_t version_hash)
+{
+  const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
+  if (__glibc_likely (bitmask != NULL))
+    {
+      Elf32_Word bucket = map->l_gnu_buckets[new_hash % map->l_nbuckets];
+      if (bucket != 0)
+        {
+          const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
+
+          do
+            if (((*hasharr ^ new_hash) >> 1) == 0)
+              {
+                Elf_Symndx symidx = ELF_MACHINE_HASH_SYMIDX (map, hasharr);
+                const ElfW(Sym) *sym = check_match (map, undef_name,
+                                                    version, version_hash,
+                                                    symidx);
+                if (sym != NULL)
+                  return sym;
+              }
+          while ((*hasharr++ & 1u) == 0);
+        }
+    }
+  else
+    {
+      /* Fallback code for lack of GNU_HASH support.  */
+      uint32_t old_hash = _dl_elf_hash (undef_name);
+
+      /* Use the old SysV-style hash table.  Search the appropriate
+         hash bucket in this object's symbol table for a definition
+         for the same symbol name.  */
+      for (Elf_Symndx symidx = map->l_buckets[old_hash % map->l_nbuckets];
+           symidx != STN_UNDEF;
+           symidx = map->l_chain[symidx])
+        {
+          const ElfW(Sym) *sym = check_match (map, undef_name,
+                                              version, version_hash, symidx);
+          if (sym != NULL)
+            return sym;
+        }
+    }
+
+  return NULL;
+}
diff --git a/elf/dl-open.c b/elf/dl-open.c
index 5a1c5b5326..fcb7aa327f 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -34,6 +34,7 @@
 #include <atomic.h>
 #include <libc-internal.h>
 #include <array_length.h>
+#include <libc-early-init.h>
 
 #include <dl-dst.h>
 #include <dl-prop.h>
@@ -52,6 +53,13 @@ struct dl_open_args
   /* Namespace ID.  */
   Lmid_t nsid;
 
+  /* Set to true if libc.so was already loaded into the namespace at
+     the time dl_open_worker was called.  This is used to determine
+     whether libc.so early initialization needs to before, and whether
+     to roll back the cached libc_map value in the namespace in case
+     of a dlopen failure.  */
+  bool libc_already_loaded;
+
   /* Original value of _ns_global_scope_pending_adds.  Set by
      dl_open_worker.  Only valid if nsid is a real namespace
      (non-negative).  */
@@ -500,6 +508,11 @@ dl_open_worker (void *a)
 	args->nsid = call_map->l_ns;
     }
 
+  /* The namespace ID is now known.  Keep track of whether libc.so was
+     already loaded, to determine whether it is necessary to call the
+     early initialization routine (or clear libc_map on error).  */
+  args->libc_already_loaded = GL(dl_ns)[args->nsid].libc_map != NULL;
+
   /* Retain the old value, so that it can be restored.  */
   args->original_global_scope_pending_adds
     = GL (dl_ns)[args->nsid]._ns_global_scope_pending_adds;
@@ -735,6 +748,11 @@ dl_open_worker (void *a)
   if (relocation_in_progress)
     LIBC_PROBE (reloc_complete, 3, args->nsid, r, new);
 
+  /* If libc.so was not there before, attempt to call its early
+     initialization routine.  */
+  if (!args->libc_already_loaded)
+    _dl_call_libc_early_init (GL(dl_ns)[args->nsid].libc_map);
+
 #ifndef SHARED
   DL_STATIC_INIT (new);
 #endif
@@ -829,6 +847,7 @@ no more namespaces available for dlmopen()"));
   args.caller_dlopen = caller_dlopen;
   args.map = NULL;
   args.nsid = nsid;
+  args.libc_already_loaded = true; /* No reset below with early failure.  */
   args.argc = argc;
   args.argv = argv;
   args.env = env;
@@ -857,6 +876,11 @@ no more namespaces available for dlmopen()"));
   /* See if an error occurred during loading.  */
   if (__glibc_unlikely (exception.errstring != NULL))
     {
+      /* Avoid keeping around a dangling reference to the libc.so link
+	 map in case it has been cached in libc_map.  */
+      if (!args.libc_already_loaded)
+	GL(dl_ns)[nsid].libc_map = NULL;
+
       /* Remove the object from memory.  It may be in an inconsistent
 	 state if relocation failed, for example.  */
       if (args.map)
diff --git a/elf/libc-early-init.h b/elf/libc-early-init.h
new file mode 100644
index 0000000000..02b855754e
--- /dev/null
+++ b/elf/libc-early-init.h
@@ -0,0 +1,35 @@
+/* Early initialization of libc.so.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _LIBC_EARLY_INIT_H
+#define _LIBC_EARLY_INIT_H
+
+struct link_map;
+
+/* If LIBC_MAP is not NULL, look up the __libc_early_init symbol in it
+   and call this function.  */
+void _dl_call_libc_early_init (struct link_map *libc_map) attribute_hidden;
+
+/* In the shared case, this function is defined in libc.so and invoked
+   from ld.so (or on the fist static dlopen) after complete relocation
+   of a new loaded libc.so, but before user-defined ELF constructors
+   run.  In the static case, this function is called directly from the
+   startup code.  */
+void __libc_early_init (void);
+
+#endif /* _LIBC_EARLY_INIT_H */
diff --git a/elf/libc_early_init.c b/elf/libc_early_init.c
new file mode 100644
index 0000000000..1ac66d895d
--- /dev/null
+++ b/elf/libc_early_init.c
@@ -0,0 +1,27 @@
+/* Early initialization of libc.so, libc.so side.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <ctype.h>
+#include <libc-early-init.h>
+
+void
+__libc_early_init (void)
+{
+  /* Initialize ctype data.  */
+  __ctype_init ();
+}
diff --git a/elf/rtld.c b/elf/rtld.c
index dd8fc5e6c6..8c27c62aa0 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -43,6 +43,7 @@
 #include <stap-probe.h>
 #include <stackinfo.h>
 #include <not-cancel.h>
+#include <libc-early-init.h>
 
 #include <assert.h>
 
@@ -2315,6 +2316,9 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
       rtld_timer_accum (&relocate_time, start);
     }
 
+  /* Relocation is complete.  Perform early libc initialization.  */
+  _dl_call_libc_early_init (GL(dl_ns)[LM_ID_BASE].libc_map);
+
   /* Do any necessary cleanups for the startup OS interface code.
      We do these now so that no calls are made after rtld re-relocation
      which might be resolved to different functions than we expect.
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index fc25a81e1c..f767baea83 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -336,6 +336,10 @@ struct rtld_global
        recursive dlopen calls from ELF constructors.  */
     unsigned int _ns_global_scope_pending_adds;
 
+    /* Once libc.so has been loaded into the namespace, this points to
+       its link map.  */
+    struct link_map *libc_map;
+
     /* Search table for unique objects.  */
     struct unique_sym_table
     {
@@ -940,6 +944,19 @@ extern lookup_t _dl_lookup_symbol_x (const char *undef,
      attribute_hidden;
 
 
+/* Restricted version of _dl_lookup_symbol_x.  Searches MAP (and only
+   MAP) for the symbol UNDEF_NAME, with GNU hash NEW_HASH (computed
+   with dl_new_hash), symbol version VERSION, and symbol version hash
+   VERSION_HASH (computed with _dl_elf_hash).  Returns a pointer to
+   the symbol table entry in MAP on success, or NULL on failure.  MAP
+   must have symbol versioning information, or otherwise the result is
+   undefined.  */
+const ElfW(Sym) *_dl_lookup_direct (struct link_map *map,
+				    const char *undef_name,
+				    uint32_t new_hash,
+				    const char *version,
+				    uint32_t version_hash) attribute_hidden;
+
 /* Add the new link_map NEW to the end of the namespace list.  */
 extern void _dl_add_to_namespace_list (struct link_map *new, Lmid_t nsid)
      attribute_hidden;
diff --git a/sysdeps/mach/hurd/i386/init-first.c b/sysdeps/mach/hurd/i386/init-first.c
index 5c21ebb958..6c18e83195 100644
--- a/sysdeps/mach/hurd/i386/init-first.c
+++ b/sysdeps/mach/hurd/i386/init-first.c
@@ -17,7 +17,6 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
-#include <ctype.h>
 #include <hurd.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -85,9 +84,6 @@ posixland_init (int argc, char **argv, char **envp)
 #endif
   __init_misc (argc, argv, envp);
 
-  /* Initialize ctype data.  */
-  __ctype_init ();
-
 #if defined SHARED && !defined NO_CTORS_DTORS_SECTIONS
   __libc_global_ctors ();
 #endif


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] Introduce <elf_machine_sym_no_match.h>
  2019-12-17 16:42   ` Florian Weimer
@ 2019-12-17 16:54     ` Andreas Schwab
  2019-12-17 17:02       ` Florian Weimer
  2020-03-26 16:39     ` H.J. Lu via Libc-alpha
  1 sibling, 1 reply; 8+ messages in thread
From: Andreas Schwab @ 2019-12-17 16:54 UTC (permalink / raw
  To: Florian Weimer; +Cc: libc-alpha

On Dez 17 2019, Florian Weimer wrote:

> The old scheme uses the ELF_MACHINE_SYM_NO_MATCH macro, with an
> inline function, within its own header, with a sysdeps override for
> MIPS.

This sentence is missing something.

Andreas.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] Introduce <elf_machine_sym_no_match.h>
  2019-12-17 16:54     ` Andreas Schwab
@ 2019-12-17 17:02       ` Florian Weimer
  0 siblings, 0 replies; 8+ messages in thread
From: Florian Weimer @ 2019-12-17 17:02 UTC (permalink / raw
  To: Andreas Schwab; +Cc: libc-alpha

* Andreas Schwab:

> On Dez 17 2019, Florian Weimer wrote:
>
>> The old scheme uses the ELF_MACHINE_SYM_NO_MATCH macro, with an
>> inline function, within its own header, with a sysdeps override for
>> MIPS.
>
> This sentence is missing something.

Oh, good point.  New version below.

Thanks,
Florian

8<------------------------------------------------------------------8<
MIPS needs to ignore certain existing symbols during symbol lookup.
The old scheme uses the ELF_MACHINE_SYM_NO_MATCH macro.  This commit
replaces it with an inline function, elf_machine_sym_no_match, within
its own header.  It ads a sysdeps override for MIPS to implement the
desired functionality there.

This allows re-use of the function from another file (without having
to include <dl-machine.h> or providing the default definition for
ELF_MACHINE_SYM_NO_MATCH).

-----
 elf/dl-lookup.c                         | 10 ++------
 elf/elf_machine_sym_no_match.h          | 34 ++++++++++++++++++++++++++
 sysdeps/mips/dl-machine.h               | 15 ------------
 sysdeps/mips/elf_machine_sym_no_match.h | 43 +++++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+), 23 deletions(-)

diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index 759b45a2c9..55e521988e 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -28,18 +28,12 @@
 #include <libc-lock.h>
 #include <tls.h>
 #include <atomic.h>
+#include <elf_machine_sym_no_match.h>
 
 #include <assert.h>
 
-/* Return nonzero if check_match should consider SYM to fail to match a
-   symbol reference for some machine-specific reason.  */
-#ifndef ELF_MACHINE_SYM_NO_MATCH
-# define ELF_MACHINE_SYM_NO_MATCH(sym) 0
-#endif
-
 #define VERSTAG(tag)	(DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
 
-
 struct sym_val
   {
     const ElfW(Sym) *s;
@@ -78,7 +72,7 @@ check_match (const char *const undef_name,
   if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
 			 && sym->st_shndx != SHN_ABS
 			 && stt != STT_TLS)
-			|| ELF_MACHINE_SYM_NO_MATCH (sym)
+			|| elf_machine_sym_no_match (sym)
 			|| (type_class & (sym->st_shndx == SHN_UNDEF))))
     return NULL;
 
diff --git a/elf/elf_machine_sym_no_match.h b/elf/elf_machine_sym_no_match.h
new file mode 100644
index 0000000000..6e299e5ee8
--- /dev/null
+++ b/elf/elf_machine_sym_no_match.h
@@ -0,0 +1,34 @@
+/* Function to ignore certain symbol matches for machine-specific reasons.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _ELF_MACHINE_SYM_NO_MATCH_H
+#define _ELF_MACHINE_SYM_NO_MATCH_H
+
+#include <link.h>
+#include <stdbool.h>
+
+/* This can be customized to ignore certain symbols during lookup in
+   case there are machine-specific rules to disregard some
+   symbols.  */
+static inline bool
+elf_machine_sym_no_match (const ElfW(Sym) *sym)
+{
+  return false;
+}
+
+#endif /* _ELF_MACHINE_SYM_NO_MATCH_H */
diff --git a/sysdeps/mips/dl-machine.h b/sysdeps/mips/dl-machine.h
index c036c7a905..b238ab6476 100644
--- a/sysdeps/mips/dl-machine.h
+++ b/sysdeps/mips/dl-machine.h
@@ -467,21 +467,6 @@ elf_machine_plt_value (struct link_map *map, const ElfW(Rel) *reloc,
   return value;
 }
 
-/* The semantics of zero/non-zero values of undefined symbols differs
-   depending on whether the non-PIC ABI is in use.  Under the non-PIC
-   ABI, a non-zero value indicates that there is an address reference
-   to the symbol and thus it must always be resolved (except when
-   resolving a jump slot relocation) to the PLT entry whose address is
-   provided as the symbol's value; a zero value indicates that this
-   canonical-address behaviour is not required.  Yet under the classic
-   MIPS psABI, a zero value indicates that there is an address
-   reference to the function and the dynamic linker must resolve the
-   symbol immediately upon loading.  To avoid conflict, symbols for
-   which the dynamic linker must assume the non-PIC ABI semantics are
-   marked with the STO_MIPS_PLT flag.  */
-#define ELF_MACHINE_SYM_NO_MATCH(sym) \
-  ((sym)->st_shndx == SHN_UNDEF && !((sym)->st_other & STO_MIPS_PLT))
-
 #endif /* !dl_machine_h */
 
 #ifdef RESOLVE_MAP
diff --git a/sysdeps/mips/elf_machine_sym_no_match.h b/sysdeps/mips/elf_machine_sym_no_match.h
new file mode 100644
index 0000000000..f2be74caaf
--- /dev/null
+++ b/sysdeps/mips/elf_machine_sym_no_match.h
@@ -0,0 +1,43 @@
+/* MIPS-specific handling of undefined symbols.
+   Copyright (C) 2008-2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _ELF_MACHINE_SYM_NO_MATCH_H
+#define _ELF_MACHINE_SYM_NO_MATCH_H
+
+#include <link.h>
+#include <stdbool.h>
+
+/* The semantics of zero/non-zero values of undefined symbols differs
+   depending on whether the non-PIC ABI is in use.  Under the non-PIC
+   ABI, a non-zero value indicates that there is an address reference
+   to the symbol and thus it must always be resolved (except when
+   resolving a jump slot relocation) to the PLT entry whose address is
+   provided as the symbol's value; a zero value indicates that this
+   canonical-address behaviour is not required.  Yet under the classic
+   MIPS psABI, a zero value indicates that there is an address
+   reference to the function and the dynamic linker must resolve the
+   symbol immediately upon loading.  To avoid conflict, symbols for
+   which the dynamic linker must assume the non-PIC ABI semantics are
+   marked with the STO_MIPS_PLT flag.  */
+static inline bool
+elf_machine_sym_no_match (const ElfW(Sym) *sym)
+{
+  return sym->st_shndx == SHN_UNDEF && !(sym->st_other & STO_MIPS_PLT);
+}
+
+#endif /* _ELF_MACHINE_SYM_NO_MATCH_H */


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] Introduce <elf_machine_sym_no_match.h>
  2019-12-17 16:42   ` Florian Weimer
  2019-12-17 16:54     ` Andreas Schwab
@ 2020-03-26 16:39     ` H.J. Lu via Libc-alpha
  1 sibling, 0 replies; 8+ messages in thread
From: H.J. Lu via Libc-alpha @ 2020-03-26 16:39 UTC (permalink / raw
  To: Florian Weimer; +Cc: GNU C Library

On Tue, Dec 17, 2019 at 8:42 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> Rebased, and dropped the false ChangeLog update.
>
> Thanks,
> Florian
>
> 8<------------------------------------------------------------------8<
> MIPS needs to ignore certain existing symbols during symbol lookup.
> The old scheme uses the ELF_MACHINE_SYM_NO_MATCH macro, with an
> inline function, within its own header, with a sysdeps override for
> MIPS.  This allows re-use of the function from another file (without
> having to include <dl-machine.h> or providing the default definition
> for ELF_MACHINE_SYM_NO_MATCH).
>
> Built with build-many-glibcs.py, with manual verification that
> sysdeps/mips/elf_machine_sym_no_match.h is picked up on MIPS.  Tested
> on aarch64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
> s390x-linux-gnu, x86_64-linux-gnu.
>
> -----
>  elf/dl-lookup.c                         | 10 ++------
>  elf/elf_machine_sym_no_match.h          | 34 ++++++++++++++++++++++++++
>  sysdeps/mips/dl-machine.h               | 15 ------------
>  sysdeps/mips/elf_machine_sym_no_match.h | 43 +++++++++++++++++++++++++++++++++
>  4 files changed, 79 insertions(+), 23 deletions(-)
>
> diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
> index 759b45a2c9..55e521988e 100644
> --- a/elf/dl-lookup.c
> +++ b/elf/dl-lookup.c
> @@ -28,18 +28,12 @@
>  #include <libc-lock.h>
>  #include <tls.h>
>  #include <atomic.h>
> +#include <elf_machine_sym_no_match.h>
>
>  #include <assert.h>
>
> -/* Return nonzero if check_match should consider SYM to fail to match a
> -   symbol reference for some machine-specific reason.  */
> -#ifndef ELF_MACHINE_SYM_NO_MATCH
> -# define ELF_MACHINE_SYM_NO_MATCH(sym) 0
> -#endif
> -
>  #define VERSTAG(tag)   (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
>
> -
>  struct sym_val
>    {
>      const ElfW(Sym) *s;
> @@ -78,7 +72,7 @@ check_match (const char *const undef_name,
>    if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
>                          && sym->st_shndx != SHN_ABS
>                          && stt != STT_TLS)
> -                       || ELF_MACHINE_SYM_NO_MATCH (sym)
> +                       || elf_machine_sym_no_match (sym)
>                         || (type_class & (sym->st_shndx == SHN_UNDEF))))
>      return NULL;
>
> diff --git a/elf/elf_machine_sym_no_match.h b/elf/elf_machine_sym_no_match.h
> new file mode 100644
> index 0000000000..6e299e5ee8
> --- /dev/null
> +++ b/elf/elf_machine_sym_no_match.h

This should be sysdeps/generic/elf_machine_sym_no_match.h

H.J.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-03-26 16:39 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-11-28 13:49 [PATCH 0/2] Introduce __libc_early_init (for rseq support) Florian Weimer
2019-11-28 13:50 ` [PATCH 1/2] Introduce <elf_machine_sym_no_match.h> Florian Weimer
2019-12-17 16:42   ` Florian Weimer
2019-12-17 16:54     ` Andreas Schwab
2019-12-17 17:02       ` Florian Weimer
2020-03-26 16:39     ` H.J. Lu via Libc-alpha
2019-11-28 13:50 ` [PATCH 2/2] Implement __libc_early_init Florian Weimer
2019-12-17 16:50   ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).