Gentoo Archives: gentoo-user

From: Michael Orlitzky <mjo@g.o>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] ...if linking a completly unused library...
Date: Sun, 02 Oct 2016 03:41:38
Message-Id: 5bd15efa-3525-673f-9db9-0f05cf3391ac@gentoo.org
In Reply to: [gentoo-user] ...if linking a completly unused library... by Meino.Cramer@gmx.de
1 On 10/01/2016 11:29 PM, Meino.Cramer@×××.de wrote:
2 > Hi,
3 >
4 > Suppose I would compile a program, which uses shared libraries and I
5 > specify an additional library, which will be completly unused by the
6 > code...will the resulting executable differ from an executable which
7 > is compiled without this library ?
8 >
9
10 It depends. From the "ld" man page:
11
12 --as-needed
13 --no-as-needed
14 This option affects ELF DT_NEEDED tags for dynamic libraries
15 mentioned on the command line after the --as-needed option.
16 Normally the linker will add a DT_NEEDED tag for each dynamic
17 library mentioned on the command line, regardless of whether the
18 library is actually needed or not. --as-needed causes a DT_NEEDED
19 tag to only be emitted for a library that at that point in the link
20 satisfies a non-weak undefined symbol reference from a regular
21 object file or, if the library is not found in the DT_NEEDED lists
22 of other needed libraries, a non-weak undefined symbol reference
23 from another needed dynamic library. Object files or libraries
24 appearing on the command line after the library in question do not
25 affect whether the library is seen as needed. This is similar to
26 the rules for extraction of object files from archives.
27 --no-as-needed restores the default behaviour.
28
29
30 Here's a program:
31
32 $ cat main.c
33 int main(int argc, char* argv[]) {
34 return 0;
35 }
36
37 And here's what happens with various C/LDFLAGS:
38
39 $ gcc main.c
40 $ ldd a.out
41 linux-vdso.so.1
42 libc.so.6 => /lib64/libc.so.6
43 /lib64/ld-linux-x86-64.so.2
44
45 $ gcc -lcrypto main.c
46 $ ldd a.out
47 linux-vdso.so.1
48 libcrypto.so.1.0.0 => /usr/lib64/libcrypto.so.1.0.0
49 libc.so.6 => /lib64/libc.so.6
50 libdl.so.2 => /lib64/libdl.so.2
51 libz.so.1 => /lib64/libz.so.1
52 /lib64/ld-linux-x86-64.so.2
53
54 $ gcc -Wl,--as-needed -lcrypto main.c
55 $ ldd a.out
56 linux-vdso.so.1
57 libc.so.6 => /lib64/libc.so.6
58 /lib64/ld-linux-x86-64.so.2
59
60 Note that the binaries in the first and third examples still differ,
61 although I don't know why off the top of my head.