[v3] aarch64-pe support for LD, GAS and BFD

Message ID 027ae69a-636b-6757-297a-eec42936401e@linaro.org
State Unresolved
Headers
Series [v3] aarch64-pe support for LD, GAS and BFD |

Checks

Context Check Description
snail/binutils-gdb-check warning Git am fail log

Commit Message

Zac Walker Oct. 14, 2022, 7:58 a.m. UTC
  V3 of the patch originally written by Jedidiah Thompson. It allows aarch64-pe to be targeted natively, not having to use objcopy to convert it from ELF to PE.


Between V2 and V3 I made a few recommended updates most notably adding the missing BFD_RELOC_AARCH64_BRANCH19 and additional const correctness. I have included a diff between patches v2 and v3. Patch and diff included as attachments to avoid text formatting issues.


Changes since that original patch:

- Added minimal relocation support and tested generated binaries run on a Surface-ProX ARM64 test machine.

- Been through a review process with colleagues at Linaro. Linaro has agreed to run CI for aarch64-pe going forward in their test lab.

- Run tests for multiple platforms; aarch64-linux-gnu, x86_64-linux-gnu, x86_64-w64-mingw32. Those tests pass and see no regressions. Multiple GAS tests still fail for aarch64-pe because of ELF specific assumptions. I plan to fix these later.


The long-term goal of this work is to help enable MSYS2 for Window on ARM64. As discussed here: https://github.com/msys2/MSYS2-packages/issues/1888


The original work by Jedidiah Thompson here can be found here: https://sourceware.org/pipermail/binutils/2022-January/119077.html


Regards,
Zac Walker
  

Comments

Nick Clifton Oct. 17, 2022, 12:20 p.m. UTC | #1
Hi Zac,

> V3 of the patch originally written by Jedidiah Thompson. It allows aarch64-pe to be targeted natively, not having to use objcopy to convert it from ELF to PE.

Looks good to me.

Patch approved - please apply.

Cheers
   Nick
  
Frager, Neal via Binutils Oct. 19, 2022, 9:48 a.m. UTC | #2
Sorry - this patch caused a build break:

checking for PT_GETDBREGS... /usr/bin/ld: ../bfd/.libs/libbfd.a(targets.o):(.data.rel.ro+0x48): undefined reference to `aarch64_pe_le_vec'
/usr/bin/ld: ../bfd/.libs/libbfd.a(targets.o):(.data.rel.ro+0x17c0): undefined reference to `aarch64_pe_le_vec'
collect2: error: ld returned 1 exit status
make[4]: *** [Makefile:1276: as-new] Error 1

Investigating now and hope to fix soon (or roll back.)

Zac

-----Original Message-----
From: Zac Walker <zac.walker@linaro.org> 
Sent: 14 October 2022 09:58
To: binutils@sourceware.org
Subject: [PATCH v3] aarch64-pe support for LD, GAS and BFD

V3 of the patch originally written by Jedidiah Thompson. It allows aarch64-pe to be targeted natively, not having to use objcopy to convert it from ELF to PE.


Between V2 and V3 I made a few recommended updates most notably adding the missing BFD_RELOC_AARCH64_BRANCH19 and additional const correctness. I have included a diff between patches v2 and v3. Patch and diff included as attachments to avoid text formatting issues.


Changes since that original patch:

- Added minimal relocation support and tested generated binaries run on a Surface-ProX ARM64 test machine.

- Been through a review process with colleagues at Linaro. Linaro has agreed to run CI for aarch64-pe going forward in their test lab.

- Run tests for multiple platforms; aarch64-linux-gnu, x86_64-linux-gnu, x86_64-w64-mingw32. Those tests pass and see no regressions. Multiple GAS tests still fail for aarch64-pe because of ELF specific assumptions. I plan to fix these later.


The long-term goal of this work is to help enable MSYS2 for Window on ARM64. As discussed here: https://github.com/msys2/MSYS2-packages/issues/1888


