<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - clang -pg produces invalid binaries on FreeBSD"
   href="https://bugs.llvm.org/show_bug.cgi?id=48165">48165</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang -pg produces invalid binaries on FreeBSD
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>FreeBSD
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>oleg-derevenetz@yandex.ru
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following program:

$ cat test.c
#include <stdio.h>

int main()
{
    printf("OK\n");
}

When built using the following command:

$ clang -pg test.c

the following binary is produced:

$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), statically
linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 12.2, FreeBSD-style, with
debug_info, not stripped

(note the "interpreter /libexec/ld-elf.so.1" part).

Once launched, it crashes inside ld-elf.so.1 with SIGSEGV:

$ lldb a.out
(lldb) target create "a.out"
Current executable set to '/usr/home/oleg/tmp/pg/a.out' (x86_64).
(lldb) run
Process 872 launching
Process 872 launched: '/usr/home/oleg/tmp/pg/a.out' (x86_64)
Process 872 stopped
* thread #1, name = 'a.out', stop reason = signal SIGSEGV: invalid address
(fault address: 0x0)
    frame #0: 0x00000008002a3ab0
->  0x8002a3ab0: movq   (%r15), %rdx
    0x8002a3ab3: cmpq   $0x6fffffef, %rdx         ; imm = 0x6FFFFFEF
    0x8002a3aba: jg     0x8002a3b10
    0x8002a3abc: cmpq   $0x21, %rdx
(lldb) disassemble
->  0x8002a3ab0: movq   (%r15), %rdx
    0x8002a3ab3: cmpq   $0x6fffffef, %rdx         ; imm = 0x6FFFFFEF
    0x8002a3aba: jg     0x8002a3b10
    0x8002a3abc: cmpq   $0x21, %rdx
    0x8002a3ac0: ja     0x8002a3ba6
    0x8002a3ac6: movslq (%r12,%rdx,4), %rax
    0x8002a3aca: addq   %r12, %rax
    0x8002a3acd: jmpq   *%rax
(lldb)

Disassembled code above is apparently from ld-elf.so.1/_rtld_is_dlopened.

However, if the following command is used for the build:

$ clang -pg -static test.c

the following binary is produced:

$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), statically
linked, for FreeBSD 12.2, FreeBSD-style, with debug_info, not stripped

(note the absence of "interpreter ..." part). This binary runs just fine:

$ lldb a.out
(lldb) target create "a.out"
Current executable set to '/usr/home/oleg/tmp/pg/a.out' (x86_64).
(lldb) run
Process 914 launching
Process 914 launched: '/usr/home/oleg/tmp/pg/a.out' (x86_64)
OK
Process 914 exited with status = 0 (0x00000000)
(lldb)

$ clang --version
FreeBSD clang version 10.0.1 (<a href="mailto:git@github.com">git@github.com</a>:llvm/llvm-project.git
llvmorg-10.0.1-0-gef32c611aa2)
Target: x86_64-unknown-freebsd12.2
Thread model: posix
InstalledDir: /usr/bin</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>