aboutsummaryrefslogtreecommitdiff
path: root/bolt/test/X86/linux-alt-instruction.s
blob: fe3abbfc2b4c936223c18bceb014fd41e0c504b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# REQUIRES: system-linux

## Check that BOLT correctly parses the Linux kernel .altinstructions section
## and annotates alternative instructions.

# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown %s -o %t.o
# RUN: %clang %cflags -nostdlib %t.o -o %t.exe \
# RUN:   -Wl,--image-base=0xffffffff80000000,--no-dynamic-linker,--no-eh-frame-hdr,--no-pie
# RUN: llvm-bolt %t.exe --print-cfg --alt-inst-feature-size=2 -o %t.out \
# RUN:   | FileCheck %s

## Older kernels used to have padlen field in alt_instr. Check compatibility.

# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown --defsym PADLEN=1 \
# RUN:   %s -o %t.padlen.o
# RUN: %clang %cflags -nostdlib %t.padlen.o -o %t.padlen.exe \
# RUN:   -Wl,--image-base=0xffffffff80000000,--no-dynamic-linker,--no-eh-frame-hdr,--no-pie
# RUN: llvm-bolt %t.padlen.exe --print-cfg --alt-inst-has-padlen -o %t.padlen.out \
# RUN:   | FileCheck %s

## Check with a larger size of "feature" field in alt_instr.

# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown \
# RUN:   --defsym FEATURE_SIZE_4=1 %s -o %t.fs4.o
# RUN: %clang %cflags -nostdlib %t.fs4.o -o %t.fs4.exe \
# RUN:   -Wl,--image-base=0xffffffff80000000,--no-dynamic-linker,--no-eh-frame-hdr,--no-pie
# RUN: llvm-bolt %t.fs4.exe --print-cfg --alt-inst-feature-size=4 -o %t.fs4.out \
# RUN:   | FileCheck %s

## Check that out-of-bounds read is handled properly.

# RUN: not llvm-bolt %t.fs4.exe --alt-inst-feature-size=2 -o %t.fs4.out

## Check that BOLT automatically detects structure fields in .altinstructions.

# RUN: llvm-bolt %t.exe --print-cfg -o %t.out | FileCheck %s
# RUN: llvm-bolt %t.exe --print-cfg -o %t.padlen.out | FileCheck %s
# RUN: llvm-bolt %t.exe --print-cfg -o %t.fs4.out | FileCheck %s

# CHECK:      BOLT-INFO: Linux kernel binary detected
# CHECK:      BOLT-INFO: parsed 3 alternative instruction entries

  .text
  .globl _start
  .type _start, %function
_start:
# CHECK: Binary Function "_start"
.L0:
  rdtsc
# CHECK:      rdtsc
# CHECK-SAME: AltInst: 1
# CHECK-SAME: AltInst2: 2
# CHECK-SAME: AltInst3: 3
  nop
# CHECK-NEXT: nop
# CHECK-SAME: AltInst: 1
# CHECK-SAME: AltInst2: 2
# CHECK-SAME: AltInst3: 3
  nop
  nop
.L1:
  ret
  .size _start, .-_start

  .section .altinstr_replacement,"ax",@progbits
.A0:
  lfence
  rdtsc
.A1:
  rdtscp
.A2:
  pushf
  pop %rax
.Ae:

## Alternative instruction info.
  .section .altinstructions,"a",@progbits

  .long .L0 - .   # org instruction
  .long .A0 - .   # alt instruction
.ifdef FEATURE_SIZE_4
  .long 0x72      # feature flags
.else
  .word 0x72      # feature flags
.endif
  .byte .L1 - .L0 # org size
  .byte .A1 - .A0 # alt size
.ifdef PADLEN
  .byte 0
.endif

  .long .L0 - .   # org instruction
  .long .A1 - .   # alt instruction
.ifdef FEATURE_SIZE_4
  .long 0x3b      # feature flags
.else
  .word 0x3b      # feature flags
.endif
  .byte .L1 - .L0 # org size
  .byte .A2 - .A1 # alt size
.ifdef PADLEN
  .byte 0
.endif

  .long .L0 - .   # org instruction
  .long .A2 - .   # alt instruction
.ifdef FEATURE_SIZE_4
  .long 0x110     # feature flags
.else
  .word 0x110     # feature flags
.endif
  .byte .L1 - .L0 # org size
  .byte .Ae - .A2 # alt size
.ifdef PADLEN
  .byte 0
.endif

## ORC unwind for "pushf; pop %rax" alternative sequence.
  .section .orc_unwind,"a",@progbits
  .align 4
  .section .orc_unwind_ip,"a",@progbits
  .align 4

  .section .orc_unwind
  .2byte 8
  .2byte 0
  .2byte 0x205
  .section .orc_unwind_ip
  .long _start - .

  .section .orc_unwind
  .2byte 16
  .2byte 0
  .2byte 0x205
  .section .orc_unwind_ip
  .long .L0 + 1 - .

  .section .orc_unwind
  .2byte 8
  .2byte 0
  .2byte 0x205
  .section .orc_unwind_ip
  .long .L0 + 2 - .

## Fake Linux Kernel sections.
  .section __ksymtab,"a",@progbits
  .section __ksymtab_gpl,"a",@progbits