From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2178) id 565013856DD8; Mon, 11 Apr 2022 14:06:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 565013856DD8 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Florian Weimer To: glibc-cvs@sourceware.org Subject: [glibc/fw/dl-bind-performance] dlsym: Do not determine caller link map if not needed X-Act-Checkin: glibc X-Git-Author: Florian Weimer X-Git-Refname: refs/heads/fw/dl-bind-performance X-Git-Oldrev: 10cf7f52c60209f34199d216ef6710568d6297bf X-Git-Newrev: 6e18a8ddf1f60596e84e25f3af4bb8f0c27ffe2a Message-Id: <20220411140651.565013856DD8@sourceware.org> Date: Mon, 11 Apr 2022 14:06:51 +0000 (GMT) X-BeenThere: glibc-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Glibc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Apr 2022 14:06:51 -0000 https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=6e18a8ddf1f60596e84e25f3af4bb8f0c27ffe2a commit 6e18a8ddf1f60596e84e25f3af4bb8f0c27ffe2a Author: Florian Weimer Date: Fri Nov 8 15:48:51 2019 +0100 dlsym: Do not determine caller link map if not needed Obtaining the link map is potentially very slow because it requires iterating over all loaded objects in the current implementation. If the caller supplied an explicit handle (i.e., not one of the RTLD_* constants), the dlsym implementation does not need the identity of the caller (except in the special cause of auditing), so this change avoids computing it in that case. Even in the minimal case (dlsym called from a main program linked with -dl), this shows a small speedup, perhaps around five percent. The performance improvement can be arbitrarily large in principle (if _dl_find_dso_for_object has to iterate over many link maps). Change-Id: Ide5d9e2cc7ac25a0ffae8fb4c26def0c898efa29 Diff: --- elf/dl-sym.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/elf/dl-sym.c b/elf/dl-sym.c index 8209342b13..7282e09dc0 100644 --- a/elf/dl-sym.c +++ b/elf/dl-sym.c @@ -80,6 +80,18 @@ call_dl_lookup (void *ptr) args->flags, NULL); } +/* Return the link map containing the caller address. */ +static inline struct link_map * +find_caller_link_map (ElfW(Addr) caller) +{ + struct link_map *l = _dl_find_dso_for_object (caller); + if (l != NULL) + return l; + else + /* If the address is not recognized the call comes from the main + program (we hope). */ + return GL(dl_ns)[LM_ID_BASE]._ns_loaded; +} static void * do_sym (void *handle, const char *name, void *who, @@ -89,13 +101,13 @@ do_sym (void *handle, const char *name, void *who, lookup_t result; ElfW(Addr) caller = (ElfW(Addr)) who; - struct link_map *l = _dl_find_dso_for_object (caller); - /* If the address is not recognized the call comes from the main - program (we hope). */ - struct link_map *match = l ? l : GL(dl_ns)[LM_ID_BASE]._ns_loaded; + /* Link map of the caller if needed. */ + struct link_map *match = NULL; if (handle == RTLD_DEFAULT) { + match = find_caller_link_map (caller); + /* 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 @@ -128,6 +140,8 @@ do_sym (void *handle, const char *name, void *who, } else if (handle == RTLD_NEXT) { + match = find_caller_link_map (caller); + if (__glibc_unlikely (match == GL(dl_ns)[LM_ID_BASE]._ns_loaded)) { if (match == NULL @@ -187,6 +201,9 @@ RTLD_NEXT used in code not dynamically loaded")); unsigned int ndx = (ref - (ElfW(Sym) *) D_PTR (result, l_info[DT_SYMTAB])); + if (match == NULL) + match = find_caller_link_map (caller); + if ((match->l_audit_any_plt | result->l_audit_any_plt) != 0) { unsigned int altvalue = 0;