]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
AVR: target/125194 - Make -mno-call-main work with -flto.
authorGeorg-Johann Lay <avr@gjlay.de>
Thu, 14 May 2026 08:21:28 +0000 (10:21 +0200)
committerGeorg-Johann Lay <avr@gjlay.de>
Thu, 14 May 2026 08:29:17 +0000 (10:29 +0200)
Instead of emitting  .global __call_main + __call_main=0  in some
module, it uses a %{mno-call-main: --defsym __call_main=0} spec.

The problem with the old implementation is that avr_no_call_main_p
was set by cc1[plus] (in avr_insert_attributes) but used by lto1
(in avr_file_end).  The new approach uses  --defsym __call_main=0
in order to avoid link fails due to multiple definitions of __call_main
in *.o and lib<mcu>.a.

PR target/125194
gcc/
* config/avr/avr.cc (avr_no_call_main_p): Remove variable...
(avr_file_end): ...and code that uses it.
(avr_insert_attributes): Same.  Add "used" to main attributes
when -mno-call-main.
* config/avr/gen-avr-mmcu-specs.cc (print_mcu): Emit code
for link_no_call_main specs.
* config/avr/specs.h (LINK_SPEC): Add %(link_no_call_main).

gcc/config/avr/avr.cc
gcc/config/avr/gen-avr-mmcu-specs.cc
gcc/config/avr/specs.h

index b0e9d9000e43391d7bbffd14b0dd3be764eb0325..25cb518b0f3ee174a394368b96068f6c7e340811 100644 (file)
@@ -249,9 +249,6 @@ 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.  */
@@ -11906,7 +11903,9 @@ avr_insert_attributes (tree node, tree *attributes)
              *attributes = tree_cons (get_identifier ("section"),
                                       arg, *attributes);
            }
-         avr_no_call_main_p = true;
+         if (!lookup_attribute ("used", *attributes))
+           *attributes = tree_cons (get_identifier ("used"),
+                                    NULL_TREE, *attributes);
        }
     } // -mno-call-main
 #endif // AVR-LibC
@@ -12467,15 +12466,6 @@ 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 42623d7e87c940a99898bfc82bd8345e3bcbeb13..470d40538e536bebb2cd3d28dbac04efc704c523 100644 (file)
@@ -355,6 +355,8 @@ print_mcu (const avr_mcu_t *mcu, const McuInfo &mi)
 
   fprintf (f, "*link_relax:\n\t%s\n\n", LINK_RELAX_SPEC);
 
+  fprintf (f, "*link_no_call_main:\n\t%s\n\n", "%{mno-call-main: --defsym __call_main=0}");
+
   fprintf (f, "*link_arch:\n\t%s", link_arch_spec);
   if (mi.is_device
       && rodata_pm_offset)
index 4f71f8a6570f72f95330d5ac9ce317e0510d495e..1fca4a1d6eecd332b537ecaa3f5b6d3afc3e94d0 100644 (file)
@@ -67,6 +67,7 @@ along with GCC; see the file COPYING3.  If not see
   "%(link_relax) "                              \
   "%(link_pmem_wrap) "                          \
   "%(link_rodata_in_ram) "                      \
+  "%(link_no_call_main) "                       \
   "%{shared:%eshared is not supported} "
 
 #undef  LIB_SPEC