public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
@ 2021-10-29 11:05 Tamar Christina
  2021-11-05 17:39 ` Tamar Christina
  2021-11-12 17:17 ` Richard Earnshaw
  0 siblings, 2 replies; 5+ messages in thread
From: Tamar Christina @ 2021-10-29 11:05 UTC (permalink / raw)
  To: binutils; +Cc: nd, Richard.Earnshaw, nickc, ramana.radhakrishnan

[-- Attachment #1: Type: text/plain, Size: 16129 bytes --]

Hi All,

This adds support for efi-*-arm by virtue of fixing the magic hashes in
pei-arm-little to have the right values according to the PE specification[1].

Because there are likely existing files out there using the wrong format I have
added a new target pei-arm-little-old so that binutils can continue to recognize
the old format.  Over time code should converge to the new one.

Additionally the pei-arm-little target is now also exposed to elf targets such
as arm-*-elf, arm*-*-linux-* etc such that one can create EFI images on linux
hosted toolchains as well.

With these changes the EFI binary is now recognized by third party tools:

> pecli info main.efi

Metadata
================================================================================
MD5:            5591110a0160b4a29497171ae44e0707
SHA1:           4f3bb55ec366e64b9166ed202a49e787f9b78a84
SHA256:         b18f1b5dd210afe9b73ff0d15be29b4fb4f52db82cae4df0f5cd1877a8e9b48c
Imphash:
Size:           1288 bytes
Type:           PE32 executable (EFI application) ARM (stripped to external PDB), for MS Windows
Compile Time:   2021-10-29 00:00:00 (UTC - 0x0       )
Entry point:    0x0 (section None)

Sections
================================================================================
Name      RWX  VirtSize   VirtAddr   RawAddr   RawSize   Entropy  md5
.text     RWX  0x44       0x8000     0x200     0x200       1.07 0b48a18cac04f6e96a1ffc44fb0d4902


Imports
================================================================================

Any magic number is based on the Microsoft PE specification [1].

[1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format

build on native hardware and regtested on
  arm-none-elf, arm-none-elf (32 bit host),
  arm-none-linux-gnueabihf, arm-none-linux-gnueabihf (32 bit host)

Cross-compiled and regtested on
  arm-none-linux-gnueabihf, armeb-none-elf, arm-wince-pe, arm-none-pe

and no issues.

Ok for master?

Thanks,
Tamar

bfd/ChangeLog:

2021-10-29  Tamar Christina  <tamar.christina@arm.com>

	PR binutils/26218
	* pei-arm-old.c: New file, define targets arm_pei_be_vec_old and
	arm_pei_le_vec_old.
	* Makefile.am: Use them.
	* Makefile.in: Likewise.
	* config.bfd: Likewise.
	* configure: Likewise.
	* configure.ac: Likewise.
	* targets.c: Likewise.
	* pei-arm.c (ARMMAGIC, (ZMAGIC): Define
	* coffcode.h (coff_set_arch_mach_hook): Handle case where
	ARMMAGIC == ARMPEMAGIC.

binutils/ChangeLog:

2021-10-29  Tamar Christina  <tamar.christina@arm.com>

	PR binutils/26218
	* NEWS: Add new support.
	* objcopy.c (convert_efi_target): Add efi-*-arm support
	* testsuite/binutils-all/arm/pei-arm-little.d: New test.
	* testsuite/binutils-all/arm/pei-arm-little.s: New test.

include/ChangeLog:

2021-10-29  Tamar Christina  <tamar.christina@arm.com>

	PR binutils/26218
	* coff/arm.h: Make ARMMAGIC and ZMAGIC overrideable.

--- inline copy of patch -- 
diff --git a/bfd/Makefile.am b/bfd/Makefile.am
index 7777f733702c09b441bc591daa5b8c3d9ab2abd7..cc7814639caf54301dfb0380b96b34b37a12e7c7 100644
--- a/bfd/Makefile.am
+++ b/bfd/Makefile.am
@@ -509,6 +509,7 @@ BFD32_BACKENDS_CFILES = \
 	pef.c \
 	pei-arm-wince.c \
 	pei-arm.c \
+	pei-arm-old.c \
 	pei-i386.c \
 	pei-mcore.c \
 	pei-sh.c \
diff --git a/bfd/Makefile.in b/bfd/Makefile.in
index b243151fea350032c6cb90f1ec1f359bdba589f5..730c845d85b2df89d1c7bb854122dd7addc8ac69 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -935,6 +935,7 @@ BFD32_BACKENDS_CFILES = \
 	pef.c \
 	pei-arm-wince.c \
 	pei-arm.c \
+	pei-arm-old.c \
 	pei-i386.c \
 	pei-mcore.c \
 	pei-sh.c \
diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index 86688d31cdb0a4809b7fc01079d16e87573dd56f..1cf02c1b7c1551b83135e3b743a2cfd35e0cb259 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -2196,7 +2196,9 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
 #endif
 #ifdef ARMMAGIC
     case ARMMAGIC:
+#if ARMMAGIC != ARMPEMAGIC
     case ARMPEMAGIC:
+#endif
     case THUMBPEMAGIC:
       arch = bfd_arch_arm;
       machine = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
diff --git a/bfd/config.bfd b/bfd/config.bfd
index 776918253ef11140df94042b379789e6f7b1e598..34c20a56b2614eabba79c09bad8fbdddc4dfd74b 100644
--- a/bfd/config.bfd
+++ b/bfd/config.bfd
@@ -362,7 +362,7 @@ case "${targ}" in
     ;;
   arm*-*-haiku*)
     targ_defvec=arm_elf32_le_vec
-    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec"
+    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
     ;;
   arm-*-nacl*)
     targ_defvec=arm_elf32_nacl_le_vec
@@ -393,7 +393,7 @@ case "${targ}" in
     ;;
   arm-*-pe*)
     targ_defvec=arm_pe_le_vec
-    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec"
+    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
     targ_underscore=yes
     ;;
   arm-*-phoenix*)
@@ -412,7 +412,7 @@ case "${targ}" in
   arm*-*-uclinux* | arm-*-kfreebsd*-gnu | \
   arm*-*-eabi* | arm-*-rtems* | arm*-*-uclinuxfdpiceabi)
     targ_defvec=arm_elf32_le_vec
-    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec"
+    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pei_le_vec"
     ;;
   arm*-*-vxworks | arm*-*-windiss)
     targ_defvec=arm_elf32_vxworks_le_vec
diff --git a/bfd/configure b/bfd/configure
index 5741e039a4fed386d826ad4910e1fd029bd37f23..19ea57d1b11e2ada1682eb4456514e1c03fb6da5 100755
--- a/bfd/configure
+++ b/bfd/configure
@@ -13281,6 +13281,8 @@ do
     arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
     arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
     arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
+    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
+    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
     arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
     arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
     arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo peigen.lo $coff" ;;
diff --git a/bfd/configure.ac b/bfd/configure.ac
index 1fadfd7d91ed7ead6b04be2173d6c596fb4af221..951ce21cec061690a7734da4a4739c1975d19710 100644
--- a/bfd/configure.ac
+++ b/bfd/configure.ac
@@ -460,6 +460,8 @@ do
     arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
     arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
     arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
+    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
+    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
     arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
     arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
     arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo peigen.lo $coff" ;;
diff --git a/bfd/pei-arm-old.c b/bfd/pei-arm-old.c
new file mode 100644
index 0000000000000000000000000000000000000000..a42f7c8a4a86b49e0e520314592dfaa3eee42951
--- /dev/null
+++ b/bfd/pei-arm-old.c
@@ -0,0 +1,55 @@
+/* BFD back-end for arm PE IMAGE COFF files.
+   Copyright (C) 1995-2021 Free Software Foundation, Inc.
+
+   This file is part of BFD, the Binary File Descriptor library.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
+
+#include "sysdep.h"
+#include "bfd.h"
+
+#ifndef TARGET_LITTLE_SYM
+#define TARGET_LITTLE_SYM  arm_pei_le_old_vec
+#define TARGET_LITTLE_NAME "pei-arm-little-old"
+#define TARGET_BIG_SYM     arm_pei_be_old_vec
+#define TARGET_BIG_NAME    "pei-arm-big-old"
+#endif
+
+#define COFF_IMAGE_WITH_PE
+#define COFF_WITH_PE
+#define PCRELOFFSET	   true
+/* Long section names not allowed in executable images, only object files.  */
+#define COFF_LONG_SECTION_NAMES 0
+
+#define COFF_SECTION_ALIGNMENT_ENTRIES \
+{ COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_EXACT_MATCH (".data"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_EXACT_MATCH (".rdata"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_EXACT_MATCH (".text"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
+
+#include "coff-arm.c"
diff --git a/bfd/pei-arm.c b/bfd/pei-arm.c
index 216b94509ad43516062118916a9ece66c3553fed..21583c9a2d58021fb34175cf859c030a9dace978 100644
--- a/bfd/pei-arm.c
+++ b/bfd/pei-arm.c
@@ -26,6 +26,9 @@
 #define TARGET_LITTLE_NAME "pei-arm-little"
 #define TARGET_BIG_SYM     arm_pei_be_vec
 #define TARGET_BIG_NAME    "pei-arm-big"
+#define ARMMAGIC 0x1c0
+/* Constant below is IMAGE_NT_OPTIONAL_HDR_MAGIC.  */
+#define ZMAGIC   0x10b    /* Demand load format, eg normal ld output 0x10b.  */
 #endif
 
 #define COFF_IMAGE_WITH_PE
diff --git a/bfd/targets.c b/bfd/targets.c
index 672dc2bb1a4d5643f84577d81d4c9cebc14429d9..a1a33ae5461daa8dc945466c26bae2d12fc79294 100644
--- a/bfd/targets.c
+++ b/bfd/targets.c
@@ -702,6 +702,8 @@ extern const bfd_target arm_pe_be_vec;
 extern const bfd_target arm_pe_le_vec;
 extern const bfd_target arm_pe_wince_be_vec;
 extern const bfd_target arm_pe_wince_le_vec;
+extern const bfd_target arm_pei_be_old_vec;
+extern const bfd_target arm_pei_le_old_vec;
 extern const bfd_target arm_pei_be_vec;
 extern const bfd_target arm_pei_le_vec;
 extern const bfd_target arm_pei_wince_be_vec;
@@ -1027,6 +1029,8 @@ static const bfd_target * const _bfd_target_vector[] =
 	&arm_pe_le_vec,
 	&arm_pe_wince_be_vec,
 	&arm_pe_wince_le_vec,
+	&arm_pei_be_old_vec,
+	&arm_pei_le_old_vec,
 	&arm_pei_be_vec,
 	&arm_pei_le_vec,
 	&arm_pei_wince_be_vec,
diff --git a/binutils/NEWS b/binutils/NEWS
index 994546330f95ccc3b859ffb17388b2cc5ec6a653..9aa45dab2b83edb3e7b3778378e7f66ed454f459 100644
--- a/binutils/NEWS
+++ b/binutils/NEWS
@@ -5,6 +5,14 @@
 * Support for efi-app-aarch64, efi-rtdrv-aarch64 and efi-bsdrv-aarch64 has been
   added to objcopy in order to enable UEFI development using binutils.
 
+* Support for efi-app-arm, efi-rtdrv-arm and efi-bsdrv-arm has been
+  added to objcopy in order to enable UEFI development using binutils.
+
+* The PE Image format pei-arm-little has been renamed to pei-arm-little-old and
+  a new pei-arm-little has been added with the MAGIC and ZMAGIC numbers from
+  the Microsoft PE specification added.  This allows the format to be
+  recognized by standard Windows tools.
+
 Changes in 2.37:
 
 * The readelf tool has a new command line option which can be used to specify
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 458a6d96cccbeb154f95e5ba54734b956d14f879..dbd18444d573048b2a262d36a72245612239efcd 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -5003,6 +5003,13 @@ convert_efi_target (char *efi)
       char *t = "aarch64-little";
       strcpy (efi + 4, t);
     }
+  else if (strcmp (efi + 4, "arm") == 0)
+    {
+      /* Change arm to arm-little.  */
+      efi = (char *) xrealloc (efi, strlen (efi) + 7);
+      char *t = "arm-little";
+      strcpy (efi + 4, t);
+    }
 }
 
 /* Allocate and return a pointer to a struct section_add, initializing the
diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.d b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
new file mode 100644
index 0000000000000000000000000000000000000000..bd251f8e3f2cedc3f4ed530d28d75c8c54942336
--- /dev/null
+++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
@@ -0,0 +1,16 @@
+#skip: armeb-*-* *-*-wince *-*-vxworks *-*-pe
+#ld: -e0
+#PROG: objcopy
+#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-arm
+#objdump: -h -f
+#name: Check if efi app format is recognized
+
+.*:     file format pei-arm-little
+architecture: armv3m, flags 0x00000132:
+EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
+start address 0x00000000
+
+Sections:
+Idx Name          Size      VMA       LMA       File off  Algn
+  0 \.text         00000044  00008000  00008000  00000200  2\*\*2
+                  CONTENTS, ALLOC, LOAD, CODE
diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.s b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
new file mode 100644
index 0000000000000000000000000000000000000000..f5e629895a2e72da9756697bec177a042a64472c
--- /dev/null
+++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
@@ -0,0 +1,56 @@
+	.arch armv7-a
+        .eabi_attribute 28, 1
+        .eabi_attribute 20, 1
+        .eabi_attribute 21, 1
+        .eabi_attribute 23, 3
+        .eabi_attribute 24, 1
+        .eabi_attribute 25, 1
+        .eabi_attribute 26, 2
+        .eabi_attribute 30, 6
+        .eabi_attribute 34, 1
+        .eabi_attribute 18, 4
+        .file   "hello.c"
+        .text
+        .align  2
+        .global foo
+        .arch armv7-a
+        .syntax unified
+        .arm
+        .fpu neon
+        .type   foo, %function
+foo:
+        @ args = 0, pretend = 0, frame = 8
+        @ frame_needed = 1, uses_anonymous_args = 0
+        @ link register save eliminated.
+        str     fp, [sp, #-4]!
+        add     fp, sp, #0
+        sub     sp, sp, #12
+        str     r0, [fp, #-8]
+        ldr     r3, [fp, #-8]
+        mul     r3, r3, r3
+        mov     r0, r3
+        add     sp, fp, #0
+        @ sp needed
+        ldr     fp, [sp], #4
+        bx      lr
+        .size   foo, .-foo
+        .align  2
+        .global main
+        .syntax unified
+        .arm
+        .fpu neon
+        .type   main, %function
+main:
+        @ args = 0, pretend = 0, frame = 0
+        @ frame_needed = 1, uses_anonymous_args = 0
+        push    {fp, lr}
+        add     fp, sp, #4
+        mov     r0, #5
+        bl      foo
+        mov     r3, r0
+        mov     r0, r3
+        pop     {fp, pc}
+        .size   main, .-main
+        .ident  "GCC: (6ea25fd023ef3674e34cc67cd97771195d6f93e4) 12.0.0 20210715 (experimental)"
+        .section        .note.GNU-stack,"",%progbits
+
diff --git a/include/coff/arm.h b/include/coff/arm.h
index 7b618d94edef0b8660544157df04f2a1e79f09a4..a46fac0f83054e27eea34ac77435ea1f4ad5d1ed 100644
--- a/include/coff/arm.h
+++ b/include/coff/arm.h
@@ -75,7 +75,9 @@
  
   XXX - NC 5/6/97.  */
 
+#ifndef ARMMAGIC
 #define	ARMMAGIC	0xa00  /* I just made this up */
+#endif
 
 #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC))
 
@@ -87,7 +89,9 @@
 #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic != ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic != ARMV7PEMAGIC))
 
 #define OMAGIC          0404    /* object files, eg as output */
+#ifndef ZMAGIC
 #define ZMAGIC          0413    /* demand load format, eg normal ld output */
+#endif
 #define STMAGIC		0401	/* target shlib */
 #define SHMAGIC		0443	/* host   shlib */
 


-- 

[-- Attachment #2: rb14985.patch --]
[-- Type: text/x-diff, Size: 13236 bytes --]

diff --git a/bfd/Makefile.am b/bfd/Makefile.am
index 7777f733702c09b441bc591daa5b8c3d9ab2abd7..cc7814639caf54301dfb0380b96b34b37a12e7c7 100644
--- a/bfd/Makefile.am
+++ b/bfd/Makefile.am
@@ -509,6 +509,7 @@ BFD32_BACKENDS_CFILES = \
 	pef.c \
 	pei-arm-wince.c \
 	pei-arm.c \
+	pei-arm-old.c \
 	pei-i386.c \
 	pei-mcore.c \
 	pei-sh.c \
diff --git a/bfd/Makefile.in b/bfd/Makefile.in
index b243151fea350032c6cb90f1ec1f359bdba589f5..730c845d85b2df89d1c7bb854122dd7addc8ac69 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -935,6 +935,7 @@ BFD32_BACKENDS_CFILES = \
 	pef.c \
 	pei-arm-wince.c \
 	pei-arm.c \
+	pei-arm-old.c \
 	pei-i386.c \
 	pei-mcore.c \
 	pei-sh.c \
diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index 86688d31cdb0a4809b7fc01079d16e87573dd56f..1cf02c1b7c1551b83135e3b743a2cfd35e0cb259 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -2196,7 +2196,9 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
 #endif
 #ifdef ARMMAGIC
     case ARMMAGIC:
+#if ARMMAGIC != ARMPEMAGIC
     case ARMPEMAGIC:
+#endif
     case THUMBPEMAGIC:
       arch = bfd_arch_arm;
       machine = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
diff --git a/bfd/config.bfd b/bfd/config.bfd
index 776918253ef11140df94042b379789e6f7b1e598..34c20a56b2614eabba79c09bad8fbdddc4dfd74b 100644
--- a/bfd/config.bfd
+++ b/bfd/config.bfd
@@ -362,7 +362,7 @@ case "${targ}" in
     ;;
   arm*-*-haiku*)
     targ_defvec=arm_elf32_le_vec
-    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec"
+    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
     ;;
   arm-*-nacl*)
     targ_defvec=arm_elf32_nacl_le_vec
@@ -393,7 +393,7 @@ case "${targ}" in
     ;;
   arm-*-pe*)
     targ_defvec=arm_pe_le_vec
