5

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

if (cond)
   foo;
   bar();

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

4

4 回答 4

7

-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
于 2008-11-12T03:30:51.683 回答
2

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.

于 2008-11-12T07:02:52.567 回答
1

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

于 2008-11-12T03:56:30.290 回答
0

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.

于 2010-08-29T21:07:22.777 回答