[RFA/commit 2/2] pspace != NULL failed assertion on ppc-aix

Joel Brobecker
2012-04-16T21:14:31Z

This patch fixes the following problem:

% gdb foo
(gdb) b main
/[...]/progspace.c:216: internal-error: set_current_program_space: Assertion `pspace != NULL' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Quit this debugging session? (y or n)

The problem happens when we try to get the program space from
a minimal symbol. For that, we go through the minimal symbol's
obj_section->objfile->pspace. But the minimal symbol's obj_section
is not set, and thus we somehow get a NULL program space. And
the reason why the obj_section is not set is because the XCOFF
reader did not pass the bfd_section when calling minsyms.c's
prim_record_minimal_symbol_and_info.

This patch fixes the problem by making sure that the obj_section
part of all minimal symbols is correctly set.

There are several layers to this patch:

1. The RECORD_MINIMAL_SYMBOL macro is replaced by a function
(record_minimal_symbol). The macro's comment explains that
it was designed as macro for performance, but we have to make
two function calls performing two searches, together with
a call to prim_record_minimal_symbol_and_info. I do not think
that the extra overhead caused by an additional function call
is going to have a noticeable impact.

This allows this function to perform the searches for both
SECT_OFF_* index and BFD section using one search instead
of two. We could have done that with a macro, but the size
of the macro would have become unreasonable.

2. The patch introduces a new function "xcoff_secnum_to_sections",
which performs the search needed in (1) above. It also
reimplements secnum_to_section and secnum_to_bfd_section
on top of that new function.

3. Adjusts all the calls to RECORD_MINIMAL_SYMBOL and
prim_record_minimal_symbol_and_info accordingly.

4. Changes the "last_csect_sec" variable in scan_xcoff_symtab to
contain the XCOFF section number rather than the SECT_OFF_*
index. This is necessary because we need to get both the
associated SECT_OFF_* index as well its BFD section. We cannot
find the BFD section from the SECT_OFF_* index, so we keep
the XCOFF index instead, and transform it into the SECT_OFF_*
index on demand.

gdb/ChangeLog:

* xcoffread.c (xcoff_secnum_to_sections): New function.
(secnum_to_section, secnum_to_bfd_section): Reimplement
using xcoff_secnum_to_sections. Rename "secnum" parameter
into "n_scnum".
(RECORD_MINIMAL_SYMBOL): Delete.
(record_minimal_symbol): New function.
(scan_xcoff_symtab): Replace uses of RECORD_MINIMAL_SYMBOL
by call to record_minimal_symbol and set misc_func_recorded
to 1. Set last_csect_sec to the XCOFF section index instead
of GDB's section_offset index. Update calls to
prim_record_minimal_symbol_and_info to pass the BFD section
as well.

Tested on ppc-aix using AdaCore's testsuite. I will also test using
the official testsuite, but I think I will have to go back to the
sources prior to Keith's linespec rewrite to be able to get one good
run.

---
gdb/xcoffread.c | 143 ++++++++++++++++++++++++++++++++++---------------------
1 files changed, 88 insertions(+), 55 deletions(-)

diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
index fcabddb..1a1b5de 100644
--- a/gdb/xcoffread.c
+++ b/gdb/xcoffread.c
@@ -272,36 +272,53 @@ find_targ_sec (bfd *abfd, asection *sect, void *obj)
}
}

-/* Return the section number (SECT_OFF_*) that CS points to. */
-static int
-secnum_to_section (int secnum, struct objfile *objfile)
-{
- int off = SECT_OFF_TEXT (objfile);
+/* Search all BFD sections for the section whose target_index is
+ equal to N_SCNUM. Set *BFD_SECT to that section. The section's
+ associated index in the objfile's section_offset table is also
+ stored in *SECNUM.

- asection *sect = NULL;
+ If no match is found, *BFD_SECT is set to NULL, and *SECNUM
+ is set to the text section's number. */
+
+static void
+xcoff_secnum_to_sections (int n_scnum, struct objfile *objfile,
+ asection **bfd_sect, int *secnum)
+{
struct find_targ_sec_arg args;
- args.targ_index = secnum;
- args.resultp = &off;
- args.bfd_sect = &sect;
+
+ args.targ_index = n_scnum;
+ args.resultp = secnum;
+ args.bfd_sect = bfd_sect;
args.objfile = objfile;
+
+ *bfd_sect = NULL;
+ *secnum = SECT_OFF_TEXT (objfile);
+
bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
- return off;
}

-/* Return the BFD section that CS points to. */
+/* Return the section number (SECT_OFF_*) that N_SCNUM points to. */
+
+static int
+secnum_to_section (int n_scnum, struct objfile *objfile)
+{
+ int secnum;
+ asection *ignored;
+
+ xcoff_secnum_to_sections (n_scnum, objfile, &ignored, &secnum);
+ return secnum;
+}
+
+/* Return the BFD section that N_SCNUM points to. */
+
static asection *
-secnum_to_bfd_section (int secnum, struct objfile *objfile)
+secnum_to_bfd_section (int n_scnum, struct objfile *objfile)
{
- int off = SECT_OFF_TEXT (objfile);
+ int ignored;
+ asection *bfd_sect;

- asection *sect = NULL;
- struct find_targ_sec_arg args;
- args.targ_index = secnum;
- args.resultp = &off;
- args.bfd_sect = &sect;
- args.objfile = objfile;
- bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
- return sect;
+ xcoff_secnum_to_sections (n_scnum, objfile, &bfd_sect, &ignored);
+ return bfd_sect;
}
\f
/* add a given stab string into given stab vector. */
@@ -877,21 +894,34 @@ enter_line_range (struct subfile *subfile, unsigned beginoffset,
This function can read past the end of the symbol table
(into the string table) but this does no harm. */

-/* Reading symbol table has to be fast! Keep the followings as macros, rather
- than functions. */
-
-#define RECORD_MINIMAL_SYMBOL(NAME, ADDR, TYPE, SECTION, OBJFILE) \
-{ \
- const char *namestr; \
- \
- namestr = (NAME); \
- if (namestr[0] == '.') ++namestr; \
- prim_record_minimal_symbol_and_info (namestr, (ADDR), (TYPE), \
- (SECTION), (asection *)NULL, \
- (OBJFILE)); \
- misc_func_recorded = 1; \
-}
+/* Create a new minimal symbol (using prim_record_minimal_symbol_and_info).
+
+ Arguments are:
+
+ NAME - the symbol's name (but if NAME starts with a period, that
+ leading period is discarded).
+ ADDRESS - the symbol's address.
+ MS_TYPE - the symbol's type.
+ N_SCNUM - the symbol's XCOFF section number.
+ OBJFILE - the objfile associated with the minimal symbol. */
+
+static void
+record_minimal_symbol (const char *name, CORE_ADDR address,
+ enum minimal_symbol_type ms_type,
+ int n_scnum,
+ struct objfile *objfile)
+{
+ struct find_targ_sec_arg args;
+ int secnum;
+ asection *bfd_sect;

+ if (name[0] == '.')
+ ++name;
+
+ xcoff_secnum_to_sections (n_scnum, objfile, &bfd_sect, &secnum);
+ prim_record_minimal_symbol_and_info (name, address, ms_type,
+ secnum, bfd_sect, objfile);
+}

/* xcoff has static blocks marked in `.bs', `.es' pairs. They cannot be
nested. At any given time, a symbol can only be in one static block.
@@ -2277,10 +2307,10 @@ scan_xcoff_symtab (struct objfile *objfile)