-    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec"
+    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
     targ_underscore=yes
     ;;
   arm-*-phoenix*)
@@ -412,7 +412,7 @@ case "${targ}" in
   arm*-*-uclinux* | arm-*-kfreebsd*-gnu | \
   arm*-*-eabi* | arm-*-rtems* | arm*-*-uclinuxfdpiceabi)
     targ_defvec=arm_elf32_le_vec
-    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec"
+    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pei_le_vec"
     ;;
   arm*-*-vxworks | arm*-*-windiss)
     targ_defvec=arm_elf32_vxworks_le_vec
diff --git a/bfd/configure b/bfd/configure
index 5741e039a4fed386d826ad4910e1fd029bd37f23..19ea57d1b11e2ada1682eb4456514e1c03fb6da5 100755
--- a/bfd/configure
+++ b/bfd/configure
@@ -13281,6 +13281,8 @@ do
     arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
     arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
     arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
+    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
+    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
     arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
     arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
     arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo peigen.lo $coff" ;;
diff --git a/bfd/configure.ac b/bfd/configure.ac
index 1fadfd7d91ed7ead6b04be2173d6c596fb4af221..951ce21cec061690a7734da4a4739c1975d19710 100644
--- a/bfd/configure.ac
+++ b/bfd/configure.ac
@@ -460,6 +460,8 @@ do
     arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
     arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
     arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
+    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
+    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
     arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
     arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
     arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo peigen.lo $coff" ;;
