Search code examples
cgccstatic-librariesvalgrindstatic-linking

Confused about gcc -static option or its behavior in virtual machine


I write a naive c program try.c

#include <stdlib.h>
int main() {return 0;}

Then I try to compile and run it with the shell script below

CFLAGS='-Wpedantic -Wall -Wextra -Werror -std=c89'
gcc -o try  ${CFLAGS}  try.c -static
valgrind ./try -v --track-origins=yes

Then the output is quite confusing:

==16641== Memcheck, a memory error detector
==16641== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==16641== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==16641== Command: ./try -v --track-origins=yes
==16641== 
==16641== Conditional jump or move depends on uninitialised value(s)
==16641==    at 0x419349: _int_free (in  /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x41D296: free (in    /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x46CCAE: fillin_rpath (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x46D57A: _dl_init_paths (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x44282B: _dl_non_dynamic_init (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x443557: __libc_init_first (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x400B77: (below main) (in /home/su/ca/hw/1_try/try_static/trytrytry/try)

However, if I remove the option "-static", everything goes well. I tried it on ubuntu 16.04 (virtual machine ) and ubuntu 14.04 (virtual machine ) Maybe it has something to do with virtual machine ?


Solution

  • This error is part of a call stack that Valgrind probably suppresses by default. The suppression is specific to the shared library that the call is part of (that is, libc) -- so building your program as a static executable prevents Valgrind from recognizing that it should ignore this error.

    In any event, this error is internal to libc initialization code, so you should ignore it.