Search code examples
linuxgcccompiler-errorsx86inline-assembly

GCC assembly language compiler errors


Consider:

void main()
{
    __asm__
        (
         "jmp    0x2a
         popl    %esi
         movl    %esi, 0x8(%esi)
         movb    $0x0, 0x7(%esi)
         movl    $0x0, 0xc(%esi)
         movl    $0xb, %eax
         movl    %esi, %ebx
         leal    0x8(%esi), %ecx
         leal    0xc(%esi), %edx
         int     $0x80
         movl    $0x1, %eax
         movl    $0x0, %ebx
         int     $0x80
         call   -0x2f
         .string \"/bin/sh\"
         ");
}

I followed the shellcode tutorial, but when I compiled it with gcc shellcodeasm.c, I got the following error:

shellcodeasm.c: In function ‘main’:
shellcodeasm.c:5:4: warning: missing terminating " character [enabled by default]
    "jmp 0x2a
    ^
shellcodeasm.c:5:4: error: missing terminating " character
shellcodeasm.c:6:4: error: expected string literal before ‘popl’
    popl %esi
    ^
shellcodeasm.c:19:4: error: stray ‘\’ in program
    .string \"/bin/sh\"
    ^
shellcodeasm.c:19:13: warning: missing terminating " character [enabled by default]
    .string \"/bin/sh\"
             ^
shellcodeasm.c:19:4: error: missing terminating " character
    .string \"/bin/sh\"
    ^
shellcodeasm.c:20:4: warning: missing terminating " character [enabled by default]
    ");
    ^
shellcodeasm.c:20:4: error: missing terminating " character

Solution

  • Although I have identified this as a duplicate of the other question, you can resolve this issue by placing a quote at the beginning and end of each line of inline assembly and embed a newline character at the end of each string:

    void main()
    {
        __asm__
            (
             "jmp   0x2a\n"
             "popl   %esi\n"
             "movl   %esi, 0x8(%esi)\n"
             "movb   $0x0, 0x7(%esi)\n"
             "movl   $0x0, 0xc(%esi)\n"
             "movl   $0xb, %eax\n"
             "movl   %esi, %ebx\n"
             "leal   0x8(%esi), %ecx\n"
             "leal   0xc(%esi), %edx\n"
             "int    $0x80\n"
             "movl   $0x1, %eax\n"
             "movl   $0x0, %ebx\n"
             "int    $0x80\n"
             "call   -0x2f\n"
             ".string \"/bin/sh\"\n"
             );
    }