From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59DC6C54EBD for ; Mon, 9 Jan 2023 08:44:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236866AbjAIIn6 (ORCPT ); Mon, 9 Jan 2023 03:43:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236702AbjAIIma (ORCPT ); Mon, 9 Jan 2023 03:42:30 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 210CD13F8D for ; Mon, 9 Jan 2023 00:42:27 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gCo8027420; Mon, 9 Jan 2023 09:42:12 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 01/22] tools/nolibc: make compiler and assembler agree on the section around _start Date: Mon, 9 Jan 2023 09:41:47 +0100 Message-Id: <20230109084208.27355-2-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The out-of-block asm() statement carrying _start does not allow the compiler to know what section the assembly code is being emitted to, and there's no easy way to push/pop the current section and restore it. It sometimes causes issues depending on the include files ordering and compiler optimizations. For example if a variable is declared immediately before the asm() block and another one after, the compiler assumes that the current section is still .bss and doesn't re-emit it, making the second variable appear inside the .text section instead. Forcing .bss at the end of the _start block doesn't work either because at certain optimizations the compiler may reorder blocks and will make some real code appear just after this block. A significant number of solutions were attempted, but many of them were still sensitive to section reordering. In the end, the best way to make sure the compiler and assembler agree on the current section is to place this code inside a function. Here the function is directly called _start and configured not to emit a frame-pointer, hence to have no prologue. If some future architectures would still emit some prologue, another working approach consists in naming the function differently and placing the _start label inside the asm statement. But the current solution is simpler. It was tested with nolibc-test at -O,-O0,-O2,-O3,-Os for arm,arm64,i386, mips,riscv,s390 and x86_64. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-aarch64.h | 29 ++++++++-------- tools/include/nolibc/arch-arm.h | 40 +++++++++------------- tools/include/nolibc/arch-i386.h | 38 +++++++++++---------- tools/include/nolibc/arch-mips.h | 51 +++++++++++++++-------------- tools/include/nolibc/arch-riscv.h | 36 ++++++++++---------- tools/include/nolibc/arch-s390.h | 44 +++++++++++++------------ tools/include/nolibc/arch-x86_64.h | 30 +++++++++-------- 7 files changed, 135 insertions(+), 133 deletions(-) diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arc= h-aarch64.h index f68baf8f395f..4d263661411f 100644 --- a/tools/include/nolibc/arch-aarch64.h +++ b/tools/include/nolibc/arch-aarch64.h @@ -182,18 +182,19 @@ struct sys_stat_struct { }) =20 /* startup code */ -__asm__ (".section .text\n" - ".weak _start\n" - "_start:\n" - "ldr x0, [sp]\n" // argc (x0) was in the stack - "add x1, sp, 8\n" // argv (x1) =3D sp - "lsl x2, x0, 3\n" // envp (x2) =3D 8*argc ... - "add x2, x2, 8\n" // + 8 (skip null) - "add x2, x2, x1\n" // + argv - "and sp, x1, -16\n" // sp must be 16-byte aligned in the cal= lee - "bl main\n" // main() returns the status code, we'll= exit with it. - "mov x8, 93\n" // NR_exit =3D=3D 93 - "svc #0\n" - ""); - +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) +{ + __asm__ volatile ( + "ldr x0, [sp]\n" // argc (x0) was in the stack + "add x1, sp, 8\n" // argv (x1) =3D sp + "lsl x2, x0, 3\n" // envp (x2) =3D 8*argc ... + "add x2, x2, 8\n" // + 8 (skip null) + "add x2, x2, x1\n" // + argv + "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee + "bl main\n" // main() returns the status code, we'll exit with = it. + "mov x8, 93\n" // NR_exit =3D=3D 93 + "svc #0\n" + ); + __builtin_unreachable(); +} #endif // _NOLIBC_ARCH_AARCH64_H diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-ar= m.h index f31be8e967d6..875b21975137 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -175,30 +175,20 @@ struct sys_stat_struct { }) =20 /* startup code */ -__asm__ (".section .text\n" - ".weak _start\n" - "_start:\n" -#if defined(__THUMBEB__) || defined(__THUMBEL__) - /* We enter here in 32-bit mode but if some previous functions were in - * 16-bit mode, the assembler cannot know, so we need to tell it we're= in - * 32-bit now, then switch to 16-bit (is there a better way to do it t= han - * adding 1 by hand ?) and tell the asm we're now in 16-bit mode so th= at - * it generates correct instructions. Note that we do not support thum= b1. - */ - ".code 32\n" - "add r0, pc, #1\n" - "bx r0\n" - ".code 16\n" -#endif - "pop {%r0}\n" // argc was in the stack - "mov %r1, %sp\n" // argv =3D sp - "add %r2, %r1, %r0, lsl #2\n" // envp =3D argv + 4*argc ... - "add %r2, %r2, $4\n" // ... + 4 - "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in = the - "mov %sp, %r3\n" // callee, an bl doesn't push (l= r=3Dpc) - "bl main\n" // main() returns the status code, we'll= exit with it. - "movs r7, $1\n" // NR_exit =3D=3D 1 - "svc $0x00\n" - ""); +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) +{ + __asm__ volatile ( + "pop {%r0}\n" // argc was in the stack + "mov %r1, %sp\n" // argv =3D sp + "add %r2, %r1, %r0, lsl #2\n" // envp =3D argv + 4*argc ... + "add %r2, %r2, $4\n" // ... + 4 + "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the + "mov %sp, %r3\n" // callee, an bl doesn't push (lr= =3Dpc) + "bl main\n" // main() returns the status code, we'll e= xit with it. + "movs r7, $1\n" // NR_exit =3D=3D 1 + "svc $0x00\n" + ); + __builtin_unreachable(); +} =20 #endif // _NOLIBC_ARCH_ARM_H diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i= 386.h index d7e7212346e2..b1bed2d87f74 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -197,23 +197,25 @@ struct sys_stat_struct { * 2) The deepest stack frame should be set to zero * */ -__asm__ (".section .text\n" - ".weak _start\n" - "_start:\n" - "pop %eax\n" // argc (first arg, %eax) - "mov %esp, %ebx\n" // argv[] (second arg, %ebx) - "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) - "xor %ebp, %ebp\n" // zero the stack frame - "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned b= efore - "sub $4, %esp\n" // the call instruction (args are aligned) - "push %ecx\n" // push all registers on the stack so that= we - "push %ebx\n" // support both regparm and plain stack mo= des - "push %eax\n" - "call main\n" // main() returns the status code in %eax - "mov %eax, %ebx\n" // retrieve exit code (32-bit int) - "movl $1, %eax\n" // NR_exit =3D=3D 1 - "int $0x80\n" // exit now - "hlt\n" // ensure it does not - ""); +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) +{ + __asm__ volatile ( + "pop %eax\n" // argc (first arg, %eax) + "mov %esp, %ebx\n" // argv[] (second arg, %ebx) + "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) + "xor %ebp, %ebp\n" // zero the stack frame + "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned bef= ore + "sub $4, %esp\n" // the call instruction (args are aligned) + "push %ecx\n" // push all registers on the stack so that we + "push %ebx\n" // support both regparm and plain stack modes + "push %eax\n" + "call main\n" // main() returns the status code in %eax + "mov %eax, %ebx\n" // retrieve exit code (32-bit int) + "movl $1, %eax\n" // NR_exit =3D=3D 1 + "int $0x80\n" // exit now + "hlt\n" // ensure it does not + ); + __builtin_unreachable(); +} =20 #endif // _NOLIBC_ARCH_I386_H diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-m= ips.h index 7380093ba9e7..11270ef25ea5 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -189,29 +189,32 @@ struct sys_stat_struct { }) =20 /* startup code, note that it's called __start on MIPS */ -__asm__ (".section .text\n" - ".weak __start\n" - ".set nomips16\n" - ".set push\n" - ".set noreorder\n" - ".option pic0\n" - ".ent __start\n" - "__start:\n" - "lw $a0,($sp)\n" // argc was in the stack - "addiu $a1, $sp, 4\n" // argv =3D sp + 4 - "sll $a2, $a0, 2\n" // a2 =3D argc * 4 - "add $a2, $a2, $a1\n" // envp =3D argv + 4*argc ... - "addiu $a2, $a2, 4\n" // ... + 4 - "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 the= re! - "jal main\n" // main() returns the status code, we'll= exit with it. - "nop\n" // delayed slot - "move $a0, $v0\n" // retrieve 32-bit exit code from v0 - "li $v0, 4001\n" // NR_exit =3D=3D 4001 - "syscall\n" - ".end __start\n" - ".set pop\n" - ""); +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __start= (void) +{ + __asm__ volatile ( + //".set nomips16\n" + ".set push\n" + ".set noreorder\n" + ".option pic0\n" + //".ent __start\n" + //"__start:\n" + "lw $a0,($sp)\n" // argc was in the stack + "addiu $a1, $sp, 4\n" // argv =3D sp + 4 + "sll $a2, $a0, 2\n" // a2 =3D argc * 4 + "add $a2, $a2, $a1\n" // envp =3D argv + 4*argc ... + "addiu $a2, $a2, 4\n" // ... + 4 + "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! + "jal main\n" // main() returns the status code, we'll exit wi= th it. + "nop\n" // delayed slot + "move $a0, $v0\n" // retrieve 32-bit exit code from v0 + "li $v0, 4001\n" // NR_exit =3D=3D 4001 + "syscall\n" + //".end __start\n" + ".set pop\n" + ); + __builtin_unreachable(); +} =20 #endif // _NOLIBC_ARCH_MIPS_H diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-= riscv.h index a3bdd9803f8c..bee769e6885c 100644 --- a/tools/include/nolibc/arch-riscv.h +++ b/tools/include/nolibc/arch-riscv.h @@ -183,22 +183,24 @@ struct sys_stat_struct { }) =20 /* startup code */ -__asm__ (".section .text\n" - ".weak _start\n" - "_start:\n" - ".option push\n" - ".option norelax\n" - "lla gp, __global_pointer$\n" - ".option pop\n" - "lw a0, 0(sp)\n" // argc (a0) was in the stack - "add a1, sp, "SZREG"\n" // argv (a1) =3D sp - "slli a2, a0, "PTRLOG"\n" // envp (a2) =3D SZREG*argc ... - "add a2, a2, "SZREG"\n" // + SZREG (skip null) - "add a2,a2,a1\n" // + argv - "andi sp,a1,-16\n" // sp must be 16-byte aligned - "call main\n" // main() returns the status code, we'll = exit with it. - "li a7, 93\n" // NR_exit =3D=3D 93 - "ecall\n" - ""); +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) +{ + __asm__ volatile ( + ".option push\n" + ".option norelax\n" + "lla gp, __global_pointer$\n" + ".option pop\n" + "lw a0, 0(sp)\n" // argc (a0) was in the stack + "add a1, sp, "SZREG"\n" // argv (a1) =3D sp + "slli a2, a0, "PTRLOG"\n" // envp (a2) =3D SZREG*argc ... + "add a2, a2, "SZREG"\n" // + SZREG (skip null) + "add a2,a2,a1\n" // + argv + "andi sp,a1,-16\n" // sp must be 16-byte aligned + "call main\n" // main() returns the status code, we'll ex= it with it. + "li a7, 93\n" // NR_exit =3D=3D 93 + "ecall\n" + ); + __builtin_unreachable(); +} =20 #endif // _NOLIBC_ARCH_RISCV_H diff --git a/tools/include/nolibc/arch-s390.h b/tools/include/nolibc/arch-s= 390.h index 76bc8fdaf922..2c0b8847c050 100644 --- a/tools/include/nolibc/arch-s390.h +++ b/tools/include/nolibc/arch-s390.h @@ -172,27 +172,29 @@ struct sys_stat_struct { }) =20 /* startup code */ -__asm__ (".section .text\n" - ".weak _start\n" - "_start:\n" - "lg %r2,0(%r15)\n" /* argument count */ - "la %r3,8(%r15)\n" /* argument pointers */ - - "xgr %r0,%r0\n" /* r0 will be our NULL value */ - /* search for envp */ - "lgr %r4,%r3\n" /* start at argv */ - "0:\n" - "clg %r0,0(%r4)\n" /* entry zero? */ - "la %r4,8(%r4)\n" /* advance pointer */ - "jnz 0b\n" /* no -> test next pointer */ - /* yes -> r4 now contains start of envp */ - - "aghi %r15,-160\n" /* allocate new stackframe */ - "xc 0(8,%r15),0(%r15)\n" /* clear backchain */ - "brasl %r14,main\n" /* ret value of main is arg to exit */ - "lghi %r1,1\n" /* __NR_exit */ - "svc 0\n" - ""); +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) +{ + __asm__ volatile ( + "lg %r2,0(%r15)\n" /* argument count */ + "la %r3,8(%r15)\n" /* argument pointers */ + + "xgr %r0,%r0\n" /* r0 will be our NULL value */ + /* search for envp */ + "lgr %r4,%r3\n" /* start at argv */ + "0:\n" + "clg %r0,0(%r4)\n" /* entry zero? */ + "la %r4,8(%r4)\n" /* advance pointer */ + "jnz 0b\n" /* no -> test next pointer */ + /* yes -> r4 now contains start of envp */ + + "aghi %r15,-160\n" /* allocate new stackframe */ + "xc 0(8,%r15),0(%r15)\n" /* clear backchain */ + "brasl %r14,main\n" /* ret value of main is arg to exit */ + "lghi %r1,1\n" /* __NR_exit */ + "svc 0\n" + ); + __builtin_unreachable(); +} =20 struct s390_mmap_arg_struct { unsigned long addr; diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch= -x86_64.h index 0e1e9eb8545d..c70a84612a9e 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -197,19 +197,21 @@ struct sys_stat_struct { * 2) The deepest stack frame should be zero (the %rbp). * */ -__asm__ (".section .text\n" - ".weak _start\n" - "_start:\n" - "pop %rdi\n" // argc (first arg, %rdi) - "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) - "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) - "xor %ebp, %ebp\n" // zero the stack frame - "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned b= efore call - "call main\n" // main() returns the status code, we'll e= xit with it. - "mov %eax, %edi\n" // retrieve exit code (32 bit) - "mov $60, %eax\n" // NR_exit =3D=3D 60 - "syscall\n" // really exit - "hlt\n" // ensure it does not return - ""); +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) +{ + __asm__ volatile ( + "pop %rdi\n" // argc (first arg, %rdi) + "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) + "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) + "xor %ebp, %ebp\n" // zero the stack frame + "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned bef= ore call + "call main\n" // main() returns the status code, we'll exi= t with it. + "mov %eax, %edi\n" // retrieve exit code (32 bit) + "mov $60, %eax\n" // NR_exit =3D=3D 60 + "syscall\n" // really exit + "hlt\n" // ensure it does not return + ); + __builtin_unreachable(); +} =20 #endif // _NOLIBC_ARCH_X86_64_H --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61238C54EBE for ; Mon, 9 Jan 2023 08:44:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233369AbjAIIo2 (ORCPT ); Mon, 9 Jan 2023 03:44:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236757AbjAIImj (ORCPT ); Mon, 9 Jan 2023 03:42:39 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7E9AD14026 for ; Mon, 9 Jan 2023 00:42:33 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gCBD027421; Mon, 9 Jan 2023 09:42:12 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 02/22] tools/nolibc: enable support for thumb1 mode for ARM Date: Mon, 9 Jan 2023 09:41:48 +0100 Message-Id: <20230109084208.27355-3-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Passing -mthumb to the kernel.org arm toolchain failed to build because it defaults to armv5 hence thumb1, which has a fairly limited instruction set compared to thumb2 enabled with armv7 that is much more complete. It's not very difficult to adjust the instructions to also build on thumb1, it only adds a total of 3 instructions, so it's worth doing it at least to ease use by casual testers. It was verified that the adjusted code now builds and works fine for armv5, thumb1, armv7 and thumb2, as long as frame pointers are not used. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-arm.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-ar= m.h index 875b21975137..e4ba77b0310f 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -180,10 +180,16 @@ void __attribute__((weak,noreturn,optimize("omit-fram= e-pointer"))) _start(void) __asm__ volatile ( "pop {%r0}\n" // argc was in the stack "mov %r1, %sp\n" // argv =3D sp - "add %r2, %r1, %r0, lsl #2\n" // envp =3D argv + 4*argc ... - "add %r2, %r2, $4\n" // ... + 4 - "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the - "mov %sp, %r3\n" // callee, an bl doesn't push (lr= =3Dpc) + + "add %r2, %r0, $1\n" // envp =3D (argc + 1) ... + "lsl %r2, %r2, $2\n" // * 4 ... + "add %r2, %r2, %r1\n" // + argv + + "mov %r3, $8\n" // AAPCS : sp must be 8-byte aligned in the + "neg %r3, %r3\n" // callee, and bl doesn't push (lr= =3Dpc) + "and %r3, %r3, %r1\n" // so we do sp =3D r1(=3Dsp) & r3(=3D-8); + "mov %sp, %r3\n" // + "bl main\n" // main() returns the status code, we'll e= xit with it. "movs r7, $1\n" // NR_exit =3D=3D 1 "svc $0x00\n" --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D865C5479D for ; Mon, 9 Jan 2023 08:46:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236810AbjAIIqD (ORCPT ); Mon, 9 Jan 2023 03:46:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236852AbjAIIn6 (ORCPT ); Mon, 9 Jan 2023 03:43:58 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6342414013 for ; Mon, 9 Jan 2023 00:42:42 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gCUY027422; Mon, 9 Jan 2023 09:42:12 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 03/22] tools/nolibc: support thumb mode with frame pointers on ARM Date: Mon, 9 Jan 2023 09:41:49 +0100 Message-Id: <20230109084208.27355-4-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In Thumb mode, register r7 is normally used to store the frame pointer. By default when optimizing at -Os there's no frame pointer so this works fine. But if no optimization is set, then build errors occur, indicating that r7 cannot not be used. It's difficult to cheat because it's the compiler that is complaining, not the assembler, so it's not even possible to report that the register was clobbered. The solution consists in saving and restoring r7 around the syscall, but this slightly inflates the code. The syscall number is passed via r6 which is never used by syscalls. The current patch adds a few macroes which do that only in Thumb mode, and which continue to directly assign the syscall number to register r7 in ARM mode. Now this always builds and works for all modes (tested on Arm, Thumbv1, Thumbv2 modes, at -Os, -O0, -O0 -fomit-frame-pointer). The code is very slightly inflated in thumb-mode without frame-pointers compared to previously (e.g. 7928 vs 7864 bytes for nolibc-test) but at least it's always operational. And it's possible to disable this mechanism by setting NOLIBC_OMIT_FRAME_POINTER. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-arm.h | 60 ++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-ar= m.h index e4ba77b0310f..ef94df2d93d5 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -70,20 +70,44 @@ struct sys_stat_struct { * don't have to experience issues with register constraints. * - the syscall number is always specified last in order to allow to fo= rce * some registers before (gcc refuses a %-register at the last positio= n). + * - in thumb mode without -fomit-frame-pointer, r7 is also used to stor= e the + * frame pointer, and we cannot directly assign it as a register varia= ble, + * nor can we clobber it. Instead we assign the r6 register and swap it + * with r7 before calling svc, and r6 is marked as clobbered. + * We're just using any regular register which we assign to r7 after s= aving + * it. * * Also, ARM supports the old_select syscall if newselect is not available */ #define __ARCH_WANT_SYS_OLD_SELECT =20 +#if (defined(__THUMBEB__) || defined(__THUMBEL__)) && \ + !defined(NOLIBC_OMIT_FRAME_POINTER) +/* swap r6,r7 needed in Thumb mode since we can't use nor clobber r7 */ +#define _NOLIBC_SYSCALL_REG "r6" +#define _NOLIBC_THUMB_SET_R7 "eor r7, r6\neor r6, r7\neor r7, r6\n" +#define _NOLIBC_THUMB_RESTORE_R7 "mov r7, r6\n" + +#else /* we're in ARM mode */ +/* in Arm mode we can directly use r7 */ +#define _NOLIBC_SYSCALL_REG "r7" +#define _NOLIBC_THUMB_SET_R7 "" +#define _NOLIBC_THUMB_RESTORE_R7 "" + +#endif /* end THUMB */ + #define my_syscall0(num) = \ ({ = \ - register long _num __asm__ ("r7") =3D (num); \ + register long _num __asm__(_NOLIBC_SYSCALL_REG) =3D (num); \ register long _arg1 __asm__ ("r0"); \ \ __asm__ volatile ( \ + _NOLIBC_THUMB_SET_R7 \ "svc #0\n" \ - : "=3Dr"(_arg1) \ - : "r"(_num) \ + _NOLIBC_THUMB_RESTORE_R7 \ + : "=3Dr"(_arg1), "=3Dr"(_num) \ + : "r"(_arg1), \ + "r"(_num) \ : "memory", "cc", "lr" \ ); \ _arg1; \ @@ -91,12 +115,14 @@ struct sys_stat_struct { =20 #define my_syscall1(num, arg1) = \ ({ = \ - register long _num __asm__ ("r7") =3D (num); \ + register long _num __asm__(_NOLIBC_SYSCALL_REG) =3D (num); \ register long _arg1 __asm__ ("r0") =3D (long)(arg1); \ \ __asm__ volatile ( \ + _NOLIBC_THUMB_SET_R7 \ "svc #0\n" \ - : "=3Dr"(_arg1) \ + _NOLIBC_THUMB_RESTORE_R7 \ + : "=3Dr"(_arg1), "=3Dr" (_num) \ : "r"(_arg1), \ "r"(_num) \ : "memory", "cc", "lr" \ @@ -106,13 +132,15 @@ struct sys_stat_struct { =20 #define my_syscall2(num, arg1, arg2) = \ ({ = \ - register long _num __asm__ ("r7") =3D (num); \ + register long _num __asm__(_NOLIBC_SYSCALL_REG) =3D (num); \ register long _arg1 __asm__ ("r0") =3D (long)(arg1); \ register long _arg2 __asm__ ("r1") =3D (long)(arg2); \ \ __asm__ volatile ( \ + _NOLIBC_THUMB_SET_R7 \ "svc #0\n" \ - : "=3Dr"(_arg1) \ + _NOLIBC_THUMB_RESTORE_R7 \ + : "=3Dr"(_arg1), "=3Dr" (_num) \ : "r"(_arg1), "r"(_arg2), \ "r"(_num) \ : "memory", "cc", "lr" \ @@ -122,14 +150,16 @@ struct sys_stat_struct { =20 #define my_syscall3(num, arg1, arg2, arg3) = \ ({ = \ - register long _num __asm__ ("r7") =3D (num); \ + register long _num __asm__(_NOLIBC_SYSCALL_REG) =3D (num); \ register long _arg1 __asm__ ("r0") =3D (long)(arg1); \ register long _arg2 __asm__ ("r1") =3D (long)(arg2); \ register long _arg3 __asm__ ("r2") =3D (long)(arg3); \ \ __asm__ volatile ( \ + _NOLIBC_THUMB_SET_R7 \ "svc #0\n" \ - : "=3Dr"(_arg1) \ + _NOLIBC_THUMB_RESTORE_R7 \ + : "=3Dr"(_arg1), "=3Dr" (_num) \ : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ "r"(_num) \ : "memory", "cc", "lr" \ @@ -139,15 +169,17 @@ struct sys_stat_struct { =20 #define my_syscall4(num, arg1, arg2, arg3, arg4) = \ ({ = \ - register long _num __asm__ ("r7") =3D (num); \ + register long _num __asm__(_NOLIBC_SYSCALL_REG) =3D (num); \ register long _arg1 __asm__ ("r0") =3D (long)(arg1); \ register long _arg2 __asm__ ("r1") =3D (long)(arg2); \ register long _arg3 __asm__ ("r2") =3D (long)(arg3); \ register long _arg4 __asm__ ("r3") =3D (long)(arg4); \ \ __asm__ volatile ( \ + _NOLIBC_THUMB_SET_R7 \ "svc #0\n" \ - : "=3Dr"(_arg1) \ + _NOLIBC_THUMB_RESTORE_R7 \ + : "=3Dr"(_arg1), "=3Dr" (_num) \ : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ "r"(_num) \ : "memory", "cc", "lr" \ @@ -157,7 +189,7 @@ struct sys_stat_struct { =20 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) = \ ({ = \ - register long _num __asm__ ("r7") =3D (num); \ + register long _num __asm__(_NOLIBC_SYSCALL_REG) =3D (num); \ register long _arg1 __asm__ ("r0") =3D (long)(arg1); \ register long _arg2 __asm__ ("r1") =3D (long)(arg2); \ register long _arg3 __asm__ ("r2") =3D (long)(arg3); \ @@ -165,8 +197,10 @@ struct sys_stat_struct { register long _arg5 __asm__ ("r4") =3D (long)(arg5); \ \ __asm__ volatile ( \ + _NOLIBC_THUMB_SET_R7 \ "svc #0\n" \ - : "=3Dr" (_arg1) \ + _NOLIBC_THUMB_RESTORE_R7 \ + : "=3Dr"(_arg1), "=3Dr" (_num) \ : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ "r"(_num) \ : "memory", "cc", "lr" \ --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89EAAC54EBE for ; Mon, 9 Jan 2023 08:47:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236764AbjAIIqv (ORCPT ); Mon, 9 Jan 2023 03:46:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236898AbjAIIpy (ORCPT ); Mon, 9 Jan 2023 03:45:54 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BCE50140F9 for ; Mon, 9 Jan 2023 00:42:50 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gCbA027423; Mon, 9 Jan 2023 09:42:12 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 04/22] tools/nolibc: remove local definitions of O_* flags for open/fcntl Date: Mon, 9 Jan 2023 09:41:50 +0100 Message-Id: <20230109084208.27355-5-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The historic nolibc code did not include asm/fcntl.h and had to define the various O_RDWR etc macros in each arch-specific file (since such values differ between certain archs). This was found at least once to induce bugs due to wrong definitions. Let's get rid of all of them and include asm/nolibc.h from sys.h instead. This was verified to work properly on all supported architectures. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-aarch64.h | 12 ------------ tools/include/nolibc/arch-arm.h | 12 ------------ tools/include/nolibc/arch-i386.h | 12 ------------ tools/include/nolibc/arch-mips.h | 12 ------------ tools/include/nolibc/arch-riscv.h | 12 ------------ tools/include/nolibc/arch-s390.h | 12 ------------ tools/include/nolibc/arch-x86_64.h | 12 ------------ tools/include/nolibc/sys.h | 1 + 8 files changed, 1 insertion(+), 84 deletions(-) diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arc= h-aarch64.h index 4d263661411f..f480993159ec 100644 --- a/tools/include/nolibc/arch-aarch64.h +++ b/tools/include/nolibc/arch-aarch64.h @@ -7,18 +7,6 @@ #ifndef _NOLIBC_ARCH_AARCH64_H #define _NOLIBC_ARCH_AARCH64_H =20 -/* O_* macros for fcntl/open are architecture-specific */ -#define O_RDONLY 0 -#define O_WRONLY 1 -#define O_RDWR 2 -#define O_CREAT 0x40 -#define O_EXCL 0x80 -#define O_NOCTTY 0x100 -#define O_TRUNC 0x200 -#define O_APPEND 0x400 -#define O_NONBLOCK 0x800 -#define O_DIRECTORY 0x4000 - /* The struct returned by the newfstatat() syscall. Differs slightly from = the * x86_64's stat one by field ordering, so be careful. */ diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-ar= m.h index ef94df2d93d5..48bd95492c87 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -7,18 +7,6 @@ #ifndef _NOLIBC_ARCH_ARM_H #define _NOLIBC_ARCH_ARM_H =20 -/* O_* macros for fcntl/open are architecture-specific */ -#define O_RDONLY 0 -#define O_WRONLY 1 -#define O_RDWR 2 -#define O_CREAT 0x40 -#define O_EXCL 0x80 -#define O_NOCTTY 0x100 -#define O_TRUNC 0x200 -#define O_APPEND 0x400 -#define O_NONBLOCK 0x800 -#define O_DIRECTORY 0x4000 - /* The struct returned by the stat() syscall, 32-bit only, the syscall ret= urns * exactly 56 bytes (stops before the unused array). In big endian, the fo= rmat * differs as devices are returned as short only. diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i= 386.h index b1bed2d87f74..ef2a836ee667 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -7,18 +7,6 @@ #ifndef _NOLIBC_ARCH_I386_H #define _NOLIBC_ARCH_I386_H =20 -/* O_* macros for fcntl/open are architecture-specific */ -#define O_RDONLY 0 -#define O_WRONLY 1 -#define O_RDWR 2 -#define O_CREAT 0x40 -#define O_EXCL 0x80 -#define O_NOCTTY 0x100 -#define O_TRUNC 0x200 -#define O_APPEND 0x400 -#define O_NONBLOCK 0x800 -#define O_DIRECTORY 0x10000 - /* The struct returned by the stat() syscall, 32-bit only, the syscall ret= urns * exactly 56 bytes (stops before the unused array). */ diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-m= ips.h index 11270ef25ea5..a3764f6d267e 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -7,18 +7,6 @@ #ifndef _NOLIBC_ARCH_MIPS_H #define _NOLIBC_ARCH_MIPS_H =20 -/* O_* macros for fcntl/open are architecture-specific */ -#define O_RDONLY 0 -#define O_WRONLY 1 -#define O_RDWR 2 -#define O_APPEND 0x0008 -#define O_NONBLOCK 0x0080 -#define O_CREAT 0x0100 -#define O_TRUNC 0x0200 -#define O_EXCL 0x0400 -#define O_NOCTTY 0x0800 -#define O_DIRECTORY 0x10000 - /* The struct returned by the stat() syscall. 88 bytes are returned by the * syscall. */ diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-= riscv.h index bee769e6885c..c2b5db383d96 100644 --- a/tools/include/nolibc/arch-riscv.h +++ b/tools/include/nolibc/arch-riscv.h @@ -7,18 +7,6 @@ #ifndef _NOLIBC_ARCH_RISCV_H #define _NOLIBC_ARCH_RISCV_H =20 -/* O_* macros for fcntl/open are architecture-specific */ -#define O_RDONLY 0 -#define O_WRONLY 1 -#define O_RDWR 2 -#define O_CREAT 0x40 -#define O_EXCL 0x80 -#define O_NOCTTY 0x100 -#define O_TRUNC 0x200 -#define O_APPEND 0x400 -#define O_NONBLOCK 0x800 -#define O_DIRECTORY 0x10000 - struct sys_stat_struct { unsigned long st_dev; /* Device. */ unsigned long st_ino; /* File serial number. */ diff --git a/tools/include/nolibc/arch-s390.h b/tools/include/nolibc/arch-s= 390.h index 2c0b8847c050..b58f64d47b82 100644 --- a/tools/include/nolibc/arch-s390.h +++ b/tools/include/nolibc/arch-s390.h @@ -7,18 +7,6 @@ #define _NOLIBC_ARCH_S390_H #include =20 -/* O_* macros for fcntl/open are architecture-specific */ -#define O_RDONLY 0 -#define O_WRONLY 1 -#define O_RDWR 2 -#define O_CREAT 0x40 -#define O_EXCL 0x80 -#define O_NOCTTY 0x100 -#define O_TRUNC 0x200 -#define O_APPEND 0x400 -#define O_NONBLOCK 0x800 -#define O_DIRECTORY 0x10000 - /* The struct returned by the stat() syscall, equivalent to stat64(). The * syscall returns 116 bytes and stops in the middle of __unused. */ diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch= -x86_64.h index c70a84612a9e..8d482505c347 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -7,18 +7,6 @@ #ifndef _NOLIBC_ARCH_X86_64_H #define _NOLIBC_ARCH_X86_64_H =20 -/* O_* macros for fcntl/open are architecture-specific */ -#define O_RDONLY 0 -#define O_WRONLY 1 -#define O_RDWR 2 -#define O_CREAT 0x40 -#define O_EXCL 0x80 -#define O_NOCTTY 0x100 -#define O_TRUNC 0x200 -#define O_APPEND 0x400 -#define O_NONBLOCK 0x800 -#define O_DIRECTORY 0x10000 - /* The struct returned by the stat() syscall, equivalent to stat64(). The * syscall returns 116 bytes and stops in the middle of __unused. */ diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index a42d7c405bdc..47bf67668860 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -11,6 +11,7 @@ #include "std.h" =20 /* system includes */ +#include // for O_* #include #include // for SIGCHLD #include --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B24CC54EBD for ; Mon, 9 Jan 2023 08:47:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236947AbjAIIr0 (ORCPT ); Mon, 9 Jan 2023 03:47:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237010AbjAIIqV (ORCPT ); Mon, 9 Jan 2023 03:46:21 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 04199140C1 for ; Mon, 9 Jan 2023 00:42:58 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gC9b027424; Mon, 9 Jan 2023 09:42:12 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 05/22] tools/nolibc: make errno a weak symbol instead of a static one Date: Mon, 9 Jan 2023 09:41:51 +0100 Message-Id: <20230109084208.27355-6-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Till now errno was declared static so that it could be eliminated if unused. While the goal is commendable for tiny executables as it allows to eliminate any data and bss segments when not used, this comes with some limitations, one of which being that the errno symbol seen in different units are not the same. Even though this has never been a real issue given the nature of the programs involved till now, it happens that referencing the same symbol from multiple units can also be achieved using weak symbols, with a difference being that only one of them will be used for all of them. Compared to weak symbols, static basically have no benefit for regular programs since there are always at least a few variables in most of these, so the bss segment cannot be eliminated. E.g: $ size nolibc-test-static-errno text data bss dec hex filename 11531 0 48 11579 2d3b nolibc-test-static-errno Furthermore, the weak symbol doesn't use bss storage at all, resulting in a slightly section: $ size nolibc-test-weak-errno text data bss dec hex filename 11531 0 40 11571 2d33 nolibc-test-weak-errno This patch thus converts errno from static to weak. Signed-off-by: Willy Tarreau --- tools/include/nolibc/errno.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/include/nolibc/errno.h b/tools/include/nolibc/errno.h index 9dc4919c769b..a44486ff0477 100644 --- a/tools/include/nolibc/errno.h +++ b/tools/include/nolibc/errno.h @@ -9,11 +9,9 @@ =20 #include =20 -/* this way it will be removed if unused */ -static int errno; - #ifndef NOLIBC_IGNORE_ERRNO #define SET_ERRNO(v) do { errno =3D (v); } while (0) +int errno __attribute__((weak)); #else #define SET_ERRNO(v) do { } while (0) #endif --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E28B3C54EBD for ; Mon, 9 Jan 2023 08:49:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236728AbjAIIsC (ORCPT ); Mon, 9 Jan 2023 03:48:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236941AbjAIIrU (ORCPT ); Mon, 9 Jan 2023 03:47:20 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9E2FB15FE0 for ; Mon, 9 Jan 2023 00:43:20 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gCB9027425; Mon, 9 Jan 2023 09:42:12 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 06/22] tools/nolibc: export environ as a weak symbol on x86_64 Date: Mon, 9 Jan 2023 09:41:52 +0100 Message-Id: <20230109084208.27355-7-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The environ is retrieved from the _start code and is easy to store at this moment. Let's declare the variable weak and store the value into it. By not being static it will be visible to all units. By being weak, if some programs already declared it, they will continue to be able to use it. This was tested both with environ inherited from _start and extracted from envp. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-x86_64.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch= -x86_64.h index 8d482505c347..683702a16a61 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -178,6 +178,8 @@ struct sys_stat_struct { _ret; \ }) =20 +char **environ __attribute__((weak)); + /* startup code */ /* * x86-64 System V ABI mandates: @@ -191,6 +193,7 @@ void __attribute__((weak,noreturn,optimize("omit-frame-= pointer"))) _start(void) "pop %rdi\n" // argc (first arg, %rdi) "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) + "mov %rdx, environ\n" // save environ "xor %ebp, %ebp\n" // zero the stack frame "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned bef= ore call "call main\n" // main() returns the status code, we'll exi= t with it. --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F707C54EBD for ; Mon, 9 Jan 2023 08:49:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236714AbjAIItf (ORCPT ); Mon, 9 Jan 2023 03:49:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237005AbjAIIrv (ORCPT ); Mon, 9 Jan 2023 03:47:51 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DAA0B15F06 for ; Mon, 9 Jan 2023 00:43:32 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gDoH027426; Mon, 9 Jan 2023 09:42:13 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 07/22] tools/nolibc: export environ as a weak symbol on i386 Date: Mon, 9 Jan 2023 09:41:53 +0100 Message-Id: <20230109084208.27355-8-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The environ is retrieved from the _start code and is easy to store at this moment. Let's declare the variable weak and store the value into it. By not being static it will be visible to all units. By being weak, if some programs already declared it, they will continue to be able to use it. This was tested both with environ inherited from _start and extracted from envp. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-i386.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i= 386.h index ef2a836ee667..60b586120727 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -178,6 +178,8 @@ struct sys_stat_struct { _eax; \ }) =20 +char **environ __attribute__((weak)); + /* startup code */ /* * i386 System V ABI mandates: @@ -191,6 +193,7 @@ void __attribute__((weak,noreturn,optimize("omit-frame-= pointer"))) _start(void) "pop %eax\n" // argc (first arg, %eax) "mov %esp, %ebx\n" // argv[] (second arg, %ebx) "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) + "mov %ecx, environ\n" // save environ "xor %ebp, %ebp\n" // zero the stack frame "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned bef= ore "sub $4, %esp\n" // the call instruction (args are aligned) --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 071F4C5479D for ; Mon, 9 Jan 2023 08:49:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231176AbjAIIt2 (ORCPT ); Mon, 9 Jan 2023 03:49:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236812AbjAIIsJ (ORCPT ); Mon, 9 Jan 2023 03:48:09 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3E60C165A0 for ; Mon, 9 Jan 2023 00:43:39 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gDWp027427; Mon, 9 Jan 2023 09:42:13 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 08/22] tools/nolibc: export environ as a weak symbol on arm64 Date: Mon, 9 Jan 2023 09:41:54 +0100 Message-Id: <20230109084208.27355-9-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The environ is retrieved from the _start code and is easy to store at this moment. Let's declare the variable weak and store the value into it. By not being static it will be visible to all units. By being weak, if some programs already declared it, they will continue to be able to use it. This was tested both with environ inherited from _start and extracted from envp. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-aarch64.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arc= h-aarch64.h index f480993159ec..2e3d9adc4c4c 100644 --- a/tools/include/nolibc/arch-aarch64.h +++ b/tools/include/nolibc/arch-aarch64.h @@ -169,6 +169,8 @@ struct sys_stat_struct { _arg1; \ }) =20 +char **environ __attribute__((weak)); + /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) { @@ -178,6 +180,8 @@ void __attribute__((weak,noreturn,optimize("omit-frame-= pointer"))) _start(void) "lsl x2, x0, 3\n" // envp (x2) =3D 8*argc ... "add x2, x2, 8\n" // + 8 (skip null) "add x2, x2, x1\n" // + argv + "adrp x3, environ\n" // x3 =3D &environ (high bits) + "str x2, [x3, #:lo12:environ]\n" // store envp into environ "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee "bl main\n" // main() returns the status code, we'll exit with = it. "mov x8, 93\n" // NR_exit =3D=3D 93 --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6498C5479D for ; Mon, 9 Jan 2023 08:49:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236852AbjAIIts (ORCPT ); Mon, 9 Jan 2023 03:49:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236943AbjAIIsV (ORCPT ); Mon, 9 Jan 2023 03:48:21 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C43B5167DF for ; Mon, 9 Jan 2023 00:43:47 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gDS7027428; Mon, 9 Jan 2023 09:42:13 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 09/22] tools/nolibc: export environ as a weak symbol on arm Date: Mon, 9 Jan 2023 09:41:55 +0100 Message-Id: <20230109084208.27355-10-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The environ is retrieved from the _start code and is easy to store at this moment. Let's declare the variable weak and store the value into it. By not being static it will be visible to all units. By being weak, if some programs already declared it, they will continue to be able to use it. This was tested in arm and thumb1 and thumb2 modes, and for each mode, both with environ inherited from _start and extracted from envp. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-arm.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-ar= m.h index 48bd95492c87..79666b590e87 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -196,6 +196,8 @@ struct sys_stat_struct { _arg1; \ }) =20 +char **environ __attribute__((weak)); + /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) { @@ -206,6 +208,8 @@ void __attribute__((weak,noreturn,optimize("omit-frame-= pointer"))) _start(void) "add %r2, %r0, $1\n" // envp =3D (argc + 1) ... "lsl %r2, %r2, $2\n" // * 4 ... "add %r2, %r2, %r1\n" // + argv + "ldr %r3, 1f\n" // r3 =3D &environ (see below) + "str %r2, [r3]\n" // store envp into environ =20 "mov %r3, $8\n" // AAPCS : sp must be 8-byte aligned in the "neg %r3, %r3\n" // callee, and bl doesn't push (lr= =3Dpc) @@ -215,7 +219,10 @@ void __attribute__((weak,noreturn,optimize("omit-frame= -pointer"))) _start(void) "bl main\n" // main() returns the status code, we'll e= xit with it. "movs r7, $1\n" // NR_exit =3D=3D 1 "svc $0x00\n" - ); + ".align 2\n" // below are the pointers to a few variabl= es + "1:\n" + ".word environ\n" + ); __builtin_unreachable(); } =20 --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 641F5C54EBE for ; Mon, 9 Jan 2023 08:51:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234272AbjAIIvY (ORCPT ); Mon, 9 Jan 2023 03:51:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237101AbjAIIsi (ORCPT ); Mon, 9 Jan 2023 03:48:38 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CDB3E17419 for ; Mon, 9 Jan 2023 00:43:57 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gDk8027429; Mon, 9 Jan 2023 09:42:13 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 10/22] tools/nolibc: export environ as a weak symbol on mips Date: Mon, 9 Jan 2023 09:41:56 +0100 Message-Id: <20230109084208.27355-11-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The environ is retrieved from the _start code and is easy to store at this moment. Let's declare the variable weak and store the value into it. By not being static it will be visible to all units. By being weak, if some programs already declared it, they will continue to be able to use it. This was tested with mips24kc (BE) both with environ inherited from _start and extracted from envp. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-mips.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-m= ips.h index a3764f6d267e..7d22f7bc38b3 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -176,6 +176,8 @@ struct sys_stat_struct { _arg4 ? -_num : _num; \ }) =20 +char **environ __attribute__((weak)); + /* startup code, note that it's called __start on MIPS */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __start= (void) { @@ -191,6 +193,9 @@ void __attribute__((weak,noreturn,optimize("omit-frame-= pointer"))) __start(void) "sll $a2, $a0, 2\n" // a2 =3D argc * 4 "add $a2, $a2, $a1\n" // envp =3D argv + 4*argc ... "addiu $a2, $a2, 4\n" // ... + 4 + "lui $a3, %hi(environ)\n" // load environ into a3 (hi) + "addiu $a3, %lo(environ)\n" // load environ into a3 (lo) + "sw $a2,($a3)\n" // store envp(a2) into environ "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! --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 28447C5479D for ; Mon, 9 Jan 2023 08:51:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236782AbjAIIvh (ORCPT ); Mon, 9 Jan 2023 03:51:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236692AbjAIItf (ORCPT ); Mon, 9 Jan 2023 03:49:35 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1AA1D1742E for ; Mon, 9 Jan 2023 00:44:06 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gDqQ027430; Mon, 9 Jan 2023 09:42:13 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 11/22] tools/nolibc: export environ as a weak symbol on riscv Date: Mon, 9 Jan 2023 09:41:57 +0100 Message-Id: <20230109084208.27355-12-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The environ is retrieved from the _start code and is easy to store at this moment. Let's declare the variable weak and store the value into it. By not being static it will be visible to all units. By being weak, if some programs already declared it, they will continue to be able to use it. This was tested on riscv64 both with environ inherited from _start and extracted from envp. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-riscv.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-= riscv.h index c2b5db383d96..1608e6bd94b9 100644 --- a/tools/include/nolibc/arch-riscv.h +++ b/tools/include/nolibc/arch-riscv.h @@ -170,6 +170,8 @@ struct sys_stat_struct { _arg1; \ }) =20 +char **environ __attribute__((weak)); + /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) { @@ -183,6 +185,8 @@ void __attribute__((weak,noreturn,optimize("omit-frame-= pointer"))) _start(void) "slli a2, a0, "PTRLOG"\n" // envp (a2) =3D SZREG*argc ... "add a2, a2, "SZREG"\n" // + SZREG (skip null) "add a2,a2,a1\n" // + argv + "lui a3, %hi(environ)\n" // a3 =3D &environ (high bits) + "sd a2,%lo(environ)(a3)\n" // store envp(a2) into environ "andi sp,a1,-16\n" // sp must be 16-byte aligned "call main\n" // main() returns the status code, we'll ex= it with it. "li a7, 93\n" // NR_exit =3D=3D 93 --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CBA2C54EBE for ; Mon, 9 Jan 2023 08:43:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236920AbjAIInf (ORCPT ); Mon, 9 Jan 2023 03:43:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236859AbjAIIm1 (ORCPT ); Mon, 9 Jan 2023 03:42:27 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 53E9F13F68 for ; Mon, 9 Jan 2023 00:42:26 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gDhN027431; Mon, 9 Jan 2023 09:42:13 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Sven Schnelle , Willy Tarreau Subject: [PATCH 12/22] tools/nolibc: export environ as a weak symbol on s390 Date: Mon, 9 Jan 2023 09:41:58 +0100 Message-Id: <20230109084208.27355-13-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Sven Schnelle The environ is retrieved from the _start code and is easy to store at this moment. Let's declare the variable weak and store the value into it. By not being static it will be visible to all units. By being weak, if some programs already declared it, they will continue to be able to use it. This was tested on s390 both with environ inherited from _start and extracted from envp. Signed-off-by: Sven Schnelle Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-s390.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/include/nolibc/arch-s390.h b/tools/include/nolibc/arch-s= 390.h index b58f64d47b82..039b454e79f0 100644 --- a/tools/include/nolibc/arch-s390.h +++ b/tools/include/nolibc/arch-s390.h @@ -159,6 +159,8 @@ struct sys_stat_struct { _arg1; \ }) =20 +char **environ __attribute__((weak)); + /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) { @@ -174,6 +176,8 @@ void __attribute__((weak,noreturn,optimize("omit-frame-= pointer"))) _start(void) "la %r4,8(%r4)\n" /* advance pointer */ "jnz 0b\n" /* no -> test next pointer */ /* yes -> r4 now contains start of envp */ + "larl %r1,environ\n" + "stg %r4,0(%r1)\n" =20 "aghi %r15,-160\n" /* allocate new stackframe */ "xc 0(8,%r15),0(%r15)\n" /* clear backchain */ --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E8E8C677F1 for ; Mon, 9 Jan 2023 08:43:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236939AbjAIInj (ORCPT ); Mon, 9 Jan 2023 03:43:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235659AbjAIIm3 (ORCPT ); Mon, 9 Jan 2023 03:42:29 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 20EAF13F64 for ; Mon, 9 Jan 2023 00:42:27 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gDGl027432; Mon, 9 Jan 2023 09:42:13 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 13/22] tools/nolibc: add auxiliary vector retrieval for i386 Date: Mon, 9 Jan 2023 09:41:59 +0100 Message-Id: <20230109084208.27355-14-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the _start block we now iterate over envp to find the auxiliary vector after the NULL. The pointer is saved into an _auxv variable that is marked as weak so that it's accessible from multiple units. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-i386.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i= 386.h index 60b586120727..e8d0cf545bf1 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -179,6 +179,7 @@ struct sys_stat_struct { }) =20 char **environ __attribute__((weak)); +const unsigned long *_auxv __attribute__((weak)); =20 /* startup code */ /* @@ -195,6 +196,12 @@ void __attribute__((weak,noreturn,optimize("omit-frame= -pointer"))) _start(void) "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) "mov %ecx, environ\n" // save environ "xor %ebp, %ebp\n" // zero the stack frame + "mov %ecx, %edx\n" // search for auxv (follows NULL after last = env) + "0:\n" + "add $4, %edx\n" // search for auxv using edx, it follows the + "cmp -4(%edx), %ebp\n" // ... NULL after last env (ebp is zero here) + "jnz 0b\n" + "mov %edx, _auxv\n" // save it into _auxv "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned bef= ore "sub $4, %esp\n" // the call instruction (args are aligned) "push %ecx\n" // push all registers on the stack so that we --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD684C5479D for ; Mon, 9 Jan 2023 08:45:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236820AbjAIIpA (ORCPT ); Mon, 9 Jan 2023 03:45:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236812AbjAIIms (ORCPT ); Mon, 9 Jan 2023 03:42:48 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5C6F813F49 for ; Mon, 9 Jan 2023 00:42:36 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gE6N027433; Mon, 9 Jan 2023 09:42:14 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 14/22] tools/nolibc: add auxiliary vector retrieval for x86_64 Date: Mon, 9 Jan 2023 09:42:00 +0100 Message-Id: <20230109084208.27355-15-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the _start block we now iterate over envp to find the auxiliary vector after the NULL. The pointer is saved into an _auxv variable that is marked as weak so that it's accessible from multiple units. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-x86_64.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch= -x86_64.h index 683702a16a61..17f6751208e7 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -179,6 +179,7 @@ struct sys_stat_struct { }) =20 char **environ __attribute__((weak)); +const unsigned long *_auxv __attribute__((weak)); =20 /* startup code */ /* @@ -195,6 +196,12 @@ void __attribute__((weak,noreturn,optimize("omit-frame= -pointer"))) _start(void) "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) "mov %rdx, environ\n" // save environ "xor %ebp, %ebp\n" // zero the stack frame + "mov %rdx, %rax\n" // search for auxv (follows NULL after last = env) + "0:\n" + "add $8, %rax\n" // search for auxv using rax, it follows the + "cmp -8(%rax), %rbp\n" // ... NULL after last env (rbp is zero here) + "jnz 0b\n" + "mov %rax, _auxv\n" // save it into _auxv "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned bef= ore call "call main\n" // main() returns the status code, we'll exi= t with it. "mov %eax, %edi\n" // retrieve exit code (32 bit) --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 697C3C54EBD for ; Mon, 9 Jan 2023 08:47:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236650AbjAIIqs (ORCPT ); Mon, 9 Jan 2023 03:46:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236968AbjAIIp4 (ORCPT ); Mon, 9 Jan 2023 03:45:56 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DCD31140B4 for ; Mon, 9 Jan 2023 00:42:52 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gFrt027434; Mon, 9 Jan 2023 09:42:15 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 15/22] tools/nolibc: add auxiliary vector retrieval for arm64 Date: Mon, 9 Jan 2023 09:42:01 +0100 Message-Id: <20230109084208.27355-16-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the _start block we now iterate over envp to find the auxiliary vector after the NULL. The pointer is saved into an _auxv variable that is marked as weak so that it's accessible from multiple units. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-aarch64.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arc= h-aarch64.h index 2e3d9adc4c4c..383baddef701 100644 --- a/tools/include/nolibc/arch-aarch64.h +++ b/tools/include/nolibc/arch-aarch64.h @@ -170,6 +170,7 @@ struct sys_stat_struct { }) =20 char **environ __attribute__((weak)); +const unsigned long *_auxv __attribute__((weak)); =20 /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) @@ -182,6 +183,12 @@ void __attribute__((weak,noreturn,optimize("omit-frame= -pointer"))) _start(void) "add x2, x2, x1\n" // + argv "adrp x3, environ\n" // x3 =3D &environ (high bits) "str x2, [x3, #:lo12:environ]\n" // store envp into environ + "mov x4, x2\n" // search for auxv (follows NULL after last env) + "0:\n" + "ldr x5, [x4], 8\n" // x5 =3D *x4; x4 +=3D 8 + "cbnz x5, 0b\n" // and stop at NULL after last env + "adrp x3, _auxv\n" // x3 =3D &_auxv (high bits) + "str x4, [x3, #:lo12:_auxv]\n" // store x4 into _auxv "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee "bl main\n" // main() returns the status code, we'll exit with = it. "mov x8, 93\n" // NR_exit =3D=3D 93 --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50EE9C5479D for ; Mon, 9 Jan 2023 08:47:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233854AbjAIIrk (ORCPT ); Mon, 9 Jan 2023 03:47:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237034AbjAIIqY (ORCPT ); Mon, 9 Jan 2023 03:46:24 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 960851581D for ; Mon, 9 Jan 2023 00:43:03 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gFI5027435; Mon, 9 Jan 2023 09:42:15 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 16/22] tools/nolibc: add auxiliary vector retrieval for arm Date: Mon, 9 Jan 2023 09:42:02 +0100 Message-Id: <20230109084208.27355-17-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the _start block we now iterate over envp to find the auxiliary vector after the NULL. The pointer is saved into an _auxv variable that is marked as weak so that it's accessible from multiple units. Signed-off-by: Willy Tarreau It was tested in arm, thumb1 and thumb2 modes. --- tools/include/nolibc/arch-arm.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-ar= m.h index 79666b590e87..42499f23e73c 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -197,6 +197,7 @@ struct sys_stat_struct { }) =20 char **environ __attribute__((weak)); +const unsigned long *_auxv __attribute__((weak)); =20 /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) @@ -211,6 +212,16 @@ void __attribute__((weak,noreturn,optimize("omit-frame= -pointer"))) _start(void) "ldr %r3, 1f\n" // r3 =3D &environ (see below) "str %r2, [r3]\n" // store envp into environ =20 + "mov r4, r2\n" // search for auxv (follows NULL after las= t env) + "0:\n" + "mov r5, r4\n" // r5 =3D r4 + "add r4, r4, #4\n" // r4 +=3D 4 + "ldr r5,[r5]\n" // r5 =3D *r5 =3D *(r4-4) + "cmp r5, #0\n" // and stop at NULL after last env + "bne 0b\n" + "ldr %r3, 2f\n" // r3 =3D &_auxv (low bits) + "str r4, [r3]\n" // store r4 into _auxv + "mov %r3, $8\n" // AAPCS : sp must be 8-byte aligned in the "neg %r3, %r3\n" // callee, and bl doesn't push (lr= =3Dpc) "and %r3, %r3, %r1\n" // so we do sp =3D r1(=3Dsp) & r3(=3D-8); @@ -222,6 +233,8 @@ void __attribute__((weak,noreturn,optimize("omit-frame-= pointer"))) _start(void) ".align 2\n" // below are the pointers to a few variabl= es "1:\n" ".word environ\n" + "2:\n" + ".word _auxv\n" ); __builtin_unreachable(); } --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2065C54EBD for ; Mon, 9 Jan 2023 08:50:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236880AbjAIIt5 (ORCPT ); Mon, 9 Jan 2023 03:49:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236914AbjAIIrg (ORCPT ); Mon, 9 Jan 2023 03:47:36 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4DA0415FE7 for ; Mon, 9 Jan 2023 00:43:25 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gF3D027436; Mon, 9 Jan 2023 09:42:15 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 17/22] tools/nolibc: add auxiliary vector retrieval for riscv Date: Mon, 9 Jan 2023 09:42:03 +0100 Message-Id: <20230109084208.27355-18-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the _start block we now iterate over envp to find the auxiliary vector after the NULL. The pointer is saved into an _auxv variable that is marked as weak so that it's accessible from multiple units. It was tested on riscv64 only. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-riscv.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-= riscv.h index 1608e6bd94b9..e197fcb10ac0 100644 --- a/tools/include/nolibc/arch-riscv.h +++ b/tools/include/nolibc/arch-riscv.h @@ -171,6 +171,7 @@ struct sys_stat_struct { }) =20 char **environ __attribute__((weak)); +const unsigned long *_auxv __attribute__((weak)); =20 /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) @@ -185,6 +186,15 @@ void __attribute__((weak,noreturn,optimize("omit-frame= -pointer"))) _start(void) "slli a2, a0, "PTRLOG"\n" // envp (a2) =3D SZREG*argc ... "add a2, a2, "SZREG"\n" // + SZREG (skip null) "add a2,a2,a1\n" // + argv + + "add a3, a2, zero\n" // iterate a3 over envp to find auxv (after= NULL) + "0:\n" // do { + "ld a4, 0(a3)\n" // a4 =3D *a3; + "add a3, a3, "SZREG"\n" // a3 +=3D sizeof(void*); + "bne a4, zero, 0b\n" // } while (a4); + "lui a4, %hi(_auxv)\n" // a4 =3D &_auxv (high bits) + "sd a3, %lo(_auxv)(a4)\n" // store a3 into _auxv + "lui a3, %hi(environ)\n" // a3 =3D &environ (high bits) "sd a2,%lo(environ)(a3)\n" // store envp(a2) into environ "andi sp,a1,-16\n" // sp must be 16-byte aligned --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10214C5479D for ; Mon, 9 Jan 2023 08:51:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233977AbjAIIvb (ORCPT ); Mon, 9 Jan 2023 03:51:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237026AbjAIIr4 (ORCPT ); Mon, 9 Jan 2023 03:47:56 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DEB2A164B9 for ; Mon, 9 Jan 2023 00:43:33 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gFGJ027437; Mon, 9 Jan 2023 09:42:15 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 18/22] tools/nolibc: add auxiliary vector retrieval for mips Date: Mon, 9 Jan 2023 09:42:04 +0100 Message-Id: <20230109084208.27355-19-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the _start block we now iterate over envp to find the auxiliary vector after the NULL. The pointer is saved into an _auxv variable that is marked as weak so that it's accessible from multiple units. Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-mips.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-m= ips.h index 7d22f7bc38b3..bf83432d23ed 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -177,6 +177,7 @@ struct sys_stat_struct { }) =20 char **environ __attribute__((weak)); +const unsigned long *_auxv __attribute__((weak)); =20 /* startup code, note that it's called __start on MIPS */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __start= (void) @@ -196,6 +197,16 @@ void __attribute__((weak,noreturn,optimize("omit-frame= -pointer"))) __start(void) "lui $a3, %hi(environ)\n" // load environ into a3 (hi) "addiu $a3, %lo(environ)\n" // load environ into a3 (lo) "sw $a2,($a3)\n" // store envp(a2) into environ + + "move $t0, $a2\n" // iterate t0 over envp, look for NULL + "0:" // do { + "lw $a3, ($t0)\n" // a3=3D*(t0); + "bne $a3, $0, 0b\n" // } while (a3); + "addiu $t0, $t0, 4\n" // delayed slot: t0+=3D4; + "lui $a3, %hi(_auxv)\n" // load _auxv into a3 (hi) + "addiu $a3, %lo(_auxv)\n" // load _auxv into a3 (lo) + "sw $t0, ($a3)\n" // store t0 into _auxv + "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! --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBD1AC5479D for ; Mon, 9 Jan 2023 08:49:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236762AbjAIItx (ORCPT ); Mon, 9 Jan 2023 03:49:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236868AbjAIIsP (ORCPT ); Mon, 9 Jan 2023 03:48:15 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A5DA2165B7 for ; Mon, 9 Jan 2023 00:43:43 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gF7A027438; Mon, 9 Jan 2023 09:42:15 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Sven Schnelle , Willy Tarreau Subject: [PATCH 19/22] tools/nolibc: add auxiliary vector retrieval for s390 Date: Mon, 9 Jan 2023 09:42:05 +0100 Message-Id: <20230109084208.27355-20-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Sven Schnelle In the _start block we now iterate over envp to find the auxiliary vector after the NULL. The pointer is saved into an _auxv variable that is marked as weak so that it's accessible from multiple units. Signed-off-by: Sven Schnelle Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-s390.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/include/nolibc/arch-s390.h b/tools/include/nolibc/arch-s= 390.h index 039b454e79f0..6b0e54ed543d 100644 --- a/tools/include/nolibc/arch-s390.h +++ b/tools/include/nolibc/arch-s390.h @@ -160,6 +160,7 @@ struct sys_stat_struct { }) =20 char **environ __attribute__((weak)); +const unsigned long *_auxv __attribute__((weak)); =20 /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(= void) @@ -179,6 +180,15 @@ void __attribute__((weak,noreturn,optimize("omit-frame= -pointer"))) _start(void) "larl %r1,environ\n" "stg %r4,0(%r1)\n" =20 + /* search for auxv */ + "lgr %r5,%r4\n" /* start at envp */ + "1:\n" + "clg %r0,0(%r5)\n" /* entry zero? */ + "la %r5,8(%r5)\n" /* advance pointer */ + "jnz 1b\n" /* no -> test next pointer */ + "larl %r1,_auxv\n" /* yes -> store value in _auxv */ + "stg %r5,0(%r1)\n" + "aghi %r15,-160\n" /* allocate new stackframe */ "xc 0(8,%r15),0(%r15)\n" /* clear backchain */ "brasl %r14,main\n" /* ret value of main is arg to exit */ --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F3DDC5479D for ; Mon, 9 Jan 2023 08:43:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236876AbjAIInP (ORCPT ); Mon, 9 Jan 2023 03:43:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236844AbjAIImX (ORCPT ); Mon, 9 Jan 2023 03:42:23 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8045012D2D for ; Mon, 9 Jan 2023 00:42:22 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gFRG027439; Mon, 9 Jan 2023 09:42:15 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Ammar Faizi Subject: [PATCH 20/22] nolibc/stdlib: Implement `getauxval(3)` function Date: Mon, 9 Jan 2023 09:42:06 +0100 Message-Id: <20230109084208.27355-21-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Ammar Faizi Previous commits save the address of the auxiliary vector into a global variable @_auxv. This commit creates a new function 'getauxval()' as a helper function to get the auxv value based on the given key. The behavior of this function is identic with the function documented in 'man 3 getauxval'. This function is also needed to implement 'getpagesize()' function that we will wire up in the next patches. Signed-off-by: Ammar Faizi --- tools/include/nolibc/stdlib.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index a24000d1e822..894c955d027e 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -12,6 +12,7 @@ #include "types.h" #include "sys.h" #include "string.h" +#include =20 struct nolibc_heap { size_t len; @@ -108,6 +109,32 @@ char *getenv(const char *name) return _getenv(name, environ); } =20 +static __attribute__((unused)) +unsigned long getauxval(unsigned long type) +{ + const unsigned long *auxv =3D _auxv; + unsigned long ret; + + if (!auxv) + return 0; + + while (1) { + if (!auxv[0] && !auxv[1]) { + ret =3D 0; + break; + } + + if (auxv[0] =3D=3D type) { + ret =3D auxv[1]; + break; + } + + auxv +=3D 2; + } + + return ret; +} + static __attribute__((unused)) void *malloc(size_t len) { --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E4EEC54EBD for ; Mon, 9 Jan 2023 08:43:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236872AbjAIInI (ORCPT ); Mon, 9 Jan 2023 03:43:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236836AbjAIImX (ORCPT ); Mon, 9 Jan 2023 03:42:23 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 803A112D2B for ; Mon, 9 Jan 2023 00:42:22 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gGfR027440; Mon, 9 Jan 2023 09:42:16 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Ammar Faizi Subject: [PATCH 21/22] nolibc/sys: Implement `getpagesize(2)` function Date: Mon, 9 Jan 2023 09:42:07 +0100 Message-Id: <20230109084208.27355-22-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Ammar Faizi This function returns the page size used by the running kernel. The page size value is taken from the auxiliary vector at 'AT_PAGESZ' key. 'getpagesize(2)' is assumed as a syscall becuase the manpage placement of this function is in entry 2 ('man 2 getpagesize') despite there is no real 'getpagesize(2)' syscall in the Linux syscall table. Define this function in 'sys.h'. Signed-off-by: Ammar Faizi --- tools/include/nolibc/sys.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 47bf67668860..b5f8cd35c03b 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -19,6 +19,7 @@ #include #include #include +#include =20 #include "arch.h" #include "errno.h" @@ -499,6 +500,26 @@ pid_t gettid(void) return sys_gettid(); } =20 +static unsigned long getauxval(unsigned long key); + +/* + * long getpagesize(void); + */ + +static __attribute__((unused)) +long getpagesize(void) +{ + long ret; + + ret =3D getauxval(AT_PAGESZ); + if (!ret) { + SET_ERRNO(ENOENT); + return -1; + } + + return ret; +} + =20 /* * int gettimeofday(struct timeval *tv, struct timezone *tz); --=20 2.17.5 From nobody Tue Dec 16 21:03:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DD76C5479D for ; Mon, 9 Jan 2023 08:43:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236889AbjAIIn3 (ORCPT ); Mon, 9 Jan 2023 03:43:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236847AbjAIImY (ORCPT ); Mon, 9 Jan 2023 03:42:24 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 30ADBDFBE for ; Mon, 9 Jan 2023 00:42:23 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3098gG7L027441; Mon, 9 Jan 2023 09:42:16 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Ammar Faizi Subject: [PATCH 22/22] selftests/nolibc: Add `getpagesize(2)` selftest Date: Mon, 9 Jan 2023 09:42:08 +0100 Message-Id: <20230109084208.27355-23-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109084208.27355-1-w@1wt.eu> References: <20230109084208.27355-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Ammar Faizi Test the getpagesize() function. Make sure it returns the correct value. Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index f14f5076fb6d..c4a0c915139c 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -442,6 +442,35 @@ int test_getdents64(const char *dir) return ret; } =20 +static int test_getpagesize(void) +{ + long x =3D getpagesize(); + int c; + + if (x < 0) + return x; + +#if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defin= ed(__i586__) || defined(__i686__) + /* + * x86 family is always 4K page. + */ + c =3D (x =3D=3D 4096); +#elif defined(__aarch64__) + /* + * Linux aarch64 supports three values of page size: 4K, 16K, and 64K + * which are selected at kernel compilation time. + */ + c =3D (x =3D=3D 4096 || x =3D=3D (16 * 1024) || x =3D=3D (64 * 1024)); +#else + /* + * Assuming other architectures must have at least 4K page. + */ + c =3D (x >=3D 4096); +#endif + + return !c; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -502,6 +531,7 @@ int run_syscall(int min, int max) CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *= )1), -1, EFAULT); break; CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *= )1), -1, EFAULT); break; #endif + CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break; CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); = break; CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); = break; CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST= ); break; --=20 2.17.5