public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "Joseph S. Myers" <joseph@codesourcery.com>
To: Alan Modra <amodra@bigpond.net.au>
Cc: binutils@sourceware.org
Subject: Re: Patch to detect invalid mergeable string sections
Date: Wed, 31 Oct 2007 18:17:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.64.0710311806580.31492@digraph.polyomino.org.uk> (raw)
In-Reply-To: <20071030225501.GF5706@bubble.grove.modra.org>

On Wed, 31 Oct 2007, Alan Modra wrote:

> On Tue, Oct 30, 2007 at 04:10:18PM +0000, Joseph S. Myers wrote:
> > +	      if (sec_end && s >= sec_end)
> > +		{
> > +		  (*_bfd_error_handler)
> > +		    (_("unterminated string in section marked for merging"));
> > +		  return NULL;
> > +		}
> 
> It would be nice if this error specified the section.  I think you can
> do that by simply returning NULL here and calling _bfd_error_handler
> at error_return in record_section after testing for bfd_get_error
> returning anything but bfd_error_no_memory.

How about this revised patch which does that?

bfd:
2007-10-31  Joseph Myers  <joseph@codesourcery.com>

	* merge.c (sec_merge_hash_lookup): Add parameter sec_end.  Check
	for unterminated strings.  All callers changed.
	(record_section): Add parameter abfd.  Give error message for
	unterminated strings.
	(_bfd_merge_sections): Update call to record_section.
	(_bfd_write_merged_section, _bfd_merged_section_offset): Handle
	NULL secinfo from merge failures.

ld/testsuite:
2007-10-31  Joseph Myers  <joseph@codesourcery.com>

	* ld-elf/merge3.d, ld-elf/merge3.s: New.

