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 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 Wielaard 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 List-Id: 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; +} -- 2.37.1