From patchwork Thu Mar 9 08:04:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nelson Chu X-Patchwork-Id: 66649 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp160324wrd; Thu, 9 Mar 2023 00:05:01 -0800 (PST) X-Google-Smtp-Source: AK7set86w9Wum8Ix4S4zpLckIx34gVX4ijfu20D1s1LOQtSyM90hMOK2BqvQSsB9XXxqlJXYfUNP X-Received: by 2002:aa7:dd15:0:b0:4af:649d:da4e with SMTP id i21-20020aa7dd15000000b004af649dda4emr18817943edv.20.1678349101263; Thu, 09 Mar 2023 00:05:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349101; cv=none; d=google.com; s=arc-20160816; b=uPvpEbiNTY7Gi4HeQlqDfN9OzPeWERn7hNi0JpMzUA+RCAJ44SVP2t8GJcKVYhgYsZ DCtt7G7NrL5wqemaDFFoukRlpXK8AUzqY0JLdTaa98C06qbFuOS/dhUFP6dC9Iw0Vej5 5MLqXCUttEiR8s8BCe/Ov0QNe3kkWPD8ZHBak5/LaS6KPLs0rV/iQUVWaXYzxiLtbltb iE+txh7QVfu1T4LVIWlXrezZzkmRHuVJ8Qp1mUxGGoaVvo3niZok/4PmQJIE4OsbhElq YLYfidqrXkV1f5dj1HA5Pzirbw54AtNYb5Z1q1uBYG8zqdvGpN+cJUSJi/ytfPFPLZuc S9mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender: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:dmarc-filter :delivered-to; bh=xEdmtwT3c+t2yAl7K3s9QKSaBoubzWIU0LAdcJ94kZU=; b=UsH4b/iNI1IpiJ8gRyFvy7KOcdRZsfEkVK9uQrhv2kO8UHTGkx7+XP5iWAe4KfAygS nRav9LxB0xhGvsA7OZR/m28G+vYPwV6qbNJ8C51ppYv0YUrbXQTBYKAGW1k0gCrEtSn3 KCQMy6nNRhFhf4J6rF7tXQtnbNB7XIZeHx9Y0pamSvwHUJ+mVs+0nd82wfoKyuQTR7Vn fSjYc5mq8lc0HkoWFf/puR33Odu+magIJo0rh90qGiXJ1Kt4NZq0gpiI2EXixPzQ6ITa GhSh2h15aOhiRFRMo9tPwPL4KavmX8mAvIeaLrfLVry/zinqGWY6DKcYTEyraJrqtQdl bljg== ARC-Authentication-Results: i=1; mx.google.com; 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" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t12-20020a05640203cc00b004aad91c6589si18292514edw.193.2023.03.09.00.05.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 00:05:01 -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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 349DF3850852 for ; Thu, 9 Mar 2023 08:04:58 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from NelsondeMBP.localdomain (36-230-141-60.dynamic-ip.hinet.net [36.230.141.60]) by sourceware.org (Postfix) with ESMTP id 90D993858C5E for ; Thu, 9 Mar 2023 08:04:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 90D993858C5E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=NelsondeMBP.localdomain Received: by NelsondeMBP.localdomain (Postfix, from userid 501) id 83C0595ABA7; Thu, 9 Mar 2023 16:04:47 +0800 (CST) From: Nelson Chu To: binutils@sourceware.org Cc: nelson@rivosinc.com Subject: [PATCH] RISC-V: Segment fault in riscv_elf_append_rela. Date: Thu, 9 Mar 2023 16:04:46 +0800 Message-Id: <20230309080446.24714-1-nelson@rivosinc.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) MIME-Version: 1.0 X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KHOP_HELO_FCRDNS, NO_DNS_FOR_FROM, PDS_RDNS_DYNAMIC_FP, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_PBL, RCVD_IN_SORBS_DUL, RDNS_DYNAMIC, SPF_HELO_NONE, SPF_NONE, 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.29 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 Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759876587342941738?= X-GMAIL-MSGID: =?utf-8?q?1759876587342941738?= I seem to remember someone had sent a pacth for this before, but I cannot find where it is for now. So I just send it by myself. ====== % cat tmp.s foo: lui a0, %hi(_end) # R_RISCV_HI20 addi a0, a0, %lo(_end) # R_RISCV_LO12 .8byte foo # R_RISCV_64 % riscv64-unknown-linux-gnu-as tmp.s -o tmp.o % riscv64-unknown-linux-gnu-ld -shared tmp.o .* tmp.o: relocation R_RISCV_HI20 against `_end' can not be used when making a shared object; recompile with -fPIC zsh: segmentation fault I accidently meet this segment fault from the above case. Since we don't allow the absolute access (R_RISCV_HI20) when building shared object, the riscv_elf_check_relocs should return false directly when analyzing the lui, so there won't have rel.dyn section. But linker still try to emit the dynamic relocation for R_RISCV_64 in the riscv_elf_relocate_section, which cause the segmant fault in the riscv_elf_append_rela. Refer to other targets, Loongarch use BFD_ASSERT, and AARCH64 call abort(), to check if the dynamic section exsits before emiting. Since BFD_ASSERT still meet the segmant fault, I think just call abort here is a better chosen. bfd/ * elfnn-riscv.c (riscv_elf_append_rela): Abort when the dynamic section doesn't exist. --- bfd/elfnn-riscv.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 1200e6b11b5..36cdd629aa2 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -182,6 +182,9 @@ riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) const struct elf_backend_data *bed; bfd_byte *loc; + if (!s || !s->contents) + abort (); + bed = get_elf_backend_data (abfd); loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); bed->s->swap_reloca_out (abfd, rel, loc);