public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Weak symbol in an executable, overridable by shared libraries at runtime
@ 2013-11-02 21:44 Lorenzo Pistone
  2013-11-03  4:31 ` Alan Modra
  2013-11-03 12:05 ` H.J. Lu
  0 siblings, 2 replies; 3+ messages in thread
From: Lorenzo Pistone @ 2013-11-02 21:44 UTC (permalink / raw)
  To: binutils

Hello,
Is there a way to define a weak symbol in an executable (not a .so!), 
that can be overridden by the runtime linking process by a symbol found 
in another library?
The aim is to provide a fallback implementation, within my executable, 
for systems where a specific function in a standard library cannot be 
found. More exactly, if I compile my program with -ffast-math, some 
calls to __pow_finite are generated: on recent systems this function is 
in libm.so, but on older systems this function simply does not exist. 
The idea is then to have a weak symbol "__pow_infinite" in my 
executable, whose implementation just calls the normal "pow": this way, 
on a recent system, the weak symbol is overridden and the libm.so 
version is used, whereas on an old system there's no other symbol than 
the weak one, that would be then picked up.

Cheers.

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

* Re: Weak symbol in an executable, overridable by shared libraries at runtime
  2013-11-02 21:44 Weak symbol in an executable, overridable by shared libraries at runtime Lorenzo Pistone
@ 2013-11-03  4:31 ` Alan Modra
  2013-11-03 12:05 ` H.J. Lu
  1 sibling, 0 replies; 3+ messages in thread
From: Alan Modra @ 2013-11-03  4:31 UTC (permalink / raw)
  To: Lorenzo Pistone; +Cc: binutils

On Sat, Nov 02, 2013 at 10:44:32PM +0100, Lorenzo Pistone wrote:
> Is there a way to define a weak symbol in an executable (not a
> .so!), that can be overridden by the runtime linking process by a
> symbol found in another library?

No, not without making changes to linker operation.  Make your
executable a shared library, then have a "stub" executable that just
consists of startup files, like so.  Also note the tweak needed to
ld.so symbol resolution via LD_DYNAMIC_WEAK, and you may need to use
-Wl,-no-as-needed for some of the linking stages.

$ cat > weakexe.c <<EOF
#include <stdio.h>

int foo (void) __attribute__ ((weak));

int
foo (void)
{
  return 1;
}

int
main (void)
{
  printf ("foo: %d\n", foo ());
  return 0;
}
EOF
$ cat > weakso.c <<EOF
int
foo (void)
{
  return 2;
}
EOF
$ gcc -O2 -fpic -c weakexe.c 
$ gcc -O2 -fpic -c weakso.c 
$ gcc -shared -o weak.so weakso.o
$ gcc -shared -o weakmain.so weakexe.o weak.so
$ gcc -o weak weakmain.so weak.so
$ LD_LIBRARY_PATH=. ./weak
foo: 1
$ LD_LIBRARY_PATH=. LD_DYNAMIC_WEAK=1 ./weak
foo: 2


-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: Weak symbol in an executable, overridable by shared libraries at runtime
  2013-11-02 21:44 Weak symbol in an executable, overridable by shared libraries at runtime Lorenzo Pistone
  2013-11-03  4:31 ` Alan Modra
@ 2013-11-03 12:05 ` H.J. Lu
  1 sibling, 0 replies; 3+ messages in thread
From: H.J. Lu @ 2013-11-03 12:05 UTC (permalink / raw)
  To: Lorenzo Pistone; +Cc: Binutils

On Sat, Nov 2, 2013 at 2:44 PM, Lorenzo Pistone <blaffablaffa@gmail.com> wrote:
> Hello,
> Is there a way to define a weak symbol in an executable (not a .so!), that
> can be overridden by the runtime linking process by a symbol found in
> another library?
> The aim is to provide a fallback implementation, within my executable, for
> systems where a specific function in a standard library cannot be found.
> More exactly, if I compile my program with -ffast-math, some calls to
> __pow_finite are generated: on recent systems this function is in libm.so,
> but on older systems this function simply does not exist. The idea is then
> to have a weak symbol "__pow_infinite" in my executable, whose
> implementation just calls the normal "pow": this way, on a recent system,
> the weak symbol is overridden and the libm.so version is used, whereas on an
> old system there's no other symbol than the weak one, that would be then
> picked up.
>
> Cheers.

The secondary symbol is what you are looking for. But it isn't supported
everywhere.

-- 
H.J.

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

end of thread, other threads:[~2013-11-03 12:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-02 21:44 Weak symbol in an executable, overridable by shared libraries at runtime Lorenzo Pistone
2013-11-03  4:31 ` Alan Modra
2013-11-03 12:05 ` H.J. Lu

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).