From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by sourceware.org (Postfix) with ESMTPS id 142A53858C51 for ; Mon, 21 Mar 2022 14:17:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 142A53858C51 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2108.outbound.protection.outlook.com [104.47.17.108]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-32-e2Y1K6eAPeihfHVG-AhxsA-1; Mon, 21 Mar 2022 15:17:01 +0100 X-MC-Unique: e2Y1K6eAPeihfHVG-AhxsA-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KdatE3riIkRe48Q2HWu3jrN9QrID+Oao8KsXLDv/rQBJeFGqYpa5jOjXlKLgAJEjkJZGG6l5jnxWsiUX5Wfa8l+c3ywe7mPpzNM+ipiZWQFymL59Y3FMoCY5MpoXrne507oVLXdt2KUA80O3J+SimtlPyBMPMzkJ0StKzYogY92BSzGxdIHq5Gb7LC17oqVWmA4yC9vIob7P4D25Z/cvjRp6yZEArZoZQicu3YaTvImOUWMeHABzDmItQkQmPaxKLMAhvVGzCmPojCYxfPL0H7u7ZsRPWrGhJTfvhM/nf5BZ9AOezAgKMvF0P1haPNDjEYZLWyT5z9/VPRFk9dn5bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=NdHhv1IEVlT1L3IFgUoz8/qR6We8FxIzCBG52qUDSEo=; b=CfWCD/eQacxMTnoxJbXL8oFeYFXB294tLm/XD7EAcG4DSyzirqBCPOll+Uf4PUwLCq1XGNGffNHXUbQYHT9w3YUpqzPqVHNsVo2Y40rRGTzIfPO75G2G87FR9uA/lRfZioeZ7ls337OgLTpjCDWbv9/nTGSBbxz0y6KVQLCpOqHe95vpxTHiH32Cf1t28OnxEDe/ypQ9vFYvb+ZsPQ/erPpAKzD0Ozs3dfs56oE13LPG9u3qjFYnFFBTJIiz6ygqbaY2rSc/SyT5aSQoHzc1NaJTpAIaPMFMeugS4+m5HUm//ScoNQ3hT1lE08yPnPl08VsLu57vpyfjEWgFw3EKVw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Received: from DU2PR04MB8616.eurprd04.prod.outlook.com (2603:10a6:10:2db::16) by AM6PR04MB5558.eurprd04.prod.outlook.com (2603:10a6:20b:9a::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.22; Mon, 21 Mar 2022 14:17:00 +0000 Received: from DU2PR04MB8616.eurprd04.prod.outlook.com ([fe80::fc39:fd17:1086:307a]) by DU2PR04MB8616.eurprd04.prod.outlook.com ([fe80::fc39:fd17:1086:307a%6]) with mapi id 15.20.5081.022; Mon, 21 Mar 2022 14:16:59 +0000 Message-ID: <20dd63c2-09c7-6308-c8a6-d2ecd9726430@suse.com> Date: Mon, 21 Mar 2022 15:16:58 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 From: Jan Beulich Subject: [PATCH v2] ELF32: don't silently truncate relocation addends To: Binutils References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-ClientProxiedBy: AS9P194CA0013.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:46d::17) To DU2PR04MB8616.eurprd04.prod.outlook.com (2603:10a6:10:2db::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: bf7fe260-8e74-4469-7bb5-08da0b4576a2 X-MS-TrafficTypeDiagnostic: AM6PR04MB5558:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5btklS3BcwdoA+uVIyOAd7mt4D/Gjr//Us2YV6j4SxvoXsnNgS+6TSJEbhiR9Z7/9g+PmeYCOANhDICt1I6N+qfMvvnErdYHXwJJ3DKkEtPJbPQnmh+AJR7hEwEAMy5/L6jjYSgqg9VvKStd0a69cuCKDfDXRLBuH3GpPSoK+1oMJgYTBK1LFinWXBH/TJWITl2RckyNY1LHZpCTE/hhwnYD3tfWMpxz8YHjKqLBzlFdGPQsRJUwpKTKOPGz7YkZUEXh2PtAjE44gWPxc8j3mp503h2l4rcqJToLh8Gb23lYtAlSaL+xSmBZg8CLiU9vm8Wla/SmjTDcoB7ZouZ4V/91NI8K/MPlFeR5b/6/dPzr/IaLgSMdxzZmsTwJkwqJZQ+mA7CPOufeUM363L2YqAgLvUVDL74JQ+ht5+foSBmHXdQkpfYFw4X1I28khEJMpgzFsNFug9bCoOPvJtlYjlBkIe6o3Ef7kMUxJlf01I8kbNVQvuC4FmnozT7gAMI54Qky3QcUg1jkgDt6ZYROIPgoIv5OtPrQdgKEGJiGsuSkTAotMr6xG9epGoIC6qGFX0+LWsoiljH87I0uJ/KJLbrSLhctrcHZqyjTDenl5TOP2FEl2cod5ywfyBBEdm1CPMWnY7Ico5KcMy9XordspYupY7OMDvKs9YtDcsLjd9qUy/thGguHplrCSfiOHMpVAzA68AmCQ6TL1+X5B3oGddya2SozaFzpLg80A9sPfU0= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU2PR04MB8616.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(4326008)(186003)(2616005)(26005)(8936002)(31696002)(66476007)(2906002)(86362001)(66556008)(66946007)(8676002)(31686004)(6486002)(508600001)(5660300002)(316002)(83380400001)(6506007)(6512007)(54906003)(38100700002)(36756003)(6916009)(43740500002)(45980500001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?Z3AyY0RxdzZLMDhhdHh3cUdSNEp4Rm1IZmQ1Y05nRXMzYU9ZemQvY3BNT3pH?= =?utf-8?B?Mlh0bzdWMEJ3NDlBaVJNUzVZR1NtNXExZDNtWEVGZzB3N1NWcDZPcFRjN3NN?= =?utf-8?B?cjdXSGx0L1prRUJraWg1dDArUk1BRHpZanN1MmxzT2QyZUlqYzZEalB2aTUr?= =?utf-8?B?SVRJR0ZlNFprNzRPRlgyNk5xb054Sm80VEM5QlgzZ2lJLytubW9LVVNxUVV6?= =?utf-8?B?N0xIbjdUeFZlSGxiN1FHRE40d3QrdTVjRWpyVlhYcEt6cXRQQ05nMVR3OXNR?= =?utf-8?B?RU9KdE1NaTk3Mnh6eG9uM0hKRlYzeHh5b0VOWW9WaGVJbGdIVXJQY3BCRUpi?= =?utf-8?B?VXlvaE5XajRmSWM0VTljbjlXY25GVXBUL3UvVGlvT0tYcnc5Y3dNYWJLNGxz?= =?utf-8?B?OTRWMHpjOHY4ay9yQzZvbk1Jc1FZVlhiWVBxQTREZmQ1c21CdmY5aHUzZk02?= =?utf-8?B?VzRXenJYSnJ1YUVLOVpVU3l0Q3FvbHBWaVM2bzhQNEtobzlDWDUxSzFjYk80?= =?utf-8?B?YmVBZzNPVUR0VlZzeVNDcWNEejBtbklnRHRYckE0d29oVzNVckRFR2ZRUXBw?= =?utf-8?B?anFDM2xPNVp6d0hHQjNDM2NQWnc1VmI4SG5GanV4N09uRUpHamhKQ0FNS1k0?= =?utf-8?B?QlpwYXgxREpQTEdtbGE0T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aUJkdUxLV3FSREo1TTR6RUMzeEJBKzlVcG1YaHJOT2hzRjdkQzNZaFhr?= =?utf-8?B?QVRGZGthd2lEcG11cUIrYkhMQ3dsdG0yTjVmbU9xZ0VBZXVlQWJQbWtpL2s2?= =?utf-8?B?N3VOWmd0NlFPTlZVZ05aOTBRUG5iNmI1aDk3eUl2USs3VjQ2clhTNjQybHBo?= =?utf-8?B?dk9NQ0xBNlVUM2xFM3FqQ21ySWQxb2EwMStjY1BVcVNVTWhFbmpUaDVJU0V1?= =?utf-8?B?a2xCdWNqRVordWdhSTVSemhLY2R4bUhFb3ZTTUtrTW8xcnhNd09kUWJWNUFt?= =?utf-8?B?ZmhTS0xXck5rUTZFY3BnSnpJZFErcUIxdkRWUUNNWW9PUzBrOTJBc1kzTE5j?= =?utf-8?Q?i7FBaj0QfzYDytdupxe0Cmjbv5WledWd?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: bf7fe260-8e74-4469-7bb5-08da0b4576a2 X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB8616.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Mar 2022 14:16:59.8761 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: K+JCNdndMyOuHPjl30fij6dwNvIxM5v9BLd0mjpa7qwgX2YORweLYGbZQ3pQGI1nK31JvUSMPz4dQr3pSFdhGQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5558 X-Spam-Status: No, score=-3031.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , X-List-Received-Date: Mon, 21 Mar 2022 14:17:05 -0000 At least x86-64's x32 sub-mode and RISC-V's 32-bit mode calculate addends as 64-bit values, but store them in signed 32-bit fields when generating the file without encountering any earlier error. When the relocated field is a 64-bit one, the value resulting after processing the relocation record when linking (or the latest when loading) may thus be wrong due to the truncation. With the code change in place, one x32 testcase actually triggers the new diagnostic. That one case of too large a (negative) addend is being adjusted alongside the addition of a new testcase to actually trigger the new error. (Note that due to internal BFD behavior the relocation in .data doesn't get processed anymore after the errors in .text.) Note that in principle it is possible to express 64-bit relocations in ELF32, but this would require .rel relocations, i.e. with the addend stored in the 64-bit field being relocated. But I guess it would be a lot of effort for little gain to actually support this. --- v2: Restrict to RELA relocations, as was originally intended, and to relocations of fields wider than 32 bits. --- I guess this may not be the only place which wants such a check, but at least this looks to be one central place. Originally I had the conditional as if ((int64_t) ptr->addend != (int32_t) ptr->addend) but I thought it would be better to avoid the non-portability of the conversion from unsigned to signed types. Afaict the way it is now it ought to be portable, as it involves only a signed->unsigned conversion. --- a/bfd/elfcode.h +++ b/bfd/elfcode.h @@ -997,6 +997,19 @@ elf_write_relocs (bfd *abfd, asection *s return; } +#if defined(BFD64) && ARCH_SIZE == 32 + if (rela_hdr->sh_type == SHT_RELA + && ptr->howto->bitsize > 32 + && ptr->addend - INT32_MIN > UINT32_MAX) + { + _bfd_error_handler (_("%pB: %pA+%"BFD_VMA_FMT"x: " + "relocation addend %"BFD_VMA_FMT"x too large"), + abfd, sec, ptr->address, ptr->addend); + *failedp = true; + bfd_set_error (bfd_error_bad_value); + } +#endif + src_rela.r_offset = ptr->address + addr_offset; src_rela.r_info = ELF_R_INFO (n, ptr->howto->type); src_rela.r_addend = ptr->addend; --- a/gas/testsuite/gas/i386/ilp32/ilp32.exp +++ b/gas/testsuite/gas/i386/ilp32/ilp32.exp @@ -38,6 +38,7 @@ if [expr ([istarget "i*86-*-*"] || [ista } run_list_test "reloc64" "--defsym _bad_=1" + run_list_test "reloc-2" set ASFLAGS "$old_ASFLAGS" } --- /dev/null +++ b/gas/testsuite/gas/i386/ilp32/reloc-2.l @@ -0,0 +1,4 @@ +.*: \.text\+2:.*addend.*too large.* +.*: \.text\+b:.*addend.*too large.* +.*: Assembler messages: +.*: Fatal error: .* --- /dev/null +++ b/gas/testsuite/gas/i386/ilp32/reloc-2.s @@ -0,0 +1,7 @@ + .text +_start: + movabs $x+0x123456789, %rax + movabs x+0x123456789, %eax + + .data + .quad x+0x123456789 --- a/gas/testsuite/gas/i386/ilp32/reloc64.d +++ b/gas/testsuite/gas/i386/ilp32/reloc64.d @@ -61,7 +61,8 @@ Disassembly of section \.text: .*[ ]+R_X86_64_TPOFF32[ ]+xtrn .*[ ]+R_X86_64_TPOFF32[ ]+xtrn .*[ ]+R_X86_64_TPOFF32[ ]+xtrn -.*[ ]+R_X86_64_64[ ]+xtrn\+0x1 +.*[ ]+R_X86_64_64[ ]+xtrn\+0x7fffffff +.*[ ]+R_X86_64_64[ ]+xtrn\-0x80000000 Disassembly of section \.data: #... .*[ ]+R_X86_64_32[ ]+xtrn --- a/gas/testsuite/gas/i386/ilp32/reloc64.l +++ b/gas/testsuite/gas/i386/ilp32/reloc64.l @@ -51,17 +51,17 @@ .*:175: Error: .* .*:176: Error: .* .*:177: Error: .* -.*:189: Error: .* -.*:192: Error: .* too large for field of 4 bytes at .* +.*:190: Error: .* .*:193: Error: .* too large for field of 4 bytes at .* .*:194: Error: .* too large for field of 4 bytes at .* .*:195: Error: .* too large for field of 4 bytes at .* -.*:196: Error: .* too large for field of 2 bytes at .* -.*:196: Error: .* too large for field of 1 byte at .* +.*:196: Error: .* too large for field of 4 bytes at .* .*:197: Error: .* too large for field of 2 bytes at .* .*:197: Error: .* too large for field of 1 byte at .* -.*:200: Error: .* too large for field of 4 bytes at .* -.*:201: Error: .* too large for field of 2 bytes at .* +.*:198: Error: .* too large for field of 2 bytes at .* +.*:198: Error: .* too large for field of 1 byte at .* +.*:201: Error: .* too large for field of 4 bytes at .* .*:202: Error: .* too large for field of 2 bytes at .* -.*:203: Error: .* too large for field of 1 byte at .* +.*:203: Error: .* too large for field of 2 bytes at .* .*:204: Error: .* too large for field of 1 byte at .* +.*:205: Error: .* too large for field of 1 byte at .* --- a/gas/testsuite/gas/i386/ilp32/reloc64.s +++ b/gas/testsuite/gas/i386/ilp32/reloc64.s @@ -178,7 +178,8 @@ bad .byte xtrn@tpoff .text mov xtrn@tpoff (%rbx), %eax - movabsq $xtrn - 4294967295, %rbp + movabsq $xtrn + 0x7fffffff, %rbx + movabsq $xtrn - 0x80000000, %rbp .data .quad xtrn