bfd/

2009-08-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR gas/10531
	* section.c: Include "elf-bfd.h".
	(bfd_get_section_by_name): Don't use a section in a section
	group.
	(bfd_make_section_old_way): Likewise.

binutils/testsuite/

2009-08-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR gas/10531
	* binutils-all/objdump.W: Remove bogus line debug info.

gas/

2009-08-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR gas/10531
	* as.c (gen_debug): New.
	(parse_args): Set gen_debug to debug_type.

	* as.h (gen_debug): New.

	* dwarf2dbg.c (dwarf2_loc_directive_used): New.
	(dwarf2_directive_loc): Set dwarf2_loc_directive_used.
	(dwarf2_loc_directive_used): Likewise.
	(dwarf2_finish): Return if -g isn't passed nor any .loc
	directive is used.

	* subsegs.c (subseg_get): Don't compare segment name.

gas/testsuite/

2009-08-20  H.J. Lu  <hongjiu.lu@intel.com>

	 PR gas/10531
	 * gas/elf/group2.d: New.
	 * gas/elf/group2.s: Likewise.
	 * gas/i386/debug1.d: Likewise.
	 * gas/i386/debug1.s: Likewise.

	* gas/elf/elf.exp: Run group2.

	* gas/i386/i386.exp: Run debug1 for both 32bit and 64bit.

Index: binutils/gas/dwarf2dbg.c
===================================================================
--- binutils/gas/dwarf2dbg.c	(revision 6552)
+++ binutils/gas/dwarf2dbg.c	(working copy)
@@ -188,6 +188,9 @@ static unsigned int dirs_allocated;
    doing work when there's nothing to do.  */
 bfd_boolean dwarf2_loc_directive_seen;
 
+/* TRUE when any .loc directive is used.  */
+static bfd_boolean dwarf2_loc_directive_used;
+
 /* TRUE when we're supposed to set the basic block mark whenever a
    label is seen.  */
 bfd_boolean dwarf2_loc_mark_labels;
@@ -688,6 +691,7 @@ dwarf2_directive_loc (int dummy ATTRIBUT
 
   demand_empty_rest_of_line ();
   dwarf2_loc_directive_seen = TRUE;
+  dwarf2_loc_directive_used = TRUE;
   debug_type = DEBUG_NONE;
 }
 
@@ -706,6 +710,8 @@ dwarf2_directive_loc_mark_labels (int du
       dwarf2_loc_mark_labels = value != 0;
       demand_empty_rest_of_line ();
     }
+
+  dwarf2_loc_directive_used = TRUE;
 }
 
 static struct frag *
@@ -1718,6 +1724,10 @@ dwarf2_finish (void)
   segT info_seg;
   int emit_other_sections = 0;
 
+  /* Return if -g isn't passed nor any .loc directive is used.  */
+  if (!dwarf2_loc_directive_used && gen_debug == DEBUG_UNSPECIFIED)
+    return;
+
   info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
   emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
 
