Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp34438659rwd; Sun, 9 Jul 2023 12:02:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlF4pFowcqbWyitY6cY64FntbubnJD2yHLa2sUm5l91x6F0B3SkdfA6bqHnLky5tzQvbstjw X-Received: by 2002:a19:911a:0:b0:4fb:89e3:5ac6 with SMTP id t26-20020a19911a000000b004fb89e35ac6mr7001421lfd.62.1688929359238; Sun, 09 Jul 2023 12:02:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688929359; cv=none; d=google.com; s=arc-20160816; b=tx/lZ3daOmAipG+LucgFL1BXbhp2ochuFBvtonF9Hja03Id+rLEmepSiu74mYaiNWE VRjb7hpxjBJQMkYDhESX7OS0/yVFy4zXeyfXPTD0CljY44EMADzjjRXCGUJB8BPXBCeF bn7NcKn06dehZbR2bR9xogdBfm3aiwkBd84Zzs2flVqL7ihzvsrPUHNRv+28VZu3gyXT xcVwjMbU18iVUfBQfK6bp+BssSlSdud+npajJO9RWSdTZvrdtgyG+lCOIR2GtQr9QOhB S9CNq330EEbaPlP+Y3zwhohd8Nwu9xRasWqrfYhDfjEgHw4b2FHhwQlkuGlgePVAQJza Sulg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=RCjD5lcaomn41URGyj+sgouaKZn4PjuZlErbr6XoTsU=; fh=w/aFR0RmDXi+lP42kXVQr3sgcXBpqVhGy2uSRpJTTV0=; b=Pk3RUjhOaoHXGSzhQPd0eApBlqvZB4wQDfVsnlQ0rM/2qKZzKYyA+VJEwsfdZv+s+E NVXn1G1uqhEHWV+d0hsqDXrcDPP9i/76eMsHmUu0ewU77bK96Vkbttuhsbmc3/CC0W1a AVMPb/ESCWIhAq6Bo9FUmWjBTNXUFbBFCkrkwF5I844B3Fvcboy7R8eEzCJeykcTtq+c y5c54eVyLxPAJky6qO0/mqY764Q73RtWhWANTgVM3ek/LYtHLNCm6UrT5eco0dnhPBeX uJLhhofsxYj6mFSof0VDM6yKkeB0IqnWfPeHrGzFtXYRPqtmCHnwTpBtm/9ahKP+azVa qPxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@t-8ch.de header.s=mail header.b=ljATuSj1; 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 h17-20020a170906719100b00965a1d85cf1si7136605ejk.890.2023.07.09.12.02.14; Sun, 09 Jul 2023 12:02:39 -0700 (PDT) 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; dkim=pass header.i=@t-8ch.de header.s=mail header.b=ljATuSj1; 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 S230449AbjGIStP (ORCPT + 99 others); Sun, 9 Jul 2023 14:49:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbjGIStO (ORCPT ); Sun, 9 Jul 2023 14:49:14 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 098F2D7; Sun, 9 Jul 2023 11:49:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=t-8ch.de; s=mail; t=1688928551; bh=FoY/caXNRdYDmaJ1xfS/LuWYYdu7mCfD+WNi2/Kt04Q=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ljATuSj1ccUDXLhjWnl9zkI4bOyIOV3PgRjyyInsu0Mclr0CtBamEXVVHEcylLWVV KIu3Id3f6q1f20Cx8c31H5S/y2sxzC5P/MqY49zVJNauV/NsIonRpMlbq+RH9BJeRN C0filRcInhNEjGDFaIuSk59snA3QDs2S9685rNKg= Date: Sun, 9 Jul 2023 20:49:10 +0200 From: Thomas =?utf-8?Q?Wei=C3=9Fschuh?= To: Zhangjin Wu Cc: w@1wt.eu, arnd@arndb.de, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH v2 04/12] tools/nolibc: crt.h: add _start_c Message-ID: <4e23cc1c-2fe0-413e-9fe1-a9428c0861b9@t-8ch.de> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On 2023-07-08 23:29:58+0800, Zhangjin Wu wrote: > As the environ and _auxv support added for nolibc, the assembly _start > function becomes more and more complex and therefore makes the porting > of nolibc to new architectures harder and harder. > > To simplify portability, this c version of _start_c() is added to do > most of the assembly start operations in C, which reduces the complexity > a lot and will eventually simplify the porting of nolibc to the new > architectures. > > The new _start_c() only requires a stack pointer argument, it will find > argv, envp and _auxv for us, and then call main(), finally, it exit() > with main's return status. With this new _start_c(), the future new > architectures only require to add very few assembly instructions. I like it! A quick test indicates that the initialization of the stackprotectors could also be moved into the C function. It also seems like a good opportunity to add some tests for argv/environment variable passing. And as general note to the full series I think that splitting the arch files is not necessary and confusing. > Signed-off-by: Zhangjin Wu > --- > tools/include/nolibc/crt.h | 44 ++++++++++++++++++++++++++++++++++++++ > 1 file changed, 44 insertions(+) > > diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h > index 221b7c5346ca..b269294e9664 100644 > --- a/tools/include/nolibc/crt.h > +++ b/tools/include/nolibc/crt.h > @@ -13,4 +13,48 @@ > char **environ __attribute__((weak)); The old code seems to avoid putting "environ" into the global symbol namespace. Could this declaration be moved into the function like in getenv()? > const unsigned long *_auxv __attribute__((weak)); > > +int main(int argc, char *argv[], char **envp); This will lead to conflicting declarations if the users use a different signature. I'm not (yet?) sure how to work around this. Also how is the case handled where main() returns "void"? I'm not sure how this is currently handled or if the compiler takes core of returning 0 in this case. > +static void exit(int); > + > +void _start_c(long *sp) > +{ > + int argc, i; > + char **argv; > + char **envp; > + > + /* > + * sp : argc <-- argument count, required by main() > + * argv: argv[0] <-- argument vector, required by main() > + * argv[1] > + * ... > + * argv[argc-1] > + * null > + * envp: envp[0] <-- environment variables, required by main() and getenv() > + * envp[1] > + * ... > + * null > + * _auxv: auxv[0] <-- auxiliary vector, required by getauxval() > + * auxv[1] > + * ... > + * null > + */ > + > + /* assign argc and argv */ > + argc = sp[0]; > + argv = (void *)(sp + 1); > + > + /* find envp */ > + envp = argv + argc + 1; > + environ = envp; > + > + /* find auxv */ > + i = 0; > + while (envp[i]) > + i++; > + _auxv = (void *)(envp + i + 1); > + > + /* go to application */ > + exit(main(argc, argv, envp)); > +} > + > #endif /* _NOLIBC_CRT_H */ > -- > 2.25.1 >