https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88641
Bug ID: 88641 Summary: crtstuff.c ctors/dtors list breaks with -fdata-sections Product: gcc Version: 8.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libgcc Assignee: unassigned at gcc dot gnu.org Reporter: ambrop7 at gmail dot com Target Milestone: --- Created attachment 45309 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45309&action=edit Patch to compile crtstuff.c with -fno-function-sections and -fno-data-sections. Breakage of ctors/dtors lists in crtbegin.o/crtend.o may occur when CFLAGS_FOR_TARGET contains -fdata-sections, leading to a crash at program startup. The issue is in libgcc/crtstuff.c where __LIBGCC_CTORS_SECTION_ASM_OP__ is used. I have experienced this with the MicroBlaze architecture, but any architecture where this code path is used has to be affected. Specifically, the problem is in the following code: static func_ptr force_to_data[1] __attribute__ ((__used__)) = { }; asm (__LIBGCC_CTORS_SECTION_ASM_OP__); STATIC func_ptr __CTOR_LIST__[1] __attribute__ ((__used__, aligned(sizeof(func_ptr)))) = { (func_ptr) (-1) }; Here asm is used to make the variable go into a specific section, usually ".ctors" or ".dtors". However, with -fdata-sections, gcc will anyway put it into its own section such as ".data.__CTOR_LIST__", and the asm will have no effect. The result is that these variables will not be found by the linker script using expressions like "KEEP (*crtbegin.o(.ctors))", which will cause a runtime failure in __do_global_ctors_aux as the ctors list will have no terminator. I believe that -ffunction-section could also cause problems in the crtstuff code, in particular where __LIBGCC_TEXT_SECTION_ASM_OP__ is used; there seems to be an assumption that all functions are by default placed in the ".text" section, which is not true with -ffunction-sections. I suggest fixing these issues by ensuring that crtstuff.c is compiled with -fno-function-sections and -fno-data-sections. I am attaching a patch that I have verified fixes the ctors/dtors section problem.