PATCH: Add bfd_get_section_ident

H. J. Lu hjl@lucon.org
Thu Jul 1 18:26:00 GMT 2004


On Thu, Jul 01, 2004 at 04:46:30PM +0100, Nick Clifton wrote:
> Hi H. J.
> 
> >>Also, would it be possible to include a test somewhere that checks the 
> >>behaviour of this patch ?
> 
> >Here is a testcase:
> >
> >[hjl@gnu 1]$ make
> >gcc -c foo.s
> >gcc -c bar.s
> >ld -r -o new.o foo.o bar.o
> >bar: discarded in section `.text[.foo]' from bar.o
> >make: [new.o] Error 1 (ignored)
> 
> 
> >/usr/bin/ld -r -o old.o foo.o bar.o
> >bar: discarded in section `.text' from bar.o
> >make: [old.o] Error 1 (ignored)
> >
> >There may be many `.text' sections in bar.o. `.text[.foo]' is more
> >useful.
> 
> Thanks - that makes your patch clearer.  Please consider the patch approved.
> 

Nick,

I have an updated patch

http://sources.redhat.com/ml/binutils/2004-06/msg00606.html

The difference is bfd_get_section_ident will malloc a buffer
for the section identifier. The change is limited to bfd. I also
updated ld to use bfd_get_section_ident.

Alan suggested use elf_group_name. Here is an updated patch.


H.J.
-------------- next part --------------
bfd/

2004-07-01  H.J. Lu  <hongjiu.lu@intel.com>

	* bfd.c (bfd_get_section_ident): New.

	* elflink.c (elf_link_read_relocs_from_section): Call
	bfd_get_section_ident to identify the section when reporting
	error.
	(_bfd_elf_link_output_relocs): Likewise.
	(elf_link_output_extsym): Likewise.
	(elf_link_input_bfd): Likewise.
	(bfd_elf_gc_record_vtinherit): Likewise.

ld/

2004-07-01  H.J. Lu  <hongjiu.lu@intel.com>

	* ldmisc.c (vfinfo): Call bfd_get_section_ident to identify
	the section.

--- binutils/bfd/bfd.c.ident	2004-05-17 18:59:10.000000000 -0700
+++ binutils/bfd/bfd.c	2004-07-01 11:15:22.695130857 -0700
@@ -1417,3 +1417,46 @@ bfd_preserve_finish (bfd *abfd ATTRIBUTE
      objalloc.  */
   bfd_hash_table_free (&preserve->section_htab);
 }
+
+/*
+FUNCTION
+	bfd_get_section_ident
+
+SYNOPSIS
+	char *bfd_get_section_ident (asection *sec);
+
+DESCRIPTION
+	This function returns "section name[group name]" in a malloced
+	buffer if @var{sec} is a member of an ELF section group and
+	returns NULL otherwise. The caller should free the non-NULL
+	return after use.
+
+*/
+
+char *
+bfd_get_section_ident (asection *sec)
+{
+  char *buf;
+  bfd_size_type nlen;
+  bfd_size_type glen;
+
+  if (sec->owner == NULL
+      || bfd_get_flavour (sec->owner) != bfd_target_elf_flavour
+      || elf_next_in_group (sec) == NULL
+      || (sec->flags & SEC_GROUP) != 0)
+    return NULL;
+
+  nlen = strlen (sec->name);
+  glen = strlen (elf_group_name (sec));
+  buf = bfd_malloc (nlen + glen + 2 + 1);
+  if (buf != NULL)
+    {
+      strcpy (buf, sec->name);
+      buf [nlen] = '[';
+      strcpy (&buf [nlen + 1], elf_group_name (sec));
+      buf [nlen + 1 + glen] = ']';
+      buf [nlen + 1 + glen + 1] = '\0';
+    }
+
+  return buf;
+}
--- binutils/bfd/elflink.c.ident	2004-07-01 09:43:59.957760818 -0700
+++ binutils/bfd/elflink.c	2004-07-01 11:22:16.655476523 -0700
@@ -1859,10 +1859,14 @@ elf_link_read_relocs_from_section (bfd *
 	r_symndx >>= 24;
       if ((size_t) r_symndx >= nsyms)
 	{
+	  char *sec_name = bfd_get_section_ident (sec);
 	  (*_bfd_error_handler)
 	    (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
 	     bfd_archive_filename (abfd), (unsigned long) r_symndx,
-	     (unsigned long) nsyms, irela->r_offset, sec->name);
+	     (unsigned long) nsyms, irela->r_offset,
+	     sec_name ? sec_name : sec->name);
+	  if (sec_name)
+	    free (sec_name);
 	  bfd_set_error (bfd_error_bad_value);
 	  return FALSE;
 	}
