Skip to contentSkip to conversation

riscv: Fix efi header for RV32

PATCH New Atish Patra 6 participants 7 messages Oct 13, 2020 – Nov 12, 2020 1 message not in the archive RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: Fix efi header for RV32

RV32 should use PE32 format instead of PE32+ as the efi header format. This requires following changes
1. A different header magic value
2. An additional parameter known as BaseOfData. Currently, it is set to zero in absence of any usage.

---
 arch/riscv/lib/crt0_riscv_efi.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/riscv/lib/crt0_riscv_efi.S +6 -1
index 87fe1e56f906..4aaa49ad0777 100644
@@ -15,11 +15,13 @@
 #define SAVE_LONG(reg, idx)	sd	reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)	ld	reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE	IMAGE_FILE_MACHINE_RISCV64
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR64_MAGIC
 #else
 #define SIZE_LONG	4
 #define SAVE_LONG(reg, idx)	sw	reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)	lw	reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE	IMAGE_FILE_MACHINE_RISCV32
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR32_MAGIC
 #endif
 
 
@@ -48,7 +50,7 @@ coff_header:
 		 IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
 		 IMAGE_FILE_DEBUG_STRIPPED)
 optional_header:
-	.short	IMAGE_NT_OPTIONAL_HDR64_MAGIC	/* PE32+ format */
+	.short	PE_MAGIC			/* PE32+ format */
 	.byte	0x02				/* MajorLinkerVersion */
 	.byte	0x14				/* MinorLinkerVersion */
 	.long	_edata - _start			/* SizeOfCode */
@@ -56,6 +58,9 @@ optional_header:
 	.long	0				/* SizeOfUninitializedData */
 	.long	_start - ImageBase		/* AddressOfEntryPoint */
 	.long	_start - ImageBase		/* BaseOfCode */
+#if __riscv_xlen == 32
+	.long	0				/* BaseOfData */
+#endif
 
 extra_header_fields:
 	.quad	0				/* ImageBase */
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 riscv: Fix efi header for RV32

Quoted 17 lines · Atish Patra

RV32 should use PE32 format instead of PE32+ as the efi header format. This requires following changes
1. A different header magic value
2. An additional parameter known as BaseOfData. Currently, it is set to zero in absence of any usage.

---
 arch/riscv/lib/crt0_riscv_efi.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/riscv/lib/crt0_riscv_efi.S +0 -0
index 87fe1e56f906..4aaa49ad0777 100644

arch/riscv/lib/crt0_riscv_efi.S line 18

Quoted 4 lines
arch/riscv/lib/crt0_riscv_efi.S +1 -0
@@ -15,11 +15,13 @@
 #define SAVE_LONG(reg, idx)    sd      reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)    ld      reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE     IMAGE_FILE_MACHINE_RISCV64
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR64_MAGIC

nits: the indentation seems to be incorrect

Quoted 10 lines
arch/riscv/lib/crt0_riscv_efi.S +1 -0
 #else
 #define SIZE_LONG      4
 #define SAVE_LONG(reg, idx)    sw      reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)    lw      reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE     IMAGE_FILE_MACHINE_RISCV32
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR32_MAGIC
 #endif
 
 

arch/riscv/lib/crt0_riscv_efi.S coff_header: · line 51

Quoted 4 lines
arch/riscv/lib/crt0_riscv_efi.S +0 -1
@@ -48,7 +50,7 @@ coff_header:
                 IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
                 IMAGE_FILE_DEBUG_STRIPPED)
 optional_header:
-       .short  IMAGE_NT_OPTIONAL_HDR64_MAGIC   /* PE32+ format */

nits: PE32(+) ?

Quoted 5 lines
arch/riscv/lib/crt0_riscv_efi.S +1 -0
+       .short  PE_MAGIC                        /* PE32+ format */
        .byte   0x02                            /* MajorLinkerVersion */
        .byte   0x14                            /* MinorLinkerVersion */
        .long   _edata - _start                 /* SizeOfCode */

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 66

