29c9b5a
diff -rup binutils.orig/bfd/.gitignore binutils-2.37/bfd/.gitignore
29c9b5a
--- binutils.orig/bfd/.gitignore	2021-12-01 13:35:33.826078119 +0000
29c9b5a
+++ binutils-2.37/bfd/.gitignore	2021-12-01 13:35:43.138988270 +0000
29c9b5a
@@ -10,6 +10,7 @@
29c9b5a
 /peigen.c
29c9b5a
 /pepigen.c
29c9b5a
 /pex64igen.c
29c9b5a
+/pe-aarch64igen.c
29c9b5a
 /stmp-bfd-h
29c9b5a
 /targmatch.h
29c9b5a
 
29c9b5a
diff -rup binutils.orig/bfd/Makefile.am binutils-2.37/bfd/Makefile.am
29c9b5a
--- binutils.orig/bfd/Makefile.am	2021-12-01 13:35:33.817078206 +0000
29c9b5a
+++ binutils-2.37/bfd/Makefile.am	2021-12-01 13:35:43.139988261 +0000
29c9b5a
@@ -567,7 +567,9 @@ BFD64_BACKENDS = \
29c9b5a
 	mach-o-aarch64.lo \
29c9b5a
 	mach-o-x86-64.lo \
29c9b5a
 	mmo.lo \
29c9b5a
+	pe-aarch64igen.lo \
29c9b5a
 	pe-x86_64.lo \
29c9b5a
+	pei-aarch64lo \
29c9b5a
 	pei-ia64.lo \
29c9b5a
 	pei-x86_64.lo \
29c9b5a
 	pepigen.lo \
29c9b5a
@@ -607,6 +609,7 @@ BFD64_BACKENDS_CFILES = \
29c9b5a
 	mach-o-x86-64.c \
29c9b5a
 	mmo.c \
29c9b5a
 	pe-x86_64.c \
29c9b5a
+	pei-aarch64.c \
29c9b5a
 	pei-ia64.c \
29c9b5a
 	pei-x86_64.c \
29c9b5a
 	vms-alpha.c
29c9b5a
@@ -666,7 +669,7 @@ BUILD_CFILES = \
29c9b5a
 	elf32-aarch64.c elf64-aarch64.c \
29c9b5a
 	elf32-ia64.c elf64-ia64.c \
29c9b5a
 	elf32-riscv.c elf64-riscv.c \
29c9b5a
-	peigen.c pepigen.c pex64igen.c
29c9b5a
+	peigen.c pepigen.c pex64igen.c pe-aarch64igen.c
29c9b5a
 
29c9b5a
 CFILES = $(SOURCE_CFILES) $(BUILD_CFILES)
29c9b5a
 
29c9b5a
@@ -862,6 +865,10 @@ pex64igen.c: peXXigen.c
29c9b5a
 	echo "#line 1 \"peXXigen.c\"" > $@
29c9b5a
 	$(SED) -e s/XX/pex64/g < $< >> $@
29c9b5a
 
29c9b5a
+pe-aarch64igen.c: peXXigen.c
29c9b5a
+	echo "#line 1 \"peXXigen.c\"" > $@
29c9b5a
+	$(SED) -e s/XX/peAArch64/g < $< >> $@
29c9b5a
+
29c9b5a
 BFD_H_DEPS= $(INCDIR)/ansidecl.h $(INCDIR)/symcat.h $(INCDIR)/diagnostics.h
29c9b5a
 LOCAL_H_DEPS= libbfd.h sysdep.h config.h
29c9b5a
 $(BFD32_LIBS) \
29c9b5a
diff -rup binutils.orig/bfd/Makefile.in binutils-2.37/bfd/Makefile.in
29c9b5a
--- binutils.orig/bfd/Makefile.in	2021-12-01 13:35:33.823078148 +0000
29c9b5a
+++ binutils-2.37/bfd/Makefile.in	2021-12-01 13:35:43.139988261 +0000
29c9b5a
@@ -994,7 +994,9 @@ BFD64_BACKENDS = \
29c9b5a
 	mach-o-aarch64.lo \
29c9b5a
 	mach-o-x86-64.lo \
29c9b5a
 	mmo.lo \
29c9b5a
+	pe-aarch64igen.lo \
29c9b5a
 	pe-x86_64.lo \
29c9b5a
+	pei-aarch64.lo \
29c9b5a
 	pei-ia64.lo \
29c9b5a
 	pei-x86_64.lo \
29c9b5a
 	pepigen.lo \
29c9b5a
@@ -1034,6 +1036,7 @@ BFD64_BACKENDS_CFILES = \
29c9b5a
 	mach-o-x86-64.c \
29c9b5a
 	mmo.c \
29c9b5a
 	pe-x86_64.c \
29c9b5a
+	pei-aarch64.c \
29c9b5a
 	pei-ia64.c \
29c9b5a
 	pei-x86_64.c \
29c9b5a
 	vms-alpha.c
29c9b5a
@@ -1092,7 +1095,7 @@ BUILD_CFILES = \
29c9b5a
 	elf32-aarch64.c elf64-aarch64.c \
29c9b5a
 	elf32-ia64.c elf64-ia64.c \
29c9b5a
 	elf32-riscv.c elf64-riscv.c \
29c9b5a
-	peigen.c pepigen.c pex64igen.c
29c9b5a
+	peigen.c pepigen.c pex64igen.c pe-aarch64igen.c
29c9b5a
 
29c9b5a
 CFILES = $(SOURCE_CFILES) $(BUILD_CFILES)
29c9b5a
 SOURCE_HFILES = \
29c9b5a
@@ -1551,9 +1554,11 @@ distclean-compile:
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pei-mcore.Plo@am__quote@
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pei-sh.Plo@am__quote@
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pei-x86_64.Plo@am__quote@
29c9b5a
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pei-aarch64.Plo@am__quote@
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/peigen.Plo@am__quote@
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pepigen.Plo@am__quote@
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pex64igen.Plo@am__quote@
29c9b5a
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pe-aarch64igen.Plo@am__quote@
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/plugin.Plo@am__quote@
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ppcboot.Plo@am__quote@
29c9b5a
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reloc.Plo@am__quote@
29c9b5a
@@ -1991,6 +1996,11 @@ pepigen.c : peXXigen.c
29c9b5a
 pex64igen.c: peXXigen.c
29c9b5a
 	echo "#line 1 \"peXXigen.c\"" > $@
29c9b5a
 	$(SED) -e s/XX/pex64/g < $< >> $@
29c9b5a
+
29c9b5a
+pe-aarch64igen.c: peXXigen.c
29c9b5a
+	echo "#line 1 \"peXXigen.c\"" > $@
29c9b5a
+	$(SED) -e s/XX/peAArch64/g < $< >> $@
29c9b5a
+
29c9b5a
 $(BFD32_LIBS) \
29c9b5a
  $(BFD64_LIBS) \
29c9b5a
  $(ALL_MACHINES) \
29c9b5a
diff -rup binutils.orig/bfd/bfd.c binutils-2.37/bfd/bfd.c
29c9b5a
--- binutils.orig/bfd/bfd.c	2021-12-01 13:35:33.821078167 +0000
29c9b5a
+++ binutils-2.37/bfd/bfd.c	2021-12-01 13:35:43.139988261 +0000
29c9b5a
@@ -1735,6 +1735,7 @@ bfd_get_sign_extend_vma (bfd *abfd)
f5189ec
       || strcmp (name, "pei-i386") == 0
