From patchwork Mon Nov 21 14:06:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: develop--- via Libc-alpha X-Patchwork-Id: 60927 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC9EE3852C70 for ; Mon, 21 Nov 2022 14:06:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC9EE3852C70 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669039599; bh=s57k0S1O+qKU6VhFpH/ciOdE8O+0vvYWY3bxfnbhZ08=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=K018BjJLeOhMHskf5LRr/seOmfwUVbSwnnU421FHzx5FLfzKqXlKrMg2fMklOceCr Hh8LSfi+oQw94xt26j3YwNhuuXYrFI7YLbep1Es+pPOERgEXnMP02EKXSn+27bL8n2 uRDiRuDVoQIt61wQe3XiqTQO8+ns3BcMRBZh0pUU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by sourceware.org (Postfix) with ESMTPS id A4C4B3853D4D for ; Mon, 21 Nov 2022 14:06:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A4C4B3853D4D Received: from kwepemi500008.china.huawei.com (unknown [172.30.72.55]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NG8H14cLhzFqM3 for ; Mon, 21 Nov 2022 22:02:53 +0800 (CST) Received: from kwepemi500012.china.huawei.com (7.221.188.12) by kwepemi500008.china.huawei.com (7.221.188.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 21 Nov 2022 22:06:07 +0800 Received: from kwepemi500012.china.huawei.com ([7.221.188.12]) by kwepemi500012.china.huawei.com ([7.221.188.12]) with mapi id 15.01.2375.031; Mon, 21 Nov 2022 22:06:07 +0800 To: Florian Weimer , "Wangbing(wangbing, RTOS/Poincare Lab) via Libc-alpha" CC: Nixiaoming , zhongjubin , "Yanhuijun (DOPRA SSP)" Subject: Add testcese// [PATCH] dlsym: Add RTLD_PROBE for situation when dlsym only wants to probe a symbol but not use it Thread-Topic: Add testcese// [PATCH] dlsym: Add RTLD_PROBE for situation when dlsym only wants to probe a symbol but not use it Thread-Index: Adj9sjCPJe/1EMMJSGGOg4+wNhp7Vg== Date: Mon, 21 Nov 2022 14:06:07 +0000 Message-ID: <7cad26c73f5b4172bbcf570882bf9d3d@huawei.com> Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.67.109.108] MIME-Version: 1.0 X-CFilter-Loop: Reflected X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, BODY_8BITS, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, MIME_CHARSET_FARAWAY, 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 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: "Wangbing\(wangbing, RTOS/Poincare Lab\) via Libc-alpha" Reply-To: "Wangbing\(wangbing,RTOS/Poincare Lab\)" Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Sorry for the delay 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. So we would like use dlsym(0, symbol) to get the first found symbol. And add testcase for RTLD_PROBE: dlsym: Add RTLD_PROBE to dlsym only probe symbol without add dependency. Signed-off-by: Wang Bing 发送时间: 2022年9月27日 18:41 收件人: Wangbing(wangbing, RTOS/Poincare Lab) via Libc-alpha 抄送: Wangbing(wangbing,RTOS/Poincare Lab) ; Nixiaoming 主题: Re: [PATCH] dlsym: Add RTLD_PROBE for situation when dlsym only wants to probe a symbol but not use it * 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. 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..84ba92c6c8 --- /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"); 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"); + ret = dlcose (handle); + TEST_VERIFY (ret == 0); + return 0; +} + + +#include -----邮件原件----- 发件人: Florian Weimer [mailto:fw@deneb.enyo.de]