File: ELF_STT_GNU_IFUNC.s

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (110 lines) | stat: -rw-r--r-- 3,830 bytes parent folder | download | duplicates (12)
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
# REQUIRES: x86_64-linux
# RUN: rm -rf %t && mkdir -p %t
# RUN: split-file %s %t
# RUN: llvm-mc -triple=x86_64-unknown-linux-gnu -filetype=obj -o %t/test_runner.o %t/test_runner.s
# RUN: llvm-mc -triple=x86_64-unknown-linux-gnu -filetype=obj -o %t/func_defs.o %t/func_defs.s
# RUN: llvm-rtdyld -triple=x86_64-unknown-linux-gnu -verify -check=%s %t/test_runner.o %t/func_defs.o
# RUN: llvm-rtdyld -triple=x86_64-unknown-linux-gnu -execute %t/test_runner.o %t/func_defs.o

#--- test_runner.s

# The _main function of this file contains calls to the two external functions
# "indirect_func" and "normal_func" that are not yet defined. They are called via
# the PLT to simulate how a compiler would emit a call to an external function.
# Eventually, indirect_func will resolve to a STT_GNU_IFUNC and normal_func to a
# regular function. We include calls to both types of functions in this test to
# test that both types of functions are executed correctly when their types are
# not known initially.
# It also contains a call to a locally defined indirect function. As RuntimeDyld
# treats local functions a bit differently than external functions, we also test
# that.
# Verify that the functions return the excpeted value. If the external indirect
# function call fails, this returns the error code 1. If the external normal
# function call fails, it's the error code 2. If the call to the locally
# defined indirect function fails, return the error code 3.

local_real_func:
	mov $0x56, %eax
	ret

local_indirect_func_resolver:
	lea local_real_func(%rip), %rax
	ret

	.type local_indirect_func, @gnu_indirect_function
	.set local_indirect_func, local_indirect_func_resolver

	.global _main
_main:
	call indirect_func@plt
	cmp $0x12, %eax
	je 1f
	mov $1, %eax
	ret
1:

	call normal_func@plt
	cmp $0x34, %eax
	je 1f
	mov $2, %eax
	ret
1:

	call local_indirect_func@plt
	cmp $0x56, %eax
	je 1f
	mov $3, %eax
	ret
1:

	xor %eax, %eax
	ret

# Test that the indirect functions have the same addresses in both calls.
# rtdyld-check: decode_operand(test_indirect_func_address_1, 4) + next_pc(test_indirect_func_address_1) = decode_operand(test_indirect_func_address_2, 4) + next_pc(test_indirect_func_address_2)
test_indirect_func_address_1:
	lea indirect_func(%rip), %rax

test_indirect_func_address_2:
	lea indirect_func(%rip), %rax

# rtdyld-check: decode_operand(test_local_indirect_func_address_1, 4) + next_pc(test_indirect_func_address_1) = decode_operand(test_local_indirect_func_address_2, 4) + next_pc(test_indirect_func_address_2)
test_local_indirect_func_address_1:
	lea local_indirect_func(%rip), %rax

test_local_indirect_func_address_2:
	lea local_indirect_func(%rip), %rax

#--- func_defs.s

# This file contains the external functions that are called above. The type of
# the indirect function is set to @gnu_indirect_function and its value is set
# to the value of ifunc_resolver. This is what gcc emits when using
# __attribute__((ifunc("ifunc_resolver"))) in C. The resolver function just
# returns the address of the real function "real_func".
# To test that everyting works correctly, the indirect function returns 0x12
# and the direct function returns 0x23. This is verified in the _main function
# above.

real_func:
	mov $0x12, %eax
	ret

ifunc_resolver:
	lea real_func(%rip), %rax
	ret

	.global indirect_func
	.type indirect_func, @gnu_indirect_function
	.set indirect_func, ifunc_resolver

	.global normal_func
normal_func:
	mov $0x34, %eax
	ret

# Test that the address of the indirect function is equal even when it is
# defined in another object file.
# rtdyld-check: decode_operand(test_indirect_func_address_1, 4) + next_pc(test_indirect_func_address_1) = decode_operand(test_indirect_func_address_3, 4) + next_pc(test_indirect_func_address_3)
test_indirect_func_address_3:
	lea indirect_func(%rip), %rax