Skip to content

Commit 0a64367

Browse files
wzssyqaYunQiang Su
andauthored
Sanitizer/MIPS: Use $t9 for preemptible function call (#76894)
Currently, almost all of the shared libraries of MIPS, rely on $t9 to get the address of current function, instead of PCREL instructions, even on MIPSr6. So we have to set $t9 properly. To get the address of preemptible function, we need the help of GOT. MIPS/O32 has .cpload, which can help to generate 3 instructions to get GOT. For __mips64, we can get GOT by: lui $t8, %hi(%neg(%gp_rel(SANITIZER_STRINGIFY(TRAMPOLINE(func))))) daddu $t8, $t8, $t9 daddiu $t8, $t8, %hi(%neg(%gp_rel(SANITIZER_STRINGIFY(TRAMPOLINE(func))))) And then get the address of __interceptor_func, and jump to it ld $t9, %got_disp(_interceptor" SANITIZER_STRINGIFY(func) ")($t8) jr $t9 Fixes #74047 Co-authored-by: YunQiang Su <[email protected]>
1 parent 8371cdc commit 0a64367

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

compiler-rt/lib/interception/interception.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ const interpose_substitution substitution_##func_name[] \
205205
ASM_TYPE_FUNCTION_STR "\n" \
206206
SANITIZER_STRINGIFY(TRAMPOLINE(func)) ":\n" \
207207
SANITIZER_STRINGIFY(CFI_STARTPROC) "\n" \
208-
SANITIZER_STRINGIFY(ASM_TAIL_CALL) " __interceptor_" \
209-
SANITIZER_STRINGIFY(ASM_PREEMPTIBLE_SYM(func)) "\n" \
208+
C_ASM_TAIL_CALL(SANITIZER_STRINGIFY(TRAMPOLINE(func)), \
209+
"__interceptor_" \
210+
SANITIZER_STRINGIFY(ASM_PREEMPTIBLE_SYM(func))) "\n" \
210211
SANITIZER_STRINGIFY(CFI_ENDPROC) "\n" \
211212
".size " SANITIZER_STRINGIFY(TRAMPOLINE(func)) ", " \
212213
".-" SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n" \

compiler-rt/lib/sanitizer_common/sanitizer_asm.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@
5353
# define ASM_TAIL_CALL tail
5454
#endif
5555

56+
// Currently, almost all of the shared libraries rely on the value of
57+
// $t9 to get the address of current function, instead of PCREL, even
58+
// on MIPSr6. To be compatiable with them, we have to set $t9 properly.
59+
// MIPS uses GOT to get the address of preemptible functions.
60+
#if defined(__mips64)
61+
# define C_ASM_TAIL_CALL(t_func, i_func) \
62+
"lui $t8, %hi(%neg(%gp_rel(" t_func ")))\n" \
63+
"daddu $t8, $t8, $t9\n" \
64+
"daddiu $t8, $t8, %lo(%neg(%gp_rel(" t_func ")))\n" \
65+
"ld $t9, %got_disp(" i_func ")($t8)\n" \
66+
"jr $t9\n"
67+
#elif defined(__mips__)
68+
# define C_ASM_TAIL_CALL(t_func, i_func) \
69+
".set noreorder\n" \
70+
".cpload $t9\n" \
71+
".set reorder\n" \
72+
"lw $t9, %got(" i_func ")($gp)\n" \
73+
"jr $t9\n"
74+
#elif defined(ASM_TAIL_CALL)
75+
# define C_ASM_TAIL_CALL(t_func, i_func) \
76+
SANITIZER_STRINGIFY(ASM_TAIL_CALL) " " i_func
77+
#endif
78+
5679
#if defined(__ELF__) && defined(__x86_64__) || defined(__i386__) || \
5780
defined(__riscv)
5881
# define ASM_PREEMPTIBLE_SYM(sym) sym@plt

0 commit comments

Comments
 (0)