f5189ec
       || strcmp (name, "pe-x86-64") == 0
f5189ec
       || strcmp (name, "pei-x86-64") == 0
f5189ec
+      || strcmp (name, "pei-aarch64-little") == 0
f5189ec
       || strcmp (name, "pe-arm-wince-little") == 0
f5189ec
       || strcmp (name, "pei-arm-wince-little") == 0
f5189ec
       || strcmp (name, "aixcoff-rs6000") == 0
29c9b5a
Only in binutils-2.37/bfd: coff-aarch64.c
29c9b5a
diff -rup binutils.orig/bfd/coffcode.h binutils-2.37/bfd/coffcode.h
29c9b5a
--- binutils.orig/bfd/coffcode.h	2021-12-01 13:35:33.822078158 +0000
29c9b5a
+++ binutils-2.37/bfd/coffcode.h	2021-12-01 13:35:43.140988251 +0000
29c9b5a
@@ -2215,6 +2215,12 @@ coff_set_arch_mach_hook (bfd *abfd, void
29c9b5a
 	}
29c9b5a
       break;
29c9b5a
 #endif
29c9b5a
+#ifdef AARCH64MAGIC
29c9b5a
+    case AARCH64MAGIC:
29c9b5a
+      arch = bfd_arch_aarch64;
29c9b5a
+      machine = internal_f->f_flags & F_AARCH64_ARCHITECTURE_MASK;
29c9b5a
+      break;
f5189ec
+#endif
29c9b5a
 #ifdef Z80MAGIC
29c9b5a
     case Z80MAGIC:
29c9b5a
       arch = bfd_arch_z80;
29c9b5a
@@ -2771,6 +2777,12 @@ coff_set_flags (bfd * abfd,
29c9b5a
       return true;
29c9b5a
 #endif
29c9b5a
 
29c9b5a
+#ifdef AARCH64MAGIC
29c9b5a
+    case bfd_arch_aarch64:
29c9b5a
+      * magicp = AARCH64MAGIC;
29c9b5a
+      return true;
f5189ec
+#endif
f5189ec
+
29c9b5a
 #ifdef ARMMAGIC
29c9b5a
     case bfd_arch_arm:
29c9b5a
 #ifdef ARM_WINCE
29c9b5a
@@ -3866,7 +3878,7 @@ coff_write_object_contents (bfd * abfd)
29c9b5a
     internal_f.f_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
29c9b5a
 #endif
29c9b5a
 
29c9b5a
-#ifndef COFF_WITH_pex64
29c9b5a
+#if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
29c9b5a
 #ifdef COFF_WITH_PE
29c9b5a
   internal_f.f_flags |= IMAGE_FILE_32BIT_MACHINE;
29c9b5a
 #else
29c9b5a
@@ -3914,6 +3926,11 @@ coff_write_object_contents (bfd * abfd)
29c9b5a
 #define __A_MAGIC_SET__
29c9b5a
     internal_a.magic = ZMAGIC;
29c9b5a
 #endif
f5189ec
+
29c9b5a
+#if defined(AARCH64)
29c9b5a
+#define __A_MAGIC_SET__
29c9b5a
+    internal_a.magic = ZMAGIC;
f5189ec
+#endif
29c9b5a
 
29c9b5a
 #if defined MCORE_PE
29c9b5a
 #define __A_MAGIC_SET__
29c9b5a
diff -rup binutils.orig/bfd/config.bfd binutils-2.37/bfd/config.bfd
29c9b5a
--- binutils.orig/bfd/config.bfd	2021-12-01 13:35:33.833078051 +0000
29c9b5a
+++ binutils-2.37/bfd/config.bfd	2021-12-01 13:35:43.140988251 +0000
29c9b5a
@@ -232,7 +232,7 @@ case "${targ}" in
29c9b5a
     ;;
29c9b5a
   aarch64-*-elf | aarch64-*-rtems* | aarch64-*-genode*)
29c9b5a
     targ_defvec=aarch64_elf64_le_vec
29c9b5a
-    targ_selvecs="aarch64_elf64_be_vec aarch64_elf32_le_vec aarch64_elf32_be_vec arm_elf32_le_vec arm_elf32_be_vec"
29c9b5a
+    targ_selvecs="aarch64_elf64_be_vec aarch64_elf32_le_vec aarch64_elf32_be_vec arm_elf32_le_vec arm_elf32_be_vec aarch64_pei_vec"
29c9b5a
     want64=true
29c9b5a
     ;;
29c9b5a
   aarch64_be-*-elf)
29c9b5a
@@ -257,7 +257,7 @@ case "${targ}" in
29c9b5a
     ;;
29c9b5a
   aarch64-*-linux* | aarch64-*-netbsd*)
29c9b5a
     targ_defvec=aarch64_elf64_le_vec
29c9b5a
-    targ_selvecs="aarch64_elf64_be_vec aarch64_elf32_le_vec aarch64_elf32_be_vec arm_elf32_le_vec arm_elf32_be_vec"
29c9b5a
+    targ_selvecs="aarch64_elf64_be_vec aarch64_elf32_le_vec aarch64_elf32_be_vec arm_elf32_le_vec arm_elf32_be_vec aarch64_pei_vec"
29c9b5a
     want64=true
29c9b5a
     ;;
29c9b5a
   aarch64_be-*-linux* | aarch64_be-*-netbsd*)
29c9b5a
diff -rup binutils.orig/bfd/configure binutils-2.37/bfd/configure
29c9b5a
--- binutils.orig/bfd/configure	2021-12-01 13:35:33.817078206 +0000
29c9b5a
+++ binutils-2.37/bfd/configure	2021-12-01 13:35:43.141988241 +0000
29c9b5a
@@ -13283,6 +13283,7 @@ do
29c9b5a
     aarch64_elf64_le_vec)	   tb="$tb elf64-aarch64.lo elfxx-aarch64.lo elf-ifunc.lo elf64.lo $elf"; target_size=64 ;;
29c9b5a
     aarch64_elf64_le_cloudabi_vec) tb="$tb elf64-aarch64.lo elfxx-aarch64.lo elf-ifunc.lo elf64.lo $elf"; target_size=64 ;;
29c9b5a
     aarch64_mach_o_vec)		 tb="$tb mach-o-aarch64.lo"; target_size=64 ;;
29c9b5a
+    aarch64_pei_vec)		 tb="$tb pei-aarch64.lo pe-aarch64igen.lo $coff"; target_size=64 ;;
29c9b5a
     alpha_ecoff_le_vec)		 tb="$tb coff-alpha.lo ecoff.lo $ecoff"; target_size=64 ;;
29c9b5a
     alpha_elf64_vec)		 tb="$tb elf64-alpha.lo elf64.lo $elf"; target_size=64 ;;
29c9b5a
     alpha_elf64_fbsd_vec)	 tb="$tb elf64-alpha.lo elf64.lo $elf"; target_size=64 ;;
