Tuesday, August 30, 2011

Monday, August 15, 2011

Asking gcc for help

Useful trick to make "the hard stuff" easier (unless you're bright
in asm) is


gcc -c -g -Wa,-a,-ad  test.c > test.s

That will ask gcc to dump generated assembly with the corresponding C lines.
For example:

   1                            .file   "test.c"
   2                            .text
   3                    .Ltext0:
   4                            .local  pm_pipe
   5                            .comm   pm_pipe,8,4
   6                            .local  pm_notify_thread
   7                            .comm   pm_notify_thread,8,8
   8                            .section        .rodata
   9                    .LC0:
  10 0000 54485245              .string "THREAD\n"
  10      41440A00
  11                    .LC1:
  12 0008 74696D65              .string "timeout...\n"
  12      6F75742E
  12      2E2E0A00
  13                    .LC2:
[..]
  17                            .text
  19                    pm_notify:
  20                    .LFB0:
  21                            .file 1 "test.c"

[..]
  26:test.c        ****                 pfd[0].fd = pm_pipe[0];
  51                            .loc 1 26 0
  52 0046 8B050000              movl    pm_pipe(%rip), %eax
  52      0000
  53 004c 8945E0                movl    %eax, -32(%rbp)
  27:test.c        ****                 pfd[0].events = POLLIN;
  54                            .loc 1 27 0
  55 004f 66C745E4              movw    $1, -28(%rbp)
  55      0100
  28:test.c        ****                
  29:test.c        ****                 ret = poll(pfd, 1, 5 * 1000);
  56                            .loc 1 29 0
  57 0055 488D45E0              leaq    -32(%rbp), %rax
  58 0059 BA881300              movl    $5000, %edx
  58      00
  59 005e BE010000              movl    $1, %esi
  59      00
  60 0063 4889C7                movq    %rax, %rdi
  61 0066 E8000000              call    poll
  61      00
  62 006b 8945FC                movl    %eax, -4(%rbp)

[..]
  33:test.c        ****                 if (pfd[0].revents & POLLIN) {
  72                            .loc 1 33 0
  73 0092 0FB745E6              movzwl  -26(%rbp), %eax
  74 0096 98                    cwtl
  75 0097 83E001                andl    $1, %eax
  76 009a 84C0                  testb   %al, %al
  77 009c 743A                  je      .L2


and so on.