if (!misc_func_recorded)
{
- RECORD_MINIMAL_SYMBOL
+ record_minimal_symbol
(last_csect_name, last_csect_val,
- mst_text, last_csect_sec,
- objfile);
+ mst_text, last_csect_sec, objfile);
+ misc_func_recorded = 1;
}

if (pst != NULL)
@@ -2312,8 +2342,7 @@ scan_xcoff_symtab (struct objfile *objfile)
{
last_csect_name = namestring;
last_csect_val = symbol.n_value;
- last_csect_sec =
- secnum_to_section (symbol.n_scnum, objfile);
+ last_csect_sec = symbol.n_scnum;
}
if (pst != NULL)
{
@@ -2337,7 +2366,8 @@ scan_xcoff_symtab (struct objfile *objfile)
(namestring, symbol.n_value,
sclass == C_HIDEXT ? mst_file_data : mst_data,
secnum_to_section (symbol.n_scnum, objfile),
- NULL, objfile);
+ secnum_to_bfd_section (symbol.n_scnum, objfile),
+ objfile);
break;

case XMC_TC0:
@@ -2371,11 +2401,13 @@ scan_xcoff_symtab (struct objfile *objfile)
if (first_fun_line_offset == 0 && symbol.n_numaux > 1)
first_fun_line_offset =
main_aux[0].x_sym.x_fcnary.x_fcn.x_lnnoptr;
- RECORD_MINIMAL_SYMBOL
- (namestring, symbol.n_value,
- sclass == C_HIDEXT ? mst_file_text : mst_text,
- secnum_to_section (symbol.n_scnum, objfile),
- objfile);
+ {
+ record_minimal_symbol
+ (namestring, symbol.n_value,
+ sclass == C_HIDEXT ? mst_file_text : mst_text,
+ symbol.n_scnum, objfile);
+ misc_func_recorded = 1;
+ }
break;

case XMC_GL:
@@ -2386,11 +2418,10 @@ scan_xcoff_symtab (struct objfile *objfile)
mst_solib_trampoline symbol. When we lookup mst
symbols, we will choose mst_text over
mst_solib_trampoline. */
- RECORD_MINIMAL_SYMBOL
+ record_minimal_symbol
(namestring, symbol.n_value,
- mst_solib_trampoline,
- secnum_to_section (symbol.n_scnum, objfile),
- objfile);
+ mst_solib_trampoline, symbol.n_scnum, objfile);
+ misc_func_recorded = 1;
break;

case XMC_DS:
@@ -2413,7 +2444,8 @@ scan_xcoff_symtab (struct objfile *objfile)
(namestring, symbol.n_value,
sclass == C_HIDEXT ? mst_file_data : mst_data,
secnum_to_section (symbol.n_scnum, objfile),
- NULL, objfile);
+ secnum_to_bfd_section (symbol.n_scnum, objfile),
+ objfile);
break;
}
break;
@@ -2430,7 +2462,8 @@ scan_xcoff_symtab (struct objfile *objfile)
(namestring, symbol.n_value,
sclass == C_HIDEXT ? mst_file_bss : mst_bss,
secnum_to_section (symbol.n_scnum, objfile),
- NULL, objfile);
+ secnum_to_bfd_section (symbol.n_scnum, objfile),
+ objfile);
break;
}
break;
@@ -2456,9 +2489,9 @@ scan_xcoff_symtab (struct objfile *objfile)
it as a function. This will take care of functions like
strcmp() compiled by xlc. */

- RECORD_MINIMAL_SYMBOL
- (last_csect_name, last_csect_val,
- mst_text, last_csect_sec, objfile);
+ record_minimal_symbol (last_csect_name, last_csect_val,
+ mst_text, last_csect_sec, objfile);
+ misc_func_recorded = 1;
}

if (pst)
--
1.6.5.rc2