Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH v2 6/7] [gdb/tui] Handle section holes when forward disassembling
Date: Wed,  9 Sep 2026 11:48:48 +0200	[thread overview]
Message-ID: <20260909094849.2745086-7-tdevries@suse.de> (raw)
In-Reply-To: <20260909094849.2745086-1-tdevries@suse.de>

Consider a hello world:
...
$ gcc hello.c -g
...
and gdb setup like this:
...
$ gdb -q a.out -ex "layout asm" -ex "disassemble _init"
...
which gives us the following asm window:
...
┌─────────────────────────────────────────────────────────────────────┐
│   0x401000 <_init>        endbr64                                   │
│   0x401004 <_init+4>      sub    $0x8,%rsp                          │
│   0x401008 <_init+8>      mov    0x2fd1(%rip),%rax        # 0x403fe0│
│   0x40100f <_init+15>     test   %rax,%rax                          │
│   0x401012 <_init+18>     je     0x401016 <_init+22>                │
│   0x401014 <_init+20>     call   *%rax                              │
│   0x401016 <_init+22>     add    $0x8,%rsp                          │
│   0x40101a <_init+26>     ret                                       │
│                                                                     │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘
...

We cannot scroll from the init section to the plt section, because there's a
section hole in between:
...
Disassembly of section .init:

0000000000401000 <_init>:
  ...
  40101a:       c3                      ret

Disassembly of section .plt:

0000000000401020 <puts@plt-0x10>:

  401020:       ff 35 ca 2f 00 00       push   0x2fca(%rip)
...
and the section hole is causing MEMORY_ERRORs in tui_disassemble.

However, that all changes if instead we do:
...
$ gdb -q a.out -ex start -ex "layout asm" -ex "disassemble _init"
...
and get:
...
┌─────────────────────────────────────────────────────────────────────┐
│   0x401000 <_init>        endbr64                                   │
│   0x401004 <_init+4>      sub    $0x8,%rsp                          │
│   0x401008 <_init+8>      mov    0x2fd1(%rip),%rax        # 0x403fe0│
│   0x40100f <_init+15>     test   %rax,%rax                          │
│   0x401012 <_init+18>     je     0x401016 <_init+22>                │
│   0x401014 <_init+20>     call   *%rax                              │
│   0x401016 <_init+22>     add    $0x8,%rsp                          │
│   0x40101a <_init+26>     ret                                       │
│   0x40101b                add    %al,(%rax)                         │
│   0x40101d                add    %al,(%rax)                         │
└─────────────────────────────────────────────────────────────────────┘
...

The section hole is no longer causing MEMORY_ERRORs, and we can scroll past it
into the plt section.

Achieve something similar in the initial setup (without start) by skipping
over the section hole, getting us:
...
┌─────────────────────────────────────────────────────────────────────┐
│   0x401000 <_init>        endbr64                                   │
│   0x401004 <_init+4>      sub    $0x8,%rsp                          │
│   0x401008 <_init+8>      mov    0x2fd1(%rip),%rax        # 0x403fe0│
│   0x40100f <_init+15>     test   %rax,%rax                          │
│   0x401012 <_init+18>     je     0x401016 <_init+22>                │
│   0x401014 <_init+20>     call   *%rax                              │
│   0x401016 <_init+22>     add    $0x8,%rsp                          │
│   0x40101a <_init+26>     ret                                       │
│   0x401020                push   0x2fca(%rip)        # 0x403ff0     │
│   0x401026                jmp    *0x2fcc(%rip)        # 0x403ff8    │
└─────────────────────────────────────────────────────────────────────┘
...
allowing to scroll (further) into the plt section.
---
 gdb/tui/tui-disasm.c | 51 +++++++++++++++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 15 deletions(-)

