$ gcov -v gcov (GCC) 9.2.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --enable-shared --enable-threads=posix --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp --enable-cet=auto gdc_include_dir=/usr/include/dlang/gdc Thread model: posix gcc version 9.2.0 (GCC) #### code for small.c #### $ cat small.c void free(void *ptr) { return; } void *foo() { return 0; } void main() { void *p = foo(); free(p); } #### coverage generated by gcov #### $ gcc -O0 --coverage small.c; ./a.out; gcov small.c; cat small.c.gcov File 'small.c' Lines executed:100.00% of 8 Creating 'small.c.gcov' -: 0:Source:small.c -: 0:Graph:small.gcno -: 0:Data:small.gcda -: 0:Runs:1 2: 1:void free(void *ptr) -: 2:{ 2: 3: return; -: 4:} -: 5: 1: 6:void *foo() -: 7:{ 1: 8: return 0; -: 9:} -: 10: 1: 11:void main() -: 12:{ 1: 13: void *p = foo(); 1: 14: free(p); 1: 15:} ##################### Function "free" is called only once in Line #14. However, we can find that Line #1 and #3 are wrongly marked as executed twice.
But it's actually called twice. The second invocation is from libgcov: #0 free (ptr=0x606280) at t.c:3 #1 0x00002aaaaad41d8e in _IO_new_fclose (fp=0x606280) at iofclose.c:84 #2 0x000000000040139e in __gcov_close () at /space/rguenther/src/svn/gcc-9-branch/libgcc/../gcc/gcov-io.c:212 #3 0x0000000000401b59 in dump_one_gcov (run_max=1, run_counted=0, gf=<synthetic pointer>, gi_ptr=<optimized out>) at /space/rguenther/src/svn/gcc-9-branch/libgcc/libgcov-driver.c:545 #4 gcov_do_dump (list=<optimized out>, run_counted=0) at /space/rguenther/src/svn/gcc-9-branch/libgcc/libgcov-driver.c:584 #5 0x00000000004025e0 in __gcov_dump_one (root=0x6042c0 <__gcov_root>) at /space/rguenther/src/svn/gcc-9-branch/libgcc/libgcov-driver.c:607 #6 __gcov_dump_one (root=0x6042c0 <__gcov_root>) at /space/rguenther/src/svn/gcc-9-branch/libgcc/libgcov-driver.c:602 #7 __gcov_exit () at /space/rguenther/src/svn/gcc-9-branch/libgcc/libgcov-driver.c:623 #8 0x0000000000400e0e in _GLOBAL__sub_D_00100_1_free () at t.c:15 #9 0x00002aaaaaabb2b3 in _dl_fini () at dl-fini.c:235 #10 0x00002aaaaad0bd78 in __run_exit_handlers (status=1, listp=0x2aaaab0876f8 <__exit_funcs>, run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at exit.c:83 #11 0x00002aaaaad0bdca in __GI_exit (status=<optimized out>) at exit.c:105 #12 0x00002aaaaacf3f91 in __libc_start_main (main=0x400dac <main>, argc=1, argv=0x7fffffffdd98, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffdd88) at ../csu/libc-start.c:342 #13 0x0000000000400cca in _start () at ../sysdeps/x86_64/start.S:120 your 'free' function interposes the one from libc.