This is the mail archive of the binutils@sourceware.cygnus.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: How to stop --gc-sections nukeing all sections?


On 7 Apr 2000, Ian Lance Taylor wrote:

>    The problem is the reshuffling code in ld/emutempl/elf32.em:gld*_place_orphan.
>    It's attempting to keep sections together, but scrambles their order in the
>    process. I think that's pretty nasty: input sections should generally appear
>    in the same order in the output. Here's a simple patch to "fix" it in elf32.em;
>    the same approach is used in other .em files as well:
> 
> Your patch works OK for ld -r, because in an object file the order of
> the sections is irrelevant (except for considerations like yours).  I
> don't think it is right when doing a final link.  You aren't seeing a
> problem because when doing a final link the input sections do appear
> in the linker script, and thus the code in question does not get
> called.
> 
> The problem here is simply that new sections are added at the start,
> not the end.  I suspect we can fix it by simply adding
>     pps = &(*pps)->next
> after the loop which sets pps in the first place.
> 

I was playing around with the following yesterday.  It leaves the orphan
sections in the right order, but unfortunately doesn't fix the problem
with the start addresses.  The code I added at the end isn't quite right
to do this for some reason, but I haven't had time to fix it.

Index: emultempl/elf32.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/elf32.em,v
retrieving revision 1.12
diff -u -p -r1.12 elf32.em
--- elf32.em	2000/02/29 05:53:53	1.12
+++ elf32.em	2000/04/07 12:41:57
@@ -980,11 +980,15 @@ gld${EMULATION_NAME}_place_orphan (file,
 	     output_bfd->xvec->name, outsecname);
   if (place->bfd_section != NULL)
     {
+      /* Unlink it first.  */
       for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
 	;
       *pps = snew->next;
-      snew->next = place->bfd_section->next;
-      place->bfd_section->next = snew;
+      snew->next = NULL;
+      /* Now tack it on to the end of the "place" list.  */
+      for (pps = &place->bfd_section; *pps; pps = &(*pps)->next)
+	;
+      *pps = snew;
     }
 
   /* Start building a list of statements for this section.  */
@@ -1038,10 +1042,20 @@
 				      exp_nameop (NAME, ".")));
     }
 
+#if 1
   /* Now stick the new statement list right after PLACE.  */
   *add.tail = place->header.next;
   place->header.next = add.head;
-
+#else
+  {
+    lang_statement_union_type **lsu;
+    /* Put the new statement list at the end of "place" list.  */
+    for (lsu = &place->header.next; *lsu; lsu = &(*lsu)->next)
+      ;
+    *add.tail = NULL;
+    *lsu = add.head;
+  }
+#endif
   stat_ptr = old;
 
   return true;

-- 
Linuxcare.  Support for the Revolution.


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