public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pexu@gcc-bugzilla.mail.kapsi.fi" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-61882-4-UHuHVdBQ1c@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-61882-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61882

Pekka S <pexu@gcc-bugzilla.mail.kapsi.fi> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pexu@gcc-bugzilla.mail.kaps
                   |                            |i.fi

--- Comment #2 from Pekka S <pexu@gcc-bugzilla.mail.kapsi.fi> ---
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 `weak'
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).  

Purely static builds are affected as well, but as there is no indirect branch,
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 <typename T>
[[gnu::weak]] extern void weak_template();

void call ()
{
  auto f0 = weak;
  auto f1 = weak_template<int>;
  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 must
not be inlined / optimized (which might require additional tricks documented in
the manual and so forth).

[[gnu::noipa]] static void indirect_call (auto f)
{
  if (f) f();
}

      parent reply	other threads:[~2023-03-07 15:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-22 16:40 [Bug c++/61882] New: " msebor at gmail dot com
2014-07-27 20:16 ` [Bug c++/61882] " olegendo at gcc dot gnu.org
2023-03-07 15:43 ` pexu@gcc-bugzilla.mail.kapsi.fi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-61882-4-UHuHVdBQ1c@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).