gcc - 内联汇编错误,阻止 gcc 编译尝试

标签 gcc assembly x86 x86-64 inline-assembly

你好,SO。

我尝试使用 gcc 编译一些代码,但我的尝试遭到了挫败。任何更精通这个主题的人都可以帮助我,也许我缺少一些东西。

我正在 Linux Kitchen 2.6.28-15-generic#49-Ubuntu SMP Tue Aug 18 19:25:34 UTC 2009 x86_64 GNU/Linux 上编译此代码。

int
main(void)
{
    __asm__(
            "xorq %rdx,%rdx"
            "movq $0x68732f6e69622fff, %rdx"
            "shr $0x8, %rbx"
            "push %rbx"
            "movq %rsp,%rdi"
            "xorq %rax,%rax"
            "pushq %rax"
            "pushq %rdi"
            "movq %rsp,%rsi"
            "mov $0x3b, %al"
            "syscall"
            "pushq $0x1"
            "pop %rdi"
            "pushq $0x3c"
            "pop %rax"
            "syscall"
    );

    return 0;
}

返回的错误是:

$ gcc -o shellcode shellcode.c
shellcode.c: Assembler messages:
shellcode.c:4: Error: bad register name `%rdxmovq $0x68732f6e69622fff'

谢谢大家。

最佳答案

您需要将换行符 (\n) 放入引用的内联程序集中。否则,它认为

xorq %rdx,%rdx
movq $0x68732f6e69622fff, %rdx

确实

xorq %rdx,%rdxmovq $0x68732f6e69622fff, %rdx

所以前两行(依此类推)应该更像这样:

"xorq %rdx,%rdx\n"
"movq $0x68732f6e69622fff, %rdx\n"

关于gcc - 内联汇编错误,阻止 gcc 编译尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1359892/

相关文章:

assembly - Intel 64、rsi 和 rdi 寄存器

assembly - `b .`在此汇编代码中是什么意思?

c++ - 写入 [ds :0x0]? 后添加 ud2 有什么意义

linux - 将 .text 部分放在 ELF 的最后

assembly - 当我的程序启动时,堆栈是否与 4 字节边界对齐?

c++ - undefined reference (但 nm 说该函数存在)

c# - 为什么.NET Native 以相反的顺序编译循环?

assembly - x86 多字节 NOP 和指令前缀

c - Windows DLL 文件的正确构建模式是什么?

c - clang 和 gcc 之间的 float 操作结果不一致