This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PATCH: PE COFF relocation overflow bug


On Sun, Jul 07, 2002 at 06:50:45PM -0700, Mark Mitchell wrote:
> 
> There's one other.  That use is ineffectual.  Nothing sets COFF_WITH_PE
> when compiling cofflink.c, ever.  COFF_WITH_PE is only set in files
> like pe-i386.c, and they don't include cofflink.c.
> 
> (I actually tried using COFF_WITH_PE first; it was when I figured out
> it didn't work that I discovered obj_pe.)

Oh, OK.  Shows how much I play with coff.

> >Better might be to use obj_pe in all places that twiddle reloc_count.
> >That way you could configure for a pe target and have some hope of
> >building a coff object with 0xffff relocs.
> 
> Well, that might be cleaner.  But coffcode.h *does* get compiled
> multiple times; it's included from every coff target.  So, using
> COFF_WITH_PE does work in coffcode.h -- just not in cofflink.c.

I meant that coffcode.h is only compiled once when you build for say
--target=i586-pe with no other targets, and the same compiled code
is then used to support both i586-pe and i586-coff objects.  (Well,
it does get compiled twice, once for pe-i386.c and once in pei-i386.c,
but they both are compiled with COFF_WITH_PE defined).

> I'd prefer to leave y'all to disentangle this mess.  May I check in the
> fix, plus the twiddling from ">" to ">=" in coffcode.h?

It sure is a mess.  Can you see if the following fixes the problem
you found?

	* coffcode.h (coff_write_relocs): Adjust reloc count only when
	obj_pe, and do so for counts >= 0xffff.
	(coff_write_object_contents): Likewise.
	* cofflink.c (_bfd_coff_final_link): On PE COFF systems, take into
	account the impact of relocation count overflow when computing
	section offsets.

Please commit it if it does.  You may add me to the ChangeLog if you
want to share the blame, otherwise it's not necessary..  :)

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

Index: bfd/coffcode.h
===================================================================
RCS file: /cvs/src/src/bfd/coffcode.h,v
retrieving revision 1.79
diff -u -p -r1.79 coffcode.h
--- bfd/coffcode.h	25 Jun 2002 06:21:47 -0000	1.79
+++ bfd/coffcode.h	8 Jul 2002 02:31:42 -0000
@@ -2394,7 +2394,7 @@ coff_write_relocs (abfd, first_undef)
 	return false;
 
 #ifdef COFF_WITH_PE
-      if (s->reloc_count > 0xffff)
+      if (obj_pe (abfd) && s->reloc_count >= 0xffff)
 	{
 	  /* encode real count here as first reloc */
 	  struct internal_reloc n;
@@ -3420,7 +3420,7 @@ coff_write_object_contents (abfd)
     {
 #ifdef COFF_WITH_PE
       /* we store the actual reloc count in the first reloc's addr */
-      if (current->reloc_count > 0xffff)
+      if (obj_pe (abfd) && current->reloc_count >= 0xffff)
 	reloc_count ++;
 #endif
       reloc_count += current->reloc_count;
@@ -3451,7 +3451,7 @@ coff_write_object_contents (abfd)
 	  reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
 #ifdef COFF_WITH_PE
 	  /* extra reloc to hold real count */
-	  if (current->reloc_count > 0xffff)
+	  if (obj_pe (abfd) && current->reloc_count >= 0xffff)
 	    reloc_base += bfd_coff_relsz (abfd);
 #endif
 	}
Index: bfd/cofflink.c
===================================================================
RCS file: /cvs/src/src/bfd/cofflink.c,v
retrieving revision 1.33
diff -u -p -r1.33 cofflink.c
--- bfd/cofflink.c	7 Jun 2002 15:04:47 -0000	1.33
+++ bfd/cofflink.c	8 Jul 2002 02:31:43 -0000
@@ -757,6 +757,10 @@ _bfd_coff_final_link (abfd, info)
 	  o->flags |= SEC_RELOC;
 	  o->rel_filepos = rel_filepos;
 	  rel_filepos += o->reloc_count * relsz;
+	  /* In PE COFF, if there are at least 0xffff relocations an
+	     extra relocation will be written out to encode the count.  */
+	  if (obj_pe (abfd) && o->reloc_count >= 0xffff)
+	    rel_filepos += relsz;
 	}
 
       if (bfd_coff_long_section_names (abfd)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]