public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Vincent Chen <vincent.chen@sifive.com>
To: libc-alpha@sourceware.org, palmer@dabbelt.com,
	darius@bluespec.com, dj@redhat.com
Cc: vincent.chen@sifive.com, nelson.chu@sifive.com
Subject: [PATCH 2/2] riscv: Initialize $gp before resolving the IRELATIVE relocation
Date: Tue,  1 Dec 2020 18:23:00 +0800	[thread overview]
Message-ID: <1606818180-17436-3-git-send-email-vincent.chen@sifive.com> (raw)
In-Reply-To: <1606818180-17436-1-git-send-email-vincent.chen@sifive.com>

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
+   <https://www.gnu.org/licenses/>.  */
+
+/* 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;
 }
 
-- 
2.29.2


  parent reply	other threads:[~2020-12-01 10:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-01 10:22 [PATCH 0/2] riscv: add support for GNU indirect function Vincent Chen
2020-12-01 10:22 ` [PATCH 1/2] riscv: Add " Vincent Chen
2020-12-01 11:14   ` Andreas Schwab
2020-12-01 11:42   ` Florian Weimer
2020-12-04  3:29     ` Vincent Chen
2020-12-04  5:57       ` Florian Weimer
2020-12-01 13:13   ` Szabolcs Nagy
2020-12-04  3:35     ` Vincent Chen
2020-12-01 10:23 ` Vincent Chen [this message]
2020-12-02  2:52 ` [PATCH 0/2] riscv: add " Jim Wilson
2020-12-07 21:36   ` Jim Wilson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1606818180-17436-3-git-send-email-vincent.chen@sifive.com \
    --to=vincent.chen@sifive.com \
    --cc=darius@bluespec.com \
    --cc=dj@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=nelson.chu@sifive.com \
    --cc=palmer@dabbelt.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).