Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp527650rdg; Thu, 12 Oct 2023 12:34:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFAN2MuHjrPnj/VwUy5IGJ+ZinjXTokJMSM0tOed+auXOanpungyTxsEtz03qAV5iE3BqId X-Received: by 2002:a05:6871:723:b0:1b7:3fd5:87cd with SMTP id f35-20020a056871072300b001b73fd587cdmr29320827oap.48.1697139255651; Thu, 12 Oct 2023 12:34:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697139255; cv=none; d=google.com; s=arc-20160816; b=I81GqDcH70SaH30Iv+bW9hvUIBgYXC8pyWBwCpglSZGmSsaY/L8ev6KDoHk2j1b6DO J/xcJtZVBmiIlG3NlZl5PaKp1PjygVe+V64SuzwzHRcXgPPwnbW7FTGw1LY65MMGo7C8 diKr+vgcXzlWWqPyEkTHBXD9URi26cunrsw0RXR/o7jvGmlkJynBf7XdOg2xRJWQwi5j 4Wz/JVmkoYADza00YaeAZO+KuK7sQqDvhKI7nCP5eaWEr4/f0i+WRTiQk55Ip/jNLqLE 3l0DFeyBljCYEzzl7T5X7VBf6iDwDkJ1+GHLUSQXSmlFHrbEhWnZ/GuCe2RBRkaWxPYp KZCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pT+Yf5AQZYIaRDCSVqHyUluAczPKl7SCfZdcWW+pXxg=; fh=n5S/AR14tYMsjDdXEOPrR3UbxswSa5eTzDqlfcgbnjE=; b=GAy/H9y9AUe7KH8Jvb/Dnq1SjlC8YPjXaY5VJbvkrkhzaXRx12ZJNgVquQSoT1kkJ9 YuT7oFrsrNmmYi1CesBc2lI1I6Y9/i4axTqfbuLaBmn+aqNUVOtcuXjT58yuKxwJ7+0T DAkb7s7YCXTygiHQF7gGL0m1q4Dyy0QeqFeSHDhgwODeP4ttUW7LrHk3PMaI5sIpdddK S8Y0KW/wxLIn5tjNvAids1SLIFpk1yoABBsgdwNX61XWP94kBo8BtyxGVdzEtdnU096D r0p6imcyfAhuaxltagUFgL/VMg4/mPqIQ0jBvzC+7cFwNCECiL1N+Zj1piW4q0B+xdN+ CJqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="pVN5/8dY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id x127-20020a636385000000b00578d78f2a70si2952993pgb.388.2023.10.12.12.34.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 12:34:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="pVN5/8dY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 9D1FD81C98DE; Thu, 12 Oct 2023 12:34:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442786AbjJLTdk (ORCPT + 99 others); Thu, 12 Oct 2023 15:33:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442177AbjJLTcm (ORCPT ); Thu, 12 Oct 2023 15:32:42 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DFD0D6 for ; Thu, 12 Oct 2023 12:32:40 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60244C433B8; Thu, 12 Oct 2023 19:32:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697139156; bh=ZOE33RJAMWGQJdaU6oZoWkIigfB3nCdV+9LKbJA6ToY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pVN5/8dYBBcPxYBYK5sFj+GGc37KK72uJ0ciy7QaETHfZIdiT+E1WeOmRFKttX4cW 9kFsb0sl9cT98dC7n14X32mxKbkWo0bKmhESX6GVihwadGyR5B1sdonGFO32/KKZrW 9TtRNvzg7cMqerD7L8nDdadI2rwEmtEFZfj4QzTIqiIo0lVURDe40kL2s5DB8nNK88 hRm9HO/DP3PXaTXJf1Pv8b0QWh/JNL3L5QY0NpufamwICn3MDESufP3nRy/iOjKBS2 0QVa1D5sj1tE1Rh7TcuJgPYmCQezMkW4JPDpYAeS5uXQz+HzgvjIF4Q26WAP4fR6bZ ihhkALCtbHAiQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 65D19CE0F15; Thu, 12 Oct 2023 12:32:35 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: gwml@vger.gnuweeb.org, kernel-team@meta.com, w@lwt.eu, =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Willy Tarreau Subject: [PATCH nolibc 17/19] tools/nolibc: add support for constructors and destructors Date: Thu, 12 Oct 2023 12:32:31 -0700 Message-Id: <20231012193233.207857-17-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Thu, 12 Oct 2023 12:34:07 -0700 (PDT) From: Thomas Weißschuh 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 Acked-by: Willy Tarreau Link: https://lore.kernel.org/lkml/20231007-nolibc-constructors-v2-1-ef84693efbc1@weissschuh.net/ --- tools/include/nolibc/crt.h | 23 +++++++++++++++++++- tools/testing/selftests/nolibc/nolibc-test.c | 17 +++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h index a05655b4ce1d..43b551468c2a 100644 --- a/tools/include/nolibc/crt.h +++ b/tools/include/nolibc/crt.h @@ -13,12 +13,23 @@ const unsigned long *_auxv __attribute__((weak)); static void __stack_chk_init(void); static void exit(int); +extern void (*const __preinit_array_start[])(void) __attribute__((weak)); +extern void (*const __preinit_array_end[])(void) __attribute__((weak)); + +extern void (*const __init_array_start[])(void) __attribute__((weak)); +extern void (*const __init_array_end[])(void) __attribute__((weak)); + +extern void (*const __fini_array_start[])(void) __attribute__((weak)); +extern void (*const __fini_array_end[])(void) __attribute__((weak)); + __attribute__((weak)) void _start_c(long *sp) { long argc; char **argv; char **envp; + int exitcode; + void (* const *func)(void); const unsigned long *auxv; /* silence potential warning: conflicting types for 'main' */ int _nolibc_main(int, char **, char **) __asm__ ("main"); @@ -55,8 +66,18 @@ void _start_c(long *sp) ; _auxv = auxv; + for (func = __preinit_array_start; func < __preinit_array_end; func++) + (*func)(); + for (func = __init_array_start; func < __init_array_end; func++) + (*func)(); + /* go to application */ - exit(_nolibc_main(argc, argv, envp)); + exitcode = _nolibc_main(argc, argv, envp); + + for (func = __fini_array_end; func > __fini_array_start;) + (*--func)(); + + exit(exitcode); } #endif /* _NOLIBC_CRT_H */ diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 7e3936c182dc..8da7f15d010e 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,19 @@ int expect_strne(const char *expr, int llen, const char *cmp) #define CASE_TEST(name) \ case __LINE__: llen += printf("%d %s", test, #name); +/* constructors validate that they are executed in definition order */ +__attribute__((constructor)) +static void constructor1(void) +{ + constructor_test_value = 1; +} + +__attribute__((constructor)) +static void constructor2(void) +{ + constructor_test_value *= 2; +} + int run_startup(int min, int max) { int test; @@ -630,6 +646,7 @@ int run_startup(int min, int max) CASE_TEST(environ_HOME); EXPECT_PTRNZ(1, getenv("HOME")); break; CASE_TEST(auxv_addr); EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break; CASE_TEST(auxv_AT_UID); EXPECT_EQ(1, getauxval(AT_UID), getuid()); break; + CASE_TEST(constructor); EXPECT_EQ(1, constructor_test_value, 2); break; case __LINE__: return ret; /* must be last */ /* note: do not set any defaults so as to permit holes above */ -- 2.40.1