Profile (-pg) segfault


I’m trying to profile a code compiled with Clang and I’m getting a segfault on mcount independently of the program I run:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b1245b in mcount () from /lib/x86_64-linux-gnu/
(gdb) bt
#0 0x00007ffff7b1245b in mcount () from /lib/x86_64-linux-gnu/
#1 0x00007ffff7dd6588 in ?? () from /lib/x86_64-linux-gnu/
#2 0x0000000000000000 in ?? ()

(gdb) disas

Dump of assembler code for function mcount:
0x00007ffff7b12430 <+0>: sub $0x38,%rsp
0x00007ffff7b12434 <+4>: mov %rax,(%rsp)
0x00007ffff7b12438 <+8>: mov %rcx,0x8(%rsp)
0x00007ffff7b1243d <+13>: mov %rdx,0x10(%rsp)
0x00007ffff7b12442 <+18>: mov %rsi,0x18(%rsp)
0x00007ffff7b12447 <+23>: mov %rdi,0x20(%rsp)
0x00007ffff7b1244c <+28>: mov %r8,0x28(%rsp)
0x00007ffff7b12451 <+33>: mov %r9,0x30(%rsp)
0x00007ffff7b12456 <+38>: mov 0x38(%rsp),%rsi
=> 0x00007ffff7b1245b <+43>: mov 0x8(%rbp),%rdi
0x00007ffff7b1245f <+47>: callq 0x7ffff7b116a0
0x00007ffff7b12464 <+52>: mov 0x30(%rsp),%r9
0x00007ffff7b12469 <+57>: mov 0x28(%rsp),%r8
0x00007ffff7b1246e <+62>: mov 0x20(%rsp),%rdi
0x00007ffff7b12473 <+67>: mov 0x18(%rsp),%rsi
0x00007ffff7b12478 <+72>: mov 0x10(%rsp),%rdx
0x00007ffff7b1247d <+77>: mov 0x8(%rsp),%rcx
0x00007ffff7b12482 <+82>: mov (%rsp),%rax
0x00007ffff7b12486 <+86>: add $0x38,%rsp
0x00007ffff7b1248a <+90>: retq
End of assembler dump.
(gdb) p $rdi
$1 = 1
(gdb) p $rsp
$2 = (void *) 0x7fffffffded0
(gdb) p $rbp
$3 = (void *) 0x0

This is Ubuntu 12.10, libc6 2.15, gcc 4.7.2, and of course, it works with gcc.

I’m not sure how to begin debugging it, since mcount was probably compiled with gcc, but it looks like LLVM is generating the wrong call standard.

It only appears in -O1 upwards, but there’s not stack trace, and I suspect this is due to how the profiling instrumentation work… Any pointers?