The original work by Jedidiah Thompson here can be found here: https://sourceware.org/pipermail/binutils/2022-January/119077.html


Regards,
Zac Walker
  
Andrew Burgess Oct. 19, 2022, 11:14 a.m. UTC | #3
Zac Walker via Binutils <binutils@sourceware.org> writes:

> Sorry - this patch caused a build break:
>
> checking for PT_GETDBREGS... /usr/bin/ld: ../bfd/.libs/libbfd.a(targets.o):(.data.rel.ro+0x48): undefined reference to `aarch64_pe_le_vec'
> /usr/bin/ld: ../bfd/.libs/libbfd.a(targets.o):(.data.rel.ro+0x17c0): undefined reference to `aarch64_pe_le_vec'
> collect2: error: ld returned 1 exit status
> make[4]: *** [Makefile:1276: as-new] Error 1
>
> Investigating now and hope to fix soon (or roll back.)

I just pushed the patch below to fix this issue.  Just needed to run
autoreconf.

Thanks,
Andrew

---

commit 6d6ae7c23dd59fe16f3e366e969b927d97fe5e01
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Wed Oct 19 12:00:42 2022 +0100

    bfd: fix undefined references to aarch64_pe_le_vec
    
    After commit:
    
      commit c60b3806799abf1d7f6cf5108a1b0e733a950b13
      Date:   Wed Oct 19 10:57:12 2022 +0200
    
          aarch64-pe support for LD, GAS and BFD
    
    It appears that bfd/Makefile.in and bfd/configure were not regenerated
    correctly.  The differences in the configure file are only whitespace,
    but in Makefile.in a critical reference to pe-aarch64.lo was missing.

diff --git a/bfd/Makefile.in b/bfd/Makefile.in
index 3f520355829..b906976a1c0 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -1048,6 +1048,7 @@ BFD64_BACKENDS = \
 	pe-loongarch64igen.lo \
 	pe-x86_64.lo \
 	pei-aarch64.lo \
+	pe-aarch64.lo \
 	pei-ia64.lo \
 	pei-loongarch64.lo \
 	pei-x86_64.lo \
@@ -1700,6 +1701,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pc532-mach.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pdb.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pdp11.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pe-aarch64.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pe-aarch64igen.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pe-arm-wince.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pe-arm.Plo@am__quote@
diff --git a/bfd/configure b/bfd/configure
index bae9d17c515..1c1760670cb 100755
--- a/bfd/configure
+++ b/bfd/configure
@@ -13604,7 +13604,7 @@ do
     aarch64_elf64_le_cloudabi_vec) tb="$tb elf64-aarch64.lo elfxx-aarch64.lo elf-ifunc.lo elf64.lo $elf"; target_size=64 ;;
     aarch64_mach_o_vec)		 tb="$tb mach-o-aarch64.lo"; target_size=64 ;;
     aarch64_pei_le_vec)		 tb="$tb pei-aarch64.lo pe-aarch64igen.lo $coff"; target_size=64 ;;
-    aarch64_pe_le_vec)     tb="$tb pe-aarch64.lo pe-aarch64igen.lo $coff"; target_size=64 ;;
+    aarch64_pe_le_vec)		 tb="$tb pe-aarch64.lo pe-aarch64igen.lo $coff"; target_size=64 ;;
     alpha_ecoff_le_vec)		 tb="$tb coff-alpha.lo ecoff.lo $ecoff"; target_size=64 ;;
     alpha_elf64_vec)		 tb="$tb elf64-alpha.lo elf64.lo $elf"; target_size=64 ;;
     alpha_elf64_fbsd_vec)	 tb="$tb elf64-alpha.lo elf64.lo $elf"; target_size=64 ;;
  
Frager, Neal via Binutils Oct. 19, 2022, 11:51 a.m. UTC | #4
Thanks Andrew - much appreciated.
That taught me a valuable lesson to do more testing with '--enable-targets=all' next time.

