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

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