From patchwork Fri Apr 28 14:04:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Palmer Dabbelt X-Patchwork-Id: 68496 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 A16B13857720 for ; Fri, 28 Apr 2023 14:05:24 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 410503858413 for ; Fri, 28 Apr 2023 14:04:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 410503858413 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-63b60365f53so29813b3a.0 for ; Fri, 28 Apr 2023 07:04:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1682690683; x=1685282683; h=to:from:cc:content-transfer-encoding:mime-version:message-id:date :subject:from:to:cc:subject:date:message-id:reply-to; bh=LZfh5pJWN3705MJyxcCCyme14OimCU61YCK0wQLN/iI=; b=Y3Y6PVd1fd8pgiDIT0Q4cKe49/fJeB2nPKF00XNaNJ94zluSKJ2N0QIeQDy+N/LwH0 H2C6C5crhOj8oDksmqSv+uiDgvYJ6k8V1DY2FeqeDJ5bN8Xgv6HbMm8NtfP0UgXyxUeR vOjFEUyod/ZZnvjI/Xc7JGuqG7bXVQsofGNcKFo5MyfLWT3qQGNtjPcEoeM06LVKYM+i PJffP4EpkwCa4/0SUieXBfMYajzjeU3IHfZ/Q0zIIeCay0HL7qMrW+XBMSYvnj/Azx1S jCP2M9dfkgZXqp+LzyXNEy61eGUwwDVLkWctLYABAU9/7v+2ClrU+LiB5Ig94xxC0VHo Zvsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682690683; x=1685282683; h=to:from:cc:content-transfer-encoding:mime-version:message-id:date :subject:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LZfh5pJWN3705MJyxcCCyme14OimCU61YCK0wQLN/iI=; b=kKQizfaWGnD+tXn9OzpnGaWME640i3uGxiaT2wkUVMvAsTlULylRdFz/L8XG8Fsfvu 3bsVxJ5d3Bj7hi27r/4nkIT3LBlFXHOPIwK3uxrXIsPbfxD3haaxVWxqK5FLFyFgndui veAzfZXYbwDJ2jajEsn/PhuAt3EEyKXEMnis5LL0RD3fp6OhnM8l3Alj5XLTlurYzEqp uCfWLJGb63puyTqHg2rX8s2OipyZuct5d9XlCEnWDKIjzZXFlC9990rh2wrmuRVaNftn UAe7y4M+38QHTWUUiigSXZbib3FjVj7gfov1PqlTdyripaTsMqiivsDbdu+CttAcf44E pBQg== X-Gm-Message-State: AC+VfDwXxwikpU/So69Bsj3Fz198yFcVcvK7F4zkVjsA/mRcJH2FAA6l jt9xZwEllZ7yL5rjWHGesWzq1a6/xEtxcFmuGgg= X-Google-Smtp-Source: ACHHUZ4UL5AFOjJVuxXTWx/LQGAeCsiODQixhRqw/Vh0l5xaIPsUn9XWh7gLyajRnMKqqZ4C+svUBQ== X-Received: by 2002:a05:6a00:2383:b0:63d:315f:560f with SMTP id f3-20020a056a00238300b0063d315f560fmr8282125pfc.13.1682690682898; Fri, 28 Apr 2023 07:04:42 -0700 (PDT) Received: from localhost ([135.180.227.0]) by smtp.gmail.com with ESMTPSA id y72-20020a62644b000000b006372791d708sm15239504pfb.104.2023.04.28.07.04.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 07:04:42 -0700 (PDT) Subject: [COMMITTED 1/2] riscv: Resolve symbols directly for symbols with STO_RISCV_VARIANT_CC. Date: Fri, 28 Apr 2023 07:04:26 -0700 Message-Id: <20230428140427.13133-1-palmer@rivosinc.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Cc: Hsiangkai Wang , Vincent Chen , Palmer Dabbelt From: Palmer Dabbelt To: libc-alpha@sourceware.org X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Hsiangkai Wang In some cases, we do not want to go through the resolver for function calls. For example, functions with vector arguments will use vector registers to pass arguments. In the resolver, we do not save/restore the vector argument registers for lazy binding efficiency. To avoid ruining the vector arguments, functions with vector arguments will not go through the resolver. To achieve the goal, we will annotate the function symbols with STO_RISCV_VARIANT_CC flag and add DT_RISCV_VARIANT_CC tag in the dynamic section. In the first pass on PLT relocations, we do not set up to call _dl_runtime_resolve. Instead, we resolve the functions directly. Signed-off-by: Hsiangkai Wang Signed-off-by: Vincent Chen Reviewed-by: Palmer Dabbelt Acked-by: Palmer Dabbelt Link: https://inbox.sourceware.org/libc-alpha/20230314162512.35802-1-kito.cheng@sifive.com Signed-off-by: Palmer Dabbelt --- elf/elf.h | 7 +++++++ manual/platform.texi | 6 ++++++ sysdeps/riscv/dl-dtprocnum.h | 21 +++++++++++++++++++++ sysdeps/riscv/dl-machine.h | 26 ++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 sysdeps/riscv/dl-dtprocnum.h diff --git a/elf/elf.h b/elf/elf.h index 94ca23c1bb..4f65b5a32d 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -3933,6 +3933,13 @@ enum #define R_TILEGX_NUM 130 +/* RISC-V specific values for the Dyn d_tag field. */ +#define DT_RISCV_VARIANT_CC (DT_LOPROC + 1) +#define DT_RISCV_NUM 2 + +/* RISC-V specific values for the st_other field. */ +#define STO_RISCV_VARIANT_CC 0x80 + /* RISC-V ELF Flags */ #define EF_RISCV_RVC 0x0001 #define EF_RISCV_FLOAT_ABI 0x0006 diff --git a/manual/platform.texi b/manual/platform.texi index c6ed73cb97..714c07813f 100644 --- a/manual/platform.texi +++ b/manual/platform.texi @@ -121,6 +121,12 @@ when it is not allowed, the priority is set to medium. @node RISC-V @appendixsec RISC-V-specific Facilities +Functions that are lazily bound must be compatible with the standard calling +convention. When a function is annotated with STO_RISCV_VARIANT_CC, it means +this function is not compatible with the standard calling convention. The +dynamic linker will directly resolve it instead of using the lazy binding +mechanism. + Cache management facilities specific to RISC-V systems that implement the Linux ABI are declared in @file{sys/cachectl.h}. diff --git a/sysdeps/riscv/dl-dtprocnum.h b/sysdeps/riscv/dl-dtprocnum.h new file mode 100644 index 0000000000..281c5aadeb --- /dev/null +++ b/sysdeps/riscv/dl-dtprocnum.h @@ -0,0 +1,21 @@ +/* Configuration of lookup functions. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +/* Number of extra dynamic section entries for this architecture. By + default there are none. */ +#define DT_THISPROCNUM DT_RISCV_NUM diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h index c0c9bd93ad..0e6c0bb155 100644 --- a/sysdeps/riscv/dl-machine.h +++ b/sysdeps/riscv/dl-machine.h @@ -53,6 +53,9 @@ || (__WORDSIZE == 64 && (type) == R_RISCV_TLS_TPREL64))) \ | (ELF_RTYPE_CLASS_COPY * ((type) == R_RISCV_COPY))) +//* Translate a processor specific dynamic tag to the index in l_info array. */ +#define DT_RISCV(x) (DT_RISCV_##x - DT_LOPROC + DT_NUM) + /* Return nonzero iff ELF header is compatible with the running host. */ static inline int __attribute_used__ elf_machine_matches_host (const ElfW(Ehdr) *ehdr) @@ -281,6 +284,29 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[], /* Check for unexpected PLT reloc type. */ if (__glibc_likely (r_type == R_RISCV_JUMP_SLOT)) { + if (__glibc_unlikely (map->l_info[DT_RISCV (VARIANT_CC)] != NULL)) + { + /* Check the symbol table for variant CC symbols. */ + const Elf_Symndx symndx = ELFW(R_SYM) (reloc->r_info); + const ElfW(Sym) *symtab = + (const void *)D_PTR (map, l_info[DT_SYMTAB]); + const ElfW(Sym) *sym = &symtab[symndx]; + if (__glibc_unlikely (sym->st_other & STO_RISCV_VARIANT_CC)) + { + /* Avoid lazy resolution of variant CC symbols. */ + const struct r_found_version *version = NULL; + if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL) + { + const ElfW(Half) *vernum = + (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]); + version = &map->l_versions[vernum[symndx] & 0x7fff]; + } + elf_machine_rela (map, scope, reloc, sym, version, reloc_addr, + skip_ifunc); + return; + } + } + if (__glibc_unlikely (map->l_mach.plt == 0)) { if (l_addr) From patchwork Fri Apr 28 14:04:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Palmer Dabbelt X-Patchwork-Id: 68495 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 3448D3857355 for ; Fri, 28 Apr 2023 14:05:02 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 71DAB3858C52 for ; Fri, 28 Apr 2023 14:04:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 71DAB3858C52 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1a67bcde3a7so100573965ad.3 for ; Fri, 28 Apr 2023 07:04:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1682690684; x=1685282684; h=to:from:cc:content-transfer-encoding:mime-version:references :in-reply-to:message-id:date:subject:from:to:cc:subject:date :message-id:reply-to; bh=bVdkek8hAYIjikW4VdY/rOdIXFjs1xc+zcgVoZbIsKc=; b=Y26paR6kI61YrX+xa8ODV85oVzjPw34j8qIBjBaZi3bqfv0dvgSXYSjU2qpyvmO5uk ulCJDfy4/8Z/WB1LqWTR0sKA7tvqgUp0c0wUTWwRKHjYxE0jNTdp6u3sHvndxuHa89dH oaFXhAMSFjT+htKIHdzm3mU5BA8EfiS8DCxoYhxaC6VKMgtmaJahO0M2AL3+7QVJmguo DfeR4kflTCjPyj1stNSsgMnEQAQp9bEuO9+0pujTfEw8TayLQJ5Zsk11NsMP7ixu1Ppx sLlTbEp2hz3kRe3mI9RWqUQH8WEeiD7gTrmhScuknChONREcicBnRSsLfMsjlfVJbVF2 a5YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682690684; x=1685282684; h=to:from:cc:content-transfer-encoding:mime-version:references :in-reply-to:message-id:date:subject:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bVdkek8hAYIjikW4VdY/rOdIXFjs1xc+zcgVoZbIsKc=; b=UV8RTdro5adELUjL67L2yMiAlO+vUbWb3vUgogizeJeLJ46NOY7JayvWhKA2jkr3qi nAYNtV408L94hmwrh9o9n9tcvrKWlQi453llXwtkDuec2uudqbQaRr+WT3tWMFtQV6rA lDQY9RfmdqCT2RVtFdUCOHluVaXc4Ra4SDw35tzn+avR/1wuQpIj6JCk2J2cRij/I2zG TyeE4IZ9bvlvF6ITmHYFG9kr2FEB1uxMPc4kA9V0NZyduCouS05ePW6bWJq+19ImQ34H wA43kfRw6oUDtjwQwcq/LGNot25YKIfC0SWbVyp3LRfHGghKBvhdkb9S3kFXMde5Ins/ xZoA== X-Gm-Message-State: AC+VfDwuyrlaTj4OQUsZrgiNb45ihGJlzh/oWFbIpVWCpprC0UbR6vYH s7WdXvN9dsjz3EMJIrz0bLHFcFRjL9419XJq+GI= X-Google-Smtp-Source: ACHHUZ4WxOeok/nG2p/dgLCtV93zKCp2Mm6P50+Lic+OLeLP2+r12OuYII9k6bL+C9GX+2k1kZB9EQ== X-Received: by 2002:a17:902:7208:b0:1a5:1b94:e63d with SMTP id ba8-20020a170902720800b001a51b94e63dmr4929510plb.65.1682690684361; Fri, 28 Apr 2023 07:04:44 -0700 (PDT) Received: from localhost ([135.180.227.0]) by smtp.gmail.com with ESMTPSA id q16-20020a170902bd9000b001a6dc4f4a8csm9277015pls.73.2023.04.28.07.04.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 07:04:43 -0700 (PDT) Subject: [COMMITTED 2/2] Remap __GLIBC_FLT_EVAL_METHOD to 0 if __FLT_EVAL_METHOD__ is -1 Date: Fri, 28 Apr 2023 07:04:27 -0700 Message-Id: <20230428140427.13133-2-palmer@rivosinc.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230428140427.13133-1-palmer@rivosinc.com> References: <20230428140427.13133-1-palmer@rivosinc.com> MIME-Version: 1.0 Cc: Kito Cheng , Palmer Dabbelt , lco=20Dijkstra=C2=A0?= From: Palmer Dabbelt To: libc-alpha@sourceware.org X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Kito Cheng __GLIBC_FLT_EVAL_METHOD will effect the definition of float_t and double_t, currently we'll set __GLIBC_FLT_EVAL_METHOD to 2 when __FLT_EVAL_METHOD__ is -1, that means we'll define float_t and double_t to long double. However some target isn't natively (HW) support long double like AArch64 and RISC-V, they defined long double as 128-bits IEEE 754 floating point type. That means setting __GLIBC_FLT_EVAL_METHOD to 2 will cause very inefficient code gen for those target who didn't provide native support for long double, and that's violate the spirit float_t and double_t - most efficient types at least as wide as float and double. So this patch propose to remap __GLIBC_FLT_EVAL_METHOD to 0 rather than 2 when __FLT_EVAL_METHOD__ is -1, which means we'll use float/double rather than long double for float_t and double_t. Note: __FLT_EVAL_METHOD__ == -1 means the precision is indeterminable, which means compiler might using indeterminable precision during optimization/code gen, clang will set this value to -1 when fast math is enabled. Note: Default definition float_t and double_t in current glibc: | __GLIBC_FLT_EVAL_METHOD | float_t | double_t | 0 or 16 | float | double | 1 | double | doulbe | 2 | long double | long double More complete list see math/math.h Note: RISC-V has defined ISA extension to support 128-bits IEEE 754 floating point operations, but only rare RISC-V core will implement that. Related link: [1] LLVM issue (__FLT_EVAL_METHOD__ is set to -1 with Ofast. #60781): https://github.com/llvm/llvm-project/issues/60781 [2] Last version of this patch: https://sourceware.org/pipermail/libc-alpha/2023-February/145622.html Acked-by: Palmer Dabbelt # RISC-V Reviewed-by: Wilco Dijkstra  Link: https://inbox.sourceware.org/libc-alpha/20230314151948.12892-1-kito.cheng@sifive.com Signed-off-by: Palmer Dabbelt --- bits/flt-eval-method.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bits/flt-eval-method.h b/bits/flt-eval-method.h index 75f57b9a0e..f9262d7d0b 100644 --- a/bits/flt-eval-method.h +++ b/bits/flt-eval-method.h @@ -26,14 +26,12 @@ -1. */ /* In the default version of this header, follow __FLT_EVAL_METHOD__. - -1 is mapped to 2 (considering evaluation as long double to be a - conservatively safe assumption), and if __FLT_EVAL_METHOD__ is not - defined then assume there is no excess precision and use the value - 0. */ + If __FLT_EVAL_METHOD__ is not defined or set to -1, assume there is no + excess precision and use the value 0 (this is correct for most targets). */ #ifdef __FLT_EVAL_METHOD__ # if __FLT_EVAL_METHOD__ == -1 -# define __GLIBC_FLT_EVAL_METHOD 2 +# define __GLIBC_FLT_EVAL_METHOD 0 # else # define __GLIBC_FLT_EVAL_METHOD __FLT_EVAL_METHOD__ # endif