Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp816693rdb; Fri, 6 Oct 2023 23:50:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHakp9NmCiTwuyRpu9MH/3YVGq7KBMdEdtJHBZpudIK8JRAltgukyrIDW4gAsqcsO5mxFcL X-Received: by 2002:a05:6a21:328a:b0:14d:7b6:cf2f with SMTP id yt10-20020a056a21328a00b0014d07b6cf2fmr11921633pzb.47.1696661449092; Fri, 06 Oct 2023 23:50:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696661449; cv=none; d=google.com; s=arc-20160816; b=mU6Jo3d9ObVwADO/3A0+aTEd1Kt+fgoAnlv0r5IkidvAR1HXh1p8/YPP6BVYUMPgcP i+lFjbaYGhzNJRuYcuWi2dKNcRZ2u+2YlCO2fedV4gRtQDCJ++QHAeD9b3jvOLNhaZKf iLy0H7w4RZGEvjBPTmla1y0+dnZnItYXbahxSZEFPzKeS3bWTKi0GMl2WAq/gOMSBZfU HzXWXwX48aWqTTtCjLRT2WRaABAVGru73iVLul2VdtRgUGKIGE1WSFOoFTTh66g2Jw/H yfhNTDUx4Q6rg0jLymLvXs4T8zfN/HuPg56WNwEkrKZHmmQqiHadCu5hzsl9OMQWoSiy f8RQ== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=qkkcfkPJcDobIXz1gbjeGsZQc2pd/r0xW6M4tAzs4+s=; fh=qYSE4JWoGuek8PPANgtObAomYOrd4iJoUXDlbhr3PnI=; b=Vcj7tZEuLUbjy2X4gVCWYo3+IyRxxU+CybmbJj4EFLVr14W5act4Gb4Glf96dp81pV k26+dZiukjCIPJcLWtpVtYvtHhbhQr98i//s+VqS6TD4f0kyWIlB0H6z3HQKebsizmpt Z3k94GHxRuqCiD0X7ZoouUIfX14zzkj9ZQaqSgY7lGuHrMjcZBW3RuVVIMjCIcErnIMl aCSu6eJ9caLeggFuH6tY1500UPOUkrtOvoIH6YgTHWyJ/wFjHeTbMcnAiiKife+dchd4 Z59M9NV9pXUEcFzt8NknA0doO1CXoHYTSHruMjHMW3BV4M7M0G39etHdkWbVUDs+5J6e fPoA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id a1-20020a655c81000000b0057ab7f46381si4983059pgt.76.2023.10.06.23.50.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 23:50:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 8DAFC801B3C4; Fri, 6 Oct 2023 23:50:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343599AbjJGGug (ORCPT + 99 others); Sat, 7 Oct 2023 02:50:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343577AbjJGGuf (ORCPT ); Sat, 7 Oct 2023 02:50:35 -0400 Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 18031B9; Fri, 6 Oct 2023 23:50:33 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3976oP6p021273; Sat, 7 Oct 2023 08:50:25 +0200 Date: Sat, 7 Oct 2023 08:50:25 +0200 From: Willy Tarreau To: Thomas =?iso-8859-1?Q?Wei=DFschuh?= Cc: Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH RFC] tools/nolibc: add support for constructors and destructors Message-ID: <20231007065025.GZ20998@1wt.eu> References: <20231005-nolibc-constructors-v1-1-776d56bbe917@weissschuh.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20231005-nolibc-constructors-v1-1-776d56bbe917@weissschuh.net> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 06 Oct 2023 23:50:46 -0700 (PDT) X-Spam-Level: ** Hi Thomas, On Thu, Oct 05, 2023 at 06:45:07PM +0200, Thomas Wei?schuh wrote: > With the startup code moved to C, implementing support for > constructors and deconstructors is fairly easy to implement. > > Examples for code size impact: > > text data bss dec hex filename > 21837 104 88 22029 560d nolibc-test.before > 22135 120 88 22343 5747 nolibc-test.after > 21970 104 88 22162 5692 nolibc-test.after-only-crt.h-changes > > The sections are defined by [0]. > > [0] https://refspecs.linuxfoundation.org/elf/gabi4+/ch5.dynamic.html > > Signed-off-by: Thomas Wei?schuh > --- > Note: > > This is only an RFC as I'm not 100% sure it belong into nolibc. > But at least the code is visible as an example. That's interesting, thanks for working on this! I thought about it in the past but didn't see how to address it. I do think some users might find it convenient with modular code that will require less ifdefs. That may be particularly true with test programs that want to register some test series for example. The code looks clean to me, and I suppose you've tested it on multiple archs. However I'm having a comment below: (...) > #endif /* _NOLIBC_CRT_H */ > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > index a3ee4496bf0a..f166b425613a 100644 > --- a/tools/testing/selftests/nolibc/nolibc-test.c > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > @@ -57,6 +57,9 @@ static int test_argc; > /* will be used by some test cases as readable file, please don't write it */ > static const char *argv0; > > +/* will be used by constructor tests */ > +static int constructor_test_value; > + > /* definition of a series of tests */ > struct test { > const char *name; /* test name */ > @@ -594,6 +597,18 @@ int expect_strne(const char *expr, int llen, const char *cmp) > #define CASE_TEST(name) \ > case __LINE__: llen += printf("%d %s", test, #name); > > +__attribute__((constructor)) > +static void constructor1(void) > +{ > + constructor_test_value = 1; > +} > + > +__attribute__((constructor)) > +static void constructor2(void) > +{ > + constructor_test_value *= 2; > +} > + In the past I learned the hard way that you can never trust the execution order of constructors, so if you're unlucky above you could very well end up with 1 and that would be correct. I suggest that instead you do something such as: constructor_test_value += 1; ... constructor_test_value += 2; and check for value 3 in the test to make sure they were both executed exactly once each. Thanks! Willy