diff --git a/bfd/pei-arm-old.c b/bfd/pei-arm-old.c
new file mode 100644
index 0000000000000000000000000000000000000000..a42f7c8a4a86b49e0e520314592dfaa3eee42951
--- /dev/null
+++ b/bfd/pei-arm-old.c
@@ -0,0 +1,55 @@
+/* BFD back-end for arm PE IMAGE COFF files.
+   Copyright (C) 1995-2021 Free Software Foundation, Inc.
+
+   This file is part of BFD, the Binary File Descriptor library.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
+
+#include "sysdep.h"
+#include "bfd.h"
+
+#ifndef TARGET_LITTLE_SYM
+#define TARGET_LITTLE_SYM  arm_pei_le_old_vec
+#define TARGET_LITTLE_NAME "pei-arm-little-old"
+#define TARGET_BIG_SYM     arm_pei_be_old_vec
+#define TARGET_BIG_NAME    "pei-arm-big-old"
+#endif
+
+#define COFF_IMAGE_WITH_PE
+#define COFF_WITH_PE
+#define PCRELOFFSET	   true
+/* Long section names not allowed in executable images, only object files.  */
+#define COFF_LONG_SECTION_NAMES 0
+
+#define COFF_SECTION_ALIGNMENT_ENTRIES \
+{ COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_EXACT_MATCH (".data"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_EXACT_MATCH (".rdata"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_EXACT_MATCH (".text"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
+{ COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
+  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
+
+#include "coff-arm.c"
diff --git a/bfd/pei-arm.c b/bfd/pei-arm.c
index 216b94509ad43516062118916a9ece66c3553fed..21583c9a2d58021fb34175cf859c030a9dace978 100644
--- a/bfd/pei-arm.c
+++ b/bfd/pei-arm.c
@@ -26,6 +26,9 @@
 #define TARGET_LITTLE_NAME "pei-arm-little"
 #define TARGET_BIG_SYM     arm_pei_be_vec
 #define TARGET_BIG_NAME    "pei-arm-big"
+#define ARMMAGIC 0x1c0
+/* Constant below is IMAGE_NT_OPTIONAL_HDR_MAGIC.  */
+#define ZMAGIC   0x10b    /* Demand load format, eg normal ld output 0x10b.  */
 #endif
 
 #define COFF_IMAGE_WITH_PE
diff --git a/bfd/targets.c b/bfd/targets.c
index 672dc2bb1a4d5643f84577d81d4c9cebc14429d9..a1a33ae5461daa8dc945466c26bae2d12fc79294 100644
--- a/bfd/targets.c
+++ b/bfd/targets.c
@@ -702,6 +702,8 @@ extern const bfd_target arm_pe_be_vec;
 extern const bfd_target arm_pe_le_vec;
 extern const bfd_target arm_pe_wince_be_vec;
 extern const bfd_target arm_pe_wince_le_vec;
+extern const bfd_target arm_pei_be_old_vec;
+extern const bfd_target arm_pei_le_old_vec;
 extern const bfd_target arm_pei_be_vec;
 extern const bfd_target arm_pei_le_vec;
 extern const bfd_target arm_pei_wince_be_vec;
@@ -1027,6 +1029,8 @@ static const bfd_target * const _bfd_target_vector[] =
 	&arm_pe_le_vec,
 	&arm_pe_wince_be_vec,
 	&arm_pe_wince_le_vec,
+	&arm_pei_be_old_vec,
+	&arm_pei_le_old_vec,
 	&arm_pei_be_vec,
 	&arm_pei_le_vec,
 	&arm_pei_wince_be_vec,
diff --git a/binutils/NEWS b/binutils/NEWS
index 994546330f95ccc3b859ffb17388b2cc5ec6a653..9aa45dab2b83edb3e7b3778378e7f66ed454f459 100644
--- a/binutils/NEWS
+++ b/binutils/NEWS
@@ -5,6 +5,14 @@
 * Support for efi-app-aarch64, efi-rtdrv-aarch64 and efi-bsdrv-aarch64 has been
   added to objcopy in order to enable UEFI development using binutils.
 
+* Support for efi-app-arm, efi-rtdrv-arm and efi-bsdrv-arm has been
+  added to objcopy in order to enable UEFI development using binutils.
+
+* The PE Image format pei-arm-little has been renamed to pei-arm-little-old and
+  a new pei-arm-little has been added with the MAGIC and ZMAGIC numbers from
+  the Microsoft PE specification added.  This allows the format to be
+  recognized by standard Windows tools.
+
 Changes in 2.37:
 
 * The readelf tool has a new command line option which can be used to specify
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 458a6d96cccbeb154f95e5ba54734b956d14f879..dbd18444d573048b2a262d36a72245612239efcd 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -5003,6 +5003,13 @@ convert_efi_target (char *efi)
       char *t = "aarch64-little";
       strcpy (efi + 4, t);
     }
+  else if (strcmp (efi + 4, "arm") == 0)
+    {
+      /* Change arm to arm-little.  */
+      efi = (char *) xrealloc (efi, strlen (efi) + 7);
+      char *t = "arm-little";
+      strcpy (efi + 4, t);
+    }
 }
 
 /* Allocate and return a pointer to a struct section_add, initializing the
diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.d b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
new file mode 100644
index 0000000000000000000000000000000000000000..bd251f8e3f2cedc3f4ed530d28d75c8c54942336
--- /dev/null
+++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
@@ -0,0 +1,16 @@
+#skip: armeb-*-* *-*-wince *-*-vxworks *-*-pe
+#ld: -e0
+#PROG: objcopy
+#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-arm
+#objdump: -h -f
+#name: Check if efi app format is recognized
+
+.*:     file format pei-arm-little
+architecture: armv3m, flags 0x00000132:
+EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
+start address 0x00000000
+
+Sections:
+Idx Name          Size      VMA       LMA       File off  Algn
+  0 \.text         00000044  00008000  00008000  00000200  2\*\*2
+                  CONTENTS, ALLOC, LOAD, CODE
diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.s b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
new file mode 100644
index 0000000000000000000000000000000000000000..f5e629895a2e72da9756697bec177a042a64472c
--- /dev/null
+++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
@@ -0,0 +1,56 @@
+	.arch armv7-a
+        .eabi_attribute 28, 1
+        .eabi_attribute 20, 1
+        .eabi_attribute 21, 1
+        .eabi_attribute 23, 3
+        .eabi_attribute 24, 1
+        .eabi_attribute 25, 1
+        .eabi_attribute 26, 2
+        .eabi_attribute 30, 6
+        .eabi_attribute 34, 1
+        .eabi_attribute 18, 4
+        .file   "hello.c"
+        .text
+        .align  2
+        .global foo
+        .arch armv7-a
+        .syntax unified
+        .arm
+        .fpu neon
+        .type   foo, %function
+foo:
+        @ args = 0, pretend = 0, frame = 8
+        @ frame_needed = 1, uses_anonymous_args = 0
+        @ link register save eliminated.
+        str     fp, [sp, #-4]!
+        add     fp, sp, #0
+        sub     sp, sp, #12
+        str     r0, [fp, #-8]
+        ldr     r3, [fp, #-8]
+        mul     r3, r3, r3
+        mov     r0, r3
+        add     sp, fp, #0
+        @ sp needed
+        ldr     fp, [sp], #4
+        bx      lr
+        .size   foo, .-foo
+        .align  2
+        .global main
+        .syntax unified
+        .arm
+        .fpu neon
+        .type   main, %function
+main:
+        @ args = 0, pretend = 0, frame = 0
+        @ frame_needed = 1, uses_anonymous_args = 0
+        push    {fp, lr}
+        add     fp, sp, #4
+        mov     r0, #5
+        bl      foo
+        mov     r3, r0
+        mov     r0, r3
+        pop     {fp, pc}
+        .size   main, .-main
+        .ident  "GCC: (6ea25fd023ef3674e34cc67cd97771195d6f93e4) 12.0.0 20210715 (experimental)"
+        .section        .note.GNU-stack,"",%progbits
+
diff --git a/include/coff/arm.h b/include/coff/arm.h
index 7b618d94edef0b8660544157df04f2a1e79f09a4..a46fac0f83054e27eea34ac77435ea1f4ad5d1ed 100644
--- a/include/coff/arm.h
+++ b/include/coff/arm.h
@@ -75,7 +75,9 @@
  
   XXX - NC 5/6/97.  */
 
+#ifndef ARMMAGIC
 #define	ARMMAGIC	0xa00  /* I just made this up */
+#endif
 
 #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC))
 
@@ -87,7 +89,9 @@
 #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic != ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic != ARMV7PEMAGIC))
 
 #define OMAGIC          0404    /* object files, eg as output */
+#ifndef ZMAGIC
 #define ZMAGIC          0413    /* demand load format, eg normal ld output */
+#endif
 #define STMAGIC		0401	/* target shlib */
 #define SHMAGIC		0443	/* host   shlib */
 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
  2021-10-29 11:05 [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm) Tamar Christina
@ 2021-11-05 17:39 ` Tamar Christina
  2021-11-12  6:11   ` Tamar Christina
  2021-11-12 17:17 ` Richard Earnshaw
  1 sibling, 1 reply; 5+ messages in thread
From: Tamar Christina @ 2021-11-05 17:39 UTC (permalink / raw)
  To: binutils; +Cc: Richard Earnshaw, nd, Ramana Radhakrishnan

Ping (adding mailing list back in)

> -----Original Message-----
> From: Binutils <binutils-bounces+tamar.christina=arm.com@sourceware.org>
> On Behalf Of Tamar Christina via Binutils
> Sent: Friday, October 29, 2021 12:06 PM
> To: binutils@sourceware.org
> Cc: Richard Earnshaw <Richard.Earnshaw@arm.com>; nd <nd@arm.com>;
> Ramana Radhakrishnan <Ramana.Radhakrishnan@arm.com>
> Subject: [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
> 
> Hi All,
> 
> This adds support for efi-*-arm by virtue of fixing the magic hashes in pei-
> arm-little to have the right values according to the PE specification[1].
> 
> Because there are likely existing files out there using the wrong format I have
> added a new target pei-arm-little-old so that binutils can continue to
> recognize the old format.  Over time code should converge to the new one.
> 
> Additionally the pei-arm-little target is now also exposed to elf targets such
> as arm-*-elf, arm*-*-linux-* etc such that one can create EFI images on linux
> hosted toolchains as well.
> 
> With these changes the EFI binary is now recognized by third party tools:
> 
> > pecli info main.efi
> 
> Metadata
> ==========================================================
> ======================
> MD5:            5591110a0160b4a29497171ae44e0707
> SHA1:           4f3bb55ec366e64b9166ed202a49e787f9b78a84
> SHA256:
> b18f1b5dd210afe9b73ff0d15be29b4fb4f52db82cae4df0f5cd1877a8e9b48c
> Imphash:
> Size:           1288 bytes
> Type:           PE32 executable (EFI application) ARM (stripped to external PDB),
> for MS Windows
> Compile Time:   2021-10-29 00:00:00 (UTC - 0x0       )
> Entry point:    0x0 (section None)
> 
> Sections
> ==========================================================
> ======================
> Name      RWX  VirtSize   VirtAddr   RawAddr   RawSize   Entropy  md5
> .text     RWX  0x44       0x8000     0x200     0x200       1.07
> 0b48a18cac04f6e96a1ffc44fb0d4902
> 
> 
> Imports
> ==========================================================
> ======================
> 
> Any magic number is based on the Microsoft PE specification [1].
> 
> [1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
> 
> build on native hardware and regtested on
>   arm-none-elf, arm-none-elf (32 bit host),
>   arm-none-linux-gnueabihf, arm-none-linux-gnueabihf (32 bit host)
> 
> Cross-compiled and regtested on
>   arm-none-linux-gnueabihf, armeb-none-elf, arm-wince-pe, arm-none-pe
> 
> and no issues.
> 
> Ok for master?
> 
> Thanks,
> Tamar
> 
> bfd/ChangeLog:
> 
> 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> 
> 	PR binutils/26218
> 	* pei-arm-old.c: New file, define targets arm_pei_be_vec_old and
> 	arm_pei_le_vec_old.
> 	* Makefile.am: Use them.
> 	* Makefile.in: Likewise.
> 	* config.bfd: Likewise.
> 	* configure: Likewise.
> 	* configure.ac: Likewise.
> 	* targets.c: Likewise.
> 	* pei-arm.c (ARMMAGIC, (ZMAGIC): Define
> 	* coffcode.h (coff_set_arch_mach_hook): Handle case where
> 	ARMMAGIC == ARMPEMAGIC.
> 
> binutils/ChangeLog:
> 
> 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> 
> 	PR binutils/26218
> 	* NEWS: Add new support.
> 	* objcopy.c (convert_efi_target): Add efi-*-arm support
> 	* testsuite/binutils-all/arm/pei-arm-little.d: New test.
> 	* testsuite/binutils-all/arm/pei-arm-little.s: New test.
> 
> include/ChangeLog:
> 
> 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> 
> 	PR binutils/26218
> 	* coff/arm.h: Make ARMMAGIC and ZMAGIC overrideable.
> 
> --- inline copy of patch --
> diff --git a/bfd/Makefile.am b/bfd/Makefile.am index
> 7777f733702c09b441bc591daa5b8c3d9ab2abd7..cc7814639caf54301dfb0380b9
> 6b34b37a12e7c7 100644
> --- a/bfd/Makefile.am
> +++ b/bfd/Makefile.am
> @@ -509,6 +509,7 @@ BFD32_BACKENDS_CFILES = \
>  	pef.c \
>  	pei-arm-wince.c \
>  	pei-arm.c \
> +	pei-arm-old.c \
>  	pei-i386.c \
>  	pei-mcore.c \
>  	pei-sh.c \
> diff --git a/bfd/Makefile.in b/bfd/Makefile.in index
> b243151fea350032c6cb90f1ec1f359bdba589f5..730c845d85b2df89d1c7bb8541
> 22dd7addc8ac69 100644
> --- a/bfd/Makefile.in
> +++ b/bfd/Makefile.in
> @@ -935,6 +935,7 @@ BFD32_BACKENDS_CFILES = \
>  	pef.c \
>  	pei-arm-wince.c \
>  	pei-arm.c \
> +	pei-arm-old.c \
>  	pei-i386.c \
>  	pei-mcore.c \
>  	pei-sh.c \
> diff --git a/bfd/coffcode.h b/bfd/coffcode.h index
> 86688d31cdb0a4809b7fc01079d16e87573dd56f..1cf02c1b7c1551b83135e3b74
> 3a2cfd35e0cb259 100644
> --- a/bfd/coffcode.h
> +++ b/bfd/coffcode.h
> @@ -2196,7 +2196,9 @@ coff_set_arch_mach_hook (bfd *abfd, void *
> filehdr)  #endif  #ifdef ARMMAGIC
>      case ARMMAGIC:
> +#if ARMMAGIC != ARMPEMAGIC
>      case ARMPEMAGIC:
> +#endif
>      case THUMBPEMAGIC:
>        arch = bfd_arch_arm;
>        machine = bfd_arm_get_mach_from_notes (abfd,
> ARM_NOTE_SECTION); diff --git a/bfd/config.bfd b/bfd/config.bfd index
> 776918253ef11140df94042b379789e6f7b1e598..34c20a56b2614eabba79c09ba
> d8fbdddc4dfd74b 100644
> --- a/bfd/config.bfd
> +++ b/bfd/config.bfd
> @@ -362,7 +362,7 @@ case "${targ}" in
>      ;;
>    arm*-*-haiku*)
>      targ_defvec=arm_elf32_le_vec
> -    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> arm_pei_be_vec"
> +    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
>      ;;
>    arm-*-nacl*)
>      targ_defvec=arm_elf32_nacl_le_vec
> @@ -393,7 +393,7 @@ case "${targ}" in
>      ;;
>    arm-*-pe*)
>      targ_defvec=arm_pe_le_vec
> -    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> arm_pei_be_vec"
> +    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
>      targ_underscore=yes
>      ;;
>    arm-*-phoenix*)
> @@ -412,7 +412,7 @@ case "${targ}" in
>    arm*-*-uclinux* | arm-*-kfreebsd*-gnu | \
>    arm*-*-eabi* | arm-*-rtems* | arm*-*-uclinuxfdpiceabi)
>      targ_defvec=arm_elf32_le_vec
> -    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> arm_elf32_fdpic_be_vec"
> +    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> arm_elf32_fdpic_be_vec arm_pei_le_vec"
>      ;;
>    arm*-*-vxworks | arm*-*-windiss)
>      targ_defvec=arm_elf32_vxworks_le_vec
> diff --git a/bfd/configure b/bfd/configure index
> 5741e039a4fed386d826ad4910e1fd029bd37f23..19ea57d1b11e2ada1682eb44
> 56514e1c03fb6da5 100755
> --- a/bfd/configure
> +++ b/bfd/configure
> @@ -13281,6 +13281,8 @@ do
>      arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
>      arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> $coff" ;;
>      arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> $coff" ;;
> +    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> $coff" ;;
> +    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> $coff" ;;
>      arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
>      arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
>      arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo
> peigen.lo $coff" ;;
> diff --git a/bfd/configure.ac b/bfd/configure.ac index
> 1fadfd7d91ed7ead6b04be2173d6c596fb4af221..951ce21cec061690a7734da4a
> 4739c1975d19710 100644
> --- a/bfd/configure.ac
> +++ b/bfd/configure.ac
> @@ -460,6 +460,8 @@ do
>      arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
>      arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> $coff" ;;
>      arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> $coff" ;;
> +    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> $coff" ;;
> +    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> $coff" ;;
>      arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
>      arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
>      arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo
> peigen.lo $coff" ;;
> diff --git a/bfd/pei-arm-old.c b/bfd/pei-arm-old.c new file mode 100644
> index
> 0000000000000000000000000000000000000000..a42f7c8a4a86b49e0e52031459
> 2dfaa3eee42951
> --- /dev/null
> +++ b/bfd/pei-arm-old.c
> @@ -0,0 +1,55 @@
> +/* BFD back-end for arm PE IMAGE COFF files.
> +   Copyright (C) 1995-2021 Free Software Foundation, Inc.
> +
> +   This file is part of BFD, the Binary File Descriptor library.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program; if not, write to the Free Software
> +   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
> +   MA 02110-1301, USA.  */
> +
> +#include "sysdep.h"
> +#include "bfd.h"
> +
> +#ifndef TARGET_LITTLE_SYM
> +#define TARGET_LITTLE_SYM  arm_pei_le_old_vec #define
> +TARGET_LITTLE_NAME "pei-arm-little-old"
> +#define TARGET_BIG_SYM     arm_pei_be_old_vec
> +#define TARGET_BIG_NAME    "pei-arm-big-old"
> +#endif
> +
> +#define COFF_IMAGE_WITH_PE
> +#define COFF_WITH_PE
> +#define PCRELOFFSET	   true
> +/* Long section names not allowed in executable images, only object
> +files.  */ #define COFF_LONG_SECTION_NAMES 0
> +
> +#define COFF_SECTION_ALIGNMENT_ENTRIES \ {
> +COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> {
> +COFF_SECTION_NAME_EXACT_MATCH (".data"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> {
> +COFF_SECTION_NAME_EXACT_MATCH (".rdata"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> {
> +COFF_SECTION_NAME_EXACT_MATCH (".text"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> {
> +COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> {
> +COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> {
> +COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
> {
> +COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
> +
> +#include "coff-arm.c"
> diff --git a/bfd/pei-arm.c b/bfd/pei-arm.c index
> 216b94509ad43516062118916a9ece66c3553fed..21583c9a2d58021fb34175cf85
> 9c030a9dace978 100644
> --- a/bfd/pei-arm.c
> +++ b/bfd/pei-arm.c
> @@ -26,6 +26,9 @@
>  #define TARGET_LITTLE_NAME "pei-arm-little"
>  #define TARGET_BIG_SYM     arm_pei_be_vec
>  #define TARGET_BIG_NAME    "pei-arm-big"
> +#define ARMMAGIC 0x1c0
> +/* Constant below is IMAGE_NT_OPTIONAL_HDR_MAGIC.  */
> +#define ZMAGIC   0x10b    /* Demand load format, eg normal ld output
> 0x10b.  */
>  #endif
> 
>  #define COFF_IMAGE_WITH_PE
> diff --git a/bfd/targets.c b/bfd/targets.c index
> 672dc2bb1a4d5643f84577d81d4c9cebc14429d9..a1a33ae5461daa8dc945466c2
> 6bae2d12fc79294 100644
> --- a/bfd/targets.c
> +++ b/bfd/targets.c
> @@ -702,6 +702,8 @@ extern const bfd_target arm_pe_be_vec;  extern
> const bfd_target arm_pe_le_vec;  extern const bfd_target
> arm_pe_wince_be_vec;  extern const bfd_target arm_pe_wince_le_vec;
> +extern const bfd_target arm_pei_be_old_vec; extern const bfd_target
> +arm_pei_le_old_vec;
>  extern const bfd_target arm_pei_be_vec;  extern const bfd_target
> arm_pei_le_vec;  extern const bfd_target arm_pei_wince_be_vec; @@ -
> 1027,6 +1029,8 @@ static const bfd_target * const _bfd_target_vector[] =
>  	&arm_pe_le_vec,
>  	&arm_pe_wince_be_vec,
>  	&arm_pe_wince_le_vec,
> +	&arm_pei_be_old_vec,
> +	&arm_pei_le_old_vec,
>  	&arm_pei_be_vec,
>  	&arm_pei_le_vec,
>  	&arm_pei_wince_be_vec,
> diff --git a/binutils/NEWS b/binutils/NEWS index
> 994546330f95ccc3b859ffb17388b2cc5ec6a653..9aa45dab2b83edb3e7b3778378
> e7f66ed454f459 100644
> --- a/binutils/NEWS
> +++ b/binutils/NEWS
> @@ -5,6 +5,14 @@
>  * Support for efi-app-aarch64, efi-rtdrv-aarch64 and efi-bsdrv-aarch64 has
> been
>    added to objcopy in order to enable UEFI development using binutils.
> 
> +* Support for efi-app-arm, efi-rtdrv-arm and efi-bsdrv-arm has been
> +  added to objcopy in order to enable UEFI development using binutils.
> +
> +* The PE Image format pei-arm-little has been renamed to
> +pei-arm-little-old and
> +  a new pei-arm-little has been added with the MAGIC and ZMAGIC
> numbers
> +from
> +  the Microsoft PE specification added.  This allows the format to be
> +  recognized by standard Windows tools.
> +
>  Changes in 2.37:
> 
>  * The readelf tool has a new command line option which can be used to
> specify diff --git a/binutils/objcopy.c b/binutils/objcopy.c index
> 458a6d96cccbeb154f95e5ba54734b956d14f879..dbd18444d573048b2a262d36a
> 72245612239efcd 100644
> --- a/binutils/objcopy.c
> +++ b/binutils/objcopy.c
> @@ -5003,6 +5003,13 @@ convert_efi_target (char *efi)
>        char *t = "aarch64-little";
>        strcpy (efi + 4, t);
>      }
> +  else if (strcmp (efi + 4, "arm") == 0)
> +    {
> +      /* Change arm to arm-little.  */
> +      efi = (char *) xrealloc (efi, strlen (efi) + 7);
> +      char *t = "arm-little";
> +      strcpy (efi + 4, t);
> +    }
>  }
> 
>  /* Allocate and return a pointer to a struct section_add, initializing the diff --
> git a/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> new file mode 100644
> index
> 0000000000000000000000000000000000000000..bd251f8e3f2cedc3f4ed530d28
> d75c8c54942336
> --- /dev/null
> +++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> @@ -0,0 +1,16 @@
> +#skip: armeb-*-* *-*-wince *-*-vxworks *-*-pe
> +#ld: -e0
> +#PROG: objcopy
> +#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-arm
> +#objdump: -h -f
> +#name: Check if efi app format is recognized
> +
> +.*:     file format pei-arm-little
> +architecture: armv3m, flags 0x00000132:
> +EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
> +start address 0x00000000
> +
> +Sections:
> +Idx Name          Size      VMA       LMA       File off  Algn
> +  0 \.text         00000044  00008000  00008000  00000200  2\*\*2
> +                  CONTENTS, ALLOC, LOAD, CODE
> diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> new file mode 100644
> index
> 0000000000000000000000000000000000000000..f5e629895a2e72da9756697be
> c177a042a64472c
> --- /dev/null
> +++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> @@ -0,0 +1,56 @@
> +	.arch armv7-a
> +        .eabi_attribute 28, 1
> +        .eabi_attribute 20, 1
> +        .eabi_attribute 21, 1
> +        .eabi_attribute 23, 3
> +        .eabi_attribute 24, 1
> +        .eabi_attribute 25, 1
> +        .eabi_attribute 26, 2
> +        .eabi_attribute 30, 6
> +        .eabi_attribute 34, 1
> +        .eabi_attribute 18, 4
> +        .file   "hello.c"
> +        .text
> +        .align  2
> +        .global foo
> +        .arch armv7-a
> +        .syntax unified
> +        .arm
> +        .fpu neon
> +        .type   foo, %function
> +foo:
> +        @ args = 0, pretend = 0, frame = 8
> +        @ frame_needed = 1, uses_anonymous_args = 0
> +        @ link register save eliminated.
> +        str     fp, [sp, #-4]!
> +        add     fp, sp, #0
> +        sub     sp, sp, #12
> +        str     r0, [fp, #-8]
> +        ldr     r3, [fp, #-8]
> +        mul     r3, r3, r3
> +        mov     r0, r3
> +        add     sp, fp, #0
> +        @ sp needed
> +        ldr     fp, [sp], #4
> +        bx      lr
> +        .size   foo, .-foo
> +        .align  2
> +        .global main
> +        .syntax unified
> +        .arm
> +        .fpu neon
> +        .type   main, %function
> +main:
> +        @ args = 0, pretend = 0, frame = 0
> +        @ frame_needed = 1, uses_anonymous_args = 0
> +        push    {fp, lr}
> +        add     fp, sp, #4
> +        mov     r0, #5
> +        bl      foo
> +        mov     r3, r0
> +        mov     r0, r3
> +        pop     {fp, pc}
> +        .size   main, .-main
> +        .ident  "GCC: (6ea25fd023ef3674e34cc67cd97771195d6f93e4) 12.0.0
> 20210715 (experimental)"
> +        .section        .note.GNU-stack,"",%progbits
> +
> diff --git a/include/coff/arm.h b/include/coff/arm.h index
> 7b618d94edef0b8660544157df04f2a1e79f09a4..a46fac0f83054e27eea34ac774
> 35ea1f4ad5d1ed 100644
> --- a/include/coff/arm.h
> +++ b/include/coff/arm.h
> @@ -75,7 +75,9 @@
> 
>    XXX - NC 5/6/97.  */
> 
> +#ifndef ARMMAGIC
>  #define	ARMMAGIC	0xa00  /* I just made this up */
> +#endif
> 
>  #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC))
> 
> @@ -87,7 +89,9 @@
>  #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic !=
> ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic !=
> ARMV7PEMAGIC))
> 
>  #define OMAGIC          0404    /* object files, eg as output */
> +#ifndef ZMAGIC
>  #define ZMAGIC          0413    /* demand load format, eg normal ld output */
> +#endif
>  #define STMAGIC		0401	/* target shlib */
>  #define SHMAGIC		0443	/* host   shlib */
> 
> 
> 
> --

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
  2021-11-05 17:39 ` Tamar Christina
@ 2021-11-12  6:11   ` Tamar Christina
  0 siblings, 0 replies; 5+ messages in thread
From: Tamar Christina @ 2021-11-12  6:11 UTC (permalink / raw)
  To: binutils; +Cc: Richard Earnshaw, nd, Ramana Radhakrishnan

Ping x2

> -----Original Message-----
> From: Binutils <binutils-bounces+tamar.christina=arm.com@sourceware.org>
> On Behalf Of Tamar Christina via Binutils
> Sent: Friday, November 5, 2021 6:40 PM
> To: binutils@sourceware.org
> Cc: Richard Earnshaw <Richard.Earnshaw@arm.com>; nd <nd@arm.com>;
> Ramana Radhakrishnan <Ramana.Radhakrishnan@arm.com>
> Subject: RE: [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
> 
> Ping (adding mailing list back in)
> 
> > -----Original Message-----
> > From: Binutils
> > <binutils-bounces+tamar.christina=arm.com@sourceware.org>
> > On Behalf Of Tamar Christina via Binutils
> > Sent: Friday, October 29, 2021 12:06 PM
> > To: binutils@sourceware.org
> > Cc: Richard Earnshaw <Richard.Earnshaw@arm.com>; nd <nd@arm.com>;
> > Ramana Radhakrishnan <Ramana.Radhakrishnan@arm.com>
> > Subject: [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
> >
> > Hi All,
> >
> > This adds support for efi-*-arm by virtue of fixing the magic hashes
> > in pei- arm-little to have the right values according to the PE specification[1].
> >
> > Because there are likely existing files out there using the wrong
> > format I have added a new target pei-arm-little-old so that binutils
> > can continue to recognize the old format.  Over time code should converge
> to the new one.
> >
> > Additionally the pei-arm-little target is now also exposed to elf
> > targets such as arm-*-elf, arm*-*-linux-* etc such that one can create
> > EFI images on linux hosted toolchains as well.
> >
> > With these changes the EFI binary is now recognized by third party tools:
> >
> > > pecli info main.efi
> >
> > Metadata
> >
> ==========================================================
> > ======================
> > MD5:            5591110a0160b4a29497171ae44e0707
> > SHA1:           4f3bb55ec366e64b9166ed202a49e787f9b78a84
> > SHA256:
> > b18f1b5dd210afe9b73ff0d15be29b4fb4f52db82cae4df0f5cd1877a8e9b48c
> > Imphash:
> > Size:           1288 bytes
> > Type:           PE32 executable (EFI application) ARM (stripped to external
> PDB),
> > for MS Windows
> > Compile Time:   2021-10-29 00:00:00 (UTC - 0x0       )
> > Entry point:    0x0 (section None)
> >
> > Sections
> >
> ==========================================================
> > ======================
> > Name      RWX  VirtSize   VirtAddr   RawAddr   RawSize   Entropy  md5
> > .text     RWX  0x44       0x8000     0x200     0x200       1.07
> > 0b48a18cac04f6e96a1ffc44fb0d4902
> >
> >
> > Imports
> >
> ==========================================================
> > ======================
> >
> > Any magic number is based on the Microsoft PE specification [1].
> >
> > [1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
> >
> > build on native hardware and regtested on
> >   arm-none-elf, arm-none-elf (32 bit host),
> >   arm-none-linux-gnueabihf, arm-none-linux-gnueabihf (32 bit host)
> >
> > Cross-compiled and regtested on
> >   arm-none-linux-gnueabihf, armeb-none-elf, arm-wince-pe, arm-none-pe
> >
> > and no issues.
> >
> > Ok for master?
> >
> > Thanks,
> > Tamar
> >
> > bfd/ChangeLog:
> >
> > 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> >
> > 	PR binutils/26218
> > 	* pei-arm-old.c: New file, define targets arm_pei_be_vec_old and
> > 	arm_pei_le_vec_old.
> > 	* Makefile.am: Use them.
> > 	* Makefile.in: Likewise.
> > 	* config.bfd: Likewise.
> > 	* configure: Likewise.
> > 	* configure.ac: Likewise.
> > 	* targets.c: Likewise.
> > 	* pei-arm.c (ARMMAGIC, (ZMAGIC): Define
> > 	* coffcode.h (coff_set_arch_mach_hook): Handle case where
> > 	ARMMAGIC == ARMPEMAGIC.
> >
> > binutils/ChangeLog:
> >
> > 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> >
> > 	PR binutils/26218
> > 	* NEWS: Add new support.
> > 	* objcopy.c (convert_efi_target): Add efi-*-arm support
> > 	* testsuite/binutils-all/arm/pei-arm-little.d: New test.
> > 	* testsuite/binutils-all/arm/pei-arm-little.s: New test.
> >
> > include/ChangeLog:
> >
> > 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> >
> > 	PR binutils/26218
> > 	* coff/arm.h: Make ARMMAGIC and ZMAGIC overrideable.
> >
> > --- inline copy of patch --
> > diff --git a/bfd/Makefile.am b/bfd/Makefile.am index
> >
> 7777f733702c09b441bc591daa5b8c3d9ab2abd7..cc7814639caf54301dfb0380b9
> > 6b34b37a12e7c7 100644
> > --- a/bfd/Makefile.am
> > +++ b/bfd/Makefile.am
> > @@ -509,6 +509,7 @@ BFD32_BACKENDS_CFILES = \
> >  	pef.c \
> >  	pei-arm-wince.c \
> >  	pei-arm.c \
> > +	pei-arm-old.c \
> >  	pei-i386.c \
> >  	pei-mcore.c \
> >  	pei-sh.c \
> > diff --git a/bfd/Makefile.in b/bfd/Makefile.in index
> >
> b243151fea350032c6cb90f1ec1f359bdba589f5..730c845d85b2df89d1c7bb8541
> > 22dd7addc8ac69 100644
> > --- a/bfd/Makefile.in
> > +++ b/bfd/Makefile.in
> > @@ -935,6 +935,7 @@ BFD32_BACKENDS_CFILES = \
> >  	pef.c \
> >  	pei-arm-wince.c \
> >  	pei-arm.c \
> > +	pei-arm-old.c \
> >  	pei-i386.c \
> >  	pei-mcore.c \
> >  	pei-sh.c \
> > diff --git a/bfd/coffcode.h b/bfd/coffcode.h index
> >
> 86688d31cdb0a4809b7fc01079d16e87573dd56f..1cf02c1b7c1551b83135e3b74
> > 3a2cfd35e0cb259 100644
> > --- a/bfd/coffcode.h
> > +++ b/bfd/coffcode.h
> > @@ -2196,7 +2196,9 @@ coff_set_arch_mach_hook (bfd *abfd, void *
> > filehdr)  #endif  #ifdef ARMMAGIC
> >      case ARMMAGIC:
> > +#if ARMMAGIC != ARMPEMAGIC
> >      case ARMPEMAGIC:
> > +#endif
> >      case THUMBPEMAGIC:
> >        arch = bfd_arch_arm;
> >        machine = bfd_arm_get_mach_from_notes (abfd,
> ARM_NOTE_SECTION);
> > diff --git a/bfd/config.bfd b/bfd/config.bfd index
> >
> 776918253ef11140df94042b379789e6f7b1e598..34c20a56b2614eabba79c09ba
> > d8fbdddc4dfd74b 100644
> > --- a/bfd/config.bfd
> > +++ b/bfd/config.bfd
> > @@ -362,7 +362,7 @@ case "${targ}" in
> >      ;;
> >    arm*-*-haiku*)
> >      targ_defvec=arm_elf32_le_vec
> > -    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> > arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> > arm_pei_be_vec"
> > +    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> > arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> > arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
> >      ;;
> >    arm-*-nacl*)
> >      targ_defvec=arm_elf32_nacl_le_vec @@ -393,7 +393,7 @@ case
> > "${targ}" in
> >      ;;
> >    arm-*-pe*)
> >      targ_defvec=arm_pe_le_vec
> > -    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> > arm_pei_be_vec"
> > +    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> > arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
> >      targ_underscore=yes
> >      ;;
> >    arm-*-phoenix*)
> > @@ -412,7 +412,7 @@ case "${targ}" in
> >    arm*-*-uclinux* | arm-*-kfreebsd*-gnu | \
> >    arm*-*-eabi* | arm-*-rtems* | arm*-*-uclinuxfdpiceabi)
> >      targ_defvec=arm_elf32_le_vec
> > -    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> > arm_elf32_fdpic_be_vec"
> > +    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> > arm_elf32_fdpic_be_vec arm_pei_le_vec"
> >      ;;
> >    arm*-*-vxworks | arm*-*-windiss)
> >      targ_defvec=arm_elf32_vxworks_le_vec
> > diff --git a/bfd/configure b/bfd/configure index
> >
> 5741e039a4fed386d826ad4910e1fd029bd37f23..19ea57d1b11e2ada1682eb44
> > 56514e1c03fb6da5 100755
> > --- a/bfd/configure
> > +++ b/bfd/configure
> > @@ -13281,6 +13281,8 @@ do
> >      arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
> >      arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> > $coff" ;;
> >      arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> > $coff" ;;
> > +    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> > $coff" ;;
> > +    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> > $coff" ;;
> >      arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
> >      arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
> >      arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo
> > peigen.lo $coff" ;;
> > diff --git a/bfd/configure.ac b/bfd/configure.ac index
> >
> 1fadfd7d91ed7ead6b04be2173d6c596fb4af221..951ce21cec061690a7734da4a
> > 4739c1975d19710 100644
> > --- a/bfd/configure.ac
> > +++ b/bfd/configure.ac
> > @@ -460,6 +460,8 @@ do
> >      arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
> >      arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> > $coff" ;;
> >      arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> > $coff" ;;
> > +    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> > $coff" ;;
> > +    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> > $coff" ;;
> >      arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
> >      arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
> >      arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo
> > peigen.lo $coff" ;;
> > diff --git a/bfd/pei-arm-old.c b/bfd/pei-arm-old.c new file mode
> > 100644 index
> >
> 0000000000000000000000000000000000000000..a42f7c8a4a86b49e0e52031459
> > 2dfaa3eee42951
> > --- /dev/null
> > +++ b/bfd/pei-arm-old.c
> > @@ -0,0 +1,55 @@
> > +/* BFD back-end for arm PE IMAGE COFF files.
> > +   Copyright (C) 1995-2021 Free Software Foundation, Inc.
> > +
> > +   This file is part of BFD, the Binary File Descriptor library.
> > +
> > +   This program is free software; you can redistribute it and/or modify
> > +   it under the terms of the GNU General Public License as published by
> > +   the Free Software Foundation; either version 3 of the License, or
> > +   (at your option) any later version.
> > +
> > +   This program is distributed in the hope that it will be useful,
> > +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +   GNU General Public License for more details.
> > +
> > +   You should have received a copy of the GNU General Public License
> > +   along with this program; if not, write to the Free Software
> > +   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
> > +   MA 02110-1301, USA.  */
> > +
> > +#include "sysdep.h"
> > +#include "bfd.h"
> > +
> > +#ifndef TARGET_LITTLE_SYM
> > +#define TARGET_LITTLE_SYM  arm_pei_le_old_vec #define
> > +TARGET_LITTLE_NAME "pei-arm-little-old"
> > +#define TARGET_BIG_SYM     arm_pei_be_old_vec
> > +#define TARGET_BIG_NAME    "pei-arm-big-old"
> > +#endif
> > +
> > +#define COFF_IMAGE_WITH_PE
> > +#define COFF_WITH_PE
> > +#define PCRELOFFSET	   true
> > +/* Long section names not allowed in executable images, only object
> > +files.  */ #define COFF_LONG_SECTION_NAMES 0
> > +
> > +#define COFF_SECTION_ALIGNMENT_ENTRIES \ {
> > +COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \
> > {
> > +COFF_SECTION_NAME_EXACT_MATCH (".data"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \
> > {
> > +COFF_SECTION_NAME_EXACT_MATCH (".rdata"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \
> > {
> > +COFF_SECTION_NAME_EXACT_MATCH (".text"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \
> > {
> > +COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \
> > {
> > +COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \
> > {
> > +COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 },
> \
> > {
> > +COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
> > +
> > +#include "coff-arm.c"
> > diff --git a/bfd/pei-arm.c b/bfd/pei-arm.c index
> >
> 216b94509ad43516062118916a9ece66c3553fed..21583c9a2d58021fb34175cf85
> > 9c030a9dace978 100644
> > --- a/bfd/pei-arm.c
> > +++ b/bfd/pei-arm.c
> > @@ -26,6 +26,9 @@
> >  #define TARGET_LITTLE_NAME "pei-arm-little"
> >  #define TARGET_BIG_SYM     arm_pei_be_vec
> >  #define TARGET_BIG_NAME    "pei-arm-big"
> > +#define ARMMAGIC 0x1c0
> > +/* Constant below is IMAGE_NT_OPTIONAL_HDR_MAGIC.  */
> > +#define ZMAGIC   0x10b    /* Demand load format, eg normal ld output
> > 0x10b.  */
> >  #endif
> >
> >  #define COFF_IMAGE_WITH_PE
> > diff --git a/bfd/targets.c b/bfd/targets.c index
> >
> 672dc2bb1a4d5643f84577d81d4c9cebc14429d9..a1a33ae5461daa8dc945466c2
> > 6bae2d12fc79294 100644
> > --- a/bfd/targets.c
> > +++ b/bfd/targets.c
> > @@ -702,6 +702,8 @@ extern const bfd_target arm_pe_be_vec;  extern
> > const bfd_target arm_pe_le_vec;  extern const bfd_target
> > arm_pe_wince_be_vec;  extern const bfd_target arm_pe_wince_le_vec;
> > +extern const bfd_target arm_pei_be_old_vec; extern const bfd_target
> > +arm_pei_le_old_vec;
> >  extern const bfd_target arm_pei_be_vec;  extern const bfd_target
> > arm_pei_le_vec;  extern const bfd_target arm_pei_wince_be_vec; @@ -
> > 1027,6 +1029,8 @@ static const bfd_target * const _bfd_target_vector[] =
> >  	&arm_pe_le_vec,
> >  	&arm_pe_wince_be_vec,
> >  	&arm_pe_wince_le_vec,
> > +	&arm_pei_be_old_vec,
> > +	&arm_pei_le_old_vec,
> >  	&arm_pei_be_vec,
> >  	&arm_pei_le_vec,
> >  	&arm_pei_wince_be_vec,
> > diff --git a/binutils/NEWS b/binutils/NEWS index
> >
> 994546330f95ccc3b859ffb17388b2cc5ec6a653..9aa45dab2b83edb3e7b3778378
> > e7f66ed454f459 100644
> > --- a/binutils/NEWS
> > +++ b/binutils/NEWS
> > @@ -5,6 +5,14 @@
> >  * Support for efi-app-aarch64, efi-rtdrv-aarch64 and
> > efi-bsdrv-aarch64 has been
> >    added to objcopy in order to enable UEFI development using binutils.
> >
> > +* Support for efi-app-arm, efi-rtdrv-arm and efi-bsdrv-arm has been
> > +  added to objcopy in order to enable UEFI development using binutils.
> > +
> > +* The PE Image format pei-arm-little has been renamed to
> > +pei-arm-little-old and
> > +  a new pei-arm-little has been added with the MAGIC and ZMAGIC
> > numbers
> > +from
> > +  the Microsoft PE specification added.  This allows the format to be
> > +  recognized by standard Windows tools.
> > +
> >  Changes in 2.37:
> >
> >  * The readelf tool has a new command line option which can be used to
> > specify diff --git a/binutils/objcopy.c b/binutils/objcopy.c index
> >
> 458a6d96cccbeb154f95e5ba54734b956d14f879..dbd18444d573048b2a262d36a
> > 72245612239efcd 100644
> > --- a/binutils/objcopy.c
> > +++ b/binutils/objcopy.c
> > @@ -5003,6 +5003,13 @@ convert_efi_target (char *efi)
> >        char *t = "aarch64-little";
> >        strcpy (efi + 4, t);
> >      }
> > +  else if (strcmp (efi + 4, "arm") == 0)
> > +    {
> > +      /* Change arm to arm-little.  */
> > +      efi = (char *) xrealloc (efi, strlen (efi) + 7);
> > +      char *t = "arm-little";
> > +      strcpy (efi + 4, t);
> > +    }
> >  }
> >
> >  /* Allocate and return a pointer to a struct section_add,
> > initializing the diff -- git
> > a/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> > b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> > new file mode 100644
> > index
> >
> 0000000000000000000000000000000000000000..bd251f8e3f2cedc3f4ed530d28
> > d75c8c54942336
> > --- /dev/null
> > +++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> > @@ -0,0 +1,16 @@
> > +#skip: armeb-*-* *-*-wince *-*-vxworks *-*-pe
> > +#ld: -e0
> > +#PROG: objcopy
> > +#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-arm
> > +#objdump: -h -f
> > +#name: Check if efi app format is recognized
> > +
> > +.*:     file format pei-arm-little
> > +architecture: armv3m, flags 0x00000132:
> > +EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED start address 0x00000000
> > +
> > +Sections:
> > +Idx Name          Size      VMA       LMA       File off  Algn
> > +  0 \.text         00000044  00008000  00008000  00000200  2\*\*2
> > +                  CONTENTS, ALLOC, LOAD, CODE
> > diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> > b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> > new file mode 100644
> > index
> >
> 0000000000000000000000000000000000000000..f5e629895a2e72da9756697be
> > c177a042a64472c
> > --- /dev/null
> > +++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> > @@ -0,0 +1,56 @@
> > +	.arch armv7-a
> > +        .eabi_attribute 28, 1
> > +        .eabi_attribute 20, 1
> > +        .eabi_attribute 21, 1
> > +        .eabi_attribute 23, 3
> > +        .eabi_attribute 24, 1
> > +        .eabi_attribute 25, 1
> > +        .eabi_attribute 26, 2
> > +        .eabi_attribute 30, 6
> > +        .eabi_attribute 34, 1
> > +        .eabi_attribute 18, 4
> > +        .file   "hello.c"
> > +        .text
> > +        .align  2
> > +        .global foo
> > +        .arch armv7-a
> > +        .syntax unified
> > +        .arm
> > +        .fpu neon
> > +        .type   foo, %function
> > +foo:
> > +        @ args = 0, pretend = 0, frame = 8
> > +        @ frame_needed = 1, uses_anonymous_args = 0
> > +        @ link register save eliminated.
> > +        str     fp, [sp, #-4]!
> > +        add     fp, sp, #0
> > +        sub     sp, sp, #12
> > +        str     r0, [fp, #-8]
> > +        ldr     r3, [fp, #-8]
> > +        mul     r3, r3, r3
> > +        mov     r0, r3
> > +        add     sp, fp, #0
> > +        @ sp needed
> > +        ldr     fp, [sp], #4
> > +        bx      lr
> > +        .size   foo, .-foo
> > +        .align  2
> > +        .global main
> > +        .syntax unified
> > +        .arm
> > +        .fpu neon
> > +        .type   main, %function
> > +main:
> > +        @ args = 0, pretend = 0, frame = 0
> > +        @ frame_needed = 1, uses_anonymous_args = 0
> > +        push    {fp, lr}
> > +        add     fp, sp, #4
> > +        mov     r0, #5
> > +        bl      foo
> > +        mov     r3, r0
> > +        mov     r0, r3
> > +        pop     {fp, pc}
> > +        .size   main, .-main
> > +        .ident  "GCC: (6ea25fd023ef3674e34cc67cd97771195d6f93e4)
> > +12.0.0
> > 20210715 (experimental)"
> > +        .section        .note.GNU-stack,"",%progbits
> > +
> > diff --git a/include/coff/arm.h b/include/coff/arm.h index
> >
> 7b618d94edef0b8660544157df04f2a1e79f09a4..a46fac0f83054e27eea34ac774
> > 35ea1f4ad5d1ed 100644
> > --- a/include/coff/arm.h
> > +++ b/include/coff/arm.h
> > @@ -75,7 +75,9 @@
> >
> >    XXX - NC 5/6/97.  */
> >
> > +#ifndef ARMMAGIC
> >  #define	ARMMAGIC	0xa00  /* I just made this up */
> > +#endif
> >
> >  #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC))
> >
> > @@ -87,7 +89,9 @@
> >  #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic !=
> > ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic !=
> > ARMV7PEMAGIC))
> >
> >  #define OMAGIC          0404    /* object files, eg as output */
> > +#ifndef ZMAGIC
> >  #define ZMAGIC          0413    /* demand load format, eg normal ld output
> */
> > +#endif
> >  #define STMAGIC		0401	/* target shlib */
> >  #define SHMAGIC		0443	/* host   shlib */
> >
> >
> >
> > --

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
  2021-10-29 11:05 [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm) Tamar Christina
  2021-11-05 17:39 ` Tamar Christina
@ 2021-11-12 17:17 ` Richard Earnshaw
  2021-11-12 18:08   ` Tamar Christina
  1 sibling, 1 reply; 5+ messages in thread
From: Richard Earnshaw @ 2021-11-12 17:17 UTC (permalink / raw)
  To: Tamar Christina, binutils; +Cc: Richard.Earnshaw, nd, ramana.radhakrishnan



On 29/10/2021 12:05, Tamar Christina via Binutils wrote:
> Hi All,
> 
> This adds support for efi-*-arm by virtue of fixing the magic hashes in
> pei-arm-little to have the right values according to the PE specification[1].
> 
> Because there are likely existing files out there using the wrong format I have
> added a new target pei-arm-little-old so that binutils can continue to recognize
> the old format.  Over time code should converge to the new one.
> 
> Additionally the pei-arm-little target is now also exposed to elf targets such
> as arm-*-elf, arm*-*-linux-* etc such that one can create EFI images on linux
> hosted toolchains as well.
> 
> With these changes the EFI binary is now recognized by third party tools:
> 
>> pecli info main.efi
> 
> Metadata
> ================================================================================
> MD5:            5591110a0160b4a29497171ae44e0707
> SHA1:           4f3bb55ec366e64b9166ed202a49e787f9b78a84
> SHA256:         b18f1b5dd210afe9b73ff0d15be29b4fb4f52db82cae4df0f5cd1877a8e9b48c
> Imphash:
> Size:           1288 bytes
> Type:           PE32 executable (EFI application) ARM (stripped to external PDB), for MS Windows
> Compile Time:   2021-10-29 00:00:00 (UTC - 0x0       )
> Entry point:    0x0 (section None)
> 
> Sections
> ================================================================================
> Name      RWX  VirtSize   VirtAddr   RawAddr   RawSize   Entropy  md5
> .text     RWX  0x44       0x8000     0x200     0x200       1.07 0b48a18cac04f6e96a1ffc44fb0d4902
> 
> 
> Imports
> ================================================================================
> 
> Any magic number is based on the Microsoft PE specification [1].
> 
> [1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
> 
> build on native hardware and regtested on
>    arm-none-elf, arm-none-elf (32 bit host),
>    arm-none-linux-gnueabihf, arm-none-linux-gnueabihf (32 bit host)
> 
> Cross-compiled and regtested on
>    arm-none-linux-gnueabihf, armeb-none-elf, arm-wince-pe, arm-none-pe
> 
> and no issues.
> 
> Ok for master?
> 
> Thanks,
> Tamar
> 
> bfd/ChangeLog:
> 
> 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> 
> 	PR binutils/26218
> 	* pei-arm-old.c: New file, define targets arm_pei_be_vec_old and
> 	arm_pei_le_vec_old.
> 	* Makefile.am: Use them.
> 	* Makefile.in: Likewise.
> 	* config.bfd: Likewise.
> 	* configure: Likewise.
> 	* configure.ac: Likewise.
> 	* targets.c: Likewise.
> 	* pei-arm.c (ARMMAGIC, (ZMAGIC): Define
> 	* coffcode.h (coff_set_arch_mach_hook): Handle case where
> 	ARMMAGIC == ARMPEMAGIC.
> 
> binutils/ChangeLog:
> 
> 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> 
> 	PR binutils/26218
> 	* NEWS: Add new support.
> 	* objcopy.c (convert_efi_target): Add efi-*-arm support
> 	* testsuite/binutils-all/arm/pei-arm-little.d: New test.
> 	* testsuite/binutils-all/arm/pei-arm-little.s: New test.
> 
> include/ChangeLog:
> 
> 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> 
> 	PR binutils/26218
> 	* coff/arm.h: Make ARMMAGIC and ZMAGIC overrideable.

I'm not sure I understand all of this patch; some of it just doesn't 
seem to make sense.

Firstly, you appear to override ZMAGIC in some cicumstances, but with 
the same value, just written in hex rather than octal (0x10b == 0413 == 
267).  I know octal is somewhat antequated these days, but this seems 
somewhat pointless.

Secondly, we already have ARMPEMAGIC defined in include/coff/arm.h with 
the same value as the one you use to override ARMMAGIC.  Wouldn't it be 
better to rename the existing ARMMAGIC definition in coff/arm.h as 
ARMOLDMAGIC and then have ARMMAGIC defined in terms of something that is 
perhaps selectable?

Thirdly, I see that coff/arm.h has some weird stuff going on that really 
needs cleaning up with this change.  We have:

#define ARMBADMAG(x) (((x).f_magic != ARMMAGIC))

And then, a few lines later:

#undef  ARMBADMAG
#define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic != 
ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic != 
ARMV7PEMAGIC))

So we define something and then almost immediately undef it again????

R.

> 
> --- inline copy of patch --
> diff --git a/bfd/Makefile.am b/bfd/Makefile.am
> index 7777f733702c09b441bc591daa5b8c3d9ab2abd7..cc7814639caf54301dfb0380b96b34b37a12e7c7 100644
> --- a/bfd/Makefile.am
> +++ b/bfd/Makefile.am
> @@ -509,6 +509,7 @@ BFD32_BACKENDS_CFILES = \
>   	pef.c \
>   	pei-arm-wince.c \
>   	pei-arm.c \
> +	pei-arm-old.c \
>   	pei-i386.c \
>   	pei-mcore.c \
>   	pei-sh.c \
> diff --git a/bfd/Makefile.in b/bfd/Makefile.in
> index b243151fea350032c6cb90f1ec1f359bdba589f5..730c845d85b2df89d1c7bb854122dd7addc8ac69 100644
> --- a/bfd/Makefile.in
> +++ b/bfd/Makefile.in
> @@ -935,6 +935,7 @@ BFD32_BACKENDS_CFILES = \
>   	pef.c \
>   	pei-arm-wince.c \
>   	pei-arm.c \
> +	pei-arm-old.c \
>   	pei-i386.c \
>   	pei-mcore.c \
>   	pei-sh.c \
> diff --git a/bfd/coffcode.h b/bfd/coffcode.h
> index 86688d31cdb0a4809b7fc01079d16e87573dd56f..1cf02c1b7c1551b83135e3b743a2cfd35e0cb259 100644
> --- a/bfd/coffcode.h
> +++ b/bfd/coffcode.h
> @@ -2196,7 +2196,9 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
>   #endif
>   #ifdef ARMMAGIC
>       case ARMMAGIC:
> +#if ARMMAGIC != ARMPEMAGIC
>       case ARMPEMAGIC:
> +#endif
>       case THUMBPEMAGIC:
>         arch = bfd_arch_arm;
>         machine = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
> diff --git a/bfd/config.bfd b/bfd/config.bfd
> index 776918253ef11140df94042b379789e6f7b1e598..34c20a56b2614eabba79c09bad8fbdddc4dfd74b 100644
> --- a/bfd/config.bfd
> +++ b/bfd/config.bfd
> @@ -362,7 +362,7 @@ case "${targ}" in
>       ;;
>     arm*-*-haiku*)
>       targ_defvec=arm_elf32_le_vec
> -    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec"
> +    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
>       ;;
>     arm-*-nacl*)
>       targ_defvec=arm_elf32_nacl_le_vec
> @@ -393,7 +393,7 @@ case "${targ}" in
>       ;;
>     arm-*-pe*)
>       targ_defvec=arm_pe_le_vec
> -    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec"
> +    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
>       targ_underscore=yes
>       ;;
>     arm-*-phoenix*)
> @@ -412,7 +412,7 @@ case "${targ}" in
>     arm*-*-uclinux* | arm-*-kfreebsd*-gnu | \
>     arm*-*-eabi* | arm-*-rtems* | arm*-*-uclinuxfdpiceabi)
>       targ_defvec=arm_elf32_le_vec
> -    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec"
> +    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec arm_elf32_fdpic_be_vec arm_pei_le_vec"
>       ;;
>     arm*-*-vxworks | arm*-*-windiss)
>       targ_defvec=arm_elf32_vxworks_le_vec
> diff --git a/bfd/configure b/bfd/configure
> index 5741e039a4fed386d826ad4910e1fd029bd37f23..19ea57d1b11e2ada1682eb4456514e1c03fb6da5 100755
> --- a/bfd/configure
> +++ b/bfd/configure
> @@ -13281,6 +13281,8 @@ do
>       arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
>       arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
>       arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
> +    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
> +    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
>       arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
>       arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
>       arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo peigen.lo $coff" ;;
> diff --git a/bfd/configure.ac b/bfd/configure.ac
> index 1fadfd7d91ed7ead6b04be2173d6c596fb4af221..951ce21cec061690a7734da4a4739c1975d19710 100644
> --- a/bfd/configure.ac
> +++ b/bfd/configure.ac
> @@ -460,6 +460,8 @@ do
>       arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
>       arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
>       arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo $coff" ;;
> +    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
> +    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo $coff" ;;
>       arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
>       arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
>       arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo peigen.lo $coff" ;;
> diff --git a/bfd/pei-arm-old.c b/bfd/pei-arm-old.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..a42f7c8a4a86b49e0e520314592dfaa3eee42951
> --- /dev/null
> +++ b/bfd/pei-arm-old.c
> @@ -0,0 +1,55 @@
> +/* BFD back-end for arm PE IMAGE COFF files.
> +   Copyright (C) 1995-2021 Free Software Foundation, Inc.
> +
> +   This file is part of BFD, the Binary File Descriptor library.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program; if not, write to the Free Software
> +   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
> +   MA 02110-1301, USA.  */
> +
> +#include "sysdep.h"
> +#include "bfd.h"
> +
> +#ifndef TARGET_LITTLE_SYM
> +#define TARGET_LITTLE_SYM  arm_pei_le_old_vec
> +#define TARGET_LITTLE_NAME "pei-arm-little-old"
> +#define TARGET_BIG_SYM     arm_pei_be_old_vec
> +#define TARGET_BIG_NAME    "pei-arm-big-old"
> +#endif
> +
> +#define COFF_IMAGE_WITH_PE
> +#define COFF_WITH_PE
> +#define PCRELOFFSET	   true
> +/* Long section names not allowed in executable images, only object files.  */
> +#define COFF_LONG_SECTION_NAMES 0
> +
> +#define COFF_SECTION_ALIGNMENT_ENTRIES \
> +{ COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> +{ COFF_SECTION_NAME_EXACT_MATCH (".data"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> +{ COFF_SECTION_NAME_EXACT_MATCH (".rdata"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> +{ COFF_SECTION_NAME_EXACT_MATCH (".text"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> +{ COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> +{ COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
> +{ COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
> +{ COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
> +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
> +
> +#include "coff-arm.c"
> diff --git a/bfd/pei-arm.c b/bfd/pei-arm.c
> index 216b94509ad43516062118916a9ece66c3553fed..21583c9a2d58021fb34175cf859c030a9dace978 100644
> --- a/bfd/pei-arm.c
> +++ b/bfd/pei-arm.c
> @@ -26,6 +26,9 @@
>   #define TARGET_LITTLE_NAME "pei-arm-little"
>   #define TARGET_BIG_SYM     arm_pei_be_vec
>   #define TARGET_BIG_NAME    "pei-arm-big"
> +#define ARMMAGIC 0x1c0
> +/* Constant below is IMAGE_NT_OPTIONAL_HDR_MAGIC.  */
> +#define ZMAGIC   0x10b    /* Demand load format, eg normal ld output 0x10b.  */
>   #endif
>   
>   #define COFF_IMAGE_WITH_PE
> diff --git a/bfd/targets.c b/bfd/targets.c
> index 672dc2bb1a4d5643f84577d81d4c9cebc14429d9..a1a33ae5461daa8dc945466c26bae2d12fc79294 100644
> --- a/bfd/targets.c
> +++ b/bfd/targets.c
> @@ -702,6 +702,8 @@ extern const bfd_target arm_pe_be_vec;
>   extern const bfd_target arm_pe_le_vec;
>   extern const bfd_target arm_pe_wince_be_vec;
>   extern const bfd_target arm_pe_wince_le_vec;
> +extern const bfd_target arm_pei_be_old_vec;
> +extern const bfd_target arm_pei_le_old_vec;
>   extern const bfd_target arm_pei_be_vec;
>   extern const bfd_target arm_pei_le_vec;
>   extern const bfd_target arm_pei_wince_be_vec;
> @@ -1027,6 +1029,8 @@ static const bfd_target * const _bfd_target_vector[] =
>   	&arm_pe_le_vec,
>   	&arm_pe_wince_be_vec,
>   	&arm_pe_wince_le_vec,
> +	&arm_pei_be_old_vec,
> +	&arm_pei_le_old_vec,
>   	&arm_pei_be_vec,
>   	&arm_pei_le_vec,
>   	&arm_pei_wince_be_vec,
> diff --git a/binutils/NEWS b/binutils/NEWS
> index 994546330f95ccc3b859ffb17388b2cc5ec6a653..9aa45dab2b83edb3e7b3778378e7f66ed454f459 100644
> --- a/binutils/NEWS
> +++ b/binutils/NEWS
> @@ -5,6 +5,14 @@
>   * Support for efi-app-aarch64, efi-rtdrv-aarch64 and efi-bsdrv-aarch64 has been
>     added to objcopy in order to enable UEFI development using binutils.
>   
> +* Support for efi-app-arm, efi-rtdrv-arm and efi-bsdrv-arm has been
> +  added to objcopy in order to enable UEFI development using binutils.
> +
> +* The PE Image format pei-arm-little has been renamed to pei-arm-little-old and
> +  a new pei-arm-little has been added with the MAGIC and ZMAGIC numbers from
> +  the Microsoft PE specification added.  This allows the format to be
> +  recognized by standard Windows tools.
> +
>   Changes in 2.37:
>   
>   * The readelf tool has a new command line option which can be used to specify
> diff --git a/binutils/objcopy.c b/binutils/objcopy.c
> index 458a6d96cccbeb154f95e5ba54734b956d14f879..dbd18444d573048b2a262d36a72245612239efcd 100644
> --- a/binutils/objcopy.c
> +++ b/binutils/objcopy.c
> @@ -5003,6 +5003,13 @@ convert_efi_target (char *efi)
>         char *t = "aarch64-little";
>         strcpy (efi + 4, t);
>       }
> +  else if (strcmp (efi + 4, "arm") == 0)
> +    {
> +      /* Change arm to arm-little.  */
> +      efi = (char *) xrealloc (efi, strlen (efi) + 7);
> +      char *t = "arm-little";
> +      strcpy (efi + 4, t);
> +    }
>   }
>   
>   /* Allocate and return a pointer to a struct section_add, initializing the
> diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.d b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> new file mode 100644
> index 0000000000000000000000000000000000000000..bd251f8e3f2cedc3f4ed530d28d75c8c54942336
> --- /dev/null
> +++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> @@ -0,0 +1,16 @@
> +#skip: armeb-*-* *-*-wince *-*-vxworks *-*-pe
> +#ld: -e0
> +#PROG: objcopy
> +#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-arm
> +#objdump: -h -f
> +#name: Check if efi app format is recognized
> +
> +.*:     file format pei-arm-little
> +architecture: armv3m, flags 0x00000132:
> +EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
> +start address 0x00000000
> +
> +Sections:
> +Idx Name          Size      VMA       LMA       File off  Algn
> +  0 \.text         00000044  00008000  00008000  00000200  2\*\*2
> +                  CONTENTS, ALLOC, LOAD, CODE
> diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.s b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> new file mode 100644
> index 0000000000000000000000000000000000000000..f5e629895a2e72da9756697bec177a042a64472c
> --- /dev/null
> +++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> @@ -0,0 +1,56 @@
> +	.arch armv7-a
> +        .eabi_attribute 28, 1
> +        .eabi_attribute 20, 1
> +        .eabi_attribute 21, 1
> +        .eabi_attribute 23, 3
> +        .eabi_attribute 24, 1
> +        .eabi_attribute 25, 1
> +        .eabi_attribute 26, 2
> +        .eabi_attribute 30, 6
> +        .eabi_attribute 34, 1
> +        .eabi_attribute 18, 4
> +        .file   "hello.c"
> +        .text
> +        .align  2
> +        .global foo
> +        .arch armv7-a
> +        .syntax unified
> +        .arm
> +        .fpu neon
> +        .type   foo, %function
> +foo:
> +        @ args = 0, pretend = 0, frame = 8
> +        @ frame_needed = 1, uses_anonymous_args = 0
> +        @ link register save eliminated.
> +        str     fp, [sp, #-4]!
> +        add     fp, sp, #0
> +        sub     sp, sp, #12
> +        str     r0, [fp, #-8]
> +        ldr     r3, [fp, #-8]
> +        mul     r3, r3, r3
> +        mov     r0, r3
> +        add     sp, fp, #0
> +        @ sp needed
> +        ldr     fp, [sp], #4
> +        bx      lr
> +        .size   foo, .-foo
> +        .align  2
> +        .global main
> +        .syntax unified
> +        .arm
> +        .fpu neon
> +        .type   main, %function
> +main:
> +        @ args = 0, pretend = 0, frame = 0
> +        @ frame_needed = 1, uses_anonymous_args = 0
> +        push    {fp, lr}
> +        add     fp, sp, #4
> +        mov     r0, #5
> +        bl      foo
> +        mov     r3, r0
> +        mov     r0, r3
> +        pop     {fp, pc}
> +        .size   main, .-main
> +        .ident  "GCC: (6ea25fd023ef3674e34cc67cd97771195d6f93e4) 12.0.0 20210715 (experimental)"
> +        .section        .note.GNU-stack,"",%progbits
> +
> diff --git a/include/coff/arm.h b/include/coff/arm.h
> index 7b618d94edef0b8660544157df04f2a1e79f09a4..a46fac0f83054e27eea34ac77435ea1f4ad5d1ed 100644
> --- a/include/coff/arm.h
> +++ b/include/coff/arm.h
> @@ -75,7 +75,9 @@
>    
>     XXX - NC 5/6/97.  */
>   
> +#ifndef ARMMAGIC
>   #define	ARMMAGIC	0xa00  /* I just made this up */
> +#endif
>   
>   #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC))
>   
> @@ -87,7 +89,9 @@
>   #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic != ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic != ARMV7PEMAGIC))
>   
>   #define OMAGIC          0404    /* object files, eg as output */
> +#ifndef ZMAGIC
>   #define ZMAGIC          0413    /* demand load format, eg normal ld output */
> +#endif
>   #define STMAGIC		0401	/* target shlib */
>   #define SHMAGIC		0443	/* host   shlib */
>   
> 
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
  2021-11-12 17:17 ` Richard Earnshaw
@ 2021-11-12 18:08   ` Tamar Christina
  0 siblings, 0 replies; 5+ messages in thread
From: Tamar Christina @ 2021-11-12 18:08 UTC (permalink / raw)
  To: Richard Earnshaw, binutils; +Cc: Richard Earnshaw, nd, Ramana Radhakrishnan



> -----Original Message-----
> From: Richard Earnshaw <Richard.Earnshaw@foss.arm.com>
> Sent: Friday, November 12, 2021 6:17 PM
> To: Tamar Christina <Tamar.Christina@arm.com>; binutils@sourceware.org
> Cc: Richard Earnshaw <Richard.Earnshaw@arm.com>; nd <nd@arm.com>;
> Ramana Radhakrishnan <Ramana.Radhakrishnan@arm.com>
> Subject: Re: [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm).
> 
> 
> 
> On 29/10/2021 12:05, Tamar Christina via Binutils wrote:
> > Hi All,
> >
> > This adds support for efi-*-arm by virtue of fixing the magic hashes
> > in pei-arm-little to have the right values according to the PE specification[1].
> >
> > Because there are likely existing files out there using the wrong
> > format I have added a new target pei-arm-little-old so that binutils
> > can continue to recognize the old format.  Over time code should converge
> to the new one.
> >
> > Additionally the pei-arm-little target is now also exposed to elf
> > targets such as arm-*-elf, arm*-*-linux-* etc such that one can create
> > EFI images on linux hosted toolchains as well.
> >
> > With these changes the EFI binary is now recognized by third party tools:
> >
> >> pecli info main.efi
> >
> > Metadata
> >
> ==========================================================
> ======================
> > MD5:            5591110a0160b4a29497171ae44e0707
> > SHA1:           4f3bb55ec366e64b9166ed202a49e787f9b78a84
> > SHA256:
> b18f1b5dd210afe9b73ff0d15be29b4fb4f52db82cae4df0f5cd1877a8e9b48c
> > Imphash:
> > Size:           1288 bytes
> > Type:           PE32 executable (EFI application) ARM (stripped to external
> PDB), for MS Windows
> > Compile Time:   2021-10-29 00:00:00 (UTC - 0x0       )
> > Entry point:    0x0 (section None)
> >
> > Sections
> >
> ==========================================================
> ======================
> > Name      RWX  VirtSize   VirtAddr   RawAddr   RawSize   Entropy  md5
> > .text     RWX  0x44       0x8000     0x200     0x200       1.07
> 0b48a18cac04f6e96a1ffc44fb0d4902
> >
> >
> > Imports
> >
> ==========================================================
> ============
> > ==========
> >
> > Any magic number is based on the Microsoft PE specification [1].
> >
> > [1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
> >
> > build on native hardware and regtested on
> >    arm-none-elf, arm-none-elf (32 bit host),
> >    arm-none-linux-gnueabihf, arm-none-linux-gnueabihf (32 bit host)
> >
> > Cross-compiled and regtested on
> >    arm-none-linux-gnueabihf, armeb-none-elf, arm-wince-pe, arm-none-pe
> >
> > and no issues.
> >
> > Ok for master?
> >
> > Thanks,
> > Tamar
> >
> > bfd/ChangeLog:
> >
> > 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> >
> > 	PR binutils/26218
> > 	* pei-arm-old.c: New file, define targets arm_pei_be_vec_old and
> > 	arm_pei_le_vec_old.
> > 	* Makefile.am: Use them.
> > 	* Makefile.in: Likewise.
> > 	* config.bfd: Likewise.
> > 	* configure: Likewise.
> > 	* configure.ac: Likewise.
> > 	* targets.c: Likewise.
> > 	* pei-arm.c (ARMMAGIC, (ZMAGIC): Define
> > 	* coffcode.h (coff_set_arch_mach_hook): Handle case where
> > 	ARMMAGIC == ARMPEMAGIC.
> >
> > binutils/ChangeLog:
> >
> > 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> >
> > 	PR binutils/26218
> > 	* NEWS: Add new support.
> > 	* objcopy.c (convert_efi_target): Add efi-*-arm support
> > 	* testsuite/binutils-all/arm/pei-arm-little.d: New test.
> > 	* testsuite/binutils-all/arm/pei-arm-little.s: New test.
> >
> > include/ChangeLog:
> >
> > 2021-10-29  Tamar Christina  <tamar.christina@arm.com>
> >
> > 	PR binutils/26218
> > 	* coff/arm.h: Make ARMMAGIC and ZMAGIC overrideable.
> 
> I'm not sure I understand all of this patch; some of it just doesn't seem to
> make sense.
> 
> Firstly, you appear to override ZMAGIC in some cicumstances, but with the
> same value, just written in hex rather than octal (0x10b == 0413 == 267).  I
> know octal is somewhat antequated these days, but this seems somewhat
> pointless.
> 

I see, I didn't realize octals in C started with 0.

> Secondly, we already have ARMPEMAGIC defined in include/coff/arm.h with
> the same value as the one you use to override ARMMAGIC.  Wouldn't it be
> better to rename the existing ARMMAGIC definition in coff/arm.h as
> ARMOLDMAGIC and then have ARMMAGIC defined in terms of something
> that is perhaps selectable?

That's because the WINCE target had the correct values already but the PE
targets didn't. The WINCE codepath select it when -DARM_WINCE is specified.

Instead of doing another such split I just made the value overridable. I can rename
the old one to ARMOLDMAGIC but I don't think that's cleaner as then I'd have to
add duplicate handling for the constants.

The constants can never be true at the same time, so the value of ARMBADMAG
shouldn't need

#undef  ARMBADMAG
#define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic != ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic != ARMV7PEMAGIC))

And ARMV7PEMAGIC is unused and THUMBPEMAGIC is only valid in PEI not COFF but the same header file is included in both.

So yes there's a lot that can be cleaned up here...
  
> 
> Thirdly, I see that coff/arm.h has some weird stuff going on that really needs
> cleaning up with this change.  We have:
> 
> #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC))
> 
> And then, a few lines later:
> 
> #undef  ARMBADMAG
> #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic !=
> ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic !=
> ARMV7PEMAGIC))
> 
> So we define something and then almost immediately undef it again????

Yes, as explained above it's because the WINCE support was tacked on to the PE
support but at that time they didn't split up the definitions.

In reality only the first define should be needed if one just changes the value of
ARMMAGIC for each target appropriately.

That's a bit of a refactoring though, and felt a bit orthogonal to this patch, where
I just chose to override the value of ARMMAGIC.

Happy to do it though if it's required.

Regards,
Tamar

> 
> R.
> 
> >
> > --- inline copy of patch --
> > diff --git a/bfd/Makefile.am b/bfd/Makefile.am index
> >
> 7777f733702c09b441bc591daa5b8c3d9ab2abd7..cc7814639caf54301dfb0380b9
> 6b
> > 34b37a12e7c7 100644
> > --- a/bfd/Makefile.am
> > +++ b/bfd/Makefile.am
> > @@ -509,6 +509,7 @@ BFD32_BACKENDS_CFILES = \
> >   	pef.c \
> >   	pei-arm-wince.c \
> >   	pei-arm.c \
> > +	pei-arm-old.c \
> >   	pei-i386.c \
> >   	pei-mcore.c \
> >   	pei-sh.c \
> > diff --git a/bfd/Makefile.in b/bfd/Makefile.in index
> >
> b243151fea350032c6cb90f1ec1f359bdba589f5..730c845d85b2df89d1c7bb8541
> 22
> > dd7addc8ac69 100644
> > --- a/bfd/Makefile.in
> > +++ b/bfd/Makefile.in
> > @@ -935,6 +935,7 @@ BFD32_BACKENDS_CFILES = \
> >   	pef.c \
> >   	pei-arm-wince.c \
> >   	pei-arm.c \
> > +	pei-arm-old.c \
> >   	pei-i386.c \
> >   	pei-mcore.c \
> >   	pei-sh.c \
> > diff --git a/bfd/coffcode.h b/bfd/coffcode.h index
> >
> 86688d31cdb0a4809b7fc01079d16e87573dd56f..1cf02c1b7c1551b83135e3b74
> 3a2
> > cfd35e0cb259 100644
> > --- a/bfd/coffcode.h
> > +++ b/bfd/coffcode.h
> > @@ -2196,7 +2196,9 @@ coff_set_arch_mach_hook (bfd *abfd, void *
> filehdr)
> >   #endif
> >   #ifdef ARMMAGIC
> >       case ARMMAGIC:
> > +#if ARMMAGIC != ARMPEMAGIC
> >       case ARMPEMAGIC:
> > +#endif
> >       case THUMBPEMAGIC:
> >         arch = bfd_arch_arm;
> >         machine = bfd_arm_get_mach_from_notes (abfd,
> > ARM_NOTE_SECTION); diff --git a/bfd/config.bfd b/bfd/config.bfd index
> >
> 776918253ef11140df94042b379789e6f7b1e598..34c20a56b2614eabba79c09ba
> d8f
> > bdddc4dfd74b 100644
> > --- a/bfd/config.bfd
> > +++ b/bfd/config.bfd
> > @@ -362,7 +362,7 @@ case "${targ}" in
> >       ;;
> >     arm*-*-haiku*)
> >       targ_defvec=arm_elf32_le_vec
> > -    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> arm_pei_be_vec"
> > +    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> arm_elf32_fdpic_be_vec arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
> >       ;;
> >     arm-*-nacl*)
> >       targ_defvec=arm_elf32_nacl_le_vec @@ -393,7 +393,7 @@ case
> > "${targ}" in
> >       ;;
> >     arm-*-pe*)
> >       targ_defvec=arm_pe_le_vec
> > -    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> arm_pei_be_vec"
> > +    targ_selvecs="arm_pe_le_vec arm_pe_be_vec arm_pei_le_vec
> arm_pei_be_vec arm_pei_le_old_vec arm_pei_be_old_vec"
> >       targ_underscore=yes
> >       ;;
> >     arm-*-phoenix*)
> > @@ -412,7 +412,7 @@ case "${targ}" in
> >     arm*-*-uclinux* | arm-*-kfreebsd*-gnu | \
> >     arm*-*-eabi* | arm-*-rtems* | arm*-*-uclinuxfdpiceabi)
> >       targ_defvec=arm_elf32_le_vec
> > -    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> arm_elf32_fdpic_be_vec"
> > +    targ_selvecs="arm_elf32_fdpic_le_vec arm_elf32_be_vec
> arm_elf32_fdpic_be_vec arm_pei_le_vec"
> >       ;;
> >     arm*-*-vxworks | arm*-*-windiss)
> >       targ_defvec=arm_elf32_vxworks_le_vec
> > diff --git a/bfd/configure b/bfd/configure index
> >
> 5741e039a4fed386d826ad4910e1fd029bd37f23..19ea57d1b11e2ada1682eb44
> 5651
> > 4e1c03fb6da5 100755
> > --- a/bfd/configure
> > +++ b/bfd/configure
> > @@ -13281,6 +13281,8 @@ do
> >       arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
> >       arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> $coff" ;;
> >       arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> $coff" ;;
> > +    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> $coff" ;;
> > +    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> $coff" ;;
> >       arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
> >       arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
> >       arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo
> peigen.lo $coff" ;;
> > diff --git a/bfd/configure.ac b/bfd/configure.ac index
> >
> 1fadfd7d91ed7ead6b04be2173d6c596fb4af221..951ce21cec061690a7734da4a
> 473
> > 9c1975d19710 100644
> > --- a/bfd/configure.ac
> > +++ b/bfd/configure.ac
> > @@ -460,6 +460,8 @@ do
> >       arm_pe_le_vec)		 tb="$tb pe-arm.lo peigen.lo $coff" ;;
> >       arm_pe_wince_be_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> $coff" ;;
> >       arm_pe_wince_le_vec)	 tb="$tb pe-arm-wince.lo pe-arm.lo peigen.lo
> $coff" ;;
> > +    arm_pei_be_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> $coff" ;;
> > +    arm_pei_le_old_vec)		 tb="$tb pei-arm-old.lo peigen.lo
> $coff" ;;
> >       arm_pei_be_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
> >       arm_pei_le_vec)		 tb="$tb pei-arm.lo peigen.lo $coff" ;;
> >       arm_pei_wince_be_vec)	 tb="$tb pei-arm-wince.lo pei-arm.lo
> peigen.lo $coff" ;;
> > diff --git a/bfd/pei-arm-old.c b/bfd/pei-arm-old.c new file mode
> > 100644 index
> >
> 0000000000000000000000000000000000000000..a42f7c8a4a86b49e0e52031459
> 2d
> > faa3eee42951
> > --- /dev/null
> > +++ b/bfd/pei-arm-old.c
> > @@ -0,0 +1,55 @@
> > +/* BFD back-end for arm PE IMAGE COFF files.
> > +   Copyright (C) 1995-2021 Free Software Foundation, Inc.
> > +
> > +   This file is part of BFD, the Binary File Descriptor library.
> > +
> > +   This program is free software; you can redistribute it and/or modify
> > +   it under the terms of the GNU General Public License as published by
> > +   the Free Software Foundation; either version 3 of the License, or
> > +   (at your option) any later version.
> > +
> > +   This program is distributed in the hope that it will be useful,
> > +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +   GNU General Public License for more details.
> > +
> > +   You should have received a copy of the GNU General Public License
> > +   along with this program; if not, write to the Free Software
> > +   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
> > +   MA 02110-1301, USA.  */
> > +
> > +#include "sysdep.h"
> > +#include "bfd.h"
> > +
> > +#ifndef TARGET_LITTLE_SYM
> > +#define TARGET_LITTLE_SYM  arm_pei_le_old_vec #define
> > +TARGET_LITTLE_NAME "pei-arm-little-old"
> > +#define TARGET_BIG_SYM     arm_pei_be_old_vec
> > +#define TARGET_BIG_NAME    "pei-arm-big-old"
> > +#endif
> > +
> > +#define COFF_IMAGE_WITH_PE
> > +#define COFF_WITH_PE
> > +#define PCRELOFFSET	   true
> > +/* Long section names not allowed in executable images, only object
> > +files.  */ #define COFF_LONG_SECTION_NAMES 0
> > +
> > +#define COFF_SECTION_ALIGNMENT_ENTRIES \ {
> > +COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \ {
> > +COFF_SECTION_NAME_EXACT_MATCH (".data"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \ {
> > +COFF_SECTION_NAME_EXACT_MATCH (".rdata"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \ {
> > +COFF_SECTION_NAME_EXACT_MATCH (".text"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \ {
> > +COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \ {
> > +COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
> \ {
> > +COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 },
> \ {
> > +COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
> > +  COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
> > +
> > +#include "coff-arm.c"
> > diff --git a/bfd/pei-arm.c b/bfd/pei-arm.c index
> >
> 216b94509ad43516062118916a9ece66c3553fed..21583c9a2d58021fb34175cf85
> 9c
> > 030a9dace978 100644
> > --- a/bfd/pei-arm.c
> > +++ b/bfd/pei-arm.c
> > @@ -26,6 +26,9 @@
> >   #define TARGET_LITTLE_NAME "pei-arm-little"
> >   #define TARGET_BIG_SYM     arm_pei_be_vec
> >   #define TARGET_BIG_NAME    "pei-arm-big"
> > +#define ARMMAGIC 0x1c0
> > +/* Constant below is IMAGE_NT_OPTIONAL_HDR_MAGIC.  */
> > +#define ZMAGIC   0x10b    /* Demand load format, eg normal ld output
> 0x10b.  */
> >   #endif
> >
> >   #define COFF_IMAGE_WITH_PE
> > diff --git a/bfd/targets.c b/bfd/targets.c index
> >
> 672dc2bb1a4d5643f84577d81d4c9cebc14429d9..a1a33ae5461daa8dc945466c2
> 6ba
> > e2d12fc79294 100644
> > --- a/bfd/targets.c
> > +++ b/bfd/targets.c
> > @@ -702,6 +702,8 @@ extern const bfd_target arm_pe_be_vec;
> >   extern const bfd_target arm_pe_le_vec;
> >   extern const bfd_target arm_pe_wince_be_vec;
> >   extern const bfd_target arm_pe_wince_le_vec;
> > +extern const bfd_target arm_pei_be_old_vec; extern const bfd_target
> > +arm_pei_le_old_vec;
> >   extern const bfd_target arm_pei_be_vec;
> >   extern const bfd_target arm_pei_le_vec;
> >   extern const bfd_target arm_pei_wince_be_vec; @@ -1027,6 +1029,8
> @@
> > static const bfd_target * const _bfd_target_vector[] =
> >   	&arm_pe_le_vec,
> >   	&arm_pe_wince_be_vec,
> >   	&arm_pe_wince_le_vec,
> > +	&arm_pei_be_old_vec,
> > +	&arm_pei_le_old_vec,
> >   	&arm_pei_be_vec,
> >   	&arm_pei_le_vec,
> >   	&arm_pei_wince_be_vec,
> > diff --git a/binutils/NEWS b/binutils/NEWS index
> >
> 994546330f95ccc3b859ffb17388b2cc5ec6a653..9aa45dab2b83edb3e7b3778378
> e7
> > f66ed454f459 100644
> > --- a/binutils/NEWS
> > +++ b/binutils/NEWS
> > @@ -5,6 +5,14 @@
> >   * Support for efi-app-aarch64, efi-rtdrv-aarch64 and efi-bsdrv-aarch64 has
> been
> >     added to objcopy in order to enable UEFI development using binutils.
> >
> > +* Support for efi-app-arm, efi-rtdrv-arm and efi-bsdrv-arm has been
> > +  added to objcopy in order to enable UEFI development using binutils.
> > +
> > +* The PE Image format pei-arm-little has been renamed to
> > +pei-arm-little-old and
> > +  a new pei-arm-little has been added with the MAGIC and ZMAGIC
> > +numbers from
> > +  the Microsoft PE specification added.  This allows the format to be
> > +  recognized by standard Windows tools.
> > +
> >   Changes in 2.37:
> >
> >   * The readelf tool has a new command line option which can be used
> > to specify diff --git a/binutils/objcopy.c b/binutils/objcopy.c index
> >
> 458a6d96cccbeb154f95e5ba54734b956d14f879..dbd18444d573048b2a262d36a
> 722
> > 45612239efcd 100644
> > --- a/binutils/objcopy.c
> > +++ b/binutils/objcopy.c
> > @@ -5003,6 +5003,13 @@ convert_efi_target (char *efi)
> >         char *t = "aarch64-little";
> >         strcpy (efi + 4, t);
> >       }
> > +  else if (strcmp (efi + 4, "arm") == 0)
> > +    {
> > +      /* Change arm to arm-little.  */
> > +      efi = (char *) xrealloc (efi, strlen (efi) + 7);
> > +      char *t = "arm-little";
> > +      strcpy (efi + 4, t);
> > +    }
> >   }
> >
> >   /* Allocate and return a pointer to a struct section_add,
> > initializing the diff --git
> > a/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> > b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> > new file mode 100644
> > index
> >
> 0000000000000000000000000000000000000000..bd251f8e3f2cedc3f4ed530d28
> d7
> > 5c8c54942336
> > --- /dev/null
> > +++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.d
> > @@ -0,0 +1,16 @@
> > +#skip: armeb-*-* *-*-wince *-*-vxworks *-*-pe
> > +#ld: -e0
> > +#PROG: objcopy
> > +#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-arm
> > +#objdump: -h -f
> > +#name: Check if efi app format is recognized
> > +
> > +.*:     file format pei-arm-little
> > +architecture: armv3m, flags 0x00000132:
> > +EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED start address 0x00000000
> > +
> > +Sections:
> > +Idx Name          Size      VMA       LMA       File off  Algn
> > +  0 \.text         00000044  00008000  00008000  00000200  2\*\*2
> > +                  CONTENTS, ALLOC, LOAD, CODE
> > diff --git a/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> > b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> > new file mode 100644
> > index
> >
> 0000000000000000000000000000000000000000..f5e629895a2e72da9756697be
> c17
> > 7a042a64472c
> > --- /dev/null
> > +++ b/binutils/testsuite/binutils-all/arm/pei-arm-little.s
> > @@ -0,0 +1,56 @@
> > +	.arch armv7-a
> > +        .eabi_attribute 28, 1
> > +        .eabi_attribute 20, 1
> > +        .eabi_attribute 21, 1
> > +        .eabi_attribute 23, 3
> > +        .eabi_attribute 24, 1
> > +        .eabi_attribute 25, 1
> > +        .eabi_attribute 26, 2
> > +        .eabi_attribute 30, 6
> > +        .eabi_attribute 34, 1
> > +        .eabi_attribute 18, 4
> > +        .file   "hello.c"
> > +        .text
> > +        .align  2
> > +        .global foo
> > +        .arch armv7-a
> > +        .syntax unified
> > +        .arm
> > +        .fpu neon
> > +        .type   foo, %function
> > +foo:
> > +        @ args = 0, pretend = 0, frame = 8
> > +        @ frame_needed = 1, uses_anonymous_args = 0
> > +        @ link register save eliminated.
> > +        str     fp, [sp, #-4]!
> > +        add     fp, sp, #0
> > +        sub     sp, sp, #12
> > +        str     r0, [fp, #-8]
> > +        ldr     r3, [fp, #-8]
> > +        mul     r3, r3, r3
> > +        mov     r0, r3
> > +        add     sp, fp, #0
> > +        @ sp needed
> > +        ldr     fp, [sp], #4
> > +        bx      lr
> > +        .size   foo, .-foo
> > +        .align  2
> > +        .global main
> > +        .syntax unified
> > +        .arm
> > +        .fpu neon
> > +        .type   main, %function
> > +main:
> > +        @ args = 0, pretend = 0, frame = 0
> > +        @ frame_needed = 1, uses_anonymous_args = 0
> > +        push    {fp, lr}
> > +        add     fp, sp, #4
> > +        mov     r0, #5
> > +        bl      foo
> > +        mov     r3, r0
> > +        mov     r0, r3
> > +        pop     {fp, pc}
> > +        .size   main, .-main
> > +        .ident  "GCC: (6ea25fd023ef3674e34cc67cd97771195d6f93e4) 12.0.0
> 20210715 (experimental)"
> > +        .section        .note.GNU-stack,"",%progbits
> > +
> > diff --git a/include/coff/arm.h b/include/coff/arm.h index
> >
> 7b618d94edef0b8660544157df04f2a1e79f09a4..a46fac0f83054e27eea34ac774
> 35
> > ea1f4ad5d1ed 100644
> > --- a/include/coff/arm.h
> > +++ b/include/coff/arm.h
> > @@ -75,7 +75,9 @@
> >
> >     XXX - NC 5/6/97.  */
> >
> > +#ifndef ARMMAGIC
> >   #define	ARMMAGIC	0xa00  /* I just made this up */
> > +#endif
> >
> >   #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC))
> >
> > @@ -87,7 +89,9 @@
> >   #define ARMBADMAG(x) (((x).f_magic != ARMMAGIC) && ((x).f_magic !=
> > ARMPEMAGIC) && ((x).f_magic != THUMBPEMAGIC) && ((x).f_magic !=
> > ARMV7PEMAGIC))
> >
> >   #define OMAGIC          0404    /* object files, eg as output */
> > +#ifndef ZMAGIC
> >   #define ZMAGIC          0413    /* demand load format, eg normal ld output
> */
> > +#endif
> >   #define STMAGIC		0401	/* target shlib */
> >   #define SHMAGIC		0443	/* host   shlib */
> >
> >
> >

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-11-12 18:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-29 11:05 [PATCH][Binutils]Arm Add support for Arm EFI (efi-*-arm) Tamar Christina
2021-11-05 17:39 ` Tamar Christina
2021-11-12  6:11   ` Tamar Christina
2021-11-12 17:17 ` Richard Earnshaw
2021-11-12 18:08   ` Tamar Christina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).