Quoted 9 lines
arch/riscv/lib/crt0_riscv_efi.S +3 -0
@@ -56,6 +58,9 @@ optional_header:
        .long   0                               /* SizeOfUninitializedData */
        .long   _start - ImageBase              /* AddressOfEntryPoint */
        .long   _start - ImageBase              /* BaseOfCode */
+#if __riscv_xlen == 32
+       .long   0                               /* BaseOfData */
+#endif
 
 extra_header_fields:
        .quad   0                               /* ImageBase */

Looks good otherwise

#2 lore raw

PATCH 1/1 riscv: Fix efi header for RV32

Am 14. Oktober 2020 03:03:28 MESZ schrieb Bin Meng <[email protected]>:

Quoted 55 lines
Quoted 2 lines · Atish Patra

RV32 should use PE32 format instead of PE32+ as the efi header

format.

Quoted 3 lines

This requires following changes
1. A different header magic value
2. An additional parameter known as BaseOfData. Currently, it is set

to

Review

Quoted 8 lines

zero in absence of any usage.

---
 arch/riscv/lib/crt0_riscv_efi.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/arch/riscv/lib/crt0_riscv_efi.S

arch/riscv/lib/crt0_riscv_efi.S line 18

Quoted 8 lines

index 87fe1e56f906..4aaa49ad0777 100644

arch/riscv/lib/crt0_riscv_efi.S +1 -0
@@ -15,11 +15,13 @@
 #define SAVE_LONG(reg, idx)    sd      reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)    ld      reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE     IMAGE_FILE_MACHINE_RISCV64
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR64_MAGIC

nits: the indentation seems to be incorrect

Quoted 10 lines
arch/riscv/lib/crt0_riscv_efi.S +1 -0
 #else
 #define SIZE_LONG      4
 #define SAVE_LONG(reg, idx)    sw      reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)    lw      reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE     IMAGE_FILE_MACHINE_RISCV32
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR32_MAGIC
 #endif
 
 

arch/riscv/lib/crt0_riscv_efi.S coff_header: · line 51

Quoted 4 lines
arch/riscv/lib/crt0_riscv_efi.S +0 -1
@@ -48,7 +50,7 @@ coff_header:
                 IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
                 IMAGE_FILE_DEBUG_STRIPPED)
 optional_header:
-       .short  IMAGE_NT_OPTIONAL_HDR64_MAGIC   /* PE32+ format */

nits: PE32(+) ?

arch/riscv/lib/crt0_riscv_efi.S coff_header: · line 54

Quoted 2 lines
arch/riscv/lib/crt0_riscv_efi.S +1 -0
+       .short  PE_MAGIC                        /* PE32+ format */
        .byte   0x02                            /* MajorLinkerVersion

*/

arch/riscv/lib/crt0_riscv_efi.S coff_header: · line 55

Quoted 1 line
arch/riscv/lib/crt0_riscv_efi.S +0 -0
        .byte   0x14                            /* MinorLinkerVersion

*/

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 58

Quoted 3 lines
arch/riscv/lib/crt0_riscv_efi.S +0 -0
        .long   _edata - _start                 /* SizeOfCode */
@@ -56,6 +58,9 @@ optional_header:
        .long   0                               /*

SizeOfUninitializedData */

Quoted 7 lines

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 59

Quoted 1 line
arch/riscv/lib/crt0_riscv_efi.S +0 -0
        .long   _start - ImageBase              /*

AddressOfEntryPoint */

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 64

Quoted 5 lines
arch/riscv/lib/crt0_riscv_efi.S +3 -0
        .long   _start - ImageBase              /* BaseOfCode */
+#if __riscv_xlen == 32
+       .long   0                               /* BaseOfData */
+#endif
 

The PE32 header has to be 96 bytes long, the PE32+ header 112 bytes long.

