AVR/ld: Propagate link-relax elf header flag correctly.
[binutils-gdb.git] / ld / emultempl / avrelf.em
index 170dc3e3743a3bfb49f8a04d09aa0271a8e8f121..f4e2654801902417f3cb99d47a4a5c04f0f29847 100644 (file)
@@ -28,6 +28,7 @@ fragment <<EOF
 
 #include "elf32-avr.h"
 #include "ldctor.h"
+#include "elf/avr.h"
 
 /* The fake file and it's corresponding section meant to hold
    the linker stubs if needed.  */
@@ -175,6 +176,41 @@ avr_elf_before_parse (void)
   gld${EMULATION_NAME}_before_parse ();
 }
 
+static void
+avr_finish (void)
+{
+  bfd *abfd;
+  bfd_boolean avr_link_relax;
+
+  if (link_info.relocatable)
+    {
+      avr_link_relax = TRUE;
+      for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
+        {
+          /* Don't let the linker stubs prevent the final object being
+             marked as link-relax ready.  */
+          if ((elf_elfheader (abfd)->e_flags
+               & EF_AVR_LINKRELAX_PREPARED) == 0
+              && abfd != stub_file->the_bfd)
+            {
+              avr_link_relax = FALSE;
+              break;
+            }
+        }
+    }
+  else
+    {
+      avr_link_relax = RELAXATION_ENABLED;
+    }
+
+  abfd = link_info.output_bfd;
+  if (avr_link_relax)
+    elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
+  else
+    elf_elfheader (abfd)->e_flags &= ~EF_AVR_LINKRELAX_PREPARED;
+
+  finish_default ();
+}
 EOF
 
 
@@ -274,3 +310,4 @@ LDEMUL_BEFORE_PARSE=avr_elf_before_parse
 LDEMUL_BEFORE_ALLOCATION=avr_elf_${EMULATION_NAME}_before_allocation
 LDEMUL_AFTER_ALLOCATION=avr_elf_after_allocation
 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=avr_elf_create_output_section_statements
+LDEMUL_FINISH=avr_finish