From: Michael Kenney (kenney@milton.u.washington.edu)
Date: 07/24/92


From: kenney@milton.u.washington.edu (Michael Kenney)
Subject: Strange gcc problem/bug
Date: 24 Jul 1992 14:50:38 GMT


Fellow Linux users,

I think I have run across a bug in gcc v2.2.2.

Consider the following bit of code (the getuname function is from the
getty_ps package):

=======================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
/*
** getuname() - retrieve the system's node name
**
** Returns pointer to name or a zero-length string if not found.
*/
char *
getuname()
{
        struct utsname uts;
        static char name[80];
        int r;
        name[0] = '\0';
        if (uname(&uts) != -1)
                (void) strcpy(name, uts.nodename);
        return(name);
}

main(ac, av)
unsigned ac;
char **av;
{
    printf("hostname = %s\n",getuname());
}
=========================================================================

When I compile the above program, I get the following output:

    /lib/libc2.2.2

If I use static linking, I get no output at all. What really makes things
interesting is; if I make "uts" static in getuname, everything works fine.

Here's the assembler output for getuname (uts local):

==========================================================================
        .file "testname.c"
gcc2_compiled.:
.lcomm _name.6,80
.text
        .align 2
.globl _getuname
_getuname:
        pushl %ebp
        movl %esp,%ebp
        subl $276,%esp
        movb $0,_name.6
        leal -272(%ebp),%eax
        pushl %eax
        call _uname
        addl $4,%esp
        movl %eax,%eax
        cmpl $-1,%eax
        je L7
        leal -207(%ebp),%eax
        pushl %eax
        pushl $_name.6
        call _strcpy
        addl $8,%esp
L7:
        movl $_name.6,%eax
        jmp L6
        .align 2,0x90
L6:
        leave
        ret
===========================================================================

Now here's the case for static uts:

============================================================================
        .file "testname.c"
gcc2_compiled.:
.lcomm _uts.6,272
.lcomm _name.7,80
.text
        .align 2
.globl _getuname
_getuname:
        pushl %ebp
        movl %esp,%ebp
        subl $4,%esp
        movb $0,_name.7
        pushl $_uts.6
        call _uname
        addl $4,%esp
        movl %eax,%eax
        cmpl $-1,%eax
        je L7
        pushl $_uts.6+65
        pushl $_name.7
        call _strcpy
        addl $8,%esp
L7:
        movl $_name.7,%eax
        jmp L6
        .align 2,0x90
L6:
        leave
        ret
============================================================================

Anybody know what's going on here? I'm not that familiar with Minix
assembler, does "leave" clean up the stack properly before returning?
I was slogging through this fairly late last night so there may be something
obvious that I am missing :-).

Mike

==========
Mike Kenney
UW Applied Physics Lab
mikek@apl.washington.edu