29c9b5a
diff -rup binutils.orig/bfd/configure.ac binutils-2.37/bfd/configure.ac
29c9b5a
--- binutils.orig/bfd/configure.ac	2021-12-01 13:35:33.829078090 +0000
29c9b5a
+++ binutils-2.37/bfd/configure.ac	2021-12-01 13:35:43.141988241 +0000
29c9b5a
@@ -439,6 +439,7 @@ do
29c9b5a
     aarch64_elf64_le_vec)	   tb="$tb elf64-aarch64.lo elfxx-aarch64.lo elf-ifunc.lo elf64.lo $elf"; target_size=64 ;;
29c9b5a
     aarch64_elf64_le_cloudabi_vec) tb="$tb elf64-aarch64.lo elfxx-aarch64.lo elf-ifunc.lo elf64.lo $elf"; target_size=64 ;;
29c9b5a
     aarch64_mach_o_vec)		 tb="$tb mach-o-aarch64.lo"; target_size=64 ;;
29c9b5a
+    aarch64_pei_vec)		 tb="$tb pei-aarch64.lo pe-aarch64igen.lo $coff"; target_size=64 ;;
29c9b5a
     alpha_ecoff_le_vec)		 tb="$tb coff-alpha.lo ecoff.lo $ecoff"; target_size=64 ;;
29c9b5a
     alpha_elf64_vec)		 tb="$tb elf64-alpha.lo elf64.lo $elf"; target_size=64 ;;
29c9b5a
     alpha_elf64_fbsd_vec)	 tb="$tb elf64-alpha.lo elf64.lo $elf"; target_size=64 ;;
29c9b5a
diff -rup binutils.orig/bfd/libpei.h binutils-2.37/bfd/libpei.h
29c9b5a
--- binutils.orig/bfd/libpei.h	2021-12-01 13:35:33.830078081 +0000
29c9b5a
+++ binutils-2.37/bfd/libpei.h	2021-12-01 13:35:43.141988241 +0000
29c9b5a
@@ -275,6 +275,41 @@
29c9b5a
 #define _bfd_XXi_write_codeview_record			_bfd_pepi_write_codeview_record
29c9b5a
 #define _bfd_XXi_slurp_codeview_record			_bfd_pepi_slurp_codeview_record
29c9b5a
 
29c9b5a
+#elif defined COFF_WITH_peAArch64
f5189ec
+
29c9b5a
+#define GET_OPTHDR_IMAGE_BASE		 H_GET_64
29c9b5a
+#define PUT_OPTHDR_IMAGE_BASE		 H_PUT_64
29c9b5a
+#define GET_OPTHDR_SIZE_OF_STACK_RESERVE H_GET_64
29c9b5a
+#define PUT_OPTHDR_SIZE_OF_STACK_RESERVE H_PUT_64
29c9b5a
+#define GET_OPTHDR_SIZE_OF_STACK_COMMIT	 H_GET_64
29c9b5a
+#define PUT_OPTHDR_SIZE_OF_STACK_COMMIT	 H_PUT_64
29c9b5a
+#define GET_OPTHDR_SIZE_OF_HEAP_RESERVE	 H_GET_64
29c9b5a
+#define PUT_OPTHDR_SIZE_OF_HEAP_RESERVE	 H_PUT_64
29c9b5a
+#define GET_OPTHDR_SIZE_OF_HEAP_COMMIT	 H_GET_64
29c9b5a
+#define PUT_OPTHDR_SIZE_OF_HEAP_COMMIT	 H_PUT_64
29c9b5a
+#define GET_PDATA_ENTRY			 bfd_get_32
f5189ec
+
29c9b5a
+#define _bfd_XX_bfd_copy_private_bfd_data_common	_bfd_peAArch64_bfd_copy_private_bfd_data_common
29c9b5a
+#define _bfd_XX_bfd_copy_private_section_data		_bfd_peAArch64_bfd_copy_private_section_data
29c9b5a
+#define _bfd_XX_get_symbol_info				_bfd_peAArch64_get_symbol_info
29c9b5a
+#define _bfd_XX_only_swap_filehdr_out			_bfd_peAArch64_only_swap_filehdr_out
29c9b5a
+#define _bfd_XX_print_private_bfd_data_common		_bfd_peAArch64_print_private_bfd_data_common
29c9b5a
+#define _bfd_XXi_final_link_postscript			_bfd_peAArch64i_final_link_postscript
29c9b5a
+#define _bfd_XXi_only_swap_filehdr_out			_bfd_peAArch64i_only_swap_filehdr_out
29c9b5a
+#define _bfd_XXi_swap_aouthdr_in			_bfd_peAArch64i_swap_aouthdr_in
29c9b5a
+#define _bfd_XXi_swap_aouthdr_out			_bfd_peAArch64i_swap_aouthdr_out
29c9b5a
+#define _bfd_XXi_swap_aux_in				_bfd_peAArch64i_swap_aux_in
29c9b5a
+#define _bfd_XXi_swap_aux_out				_bfd_peAArch64i_swap_aux_out
29c9b5a
+#define _bfd_XXi_swap_lineno_in				_bfd_peAArch64i_swap_lineno_in
29c9b5a
+#define _bfd_XXi_swap_lineno_out			_bfd_peAArch64i_swap_lineno_out
29c9b5a
+#define _bfd_XXi_swap_scnhdr_out			_bfd_peAArch64i_swap_scnhdr_out
29c9b5a
+#define _bfd_XXi_swap_sym_in				_bfd_peAArch64i_swap_sym_in
29c9b5a
+#define _bfd_XXi_swap_sym_out				_bfd_peAArch64i_swap_sym_out
29c9b5a
+#define _bfd_XXi_swap_debugdir_in			_bfd_peAArch64i_swap_debugdir_in
29c9b5a
+#define _bfd_XXi_swap_debugdir_out			_bfd_peAArch64i_swap_debugdir_out
29c9b5a
+#define _bfd_XXi_write_codeview_record			_bfd_peAArch64i_write_codeview_record
29c9b5a
+#define _bfd_XXi_slurp_codeview_record			_bfd_peAArch64i_slurp_codeview_record
f5189ec
+
29c9b5a
 #else /* !COFF_WITH_pep */
29c9b5a
 
29c9b5a
 #define GET_OPTHDR_IMAGE_BASE H_GET_32
29c9b5a
@@ -369,5 +404,6 @@ bool _bfd_XX_bfd_copy_private_section_da
29c9b5a
 bool _bfd_pe_print_ce_compressed_pdata (bfd *, void *);
29c9b5a
 bool _bfd_pe64_print_ce_compressed_pdata (bfd *, void *);
29c9b5a
 bool _bfd_pex64_print_ce_compressed_pdata (bfd *, void *);
29c9b5a
+bool _bfd_peAArch64_print_ce_compressed_pdata (bfd *, void *);
29c9b5a
 bool _bfd_pep_print_ce_compressed_pdata (bfd *, void *);
29c9b5a
 
29c9b5a
diff -rup binutils.orig/bfd/peXXigen.c binutils-2.37/bfd/peXXigen.c
29c9b5a
--- binutils.orig/bfd/peXXigen.c	2021-12-01 13:35:33.827078110 +0000
29c9b5a
+++ binutils-2.37/bfd/peXXigen.c	2021-12-01 13:35:43.142988232 +0000
f5189ec
@@ -60,8 +60,9 @@
f5189ec
    on this code has a chance of getting something accomplished without
f5189ec
    wasting too much time.  */
f5189ec
 
