From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 542B13858CDB for ; Wed, 17 Jan 2024 08:56:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 542B13858CDB Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=google.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 542B13858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705481806; cv=none; b=r6mMpDa1DDgGmxnC6M7WgDq/z3NKVwdPPSu3AmKI7hKyn4n1YD0yYd2Ge+LbmsdRDrTt/sJtzh+G48NJbl77ly2JPSHm9QAHLbHxV8zYQgGwKsAWVPBjVMR8P3TM7efbLU5G4DvevmI0JAF/PxI9XYYf0K9LIWJC+wBKDz9MVG8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705481806; c=relaxed/simple; bh=hhBtWMaS3JPNhYmdiMoifsjwP16ZaeKhzYs05JTlz2g=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=p8tnQH4ehgDB5OWbMl1/M74Lhvh806zpszlhCb+AHv/LDT8L70uJoiu4E9htG20dKwDLib1nDXV61pQmSFm4UIasysfAhIoQ7aWWhEBCx/kLPbPJopgC3BP5DTs7Popldy7+gfVqhJYUEeF38K0vnWHdF+3msL77H0ZOSGplDfc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1d5ce88b51cso117735ad.0 for ; Wed, 17 Jan 2024 00:56:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705481803; x=1706086603; darn=sourceware.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=Y148La6i4bTi/EDttpIFPPJvicYtuhWXQrHn6PL4JYY=; b=l+rAr7OlXl0pk5NJngFWU3IDd6mFix65hYPFxM59/S8SqvWjajuH2IaG579jCArmp6 zTfFImO07vXqpVzNllWG98HucTpE0gIPfTQ/2FsXyF4wg7Q2oqaJ7aT8+n2Copqz373l fk9WsMyZIgLUguyjom1z6lhpQq9ZXC/OhC80lLtDudOC6mxIAwpzMTwSyoP2ggXBE42j gAMvKpPtKmUNwKd9xApRREz2dCOlIGsz8EJKa8NbW4eR+lrJ7XLzLPclEHX+nSL+bfli ye8dKpfXocV59VaFzZKdzxS5vAXv9KbUUe8uKDCKCdl9ppeO28pAsLVGzRsFIt14/DOl 7C4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705481803; x=1706086603; h=in-reply-to:content-transfer-encoding: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=Y148La6i4bTi/EDttpIFPPJvicYtuhWXQrHn6PL4JYY=; b=ibudJHUwd87am/SfNIqrsdAiox5UCf/UYhVyYEC46x0O6JyjziLqGJauosUgAT+tWk Kis/ltv/5RobOlgheNROk3N0nsgj75XXNcKK3yRG9QgDdUANfKeCgdnILPhzQpguDdzZ QNCCSuWpmwKOHB8P4dFd3efn+HmCL/UtS7OfIkiJDntpQT5L+vgglKfKgUsVBzH13MZv hIRjKIfhlwH/bNz6wK0/lgsz4MHDS3HcnrPJF8872dq34JLojVIQi7kByhqMWpCCwbG2 0GLUDjO16/QLgxY8ut4mnRuO1uvMal1MeWEq/kqp/1xMtFy574LMD7KPBfx87Aa7egoX YVRw== X-Gm-Message-State: AOJu0YzSbbwZxUlSeIWd3jaIgI9syVqcDn39+Azs95oDgqbNjfay2Z8s Hl7E+2wfQgnJGbJejG+ltlJP2hMp50vG/2y/gPp+lEnSYRUlKOemAieiBrZUFw== X-Google-Smtp-Source: AGHT+IEymiXMYAgjjS0z7YLlvhhbiWnAHWLEgk8hiQ4DfyrPz+0Z4hB0PPK7ObeaN7rF5DsoPpat6w== X-Received: by 2002:a17:902:c412:b0:1d4:55b3:45d9 with SMTP id k18-20020a170902c41200b001d455b345d9mr135745plk.6.1705481802987; Wed, 17 Jan 2024 00:56:42 -0800 (PST) Received: from google.com ([2620:15c:2d3:205:73e1:df02:f103:80c4]) by smtp.gmail.com with ESMTPSA id gx13-20020a056a001e0d00b006db6fc4c292sm909793pfb.49.2024.01.17.00.56.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 00:56:42 -0800 (PST) Date: Wed, 17 Jan 2024 00:56:38 -0800 From: Fangrui Song To: "H.J. Lu" , Matthew Wilcox Cc: Cristian =?utf-8?Q?Rodr=C3=ADguez?= , Sam James , Adhemerval Zanella Netto , Noah Goldstein , libc-alpha@sourceware.org Subject: Re: [PATCH v7] elf: Add ELF_DYNAMIC_AFTER_RELOC to rewrite PLT Message-ID: <20240117085638.rk43dm6bz6aldtto@google.com> References: <20240105041939.3707586-1-hjl.tools@gmail.com> <87o7dslz2q.fsf@gentoo.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-19.4 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 2024-01-11, H.J. Lu wrote: >On Thu, Jan 11, 2024 at 6:44 AM Cristian Rodríguez > wrote: >> >> >> >> On Thu, Jan 11, 2024 at 11:33 AM Sam James wrote: >>> >>> >>> >>> systemd also enables this for its own services too, etc. >> >> >> Yes, I wonder why I do not get any service failure since I enabled this tunable globally on my system..by using DefaultEnvironment= in system.conf > >A patch was sent to > >https://patchwork.sourceware.org/project/glibc/patch/20240111155849.8976-1-hjl.tools@gmail.com/ > >It fixed sound on Fedora 39. > >-- >H.J. I wonder whether this delicate micro-optimization (with many constraints) might lead to the loss of file-backed transparent huge pages. (I am suspecting, but I know very little about huge pages, so ...) +Matthew Wilcox I've jotted down some notes (https://maskray.me/blog/2023-02-19-linker-notes-on-x86#mark-plt). Copying them here: --- In 2023-09, GNU ld [introduced `-z mark-plt`](https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=832ca732b8a96ff9a3e7c4abf24098bf2a59a96d) to communicate information to rtld to rewrite eligible indirect jump instructions to direct jump instructions. * The linker adds dynamic tags `DT_X86_64_PLT/DT_X86_64_PLTSZ/DT_X86_64_PLTENT`. * The addend in `R_X86_64_JUMP_SLOT` relocations are adjusted to indicate the offset of the indirect jump instruction. Since 2024-01, if binutils is configured with `--enable-mark-plt`, `-z mark-plt` will be the default. In glibc, when `GLIBC_TUNABLES=glibc.cpu.plt_rewrite=1` or 2 is specified, lazy PLT binding is disabled, and an object file enables `DT_X86_64_PLT/DT_X86_64_PLTSZ/DT_X86_64_PLTENT` tags, glibc [rewrites eligible PLT entries](https://sourceware.org/git/?p=glibc.git;a=commit;h=848746e88ec2aa22e8dea25f2110e2b2c59c712e). After relocating an object file, `x86_64_dynamic_after_reloc` (due to the `ELF_DYNAMIC_AFTER_RELOC` hook) calls `x86_64_rewrite_plt_in_place`, which changes the permission of the `.plt` memory page (`MAP_PRIVATE`) to `PROT_READ|PROT_WRITE`, rewrites eligible PLT entries, then changes the page to `PROT_READ|PROT_EXEC`. For each `R_X86_64_JUMP_SLOT` relocation, `x86_64_rewrite_plt_in_place` reads the target address from the `.got.plt` entry, computes the indirect jump address using the addend, then checks whether the jump target is reachable with a direct JMP instruction. If so, the indirect jump instruction is rewritten to `jmp $target`; otherwise, when `GLIBC_TUNABLES=glibc.cpu.plt_rewrite=2` is specified on APX processors, the indirect jump instruction is rewritten to `jmpabs $target` (64-bit absolute jump). The mprotect operations increase private data uses and are incompatible with [memory-deny-write-execute](https://git.kernel.org/linus/b507808ebce23561d4ff8c2aa1fb949fe402bc61). One primary cost of PLT is the use of an extra instruction cache line. Rewriting jump instruction does not eliminate this overhead. Since the main executable is far away from shared objects in the address space, in the absence of APX `jmpabs`, the PLT rewriting will very likely not occur. PowerPC32 has a BSS-PLT ABI that generates PLT entries on the fly, which shares some similarity. BSS-PLT is obsoleted primarily due to security concerns. As a minor note, this optimization will [nullify](https://groups.google.com/g/x86-64-abi/c/vbuHVMK_RIA/m/zi0qi_0pBQAJ) the [`.plt.got` optimization (little benefit, but clever)](/blog/2021-08-29-all-about-global-offset-table#combining-.got-and-.got.plt). -fno-plt may also do better than this PLT optimization.