From patchwork Sun Feb 9 10:26:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Georg-Johann Lay X-Patchwork-Id: 106189 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1EB7A3858C62 for ; Sun, 9 Feb 2025 10:27:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1EB7A3858C62 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gjlay.de header.i=@gjlay.de header.a=rsa-sha256 header.s=strato-dkim-0002 header.b=REC5qkhU; dkim=pass header.d=gjlay.de header.i=@gjlay.de header.a=ed25519-sha256 header.s=strato-dkim-0003 header.b=XOMzDX/n X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mo4-p00-ob.smtp.rzone.de (mo4-p00-ob.smtp.rzone.de [85.215.255.22]) by sourceware.org (Postfix) with ESMTPS id 400913858415 for ; Sun, 9 Feb 2025 10:26:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 400913858415 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=gjlay.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gjlay.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 400913858415 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=85.215.255.22 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1739096793; cv=pass; b=S0ULdOYlrG2u7pRkXMD4rftxSUvXBDomgm+eTmXUwPVmTdHJh2w5zvwgsUiuNMkS1tFam9kLaXmyvKoWttkJMqpzs4kR9sZdZYWFHAKFLki+WszY14/D4/45U3j8ApgSaCc2+w0J8YenA83wMblaHeTWxEj+31ON8Xtp8s3y/48= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1739096793; c=relaxed/simple; bh=AMECUY7hCQv+wnIPEnIPkU8Y0jo0gAr4NUGXGVnO+7k=; h=DKIM-Signature:DKIM-Signature:Message-ID:Date:MIME-Version:From: To:Subject; b=nyWhBkXOtYeKcnJ/svmpclCvLEarLWGySl5IxOlaoAaSjukhetDy3P0BwoQ/mKSJiHJWKzWIYSB3LQ03p9UF3itogKim4huObcj0V3miIBXOiFwS6hMI1Xqa3mZHFkR5YCkmKlh6er6ENs5efig2/RLov+pqpUGOaBVXbQ4rs4s= ARC-Authentication-Results: i=2; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 400913858415 ARC-Seal: i=1; a=rsa-sha256; t=1739096791; cv=none; d=strato.com; s=strato-dkim-0002; b=WqaXtFG2g6i2ui6lD3TztBE2MebouEadyu6K5PUJWz9uYW5MXCMTsd6Ekgl5mDHEGV 9Q/Zex+ltpYdwjJGw1B6X+kgzFyZBf6W29yEpedP0woVe4Dbtlico0Q1GHc3IevxzA9e YWQG6aV6SH5mg3nXH7NzK0VC6h3aZANSrxNUfQW9kjO5g/KVajBxgznpUxoXGqYI/v2V YOf+uUk1zEyf8GpTyk+55bL6fZLzmjbp7HCQZSYfuln46xfJMAGGSgGuk/0DzCjrbZnZ f5OTcugDpTB28AN55H2JmU8Q3TCVTCiF/J9XXnHZa/pWdryltJthqyxU7wKuKAMjv490 bYcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1739096791; s=strato-dkim-0002; d=strato.com; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=MaqoObYQ6Cif+p9Q/Yxq34i86y7UEpQ6kNY6bp+7n7U=; b=hZJvVlHMvVDvCrNI9fA75zd6XnI4TS5PB3Nry+w1fY1cxmchVPtHqgfVJiiD51txWf TXT/nUxAZHw/7T7nVTx0Pt9BZEUjsfAuh0A1jJ438eYmusDatRzT7St7P3OfzmrnbVx+ e9mNF3jf+GHUSw0wU0rW1Y+d2Y0Q+B/Tz7OgFzL2E174bYVSTAuA+wretwuic8INPYeZ T3dhFzY4KlzMV8Y+0lrQMujrOwBR7VinqxeSZZFNRYNSQ+JDs5e7RAxq4fcRHrHzOfAJ TmK26mmV1Or3Nuyy5A6si3u3zch+bHkbNESpd102Z0lJxOyd92B9cE5iWCBI6DtHLRRs VzVg== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1739096791; s=strato-dkim-0002; d=gjlay.de; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=MaqoObYQ6Cif+p9Q/Yxq34i86y7UEpQ6kNY6bp+7n7U=; b=REC5qkhUGlfZAYbwSYccqY3p6dIHSGYOh4kpq6ymte6+9fNgCV5q/7HOvJHQJLVXKa hfZMrW8ytMQ9SnHItpJe/5koZZgroRWTB1Ks09LcWhMi0iXE1N7txkNSPMLEI4xN0+3B UKCWe4LcnER4gxaJBN83An5EEjEwTMJNzqmGTtWeUM+oZ/GbIo8M1LpjTdKXl4J2og/3 rZHsICZeysJuGYCK2Wxk+B47aG8sF8+Q8oOz5xsOMzSAXzlhqqUT6ymq3a/c9U0NuApg GJXVSQeBqidOGx51Wh6+Ec8+NZAeH1ZN1gxFo19pmi6fIGxv26mLiYqciJG5/CYPEg4y fLZQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1739096791; s=strato-dkim-0003; d=gjlay.de; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=MaqoObYQ6Cif+p9Q/Yxq34i86y7UEpQ6kNY6bp+7n7U=; b=XOMzDX/nmPXFRqSR79uYNmvEj64KgHrzg9i2FmSg/8QY+o1lxpdZ0RNmWK5oh9khF3 DC7m4q+0uR2VBkkV0aDw== X-RZG-AUTH: ":LXoWVUeid/7A29J/hMvvT3koxZnKXKoq0dKoR0vdrjJzUczcM7FHjKc5" Received: from [192.168.2.102] by smtp.strato.de (RZmta 51.2.23 DYNA|AUTH) with ESMTPSA id xfbeeb119AQV634 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Sun, 9 Feb 2025 11:26:31 +0100 (CET) Message-ID: <515a48f5-79df-46e8-a6ce-388398f01da3@gjlay.de> Date: Sun, 9 Feb 2025 11:26:30 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Georg-Johann Lay Content-Language: en-US To: "gcc-patches@gcc.gnu.org" , Denis Chertykov Subject: [patch,avr] Add -mno-call-main to tweak running main() X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS, TXREP, URIBL_SBL_A autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org On devices with very limited resources, it may be desirable to run main in a more efficient way than provided by the startup code XCALL main XJMP exit from section .init9. In AVR-LibC v2.3, that code has been moved to libmcu.a, hence symbol __call_main can be satisfied so that the respective code is no more pulled in from that library. Instead, main can be run by putting it in section .init9. The patch adds attributes noreturn and section(".init9"), and sets __call_main=0 when it encounters main(). Ok for trunk? Johann --- AVR: target/118806 - Add -mno-call-main to tweak running main(). On devices with very limited resources, it may be desirable to run main in a more efficient way than provided by the startup code XCALL main XJMP exit from section .init9. In AVR-LibC v2.3, that code has been moved to libmcu.a, hence symbol __call_main can be satisfied so that the respective code is no more pulled in from that library. Instead, main can be run by putting it in section .init9. The patch adds attributes noreturn and section(".init9"), and sets __call_main=0 when it encounters main(). gcc/ PR target/118806 * config/avr/avr.opt (-mcall-main): New option and... (avropt_call_main): ...variable. * config/avr/avr.cc (avr_no_call_main_p): New variable. (avr_insert_attributes) [-mno-call-main, main]: Add attributes noreturn and section(".init9") to main. Set avr_no_call_main_p. (avr_file_end) [avr_no_call_main_p]: Define symbol __call_main. * doc/invoke.texi (AVR Options) <-mno-call-main>: Document. <-mnodevicelib>: Extend explanation. AVR: target/118806 - Add -mno-call-main to tweak running main(). On devices with very limited resources, it may be desirable to run main in a more efficient way than provided by the startup code XCALL main XJMP exit from section .init9. In AVR-LibC v2.3, that code has been moved to libmcu.a, hence symbol __call_main can be satisfied so that the respective code is no more pulled in from that library. Instead, main can be run by putting it in section .init9. The patch adds attributes noreturn and section(".init9"), and sets __call_main=0 when it encounters main(). gcc/ PR target/118806 * config/avr/avr.opt (-mcall-main): New option and... (avropt_call_main): ...variable. * config/avr/avr.cc (avr_no_call_main_p): New variable. (avr_insert_attributes) [-mno-call-main, main]: Add attributes noreturn and section(".init9") to main. Set avr_no_call_main_p. (avr_file_end) [avr_no_call_main_p]: Define symbol __call_main. * doc/invoke.texi (AVR Options) <-mno-call-main>: Document. <-mnodevicelib>: Extend explanation. diff --git a/gcc/config/avr/avr.cc b/gcc/config/avr/avr.cc index 9bfb3785ddb..c31d06d6d20 100644 --- a/gcc/config/avr/avr.cc +++ b/gcc/config/avr/avr.cc @@ -230,6 +230,9 @@ bool avr_need_clear_bss_p = false; bool avr_need_copy_data_p = false; bool avr_has_rodata_p = false; +/* To track if we satisfy __call_main from AVR-LibC. */ +bool avr_no_call_main_p = false; + /* Counts how often pass avr-fuse-add has been executed. Is is kept in sync with cfun->machine->n_avr_fuse_add_executed and serves as an insn condition for shift insn splitters. */ @@ -11712,6 +11715,32 @@ avr_insert_attributes (tree node, tree *attributes) NULL, *attributes); } +#if defined WITH_AVRLIBC + if (avropt_call_main == 0 + && TREE_CODE (node) == FUNCTION_DECL + && MAIN_NAME_P (DECL_NAME (node))) + { + if (lookup_attribute ("section", *attributes)) + { + warning (OPT_Wattributes, "% attribute on main function" + " inhibits %<-mno-call-main%>"); + } + else + { + if (!lookup_attribute ("noreturn", *attributes)) + *attributes = tree_cons (get_identifier ("noreturn"), + NULL_TREE, *attributes); + // Put main into section .init9 so that it is executed even + // though it's not called. + tree init9 = build_string (1 + strlen (".init9"), ".init9"); + tree arg = build_tree_list (NULL_TREE, init9); + *attributes = tree_cons (get_identifier ("section"), + arg, *attributes); + avr_no_call_main_p = true; + } + } // -mno-call-main +#endif // AVR-LibC + avr_handle_isr_attribute (node, attributes, "signal"); avr_handle_isr_attribute (node, attributes, "interrupt"); @@ -12311,6 +12340,15 @@ avr_file_end (void) if (avr_need_clear_bss_p) fputs (".global __do_clear_bss\n", asm_out_file); + + /* Don't let __call_main call main() and exit(). + Defining this symbol will keep the code from being pulled + in from lib.a as requested by AVR-LibC's gcrt1.S. + We invoke main() by other means: putting it in .init9. */ + + if (avr_no_call_main_p) + fputs (".global __call_main\n" + "__call_main = 0\n", asm_out_file); } diff --git a/gcc/config/avr/avr.opt b/gcc/config/avr/avr.opt index 0184b40f4e5..af46898ba78 100644 --- a/gcc/config/avr/avr.opt +++ b/gcc/config/avr/avr.opt @@ -42,6 +42,10 @@ mn-flash= Target RejectNegative Joined Var(avropt_n_flash) UInteger Init(-1) This option is used internally. Set the number of 64 KiB flash segments. +mcall-main +Target Var(avropt_call_main) UInteger Init(1) +Call main and exit. + mskip-bug Target Mask(SKIP_BUG) This option is used internally. Indicate presence of a processor erratum. Do not skip 32-bit instructions. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 0aef2abf05b..986b1a5fb8b 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -908,7 +908,7 @@ Objective-C and Objective-C++ Dialects}. @gccoptlist{-mmcu=@var{mcu} -mabsdata -maccumulate-args -mcvt -mbranch-cost=@var{cost} -mfuse-add=@var{level} -mfuse-move=@var{level} -mcall-prologues -mgas-isr-prologues -mint8 -mflmap --mdouble=@var{bits} -mlong-double=@var{bits} +-mdouble=@var{bits} -mlong-double=@var{bits} -mno-call-main -mn_flash=@var{size} -mfract-convert-truncate -mno-interrupts -mmain-is-OS_task -mrelax -mrmw -mstrict-X -mtiny-stack -mrodata-in-ram -msplit-bit-shift -msplit-ldst @@ -24457,6 +24457,18 @@ Do not save registers in @code{main}. The effect is the same like attaching attribute @ref{AVR Function Attributes,,@code{OS_task}} to @code{main}. It is activated per default if optimization is on. +@opindex mno-call-main +@opindex mcall-main +@item -mno-call-main +Don't call @code{main} and @code{exit}. +Instead, put @code{main} in section +@w{@uref{https://avrdudes.github.io/avr-libc/avr-libc-user-manual/mem_sections.html#sec_dot_init,@code{.init9}}}. + +This option can be used for devices with very limited resources in order +to save a few bytes of code and stack space. It will work as expected since +@w{@uref{https://github.com/avrdudes/avr-libc/issues/1012,AVR-LibC v2.3}}. +With older versions, there will be no performance gain. + @opindex mno-interrupts @item -mno-interrupts Generated code is not compatible with hardware interrupts. @@ -24535,7 +24547,19 @@ Allow to use truncation instead of rounding towards zero for fractional fixed-po @opindex nodevicelib @item -nodevicelib -Don't link against AVR-LibC's device specific library @code{lib.a}. +Don't link against AVR-LibC's device specific library @code{lib@var{mcu}.a}. + +Notice that since AVR-LibC v2.3, that library contains code that is +essential for the correct functioning of a program. In particular, +it contains parts of the startup code like: +@w{@uref{https://github.com/avrdudes/avr-libc/issues/1011,@code{__init_sp}}} +to initialize the stack pointer with symbol @code{__stack}, +@w{@uref{https://github.com/avrdudes/avr-libc/issues/1010,@code{__init_cvt}}} +to set up the hardware to use a compact vector table with @option{-mcvt}, +@w{@uref{https://github.com/avrdudes/avr-libc/issues/1012,@code{__call_main}}} +to call @code{main} and @code{exit}, and +@w{@uref{https://github.com/avrdudes/avr-libc/issues/931,@code{__do_flmap_init}}} +to set up FLMAP according to symbol @code{__flmap}. @opindex nodevicespecs @item -nodevicespecs