From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.2 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 560571F910 for ; Wed, 23 Nov 2022 02:22:21 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="KB6B+RQv"; dkim-atps=neutral Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2C0023854544 for ; Wed, 23 Nov 2022 02:22:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C0023854544 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669170140; bh=ETHfCGEjL7guOw92G0L5RDaPOBrNMUSbOZvQCXJkrjc=; h=To:CC:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=KB6B+RQvz3eZxfEAbL7y/UlzGO01jqtLD40ohgh2TlaCX1yKlr2RCnzkXiW6rzQjy O/cHyzB6ruIn0eOa69CqmIGzeFiYKDPvg7sI4hx+NBQEeF0bucI0GR0F1LCWcTWB4n FJmmCz93hVlscMjCF4mYE/V0Cy/fzTOFPYNlv89w= Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by sourceware.org (Postfix) with ESMTPS id 44900385703F for ; Wed, 23 Nov 2022 02:21:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 44900385703F Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NH4Xl6fhvzqSc1; Wed, 23 Nov 2022 10:17:59 +0800 (CST) Received: from linux-suse12sp5.huawei.com (10.67.133.76) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 23 Nov 2022 10:21:54 +0800 To: CC: , , , , , Subject: [PATCH v2] dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency. Date: Wed, 23 Nov 2022 10:21:37 +0800 Message-ID: <20221123022137.65977-1-wangbing6@huawei.com> X-Mailer: git-send-email 2.12.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="yes" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.133.76] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Wang Bing via Libc-alpha Reply-To: Wang Bing Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "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. 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 --- 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 + . */ + +#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