From nobody Fri Oct 18 18:15:44 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XVXv456X4z5ZWpr; Fri, 18 Oct 2024 18:15:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XVXv44YSgz41Lm; Fri, 18 Oct 2024 18:15:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729275344; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6UM5bJf3wV+HYalXjAjCl/ORQOnaHzSNfs/hY2X5I/s=; b=c1k+vi3L8vJx/TOc79Jo8tF0J/sPhkBQtMnWmypOvzMmLMd223d2YZOT17Z+cIusf5pe4u IOLkpAa5FEGf8vI1UI8m3+Mh4X1VQp2Ha3rR1kbpqxOOpFbk6UGYi66gbDOWK74r88Q8r7 B3XnHkW+HigQHmUnblBUsnZ8g+JXYBACPkVJW9WYkx/Me6WevsqYhKGIUhcX6VM8NfsZOj LA/J8B0/FSVBTUm+ZXLTWOgaAYvUQ9fZCkyckmimX490sSZvaAByDeIA/taZmjRmw0dn+m Wf6BJxquyqCt5XvBmkCu+msfdjsWbXj81XJC9WSCv+bZBn+u8lsWFWSnfARC9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729275344; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6UM5bJf3wV+HYalXjAjCl/ORQOnaHzSNfs/hY2X5I/s=; b=HnzdepUhooDeK8Keto5UktXZFrriVc8+q5jbLeb4zdu5KTQBcO3Dd6gH3wTrTX2g2k0npB H/am5Nq6edI+BpY4w7yPigl0MzYWG1NdV0bhcC0N8kP9sovBTDHCsV3JIy+94ZVGeOtTJ8 kbgIDxXMFglMtxp/Ds6Wm/gD46hN4AVmjSc19MM5bg74aNdZRbZVuBHdB/UyN+++XUhbnE lYb6yOIWOeV7CX0WoE4sL1VPKS4eggRF6O4uEzFMNg2rIX/LxX+//GZx8fpj+halHmWtTB 0HCWtXGgfLwNrEwzySYCFDVnPyzRHUGTQzrCxvhkUhRwOFgbBlFuH+OAP4puBg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729275344; a=rsa-sha256; cv=none; b=x7Y63X4wLq63tb5Gt8H6XH9M6xERL+DoBVnqKsLDnmPRQ912ltTiSedUN2/A6uGMUlMZwI Hl+ZSoOkPSm/smGp5dCbbO3FGHeTbbVw5Ui3PY2TeUlHwd2OvNAeaMDz32g/dx27SpS3RY 2Hj54B1vUvBAm9WSJaobNjtbtG67zqx10ATKTRHsx1yhfRcQS/ZRqqknFwcci2lZ67ABSj 17f/7Sd3mrvxhsLGVBMvuCnYEmyjQh7WwVQkS7KCOmKURTGJJJicovMTWXvtZaAwBRheuH nuK/VpyvEOxuEJ0quM+D2a/wclp6AHE99AVeH0Xv4xb+q2VzJJHrfRjlRxi4PQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XVXv43xYtzkqZ; Fri, 18 Oct 2024 18:15:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49IIFi6F055647; Fri, 18 Oct 2024 18:15:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49IIFiKi055644; Fri, 18 Oct 2024 18:15:44 GMT (envelope-from git) Date: Fri, 18 Oct 2024 18:15:44 GMT Message-Id: <202410181815.49IIFiKi055644@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jessica Clarke Subject: git: 1363acbf25de - main - libc/csu: Support IFUNCs on riscv List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1363acbf25de4c36e183cfa0b0e801d4dd9bf2ad Auto-Submitted: auto-generated The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=1363acbf25de4c36e183cfa0b0e801d4dd9bf2ad commit 1363acbf25de4c36e183cfa0b0e801d4dd9bf2ad Author: Jessica Clarke AuthorDate: 2024-10-18 18:14:58 +0000 Commit: Jessica Clarke CommitDate: 2024-10-18 18:14:58 +0000 libc/csu: Support IFUNCs on riscv When adding support to rtld-elf I neglected the fact that static binaries can have IFUNCs. Add support for this too. Fixes: 729d2b16b74f ("rtld-elf: Support IFUNCs on riscv") MFC after: 1 week --- lib/libc/csu/riscv/Makefile.inc | 4 +-- lib/libc/csu/riscv/reloc.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/lib/libc/csu/riscv/Makefile.inc b/lib/libc/csu/riscv/Makefile.inc index 2534e6579f38..5d59d40eb393 100644 --- a/lib/libc/csu/riscv/Makefile.inc +++ b/lib/libc/csu/riscv/Makefile.inc @@ -1,4 +1,4 @@ # -CFLAGS+= -DCRT_IRELOC_SUPPRESS \ - -DINIT_IRELOCS="" +CFLAGS+= -DCRT_IRELOC_RELA \ + -DINIT_IRELOCS="init_cpu_features(env)" diff --git a/lib/libc/csu/riscv/reloc.c b/lib/libc/csu/riscv/reloc.c new file mode 100644 index 000000000000..036ea3de8701 --- /dev/null +++ b/lib/libc/csu/riscv/reloc.c @@ -0,0 +1,63 @@ +/*- + * Copyright (c) 2019 Leandro Lupori + * Copyright (c) 2024 Jessica Clarke + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +static unsigned long elf_hwcap; + +static void +init_cpu_features(char **env) +{ + const Elf_Auxinfo *aux; + + /* Find the auxiliary vector on the stack. */ + while (*env++ != 0) /* Skip over environment, and NULL terminator */ + ; + aux = (const Elf_Auxinfo *)env; + + /* Digest the auxiliary vector. */ + for (; aux->a_type != AT_NULL; aux++) { + switch (aux->a_type) { + case AT_HWCAP: + elf_hwcap = (uint32_t)aux->a_un.a_val; + break; + } + } +} + +static void +crt1_handle_rela(const Elf_Rela *r) +{ + typedef Elf_Addr (*ifunc_resolver_t)( + unsigned long, unsigned long, unsigned long, unsigned long, + unsigned long, unsigned long, unsigned long, unsigned long); + Elf_Addr *ptr, *where, target; + + switch (ELF_R_TYPE(r->r_info)) { + case R_RISCV_IRELATIVE: + ptr = (Elf_Addr *)r->r_addend; + where = (Elf_Addr *)r->r_offset; + target = ((ifunc_resolver_t)ptr)(elf_hwcap, + 0, 0, 0, 0, 0, 0, 0); + *where = target; + break; + } +}