[klibc] [klibc:riscv64-enable-relax] riscv64: Make linker relaxation work and enable it
klibc-bot for Ben Hutchings
ben at decadent.org.uk
Sat Aug 29 15:36:05 PDT 2020
Commit-ID: a966c3a12ee8b921dc669647c1fe75b25f72e604
Gitweb: http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=a966c3a12ee8b921dc669647c1fe75b25f72e604
Author: Ben Hutchings <ben at decadent.org.uk>
AuthorDate: Sat, 29 Aug 2020 21:59:24 +0100
Committer: Ben Hutchings <ben at decadent.org.uk>
CommitDate: Sat, 29 Aug 2020 23:33:09 +0100
[klibc] riscv64: Make linker relaxation work and enable it
RISC-V code generally needs two instructions to address static data,
but up to 4K of static data in an executable can be addressed using a
single instruction. The linker "relaxes" a two-instruction sequence
to one instruction, assuming that the gp register is initialised
appropriately. This is *not* supported in shared libraries, as I
originally thought.
This wasn't working in my initial port, so I disabled relaxation.
Make it work now:
* For statically-linked executables, the _entry routine in crt0.S
correctly initialises gp
* For executables using the shared-library, add a _main routine in
_main.S that initialises gp and tail-calls main
* Add _main.o to $(KLIBCCRTSHARED) and change the entry point to _main
* Install _main.o so klcc can use it
* Drop the --no-relax linker option
Signed-off-by: Ben Hutchings <ben at decadent.org.uk>
---
usr/klibc/arch/riscv64/Kbuild | 8 ++++++--
usr/klibc/arch/riscv64/MCONFIG | 6 +++---
usr/klibc/arch/riscv64/_main.S | 17 +++++++++++++++++
3 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/usr/klibc/arch/riscv64/Kbuild b/usr/klibc/arch/riscv64/Kbuild
index 242ac5bb..218f3869 100644
--- a/usr/klibc/arch/riscv64/Kbuild
+++ b/usr/klibc/arch/riscv64/Kbuild
@@ -2,7 +2,11 @@
#
# klibc files for riscv64
-always := crt0.o
-targets := crt0.o
+always := crt0.o _main.o
+targets := crt0.o _main.o
klib-y := setjmp.o syscall.o
+
+install-rule:
+ $(Q)$(shell $(install-data) $(call objectify,_main.o) \
+ $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib)
diff --git a/usr/klibc/arch/riscv64/MCONFIG b/usr/klibc/arch/riscv64/MCONFIG
index 9bc7bd24..fdd6c92c 100644
--- a/usr/klibc/arch/riscv64/MCONFIG
+++ b/usr/klibc/arch/riscv64/MCONFIG
@@ -7,9 +7,6 @@
# accordingly.
#
-# We should get klibc.so and the executables to agree on what gp
-# should be. For now, disable gp-relative addressing.
-KLIBCLDFLAGS = --no-relax
KLIBCOPTFLAGS += -Os -fomit-frame-pointer
ifeq ($(DEBUG),y)
KLIBCOPTFLAGS += -g
@@ -21,3 +18,6 @@ KLIBCSHAREDFLAGS = $(LD_IMAGE_BASE_OPT) 0x00200000
# Kernel has never used stack trampolines
KLIBCEXECSTACK := n
+
+KLIBCEMAIN := -e _main
+KLIBCCRTSHARED += $(KLIBCOBJ)/arch/riscv64/_main.o
diff --git a/usr/klibc/arch/riscv64/_main.S b/usr/klibc/arch/riscv64/_main.S
new file mode 100644
index 00000000..284a2222
--- /dev/null
+++ b/usr/klibc/arch/riscv64/_main.S
@@ -0,0 +1,17 @@
+#
+# arch/riscv64/_main.S
+#
+# Does arch-specific initialization and invokes main with the
+# appropriate arguments.
+#
+
+#include <machine/asm.h>
+
+ENTRY(_main)
+ .option push
+ .option norelax
+ lla gp, __global_pointer$
+ .option pop
+
+ j main
+END(_main)
More information about the klibc
mailing list