Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [patch, dwarf2] avoid segfault on missing directory table
@ 2015-05-08 17:10 Sandra Loosemore
  2015-05-08 17:23 ` Joel Brobecker
  0 siblings, 1 reply; 2+ messages in thread
From: Sandra Loosemore @ 2015-05-08 17:10 UTC (permalink / raw)
  To: gdb-patches; +Cc: Yao Qi

[-- Attachment #1: Type: text/plain, Size: 731 bytes --]

A while back we were given a nios2-elf executable that caused GDB to 
segfault while reading its debug information.  The binary turned out to 
have invalid DWARF-2 information in the .debug_line section: although 
the file name table had references to entries in the directory table, 
the directory table itself was empty.  The executable was produced by 
some very old version of GCC (4.1.2?), and we verified that more current 
toolchain versions don't produce such bad debug information any more. 
But, since it's generally a bad thing for GDB to segfault, here is a 
patch that makes the DWARF-2 reader more robust by making sure the 
directory table is non-NULL before trying to access entries in it.

OK to commit?

-Sandra


[-- Attachment #2: gdb-dwarf.log --]
[-- Type: text/x-log, Size: 392 bytes --]

2015-05-07  Yao Qi  <yao@codesourcery.com>
	    Sandra Loosemore  <sandra@codesourcery.com>

	Avoid segfault on missing directory table.

	gdb/
	* dwarf2read.c (setup_type_unit_groups): Do NULL pointer check
	to 'lh->include_dirs' before accessing to it.
	(psymtab_include_file_name): Likewise.
	(dwarf_decode_lines_1): Likewise.
	(dwarf_decode_lines): Likewise.
	(file_file_name): Likewise.

[-- Attachment #3: gdb-dwarf.patch --]
[-- Type: text/x-patch, Size: 2328 bytes --]

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 4982922..e2ea7e2 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -9320,7 +9320,7 @@ setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
 	  const char *dir = NULL;
 	  struct file_entry *fe = &lh->file_names[i];
 
-	  if (fe->dir_index)
+	  if (fe->dir_index && lh->include_dirs != NULL)
 	    dir = lh->include_dirs[fe->dir_index - 1];
 	  dwarf2_start_subfile (fe->name, dir);
 
@@ -17396,7 +17396,7 @@ psymtab_include_file_name (const struct line_header *lh, int file_index,
   char *copied_name = NULL;
   int file_is_pst;
 
-  if (fe.dir_index)
+  if (fe.dir_index && lh->include_dirs != NULL)
     dir_name = lh->include_dirs[fe.dir_index - 1];
 
   if (!IS_ABSOLUTE_PATH (include_name)
@@ -17595,7 +17595,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
           struct file_entry *fe = &lh->file_names[file - 1];
           const char *dir = NULL;
 
-          if (fe->dir_index)
+          if (fe->dir_index && lh->include_dirs != NULL)
             dir = lh->include_dirs[fe->dir_index - 1];
 
 	  dwarf2_start_subfile (fe->name, dir);
@@ -17815,7 +17815,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
                 else
                   {
                     fe = &lh->file_names[file - 1];
-                    if (fe->dir_index)
+                    if (fe->dir_index && lh->include_dirs != NULL)
                       dir = lh->include_dirs[fe->dir_index - 1];
                     if (!decode_for_pst_p)
                       {
@@ -17958,7 +17958,7 @@ dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
 	  struct file_entry *fe;
 
 	  fe = &lh->file_names[i];
-	  if (fe->dir_index)
+	  if (fe->dir_index && lh->include_dirs != NULL)
 	    dir = lh->include_dirs[fe->dir_index - 1];
 	  dwarf2_start_subfile (fe->name, dir);
 
@@ -20640,7 +20640,8 @@ file_file_name (int file, struct line_header *lh)
     {
       struct file_entry *fe = &lh->file_names[file - 1];
 
-      if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
+      if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
+	  || lh->include_dirs == NULL)
         return xstrdup (fe->name);
       return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
 		     fe->name, NULL);

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [patch, dwarf2] avoid segfault on missing directory table
  2015-05-08 17:10 [patch, dwarf2] avoid segfault on missing directory table Sandra Loosemore
@ 2015-05-08 17:23 ` Joel Brobecker
  0 siblings, 0 replies; 2+ messages in thread
