Bug 88641 - crtstuff.c ctors/dtors list breaks with -fdata-sections
Summary: crtstuff.c ctors/dtors list breaks with -fdata-sections
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: libgcc (show other bugs)
Version: 8.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: build
Depends on:
Blocks:
 
Reported: 2018-12-31 10:03 UTC by Ambroz Bizjak
Modified: 2021-09-19 09:22 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-09-19 00:00:00


Attachments
Patch to compile crtstuff.c with -fno-function-sections and -fno-data-sections. (421 bytes, patch)
2018-12-31 10:03 UTC, Ambroz Bizjak
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ambroz Bizjak 2018-12-31 10:03:25 UTC
Created attachment 45309 [details]
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.
Comment 1 Andrew Pinski 2021-09-19 09:22:31 UTC
Confirmed.