git: 21d56b796634 - main - loader.efi: Fix some arm64 PE metadata

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 18 Apr 2023 18:49:16 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=21d56b796634bda628b149c717461bf88c23677d

commit 21d56b796634bda628b149c717461bf88c23677d
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-04-18 18:32:04 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-04-18 18:36:24 +0000

    loader.efi: Fix some arm64 PE metadata
    
    - Mark the file as an executable in the COFF header.
    - Provide separate .text and .data sections.
    - Provide sane file and section alignment values.  These values are the
      defaults defined in the PE specification.
    - Set appropriate characteristics for each of .text and .data.
    
    This is required for the MS devkit to load our UEFI image.
    
    Obtained from:  OpenBSD via allanjude
    MFC after:      1 month
    Differential Revision:  https://reviews.freebsd.org/D37765
---
 stand/efi/loader/arch/arm64/ldscript.arm64 |  5 +++-
 stand/efi/loader/arch/arm64/start.S        | 46 ++++++++++++++++--------------
 2 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/stand/efi/loader/arch/arm64/ldscript.arm64 b/stand/efi/loader/arch/arm64/ldscript.arm64
index d0ed320a319c..c10448f4180e 100644
--- a/stand/efi/loader/arch/arm64/ldscript.arm64
+++ b/stand/efi/loader/arch/arm64/ldscript.arm64
@@ -16,7 +16,9 @@ SECTIONS
     *(.gnu.warning)
     *(.plt)
   } =0xD4200000
-  . = ALIGN(16);
+  . = ALIGN(4096);
+  _etext = .;
+  __data_start = .;
   .data		: {
     *(.rodata .rodata.* .gnu.linkonce.r.*)
     *(.rodata1)
@@ -78,6 +80,7 @@ SECTIONS
   . = ALIGN(16);
   .dynsym	: { *(.dynsym) }
   _edata = .;
+  __data_size = . - __data_start;
 
   /* Unused sections */
   .interp	: { *(.interp) }
diff --git a/stand/efi/loader/arch/arm64/start.S b/stand/efi/loader/arch/arm64/start.S
index 675d4e153f36..0b8537fed30e 100644
--- a/stand/efi/loader/arch/arm64/start.S
+++ b/stand/efi/loader/arch/arm64/start.S
@@ -34,11 +34,14 @@
 
 #define	IMAGE_FILE_MACHINE_ARM64	0xaa64
 
+#define	IMAGE_FILE_EXECUTABLE		0x0002
+
 #define	IMAGE_SCN_CNT_CODE		0x00000020
 #define	IMAGE_SCN_CNT_INITIALIZED_DATA	0x00000040
 #define	IMAGE_SCN_MEM_DISCARDABLE	0x02000000
 #define	IMAGE_SCN_MEM_EXECUTE		0x20000000
 #define	IMAGE_SCN_MEM_READ		0x40000000
+#define	IMAGE_SCN_MEM_WRITE		0x80000000
 
 	.section .peheader,"a"
 efi_start:
@@ -60,22 +63,22 @@ coff_head:
 	.long	0				/* No symbol table */
 	.long	0				/* No symbols */
 	.short	section_table - optional_header	/* Optional header size */
-	.short	0	/* Characteristics TODO: Fill in */
+	.short	IMAGE_FILE_EXECUTABLE		/* Characteristics */
 
 optional_header:
 	.short	0x020b				/* PE32+ (64-bit addressing) */
 	.byte	0				/* Major linker version */
 	.byte	0				/* Minor linker version */
-	.long	_edata - _end_header		/* Code size */
-	.long	0				/* No initialized data */
+	.long	_etext - _end_header		/* Code size */
+	.long	__data_size			/* Initialized data size */
 	.long	0				/* No uninitialized data */
 	.long	_start - efi_start		/* Entry point */
 	.long	_end_header - efi_start		/* Start of code */
 
 optional_windows_header:
 	.quad	0				/* Image base */
-	.long	32				/* Section Alignment */
-	.long	8				/* File alignment */
+	.long	4096				/* Section Alignment */
+	.long	512				/* File alignment */
 	.short	0				/* Major OS version */
 	.short	0				/* Minor OS version */
 	.short	0				/* Major image version */
@@ -104,36 +107,37 @@ optional_windows_header:
 	.quad	0
 
 section_table:
-	/* We need a .reloc section for EFI */
-	.ascii	".reloc"
+	.ascii	".text"
+	.byte	0
 	.byte	0
 	.byte	0				/* Pad to 8 bytes */
-	.long	0				/* Virtual size */
-	.long	0				/* Virtual address */
-	.long	0				/* Size of raw data */
-	.long	0				/* Pointer to raw data */
+	.long	_etext - _end_header		/* Virtual size */
+	.long	_end_header - efi_start		/* Virtual address */
+	.long	_etext - _end_header		/* Size of raw data */
+	.long	_end_header - efi_start		/* Pointer to raw data */
 	.long	0				/* Pointer to relocations */
 	.long	0				/* Pointer to line numbers */
 	.short	0				/* Number of relocations */
 	.short	0				/* Number of line numbers */
-	.long	(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | \
-		 IMAGE_SCN_MEM_DISCARDABLE)	/* Characteristics */
+	.long	(IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | \
+		 IMAGE_SCN_MEM_READ)		/* Characteristics */
 
-	/* The contents of the loader */
-	.ascii	".text"
+	.ascii	".data"
 	.byte	0
 	.byte	0
 	.byte	0				/* Pad to 8 bytes */
-	.long	_edata - _end_header		/* Virtual size */
-	.long	_end_header - efi_start		/* Virtual address */
-	.long	_edata - _end_header		/* Size of raw data */
-	.long	_end_header - efi_start		/* Pointer to raw data */
+	.long	__data_size			/* Virtual size */
+	.long	__data_start - efi_start	/* Virtual address */
+	.long	__data_size			/* Size of raw data */
+	.long	__data_start - efi_start	/* Pointer to raw data */
 	.long	0				/* Pointer to relocations */
 	.long	0				/* Pointer to line numbers */
 	.short	0				/* Number of relocations */
 	.short	0				/* Number of line numbers */
-	.long	(IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | \
-		 IMAGE_SCN_MEM_READ)		/* Characteristics */
+	.long	(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | \
+		 IMAGE_SCN_MEM_WRITE)		/* Characteristics */
+
+	.align	12
 _end_header:
 
 	.text