From patchwork Sun Sep 22 11:05:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 97811 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 760FE3858C66 for ; Sun, 22 Sep 2024 11:06:18 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-yw1-x1133.google.com (mail-yw1-x1133.google.com [IPv6:2607:f8b0:4864:20::1133]) by sourceware.org (Postfix) with ESMTPS id A02283858D20 for ; Sun, 22 Sep 2024 11:05:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A02283858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A02283858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1133 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727003152; cv=none; b=UePkuvUs9w3EsVhyeEWpunpK2C/J1w5PZFTTjoEWRXHHFsPGENjpmTMmVoCY92mHZtSuqCViRT7Tq4HvP0fVN57Ux7XP21vkt+iOKs2PwFXRzRWry73lZkWcQFJvTU+v5LuaYmUOMArT7rMSpQOrewZfAyFiQvjFnJu1XhICOPk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727003152; c=relaxed/simple; bh=e/D5TT89CXxvWWOQIpG02sxipBN7vadD0y1bFm+lkvk=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=Q44fiCVYO44SkoSQiJNEHko3shctb6LrZxL2ZvV0UNOl4uiK9pN3jDdJPU7iLy/vQC3OIsgl56lPNUp3jlc9FrUVvBsSvalITaYFQ63e7nG2pM2IDXf/SRorEayLgUG1FyqhZ4HMsC74wCXX7lTgYj3/yvArnFUIV+NuGZ2fukY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-6d5893cd721so27506007b3.0 for ; Sun, 22 Sep 2024 04:05:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727003150; x=1727607950; darn=sourceware.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=9eQp8pbPon9abkkkEBdhwfvGu85oJTlZj+6aQc4cD3Q=; b=BcF/gfcmbDQPeqzKPIMirx3BQUx34p4PkSSoBPatsbX1MTjJ/zyWDvbetBJxh89IZy o9r9pCrRHdUX0833fYSQRIP7XjzOLd8utu6ynrgAN+PQ/s5jkD/oZgG/jYkVDVdErqtB pd0L5f/13UeBYY0/6rPF3MG9xikDN52NVVdoS2lrtQ3e3aMCUlm3U8hHSIXYZ8SWtxSV y3spVPpYqdm/4ZiPssMsiJdqmLCNpPRCScoknlfowp+hFQyPtJT7bbTvYADMphI7xNT5 6tbi99On7ChcDjLQjFZBHIhIJrzCk+SgXgf1qo9bmUq5gMG0EIXvsMQHei+79IHHiDcL UxQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727003150; x=1727607950; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=9eQp8pbPon9abkkkEBdhwfvGu85oJTlZj+6aQc4cD3Q=; b=tLq52meIQ2+FH5t+tUTWkEQ0iHfd5pf/DYzniWN892XpgqRuVPZ+eABs4xwe09XOQc 9Lzh6YEBcxvEdOnZxyzd0y2chuFmnCJeVNU0fUGhq+fe0KKY5Nbnzuz8z+pEHNzpwLfR 7dbMbfxVyF8I7qPbNzPfpvwKPX98HO/5WjN1LzyFwsYktYusUYJazB20HG0fPgaaGjwf cbk6OEU1FmZRD7Rb00qQ6GDP1XpjUMgEO3FRKfrBWLbAxyk7hrPhglbBtdZEX4e/VoVf /v2Nzml37ndW8C4qZ/XiPqP65TdicZJ0I9GsLIYlD3MD4Mzwr13nR8FfeWfzwcyIBFI8 4law== X-Gm-Message-State: AOJu0Yz/SqSFCcONaABJR8mTfdpIPTWP/r07WNNRcnqPHMFoYoiWnspE yJq2uIr/SvLhATB22w6G7rJP+AW720q/cMuiTHAADiTad2iLqqpKuHuIzCmaiE+1mEPEp2DoOhb fAOdpiFt754EwfYgdPHM+6Wy2zptpxmgw6ZJmNo1I X-Google-Smtp-Source: AGHT+IHBpq09R0SCDNNrSI+NgIYXoBlJ+oWTdUVYakVMHRPXJEigfwY2poCumlk13d6f3T90MVTgLAHw/N0BIn5nSw4= X-Received: by 2002:a05:690c:6f12:b0:6dd:c2e4:d245 with SMTP id 00721157ae682-6dfeed2dcd0mr68405787b3.14.1727003149578; Sun, 22 Sep 2024 04:05:49 -0700 (PDT) MIME-Version: 1.0 From: "H.J. Lu" Date: Sun, 22 Sep 2024 19:05:13 +0800 Message-ID: Subject: [PATCH] x86: Turn PLT32 to PC32 only for PC-relative relocations To: Binutils , mail@felix-potthast.de X-Spam-Status: No, score=-3017.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_LOTSOFHASH, 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org commit 292676c15a615b5a95bede9ee91004d3f7ee7dfd Author: H.J. Lu Date: Thu Feb 13 13:44:17 2020 -0800 x86: Resolve PLT32 reloc aganst local symbol to section resolved PLT32 relocation against local symbol to section and commit 2585b7a5ce5830e60a089aa2316a329558902f0c Author: H.J. Lu Date: Sun Jul 19 06:51:19 2020 -0700 x86: Change PLT32 reloc against section to PC32 turned PLT32 relocation against section into PC32 relocation. But these transformations are valid only for PC-relative relocations. Add fx_pcrel check for PC-relative relocations when performing these transformations. From c580d18543cde2211007f61a9e94940275e390b4 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Sun, 22 Sep 2024 17:06:45 +0800 Subject: [PATCH] x86: Turn PLT32 to PC32 only for PC-relative relocations commit 292676c15a615b5a95bede9ee91004d3f7ee7dfd Author: H.J. Lu Date: Thu Feb 13 13:44:17 2020 -0800 x86: Resolve PLT32 reloc aganst local symbol to section resolved PLT32 relocation against local symbol to section and commit 2585b7a5ce5830e60a089aa2316a329558902f0c Author: H.J. Lu Date: Sun Jul 19 06:51:19 2020 -0700 x86: Change PLT32 reloc against section to PC32 turned PLT32 relocation against section into PC32 relocation. But these transformations are valid only for PC-relative relocations. Add fx_pcrel check for PC-relative relocations when performing these transformations. gas/ PR gas/32196 * config/tc-i386.c (tc_i386_fix_adjustable): Return fixP->fx_pcrel for PLT32 relocations. (i386_validate_fix): Turn PLT32 relocation into PC32 relocation only if fixp->fx_pcrel is set. * testsuite/gas/i386/reloc32.d: Updated. * testsuite/gas/i386/reloc64.d: Likewise. * testsuite/gas/i386/reloc32.s: Add PR gas/32196 test. * testsuite/gas/i386/reloc64.s: Likewise. ld/ PR gas/32196 * testsuite/ld-x86-64/pr32196.s: New file. * testsuite/ld-x86-64/x86-64.exp: Run PR gas/32196 test. Signed-off-by: H.J. Lu --- gas/config/tc-i386.c | 11 ++++++++++- gas/testsuite/gas/i386/reloc32.d | 1 + gas/testsuite/gas/i386/reloc32.s | 7 +++++++ gas/testsuite/gas/i386/reloc64.d | 1 + gas/testsuite/gas/i386/reloc64.s | 2 ++ ld/testsuite/ld-x86-64/pr32196.s | 26 ++++++++++++++++++++++++++ ld/testsuite/ld-x86-64/x86-64.exp | 8 ++++++++ 7 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 ld/testsuite/ld-x86-64/pr32196.s diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c index 11565ac7cb0..864a38a9a20 100644 --- a/gas/config/tc-i386.c +++ b/gas/config/tc-i386.c @@ -3930,6 +3930,11 @@ tc_i386_fix_adjustable (fixS *fixP) || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY) return 0; + /* Resolve PLT32 relocation against local symbol to section only for + PC-relative relocations. */ + if (fixP->fx_r_type == BFD_RELOC_386_PLT32 + || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32) + return fixP->fx_pcrel; return 1; } #endif @@ -17792,8 +17797,12 @@ i386_validate_fix (fixS *fixp) { /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol to section. Since PLT32 relocation must be against symbols, - turn such PLT32 relocation into PC32 relocation. */ + turn such PLT32 relocation into PC32 relocation. NB: We can + turn PLT32 relocation into PC32 relocation only for PC-relative + relocations since non-PC-relative relocations need PLT entries. + */ if (fixp->fx_addsy + && fixp->fx_pcrel && (fixp->fx_r_type == BFD_RELOC_386_PLT32 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32) && symbol_section_p (fixp->fx_addsy)) diff --git a/gas/testsuite/gas/i386/reloc32.d b/gas/testsuite/gas/i386/reloc32.d index 7d1b1ba2ae0..ebac545b200 100644 --- a/gas/testsuite/gas/i386/reloc32.d +++ b/gas/testsuite/gas/i386/reloc32.d @@ -43,6 +43,7 @@ Disassembly of section \.text: .*[ ]+R_386_TLS_LE[ ]+xtrn .*[ ]+R_386_TLS_LE_32[ ]+xtrn .*[ ]+R_386_TLS_LE_32[ ]+xtrn +.*[ ]+R_386_PLT32[ ]+ptr Disassembly of section \.data: #... .*[ ]+R_386_32[ ]+xtrn diff --git a/gas/testsuite/gas/i386/reloc32.s b/gas/testsuite/gas/i386/reloc32.s index e766a3dcc25..5616cd57e3f 100644 --- a/gas/testsuite/gas/i386/reloc32.s +++ b/gas/testsuite/gas/i386/reloc32.s @@ -162,3 +162,10 @@ bad .byte xtrn@tpoff .long xtrn@got + 4 .long xtrn@got - 4 bad .long xtrn@plt - . + + .text + movl $ptr@PLT, %eax + + .data +ptr: + .dc.a 0 diff --git a/gas/testsuite/gas/i386/reloc64.d b/gas/testsuite/gas/i386/reloc64.d index 665ede6f264..5fee029e56a 100644 --- a/gas/testsuite/gas/i386/reloc64.d +++ b/gas/testsuite/gas/i386/reloc64.d @@ -59,6 +59,7 @@ Disassembly of section \.text: .*[ ]+R_X86_64_32[ ]+xtrn .*[ ]+R_X86_64_GOT64[ ]+ptr .*[ ]+R_X86_64_GOTOFF64[ ]+Ldst +.*[ ]+R_X86_64_PLT32[ ]+ptr Disassembly of section \.data: #... .*[ ]+R_X86_64_64[ ]+xtrn diff --git a/gas/testsuite/gas/i386/reloc64.s b/gas/testsuite/gas/i386/reloc64.s index 2293865df20..5c0f4136a09 100644 --- a/gas/testsuite/gas/i386/reloc64.s +++ b/gas/testsuite/gas/i386/reloc64.s @@ -229,6 +229,8 @@ bad .long xtrn@plt - . .text movabs $ptr@GOT, %rax movabs $Ldst@GOTOFF, %rdx + movq $ptr@PLT, %rax + .data ptr: .quad 0 diff --git a/ld/testsuite/ld-x86-64/pr32196.s b/ld/testsuite/ld-x86-64/pr32196.s new file mode 100644 index 00000000000..e2bd5125d95 --- /dev/null +++ b/ld/testsuite/ld-x86-64/pr32196.s @@ -0,0 +1,26 @@ + .text + .global _start +_start: + movq $foo@PLT, %rax + leaq -11(%rip), %rbx + movq (%rbx, %rax), %rax + + # Write out "PASS\n". + movl $5, %edx + movl $.LC0, %esi + movl $1, %edi + movl $1, %eax + syscall + + # exit + movq $60, %rax + movq $0, %rdi + syscall + +foo: + ret + + .section .rodata.str1.1,"aMS",@progbits,1 +.LC0: + .string "PASS\n" + .section .note.GNU-stack,"",@progbits diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp index cf78fffe29f..a1c7d978932 100644 --- a/ld/testsuite/ld-x86-64/x86-64.exp +++ b/ld/testsuite/ld-x86-64/x86-64.exp @@ -1929,6 +1929,14 @@ if { [isnative] && [check_compiler_available] } { "pr32189" \ "pass.out" \ ] \ + [list \ + "Run pr32196" \ + "$NOPIE_LDFLAGS -nostdlib -nostartfiles" \ + "" \ + { pr32196.s } \ + "pr32196" \ + "pass.out" \ + ] \ ] # Run-time tests which require working ifunc attribute support. -- 2.46.1