unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency.
@ 2022-11-22  2:30 Wang Bing via Libc-alpha
  2022-11-22  2:36 ` H.J. Lu via Libc-alpha
  0 siblings, 1 reply; 5+ messages in thread
From: Wang Bing via Libc-alpha @ 2022-11-22  2:30 UTC (permalink / raw)
  To: libc-alpha, szabolcs.nagy; +Cc: nixiaoming, zhongjubin, yanhuijun

Signed-off-by: Wang Bing <wangbing6@huawei.com>
---
 dlfcn/dlfcn.h              |  4 ++++
 elf/Makefile               |  1 +
 elf/dl-sym.c               | 13 +++++++++---
 elf/tst-dlsym-rtld-probe.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+), 3 deletions(-)
 create mode 100644 elf/tst-dlsym-rtld-probe.c

diff --git a/dlfcn/dlfcn.h b/dlfcn/dlfcn.h
index 6f7cad8682..ab709883a6 100644
--- a/dlfcn/dlfcn.h
+++ b/dlfcn/dlfcn.h
@@ -49,6 +49,10 @@ typedef long int Lmid_t;
    is returned.  */
 #define RTLD_DEFAULT	((void *) 0)
 
+/* If only find sym in the global scope, but will not use it, do not
+   set sym dependency. */
+# define RTLD_PROBE    ((void *) -2l)
+
 __BEGIN_DECLS
 
 /* Open the shared object FILE and map it in; return a handle that can be
diff --git a/elf/Makefile b/elf/Makefile
index eca7b28ab5..f9fc9fbebb 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -472,6 +472,7 @@ tests += \
   unload7 \
   unload8 \
   valgrind-test \
+  tst-dlsym-rtld-probe \
   # tests
 tests-cxx = \
   tst-dlopen-nodelete-reloc \
diff --git a/elf/dl-sym.c b/elf/dl-sym.c
index b1cf42f36d..bc95b12a19 100644
--- a/elf/dl-sym.c
+++ b/elf/dl-sym.c
@@ -92,10 +92,17 @@ do_sym (void *handle, const char *name, void *who,
   /* Link map of the caller if needed.  */
   struct link_map *match = NULL;
 
-  if (handle == RTLD_DEFAULT)
+  int def_flags;
+
+  if (handle == RTLD_DEFAULT || handle == RTLD_PROBE)
     {
       match = _dl_sym_find_caller_link_map (caller);
 
+      def_flags = flags
+      if (def_flags == RTLD_DEFAULT) {
+          def_flags != DL_LOOKUP_ADD_DEPENDENCY;
+      }
+
       /* Search the global scope.  We have the simple case where
 	 we look up in the scope of an object which was part of
 	 the initial binary.  And then the more complex part
@@ -104,7 +111,7 @@ do_sym (void *handle, const char *name, void *who,
       if (RTLD_SINGLE_THREAD_P)
 	result = GLRO(dl_lookup_symbol_x) (name, match, &ref,
 					   match->l_scope, vers, 0,
-					   flags | DL_LOOKUP_ADD_DEPENDENCY,
+					   def_flags,
 					   NULL);
       else
 	{
@@ -113,7 +120,7 @@ do_sym (void *handle, const char *name, void *who,
 	  args.map = match;
 	  args.vers = vers;
 	  args.flags
-	    = flags | DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK;
+	    = def_flags | DL_LOOKUP_GSCOPE_LOCK;
 	  args.refp = &ref;
 
 	  THREAD_GSCOPE_SET_FLAG ();
diff --git a/elf/tst-dlsym-rtld-probe.c b/elf/tst-dlsym-rtld-probe.c
new file mode 100644
index 0000000000..c72ceaa182
--- /dev/null
+++ b/elf/tst-dlsym-rtld-probe.c
@@ -0,0 +1,50 @@
+/* Test RTLD_PROBE for dlsym.
+   Copyright (C) 2022-2022 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 <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/xdlfcn.h>
+
+static int
+do_test (void)
+{
+  int *iptr;
+  int ret;
+  void *handle;
+
+  handle = dlopen (LIBM_SO, RTLD_LAZY);
+  TEST_VERIFY (handle == NULL);
+  iptr = (int *)dlsym (RTLD_PROBE, "finite");	// get sym but not call --detect if symbol exist
+  ret = dlclose (handle);
+  TEST_VERIFY (ret != 0);
+  ret = 0;
+
+  handle = dlopen (LIBM_SO, RTLD_LAZY);
+  TEST_VERIFY (handle == NULL);
+  iptr = (int *)dlsym (RTLD_DEFAULT, "finite"); // get sym and keep
+  ret = dlcose (handle);
+  TEST_VERIFY (ret == 0);
+  return 0;
+}
+
+
+#include <support/test-driver.c>
-- 
2.12.3


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

* Re: [PATCH] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency.
  2022-11-22  2:30 [PATCH] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency Wang Bing via Libc-alpha
@ 2022-11-22  2:36 ` H.J. Lu via Libc-alpha
  2022-11-22 16:35   ` H.J. Lu via Libc-alpha
  0 siblings, 1 reply; 5+ messages in thread
From: H.J. Lu via Libc-alpha @ 2022-11-22  2:36 UTC (permalink / raw)
  To: Wang Bing; +Cc: libc-alpha, szabolcs.nagy, nixiaoming, zhongjubin, yanhuijun

On Mon, Nov 21, 2022 at 6:31 PM Wang Bing via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> Signed-off-by: Wang Bing <wangbing6@huawei.com>
> ---
>  dlfcn/dlfcn.h              |  4 ++++
>  elf/Makefile               |  1 +
>  elf/dl-sym.c               | 13 +++++++++---
>  elf/tst-dlsym-rtld-probe.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 65 insertions(+), 3 deletions(-)
>  create mode 100644 elf/tst-dlsym-rtld-probe.c
>
> diff --git a/dlfcn/dlfcn.h b/dlfcn/dlfcn.h
> index 6f7cad8682..ab709883a6 100644
> --- a/dlfcn/dlfcn.h
> +++ b/dlfcn/dlfcn.h
> @@ -49,6 +49,10 @@ typedef long int Lmid_t;
>     is returned.  */
>  #define RTLD_DEFAULT   ((void *) 0)
>
> +/* If only find sym in the global scope, but will not use it, do not
> +   set sym dependency. */
> +# define RTLD_PROBE    ((void *) -2l)
> +
>  __BEGIN_DECLS
>
>  /* Open the shared object FILE and map it in; return a handle that can be
> diff --git a/elf/Makefile b/elf/Makefile
> index eca7b28ab5..f9fc9fbebb 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -472,6 +472,7 @@ tests += \
>    unload7 \
>    unload8 \
>    valgrind-test \
> +  tst-dlsym-rtld-probe \
>    # tests
>  tests-cxx = \
>    tst-dlopen-nodelete-reloc \
> diff --git a/elf/dl-sym.c b/elf/dl-sym.c
> index b1cf42f36d..bc95b12a19 100644
> --- a/elf/dl-sym.c
> +++ b/elf/dl-sym.c
> @@ -92,10 +92,17 @@ do_sym (void *handle, const char *name, void *who,
>    /* Link map of the caller if needed.  */
>    struct link_map *match = NULL;
>
> -  if (handle == RTLD_DEFAULT)
> +  int def_flags;
> +
> +  if (handle == RTLD_DEFAULT || handle == RTLD_PROBE)
>      {
>        match = _dl_sym_find_caller_link_map (caller);
>
> +      def_flags = flags
> +      if (def_flags == RTLD_DEFAULT) {
> +          def_flags != DL_LOOKUP_ADD_DEPENDENCY;

What does it do?

> +      }
> +
>        /* Search the global scope.  We have the simple case where
>          we look up in the scope of an object which was part of
>          the initial binary.  And then the more complex part
> @@ -104,7 +111,7 @@ do_sym (void *handle, const char *name, void *who,
>        if (RTLD_SINGLE_THREAD_P)
>         result = GLRO(dl_lookup_symbol_x) (name, match, &ref,
>                                            match->l_scope, vers, 0,
> -                                          flags | DL_LOOKUP_ADD_DEPENDENCY,
> +                                          def_flags,
>                                            NULL);
>        else
>         {
> @@ -113,7 +120,7 @@ do_sym (void *handle, const char *name, void *who,
>           args.map = match;
>           args.vers = vers;
>           args.flags
> -           = flags | DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK;
> +           = def_flags | DL_LOOKUP_GSCOPE_LOCK;
>           args.refp = &ref;
>
>           THREAD_GSCOPE_SET_FLAG ();
> diff --git a/elf/tst-dlsym-rtld-probe.c b/elf/tst-dlsym-rtld-probe.c
> new file mode 100644
> index 0000000000..c72ceaa182
> --- /dev/null
> +++ b/elf/tst-dlsym-rtld-probe.c
> @@ -0,0 +1,50 @@
> +/* Test RTLD_PROBE for dlsym.
> +   Copyright (C) 2022-2022 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 <dlfcn.h>
> +#include <gnu/lib-names.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <support/check.h>
> +#include <support/xdlfcn.h>
> +
> +static int
> +do_test (void)
> +{
> +  int *iptr;
> +  int ret;
> +  void *handle;
> +
> +  handle = dlopen (LIBM_SO, RTLD_LAZY);
> +  TEST_VERIFY (handle == NULL);
> +  iptr = (int *)dlsym (RTLD_PROBE, "finite");  // get sym but not call --detect if symbol exist
> +  ret = dlclose (handle);
> +  TEST_VERIFY (ret != 0);
> +  ret = 0;
> +
> +  handle = dlopen (LIBM_SO, RTLD_LAZY);
> +  TEST_VERIFY (handle == NULL);
> +  iptr = (int *)dlsym (RTLD_DEFAULT, "finite"); // get sym and keep
> +  ret = dlcose (handle);
> +  TEST_VERIFY (ret == 0);
> +  return 0;
> +}
> +
> +
> +#include <support/test-driver.c>
> --
> 2.12.3
>


-- 
H.J.

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

* Re: [PATCH] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency.
  2022-11-22  2:36 ` H.J. Lu via Libc-alpha
