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; }