See https://docs.microsoft.com/en-us/windows/win32/debug/pe-format

Best regards

Heinrich

Quoted 5 lines

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 66

Quoted 2 lines
arch/riscv/lib/crt0_riscv_efi.S +0 -0
 extra_header_fields:
        .quad   0                               /* ImageBase */

Looks good otherwise

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

PATCH 1/1 riscv: Fix efi header for RV32

Hi Atish

Quoted 6 lines

From: U-Boot [mailto:u-boot-bounces at lists.denx.de] On Behalf Of Heinrich Schuchardt
Sent: Wednesday, October 14, 2020 9:16 AM
To: Bin Meng; Atish Patra
Cc: U-Boot Mailing List; Anup Patel; Jagan Teki; Marek Vasut; Simon Goldschmidt; David Abdurachmanov; Tom Rini
Subject: Re: [PATCH] riscv: Fix efi header for RV32

Quoted 17 lines

Am 14. Oktober 2020 03:03:28 MESZ schrieb Bin Meng <[email protected]>:

Quoted 16 lines
Quoted 2 lines · Atish Patra

RV32 should use PE32 format instead of PE32+ as the efi header

format.

Quoted 3 lines

This requires following changes
1. A different header magic value
2. An additional parameter known as BaseOfData. Currently, it is set

to

Quoted 7 lines

zero in absence of any usage.

---
 arch/riscv/lib/crt0_riscv_efi.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 49 lines
Quoted 39 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/arch/riscv/lib/crt0_riscv_efi.S

arch/riscv/lib/crt0_riscv_efi.S line 18

Quoted 8 lines

index 87fe1e56f906..4aaa49ad0777 100644

arch/riscv/lib/crt0_riscv_efi.S +1 -0
@@ -15,11 +15,13 @@
 #define SAVE_LONG(reg, idx)    sd      reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)    ld      reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE     IMAGE_FILE_MACHINE_RISCV64
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR64_MAGIC

nits: the indentation seems to be incorrect

Quoted 10 lines
arch/riscv/lib/crt0_riscv_efi.S +1 -0
 #else
 #define SIZE_LONG      4
 #define SAVE_LONG(reg, idx)    sw      reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)    lw      reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE     IMAGE_FILE_MACHINE_RISCV32
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR32_MAGIC
 #endif
 
 

arch/riscv/lib/crt0_riscv_efi.S coff_header: · line 51

Quoted 4 lines
arch/riscv/lib/crt0_riscv_efi.S +0 -1
@@ -48,7 +50,7 @@ coff_header:
                 IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
                 IMAGE_FILE_DEBUG_STRIPPED)
 optional_header:
-       .short  IMAGE_NT_OPTIONAL_HDR64_MAGIC   /* PE32+ format */

nits: PE32(+) ?

arch/riscv/lib/crt0_riscv_efi.S coff_header: · line 54

Quoted 2 lines
arch/riscv/lib/crt0_riscv_efi.S +1 -0
+       .short  PE_MAGIC                        /* PE32+ format */
        .byte   0x02                            /* MajorLinkerVersion

*/

arch/riscv/lib/crt0_riscv_efi.S coff_header: · line 55

Quoted 1 line
arch/riscv/lib/crt0_riscv_efi.S +0 -0
        .byte   0x14                            /* MinorLinkerVersion

*/

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 58

Quoted 3 lines
arch/riscv/lib/crt0_riscv_efi.S +0 -0
        .long   _edata - _start                 /* SizeOfCode */
@@ -56,6 +58,9 @@ optional_header:
        .long   0                               /*

SizeOfUninitializedData */

Quoted 7 lines

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 59

Quoted 1 line
arch/riscv/lib/crt0_riscv_efi.S +0 -0
        .long   _start - ImageBase              /*

AddressOfEntryPoint */

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 64

Quoted 5 lines
arch/riscv/lib/crt0_riscv_efi.S +3 -0
        .long   _start - ImageBase              /* BaseOfCode */
