Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding locations in machine code (gcc/objdump -d)

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?

like image 997
Setjmp Avatar asked Nov 12 '08 03:11

Setjmp


3 Answers

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
like image 104
Johannes Schaub - litb Avatar answered Dec 01 '22 02:12

Johannes Schaub - litb


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.

like image 23
Krunch Avatar answered Dec 01 '22 02:12

Krunch


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).

like image 32
SoapBox Avatar answered Dec 01 '22 01:12

SoapBox