f5189ec
-/* This expands into COFF_WITH_pe, COFF_WITH_pep, or COFF_WITH_pex64
f5189ec
-   depending on whether we're compiling for straight PE or PE+.  */
f5189ec
+/* This expands into COFF_WITH_pe, COFF_WITH_pep, COFF_WITH_pex64 or
f5189ec
+   COFF_WITH_peAArch64 depending on whether we're compiling for straight
f5189ec
+   PE or PE+.  */
f5189ec
 #define COFF_WITH_XX
f5189ec
 
f5189ec
 #include "sysdep.h"
f5189ec
@@ -83,6 +84,8 @@
f5189ec
 # include "coff/x86_64.h"
f5189ec
 #elif defined COFF_WITH_pep
f5189ec
 # include "coff/ia64.h"
f5189ec
+#elif defined COFF_WITH_peAArch64
f5189ec
+# include "coff/aarch64.h"
f5189ec
 #else
f5189ec
 # include "coff/i386.h"
f5189ec
 #endif
f5189ec
@@ -92,7 +95,7 @@
f5189ec
 #include "libpei.h"
f5189ec
 #include "safe-ctype.h"
f5189ec
 
f5189ec
-#if defined COFF_WITH_pep || defined COFF_WITH_pex64
f5189ec
+#if defined COFF_WITH_pep || defined COFF_WITH_pex64 || defined COFF_WITH_peAArch64
f5189ec
 # undef AOUTSZ
f5189ec
 # define AOUTSZ		PEPAOUTSZ
f5189ec
 # define PEAOUTHDR	PEPAOUTHDR
