From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by sourceware.org (Postfix) with ESMTPS id E90E93858288 for ; Thu, 28 Dec 2023 14:58:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E90E93858288 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 E90E93858288 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::529 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703775490; cv=none; b=b4bVpRn3IeZlRl4FvH9R1Ut7wAFnM9s0ytCkE1P3/6Q2iXNvxZIWg29HqGlHgXmCX3MW48SN8V67WngLLDIgtYPDYN++7dPWcDPTTG9L4/9SkPvHzSQ7+LdOv5tE4XQktOAWC2O2R5XuoA62TlaQ/uMXk72JOUb4KcBNwbL4s1s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703775490; c=relaxed/simple; bh=ZfYhQtvhD5amxjySmMNOAwjxONNNBXHpxoexphDUnmA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kPk04JJGEqUd1YeCppCVZ/vuZ5o+Xgzp30swbU4Upp40cFRS1xqWgrvpfoXmEDE82nzwCRAAPvjlXTi2AFuRrERVI4RGe2MBX3yU9SyHXXnf3AmqJb05xWQfBdXQap37Diib0rIB7cL2EX2nNhpdzs0rMbL8PMc02vf6V3oSyu0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-5cd51c0e8ebso2260111a12.3 for ; Thu, 28 Dec 2023 06:58:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703775486; x=1704380286; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=g4azVjjVGpRdoUKZneI6fghF77OOJDWSdm51IY4UgLo=; b=V8aeKWyRRPtFaPd8d2F8V2B+yMUEoIvMms/Oc0TNohPpH66MUfWFzcEUP+e8YNyHjO LZbMNYP5ATVsTTRFeTDeqL7XPEZWnvlSq16YCix8UcNe+tB1WgtYc+0phG+oXbLHAoTs 3h28web4Y8IG7UsckmE1798QMF444PhuWkAgYJuyFebVymfeVPYo5NE0PJmYNXxvGjpA X949Z8OLGORF0haL9TmgFwlq5xnPCC3mTrNWlSCeygZeLD64VoqbbYjMyRjsBZ2vwKT1 GwASQIU8VLb8EARSSxgRqPN+JhQQ49T6Uw9DzpEN8GRz3o3/2Pg2I0aYsmTaegiZOVxh D4+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703775486; x=1704380286; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g4azVjjVGpRdoUKZneI6fghF77OOJDWSdm51IY4UgLo=; b=SUXezQrcUaBiuiOhGM6uoGlpEeep/iDcVGAiJsGetfA4e4yTpj63IoU42AVeJz9irP Q6MQnt22AUWEDhlj9W6nRp/A+Y9KN6RSuin7qxLhJSvWLWtEWQmUCSyFshnnsfS/fA40 mh+n3ZENCP9m02NeFqpTLK5PhUZQ1jPWyRij9EQjkrmPmJcvuh2BZhmTKQ8pQQRwNnPb GwRV6PjfDq442dTMxgmFgLkm/abO35HbAcww/paLKa0n4tyq2DRE9/MqWWzX033HH6RE dNOrpKWBn0kL/5uh+fsgkWPBAuX2d+qpfegGERZBSIO9AcrRKy3Pk2PvGxRw6qeWq0cm 5SMA== X-Gm-Message-State: AOJu0YyGUWw3ddcr+KKq/G/o3+zBlFo9dzPXI2RHKO9SgGdIQE9O57FL a0hma6XjyB+aQ4nYJdNje5p1+VnVqYg= X-Google-Smtp-Source: AGHT+IH8GK8xkWwfymjSJu5li0FokmDMcI5Xsci3IrGKjxy95iK7uyimpjNg4iGcDYnww1Fvs4wMlg== X-Received: by 2002:a05:6a20:429b:b0:196:6208:8ffb with SMTP id o27-20020a056a20429b00b0019662088ffbmr330374pzj.11.1703775486260; Thu, 28 Dec 2023 06:58:06 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.169.119]) by smtp.gmail.com with ESMTPSA id y12-20020aa7854c000000b006d9af59eecesm8626196pfn.20.2023.12.28.06.58.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Dec 2023 06:58:05 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id C7562740652 for ; Thu, 28 Dec 2023 06:58:04 -0800 (PST) From: "H.J. Lu" To: binutils@sourceware.org Subject: [PATCH 2/4] gold: Handle R_X86_64_CODE_4_GOTPCRELX Date: Thu, 28 Dec 2023 06:58:02 -0800 Message-ID: <20231228145804.658337-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231228145804.658337-1-hjl.tools@gmail.com> References: <20231228145804.658337-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3024.7 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: Handle R_X86_64_CODE_4_GOTPCRELX and convert mov name@GOTPCREL(%rip), %r31 to lea name@GOTPCREL(%rip), %r31 if the instruction is encoded with the REX2 prefix when possible. elfcpp/ * x86_64.h (R_X86_64_CODE_4_GOTPCRELX): New. gold/ * x86_64.cc (Target_x86_64::can_convert_mov_to_lea): Handle R_X86_64_CODE_4_GOTPCRELX. (Target_x86_64::Scan::get_reference_flags): Likewise. (Target_x86_64::Scan::local): Likewise. (Target_x86_64::Scan::possible_function_pointer_reloc): Likewise. (Target_x86_64::Scan::global): Likewise. (Target_x86_64::Relocate::relocate): Likewise. * testsuite/x86_64_mov_to_lea1.s: Add a test for R_X86_64_CODE_4_GOTPCRELX. * testsuite/x86_64_mov_to_lea2.s: Likewise. * testsuite/x86_64_mov_to_lea3.s: Likewise. * testsuite/x86_64_mov_to_lea4.s: Likewise. * testsuite/x86_64_mov_to_lea5.s: Likewise. * testsuite/x86_64_mov_to_lea.sh: Updated. --- elfcpp/x86_64.h | 6 ++++- gold/testsuite/x86_64_mov_to_lea.sh | 16 +++++++++++++ gold/testsuite/x86_64_mov_to_lea1.s | 1 + gold/testsuite/x86_64_mov_to_lea2.s | 1 + gold/testsuite/x86_64_mov_to_lea3.s | 1 + gold/testsuite/x86_64_mov_to_lea4.s | 1 + gold/testsuite/x86_64_mov_to_lea5.s | 1 + gold/x86_64.cc | 37 ++++++++++++++++++++++------- 8 files changed, 54 insertions(+), 10 deletions(-) diff --git a/elfcpp/x86_64.h b/elfcpp/x86_64.h index 0377e773071..97a87ae424d 100644 --- a/elfcpp/x86_64.h +++ b/elfcpp/x86_64.h @@ -95,9 +95,13 @@ enum R_X86_64_PC32_BND = 39, // PC relative 32 bit signed with BND prefix R_X86_64_PLT32_BND = 40, // 32 bit PLT address with BND prefix R_X86_64_GOTPCRELX = 41, // 32 bit signed PC relative offset to GOT - // without REX prefix, relaxable. + // without REX nor REX2 prefixes, relaxable. R_X86_64_REX_GOTPCRELX = 42, // 32 bit signed PC relative offset to GOT // with REX prefix, relaxable. + R_X86_64_CODE_4_GOTPCRELX = 43, // 32 bit signed PC relative offset to + // GOT if the instruction starts at 4 + // bytes before the relocation offset, + // relaxable. // GNU vtable garbage collection extensions. R_X86_64_GNU_VTINHERIT = 250, R_X86_64_GNU_VTENTRY = 251 diff --git a/gold/testsuite/x86_64_mov_to_lea.sh b/gold/testsuite/x86_64_mov_to_lea.sh index 1b30e1aa048..3e248ebec3c 100755 --- a/gold/testsuite/x86_64_mov_to_lea.sh +++ b/gold/testsuite/x86_64_mov_to_lea.sh @@ -25,20 +25,36 @@ set -e grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea1.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea1.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea2.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea2.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea3.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea3.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea4.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea4.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea5.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea5.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea6.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea6.stdout grep -q "mov 0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea7.stdout +grep -q "mov 0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea7.stdout grep -q "mov 0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea8.stdout +grep -q "mov 0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea8.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea9.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea9.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea10.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea10.stdout grep -q "mov 0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea11.stdout +grep -q "mov 0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea11.stdout grep -q "mov 0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea12.stdout +grep -q "mov 0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea12.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea13.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea13.stdout grep -q "lea -0x[a-f0-9]\+(%rip),%rax" x86_64_mov_to_lea14.stdout +grep -q "lea -0x[a-f0-9]\+(%rip),%r26" x86_64_mov_to_lea14.stdout grep -q "mov 0x[a-f0-9]\+(%rip),%eax" x86_64_mov_to_lea15.stdout +grep -q "mov 0x[a-f0-9]\+(%rip),%r26d" x86_64_mov_to_lea15.stdout grep -q "mov 0x[a-f0-9]\+(%rip),%eax" x86_64_mov_to_lea16.stdout +grep -q "mov 0x[a-f0-9]\+(%rip),%r26d" x86_64_mov_to_lea16.stdout exit 0 diff --git a/gold/testsuite/x86_64_mov_to_lea1.s b/gold/testsuite/x86_64_mov_to_lea1.s index 4dce487c3fc..298ede12f80 100644 --- a/gold/testsuite/x86_64_mov_to_lea1.s +++ b/gold/testsuite/x86_64_mov_to_lea1.s @@ -8,4 +8,5 @@ foo: .type _start, @function _start: movq foo@GOTPCREL(%rip), %rax + movq foo@GOTPCREL(%rip), %r26 .size _start, .-_start diff --git a/gold/testsuite/x86_64_mov_to_lea2.s b/gold/testsuite/x86_64_mov_to_lea2.s index 2a11b7a19f4..404f4c1dd48 100644 --- a/gold/testsuite/x86_64_mov_to_lea2.s +++ b/gold/testsuite/x86_64_mov_to_lea2.s @@ -3,4 +3,5 @@ .type _start, @function _start: movq _DYNAMIC@GOTPCREL(%rip), %rax + movq _DYNAMIC@GOTPCREL(%rip), %r26 .size _start, .-_start diff --git a/gold/testsuite/x86_64_mov_to_lea3.s b/gold/testsuite/x86_64_mov_to_lea3.s index ac43b783758..838c33edae0 100644 --- a/gold/testsuite/x86_64_mov_to_lea3.s +++ b/gold/testsuite/x86_64_mov_to_lea3.s @@ -7,4 +7,5 @@ foo: .type _start, @function _start: movq foo@GOTPCREL(%rip), %rax + movq foo@GOTPCREL(%rip), %r26 .size _start, .-_start diff --git a/gold/testsuite/x86_64_mov_to_lea4.s b/gold/testsuite/x86_64_mov_to_lea4.s index 37bee32bb4e..a68a4cf0d19 100644 --- a/gold/testsuite/x86_64_mov_to_lea4.s +++ b/gold/testsuite/x86_64_mov_to_lea4.s @@ -9,4 +9,5 @@ foo: .type _start, @function _start: movq foo@GOTPCREL(%rip), %rax + movq foo@GOTPCREL(%rip), %r26 .size _start, .-_start diff --git a/gold/testsuite/x86_64_mov_to_lea5.s b/gold/testsuite/x86_64_mov_to_lea5.s index e793a2b9b73..e818989deb7 100644 --- a/gold/testsuite/x86_64_mov_to_lea5.s +++ b/gold/testsuite/x86_64_mov_to_lea5.s @@ -9,4 +9,5 @@ foo: .type _start, @function _start: movl foo@GOTPCREL+4(%rip), %eax + movl foo@GOTPCREL+4(%rip), %r26d .size _start, .-_start diff --git a/gold/x86_64.cc b/gold/x86_64.cc index 928dfa8ea34..b7be9bf0d48 100644 --- a/gold/x86_64.cc +++ b/gold/x86_64.cc @@ -1053,8 +1053,9 @@ class Target_x86_64 : public Sized_target gold_assert(gsym != NULL); // We cannot do the conversion unless it's one of these relocations. if (r_type != elfcpp::R_X86_64_GOTPCREL - && r_type != elfcpp::R_X86_64_GOTPCRELX - && r_type != elfcpp::R_X86_64_REX_GOTPCRELX) + && r_type != elfcpp::R_X86_64_GOTPCRELX + && r_type != elfcpp::R_X86_64_REX_GOTPCRELX + && r_type != elfcpp::R_X86_64_CODE_4_GOTPCRELX) return false; // We cannot convert references to IFUNC symbols, or to symbols that // are not local to the current module. @@ -2971,6 +2972,7 @@ Target_x86_64::Scan::get_reference_flags(unsigned int r_type) case elfcpp::R_X86_64_GOTPCREL: case elfcpp::R_X86_64_GOTPCRELX: case elfcpp::R_X86_64_REX_GOTPCRELX: + case elfcpp::R_X86_64_CODE_4_GOTPCRELX: case elfcpp::R_X86_64_GOTPLT64: // Absolute in GOT. return Symbol::ABSOLUTE_REF; @@ -3251,6 +3253,7 @@ Target_x86_64::Scan::local(Symbol_table* symtab, case elfcpp::R_X86_64_GOTPCREL: case elfcpp::R_X86_64_GOTPCRELX: case elfcpp::R_X86_64_REX_GOTPCRELX: + case elfcpp::R_X86_64_CODE_4_GOTPCRELX: case elfcpp::R_X86_64_GOTPLT64: { // The symbol requires a GOT section. @@ -3261,21 +3264,30 @@ Target_x86_64::Scan::local(Symbol_table* symtab, // mov foo@GOTPCREL(%rip), %reg // to lea foo(%rip), %reg. // in Relocate::relocate. + size_t r_offset = reloc.get_r_offset(); if (!parameters->incremental() - && (r_type == elfcpp::R_X86_64_GOTPCREL - || r_type == elfcpp::R_X86_64_GOTPCRELX - || r_type == elfcpp::R_X86_64_REX_GOTPCRELX) + && (((r_type == elfcpp::R_X86_64_GOTPCREL + || r_type == elfcpp::R_X86_64_GOTPCRELX + || r_type == elfcpp::R_X86_64_REX_GOTPCRELX) + && r_offset >= 2) + || (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX + && r_offset >= 4)) && reloc.get_r_addend() == -4 - && reloc.get_r_offset() >= 2 && !is_ifunc) { section_size_type stype; const unsigned char* view = object->section_contents(data_shndx, &stype, true); - if (view[reloc.get_r_offset() - 2] == 0x8b) + if (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX + && view[r_offset - 4] != 0xd5) + goto need_got; + + if (view[r_offset - 2] == 0x8b) break; } +need_got: + // The symbol requires a GOT entry. unsigned int r_sym = elfcpp::elf_r_sym(reloc.get_r_info()); @@ -3498,6 +3510,7 @@ Target_x86_64::Scan::possible_function_pointer_reloc( case elfcpp::R_X86_64_GOTPCREL: case elfcpp::R_X86_64_GOTPCRELX: case elfcpp::R_X86_64_REX_GOTPCRELX: + case elfcpp::R_X86_64_CODE_4_GOTPCRELX: case elfcpp::R_X86_64_GOTPLT64: { return true; @@ -3714,6 +3727,7 @@ Target_x86_64::Scan::global(Symbol_table* symtab, case elfcpp::R_X86_64_GOTPCREL: case elfcpp::R_X86_64_GOTPCRELX: case elfcpp::R_X86_64_REX_GOTPCRELX: + case elfcpp::R_X86_64_CODE_4_GOTPCRELX: case elfcpp::R_X86_64_GOTPLT64: { // The symbol requires a GOT entry. @@ -3736,8 +3750,12 @@ Target_x86_64::Scan::global(Symbol_table* symtab, size_t r_offset = reloc.get_r_offset(); if (!parameters->incremental() && reloc.get_r_addend() == -4 - && r_offset >= 2 - && Target_x86_64::can_convert_mov_to_lea(gsym, r_type, + && ((r_type != elfcpp::R_X86_64_CODE_4_GOTPCRELX + && r_offset >= 2) + || (r_type == elfcpp::R_X86_64_CODE_4_GOTPCRELX + && r_offset >= 4 + && view[r_offset - 4] == 0xd5)) + && Target_x86_64::can_convert_mov_to_lea(gsym, r_type, r_offset, &view)) break; @@ -4420,6 +4438,7 @@ Target_x86_64::Relocate::relocate( case elfcpp::R_X86_64_GOTPCREL: case elfcpp::R_X86_64_GOTPCRELX: case elfcpp::R_X86_64_REX_GOTPCRELX: + case elfcpp::R_X86_64_CODE_4_GOTPCRELX: { bool converted_p = false; -- 2.43.0