From patchwork Thu Nov 2 06:56:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ying Huang X-Patchwork-Id: 78909 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 E80183858C00 for ; Thu, 2 Nov 2023 06:56:54 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from s01.bc.larksuite.com (s01.bc.larksuite.com [209.127.230.14]) by sourceware.org (Postfix) with UTF8SMTPS id DA936385841B for ; Thu, 2 Nov 2023 06:56:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA936385841B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=oss.cipunited.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=oss.cipunited.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DA936385841B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.127.230.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698908202; cv=none; b=cDRkroNhlokQorZTHujHqhlSYQIf5jaJFXwlsc2dNl8k4iUODMhNYz9dcsZnRh6shE9BdZVuwHgLu+RW2ggqedUDaaF2MtwM2DXz+l0mfrkBdTVQ1kg3xeMxI0XeW6Q/XF49OEAs8uc6XGkgXb4WeZrXq/lPT1Ys/j8kwCtiYkw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698908202; c=relaxed/simple; bh=RdRVlY2ZXgvCksQ8slJ9xPz4C8qonYIwMXzoM8/t2MM=; h=DKIM-Signature:Mime-Version:Message-Id:From:Subject:To:Date; b=XBrRW0TMiXCsLK32Sa7wLOiLo7XDEvpksMGpUbb+G5svn1HBPLFN7+hvlNinr7UeBBKtcoTDNJre4wCVe0ATshdO1+MlN65ZVtmg5CQEpxzTFtW1/QIcN0Nucc36f2C3ocJCr+QnlmHi4mw0eSAXRsgWrZi8Bst2K5y8Rdz6Y0s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=feishu2303200042; d=oss.cipunited.com; t=1698908188; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=XnR+6iCW4FkWPigcdfAqjzoEyZMRuDUYWAtP9avqYIE=; b=os6wtVKlRarYCA0tBBovVeKAIG1y//jgclbp2qIk+P/tOuNRVB3LwwF61iIBtSZ4SiU4Fm zsOUTIDhkVoXDl7KdEGH1seI+Dns32+vwFVoklx2N4YUjmTGlBFDs+1S6zVHn3H27mAf1S lIpdpfkimDQSGaUVftx8g070CVEwW6mH1lPzr/LTaWDhRjN0Kq2Jt7k557B9n6SqrfuquH FIEmyaupqTcwZQglYwlgFNlHPhiky98/u/S0bnQgcKCNmC33+XgSy19pDb4pLtyCj0Ck9a AHpbga8eBKLugKWLfO8LsfVU3SZDVk8wMd2obWorDNxoZijy8jxdQyzxdrrFxQ== Mime-Version: 1.0 In-Reply-To: <20231102065602.3128526-1-ying.huang@oss.cipunited.com> X-Original-From: Ying Huang Message-Id: <20231102065602.3128526-6-ying.huang@oss.cipunited.com> X-Mailer: git-send-email 2.30.2 X-Lms-Return-Path: From: "Ying Huang" Subject: [PATCH v2 5/5] backends: Add register_info, return_value_location, core_note function on mips References: <20231102065602.3128526-1-ying.huang@oss.cipunited.com> To: Cc: , "Ying Huang" Date: Thu, 2 Nov 2023 14:56:02 +0800 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, NO_DNS_FOR_FROM, SPF_HELO_NONE, SPF_NONE, 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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org From: Ying Huang --- backends/Makefile.am | 3 +- backends/mips_corenote.c | 85 +++++++++++++++++ backends/mips_init.c | 3 + backends/mips_regs.c | 135 +++++++++++++++++++++++++++ backends/mips_retval.c | 196 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 421 insertions(+), 1 deletion(-) create mode 100644 backends/mips_corenote.c create mode 100644 backends/mips_regs.c create mode 100644 backends/mips_retval.c diff --git a/backends/Makefile.am b/backends/Makefile.am index 5e7b7f21..7c7f3351 100644 --- a/backends/Makefile.am +++ b/backends/Makefile.am @@ -103,7 +103,8 @@ loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \ arc_SRCS = arc_init.c arc_symbol.c mips_SRCS = mips_init.c mips_symbol.c mips_attrs.c mips_initreg.c \ - mips_cfi.c mips_unwind.c + mips_cfi.c mips_unwind.c mips_regs.c mips_retval.c \ + mips_corenote.c libebl_backends_a_SOURCES = $(i386_SRCS) $(sh_SRCS) $(x86_64_SRCS) \ $(ia64_SRCS) $(alpha_SRCS) $(arm_SRCS) \ diff --git a/backends/mips_corenote.c b/backends/mips_corenote.c new file mode 100644 index 00000000..44e30e3e --- /dev/null +++ b/backends/mips_corenote.c @@ -0,0 +1,85 @@ +/* MIPS specific core note handling. + Copyright (C) 2023 CIP United Inc. + 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 mips_ +#include "libebl_CPU.h" + +#define BITS 64 +#ifndef BITS +# define BITS 32 +#else +# define BITS 64 +#endif + +#define PRSTATUS_REGS_SIZE (45 * (BITS / 8)) +static const Ebl_Register_Location prstatus_regs[] = + { + { .offset = 0, .regno = 0, .count = (BITS == 32 ? 40 : 34), .bits = BITS }, + { .offset = BITS/8 * (BITS == 32 ? 41 : 35), .regno = (BITS == 32 ? 41 : 35), .count = (BITS == 32 ? 4 : 10), .bits = BITS }, + }; + +#define PRSTATUS_REGSET_ITEMS \ + { \ + .name = "pc", .type = ELF_T_ADDR, .format = 'x', \ + .offset = offsetof (struct EBLHOOK(prstatus), pr_reg) + ((BITS/8) * (BITS == 32 ? 40 : 34)), \ + .group = "register", \ + .pc_register = true \ + } + +#if BITS == 32 +# define ULONG uint32_t +# define ALIGN_ULONG 4 +# define TYPE_ULONG ELF_T_WORD +#define TYPE_LONG ELF_T_SWORD +#else +#define ULONG uint64_t +#define ALIGN_ULONG 8 +#define TYPE_ULONG ELF_T_XWORD +#define TYPE_LONG ELF_T_SXWORD +#endif +#define PID_T int32_t +#define UID_T uint32_t +#define GID_T uint32_t +#define ALIGN_PID_T 4 +#define ALIGN_UID_T 4 +#define ALIGN_GID_T 4 +#define TYPE_PID_T ELF_T_SWORD +#define TYPE_UID_T ELF_T_WORD +#define TYPE_GID_T ELF_T_WORD + +#include "linux-core-note.c" diff --git a/backends/mips_init.c b/backends/mips_init.c index a6ed8a47..e5617f0f 100644 --- a/backends/mips_init.c +++ b/backends/mips_init.c @@ -61,6 +61,9 @@ mips_init (Elf *elf __attribute__ ((unused)), HOOK (eh, set_initial_registers_tid); HOOK (eh, abi_cfi); HOOK (eh, unwind); + HOOK (eh, register_info); + HOOK (eh, return_value_location); + HOOK (eh, core_note); eh->frame_nregs = 71; return eh; } diff --git a/backends/mips_regs.c b/backends/mips_regs.c new file mode 100644 index 00000000..28fe7cae --- /dev/null +++ b/backends/mips_regs.c @@ -0,0 +1,135 @@ +/* Register names and numbers for mips DWARF. + Copyright (C) 2006 Red Hat, Inc. + Copyright (C) 2023 CIP United Inc. + 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 + +#define BACKEND mips_ +#include "libebl_CPU.h" +#include +ssize_t +mips_register_info (Ebl *ebl __attribute__ ((unused)), + int regno, char *name, size_t namelen, + const char **prefix, const char **setname, + int *bits, int *type) +{ + if (name == NULL) + return 72; + + if (regno < 0 || regno > 71 || namelen < 4) + return -1; + + *prefix = "$"; + if (regno < 38) + { + *setname = "integer"; + *type = DW_ATE_signed; + *bits = 32; + } + else + { + *setname = "FPU"; + *type = DW_ATE_float; + *bits = 64; + } + + if (regno < 32) + { + if (regno < 10) + { + name[0] = regno + '0'; + namelen = 1; + } + else + { + name[0] = (regno / 10) + '0'; + name[1] = (regno % 10) + '0'; + namelen = 2; + } + if (regno == 28 || regno == 29 || regno == 31) + *type = DW_ATE_address; + } + else if (regno == 32) + { + return stpcpy (name, "lo") + 1 - name; + } + else if (regno == 33) + { + return stpcpy (name, "hi") + 1 - name; + } + else if (regno == 34) + { + return stpcpy (name, "pc") + 1 - name; + } + else if (regno == 35) + { + *type = DW_ATE_address; + return stpcpy (name, "bad") + 1 - name; + } + else if (regno == 36) + { + return stpcpy (name, "sr") + 1 - name; + } + else if (regno == 37) + { + *type = DW_ATE_address; + return stpcpy (name, "cause") + 1 - name; + } + else if (regno < 70) + { + name[0] = 'f'; + if (regno < 38 + 10) + { + name[1] = (regno - 38) + '0'; + namelen = 2; + } + else + { + name[1] = (regno - 38) / 10 + '0'; + name[2] = (regno - 38) % 10 + '0'; + namelen = 3; + } + } + else if (regno == 70) + { + return stpcpy (name, "fsr") + 1 - name; + } + else if (regno == 71) + { + return stpcpy (name, "fir") + 1 - name; + } + + name[namelen++] = '\0'; + return namelen; +} diff --git a/backends/mips_retval.c b/backends/mips_retval.c new file mode 100644 index 00000000..5984d072 --- /dev/null +++ b/backends/mips_retval.c @@ -0,0 +1,196 @@ +/* Function return value location for Linux/mips ABI. + Copyright (C) 2005 Red Hat, Inc. + Copyright (C) 2023 CIP United Inc. + 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 mips_ +#include "libebl_CPU.h" +#include "libdwP.h" +#include + +/* $v0 or pair $v0, $v1 */ +static const Dwarf_Op loc_intreg_o32[] = + { + { .atom = DW_OP_reg2 }, { .atom = DW_OP_piece, .number = 4 }, + { .atom = DW_OP_reg3 }, { .atom = DW_OP_piece, .number = 4 }, + }; + +static const Dwarf_Op loc_intreg[] = + { + { .atom = DW_OP_reg2 }, { .atom = DW_OP_piece, .number = 8 }, + { .atom = DW_OP_reg3 }, { .atom = DW_OP_piece, .number = 8 }, + }; +#define nloc_intreg 1 +#define nloc_intregpair 4 + +/* $f0 (float), or pair $f0, $f1 (double). + * f2/f3 are used for COMPLEX (= 2 doubles) returns in Fortran */ +static const Dwarf_Op loc_fpreg_o32[] = + { + { .atom = DW_OP_regx, .number = 32 }, { .atom = DW_OP_piece, .number = 4 }, + { .atom = DW_OP_regx, .number = 33 }, { .atom = DW_OP_piece, .number = 4 }, + { .atom = DW_OP_regx, .number = 34 }, { .atom = DW_OP_piece, .number = 4 }, + { .atom = DW_OP_regx, .number = 35 }, { .atom = DW_OP_piece, .number = 4 }, + }; + +/* $f0, or pair $f0, $f2. */ +static const Dwarf_Op loc_fpreg[] = + { + { .atom = DW_OP_regx, .number = 32 }, { .atom = DW_OP_piece, .number = 8 }, + { .atom = DW_OP_regx, .number = 34 }, { .atom = DW_OP_piece, .number = 8 }, + }; +#define nloc_fpreg 1 +#define nloc_fpregpair 4 +#define nloc_fpregquad 8 + +/* The return value is a structure and is actually stored in stack space + passed in a hidden argument by the caller. But, the compiler + helpfully returns the address of that space in $v0. */ +static const Dwarf_Op loc_aggregate[] = + { + { .atom = DW_OP_breg2, .number = 0 } + }; +#define nloc_aggregate 1 + +int +mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) +{ + unsigned int regsize = (gelf_getclass (functypedie->cu->dbg->elf) == ELFCLASS32 ) ? 4 : 8; + if (!regsize) + return -2; + + /* Start with the function's type, and get the DW_AT_type attribute, + which is the type of the return value. */ + + Dwarf_Attribute attr_mem; + Dwarf_Attribute *attr = dwarf_attr_integrate (functypedie, DW_AT_type, &attr_mem); + if (attr == NULL) + /* The function has no return value, like a `void' function in C. */ + return 0; + + Dwarf_Die die_mem; + Dwarf_Die *typedie = dwarf_formref_die (attr, &die_mem); + int tag = dwarf_tag (typedie); + + /* Follow typedefs and qualifiers to get to the actual type. */ + while (tag == DW_TAG_typedef + || tag == DW_TAG_const_type || tag == DW_TAG_volatile_type + || tag == DW_TAG_restrict_type) + { + attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem); + typedie = dwarf_formref_die (attr, &die_mem); + tag = dwarf_tag (typedie); + } + + switch (tag) + { + case -1: + return -1; + + case DW_TAG_subrange_type: + if (! dwarf_hasattr_integrate (typedie, DW_AT_byte_size)) + { + attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem); + typedie = dwarf_formref_die (attr, &die_mem); + tag = dwarf_tag (typedie); + } + /* Fall through. */ + FALLTHROUGH; + + case DW_TAG_base_type: + case DW_TAG_enumeration_type: + CASE_POINTER: + { + Dwarf_Word size; + if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size, + &attr_mem), &size) != 0) + { + if (dwarf_is_pointer (tag)) + size = regsize; + else + return -1; + } + 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; + +#define ARCH_LOC(loc, regsize) ((regsize) == 4 ? (loc ## _o32) : (loc)) + + if (encoding == DW_ATE_float) + { + *locp = ARCH_LOC(loc_fpreg, regsize); + if (size <= regsize) + return nloc_fpreg; + + if (size <= 2*regsize) + return nloc_fpregpair; + + if (size <= 4*regsize) + return nloc_fpregquad; + + goto aggregate; + } + } + *locp = ARCH_LOC(loc_intreg, regsize); + if (size <= regsize) + return nloc_intreg; + if (size <= 2*regsize) + return nloc_intregpair; + + /* Else fall through. Shouldn't happen though (at least with gcc) */ + } + FALLTHROUGH; + + case DW_TAG_structure_type: + case DW_TAG_class_type: + case DW_TAG_union_type: + case DW_TAG_array_type: + aggregate: + *locp = loc_aggregate; + return nloc_aggregate; + case DW_TAG_unspecified_type: + return 0; + } + + /* XXX We don't have a good way to return specific errors from ebl calls. + This value means we do not understand the type, but it is well-formed + DWARF and might be valid. */ + return -2; +}