]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
AVR: target/118806 - Add -mno-call-main to tweak running main().
authorGeorg-Johann Lay <avr@gjlay.de>
Sun, 9 Feb 2025 09:55:20 +0000 (10:55 +0100)
committerGeorg-Johann Lay <avr@gjlay.de>
Wed, 12 Feb 2025 20:53:19 +0000 (21:53 +0100)
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.

gcc/config/avr/avr.cc
gcc/config/avr/avr.opt
gcc/doc/invoke.texi

index 9bfb3785ddbd3a3e183a6671aec208936c07d22e..bb00e3347206cefadb2372e43842c8576e2d1fc8 100644 (file)
@@ -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,45 @@ 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)))
+    {
+      const char *s_section_name = nullptr;
+
+      if (tree a_sec = lookup_attribute ("section", *attributes))
+       if (TREE_VALUE (a_sec))
+         if (tree t_section_name = TREE_VALUE (TREE_VALUE (a_sec)))
+           if (TREE_CODE (t_section_name) == STRING_CST)
+             s_section_name = TREE_STRING_POINTER (t_section_name);
+
+      bool in_init9_p = s_section_name && !strcmp (s_section_name, ".init9");
+
+      if (s_section_name && !in_init9_p)
+       {
+         warning (OPT_Wattributes, "%<section(\"%s\")%> attribute on main"
+                  " function inhibits %<-mno-call-main%>", s_section_name);
+       }
+      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.
+         if (!in_init9_p)
+           {
+             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 +12353,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<mcu>.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);
 }
 
 
index 0184b40f4e50bdeb11f0c47e94389870b487c08e..d22a118ad9e50440fd91624121118b0177f68dac 100644 (file)
@@ -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 (default).
+
 mskip-bug
 Target Mask(SKIP_BUG)
 This option is used internally. Indicate presence of a processor erratum.  Do not skip 32-bit instructions.
index bd42a8bc40024501def0b404c0c43a2ae5f5bfa3..b3ff3bbaa36dead3e4d9cb350c764cb06bb7f83b 100644 (file)
@@ -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
@@ -24456,6 +24456,24 @@ 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 run @code{main} by means of
+@example
+XCALL  main
+XJMP   exit
+@end example
+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}}}
+so that no call is required.
+By setting this options the user asserts that @code{main} will not return.
+
+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.
@@ -24534,7 +24552,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<mcu>.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