Tested this along with the patches for binutils and gcc. Everything is working for me when tested with buildroot's qemu_xtensa_lx60_defconfig and qemu.

On Thu, Sep 15, 2022 at 3:31 PM Max Filippov <jcmvbkbc@gmail.com> wrote:
Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
---
 extra/Configs/Config.in                |  4 +++-
 ldso/ldso/xtensa/dl-startup.h          |  2 ++
 libc/misc/internals/reloc_static_pie.c |  2 +-
 libc/sysdeps/linux/xtensa/crt1.S       | 27 ++++++++++++++++++++++++++
 4 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/extra/Configs/Config.in b/extra/Configs/Config.in
index 43c04fd0a271..dd1beaadcee3 100644
--- a/extra/Configs/Config.in
+++ b/extra/Configs/Config.in
@@ -324,7 +324,9 @@ config DOPIC
 config STATIC_PIE
        bool "Add support for Static Position Independent Executables (PIE)"
        default n
-       depends on DOPIC && !UCLIBC_FORMAT_FDPIC_ELF && (TARGET_arm || TARGET_i386 || TARGET_x86_64 || TARGET_aarch64 || TARGET_mips)
+       depends on DOPIC && !UCLIBC_FORMAT_FDPIC_ELF && \
+               (TARGET_arm || TARGET_i386 || TARGET_x86_64 || TARGET_aarch64 || \
+                TARGET_mips || TARGET_xtensa)

 config ARCH_HAS_NO_SHARED
        bool
diff --git a/ldso/ldso/xtensa/dl-startup.h b/ldso/ldso/xtensa/dl-startup.h
index 439bdbd7a5e2..c9350c0f201e 100644
--- a/ldso/ldso/xtensa/dl-startup.h
+++ b/ldso/ldso/xtensa/dl-startup.h
@@ -7,6 +7,7 @@
  * Parts taken from glibc/sysdeps/xtensa/dl-machine.h.
  */

+#ifndef L_rcrt1
 __asm__ (
     "  .text\n"
     "  .align  4\n"
@@ -81,6 +82,7 @@ __asm__ (
     "  addi    a5, a5, 8\n"
     "  bnez    a6, 3b\n"
     "  j      .Lfixup_stack_ret");
+#endif

 /* Get a pointer to the argv value.  */
 #define GET_ARGV(ARGVP, ARGS) ARGVP = (((unsigned long *) ARGS) + 1)
diff --git a/libc/misc/internals/reloc_static_pie.c b/libc/misc/internals/reloc_static_pie.c
index ce42cb9b30df..ab1923024c2b 100644
--- a/libc/misc/internals/reloc_static_pie.c
+++ b/libc/misc/internals/reloc_static_pie.c
@@ -21,7 +21,7 @@
 #include <dl-elf.h>

 #include <ldso.h>
-#ifdef __mips__
+#if defined(__mips__) || defined(__xtensa__)
 #include <dl-startup.h>
 #endif

diff --git a/libc/sysdeps/linux/xtensa/crt1.S b/libc/sysdeps/linux/xtensa/crt1.S
index efbe264c03f0..3fa14ae583a9 100644
--- a/libc/sysdeps/linux/xtensa/crt1.S
+++ b/libc/sysdeps/linux/xtensa/crt1.S
@@ -76,9 +76,26 @@
        .global _start
        .type   _start, @function
 _start:
+#ifdef L_rcrt1
+       .begin  no-transform
+       call0   1f
+.Lret_addr:
+       .end    no-transform
+       .align  4
+1:
+#endif
 #if defined(__XTENSA_WINDOWED_ABI__)
+#ifdef L_rcrt1
+       movi    a6, .Lret_addr
+       sub     a6, a0, a6
+       movi    a0, 0
+       movi    a4, reloc_static_pie
+       add     a4, a4, a6
+       callx4  a4
+#else
        /* Clear a0 to obviously mark the outermost frame.  */
        movi    a0, 0
+#endif

        /* Load up the user's main function.  */
        movi    a6, main
@@ -106,8 +123,18 @@ _start:
        movi    a4, __uClibc_main
        callx4  a4
 #elif defined(__XTENSA_CALL0_ABI__)
+#ifdef L_rcrt1
+       mov     a12, a2
+       movi    a2, .Lret_addr
+       sub     a2, a0, a2
+       movi    a0, reloc_static_pie
+       add     a0, a0, a2
+       callx0  a0
+       mov     a7, a12
+#else
        /* Setup the shared library termination function.  */
        mov     a7, a2
+#endif

        /* Load up the user's main function.  */
        movi    a2, main
--
2.30.2