Zac

-----Original Message-----
From: Andrew Burgess <aburgess@redhat.com> 
Sent: 19 October 2022 13:14
To: zac.walker@linaro.org; binutils@sourceware.org
Subject: RE: [PATCH v3] aarch64-pe support for LD, GAS and BFD

Zac Walker via Binutils <binutils@sourceware.org> writes:

> Sorry - this patch caused a build break:
>
> checking for PT_GETDBREGS... /usr/bin/ld: ../bfd/.libs/libbfd.a(targets.o):(.data.rel.ro+0x48): undefined reference to
`aarch64_pe_le_vec'
> /usr/bin/ld: ../bfd/.libs/libbfd.a(targets.o):(.data.rel.ro+0x17c0): undefined reference to `aarch64_pe_le_vec'
> collect2: error: ld returned 1 exit status
> make[4]: *** [Makefile:1276: as-new] Error 1
>
> Investigating now and hope to fix soon (or roll back.)

I just pushed the patch below to fix this issue.  Just needed to run autoreconf.

Thanks,
Andrew

---

commit 6d6ae7c23dd59fe16f3e366e969b927d97fe5e01
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Wed Oct 19 12:00:42 2022 +0100

    bfd: fix undefined references to aarch64_pe_le_vec
    
    After commit:
    
      commit c60b3806799abf1d7f6cf5108a1b0e733a950b13
      Date:   Wed Oct 19 10:57:12 2022 +0200
    
          aarch64-pe support for LD, GAS and BFD
    
    It appears that bfd/Makefile.in and bfd/configure were not regenerated
    correctly.  The differences in the configure file are only whitespace,
    but in Makefile.in a critical reference to pe-aarch64.lo was missing.

diff --git a/bfd/Makefile.in b/bfd/Makefile.in index 3f520355829..b906976a1c0 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -1048,6 +1048,7 @@ BFD64_BACKENDS = \
 	pe-loongarch64igen.lo \
 	pe-x86_64.lo \
 	pei-aarch64.lo \
+	pe-aarch64.lo \
 	pei-ia64.lo \
 	pei-loongarch64.lo \
 	pei-x86_64.lo \
@@ -1700,6 +1701,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pc532-mach.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pdb.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pdp11.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ 
+@am__quote@./$(DEPDIR)/pe-aarch64.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pe-aarch64igen.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pe-arm-wince.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pe-arm.Plo@am__quote@
diff --git a/bfd/configure b/bfd/configure index bae9d17c515..1c1760670cb 100755
--- a/bfd/configure
+++ b/bfd/configure
@@ -13604,7 +13604,7 @@ do
     aarch64_elf64_le_cloudabi_vec) tb="$tb elf64-aarch64.lo elfxx-aarch64.lo elf-ifunc.lo elf64.lo $elf"; target_size=64 ;;
     aarch64_mach_o_vec)		 tb="$tb mach-o-aarch64.lo"; target_size=64 ;;
     aarch64_pei_le_vec)		 tb="$tb pei-aarch64.lo pe-aarch64igen.lo $coff"; target_size=64 ;;
-    aarch64_pe_le_vec)     tb="$tb pe-aarch64.lo pe-aarch64igen.lo $coff"; target_size=64 ;;
+    aarch64_pe_le_vec)		 tb="$tb pe-aarch64.lo pe-aarch64igen.lo $coff"; target_size=64 ;;
     alpha_ecoff_le_vec)		 tb="$tb coff-alpha.lo ecoff.lo $ecoff"; target_size=64 ;;
     alpha_elf64_vec)		 tb="$tb elf64-alpha.lo elf64.lo $elf"; target_size=64 ;;
     alpha_elf64_fbsd_vec)	 tb="$tb elf64-alpha.lo elf64.lo $elf"; target_size=64 ;;
  

Patch

