简体   繁体   English

gcc内联汇编器定义字符串

[英]gcc inline assembler define string

I try to define string out of .text section. 我尝试从.text部分定义字符串。 It's compiled with no error but when I disassemble with gdb , I get bad instruction. 它的编译没有错误,但是当我用gdb反汇编时,我得到了错误的指令。 Here , it's code in c : 在这里,它是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\";"
    );

} }

disassemble : 拆卸:

   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

How can I avoid this error? 如何避免此错误?

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

How can I avoid this error? 如何避免此错误?

If you don't want the string data appearing directly in the code ( .text ) section then you can use the .section directive to switch to the .data section, store the string, and then (optionally) switch back to .text again if you need to use the address of the data in more inline asm code: 如果您不希望字符串数据直接出现在代码( .text )节中,则可以使用.section指令切换到.data节,存储字符串,然后(可选)再次切换回.text如果您需要在更多内联asm代码中使用数据地址:

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

This is described well by the accepted answer to Defining Bytes in GCC Inline Assembly in Dev-C++(.ascii in AT&T syntax on Windows) 通过在Dev-C ++中在GCC内联汇编中定义字节的公认答案(在Windows上为AT&T语法的.ascii),可以很好地描述这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM