diff mbox series

[2/2] riscv: Initialize $gp before resolving the IRELATIVE relocation

Message ID 1606818180-17436-3-git-send-email-vincent.chen@sifive.com
State New
Headers show
Series riscv: add support for GNU indirect function | expand

Commit Message

Vincent Chen Dec. 1, 2020, 10:23 a.m. UTC
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 mbox series

Patch

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