From patchwork Tue May 3 19:14:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nicholas Guriev X-Patchwork-Id: 1625712 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=guriev.su header.i=@guriev.su header.a=rsa-sha256 header.s=bvj00itehu6icf4 header.b=XdvTDwRz; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Kt8mR132Lz9s0r for ; Wed, 4 May 2022 05:15:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1AEF4395BC44 for ; Tue, 3 May 2022 19:15:00 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from dandelion.mymedia.su (dandelion.mymedia.su [107.191.98.137]) by sourceware.org (Postfix) with ESMTPS id 4A60E395A440 for ; Tue, 3 May 2022 19:14:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4A60E395A440 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=guriev.su Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=guriev.su Received: by dandelion.mymedia.su (Postfix, from userid 1000) id 9B9E8C00B3; Tue, 3 May 2022 22:14:49 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=guriev.su; s=bvj00itehu6icf4; t=1651605289; bh=K/c7uev1DKHQcE+blqnxc7Tbo4PdLj4wuGcustK8b7w=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:From; b=XdvTDwRzwdxC6pLw7Amu2rG07ZsjR6s+E6b7E6AtqBztbEYDlo+5rUYJf1F7Bh9Qv YZ35NDfdE2FmFO2+ODosHdhlmJ0Y+RBob9al2lwloV+Gh1vpm6m4vqCLwELFjluJzK 3yCHVHZbD+zb0S2ykPqkEhl/MToFdWkKsvsyv3iif964HYk5bdQwcQObBpFiSTFI4E polI8dTkX0/dons/ZWEAtoHBnoy4VgMGjL4QtPhG70qHX6ECFQxzRdcWFZzt4OyBwt gS26PQM0vdpteiItJIYORiIj47eRgWaBPjWsvvQH294njg8onA9Ui3MA0oKoWpBRh7 TguEfx6OBs/PQ== Message-ID: Subject: [PATCH v2] elf: Rewrite long RESOLVE_MAP macro to a debug friendly function From: Nicholas Guriev To: Fangrui Song Date: Tue, 03 May 2022 22:14:38 +0300 In-Reply-To: <20220502212035.45lvk22rjui65pvq@google.com> References: <20220502212035.45lvk22rjui65pvq@google.com> User-Agent: Evolution 3.40.4-1ubuntu2 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Cc: libc-alpha@sourceware.org Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" On Пн, 2022-05-02 at 14:20 -0700, Fangrui Song wrote: > Use `lookup_t _lr = ` ? I was going to leave most of the formatting as it was, including local variables definitions. But here is a version with joined lr declaration and initialization. The line became too long, and to fit 79-symbol limit I put another variable undef_name. I also removed the useless underscore in the variable names. -- >8 -- A static function that may be inlined is way better to find where exactly a crash happens. So one can step into the function with GDB. The macro still remains for compatibility with other code. Signed-off-by: Nicholas Guriev --- elf/dl-reloc.c | 56 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index 771a34bd14..106d458f8b 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -162,29 +162,41 @@ _dl_nothread_init_static_tls (struct link_map *map) } #endif /* !PTHREAD_IN_LIBC */ +static lookup_t +_dl_resolve_map (lookup_t l, struct r_scope_elem *scope[], const ElfW(Sym) **ref, + const struct r_found_version *version, unsigned long int r_type) +{ + if (ELFW(ST_BIND) ((*ref)->st_info) == STB_LOCAL + || __glibc_unlikely (dl_symbol_visibility_binds_local_p (*ref))) + return l; + + if (__glibc_unlikely (*ref == l->l_lookup_cache.sym) + && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) + { + bump_num_cache_relocations (); + *ref = l->l_lookup_cache.ret; + } + else + { + int tc = elf_machine_type_class (r_type); + l->l_lookup_cache.type_class = tc; + l->l_lookup_cache.sym = *ref; + const char *undef_name + = (const char *) D_PTR (l, l_info[DT_STRTAB]) + (*ref)->st_name; + const struct r_found_version *v = NULL; + if (version != NULL && version->hash != 0) + v = version; + lookup_t lr = _dl_lookup_symbol_x (undef_name, l, ref, scope, v, tc, + DL_LOOKUP_ADD_DEPENDENCY + | DL_LOOKUP_FOR_RELOCATE, NULL); + l->l_lookup_cache.ret = *ref; + l->l_lookup_cache.value = lr; + } + return l->l_lookup_cache.value; +} + /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */ -#define RESOLVE_MAP(l, scope, ref, version, r_type) \ - ((ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \ - && __glibc_likely (!dl_symbol_visibility_binds_local_p (*ref))) \ - ? ((__glibc_unlikely ((*ref) == l->l_lookup_cache.sym) \ - && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \ - ? (bump_num_cache_relocations (), \ - (*ref) = l->l_lookup_cache.ret, \ - l->l_lookup_cache.value) \ - : ({ lookup_t _lr; \ - int _tc = elf_machine_type_class (r_type); \ - l->l_lookup_cache.type_class = _tc; \ - l->l_lookup_cache.sym = (*ref); \ - const struct r_found_version *v = NULL; \ - if ((version) != NULL && (version)->hash != 0) \ - v = (version); \ - _lr = _dl_lookup_symbol_x ((const char *) D_PTR (l, l_info[DT_STRTAB]) + (*ref)->st_name, \ - l, (ref), scope, v, _tc, \ - DL_LOOKUP_ADD_DEPENDENCY \ - | DL_LOOKUP_FOR_RELOCATE, NULL); \ - l->l_lookup_cache.ret = (*ref); \ - l->l_lookup_cache.value = _lr; })) \ - : l) +#define RESOLVE_MAP _dl_resolve_map #include "dynamic-link.h"