From patchwork Fri Apr 7 02:59:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Youling Tang X-Patchwork-Id: 67483 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EC7113857727 for ; Fri, 7 Apr 2023 03:00:11 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 9A22C3858D28 for ; Fri, 7 Apr 2023 03:00:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A22C3858D28 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 loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8DxJYwyhy9kyq0XAA--.36514S3; Fri, 07 Apr 2023 11:00:02 +0800 (CST) Received: from bogon.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxwOQQhy9kEc0XAA--.59209S3; Fri, 07 Apr 2023 10:59:59 +0800 (CST) From: Youling Tang To: elfutils-devel@sourceware.org, Mark@sourceware.org, Wielaard@sourceware.org, mark@klomp.org Cc: Hengqi Chen , Liwei Ge Subject: [PATCH 1/5] backends: Add abi_cfi and register_info callbacks for LoongArch Date: Fri, 7 Apr 2023 10:59:24 +0800 Message-Id: <1680836368-5373-2-git-send-email-tangyouling@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> References: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> X-CM-TRANSID: AQAAf8DxwOQQhy9kEc0XAA--.59209S3 X-CM-SenderInfo: 5wdqw5prxox03j6o00pqjv00g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f9x07jOb18UUUUU= X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, TXREP 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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" LoongArch Reference Manual - Volume 1: https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html LoongArch ELF ABI: https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html Signed-off-by: Liwei Ge Signed-off-by: Youling Tang --- backends/ChangeLog | 9 ++- backends/Makefile.am | 3 +- backends/loongarch_cfi.c | 83 ++++++++++++++++++++++ backends/loongarch_init.c | 4 ++ backends/loongarch_regs.c | 141 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 238 insertions(+), 2 deletions(-) create mode 100644 backends/loongarch_cfi.c create mode 100644 backends/loongarch_regs.c diff --git a/backends/ChangeLog b/backends/ChangeLog index 7bde2919..926c76ed 100644 --- a/backends/ChangeLog +++ b/backends/ChangeLog @@ -1,4 +1,11 @@ -2023-04-01 Youling Tang +2023-04-07 Youling Tang + + * Makefile.am (loongarch_SRCS): Add loongarch_cfi.c and loongarch_regs.c. + * loongarch_cfi.c: New file. + * loongarch_regs.c: Likewise. + * loongarch_init.c (loongarch_init): Hook register_info and abi_cfi. + +2023-04-01 Youling Tang * loongarch_init.c (loongarch_init): Hook check_special_symbol. * loongarch_symbol.c (loongarch_check_special_symbol): New function. diff --git a/backends/Makefile.am b/backends/Makefile.am index f373e5fb..2b6f08ce 100644 --- a/backends/Makefile.am +++ b/backends/Makefile.am @@ -96,7 +96,8 @@ riscv_SRCS = riscv_init.c riscv_symbol.c riscv_cfi.c riscv_regs.c \ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c \ csky_regs.c csky_initreg.c csky_corenote.c -loongarch_SRCS = loongarch_init.c loongarch_symbol.c +loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \ + loongarch_regs.c arc_SRCS = arc_init.c arc_symbol.c diff --git a/backends/loongarch_cfi.c b/backends/loongarch_cfi.c new file mode 100644 index 00000000..5f3cd2c7 --- /dev/null +++ b/backends/loongarch_cfi.c @@ -0,0 +1,83 @@ +/* LoongArch ABI-specified defaults for DWARF CFI. + Copyright (C) 2023 OpenAnolis community LoongArch SIG. + Copyright (C) 2023 Loongson Technology Corporation Limted. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils 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 copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#define BACKEND loongarch_ +#include "libebl_CPU.h" + +/* LoongArch ELF ABI specification: +https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_register_convention +*/ +int +loongarch_abi_cfi (Ebl *ebl __attribute__ ((unused)), Dwarf_CIE *abi_info) +{ + static const uint8_t abi_cfi[] = + { + /* The initial Canonical Frame Address is the value of the + Stack Pointer ($r3) as setup in the previous frame. */ + DW_CFA_def_cfa, ULEB128_7 (3), ULEB128_7 (0), + + /* The Stack Pointer ($r3) is restored from CFA address by default. */ + DW_CFA_val_offset, ULEB128_7 (3), ULEB128_7 (0), + +#define SV(n) DW_CFA_same_value, ULEB128_7 (n) + /* The return address register contains the return address setup by + caller. */ + SV (1), + + /* Callee-saved registers $s0-$s7. */ + SV (23), SV (24), SV (25), SV (26), SV (27), SV (28), + SV (29), SV (30), SV (31), + + /* The Frame Pointer ($fp, $r22) */ + SV(22), + + /* Callee-saved registers $fs0-$fs7. */ + SV (56), SV (57), SV (58), SV (59), SV (60), SV (61), + SV (62), SV (63), +#undef SV + + /* XXX Note: registers intentionally unused by the program, + for example as a consequence of the procedure call standard + should be initialized as if by DW_CFA_same_value. */ + }; + + abi_info->initial_instructions = abi_cfi; + abi_info->initial_instructions_end = &abi_cfi[sizeof abi_cfi]; + abi_info->data_alignment_factor = -4; + + abi_info->return_address_register = 1; /* ra. */ + + return 0; +} diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c index b641b07f..7bfaaa7f 100644 --- a/backends/loongarch_init.c +++ b/backends/loongarch_init.c @@ -46,6 +46,10 @@ loongarch_init (Elf *elf __attribute__ ((unused)), loongarch_init_reloc (eh); HOOK (eh, reloc_simple_type); HOOK (eh, machine_flag_check); + HOOK (eh, register_info); + HOOK (eh, abi_cfi); + /* gcc/config/ #define DWARF_FRAME_REGISTERS. */ + eh->frame_nregs = 74; HOOK (eh, check_special_symbol); return eh; diff --git a/backends/loongarch_regs.c b/backends/loongarch_regs.c new file mode 100644 index 00000000..4a4b2052 --- /dev/null +++ b/backends/loongarch_regs.c @@ -0,0 +1,141 @@ +/* Register names and numbers for LoongArch DWARF. + Copyright (C) 2023 OpenAnolis community LoongArch SIG. + Copyright (C) 2023 Loongson Technology Corporation Limted. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils 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 copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include + +#define BACKEND loongarch_ +#include "libebl_CPU.h" + +ssize_t +loongarch_register_info (Ebl *ebl, int regno, char *name, size_t namelen, + const char **prefix, const char **setname, + int *bits, int *type) +{ + if (name == NULL) + return 64; + + *prefix = ""; + + if (regno < 32) + { + *setname = "integer"; + *type = DW_ATE_signed; + *bits = ebl->class == ELFCLASS64 ? 64 : 32; + } + else + { + *setname = "FPU"; + *type = DW_ATE_float; + *bits = 64; + } + + switch (regno) + { + case 0: + return stpcpy (name, "zero") + 1 - name; + + case 1: + *type = DW_ATE_address; + return stpcpy (name, "ra") + 1 - name; + + case 2: + *type = DW_ATE_address; + return stpcpy (name, "tp") + 1 - name; + + case 3: + *type = DW_ATE_address; + return stpcpy (name, "sp") + 1 - name; + + case 4 ... 11: + name[0] = 'a'; + name[1] = regno - 4 + '0'; + namelen = 2; + break; + + case 12 ... 20: + name[0] = 't'; + name[1] = regno - 12 + '0'; + namelen = 2; + break; + + case 21: + return stpcpy (name, "u0") + 1 - name; + + case 22: + *type = DW_ATE_address; + return stpcpy (name, "fp") + 1 - name; + + case 23 ... 31: + name[0] = 's'; + name[1] = regno - 23 + '0'; + namelen = 2; + break; + + case 32 ... 39: + name[0] = 'f'; + name[1] = 'a'; + name[2] = regno - 32 + '0'; + namelen = 3; + break; + + case 40 ... 49: + name[0] = 'f'; + name[1] = 't'; + name[2] = regno - 40 + '0'; + namelen = 3; + break; + + case 50 ... 55: + name[0] = 'f'; + name[1] = 't'; + name[2] = '1'; + name[3] = regno - 50 + '0'; + namelen = 4; + break; + + case 56 ... 63: + name[0] = 'f'; + name[1] = 's'; + name[2] = regno - 56 + '0'; + namelen = 3; + break; + + default: + *setname = NULL; + return 0; + } + + name[namelen++] = '\0'; + return namelen; +} From patchwork Fri Apr 7 02:59:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Youling Tang X-Patchwork-Id: 67484 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB29B3858436 for ; Fri, 7 Apr 2023 03:00:14 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 183113858D33 for ; Fri, 7 Apr 2023 03:00:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 183113858D33 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 loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8AxJPw1hy9kza0XAA--.36692S3; Fri, 07 Apr 2023 11:00:05 +0800 (CST) Received: from bogon.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxwOQQhy9kEc0XAA--.59209S4; Fri, 07 Apr 2023 11:00:05 +0800 (CST) From: Youling Tang To: elfutils-devel@sourceware.org, Mark@sourceware.org, Wielaard@sourceware.org, mark@klomp.org Cc: Hengqi Chen , Liwei Ge Subject: [PATCH 2/5] backends: Add set_initial_registers_tid callback for LoongArch Date: Fri, 7 Apr 2023 10:59:25 +0800 Message-Id: <1680836368-5373-3-git-send-email-tangyouling@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> References: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> X-CM-TRANSID: AQAAf8DxwOQQhy9kEc0XAA--.59209S4 X-CM-SenderInfo: 5wdqw5prxox03j6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW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, score=-12.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, TXREP 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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" This patch implements the set_initial_registers_tid hook for LoongArch. Signed-off-by: Liwei Ge Signed-off-by: Youling Tang --- backends/ChangeLog | 6 +++ backends/Makefile.am | 2 +- backends/loongarch_init.c | 1 + backends/loongarch_initreg.c | 91 ++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 backends/loongarch_initreg.c diff --git a/backends/ChangeLog b/backends/ChangeLog index 926c76ed..85abc8a5 100644 --- a/backends/ChangeLog +++ b/backends/ChangeLog @@ -1,3 +1,9 @@ +2023-04-07 Youling Tang + + * Makefile.am (loongarch_SRCS): Add loongarch_initreg.c. + * loongarch_initreg.c: New file. + * loongarch_init.c (loongarch_init): Hook set_initial_registers_tid. + 2023-04-07 Youling Tang * Makefile.am (loongarch_SRCS): Add loongarch_cfi.c and loongarch_regs.c. diff --git a/backends/Makefile.am b/backends/Makefile.am index 2b6f08ce..e7055d7e 100644 --- a/backends/Makefile.am +++ b/backends/Makefile.am @@ -97,7 +97,7 @@ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c \ csky_regs.c csky_initreg.c csky_corenote.c loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \ - loongarch_regs.c + loongarch_regs.c loongarch_initreg.c arc_SRCS = arc_init.c arc_symbol.c diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c index 7bfaaa7f..9c4b94a0 100644 --- a/backends/loongarch_init.c +++ b/backends/loongarch_init.c @@ -51,6 +51,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)), /* gcc/config/ #define DWARF_FRAME_REGISTERS. */ eh->frame_nregs = 74; HOOK (eh, check_special_symbol); + HOOK (eh, set_initial_registers_tid); return eh; } diff --git a/backends/loongarch_initreg.c b/backends/loongarch_initreg.c new file mode 100644 index 00000000..75497a51 --- /dev/null +++ b/backends/loongarch_initreg.c @@ -0,0 +1,91 @@ +/* Fetch live process registers from TID. + Copyright (C) 2023 OpenAnolis community LoongArch SIG. + Copyright (C) 2023 Loongson Technology Corporation Limted. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils 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 copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "system.h" +#include +#if defined __loongarch__ && defined __linux__ +# include +# include +# include +#endif + +#define BACKEND loongarch_ +#include "libebl_CPU.h" + +bool +loongarch_set_initial_registers_tid (pid_t tid __attribute__ ((unused)), + ebl_tid_registers_t *setfunc __attribute__ ((unused)), + void *arg __attribute__ ((unused))) +{ +#if !defined __loongarch__ || !defined __linux__ + return false; +#else /* __loongarch__ */ + + /* General registers. */ + struct user_regs_struct gregs; + struct iovec iovec; + iovec.iov_base = &gregs; + iovec.iov_len = sizeof (gregs); + if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec) != 0) + return false; + + /* $r0 is constant 0. */ + Dwarf_Word zero = 0; + if (! setfunc (0, 1, &zero, arg)) + return false; + + /* $r1-$r31. */ + if (! setfunc (1, 32, (Dwarf_Word *) &gregs.regs[1], arg)) + return false; + + /* PC. */ + if (! setfunc (-1, 1, (Dwarf_Word *) &gregs.csr_era, arg)) + return false; + + /* Floating-point registers (only 64bits are used). */ + struct user_fp_struct fregs; + iovec.iov_base = &fregs; + iovec.iov_len = sizeof (fregs); + if (ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec) != 0) + return false; + + Dwarf_Word dwarf_fregs[32]; + for (int r = 0; r < 32; r++) + dwarf_fregs[r] = fregs.fpr[r] & 0xFFFFFFFF; + + if (! setfunc (32, 32, dwarf_fregs, arg)) + return false; + + return true; +#endif /* __loongarch__ */ +} From patchwork Fri Apr 7 02:59:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Youling Tang X-Patchwork-Id: 67487 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4DBD83857C44 for ; Fri, 7 Apr 2023 03:00:30 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id C8F1D3857701 for ; Fri, 7 Apr 2023 03:00:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C8F1D3857701 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 loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8DxE0w3hy9k1K0XAA--.36973S3; Fri, 07 Apr 2023 11:00:07 +0800 (CST) Received: from bogon.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxwOQQhy9kEc0XAA--.59209S5; Fri, 07 Apr 2023 11:00:06 +0800 (CST) From: Youling Tang To: elfutils-devel@sourceware.org, Mark@sourceware.org, Wielaard@sourceware.org, mark@klomp.org Cc: Hengqi Chen , Liwei Ge Subject: [PATCH 3/5] backends: Add initial return value location support for LoongArch Date: Fri, 7 Apr 2023 10:59:26 +0800 Message-Id: <1680836368-5373-4-git-send-email-tangyouling@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> References: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> X-CM-TRANSID: AQAAf8DxwOQQhy9kEc0XAA--.59209S5 X-CM-SenderInfo: 5wdqw5prxox03j6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoWxuFyDZrW5JrW5ZF47CF43GFg_yoW3XF4UpF WfurW5GF1UCFsrK397Jw15Xr1rZw4fWF12qFySvF1Iyw15WrZav3s2yryj9ay8Gw4xGFyY vr43KayUuF4UAaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b7kYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4 x0Y4vEx4A2jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr1j6rxdM2AI xVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx1l5I8CrVACY4xI64 kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm 72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I 0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWU GVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI 0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0 rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r 4UJbIYCTnIWIevJa73UjIFyTuYvjxU2F4iUUUUU X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, TXREP 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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" LoongArch ELF ABI specification - Return values: https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_return_values Signed-off-by: Liwei Ge Signed-off-by: Youling Tang --- backends/ChangeLog | 6 ++ backends/Makefile.am | 2 +- backends/loongarch_init.c | 3 + backends/loongarch_retval.c | 202 ++++++++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 backends/loongarch_retval.c diff --git a/backends/ChangeLog b/backends/ChangeLog index 85abc8a5..40564ca7 100644 --- a/backends/ChangeLog +++ b/backends/ChangeLog @@ -1,3 +1,9 @@ +2023-04-07 Youling Tang + + * Makefile.am (loongarch_SRCS): Add loongarch_retval.c. + * loongarch_init.c (loongarch_init): Hook return_value_location. + * loongarch_retval.c: New file. + 2023-04-07 Youling Tang * Makefile.am (loongarch_SRCS): Add loongarch_initreg.c. diff --git a/backends/Makefile.am b/backends/Makefile.am index e7055d7e..9277ed59 100644 --- a/backends/Makefile.am +++ b/backends/Makefile.am @@ -97,7 +97,7 @@ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c \ csky_regs.c csky_initreg.c csky_corenote.c loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \ - loongarch_regs.c loongarch_initreg.c + loongarch_regs.c loongarch_initreg.c loongarch_retval.c arc_SRCS = arc_init.c arc_symbol.c diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c index 9c4b94a0..8892a2e6 100644 --- a/backends/loongarch_init.c +++ b/backends/loongarch_init.c @@ -1,5 +1,7 @@ /* Initialization of LoongArch specific backend library. Copyright (C) 2022 Hengqi Chen + Copyright (C) 2023 OpenAnolis community LoongArch SIG. + Copyright (C) 2023 Loongson Technology Corporation Limted. This file is part of elfutils. This file is free software; you can redistribute it and/or modify @@ -52,6 +54,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)), eh->frame_nregs = 74; HOOK (eh, check_special_symbol); HOOK (eh, set_initial_registers_tid); + HOOK (eh, return_value_location); return eh; } diff --git a/backends/loongarch_retval.c b/backends/loongarch_retval.c new file mode 100644 index 00000000..23b33ed5 --- /dev/null +++ b/backends/loongarch_retval.c @@ -0,0 +1,202 @@ +/* Function return value location for Linux/LoongArch ABI. + Copyright (C) 2013 Red Hat, Inc. + Copyright (C) 2023 OpenAnolis community LoongArch SIG. + Copyright (C) 2023 Loongson Technology Corporation Limited. + + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils 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 copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include + +#include +#include + +#define BACKEND loongarch_ +#include "libebl_CPU.h" + +static int +dwarf_bytesize_aux (Dwarf_Die *die, Dwarf_Word *sizep) +{ + int bits; + if (((bits = 8 * dwarf_bytesize (die)) < 0 + && (bits = dwarf_bitsize (die)) < 0) + || bits % 8 != 0) + return -1; + + *sizep = bits / 8; + return 0; +} + +static int +pass_in_gpr (const Dwarf_Op **locp, Dwarf_Word size) +{ + static const Dwarf_Op loc[] = + { + { .atom = DW_OP_reg4 }, { .atom = DW_OP_piece, .number = 8 }, + { .atom = DW_OP_reg5 }, { .atom = DW_OP_piece, .number = 8 } + }; + + *locp = loc; + return size <= 8 ? 1 : 4; +} + +static int +pass_by_ref (const Dwarf_Op **locp) +{ + static const Dwarf_Op loc[] = { { .atom = DW_OP_breg4 } }; + + *locp = loc; + return 1; +} + +static int +pass_in_fpr (const Dwarf_Op **locp, Dwarf_Word size) +{ + static const Dwarf_Op loc[] = + { + { .atom = DW_OP_regx, .number = 32 }, + { .atom = DW_OP_piece, .number = 8 }, + { .atom = DW_OP_regx, .number = 33 }, + { .atom = DW_OP_piece, .number = 8 } + }; + + *locp = loc; + return size <= 8 ? 1 : 4; +} + +int +loongarch_return_value_location(Dwarf_Die *functypedie, + const Dwarf_Op **locp) +{ + /* Start with the function's type, and get the DW_AT_type attribute, + which is the type of the return value. */ + Dwarf_Die typedie; + int tag = dwarf_peeled_die_type (functypedie, &typedie); + if (tag <= 0) + return tag; + + Dwarf_Word size = (Dwarf_Word)-1; + + /* If the argument type is a Composite Type that is larger than 16 + bytes, then the argument is copied to memory allocated by the + caller and the argument is replaced by a pointer to the copy. */ + if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type + || tag == DW_TAG_class_type || tag == DW_TAG_array_type) + { + if (dwarf_aggregate_size (&typedie, &size) < 0) + return -1; + + /* Aggregates larger than 2*GRLEN bits are passed by reference. */ + if (size > 16) + return pass_by_ref (locp); + /* Aggregates whose total size is no more than GRLEN bits are passed in + a register. Aggregates whose total size is no more than 2*GRLEN bits + are passed in a pair of registers. */ + else + return pass_in_gpr (locp, size); + } + + if (tag == DW_TAG_base_type || dwarf_is_pointer (tag)) + { + if (dwarf_bytesize_aux (&typedie, &size) < 0) + { + if (dwarf_is_pointer (tag)) + size = 8; + else + return -1; + } + + Dwarf_Attribute attr_mem; + if (tag == DW_TAG_base_type) + { + Dwarf_Word encoding; + if (dwarf_formudata (dwarf_attr_integrate (&typedie, DW_AT_encoding, + &attr_mem), + &encoding) != 0) + return -1; + + switch (encoding) + { + case DW_ATE_boolean: + case DW_ATE_signed: + case DW_ATE_unsigned: + case DW_ATE_unsigned_char: + case DW_ATE_signed_char: + /* Scalars that are at most GRLEN bits wide are passed in a single + argument register. Scalars that are 2*GRLEN bits wide are + passed in a pair of argument registers. Scalars wider than + 2*GRLEN are passed by reference. */ + return pass_in_gpr (locp, size); + + case DW_ATE_float: + /* A real floating-point argument is passed in a floating-point + argument register if it is no more than FLEN bits wide, + otherwise it is passed according to the integer calling + convention. */ + switch (size) + { + case 4: /* single */ + case 8: /* double */ + return pass_in_fpr (locp, size); + + case 16: /* quad */ + return pass_in_gpr (locp, size); + + default: + return -2; + } + + case DW_ATE_complex_float: + /* A complex floating-point number is passed as though it were a + struct containing two floating-point reals. */ + switch (size) + { + case 8: /* float _Complex */ + case 16: /* double _Complex */ + return pass_in_fpr (locp, size); + + case 32: /* long double _Complex */ + return pass_by_ref (locp); + + default: + return -2; + } + } + + return -2; + } + else + return pass_in_gpr (locp, size); + } + + *locp = NULL; + return 0; +} From patchwork Fri Apr 7 02:59:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Youling Tang X-Patchwork-Id: 67485 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 14EAD3857710 for ; Fri, 7 Apr 2023 03:00:19 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id F0C613857712 for ; Fri, 7 Apr 2023 03:00:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F0C613857712 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 loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8AxJDQ5hy9k2K0XAA--.37467S3; Fri, 07 Apr 2023 11:00:09 +0800 (CST) Received: from bogon.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxwOQQhy9kEc0XAA--.59209S6; Fri, 07 Apr 2023 11:00:08 +0800 (CST) From: Youling Tang To: elfutils-devel@sourceware.org, Mark@sourceware.org, Wielaard@sourceware.org, mark@klomp.org Cc: Hengqi Chen , Liwei Ge Subject: [PATCH 4/5] backends: Add frame pointer unwinding for LoongArch Date: Fri, 7 Apr 2023 10:59:27 +0800 Message-Id: <1680836368-5373-5-git-send-email-tangyouling@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> References: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> X-CM-TRANSID: AQAAf8DxwOQQhy9kEc0XAA--.59209S6 X-CM-SenderInfo: 5wdqw5prxox03j6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoWxXFyrZr4rCr4rZFyDAw4fKrg_yoW7Jr1fpF WYkr45Gr1kJFW2yas3J345Zrn8Xr93Gr12qF1Sga1xAw15Xa42v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=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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" If we don't find any debug information for a given frame, we usually cannot unwind any further. However, the binary in question might have been compiled with frame pointers, in which case we can look up the well known frame pointer locations in the stack snapshot and use them to bridge the frames without debug information. Signed-off-by: Liwei Ge Signed-off-by: Youling Tang --- backends/ChangeLog | 6 +++ backends/Makefile.am | 3 +- backends/loongarch_init.c | 1 + backends/loongarch_unwind.c | 84 +++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 backends/loongarch_unwind.c diff --git a/backends/ChangeLog b/backends/ChangeLog index 40564ca7..ae385fe0 100644 --- a/backends/ChangeLog +++ b/backends/ChangeLog @@ -1,3 +1,9 @@ +2023-04-07 Youling Tang + + * Makefile.am (loongarch_SRCS): Add loongarch_unwind.c. + * loongarch_init.c (loongarch_init): Hook unwind. + * loongarch_unwind.c: New file. + 2023-04-07 Youling Tang * Makefile.am (loongarch_SRCS): Add loongarch_retval.c. diff --git a/backends/Makefile.am b/backends/Makefile.am index 9277ed59..848e520c 100644 --- a/backends/Makefile.am +++ b/backends/Makefile.am @@ -97,7 +97,8 @@ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c \ csky_regs.c csky_initreg.c csky_corenote.c loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \ - loongarch_regs.c loongarch_initreg.c loongarch_retval.c + loongarch_regs.c loongarch_initreg.c loongarch_retval.c \ + loongarch_unwind.c arc_SRCS = arc_init.c arc_symbol.c diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c index 8892a2e6..808ff131 100644 --- a/backends/loongarch_init.c +++ b/backends/loongarch_init.c @@ -55,6 +55,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)), HOOK (eh, check_special_symbol); HOOK (eh, set_initial_registers_tid); HOOK (eh, return_value_location); + HOOK (eh, unwind); return eh; } diff --git a/backends/loongarch_unwind.c b/backends/loongarch_unwind.c new file mode 100644 index 00000000..fb748083 --- /dev/null +++ b/backends/loongarch_unwind.c @@ -0,0 +1,84 @@ +/* Get previous frame state for an existing frame state. + Copyright (C) 2023 OpenAnolis community LoongArch SIG. + Copyright (C) 2023 Loongson Technology Corporation Limited. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils 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 copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#define BACKEND loongarch_ +#define RA_REG 1 +#define SP_REG 3 +#define FP_REG 22 + +#define RA_OFFSET 8 +#define FP_OFFSET 16 + +#include "libebl_CPU.h" + +/* There was no CFI. Maybe we happen to have a frame pointer and can unwind from that? */ + +bool +EBLHOOK(unwind) (Ebl *ebl __attribute__ ((unused)), Dwarf_Addr pc __attribute__ ((unused)), + ebl_tid_registers_t *setfunc, ebl_tid_registers_get_t *getfunc, + ebl_pid_memory_read_t *readfunc, void *arg, + bool *signal_framep __attribute__ ((unused))) +{ + Dwarf_Word fp, ra, sp; + + if (!getfunc(RA_REG, 1, &ra, arg)) + return false; + + if (ra == 0 || !setfunc(-1, 1, &ra, arg)) + return false; + + if (!getfunc(FP_REG, 1, &fp, arg)) + fp = 0; + + if (!getfunc(SP_REG, 1, &sp, arg)) + sp = 0; + + Dwarf_Word newRa, newFp, newSp; + + if (!readfunc(fp - RA_OFFSET, &newRa, arg)) + newRa = 0; + + if (!readfunc(fp - FP_OFFSET, &newFp, arg)) + newFp = 0; + + newSp = fp; + + // These are not fatal if they don't work. They will just prevent unwinding at the next frame. + setfunc(RA_REG, 1, &newRa, arg); + setfunc(FP_REG, 1, &newFp, arg); + setfunc(SP_REG, 1, &newSp, arg); + + // If the fp is invalid, we might still have a valid ra. + // But if the fp is valid, then the stack should be moving in the right direction. + return fp == 0 || newSp > sp; +} From patchwork Fri Apr 7 02:59:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Youling Tang X-Patchwork-Id: 67486 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF0903857726 for ; Fri, 7 Apr 2023 03:00:24 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 8C6553858416 for ; Fri, 7 Apr 2023 03:00:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8C6553858416 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 loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxONk6hy9k2q0XAA--.36686S3; Fri, 07 Apr 2023 11:00:10 +0800 (CST) Received: from bogon.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxwOQQhy9kEc0XAA--.59209S7; Fri, 07 Apr 2023 11:00:09 +0800 (CST) From: Youling Tang To: elfutils-devel@sourceware.org, Mark@sourceware.org, Wielaard@sourceware.org, mark@klomp.org Cc: Hengqi Chen , Liwei Ge Subject: [PATCH 5/5] backends: Add core_note callback for LoongArch Date: Fri, 7 Apr 2023 10:59:28 +0800 Message-Id: <1680836368-5373-6-git-send-email-tangyouling@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> References: <1680836368-5373-1-git-send-email-tangyouling@loongson.cn> X-CM-TRANSID: AQAAf8DxwOQQhy9kEc0XAA--.59209S7 X-CM-SenderInfo: 5wdqw5prxox03j6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW3JFW7Xw43Jw4kAr4fCF13XFb_yoW7Wr48pF yUZr45Gr18XF4aka4kJ398XFn5J3Z5WF12v3WIqF40vryagry0vrW7KFW5ua4DJa97urWU uF4S9ayj9anxAF7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b7kYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_JrI_Jryl8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4 x0Y4vEx4A2jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr1j6rxdM2AI xVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx1l5I8CrVACY4xI64 kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm 72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I 0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWU GVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI 0_Xr0_Ar1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0 rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r 4UJbIYCTnIWIevJa73UjIFyTuYvjxUcHUqUUUUU X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, TXREP 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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" Signed-off-by: Youling Tang --- backends/ChangeLog | 6 ++ backends/Makefile.am | 2 +- backends/loongarch_corenote.c | 114 ++++++++++++++++++++++++++++++++++ backends/loongarch_init.c | 1 + 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 backends/loongarch_corenote.c diff --git a/backends/ChangeLog b/backends/ChangeLog index ae385fe0..4575c16b 100644 --- a/backends/ChangeLog +++ b/backends/ChangeLog @@ -1,3 +1,9 @@ +2023-04-07 Youling Tang + + * Makefile.am (loongarch_SRCS): Add loongarch_corenote.c. + * loongarch_init.c (loongarch_init): Hook core_note. + * loongarch_corenote.c: New file. + 2023-04-07 Youling Tang * Makefile.am (loongarch_SRCS): Add loongarch_unwind.c. diff --git a/backends/Makefile.am b/backends/Makefile.am index 848e520c..c8459128 100644 --- a/backends/Makefile.am +++ b/backends/Makefile.am @@ -98,7 +98,7 @@ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c \ loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \ loongarch_regs.c loongarch_initreg.c loongarch_retval.c \ - loongarch_unwind.c + loongarch_unwind.c loongarch_corenote.c arc_SRCS = arc_init.c arc_symbol.c diff --git a/backends/loongarch_corenote.c b/backends/loongarch_corenote.c new file mode 100644 index 00000000..b549edc0 --- /dev/null +++ b/backends/loongarch_corenote.c @@ -0,0 +1,114 @@ +/* LoongArch specific core note handling. + Copyright (C) 2023 Loongson Technology Corporation Limited. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils 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 copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include +#include + +#define BACKEND loongarch_ +#include "libebl_CPU.h" + +#define ULONG uint64_t +#define PID_T int32_t +#define UID_T uint32_t +#define GID_T uint32_t +#define ALIGN_ULONG 8 +#define ALIGN_PID_T 4 +#define ALIGN_UID_T 4 +#define ALIGN_GID_T 4 +#define TYPE_ULONG ELF_T_XWORD +#define TYPE_PID_T ELF_T_SWORD +#define TYPE_UID_T ELF_T_WORD +#define TYPE_GID_T ELF_T_WORD + +#define PRSTATUS_REGS_SIZE (45 * 8) + +static const Ebl_Register_Location prstatus_regs[] = + { + { .offset = 0, .regno = 0, .count = 32, .bits = 64 }, /* r0..r31 */ + }; + +#define PRSTATUS_REGSET_ITEMS \ + { \ + .name = "orig_a0", .type = ELF_T_XWORD, .format = 'x', \ + .offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \ + + 32 * 8), \ + .group = "register" \ + }, \ + { \ + .name = "csr_era", .type = ELF_T_XWORD, .format = 'x', \ + .offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \ + + 33 * 8), \ + .group = "register", \ + .pc_register = true \ + }, \ + { \ + .name = "csr_badvaddr", .type = ELF_T_XWORD, .format = 'x', \ + .offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \ + + 34 * 8), \ + .group = "register" \ + }, \ + { \ + .name = "csr_crmd", .type = ELF_T_XWORD, .format = 'x', \ + .offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \ + + 35 * 8), \ + .group = "register" \ + }, \ + { \ + .name = "csr_prmd", .type = ELF_T_XWORD, .format = 'x', \ + .offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \ + + 36 * 8), \ + .group = "register" \ + }, \ + { \ + .name = "csr_euen", .type = ELF_T_XWORD, .format = 'x', \ + .offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \ + + 37 * 8), \ + .group = "register" \ + }, \ + { \ + .name = "csr_ecfg", .type = ELF_T_XWORD, .format = 'x', \ + .offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \ + + 38 * 8), \ + .group = "register" \ + }, \ + { \ + .name = "csr_estat", .type = ELF_T_XWORD, .format = 'x', \ + .offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \ + + 39 * 8), \ + .group = "register" \ + } + /* 40 ~ 44 reserved */ + +#include "linux-core-note.c" diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c index 808ff131..2f0d46c9 100644 --- a/backends/loongarch_init.c +++ b/backends/loongarch_init.c @@ -49,6 +49,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)), HOOK (eh, reloc_simple_type); HOOK (eh, machine_flag_check); HOOK (eh, register_info); + HOOK (eh, core_note); HOOK (eh, abi_cfi); /* gcc/config/ #define DWARF_FRAME_REGISTERS. */ eh->frame_nregs = 74;