How do you get assembler output from C/C++ source in gcc?

后端 未结 17 1790
天命终不由人
天命终不由人 2020-11-22 02:43

How does one do this?

If I want to analyze how something is getting compiled, how would I get the emitted assembly code?

17条回答
  •  隐瞒了意图╮
    2020-11-22 03:06

    If what you want to see depends on the linking of the output, then objdump on the output object file/executable may also be useful in addition to the aforementioned gcc -S. Here's a very useful script by Loren Merritt that converts the default objdump syntax into the more readable nasm syntax:

    #!/usr/bin/perl -w
    $ptr='(BYTE|WORD|DWORD|QWORD|XMMWORD) PTR ';
    $reg='(?:[er]?(?:[abcd]x|[sd]i|[sb]p)|[abcd][hl]|r1?[0-589][dwb]?|mm[0-7]|xmm1?[0-9])';
    open FH, '-|', '/usr/bin/objdump', '-w', '-M', 'intel', @ARGV or die;
    $prev = "";
    while(){
        if(/$ptr/o) {
            s/$ptr(\[[^\[\]]+\],$reg)/$2/o or
            s/($reg,)$ptr(\[[^\[\]]+\])/$1$3/o or
            s/$ptr/lc $1/oe;
        }
        if($prev =~ /\t(repz )?ret / and
           $_ =~ /\tnop |\txchg *ax,ax$/) {
           # drop this line
        } else {
           print $prev;
           $prev = $_;
        }
    }
    print $prev;
    close FH;
    

    I suspect this can also be used on the output of gcc -S.

提交回复
热议问题