From patchwork Wed Jan 17 09:37:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YunQiang Su X-Patchwork-Id: 84233 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EADE33858C30 for ; Wed, 17 Jan 2024 09:38:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EADE33858C30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705484315; bh=dVJO0IK62NKsrR6C5SRe8HpSRZxbCgRo01qo8pEIQ0I=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=WxJt0WMzAjDMs4zb/q/3UeDB7ctb7wx9Y9bNoJZpqyqstM6DeKLgD2VT/j1U5kYff X4T3nJzVFbebRQX8F0W8O372RzMI/EpGs6X8rrMNyr2q71D/zsuHQyDjEu1i/vjACX HO7E6tnizZKJmegDRKtgXaju+sxP36Mq73RDHRjA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by sourceware.org (Postfix) with ESMTPS id BC4223858C42 for ; Wed, 17 Jan 2024 09:37:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BC4223858C42 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BC4223858C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705484271; cv=none; b=d2RHIiLsyhZZaVnhudp5BhxlSAdUGFGX32z1GPHq610yddPNaUpfumrbdfJRmL1at0wJNADYcG72q680Ix7Kh9LElknSYVFpBx1Z7YcT9D0+Nz2/i6q2zG4CdKhxO/qZamVM2Opf4bRryYBVRmIKzrote+cReea4nvObOYA+KDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705484271; c=relaxed/simple; bh=naDCWkpPuNINcyOsE55Q4DCZ3xl8zypXEXUVx7W8Akc=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=h/MJb9mrXHlLdBX//kFEbjrLLp3HvGMDy2NnkJ7OD/GC2pohZSJLyBDPmRd5Bc8sQVv0Lv74mDGQk9LGooYAMEoqkH+zqT8hzQ/1T9RpteH00rhHdCm13h7eTew0RXEwliEBpOgkLMfpykYUKxgSkmHRrdOnZetDA2TU4N5i9xE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1d427518d52so79611205ad.0 for ; Wed, 17 Jan 2024 01:37:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705484268; x=1706089068; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dVJO0IK62NKsrR6C5SRe8HpSRZxbCgRo01qo8pEIQ0I=; b=pmABzYoGp/ajK4p/JpPG3j8UXFRwga9GZ+PknNpIqKZS2QorWPM4Ob97q8FWEuLzeD gBFvQxU6iS8WMyP812ic6QqIcskFvayG9vfLvH/PXj8A1v9B2L1HRwmWwJauKIeTyhzn u4PNW7wxDJsQg/UXQ4Rd4ajacUYpwYqcJh/KwIiei8ml2ozM0w0I+Xn8AlB3/jcBZpxM 5Z1LzPdM1vd/51B3BOYWi+7bkO8Fub/UmNdDCZW1ONcUBZHYcxWkxGKxpKw9DqyrWITL Q9ikCmzXIcT1vCRgH84qdodbK/WcVBxSdhNzmAeWTySm6z/MaTFjsAjvvr2nJ18FKFJt TqKQ== X-Gm-Message-State: AOJu0YxCYr3Bp6Iew+2qUGIlH5bVW0MqPHUCCzaoa1fOCIG8Vv0+ak17 /7nKED8oJe+fUgsmOynBBra8vmbE3Ao= X-Google-Smtp-Source: AGHT+IFtUxKCYNNQ1sGuaI233f90Jz8PmB0o5tU/Zkf9hNFOdJ8UQOIt8tt2yksO8VKqw515+O6Hgg== X-Received: by 2002:a17:903:278f:b0:1d3:b609:eb0b with SMTP id jw15-20020a170903278f00b001d3b609eb0bmr749537plb.27.1705484267983; Wed, 17 Jan 2024 01:37:47 -0800 (PST) Received: from localhost.localdomain ([149.248.38.156]) by smtp.gmail.com with ESMTPSA id v18-20020a170902d09200b001d5e88b31e5sm2515691plv.203.2024.01.17.01.37.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 01:37:47 -0800 (PST) From: YunQiang Su To: gcc-patches@gcc.gnu.org Cc: YunQiang Su Subject: [commit] Sanitizer/MIPS: Use $t9 for preemptible function call Date: Wed, 17 Jan 2024 17:37:40 +0800 Message-Id: <20240117093740.2784008-1-syq@gcc.gnu.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org From: YunQiang Su 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 Upstream-Commit: 0a64367a72f1634321f5051221f05f2f364bd882 libsanitizer * interception/interception.h (substitution_##func_name): Use macro C_ASM_TAIL_CALL. * sanitizer_common/sanitizer_asm.h: Define C_ASM_TAIL_CALL for MIPS with help of t9. --- libsanitizer/interception/interception.h | 5 ++-- libsanitizer/sanitizer_common/sanitizer_asm.h | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/libsanitizer/interception/interception.h b/libsanitizer/interception/interception.h index 9d8b60b2eef..58e969378a9 100644 --- a/libsanitizer/interception/interception.h +++ b/libsanitizer/interception/interception.h @@ -205,8 +205,9 @@ const interpose_substitution substitution_##func_name[] \ ASM_TYPE_FUNCTION_STR "\n" \ SANITIZER_STRINGIFY(TRAMPOLINE(func)) ":\n" \ SANITIZER_STRINGIFY(CFI_STARTPROC) "\n" \ - SANITIZER_STRINGIFY(ASM_TAIL_CALL) " __interceptor_" \ - SANITIZER_STRINGIFY(ASM_PREEMPTIBLE_SYM(func)) "\n" \ + C_ASM_TAIL_CALL(SANITIZER_STRINGIFY(TRAMPOLINE(func)), \ + "__interceptor_" \ + SANITIZER_STRINGIFY(ASM_PREEMPTIBLE_SYM(func))) "\n" \ SANITIZER_STRINGIFY(CFI_ENDPROC) "\n" \ ".size " SANITIZER_STRINGIFY(TRAMPOLINE(func)) ", " \ ".-" SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n" \ diff --git a/libsanitizer/sanitizer_common/sanitizer_asm.h b/libsanitizer/sanitizer_common/sanitizer_asm.h index bbb18cfbdf1..3af66a4e449 100644 --- a/libsanitizer/sanitizer_common/sanitizer_asm.h +++ b/libsanitizer/sanitizer_common/sanitizer_asm.h @@ -53,6 +53,29 @@ # define ASM_TAIL_CALL tail #endif +// Currently, almost all of the shared libraries rely on the value of +// $t9 to get the address of current function, instead of PCREL, even +// on MIPSr6. To be compatiable with them, we have to set $t9 properly. +// MIPS uses GOT to get the address of preemptible functions. +#if defined(__mips64) +# define C_ASM_TAIL_CALL(t_func, i_func) \ + "lui $t8, %hi(%neg(%gp_rel(" t_func ")))\n" \ + "daddu $t8, $t8, $t9\n" \ + "daddiu $t8, $t8, %lo(%neg(%gp_rel(" t_func ")))\n" \ + "ld $t9, %got_disp(" i_func ")($t8)\n" \ + "jr $t9\n" +#elif defined(__mips__) +# define C_ASM_TAIL_CALL(t_func, i_func) \ + ".set noreorder\n" \ + ".cpload $t9\n" \ + ".set reorder\n" \ + "lw $t9, %got(" i_func ")($gp)\n" \ + "jr $t9\n" +#elif defined(ASM_TAIL_CALL) +# define C_ASM_TAIL_CALL(t_func, i_func) \ + SANITIZER_STRINGIFY(ASM_TAIL_CALL) " " i_func +#endif + #if defined(__ELF__) && defined(__x86_64__) || defined(__i386__) || \ defined(__riscv) # define ASM_PREEMPTIBLE_SYM(sym) sym@plt