public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] dlsym: Add RTLD_PROBE for situation when dlsym only wants to probe a symbol but not use it
@ 2022-09-27  9:21 Wangbing(wangbing,RTOS/Poincare Lab)
  2022-09-27 10:40 ` Florian Weimer
  0 siblings, 1 reply; 2+ messages in thread
From: Wangbing(wangbing,RTOS/Poincare Lab) @ 2022-09-27  9:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: Nixiaoming

[-- Attachment #1: Type: text/plain, Size: 2470 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.



commit f769328dba5351107097c3a3ad7e1192604318d0
Author: Wang Bing <wangbing6@huawei.com<mailto:wangbing6@huawei.com>>
Date:   Tue Sep 27 14:58:43 2022 +0800

    dlsym: Add RTLD_PROBE for situation when dlsym only wants to probe a symbol but not use it

diff --git a/dlfcn/dlfcn.h b/dlfcn/dlfcn.h
index fe181085..7ac0d5f1 100644
--- a/dlfcn/dlfcn.h
+++ b/dlfcn/dlfcn.h
@@ -39,6 +39,9 @@
    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)

/* Type for namespace indices.  */
typedef long int Lmid_t;
diff --git a/elf/dl-sym.c b/elf/dl-sym.c
index de5769f9..970f2028 100644
--- a/elf/dl-sym.c
+++ b/elf/dl-sym.c
@@ -92,10 +92,15 @@ 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)
+  if (handle == RTLD_DEFAULT || handle == RTLD_PROBE)
     {
       match = _dl_sym_find_caller_link_map (caller);

+      int def_flags = flags;
+      if (handle == 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 +109,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 +118,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 ();



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

* Re: [PATCH] dlsym: Add RTLD_PROBE for situation when dlsym only wants to probe a symbol but not use it
  2022-09-27  9:21 [PATCH] dlsym: Add RTLD_PROBE for situation when dlsym only wants to probe a symbol but not use it Wangbing(wangbing,RTOS/Poincare Lab)
@ 2022-09-27 10:40 ` Florian Weimer
  0 siblings, 0 replies; 2+ messages in thread
From: Florian Weimer @ 2022-09-27 10:40 UTC (permalink / raw)
  To: Wangbing(wangbing, RTOS/Poincare Lab) via Libc-alpha
  Cc: Wangbing(wangbing,RTOS/Poincare Lab), Nixiaoming

* Wangbing via Libc-alpha:

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

I'd appreciate if you could write a test case for this functionality.

I'm not sure if the new functionality is needed.  If we document that
a struct link_map * obtained from _dl_find_object or via
_r_debug.r_map can be used as an argument to dlsym (and other places
where a DSO handle is currently accepted), programmers could use that
to get this effect.  I believe this works today (as an undocumented
feature) and does not record the dependency.

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

end of thread, other threads:[~2022-09-27 10:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-27  9:21 [PATCH] dlsym: Add RTLD_PROBE for situation when dlsym only wants to probe a symbol but not use it Wangbing(wangbing,RTOS/Poincare Lab)
2022-09-27 10:40 ` 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).