From patchwork Tue May 9 08:58:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 91460 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2724837vqo; Tue, 9 May 2023 01:59:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6mOaJBZIp2eB8DdIFeDyA/ZOn/eTCjWmDf41Oahx1ee5VhGGNnHj/GFcGLYx5PHB5khvrj X-Received: by 2002:a17:907:74c:b0:953:37eb:7727 with SMTP id xc12-20020a170907074c00b0095337eb7727mr12707077ejb.43.1683622780614; Tue, 09 May 2023 01:59:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683622780; cv=none; d=google.com; s=arc-20160816; b=PAZGMM2DZ8JIYlurzEOavf389axvhKqY6wPt5d48asChNRI0ESlpH5SjT1Sp7Ef7kB kbgkCcemKUWAXG3PcFpnNRa9Z9/KTTEuiwCPSti7QdcuIFJ398Q0lw2yz+UnowHFxmhX aHzojpbNpXhgjkcigIKJLsTvUc+1TS1wFZiuPOzIkWcAdhgD8la4oRIljO8x6o3709GE yVIV/va55hPAOGoL3QLq4fkAEcDRIq2rHdIcX7jLA5sBg97NwG+R5NcL1F0E3/a8zMNJ D/9eoSbd1gTNhHuCsWv/r/3iqPyKXIdsgE05+kBPVQFJ8Diz5Ao9xID41IbdVVvCDCgg LRLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=1uVgCGFG5yKewfxkRVpkNZE8baZEK4qebImHJhjDHwA=; b=qqNyk/l1tOqorCMGzODh/o/7uOLmQ3z9x2FBeO+e6qh+S+vp309FRG61JiGiwt/N2B JtoID+xIFVC3RKDTLNyv2em8aEIhSDlKexPubqmrLuI+loTyqmBuiet7/aH2BzYGnRRV HQ725IYuUDlomlVW+xlTQYIm49+RQs5ZoIYDIS/KsX4u2WTak4783WF2LZaxV5WOBZ+d Qo1tmdn8B8PDozWjkOO0ZIIQ/lErbOeXInO2GTlfRdBzatdwMPQr/gGjx5JYFrBbm+cB z8EqfZYO8XmJisyAVS12q9uNah796CWYuWn62xTBKVopgCvTPSM/NwfZl7Lp312LUmPh +1hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qTrgphFn; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hr34-20020a1709073fa200b00969ea55d647si1263530ejc.92.2023.05.09.01.59.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 01:59:40 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qTrgphFn; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4950F385770B for ; Tue, 9 May 2023 08:59:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4950F385770B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683622779; bh=1uVgCGFG5yKewfxkRVpkNZE8baZEK4qebImHJhjDHwA=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=qTrgphFnvIlM/juL/J+9Eb1DPFMepByTRe2QPvfMqnklrLkP603bqIfcEO8ifJmWW yzg7DKxU0p4VqiB+oDwLPE4b1n/hRHNVJaWVAd12C8R4OrqCVuI2mNiDaYFSOq8Lik O5xQJAzJHG37ovShrB7YSEUkDsWTXcptDvyTcQ+I= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by sourceware.org (Postfix) with ESMTPS id 8736B3858C53 for ; Tue, 9 May 2023 08:58:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8736B3858C53 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C601863269; Tue, 9 May 2023 08:58:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 39393C433EF; Tue, 9 May 2023 08:58:51 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: keescook@chromium.org, Ard Biesheuvel , "H . J . Lu" , Jakub Jelinek , Richard Biener , Uros Bizjak , Hou Wenlong Subject: [PATCH] i386: Honour -mdirect-extern-access when calling __fentry__ Date: Tue, 9 May 2023 10:58:35 +0200 Message-Id: <20230509085835.1143661-1-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Ard Biesheuvel via Gcc-patches From: Ard Biesheuvel Reply-To: Ard Biesheuvel Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765406440667496983?= X-GMAIL-MSGID: =?utf-8?q?1765406440667496983?= The small and medium PIC code models generate profiling calls that always load the address of __fentry__() via the GOT, even if -mdirect-extern-access is in effect. This deviates from the behavior with respect to other external references, and results in a longer opcode that relies on linker relaxation to eliminate the GOT load. In this particular case, the transformation replaces an indirect 'CALL *__fentry__@GOTPCREL(%rip)' with either 'CALL __fentry__; NOP' or 'NOP; CALL __fentry__', where the NOP is a 1 byte NOP that preserves the 6 byte length of the sequence. This is problematic for the Linux kernel, which generally relies on -mdirect-extern-access and hidden visibility to eliminate GOT based symbol references in code generated with -fpie/-fpic, without having to depend on linker relaxation. The Linux kernel relies on code patching to replace these opcodes with NOPs at runtime, and this is complicated code that we'd prefer not to complicate even more by adding support for patching both 5 and 6 byte sequences as well as parsing the instruction stream to decide which variant of CALL+NOP we are dealing with. So let's honour -mdirect-extern-access, and only load the address of __fentry__ via the GOT if direct references to external symbols are not permitted. Note that the GOT reference in question is in fact a data reference: we explicitly load the address of __fentry__ from the GOT, which amounts to eager binding, rather than emitting a PLT call that could bind eagerly, lazily or directly at link time. gcc/ChangeLog: * config/i386/i386.cc (x86_function_profiler): Take ix86_direct_extern_access into account when generating calls to __fentry__() Cc: H.J. Lu Cc: Jakub Jelinek Cc: Richard Biener Cc: Uros Bizjak Cc: Hou Wenlong Signed-off-by: Ard Biesheuvel --- gcc/config/i386/i386.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index b1d08ecdb3d44729..69b183abb4318b0a 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -21836,8 +21836,12 @@ x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED) break; case CM_SMALL_PIC: case CM_MEDIUM_PIC: - fprintf (file, "1:\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_name); - break; + if (!ix86_direct_extern_access) + { + fprintf (file, "1:\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_name); + break; + } + /* fall through */ default: x86_print_call_or_nop (file, mcount_name); break;