Skip to content

Commit 1adca7a

Browse files
Reapply "[XRay][AArch64] Support -fxray-shared (#114431)" (#115300)
This patch implements support for `-fxray-shared` on AArch64 and fixes a remaining issue in the previous PR #114431. A bug in the XRay `CMakeLists.txt` caused the XRay assembly sources to be built for every architecture in `XRAY_DSO_SUPPORTED_ARCH` on Apple. This led to the compiler trying to compile AArch64 assembly for X86 targets and vice versa. This is addressed here by ensuring that assembly sources are only built for the matching architecture (see fixup commit). **Original PR description:** This patch adds support for `-fxray-shared` on AArch64. This feature, introduced in #113548 for x86_64, enables the instrumentation of shared libraries with XRay. Changes: - Adds AArch64 to the list of targets supporting `-fxray-shared` - Introduces PIC versions of the AArch64 XRay trampolines - Adjusts relevant XRay tests
1 parent c17a914 commit 1adca7a

File tree

11 files changed

+48
-30
lines changed

11 files changed

+48
-30
lines changed

clang/lib/Driver/XRayArgs.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
6868
false)) {
6969
XRayShared = true;
7070

71-
// DSO instrumentation is currently limited to x86_64
72-
if (Triple.getArch() != llvm::Triple::x86_64) {
71+
// Certain targets support DSO instrumentation
72+
switch (Triple.getArch()) {
73+
case llvm::Triple::aarch64:
74+
case llvm::Triple::x86_64:
75+
break;
76+
default:
7377
D.Diag(diag::err_drv_unsupported_opt_for_target)
7478
<< "-fxray-shared" << Triple.str();
7579
}

clang/test/Driver/XRay/xray-shared.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
// Check supported targets
12
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s
3+
// RUN: %clang -### --target=aarch64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s
4+
5+
// Check unsupported targets
6+
// RUN: not %clang -### --target=arm-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET
7+
// RUN: not %clang -### --target=mips-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET
8+
// RUN: not %clang -### --target=loongarch64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET
9+
// RUN: not %clang -### --target=hexagon-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET
10+
// RUN: not %clang -### --target=powerpc64le-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET
11+
12+
// Check PIC requirement
213
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fpic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s
314
// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-PIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC
415
// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-pic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC
5-
616
// On 64 bit darwin, PIC is always enabled
717
// RUN: %clang -### --target=x86_64-apple-darwin -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s
818

9-
// Check unsupported targets
10-
// RUN: not %clang -### --target=aarch64-pc-freebsd -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET
11-
// RUN: not %clang -### --target=arm64-apple-macos -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET
12-
1319
// CHECK: "-cc1" {{.*}}"-fxray-instrument" {{.*}}"-fxray-shared"
1420
// ERR-TARGET: error: unsupported option '-fxray-shared' for target
1521
// ERR-PIC: error: option '-fxray-shared' cannot be specified without '-fPIC'

compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ else()
104104
set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM32} ${ARM64} ${MIPS32} ${MIPS64}
105105
powerpc64le ${HEXAGON} ${LOONGARCH64})
106106
endif()
107-
set(ALL_XRAY_DSO_SUPPORTED_ARCH ${X86_64})
107+
set(ALL_XRAY_DSO_SUPPORTED_ARCH ${X86_64} ${ARM64})
108108
set(ALL_SHADOWCALLSTACK_SUPPORTED_ARCH ${ARM64})
109109

110110
if (UNIX)

compiler-rt/lib/xray/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ set(aarch64_SOURCES
5656
xray_trampoline_AArch64.S
5757
)
5858

