From nobody Mon Feb 9 05:58:18 2026 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C46CA40756F for ; Wed, 4 Feb 2026 12:46:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770209165; cv=none; b=eOSVaB0wLke4DpNvCS+fIHI2NnZ2/aBRGvn7le9bKwO9lJvNXd8hFhAflx0E2kuJVLFqnPByY5EZaQVmHnZjU91+3xgL+E38eQ+PO0ifPSJgSMOmwndXdmoGVx305v2EOxqRilda7R8lqZUnHzyqqSJUT0m8mxxq1Adfx1c0VoU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770209165; c=relaxed/simple; bh=kw8nIebEJ1W/2MtV19pDi8GYjx1nCCzD9M9vXLLoZ8U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=juLVfmlOq+3XTyrX83yddMkARxlR27U/HbjcfMNzJWZuPJxRFF4EZ0CkbZZLwpONlamzdCO7kCOwJh1PesTHEZFO6POz5RtKLH6A02rQtKkdqMe8nzLBDpqM/75SqFjYZjVAOmkfCbRke8sSYMPymnB/pG8QtpDIAeQt0nz/V6s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=thingy.jp; spf=pass smtp.mailfrom=0x0f.com; dkim=pass (1024-bit key) header.d=thingy.jp header.i=@thingy.jp header.b=e/G7gIPg; arc=none smtp.client-ip=209.85.216.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=thingy.jp Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=0x0f.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=thingy.jp header.i=@thingy.jp header.b="e/G7gIPg" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-34c27d14559so3529327a91.2 for ; Wed, 04 Feb 2026 04:46:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thingy.jp; s=google; t=1770209165; x=1770813965; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3yVWdm59KuOGRwQxop0ZdtM+o+m7FWy3RTyTSA11l9g=; b=e/G7gIPgW0NUSrQmnoZv/Lrbhc6po/9uBhryAMSknrX/oI3RlkpRY8YzirVxZx92qx PqTi94ye6/FllCQc9AKOJVfotky/dpSxmzTcEQ8Sh7knDdzZe+D6dZerKkUaJ/rbyrP+ UkJxs1Aq2zBbJSe2C1MOyfduchqNA66HdPUx4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770209165; x=1770813965; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=3yVWdm59KuOGRwQxop0ZdtM+o+m7FWy3RTyTSA11l9g=; b=wzHFCZxAB+/g/n+CWECOamTfOjw4yVjOwNiAZj3Wmz2sDbO/bwRDZmHJG9VyBRYrcH JGyIPLnOG3tY/kREY3TBHUbNAT7vc5yEzTqexbCGgrtVf1aMDOSFSlEvEaIMxd1JmIMM mdA5+ey0mXtcwxWU+N8Dp/F8vxeNBAy0uOHOTmBgHXfZ065sH7ydf5Lv/7Chq0kRDuaF ql5rX5VH8c1tnYnck7qAyhDZnCO8etSWB1CyE7hOYqxvB6pEsSLSUrHkzGgkFQx/fdQo lIVwYv9S+TFQcAppxBvzOE7g61HovUEcu+2jP6TJz1YVOB3kb86gjdGwIYjifrfDl8bn mLUA== X-Gm-Message-State: AOJu0YyjxToaaUeMPZ/m31KCe0156Y9/F8tzCf3Ru0/AVX4NLqTv9PpE bDFsZ53004dj2TaPPkFzC0wl+C+v/k/Nb4711Iy+AISHT3J80HfB82oYxGTzOs3fClI= X-Gm-Gg: AZuq6aLsgHOU+gIk+Ohgd6+omBFT8AWkr18fuf5YdDlgUvbBTqLaphf/HCl2NB2L2Zw P0qO8NI6/Hyjr6RhHiNcaQrEjvhXe5iWHaK6jFhbqkWh0Kifbbk/39xr6UXDrG7Y6B9bpl8Qe7l y4FODmuORH8GqFdSSr3EZbnv6ovSfG+bBctlvCyPKamc9rOk27UMyj/LDRXjlLjCqNNc8SwtX43 XlhnBamNtYL75aL9NmgiuuUIINMQq4PPcObaVNtxRiO7IpR15oJFN4jois/dxV9S5XsNQYpRZpe txz+DB3YZuqutDbPMM+JsWu6sYy5TnnbowEvUBEcxdK6JSbLWOPQEuUqnTdY960tpfzG9Wf3sU8 s9z6EjrsC3ATAYZTjdtLzoEXiQhvElifpV/T2FgsF7aeLtztt6Pni55lbPPp+dDjd3XV0nZhcHT 4Ea0n0i6s5Lc1ol1pnR+Kn2urLoC3PNIxO/0WLRgIiBoqYp0e4hd06fLvErCCPLKslm5X+hcCRe 9BC X-Received: by 2002:a17:90b:2fc8:b0:340:2f48:b51a with SMTP id 98e67ed59e1d1-35487106f8cmr2922872a91.15.1770209165090; Wed, 04 Feb 2026 04:46:05 -0800 (PST) Received: from kinako.work.home.arpa (p1490223-ipxg00b01sizuokaden.shizuoka.ocn.ne.jp. [153.223.134.223]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-354862effa6sm2378920a91.9.2026.02.04.04.46.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Feb 2026 04:46:04 -0800 (PST) From: Daniel Palmer To: w@1wt.eu, linux@weissschuh.net Cc: linux-kernel@vger.kernel.org, Daniel Palmer Subject: [RFC PATCH v2 02/10] tools/nolibc: crt: Split _start_c() into stack-only and normal parts Date: Wed, 4 Feb 2026 21:45:34 +0900 Message-ID: <20260204124542.523567-3-daniel@thingy.jp> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260204124542.523567-1-daniel@thingy.jp> References: <20260204124542.523567-1-daniel@thingy.jp> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To prepare for nolibc programs being able to relocate themselves we need to split _start_c() into two parts: - One part that only uses the stack so there are no accesses via the GOT etc that isn't setup yet. Note that on m68k at least this also means forcing the stackprotector off because accessing the stack protector canary is done via the GOT. - Another part that is called after we have done relocation so it is safe to access global variables etc that might use the GOT etc. Signed-off-by: Daniel Palmer --- tools/include/nolibc/compiler.h | 6 ++++ tools/include/nolibc/crt.h | 57 ++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/tools/include/nolibc/compiler.h b/tools/include/nolibc/compile= r.h index 87090bbc53e0..3f403e54e4f4 100644 --- a/tools/include/nolibc/compiler.h +++ b/tools/include/nolibc/compiler.h @@ -47,4 +47,10 @@ # define __nolibc_fallthrough do { } while (0) #endif /* __nolibc_has_attribute(fallthrough) */ =20 +#if __nolibc_has_feature(undefined_behavior_sanitizer) +# define __no_sanitize __attribute__((no_sanitize("function"))) +#else +# define __no_sanitize +#endif + #endif /* _NOLIBC_COMPILER_H */ diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h index d9262998dae9..fab042f1ff62 100644 --- a/tools/include/nolibc/crt.h +++ b/tools/include/nolibc/crt.h @@ -27,26 +27,45 @@ extern void (*const __init_array_end[])(int, char **, c= har**) __attribute__((wea extern void (*const __fini_array_start[])(void) __attribute__((weak)); extern void (*const __fini_array_end[])(void) __attribute__((weak)); =20 -void _start_c(long *sp); -__attribute__((weak,used)) -#if __nolibc_has_feature(undefined_behavior_sanitizer) - __attribute__((no_sanitize("function"))) -#endif -void _start_c(long *sp) +void _start_c_global_data(long argc, char **argv, char **envp, const unsig= ned long *auxv); +__attribute__((weak, used)) +void __no_sanitize __no_stack_protector _start_c_global_data(long argc, ch= ar **argv, char **envp, const unsigned long *auxv) { - long argc; - char **argv; - char **envp; - int exitcode; void (* const *ctor_func)(int, char **, char **); void (* const *dtor_func)(void); - const unsigned long *auxv; /* silence potential warning: conflicting types for 'main' */ int _nolibc_main(int, char **, char **) __asm__ ("main"); + int exitcode; =20 /* initialize stack protector */ __stack_chk_init(); =20 + environ =3D envp; + _auxv =3D auxv; + + for (ctor_func =3D __preinit_array_start; ctor_func < __preinit_array_end= ; ctor_func++) + (*ctor_func)(argc, argv, envp); + for (ctor_func =3D __init_array_start; ctor_func < __init_array_end; ctor= _func++) + (*ctor_func)(argc, argv, envp); + + /* go to application */ + exitcode =3D _nolibc_main(argc, argv, envp); + + for (dtor_func =3D __fini_array_end; dtor_func > __fini_array_start;) + (*--dtor_func)(); + + exit(exitcode); +} + +void _start_c(long *sp); +__attribute__((weak, used)) +void __no_sanitize __no_stack_protector _start_c(long *sp) +{ + long argc; + char **argv; + char **envp; + const unsigned long *auxv; + /* * sp : argc <-- argument count, required by main() * argv: argv[0] <-- argument vector, required by main() @@ -69,25 +88,13 @@ void _start_c(long *sp) argv =3D (void *)(sp + 1); =20 /* find environ */ - environ =3D envp =3D argv + argc + 1; + envp =3D argv + argc + 1; =20 /* find _auxv */ for (auxv =3D (void *)envp; *auxv++;) ; - _auxv =3D auxv; =20 - for (ctor_func =3D __preinit_array_start; ctor_func < __preinit_array_end= ; ctor_func++) - (*ctor_func)(argc, argv, envp); - for (ctor_func =3D __init_array_start; ctor_func < __init_array_end; ctor= _func++) - (*ctor_func)(argc, argv, envp); - - /* go to application */ - exitcode =3D _nolibc_main(argc, argv, envp); - - for (dtor_func =3D __fini_array_end; dtor_func > __fini_array_start;) - (*--dtor_func)(); - - exit(exitcode); + _start_c_global_data(argc, argv, envp, auxv); } =20 #endif /* NOLIBC_NO_RUNTIME */ --=20 2.51.0