gcc内联汇编程序定义字符串

时间:2013-12-22 14:27:38

标签: c string gcc assembly inline-assembly

我尝试从.text部分定义字符串。它编译时没有错误但是当我用gdb反汇编时,我得到了错误的指令。 这是c:

中的代码
void main(){
    __asm__(
            "jmp .+0x35;"
            "pop %rsi;"
            "mov %rsi, -0x10(%rsi);"
            "movq $0x0,-0x8(%rsi);"
            "mov -0x10(%rsi), %rax;"
            "lea -0x10(%rsi), %rcx;"
            "mov $0x0, %edx;"
            "mov %rcx, %rsi;"
            "mov %rax, %rdi;"
            "mov $0x3b,%eax;"
            "syscall;"
            "mov $0x0,%ebx;"
            "mov $0x1,%eax;"
            "syscall;"
            "call .-0x33;"
            ".string \"/bin/bash\";"
    );

}

反汇编:

   0x0000000000400494 <+0>:     push   %rbp
   0x0000000000400495 <+1>:     mov    %rsp,%rbp
   0x0000000000400498 <+4>:     jmp    0x4004cd <main+57>
   0x000000000040049a <+6>:     pop    %rsi
   0x000000000040049b <+7>:     mov    %rsi,-0x10(%rsi)
   0x000000000040049f <+11>:    movq   $0x0,-0x8(%rsi)
   0x00000000004004a7 <+19>:    mov    -0x10(%rsi),%rax
   0x00000000004004ab <+23>:    lea    -0x10(%rsi),%rcx
   0x00000000004004af <+27>:    mov    $0x0,%edx
   0x00000000004004b4 <+32>:    mov    %rcx,%rsi
   0x00000000004004b7 <+35>:    mov    %rax,%rdi
   0x00000000004004ba <+38>:    mov    $0x3b,%eax
   0x00000000004004bf <+43>:    syscall
   0x00000000004004c1 <+45>:    mov    $0x0,%ebx
   0x00000000004004c6 <+50>:    mov    $0x1,%eax
   0x00000000004004cb <+55>:    syscall
   0x00000000004004cd <+57>:    callq  0x40049a <main+6>
   0x00000000004004d2 <+62>:    (bad) ( **here is define string** )
   0x00000000004004d3 <+63>:    (bad)
   0x00000000004004d4 <+64>:    imul   $0x68736162,0x2f(%rsi),%ebp
   0x00000000004004db <+71>:    add    %cl,%cl
   0x00000000004004dd <+73>:    retq

如何避免此错误?

1 个答案:

答案 0 :(得分:1)

   0x00000000004004cd <+57>:    callq  0x40049a <main+6>
   0x00000000004004d2 <+62>:    (bad) ( **here is define string** )
   0x00000000004004d3 <+63>:    (bad)
  

如何避免此错误?

如果您不希望字符串数据直接出现在代码(.text)部分中,那么您可以使用.section指令切换到.data部分,存储字符串,然后(可选)再次切换回.text,如果您需要使用更内联的asm代码中的数据地址:

        "call .-0x33;"
        ".section .data;"
        "1: .string \"/bin/bash\";"

Defining Bytes in GCC Inline Assembly in Dev-C++(.ascii in AT&T syntax on Windows)

的接受答案很好地描述了这一点