LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Blogs > Bits and Pixels
User Name
Password

Notices

Concerning free software, programming, and whatever else I find interesting. Also the blog for my Web site, frigidcode.com.
Rate this Entry

See source code intermixed with assembly

Posted 01-16-2012 at 03:25 AM by hydraMax

Maybe everybody else already knows this, but I thought it was pretty neat anyway:

If you compile some C code with -g option (to include debugging symbols), you can then use objdump on the binary, with the -S switch, to get source code intermixed with assembly. For example, here is a snippet from one of my binaries:

Code:
int main() {
  401163:       55                      push   %rbp
  401164:       48 89 e5                mov    %rsp,%rbp
  401167:       53                      push   %rbx
  401168:       48 83 ec 58             sub    $0x58,%rsp
  struct archive * a = archive_read_new();
  40116c:       e8 1f fc ff ff          callq  400d90 <archive_read_new@plt>
  401171:       48 89 45 d8             mov    %rax,-0x28(%rbp)
  if(archive_read_support_compression_all(a) == ARCHIVE_WARN) {
  401175:       48 8b 45 d8             mov    -0x28(%rbp),%rax
  401179:       48 89 c7                mov    %rax,%rdi
  40117c:       e8 2f fb ff ff          callq  400cb0 <archive_read_support_compression_all@plt>
  401181:       83 f8 ec                cmp    $0xffffffffffffffec,%eax
  401184:       75 24                   jne    4011aa <main+0x47>
    fprintf(stderr, "warning: external compression support detected");
  401186:       48 8b 05 63 0f 20 00    mov    0x200f63(%rip),%rax        # 6020f0 <__bss_start>
  40118d:       48 89 c2                mov    %rax,%rdx
  401190:       b8 b0 15 40 00          mov    $0x4015b0,%eax
  401195:       48 89 d1                mov    %rdx,%rcx
  401198:       ba 2e 00 00 00          mov    $0x2e,%edx
  40119d:       be 01 00 00 00          mov    $0x1,%esi
  4011a2:       48 89 c7                mov    %rax,%rdi
  4011a5:       e8 16 fc ff ff          callq  400dc0 <fwrite@plt>
  }
It's rather educational, I would say. The only thing to remember is that the results can be very different (and harder to understand) if you use optimization flags (e.g., -O2) because a lot of code will be removed or condensed. Includes comments as well!
Posted in Uncategorized
Views 599 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration