From nobody Mon Jun 22 16:51:05 2026 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 0ED79C433EF for ; Sun, 20 Mar 2022 09:39:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243517AbiCTJkT (ORCPT ); Sun, 20 Mar 2022 05:40:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243323AbiCTJkL (ORCPT ); Sun, 20 Mar 2022 05:40:11 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B8D311A20 for ; Sun, 20 Mar 2022 02:38:48 -0700 (PDT) Received: from integral2.. (unknown [182.2.42.189]) by gnuweeb.org (Postfix) with ESMTPSA id 89E937E324; Sun, 20 Mar 2022 09:38:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647769127; bh=CqkJi4gcwtTSWVKYAtlVVoCfJnF5oK9/pwQGZcP0YAI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mGobdSn1ABsRF1yZIw4UyIDUlCf6GLC/BcGhWqquifBfpRLRVigYYA3QVTyau/AzM RODzfjiCsBtSKmg1Vm+Fgcds7AASdN/On/Q/j9s4K0bMaGt4UenVh5rpfhO71iNdk9 jPJjIPhfJ8m6ODIfznfjq0Bl3sQ1E/iPGWsZq8TBZmYPWOJRn4Z32hcJYo0uKN5mno 8S9+rI+nkTT7pULN5HRakimI58lIYnOEq5/7wDt9BiT2DDrEq5z2EUfvl8YsiAshR9 +b+T64414N+X/Mi78/xtzlsWQA6g8KcNiYj7SJMc5zatdIGfndyrymBS0j43E/V43f UHKKIdWrKSOLw== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v1 1/6] tools/nolibc: x86-64: Update System V ABI document link Date: Sun, 20 Mar 2022 16:37:45 +0700 Message-Id: <20220320093750.159991-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The old link no longer works, update it. Signed-off-by: Ammar Faizi --- tools/include/nolibc/arch-x86_64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch= -x86_64.h index fe517c16cd4d..a7b70ea51b68 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -61,7 +61,7 @@ struct sys_stat_struct { * - see also x86-64 ABI section A.2 AMD64 Linux Kernel Conventions, A.2= .1 * Calling Conventions. * - * Link x86-64 ABI: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/x86-6= 4-psABI + * Link x86-64 ABI: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/home * */ =20 --=20 Ammar Faizi From nobody Mon Jun 22 16:51:05 2026 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 F3C5EC433F5 for ; Sun, 20 Mar 2022 09:39:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243626AbiCTJkZ (ORCPT ); Sun, 20 Mar 2022 05:40:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243310AbiCTJkN (ORCPT ); Sun, 20 Mar 2022 05:40:13 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2724111A20 for ; Sun, 20 Mar 2022 02:38:51 -0700 (PDT) Received: from integral2.. (unknown [182.2.42.189]) by gnuweeb.org (Postfix) with ESMTPSA id 1E4AA7E32F; Sun, 20 Mar 2022 09:38:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647769130; bh=BXzzI1AHGnhVngox7MmLibr5gzV+RXSwMy3QZ9xcVDU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PTXPJDYF2yyWTk2sjoZoOISwOfNp+Dtv5i7SIky2i2yas1T444ZvVGGLkVk3EzOjH 4/bKKvJ7R4O2R1VLa7LAQgbBO5tSF8Ug2oVkaZt+2MqqM7GpVY6naWZtmcOFVompyt MbWyGY7ctisWzmb1UjtNLGPVBsS4h7WuiDP1S7M1Uu8Ae8LTnHkEWbaVTbMWLBnixW V2rjfsLBzG7wqKElpn5ykqst5hc47ubw9NVJqDleB9MGj8OzzhHkr5edJDxYBRPePR nWZ1jZk3dkoPWz855mUHE/nQshvZqjGbDAWflvxgXx4spVeHXXVCDSOgkXBnogArYw ObuL9eGCYEaSg== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi , llvm@lists.linux.dev Subject: [RFC PATCH v1 2/6] tools/nolibc: Make the entry point not weak for clang Date: Sun, 20 Mar 2022 16:37:46 +0700 Message-Id: <20220320093750.159991-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Budilig with clang yields the following error: ``` :3:1: error: _start changed binding to STB_GLOBAL .global _start ^ 1 error generated. ``` Don't make the entry point weak if we're compiling with clang. Cc: llvm@lists.linux.dev Signed-off-by: Ammar Faizi --- tools/include/nolibc/arch-aarch64.h | 2 ++ tools/include/nolibc/arch-arm.h | 2 ++ tools/include/nolibc/arch-i386.h | 2 ++ tools/include/nolibc/arch-mips.h | 2 ++ tools/include/nolibc/arch-riscv.h | 2 ++ tools/include/nolibc/arch-x86_64.h | 2 ++ 6 files changed, 12 insertions(+) diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arc= h-aarch64.h index 87d9e434820c..5084cd58b429 100644 --- a/tools/include/nolibc/arch-aarch64.h +++ b/tools/include/nolibc/arch-aarch64.h @@ -183,7 +183,9 @@ struct sys_stat_struct { =20 /* startup code */ asm(".section .text\n" +#if !defined(__clang__) ".weak _start\n" +#endif ".global _start\n" "_start:\n" "ldr x0, [sp]\n" // argc (x0) was in the stack diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-ar= m.h index 001a3c8c9ad5..b3f135a615a6 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -176,7 +176,9 @@ struct sys_stat_struct { =20 /* startup code */ asm(".section .text\n" +#if !defined(__clang__) ".weak _start\n" +#endif ".global _start\n" "_start:\n" #if defined(__THUMBEB__) || defined(__THUMBEL__) diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i= 386.h index d7e4d53325a3..82bf797849ae 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -175,7 +175,9 @@ struct sys_stat_struct { * */ asm(".section .text\n" +#if !defined(__clang__) ".weak _start\n" +#endif ".global _start\n" "_start:\n" "pop %eax\n" // argc (first arg, %eax) diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-m= ips.h index c9a6aac87c6d..719d3808614d 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -190,7 +190,9 @@ struct sys_stat_struct { =20 /* startup code, note that it's called __start on MIPS */ asm(".section .text\n" +#if !defined(__clang__) ".weak __start\n" +#endif ".set nomips16\n" ".global __start\n" ".set noreorder\n" diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-= riscv.h index bc10b7b5706d..a9704affd7de 100644 --- a/tools/include/nolibc/arch-riscv.h +++ b/tools/include/nolibc/arch-riscv.h @@ -184,7 +184,9 @@ struct sys_stat_struct { =20 /* startup code */ asm(".section .text\n" +#if !defined(__clang__) ".weak _start\n" +#endif ".global _start\n" "_start:\n" ".option push\n" diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch= -x86_64.h index a7b70ea51b68..f453f1a05a48 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -198,7 +198,9 @@ struct sys_stat_struct { * */ asm(".section .text\n" +#if !defined(__clang__) ".weak _start\n" +#endif ".global _start\n" "_start:\n" "pop %rdi\n" // argc (first arg, %rdi) --=20 Ammar Faizi From nobody Mon Jun 22 16:51:05 2026 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 9088DC433EF for ; Sun, 20 Mar 2022 09:39:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243700AbiCTJkb (ORCPT ); Sun, 20 Mar 2022 05:40:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243465AbiCTJkQ (ORCPT ); Sun, 20 Mar 2022 05:40:16 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A32ADF4D for ; Sun, 20 Mar 2022 02:38:54 -0700 (PDT) Received: from integral2.. (unknown [182.2.42.189]) by gnuweeb.org (Postfix) with ESMTPSA id 4FF1E7E330; Sun, 20 Mar 2022 09:38:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647769134; bh=aEfE9CLxI0sxWAhv7IlCmtmu0aC+hA6/IQ9IGhanNbs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n+n4WgnLrhp8p2OXLuYusMr1M3M9aqB41+ilomgxOrpC1ocIT+TYfwshpgHz3whOM V14BlNdO6aQAElqIRrY0FCjBnHwAIgBQXrrVbIkAyzIv02pDrl3mTIjOzhH2lGRURQ Y51ubU1zK2XBxPwKWQZcJGjza1Y33aSdXokoO07oq/3m41cctEmMAtRKfTfP8DHVtT uKdB4RpsuKdi7YW8zrtUI1leYiU9EPe5hEOksGDt2DDKT4j8Tv21Jr8AsyFYEURtZD ouSLTmUJSFoD8JyL2Nc/m3u3dUCBJPQ9mbmOiIJ34OJR0AY+7yBrZglFNSisBmH5dJ rhetl2I0p5vpA== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi , x86@kernel.org, llvm@lists.linux.dev Subject: [RFC PATCH v1 3/6] tools/nolibc: i386: Implement syscall with 6 arguments Date: Sun, 20 Mar 2022 16:37:47 +0700 Message-Id: <20220320093750.159991-4-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In i386, the 6th argument of syscall goes in %ebp. However, both Clang and GCC cannot use %ebp in the clobber list and in the "r" constraint without using -fomit-frame-pointer. To make it always available for any kind of compilation, the below workaround is implemented. For clang (the Assembly statement can't clobber %ebp): 1) Save the %ebp value to the redzone area -4(%esp). 2) Load the 6-th argument from memory to %ebp. 3) Subtract the %esp by 4. 4) Do the syscall (int $0x80). 5) Pop %ebp. For GCC, fortunately it has a #pragma that can force a specific function to be compiled with -fomit-frame-pointer, so it can always use "r"(var) where `var` is a variable bound to %ebp. Cc: x86@kernel.org Cc: llvm@lists.linux.dev Signed-off-by: Ammar Faizi --- tools/include/nolibc/arch-i386.h | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i= 386.h index 82bf797849ae..10de54d4b4d6 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -167,6 +167,70 @@ struct sys_stat_struct { _ret; \ }) =20 + +/* + * Both Clang and GCC cannot use %ebp in the clobber list and in the "r" + * constraint without using -fomit-frame-pointer. To make it always + * available for any kind of compilation, the below workaround is + * implemented. + * + * For clang (the Assembly statement can't clobber %ebp): + * 1) Save the %ebp value to the redzone area -4(%esp). + * 2) Load the 6-th argument from memory to %ebp. + * 3) Subtract the %esp by 4. + * 4) Do the syscall (int $0x80). + * 5) Pop %ebp. + * + * For GCC, fortunately it has a #pragma that can force a specific function + * to be compiled with -fomit-frame-pointer, so it can use "r"(var) where + * var is a variable bound to %ebp. + * + */ +#if defined(__clang__) +static inline long ____do_syscall6(long eax, long ebx, long ecx, long edx, + long esi, long edi, long ebp) +{ + __asm__ volatile ( + "movl %%ebp, -4(%%esp)\n\t" + "movl %[arg6], %%ebp\n\t" + "subl $4, %%esp\n\t" + "int $0x80\n\t" + "popl %%ebp\n\t" + : "=3Da"(eax) + : "a"(eax), "b"(ebx), "c"(ecx), "d"(edx), "S"(esi), "D"(edi), + [arg6]"m"(ebp) + : "memory", "cc" + ); + return eax; +} + +#else /* #if defined(__clang__) */ +#pragma GCC push_options +#pragma GCC optimize "-fomit-frame-pointer" +static inline long ____do_syscall6(long eax, long ebx, long ecx, long edx, + long esi, long edi, long ebp) +{ + register long __ebp __asm__("ebp") =3D ebp; + __asm__ volatile ( + "int $0x80" + : "=3Da"(eax) + : "a"(eax), "b"(ebx), "c"(ecx), "d"(edx), "S"(esi), "D"(edi), + "r"(__ebp) + : "memory", "cc" + ); + return eax; +} +#pragma GCC pop_options +#endif /* #if defined(__clang__) */ + +#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) ( \ + ____do_syscall6((long)(num), (long)(arg1), \ + (long)(arg2), (long)(arg3), \ + (long)(arg4), (long)(arg5), \ + (long)(arg6)) \ +) + + /* startup code */ /* * i386 System V ABI mandates: --=20 Ammar Faizi From nobody Mon Jun 22 16:51:05 2026 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 A677CC433EF for ; Sun, 20 Mar 2022 09:39:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243597AbiCTJkk (ORCPT ); Sun, 20 Mar 2022 05:40:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243718AbiCTJkb (ORCPT ); Sun, 20 Mar 2022 05:40:31 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A2E913CC9 for ; Sun, 20 Mar 2022 02:38:58 -0700 (PDT) Received: from integral2.. (unknown [182.2.42.189]) by gnuweeb.org (Postfix) with ESMTPSA id 99F0E7E331; Sun, 20 Mar 2022 09:38:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647769137; bh=C1oZNMFG4cuk3f1H0bPYLqrwXcCeXBkHivilMNbv6lI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ojla95b/9/b9BpaT1zzs9+dxgDrZ+9hB78XUhoQ/LtNl+GgUcvzGJI8dysOf4LYFT CE44R07K+pUV527UQXiHjfHauUJSb9Wv7f0MZwGhbNlijDFw/sxkNB4t0mrscN0wrc 0kTZKKg4iEsOjbqy/oHa0nu1w/46xbenOFVr6oAAGj33nnI7qFMiYCecvaNH2QZn5N qwLngx9J5gNMPOlccDSdQBdMvK61A1T+vUMB74HisAIlYHAbSQAfgYTahCB+mmc4+N zle9QFAj+1w/Sagsmimp2046IUOe4vTKvuhnUa0Q+myLhwvlJNh1UTfzX74W/F5s35 6F9zB17qhhFtA== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v1 4/6] tools/nolibc/sys: Implement `mmap()` and `munmap()` Date: Sun, 20 Mar 2022 16:37:48 +0700 Message-Id: <20220320093750.159991-5-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement mmap() and munmap(). Currently, they are only available for architecures that have my_syscall6 macro. For architectures that don't have, this function will return -1 with errno set to -ENOSYS (Function not implemented). This has been tested on x86 and i386. Notes for i386: 1) The common mmap() syscall implementation uses __NR_mmap2 instead of __NR_mmap. 2) The offset must be shifted-right by 12-bit. Signed-off-by: Ammar Faizi --- tools/include/nolibc/sys.h | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 28437863c63f..76510b19b7cf 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -14,6 +14,7 @@ #include #include // for SIGCHLD #include +#include #include #include #include @@ -658,6 +659,67 @@ int mknod(const char *path, mode_t mode, dev_t dev) return ret; } =20 +#ifndef MAP_SHARED +#define MAP_SHARED 0x01 /* Share changes */ +#define MAP_PRIVATE 0x02 /* Changes are private */ +#define MAP_SHARED_VALIDATE 0x03 /* share + validate extension flags */ +#endif + +#ifndef MAP_FAILED +#define MAP_FAILED ((void *)-1) +#endif + +static __attribute__((unused)) +void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, + off_t offset) +{ +#ifndef my_syscall6 + /* Function not implemented. */ + return -ENOSYS; +#else + + int n; + +#if defined(__i386__) + n =3D __NR_mmap2; + offset >>=3D 12; +#else + n =3D __NR_mmap; +#endif + + return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset); +#endif +} + +static __attribute__((unused)) +void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t o= ffset) +{ + void *ret =3D sys_mmap(addr, length, prot, flags, fd, offset); + + if ((unsigned long)ret >=3D -4095UL) { + SET_ERRNO(-(long)ret); + ret =3D MAP_FAILED; + } + return ret; +} + +static __attribute__((unused)) +int sys_munmap(void *addr, size_t length) +{ + return my_syscall2(__NR_munmap, addr, length); +} + +static __attribute__((unused)) +int munmap(void *addr, size_t length) +{ + int ret =3D sys_munmap(addr, length); + + if (ret < 0) { + SET_ERRNO(-ret); + ret =3D -1; + } + return ret; +} =20 /* * int mount(const char *source, const char *target, --=20 Ammar Faizi From nobody Mon Jun 22 16:51:05 2026 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 84515C433F5 for ; Sun, 20 Mar 2022 09:39:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243938AbiCTJkw (ORCPT ); Sun, 20 Mar 2022 05:40:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243812AbiCTJkc (ORCPT ); Sun, 20 Mar 2022 05:40:32 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EECB113CF7 for ; Sun, 20 Mar 2022 02:39:00 -0700 (PDT) Received: from integral2.. (unknown [182.2.42.189]) by gnuweeb.org (Postfix) with ESMTPSA id B40567E333; Sun, 20 Mar 2022 09:38:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647769140; bh=RSEq8dI5RloSlWnt62U9OOk+SbaLshrfq3hpAG2JDpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EvatmLBmNzRkFYsBQuFHEtUP6oTAmULHalxKHODm24mf0pEraWorzAhDCnV8J/P69 lYp//Sw2RRvob30XXxfYOs8gVxEYEnfE98wkKgtKo+ovQwAeP9EI+hcQHowj+fsF9k T1W2sg4adNOwY9SxD3lCNFsHirdDLPkKY0VOVLE2/9xFTJkM69Zfz3KGrcXycvPyfg Lh0tiIBoufRvogJblWrXMx7B3K+UYdbtGf/wh1d1jUVDqWJFG+XmpNpPaIWnotesWS igV5zUBo6E1A9mLpFSkDlgahAPONLbVjBcEH6OKMYjVSIm8gvIcRHylusWx/FJhL56 EnnSJKKtjp9eg== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v1 5/6] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()` Date: Sun, 20 Mar 2022 16:37:49 +0700 Message-Id: <20220320093750.159991-6-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement basic dynamic allocator functions. These functions are currently only available on architectures that have nolibc mmap() syscall implemented. These are not a super-fast memory allocator, but at least they can satisfy basic needs for having heap without libc. Signed-off-by: Ammar Faizi --- tools/include/nolibc/stdlib.h | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index 733105c574ee..13600e73404d 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -10,8 +10,24 @@ #include "std.h" #include "arch.h" #include "types.h" +#include "string.h" #include "sys.h" =20 +struct nolibc_heap { + size_t len; + char user_p[] __attribute__((__aligned__)); +}; + +#ifndef offsetof +#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD) +#endif + +#ifndef container_of +#define container_of(PTR, TYPE, FIELD) ({ \ + __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR =3D (PTR); \ + (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \ +}) +#endif =20 /* Buffer used to store int-to-ASCII conversions. Will only be implemented= if * any of the related functions is implemented. The area is large enough to @@ -60,6 +76,18 @@ int atoi(const char *s) return atol(s); } =20 +static __attribute__((unused)) +void free(void *ptr) +{ + struct nolibc_heap *heap; + + if (!ptr) + return; + + heap =3D container_of(ptr, struct nolibc_heap, user_p); + munmap(heap, heap->len); +} + /* Converts the unsigned long integer to its hex representation into * buffer , which must be long enough to store the number and the * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The @@ -182,6 +210,57 @@ char *ltoa(long in) return itoa_buffer; } =20 +static inline __attribute__((unused)) +void *malloc(size_t len) +{ + struct nolibc_heap *heap; + + heap =3D mmap(NULL, sizeof(*heap) + len, PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if (__builtin_expect(heap =3D=3D MAP_FAILED, 0)) + return NULL; + + heap->len =3D sizeof(*heap) + len; + return heap->user_p; +} + +static inline __attribute__((unused)) +void *calloc(size_t size, size_t nmemb) +{ + void *orig; + size_t res =3D 0; + + if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) { + SET_ERRNO(ENOMEM); + return NULL; + } + + /* + * No need to zero the heap, the MAP_ANONYMOUS in malloc() + * already does it. + */ + return malloc(res); +} + +static inline __attribute__((unused)) +void *realloc(void *old_ptr, size_t new_size) +{ + struct nolibc_heap *heap; + void *ret; + + if (!old_ptr) + return malloc(new_size); + + ret =3D malloc(new_size); + if (__builtin_expect(!ret, 0)) + return NULL; + + heap =3D container_of(old_ptr, struct nolibc_heap, user_p); + memcpy(ret, heap->user_p, heap->len); + munmap(heap, heap->len); + return ret; +} + /* converts unsigned long integer to a string using the static itoa_b= uffer * and returns the pointer to that string. */ --=20 Ammar Faizi From nobody Mon Jun 22 16:51:05 2026 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 27008C433F5 for ; Sun, 20 Mar 2022 09:39:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243738AbiCTJk7 (ORCPT ); Sun, 20 Mar 2022 05:40:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243912AbiCTJkd (ORCPT ); Sun, 20 Mar 2022 05:40:33 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE05A140E7 for ; Sun, 20 Mar 2022 02:39:04 -0700 (PDT) Received: from integral2.. (unknown [182.2.42.189]) by gnuweeb.org (Postfix) with ESMTPSA id 0797F7E335; Sun, 20 Mar 2022 09:39:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647769144; bh=BuWwbvbdO1pAfMLZtLiokH1rP8SoQ7kLSInYGGGXMhg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kdnl13VXhHYGESqMLY0vF7YRt97GWcB5+OaEz4Gg55d5dm/lOKGzYEk8MtTZBZMit OKtQxTLqKB80mkKA8pit287FgFBPTCowBq/JUBcCpKtYsyeAmR5Yw/aKOzM5+rFCZK su756yLKNStlVTjU4ElSGMzJCOZARrmsmhczBzWJydaGMR4iZ+HY1SBqborb9MzYbT pAxsR4zRYYT06m/SdoYgdUHJBAjw2J9nzZIoZ1EEY87fjKC6auA9j3A2d5SvEdDp/c mvm0DXO3PWidxbrd/CjbRsf0MyH+QOupHNaON4slhyhc5Y3wjYCGzM3z3pmXh88cea iTXfJF1S5Y20w== From: Ammar Faizi To: Willy Tarreau Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List , Ammar Faizi Subject: [RFC PATCH v1 6/6] tools/include/string: Implement `strdup()` and `strndup()` Date: Sun, 20 Mar 2022 16:37:50 +0700 Message-Id: <20220320093750.159991-7-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add strdup and strndup support. These functions are only available on architectures that have my_syscall6() macro from nolibc. Signed-off-by: Ammar Faizi --- tools/include/nolibc/string.h | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index 4554b6fcb400..413c65f7c853 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -9,6 +9,10 @@ =20 #include "std.h" =20 +static void free(void *ptr); +static void *malloc(size_t len); +static void *realloc(void *old_ptr, size_t new_size); + /* * As much as possible, please keep functions alphabetically sorted. */ @@ -127,6 +131,70 @@ size_t nolibc_strlen(const char *str) nolibc_strlen((str)); \ }) =20 +static __attribute__((unused)) +char *strdup(const char *str) +{ + size_t allocated =3D 2048; + size_t i; + char *ret; + char *tmp; + + ret =3D malloc(allocated); + if (__builtin_expect(!ret, 0)) + return NULL; + + i =3D 0; + for (;;) { + char c =3D *str; + if (!c) + break; + + if (i =3D=3D allocated) { + allocated +=3D 2048; + tmp =3D realloc(ret, allocated); + if (__builtin_expect(!tmp, 0)) { + free(ret); + return NULL; + } + ret =3D tmp; + } + + ret[i++] =3D c; + str++; + } + + ret[i] =3D '\0'; + return ret; +} + +static __attribute__((unused)) +char *strndup(const char *str, size_t maxlen) +{ + size_t i; + char *ret; + + ret =3D malloc(maxlen + 1); + if (__builtin_expect(!ret, 0)) + return NULL; + + i =3D 0; + for (;;) { + char c =3D *str; + if (!c) + break; + + if (i =3D=3D maxlen) + break; + + ret[i++] =3D c; + str++; + } + + ret[i] =3D '\0'; + return ret; +} + + static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) { --=20 Ammar Faizi