[llvm-bugs] [Bug 37528] New: Miscompilation with intersegment jmp on Mac OsX

via llvm-bugs llvm-bugs at lists.llvm.org
Sat May 19 00:10:05 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=37528

            Bug ID: 37528
           Summary: Miscompilation with intersegment jmp on Mac OsX
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: MacOS X
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: valerakonychev at gmail.com
                CC: llvm-bugs at lists.llvm.org

Hello. I'm making a special debugging/testing tool for my work. During this I
faced clang miscompilation bug on Mac OsX. If in source code there is a jmp
from .text section to others, the resulting executable file will be incorrect.
It's strange because the object file is correct.

I've gotten this bug with my standard Mac OsX compiler: Apple LLVM version
9.0.0 (clang-900.0.37). Also I had downloaded trunk(clang version 7.0.0 (trunk
332669)) and got this bug again.

I've checked behaviour of clang and gcc on Linux (Ubuntu 16.04) and everything
are fine: executable files are correct.

The simple way to reproduce the bug:
clang_jmp_test.c:
#include <stdio.h>

int main () {
    asm volatile(
        "jmp 1f\n"
        ".section __TEXT,__my_sect, regular,pure_instructions\n"
        "1: jmp 2f\n"
        ".text\n"
        "2: nop\n");

    printf("Hello world\n");

    return 0;
}

After clang -c clang_jmp_test.c into the object file everything are fine:
clang_jmp_test.o:
_main:
0000000000000000  55                    pushq   %rbp
0000000000000001  48 89 e5              movq    %rsp, %rbp
0000000000000004  48 83 ec 10           subq    $0x10, %rsp
0000000000000008  c7 45 fc 00 00 00 00  movl    $0x0, -0x4(%rbp)
000000000000000f  e9 1c 00 00 00        jmp     0x30 // right
0000000000000014  90                    nop
0000000000000015  48 8d 3d 19 00 00 00  leaq    0x19(%rip), %rdi
000000000000001c  b0 00                 movb    $0x0, %al
000000000000001e  e8 00 00 00 00        callq   0x23
0000000000000023  31 c9                 xorl    %ecx, %ecx
0000000000000025  89 45 f8              movl    %eax, -0x8(%rbp)
0000000000000028  89 c8                 movl    %ecx, %eax
000000000000002a  48 83 c4 10           addq    $0x10, %rsp
000000000000002e  5d                    popq    %rbp
000000000000002f  c3                    retq
Contents of (__TEXT,__my_sect) section
0000000000000030  e9 14 00 00 00        jmp     0x49 // with relocation right

Relocations in the clang_jmp_test.o are fine too:
RELOCATION RECORDS FOR [__text]:
000000000000001f X86_64_RELOC_BRANCH _printf
0000000000000018 X86_64_RELOC_SIGNED __cstring
0000000000000010 X86_64_RELOC_BRANCH __my_sect

RELOCATION RECORDS FOR [__my_sect]:
0000000000000001 X86_64_RELOC_BRANCH _main

But in the executable file jmp from .text section to __my_sect is wrong! and
there are no any relocations.
clang_jmp_test:
_main:
0000000100000f40  55                    pushq   %rbp
0000000100000f41  48 89 e5              movq    %rsp, %rbp
0000000100000f44  48 83 ec 10           subq    $0x10, %rsp
0000000100000f48  c7 45 fc 00 00 00 00  movl    $0x0, -0x4(%rbp)
0000000100000f4f  e9 18 00 00 00        jmp     0x100000f6c // wrong
0000000100000f54  90                    nop
0000000100000f55  48 8d 3d 3a 00 00 00  leaq    0x3a(%rip), %rdi
0000000100000f5c  b0 00                 movb    $0x0, %al
0000000100000f5e  e8 13 00 00 00        callq   0x100000f76
0000000100000f63  31 c9                 xorl    %ecx, %ecx
0000000100000f65  89 45 f8              movl    %eax, -0x8(%rbp)
0000000100000f68  89 c8                 movl    %ecx, %eax
0000000100000f6a  48 83 c4 10           addq    $0x10, %rsp
0000000100000f6e  5d                    popq    %rbp
0000000100000f6f  c3                    retq
Contents of (__TEXT,__my_sect) section
0000000100000f70  e9 df ff ff ff        jmp     0x100000f54 // right

This problem is related to standard sections too. I've checked it with __data
section.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180519/312a6a34/attachment-0001.html>


More information about the llvm-bugs mailing list