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]

[PATCH] don't return zero too early in elf_sort_sections


The patch covers the following situation. Let's say we have three
sections with the names, sizes and addresses as follow:

	 .empty	  =0    lma_0
	 .foo	  >0	lma_0
	 .bar	  >0	lma_1

The function map_sections_to_segments before doing the actual mapping
it qsorts the array of sections. The compare function elf_sort_section
without this patch might return 0 even if it did not finish comparing
every aspect (_raw_size).

Since qsort is not stable, by returning 0 before checking the sizes
might leave us with the following order (assuming both .foo and .empty
are SEC_LOAD and target_index=0):

      .foo     >0 lma_0
      .empty   =0 lma_0     (wrong, should be lma_1)
      .bar     >0 lma_1

Which could in some cases force map_sections_to_segments to move .bar
into a new segment as the end of .empty might be on a different page
than the start of .bar (depending on the size of .foo).

If this patch is accepted, please also apply.

bfd/ChangeLog:

2001-07-10  Adam Nemet  <anemet@lnxw.com>

	* elf.c (elf_sort_sections): Return zero only as the last step.

Index: elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.74
diff -u -p -r1.74 elf.c
--- elf.c	2001/06/30 04:05:12	1.74
+++ elf.c	2001/07/10 19:06:01
@@ -2677,12 +2677,16 @@ elf_sort_sections (arg1, arg2)
   if (TOEND (sec1))
     {
       if (TOEND (sec2))
-	return sec1->target_index - sec2->target_index;
+        { 
+	  /* If cannot sort by target_index do not return 0 but rather
+	     try the next comparision.  */
+	  if (sec1->target_index - sec2->target_index != 0)
+            return sec1->target_index - sec2->target_index;
+        }
       else
-	return 1;
+        return 1;
     }
-
-  if (TOEND (sec2))
+  else if (TOEND (sec2))
     return -1;
 
 #undef TOEND


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