From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id HDGgJ/V2umqS+REAWB0awg (envelope-from ) for ; Mon, 28 Sep 2026 10:17:25 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=aDTIrNPL; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 741D11E06B; Mon, 28 Sep 2026 10:17:25 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 3B7C11E01F for ; Mon, 28 Sep 2026 10:17:24 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 89DDF4BAE7EE for ; Mon, 28 Sep 2026 14:17:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 89DDF4BAE7EE Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=aDTIrNPL Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 179574BAE7E9 for ; Mon, 28 Sep 2026 14:16:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 179574BAE7E9 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 179574BAE7E9 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790605000; cv=none; b=dgmGN8aqx0RwCNOWlRQNj2A8loXG1u29iBl023F4bnUmp/D0rz/XiYd2vlB23AIscgYTYga9J0Q+dcS/pJVHqVSA+mxygPOazGTe6JbfCFGdEU+zu9jT57LyCuTDG1/SiFfrBdfSqYOz9cTfvyHmTVqBrt6YCVOkA+svJZQe+Rk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790605000; c=relaxed/simple; bh=wH9KuTyONJmwncFrd+fe4vVKf56g6pSwlQBuss/1+Io=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=ExcEqwleHZNmm3Evk4saVO+g1whFAuZcqWfwHtoNGTxzhs8qMQSg15d+YdyccZasILqt0jiP0kU0dN6fkYtp11p14vfvdnb2BXewahGDGiqU13GYCQyYL5nGm+M+aHZR4EicXkpM9idmDIj+fztBsRK2SfsBggy+pfhSbNy9ZRY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=aDTIrNPL DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 179574BAE7E9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1790604999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4brT7Sa5oLctcXgZ/8kZ3BnpGJmdh3yc08pu+52dR/0=; b=aDTIrNPL3i2y7GlNLx06RcaoKa60z0C0il/hcJu4+hdAdlfUzN58/2T8GDrnPgW3EhRrCa 73mB4WTHnRxyVZB7k+OaTVfUwm3EdmCEf/0mdMkZVZn7Rfrl7DZMzxsIBg0ixaxSkLUxLS 9NooWgK+DS2MMjcY57dKiFEssyvRhig= Received: from mail-yw1-f197.google.com (mail-yw1-f197.google.com [209.85.128.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-31-_0APm-zZNEe93RaNtNX7Lw-1; Mon, 28 Sep 2026 10:16:35 -0400 X-MC-Unique: _0APm-zZNEe93RaNtNX7Lw-1 X-Mimecast-MFC-AGG-ID: _0APm-zZNEe93RaNtNX7Lw_1790604995 Received: by mail-yw1-f197.google.com with SMTP id 00721157ae682-81eddfe28b4so45746127b3.3 for ; Mon, 28 Sep 2026 07:16:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790604995; x=1791209795; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=4brT7Sa5oLctcXgZ/8kZ3BnpGJmdh3yc08pu+52dR/0=; b=CSi15WmknjmQyLGo7Ehms4f6MXFXfFuf3sde2Gb5bJPlQ4rfbBwu10ftLy/5Dl8y9v OFSXLU3sFaZw0ZcZfci8SktslQFoam2L38++uI1oyz0WtGaap6HKxYHPSILWyISQIN+A PNs0nW7tciP03qDz2efirw1gMxVD78c9p8GomuvRAwlanugluH0cX1Xs2pulyQFgGohB xEPK5XeaVy9vYCk53S3i7uxkBmp/Cwd5KAZSjnHNVmfvJEi+omu0fZJB8E/Fo/Jf+23i 3bOs938g89Q8bkwNlwbOj4KmjfIo1nHHZaqH3qmDrA555GyxfrJXUQ4GFvoqYEOmWvyD IMwQ== X-Forwarded-Encrypted: i=1; AKwUvBwF21JlkM6exSUsXt/EDaAq78M/YrQcm4rVumdwWOUEA+ug4cPCgx2pD5zH4Z18HalpAysgqL2qTYsL7w==@sourceware.org X-Gm-Message-State: AFuF++kPIkvJgND8pimzG/Z3hmP4wYPp6ZSy27umKqHw/lUjjsNecG5q Xad6lt2czVZEkhLtkeFwmEVlry5bQGKq6A5E/VeOeqZU/2BESfqnR16TPmJ7EKjmaXzAienec4f gJosIDXGJzZ5SWhvJDbcJkMZj2WrjtCx3rtHwJpb5t1kStYL9K/PWbmO5Jrx8hjs= X-Gm-Gg: AYBFou3Mg9FRQC7/qsAiX5LBMb5KYB6wYJWsgd1BJqxRqf6bczIkPDNuUFCvr7G4ui5 FCp7SWr4UU/q8s6KHXRm7l9DOWiD4Kj7o48d+j/t/Sj6A11aPAHQbP9xjSFL3wc1ULKJLl9Bd9u U74fEigGF+29QLT+Zx7kKdAs3k3lUF3D7qV6407JoN8QqP800xy2aXhL+gLmJRCHgJ/YqcxhkdX RBgCMaGkn1T3Eye5KG1mERCfnIdTJIGBPcLsCycBHToRlFQFi7QmgHtvZ5HDwlOJB1jMp3dhIJR gyPntGtBR2Bp7Sx14QIK7RessEim4IjYXWitNRDDxvg+KXn0TkhJpE+/vO/ULGNCiSCPmtEx2zW JS9DYA4r7U065u5iRdoh0Ssf4tyn46UBXXDBGHxP7pm+MgwTd X-Received: by 2002:a05:690e:440c:b0:675:5ce8:f037 with SMTP id 956f58d0204a3-6755ce90ee9mr225175d50.35.1790604994922; Mon, 28 Sep 2026 07:16:34 -0700 (PDT) X-Received: by 2002:a05:690e:440c:b0:675:5ce8:f037 with SMTP id 956f58d0204a3-6755ce90ee9mr225167d50.35.1790604994255; Mon, 28 Sep 2026 07:16:34 -0700 (PDT) Received: from ?IPV6:2804:14d:8084:993e:22e8:4ed1:527a:55c0? ([2804:14d:8084:993e:22e8:4ed1:527a:55c0]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8a860325fbesm45252567b3.5.2026.09.28.07.16.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Sep 2026 07:16:33 -0700 (PDT) Message-ID: <334ae7d5-410d-470c-b370-2fb099bbecd8@redhat.com> Date: Mon, 28 Sep 2026 11:16:30 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] [gdb] Refactor svr4_solib_ops::lm_addr_check To: Tom de Vries , gdb-patches@sourceware.org References: <20260911191940.2892092-1-tdevries@suse.de> From: Guinevere Larsen In-Reply-To: <20260911191940.2892092-1-tdevries@suse.de> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: NCzIIkZmEaxmieVM8Li36erXRcmF9aAXitHeBZeDupQ_1790604995 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org On 9/11/26 4:19 PM, Tom de Vries wrote: > I came across svr4_solib_ops::lm_addr_check and found it hard to follow. > > In particular, it's indent-happy and minimizes code using a goto, resulting in > the following end-of-function: > ... > } > } > > set_addr: > li.l_addr = l_addr; > li.l_addr_p = 1; > } > > return li.l_addr; > } > ... > > I could just address the indentation, but I'd be adding a goto: > ... > - if (dynaddr + l_addr != l_dynaddr) > + if (dynaddr + l_addr == l_dynaddr) > + goto set_addr; > ... > > Instead, refactor the function into this form: > ... > svr4_solib_ops::lm_addr_check (const solib &so, bfd *abfd) const > { > auto &li = get_lm_info_svr4 (so); > if (li.l_addr_p) > return li.l_addr; > > auto set_addr = [&li] (CORE_ADDR addr) > { > li.l_addr = addr; > li.l_addr_p = true; > return addr; > } > > if (...) > return set_addr (l_addr); > > ... > > return set_addr (l_addr); > } > ... > --- Hi Tom! Thanks for doing this! The final result is indeed much simpler to read, and the refactor is quite straight-forward. Reviewed-By: Guinevere Larsen Hopefully this gets approved soon! -- Cheers, Guinevere Larsen it/its she/her (deprecated) > gdb/solib-svr4.c | 160 +++++++++++++++++++++++------------------------ > 1 file changed, 80 insertions(+), 80 deletions(-) > > diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c > index c4af1b11a4d..40b0fc55113 100644 > --- a/gdb/solib-svr4.c > +++ b/gdb/solib-svr4.c > @@ -225,106 +225,106 @@ CORE_ADDR > svr4_solib_ops::lm_addr_check (const solib &so, bfd *abfd) const > { > auto &li = get_lm_info_svr4 (so); > + if (li.l_addr_p) > + return li.l_addr; > > - if (!li.l_addr_p) > - { > - struct bfd_section *dyninfo_sect; > - CORE_ADDR l_addr, l_dynaddr, dynaddr; > + auto set_addr = [&li] (CORE_ADDR addr) > + { > + li.l_addr = addr; > + li.l_addr_p = true; > + return addr; > + }; > > - l_addr = li.l_addr_inferior; > + struct bfd_section *dyninfo_sect; > + CORE_ADDR l_addr, l_dynaddr, dynaddr; > > - if (!abfd || !this->has_lm_dynamic_from_link_map ()) > - goto set_addr; > + l_addr = li.l_addr_inferior; > + if (!abfd || !this->has_lm_dynamic_from_link_map ()) > + return set_addr (l_addr); > > - l_dynaddr = li.l_ld; > + l_dynaddr = li.l_ld; > > - dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic"); > - if (dyninfo_sect == NULL) > - goto set_addr; > + dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic"); > + if (dyninfo_sect == NULL) > + return set_addr (l_addr); > > - dynaddr = bfd_section_vma (dyninfo_sect); > + dynaddr = bfd_section_vma (dyninfo_sect); > + if (dynaddr + l_addr == l_dynaddr) > + return set_addr (l_addr); > > - if (dynaddr + l_addr != l_dynaddr) > - { > - CORE_ADDR align = 0x1000; > - CORE_ADDR minpagesize = align; > + CORE_ADDR align = 0x1000; > + CORE_ADDR minpagesize = align; > > - if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) > - { > - Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header; > - Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr; > - int i; > + if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) > + { > + Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header; > + Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr; > + int i; > > - align = 1; > + align = 1; > > - for (i = 0; i < ehdr->e_phnum; i++) > - if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align) > - align = phdr[i].p_align; > + for (i = 0; i < ehdr->e_phnum; i++) > + if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align) > + align = phdr[i].p_align; > > - minpagesize = get_elf_backend_data (abfd)->minpagesize; > - } > + minpagesize = get_elf_backend_data (abfd)->minpagesize; > + } > > - /* Turn it into a mask. */ > - align--; > + /* Turn it into a mask. */ > + align--; > > - /* If the changes match the alignment requirements, we > - assume we're using a core file that was generated by the > - same binary, just prelinked with a different base offset. > - If it doesn't match, we may have a different binary, the > - same binary with the dynamic table loaded at an unrelated > - location, or anything, really. To avoid regressions, > - don't adjust the base offset in the latter case, although > - odds are that, if things really changed, debugging won't > - quite work. > + /* If the changes match the alignment requirements, we > + assume we're using a core file that was generated by the > + same binary, just prelinked with a different base offset. > + If it doesn't match, we may have a different binary, the > + same binary with the dynamic table loaded at an unrelated > + location, or anything, really. To avoid regressions, > + don't adjust the base offset in the latter case, although > + odds are that, if things really changed, debugging won't > + quite work. > > - One could expect more the condition > - ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0) > - but the one below is relaxed for PPC. The PPC kernel supports > - either 4k or 64k page sizes. To be prepared for 64k pages, > - PPC ELF files are built using an alignment requirement of 64k. > - However, when running on a kernel supporting 4k pages, the memory > - mapping of the library may not actually happen on a 64k boundary! > + One could expect more the condition > + ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0) > + but the one below is relaxed for PPC. The PPC kernel supports > + either 4k or 64k page sizes. To be prepared for 64k pages, > + PPC ELF files are built using an alignment requirement of 64k. > + However, when running on a kernel supporting 4k pages, the memory > + mapping of the library may not actually happen on a 64k boundary! > > - (In the usual case where (l_addr & align) == 0, this check is > - equivalent to the possibly expected check above.) > + (In the usual case where (l_addr & align) == 0, this check is > + equivalent to the possibly expected check above.) > > - Even on PPC it must be zero-aligned at least for MINPAGESIZE. */ > + Even on PPC it must be zero-aligned at least for MINPAGESIZE. */ > > - l_addr = l_dynaddr - dynaddr; > + l_addr = l_dynaddr - dynaddr; > > - if ((l_addr & (minpagesize - 1)) == 0 > - && (l_addr & align) == ((l_dynaddr - dynaddr) & align)) > - { > - if (info_verbose) > - gdb_printf (_("Using PIC (Position Independent Code) " > - "prelink displacement %s for \"%s\".\n"), > - paddress (current_inferior ()->arch (), l_addr), > - so.name.c_str ()); > - } > - else > - { > - /* There is no way to verify the library file matches. prelink > - can during prelinking of an unprelinked file (or unprelinking > - of a prelinked file) shift the DYNAMIC segment by arbitrary > - offset without any page size alignment. There is no way to > - find out the ELF header and/or Program Headers for a limited > - verification if it they match. One could do a verification > - of the DYNAMIC segment. Still the found address is the best > - one GDB could find. */ > - > - warning (_(".dynamic section for \"%s\" " > - "is not at the expected address " > - "(wrong library or version mismatch?)"), > - so.name.c_str ()); > - } > - } > - > - set_addr: > - li.l_addr = l_addr; > - li.l_addr_p = 1; > + if ((l_addr & (minpagesize - 1)) == 0 > + && (l_addr & align) == ((l_dynaddr - dynaddr) & align)) > + { > + if (info_verbose) > + gdb_printf (_("Using PIC (Position Independent Code) " > + "prelink displacement %s for \"%s\".\n"), > + paddress (current_inferior ()->arch (), l_addr), > + so.name.c_str ()); > + } > + else > + { > + /* There is no way to verify the library file matches. prelink > + can during prelinking of an unprelinked file (or unprelinking > + of a prelinked file) shift the DYNAMIC segment by arbitrary > + offset without any page size alignment. There is no way to > + find out the ELF header and/or Program Headers for a limited > + verification if it they match. One could do a verification > + of the DYNAMIC segment. Still the found address is the best > + one GDB could find. */ > + > + warning (_(".dynamic section for \"%s\" " > + "is not at the expected address " > + "(wrong library or version mismatch?)"), > + so.name.c_str ()); > } > > - return li.l_addr; > + return set_addr (l_addr); > } > > struct svr4_so > > base-commit: 0855b93cfb911a4feab76a31bb1914982b3e6979