Re: [PATCH 16/16] efi: LoongArch: Implement everything

2024-05-23 Thread Jiaxun Yang



在2024年5月23日五月 下午5:26,Heinrich Schuchardt写道:

>> diff --git a/arch/loongarch/lib/reloc_loongarch_efi.c 
>> b/arch/loongarch/lib/reloc_loongarch_efi.c
>> new file mode 100644
>> index ..32a7d792103d
>> --- /dev/null
>> +++ b/arch/loongarch/lib/reloc_loongarch_efi.c
>
> The code seems to be very similar for all architectures. I wonder if one
> implementation could cover them all.

Not for MIPS due to differences on psABI dynamic relocations.
We still need to handle differences between Rel and Rela and other tiny bits.

I think most architectures have their relocation handling code rooted from
gnu-efi.

Thanks
- Jiaxun

>
> Best regards
>
> Heinrich
>

-- 
- Jiaxun


Re: [PATCH 16/16] efi: LoongArch: Implement everything

2024-05-23 Thread Heinrich Schuchardt

On 22.05.24 17:34, Jiaxun Yang wrote:

Implement crt, reloc, linker scripts, wire things up in
Makefiles and Kconfig.

Signed-off-by: Jiaxun Yang 
---
  arch/loongarch/config.mk |   4 +
  arch/loongarch/lib/Makefile  |  12 ++
  arch/loongarch/lib/crt0_loongarch_efi.S  | 182 +++
  arch/loongarch/lib/elf_loongarch_efi.lds |  76 +
  arch/loongarch/lib/reloc_loongarch_efi.c | 107 ++
  lib/efi_loader/Kconfig   |   2 +-
  6 files changed, 382 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/config.mk b/arch/loongarch/config.mk
index 7c247400e361..bae4566e9b62 100644
--- a/arch/loongarch/config.mk
+++ b/arch/loongarch/config.mk
@@ -21,3 +21,7 @@ endif
  PLATFORM_CPPFLAGS += -fpic
  PLATFORM_RELFLAGS += -fno-common -ffunction-sections -fdata-sections
  LDFLAGS_u-boot+= --gc-sections -static -pie
+
+EFI_LDS:= elf_loongarch_efi.lds
+EFI_CRT0   := crt0_loongarch_efi.o
+EFI_RELOC  := reloc_loongarch_efi.o
diff --git a/arch/loongarch/lib/Makefile b/arch/loongarch/lib/Makefile
index e65e66357a9b..17d2b1160b41 100644
--- a/arch/loongarch/lib/Makefile
+++ b/arch/loongarch/lib/Makefile
@@ -12,3 +12,15 @@ ifeq ($(CONFIG_$(SPL_)SYSRESET),)
  obj-y += reset.o
  endif
  obj-y += setjmp.o
