public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/61882] New: attribute weak ignored for function templates
@ 2014-07-22 16:40 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
  0 siblings, 2 replies; 3+ messages in thread
From: msebor at gmail dot com @ 2014-07-22 16:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 61882
           Summary: attribute weak ignored for function templates
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gmail dot com

In GCC 4.5 and later, and at -O and above, attribute weak (but not weak alias)
is silently ignored on declarations of function templates and calls to
specializations of such templates are inlined into their callers.

The program below shows that GCC inlines calls to the function template
specialization instantiated from the weak primary template baz, but emits a
weak symbol for the weak alias foo<int>. GCC 4.2 honored the attribute and
emitted a weak symbol for both foo<int> and baz<int>. Clang 3.4 also emits a
weak symbol for both as expected. Disabling optimization changes the current
behavior to match that of GCC 4.2.1 and Clang 3.4.

The GCC documentation of attribute weak doesn't say whether or not the
attribute is intended to have the same effect on specializations of function
templates as it does on ordinary functions. If the current GCC behavior is
intended, the documentation should be updated to make it clear when attribute
weak is ignored, and GCC should be enhanced to issue a warning when the
attribute is ignored.

$ (set -x; cc=/auto/compiler-dev/msebor/contrib/cel-5.50/bin/g++; cat z.c &&
$cc -c -fpic -DFOO=1 -O -Wall -Wextra z.c && $cc -fpic -Wall -Wextra z.c z.o &&
./a.out)
+ cc=/auto/compiler-dev/msebor/contrib/cel-5.50/bin/g++
+ cat z.c
template <class T> void foo (T);
template <class T> void baz (T);
void foobar ();

#if FOO
template <class T> void __attribute__ ((weak, alias ("bar"))) foo (T);

static void bar (int) { }

template <class T> void __attribute__ ((weak)) baz (T) { }

void foobar () { foo (0), baz (0); }

#else

#  include <stdio.h>

template <> void foo (int) { puts (__func__); }
template <> void baz (int) { puts (__func__); }

int main (void) { foobar (); }

#endif

+ /auto/compiler-dev/msebor/contrib/cel-5.50/bin/g++ -c -fpic -DFOO=1 -O -Wall
-Wextra z.c
+ /auto/compiler-dev/msebor/contrib/cel-5.50/bin/g++ -fpic -Wall -Wextra z.c
z.o
+ ./a.out
foo<int>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/61882] attribute weak ignored for function templates
  2014-07-22 16:40 [Bug c++/61882] New: attribute weak ignored for function templates msebor at gmail dot com
@ 2014-07-27 20:16 ` olegendo at gcc dot gnu.org
  2023-03-07 15:43 ` pexu@gcc-bugzilla.mail.kapsi.fi
  1 sibling, 0 replies; 3+ messages in thread
From: olegendo at gcc dot gnu.org @ 2014-07-27 20:16 UTC (permalink / raw)
  To: gcc-bugs

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

Oleg Endo <olegendo at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |olegendo at gcc dot gnu.org

--- Comment #1 from Oleg Endo <olegendo at gcc dot gnu.org> ---
Note: not only attribute weak, but also other attributes.  E.g. per-function
optimize attribute in order to enable unrolling of all loops inside of the
template function.  An example where this can be useful is e.g. PR 54236
comment #6, operator + function.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/61882] attribute weak ignored for function templates
  2014-07-22 16:40 [Bug c++/61882] New: attribute weak ignored for function templates 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
  1 sibling, 0 replies; 3+ messages in thread
From: pexu@gcc-bugzilla.mail.kapsi.fi @ 2023-03-07 15:43 UTC (permalink / raw)
  To: gcc-bugs

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();
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-03-07 15:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-22 16:40 [Bug c++/61882] New: attribute weak ignored for function templates 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 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).