Index: binutils/gas/testsuite/gas/i386/i386.exp
===================================================================
--- binutils/gas/testsuite/gas/i386/i386.exp	(revision 6552)
+++ binutils/gas/testsuite/gas/i386/i386.exp	(working copy)
@@ -190,6 +190,7 @@ if [expr ([istarget "i*86-*-*"] ||  [ist
 	run_list_test "inval-equ-2" "-al"
 	run_dump_test "ifunc"
 	run_list_test "l1om-inval" "-march=l1om --32"
+	run_dump_test "debug1"
     }
 
     # This is a PE specific test.
@@ -348,6 +349,7 @@ if [expr ([istarget "i*86-*-*"] || [ista
 	run_dump_test "mixed-mode-reloc64"
 	run_dump_test "x86-64-ifunc"
 	run_dump_test "l1om"
+	run_dump_test "debug1"
     }
 
     set ASFLAGS "$old_ASFLAGS"
Index: binutils/gas/testsuite/gas/i386/debug1.d
===================================================================
--- binutils/gas/testsuite/gas/i386/debug1.d	(revision 0)
+++ binutils/gas/testsuite/gas/i386/debug1.d	(revision 0)
@@ -0,0 +1,46 @@
+#as: --gdwarf-2
+#readelf: -wl
+#name: DWARF2 debugging information 1
+
+Raw dump of debug contents of section .debug_line:
+
+  Offset:                      0x0
+  Length:                      12.
+  DWARF Version:               2
+  Prologue Length:             101
+  Minimum Instruction Length:  1
+  Initial value of 'is_stmt':  1
+  Line Base:                   -5
+  Line Range:                  14
+  Opcode Base:                 13
+
+ Opcodes:
+  Opcode 1 has 0 args
+  Opcode 2 has 1 args
+  Opcode 3 has 1 args
+  Opcode 4 has 1 args
+  Opcode 5 has 1 args
+  Opcode 6 has 0 args
+  Opcode 7 has 0 args
+  Opcode 8 has 0 args
+  Opcode 9 has 1 args
+  Opcode 10 has 0 args
+  Opcode 11 has 0 args
+  Opcode 12 has 1 args
+
+ The Directory Table:
+  /export/gnu/import/svn/binutils-group/binutils/gas/testsuite/gas/i386
+
+ The File Name Table:
+  Entry	Dir	Time	Size	Name
+  1	1	0	0	debug1.s
+
+ Line Number Statements:
+  Extended opcode 2: set Address to 0x0
+  Special opcode 7: advance Address by 0 to 0x0 and Line by 2 to 3
+  Special opcode 20: advance Address by 1 to 0x1 and Line by 1 to 4
+  Special opcode 20: advance Address by 1 to 0x2 and Line by 1 to 5
+  Advance PC by 1 to 0x3
+  Extended opcode 1: End of Sequence
+
+
Index: binutils/gas/testsuite/gas/i386/debug1.s
===================================================================
--- binutils/gas/testsuite/gas/i386/debug1.s	(revision 0)
+++ binutils/gas/testsuite/gas/i386/debug1.s	(revision 0)
@@ -0,0 +1,5 @@
+	.file "debug1.s"
+	.text
+	nop
+	nop
+	nop
Index: binutils/gas/testsuite/gas/elf/group2.s
===================================================================
--- binutils/gas/testsuite/gas/elf/group2.s	(revision 0)
+++ binutils/gas/testsuite/gas/elf/group2.s	(revision 0)
@@ -0,0 +1,10 @@
+	.section .debug_info,"G",%progbits,foo,comdat
+	.byte 0x0
+	.section .debug_line,"G",%progbits,foo,comdat
+	.byte 0x0
+	.section .debug_abbrev,"G",@progbits,foo,comdat
+	.byte 0x0
+	.section .debug_ranges,"G",@progbits,foo,comdat
+	.byte 0x0
+	.section .gnu.linkonce.t.foo, "xaG",@progbits,foo,comdat
+	nop
Index: binutils/gas/testsuite/gas/elf/group2.d
===================================================================
--- binutils/gas/testsuite/gas/elf/group2.d	(revision 0)
+++ binutils/gas/testsuite/gas/elf/group2.d	(revision 0)
@@ -0,0 +1,14 @@
+#as:
+#readelf: -SWg
+#name: group section with debug sections
+
+
+#...
+COMDAT group section \[    1\] `foo' \[foo\] contains 5 sections:
+[ 	]+\[Index\][ 	]+Name
+[ 	]+\[.*\][ 	]+.debug_info
+[ 	]+\[.*\][ 	]+.debug_line
+[ 	]+\[.*\][ 	]+.debug_abbrev
+[ 	]+\[.*\][ 	]+.debug_ranges
+[ 	]+\[.*\][ 	]+.gnu.linkonce.t.foo
+#pass
Index: binutils/gas/testsuite/gas/elf/elf.exp
===================================================================
--- binutils/gas/testsuite/gas/elf/elf.exp	(revision 6552)
+++ binutils/gas/testsuite/gas/elf/elf.exp	(working copy)
@@ -100,6 +100,7 @@ if { ([istarget "*-*-*elf*"]		
     run_dump_test "group0b" 
     run_dump_test "group1a" 
     run_dump_test "group1b" 
+    run_dump_test "group2" 
     case $target_triplet in {
 	{ alpha*-*-* } { }
 	{ cr16*-*-* } { }
Index: binutils/gas/as.c
===================================================================
--- binutils/gas/as.c	(revision 6552)
+++ binutils/gas/as.c	(working copy)
@@ -72,6 +72,9 @@ struct defsym_list
 /* True if a listing is wanted.  */
 int listing;
 
+/* If assembler shiuld debug info.  */
+enum debug_info_type gen_debug = DEBUG_UNSPECIFIED;
+
 /* Type of debugging to generate.  */
 enum debug_info_type debug_type = DEBUG_UNSPECIFIED;
 int use_gnu_debug_info_extensions = 0;
@@ -908,6 +911,8 @@ This program has absolutely no warranty.
 	}
     }
 
+  gen_debug = debug_type;
+
   free (shortopts);
   free (longopts);
 
Index: binutils/gas/as.h
===================================================================
--- binutils/gas/as.h	(revision 6552)
+++ binutils/gas/as.h	(working copy)
@@ -429,6 +429,7 @@ enum debug_info_type
   DEBUG_DWARF2
 };
 
+extern enum debug_info_type gen_debug;
 extern enum debug_info_type debug_type;
 extern int use_gnu_debug_info_extensions;
 
Index: binutils/gas/subsegs.c
===================================================================
--- binutils/gas/subsegs.c	(revision 6552)
+++ binutils/gas/subsegs.c	(working copy)
@@ -148,15 +148,6 @@ subseg_get (const char *segname, int for
 {
   segT secptr;
   segment_info_type *seginfo;
-  const char *now_seg_name = (now_seg
-			      ? bfd_get_section_name (stdoutput, now_seg)
-			      : 0);
-
-  if (!force_new
-      && now_seg_name
-      && (now_seg_name == segname
-	  || !strcmp (now_seg_name, segname)))
-    return now_seg;
 
   if (!force_new)
     secptr = bfd_make_section_old_way (stdoutput, segname);
Index: binutils/binutils/testsuite/binutils-all/objdump.W
===================================================================
--- binutils/binutils/testsuite/binutils-all/objdump.W	(revision 6552)
+++ binutils/binutils/testsuite/binutils-all/objdump.W	(working copy)
@@ -73,36 +73,6 @@ Raw dump of debug contents of section .d
   Extended opcode 1: End of Sequence
 
 
-  Offset:                      0x42
-  Length:                      25
-  DWARF Version:               2
-  Prologue Length:             19
-  Minimum Instruction Length:  [1248]
-  Initial value of 'is_stmt':  1
-  Line Base:                   -5
-  Line Range:                  14
-  Opcode Base:                 13
-
- Opcodes:
-  Opcode 1 has 0 args
-  Opcode 2 has 1 args
-  Opcode 3 has 1 args
-  Opcode 4 has 1 args
-  Opcode 5 has 1 args
-  Opcode 6 has 0 args
-  Opcode 7 has 0 args
-  Opcode 8 has 0 args
-  Opcode 9 has 1 args
-  Opcode 10 has 0 args
-  Opcode 11 has 0 args
-  Opcode 12 has 1 args
-
- The Directory Table is empty.
-
- The File Name Table is empty.
-
- Line Number Statements:
-
 Contents of the .zdebug_abbrev section:
 
   Number TAG
Index: binutils/bfd/section.c
===================================================================
--- binutils/bfd/section.c	(revision 6552)
+++ binutils/bfd/section.c	(working copy)
@@ -138,6 +138,7 @@ SUBSECTION
 #include "bfd.h"
 #include "libbfd.h"
 #include "bfdlink.h"
+#include "elf-bfd.h"
 
 /*
 DOCDD
@@ -854,11 +855,31 @@ asection *
 bfd_get_section_by_name (bfd *abfd, const char *name)
 {
   struct section_hash_entry *sh;
+  unsigned long hash;
+  asection *sec;
 
   sh = section_hash_lookup (&abfd->section_htab, name, FALSE, FALSE);
-  if (sh != NULL)
+  if (sh == NULL)
+    return NULL;
+
+  /* Only ELF has section group. */
+  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
     return &sh->section;
 
+  hash = sh->root.hash;
+  do
+    {
+      sec = &sh->section;
+      /* Don't return a section in a section group.  */
+      if (elf_section_data (sec) == NULL
+	  || elf_group_name (sec) == NULL)
+	return sec;
+      sh = (struct section_hash_entry *) sh->root.next;
+    }
+  while (sh != NULL
+	 && sh->root.hash == hash
+	 && strcmp (sh->root.string, name) == 0);
+
   return NULL;
 }
 
@@ -1011,7 +1032,43 @@ bfd_make_section_old_way (bfd *abfd, con
       if (sh == NULL)
 	return NULL;
 
-      newsect = &sh->section;
+      /* Only ELF has section group. */
+      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+	{
+	  struct section_hash_entry *old_sh = sh;
+	  unsigned long hash = sh->root.hash;
+
+	  do
+	    {
+	      newsect = &sh->section;
+	      /* Don't use a section in a section group.  */
+	      if (elf_section_data (newsect) == NULL
+		  || elf_group_name (newsect) == NULL)
+		break;
+	      sh = (struct section_hash_entry *) sh->root.next;
+	    }
+	  while (sh != NULL
+		 && sh->root.hash == hash
+		 && strcmp (sh->root.string, name) == 0);
+
+	  if (sh == NULL)
+	    {
+	      /* Allocate a new one since existing sections are in
+		 section groups.  */
+	      struct section_hash_entry *new_sh;
+	      new_sh = (struct section_hash_entry *)
+		bfd_section_hash_newfunc (NULL, &abfd->section_htab, name);
+	      if (new_sh == NULL)
+		return NULL;
+
+	      new_sh->root = old_sh->root;
+	      old_sh->root.next = &new_sh->root;
+	      newsect = &new_sh->section;
+	    }
+	}
+      else
+	newsect = &sh->section;
+
       if (newsect->name != NULL)
 	{
 	  /* Section already exists.  */
