This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Problem with static linking


Hello, I've been trying to write a program that links to static
libraries, and I've been having a lot of difficulties.  Was wondering
if someone can help me identify what's going wrong.

The codebase is large, but is new to linux.  It was originally
developed on windows and then ported to linux.  It makes heavy use of
C++, STL, and boost and we'd like to (if possible) link *everything*
statically.  This means libc, libgcc, libstdc++, boost, libpthread,
etc.

First things first however.  For the past few weeks we've had
everything compiling and linking just fine so we didn't suspect
anything.  We just knew that we had sporadic errors that didn't really
make sense but figured they were just obscure coding errors.  We
decided to run the program under valgrind and it generated about
60,000 errors.  Currently we are *assuming* that this is what's
causing all the other strange errors in our program, so we'd like to
fix them.  We spent a day or two playing around with various linker
settings / compiler and finally realized that removing the -static
command line options brings valgrind errors down to about 5, all of
which appear to be actual problems with our own code.

To get a baseline for comparison, we tried to make the simplest C++
program imaginable and compile it with -static.  Even that doesn't
work.  Technically it runs without segfaulting, but it still generates
19 valgrind errors, so we feel like the problem is still present even
in this simple case.  The source of said program is as follows:

//test.cpp
int main(int argc, char** argv) { return 0; }

That's it.   When I change the filename to test.c and compile it using
gcc instead of g++, I get the exact same problem if I use -static, and
the problem again goes away if I don't use -static.The output from g++
-v, as well as the output from valgrind when I run this program is as
follows (I've snipped valgrind errors down to only a few for the sake
of brevity.  The rest of the errors are somewhat similar):

% g++ -static test.cpp -v -o test
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc
--enable-mpfr --enable-targets=all --with-tune=generic
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu
--target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Debian 4.2.4-6)
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1plus -quiet -v -D_GNU_SOURCE
test.cpp -quiet -dumpbase test.cpp -mtune=generic -auxbase test
-version -o /tmp/ccjuzsji.s
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2
 /usr/include/c++/4.2/i486-linux-gnu
 /usr/include/c++/4.2/backward
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.2.4/include
 /usr/include
End of search list.
GNU C++ version 4.2.4 (Debian 4.2.4-6) (i486-linux-gnu)
 compiled by GNU C version 4.2.4 (Debian 4.2.4-6).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f71704aa7b35be12ec19d4deb7ce4f04
 as -V -Qy -o /tmp/ccmPieVt.o /tmp/ccjuzsji.s
GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU
Binutils for Debian) 2.18.0.20080103
 /usr/lib/gcc/i486-linux-gnu/4.2.4/collect2 -m elf_i386
--hash-style=both -static -o test
/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crt1.o
/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crti.o
/usr/lib/gcc/i486-linux-gnu/4.2.4/crtbeginT.o
-L/usr/lib/gcc/i486-linux-gnu/4.2.4
-L/usr/lib/gcc/i486-linux-gnu/4.2.4
-L/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib -L/lib/../lib
-L/usr/lib/../lib -L/usr/lib/gcc/i486-linux-gnu/4.2.4/../../..
/tmp/ccmPieVt.o -lstdc++ -lm --start-group -lgcc -lgcc_eh -lc
--end-group /usr/lib/gcc/i486-linux-gnu/4.2.4/crtend.o
/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crtn.o



% valgrind ./test
==16624== Memcheck, a memory error detector.
==16624== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==16624== Using LibVEX rev 1854, a library for dynamic binary translation.
==16624== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==16624== Using valgrind-3.3.1-Debian, a dynamic binary
instrumentation framework.
==16624== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==16624== For more details, rerun with: -v
==16624==
==16624== Conditional jump or move depends on uninitialised value(s)
==16624==    at 0x805BF16: __register_atfork (in /home/zturner/test)
==16624==    by 0x805663C: ptmalloc_init (in /home/zturner/test)
==16624==    by 0x8059AB5: malloc_hook_ini (in /home/zturner/test)
==16624==    by 0x8059957: malloc (in /home/zturner/test)
==16624==    by 0x807C96B: _dl_init_paths (in /home/zturner/test)
==16624==    by 0x805C75B: _dl_non_dynamic_init (in /home/zturner/test)
==16624==    by 0x805CFB5: __libc_init_first (in /home/zturner/test)
==16624==    by 0x8052AC0: (below main) (in /home/zturner/test)
==16624==
==16624== Conditional jump or move depends on uninitialised value(s)
==16624==    at 0x805BF93: __register_atfork (in /home/zturner/test)
==16624==    by 0x805663C: ptmalloc_init (in /home/zturner/test)
==16624==    by 0x8059AB5: malloc_hook_ini (in /home/zturner/test)
==16624==    by 0x8059957: malloc (in /home/zturner/test)
==16624==    by 0x807C96B: _dl_init_paths (in /home/zturner/test)
==16624==    by 0x805C75B: _dl_non_dynamic_init (in /home/zturner/test)
==16624==    by 0x805CFB5: __libc_init_first (in /home/zturner/test)
==16624==    by 0x8052AC0: (below main) (in /home/zturner/test)
==16624==
==16624== Conditional jump or move depends on uninitialised value(s)
==16624==    at 0x805998B: malloc (in /home/zturner/test)
==16624==    by 0x8059957: malloc (in /home/zturner/test)
==16624==    by 0x807C96B: _dl_init_paths (in /home/zturner/test)
==16624==    by 0x805C75B: _dl_non_dynamic_init (in /home/zturner/test)
==16624==    by 0x805CFB5: __libc_init_first (in /home/zturner/test)
==16624==    by 0x8052AC0: (below main) (in /home/zturner/test)
==16624==
==16624== Conditional jump or move depends on uninitialised value(s)
==16624==    at 0x80599C0: malloc (in /home/zturner/test)
==16624==    by 0x8059957: malloc (in /home/zturner/test)
==16624==    by 0x807C96B: _dl_init_paths (in /home/zturner/test)
==16624==    by 0x805C75B: _dl_non_dynamic_init (in /home/zturner/test)
==16624==    by 0x805CFB5: __libc_init_first (in /home/zturner/test)
==16624==    by 0x8052AC0: (below main) (in /home/zturner/test)



Note for the record that I've tried -static-libgcc in conjuction with
-static, but no matter what I do I cannot compile even the simplest
program with static linkage without receiving tons of errors like
this.  Note that in this simple program I'm not even using libstdc++
at all or including any header files of any kind.  What's worse, this
same problem happens across multiple versions of GCC (at the very
least it happens on 4.2.4 and 4.4.0) and multiple linux distributions
/ kernels.

So I guess I have three questions.  1) Is this actually a problem or
are these errors spurious?  2) Why do they disappear when I delete the
-static option?  and 3) Is there a way to fix them?  I've even gone so
far as to manually run collect2 specifying my own hand edited command
line, but nothing I've tried there has worked either.

I feel really insecure just writing these off as spurious warnings
just for the simple fact that our program behaves in unexplainable
ways.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]