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

标签 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

我怎样才能避免这个错误?

最佳答案

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

How can I avoid this error?

如果您不希望字符串数据直接出现在代码 (.text) 部分,那么您可以使用 .section指令切换到 .data 部分,存储字符串,然后(可选)如果需要在 more 中使用数据的地址,再次切换回 .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) 的公认答案很好地描述了这一点

关于c - gcc 内联汇编程序定义字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20730432/

相关文章:

c - 遍历客户端程序接收到的缓冲区

javascript - iMacros - 删除字符串中的最后一个字符

c - 字符串文字的地址长度

c - 如何将结构 B 中的节点插入到结构 A(二叉树)

c - 是否可以使用格式说明符作为函数的参数

c - 如何为我的 Gtk+ 应用程序设置默认主题?

c - 用C打开文件时出现段错误

python - 如何在一行中写多个字符串?

c++ - GCC 如何处理宏中的引号?

c++ - 构建时的静态变量解析