c - 反汇编 C 代码

标签 c assembly nasm

如何反汇编 C 代码?我已经在这里阅读了一些问题(stackoverflow)。但是如果你想反汇编,你需要一个机器代码,那么如何使用 nasm 来反汇编呢?所以如果我为前创建。 C 语言的 Hello World 如何做到这一点?

最佳答案

纳斯姆是个坏主意。有几种选择。 IDA pro 给了我一些成功,但是如果你真的了解你的程序集,你可以 nm 查找符号,然后从那里十六进制转储代码并手动从中生成程序集。不过,确实没有一种方法可以使用 nasm 生成可重新编译的代码。

otool(或 objdump)将生成程序集。

如果您需要一些示例:此处:

#include <stdio.h>
main(argc, argv)
int argc; char * * argv;
{
    printf("Hello, World\n");
}

纳米输出:

hydrogen:tmp phyrrus9$ nm a.out
0000000100000000 T __mh_execute_header
0000000100000f40 T _main
                 U _printf
                 U dyld_stub_binder

otool 输出:

hydrogen:tmp phyrrus9$ otool -tv a.out
a.out:
(__TEXT,__text) section
_main:
0000000100000f40    pushq   %rbp
0000000100000f41    movq    %rsp, %rbp
0000000100000f44    subq    $0x10, %rsp
0000000100000f48    leaq    0x37(%rip), %rdi         ; this is our string
0000000100000f4f    movb    $0x0, %al
0000000100000f51    callq   0x100000f66              ; call printf
0000000100000f56    movl    $0x0, %ecx
0000000100000f5b    movl    %eax, 0xfffffffffffffffc(%rbp)
0000000100000f5e    movl    %ecx, %eax
0000000100000f60    addq    $0x10, %rsp
0000000100000f64    popq    %rbp
0000000100000f65    ret

未显示十六进制转储输出。

实际组装:

hydrogen:tmp phyrrus9$ cat tmp.s
.section    __TEXT,__text,regular,pure_instructions
.globl  _main
.align  4, 0x90
_main:                                  ## @main
.cfi_startproc
## BB#0:
pushq   %rbp
Ltmp2:
.cfi_def_cfa_offset 16
Ltmp3:
.cfi_offset %rbp, -16
movq    %rsp, %rbp
Ltmp4:
.cfi_def_cfa_register %rbp
subq    $16, %rsp
leaq    L_.str(%rip), %rdi
movb    $0, %al
callq   _printf
movl    $0, %ecx
movl    %eax, -4(%rbp)          ## 4-byte Spill
movl    %ecx, %eax
addq    $16, %rsp
popq    %rbp
ret
.cfi_endproc

.section    __TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str
.asciz   "Hello, world!\n"


.subsections_via_symbols

希望这可以帮助您掌握。

关于c - 反汇编 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23460544/

相关文章:

assembly - 整数溢出问题

c - 0x0d和0xff(来自终结器金丝雀)可防止哪些字符串操作

汇编 8086 光标放置

windows - 如何在 64 位 Windows 计算机上链接 32 位 Nasm 汇编目标代码

c - 在 C 中的循环中包含第一次迭代

java - “堆栈”未声明的错误。 Java 到 C

c - strstr() 函数

c - 如何优化我的 C 代码?

linux - 程序集:printf 不打印新行

assembly - nasm 程序集中第一个结构成员列表的指针