tools/nolibc: mips: add support for PIC
authorThomas Weißschuh <linux@weissschuh.net>
Wed, 8 Nov 2023 18:14:43 +0000 (19:14 +0100)
committerThomas Weißschuh <linux@weissschuh.net>
Mon, 11 Dec 2023 21:38:30 +0000 (22:38 +0100)
MIPS requires some extra instructions to set up the $gp register for the
with a pointer to the global data area.

This isn't needed for non-PIC builds, but this patch enables the code
unconditionally to prevent bitrot.

Also enable PIC in one of the test configurations for ongoing
validation.

Link: https://lore.kernel.org/r/20231108-nolibc-pic-v2-1-4fb0d6284757@weissschuh.net
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/include/nolibc/arch-mips.h
tools/testing/selftests/nolibc/Makefile

index 3a2c76716b8392b3a7b1d6c4800ea3d03d8f8877..62cc50ef32882168996f20fd7e203aa78966c717 100644 (file)
@@ -184,8 +184,13 @@ void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_
        __asm__ volatile (
                ".set push\n"
                ".set noreorder\n"
-               ".option pic0\n"
+               "bal 1f\n"               /* prime $ra for .cpload                            */
+               "nop\n"
+               "1:\n"
+               ".cpload $ra\n"
                "move  $a0, $sp\n"       /* save stack pointer to $a0, as arg1 of _start_c */
+               "addiu $sp, $sp, -4\n"   /* space for .cprestore to store $gp              */
+               ".cprestore 0\n"
                "li    $t0, -8\n"
                "and   $sp, $sp, $t0\n"  /* $sp must be 8-byte aligned                     */
                "addiu $sp, $sp, -16\n"  /* the callee expects to save a0..a3 there        */
index 47f0fb52fe55b370afccc0ee610e744cba91877e..40dd95228051eca6eac169b45a686d318d5ac67e 100644 (file)
@@ -147,7 +147,7 @@ CFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)
 CFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)
 CFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2)
 CFLAGS_s390 = -m64
-CFLAGS_mips32le = -EL -mabi=32
+CFLAGS_mips32le = -EL -mabi=32 -fPIC
 CFLAGS_mips32be = -EB -mabi=32
 CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all))
 CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \