From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 035C23858C53 for ; Fri, 5 Aug 2022 10:07:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 035C23858C53 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from [10.130.0.193] (unknown [113.200.148.30]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9BxHOTh6+xig8AHAA--.28402S3; Fri, 05 Aug 2022 18:07:30 +0800 (CST) Subject: Re: [PATCH] bfd: Add support for LoongArch64 EFI (efi-*-loongarch64). To: WANG Xuerui References: <1659666386-5500-1-git-send-email-tangyouling@loongson.cn> <24a9300d-24f9-d4cf-4b71-198d5f4da511@xen0n.name> From: Youling Tang Cc: binutils@sourceware.org, Chenghua Xu , Zhensong Liu Message-ID: Date: Fri, 5 Aug 2022 18:07:29 +0800 User-Agent: Mozilla/5.0 (X11; Linux mips64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <24a9300d-24f9-d4cf-4b71-198d5f4da511@xen0n.name> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-CM-TRANSID: AQAAf9BxHOTh6+xig8AHAA--.28402S3 X-Coremail-Antispam: 1UD129KBjvAXoW3tF4DAFW7CFW7WrW3CFyxuFg_yoW8Jw48uo WfuayI9w48Kr15CrnYgwn3XryUtw1Fgay5XFW3Za1kGanFga45Cw18t3W5W3W7t3Z7Ga1r Ga45GF98Zryavwn3n29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UjIYCTnIWjp_UUUYT7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xva j40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2 x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8 Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26r xl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj 6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr 0_Gr1lF7xvr2IY64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7Mxk0xIA0c2IEe2xFo4CE bIxvr21lc2xSY4AK67AK6r4DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r 4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF 67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2I x0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_WFyUJVCq3wCI42IY 6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa 73UjIFyTuYvjfUnBMNUUUUU X-CM-SenderInfo: 5wdqw5prxox03j6o00pqjv00gofq/ X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, NICE_REPLY_A, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Aug 2022 10:07:40 -0000 Hi, Xuerui On 08/05/2022 05:41 PM, WANG Xuerui wrote: > > On 2022/8/5 10:26, Youling Tang wrote: >> This adds support for efi-loongarch64 by virtue of adding a new PEI >> target >> pei-loongarch64. This is not a full target and only exists to support >> EFI at >> this time. >> >> This means that this target does not support relocation processing and >> is mostly >> a container format. This format has been added to elf based >> loongarch64 targets >> such that efi images can be made natively on Linux. >> >> However this target is not valid for use with gas but only with objcopy. >> >> We should't limit addresses to 32-bits for 64-bit vma, otherwise there >> will be >> "RVA truncated" error when using objcopy on loongarch64. >> >> With these changes the resulting file is recognized as an efi image. >> >> Any magic number is based on the Microsoft PE specification [1]. >> >> The test results are as follows: >> $ make check-binutils RUNTESTFLAGS='loongarch64.exp' >> PASS: Check if efi app format is recognized >> >> $ objdump -h -f tmpdir/loongarch64copy.o >> tmpdir/loongarch64copy.o: file format pei-loongarch64 >> architecture: Loongarch64, flags 0x00000132: >> EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED >> start address 0x0000000000000000 >> >> Sections: >> Idx Name Size VMA LMA >> File off Algn >> 0 .text 0000003c 00000000200000b0 00000000200000b0 >> 00000200 2**2 >> CONTENTS, ALLOC, LOAD, READONLY, CODE >> >> [1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format >> >> bfd: >> * .gitignore (pe-loongarch64igen.c): New. >> * Makefile.am (pei-loongarch64.lo, pe-loongarch64igen.lo, >> pei-loongarch64.c, >> pe-loongarch64igen.c): Add support. >> * Makefile.in: Likewise. >> * bfd.c (bfd_get_sign_extend_vma): Add pei-loongarch64. >> * coff-loongarch64.c: New file. >> * coffcode.h (coff_set_arch_mach_hook, coff_set_flags, >> coff_write_object_contents) Add loongarch64 (loongarch64_pei_vec) >> support. >> * config.bfd: Likewise. >> * configure: Likewise. >> * configure.ac: Likewise. >> * 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_peLoongArch64_bfd_copy_private_bfd_data_common, >> _bfd_peLoongArch64_bfd_copy_private_section_data, >> _bfd_peLoongArch64_get_symbol_info, >> _bfd_peLoongArch64_only_swap_filehdr_out, >> _bfd_peLoongArch64_print_private_bfd_data_common, >> _bfd_peLoongArch64i_final_link_postscript, >> _bfd_peLoongArch64i_only_swap_filehdr_out, >> _bfd_peLoongArch64i_swap_aouthdr_in, >> _bfd_peLoongArch64i_swap_aouthdr_out, _bfd_peLoongArch64i_swap_aux_in, >> _bfd_peLoongArch64i_swap_aux_out, _bfd_peLoongArch64i_swap_lineno_in, >> _bfd_peLoongArch64i_swap_lineno_out, >> _bfd_peLoongArch64i_swap_scnhdr_out, >> _bfd_peLoongArch64i_swap_sym_in, _bfd_peLoongArch64i_swap_sym_out, >> _bfd_peLoongArch64i_swap_debugdir_in, >> _bfd_peLoongArch64i_swap_debugdir_out, >> _bfd_peLoongArch64i_write_codeview_record, >> _bfd_peLoongArch64i_slurp_codeview_record, >> _bfd_peLoongArch64_print_ce_compressed_pdata): New. >> * 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_peLoongArch64, >> * pei-loongarch64.c: New file. >> * peicode.h (coff_swap_scnhdr_in, pe_ILF_build_a_bfd, >> pe_ILF_object_p): >> Support COFF_WITH_peLoongArch64. >> (jtab): Add dummy entry that traps. >> * targets.c (loongarch64_pei_vec): New. >> >> binutils >> * NEWS: Add new support. >> * testsuite/binutils-all/loongarch64/loongarch64.exp: New file. >> * testsuite/binutils-all/loongarch64/pei-loongarch64.d: New test. >> * testsuite/binutils-all/loongarch64/pei-loongarch64.s: New test. >> >> include >> * coff/loongarch64.h: New file. >> * coff/pe.h (IMAGE_FILE_MACHINE_LOONGARCH64): New. >> >> Signed-off-by: Youling Tang >> --- >> bfd/.gitignore | 1 + >> bfd/Makefile.am | 9 +- >> bfd/Makefile.in | 11 +- >> bfd/bfd.c | 1 + >> bfd/coff-loongarch64.c | 165 ++++++++++++++++++ >> bfd/coffcode.h | 19 +- >> bfd/config.bfd | 2 +- >> bfd/configure | 1 + >> bfd/configure.ac | 1 + >> bfd/libpei.h | 36 ++++ >> bfd/peXXigen.c | 49 +++--- >> bfd/pei-loongarch64.c | 75 ++++++++ >> bfd/peicode.h | 21 ++- >> bfd/targets.c | 2 + >> binutils/NEWS | 3 + >> .../binutils-all/loongarch64/loongarch64.exp | 30 ++++ >> .../loongarch64/pei-loongarch64.d | 15 ++ >> .../loongarch64/pei-loongarch64.s | 33 ++++ >> include/coff/loongarch64.h | 61 +++++++ >> include/coff/pe.h | 1 + >> 20 files changed, 509 insertions(+), 27 deletions(-) >> create mode 100644 bfd/coff-loongarch64.c >> create mode 100644 bfd/pei-loongarch64.c >> create mode 100644 >> binutils/testsuite/binutils-all/loongarch64/loongarch64.exp >> create mode 100644 >> binutils/testsuite/binutils-all/loongarch64/pei-loongarch64.d >> create mode 100644 >> binutils/testsuite/binutils-all/loongarch64/pei-loongarch64.s >> create mode 100644 include/coff/loongarch64.h >> >> >> >> diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c >> index 5ab09387e72..ba12cf8cba0 100644 >> --- a/bfd/peXXigen.c >> +++ b/bfd/peXXigen.c >> @@ -60,9 +60,9 @@ >> on this code has a chance of getting something accomplished without >> wasting too much time. */ >> -/* This expands into COFF_WITH_pe, COFF_WITH_pep, COFF_WITH_pex64 or >> - COFF_WITH_peAArch64 depending on whether we're compiling for straight >> - PE or PE+. */ >> +/* This expands into COFF_WITH_pe, COFF_WITH_pep, COFF_WITH_pex64 , > Nit: extra space before the comma. my carelessness. >> + COFF_WITH_peAArch64 or COFF_WITH_peLoongArch64 depending on >> whether we're >> + compiling for straight PE or PE+. */ >> #define COFF_WITH_XX >> #include "sysdep.h" >> >> >> >> diff --git a/bfd/pei-loongarch64.c b/bfd/pei-loongarch64.c >> new file mode 100644 >> index 00000000000..28220074157 >> --- /dev/null >> +++ b/bfd/pei-loongarch64.c >> @@ -0,0 +1,75 @@ >> +/* BFD back-end for LoongArch64 PE IMAGE COFF files. >> + Copyright (C) 2022 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" >> + >> +#define TARGET_SYM loongarch64_pei_vec >> +#define TARGET_NAME "pei-loongarch64" >> +#define TARGET_ARCHITECTURE bfd_arch_loongarch >> +#define TARGET_PAGESIZE 0x4000 >> +#define TARGET_BIG_ENDIAN 0 >> +#define TARGET_ARCHIVE 0 >> +#define TARGET_PRIORITY 0 >> + >> +#define COFF_IMAGE_WITH_PE >> +/* Rename the above into.. */ > I know this is copied from bfd/pei-aarch64.c, but you could reformat it > a little by adding an additional period and space, for style consistency. In the previous macro definition, I used tab alignment. Do you want to use space alignment? >> +#define COFF_WITH_peLoongArch64 >> +#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 } >> + >> +#define PEI_HEADERS >> +#include "sysdep.h" >> +#include "bfd.h" >> +#include "libbfd.h" >> +#include "coff/loongarch64.h" >> +#include "coff/internal.h" >> +#include "coff/pe.h" >> +#include "libcoff.h" >> +#include "libpei.h" >> +#include "libiberty.h" >> + >> +/* Make sure we're setting a 64-bit format. */ >> +#undef AOUTSZ >> +#define AOUTSZ PEPAOUTSZ >> +#define PEAOUTHDR PEPAOUTHDR >> + >> +#include "coff-loongarch64.c" >> diff --git a/bfd/peicode.h b/bfd/peicode.h >> index 02573c84694..a337493994c 100644 >> --- a/bfd/peicode.h >> +++ b/bfd/peicode.h >> @@ -231,7 +231,7 @@ coff_swap_scnhdr_in (bfd * abfd, void * ext, void >> * in) >> { >> scnhdr_int->s_vaddr += pe_data (abfd)->pe_opthdr.ImageBase; >> /* Do not cut upper 32-bits for 64-bit vma. */ >> -#if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) >> +#if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && >> !defined(COFF_WITH_peLoongArch64) >> scnhdr_int->s_vaddr &= 0xffffffff; >> #endif >> } >> @@ -763,6 +763,17 @@ static const jump_table jtab[] = >> 16, 12 >> }, >> #endif >> + >> +#ifdef LOONGARCH64MAGIC >> +/* We don't currently support jumping to DLLs, so if >> + someone does try emit a runtime trap. Through UDF #0. */ >> + { LOONGARCH64MAGIC, >> + { 0x00, 0x00, 0x00, 0x00 }, > I think "UDF #0" is an ARM instruction. Here I think we could use a > "BREAK 0" or something like that instead... The instruction word for > BREAK 0 is 0x002a0000, FYI. Thanks for the suggestion, I will use the "break 0" instruction. >> + 4, 0 >> + }, >> + >> +#endif >> + >> { 0, { 0 }, 0, 0 } >> }; >> @@ -920,7 +931,7 @@ pe_ILF_build_a_bfd (bfd * abfd, >> /* See PR 20907 for a reproducer. */ >> goto error_return; >> -#if defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) >> +#if defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) || >> defined(COFF_WITH_peLoongArch64) >> ((unsigned int *) id4->contents)[0] = ordinal; >> ((unsigned int *) id4->contents)[1] = 0x80000000; >> ((unsigned int *) id5->contents)[0] = ordinal; >> @@ -1222,6 +1233,12 @@ pe_ILF_object_p (bfd * abfd) >> #endif >> break; >> + case IMAGE_FILE_MACHINE_LOONGARCH64: >> +#ifdef LOONGARCH64MAGIC >> + magic = LOONGARCH64MAGIC; >> +#endif >> + break; >> + >> case IMAGE_FILE_MACHINE_THUMB: >> #ifdef THUMBPEMAGIC >> { >> diff --git a/bfd/targets.c b/bfd/targets.c >> index 3284bb88aa8..8ad3ef366ce 100644 >> --- a/bfd/targets.c >> +++ b/bfd/targets.c >> @@ -768,6 +768,7 @@ extern const bfd_target lm32_elf32_vec; >> extern const bfd_target lm32_elf32_fdpic_vec; >> extern const bfd_target loongarch_elf64_vec; >> extern const bfd_target loongarch_elf32_vec; >> +extern const bfd_target loongarch64_pei_vec; >> extern const bfd_target m32c_elf32_vec; >> extern const bfd_target m32r_elf32_vec; >> extern const bfd_target m32r_elf32_le_vec; >> @@ -1358,6 +1359,7 @@ static const bfd_target * const >> _bfd_target_vector[] = >> #ifdef BFD64 >> &loongarch_elf32_vec, >> &loongarch_elf64_vec, >> + &loongarch64_pei_vec, >> #endif >> #endif /* not SELECT_VECS */ >> diff --git a/binutils/NEWS b/binutils/NEWS >> index 4fdf1c3b4e8..3b5a96b9819 100644 >> --- a/binutils/NEWS >> +++ b/binutils/NEWS >> @@ -14,6 +14,9 @@ Changes in 2.39: >> architectures. Use the --disassembler-color=MODE command line >> flag, with >> mode being either off, color, or extended-color. >> +* Support for pei-loongarch64 has been added to objcopy in order to >> enable >> + UEFI development using binutils. >> + > Isn't binutils 2.39 already frozen? I will add to the next version. Thanks, Youling