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]

Re: binutils ld padding statements


On Tue, Oct 30, 2001 at 03:55:47PM -0800, David Heine wrote:
>[snip] 
> I ran into a problem with a corner case.  It can cause the linker to
> fail under relaxation because a
> padding_statement's "output_offset" field can be invalid.
> 
> The issue can occur under the following conditions:
>     1. some  pass of lang_size_sections() generates a pad statement to
> align an input section
>     2. the final pass of lang_size_sections() does not need the pad to
> align the input section.
> 
> The pad's "size" field is reset to 0, but because insert_pad() is not
> called during
> size_input_section(), the padding statement's "output_offset" field is
> not updated.
> 
> At the end of relaxation, a bfd_link_order structure is happily created
> for this 0 sized
> padding statement.
> On output, bfd_set_section_contents() is called with zero-sized contents
> 
> and bfd_set_section_contents() will fail if the invalid offset field is
> outside of the
> size of the section.  (Which has happened to me).

Oops.  There originally was code to set output_offset, and I took it out
as I thought it wasn't needed.

> I thought I would check with you before posting a patch to the binutils
> mailing list.
> If you are too swamped to take a look, I'll post it there next week.
> 
> 
> There are a few easy ways to deal with this
>     1) keep a valid output_offset field for padding statements.
>     2) don't create a 0 sized bfd_link_order
>     3) don't return an error trying to write 0 sized contents.
> 
> I propose dealing with (1) in my patch that follows:

Looks good.  I'm committing it (with added comment), and copying your
clear explanation to the list.  Thanks!

ld/ChangeLog
2001-10-31  David Heine  <dlheine@tensilica.com>

	* ldlang.c (lang_size_sections): Keep a valid output_offset field
	for padding statements.

-- 
Alan Modra

Index: ldlang.c
===================================================================
RCS file: /cvs/src/src/ld/ldlang.c,v
retrieving revision 1.65
diff -u -p -r1.65 ldlang.c
--- ldlang.c	2001/10/29 22:36:32	1.65
+++ ldlang.c	2001/10/31 00:29:23
@@ -3156,6 +3156,14 @@ lang_size_sections (s, output_section_st
 	     padding to shrink.  If padding is needed on this pass, it
 	     will be added back in.  */
 	  s->padding_statement.size = 0;
+
+	  /* Make sure output_offset is valid.  If relaxation shrinks
+	     the section and this pad isn't needed, it's possible to
+	     have output_offset larger than the final size of the
+	     section.  bfd_set_section_contents will complain even for
+	     a pad size of zero.  */
+	  s->padding_statement.output_offset
+	    = dot - output_section_statement->bfd_section->vma;
 	  break;
 
 	case lang_group_statement_enum:


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