59+
set(aarch64_DSO_SOURCES
60+
xray_trampoline_AArch64.S
61+
)
62+
5963
set(loongarch64_SOURCES
6064
xray_loongarch64.cpp
6165
xray_trampoline_loongarch64.S
@@ -241,7 +245,7 @@ if (APPLE)
241245
if (${arch} IN_LIST XRAY_DSO_SUPPORTED_ARCH)
242246
add_compiler_rt_object_libraries(RTXrayDSO_${arch}
243247
OS ${XRAY_SUPPORTED_OS}
244-
ARCHS ${XRAY_DSO_SUPPORTED_ARCH}
248+
ARCHS ${arch}
245249
SOURCES ${${arch}_DSO_SOURCES}
246250
ADDITIONAL_HEADERS ${XRAY_IMPL_HEADERS}
247251
CFLAGS ${XRAY_CFLAGS}
@@ -403,7 +407,6 @@ else() # not Apple
403407
PARENT_TARGET xray)
404408

405409
if (${arch} IN_LIST XRAY_DSO_SUPPORTED_ARCH)
406-
# TODO: Only implemented for X86 at the moment
407410
add_compiler_rt_object_libraries(RTXrayDSO
408411
ARCHS ${arch}
409412
SOURCES ${XRAY_DSO_SOURCES} ${${arch}_DSO_SOURCES}

compiler-rt/lib/xray/xray_trampoline_AArch64.S

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
ldp x1, x2, [sp], #16
2727
.endm
2828

29+
.macro LOAD_HANDLER_ADDR reg handler
30+
#if !defined(XRAY_PIC)
31+
adrp \reg, ASM_SYMBOL(\handler)
32+
ldr \reg, [\reg, :lo12:ASM_SYMBOL(\handler)]
33+
#else
34+
adrp \reg, :got:ASM_SYMBOL(\handler)
35+
ldr \reg, [\reg, :got_lo12:ASM_SYMBOL(\handler)]
36+
ldr \reg, [\reg]
37+
#endif
38+
.endm
39+
2940
.text
3041
.p2align 2
3142
.global ASM_SYMBOL(__xray_FunctionEntry)
@@ -42,8 +53,7 @@ ASM_SYMBOL(__xray_FunctionEntry):
4253
SAVE_REGISTERS
4354

4455
// Load the handler function pointer.
45-
adrp x2, ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)
46-
ldr x2, [x2, #:lo12:ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)]
56+
LOAD_HANDLER_ADDR x2, _ZN6__xray19XRayPatchedFunctionE
4757
cbz x2, 1f
4858
// Set w0 to the function ID (w17). Set x1 to XRayEntryType::ENTRY = 0.
4959
mov w0, w17
@@ -69,8 +79,7 @@ ASM_SYMBOL(__xray_FunctionExit):
6979
SAVE_REGISTERS
7080

7181
// Load the handler function pointer into x2.
72-
adrp x2, ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)
73-
ldr x2, [x2, #:lo12:ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)]
82+
LOAD_HANDLER_ADDR x2, _ZN6__xray19XRayPatchedFunctionE
7483
cbz x2, 1f
7584
// Set w0 to the function ID (w17). Set x1 to XRayEntryType::EXIT = 1.
7685
mov w0, w17
@@ -96,8 +105,7 @@ ASM_SYMBOL(__xray_FunctionTailExit):
96105
// Save the registers which may be modified by the handler function.
97106
SAVE_REGISTERS
98107
// Load the handler function pointer into x2.
99-
adrp x2, ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)
100-
ldr x2, [x2, #:lo12:ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)]
108+
LOAD_HANDLER_ADDR x2, _ZN6__xray19XRayPatchedFunctionE
101109
cbz x2, 1f
102110
// Set w0 to the function ID (w17). Set x1 to XRayEntryType::TAIL = 2.
103111
mov w0, w17
@@ -118,13 +126,11 @@ ASM_SYMBOL(__xray_ArgLoggerEntry):
118126
// Push the registers which may be modified by the handler function.
119127
SAVE_REGISTERS
120128

121-
adrp x8, ASM_SYMBOL(_ZN6__xray13XRayArgLoggerE)
122-
ldr x8, [x8, #:lo12:ASM_SYMBOL(_ZN6__xray13XRayArgLoggerE)]
129+
LOAD_HANDLER_ADDR x8, _ZN6__xray13XRayArgLoggerE
123130
cbnz x8, 2f
124131

125132
// Load the handler function pointer.
126-
adrp x8, ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)
127-
ldr x8, [x8, #:lo12:ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)]
133+
LOAD_HANDLER_ADDR x8, _ZN6__xray19XRayPatchedFunctionE
128134
cbz x8, 1f
129135

130136
2:
@@ -144,8 +150,7 @@ ASM_SIZE(__xray_ArgLoggerEntry)
144150
ASM_TYPE_FUNCTION(__xray_CustomEvent)
145151
ASM_SYMBOL(__xray_CustomEvent):
146152
SAVE_REGISTERS
147-
adrp x8, ASM_SYMBOL(_ZN6__xray22XRayPatchedCustomEventE)
148-
ldr x8, [x8, #:lo12:ASM_SYMBOL(_ZN6__xray22XRayPatchedCustomEventE)]
153+
LOAD_HANDLER_ADDR x8, _ZN6__xray22XRayPatchedCustomEventE
149154
cbz x8, 1f
150155
blr x8
151156
1:
@@ -157,8 +162,7 @@ ASM_SIZE(__xray_CustomEvent)
157162
ASM_TYPE_FUNCTION(__xray_TypedEvent)
158163
ASM_SYMBOL(__xray_TypedEvent):
159164
SAVE_REGISTERS
160-
adrp x8, ASM_SYMBOL(_ZN6__xray21XRayPatchedTypedEventE)
161-
ldr x8, [x8, #:lo12:ASM_SYMBOL(_ZN6__xray21XRayPatchedTypedEventE)]
165+
LOAD_HANDLER_ADDR x8, _ZN6__xray21XRayPatchedTypedEventE
162166
cbz x8, 1f
163167
blr x8
164168
1:

compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// RUN: %llvm_xray account --format=csv --sort=funcid "`ls basic-mode-dso-* | head -1`" | FileCheck --check-prefix=ACCOUNT %s
99
// RUN: rm basic-mode-dso-*
1010

11-
// REQUIRES: target=x86_64{{.*}}
11+
// REQUIRES: target={{(aarch64|x86_64)-.*}}
12+
// REQUIRES: built-in-llvm-tree
1213

1314
//--- main.cpp
1415

compiler-rt/test/xray/TestCases/Posix/clang-xray-shared.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// RUN: %clangxx -fxray-instrument %s -shared -o %t.so
77
// RUN: llvm-nm %t.so | FileCheck %s --check-prefix DISABLED
88
//
9-
// REQUIRES: target=x86_64{{.*}}
9+
// REQUIRES: target={{(aarch64|x86_64)-.*}}
1010

1111
[[clang::xray_always_instrument]] int always_instrumented() { return 42; }
1212

compiler-rt/test/xray/TestCases/Posix/dlopen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88
// RUN: XRAY_OPTIONS="patch_premain=true" %run %t/main.o %t/testlib.so 2>&1 | FileCheck %s
99

10-
// REQUIRES: target=x86_64{{.*}}
10+
// REQUIRES: target={{(aarch64|x86_64)-.*}}
1111

1212
//--- main.cpp
1313

compiler-rt/test/xray/TestCases/Posix/dso-dep-chains.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//
1818
// RUN: XRAY_OPTIONS="patch_premain=true" %run %t/main.o %t/testlibd.so %t/testlibe.so 2>&1 | FileCheck %s
1919

20-
// REQUIRES: target=x86_64{{.*}}
20+
// REQUIRES: target={{(aarch64|x86_64)-.*}}
2121

2222
//--- main.cpp
2323

compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// RUN: XRAY_OPTIONS="patch_premain=true,verbosity=1" %run %t/main.o 2>&1 | FileCheck %s
88

9-
// REQUIRES: target=x86_64{{.*}}
9+
// REQUIRES: target={{(aarch64|x86_64)-.*}}
1010

1111
//--- main.cpp
1212

compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// RUN: XRAY_OPTIONS="patch_premain=false" %run %t/main.o 2>&1 | FileCheck %s
1010

11-
// REQUIRES: target=x86_64{{.*}}
11+
// REQUIRES: target={{(aarch64|x86_64)-.*}}
1212

1313
//--- main.cpp
1414

0 commit comments

Comments
 (0)