+#if __riscv_xlen == 32
+       .long   0                               /* BaseOfData */
+#endif
 

The PE32 header has to be 96 bytes long, the PE32+ header 112 bytes long.

Quoted 2 lines

See https://docs.microsoft.com/en-us/windows/win32/debug/pe-format

Do you have any comments ?

The other headers seem for Windows-Specific Fields. If no other comments furthermore.
I will pull it into riscv tree later.

Thanks,
Rick

Quoted 10 lines

Best regards

Heinrich

Quoted 5 lines

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 66

Quoted 2 lines
arch/riscv/lib/crt0_riscv_efi.S +0 -0
 extra_header_fields:
        .quad   0                               /* ImageBase */

Looks good otherwise

#5 lore raw

PATCH 1/1 riscv: Fix efi header for RV32

In reply to Atish Patra

Hi Atish and Heinrich,

Quoted 41 lines · Atish Patra

RV32 should use PE32 format instead of PE32+ as the efi header format. This requires following changes
1. A different header magic value
2. An additional parameter known as BaseOfData. Currently, it is set to zero in absence of any usage.

---
 arch/riscv/lib/crt0_riscv_efi.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/riscv/lib/crt0_riscv_efi.S +3 -1
index 87fe1e56f906..4aaa49ad0777 100644
@@ -15,11 +15,13 @@
 #define SAVE_LONG(reg, idx)	sd	reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)	ld	reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE	IMAGE_FILE_MACHINE_RISCV64
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR64_MAGIC
 #else
 #define SIZE_LONG	4
 #define SAVE_LONG(reg, idx)	sw	reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)	lw	reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE	IMAGE_FILE_MACHINE_RISCV32
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR32_MAGIC
 #endif
 
 
@@ -48,7 +50,7 @@ coff_header:
 		 IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
 		 IMAGE_FILE_DEBUG_STRIPPED)
 optional_header:
-	.short	IMAGE_NT_OPTIONAL_HDR64_MAGIC	/* PE32+ format */
+	.short	PE_MAGIC			/* PE32+ format */
 	.byte	0x02				/* MajorLinkerVersion */
 	.byte	0x14				/* MinorLinkerVersion */
 	.long	_edata - _start			/* SizeOfCode */

arch/riscv/lib/crt0_riscv_efi.S optional_header: · line 66

Quoted 9 lines
arch/riscv/lib/crt0_riscv_efi.S +3 -0
@@ -56,6 +58,9 @@ optional_header:
 	.long	0				/* SizeOfUninitializedData */
 	.long	_start - ImageBase		/* AddressOfEntryPoint */
 	.long	_start - ImageBase		/* BaseOfCode */
+#if __riscv_xlen == 32
+	.long	0				/* BaseOfData */
+#endif
 
 extra_header_fields:
 	.quad	0				/* ImageBase */
arch/riscv/lib/crt0_riscv_efi.S +11 -0
index 4aaa49ad07..f47c4a6d82 100644
@@ -63,7 +63,11 @@ optional_header:
 #endif
 
 extra_header_fields:
+#if __riscv_xlen == 32
+       .long   0                               /* ImageBase */
+#else if
        .quad   0                               /* ImageBase */
+#endif
        .long   0x20                            /* SectionAlignment */
        .long   0x8                             /* FileAlignment */
        .short  0                               /* MajorOperatingSystemVersion */
@@ -83,10 +87,17 @@ extra_header_fields:
        .long   0                               /* CheckSum */
        .short  IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
        .short  0                               /* DllCharacteristics */
+#if __riscv_xlen == 32
+       .long   0                               /* SizeOfStackReserve */
+       .long   0                               /* SizeOfStackCommit */
+       .long   0                               /* SizeOfHeapReserve */
+       .long   0                               /* SizeOfHeapCommit */
+#else if
        .quad   0                               /* SizeOfStackReserve */
        .quad   0                               /* SizeOfStackCommit */
        .quad   0                               /* SizeOfHeapReserve */
        .quad   0                               /* SizeOfHeapCommit */
