https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63318

            Bug ID: 63318
           Summary: Hello World C program using inline assembly to invoke
                    write(2) on amd64 Linux fails to print Hello World
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ryao at gentoo dot org
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

Created attachment 33522
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33522&action=edit
A hello world program from stack overflow

I tried building a Hello World program from stack overflow that used asm
volatile to invoke write(2):

https://stackoverflow.com/questions/9506353/how-to-invoke-a-system-call-via-sysenter-in-inline-assembly-x86-amd64-linux/9508738#9508738

Unfortunately, it fails to print Hello World when compiled with GCC, but prints
Hello World fine when compiled with Clang. This is because GCC fails to emit a
string, while Clang does not.

Here is the assembly output of GCC:

$ gcc -S -o - syscall.c
        .file   "syscall.c"
        .text
        .globl  main
        .type   main, @function
main:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        subq    $48, %rsp
        movq    %fs:40, %rax
        movq    %rax, -8(%rbp)
        xorl    %eax, %eax
        movabsq $8022916924116329800, %rax
        movq    %rax, -32(%rbp)
        movl    $560229490, -24(%rbp)
        movw    $10, -20(%rbp)
        movq    $14, -48(%rbp)
        leaq    -32(%rbp), %rax
#APP
# 8 "syscall.c" 1
        movl $1, %eax
        movl $1, %edi
        movq %rax, %rsi
        movl -48(%rbp), %edx
        syscall
# 0 "" 2
#NO_APP
        movq    %rax, -40(%rbp)
        movl    $0, %eax
        movq    -8(%rbp), %rdx
        xorq    %fs:40, %rdx
        je      .L3
        call    __stack_chk_fail
.L3:
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   main, .-main
        .ident  "GCC: (Gentoo 4.8.3 p1.1, pie-0.5.9) 4.8.3"
        .section        .note.GNU-stack,"",@progbits

And here is the assembly output of Clang:

$ clang -S -o - syscall.c
        .file   "syscall.c"
        .text
        .globl  main
        .align  16, 0x90
        .type   main,@function
main:                                   # @main
        .cfi_startproc
# BB#0:
        pushq   %rbp
.Ltmp2:
        .cfi_def_cfa_offset 16
.Ltmp3:
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
.Ltmp4:
        .cfi_def_cfa_register %rbp
        movl    $0, %eax
        movl    $0, -4(%rbp)
        movq    $14, -16(%rbp)
        movl    %eax, -28(%rbp)         # 4-byte Spill
        #APP
        movl $1, %eax
        movl $1, %edi
        movq $main.hello, %rsi
        movl $14, %edx
        syscall
        #NO_APP
        movq    %rax, -24(%rbp)
        movl    -28(%rbp), %eax         # 4-byte Reload
        popq    %rbp
        ret
.Ltmp5:
        .size   main, .Ltmp5-main
        .cfi_endproc

        .type   main.hello,@object      # @main.hello
        .section        .rodata,"a",@progbits
main.hello:
        .asciz   "Hello World!\n"
        .size   main.hello, 14


        .section        ".note.GNU-stack","",@progbits

Here is information on my compiler versions:

$ clang -v
clang version 3.3 (tags/RELEASE_33/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.8.3/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.8.3/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-4.8.3/work/gcc-4.8.3/configure
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr
--bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.8.3
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.3
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.3/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.8.3/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.8.3/python
--enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt
--disable-werror --with-system-zlib --enable-nls --without-included-gettext
--enable-checking=release --with-bugurl=https://bugs.gentoo.org/
--with-pkgversion='Gentoo 4.8.3 p1.1, pie-0.5.9' --enable-libstdcxx-time
--enable-shared --enable-threads=posix --enable-__cxa_atexit
--enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64
--disable-altivec --disable-fixed-point --enable-targets=all --disable-libgcj
--enable-libgomp --disable-libmudflap --disable-libssp --enable-lto
--without-cloog
Thread model: posix
gcc version 4.8.3 (Gentoo 4.8.3 p1.1, pie-0.5.9)

Reply via email to