Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the source lines inline with the assembly output using GCC?

I'd like to get the C source lines inline with the assembly output to see what code is being generated.

I have tried GCC options like -S -Wa,-ahlms (and even -Wa,--gstabs 'cause I read it somewhere).

Oh! BTW, I am on a Mac so I don't have objdump.

(Sorry this is short, I have to get off the train!)

Output of gcc pc-clisp.c -S -g -fverbose-asm -fnested-functions

.globl _getBool
_getBool:
LFB32:
LM21:
    pushl   %ebp    #
LCFI30:
    movl    %esp, %ebp      #,
LCFI31:
    subl    $8, %esp        #,
LCFI32:
LM22:
    movzbl  8(%ebp), %eax   # atom.pred, D.4112
    movzbl  %al, %eax       # D.4112, D.4113
    andl    $-16, %eax      #, D.4114
    sarl    $4, %eax        #, D.4115
    testl   %eax, %eax      # D.4115
    setne   %al     #, tmp64
    movzbl  %al, %eax       # tmp64, D.4111
    leave
    ret
LFE32:
like image 867
philcolbourn Avatar asked Mar 09 '10 21:03

philcolbourn


People also ask

Does gcc output assembly?

Luckily, gcc does not output binary machine code directly. Instead, it internally writes assembler code, which then is translated by as into binary machine code (actually, gcc creates more intermediate structures). This internal assembler code can be outputted to a file, with some annotation to make it easier to read.

Can you compile assembly with gcc?

Yes, gcc can also compile assembly source code. Alternatively, you can invoke as , which is the assembler. (gcc is just a "driver" program that uses heuristics to call C compiler, C++ compiler, assembler, linker, etc..)

What is option in gcc?

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker.


2 Answers

Maybe debug + a post-process step?

gcc <source.c> -S -g -fverbose-asm

Find .file 1 "1.c" to tell you the file number to file name mapping. Then add source after the .loc 1 8 0 lines. I'm not sure how to do it with a single shell command, but a short script should be able to do it:

#!/usr/bin/env python

import re
import sys

filename = sys.argv[1]

f = open(filename)
lines = f.readlines()
f.close()

FILE_RE=re.compile(r"\s+\.file (\d+) \"(.*)\"")
LOC_RE =re.compile(r"\s+\.loc (\d+) (\d+)")

output = []
files = {}
for line in lines:
    output.append(line)
    mo = FILE_RE.match(line)
    if mo is not None:
       files[mo.group(1)] = open(mo.group(2)).readlines()
       print mo.group(1),"=",mo.group(2)
       continue
    mo = LOC_RE.match(line)
    if mo is not None:
       print mo.group(1),"=",mo.group(2)
       source = files[mo.group(1)][int(mo.group(2))-1]
       output.append("\t#"+source)

f = open(filename+".2","w")
f.writelines(output)
f.close()
like image 100
Douglas Leeder Avatar answered Oct 21 '22 13:10

Douglas Leeder


It's not exactly what you're asking for, but you may find -S -fverbose-asm helpful.

like image 23
Stephen Canon Avatar answered Oct 21 '22 11:10

Stephen Canon