From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 58FE73858412; Tue, 7 Mar 2023 15:43:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 58FE73858412 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678203824; bh=zy4Hv9GfOYZ8FbbXrxR7NCLWqbk3bdMKKpgb99EGFhQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=xY9btwqUBVKBK+IPiyV8HUOj/+31QxeU7WBjPw3T+PSrkZZkU/RiiEo6FhxM00uxl QzQgp3rHmnVS5vmFgIda8UWYl3wbbBCKyL51iOa8Po5wxHTLP1tFl0cZdb04sf/JjO O3A1mQMmho1WZnvz2CH2pDpPo866/ROZY+q8nUuA= From: "pexu@gcc-bugzilla.mail.kapsi.fi" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/61882] attribute weak ignored for function templates Date: Tue, 07 Mar 2023 15:43:43 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 4.8.2 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pexu@gcc-bugzilla.mail.kapsi.fi 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: cc Message-ID: In-Reply-To: References: 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=3D61882 Pekka S changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |pexu@gcc-bugzilla.mail.kaps | |i.fi --- Comment #2 from Pekka S --- This is problem is still present on GCC 13 to a certain degree. For an example, the following construct is not possible, as `weak_template'= is essentially always considered being present unless using -O0. Note that `w= eak' behaves as expected even it is not extern "C". If weak address is sourced from GOT (PIC/PIE) and the symbol is undefined, = the program will likely crash, as the address is zero (typically optimized as an unconditional indirect branch).=20=20 Purely static builds are affected as well, but as there is no indirect bran= ch, the non-existing branch is likely replaced by a no-operation during linking= and possibly goes unnoticed for simple constructs like these. [[gnu::weak]] extern void weak(); template [[gnu::weak]] extern void weak_template(); void call () { auto f0 =3D weak; auto f1 =3D weak_template; if (f0) f0(); if (f1) f1(); /* NB: else statement would be always ignored here. */ // indirect_call(f1); /* see below. */ } It is possible to overcome this issue by not calling the acquired address directly but passing it first to another function. This helper function mu= st not be inlined / optimized (which might require additional tricks documente= d in the manual and so forth). [[gnu::noipa]] static void indirect_call (auto f) { if (f) f(); }=