Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Cc: Eli Zaretskii <eliz@gnu.org>, Simon Marchi <simon.marchi@efficios.com>
Subject: Re: [PATCHv10 3/5] gdb: introduce program_space::get_entry_point_info function
Date: Fri, 30 Oct 2026 12:11:28 -0400	[thread overview]
Message-ID: <b2f414ec-5371-456f-b001-e50a1868f0cc@simark.ca> (raw)
In-Reply-To: <e1c555ad4feb6b1c6fefd84b0d562e5fd232b457.1793268860.git.aburgess@redhat.com>

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<CORE_ADDR> 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<CORE_ADDR> 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<CORE_ADDR>
> +svr4_solib_ops::inferior_entry_point_address () const
> +{
> +  std::optional<gdb::byte_vector> 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<CORE_ADDR> 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<CORE_ADDR> 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<CORE_ADDR> 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

  reply	other threads:[~2026-10-30 16:12 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-08 18:14 [PATCH] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-06-11 19:24 ` Guinevere Larsen
2026-06-11 20:40   ` Andrew Burgess
2026-06-12 12:49     ` Guinevere Larsen
2026-06-11 21:59 ` [PATCHv2 0/3] " Andrew Burgess
2026-06-11 21:59   ` [PATCHv2 1/3] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-06-12 15:41     ` Pedro Alves
2026-06-11 21:59   ` [PATCHv2 2/3] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-06-12  6:07     ` Eli Zaretskii
2026-06-15 10:14       ` Andrew Burgess
2026-06-15 12:01         ` Eli Zaretskii
2026-06-12 15:41     ` Pedro Alves
2026-06-15 10:29       ` Andrew Burgess
2026-06-16 18:36         ` Pedro Alves
2026-06-23  9:46           ` Andrew Burgess
2026-06-23 10:20             ` Pedro Alves
2026-06-11 21:59   ` [PATCHv2 3/3] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-06-12 15:57     ` Pedro Alves
2026-06-16 19:47   ` [PATCHv3 0/4] " Andrew Burgess
2026-06-16 19:47     ` [PATCHv3 1/4] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-06-16 19:47     ` [PATCHv3 2/4] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-06-17 11:52       ` Eli Zaretskii
2026-06-16 19:47     ` [PATCHv3 3/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-06-16 19:47     ` [PATCHv3 4/4] gdb: cache program space entry point information Andrew Burgess
2026-06-23 10:47     ` [PATCHv4 0/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-06-23 10:47       ` [PATCHv4 1/4] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-06-23 10:47       ` [PATCHv4 2/4] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-06-23 10:47       ` [PATCHv4 3/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-06-23 10:47       ` [PATCHv4 4/4] gdb: cache program space entry point information Andrew Burgess
2026-06-25 15:26       ` [PATCHv5 0/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-06-25 15:26         ` [PATCHv5 1/4] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-06-25 15:26         ` [PATCHv5 2/4] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-06-25 15:26         ` [PATCHv5 3/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-06-25 15:26         ` [PATCHv5 4/4] gdb: cache program space entry point information Andrew Burgess
2026-07-10 14:24         ` [PATCHv6 0/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-07-10 14:24           ` [PATCHv6 1/4] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-07-10 14:24           ` [PATCHv6 2/4] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-07-10 15:39             ` Simon Marchi
2026-07-10 14:24           ` [PATCHv6 3/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-07-10 17:00             ` Simon Marchi
2026-07-10 17:01               ` Simon Marchi
2026-07-16 15:06               ` Andrew Burgess
2026-07-10 14:24           ` [PATCHv6 4/4] gdb: cache program space entry point information Andrew Burgess
2026-07-10 17:07             ` Simon Marchi
2026-07-18 13:11           ` [PATCHv7 0/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-07-18 13:11             ` [PATCHv7 1/4] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-07-18 13:11             ` [PATCHv7 2/4] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-07-18 13:11             ` [PATCHv7 3/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-07-18 13:11             ` [PATCHv7 4/4] gdb: cache program space entry point information Andrew Burgess
2026-07-20  9:52             ` [PATCHv8 0/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-07-20  9:52               ` [PATCHv8 1/4] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-07-20  9:52               ` [PATCHv8 2/4] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-08-12 15:44                 ` Simon Marchi
2026-07-20  9:52               ` [PATCHv8 3/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-08-12 17:53                 ` Simon Marchi
2026-08-20 10:32                   ` Andrew Burgess
2026-07-20  9:52               ` [PATCHv8 4/4] gdb: cache program space entry point information Andrew Burgess
2026-08-24 16:22               ` [PATCHv9 0/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-08-24 16:22                 ` [PATCHv9 1/4] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-08-24 16:22                 ` [PATCHv9 2/4] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-08-24 16:22                 ` [PATCHv9 3/4] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-10-27 14:33                   ` Simon Marchi
2026-08-24 16:22                 ` [PATCHv9 4/4] gdb: cache program space entry point information Andrew Burgess
2026-10-29 10:20                 ` [PATCHv10 0/5] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-10-29 10:20                   ` [PATCHv10 1/5] gdb: more 'const' in the multi-so-lib progspace code Andrew Burgess
2026-10-30 15:28                     ` Simon Marchi
2026-10-29 10:20                   ` [PATCHv10 2/5] gdb: rename program_space::entry_point_address* functions Andrew Burgess
2026-10-30 15:34                     ` Simon Marchi
2026-10-29 10:20                   ` [PATCHv10 3/5] gdb: introduce program_space::get_entry_point_info function Andrew Burgess
2026-10-30 16:11                     ` Simon Marchi [this message]
2026-10-29 10:20                   ` [PATCHv10 4/5] gdb: allow 'until' to work in outermost frame Andrew Burgess
2026-10-30 16:31                     ` Simon Marchi
2026-10-29 10:20                   ` [PATCHv10 5/5] gdb: cache program space entry point information Andrew Burgess
2026-10-30 17:19                     ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b2f414ec-5371-456f-b001-e50a1868f0cc@simark.ca \
    --to=simark@simark.ca \
    --cc=aburgess@redhat.com \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox