From patchwork Tue Dec 15 09:16:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 41390 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 4E5DF386189F; Tue, 15 Dec 2020 09:17:09 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by sourceware.org (Postfix) with ESMTPS id 86F3238618D4 for ; Tue, 15 Dec 2020 09:17:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 86F3238618D4 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-x544.google.com with SMTP id w4so14779069pgg.13 for ; Tue, 15 Dec 2020 01:17:04 -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=tb2ghGZCfwaWZpOWh5Vm0ka8C2V8bOMUhWtVHcmBcN0=; b=POXEZtVRLnGE8DLVsgnRntiZBnL/hMdM6WIUUGbnykfRG/9ay7jYREzaiZTtdrP3Qc MbR/hGw1+Y/q3SLXnrWc0jug86+ueOrFLPmrrVung6w23UEF9he5x+MYSc5WlY3cO7+q A59jovkegmucaE3yL840FTdONrhfKk6wp/NkMn9p+NlmcoNRzfuK0j69ILp6T72N9L8c qDBb+5W+b4aApZVxH2sClQUvMqJMET9KgORQoFhO8ybai6lKmNmGCid/hKw5EpDwSvEs 5XdYUglPzThMnXX6sJINhXbfzwqwRq2QHn98yIMmVeS1++qJOQ0ZkTGF8oLdaJqwIXy4 4Dvw== 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=tb2ghGZCfwaWZpOWh5Vm0ka8C2V8bOMUhWtVHcmBcN0=; b=Ad4bHxpcg5EZGXW+nkeNniSE6psrkFhjgnU1ANyfXCnd8HaLT4jGMiNEIMaBeSl9rc 8Q/mtB1u6L7+617YFvtLK9XleDoW5aCAXKfLnWp45yPrpwAL8A6+K2H/lU1ruSMLFEj5 D48Vhvz3rD/GGYAcQMFyU3WvU/Ac1/djTpKkBP7j3kLfD64iyOy1luAi7gjQ9i8sR6Qd tN0jGhg44gDg3bIrUQe8t4JEvwCuOW+Z1DCt4VCYHPIzzirYczYGtGWW1aR6DTxzO976 XkV9AWef49zvxO46t60kYiUF6UNNS2cDyBCflnQrmPo3LzJU5qtWamtT/XebgyKts+0e cL9w== X-Gm-Message-State: AOAM5336CTRf4Hb59kxzuzbhiPeVHTvkaI7JkA3jaRUToYzHjMoog7uk ndPq4m5R2FdgSxo9hDcuexgU2L8ggpo3k4HwbDQ= X-Google-Smtp-Source: ABdhPJyMKyySPl5BSpoX1l7Gk+wHFYcTpwlNa0gkX1cH32FNqYSKnN01q3ubmKTM5I+CqgLjNBeNAA== X-Received: by 2002:aa7:96d8:0:b029:19e:bc79:cf7 with SMTP id h24-20020aa796d80000b029019ebc790cf7mr23942880pfq.22.1608023823475; Tue, 15 Dec 2020 01:17:03 -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 e8sm19193256pfn.176.2020.12.15.01.17.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Dec 2020 01:17:03 -0800 (PST) From: Vincent Chen To: libc-alpha@sourceware.org, palmer@dabbelt.com, darius@bluespec.com, dj@redhat.com Subject: [PATCH v2 1/2] riscv: support GNU indirect function Date: Tue, 15 Dec 2020 17:16:39 +0800 Message-Id: <1608023800-14560-2-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1608023800-14560-1-git-send-email-vincent.chen@sifive.com> References: <1608023800-14560-1-git-send-email-vincent.chen@sifive.com> X-Spam-Status: No, score=-11.2 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: nelson.chu@sifive.com, Vincent Chen , paul.walmsley@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 | 56 ++++++++++++++++++++++++++++++++++++++ sysdeps/riscv/dl-machine.h | 22 +++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 sysdeps/riscv/dl-irel.h diff --git a/libc-abis b/libc-abis index e702f6ae24..70b154de4b 100644 --- a/libc-abis +++ b/libc-abis @@ -48,3 +48,4 @@ IFUNC sparc64-*-linux* IFUNC sparc-*-linux* # Absolute (SHN_ABS) symbols working correctly. ABSOLUTE +IFUNC riscv*-linux* diff --git a/sysdeps/riscv/dl-irel.h b/sysdeps/riscv/dl-irel.h new file mode 100644 index 0000000000..cad9cc87b1 --- /dev/null +++ b/sysdeps/riscv/dl-irel.h @@ -0,0 +1,56 @@ +/* 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) +{ + /* The second argument is a void pointer to preserve the extension + fexibility. */ + return ((ElfW(Addr) (*) (uint64_t, void *)) (addr)) + (GLRO(dl_hwcap), NULL); +} + +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 15 09:16:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 41391 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 ADA6738618C9; Tue, 15 Dec 2020 09:17:13 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by sourceware.org (Postfix) with ESMTPS id 4F29E38618B6 for ; Tue, 15 Dec 2020 09:17:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4F29E38618B6 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-pf1-x443.google.com with SMTP id t22so4732944pfl.3 for ; Tue, 15 Dec 2020 01:17:07 -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=tmphHp54vGV4mg5PxnQWZBYadK6tvJwd6MBeW6YHQN4=; b=OJms05jHLMqshozM293OlW8HjvkdkjhvYgvJGXzQClhTxKFkghg6kf5S477kke7l1x i3KGJAP53z5+J7DxUTjNcxrDm9Jkxvn9GBa5PgMedrSR9Je+6RX4i170Df/AKfAODiaY /ADoQ4HuwNgaa/z3sEDz33myETQ7wv695dkSJS3fhH/QHzFc8Y7HcYlncPYW2cCBZvWo maKV+ZwNlrZw2IXWGaIUhHl9Zm5KeoxKkVeptvy1GcLqUG9a1dNUFcWX0fXlGNqhfAU8 2scpKNYFTpoTpGo+T3wAJ7Tv/cHRhMON5ss75GOqyebzJ8+KE+LeY54+rFpoLY1mCKfA 3K/Q== 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=tmphHp54vGV4mg5PxnQWZBYadK6tvJwd6MBeW6YHQN4=; b=aSHp6Q1qwdVT1Bi4uypeRq3+BRSnDbFHU9ty+tprksJMoA4D4fCyEeNpEXKCNv3N1O 2wk7b/ZrAIfPkQJwnznVTkTCwNt3r9FZfdazYEQH/uwLZYUI9nLflE+GvkZpeAX62vBT QSwKnDe2DlzAsF874wQhLTJ1Q5DGENmzH9//YLsA7k73Rcc3RhGrTS4Re4CsajD9gZ4X JE1ONm/lHbAGn+kmvNtDJyOFbtapkeyX49DUQjcpn/w4HRdZEhXF8zyRuiqGOdpgY4Ej Q4mqQI/sJyxxNv0UYHSa3PtmewkJFsaVUTGWrxvPmjdTYwv1GAxaM1weWZ1TBcl97hw5 7/9Q== X-Gm-Message-State: AOAM532tk9Ap3JDPs73l/J6Qw8dFMr5vP6VcTFgiY16U2jl/BJCYy7jy p78ZxPXtLPvN6ZXlHqDoNgAyAsd0lPfCJskcqoI= X-Google-Smtp-Source: ABdhPJzGHtrZYTlveNMVoMuPx91TFtQ3u7mjFxQxJ3TsStIaJcAhmSH+9VDAQVRoAoJLXWuNpvbPhA== X-Received: by 2002:a63:da58:: with SMTP id l24mr27965915pgj.178.1608023826274; Tue, 15 Dec 2020 01:17:06 -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 e8sm19193256pfn.176.2020.12.15.01.17.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Dec 2020 01:17:05 -0800 (PST) From: Vincent Chen To: libc-alpha@sourceware.org, palmer@dabbelt.com, darius@bluespec.com, dj@redhat.com Subject: [PATCH v2 2/2] riscv: Initialize $gp before resolving the IRELATIVE relocation Date: Tue, 15 Dec 2020 17:16:40 +0800 Message-Id: <1608023800-14560-3-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1608023800-14560-1-git-send-email-vincent.chen@sifive.com> References: <1608023800-14560-1-git-send-email-vincent.chen@sifive.com> X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: nelson.chu@sifive.com, Vincent Chen , paul.walmsley@sifive.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The $gp register may be used to access the global variable in the PDE program, so the $gp register should be initialized before executing the IFUNC resolver of PDE program to avoid unexpected error occurs. --- sysdeps/riscv/dl-machine.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h index 0ae45dd0c3..211835f0e6 100644 --- a/sysdeps/riscv/dl-machine.h +++ b/sysdeps/riscv/dl-machine.h @@ -339,8 +339,28 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile) gotplt[0] = (ElfW(Addr)) &_dl_runtime_resolve; gotplt[1] = (ElfW(Addr)) l; } -#endif + if (l->l_type == lt_executable) + { + /* The __global_pointer$ may not be defined by the linker if the + $gp register does not be used to access the global variable + in the executable program. Therefore, the search symbol is + set to a weak symbol to avoid we error out if the + __global_pointer$ is not found. */ + ElfW(Sym) gp_sym = { 0 }; + gp_sym.st_info = (unsigned char) ELFW (ST_INFO (STB_WEAK, STT_NOTYPE)); + + const ElfW(Sym) *ref = &gp_sym; + _dl_lookup_symbol_x ("__global_pointer$", l, &ref, + l->l_scope, NULL, 0, 0, NULL); + if (ref) + asm ( + "mv gp, %0\n" + : + : "r" (ref->st_value) + ); + } +#endif return lazy; }