From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by sourceware.org (Postfix) with ESMTPS id D24E63858C66 for ; Tue, 22 Nov 2022 02:37:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D24E63858C66 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-x22c.google.com with SMTP id m204so14520309oib.6 for ; Mon, 21 Nov 2022 18:37:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=nWMQtu+z9eGfMuoOqSP2Dq9+Z2W7yi/VsshVk9+PU+8=; b=KtzXdGJZR2Ll86h/csJfJfIqyJ3h2bXUXeoA+KgWof4okTWF/DDFZd3vNTuiiln0zT baHxsZrdTOLatOmJK98VsG096BrCPLgt5CvmpI5ul8IVnh4L64kgIRmbnCWy/P1Kd20S xOCepvj4ID/4wpRNiEJ40KLFCk8fhO+F53TivsiObn0xg0liShkuCGkDVuIkVfaufesG 5QdlePJ54SZXDMJVrrLwTtFgrXP227rb9L9dx0zQyzeajBeNrZ7lVjUkAzeOE06SsIZ/ ApOtCzZBMYIN656RSjNyVhFA7n7Zj5upASIz86CCtqQh/5ZbWqsktvmPbYcHB/Eyssru W+AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nWMQtu+z9eGfMuoOqSP2Dq9+Z2W7yi/VsshVk9+PU+8=; b=JAOfn1V/g7Enf88AREkLnPTqIxgEerQ1WIpQZNAbtr4igHwf2R8vpmhH5BDx0y0R+G eSF4uOv3mw1hjeK49rJ6RHHSURU1c70U6PXhd9jcAJoX/HXnduaUHHXqq1fusnk0nJIz W8FvCVBhjeZnyqgLUe0MNVOz3FGir+nQIeduOVOuK684CjMSoLsyEwHN43EjPeJzXFPI xct1EF1nCgz56HFKe39Ck82ilzN3BArjuVQKHggg1x4oR7cxP7EODkSWgcKC9EMYgt8r QNiUJCbDBgQiN2tGG4W4rRUuVkx/7mZEcCOidcPWXKT7FRcUEeuzYzHySmUDNhXjFxf5 WEzw== X-Gm-Message-State: ANoB5pmCEIf8G7zb+tOomBGqHRvY5qauAK/R45Mr4PcsYlISs3kjp5+X 1Qu5uxleb+/2+V2U8/VgGU190J1M4EF/cJRsLzc= X-Google-Smtp-Source: AA0mqf4gC55lpW0dN0PJqPHbkKkzOPQfi3iX9bKjrDdJdDO2Z/I5lV+QnwDpW1NaMYMxQzory1bymUmMPOmsDcdTnxY= X-Received: by 2002:aca:bc87:0:b0:35b:1f92:e4ff with SMTP id m129-20020acabc87000000b0035b1f92e4ffmr2833996oif.266.1669084639110; Mon, 21 Nov 2022 18:37:19 -0800 (PST) MIME-Version: 1.0 References: <20221122023041.36724-1-wangbing6@huawei.com> In-Reply-To: <20221122023041.36724-1-wangbing6@huawei.com> From: "H.J. Lu" Date: Mon, 21 Nov 2022 18:36:42 -0800 Message-ID: Subject: Re: [PATCH] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency. To: Wang Bing Cc: libc-alpha@sourceware.org, szabolcs.nagy@arm.com, nixiaoming@huawei.com, zhongjubin@huawei.com, yanhuijun@huawei.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3023.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Mon, Nov 21, 2022 at 6:31 PM Wang Bing via Libc-alpha wrote: > > Signed-off-by: Wang Bing > --- > 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 > + . */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +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 > -- > 2.12.3 > -- H.J.