+2017-08-03  Nick Clifton  <nickc@redhat.com>
+
+       PR ld/21884
+       * testsuite/ld-elf/pr21884.d: Add AVR, HPPA, IA64, M68HC1x and
+       SCORE to list of targets not supporting file format changes during
+       linking.
+       * testsuite/ld-unique/pr21529.d: Likewise.
+       * emultempl/avrelf.em (_before_allocation): Skip for non-ELF
+       output formats.
+       (avr_elf_create_output_section_statements): Fail if the output
+       format is not ELF.
+       (avr_finish): Do not access the ELF header in non-ELF format
+       output bfds.
+       * emultempl/m68hc1xelf.em (_before_allocation): Skip for non-ELF
+       output formats.
+       (m68hc11elf_create_output_section_statements): Fail if the putput
+       format is not ELF.
+       (m68hc11elf_after_allocation): Skip for non-ELF output formats.
+
 2017-08-03  Alan Modra  <amodra@gmail.com>
 
        PR ld/21884
 
 
   gld${EMULATION_NAME}_before_allocation ();
 
+  if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
+    {
+      avr_no_stubs = TRUE;
+      return;
+    }
+
   /* We only need stubs for avr6, avrxmega6, and avrxmega7. */
   if (strcmp ("${EMULATION_NAME}","avr6")
       && strcmp ("${EMULATION_NAME}","avrxmega6")
 {
   flagword flags;
 
+  if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
+    {
+      einfo ("%X%P: changing output format whilst linking is not supported\n");
+      return;
+    }
+
   stub_file = lang_add_input_file ("linker stubs",
                                    lang_input_file_is_fake_enum,
                                    NULL);
     }
 
   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;
+
+  if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
+    {
+      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 ();
 }
 
 
   gld${EMULATION_NAME}_before_allocation ();
 
+  if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
+    return;
+
   /* If generating a relocatable output file, then we don't
      have to generate the trampolines.  */
   if (bfd_link_relocatable (&link_info))
 static void
 m68hc11elf_create_output_section_statements (void)
 {
+  if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
+    {
+      einfo ("%X%P: changing output format whilst linking is not supported\n");
+      return;
+    }
+
   stub_file = lang_add_input_file ("linker stubs",
                                   lang_input_file_is_fake_enum,
                                   NULL);
 static void
 m68hc11elf_after_allocation (void)
 {
-  /* Now build the linker stubs.  */
-  if (stub_file->the_bfd->sections != NULL)
+  if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
     {
-      /* Call again the trampoline analyzer to initialize the trampoline
-        stubs with the correct symbol addresses.  Since there could have
-        been relaxation, the symbol addresses that were found during
-        first call may no longer be correct.  */
-      if (!elf32_m68hc11_size_stubs (link_info.output_bfd,
-                                    stub_file->the_bfd,
-                                    &link_info, 0))
+      /* Now build the linker stubs.  */
+      if (stub_file->the_bfd->sections != NULL)
        {
-         einfo ("%X%P: can not size stub section: %E\n");
-         return;
+         /* Call again the trampoline analyzer to initialize the trampoline
+            stubs with the correct symbol addresses.  Since there could have
+            been relaxation, the symbol addresses that were found during
+            first call may no longer be correct.  */
+         if (!elf32_m68hc11_size_stubs (link_info.output_bfd,
+                                        stub_file->the_bfd,
+                                        &link_info, 0))
+           {
+             einfo ("%X%P: can not size stub section: %E\n");
+             return;
+           }
+         if (!elf32_m68hc11_build_stubs (link_info.output_bfd, &link_info))
+           einfo ("%X%P: can not build stubs: %E\n");
        }
-      if (!elf32_m68hc11_build_stubs (link_info.output_bfd, &link_info))
-       einfo ("%X%P: can not build stubs: %E\n");
     }
 
   gld${EMULATION_NAME}_after_allocation ();