diff --git a/bfd/coff-aarch64.c b/bfd/coff-aarch64.c
index 74dc7e8f55..2c3e225a22 100644
--- a/bfd/coff-aarch64.c
+++ b/bfd/coff-aarch64.c
@@ -39,56 +39,59 @@ 
 
 #include "libcoff.h"
 
-
-
-
 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
 #define MINUS_ONE (~ (bfd_vma) 0)
 
-static reloc_howto_type arm64_reloc_howto_64 = HOWTO(IMAGE_REL_ARM64_ADDR64, 0, 8, 64, false, 0,
+static const reloc_howto_type arm64_reloc_howto_64 = HOWTO(IMAGE_REL_ARM64_ADDR64, 0, 8, 64, false, 0,
 	 complain_overflow_bitfield,
 	 NULL, "64",
 	 false, MINUS_ONE, MINUS_ONE, false);
 
-static reloc_howto_type arm64_reloc_howto_32 = HOWTO (IMAGE_REL_ARM64_ADDR32, 0, 4, 32, false, 0,
+static const reloc_howto_type arm64_reloc_howto_32 = HOWTO (IMAGE_REL_ARM64_ADDR32, 0, 4, 32, false, 0,
 	 complain_overflow_bitfield,
 	 NULL, "32",
 	 false, 0xffffffff, 0xffffffff, false);
 
-static reloc_howto_type arm64_reloc_howto_32_pcrel = HOWTO (IMAGE_REL_ARM64_REL32, 0, 4, 32, true, 0,
+static const reloc_howto_type arm64_reloc_howto_32_pcrel = HOWTO (IMAGE_REL_ARM64_REL32, 0, 4, 32, true, 0,
 	 complain_overflow_bitfield,
 	 NULL, "DISP32",
 	 false, 0xffffffff, 0xffffffff, true);
 
-static reloc_howto_type arm64_reloc_howto_branch26 = HOWTO (IMAGE_REL_ARM64_BRANCH26, 0, 4, 26, true, 0,
+static const reloc_howto_type arm64_reloc_howto_branch26 = HOWTO (IMAGE_REL_ARM64_BRANCH26, 0, 4, 26, true, 0,
 	 complain_overflow_bitfield,
 	 NULL, "BRANCH26",
 	 false, 0x03ffffff, 0x03ffffff, true);
 
-static reloc_howto_type arm64_reloc_howto_page21 = HOWTO (IMAGE_REL_ARM64_PAGEBASE_REL21, 12, 4, 21, true, 0,
+static const reloc_howto_type arm64_reloc_howto_page21 = HOWTO (IMAGE_REL_ARM64_PAGEBASE_REL21, 12, 4, 21, true, 0,
 	 complain_overflow_signed,
 	 NULL, "PAGE21",
 	 false, 0x1fffff, 0x1fffff, false);
 
-static reloc_howto_type arm64_reloc_howto_lo21 = HOWTO (IMAGE_REL_ARM64_REL21, 0, 4, 21, true, 0,
+static const reloc_howto_type arm64_reloc_howto_lo21 = HOWTO (IMAGE_REL_ARM64_REL21, 0, 4, 21, true, 0,
 	 complain_overflow_signed,
 	 NULL, "LO21",
 	 false, 0x1fffff, 0x1fffff, true);
 
-static reloc_howto_type arm64_reloc_howto_pgoff12 = HOWTO (IMAGE_REL_ARM64_PAGEOFFSET_12L, 1, 4, 12, true, 0,
+static const reloc_howto_type arm64_reloc_howto_pgoff12 = HOWTO (IMAGE_REL_ARM64_PAGEOFFSET_12L, 1, 4, 12, true, 0,
 	 complain_overflow_signed,
 	 NULL, "PGOFF12",
 	 false, 0xffe, 0xffe, true);
 
+static const reloc_howto_type arm64_reloc_howto_branch19 = HOWTO (IMAGE_REL_ARM64_BRANCH19, 2, 4, 19, true, 0,
+	 complain_overflow_signed,
+	 NULL, "BRANCH19",
+	 false, 0x7ffff, 0x7ffff, true);
+
 
-static reloc_howto_type* arm64_howto_table[] = {
+static const reloc_howto_type* const arm64_howto_table[] = {
      &arm64_reloc_howto_64,
      &arm64_reloc_howto_32,
      &arm64_reloc_howto_32_pcrel,
      &arm64_reloc_howto_branch26,
      &arm64_reloc_howto_page21,
      &arm64_reloc_howto_lo21,
-     &arm64_reloc_howto_pgoff12
+     &arm64_reloc_howto_pgoff12,
+     &arm64_reloc_howto_branch19
 };
 
 #ifndef NUM_ELEM
@@ -100,8 +103,6 @@  static reloc_howto_type* arm64_howto_table[] = {
 #define coff_bfd_reloc_type_lookup		coff_aarch64_reloc_type_lookup
 #define coff_bfd_reloc_name_lookup		coff_aarch64_reloc_name_lookup
 
-
-
 static reloc_howto_type *
 coff_aarch64_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED, bfd_reloc_code_real_type code)
 {
@@ -122,6 +123,8 @@  coff_aarch64_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED, bfd_reloc_code_real
     return &arm64_reloc_howto_lo21;
   case BFD_RELOC_AARCH64_LDST16_LO12:
     return &arm64_reloc_howto_pgoff12;
+  case BFD_RELOC_AARCH64_BRANCH19:
+    return &arm64_reloc_howto_branch19;
   default:
     BFD_FAIL ();
     return NULL;
@@ -148,7 +151,7 @@  coff_aarch64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
 #define COFF_PAGE_SIZE			      0x1000
 
 static reloc_howto_type *
-coff_aarch64_rtype_lookup (unsigned int code ATTRIBUTE_UNUSED)
+coff_aarch64_rtype_lookup (unsigned int code)
 {
   switch (code)
   {
@@ -164,8 +167,10 @@  coff_aarch64_rtype_lookup (unsigned int code ATTRIBUTE_UNUSED)
       return &arm64_reloc_howto_page21;
     case IMAGE_REL_ARM64_REL21:
       return &arm64_reloc_howto_lo21;
-    case BFD_RELOC_AARCH64_LDST16_LO12:
+    case IMAGE_REL_ARM64_PAGEOFFSET_12L:
       return &arm64_reloc_howto_pgoff12;
+    case IMAGE_REL_ARM64_BRANCH19:
+      return &arm64_reloc_howto_branch19;
     default:
       BFD_FAIL ();
       return NULL;
diff --git a/gas/config/tc-aarch64.c b/gas/config/tc-aarch64.c
index 6f6b204cdc..f6fa158583 100644
--- a/gas/config/tc-aarch64.c
+++ b/gas/config/tc-aarch64.c
@@ -10250,10 +10250,13 @@  struct aarch64_option_abi_value_table
   enum aarch64_abi_type value;
 };
 
-#ifdef OBJ_ELF
 static const struct aarch64_option_abi_value_table aarch64_abis[] = {
+#ifdef OBJ_ELF
   {"ilp32",		AARCH64_ABI_ILP32},
   {"lp64",		AARCH64_ABI_LP64},
+#else
+  {"llp64",		AARCH64_ABI_LLP64},
+#endif
 };
 
 static int
@@ -10277,13 +10280,10 @@  aarch64_parse_abi (const char *str)
   as_bad (_("unknown abi `%s'\n"), str);
   return 0;
 }
-#endif
 
 static struct aarch64_long_option_table aarch64_long_opts[] = {
-#ifdef OBJ_ELF
   {"mabi=", N_("<abi name>\t  specify for ABI <abi name>"),
    aarch64_parse_abi, NULL},
-#endif /* OBJ_ELF */
   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
    aarch64_parse_cpu, NULL},
   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),