> Hi,
>
> The following program segfaults when compiled with gcc
> but runs fine when compiled with g++ or icc (the intel C compiler)
>
> #include <stdio.h>
> struct Hello {
>         char world[20];
> };
> struct Hello s(){
>         struct Hello r;
>         r.world[0]='H';
>         r.world[1]='\0';
>         return r;
> }
>
> int main(){
>         printf("%s\n",s().world);
> }
>
> Assigning s() to a variable and then using the variable avoids the
> segfault.

compiles and works fine with GCC 4.3.2 on Solaris 8/9/10 sun4m/sun4u/i386

$ /opt/csw/gcc4/bin/gcc -v -o foo.o -c foo.c
Using built-in specs.
Target: sparc-sun-solaris2.8
Configured with: ../gcc-4.3.2/configure --prefix=/opt/csw/gcc4
--with-local-prefix=/opt/csw --with-as=/usr/ccs/bin/as --without-gnu-ld
--with-ld=/usr/ccs/bin/ld --with-cpu=v7 --enable-threads=posix
--enable-nls --enable-shared --enable-languages=c,c++,fortran,objc
--with-gmp=/opt/csw --with-mpfr=/opt/csw --enable-multilib
--with-included-gettext --with-libiconv-prefix=/opt/csw --with-x
--enable-java-awt=xlib --with-system-zlib --enable-bootstrap
Thread model: posix
gcc version 4.3.2 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'foo.o' '-c' '-mcpu=v7'
 /opt/csw/gcc4/libexec/gcc/sparc-sun-solaris2.8/4.3.2/cc1 -quiet -v foo.c
-quiet -dumpbase foo.c -mcpu=v7 -auxbase-strip foo.o -version -o
/var/tmp//ccAHrz2q.s
ignoring nonexistent directory
"/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/../../../../sparc-sun-solaris2.8/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/csw/include
 /opt/csw/gcc4/include
 /opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/include
 /opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/include-fixed
 /usr/include
End of search list.
GNU C (GCC) version 4.3.2 (sparc-sun-solaris2.8)
        compiled by GNU C version 4.3.2, GMP version 4.2.2, MPFR version
2.3.1.
warning: GMP header version 4.2.2 differs from library version 4.2.4.
GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32768
Compiler executable checksum: 1ac791ab3c2b7cc8775dc74d45095fef
COLLECT_GCC_OPTIONS='-v' '-o' 'foo.o' '-c' '-mcpu=v7'
 /usr/ccs/bin/as -V -Qy -s -xarch=v8 -o foo.o /var/tmp//ccAHrz2q.s
/usr/ccs/bin/as: Sun WorkShop 6 2003/12/18 Compiler Common 6.0 Patch
114802-02
COMPILER_PATH=/opt/csw/gcc4/libexec/gcc/sparc-sun-solaris2.8/4.3.2/:/opt/csw/gcc4/libexec/gcc/sparc-sun-solaris2.8/4.3.2/:/opt/csw/gcc4/libexec/gcc/sparc-sun-solaris2.8/:/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/:/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/:/usr/ccs/bin/
LIBRARY_PATH=/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/:/usr/ccs/lib/:/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'foo.o' '-c' '-mcpu=v7'

$ /opt/csw/gcc4/bin/gcc -v -o foo.s -S -c foo.c
Using built-in specs.
Target: sparc-sun-solaris2.8
Configured with: ../gcc-4.3.2/configure --prefix=/opt/csw/gcc4
--with-local-prefix=/opt/csw --with-as=/usr/ccs/bin/as --without-gnu-ld
--with-ld=/usr/ccs/bin/ld --with-cpu=v7 --enable-threads=posix
--enable-nls --enable-shared --enable-languages=c,c++,fortran,objc
--with-gmp=/opt/csw --with-mpfr=/opt/csw --enable-multilib
--with-included-gettext --with-libiconv-prefix=/opt/csw --with-x
--enable-java-awt=xlib --with-system-zlib --enable-bootstrap
Thread model: posix
gcc version 4.3.2 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'foo.s' '-S' '-c' '-mcpu=v7'
 /opt/csw/gcc4/libexec/gcc/sparc-sun-solaris2.8/4.3.2/cc1 -quiet -v foo.c
-quiet -dumpbase foo.c -mcpu=v7 -auxbase-strip foo.s -version -o foo.s
ignoring nonexistent directory
"/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/../../../../sparc-sun-solaris2.8/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/csw/include
 /opt/csw/gcc4/include
 /opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/include
 /opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/include-fixed
 /usr/include
End of search list.
GNU C (GCC) version 4.3.2 (sparc-sun-solaris2.8)
        compiled by GNU C version 4.3.2, GMP version 4.2.2, MPFR version
2.3.1.
warning: GMP header version 4.2.2 differs from library version 4.2.4.
GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32768
Compiler executable checksum: 1ac791ab3c2b7cc8775dc74d45095fef
COMPILER_PATH=/opt/csw/gcc4/libexec/gcc/sparc-sun-solaris2.8/4.3.2/:/opt/csw/gcc4/libexec/gcc/sparc-sun-solaris2.8/4.3.2/:/opt/csw/gcc4/libexec/gcc/sparc-sun-solaris2.8/:/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/:/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/:/usr/ccs/bin/
LIBRARY_PATH=/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/:/usr/ccs/lib/:/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'foo.s' '-S' '-c' '-mcpu=v7'

$ cat foo.s
        .file   "foo.c"
        .section        ".text"
        .align 4
        .global s
        .type   s, #function
        .proc   010
s:
        save    %sp, -136, %sp
        ld      [%fp+64], %l0
        mov     72, %g1
        stb     %g1, [%fp-36]
        stb     %g0, [%fp-35]
        mov     %l0, %g1
        add     %fp, -36, %g2
        mov     20, %g3
        mov     %g1, %o0
        mov     %g2, %o1
        mov     %g3, %o2
        call    memcpy, 0
         nop
        mov     %l0, %i0
        restore
        jmp     %o7+12
         nop
        .size   s, .-s
        .section        ".rodata"
        .align 8
.LLC0:
        .asciz  "%s\n"
        .section        ".text"
        .align 4
        .global main
        .type   main, #function
        .proc   04
main:
        save    %sp, -168, %sp
        add     %fp, -36, %g1
        st      %g1, [%sp+64]
        call    s, 0
         nop
        unimp   20
        ld      [%fp-36], %g1
        st      %g1, [%fp-64]
        ld      [%fp-32], %g1
        st      %g1, [%fp-60]
        ld      [%fp-28], %g1
        st      %g1, [%fp-56]
        ld      [%fp-24], %g1
        st      %g1, [%fp-52]
        ld      [%fp-20], %g1
        st      %g1, [%fp-48]
        add     %fp, -64, %g2
        sethi   %hi(.LLC0), %g1
        or      %g1, %lo(.LLC0), %o0
        mov     %g2, %o1
        call    printf, 0
         nop
        restore
        jmp     %o7+8
         nop
        .size   main, .-main
        .ident  "GCC: (GNU) 4.3.2"
$
$
$ /opt/csw/gcc4/bin/gcc -o foo foo.s
$ ./foo
H

works right ?

Dennis Clarke
http://www.blastwave.org/

Reply via email to