Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp2776285rwl; Mon, 26 Dec 2022 22:34:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXvTYweWOYGC1UDfGeR6cWOYLd8R72/BTKand5Qu2iTYx0AadnA7uZE4AYFjdAcE6nX5vpTE X-Received: by 2002:a17:902:7c95:b0:190:ee85:b25f with SMTP id y21-20020a1709027c9500b00190ee85b25fmr21191692pll.48.1672122894633; Mon, 26 Dec 2022 22:34:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672122894; cv=none; d=google.com; s=arc-20160816; b=OQAxyMgDMme7Drdl2JA/E2pLzI3fyAteXfqzQ55FdYfdgNTnkaV2KO21yD36hj5A31 ue2axFCie/xMvi7WpdRlXHU2t8K/6Mbi69yRCIsOLT2UB+sKMNhkFGhlIrgPS1dbCh2C zL/IdZVw88zdWgv7gL8mD/GiAI1cZF6wPCMLqetV+a4rKg66PuDXiT7ufNBkY7A61afC 13VoZOI/sPkXj08TrMMM+Qnl6OFPy9ACab6XI1eOaXLrnSU8FffI6ZGDiRjls0GJP24d bzkBs8JlDQTH+NcHB4qD5A56DviGV9k9WqHCQeEhb5WhBQbgNT/bkSB2nX6u4jslVb7g vNxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=XhgZyqaTkFH3jg3rpJdcaKzZ7luck5/eW1uXwY10n08=; b=h8w54kDD3T30cD5Zro4SJultmMPzsegQaWpoGVkNtIiUYhYBGhGxWKpqHhCmVwgNh+ U5Dj0Q5RqfLiEsh4WSKpkOaiFdi7Ljx5bSu2XORsAn6u+YCBiIOnkLguGnRZX7CrfE/D mS7QoACcLQqdCyDvLFD3bi7iAcgXet8wGra0T1iELrahrvPwu99YVDyAtIVPcK7Q+AIG vziEb7nukwr5CsJSeAFA6+EPdqRKgDFbaCJ/3dtTffzHb89+9FpOZXPr80BZfgtwKekR NqLRNaSxI0QHT5/VJ3dE7cKvodTdQ+oZZ6IA86VfiwXV1Rz7YbEKkdeL9ZNj8mOW9esk 08lw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n1-20020a170902f60100b00192682632f2si9004181plg.396.2022.12.26.22.34.44; Mon, 26 Dec 2022 22:34:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229777AbiL0G1E (ORCPT + 67 others); Tue, 27 Dec 2022 01:27:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229496AbiL0G1B (ORCPT ); Tue, 27 Dec 2022 01:27:01 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EDA20BA1; Mon, 26 Dec 2022 22:26:58 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 2BR6QemS005393; Tue, 27 Dec 2022 07:26:40 +0100 Date: Tue, 27 Dec 2022 07:26:40 +0100 From: Willy Tarreau To: Ammar Faizi Cc: Shuah Khan , "Paul E. McKenney" , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: Re: [RFC PATCH v1 0/8] nolibc signal handling support Message-ID: <20221227062640.GA5337@1wt.eu> References: <20221222035134.3467659-1-ammar.faizi@intel.com> <20221222043452.GB29086@1wt.eu> <20221222134615.3535422-1-ammar.faizi@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221222134615.3535422-1-ammar.faizi@intel.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Ammar, On Thu, Dec 22, 2022 at 08:46:15PM +0700, Ammar Faizi wrote: > I agree with following the @envp pointer to get the auxv. I was > trying to wire up a new function '__start' (with double underscores) > written in C that accepts @argc, @argv and @envp. Then it calls 'main'. > Then we call '__start' instead of 'main' from '_start'. This way, we > can arrange nolibc-defined data without touching Assembly much in > '__start' (before main). > > But then I noticed that it wouldn't work because we may have users > who define the 'main' function differently, e.g.: > > int main(void); > int main(int argc, char **argv); > int main(int argc, char **argv, char **envp); > > So '__start' can't call main. We still need to call the main from the > inline Assembly (from '_start'). Yes, and quite frankly I prefer to make that the least complicated. Doing just a simple loop in the _start code is trivial. The main concern was to store the data. Till now we had an optional .bss section, we didn't save environ and errno was optional. But let's be honest, while it does allow for writing the smallest programs, most programs will have at least one global variable and will get this section anyway, so we don't save anything in practice. This concern used to be valid when I was making tiny executables when running on floppies where each byte mattered, but now that's pointless. Thus what I'm proposing is to switch to weak symbol definitions for errno, environ, and auxv. I did a quick test to make sure that the same symbol was properly used when accessed from two units and that's OK, I'm seeing the same instance for all of them (which is better than the current situation where errno is static, hence per-unit). My quick-and-dirty test looks like this: diff --git a/arch-x86_64.h b/arch-x86_64.h index e780fdf..73f7b5f 100644 --- a/arch-x86_64.h +++ b/arch-x86_64.h @@ -209,6 +209,9 @@ struct sys_stat_struct { _ret; \ }) +char **environ __attribute__((weak,unused)); +long *auxv __attribute__((weak,unused)); + /* startup code */ /* * x86-64 System V ABI mandates: @@ -218,11 +221,17 @@ struct sys_stat_struct { */ 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) + "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 en> + "0: add $8, %rax\n" + " cmp -8(%rax), %rbp\n" + " jnz 0b\n" + "mov %rax, auxv\n" // save auxv "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned befor> "call main\n" // main() returns the status code, we'll exit > "mov %eax, %edi\n" // retrieve exit code (32 bit) diff --git a/errno.h b/errno.h index df0e473..9781077 100644 --- a/errno.h +++ b/errno.h @@ -29,7 +29,8 @@ #include /* this way it will be removed if unused */ -static int errno; +//static int errno; +int errno __attribute__((weak)); #ifndef NOLIBC_IGNORE_ERRNO #define SET_ERRNO(v) do { errno = (v); } while (0) $ cat a.c #include "nolibc.h" extern void b(void); int main(int argc, char **argv, char **envp) { //environ = envp; errno = 1234; printf("main(): errno=%d env(TERM)=%s auxv=%p auxv[0].t=0x%lx auxv[0].v=0x%lx\n", errno, getenv("TERM"), auxv, auxv?auxv[0]:0, auxv?auxv[1]:0); b(); return 0; } $ cat b.c #include "nolibc.h" void b(void) { long *v = auxv; printf("b(): errno=%d env(TERM)=%s auxv=%p auxv[0].t=0x%lx auxv[0].v=0x%lx\n", errno, getenv("TERM"), auxv, auxv?auxv[0]:0, auxv?auxv[1]:0); printf("auxv:\n"); while (v && v[0]) { printf(" 0x%lx: 0x%lx\n", v[0], v[1]); v += 2; } } $ gcc -Os -fno-asynchronous-unwind-tables -include /g/public/nolibc/nolibc.h -Wall -nostdlib -static -o ab a.c b.c $ nm --size ab 0000000000000004 V errno 0000000000000008 V auxv 0000000000000008 V environ 0000000000000014 W memset 0000000000000018 W memcpy 0000000000000018 W raise 000000000000001b W abort 0000000000000030 W memmove 0000000000000053 t u64toa_r 0000000000000053 t u64toa_r 0000000000000082 T main 00000000000000a4 T b 0000000000000289 t printf 000000000000028c t printf.constprop.0 $ ./ab main(): errno=1234 env(TERM)=xterm auxv=0x7ffdd0c31df8 auxv[0].t=0x21 auxv[0].v=0x7ffdd0d56000 b(): errno=1234 env(TERM)=xterm auxv=0x7ffdd0c31df8 auxv[0].t=0x21 auxv[0].v=0x7ffdd0d56000 auxv: 0x21: 0x7ffdd0d56000 0x10: 0xbfebfbff 0x6: 0x1000 0x11: 0x64 0x3: 0x400040 0x4: 0x38 0x5: 0x7 0x7: 0x0 0x8: 0x0 0x9: 0x401082 0xb: 0x1fd 0xc: 0x1fd 0xd: 0x64 0xe: 0x64 0x17: 0x0 0x19: 0x7ffdd0c31f39 0x1a: 0x2 0x1f: 0x7ffdd0c33ff3 0xf: 0x7ffdd0c31f49 Note that I could verify that some of the entries above are valid (e.g. "x86_64" in 0xf = AT_PLATFORM). Thus now my focus will be on storing these variables where relevant for all archs, so that your getauxval() implementation works on top of it. It will be much cleaner and will also improve programs' ease of implementation and reliability. Cheers, Willy PS: maybe we should trim the Cc list for future exchanges.