+
+# For building EFI apps
+CFLAGS_NON_EFI := -fstack-protector-strong
+CFLAGS_$(EFI_CRT0) := $(CFLAGS_EFI)
+CFLAGS_REMOVE_$(EFI_CRT0) := $(CFLAGS_NON_EFI)
+
+CFLAGS_$(EFI_RELOC) := $(CFLAGS_EFI)
+CFLAGS_REMOVE_$(EFI_RELOC) := $(CFLAGS_NON_EFI)
+
+extra-$(CONFIG_CMD_BOOTEFI_HELLO_COMPILE) += $(EFI_CRT0) $(EFI_RELOC)
+extra-$(CONFIG_CMD_BOOTEFI_SELFTEST) += $(EFI_CRT0) $(EFI_RELOC)
+extra-$(CONFIG_EFI) += $(EFI_CRT0) $(EFI_RELOC)
diff --git a/arch/loongarch/lib/crt0_loongarch_efi.S 
b/arch/loongarch/lib/crt0_loongarch_efi.S
new file mode 100644
index ..5be47045ad8a
--- /dev/null
+++ b/arch/loongarch/lib/crt0_loongarch_efi.S
@@ -0,0 +1,182 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * crt0-efi-loongarch.S - PE/COFF header for LoongArch EFI applications
+ *
+ * Copright (C) 2024 Jiaxun Yang 
+ */
+
+#include 
+#include 
+
+#ifdef __loongarch64
+#define PE_MACHINE IMAGE_FILE_MACHINE_LOONGARCH64
+#define PE_MAGICIMAGE_NT_OPTIONAL_HDR64_MAGIC
+#define IMG_CHARACTERISTICS \
+   (IMAGE_FILE_EXECUTABLE_IMAGE | \
+IMAGE_FILE_LINE_NUMS_STRIPPED | \
+IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
+IMAGE_FILE_LARGE_ADDRESS_AWARE | \
+IMAGE_FILE_DEBUG_STRIPPED)
+#else
+#define PE_MACHINE IMAGE_FILE_MACHINE_LOONGARCH32
+#define PE_MAGICIMAGE_NT_OPTIONAL_HDR32_MAGIC
+#define IMG_CHARACTERISTICS \
+   (IMAGE_FILE_EXECUTABLE_IMAGE | \
+IMAGE_FILE_LINE_NUMS_STRIPPED | \
+IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
+IMAGE_FILE_DEBUG_STRIPPED)
+#endif
+
+   .section.text.head
+
+   /*
+* Magic "MZ" signature for PE/COFF
+*/
+   .globl  ImageBase
+ImageBase:
+   .short  IMAGE_DOS_SIGNATURE /* 'MZ' */
+   .skip   58
+   .long   pe_header - ImageBase   /* Offset to the PE header */
+pe_header:
+   .long   IMAGE_NT_SIGNATURE  /* 'PE' */
+coff_header:
+   .short  PE_MACHINE  /* LoongArch 64/32-bit */
+   .short  3   /* nr_sections */
+   .long   0   /* TimeDateStamp */
+   .long   0   /* PointerToSymbolTable */
+   .long   0   /* NumberOfSymbols */
+   .short  section_table - optional_header /* SizeOfOptionalHeader */
+   .short  IMG_CHARACTERISTICS /* Characteristics */
+optional_header:
+   .short  PE_MAGIC/* PE32(+) format */
+   .byte   0x02/* MajorLinkerVersion */
+   .byte   0x14/* MinorLinkerVersion */
+   .long   _edata - _start /* SizeOfCode */
+   .long   0   /* SizeOfInitializedData */
+   .long   0   /* SizeOfUninitializedData */
+   .long   _start - ImageBase  /* AddressOfEntryPoint */
+   .long   _start - ImageBase  /* BaseOfCode */
+#ifndef __loongarch64
+   .long   0   /* BaseOfData */
+#endif
+
+extra_header_fields:
+   LONG0
+   .long   0x200   /* SectionAlignment */
+   .long   0x200   /* FileAlignment */
+   .short  0   /* MajorOperatingSystemVersion 
*/
+   .short  0   /* MinorOperatingSystemVersion 
*/
+   .short  1   /* MajorImageVersion */
+   .short  0   /* MinorImageVersion */
+   .short

[PATCH 16/16] efi: LoongArch: Implement everything

2024-05-22 Thread Jiaxun Yang
Implement crt, reloc, linker scripts, wire things up in
Makefiles and Kconfig.

Signed-off-by: Jiaxun Yang 
---
 arch/loongarch/config.mk |   4 +
 arch/loongarch/lib/Makefile  |  12 ++
 arch/loongarch/lib/crt0_loongarch_efi.S  | 182 +++
 arch/loongarch/lib/elf_loongarch_efi.lds |  76 +
 arch/loongarch/lib/reloc_loongarch_efi.c | 107 ++
 lib/efi_loader/Kconfig   |   2 +-
 6 files changed, 382 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/config.mk b/arch/loongarch/config.mk
index 7c247400e361..bae4566e9b62 100644
--- a/arch/loongarch/config.mk
+++ b/arch/loongarch/config.mk
@@ -21,3 +21,7 @@ endif
 PLATFORM_CPPFLAGS  += -fpic
 PLATFORM_RELFLAGS  += -fno-common -ffunction-sections -fdata-sections
 LDFLAGS_u-boot += --gc-sections -static -pie
+
+EFI_LDS:= elf_loongarch_efi.lds
+EFI_CRT0   := crt0_loongarch_efi.o
+EFI_RELOC  := reloc_loongarch_efi.o
diff --git a/arch/loongarch/lib/Makefile b/arch/loongarch/lib/Makefile
index e65e66357a9b..17d2b1160b41 100644
--- a/arch/loongarch/lib/Makefile
+++ b/arch/loongarch/lib/Makefile
@@ -12,3 +12,15 @@ ifeq ($(CONFIG_$(SPL_)SYSRESET),)
 obj-y  += reset.o
 endif
 obj-y  += setjmp.o