+#endif
        .long   0                               /* LoaderFlags */
        .long   0x6                             /* NumberOfRvaAndSizes */
 

Based on Atish's patch,
would the above modification fit more closely to the specification that Heinrich provided ?

Best regards,
Leo

#6 lore raw

PATCH 1/1 riscv: Fix efi header for RV32

Quoted 91 lines · Leo Liang

Hi Atish and Heinrich,

Quoted 50 lines · Atish Patra

RV32 should use PE32 format instead of PE32+ as the efi header format. This requires following changes
1. A different header magic value
2. An additional parameter known as BaseOfData. Currently, it is set to zero in absence of any usage.

---
 arch/riscv/lib/crt0_riscv_efi.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/riscv/lib/crt0_riscv_efi.S +6 -1
index 87fe1e56f906..4aaa49ad0777 100644
@@ -15,11 +15,13 @@
 #define SAVE_LONG(reg, idx)	sd	reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)	ld	reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE	IMAGE_FILE_MACHINE_RISCV64
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR64_MAGIC
 #else
 #define SIZE_LONG	4
 #define SAVE_LONG(reg, idx)	sw	reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)	lw	reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE	IMAGE_FILE_MACHINE_RISCV32
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR32_MAGIC
 #endif
 
 
@@ -48,7 +50,7 @@ coff_header:
 		 IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
 		 IMAGE_FILE_DEBUG_STRIPPED)
 optional_header:
-	.short	IMAGE_NT_OPTIONAL_HDR64_MAGIC	/* PE32+ format */
+	.short	PE_MAGIC			/* PE32+ format */
 	.byte	0x02				/* MajorLinkerVersion */
 	.byte	0x14				/* MinorLinkerVersion */
 	.long	_edata - _start			/* SizeOfCode */
@@ -56,6 +58,9 @@ optional_header:
 	.long	0				/* SizeOfUninitializedData */
 	.long	_start - ImageBase		/* AddressOfEntryPoint */
 	.long	_start - ImageBase		/* BaseOfCode */
+#if __riscv_xlen == 32
+	.long	0				/* BaseOfData */
+#endif
 
 extra_header_fields:
 	.quad	0				/* ImageBase */
arch/riscv/lib/crt0_riscv_efi.S +11 -0
index 4aaa49ad07..f47c4a6d82 100644
@@ -63,7 +63,11 @@ optional_header:
 #endif
 
 extra_header_fields:
+#if __riscv_xlen == 32
+       .long   0                               /* ImageBase */
+#else if
        .quad   0                               /* ImageBase */
+#endif
        .long   0x20                            /* SectionAlignment */
        .long   0x8                             /* FileAlignment */
        .short  0                               /* MajorOperatingSystemVersion */
@@ -83,10 +87,17 @@ extra_header_fields:
        .long   0                               /* CheckSum */
        .short  IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
        .short  0                               /* DllCharacteristics */
+#if __riscv_xlen == 32
+       .long   0                               /* SizeOfStackReserve */
+       .long   0                               /* SizeOfStackCommit */
+       .long   0                               /* SizeOfHeapReserve */
+       .long   0                               /* SizeOfHeapCommit */
+#else if
        .quad   0                               /* SizeOfStackReserve */
        .quad   0                               /* SizeOfStackCommit */
        .quad   0                               /* SizeOfHeapReserve */
        .quad   0                               /* SizeOfHeapCommit */
+#endif
        .long   0                               /* LoaderFlags */
        .long   0x6                             /* NumberOfRvaAndSizes */
 

Based on Atish's patch,
would the above modification fit more closely to the specification that Heinrich provided ?

Quoted 2 lines

Best regards,
Leo

Sending a new patch seems more reasonable. I will send a new patch later based on this patch.

Best regards,
Leo

#7 lore raw

