From patchwork Mon Aug 9 03:47:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hsiangkai Wang X-Patchwork-Id: 44606 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 A0EA43839410 for ; Mon, 9 Aug 2021 03:48:46 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id 9BCC9383F403 for ; Mon, 9 Aug 2021 03:48:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9BCC9383F403 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pj1-x1029.google.com with SMTP id w13-20020a17090aea0db029017897a5f7bcso4320059pjy.5 for ; Sun, 08 Aug 2021 20:48:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id; bh=D+Ks/b7ZkGOHgXW/VqAFG/quNOO4acIqvrWBsss/UtQ=; b=DCQV5Dp/ZXpXgzUX2XdIFRikZ0DOE8KKfl1+C9QVvTjWM1K5P8iBINxYodzP1n0bPa hQ8W3NKWf1i56bwskdL1WDkC4QemMGina1FzrDVk1hV7N410lbG9BBJ1FnYpF2NsjI0Y 7mkehgvQGLAyw2EeYWHB7YQkuWRf2TJ9KUS28oLtIzjijqZSxVHCX62IjeNf55wM/lKM i0ORHEjh0Fg6ZbNb5+AsA3OD9+GGm5Y73C33g5WBo00CuQh/7Dy+E55ZQCp2YrkBOEv/ L0APbA+qeAeDXlRreNnG02DzBu/aR7ktW6llv/RNsVoZNkHdSg4yKetzRAqPVyPi6tDz Dy+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=D+Ks/b7ZkGOHgXW/VqAFG/quNOO4acIqvrWBsss/UtQ=; b=iTmn7aHQp66dAsUJHoRxIRL1ZYPR11J+1Y0FIz6BFR+le8LWfFdnEcct/rKNbOaVaW OrtjlLzhDNPnEqIu1gFPVGTBistXC9UEMuAwQ5+o8HBIjFcgGjDf9KWQJe3FOZJMwTA0 XVBgjKzWQc5NqeiWSxbYy3bGFAT1xKuEckISqTxYRTs0jZoOqvaz5wCU5Qx/XijmREFd tmm7NZB38SENCW5YP7yRLkvLiuZmItf92NGDBd9FfkgfEmti83Z9WyW/o48YE9nOt0QP G5zBKaQOEV7e/MdPDMCdmvUQoPXetOeepqGz727UNxmIZ/IG+UdXfg5bgWrHVKGTg0KA HqIA== X-Gm-Message-State: AOAM531HcHZjnc5ZZ/9kIXQuFxJZ8ennEabROuLb7mJBJVxs7jsiIeWM pI71ig2LiLs3aw/8WBNx4E9yOiIkUEqFEg== X-Google-Smtp-Source: ABdhPJyUxkJeGyR+gw4fnvZAdv87TM7CN1ZrBK7dTF3v6EnboNZdTN2K+k8nL6JpN0DOYP6aEsGsOA== X-Received: by 2002:a17:90b:164b:: with SMTP id il11mr22695746pjb.47.1628480912453; Sun, 08 Aug 2021 20:48:32 -0700 (PDT) Received: from gamma24.internal.sifive.com ([64.62.193.194]) by smtp.gmail.com with ESMTPSA id o127sm18574588pfb.48.2021.08.08.20.48.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Aug 2021 20:48:31 -0700 (PDT) From: Hsiangkai Wang To: libc-alpha@sourceware.org, darius@bluespec.com, palmer@dabbelt.com, andrew@sifive.com, dj@redhat.com, kito.cheng@sifive.com, jrtc27@jrtc27.com Subject: [PATCH] riscv: Resolve symbols directly for symbols with STO_RISCV_VARIANT_CC. Date: Mon, 9 Aug 2021 11:47:07 +0800 Message-Id: <1628480827-366232-1-git-send-email-kai.wang@sifive.com> X-Mailer: git-send-email 2.7.4 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Cc: Hsiangkai Wang Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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. The related discussion could be found in https://github.com/riscv/riscv-elf-psabi-doc/pull/190. --- elf/elf.h | 7 +++++++ sysdeps/riscv/dl-dtprocnum.h | 21 +++++++++++++++++++++ sysdeps/riscv/dl-machine.h | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 sysdeps/riscv/dl-dtprocnum.h diff --git a/elf/elf.h b/elf/elf.h index 4738dfa..0de29bf 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -3889,6 +3889,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/sysdeps/riscv/dl-dtprocnum.h b/sysdeps/riscv/dl-dtprocnum.h new file mode 100644 index 0000000..f189fd7 --- /dev/null +++ b/sysdeps/riscv/dl-dtprocnum.h @@ -0,0 +1,21 @@ +/* Configuration of lookup functions. RISC-V version. + Copyright (C) 2019-2021 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 aedf69f..6488483 100644 --- a/sysdeps/riscv/dl-machine.h +++ b/sysdeps/riscv/dl-machine.h @@ -54,6 +54,9 @@ #define ELF_MACHINE_NO_REL 1 #define ELF_MACHINE_NO_RELA 0 +/* 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) @@ -299,6 +302,29 @@ elf_machine_lazy_rel (struct link_map *map, ElfW(Addr) l_addr, /* 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, reloc, sym, version, reloc_addr, + skip_ifunc); + return; + } + } + if (__glibc_unlikely (map->l_mach.plt == 0)) { if (l_addr)