How to disassemble a binary executable in Linux to get the assembly code?

后端 未结 9 1291
情歌与酒
情歌与酒 2020-11-28 02:57

I was told to use a disassembler. Does gcc have anything built in? What is the easiest way to do this?

相关标签:
9条回答
  • 2020-11-28 03:38

    Let's say that you have:

    #include <iostream>
    
    double foo(double x)
    {
      asm("# MyTag BEGIN"); // <- asm comment,
                            //    used later to locate piece of code
      double y = 2 * x + 1;
    
      asm("# MyTag END");
    
      return y;
    }
    
    int main()
    {
      std::cout << foo(2);
    }
    

    To get assembly code using gcc you can do:

     g++ prog.cpp -c -S -o - -masm=intel | c++filt | grep -vE '\s+\.'
    

    c++filt demangles symbols

    grep -vE '\s+\.' removes some useless information

    Now if you want to visualize the tagged part, simply use:

    g++ prog.cpp -c -S -o - -masm=intel | c++filt | grep -vE '\s+\.' | grep "MyTag BEGIN" -A 20
    

    With my computer I get:

        # MyTag BEGIN
    # 0 "" 2
    #NO_APP
        movsd   xmm0, QWORD PTR -24[rbp]
        movapd  xmm1, xmm0
        addsd   xmm1, xmm0
        addsd   xmm0, xmm1
        movsd   QWORD PTR -8[rbp], xmm0
    #APP
    # 9 "poub.cpp" 1
        # MyTag END
    # 0 "" 2
    #NO_APP
        movsd   xmm0, QWORD PTR -8[rbp]
        pop rbp
        ret
    .LFE1814:
    main:
    .LFB1815:
        push    rbp
        mov rbp, rsp
    

    A more friendly approach is to use: Compiler Explorer

    0 讨论(0)
  • 2020-11-28 03:39

    Use IDA Pro and the Decompiler.

    0 讨论(0)
  • 2020-11-28 03:39

    You might find ODA useful. It's a web-based disassembler that supports tons of architectures.

    http://onlinedisassembler.com/

    0 讨论(0)
提交回复
热议问题