查找机器代码中的位置(gcc/objdump -d)

发布于 2024-07-08 22:27:04 字数 174 浏览 8 评论 0原文

如果您想要在机器输出中检查特定的 C 代码行,您将如何在 objdump 输出中找到它。 这是一个示例

if (cond)
   foo;
   bar();

,我想看看 bar 是否按照我的意愿内联。 或者你会使用一些替代工具来代替 objdump 吗?

If you have a particular line of C code in mind to examine in the machine output, how would you locate it in objdump output. Here is an example

if (cond)
   foo;
   bar();

and I want to see if bar was inlined as I'd like.
Or would you use some alternative tool instead of objdump?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羞稚 2024-07-15 22:27:05

您可以使用 -S 选项启动 objdump(例如 "objdump -Sd a.out")。 如果编译代码的源文件可用,它将显示与汇编代码混合的源代码。

或者,您可以使用以下方式:

int main(void) {
    int a = 0;
    asm("#");
    return a;
}

成为

       .file   "a.c"
        .text
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $16, %esp
        movl    $0, -8(%ebp)
#APP
# 3 "a.c" 1
        #
# 0 "" 2
#NO_APP
        movl    -8(%ebp), %eax
        addl    $16, %esp
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.3.2"
        .section        .note.GNU-stack,"",@progbits

You can start objdump using the -S option (like "objdump -Sd a.out"). It will display the sourcecode intermixxed with the assembler code, if the source-files the code was compiled from are available.

Alternatively, you can use the following way:

int main(void) {
    int a = 0;
    asm("#");
    return a;
}

becomes

       .file   "a.c"
        .text
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $16, %esp
        movl    $0, -8(%ebp)
#APP
# 3 "a.c" 1
        #
# 0 "" 2
#NO_APP
        movl    -8(%ebp), %eax
        addl    $16, %esp
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.3.2"
        .section        .note.GNU-stack,"",@progbits
妄断弥空 2024-07-15 22:27:05

如果使用调试符号进行编译,调试器还应该让您看到源代码和匹配的程序集。 这是 gcc 选项 -g 和 gdb disass 命令。

You debugger should also let you see source code and matching assembly if you compiled with debug symbols. This is gcc option -g and gdb disass command.

比忠 2024-07-15 22:27:05

如果使用 gcc 编译,可以使用 -S 直接生成汇编文件。 该文件通常包含一些有用的信息,包括函数名称,有时还包括代码的行号(取决于您使用的编译选项)。

If you're compiling with gcc, you can use -S to generate an assembly file directly. This file usually has some useful information in it, including function names and sometimes line numbers for code (depending on the compile options you use).

夏日落 2024-07-15 22:27:05

函数调用由公共函数序言在汇编中检测到。

对于 i386,它

  55      push %ebp
  89 e5   mov %esp, %ebp
  ...
  c9      leave # optional
  c3      ret

与 amd64/x86_64 类似(只是四元前缀 48):

  55                    push   %rbp
  48 89 e5              mov    %rsp,%rbp
  ..
  c9                    leaveq # optional
  c3                    retq   

因此,当您在 objdump -S bla.o 中检测到这一点时
或者
主函数的 gcc bla.c -g -fsave-temps -fverbose-asm 输出
对于 bar 来说,bar 也不是内联的。 当 main 有调用或跳转时
禁止它没有内联。

在您的情况下,您可以查看 bar 是否有本地变量,它需要空间
本地堆栈。 如果 bar 内联,则堆栈调整(例如 sub $0x8,%esp
在主序言之后完成,main 可以访问该变量。
如果不是的话,它是酒吧私有的。

Function calls are detected in the assembly by the common function prolog.

With i386 it is

  55      push %ebp
  89 e5   mov %esp, %ebp
  ...
  c9      leave # optional
  c3      ret

with amd64/x86_64 is is similar (just the quad prefix 48):

  55                    push   %rbp
  48 89 e5              mov    %rsp,%rbp
  ..
  c9                    leaveq # optional
  c3                    retq   

So when you detect that inside your objdump -S bla.o
or
gcc bla.c -g -fsave-temps -fverbose-asm output of your main function
and for bar also, bar is not inlined. Also when main has a call or jump
to bar it is not inlined.

In your case you could see if bar has local vars, which needs room on
the local stack. If bar is inlined the stack adjust (e.g. sub $0x8,%esp)
is done right after the main prolog, main could access that var.
If not it is private to bar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文