Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp828619rdb; Sat, 7 Oct 2023 00:29:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFitp7YfvI/9/aqZA3m/4qIEA34u1bTCRtMLfNwWOksdM4v48uo+se19+qcx1OhC3b33Tfz X-Received: by 2002:a05:6a20:324d:b0:14d:7511:1c2 with SMTP id hm13-20020a056a20324d00b0014d751101c2mr9386873pzc.55.1696663756111; Sat, 07 Oct 2023 00:29:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696663756; cv=none; d=google.com; s=arc-20160816; b=FFr+DijEu4VDShqeQf7D0HnxLImg6xOKFHLs8MT9YaDwiRBVLP0/IJpcuVcmqqeHNm 8orJ+EdTtMyBS5ZGpYLvHgv/qfa6lDlADPw3b43C33q2DgME/aHYA+l3gJTvRGO3eJ/c X9qv6WZ0oN6SnSXX4Vj9IRF2X9/trRJhNu4gMfIC57tEZaDos6VakFmMvQRSEKnzPDlZ Hkm9pi0iHDz61Lp0mQIT6wKLqv/S/k9yd8JxYId0oza3jNx5sEpTHIzYt8KvkPXhoroz vBI5UTCwAflghGitefoAroea63DU8+44QKfo3xxopHLIDzfF3z2Ya3jj3P9iAqNzLsdO 995w== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=7RwjaJQmg8S+r6RGSDufgmLRODJVeOvVBNqmCXQTIEc=; fh=pTx9fEY7ua+lm7qrJvkJuQzqwoXTh0iZZAyDx4UpkYw=; b=Z3/WdTUa9Lp91neRRmC+zPLWDA7Axzo/hsleaI+ZGqeJmQdBS/9jvsYjV9lhsltzOy DX4WUsqlMZgsfzW8oxr3kkphpoOKBKbtABWlwt+mmdE4Dr3jk07moJDT22iAjNctQ+8d x7yWUkKMzFF5F6jaUTc3BrPKcxZqmMuEyyyrFjuXXWkPS7UDJEHetw8V0/k3o4cOwaPh EXA157/qvrHCrC2MDmegZcyTkDaQgguJVgsHEURVNVLWsxdtuWMV2GBICfrVcAbkYaGb BfF+BxGxxF1BJ49G/+V7Nx++42vOEKh5pQ/oFaafG9zHpGNqbTFHZCsbAFm8wuy69MOy WU1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=cGzDPCCv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id h13-20020a170902f2cd00b001b9ea0f0e8esi4882864plc.650.2023.10.07.00.29.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 00:29:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=cGzDPCCv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id D89468022C41; Sat, 7 Oct 2023 00:29:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343700AbjJGH27 (ORCPT + 99 others); Sat, 7 Oct 2023 03:28:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343725AbjJGH2y (ORCPT ); Sat, 7 Oct 2023 03:28:54 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE8C2C5; Sat, 7 Oct 2023 00:28:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1696663725; bh=gG9Hvh9GA1HUqwNKeMMjfy0tQ09efcDbsUFUUdSbOnE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=cGzDPCCvvGI1pgHbX/tmMcfUEx+FZD6DiJXn3h+6FQLk46o6/DbUVzqCG+kkG2ZjN csLzodffH9mLlH6Fi1L8MnEDElnYrF5iWqXwgHTBqxcJxMByxBzXAmh7dLj5PBGiMN ebtZvujlH6bNag1k2S9DhwmI1vPcoMq9MaIWKqVw= Date: Sat, 7 Oct 2023 09:28:45 +0200 From: Thomas =?utf-8?Q?Wei=C3=9Fschuh?= To: Willy Tarreau 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: <485b8b48-673a-4b1d-8651-2c0038d478cf@t-8ch.de> References: <20231005-nolibc-constructors-v1-1-776d56bbe917@weissschuh.net> <20231007065025.GZ20998@1wt.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20231007065025.GZ20998@1wt.eu> X-Spam-Status: No, score=2.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,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 groat.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 (groat.vger.email [0.0.0.0]); Sat, 07 Oct 2023 00:29:13 -0700 (PDT) X-Spam-Level: ** Hi Willy, On 2023-10-07 08:50:25+0200, Willy Tarreau wrote: > 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. > [..] > > 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. Was this indeterminism for constructors from the same translation unit? Or across different translation units/shared objects? I'm not entirely sure, but the GCC [0] docs could be read that within a given TU the execution order for constructors is the same as the definition order, even for C. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects And linked from there: In Standard C++, objects defined at namespace scope are guaranteed to be initialized in an order in strict accordance with that of their definitions *in a given translation unit*. No guarantee is made for initializations across translation units. [0] https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html (using an old version of the docs to make sure this didn't change recently)