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

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