1 ## Test the different ways of hooking the fini function for instrumentation (via
2 ## DT_FINI and via DT_FINI_ARRAY). We test the latter for both PIE and non-PIE
3 ## binaries because of the different ways of handling relocations (static or
5 ## All tests perform the following steps:
6 ## - Compile and link for the case to be tested
7 ## - Some sanity-checks on the dynamic section and relocations in the binary to
8 ## verify it has the shape we want for testing:
9 ## - DT_FINI or DT_FINI_ARRAY in dynamic section
10 ## - No relative relocations for non-PIE
12 ## - Verify generated binary
13 # REQUIRES: system-linux,bolt-runtime,target=aarch64{{.*}}
15 # RUN: %clang %cflags -pie %s -Wl,-q -o %t.exe
16 # RUN: llvm-readelf -d %t.exe | FileCheck --check-prefix=DYN-FINI %s
17 # RUN: llvm-readelf -r %t.exe | FileCheck --check-prefix=RELOC-PIE %s
18 # RUN: llvm-bolt %t.exe -o %t --instrument
19 # RUN: llvm-readelf -drs %t | FileCheck --check-prefix=CHECK-FINI %s
21 # RUN: %clang %cflags -pie %s -Wl,-q,-fini=0 -o %t-no-fini.exe
22 # RUN: llvm-readelf -d %t-no-fini.exe | FileCheck --check-prefix=DYN-NO-FINI %s
23 # RUN: llvm-readelf -r %t-no-fini.exe | FileCheck --check-prefix=RELOC-PIE %s
24 # RUN: llvm-bolt %t-no-fini.exe -o %t-no-fini --instrument
25 # RUN: llvm-readelf -drs %t-no-fini | FileCheck --check-prefix=CHECK-NO-FINI %s
26 # RUN: llvm-readelf -ds -x .fini_array %t-no-fini | FileCheck --check-prefix=CHECK-NO-FINI-RELOC %s
28 ## Create a dummy shared library to link against to force creation of the dynamic section.
29 # RUN: %clang %cflags %p/../Inputs/stub.c -fPIC -shared -o %t-stub.so
30 # RUN: %clang %cflags %s -no-pie -Wl,-q,-fini=0 %t-stub.so -o %t-no-pie-no-fini.exe
31 # RUN: llvm-readelf -r %t-no-pie-no-fini.exe | FileCheck --check-prefix=RELOC-NO-PIE %s
32 # RUN: llvm-bolt %t-no-pie-no-fini.exe -o %t-no-pie-no-fini --instrument
33 # RUN: llvm-readelf -ds -x .fini_array %t-no-pie-no-fini | FileCheck --check-prefix=CHECK-NO-PIE-NO-FINI %s
35 ## With fini: dynamic section should contain DT_FINI
38 ## Without fini: dynamic section should only contain DT_FINI_ARRAY
39 # DYN-NO-FINI-NOT: (FINI)
40 # DYN-NO-FINI: (FINI_ARRAY)
41 # DYN-NO-FINI: (FINI_ARRAYSZ)
43 ## With PIE: binary should have relative relocations
44 # RELOC-PIE: R_AARCH64_RELATIVE
46 ## Without PIE: binary should not have relative relocations
47 # RELOC-NO-PIE-NOT: R_AARCH64_RELATIVE
49 ## Check that DT_FINI is set to __bolt_runtime_fini
50 # CHECK-FINI: Dynamic section at offset {{.*}} contains {{.*}} entries:
51 # CHECK-FINI-DAG: (FINI) 0x[[FINI:[[:xdigit:]]+]]
52 # CHECK-FINI-DAG: (FINI_ARRAY) 0x[[FINI_ARRAY:[[:xdigit:]]+]]
53 ## Check that the dynamic relocation at .fini_array was not patched
54 # CHECK-FINI: Relocation section '.rela.dyn' at offset {{.*}} contains {{.*}} entries
55 # CHECK-FINI-NOT: {{0+}}[[FINI_ARRAY]] {{.*}} R_AARCH64_RELATIVE [[FINI]]
56 # CHECK-FINI: Symbol table '.symtab' contains {{.*}} entries:
57 # CHECK-FINI: {{0+}}[[FINI]] {{.*}} __bolt_runtime_fini
59 ## Check that DT_FINI_ARRAY has a dynamic relocation for __bolt_runtime_fini
60 # CHECK-NO-FINI: Dynamic section at offset {{.*}} contains {{.*}} entries:
61 # CHECK-NO-FINI-NOT: (FINI)
62 # CHECK-NO-FINI: (FINI_ARRAY) 0x[[FINI_ARRAY:[[:xdigit:]]+]]
63 # CHECK-NO-FINI: Relocation section '.rela.dyn' at offset {{.*}} contains {{.*}} entries
64 # CHECK-NO-FINI: {{0+}}[[FINI_ARRAY]] {{.*}} R_AARCH64_RELATIVE [[FINI_ADDR:[[:xdigit:]]+]]
65 # CHECK-NO-FINI: Symbol table '.symtab' contains {{.*}} entries:
66 # CHECK-NO-FINI: {{0+}}[[FINI_ADDR]] {{.*}} __bolt_runtime_fini
68 ## Check that the static relocation in .fini_array is patched even for PIE
69 # CHECK-NO-FINI-RELOC: Dynamic section at offset {{.*}} contains {{.*}} entries:
70 # CHECK-NO-FINI-RELOC: (FINI_ARRAY) 0x[[FINI_ARRAY:[[:xdigit:]]+]]
71 # CHECK-NO-FINI-RELOC: Symbol table '.symtab' contains {{.*}} entries:
72 ## Read bytes separately so we can reverse them later
73 # CHECK-NO-FINI-RELOC: {{0+}}[[FINI_ADDR_B0:[[:xdigit:]]{2}]][[FINI_ADDR_B1:[[:xdigit:]]{2}]][[FINI_ADDR_B2:[[:xdigit:]]{2}]][[FINI_ADDR_B3:[[:xdigit:]]{2}]] {{.*}} __bolt_runtime_fini
74 # CHECK-NO-FINI-RELOC: Hex dump of section '.fini_array':
75 # CHECK-NO-FINI-RELOC: 0x{{0+}}[[FINI_ARRAY]] [[FINI_ADDR_B3]][[FINI_ADDR_B2]][[FINI_ADDR_B1]][[FINI_ADDR_B0]] 00000000
77 ## Check that DT_FINI_ARRAY has static relocation applied for __bolt_runtime_fini
78 # CHECK-NO-PIE-NO-FINI: Dynamic section at offset {{.*}} contains {{.*}} entries:
79 # CHECK-NO-PIE-NO-FINI-NOT: (FINI)
80 # CHECK-NO-PIE-NO-FINI: (FINI_ARRAY) 0x[[FINI_ARRAY:[a-f0-9]+]]
81 # CHECK-NO-PIE-NO-FINI: Symbol table '.symtab' contains {{.*}} entries:
82 ## Read address bytes separately so we can reverse them later
83 # CHECK-NO-PIE-NO-FINI: {{0+}}[[FINI_ADDR_B0:[[:xdigit:]]{2}]][[FINI_ADDR_B1:[[:xdigit:]]{2}]][[FINI_ADDR_B2:[[:xdigit:]]{2}]][[FINI_ADDR_B3:[[:xdigit:]]{2}]] {{.*}} __bolt_runtime_fini
84 # CHECK-NO-PIE-NO-FINI: Hex dump of section '.fini_array':
85 # CHECK-NO-PIE-NO-FINI: 0x{{0+}}[[FINI_ARRAY]] [[FINI_ADDR_B3]][[FINI_ADDR_B2]][[FINI_ADDR_B1]][[FINI_ADDR_B0]] 00000000
88 .type _start, %function
90 # Dummy relocation to force relocation mode.
91 .reloc 0, R_AARCH64_NONE
93 .size _start, .-_start
96 .type _fini, %function
101 .section .fini_array,"aw"