PATCH 1/1 riscv: Fix efi header for RV32

In reply to Leo Liang
Quoted 79 lines · Leo Liang

Hi Atish and Heinrich,

Quoted 50 lines · Atish Patra

RV32 should use PE32 format instead of PE32+ as the efi header format. This requires following changes
1. A different header magic value
2. An additional parameter known as BaseOfData. Currently, it is set to zero in absence of any usage.

---
 arch/riscv/lib/crt0_riscv_efi.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/riscv/lib/crt0_riscv_efi.S +6 -1
index 87fe1e56f906..4aaa49ad0777 100644
@@ -15,11 +15,13 @@
 #define SAVE_LONG(reg, idx)  sd      reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)  ld      reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE   IMAGE_FILE_MACHINE_RISCV64
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR64_MAGIC
 #else
 #define SIZE_LONG    4
 #define SAVE_LONG(reg, idx)  sw      reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)  lw      reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE   IMAGE_FILE_MACHINE_RISCV32
+#define PE_MAGIC        IMAGE_NT_OPTIONAL_HDR32_MAGIC
 #endif
 
 
@@ -48,7 +50,7 @@ coff_header:
               IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
               IMAGE_FILE_DEBUG_STRIPPED)
 optional_header:
-     .short  IMAGE_NT_OPTIONAL_HDR64_MAGIC   /* PE32+ format */
+     .short  PE_MAGIC                        /* PE32+ format */
      .byte   0x02                            /* MajorLinkerVersion */
      .byte   0x14                            /* MinorLinkerVersion */
      .long   _edata - _start                 /* SizeOfCode */
@@ -56,6 +58,9 @@ optional_header:
      .long   0                               /* SizeOfUninitializedData */
      .long   _start - ImageBase              /* AddressOfEntryPoint */
      .long   _start - ImageBase              /* BaseOfCode */
+#if __riscv_xlen == 32
+     .long   0                               /* BaseOfData */
+#endif
 
 extra_header_fields:
      .quad   0                               /* ImageBase */
arch/riscv/lib/crt0_riscv_efi.S +8 -0
index 4aaa49ad07..f47c4a6d82 100644
@@ -63,7 +63,11 @@ optional_header:
 #endif
 
 extra_header_fields:
+#if __riscv_xlen == 32
+       .long   0                               /* ImageBase */
+#else if
        .quad   0                               /* ImageBase */
+#endif
        .long   0x20                            /* SectionAlignment */
        .long   0x8                             /* FileAlignment */
        .short  0                               /* MajorOperatingSystemVersion */
@@ -83,10 +87,17 @@ extra_header_fields:
        .long   0                               /* CheckSum */
        .short  IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
        .short  0                               /* DllCharacteristics */
+#if __riscv_xlen == 32
+       .long   0                               /* SizeOfStackReserve */
+       .long   0                               /* SizeOfStackCommit */
+       .long   0                               /* SizeOfHeapReserve */

arch/riscv/lib/crt0_riscv_efi.S extra_header_fields: · line 103

Quoted 10 lines
arch/riscv/lib/crt0_riscv_efi.S +3 -0
+       .long   0                               /* SizeOfHeapCommit */
+#else if
        .quad   0                               /* SizeOfStackReserve */
        .quad   0                               /* SizeOfStackCommit */
        .quad   0                               /* SizeOfHeapReserve */
        .quad   0                               /* SizeOfHeapCommit */
+#endif
        .long   0                               /* LoaderFlags */
        .long   0x6                             /* NumberOfRvaAndSizes */
 

Sorry for not responding to this thread earlier. It went to a different filter due to a filtering mistake.

Quoted 3 lines

Based on Atish's patch,
would the above modification fit more closely to the specification that Heinrich provided ?

The above changes look good to me and matches the PE32 spec. Thanks for the fix.

Quoted 2 lines

Best regards,
Leo

Signature · 2 lines
Regards,
Atish