165 lines · plain
1# REQUIRES: system-linux2 3# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown %s -o %t.o4# RUN: llvm-strip --strip-unneeded %t.o5# RUN: %clang %cflags -no-pie -nostartfiles -nostdlib -lc %t.o -o %t.exe -Wl,-q6 7# RUN: llvm-bolt %t.exe -o %t.exe.bolt --relocs=1 --lite=08 9# RUN: %t.exe.bolt10 11## Check that BOLT's jump table detection diffrentiates between12## __builtin_unreachable() targets and function pointers.13 14## The test case was built from the following two source files and15## modiffied for standalone build. main became _start, etc.16## $ $(CC) a.c -O1 -S -o a.s17## $ $(CC) b.c -O0 -S -o b.s18 19## a.c:20 21## typedef int (*fptr)(int);22## void check_fptr(fptr, int);23##24## int foo(int a) {25## check_fptr(foo, 0);26## switch (a) {27## default:28## __builtin_unreachable();29## case 0:30## return 3;31## case 1:32## return 5;33## case 2:34## return 7;35## case 3:36## return 11;37## case 4:38## return 13;39## case 5:40## return 17;41## }42## return 0;43## }44##45## int main(int argc) {46## check_fptr(main, 1);47## return foo(argc);48## }49##50## const fptr funcs[2] = {foo, main};51 52## b.c.:53 54## typedef int (*fptr)(int);55## extern const fptr funcs[2];56##57## #define assert(C) { if (!(C)) (*(unsigned long long *)0) = 0; }58## void check_fptr(fptr f, int i) {59## assert(f == funcs[i]);60## }61 62 63 .text64 .globl foo65 .type foo, @function66foo:67.LFB0:68 .cfi_startproc69 pushq %rbx70 .cfi_def_cfa_offset 1671 .cfi_offset 3, -1672 movl %edi, %ebx73 movl $0, %esi74 movl $foo, %edi75 call check_fptr76 movl %ebx, %ebx77 jmp *.L4(,%rbx,8)78.L8:79 movl $5, %eax80 jmp .L181.L7:82 movl $7, %eax83 jmp .L184.L6:85 movl $11, %eax86 jmp .L187.L5:88 movl $13, %eax89 jmp .L190.L3:91 movl $17, %eax92 jmp .L193.L10:94 movl $3, %eax95.L1:96 popq %rbx97 .cfi_def_cfa_offset 898 ret99 .cfi_endproc100.LFE0:101 .size foo, .-foo102 .globl _start103 .type _start, @function104_start:105.LFB1:106 .cfi_startproc107 pushq %rbx108 .cfi_def_cfa_offset 16109 .cfi_offset 3, -16110 movl %edi, %ebx111 movl $1, %esi112 movl $_start, %edi113 call check_fptr114 movl $1, %edi115 call foo116 popq %rbx117 .cfi_def_cfa_offset 8118 callq exit@PLT119 .cfi_endproc120.LFE1:121 .size _start, .-_start122 .globl check_fptr123 .type check_fptr, @function124check_fptr:125.LFB2:126 .cfi_startproc127 pushq %rbp128 .cfi_def_cfa_offset 16129 .cfi_offset 6, -16130 movq %rsp, %rbp131 .cfi_def_cfa_register 6132 movq %rdi, -8(%rbp)133 movl %esi, -12(%rbp)134 movl -12(%rbp), %eax135 cltq136 movq funcs(,%rax,8), %rax137 cmpq %rax, -8(%rbp)138 je .L33139 movl $0, %eax140 movq $0, (%rax)141.L33:142 nop143 popq %rbp144 .cfi_def_cfa 7, 8145 ret146 .cfi_endproc147 148 .section .rodata149 .align 8150 .align 4151.L4:152 .quad .L10153 .quad .L8154 .quad .L7155 .quad .L6156 .quad .L5157 .quad .L3158 159 .globl funcs160 .type funcs, @object161 .size funcs, 16162funcs:163 .quad foo164 .quad _start165