@@ -2048,11 +2052,14 @@ _bfd_elf_link_output_relocs (bfd *output
     }
   else
     {
+      char *sec_name = bfd_get_section_ident (input_section);
       (*_bfd_error_handler)
 	(_("%s: relocation size mismatch in %s section %s"),
 	 bfd_get_filename (output_bfd),
 	 bfd_archive_filename (input_section->owner),
-	 input_section->name);
+	 sec_name ? sec_name : input_section->name);
+      if (sec_name)
+	free (sec_name);
       bfd_set_error (bfd_error_wrong_object_format);
       return FALSE;
     }
@@ -6073,11 +6080,14 @@ elf_link_output_extsym (struct elf_link_
 						 input_sec->output_section);
 	    if (sym.st_shndx == SHN_BAD)
 	      {
+		char *sec_name = bfd_get_section_ident (input_sec);
 		(*_bfd_error_handler)
 		  (_("%s: could not find output section %s for input section %s"),
 		   bfd_get_filename (finfo->output_bfd),
 		   input_sec->output_section->name,
-		   input_sec->name);
+		   sec_name ? sec_name : input_sec->name);
+		if (sec_name)
+		  free (sec_name);
 		eoinfo->failed = TRUE;
 		return FALSE;
 	      }
@@ -6638,13 +6648,21 @@ elf_link_input_bfd (struct elf_final_lin
 			}
 		      else if (complain)
 			{
+			  char *r_sec
+			    = bfd_get_section_ident (o);
+			  char *d_sec
+			    = bfd_get_section_ident (sec);
 			  finfo->info->callbacks->error_handler
 			    (LD_DEFINITION_IN_DISCARDED_SECTION,
 			     _("`%T' referenced in section `%s' of %B: "
 			       "defined in discarded section `%s' of %B\n"),
-			     sym_name,
-			     sym_name, o->name, input_bfd,
-			     sec->name, sec->owner);
+			     sym_name, sym_name,
+			     r_sec ? r_sec : o->name, input_bfd,
+			     d_sec ? d_sec : sec->name, sec->owner);
+			  if (r_sec)
+			    free (r_sec);
+			  if (d_sec)
+			    free (d_sec);
 			}
 
 		      /* Remove the symbol reference from the reloc, but
@@ -8567,6 +8585,7 @@ bfd_elf_gc_record_vtinherit (bfd *abfd,
   struct elf_link_hash_entry **search, *child;
   bfd_size_type extsymcount;
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  char *sec_name;
 
   /* The sh_info field of the symtab header tells us where the
      external symbols start.  We don't care about the local symbols at
@@ -8590,8 +8609,10 @@ bfd_elf_gc_record_vtinherit (bfd *abfd,
 	goto win;
     }
 
+  sec_name = bfd_get_section_ident (sec);
   (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
-			 bfd_archive_filename (abfd), sec->name,
+			 bfd_archive_filename (abfd),
+			 sec_name ? sec_name : sec->name,
 			 (unsigned long) offset);
   bfd_set_error (bfd_error_invalid_operation);
   return FALSE;
--- binutils/ld/ldmisc.c.ident	2003-07-24 12:18:12.000000000 -0700
+++ binutils/ld/ldmisc.c	2004-07-01 11:05:38.445856724 -0700
@@ -241,6 +241,7 @@ vfinfo (FILE *fp, const char *fmt, va_li
 		const char *functionname;
 		unsigned int linenumber;
 		bfd_boolean discard_last;
+		char *sec_name;
 
 		abfd = va_arg (arg, bfd *);
 		section = va_arg (arg, asection *);
@@ -269,7 +270,11 @@ vfinfo (FILE *fp, const char *fmt, va_li
 		      }
 		  }
 
-		lfinfo (fp, "%B(%s+0x%v)", abfd, section->name, offset);
+		sec_name = bfd_get_section_ident (section);
+		lfinfo (fp, "%B(%s+0x%v)", abfd,
+			sec_name ? sec_name : section->name, offset);
+		if (sec_name)
+		  free (sec_name);
 
 		discard_last = TRUE;
 		if (bfd_find_nearest_line (abfd, section, asymbols, offset,


More information about the Binutils mailing list