From: Joel Brobecker @ 2015-05-08 17:23 UTC (permalink / raw)
  To: Sandra Loosemore; +Cc: gdb-patches, Yao Qi

> 2015-05-07  Yao Qi  <yao@codesourcery.com>
> 	    Sandra Loosemore  <sandra@codesourcery.com>
> 
> 	Avoid segfault on missing directory table.
> 
> 	gdb/
> 	* dwarf2read.c (setup_type_unit_groups): Do NULL pointer check
> 	to 'lh->include_dirs' before accessing to it.
> 	(psymtab_include_file_name): Likewise.
> 	(dwarf_decode_lines_1): Likewise.
> 	(dwarf_decode_lines): Likewise.
> 	(file_file_name): Likewise.

It looks reasonable to me.

I might also have added a complaint somewhere, to help diagnoze
mysterious behavior caused by the lack of the include_dirs table.
But I'm not sure where we would have put that complaint without
causing it to be triggered for each and every reference. It's not
very important, as the situation is unlikely.

OK to push.

> diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
> index 4982922..e2ea7e2 100644
> --- a/gdb/dwarf2read.c
> +++ b/gdb/dwarf2read.c
> @@ -9320,7 +9320,7 @@ setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
>  	  const char *dir = NULL;
>  	  struct file_entry *fe = &lh->file_names[i];
>  
> -	  if (fe->dir_index)
> +	  if (fe->dir_index && lh->include_dirs != NULL)
>  	    dir = lh->include_dirs[fe->dir_index - 1];
>  	  dwarf2_start_subfile (fe->name, dir);
>  
> @@ -17396,7 +17396,7 @@ psymtab_include_file_name (const struct line_header *lh, int file_index,
>    char *copied_name = NULL;
>    int file_is_pst;
>  
> -  if (fe.dir_index)
> +  if (fe.dir_index && lh->include_dirs != NULL)
>      dir_name = lh->include_dirs[fe.dir_index - 1];
>  
>    if (!IS_ABSOLUTE_PATH (include_name)
> @@ -17595,7 +17595,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
>            struct file_entry *fe = &lh->file_names[file - 1];
>            const char *dir = NULL;
>  
> -          if (fe->dir_index)
> +          if (fe->dir_index && lh->include_dirs != NULL)
>              dir = lh->include_dirs[fe->dir_index - 1];
>  
>  	  dwarf2_start_subfile (fe->name, dir);
> @@ -17815,7 +17815,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
>                  else
>                    {
>                      fe = &lh->file_names[file - 1];
> -                    if (fe->dir_index)
> +                    if (fe->dir_index && lh->include_dirs != NULL)
>                        dir = lh->include_dirs[fe->dir_index - 1];
>                      if (!decode_for_pst_p)
>                        {
> @@ -17958,7 +17958,7 @@ dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
>  	  struct file_entry *fe;
>  
>  	  fe = &lh->file_names[i];
> -	  if (fe->dir_index)
> +	  if (fe->dir_index && lh->include_dirs != NULL)
>  	    dir = lh->include_dirs[fe->dir_index - 1];
>  	  dwarf2_start_subfile (fe->name, dir);
>  
> @@ -20640,7 +20640,8 @@ file_file_name (int file, struct line_header *lh)
>      {
>        struct file_entry *fe = &lh->file_names[file - 1];
>  
> -      if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
> +      if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
> +	  || lh->include_dirs == NULL)
>          return xstrdup (fe->name);
>        return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
>  		     fe->name, NULL);


-- 
Joel


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-05-08 17:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-08 17:10 [patch, dwarf2] avoid segfault on missing directory table Sandra Loosemore
2015-05-08 17:23 ` Joel Brobecker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox