This is the mail archive of the 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: ld lma assignment

On Wed, Jul 26, 2006 at 02:39:14PM +0930, Alan Modra wrote:
> While playing with overlays recently, I noticed an annoying linker
> script requirement.  An output section following an overlay must have
> its lma specified (with AT or AT>).  As must any section following one
> with an lma adjustment, because ld defaults to setting lma equal to
> vma and this may cause lma overlap.

Fixes doco for this change, and warns on backward movement of VMA
unless accompanied with an explicit LMA assignment.  I guess I should
have added the news item when I committed the first patch to raise
awareness of the change in default lma.  Apologies for that, I wasn't
trying to slip a change in under the radar!  I still feel it's a good
change for 2.18, one that shouldn't affect too many scripts, and those
that it does are easily fixed.

	* NEWS: Mention LMA default change.
	* ld.texinfo (Output Section LMA): Update default description.
	(Location Counter): Clarify backward movement.
	* ldlang.c (lang_size_sections_1): Leave non-alloc sections with
	default lma equal to vma.  Warn on backward movement of dot.

Index: ld/NEWS
RCS file: /cvs/src/src/ld/NEWS,v
retrieving revision 1.66
diff -u -p -r1.66 NEWS
--- ld/NEWS	4 Aug 2006 14:53:26 -0000	1.66
+++ ld/NEWS	22 Aug 2006 04:39:19 -0000
@@ -1,4 +1,10 @@
 -*- text -*-
+* The default output section LMA has changed for allocatable sections from
+  being equal to VMA, to keeping the difference between LMA and VMA the same as
+  the previous output section in the same region.  This is a more useful
+  default when using overlays and other cases where you specify an LMA
+  differing from the VMA for some sections.
 * New switch: --print-gc-sections to list any sections removed by garabge
Index: ld/ld.texinfo
RCS file: /cvs/src/src/ld/ld.texinfo,v
retrieving revision 1.171
diff -u -p -r1.171 ld.texinfo
--- ld/ld.texinfo	4 Aug 2006 14:53:26 -0000	1.171
+++ ld/ld.texinfo	22 Aug 2006 04:39:24 -0000
@@ -3764,15 +3764,20 @@ Every section has a virtual address (VMA
 an output section description sets the VMA (@pxref{Output Section
-The linker will normally set the LMA equal to the VMA.  You can change
-that by using the @code{AT} keyword.  The expression @var{lma} that
-follows the @code{AT} keyword specifies the load address of the
+The expression @var{lma} that follows the @code{AT} keyword specifies
+the load address of the section.
 Alternatively, with @samp{AT>@var{lma_region}} expression, you may
 specify a memory region for the section's load address. @xref{MEMORY}.
 Note that if the section has not had a VMA assigned to it then the
 linker will use the @var{lma_region} as the VMA region as well.
+If neither @code{AT} nor @code{AT>} is specified for an allocatable
+section, the linker will set the LMA such that the difference between
+VMA and LMA for the section is the same as the preceding output
+section in the same region.  If there is no preceding output section
+or the section is not allocatable, the linker will set the LMA equal
+to the VMA.
 @xref{Output Section Region}.
 @cindex ROM initialized data
@@ -4586,7 +4591,9 @@ anywhere that an ordinary symbol is allo
 @cindex holes
 Assigning a value to @code{.} will cause the location counter to be
 moved.  This may be used to create holes in the output section.  The
-location counter may never be moved backwards.
+location counter may not be moved backwards inside an output section,
+and may not be moved backwards outside of an output section if so
+doing creates areas with overlapping LMAs.
Index: ld/ldlang.c
RCS file: /cvs/src/src/ld/ldlang.c,v
retrieving revision 1.234
diff -u -p -r1.234 ldlang.c
--- ld/ldlang.c	16 Aug 2006 08:31:45 -0000	1.234
+++ ld/ldlang.c	22 Aug 2006 04:39:28 -0000
@@ -4400,16 +4400,30 @@ lang_size_sections_1
 		  lma = align_power (lma, os->section_alignment);
 		os->bfd_section->lma = lma;
-	    else if (r->last_os != NULL)
+	    else if (r->last_os != NULL
+		     && (os->bfd_section->flags & SEC_ALLOC) != 0)
 		bfd_vma lma;
 		asection *last;
 		last = r->last_os->output_section_statement.bfd_section;
-		/* If dot moved backwards (which is invalid according
-		   to ld docs) then leave lma equal to vma.  This
-		   keeps users of buggy ld scripts happy.  */
-		if (dot >= last->vma)
+		/* A backwards move of dot should be accompanied by
+		   an explicit assignment to the section LMA (ie.
+		   os->load_base set) because backwards moves normally
+		   create overlapping LMAs.  */
+		if (dot < last->vma)
+		  {
+		    einfo (_("%P: warning: dot moved backwards before `%s'\n"),
+			   os->name);
+		    /* If dot moved backwards then leave lma equal to
+		       vma.  This is the old default lma, which might
+		       just happen to work when the backwards move is
+		       sufficiently large.  Nag anyway, so people fix
+		       their linker scripts.  */
+		  }
+		else
 		    /* If the current vma overlaps the previous section,
 		       then set the current lma to that at the end of
@@ -4440,9 +4454,16 @@ lang_size_sections_1
 	       lma region.  We use this to set the lma for
 	       following sections.  Overlays or other linker
 	       script assignment to lma might mean that the
-	       default lma == vma is incorrect.  */
+	       default lma == vma is incorrect.
+	       To avoid warnings about dot moving backwards when using
+	       -Ttext, don't start tracking sections until we find one
+	       of non-zero size or with lma set differently to vma.  */
 	    if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
 		 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
+		&& (os->bfd_section->flags & SEC_ALLOC) != 0
+		&& (os->bfd_section->size != 0
+		    || os->bfd_section->vma != os->bfd_section->lma
+		    || r->last_os != NULL)
 		&& os->lma_region == NULL
 		&& !link_info.relocatable)
 	      r->last_os = s;

Alan Modra
IBM OzLabs - Linux Technology Centre

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