From patchwork Thu Feb 22 06:51:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YunQiang Su X-Patchwork-Id: 204577 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:aa16:b0:108:e6aa:91d0 with SMTP id by22csp72364dyb; Wed, 21 Feb 2024 22:52:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXGCMv7KEi/7PQUXlyf12nBXdMcljocln+guUEZfY52bB1iNsP4X+nW8UHpt/P9ifaRuA0WoLizNWVBj3QwzS6sTdP/Xg== X-Google-Smtp-Source: AGHT+IHRnOaqYWh5AMvZGortsVeqgnS9ePygk2xw+Wb+hcWS8XF5/IgSHgn1Nuf5wRdJfjH/ezT8 X-Received: by 2002:a05:6214:daf:b0:68f:2abb:b4b2 with SMTP id h15-20020a0562140daf00b0068f2abbb4b2mr29266470qvh.30.1708584720515; Wed, 21 Feb 2024 22:52:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708584720; cv=pass; d=google.com; s=arc-20160816; b=S63gND+2xtY4HXM7LNardSRMZviKvFXK/CealWRE+ItQCWlqbnce4YGxD8BVMvDPa4 iqxxsz9I3GvOtDDr5QXiJW/BPwnM5iNLfBqMoYMvvcbrTBvFKfXXUSEaDavaaDJid85z GYA7Ay+wlEJ/++P2wTudIsfkOfluYzY40u+ow5KdLwq65DNdVakgWbGDpgb3lut0mT+o e2OYRaaCGLLIZ2e1veNUL3HtngLt9LAP49dO4a2uO/XEfX3kRjBOJoQcUzd5tOoZo6UR Jz6J0NISi3XxXqQzAZgM939QvAE6lH1DnZE9wbDA62ANQ/tz5ILjy8qy30SN/mfhw4GL Rv5w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:arc-filter :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=jeCqEQquySglt7YMYH1IihjANwsnnRNRUpL4HsGdq9I=; fh=aLfIqSE0ZAz0y9OZZ7DbEg7tg8klNyv5+gEVfKCR14s=; b=YiWFZQn0K4dzc0plQoWi9XDuXxeePNOwrvRO37eKt9aIAlgyIeQp3FxX7dppG74gl3 GX4XBoetmmPQWQcCA0rvF/m5Td+icqMkfBOre6aQqECS/wYqQDbR9ilmhtpSipccpxId ltBoIBORhtiKsXfQvjC6aA3wX7N41zeqcH7Uhmoeum0OP4/pbCKWsrOuhpKR+yk50gJB 2vg7OQ6VpyVM20SbslO2KDPz/KbB+T0BAaqE1D8m4L/VVopygxyOVv2CICNc/HvFUK+M V897BFkoTFJPd51Snj7rLFBxGvbTpPrICFgoD/ws2oiDQUVQBwNel2+m7pkn2GfwFSDr YIRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Px4i1f5K; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id im12-20020a056214246c00b0068f2bb8bf9esi12300855qvb.65.2024.02.21.22.52.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 22:52:00 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Px4i1f5K; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 28B22385B50E for ; Thu, 22 Feb 2024 06:52:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 28B22385B50E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708584720; bh=jeCqEQquySglt7YMYH1IihjANwsnnRNRUpL4HsGdq9I=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=Px4i1f5KXU+SY1B/eO5C/RFjHOfx78Y9Z9qfB5ltkMvjZoJiFPJgOg5UgwR8EImYa fY9zFcdqheMfWouRfzYUSp+j9SPaEoHfHuho7jAmPQN0DYRrpL59b+i+EYbhqTQTyL 8uPY6yIad9euex3j6/9Got88GGUAVhZm/G1yHINo= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by sourceware.org (Postfix) with ESMTPS id 360F0385802F for ; Thu, 22 Feb 2024 06:51:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 360F0385802F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 360F0385802F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708584708; cv=none; b=mU83olNq0smuJIOOBOOPCld8IEpJWXyjtrkjTNKFaL3v/9ze5UwRWatjwXSu718HNFJ6LEFcX+CSWp4d2cpsjFhr9bKTIulm0Zlc4dC5wIZb9bIn+yrgLU1SbZM2a3rZp/kcX/uEwxC8PTv6DSt0nv1tHvNv+cgU1cxoMRiJsow= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708584708; c=relaxed/simple; bh=PF3cCLLB2ScAI+kVE5z47avHoNa2QhQS2Hbd7uADXeI=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=mKoER7YGawINDn/hZUYdX7koYIFWuR7Zsx2gjqXpj7xSniDA2VhwAwwVH7DeZfTImjoEkyv28+43p+U1wWKmrWbbAMjzmjK3GwgsPe0N9SnDFaALyMI/r60FqSOmLa/FZsG4NJHNb4kNL+hcmWMKA7vr3YNWond4o7N8DNzJzck= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1dc29f1956cso11230085ad.0 for ; Wed, 21 Feb 2024 22:51:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708584697; x=1709189497; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jeCqEQquySglt7YMYH1IihjANwsnnRNRUpL4HsGdq9I=; b=F+r7b9o7OeBudrwHllioGThmi6gtXAuAF00SMkIlginPwV/SJWroTidk6j0qHPqU+p UThMxtawiyZwVTy2cxlWRUrksIw415bsXYN+yQDFiu7eowXgBL2KHFOCZv5WJk9sQkRl gATeXocy6zmK+LqDXTW/gqYIOtQumaIZxSl/HK3bw2aRL68JxDTJJOHCYFjJur9x5J9U 1JA+rdqdMssHdmOlIDnB5jnCj5xqghgMr9ykK6GLJw4Nh9CeSA2kudMsjgfmGQjVlM8T SKxjldEp22BU+82Au4DZ/NFxoKZBMsmihclWr+Fa8BkZN4sVKXdcxdS0UzunPZ6KOlFw Utvw== X-Gm-Message-State: AOJu0YxQmv5TaDxbNucv7InfZtJHC307+WtirEUETQ9+PVKknpoyoAt8 MeifstCXeLNfA0h7L5Dct8diPA+HLb+YdSdo67dfLpxzA1Vi3yFG25BKALdggzk= X-Received: by 2002:a17:903:230e:b0:1dc:248:28e6 with SMTP id d14-20020a170903230e00b001dc024828e6mr13567183plh.67.1708584696988; Wed, 21 Feb 2024 22:51:36 -0800 (PST) Received: from localhost.localdomain ([149.248.38.156]) by smtp.gmail.com with ESMTPSA id j6-20020a17090276c600b001db4c89aea5sm9162077plt.158.2024.02.21.22.51.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 22:51:36 -0800 (PST) From: YunQiang Su To: nickc@redhat.com Cc: binutils@sourceware.org, macro@orcam.me.uk, xry111@xry111.site Subject: [PATCH v7] MIPS: Reject branch absolute relocs for PIC for linking Date: Thu, 22 Feb 2024 14:51:28 +0800 Message-Id: <20240222065128.3693606-1-syq@gcc.gnu.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 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+ouuuleilei=gmail.com@sourceware.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791520619926184497 X-GMAIL-MSGID: 1791580932196873154 The asm code like: b 8 will emit absolute relocs like: R_MIPS_PC16 *ABS* If they are included into PIC shared objects or PIE executables, the branch target will be like 0x12340000, which will make the programs crash. --- bfd/elfxx-mips.c | 9 +++++++++ ld/testsuite/ld-mips-elf/mips-elf.exp | 1 + ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d | 5 +++++ ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s | 2 ++ 4 files changed, 17 insertions(+) create mode 100644 ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d create mode 100644 ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c index 69dd71419ff..8527a35010e 100644 --- a/bfd/elfxx-mips.c +++ b/bfd/elfxx-mips.c @@ -9258,6 +9258,15 @@ _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, (h) ? h->root.root.string : "a local symbol"); break; default: + if (branch_reloc_p (r_type) && r_symndx == STN_UNDEF) + { + howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd)); + info->callbacks->einfo + /* xgettext:c-format */ + (_("%X%H: relocation %s against an absolute value cannot be" + " used when making a PIC/PIE object\n"), + abfd, sec, rel->r_offset, howto->name); + } break; } } diff --git a/ld/testsuite/ld-mips-elf/mips-elf.exp b/ld/testsuite/ld-mips-elf/mips-elf.exp index 50af78d1430..a8e1b91b3a1 100644 --- a/ld/testsuite/ld-mips-elf/mips-elf.exp +++ b/ld/testsuite/ld-mips-elf/mips-elf.exp @@ -1678,6 +1678,7 @@ run_dump_test_o32 "pic-reloc-6" run_dump_test_n64 "pic-reloc-7" run_dump_test_n64 "pic-reloc-7" [list [list name (microMIPS)] \ [list as "-mmicromips"]] +run_dump_test "pic-reject-abs-reloc" run_dump_test_o32 "reloc-pcrel-r6" diff --git a/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d b/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d new file mode 100644 index 00000000000..b18c2698f81 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d @@ -0,0 +1,5 @@ +#name: MIPS PIC rejects branch absolute +#ld: -shared -T pic-reloc-absolute-lo.ld +#target: [check_shared_lib_support] +#error: \A[^\n]*: in function `foo':\n +#error: \(\.text\+0x0\): relocation R_MIPS_PC16 against an absolute value cannot be used when making a PIC/PIE object diff --git a/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s b/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s new file mode 100644 index 00000000000..a845fd50a77 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s @@ -0,0 +1,2 @@ +foo: + b 8