You are not logged in.

#1 2015-03-09 02:34:25

vapourismo
Member
Registered: 2014-10-17
Posts: 6

Linker strips needed libraries due to --as-needed flag

Hello,

to be honest this problem is more related to the dynamic linker and a little bit to the ABS. Nevertheless I am asking you guys, how to deal with the situation I am in. Let me explain ...

My library builds using a Makefile which adds several linker flags to the LDFLAGS environment variable, like so:

LDFLAGS += -flto -pthread -lm -levent -levent_pthreads -shared -Wl,-soname,$(SONAME)

It compiles fine like that, because LDFLAGS is ordinarily not supplied - but when building packages from the AUR, the build system supplies it, which results in something like that:

clang -Wl,-O1,--sort-common,--as-needed,-z,relro -flto -pthread -lm -levent -levent_pthreads -shared -Wl,-soname,libknxclient.so.0 -odist/libknxclient.so.0.0 <list of object files ...>

For some reason --as-needed makes the linker strip the dynamic linkage for libevent and libevent_pthreads:

knxclient @master λ readelf -s /usr/lib/libknxclient.so | grep "UND event_"
     8: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND event_base_new
    11: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND event_free
    27: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND event_del
    45: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND event_new
    50: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND event_add
    52: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND event_base_free
    94: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND event_base_dispatch
knxclient @master λ ldd /usr/lib/libknxclient.so
	linux-vdso.so.1 (0x00007fffaa2ca000)
	libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f4b80648000)
	libc.so.6 => /usr/lib/libc.so.6 (0x00007f4b802a0000)
	/usr/lib64/ld-linux-x86-64.so.2 (0x00007f4b80a98000)

The big question is, how do I prevent that? Is it a bug? I've tried adding --no-as-needed, but that does not negate the previous mentioning of --as-needed.
As state in ld's manpage, libraries that are added with -l before the --as-needed flag are exempt. But why does it remove the linkage when I supply -l after --as-needed?

In case you are interested, the entire Makefile lies here.

The result I am looking for:

knxclient @master λ ldd dist/libknxclient.so.0.0 
	linux-vdso.so.1 (0x00007fff606da000)
	libm.so.6 => /usr/lib/libm.so.6 (0x00007fed539a0000)
	libevent-2.0.so.5 => /usr/lib/libevent-2.0.so.5 (0x00007fed53750000)
	libevent_pthreads-2.0.so.5 => /usr/lib/libevent_pthreads-2.0.so.5 (0x00007fed53548000)
	libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007fed53328000)
	libc.so.6 => /usr/lib/libc.so.6 (0x00007fed52f80000)
	/usr/lib64/ld-linux-x86-64.so.2 (0x00007fed53ed8000)

I'd appreciate any advice you can give me.

I have tried this with GNU ld 2.25.0 and gold 1.11.

Last edited by vapourismo (2015-03-09 02:47:45)

Offline

#2 2015-03-09 03:26:44

progandy
Member
Registered: 2012-05-17
Posts: 5,321

Re: Linker strips needed libraries due to --as-needed flag

Hello, Don't forget the correct order:

LDFLAGS         += -shared -Wl,-soname,$(SONAME) -flto -pthread
LDLIBS            = -lm -levent -levent_pthreads
...
$(CC) $(LDFLAGS) $(SOURCEOBJS) $(LDLIBS) -o$@

Last edited by progandy (2015-03-09 03:29:43)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#3 2015-03-09 03:36:51

vapourismo
Member
Registered: 2014-10-17
Posts: 6

Re: Linker strips needed libraries due to --as-needed flag

That works when I put LDLIBS before LDFLAGS, thanks.

I am still curious why the linker strips the dynamic linkage? The symbols from my object files which are marked as unknown match the symbols of the given libraries. It's a mystery to me - but lesson learned.

Offline

#4 2015-03-09 03:50:22

progandy
Member
Registered: 2012-05-17
Posts: 5,321

Re: Linker strips needed libraries due to --as-needed flag

You don't have to put LDLIBS before LDFLAGS. I already gave you the correct order. First the flags, then your source files which need the libraries, then the libraries.
http://wiki.gentoo.org/wiki/Project:Qua … king_order
The gentoo article mentions LIBS/LDADD/LIBADD as possible variables names, while the make manual describes a LDLIBS variable.
https://www.gnu.org/software/make/manua … ables.html
http://www.roland-illig.de/articles/article-1.html

Last edited by progandy (2015-03-09 03:51:34)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#5 2015-03-09 03:57:09

vapourismo
Member
Registered: 2014-10-17
Posts: 6

Re: Linker strips needed libraries due to --as-needed flag

Ah I see. I assumed the solution was to link them naively by putting the shared targets before --as-needed.

Offline

Board footer

Powered by FluxBB