Blame SOURCES/gdb-rhbz2042664-fix-sect_index_data-internal-error.patch

ed07ac
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
ed07ac
From: Kevin Buettner <kevinb@redhat.com>
ed07ac
Date: Tue, 1 Feb 2022 11:32:48 -0700
ed07ac
Subject: gdb-rhbz2042664-fix-sect_index_data-internal-error.patch
ed07ac
ed07ac
;; Backport fix which fixes internal error due to libcc_s lacking a
ed07ac
;; .data section.
ed07ac
ed07ac
Fix GDB internal error by using text (instead of data) section offset
ed07ac
ed07ac
Fedora Rawhide is now using gcc-12.0.  As part of updating to the
ed07ac
gcc-12.0 package set, Rawhide is also now using a version of libgcc_s
ed07ac
which lacks a .data section.  This causes gdb to fail in the following
ed07ac
fashion while debugging a program (such as gdb) which uses libgcc_s:
ed07ac
ed07ac
    (top-gdb) run
ed07ac
    Starting program: rawhide-master/bld/gdb/gdb
ed07ac
    ...
ed07ac
    objfiles.h:467: internal-error: sect_index_data not initialized
ed07ac
    A problem internal to GDB has been detected,
ed07ac
    further debugging may prove unreliable.
ed07ac
    ...
ed07ac
ed07ac
I snipped the backtrace from the above output.  Instead, here's a
ed07ac
portion of a backtrace obtained using GDB's backtrace command.
ed07ac
(Obviously, in order to obtain it, I used a GDB which has been patched
ed07ac
with this commit.)
ed07ac
ed07ac
    #0  internal_error (
ed07ac
	file=0xc6a508 "gdb/objfiles.h", line=467,
ed07ac
	fmt=0xc6a4e8 "sect_index_data not initialized")
ed07ac
	at gdbsupport/errors.cc:51
ed07ac
    #1  0x00000000005f9651 in objfile::data_section_offset (this=0x4fa48f0)
ed07ac
	at gdb/objfiles.h:467
ed07ac
    #2  0x000000000097c5f8 in relocate_address (address=0x17244, objfile=0x4fa48f0)
ed07ac
	at gdb/stap-probe.c:1333
ed07ac
    #3  0x000000000097c630 in stap_probe::get_relocated_address (this=0xa1a17a0,
ed07ac
	objfile=0x4fa48f0)
ed07ac
	at gdb/stap-probe.c:1341
ed07ac
    #4  0x00000000004d7025 in create_exception_master_breakpoint_probe (
ed07ac
	objfile=0x4fa48f0)
ed07ac
	at gdb/breakpoint.c:3505
ed07ac
    #5  0x00000000004d7426 in create_exception_master_breakpoint ()
ed07ac
	at gdb/breakpoint.c:3575
ed07ac
    #6  0x00000000004efcc1 in breakpoint_re_set ()
ed07ac
	at gdb/breakpoint.c:13407
ed07ac
    #7  0x0000000000956998 in solib_add (pattern=0x0, from_tty=0, readsyms=1)
ed07ac
	at gdb/solib.c:1001
ed07ac
    #8  0x00000000009576a8 in handle_solib_event ()
ed07ac
	at gdb/solib.c:1269
ed07ac
    ...
ed07ac
ed07ac
The function 'relocate_address' in gdb/stap-probe.c attempts to do
ed07ac
its "relocation" by using objfile->data_section_offset().  That
ed07ac
method, data_section_offset() is defined as follows in objfiles.h:
ed07ac
ed07ac
  CORE_ADDR data_section_offset () const
ed07ac
  {
ed07ac
    return section_offsets[SECT_OFF_DATA (this)];
ed07ac
  }
ed07ac
ed07ac
The internal error occurs when the SECT_OFF_DATA macro finds that the
ed07ac
'sect_index_data' field is -1:
ed07ac
ed07ac
    #define SECT_OFF_DATA(objfile) \
ed07ac
	 ((objfile->sect_index_data == -1) \
ed07ac
	  ? (internal_error (__FILE__, __LINE__, \
ed07ac
			     _("sect_index_data not initialized")), -1)	\
ed07ac
	  : objfile->sect_index_data)
ed07ac
ed07ac
relocate_address() is obtaining the section offset in order to compute
ed07ac
a relocated address.  For some ABIs, such as the System V ABI, the
ed07ac
section offsets will all be the same.  So for those ABIs, it doesn't
ed07ac
matter which offset is used.  However, other ABIs, such as the FDPIC
ed07ac
ABI, will have different offsets for the various sections.  Thus, for
ed07ac
those ABIs, it is vital that this and other relocation code use the
ed07ac
correct offset.
ed07ac
ed07ac
In stap_probe::get_relocated_address, the address to which to add the
ed07ac
offset (thus forming the relocated address) is obtained via
ed07ac
this->get_address (); get_address is a getter for m_address in
ed07ac
probe.h.  It's documented/defined as follows (also in probe.h):
ed07ac
ed07ac
  /* The address where the probe is inserted, relative to
ed07ac
     SECT_OFF_TEXT.  */
ed07ac
  CORE_ADDR m_address;
ed07ac
ed07ac
(Thanks to Tom Tromey for this observation.)
ed07ac
ed07ac
So, based on this, the current use of data_section_offset /
ed07ac
SECT_OFF_DATA is wrong.  This relocation code should have been using
ed07ac
text_section_offset / SECT_OFF_TEXT all along.  That being the
ed07ac
case, I've adjusted the stap-probe.c relocation code accordingly.
ed07ac
ed07ac
Searching the sources turned up one other use of data_section_offset,
ed07ac
in gdb/dtrace-probe.c, so I've updated that code as well.  The same
ed07ac
reasoning presented above applies to this case too.
ed07ac
ed07ac
Summary:
ed07ac
ed07ac
	* gdb/dtrace-probe.c (dtrace_probe::get_relocated_address):
ed07ac
	Use method text_section_offset instead of data_section_offset.
ed07ac
	* gdb/stap-probe.c (relocate_address): Likewise.
ed07ac
ed07ac
diff --git a/gdb/dtrace-probe.c b/gdb/dtrace-probe.c
ed07ac
--- a/gdb/dtrace-probe.c
ed07ac
+++ b/gdb/dtrace-probe.c
ed07ac
@@ -684,7 +684,7 @@ dtrace_probe::is_enabled () const
ed07ac
 CORE_ADDR
ed07ac
 dtrace_probe::get_relocated_address (struct objfile *objfile)
ed07ac
 {
ed07ac
-  return this->get_address () + objfile->data_section_offset ();
ed07ac
+  return this->get_address () + objfile->text_section_offset ();
ed07ac
 }
ed07ac
 
ed07ac
 /* Implementation of the get_argument_count method.  */
ed07ac
diff --git a/gdb/stap-probe.c b/gdb/stap-probe.c
ed07ac
--- a/gdb/stap-probe.c
ed07ac
+++ b/gdb/stap-probe.c
ed07ac
@@ -1330,7 +1330,7 @@ stap_probe::parse_arguments (struct gdbarch *gdbarch)
ed07ac
 static CORE_ADDR
ed07ac
 relocate_address (CORE_ADDR address, struct objfile *objfile)
ed07ac
 {
ed07ac
-  return address + objfile->data_section_offset ();
ed07ac
+  return address + objfile->text_section_offset ();
ed07ac
 }
ed07ac
 
ed07ac
 /* Implementation of the get_relocated_address method.  */