Subject: Global variable name which equals to func name causes coredump(gcc).
To: None <tech-userlevel@netbsd.org>
From: =?GB2312?B?seXJ3MDX?= <bianshaolei@gmail.com>
List: tech-userlevel
Date: 09/27/2007 18:04:05
Hi, all.
Please see a simple program as list:
cat a.c:
 #include <stdio.h>
    int read;
    int main()
    {
        printf("%p\n", &read);
        char buf[1024];
        FILE * fp = fopen("a.c", "r");
        int len = fread(buf, 1, 1024, fp);
        buf[len] = 0;
        printf("%s\n", buf);
        return 0;
    }
======
gcc a.c -ggdb -Wall #There was no warning.
======
./a.out
======
0x80499f8
(coredump)
======
gdb a.out -c a.out.core
#0  0x080499f8 in read ()
(gdb) where
#0  0x080499f8 in read ()
#1  0xbbbc6435 in __sread () from /usr/lib/libc.so.12
#2  0xbbbafc39 in __srefill () from /usr/lib/libc.so.12
#3  0xbbbaa92b in fread () from /usr/lib/libc.so.12
#4  0x0804879e in main () at a.c:7
======
Obviously fread called the wrong func address of read.



See another simpler program:
cat b.c
======
int mlock;
int main()
{
}
=====
gcc b.c -lpthread -o b.out
=====
./b.out
(coredump)

b.out did not even run to func main before it coredumped. I guess
libpthread.so need to run func mlock when loaded (maybe in _init), but
in my program mlock is just an integer -- not a func.

=====
=====
=====
I have tested two program above in FreeBSD, OpenBSD, Mingw+gcc4.2.1
and ubuntu 7.04 with gcc 4. None of others had the same problem.

I met the second problem in 2006. But at that time I was compling a
big project (RGTK) and did not know where the problem was (At that
time I used NetBSD amd64 3.0).

Is this a bug, or something I miss to understand it?

Thanks for any comment.