From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by sourceware.org (Postfix) with ESMTPS id A593D3857030 for ; Wed, 10 May 2023 09:17:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A593D3857030 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-x836.google.com with SMTP id d75a77b69052e-3ef4a74b42dso35467581cf.1 for ; Wed, 10 May 2023 02:17:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683710222; x=1686302222; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=J3bjEYXrat8UAVYkXMahq66HQVSp7C9cDL2FOYCWhaU=; b=V2nJiBFpjacIOFJ3JyCJpPPapzZwBg0HtbVXUJ4GCEpcGxjWx2YLEQEsGp6OKDCa3l lyfRq1MF7QQo1orv4W2aMq1fvDVYUSXpL1+yKSbdK+z2oeE1MsZGxmVudtU0EtvCTSj/ n+PknIc1VQOeav0dNTxEcF2ULF9qnmpBAWOUl0euhHSdsqofRhrakfctqquIKaprlq9X x2Rb1WWfuoUCWGQ6lB9q6IZRAyEYWFwe9iBzq04t90RVD9pv5E5uMtq1uYyZ1tAXkkBt dsjdFgXCMjtIOBYTFpzqHXj3CnXq06a9h7dZDK69N7vJJx5G8A5n5lON7oBP/AT3IW16 dHtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683710222; x=1686302222; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J3bjEYXrat8UAVYkXMahq66HQVSp7C9cDL2FOYCWhaU=; b=LXjVYHM8nHY9gvt8U0iJt3AsmeiqAJEjjRL9Pf99cNeqoHVaYOWAmMin/xdSdfYWTq 4gY1eDDd2NrXv0+Ud/nGfdSYsxFABhZv+dGt/9nJxUAQcNLue/YgKfiF2kcZwkq05gUu FWzyWGQjcw6lyQOzQ7/cHoaEmSiZpzkTLgh9gEBTwctFZeq4LkBhLGgu4UGZhuz8qMx9 tv4DaFegrXlrztd6Jr2/7/1FqDuLxnwF2lFS6ctFdXj0PfBz03ENkus7wCaCYed5KT1k ukgCit1nYVY/U9AXSPIaY5eG6BtfXgMh7xIRnBx5QWcy8LGwlqLzBNgo31jDF76vR6k7 irfA== X-Gm-Message-State: AC+VfDyCLBnri3XbTw5/KybqkN1TAycFGtZonNnzUrVfJfKtolmfOmO5 FI32iHUJs/uJvU7IUFgfvN7JahLEQMeSC9nE4gg= X-Google-Smtp-Source: ACHHUZ7nyCbbGmdnKHA3DOFcErL6XZ/1fUWYB6e4c+mjnj4CDzTCMYUrMikgNOXIrBVEEcKt4HsS8rgnjn6Utwzn6sI= X-Received: by 2002:a05:6214:226a:b0:5e7:4839:3c2a with SMTP id gs10-20020a056214226a00b005e748393c2amr28376307qvb.21.1683710221854; Wed, 10 May 2023 02:17:01 -0700 (PDT) MIME-Version: 1.0 References: <20230509085835.1143661-1-ardb@kernel.org> In-Reply-To: <20230509085835.1143661-1-ardb@kernel.org> From: Uros Bizjak Date: Wed, 10 May 2023 11:16:50 +0200 Message-ID: Subject: Re: [PATCH] i386: Honour -mdirect-extern-access when calling __fentry__ To: Ard Biesheuvel Cc: gcc-patches@gcc.gnu.org, keescook@chromium.org, "H . J . Lu" , Jakub Jelinek , Richard Biener , Hou Wenlong Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,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 List-Id: On Tue, May 9, 2023 at 10:58=E2=80=AFAM Ard Biesheuvel wr= ote: > > 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__() HJ, is the patch OK with you? Uros. > > Cc: H.J. Lu > Cc: Jakub Jelinek > Cc: Richard Biener > Cc: Uros Bizjak > Cc: Hou Wenlong > --- > 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 A= TTRIBUTE_UNUSED) > break; > case CM_SMALL_PIC: > case CM_MEDIUM_PIC: > - fprintf (file, "1:\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_na= me); > - break; > + if (!ix86_direct_extern_access) > + { > + fprintf (file, "1:\tcall\t*%s@GOTPCREL(%%rip)\n", mcoun= t_name); > + break; > + } > + /* fall through */ > default: > x86_print_call_or_nop (file, mcount_name); > break; > -- > 2.39.2 >