From 1b183017aa2fbb73785dc24b4cff32daae942c9c Mon Sep 17 00:00:00 2001
From: Andreas Schwab <schwab@suse.de>
Date: Thu, 3 Aug 2023 17:22:42 +0200
Subject: Add basic support for RISC-V 64-bit EFI objects

This adds a new PEI target pei-riscv64-little.  Only objdump and objcopy
are supported.

bfd:
	* .gitignore: Add pe-riscv64igen.c.
	* Makefile.am (BFD64_BACKENDS): Add pei-riscv64.lo,
	pe-riscv64igen.lo.
	(BFD64_BACKENDS_CFILES): Add pei-riscv64.c.
	(BUILD_CFILES): Add pe-riscv64igen.c.
	(pe-riscv64igen.c): New rule.
	* Makefile.in: Regenerate.
	* bfd.c (bfd_get_sign_extend_vma): Add pei-riscv64-little.
	* coff-riscv64.c: New file.
	* coffcode.h (coff_set_arch_mach_hook, coff_set_flags)
	(coff_write_object_contents): Add riscv64 (riscv64_pei_vec)
	support.
	* config.bfd (targ_selvecs): Add riscv64_pei_vec to all riscv*
	targets.
	* configure.ac: Handle riscv64_pei_vec.
	* configure: Regenerate.
	* libpei.h (GET_OPTHDR_IMAGE_BASE, PUT_OPTHDR_IMAGE_BASE)
	(GET_OPTHDR_SIZE_OF_STACK_RESERVE)
	(PUT_OPTHDR_SIZE_OF_STACK_RESERVE)
	(GET_OPTHDR_SIZE_OF_STACK_COMMIT, PUT_OPTHDR_SIZE_OF_STACK_COMMIT)
	(GET_OPTHDR_SIZE_OF_HEAP_RESERVE, PUT_OPTHDR_SIZE_OF_HEAP_RESERVE)
	(GET_OPTHDR_SIZE_OF_HEAP_COMMIT, PUT_OPTHDR_SIZE_OF_HEAP_COMMIT)
	(GET_PDATA_ENTRY, _bfd_XX_bfd_copy_private_bfd_data_common)
	(_bfd_XX_bfd_copy_private_section_data)
	(_bfd_XX_get_symbol_info, _bfd_XX_only_swap_filehdr_out)
	(_bfd_XX_print_private_bfd_data_common)
	(_bfd_XXi_final_link_postscript, _bfd_XXi_only_swap_filehdr_out)
	(_bfd_XXi_swap_aouthdr_in, _bfd_XXi_swap_aouthdr_out)
	(_bfd_XXi_swap_aux_in, _bfd_XXi_swap_aux_out)
	(_bfd_XXi_swap_lineno_in, _bfd_XXi_swap_lineno_out)
	(_bfd_XXi_swap_scnhdr_out, _bfd_XXi_swap_sym_in)
	(_bfd_XXi_swap_sym_out, _bfd_XXi_swap_debugdir_in)
	(_bfd_XXi_swap_debugdir_out, _bfd_XXi_write_codeview_record)
	(_bfd_XXi_slurp_codeview_record) [COFF_WITH_peRiscV64]: Define.
	(_bfd_peRiscV64_print_ce_compressed_pdata): Declare.
	* peXXigen.c (_bfd_XXi_swap_aouthdr_in, _bfd_XXi_swap_aouthdr_out)
	(_bfd_XXi_swap_scnhdr_out, pe_print_pdata)
	(_bfd_XX_print_private_bfd_data_common)
	(_bfd_XX_bfd_copy_private_section_data)
	(_bfd_XXi_final_link_postscript): Support COFF_WITH_peRiscV64.
	* pei-riscv64.c: New file.
	* peicode.h (coff_swap_scnhdr_in, pe_ILF_build_a_bfd)
	(pe_ILF_object_p): Support COFF_WITH_peRiscV64.
	(jtab): Add dummy entry that traps.
	* targets.c (_bfd_target_vector): Add riscv64_pei_vec.

binutils:
	* testsuite/binutils-all/riscv/pei-riscv64.d: New.
	* testsuite/binutils-all/riscv/pei-riscv64.s: New.

include:
	* coff/riscv64.h: New file.
	* coff/pe.h (IMAGE_FILE_MACHINE_RISCV32)
	(IMAGE_FILE_MACHINE_RISCV64): Define.
---
 .../testsuite/binutils-all/riscv/pei-riscv64.d     | 16 +++++++++++
 .../testsuite/binutils-all/riscv/pei-riscv64.s     | 31 ++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 binutils/testsuite/binutils-all/riscv/pei-riscv64.d
 create mode 100644 binutils/testsuite/binutils-all/riscv/pei-riscv64.s

(limited to 'binutils')

diff --git a/binutils/testsuite/binutils-all/riscv/pei-riscv64.d b/binutils/testsuite/binutils-all/riscv/pei-riscv64.d
new file mode 100644
index 0000000..189b016
--- /dev/null
+++ b/binutils/testsuite/binutils-all/riscv/pei-riscv64.d
@@ -0,0 +1,16 @@
+#as: -march=rv64gc -mabi=lp64d
+#ld: -m elf64lriscv -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=pei-riscv64-little
+#objdump: -h -f
+#name: Check if efi app format is recognized
+
+.*:     file format pei-riscv64-little
+architecture: riscv:rv64, flags 0x00000132:
+EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
+start address 0x0000000000000000
+
+Sections:
+Idx Name          Size      VMA               LMA               File off  Algn
+  0 \.text         00000010  0[^ ]+  0[^ ]+  0[^ ]+  2\*\*2
+                  CONTENTS, ALLOC, LOAD, READONLY, CODE
diff --git a/binutils/testsuite/binutils-all/riscv/pei-riscv64.s b/binutils/testsuite/binutils-all/riscv/pei-riscv64.s
new file mode 100644
index 0000000..68c0e5b
--- /dev/null
+++ b/binutils/testsuite/binutils-all/riscv/pei-riscv64.s
@@ -0,0 +1,31 @@
+	.file	"test_pei.c"
+	.option nopic
+	.attribute arch, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0"
+	.attribute unaligned_access, 0
+	.attribute stack_align, 16
+	.text
+	.align	1
+	.globl	main
+	.type	main, @function
+main:
+.LFB0:
+	.cfi_startproc
+	addi	sp,sp,-16
+	.cfi_def_cfa_offset 16
+	sd	s0,8(sp)
+	.cfi_offset 8, -8
+	addi	s0,sp,16
+	.cfi_def_cfa 8, 0
+	li	a5,0
+	mv	a0,a5
+	ld	s0,8(sp)
+	.cfi_restore 8
+	.cfi_def_cfa 2, 16
+	addi	sp,sp,16
+	.cfi_def_cfa_offset 0
+	jr	ra
+	.cfi_endproc
+.LFE0:
+	.size	main, .-main
+	.ident	"GCC: (SUSE Linux) 13.1.1 20230720 [revision 9aac37ab8a7b919a89c6d64bc7107a8436996e93]"
+	.section	.note.GNU-stack,"",@progbits
-- 
cgit v1.1