Bug 7516 - Ambiguous driver behaviour with "-shared -static" cmd line options combination
Summary: Ambiguous driver behaviour with "-shared -static" cmd line options combination
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: driver (show other bugs)
Version: 3.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-08-06 23:56 UTC by grigory
Modified: 2023-03-20 17:15 UTC (History)
2 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2005-05-22 05:53:20


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description grigory 2002-08-06 23:56:01 UTC
1. Intro
--------
GCC manual defines the following cmd line options meaning:
"-static
On systems that support dynamic linking, this prevents linking with the shared libraries.

-shared 
Produce a shared object..."


2. The issue
------------
So, if we wish to produce a shared object linked against static libraries, we have to specify "-shared -static" driver options. For instance:
	$ gcc foo.c -shared -o libfo.so -static

But if we check the library built we will see that it is linked against some shared libs.
	$ ldd ./libfoo.so
	/lib/libNoVersion.so.1 => /lib/libNoVersion.so.1 (0x40003000)
	libc.so.6 => /lib/i686/libc.so.6 (0x40015000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x2aaaa000)

*** This is not what expected! ***


3. Driver work
--------------
Let's check what the GCC compiler driver is doing. We use --verbose to see details.

$ gcc foo.c -shared -o libfo.so -static --verbose
...
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1/collect2 -m elf_i386 -shared -o libfoo.so /usr/lib/crti.o /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1/crtbeginT.o -L/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1 -L/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1/../../.. /home/grigory/tmp/cc6Ymc3Z.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1/crtendS.o /usr/lib/crtn.o

We can see a kind of ambiguity here:
(a) linker command line does not contain '-static' option requested, but at the same time, (b) the set of libraries "-lgcc -lgcc_eh -lc -lgcc -lgcc_eh" is specific for static build. I.e. we can see the same set while building executable linked against static libraries.

4. Question
-----------
So, what is going on here:
  - Driver just lost '-static' in the linker command line?
  - Or this is intentional restriction?

Release:
3.1.1

Environment:
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ./configure --enable-threads
Comment 1 Wolfgang Bangerth 2003-03-14 01:48:55 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed. This is a weird case, still existing in present
    mainline.
    W.
Comment 2 Andrew Pinski 2004-11-20 01:28:28 UTC
I and some others think this should be rejected and the documentation changed.
Comment 3 Pawel Sikora 2006-05-04 13:21:26 UTC
(In reply to comment #0)

>         $ ldd ./libfoo.so
>         /lib/libNoVersion.so.1 => /lib/libNoVersion.so.1 (0x40003000)
>         libc.so.6 => /lib/i686/libc.so.6 (0x40015000)
>         /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x2aaaa000)
> 
> *** This is not what expected! ***

i'm using a little workaround:

gcc libfoo.c -o libfoo.so -shared -fPIC \
             -nodefaultlibs -Wl,-Bstatic -lc -lgcc -lgcc_eh -lc

$ ldd libfoo.so
        statically linked

$ file libfoo.so
libfoo.so: ELF 64-bit LSB shared object, AMD x86-64,
           version 1 (SYSV), not stripped