Index: bfd/merge.c
===================================================================
RCS file: /cvs/src/src/bfd/merge.c,v
retrieving revision 1.33
diff -u -p -r1.33 merge.c
--- bfd/merge.c	19 Sep 2007 12:08:34 -0000	1.33
+++ bfd/merge.c	31 Oct 2007 17:58:02 -0000
@@ -133,6 +133,7 @@ sec_merge_hash_newfunc (struct bfd_hash_
 
 static struct sec_merge_hash_entry *
 sec_merge_hash_lookup (struct sec_merge_hash *table, const char *string,
+		       const unsigned char *sec_end,
 		       unsigned int alignment, bfd_boolean create)
 {
   register const unsigned char *s;
@@ -154,6 +155,8 @@ sec_merge_hash_lookup (struct sec_merge_
 	      hash += c + (c << 17);
 	      hash ^= hash >> 2;
 	      ++len;
+	      if (sec_end && s >= sec_end)
+		return NULL;
 	    }
 	  hash += len + (len << 17);
 	}
@@ -161,6 +164,8 @@ sec_merge_hash_lookup (struct sec_merge_
 	{
 	  for (;;)
 	    {
+	      if (sec_end && s + table->entsize > sec_end)
+		return NULL;
 	      for (i = 0; i < table->entsize; ++i)
 		if (s[i] != '\0')
 		  break;
@@ -264,7 +269,9 @@ sec_merge_add (struct sec_merge_hash *ta
 {
   register struct sec_merge_hash_entry *entry;
 
-  entry = sec_merge_hash_lookup (tab, str, alignment, TRUE);
+  entry = sec_merge_hash_lookup (tab, str,
+				 secinfo->contents + secinfo->sec->size,
+				 alignment, TRUE);
   if (entry == NULL)
     return NULL;
 
@@ -436,7 +443,8 @@ _bfd_add_merge_section (bfd *abfd, void 
 
 /* Record one section into the hash table.  */
 static bfd_boolean
-record_section (struct sec_merge_info *sinfo,
+record_section (bfd *abfd,
+		struct sec_merge_info *sinfo,
 		struct sec_merge_sec_info *secinfo)
 {
   asection *sec = secinfo->sec;
@@ -513,6 +521,10 @@ record_section (struct sec_merge_info *s
   return TRUE;
 
 error_return:
+  if (bfd_get_error () != bfd_error_no_memory)
+    (*_bfd_error_handler)
+      (_("%B: unterminated string in section `%A' marked for merging"),
+       abfd, sec);
   for (secinfo = sinfo->chain; secinfo; secinfo = secinfo->next)
     *secinfo->psecinfo = NULL;
   return FALSE;
@@ -695,7 +707,7 @@ alloc_failure:
    with _bfd_merge_section.  */
 
 bfd_boolean
-_bfd_merge_sections (bfd *abfd ATTRIBUTE_UNUSED,
+_bfd_merge_sections (bfd *abfd,
 		     struct bfd_link_info *info ATTRIBUTE_UNUSED,
 		     void *xsinfo,
 		     void (*remove_hook) (bfd *, asection *))
@@ -722,7 +734,7 @@ _bfd_merge_sections (bfd *abfd ATTRIBUTE
 	    if (remove_hook)
 	      (*remove_hook) (abfd, secinfo->sec);
 	  }
-	else if (! record_section (sinfo, secinfo))
+	else if (! record_section (abfd, sinfo, secinfo))
 	  break;
 
       if (secinfo)
@@ -779,6 +791,9 @@ _bfd_write_merged_section (bfd *output_b
 
   secinfo = (struct sec_merge_sec_info *) psecinfo;
 
+  if (!secinfo)
+    return FALSE;
+
   if (secinfo->first_str == NULL)
     return TRUE;
 
@@ -807,6 +822,9 @@ _bfd_merged_section_offset (bfd *output_
 
   secinfo = (struct sec_merge_sec_info *) psecinfo;
 
+  if (!secinfo)
+    return 0;
+
   if (offset >= sec->rawsize)
     {
       if (offset > sec->rawsize)
@@ -849,7 +867,7 @@ _bfd_merged_section_offset (bfd *output_
     {
       p = secinfo->contents + (offset / sec->entsize) * sec->entsize;
     }
-  entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, 0, FALSE);
+  entry = sec_merge_hash_lookup (secinfo->htab, (char *) p, NULL, 0, FALSE);
   if (!entry)
     {
       if (! secinfo->htab->strings)
Index: ld/testsuite/ld-elf/merge3.d
===================================================================
RCS file: ld/testsuite/ld-elf/merge3.d
diff -N ld/testsuite/ld-elf/merge3.d
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-elf/merge3.d	31 Oct 2007 17:58:03 -0000
@@ -0,0 +1,3 @@
+#source: merge3.s
+#ld: -T merge.ld
+#error: unterminated string in section `.rodata.str' marked for merging
Index: ld/testsuite/ld-elf/merge3.s
===================================================================
RCS file: ld/testsuite/ld-elf/merge3.s
diff -N ld/testsuite/ld-elf/merge3.s
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-elf/merge3.s	31 Oct 2007 17:58:03 -0000
@@ -0,0 +1,7 @@
+	.section .rodata.str,"aMS","progbits",1
+.LC0:	
+	.ascii	"abcd"
+	.text
+	.global _start
+_start:	
+	.long	.LC0

-- 
Joseph S. Myers
joseph@codesourcery.com

  reply	other threads:[~2007-10-31 18:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-30 16:19 Joseph S. Myers
2007-10-30 23:39 ` Alan Modra
2007-10-31 18:17   ` Joseph S. Myers [this message]
2007-11-01  7:11     ` Alan Modra
2007-11-05  2:12       ` Alan Modra
2007-11-05  2:31         ` Joseph S. Myers
2007-11-05  3:59           ` Alan Modra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0710311806580.31492@digraph.polyomino.org.uk \
    --to=joseph@codesourcery.com \
    --cc=amodra@bigpond.net.au \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).