From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) by sourceware.org (Postfix) with ESMTPS id 2E7B93858D33 for ; Fri, 6 Jan 2023 22:42:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2E7B93858D33 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-oi1-x233.google.com with SMTP id n8so2305370oih.0 for ; Fri, 06 Jan 2023 14:42:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=hUzipG8T37LDi+EJR2TIhl4cV3oJ6PxGONxMwvRCTJo=; b=CtqZ20dj5tkBPjZH/g1OpX/eLNT/vwgRL4og6kR911l/Ms/5Nb0Wy/hej9B3X4MI7K 2hzQyaffeyQe+fB5zmf8jwPNyvrD822dDFu751wCGg0F5pH6TvjpSruFf8ghYPul3ews jA1UviIX2qO/FkEcaFYWmC+6FpXI7YUhFd6jQsb+W3I6Dq0tNgZryIGoiKTxAimRrZuL T/2seD1tP2Bnn/z36mJowNdHcCspObSoT5tknPOF084qbJCgdyCUFpjsXqIPftiax7gz mCPOTaRylzMZ7GYATR8ERLkv2CIOZs2v6X3pZfEznkw8sY95aWxYUjpAFGTD8La1/Jmd FpDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=hUzipG8T37LDi+EJR2TIhl4cV3oJ6PxGONxMwvRCTJo=; b=tDit9ENnD9xlYPZeNgBdDHBFfjciAOuZDRKwBhO61JkFPngjr73j8L0sp89pSWL+Rb PRLwYyDYpyTCcvVDBTdqw0AZxRuNSXrqU3oW34JlNgGsZUKyRzReaLDLYMJYiqC41/79 rSyHt29+wWN5k4tbgdwXbgdqSo/0At1y8KEISw+DC7Z2Umw2E5KFJ5yVG2ALML8P1KUL 25faXQSHHMu1mX5s3HxspDus5yepfTdr+J3AGm1VW4BQE+4vPmPYhr6N61KCg2vnn0+O sa8A04DyhUs3rQMtrmYczaX43OL+zAEzuRvF2eHRnMZGbevLj5vPXfRxy+n1BRvXvYGh XqHg== X-Gm-Message-State: AFqh2krcphi2gVeLtDtOv2KUGmLXB9757098/O/eVK4UQV9lAeG41BAt inWt2XSEQhJroD4SjiNNEPtc4U+sFZUh0Sv0VKg= X-Google-Smtp-Source: AMrXdXuEmUkY4v32nZ1p8REiDkjXE4AataxbI7glw4QjsmoYfDtHDL9ItA25UZDjPuRyyWlEN/YlDlZYISvK2GVtkko= X-Received: by 2002:a54:4596:0:b0:364:856:31a4 with SMTP id z22-20020a544596000000b00364085631a4mr364995oib.298.1673044948368; Fri, 06 Jan 2023 14:42:28 -0800 (PST) MIME-Version: 1.0 References: <20230105210542.3573076-1-maskray@google.com> In-Reply-To: From: "H.J. Lu" Date: Fri, 6 Jan 2023 14:41:52 -0800 Message-ID: Subject: Re: [PATCH] ld: Allow R_X86_64_GOTPCREL for call *__tls_get_addr@GOTPCREL(%rip) To: Fangrui Song Cc: binutils@sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3021.4 required=5.0 tests=BAYES_00,BODY_8BITS,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 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 Fri, Jan 6, 2023 at 1:44 PM Fangrui Song wrote: > > On Fri, Jan 6, 2023 at 1:27 PM H.J. Lu wrote: > > > > On Fri, Jan 6, 2023 at 1:25 PM Fangrui Song wrote: > > > > > > On Fri, Jan 6, 2023 at 1:14 PM H.J. Lu wrote: > > > > > > > > On Fri, Jan 6, 2023 at 10:48 AM Fangrui Song w= rote: > > > > > > > > > > On Fri, Jan 6, 2023 at 9:04 AM H.J. Lu wrot= e: > > > > > > > > > > > > On Thu, Jan 5, 2023 at 1:06 PM Fangrui Song via Binutils > > > > > > wrote: > > > > > > > > > > > > > > _Thread_local int a; > > > > > > > int main() { return a; } > > > > > > > > > > > > > > % gcc -fno-plt -fpic a.c -fuse-ld=3Dbfd -Wa,-mrelax-relocatio= ns=3Dno > > > > > > > /usr/bin/ld.bfd: /tmp/ccSSBgrg.o: TLS transition from R_X86_6= 4_TLSGD to R_X86_64_GOTTPOFF against `a' at 0xd in section `.text' failed > > > > > > > /usr/bin/ld.bfd: failed to set dynamic section sizes: bad val= ue > > > > > > > collect2: error: ld returned 1 exit status > > > > > > > > > > > > > > This commit fixes the issue. > > > > > > > > > > > > > > PR ld/24784 > > > > > > > * bfd/elf64-x86-64.c (elf_x86_64_check_tls_transition): A= llow > > > > > > > R_X86_64_GOTPCREL. > > > > > > > --- > > > > > > > bfd/elf64-x86-64.c | 2 +- > > > > > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > > > > > > > > > diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c > > > > > > > index 914f82d0151..095fe2e0fe6 100644 > > > > > > > --- a/bfd/elf64-x86-64.c > > > > > > > +++ b/bfd/elf64-x86-64.c > > > > > > > @@ -1241,7 +1241,7 @@ elf_x86_64_check_tls_transition (bfd *a= bfd, > > > > > > > if (largepic) > > > > > > > return r_type =3D=3D R_X86_64_PLTOFF64; > > > > > > > else if (indirect_call) > > > > > > > - return r_type =3D=3D R_X86_64_GOTPCRELX; > > > > > > > + return (r_type =3D=3D R_X86_64_GOTPCRELX || r_typ= e =3D=3D R_X86_64_GOTPCREL); > > > > > > > else > > > > > > > return (r_type =3D=3D R_X86_64_PC32 || r_type =3D= =3D R_X86_64_PLT32); > > > > > > > } > > > > > > > -- > > > > > > > 2.39.0.314.g84b9a713c41-goog > > > > > > > > > > > > > > > > > > > Since the new TLS sequence was added after R_X86_64_GOTPCRELX w= as > > > > > > required for call, R_X86_64_GOTPCREL should be invalid in this = TLS sequence. > > > > > > > > > > > > -- > > > > > > H.J. > > > > > > > > > > I have multiple arguments (albeit no single one is very strong) t= hat > > > > > this 1-deletion-1-addition change provides benefits for users (IM= HO > > > > > with no burden to binutils at all). > > > > > > > > > > Some projects may add -Wa,-mrelax-relocations=3Dno to work around= older > > > > > GNU ld. Then the project's toolchain requirement may increase and= no > > > > > longer need to work around older GNU ld. > > > > > But a distribution may for some reason use a global -fno-plt (e.g= . > > > > > Arch Linux) and then run into this TLS GD/LD->IE/LE optimization > > > > > issue. > > > > > > > > > > rust src/ci/docker/host-x86_64/*musl/Dockerfile > > > > > openjdk/jdk19u make/autoconf/flags-cflags.m4 (this file appears t= o be > > > > > copied into quite a few projects) > > > > > Linux kernel arch/x86/boot/compressed/Makefile (not a good exampl= e as > > > > > it doesn't use TLS AFAICT) > > > > > > > > > > R_X86_64_GOTPCREL isn't purely usefull. It may help linker design= : for > > > > > R_X86_64_GOTPCRELX/R_X86_64_REX_GOTPCRELX, the linker can make a > > > > > decision upfront whether a GOT entry is needed > > > > > (this affects the size of .got, which may affect section layout a= nd > > > > > whether other relocations may overflow). > > > > > This may increase risk of 32-bit relocation overflow. > > > > > R_X86_64_GOTPCREL can mitigate the risk while being aware to the = user. > > > > > > > > > > rustc somehow disables x86 relaxed relocations and defaults to `-= Z > > > > > > > > Why is that? > > > > > > It's assuredly a rust's problem and I am trying to fix that in > > > https://github.com/rust-lang/rust/pull/106511 > > > > > > The -Wa,-mrelax-relocations=3Dno problem may affect more packages. > > > > -mrelax-relocations=3Dno should be a workaround for the older linker. = It > > shouldn't be used with the current linker. > > A project may choose to work with many linker versions. > For simplicity, before it decides to drop compatibility with GNU > ld<2.26 (AIUI GOTPCRELX was supported in 2.26), > it may unconditionally add -Wa,-mrelax-relocations=3Dno, instead of -mrelax-relocations=3Dno is only supported with the newer binutils. > doing configure work to check linker support. The TLS sequence from -fno-plt doesn't work for the older linker. The older linker support should be dropped for -fno-plt. > Now a user may use -fno-plt (Arch Linux, rustc, maybe Alpine) and run > into the aforementioned TLS problem. > > This 1-deletion-1-addition change can address this issue with no > maintenance burden on binutils side in my opinion, > so I made this patch. > > The linker design I described is true as well. Whether GOTPCRELX leads > to a GOT entry can be decided at relocation scanning time, before the > section layout is decided. > Users may make a conscious decision to use GOTPCREL to avoid potential > relocation overflow risk. > > GOTPCREL isn't really dead. It can be used with Intel LAM and tagged > global variables (with non-zero high address bits) > https://reviews.llvm.org/D111343 > GOTPCREL instead of GOTPCRELX makes it clear an instruction > referencing the variable isn't supposed to be relaxed. The address of the local symbol, foo, in movq foo@GOTPCREL(%rip), %rax is assigned by the linker. I am not sure how the tag is involved here. Besides, it is the call instruction here. > > > > > plt=3Dno` and now relies on llvm-project to work around the GNU l= d > > > > > compatibility issue. > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > =E5=AE=8B=E6=96=B9=E7=9D=BF > > > > > > > > > > > > > > > > -- > > > > H.J. > > > > > > > > > > > > -- > > > =E5=AE=8B=E6=96=B9=E7=9D=BF > > > > > > > > -- > > H.J. > > > > -- > =E5=AE=8B=E6=96=B9=E7=9D=BF --=20 H.J.