From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id E05B13858CDB for ; Sat, 13 May 2023 00:05:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E05B13858CDB Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=google.com Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1ac65ab7432so571715ad.0 for ; Fri, 12 May 2023 17:05:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683936307; x=1686528307; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=AJyA2KRcv/+OeExlabua2PnqqARcJrVAN3eNwfTIt4w=; b=uRm6wd3rEV9NS08yxrQl65G3bN7LKmNsWC2Yn4KXk9uDiWnMH3aTownjiFYWaL/d8q kQDmCrMh0OPxXj1DbyWL/mJNxbrZg6qE6JpsYpJFJPuQX3bbeKRCxHM/PsxAtz9jzkUG 8FoKFEIUUlh383hTGRfnJGh+/4y3nBSJLDJdHvor3LoJp8BWT0XsmlRAvEAqRijZ2s/B o7D69VpPiYUrATBZW73Gy33enSF0zKUN/XubmEE68xZVNaKwJbe69hKxBXb83UhIuHw5 cPOpNy9H26Tu+tfR0UEyYytC0PMTiyLAWf781euyaeKoMHvv0h1F952F5cj9W485ptEJ ccdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683936307; x=1686528307; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=AJyA2KRcv/+OeExlabua2PnqqARcJrVAN3eNwfTIt4w=; b=YvNEfd7A9F45wMyX1q+ZMlD07BENBFFSO7VfkgZSIFfJ8GBqHEsQqMYRRCidIFGTfG rcjWgR3f0lLqn3k6LE1MQgqaohKXNAtnobTQavHbA7Wb4gVCXGdG3wvHnky1LG+lIrDP DNW0NxMcbXRVV/GgMuXrCmf63V+IfeRadVAUFMOPModZzcuI0BEbVTvmzMK2/8mhESmX ld+xFic5HTiMaH182ZXDgdDmv9Se55xeen/y+Udmmf+aC3yWbedcXT4g5n3QxcD/uyr5 Ff3fmEa5G11ZH5zeMhmoI90WI76GKACCWBXiPSJFBgTQxt9vu96KFhm6f84LrqYBZjIq sG9w== X-Gm-Message-State: AC+VfDzmZEGPB0ayRHl9P0Ph9pJuC1Msz1Ia0YhrCex0OYFco6IrsYYN IxD1dMrLltnKxvbPnDifA8pX6WCPQL5+9B63RtOs8w== X-Google-Smtp-Source: ACHHUZ67U3Ml2yLTAKgb9SkzdetlOENGfY/ERgQggOFuaMQqpSWhibmSqGvGD8CMf5xL463lfrt7Ug== X-Received: by 2002:a17:902:ea12:b0:1a9:bb1d:64e with SMTP id s18-20020a170902ea1200b001a9bb1d064emr335705plg.15.1683936306651; Fri, 12 May 2023 17:05:06 -0700 (PDT) Received: from google.com (25.11.145.34.bc.googleusercontent.com. [34.145.11.25]) by smtp.gmail.com with ESMTPSA id i2-20020a170902c94200b001a988a71617sm8555532pla.192.2023.05.12.17.05.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 May 2023 17:05:06 -0700 (PDT) Date: Sat, 13 May 2023 00:05:02 +0000 From: Fangrui Song To: Palmer Dabbelt Cc: fweimer@redhat.com, l.stelmach@samsung.com, libc-alpha@sourceware.org, schwab@suse.de, adhemerval.zanella@linaro.org, joseph@codesourcery.com, binutils@sourceware.org, m.pikula@partner.samsung.com, m.szyprowski@samsung.com, k.lewandowsk@samsung.com, jeffreyalaw@gmail.com Subject: Re: global pointer gets overwritten with dlopen(3) on RISC-V Message-ID: <20230513000502.dui6zvz43se3jhlf@google.com> References: <20230512223421.zvvbi374qx6kazel@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-19.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH,KAM_INFOUSMEBIZ,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=no 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 2023-05-12, Palmer Dabbelt wrote: >On Fri, 12 May 2023 15:34:21 PDT (-0700), maskray@google.com wrote: >>On 2023-05-12, Palmer Dabbelt wrote: >>>On Fri, 12 May 2023 14:09:08 PDT (-0700), maskray@google.com wrote: >>>>On 2023-05-12, Palmer Dabbelt wrote: >>>>>On Fri, 12 May 2023 13:11:43 PDT (-0700), fweimer@redhat.com wrote: >>>>>>* Fangrui Song: >>>>>> >>>>>>>>1. Make -mno-relax the default for ld(1) (on Linux?). We have no >>>>>>>>benchmarks whatsoever, but global variables aren't very popular in >>>>>>>>application code these days and the gp register allows access to a >>>>>>>>single memory page (4kB) only. No big deal really. >>>>>>> >>>>>>>I do agree that --no-relax-gp is a sensible default choice for GNU ld. >>>>>>>https://maskray.me/blog/2021-03-14-the-dark-side-of-riscv-linker-relaxation#global-pointer-relaxation >>>>>>> >>>>>>>Perhaps you can start a separate topic on binutils? :) >>>>>>> >>>>>>>According to a doc from SiFive about -static -mcpu=sifive-u74 builds, >>>>>>>https://docs.google.com/spreadsheets/d/14V7cPbyc80AcGHzsMaw9hYb232dzRbGCmTApnxj-SpU/edit#gid=0 >>>>>>>global pointer relaxation saves at best 0.5% size (I guess that refers >>>>>>>to .text. If we count all allocable sections, the percentage is likely >>>>>>>even smaller.) >>>>>> >>>>>>For a mature toolchain, 0.5% in code size reduction would be *a lot*, >>>>>>so I wouldn't dismiss that. >>>>> >>>>>That's broadly speaking why it sticks around. We've got a bunch of >>>>>headaches related to relaxation, GP or otherwise, but they improve >>>>>performance and nobody's figured out how to replace that yet. >>>>> >>>>>>Do we have a reproducer? Is the issue actually about gp relaxation for >>>>>>the main executable? >>>>> >>>>>In general we don't reference GP from shared libraries as we don't >>>>>have a GP save/restore scheme. There may be a bug floating around >>>>>here somewhere, in which case we should fix it, but the original post >>>>>sounds like it wasn't a supported use case. >>>>> >>>>>>Thanks, >>>>>>Florian >>>> >>>>Global pointer relaxation only applies to +-2KiB data relative to __global_pointer$ (= .sdata + 0x800). >>>>The area that potentially benefits global pointer relaxation is very small. >>>> >>>>0.5% code size reduction (relative to .text?) is the best case. I >>>>suspect the program somehow has a lot global variable accesses and >>>>placing these variables in .sdata helps. >>>> >>>>I've got results from Yingwei Zheng at PLCT lab using many >>>>configurations. The saving is like 0.1%. >>>>https://docs.google.com/spreadsheets/d/1Gz0h-C4U0toa9qELFtRaEWT_CzauE5JD9xMsLR8RyK8/edit#gid=1721258109 >>>> >>>>On the binutils side, we occasionally see patches to fix global pointer >>>>relaxation bugs, e.g. the patch just sent a few hours ago: >>>>https://sourceware.org/pipermail/binutils/2023-May/127413.html >>>> >>>>I do not know the embedded toolchain well, but for Linux desktop/server, >>>>disabling global pointer relaxation seems like a sensible choice. If we >>>>discover a better way to utilize GP (x3) in the future, disabling global >>>>pointer relaxation today will result in fewer compatibility issues. >>> >>>This comes up all the time, you're just pushing for a backdoor ABI >>>break. I get the desire to remove GP, if we were to be able to redo >>>things I'd also not have it, but it's in the ABI and we can't change >>>the binaries that exist. >>> >>>If you want a GP-free ABI then you should just go write one up. Then >>>it'll become a distro problem, and if it turns out that users also >>>don't want in then the GP ABI will rot and we can eventually deprecate >>>it. >> >>I am advocating for a change in GNU ld to make --no-relax-gp the default >>option, but I am not sure it can be considered an ABI break. >> >>When using ld --no-relax-gp, the conversion of code sequences to gp is >>disabled, thus eliminating an assumption related to global pointer relaxation. >>If an executable is relinked without global pointer relaxation, it should still >>function properly. >> >>To the best of my knowledge, there is no official documentation that designates >>linker relaxation as a mandatory feature. Relaxation schemes, including global >>pointer relaxation, are optional. Making an optional feature opt-in does not >>constitute an ABI break. >> >>glibc continues to initialize gp to __global_pointer$ to accommodate users who >>opt-in for global pointer relaxation. Removing the initialization of gp would >>indeed be an ABI break, and I have never proposed such a change. > >The ABI break is using GP for something else, but that always ends up >being part of the argument for disabling GP-based relaxation. Without >that the argument ends up just being that the GP-relaxations are too >complicated for the benefit they provide. This has happened. Some platforms are investigating or using GP for software shadow call stack. I think they include Android and Fuchsia. I am not affiliated with them. >I certainly understand that argument, these (and for the rest of the >relaxation stuff) is a huge amount of pain for a small amount of >benefit (which seems to be mostly based on benchmark dragracing, which >is never that strong of an argument). That said, last time we had >this discussion was only a few months ago. I don't think anything has >changed since then so I doubt things well go any differently. The rest of the relaxation mechanism does offer significant savings that should not be overlooked. I have heard claims of up to a 10% .text reduction in certain embedded systems. However, it is important to acknowledge the costs associated with increased toolchain complexity and bloating of debug information, LSDA , and custom metadata sections. I discussed these concerns in detail at https://maskray.me/blog/2021-03-14-the-dark-side-of-riscv-linker-relaxation (Apologies if the title came across as derogatory. It was never my intention, and I genuinely enjoyed delving into the intricacies of this toolchain technology.) >Just turning GP-based relaxation off by default doesn't get rid of the >complexity, they're still in the ABI and people will still use them >(even if just for benchmark dragracing) so they'll still need to work. Regarding the ABI story, shared objects do not utilize global pointer relaxation. Therefore, if only an executable requires GP for a platform-specific purpose, I believe there is no ABI risk involved. The risk arises when shared objects rely on the executable's initialized GP for certain purposes. ABI breaks related to global pointer usage occur when a platform initially ships something using GP and then switches to a different GP usage. As I mentioned earlier, many non-Linux/glibc platforms don't use global pointer relaxation from the beginning. Even within Linux/glibc, a new distribution may choose not to use global pointer relaxation initially. For these platforms, there would be no ABI break. Changing the default behavior does indeed have an impact. --no-relax-gp is a binutils 2.41 feature, which means that projects that discover improved ways to utilize GP in the future need to avoid --no-relax-gp to maintain compatibility with older GNU ld versions. They could use --no-relax with GNU ld versions, but that's a big hammer. >> >>>>Haiku >>>>(https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/298#issuecomment-1344724796), Android, and Fuchsia >>>>have mentioned that they don't use global pointer relaxation.