@ 2022-11-22 16:35   ` H.J. Lu via Libc-alpha
  2022-11-23  2:21     ` [PATCH v2] " Wang Bing via Libc-alpha
  0 siblings, 1 reply; 5+ messages in thread
From: H.J. Lu via Libc-alpha @ 2022-11-22 16:35 UTC (permalink / raw)
  To: Wang Bing; +Cc: libc-alpha, szabolcs.nagy, nixiaoming, zhongjubin, yanhuijun

On Mon, Nov 21, 2022 at 6:36 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Mon, Nov 21, 2022 at 6:31 PM Wang Bing via Libc-alpha
> <libc-alpha@sourceware.org> wrote:
> >
> > Signed-off-by: Wang Bing <wangbing6@huawei.com>
> > ---
> >  dlfcn/dlfcn.h              |  4 ++++
> >  elf/Makefile               |  1 +
> >  elf/dl-sym.c               | 13 +++++++++---
> >  elf/tst-dlsym-rtld-probe.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 65 insertions(+), 3 deletions(-)
> >  create mode 100644 elf/tst-dlsym-rtld-probe.c
> >
> > diff --git a/dlfcn/dlfcn.h b/dlfcn/dlfcn.h
> > index 6f7cad8682..ab709883a6 100644
> > --- a/dlfcn/dlfcn.h
> > +++ b/dlfcn/dlfcn.h
> > @@ -49,6 +49,10 @@ typedef long int Lmid_t;
> >     is returned.  */
> >  #define RTLD_DEFAULT   ((void *) 0)
> >
> > +/* If only find sym in the global scope, but will not use it, do not
> > +   set sym dependency. */
> > +# define RTLD_PROBE    ((void *) -2l)
> > +
> >  __BEGIN_DECLS
> >
> >  /* Open the shared object FILE and map it in; return a handle that can be
> > diff --git a/elf/Makefile b/elf/Makefile
> > index eca7b28ab5..f9fc9fbebb 100644
> > --- a/elf/Makefile
> > +++ b/elf/Makefile
> > @@ -472,6 +472,7 @@ tests += \
> >    unload7 \
> >    unload8 \
> >    valgrind-test \
> > +  tst-dlsym-rtld-probe \
> >    # tests
> >  tests-cxx = \
> >    tst-dlopen-nodelete-reloc \
> > diff --git a/elf/dl-sym.c b/elf/dl-sym.c
> > index b1cf42f36d..bc95b12a19 100644
> > --- a/elf/dl-sym.c
> > +++ b/elf/dl-sym.c
> > @@ -92,10 +92,17 @@ do_sym (void *handle, const char *name, void *who,
> >    /* Link map of the caller if needed.  */
> >    struct link_map *match = NULL;
> >
> > -  if (handle == RTLD_DEFAULT)
> > +  int def_flags;
> > +
> > +  if (handle == RTLD_DEFAULT || handle == RTLD_PROBE)
> >      {
> >        match = _dl_sym_find_caller_link_map (caller);
> >
> > +      def_flags = flags
> > +      if (def_flags == RTLD_DEFAULT) {
> > +          def_flags != DL_LOOKUP_ADD_DEPENDENCY;
>
> What does it do?

Did you mean

def_flags |= DL_LOOKUP_ADD_DEPENDENCY;


> > +      }
> > +
> >        /* Search the global scope.  We have the simple case where
> >          we look up in the scope of an object which was part of
> >          the initial binary.  And then the more complex part
> > @@ -104,7 +111,7 @@ do_sym (void *handle, const char *name, void *who,
> >        if (RTLD_SINGLE_THREAD_P)
> >         result = GLRO(dl_lookup_symbol_x) (name, match, &ref,
> >                                            match->l_scope, vers, 0,
> > -                                          flags | DL_LOOKUP_ADD_DEPENDENCY,
> > +                                          def_flags,
> >                                            NULL);
> >        else
> >         {
> > @@ -113,7 +120,7 @@ do_sym (void *handle, const char *name, void *who,
> >           args.map = match;
> >           args.vers = vers;
> >           args.flags
> > -           = flags | DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK;
> > +           = def_flags | DL_LOOKUP_GSCOPE_LOCK;
> >           args.refp = &ref;
> >
> >           THREAD_GSCOPE_SET_FLAG ();
> > diff --git a/elf/tst-dlsym-rtld-probe.c b/elf/tst-dlsym-rtld-probe.c
> > new file mode 100644
> > index 0000000000..c72ceaa182
> > --- /dev/null
> > +++ b/elf/tst-dlsym-rtld-probe.c
> > @@ -0,0 +1,50 @@
> > +/* Test RTLD_PROBE for dlsym.
> > +   Copyright (C) 2022-2022 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 <dlfcn.h>
> > +#include <gnu/lib-names.h>
> > +#include <stdio.h>
> > +#include <stdlib.h>
> > +#include <string.h>
> > +#include <support/check.h>
> > +#include <support/xdlfcn.h>
> > +
> > +static int
> > +do_test (void)
> > +{
> > +  int *iptr;
> > +  int ret;
> > +  void *handle;
> > +
> > +  handle = dlopen (LIBM_SO, RTLD_LAZY);
> > +  TEST_VERIFY (handle == NULL);
> > +  iptr = (int *)dlsym (RTLD_PROBE, "finite");  // get sym but not call --detect if symbol exist
> > +  ret = dlclose (handle);
> > +  TEST_VERIFY (ret != 0);
> > +  ret = 0;
> > +
> > +  handle = dlopen (LIBM_SO, RTLD_LAZY);
> > +  TEST_VERIFY (handle == NULL);
> > +  iptr = (int *)dlsym (RTLD_DEFAULT, "finite"); // get sym and keep
> > +  ret = dlcose (handle);
> > +  TEST_VERIFY (ret == 0);
> > +  return 0;
> > +}
> > +
> > +
> > +#include <support/test-driver.c>
> > --
> > 2.12.3
> >
>
>
> --
> H.J.



-- 
H.J.

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

* [PATCH v2] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency.
  2022-11-22 16:35   ` H.J. Lu via Libc-alpha
@ 2022-11-23  2:21     ` Wang Bing via Libc-alpha
  2022-12-05 14:45       ` Florian Weimer via Libc-alpha
  0 siblings, 1 reply; 5+ messages in thread
From: Wang Bing via Libc-alpha @ 2022-11-23  2:21 UTC (permalink / raw)
  To: libc-alpha
  Cc: hjl.tools, nixiaoming, szabolcs.nagy, wangbing6, yanhuijun,
	zhongjubin

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="yes", Size: 4868 bytes --]

dlsym with RTLD_DEFAULT add dependency for target symbol, if program 
use dlsym only to detect if a symbol exist, and will not use it.

this operation will make unable to dlclose so file containing target 
symbol, add RTLD_PROBE to support symbol probe.

The reason why not useing struct link_map * as an argument to dlsym is 
that there may be more than one same-named symbol in different so 
files(provided by different vendors or components) in one process, and 
by -Wl,-z,now,only the first symbol is needed, and we cannot be sure 
which struct link_map* should be send to dlsym.

Signed-off-by: Wang Bing <wangbing6@huawei.com>

---
v2: fix code corruption
v1: https://public-inbox.org/libc-alpha/7cad26c73f5b4172bbcf570882bf9d3d@huawei.com/#R

---
 dlfcn/dlfcn.h              |  4 ++++
 elf/Makefile               |  1 +
 elf/dl-sym.c               | 13 +++++++++---
 elf/tst-dlsym-rtld-probe.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+), 3 deletions(-)
 create mode 100644 elf/tst-dlsym-rtld-probe.c

diff --git a/dlfcn/dlfcn.h b/dlfcn/dlfcn.h
index 6f7cad8682..ab709883a6 100644
--- a/dlfcn/dlfcn.h
+++ b/dlfcn/dlfcn.h
@@ -49,6 +49,10 @@ typedef long int Lmid_t;
    is returned.  */
 #define RTLD_DEFAULT	((void *) 0)
 
+/* If only find sym in the global scope, but will not use it, do not
+   set sym dependency. */
+# define RTLD_PROBE    ((void *) -2l)
+
 __BEGIN_DECLS
 
 /* Open the shared object FILE and map it in; return a handle that can be
diff --git a/elf/Makefile b/elf/Makefile
index eca7b28ab5..f9fc9fbebb 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -472,6 +472,7 @@ tests += \
   unload7 \
   unload8 \
   valgrind-test \
+  tst-dlsym-rtld-probe \
   # tests
 tests-cxx = \
   tst-dlopen-nodelete-reloc \
diff --git a/elf/dl-sym.c b/elf/dl-sym.c
index b1cf42f36d..9fdf7fb036 100644
--- a/elf/dl-sym.c
+++ b/elf/dl-sym.c
@@ -92,10 +92,17 @@ do_sym (void *handle, const char *name, void *who,
   /* Link map of the caller if needed.  */
   struct link_map *match = NULL;
 
-  if (handle == RTLD_DEFAULT)
+  int def_flags;
+
+  if (handle == RTLD_DEFAULT || handle == RTLD_PROBE)
     {
       match = _dl_sym_find_caller_link_map (caller);
 
+      def_flags = flags
+      if (def_flags == RTLD_DEFAULT) {
+          def_flags |= DL_LOOKUP_ADD_DEPENDENCY;
+      }
+
       /* Search the global scope.  We have the simple case where
 	 we look up in the scope of an object which was part of
 	 the initial binary.  And then the more complex part
@@ -104,7 +111,7 @@ do_sym (void *handle, const char *name, void *who,
       if (RTLD_SINGLE_THREAD_P)
 	result = GLRO(dl_lookup_symbol_x) (name, match, &ref,
 					   match->l_scope, vers, 0,
-					   flags | DL_LOOKUP_ADD_DEPENDENCY,
+					   def_flags,
 					   NULL);
       else
 	{
@@ -113,7 +120,7 @@ do_sym (void *handle, const char *name, void *who,
 	  args.map = match;
 	  args.vers = vers;
 	  args.flags
-	    = flags | DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK;
+	    = def_flags | DL_LOOKUP_GSCOPE_LOCK;
 	  args.refp = &ref;
 
 	  THREAD_GSCOPE_SET_FLAG ();
diff --git a/elf/tst-dlsym-rtld-probe.c b/elf/tst-dlsym-rtld-probe.c
new file mode 100644
index 0000000000..c72ceaa182
--- /dev/null
+++ b/elf/tst-dlsym-rtld-probe.c
@@ -0,0 +1,50 @@
+/* Test RTLD_PROBE for dlsym.
+   Copyright (C) 2022-2022 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 <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/xdlfcn.h>
+
+static int
+do_test (void)
+{
+  int *iptr;
+  int ret;
+  void *handle;
+
+  handle = dlopen (LIBM_SO, RTLD_LAZY);
+  TEST_VERIFY (handle == NULL);
+  iptr = (int *)dlsym (RTLD_PROBE, "finite");	// get sym but not call --detect if symbol exist
+  ret = dlclose (handle);
+  TEST_VERIFY (ret != 0);
+  ret = 0;
+
+  handle = dlopen (LIBM_SO, RTLD_LAZY);
+  TEST_VERIFY (handle == NULL);
+  iptr = (int *)dlsym (RTLD_DEFAULT, "finite"); // get sym and keep
+  ret = dlcose (handle);
+  TEST_VERIFY (ret == 0);
+  return 0;
+}
+
+
+#include <support/test-driver.c>
-- 
2.12.3


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

* Re: [PATCH v2] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency.
  2022-11-23  2:21     ` [PATCH v2] " Wang Bing via Libc-alpha
@ 2022-12-05 14:45       ` Florian Weimer via Libc-alpha
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Weimer via Libc-alpha @ 2022-12-05 14:45 UTC (permalink / raw)
  To: Wang Bing via Libc-alpha
  Cc: Wang Bing, hjl.tools, nixiaoming, szabolcs.nagy, yanhuijun,
	zhongjubin

* Wang Bing via Libc-alpha:

> diff --git a/elf/tst-dlsym-rtld-probe.c b/elf/tst-dlsym-rtld-probe.c
> new file mode 100644
> index 0000000000..c72ceaa182
> --- /dev/null
> +++ b/elf/tst-dlsym-rtld-probe.c
> @@ -0,0 +1,50 @@
> +/* Test RTLD_PROBE for dlsym.
> +   Copyright (C) 2022-2022 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 <dlfcn.h>
> +#include <gnu/lib-names.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <support/check.h>
> +#include <support/xdlfcn.h>
> +
> +static int
> +do_test (void)
> +{
> +  int *iptr;
> +  int ret;
> +  void *handle;
> +
> +  handle = dlopen (LIBM_SO, RTLD_LAZY);
> +  TEST_VERIFY (handle == NULL);
> +  iptr = (int *)dlsym (RTLD_PROBE, "finite");	// get sym but not call --detect if symbol exist
> +  ret = dlclose (handle);
> +  TEST_VERIFY (ret != 0);
> +  ret = 0;
> +
> +  handle = dlopen (LIBM_SO, RTLD_LAZY);
> +  TEST_VERIFY (handle == NULL);
> +  iptr = (int *)dlsym (RTLD_DEFAULT, "finite"); // get sym and keep
> +  ret = dlcose (handle);
> +  TEST_VERIFY (ret == 0);
> +  return 0;
> +}
> +
> +
> +#include <support/test-driver.c>

I tried to fix the test case to work stand-alone, and with RTLD_DEFAULT:

#include <dlfcn.h>
#include <err.h>
#include <gnu/lib-names.h>
#include <stddef.h>

int
main (void)
{
  void *handle = dlopen (LIBM_SO, RTLD_LAZY);
  if (handle == NULL)
    errx (1, dlerror ());

  void *p = dlsym (RTLD_DEFAULT, "finite");

  if (p == NULL)
    errx (1, dlerror ());
  if (dlclose (handle) != 0)
    errx (1, dlerror ());

  if (dlopen (LIBM_SO, RTLD_LAZY | RTLD_NOLOAD) != NULL)
    errx (1, "libm not unloaded");
  return 0;
}

It does not show that unloading is blocked: RTLD_NOLOAD subsequently
fails.  Furthermore, the LD_DEBUG=all output indicates that unloading
happens before the return from main (before the ELF destructors are
called):

    268899:	symbol=dlclose;  lookup in file=./a.out [0]
    268899:	symbol=dlclose;  lookup in file=/lib64/libc.so.6 [0]
    268899:	binding file ./a.out [0] to /lib64/libc.so.6 [0]: normal symbol `dlclose' [GLIBC_2.34]
    268899:	
    268899:	calling fini: /lib64/libm.so.6 [0]
    268899:	
    268899:	
    268899:	file=/lib64/libm.so.6 [0];  destroying link map
    268899:	
    268899:	file=libm.so.6 [0];  dynamically loaded by ./a.out [0]
    268899:	find library=libm.so.6 [0]; searching
    268899:	 search cache=/etc/ld.so.cache
    268899:	  trying file=/lib64/libm.so.6
    268899:	
    268899:	
    268899:	calling fini:  [0]

Please post a test case that demonstrates the problem you are trying to
fix.

Thanks,
Florian


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

end of thread, other threads:[~2022-12-05 14:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-22  2:30 [PATCH] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency Wang Bing via Libc-alpha
2022-11-22  2:36 ` H.J. Lu via Libc-alpha
2022-11-22 16:35   ` H.J. Lu via Libc-alpha
2022-11-23  2:21     ` [PATCH v2] " Wang Bing via Libc-alpha
2022-12-05 14:45       ` Florian Weimer via Libc-alpha

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).