[PATCHv2] gdb: fix segfault wrong section index for symbols

Message ID aa2270f65856fb5d4fbe417241498efbddccb3b7.camel@espressif.com
State New
Headers
Series [PATCHv2] gdb: fix segfault wrong section index for symbols |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Alexey Lapshin March 24, 2026, 12:51 p.m. UTC
  Set each symbol's section from the block's real PC range instead
of always using sect_index_text (the ".text" slot).  Blindly using
sect_index_text was wrong for executables where code lives only in
sections such as .iram.text / .flash.text and the canonical ".text"
section is missing.
That led to bad section_index / SAL handling and eventually a null
dereference in get_sal_arch: it does "if (sal.section)" but then uses
sal.section->objfile without checking. Objfile can be NULL even when
sal.section is not due to wrong section index.

Example layouts that trigger this class of bug (simplified):
  .iram.text {
    *(.text.iram*)
  }
  .flash.text {
    *(.text .text.*)
  }

Segfault looked like:
  0  in objfile::arch (this=0x0) at gdb/objfiles.h:517
  1  in get_sal_arch (sal=...) at gdb/breakpoint.c:7764
  2  in code_breakpoint::code_breakpoint (...) at gdb/breakpoint.c:8854
---
 gdb/buildsym.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

-- 
2.43.0
  

Patch

diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index 6dc079f29b1..f9398f6331d 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -244,7 +244,16 @@  buildsym_compunit::finish_block_internal
     {
       struct type *ftype = symbol->type ();
       symbol->set_value_block (block);
-      symbol->set_section_index (SECT_OFF_TEXT (m_objfile));
+      /* Section lookup uses the block's low PC (function entry).  */
+      for (obj_section &s : m_objfile->sections ())
+	{
+	  if (s.contains (start))
+	    {
+	      int symbol_section = &s - m_objfile->sections_start;
+	      symbol->set_section_index (symbol_section);
+	      break;
+	    }
+	}
       block->set_function (symbol);
 
       if (ftype->num_fields () <= 0)