From patchwork Tue Dec 1 10:22:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 41241 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 C48783944801; Tue, 1 Dec 2020 10:23:13 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by sourceware.org (Postfix) with ESMTPS id 217433944433 for ; Tue, 1 Dec 2020 10:23:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 217433944433 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vincent.chen@sifive.com Received: by mail-pg1-x543.google.com with SMTP id o4so954551pgj.0 for ; Tue, 01 Dec 2020 02:23:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=pelBXKhaRyhe55hfJcRU7z2/xZcN5WOjnLmHCiF9pM0=; b=cft9isv7wPsb+BIaZmUk+Cv1IXFlxdwpPE8yqac6qLNuUUXrh6p0sUKUqOfFKkujKF hiWwTL8f0rrv0GTmyT3kf3WdNARPPmOZg0q7AYpBNces0aO+p3cgJMcUwiVCddbWejuB bCEEYJPlkICh5TwSv1XQ18cOCkAluiYBoje4LNPWqE0lWx5AiU8BmTCSQLrek5n5loQN 8lqDyrgmEzogmLYcIc6NWcsdF6H3zkkDL3hSKl1P0M+PhwRqhbtt2i2Z5bxjlTUwFxmc 8CJRmQRO/RauRDl1p7jdE8vKEY45QnP0ksL+Kr6hxABRUXz4jroLdHp9qtnOJWRNnaeq A1ig== 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:in-reply-to :references; bh=pelBXKhaRyhe55hfJcRU7z2/xZcN5WOjnLmHCiF9pM0=; b=pJ5UQFSpB1vy5cltzPcXoAIpZL5QBDItHNDiV4Y1qDaxPGO8001P9cvw7sEWCMp1ZK RRtil6LADGlMiTtGf+U+1mlBaTgsdVWZaFGxYIOkIb7VpcTCf/JZex7PYpgvCmd3fpkb 1LGj7nupGKX5Fqy08SrU8zqtbK/QJP3mwggABdR9hRNHAoCUenxdsKPd/Es41UU4Ytr5 VN/MAWPo1OUO4sEyFGB2qDUgEvj2nfA7Es4+PzVHe0Mk5oELLWoDxNI3YHlqUEeZzQx6 WK59j5YCN4/lqO3W3lCvpoQPorHsFT7/JUJQa5oGcOrTAlCvXTISHot6wkALsjhaoZR3 +Sig== X-Gm-Message-State: AOAM532A7WAykSRVaITP8+A9LcFCjdDKlk+EN6URW+TLiBGZU285HG8l atK6+JKt3xjqH+yjZPj/HoH54Ehaf1kTtfxN X-Google-Smtp-Source: ABdhPJzZ1HNlowKawSM/Sl7UQZj8lVNnxT+Fi8Ag/BlSjJrnIahDDCxAEobLNDQM9gc/oYey7yn/Vg== X-Received: by 2002:a05:6a00:16c4:b029:198:a95:a2ef with SMTP id l4-20020a056a0016c4b02901980a95a2efmr2071375pfc.43.1606818189857; Tue, 01 Dec 2020 02:23:09 -0800 (PST) Received: from VincentChen-ThinkPad-T480s.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id y10sm2096785pjm.34.2020.12.01.02.23.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Dec 2020 02:23:09 -0800 (PST) From: Vincent Chen To: libc-alpha@sourceware.org, palmer@dabbelt.com, darius@bluespec.com, dj@redhat.com Subject: [PATCH 1/2] riscv: Add support for GNU indirect function Date: Tue, 1 Dec 2020 18:22:59 +0800 Message-Id: <1606818180-17436-2-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1606818180-17436-1-git-send-email-vincent.chen@sifive.com> References: <1606818180-17436-1-git-send-email-vincent.chen@sifive.com> X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: vincent.chen@sifive.com, nelson.chu@sifive.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Enable riscv Glibc to support GNU indirect function --- libc-abis | 1 + sysdeps/riscv/dl-irel.h | 53 ++++++++++++++++++++++++++++++++++++++ sysdeps/riscv/dl-machine.h | 22 ++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 sysdeps/riscv/dl-irel.h diff --git a/libc-abis b/libc-abis index e702f6ae24..fb772499b2 100644 --- a/libc-abis +++ b/libc-abis @@ -46,5 +46,6 @@ IFUNC powerpc64-*-linux* IFUNC powerpc-*-linux* IFUNC sparc64-*-linux* IFUNC sparc-*-linux* +IFUNC riscv*-linux* # Absolute (SHN_ABS) symbols working correctly. ABSOLUTE diff --git a/sysdeps/riscv/dl-irel.h b/sysdeps/riscv/dl-irel.h new file mode 100644 index 0000000000..7dcfffae6a --- /dev/null +++ b/sysdeps/riscv/dl-irel.h @@ -0,0 +1,53 @@ +/* Machine-dependent ELF indirect relocation inline functions. + RISC-V version. + Copyright (C) 2020 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 + . */ + +#ifndef _DL_IREL_H +#define _DL_IREL_H + +#include +#include +#include +#include + +#define ELF_MACHINE_IRELA 1 + +static inline ElfW(Addr) +__attribute ((always_inline)) +elf_ifunc_invoke (ElfW(Addr) addr) +{ + return ((ElfW(Addr) (*) (uint64_t)) (addr)) (GLRO(dl_hwcap)); +} + +static inline void +__attribute ((always_inline)) +elf_irela (const ElfW(Rela) *reloc) +{ + ElfW(Addr) *const reloc_addr = (void *) reloc->r_offset; + const unsigned long int r_type = ELFW(R_TYPE) (reloc->r_info); + + if (__glibc_likely (r_type == R_RISCV_IRELATIVE)) + { + ElfW(Addr) value = elf_ifunc_invoke (reloc->r_addend); + *reloc_addr = value; + } + else + __libc_fatal ("Unexpected reloc type in static binary.\n"); +} + +#endif diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h index 511140864e..0ae45dd0c3 100644 --- a/sysdeps/riscv/dl-machine.h +++ b/sysdeps/riscv/dl-machine.h @@ -25,6 +25,7 @@ #include #include #include +#include #ifndef _RTLD_PROLOGUE # define _RTLD_PROLOGUE(entry) \ @@ -176,6 +177,13 @@ elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc, if (sym_map != NULL) value = SYMBOL_ADDRESS (sym_map, sym, true) + reloc->r_addend; + if (sym != NULL + && __glibc_unlikely (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC) + && __glibc_likely (sym->st_shndx != SHN_UNDEF) + && __glibc_likely (!skip_ifunc)) + value = elf_ifunc_invoke (value); + + switch (r_type) { #ifndef RTLD_BOOTSTRAP @@ -251,6 +259,13 @@ elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc, } #endif + case R_RISCV_IRELATIVE: + value = map->l_addr + reloc->r_addend; + if (__glibc_likely (!skip_ifunc)) + value = elf_ifunc_invoke (value); + *addr_field = value; + break; + case R_RISCV_JUMP_SLOT: case __WORDSIZE == 64 ? R_RISCV_64 : R_RISCV_32: *addr_field = value; @@ -292,6 +307,13 @@ elf_machine_lazy_rel (struct link_map *map, ElfW(Addr) l_addr, else *reloc_addr = map->l_mach.plt; } + else if (__glibc_unlikely (r_type == R_RISCV_IRELATIVE)) + { + ElfW(Addr) value = map->l_addr + reloc->r_addend; + if (__glibc_likely (!skip_ifunc)) + value = elf_ifunc_invoke (value); + *reloc_addr = value; + } else _dl_reloc_bad_type (map, r_type, 1); } From patchwork Tue Dec 1 10:23:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 41242 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 26F733944823; Tue, 1 Dec 2020 10:23:15 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id A69B43944424 for ; Tue, 1 Dec 2020 10:23:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A69B43944424 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vincent.chen@sifive.com Received: by mail-pg1-x532.google.com with SMTP id s63so930901pgc.8 for ; Tue, 01 Dec 2020 02:23:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=OhO862Ltddd9OczZSkz7yNi+FRlywe7czdbb6Wv8270=; b=hNasPr6U1T0msXd7GPiMA1a2jgp4qKOanJY4+oSP37PpRoYZCsLC8/pTVNx3GqSw+q aRQjKeYeTkmOUvd2pmtzce+lTNS1pbBGkma0upmPI6D6bRq6MXD00Cny+6jAKN9NUXgu jDuXpS+Ep9CBIr1OtLLN7S95+FF50XPsAbn27BLyTfWAvZh0e+ncGjeyH2clZPq20D9J TM0xLH69tVxTzafUIHmjanxbHX8kkDR070zVho8lmTOv6ZSaJR9tU9KOPASC1RRy0I2S ZeHCAKXtSuSqyhg30ldJ7jylKVlU5OyFlh1EjDuQLRulmwMOMtdq+D2wPaGoNkKLLui4 uCKw== 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:in-reply-to :references; bh=OhO862Ltddd9OczZSkz7yNi+FRlywe7czdbb6Wv8270=; b=fn8Z4BwYOO5GE6EJyCR3c3YMnTv1GkTKCl6Iiy9irdhNFtqadR+4UuQgvj9Toax1aA oAoXB6K4fgCebg4kcKdFNvbYbznb2YQht7gXgC3G7YlcJw9gpQSOZz5f9gv7TDuPT949 Fxxw6qa5E+tCoRFG3pM2wEf0fuW3SU4be3AaGyRv5idub5jrT+CdM6yJb41zkrFA2Oc0 1tQGacKkotwr828TUUv47BKgjC6yeR9KMc6dNyCxZPkxXWw5lyvhGFuQW601RCdKmy31 y2LL0uwnHOoPNVlYRamBWSCTD8rUUx7vSu2q9F2ZYRmr9pleNWsgyUW1ObleAyQpT5S0 LznA== X-Gm-Message-State: AOAM532xkYrTMr2OJhFCmARdrNQuztfAiniz8TJgUocfZNJ6bMU0wmnB +p1D/rpMok8JWr92Z7rKBp7D62FkJLUg9SM9 X-Google-Smtp-Source: ABdhPJyiw0IhdH2XGzg2hvuuCkllqVSoEVgdUZMAoevznJ3SS/zsqE+eAYksa9/GbyHkNh70uDsx4w== X-Received: by 2002:a63:4d5c:: with SMTP id n28mr1695946pgl.88.1606818191615; Tue, 01 Dec 2020 02:23:11 -0800 (PST) Received: from VincentChen-ThinkPad-T480s.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id y10sm2096785pjm.34.2020.12.01.02.23.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Dec 2020 02:23:11 -0800 (PST) From: Vincent Chen To: libc-alpha@sourceware.org, palmer@dabbelt.com, darius@bluespec.com, dj@redhat.com Subject: [PATCH 2/2] riscv: Initialize $gp before resolving the IRELATIVE relocation Date: Tue, 1 Dec 2020 18:23:00 +0800 Message-Id: <1606818180-17436-3-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1606818180-17436-1-git-send-email-vincent.chen@sifive.com> References: <1606818180-17436-1-git-send-email-vincent.chen@sifive.com> X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: vincent.chen@sifive.com, nelson.chu@sifive.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" RISC-V $gp register may be used to access the global variable in the PDE program. Currently, the initialization of $gp register is addressed in the _start function. However, the IFUNC resolver is executed before the _start function. In this condition, if a IFUNC resolver function uses the uninitialized $gp to access global variable, an unexpected error will occur. This patch makes ld.so initialize the $gp with the DT_RISCV_GP dynamic entry before resolving the IRELATIVE symbol. --- elf/elf.h | 4 ++++ sysdeps/riscv/dl-dtprocnum.h | 22 ++++++++++++++++++++++ sysdeps/riscv/dl-machine.h | 9 +++++++++ 3 files changed, 35 insertions(+) create mode 100644 sysdeps/riscv/dl-dtprocnum.h diff --git a/elf/elf.h b/elf/elf.h index 6439c1a4d5..7566da2dca 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -3955,6 +3955,10 @@ enum #define R_RISCV_NUM 59 +/* RISCV specific values for the Dyn d_tag field. */ +#define DT_RISCV_GP (DT_LOPROC + 0) +#define DT_RISCV_NUM 1 + /* BPF specific declarations. */ #define R_BPF_NONE 0 /* No reloc */ diff --git a/sysdeps/riscv/dl-dtprocnum.h b/sysdeps/riscv/dl-dtprocnum.h new file mode 100644 index 0000000000..5b5f99571a --- /dev/null +++ b/sysdeps/riscv/dl-dtprocnum.h @@ -0,0 +1,22 @@ +/* Configuration of lookup functions. RISC-V version. + Copyright (C) 2020 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 0ae45dd0c3..4b1b8e1444 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 RISC-V 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) @@ -341,6 +344,12 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile) } #endif + if (l->l_info[DT_RISCV(GP)]) + asm ( + "mv gp, %0\n" + : + :"r"(D_PTR (l, l_info[DT_RISCV(GP)])) + ); return lazy; }