From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH v2 3/7] [gdb/tui] Improve section handling in tui_find_backward_disassembly_start_address
Date: Wed, 9 Sep 2026 11:48:45 +0200 [thread overview]
Message-ID: <20260909094849.2745086-4-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 a.out -ex "layout asm" -ex "disassemble _start"
...
which gives us the following asm window:
...
┌─────────────────────────────────────────────────────────────────────┐
│ 0x401040 <_start> endbr64 │
│ 0x401044 <_start+4> xor %ebp,%ebp │
│ 0x401046 <_start+6> mov %rdx,%r9 │
│ 0x401049 <_start+9> pop %rsi │
│ 0x40104a <_start+10> mov %rsp,%rdx │
│ 0x40104d <_start+13> and $0xfffffffffffffff0,%rsp │
│ 0x401051 <_start+17> push %rax │
│ 0x401052 <_start+18> push %rsp │
│ 0x401053 <_start+19> xor %r8d,%r8d │
│ 0x401056 <_start+22> xor %ecx,%ecx │
└─────────────────────────────────────────────────────────────────────┘
...
The _start function sits at the start of the text section:
...
Disassembly of section .plt:
...
0000000000401030 <puts@plt>:
401030: ff 25 ca 2f 00 00 jmp *0x2fca(%rip)
401036: 68 00 00 00 00 push $0x0
40103b: e9 e0 ff ff ff jmp 401020 <_init+0x20>
Disassembly of section .text:
0000000000401040 <_start>:
...
and using <Up Arrow> we can scroll to the last instruction of the plt section,
at 0x40103b.
To make this happen:
- tui_find_backward_disassembly_start_address is called with addr == 0x401040
- lookup_minimal_symbol_by_pc_section is called with addr - 1
- minimal symbol puts@plt is found
- its address is returned and used as starting point for disassembly
However, tui_find_backward_disassembly_start_address has two types of
information it uses: minimal symbols, and sections.
If we disable the minimal symbol handling in
tui_find_backward_disassembly_start_address, we're no longer able to scroll to
the instruction at 0x40103b.
The section handling code:
....
/* Find the section that ADDR is in, and look for the start of the
section. */
struct obj_section *section = find_pc_section (addr);
if (section != NULL)
return section->addr ();
...
looks up the section for 0x401040, which is .text, and it returns its start
address, which is also 0x401040, signalling that we cannot scroll to before
that address.
Fix this by detecting this case and using the previous section instead:
...
if (section != nullptr && section->addr () == addr)
section = find_pc_section (addr - 1);
...
This happens to work because there's no section hole between sections plt and
text.
---
gdb/tui/tui-disasm.c | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/gdb/tui/tui-disasm.c b/gdb/tui/tui-disasm.c
index 7dcb59e6264..3e525000efd 100644
--- a/gdb/tui/tui-disasm.c
+++ b/gdb/tui/tui-disasm.c
@@ -177,9 +177,22 @@ tui_find_backward_disassembly_start_address (CORE_ADDR addr)
else if (msym_prev.minsym != nullptr)
return msym_prev.value_address ();
- /* Find the section that ADDR is in, and look for the start of the
- section. */
- struct obj_section *section = find_pc_section (addr);
+ /* Find the first section with start address before ADDR, and use its start
+ address. The found section may be the one containing ADDR, or the one
+ before it. */
+ struct obj_section *section;
+ for (int offset = 0; offset <= 1; ++offset)
+ {
+ section = find_pc_section (addr - offset);
+ if (offset == 0 && section != nullptr && section->addr () == addr)
+ {
+ /* If ADDR is the start of its section, use ADDR - 1. */
+ continue;
+ }
+
+ break;
+ }
+
if (section != NULL)
return section->addr ();
--
2.51.0
next prev parent reply other threads:[~2026-09-09 9:51 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 ` Tom de Vries [this message]
2026-09-11 18:26 ` [PATCH v2 3/7] [gdb/tui] Improve section handling " 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 ` [PATCH v2 6/7] [gdb/tui] Handle section holes when forward disassembling Tom de Vries
2026-09-11 18:39 ` 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-4-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