[RFC PATCH v2 06/10] tools/nolibc: riscv: Add relocation support

Daniel Palmer posted 10 patches 4 days, 6 hours ago
[RFC PATCH v2 06/10] tools/nolibc: riscv: Add relocation support
Posted by Daniel Palmer 4 days, 6 hours ago
Add relocation support for 32 and 64bit riscv.

Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
 tools/include/nolibc/arch-riscv.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-riscv.h
index 1c00cacf57e1..0a6a4300c318 100644
--- a/tools/include/nolibc/arch-riscv.h
+++ b/tools/include/nolibc/arch-riscv.h
@@ -7,8 +7,18 @@
 #ifndef _NOLIBC_ARCH_RISCV_H
 #define _NOLIBC_ARCH_RISCV_H
 
+#include "elf.h"
+
+#ifdef R_RISCV_RELATIVE
+#define _NOLIBC_ARCH_HAS_RELOC
+#if __riscv_xlen == 32
+#define _NOLIBC_ARCH_ELF32
+#endif
+#endif
+
 #include "compiler.h"
 #include "crt.h"
+#include "reloc.h"
 
 /* Syscalls for RISCV :
  *   - stack is 16-byte aligned
@@ -140,6 +150,22 @@
 })
 
 #ifndef NOLIBC_NO_RUNTIME
+
+#ifdef NOLIBC_WANT_RELOC
+static __inline__ int __relocate_rela(unsigned long base, _nolibc_elf_rela *entry)
+{
+	switch (_nolibc_elf_r_type(entry->r_info)) {
+	case R_RISCV_RELATIVE:
+		__relocate_rela_relative(base, entry);
+		break;
+	default:
+		return -1;
+	}
+
+	return 0;
+}
+#endif /* NOLIBC_WANT_RELOC */
+
 /* startup code */
 void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _start(void)
 {
-- 
2.51.0