https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86410
Bug ID: 86410 Summary: Incorrect init_array ordering Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: iripoll at disca dot upv.es Target Milestone: --- Created attachment 44353 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44353&action=edit Samplig the init, fin, constructor and atexit functions. Hello, While testing the glibc patch of Florian (from Red Hat) to remove some unnecessary code from the executable image, we have found what seems to be a gcc issue. The problem is related to the way the .init_array and .fini_array sections are created. When not using any optimizations the resulting initialization sequence is different (and seems to be incorrect). When using any form of optimization (i.e. -O1 -O2 -O3 -Os) the init sequence is correct. The issue occurs to dynamic and static executables. Attached is a PoC of the problem. The same code results in a different execution when compiled with different flags: ---- When compile with -O2 ---- gcc testing_init_arrays.c -o testing_init_arrays-O2 ./testing_init_arrays-O2 preinit2 preinit1 constructor1 constructor2 init2 init1 Main begins my_atexit2 my_atexit fini1 fini2 destructor ---- When compiled without optimization ---- gcc testing_init_arrays.c -o testing_init_arrays-none ./testing_init_arrays-none preinit1 preinit2 constructor1 init1 init2 constructor2 Main begins my_atexit2 my_atexit destructor fini2 fini1 Regards, Ismael and Hector.