+
+# For building EFI apps
+CFLAGS_NON_EFI := -fstack-protector-strong
+CFLAGS_$(EFI_CRT0) := $(CFLAGS_EFI)
+CFLAGS_REMOVE_$(EFI_CRT0) := $(CFLAGS_NON_EFI)
+
+CFLAGS_$(EFI_RELOC) := $(CFLAGS_EFI)
+CFLAGS_REMOVE_$(EFI_RELOC) := $(CFLAGS_NON_EFI)
+
+extra-$(CONFIG_CMD_BOOTEFI_HELLO_COMPILE) += $(EFI_CRT0) $(EFI_RELOC)
+extra-$(CONFIG_CMD_BOOTEFI_SELFTEST) += $(EFI_CRT0) $(EFI_RELOC)
+extra-$(CONFIG_EFI) += $(EFI_CRT0) $(EFI_RELOC)
diff --git a/arch/loongarch/lib/crt0_loongarch_efi.S 
b/arch/loongarch/lib/crt0_loongarch_efi.S
new file mode 100644
index ..5be47045ad8a
--- /dev/null
+++ b/arch/loongarch/lib/crt0_loongarch_efi.S
@@ -0,0 +1,182 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * crt0-efi-loongarch.S - PE/COFF header for LoongArch EFI applications
+ *
+ * Copright (C) 2024 Jiaxun Yang 
+ */
+
+#include 
+#include 
+
+#ifdef __loongarch64
+#define PE_MACHINE IMAGE_FILE_MACHINE_LOONGARCH64
+#define PE_MAGICIMAGE_NT_OPTIONAL_HDR64_MAGIC
+#define IMG_CHARACTERISTICS \
+   (IMAGE_FILE_EXECUTABLE_IMAGE | \
+IMAGE_FILE_LINE_NUMS_STRIPPED | \
+IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
+IMAGE_FILE_LARGE_ADDRESS_AWARE | \
+IMAGE_FILE_DEBUG_STRIPPED)
+#else
+#define PE_MACHINE IMAGE_FILE_MACHINE_LOONGARCH32
+#define PE_MAGICIMAGE_NT_OPTIONAL_HDR32_MAGIC
+#define IMG_CHARACTERISTICS \
+   (IMAGE_FILE_EXECUTABLE_IMAGE | \
+IMAGE_FILE_LINE_NUMS_STRIPPED | \
+IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
+IMAGE_FILE_DEBUG_STRIPPED)
+#endif
+
+   .section.text.head
+
+   /*
+* Magic "MZ" signature for PE/COFF
+*/
+   .globl  ImageBase
+ImageBase:
+   .short  IMAGE_DOS_SIGNATURE /* 'MZ' */
+   .skip   58
+   .long   pe_header - ImageBase   /* Offset to the PE header */
+pe_header:
+   .long   IMAGE_NT_SIGNATURE  /* 'PE' */
+coff_header:
+   .short  PE_MACHINE  /* LoongArch 64/32-bit */
+   .short  3   /* nr_sections */
+   .long   0   /* TimeDateStamp */
+   .long   0   /* PointerToSymbolTable */
+   .long   0   /* NumberOfSymbols */
+   .short  section_table - optional_header /* SizeOfOptionalHeader */
+   .short  IMG_CHARACTERISTICS /* Characteristics */
+optional_header:
+   .short  PE_MAGIC/* PE32(+) format */
+   .byte   0x02/* MajorLinkerVersion */
+   .byte   0x14/* MinorLinkerVersion */
+   .long   _edata - _start /* SizeOfCode */
+   .long   0   /* SizeOfInitializedData */
+   .long   0   /* SizeOfUninitializedData */
+   .long   _start - ImageBase  /* AddressOfEntryPoint */
+   .long   _start - ImageBase  /* BaseOfCode */
+#ifndef __loongarch64
+   .long   0   /* BaseOfData */
+#endif
+
+extra_header_fields:
+   LONG0
+   .long   0x200   /* SectionAlignment */
+   .long   0x200   /* FileAlignment */
+   .short  0   /* MajorOperatingSystemVersion 
*/
+   .short  0   /* MinorOperatingSystemVersion 
*/
+   .short  1   /* MajorImageVersion */
+   .short  0   /* MinorImageVersion */
+   .short  0   /* MajorSubsystemVersi