I know I can get the assembler source code generated by the compiler by using:
gcc -S ...
even though that annoyingly doesn’t give me an object file as part of the process.
But how can I get everything about the compiled code? I mean addresses, the bytes generated and so forth.
The instructions output by gcc -S do not tell me anything about instruction lengths or encodings, which is what I want to see.
I like
objdumpfor this, but the most useful options are non-obvious – especially if you’re using it on an object file which contains relocations, rather than a final binary.objdump -d some_binarydoes a reasonable job.objdump -d some_object.ois less useful because calls to external functions don’t get disassembled helpfully:The
callis actually toprintf()… adding the-rflag helps with that; it marks relocations.objdump -dr some_object.ogives:Then, I find it useful to see each line annotated as
<symbol+offset>.objdumphas a handy option for that, but it has the annoying side effect of turning off the dump of the actual bytes –objdump --prefix-addresses -dr some_object.ogives:But it turns out that you can undo that by providing another obscure option, finally arriving at my favourite
objdumpincantation:objdump --prefix-addresses --show-raw-insn -dr file.owhich gives output like this:
And if you’ve built with debugging symbols (i.e. compiled with
-g), and you replace the-drwith-Srl, it will attempt to annotate the output with the corresponding source lines.