f5189ec
@@ -469,7 +472,7 @@ _bfd_XXi_swap_aouthdr_in (bfd * abfd,
f5189ec
   aouthdr_int->text_start =
f5189ec
     GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
f5189ec
 
f5189ec
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
   /* PE32+ does not have data_start member!  */
f5189ec
   aouthdr_int->data_start =
f5189ec
     GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
f5189ec
@@ -555,7 +558,7 @@ _bfd_XXi_swap_aouthdr_in (bfd * abfd,
f5189ec
   if (aouthdr_int->entry)
f5189ec
     {
f5189ec
       aouthdr_int->entry += a->ImageBase;
f5189ec
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
       aouthdr_int->entry &= 0xffffffff;
f5189ec
 #endif
f5189ec
     }
f5189ec
@@ -563,12 +566,12 @@ _bfd_XXi_swap_aouthdr_in (bfd * abfd,
f5189ec
   if (aouthdr_int->tsize)
f5189ec
     {
f5189ec
       aouthdr_int->text_start += a->ImageBase;
f5189ec
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
       aouthdr_int->text_start &= 0xffffffff;
f5189ec
 #endif
f5189ec
     }
f5189ec
 
f5189ec
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
   /* PE32+ does not have data_start member!  */
f5189ec
   if (aouthdr_int->dsize)
f5189ec
     {
29c9b5a
@@ -628,7 +631,7 @@ _bfd_XXi_swap_aouthdr_out (bfd * abfd, v
f5189ec
   if (aouthdr_in->tsize)
f5189ec
     {
f5189ec
       aouthdr_in->text_start -= ib;
f5189ec
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
       aouthdr_in->text_start &= 0xffffffff;
f5189ec
 #endif
f5189ec
     }
29c9b5a
@@ -636,7 +639,7 @@ _bfd_XXi_swap_aouthdr_out (bfd * abfd, v
f5189ec
   if (aouthdr_in->dsize)
f5189ec
     {
f5189ec
       aouthdr_in->data_start -= ib;
f5189ec
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
       aouthdr_in->data_start &= 0xffffffff;
f5189ec
 #endif
f5189ec
     }
29c9b5a
@@ -644,7 +647,7 @@ _bfd_XXi_swap_aouthdr_out (bfd * abfd, v
f5189ec
   if (aouthdr_in->entry)
f5189ec
     {
f5189ec
       aouthdr_in->entry -= ib;
f5189ec
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
       aouthdr_in->entry &= 0xffffffff;
f5189ec
 #endif
f5189ec
     }
29c9b5a
@@ -748,7 +751,7 @@ _bfd_XXi_swap_aouthdr_out (bfd * abfd, v
f5189ec
   PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
f5189ec
 			  aouthdr_out->standard.text_start);
f5189ec
 
f5189ec
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
   /* PE32+ does not have data_start member!  */
f5189ec
   PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
f5189ec
 			  aouthdr_out->standard.data_start);
29c9b5a
@@ -1800,7 +1803,7 @@ pe_print_edata (bfd * abfd, void * vfile
f5189ec
 static bool
f5189ec
 pe_print_pdata (bfd * abfd, void * vfile)
f5189ec
 {
f5189ec
-#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
 # define PDATA_ROW_SIZE	(3 * 8)
f5189ec
 #else
f5189ec
 # define PDATA_ROW_SIZE	(5 * 4)
29c9b5a
@@ -1827,7 +1830,7 @@ pe_print_pdata (bfd * abfd, void * vfile
f5189ec
 
f5189ec
   fprintf (file,
f5189ec
 	   _("\nThe Function Table (interpreted .pdata section contents)\n"));
f5189ec
-#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
f5189ec
+#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
   fprintf (file,
f5189ec
 	   _(" vma:\t\t\tBegin Address    End Address      Unwind Info\n"));
f5189ec
 #else
29c9b5a
@@ -1864,7 +1867,7 @@ pe_print_pdata (bfd * abfd, void * vfile
f5189ec
       bfd_vma eh_handler;
f5189ec
       bfd_vma eh_data;
f5189ec
       bfd_vma prolog_end_addr;
f5189ec
-#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
f5189ec
+#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
f5189ec
       int em_data;
f5189ec
 #endif
f5189ec
 
29c9b5a
@@ -1882,7 +1885,7 @@ pe_print_pdata (bfd * abfd, void * vfile
29c9b5a
 	/* We are probably into the padding of the section now.  */
29c9b5a
 	break;
f5189ec
 
29c9b5a
-#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
29c9b5a
+#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
29c9b5a
       em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
29c9b5a
 #endif
29c9b5a
       eh_handler &= ~(bfd_vma) 0x3;
29c9b5a
@@ -1893,7 +1896,7 @@ pe_print_pdata (bfd * abfd, void * vfile
29c9b5a
       bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
29c9b5a
       bfd_fprintf_vma (abfd, file, end_addr); fputc (' ', file);
29c9b5a
       bfd_fprintf_vma (abfd, file, eh_handler);
29c9b5a
-#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
29c9b5a
+#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
29c9b5a
       fputc (' ', file);
29c9b5a
       bfd_fprintf_vma (abfd, file, eh_data); fputc (' ', file);
29c9b5a
       bfd_fprintf_vma (abfd, file, prolog_end_addr);
29c9b5a
@@ -2784,7 +2787,7 @@ _bfd_XX_print_private_bfd_data_common (b
29c9b5a
   bfd_fprintf_vma (abfd, file, i->AddressOfEntryPoint);
29c9b5a
   fprintf (file, "\nBaseOfCode\t\t");
29c9b5a
   bfd_fprintf_vma (abfd, file, i->BaseOfCode);
29c9b5a
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
29c9b5a
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
29c9b5a
   /* PE32+ does not have BaseOfData member!  */
29c9b5a
   fprintf (file, "\nBaseOfData\t\t");
29c9b5a
   bfd_fprintf_vma (abfd, file, i->BaseOfData);
29c9b5a
@@ -3085,7 +3088,7 @@ _bfd_XX_get_symbol_info (bfd * abfd, asy
29c9b5a
   coff_get_symbol_info (abfd, symbol, ret);
29c9b5a
 }
f5189ec
 
29c9b5a
-#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
29c9b5a
+#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64) && defined(COFF_WITH_peAArch64)
29c9b5a
 static int
29c9b5a
 sort_x64_pdata (const void *l, const void *r)
29c9b5a
 {
29c9b5a
@@ -4504,7 +4507,7 @@ _bfd_XXi_final_link_postscript (bfd * ab
29c9b5a
 	the TLS data directory consists of 4 pointers, followed
29c9b5a
 	by two 4-byte integer. This implies that the total size
29c9b5a
 	is different for 32-bit and 64-bit executables.  */
29c9b5a
-#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
29c9b5a
+#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
29c9b5a
       pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
29c9b5a
 #else
29c9b5a
       pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x28;
29c9b5a
@@ -4513,7 +4516,7 @@ _bfd_XXi_final_link_postscript (bfd * ab
f5189ec
 
29c9b5a
 /* If there is a .pdata section and we have linked pdata finally, we
29c9b5a
      need to sort the entries ascending.  */
29c9b5a
-#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
29c9b5a
+#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64) && defined(COFF_WITH_peAArch64)
29c9b5a
   {
29c9b5a
     asection *sec = bfd_get_section_by_name (abfd, ".pdata");
f5189ec
 
29c9b5a
Only in binutils-2.37/bfd: pei-aarch64.c
29c9b5a
diff -rup binutils.orig/bfd/peicode.h binutils-2.37/bfd/peicode.h
29c9b5a
--- binutils.orig/bfd/peicode.h	2021-12-01 13:35:33.829078090 +0000
29c9b5a
+++ binutils-2.37/bfd/peicode.h	2021-12-01 13:35:43.142988232 +0000
29c9b5a
@@ -231,7 +231,7 @@ coff_swap_scnhdr_in (bfd * abfd, void *
f5189ec
     {
f5189ec
       scnhdr_int->s_vaddr += pe_data (abfd)->pe_opthdr.ImageBase;
f5189ec
       /* Do not cut upper 32-bits for 64-bit vma.  */
f5189ec
-#ifndef COFF_WITH_pex64
f5189ec
+#if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
f5189ec
       scnhdr_int->s_vaddr &= 0xffffffff;
f5189ec
 #endif
f5189ec
     }
f5189ec
@@ -738,6 +738,16 @@ static const jump_table jtab[] =
f5189ec
   },
f5189ec
 #endif
f5189ec
 
f5189ec
+#ifdef AARCH64MAGIC
f5189ec
+/* We don't currently support jumping to DLLs, so if
f5189ec
+   someone does try emit a runtime trap.  Through UDF #0.  */
f5189ec
+  { AARCH64MAGIC,
f5189ec
+    { 0x00, 0x00, 0x00, 0x00 },
f5189ec
+    4, 0
f5189ec
+  },
f5189ec
+
f5189ec
+#endif
f5189ec
+
f5189ec
 #ifdef  ARMPEMAGIC
f5189ec
   { ARMPEMAGIC,
f5189ec
     { 0x00, 0xc0, 0x9f, 0xe5, 0x00, 0xf0,
f5189ec
@@ -910,7 +920,7 @@ pe_ILF_build_a_bfd (bfd *	    abfd,
f5189ec
 	/* See PR 20907 for a reproducer.  */
f5189ec
 	goto error_return;
f5189ec
 
f5189ec
-#ifdef COFF_WITH_pex64
f5189ec
+#if defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
f5189ec
       ((unsigned int *) id4->contents)[0] = ordinal;
f5189ec
       ((unsigned int *) id4->contents)[1] = 0x80000000;
f5189ec
       ((unsigned int *) id5->contents)[0] = ordinal;
f5189ec
@@ -1206,6 +1216,12 @@ pe_ILF_object_p (bfd * abfd)
f5189ec
 #endif
f5189ec
       break;
f5189ec
 
f5189ec
+    case IMAGE_FILE_MACHINE_ARM64:
f5189ec
+#ifdef AARCH64MAGIC
f5189ec
+      magic = AARCH64MAGIC;
f5189ec
+#endif
f5189ec
+      break;
f5189ec
+
f5189ec
     case IMAGE_FILE_MACHINE_THUMB:
f5189ec
 #ifdef THUMBPEMAGIC
f5189ec
       {
29c9b5a
diff -rup binutils.orig/bfd/targets.c binutils-2.37/bfd/targets.c
29c9b5a
--- binutils.orig/bfd/targets.c	2021-12-01 13:35:33.829078090 +0000
29c9b5a
+++ binutils-2.37/bfd/targets.c	2021-12-01 13:35:43.142988232 +0000
29c9b5a
@@ -679,6 +679,7 @@ extern const bfd_target aarch64_elf64_be
29c9b5a
 extern const bfd_target aarch64_elf64_le_vec;
29c9b5a
 extern const bfd_target aarch64_elf64_le_cloudabi_vec;
29c9b5a
 extern const bfd_target aarch64_mach_o_vec;
29c9b5a
+extern const bfd_target aarch64_pei_vec;
29c9b5a
 extern const bfd_target alpha_ecoff_le_vec;
29c9b5a
 extern const bfd_target alpha_elf64_vec;
29c9b5a
 extern const bfd_target alpha_elf64_fbsd_vec;
29c9b5a
@@ -991,6 +992,7 @@ static const bfd_target * const _bfd_tar
29c9b5a
 	&aarch64_elf64_le_vec,
29c9b5a
 	&aarch64_elf64_le_cloudabi_vec,
29c9b5a
 	&aarch64_mach_o_vec,
29c9b5a
+	&aarch64_pei_vec,
29c9b5a
 #endif
29c9b5a
 
29c9b5a
 #ifdef BFD64
29c9b5a
diff -rup binutils.orig/binutils/NEWS binutils-2.37/binutils/NEWS
29c9b5a
--- binutils.orig/binutils/NEWS	2021-12-01 13:35:33.320083001 +0000
29c9b5a
+++ binutils-2.37/binutils/NEWS	2021-12-01 13:35:43.142988232 +0000
29c9b5a
@@ -9,6 +9,9 @@
29c9b5a
   using --unicode=highlight will display them as unicode escape sequences
29c9b5a
   highlighted in red (if supported by the output device).
29c9b5a
 
29c9b5a
+* Support for efi-app-aarch64, efi-rtdrv-aarch64 and efi-bsdrv-aarch64 has been
29c9b5a
+  added to objcopy in order to enable UEFI development using binutils.
29c9b5a
+
29c9b5a
 Changes in 2.37:
29c9b5a
 
29c9b5a
 * The readelf tool has a new command line option which can be used to specify
29c9b5a
Only in binutils.orig/binutils: NEWS.orig
29c9b5a
Only in binutils.orig/binutils: NEWS.rej
29c9b5a
Only in binutils.orig/binutils: nm.c.orig
29c9b5a
diff -rup binutils.orig/binutils/objcopy.c binutils-2.37/binutils/objcopy.c
29c9b5a
--- binutils.orig/binutils/objcopy.c	2021-12-01 13:35:33.302083174 +0000
29c9b5a
+++ binutils-2.37/binutils/objcopy.c	2021-12-01 13:35:43.143988222 +0000
29c9b5a
@@ -4987,6 +4987,13 @@ convert_efi_target (char *efi)
29c9b5a
       /* Change x86_64 to x86-64.  */
29c9b5a
       efi[7] = '-';
29c9b5a
     }
29c9b5a
+  else if (strcmp (efi + 4, "aarch64") == 0)
29c9b5a
+    {
29c9b5a
+      /* Change aarch64 to aarch64-little.  */
29c9b5a
+      efi = (char *) xrealloc (efi, strlen (efi) + 7);
29c9b5a
+      char *t = "aarch64-little";
29c9b5a
+      strcpy (efi + 4, t);
29c9b5a
+    }
29c9b5a
 }
29c9b5a
 
29c9b5a
 /* Allocate and return a pointer to a struct section_add, initializing the
29c9b5a
Only in binutils.orig/binutils: objdump.c.orig
29c9b5a
Only in binutils.orig/binutils: objdump.c.rej
29c9b5a
Only in binutils.orig/binutils: readelf.c.orig
29c9b5a
Only in binutils-2.37/binutils/testsuite/binutils-all/aarch64: pei-aarch64-little.d
29c9b5a
Only in binutils-2.37/binutils/testsuite/binutils-all/aarch64: pei-aarch64-little.s
29c9b5a
Only in binutils-2.37/include/coff: aarch64.h
29c9b5a
diff -rup binutils.orig/include/coff/pe.h binutils-2.37/include/coff/pe.h
29c9b5a
--- binutils.orig/include/coff/pe.h	2021-12-01 13:35:33.251083666 +0000
29c9b5a
+++ binutils-2.37/include/coff/pe.h	2021-12-01 13:35:43.143988222 +0000
29c9b5a
@@ -137,6 +137,7 @@
29c9b5a
 #define IMAGE_FILE_MACHINE_AM33              0x01d3
29c9b5a
 #define IMAGE_FILE_MACHINE_AMD64             0x8664
29c9b5a
 #define IMAGE_FILE_MACHINE_ARM               0x01c0
29c9b5a
+#define IMAGE_FILE_MACHINE_ARM64             0xaa64
29c9b5a
 #define IMAGE_FILE_MACHINE_AXP64             IMAGE_FILE_MACHINE_ALPHA64
29c9b5a
 #define IMAGE_FILE_MACHINE_CEE               0xc0ee
29c9b5a
 #define IMAGE_FILE_MACHINE_CEF               0x0cef
29c9b5a
--- /dev/null	2021-12-01 09:13:46.241760896 +0000
29c9b5a
+++ binutils-2.37/include/coff/aarch64.h	2021-12-01 13:35:43.143988222 +0000
f5189ec
@@ -0,0 +1,63 @@
f5189ec
+/* AArch64 COFF support for BFD.
f5189ec
+   Copyright (C) 2021 Free Software Foundation, Inc.
f5189ec
+
f5189ec
+   This file is part of BFD, the Binary File Descriptor library.
f5189ec
+
f5189ec
+   This program is free software; you can redistribute it and/or modify
f5189ec
+   it under the terms of the GNU General Public License as published by
f5189ec
+   the Free Software Foundation; either version 3 of the License, or
f5189ec
+   (at your option) any later version.
f5189ec
+
f5189ec
+   This program is distributed in the hope that it will be useful,
f5189ec
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
f5189ec
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f5189ec
+   GNU General Public License for more details.
f5189ec
+
f5189ec
+   You should have received a copy of the GNU General Public License
f5189ec
+   along with this program; if not, write to the Free Software Foundation,
f5189ec
+   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
f5189ec
+
f5189ec
+#define COFFAARCH64 1
f5189ec
+
f5189ec
+#define L_LNNO_SIZE 2
f5189ec
+#define INCLUDE_COMDAT_FIELDS_IN_AUXENT
f5189ec
+#include "coff/external.h"
f5189ec
+
f5189ec
+#define F_AARCH64_ARCHITECTURE_MASK	(0x4000)
f5189ec
+
f5189ec
+#define	AARCH64MAGIC	0xaa64  /* From Microsoft specification. */
f5189ec
+
f5189ec
+#undef  BADMAG
f5189ec
+#define BADMAG(x) ((x).f_magic != AARCH64MAGIC)
f5189ec
+#define AARCH64         1                 /* Customize coffcode.h.  */
f5189ec
+
f5189ec
+#define IMAGE_NT_OPTIONAL_HDR64_MAGIC      0x20b
f5189ec
+
f5189ec
+#define OMAGIC          0404    /* Object files, eg as output.  */
f5189ec
+#define ZMAGIC          IMAGE_NT_OPTIONAL_HDR64_MAGIC    /* Demand load format, eg normal ld output 0x10b.  */
f5189ec
+#define STMAGIC		0401	/* Target shlib.  */
f5189ec
+#define SHMAGIC		0443	/* Host   shlib.  */
f5189ec
+
f5189ec
+/* define some NT default values */
f5189ec
+/*  #define NT_IMAGE_BASE        0x400000 moved to internal.h */
f5189ec
+#define NT_SECTION_ALIGNMENT 0x1000
f5189ec
+#define NT_FILE_ALIGNMENT    0x200
f5189ec
+#define NT_DEF_RESERVE       0x100000
f5189ec
+#define NT_DEF_COMMIT        0x1000
f5189ec
+
f5189ec
+/* We use the .rdata section to hold read only data.  */
f5189ec
+#define _LIT	".rdata"
f5189ec
+
f5189ec
+/********************** RELOCATION DIRECTIVES **********************/
f5189ec
+struct external_reloc
f5189ec
+{
f5189ec
+  char r_vaddr[4];
f5189ec
+  char r_symndx[4];
f5189ec
+  char r_type[2];
f5189ec
+  char r_offset[4];
f5189ec
+};
f5189ec
+
f5189ec
+#define RELOC struct external_reloc
f5189ec
+#define RELSZ 14
f5189ec
+
f5189ec
+#define ARM_NOTE_SECTION ".note"
29c9b5a
--- /dev/null	2021-12-01 09:13:46.241760896 +0000
29c9b5a
+++ binutils-2.37/bfd/coff-aarch64.c	2021-12-01 13:35:43.139988261 +0000
29c9b5a
@@ -0,0 +1,166 @@
29c9b5a
+/* BFD back-end for AArch64 COFF files.
29c9b5a
+   Copyright (C) 2021 Free Software Foundation, Inc.
29c9b5a
+
29c9b5a
+   This file is part of BFD, the Binary File Descriptor library.
29c9b5a
+
29c9b5a
+   This program is free software; you can redistribute it and/or modify
29c9b5a
+   it under the terms of the GNU General Public License as published by
29c9b5a
+   the Free Software Foundation; either version 3 of the License, or
29c9b5a
+   (at your option) any later version.
29c9b5a
+
29c9b5a
+   This program is distributed in the hope that it will be useful,
29c9b5a
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
29c9b5a
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29c9b5a
+   GNU General Public License for more details.
29c9b5a
+
29c9b5a
+   You should have received a copy of the GNU General Public License
29c9b5a
+   along with this program; if not, write to the Free Software
29c9b5a
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
29c9b5a
+   MA 02110-1301, USA.  */
29c9b5a
+
29c9b5a
+
29c9b5a
+#ifndef COFF_WITH_peAArch64
29c9b5a
+#define COFF_WITH_peAArch64
29c9b5a
+#endif
29c9b5a
+
29c9b5a
+/* Note we have to make sure not to include headers twice.
29c9b5a
+   Not all headers are wrapped in #ifdef guards, so we define
29c9b5a
+   PEI_HEADERS to prevent double including here.  */
29c9b5a
+#ifndef PEI_HEADERS
29c9b5a
+#include "sysdep.h"
29c9b5a
+#include "bfd.h"
29c9b5a
+#include "libbfd.h"
29c9b5a
+#include "coff/aarch64.h"
29c9b5a
+#include "coff/internal.h"
29c9b5a
+#include "coff/pe.h"
29c9b5a
+#include "libcoff.h"
29c9b5a
+#include "libiberty.h"
29c9b5a
+#endif
29c9b5a
+
29c9b5a
+#include "libcoff.h"
29c9b5a
+
29c9b5a
+/* The page size is a guess based on ELF.  */
29c9b5a
+
29c9b5a
+#define COFF_PAGE_SIZE 0x1000
29c9b5a
+
29c9b5a
+/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
29c9b5a
+#define OCTETS_PER_BYTE(ABFD, SEC) 1
29c9b5a
+
29c9b5a
+#ifndef PCRELOFFSET
29c9b5a
+#define PCRELOFFSET true
29c9b5a
+#endif
29c9b5a
+
29c9b5a
+/* Currently we don't handle any relocations.  */
29c9b5a
+static reloc_howto_type pe_aarch64_std_reloc_howto[] =
29c9b5a
+  {
29c9b5a
+
29c9b5a
+  };
29c9b5a
+
29c9b5a
+#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER  2
29c9b5a
+#define COFF_PAGE_SIZE			      0x1000
29c9b5a
+
29c9b5a
+#ifndef NUM_ELEM
29c9b5a
+#define NUM_ELEM(a) ((sizeof (a)) / sizeof ((a)[0]))
29c9b5a
+#endif
29c9b5a
+
29c9b5a
+#define NUM_RELOCS NUM_ELEM (pe_aarch64_std_reloc_howto)
29c9b5a
+
29c9b5a
+#define RTYPE2HOWTO(cache_ptr, dst)             \
29c9b5a
+  (cache_ptr)->howto = NULL
29c9b5a
+
29c9b5a
+#ifndef bfd_pe_print_pdata
29c9b5a
+#define bfd_pe_print_pdata      NULL
29c9b5a
+#endif
f5189ec
+
29c9b5a
+/* Return TRUE if this relocation should
29c9b5a
+   appear in the output .reloc section.  */
f5189ec
+
29c9b5a
+static bool
29c9b5a
+in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
29c9b5a
+            reloc_howto_type * howto)
29c9b5a
+{
29c9b5a
+  return !howto->pc_relative;
29c9b5a
+}
f5189ec
+
29c9b5a
+#include "coffcode.h"
f5189ec
+
29c9b5a
+/* Target vectors.  */
29c9b5a
+const bfd_target
29c9b5a
+#ifdef TARGET_SYM
29c9b5a
+  TARGET_SYM =
29c9b5a
+#else
29c9b5a
+  aarch64_pei_vec =
29c9b5a
+#endif
29c9b5a
+{
29c9b5a
+#ifdef TARGET_NAME
29c9b5a
+  TARGET_NAME,
29c9b5a
+#else
29c9b5a
+ "pei-aarch64-little",			/* Name.  */
29c9b5a
+#endif
29c9b5a
+  bfd_target_coff_flavour,
29c9b5a
+  BFD_ENDIAN_LITTLE,		/* Data byte order is little.  */
29c9b5a
+  BFD_ENDIAN_LITTLE,		/* Header byte order is little.  */
f5189ec
+
29c9b5a
+  (HAS_RELOC | EXEC_P		/* Object flags.  */
29c9b5a
+   | HAS_LINENO | HAS_DEBUG
29c9b5a
+   | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS),
29c9b5a
+
29c9b5a
+  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags.  */
29c9b5a
+#if defined(COFF_WITH_PE)
29c9b5a
+   | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING
29c9b5a
+#endif
29c9b5a
+   | SEC_CODE | SEC_DATA | SEC_EXCLUDE ),
29c9b5a
+
29c9b5a
+#ifdef TARGET_UNDERSCORE
29c9b5a
+  TARGET_UNDERSCORE,		/* Leading underscore.  */
29c9b5a
+#else
29c9b5a
+  0,				/* Leading underscore.  */
29c9b5a
+#endif
29c9b5a
+  '/',				/* Ar_pad_char.  */
29c9b5a
+  15,				/* Ar_max_namelen.  */
29c9b5a
+  0,				/* match priority.  */
29c9b5a
+  TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
29c9b5a
+
29c9b5a
+     /* Data conversion functions.  */
29c9b5a
+     bfd_getl64, bfd_getl_signed_64, bfd_putl64,
29c9b5a
+     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
29c9b5a
+     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data.  */
29c9b5a
+     /* Header conversion functions.  */
29c9b5a
+     bfd_getl64, bfd_getl_signed_64, bfd_putl64,
29c9b5a
+     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
29c9b5a
+     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Hdrs.  */
29c9b5a
+
29c9b5a
+  /* Note that we allow an object file to be treated as a core file as well.  */
29c9b5a
+  {				/* bfd_check_format.  */
29c9b5a
+    _bfd_dummy_target,
29c9b5a
+    coff_object_p,
29c9b5a
+    bfd_generic_archive_p,
29c9b5a
+    coff_object_p
29c9b5a
+  },
29c9b5a
+  {				/* bfd_set_format.  */
29c9b5a
+    _bfd_bool_bfd_false_error,
29c9b5a
+    coff_mkobject,
29c9b5a
+    _bfd_generic_mkarchive,
29c9b5a
+    _bfd_bool_bfd_false_error
29c9b5a
+  },
29c9b5a
+  {				/* bfd_write_contents.  */
29c9b5a
+    _bfd_bool_bfd_false_error,
29c9b5a
+    coff_write_object_contents,
29c9b5a
+    _bfd_write_archive_contents,
29c9b5a
+    _bfd_bool_bfd_false_error
29c9b5a
+  },
29c9b5a
+
29c9b5a
+  BFD_JUMP_TABLE_GENERIC (coff),
29c9b5a
+  BFD_JUMP_TABLE_COPY (coff),
29c9b5a
+  BFD_JUMP_TABLE_CORE (_bfd_nocore),
29c9b5a
+  BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
29c9b5a
+  BFD_JUMP_TABLE_SYMBOLS (coff),
29c9b5a
+  BFD_JUMP_TABLE_RELOCS (coff),
29c9b5a
+  BFD_JUMP_TABLE_WRITE (coff),
29c9b5a
+  BFD_JUMP_TABLE_LINK (coff),
29c9b5a
+  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
29c9b5a
+
29c9b5a
+  NULL,
29c9b5a
+
29c9b5a
+  COFF_SWAP_TABLE
29c9b5a
+};
29c9b5a
--- /dev/null	2021-12-01 09:13:46.241760896 +0000
29c9b5a
+++ binutils-2.37/binutils/testsuite/binutils-all/aarch64/pei-aarch64-little.s	2021-12-01 13:35:43.143988222 +0000
f5189ec
@@ -0,0 +1,42 @@
f5189ec
+	.arch armv8-a
f5189ec
+	.text
f5189ec
+	.align	2
f5189ec
+	.global	foo
f5189ec
+	.type	foo, %function
f5189ec
+foo:
f5189ec
+.LFB0:
f5189ec
+	.cfi_startproc
f5189ec
+	sub	sp, sp, #16
f5189ec
+	.cfi_def_cfa_offset 16
f5189ec
+	str	w0, [sp, 12]
f5189ec
+	ldr	w0, [sp, 12]
f5189ec
+	mul	w0, w0, w0
f5189ec
+	add	sp, sp, 16
f5189ec
+	.cfi_def_cfa_offset 0
f5189ec
+	ret
f5189ec
+	.cfi_endproc
f5189ec
+.LFE0:
f5189ec
+	.size	foo, .-foo
f5189ec
+	.align	2
f5189ec
+	.global	main
f5189ec
+	.type	main, %function
f5189ec
+main:
f5189ec
+.LFB1:
f5189ec
+	.cfi_startproc
f5189ec
+	stp	x29, x30, [sp, -16]!
f5189ec
+	.cfi_def_cfa_offset 16
f5189ec
+	.cfi_offset 29, -16
f5189ec
+	.cfi_offset 30, -8
f5189ec
+	mov	x29, sp
f5189ec
+	mov	w0, 5
f5189ec
+	bl	foo
f5189ec
+	ldp	x29, x30, [sp], 16
f5189ec
+	.cfi_restore 30
f5189ec
+	.cfi_restore 29
f5189ec
+	.cfi_def_cfa_offset 0
f5189ec
+	ret
f5189ec
+	.cfi_endproc
f5189ec
+.LFE1:
f5189ec
+	.size	main, .-main
f5189ec
+	.ident	"GCC: (fsf-trunk.2870) 12.0.0 20210930 (experimental)"
f5189ec
+	.section	.note.GNU-stack,"",@progbits
29c9b5a
--- /dev/null	2021-12-01 09:13:46.241760896 +0000
29c9b5a
+++ binutils-2.37/binutils/testsuite/binutils-all/aarch64/pei-aarch64-little.d	2021-12-01 13:35:43.143988222 +0000
29c9b5a
@@ -0,0 +1,16 @@
29c9b5a
+#skip: aarch64_be-*-*
29c9b5a
+#ld: -e0
29c9b5a
+#PROG: objcopy
29c9b5a
+#objcopy: -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .rel* -j .rela* -j .reloc --target=efi-app-aarch64
29c9b5a
+#objdump: -h -f
29c9b5a
+#name: Check if efi app format is recognized
29c9b5a
+
29c9b5a
+.*:     file format pei-aarch64-little
29c9b5a
+architecture: aarch64, flags 0x00000132:
29c9b5a
+EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
29c9b5a
+start address 0x0000000000000000
29c9b5a
+
29c9b5a
+Sections:
29c9b5a
+Idx Name          Size      VMA               LMA               File off  Algn
29c9b5a
+  0 \.text         00000030  0[^ ]+  0[^ ]+  0[^ ]+  2\*\*2
29c9b5a
+                  CONTENTS, ALLOC, LOAD, READONLY, CODE
29c9b5a
--- /dev/null	2021-11-30 07:48:35.901044247 +0000
29c9b5a
+++ binutils-2.35.2/bfd/pei-aarch64.c	2021-11-30 13:38:37.255656936 +0000
29c9b5a
@@ -0,0 +1,75 @@
29c9b5a
+/* BFD back-end for AArch64 PE IMAGE COFF files.
29c9b5a
+   Copyright (C) 2021 Free Software Foundation, Inc.
29c9b5a
+
29c9b5a
+   This file is part of BFD, the Binary File Descriptor library.
29c9b5a
+
29c9b5a
+   This program is free software; you can redistribute it and/or modify
29c9b5a
+   it under the terms of the GNU General Public License as published by
29c9b5a
+   the Free Software Foundation; either version 3 of the License, or
29c9b5a
+   (at your option) any later version.
29c9b5a
+
29c9b5a
+   This program is distributed in the hope that it will be useful,
29c9b5a
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
29c9b5a
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29c9b5a
+   GNU General Public License for more details.
29c9b5a
+
29c9b5a
+   You should have received a copy of the GNU General Public License
29c9b5a
+   along with this program; if not, write to the Free Software
29c9b5a
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
29c9b5a
+   MA 02110-1301, USA.  */
29c9b5a
+
29c9b5a
+#include "sysdep.h"
29c9b5a
+#include "bfd.h"
29c9b5a
+
29c9b5a
+#define TARGET_SYM		aarch64_pei_vec
29c9b5a
+#define TARGET_NAME		"pei-aarch64-little"
29c9b5a
+#define TARGET_ARCHITECTURE	bfd_arch_aarch64
29c9b5a
+#define TARGET_PAGESIZE		4096
29c9b5a
+#define TARGET_BIG_ENDIAN	0
29c9b5a
+#define TARGET_ARCHIVE		0
29c9b5a
+#define TARGET_PRIORITY		0
29c9b5a
+
29c9b5a
+#define COFF_IMAGE_WITH_PE
29c9b5a
+/* Rename the above into.. */
29c9b5a
+#define COFF_WITH_peAArch64
29c9b5a
+#define COFF_WITH_PE
29c9b5a
+#define PCRELOFFSET	   true
29c9b5a
+
29c9b5a
+/* Long section names not allowed in executable images, only object files.  */
29c9b5a
+#define COFF_LONG_SECTION_NAMES 0
29c9b5a
+
29c9b5a
+#define COFF_SECTION_ALIGNMENT_ENTRIES \
29c9b5a
+{ COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
29c9b5a
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
29c9b5a
+{ COFF_SECTION_NAME_EXACT_MATCH (".data"), \
29c9b5a
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
29c9b5a
+{ COFF_SECTION_NAME_EXACT_MATCH (".rdata"), \
29c9b5a
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
29c9b5a
+{ COFF_SECTION_NAME_EXACT_MATCH (".text"), \
29c9b5a
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
29c9b5a
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
29c9b5a
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
29c9b5a
+{ COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
29c9b5a
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
29c9b5a
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
29c9b5a
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
29c9b5a
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
29c9b5a
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
29c9b5a
+
29c9b5a
+#define PEI_HEADERS
29c9b5a
+#include "sysdep.h"
29c9b5a
+#include "bfd.h"
29c9b5a
+#include "libbfd.h"
29c9b5a
+#include "coff/aarch64.h"
29c9b5a
+#include "coff/internal.h"
29c9b5a
+#include "coff/pe.h"
29c9b5a
+#include "libcoff.h"
29c9b5a
+#include "libpei.h"
29c9b5a
+#include "libiberty.h"
29c9b5a
+
29c9b5a
+/* Make sure we're setting a 64-bit format.  */
29c9b5a
+#undef AOUTSZ
29c9b5a
+#define AOUTSZ          PEPAOUTSZ
29c9b5a
+#define PEAOUTHDR       PEPAOUTHDR
29c9b5a
+
29c9b5a
+#include "coff-aarch64.c"