From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 68161385803F; Thu, 22 Sep 2022 19:12:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 68161385803F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663873955; bh=YBIWPH3bYviIQZc/0eENffGfgNHLI0K0ItUezmmQLp0=; h=From:To:Subject:Date:From; b=bEVvG6OyDpunkQlybgwShqsQk14K4DM/9pa+sW8cHbd7Y+tkrm/mrX+8k8AggFugd 6eRVv1HjCozw6EYd1s2f8pfPx4NsPfAtJAmPoLNC9c05TP4LCfcT71jKEXxjZXSm8C yBt74VdcE2B5nKzDgvKPv7eAccr8PFy3zyPyqO7M= From: "stian.skjelstad at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/107010] New: iterate section("mysection") struct mystruct fails if first node is const and compiling with O2 or greater Date: Thu, 22 Sep 2022 19:12:34 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: stian.skjelstad at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107010 Bug ID: 107010 Summary: iterate section("mysection") struct mystruct fails if first node is const and compiling with O2 or greater Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: stian.skjelstad at gmail dot com Target Milestone: --- Created attachment 53612 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D53612&action=3Dedit example setup compiled with both working and non-working flags I have a use-case where plugins can be be compiled built in, and they are detected by having some meta-data added to a section, and in runtime they c= an be iterated. #define SECTION __attribute__ ((section ("plugin_list"))) #define USED __attribute__ ((used)) Core code: SECTION const struct plugindata first =3D {"dummy", 0, 0}; Each plugin (many copies): SECTION USED static const struct plugindata =3D {"pluginname", foo, bar}; Last file linked contains a terminator SECTION USED static const struct plugindata end =3D {0, 0, 0}; During linking they will gathered and can iterated using code like this: void show_plugin_names(void) { const struct plugindata *iterator =3D &first; while (iterator->name) { fprintf (stderr, "name=3D%s\n", iterator->name); iterator++; } } This works as long as you do not combine "first" being const and using gcc = -O2 Tested with: gcc-9 (Ubuntu 9.4.0-5ubuntu1) 9.4.0 gcc-10 (Ubuntu 10.3.0-15ubuntu1) 10.3.0 gcc-11 (Ubuntu 11.2.0-19ubuntu1) 11.2.0 gcc-12 (Ubuntu 12.1.0-2ubuntu1~22.04) 12.1.0 (using clang works) I have also tested -fno-strict-aliasing -fwrapv and it didn't change the behaviour. I do accept that the use-case is probably a bit unorthodox and that it migh= t be dismissed as compiler-abuse.=