1# REQUIRES: ppc
2
3# RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t.o
4# RUN: ld.lld %t.o -o %t
5# RUN: llvm-readelf -s %t | FileCheck --check-prefix=SYM %s
6# RUN: llvm-readelf -S %t | FileCheck --check-prefix=SECTIONS %s
7# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
8# RUN: llvm-readobj -r %t | FileCheck --check-prefix=REL %s
9
10# RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %s -o %t.o
11# RUN: ld.lld %t.o -o %t
12# RUN: llvm-readelf -s %t | FileCheck --check-prefix=SYM %s
13# RUN: llvm-readelf -S %t | FileCheck --check-prefix=SECTIONS %s
14# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
15# RUN: llvm-readobj -r %t | FileCheck --check-prefix=REL %s
16
17# SYM: Value            Size Type   Bind   Vis     Ndx
18# SYM: 0000000010028298    0 NOTYPE LOCAL  HIDDEN    4 .TOC.
19# SYM: 0000000010010288    0 FUNC   GLOBAL DEFAULT   3 ifunc1
20# SYM: 0000000010010210    0 IFUNC  GLOBAL DEFAULT   2 ifunc2
21# SYM: 0000000010010278    0 FUNC   GLOBAL DEFAULT   3 ifunc3
22
23# SECTIONS: .plt NOBITS 00000000100302a0 0002a0 000018 00 WA 0 0 8
24
25# __plt_ifunc - . = 0x10010218 - 0x10010208 = 16
26# __plt_ifunc2 - . = 0x1001022c - 0x10010210 = 28
27# CHECK: <_start>:
28# CHECK-NEXT:                 addis 2, 12, 2
29# CHECK-NEXT:                 addi 2, 2, -32636
30# CHECK-NEXT: 1001021c:       bl 0x10010240
31# CHECK-NEXT:                 ld 2, 24(1)
32# CHECK-NEXT: 10010224:       bl 0x10010254
33# CHECK-NEXT:                 ld 2, 24(1)
34# CHECK-NEXT:                 addis 3, 2, -2
35# CHECK-NEXT:                 addi 3, 3, 32752
36# CHECK-NEXT:                 addis 3, 2, -2
37# CHECK-NEXT:                 addi 3, 3, 32736
38
39# .plt[0] - .TOC. = 0x100302a0 - 0x10028298 = (1<<16) - 32760
40# CHECK: <__plt_ifunc2>:
41# CHECK-NEXT:     std 2, 24(1)
42# CHECK-NEXT:     addis 12, 2, 1
43# CHECK-NEXT:     ld 12, -32760(12)
44# CHECK-NEXT:     mtctr 12
45# CHECK-NEXT:     bctr
46
47# .plt[1] - .TOC. = 0x100302a0+8 - 0x10028298 = (1<<16) - 32752
48# CHECK: <__plt_ifunc3>:
49# CHECK-NEXT:     std 2, 24(1)
50# CHECK-NEXT:     addis 12, 2, 1
51# CHECK-NEXT:     ld 12, -32752(12)
52# CHECK-NEXT:     mtctr 12
53# CHECK-NEXT:     bctr
54# CHECK-EMPTY:
55
56## .glink has 3 IPLT entries for ifunc1, ifunc2 and ifunc3.
57## ifunc2 and ifunc3 have the same code sequence as their PLT call stubs.
58# CHECK:      Disassembly of section .glink:
59# CHECK-EMPTY:
60# CHECK-NEXT: 0000000010010268 <.glink>:
61# CHECK-NEXT:     addis 12, 2, 1
62# CHECK-NEXT:     ld 12, -32760(12)
63# CHECK-NEXT:     mtctr 12
64# CHECK-NEXT:     bctr
65# CHECK-EMPTY:
66# CHECK-NEXT: 0000000010010278 <ifunc3>:
67# CHECK-NEXT:     addis 12, 2, 1
68# CHECK-NEXT:     ld 12, -32752(12)
69# CHECK-NEXT:     mtctr 12
70# CHECK-NEXT:     bctr
71# CHECK-EMPTY:
72# CHECK-NEXT: 0000000010010288 <ifunc1>:
73# CHECK-NEXT:     addis 12, 2, 1
74# CHECK-NEXT:     ld 12, -32744(12)
75# CHECK-NEXT:     mtctr 12
76# CHECK-NEXT:     bctr
77
78## Check that we emit 3 R_PPC64_IRELATIVE in .rela.dyn.
79# REL:      .rela.dyn {
80# REL-NEXT:   0x100302A0 R_PPC64_IRELATIVE - 0x10010210
81# REL-NEXT:   0x100302A8 R_PPC64_IRELATIVE - 0x10010210
82# REL-NEXT:   0x100302B0 R_PPC64_IRELATIVE - 0x10010210
83# REL-NEXT: }
84
85.type ifunc1,@gnu_indirect_function
86.type ifunc2,@gnu_indirect_function
87.type ifunc3,@gnu_indirect_function
88.globl ifunc1, ifunc2, ifunc3
89ifunc1:
90ifunc2:
91ifunc3:
92  blr
93
94.global _start
95.type   _start,@function
96
97_start:
98.Lfunc_gep0:
99  addis 2, 12, .TOC.-.Lfunc_gep0@ha
100  addi 2, 2, .TOC.-.Lfunc_gep0@l
101.Lfunc_lep0:
102  .localentry     _start, .Lfunc_lep0-.Lfunc_gep0
103
104  ## ifunc1 is taken address.
105  ## ifunc2 is called.
106  ## ifunc3 is both taken address and called.
107  ## We need to create IPLT entries in .glink for ifunc1 and ifunc3, change
108  ## their types from STT_GNU_IFUNC to STT_FUNC, and set their st_shndx/st_value
109  ## to their .glink entries. Technically we don't need an entry for ifunc2 in
110  ## .glink, but we currently do that.
111  bl ifunc2
112  nop
113  bl ifunc3
114  nop
115
116  addis 3, 2, ifunc1@toc@ha
117  addi  3, 3, ifunc1@toc@l
118  addis 3, 2, ifunc3@toc@ha
119  addi  3, 3, ifunc3@toc@l
120