From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from forward106p.mail.yandex.net (forward106p.mail.yandex.net [77.88.28.109]) by sourceware.org (Postfix) with ESMTPS id 2E0B53858D3C for ; Wed, 19 Oct 2022 16:19:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2E0B53858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=syntacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=syntacore.com Received: from iva4-143b1447cf50.qloud-c.yandex.net (iva4-143b1447cf50.qloud-c.yandex.net [IPv6:2a02:6b8:c0c:7511:0:640:143b:1447]) by forward106p.mail.yandex.net (Yandex) with ESMTP id 619F02FC2BE7 for ; Wed, 19 Oct 2022 19:19:34 +0300 (MSK) Received: by iva4-143b1447cf50.qloud-c.yandex.net (smtp/Yandex) with ESMTPSA id XrWwHBpd83-JYU0ghYw; Wed, 19 Oct 2022 19:19:34 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; s=mail; t=1666196374; bh=i389bta5vCEtgvWB9FUvSGBqCQPG/yGdkdJUVpx7Jz0=; h=Subject:From:To:Date:Message-ID; b=Je+a8i3a6A3F/wuRWZTQbAadKuoHscs7niB5wtPtGXyAo45lfz5C2P76g67RGWzkZ XmtTzxG4FmfsZ9qit7H/hZNf0LDR5Bk61xZX5qPAOzHGApnmvN6AXS1ke0mMDnWO7k jBPcLRbr35SzFnEYDyR6ByiCNPR48r7tMDXEOucw= Authentication-Results: iva4-143b1447cf50.qloud-c.yandex.net; dkim=pass header.i=@syntacore.com Message-ID: Date: Wed, 19 Oct 2022 19:19:33 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.3 Content-Language: en-US To: binutils@sourceware.org From: Dmitry Bushev Subject: Re: RISCV PC to GP-relative relaxations vs section movement Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,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 List-Id: Hi! I discovered one serious issue in the gnu ld riscv backend with custom linker script. It is about pc-to-gp relaxation. In some cases linker reports error: relocation truncated to fit: R_RISCV_GPREL_I against symbol ... I attached minimal reproduction (program source, makefile, default and slightly modified linker scripts). Everything works fine if linked using default linker script. But with modified one it fails. The only difference from default script is explicit memory region specification, which is essential for embedded environments, so I cannot remove it. I've done some investigation and root caused the issue: 1) On relaxation stage, linker sees, that it can relax pc-relative relocation to gp-relative, because symbol address is close enough to global pointer (fits in 12 bits immediate offset). 2) Later due to code size change during relaxation linker rearranges sections which causes symbol address shift. Global pointer also shifts. 3) On ldwrite() relocations are written, but due to symbol address and global pointer relative shift, the offset happens to not fit into 12 bit immediate anymore, which causes linker to report the problem. I am trying to fix this. Problem is: fix shall be somewhere in RISCV-specific code, but most of the linker section motion logic is platform-independent and it is not clear for me how to interfere in this process in the cleanest possible way. Will appreciate any help. Best regards, Dmitry