summaryrefslogtreecommitdiff
path: root/arch/riscv/include
diff options
context:
space:
mode:
authorYao Zi <[email protected]>2025-04-27 14:50:11 +0000
committerLeo Yu-Chi Liang <[email protected]>2025-05-21 16:46:16 +0800
commit307666be28db0d89cbdcfafd2cf0e0cf5bf386db (patch)
tree318ba2cf0407a5e5bd51234177ff04aa03f8bd40 /arch/riscv/include
parent60163080949a57ba28bc10cb599dc2a9b53c75c4 (diff)
riscv: Access gd with inline assembly when building with LTO or Clang
Similar to AArch64's case, Clang may wrongly fold accesses to gd pointer which is defined with register qualifier into constants, breaking various components. This patch defines gd as a macro when building with Clang or LTO, which expands to get_gd() that accesses gp pointer in assembly, making RISC-V ports function properly and preparing for introduction of LTO in the future. Board initialization code is also adapted for non-assignable gd. Reported-by: Nathaniel Hourt <[email protected]> Signed-off-by: Yao Zi <[email protected]> Reviewed-by: Leo Yu-Chi Liang <[email protected]>
Diffstat (limited to 'arch/riscv/include')
-rw-r--r--arch/riscv/include/asm/global_data.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/arch/riscv/include/asm/global_data.h b/arch/riscv/include/asm/global_data.h
index d356752a56a..47b5e2cfc8f 100644
--- a/arch/riscv/include/asm/global_data.h
+++ b/arch/riscv/include/asm/global_data.h
@@ -14,6 +14,7 @@
#include <asm/smp.h>
#include <asm/u-boot.h>
#include <compiler.h>
+#include <config.h>
/* Architecture-specific global data */
struct arch_global_data {
@@ -47,8 +48,26 @@ struct arch_global_data {
#include <asm-generic/global_data.h>
+#if defined(__clang__) || CONFIG_IS_ENABLED(LTO)
+
+#define DECLARE_GLOBAL_DATA_PTR
+#define gd get_gd()
+
+static inline gd_t *get_gd(void)
+{
+ gd_t *gd_ptr;
+
+ __asm__ volatile ("mv %0, gp\n" : "=r" (gd_ptr));
+
+ return gd_ptr;
+}
+
+#else
+
#define DECLARE_GLOBAL_DATA_PTR register gd_t *gd asm ("gp")
+#endif
+
static inline void set_gd(volatile gd_t *gd_ptr)
{
#ifdef CONFIG_64BIT