[RFC,v12,2/8] Abstract loaded-DSO search code into a helper function

Message ID 20210708163255.812-3-vivek@collabora.com
State Superseded
Delegated to: Adhemerval Zanella Netto
Headers
Series Implementation of RTLD_SHARED for dlmopen |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Vivek Dasmohapatra July 8, 2021, 4:32 p.m. UTC
  ---
 elf/dl-load.c              | 38 ++++++++++++++++++++++++++++++++++++++
 sysdeps/generic/ldsodefs.h |  4 ++++
 2 files changed, 42 insertions(+)
  

Comments

Adhemerval Zanella Aug. 9, 2021, 1:04 p.m. UTC | #1
LGTM, but I think it should be merge it with next patch.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

On 08/07/2021 13:32, Vivek Das Mohapatra via Libc-alpha wrote:
> ---
>  elf/dl-load.c              | 38 ++++++++++++++++++++++++++++++++++++++
>  sysdeps/generic/ldsodefs.h |  4 ++++
>  2 files changed, 42 insertions(+)
> 
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index a08df001af..26680b7f68 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -2027,6 +2027,44 @@ open_path (const char *name, size_t namelen, int mode,
>    return -1;
>  }
>  
> +/* Search for a shared object in a given namespace.  */
> +struct link_map *
> +_dl_find_dso (const char *name, Lmid_t nsid)
> +{
> +  struct link_map *l;
> +
> +  for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
> +    {
> +      /* If the requested name matches the soname of a loaded object,
> +	 use that object.  Elide this check for names that have not
> +	 yet been opened.  */
> +      if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0))
> +	continue;
> +      if (!_dl_name_match_p (name, l))
> +	{
> +	  const char *soname;
> +
> +	  if (__glibc_likely (l->l_soname_added)
> +	      || l->l_info[DT_SONAME] == NULL)
> +	    continue;
> +
> +	  soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
> +		    + l->l_info[DT_SONAME]->d_un.d_val);
> +	  if (strcmp (name, soname) != 0)
> +	    continue;
> +
> +	  /* We have a match on a new name -- cache it.  */
> +	  add_name_to_object (l, soname);
> +	  l->l_soname_added = 1;
> +	}
> +
> +      /* We have a match.  */
> +      return l;
> +    }
> +
> +  return NULL;
> +}
> +
>  /* Map in the shared object file NAME.  */
>  
>  struct link_map *
> diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
> index 176394de4d..44e7097712 100644
> --- a/sysdeps/generic/ldsodefs.h
> +++ b/sysdeps/generic/ldsodefs.h
> @@ -1287,6 +1287,10 @@ extern void _dl_show_scope (struct link_map *new, int from)
>  extern struct link_map *_dl_find_dso_for_object (const ElfW(Addr) addr);
>  rtld_hidden_proto (_dl_find_dso_for_object)
>  
> +extern struct link_map *_dl_find_dso (const char *name, Lmid_t nsid);
> +rtld_hidden_proto (_dl_find_dso)
> +
> +
>  /* Initialization which is normally done by the dynamic linker.  */
>  extern void _dl_non_dynamic_init (void)
>       attribute_hidden;
>
  

Patch

diff --git a/elf/dl-load.c b/elf/dl-load.c
index a08df001af..26680b7f68 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -2027,6 +2027,44 @@  open_path (const char *name, size_t namelen, int mode,
   return -1;
 }
 
+/* Search for a shared object in a given namespace.  */
+struct link_map *
+_dl_find_dso (const char *name, Lmid_t nsid)
+{
+  struct link_map *l;
+
+  for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
+    {
+      /* If the requested name matches the soname of a loaded object,
+	 use that object.  Elide this check for names that have not
+	 yet been opened.  */
+      if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0))
+	continue;
+      if (!_dl_name_match_p (name, l))
+	{
+	  const char *soname;
+
+	  if (__glibc_likely (l->l_soname_added)
+	      || l->l_info[DT_SONAME] == NULL)
+	    continue;
+
+	  soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
+		    + l->l_info[DT_SONAME]->d_un.d_val);
+	  if (strcmp (name, soname) != 0)
+	    continue;
+
+	  /* We have a match on a new name -- cache it.  */
+	  add_name_to_object (l, soname);
+	  l->l_soname_added = 1;
+	}
+
+      /* We have a match.  */
+      return l;
+    }
+
+  return NULL;
+}
+
 /* Map in the shared object file NAME.  */
 
 struct link_map *
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 176394de4d..44e7097712 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -1287,6 +1287,10 @@  extern void _dl_show_scope (struct link_map *new, int from)
 extern struct link_map *_dl_find_dso_for_object (const ElfW(Addr) addr);
 rtld_hidden_proto (_dl_find_dso_for_object)
 
+extern struct link_map *_dl_find_dso (const char *name, Lmid_t nsid);
+rtld_hidden_proto (_dl_find_dso)
+
+
 /* Initialization which is normally done by the dynamic linker.  */
 extern void _dl_non_dynamic_init (void)
      attribute_hidden;