From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id YOnXFNLB5GpwwhcAWB0awg (envelope-from ) for ; Fri, 30 Oct 2026 12:12:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1793376722; bh=uWKrdamjy6sCrzcRFDQKwKlQaUN+yVfENefTLpqHqCo=; h=Date:Subject:To:Cc:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=sXTQird25bMxRJxinU0JdFGqI69v1T9MXz3SzgmRNz8MC8AGafBTxjIDfA2I53BuJ KG0ZRjeqAS/bXnAeAslt6oEmXs6gtQCs+SKS/aU9fubaSzx4V8FI5AwiZ1tJu91BgF vu8jHY08NQueP5faU+lmn1u2FE+oLKee5FWajZ5E= Received: by simark.ca (Postfix, from userid 112) id 25EB7378002; Fri, 30 Oct 2026 12:12:02 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=IsT2QuUC; dkim-atps=neutral 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 B2E99378002 for ; Fri, 30 Oct 2026 12:11:59 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4D6394BAE7C3 for ; Fri, 30 Oct 2026 16:11:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D6394BAE7C3 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=IsT2QuUC Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 390E44BA9002 for ; Fri, 30 Oct 2026 16:11:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 390E44BA9002 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 390E44BA9002 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793376690; cv=none; b=Rsyik5PaClYCaFsB9wyC20rnU1t7K/FGYtg0hanRKQi3byDCwBqROsUAJeIfGwDCPkSAceDGpQ9aHSnOnVk03+AlB4zP3Ia63eFD0wdqHeQlkRG1IcvF/gWMGMhw9DLckXWko2JNZSbLfhgmmNtZVvHQrsQBkoGYs51jnE0BAQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793376690; c=relaxed/simple; bh=uWKrdamjy6sCrzcRFDQKwKlQaUN+yVfENefTLpqHqCo=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=hCr1CTuNONhcOdwWdqfB+WPRzIxpE06CT17ODmZeunsbMDYBVVL4HelDOXjfksQ4ireAzg4lkOWmlqiM7SA7je6KXPP6S1/6lbrbwuxBvlainU9lCqrzOLzfL6fpjnc6lW7Sm8anU/RWwZ+HuyFd+ppFWpr9uTQkhNUg8lNI/Lw= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=IsT2QuUC DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 390E44BA9002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1793376689; bh=uWKrdamjy6sCrzcRFDQKwKlQaUN+yVfENefTLpqHqCo=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=IsT2QuUCUYV9qVHPPNlhMaxt8sq2lz9c9RSHr7p6+Tz/0PlUuHTcmSbBPBet+7rLv eYFe8j1xqf+B3b5zPCtQooyj8LG/FcLOYQcgOgciaoyVQyZ5xwdxVQjiDjb863AY/I xpoaGQ5xe90oSsZSMwodV6AYxDBvp7i6crW9dn6E= Received: by simark.ca (Postfix) id 9E4E11E023; Fri, 30 Oct 2026 12:11:28 -0400 (EDT) Message-ID: Date: Fri, 30 Oct 2026 12:11:28 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCHv10 3/5] gdb: introduce program_space::get_entry_point_info function To: Andrew Burgess , gdb-patches@sourceware.org Cc: Eli Zaretskii , Simon Marchi References: Content-Language: fr From: Simon Marchi In-Reply-To: Content-Type: text/plain; charset=UTF-8 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 10/29/26 6:20 AM, Andrew Burgess wrote: > I noticed that when debugging a dynamically linked executable, if I > started the inferior with 'starti' then used 'bt' I would see some > bogus frames: > > (gdb) starti > Starting program: /tmp/hello > > Program stopped. > 0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2 > (gdb) bt > #0 0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2 > #1 0x0000000000000001 in ?? () > #2 0x00007fffffffac13 in ?? () > #3 0x0000000000000000 in ?? () > (gdb) > > This surprised me as 'backtrace past-entry' was off: > > (gdb) show backtrace past-entry > Whether backtraces should continue past the entry point of a program is off. > > I was expecting GDB to stop the backtrace at the inferior's entry > address. > > Frame unwinding starts in get_prev_frame, and in here we find this > block: > > if (this_frame->level >= 0 > && get_frame_type (this_frame) == NORMAL_FRAME > && !user_set_backtrace_options.backtrace_past_entry > && frame_pc.has_value () > && inside_entry_func (this_frame)) > { > frame_debug_got_null_frame (this_frame, "inside entry func"); > return NULL; > } > > Which uses inside_entry_func to terminate the backtrace when we reach > the entry frame. The inside_entry_func function calls > current_program_space->exec_entry_point_address_if_available and uses > the result to figure out if we are in the entry frame. > > And here the problem becomes obvious, we are only checking if we are > in the "entry frame" for the main executable, not for the inferior as > a whole. And indeed, if I compile the same test program as a static > binary, where there will be no run-time linker, and the entry point of > the executable is the entry point for the inferior, then the 'bt' > problem I saw above goes away. > > This suggests, I think, that we need to track two different entry > addresses, the entry address for the executable file, and the entry > address for the entire inferior. Then, for dynamically linked > executables, these two addresses can be different, the former will > still be the same address within the main executable file, while the > latter will be the address of the entry point within the run-time > linker. > > If we had this information then we could extend inside_entry_func to > check both addresses, and the 'bt' problem seen above will be > resolved. > > To make this information available I added a new solib_ops method, > solib_ops::inferior_entry_point_address, for svr4 targets this figures > out if the main executable is dynamically linked, and if it is, uses > the AT_BASE auxv entry and the entry address pulled from the ELF > header to compute the inferior entry address. > > I then added program_space::get_entry_point_info, which returns a > struct containing the two entry point addresses, one comes from the > new solib_ops method, and one comes from the existing method > program_space::exec_entry_point_address_if_available. > > The program_space::get_entry_point_info function loops over every > solib_ops in the program_space and calls inferior_entry_point_address > on each. The first to return a non-empty value "wins" and this is > taken as the inferior's entry address. My thinking here is that in a > multi-solib_ops setup only one will be the solib_ops for the > environment that actually started the inferior; this will be the > solib_ops that returns the entry address. Any additional solib_ops > will be supporting the primary solib_ops and will not try to provide > the inferior entry address. This is a bit of a guess at how such a > multi-solib_ops system would work, as this whole mechanism is rather > new to GDB. I'm sure this can be adjusted in the future though if it > turns out that we want multiple solib_ops to provide entry addresses. This sounds perfectly fine. > @@ -370,6 +371,63 @@ program_space::exec_entry_point_address () const > > /* See progspace.h. */ > > +entry_point_info > +program_space::get_entry_point_info () const > +{ > + std::optional exec_entry_address > + = this->exec_entry_point_address_if_available (); > + > + /* Ask the solib_ops for the inferior entry address. The first solib_ops > + to provide an answer "wins". The assumption is that only one > + solib_ops will represent the part of the system that started the > + process and knows the actual process entry address. */ > + std::optional inferior_entry_address; > + for (const solib_ops_up &solib_ops : this->solib_ops ()) > + { > + inferior_entry_address = solib_ops->inferior_entry_point_address (); > + if (inferior_entry_address.has_value ()) > + break; > + } > + > + return entry_point_info (std::move (inferior_entry_address), > + std::move (exec_entry_address)); Unnecessary std::moves. > +} > + > +/* Implement the 'maint info entry-address' command. */ > + > +static void > +maintenance_info_entry_address (const char *args, int from_tty) > +{ > + if (args != nullptr && *args != '\0') > + error (_("unknown argument: %s"), args); > + > + struct gdbarch *gdbarch = get_current_arch (); > + > + const entry_point_info &ep_info > + = current_program_space->get_entry_point_info (); > + > + /* Display a single entry address ADDR with TITLE as a description. */ > + auto display_entry_address > + = [&gdbarch] (const char *title, gdbarch is a pointer already, so you can capture by value. > diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c > index 40b0fc55113..241a4e1a46f 100644 > --- a/gdb/solib-svr4.c > +++ b/gdb/solib-svr4.c > @@ -3787,6 +3787,91 @@ svr4_solib_ops::get_solibs_in_ns (int nsid) const > return ns_solibs; > } > > +/* See solib.h. */ > + > +std::optional > +svr4_solib_ops::inferior_entry_point_address () const > +{ > + std::optional interp_name_holder > + = svr4_find_program_interpreter (); > + > + /* No interpreter means this is a static executable. Ask the > + program_space for the entry address within the main executable. */ > + if (!interp_name_holder.has_value ()) > + return m_pspace->exec_entry_point_address_if_available (); It's really minor, but maybe we could slightly reduce the scope of responsibility of the inferior_entry_point_address method to only returns a value if there is some kind of loader that gets executed before the main executable (I would rename it to loader_entry_point_address). If the executable is static, we would return an empty optional. The caller already calls program_space::exec_entry_point_address_if_available anyway, so it is a bit redundant to have the solib_ops have to handle the fallback too. Then we can have a simple method of entry_point_info whose job is to return the first instruction executed by the inferior. I imagine entry_point_info could become: /* The entry address of the program loader, if any. See the member variable definition below for more details. */ std::optional loader_entry_address () const { return m_loader_entry_address; } /* The entry address within the main executable. See the member variable definition below for more details. */ std::optional exec_entry_address () const { return m_exec_entry_address; } /* The address of the first instruction executed by the inferior: the loader entry address if there is one, else the executable entry address. */ std::optional inferior_entry_address () const { if (m_loader_entry_address) return *m_loader_entry_address; if (m_exec_entry_address) *m_exec_entry_address; return std::nullopt; } > + > + /* For a dynamically linked executable the inferior's true entry point > + is the entry point of the dynamic linker. We find this using the > + AT_BASE auxiliary vector entry, which gives the dynamic linker's > + load address, combined with e_entry address pulled from the > + inferior. We assume that the ELF header can be read from AT_BASE. */ > + CORE_ADDR at_base_addr; > + if (target_auxv_search (AT_BASE, &at_base_addr) <= 0) > + return {}; > + > + /* Determine ELF architecture type. Use the size of a program header > + entry to determine which ELF header we can expect to find. */ > + size_t e_entry_offset = 0; > + size_t e_entry_size = 0; > + CORE_ADDR at_phent; > + if (target_auxv_search (AT_PHENT, &at_phent) <= 0) > + return {}; > + if (at_phent == sizeof (Elf32_External_Phdr)) > + { > + e_entry_offset = offsetof (Elf32_External_Ehdr, e_entry); > + e_entry_size = 4; > + } > + else if (at_phent == sizeof (Elf64_External_Phdr)) > + { > + e_entry_offset = offsetof (Elf64_External_Ehdr, e_entry); > + e_entry_size = 8; > + } > + else > + return {}; > + > + /* Architecture of the current inferior. */ > + gdbarch *gdbarch = current_inferior ()->arch (); > + > + /* Read the entry address from the ELF header at AT_BASE_ADDR. */ > + CORE_ADDR e_entry; Declare below, where initialized? > + gdb_byte buffer[sizeof (CORE_ADDR)]; > + if (target_read_memory (at_base_addr + e_entry_offset, buffer, e_entry_size)) > + return {}; > + bfd_endian byte_order = gdbarch_byte_order (gdbarch); > + e_entry = extract_unsigned_integer (buffer, e_entry_size, byte_order); > + > + /* Ensure AT_BASE_ADDR has proper sign in its possible upper bits so > + that `+ at_base_addr' will overflow CORE_ADDR width not creating > + invalid addresses like 0x101234567 for 32bit inferiors on 64bit > + GDB. */ > + int addr_bit = gdbarch_addr_bit (gdbarch); > + if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) > + { > + CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit; > + > + gdb_assert (at_base_addr < space_size); > + > + /* E_ENTRY exceeding SPACE_SIZE would be for prelinked > + 64bit ld.so with 32bit executable, it should not happen. */ > + if (e_entry < space_size > + && e_entry + at_base_addr >= space_size) > + at_base_addr -= space_size; > + } I must admit that I don't understand this address handling. But in a pevious review I asked if we could share this computation with enable_break (from which it seems to have been copied), I haven't seen a follow up on that. Simon