diff --git a/gdb/tui/tui-disasm.c b/gdb/tui/tui-disasm.c
index 3e525000efd..d96048dc1de 100644
--- a/gdb/tui/tui-disasm.c
+++ b/gdb/tui/tui-disasm.c
@@ -112,22 +112,43 @@ tui_disassemble (struct gdbarch *gdbarch,
     {
       tui_asm_line tal;
 
-      /* Save the instruction address.  */
-      tal.addr = pc;
-
-      try
-	{
-	  pc += gdb_print_insn (gdbarch, pc, stream, NULL);
-	}
-      catch (const gdb_exception_error &except)
+      bool retry;
+      do
 	{
-	  /* If PC points to an invalid address then we'll catch a
-	     MEMORY_ERROR here, this should stop the disassembly, but
-	     otherwise is fine.  */
-	  if (except.error != MEMORY_ERROR)
-	    throw;
-	  return pc;
+	  retry = false;
+
+	  /* Save the instruction address.  */
+	  tal.addr = pc;
+
+	  try
+	    {
+	      pc += gdb_print_insn (gdbarch, pc, stream, NULL);
+	    }
+	  catch (const gdb_exception_error &except)
+	    {
+	      /* If PC points to an invalid address then we'll catch a
+		 MEMORY_ERROR here, this should stop the disassembly, but
+		 otherwise is fine.  */
+	      if (except.error != MEMORY_ERROR)
+		throw;
+
+	      struct obj_section *next;
+	      struct obj_section *section
+		= find_pc_section (pc, nullptr, &next);
+	      if (section == nullptr
+		  && next != nullptr
+		  && ((bfd_section_flags (next->the_bfd_section) & SEC_ALLOC)
+		      != 0))
+		{
+		  /* Skip over section hole.  */
+		  pc = next->addr ();
+		  retry = true;
+		}
+	      else
+		return pc;
+	    }
 	}
+      while (retry);
 
       /* If that's all we need, continue.  */
       if (addr_size == nullptr)
@@ -563,7 +584,7 @@ run_tests ()
 
       /* Check that tui_find_disassembly_address robustly handles the case of
 	 being passed a PC for which gdb_print_insn throws a MEMORY_ERROR.  */
-      SELF_CHECK (tui_find_disassembly_address (gdbarch, 0, 1) == 0);
+      tui_find_disassembly_address (gdbarch, 0, 1);
       SELF_CHECK (tui_find_disassembly_address (gdbarch, 0, -1) == 0);
     }
 }
-- 
2.51.0


  parent reply	other threads:[~2026-09-09  9:49 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-09  9:48 [PATCH v2 0/7] [gdb/tui] Some section hole handling fixes Tom de Vries
2026-09-09  9:48 ` [PATCH v2 1/7] [gdb/tui] Improve tui_disassemble comment Tom de Vries
2026-09-09  9:48 ` [PATCH v2 2/7] [gdb/tui] Fix underflow in tui_find_backward_disassembly_start_address Tom de Vries
2026-09-11 18:12   ` Tom Tromey
2026-09-11 18:14     ` Tom Tromey
2026-09-09  9:48 ` [PATCH v2 3/7] [gdb/tui] Improve section handling " Tom de Vries
2026-09-11 18:26   ` Tom Tromey
2026-09-14  7:31     ` Tom de Vries
2026-09-09  9:48 ` [PATCH v2 4/7] [gdb] replace bsearch with std::lower_bound in find_pc_section Tom de Vries
2026-09-11 18:19   ` Tom Tromey
2026-09-14  7:34     ` Tom de Vries
2026-09-09  9:48 ` [PATCH v2 5/7] [gdb] Add prev/next params to find_pc_section Tom de Vries
2026-09-11 18:40   ` Tom Tromey
2026-09-09  9:48 ` Tom de Vries [this message]
2026-09-11 18:39   ` [PATCH v2 6/7] [gdb/tui] Handle section holes when forward disassembling Tom Tromey
2026-09-09  9:48 ` [PATCH v2 7/7] [gdb/tui] Handle section holes when backward disassembling Tom de Vries
2026-09-11 18:48   ` Tom Tromey
2026-09-11 18:52 ` [PATCH v2 0/7] [gdb/tui] Some section hole handling fixes Tom Tromey

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=20260909094849.2745086-7-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /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