From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11olkn2096.outbound.protection.outlook.com [40.92.18.96]) by sourceware.org (Postfix) with ESMTPS id 8D90F385AE41 for ; Wed, 13 Dec 2023 01:31:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D90F385AE41 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=maskray.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=maskray.me ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8D90F385AE41 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=40.92.18.96 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1702431085; cv=pass; b=LURd/4iTAzWBvrDKc9Mgel0i3idNNAnhnhwpn2rAcLTaZHoOC5lknEzOHlXRKqQDMuUgCAq6BF3JupRYwCgdEnJPRSYWna2tHyvoJjp3hZokUUX23+aWO3ysq3A4B1sWjgr2zujOLJiJe3Gmz1qNW2MsrxZb7jTFjGzNYwhmxOA= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1702431085; c=relaxed/simple; bh=nKojbGq2mFKThIMcAKfeRWxr+R6DKu2ys69dGqvgYMM=; h=From:Date:Message-ID:Subject:To:MIME-Version; b=oSK4tb2rsHzzDGje1xmAB1h7RZdo3uL949qtj9GvmUzXX2DAhETiy3JwqcYumRVzyvBobR/SwajkrwjtRyxGX4M16roa7fV3FA2UeKgcGiV13vgBT+k+u2aXBVIjWvtBPYiBJvHPqsIOoyT3BQGKHxd9eTZkCMMWd0qPN+7OYf8= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=M8+cooy6a45qH9OiyRn8qNrCFrRtyND1N5bt8rrmCOi3pMLlipNc7nSfzfsHCAt2hhoyLYMVge3SAwEUjXqxz620aMJuDVGPF9YQ7U0+Ibvfk4mLp+teNhPrs8AhmKdSffEQzEZPxTnPjfY8fTZr0CvqLhDykNv6lQsh+vYDkHiUOvgdnnHPAq/Rl10bWu3NA/UspU0t/ndGpbXIDXsRWArsw62nH5FnKb11r07xjwuywswlLwHvtlBI2xfDOD4IKbwcpEJ5rDeTdDLx4CfVtngeBEyoloy2IjQDVg0jXFiRSdKS+dQlPYqGnJvxBjKLfuo16KwKwVsizDwmnCxEHw== 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=nXJhzsqnWAwMz1JqCEnNrlKNX8G6e8pixzBiXnklYI8=; b=YZ+qwkuPdrXquEIS2/68e+Abv4EDCE1D9bHk2qtI0PDY6Qv2stbto9M5z5uxJuaT7RZE4tbTh6ZVWWVjnhckiKiZrSuXbc450CNjtEXp+oY87EZ4yqVmYxbX2uPRDxYB3zPm50iDNDcGtOUlIZpxJ0+F3MJAx79I/bdkjntI7GQwb77FiHtlbqGJ9vwdlMv54CARNKt5ASLSuleeIgGWpfus8f666JeucS1aS/4tDXcw5Bp9bpkUvGavtjwnokcZ4JlnQDrq8DMi9iy4KZwZCRu1Mb94r34KawviYq8+p0VS53hymZhMgkh1WDwRrCsiRu46NE7WaUZ92DUFPkwdHA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from DS7PR12MB5765.namprd12.prod.outlook.com (2603:10b6:8:74::19) by BY5PR12MB5510.namprd12.prod.outlook.com (2603:10b6:a03:1d2::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26; Wed, 13 Dec 2023 01:31:21 +0000 Received: from DS7PR12MB5765.namprd12.prod.outlook.com ([fe80::2493:49ba:b06b:fa80]) by DS7PR12MB5765.namprd12.prod.outlook.com ([fe80::2493:49ba:b06b:fa80%6]) with mapi id 15.20.7068.033; Wed, 13 Dec 2023 01:31:21 +0000 X-Gm-Message-State: AOJu0YzXTS4qHUXCSdTOhGsRoJpvPxj9+lmdtRT6N537jAV45rYd94EJ ic2YjOcxA8os9sfk/PmMgJYDRu1zVeWUgnf4sdY= X-Google-Smtp-Source: AGHT+IE8SJ/mKQKmOjah+UNrPQKfgy9L/gWETTxyiHZCh5oZsIhAonHH+bKR8DjQBDP2u5udMsZ8x3if14g6vBZuseY= X-Received: by 2002:a05:6122:16a9:b0:49d:20fb:c899 with SMTP id 41-20020a05612216a900b0049d20fbc899mr7078181vkl.4.1702430774095; Tue, 12 Dec 2023 17:26:14 -0800 (PST) References: In-Reply-To: From: Fangrui Song Date: Tue, 12 Dec 2023 17:26:03 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] RISC-V: Do fixup for local symbols while with "-mno-relax" To: Palmer Dabbelt , lifang_xia@linux.alibaba.com Cc: nelson@rivosinc.com, binutils@sourceware.org, Kito Cheng , Andrew Waterman Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-TMN: [lcFMOir4DAHu8qAcspPW2+/w/XjzASEi] X-ClientProxiedBy: BN9PR03CA0591.namprd03.prod.outlook.com (2603:10b6:408:10d::26) To DS7PR12MB5765.namprd12.prod.outlook.com (2603:10b6:8:74::19) X-Microsoft-Original-Message-ID: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB5765:EE_|BY5PR12MB5510:EE_ X-MS-Office365-Filtering-Correlation-Id: 18169305-3610-4234-e1b5-08dbfb7b35b9 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4aJB9BcOXIT2DGX8q48jVOJtc0MuwzQ6S14ihaJ1rRDMXM5luOGLe7k/6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?= =?utf-8?B?Y2QzYTJnck1WOEFuUXdOSFpWK2ZzUkE1eWM5NENWVlkxMEVXTU9xaFB0aVkw?= =?utf-8?B?RDBTVVlsak9kaUg3ek9nQVN1S2JVTUxnYU81aWU3SkJLeTliSDd3Y3BXVXpE?= =?utf-8?B?S3ZHdlAvbjR0WGZDRW44dHNLTzRSa1ZjSU9Fbys4OWRFaXR1VElNNEt0SjVt?= =?utf-8?B?TUxRK3EvZjVNZGZUTTZ6dmUzcVNaUjExa3hVNFgyL24zQVZiV001djE5cTNu?= =?utf-8?B?U3VuVTFQZE04dmFiMFJGM0xYU2k2R281VENXUlZPLytkQy9jRXovWUllTHJr?= =?utf-8?Q?dF77jYTa57ST396kwWEKskg6wKj9NgXRyHYlaeb/RfaI8e?= X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-71ea3.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 18169305-3610-4234-e1b5-08dbfb7b35b9 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB5765.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Dec 2023 01:31:20.8025 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5510 X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,KAM_DMARC_STATUS,KAM_INFOUSMEBIZ,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: > In the scenario of generating .ko files, the kernel does not relax the .k= o > files. However, due to the large amount of relax and local relocation > information, this increases the size of the .ko files. > > In this patch, it will finish the fixup of the local relocations while wi= th > "-mno-relax" option. This can reduce the size of the relocation table. Thanks for the patch. I was initially confused by the description. Perhaps clarify it to: "... the Linux kernel uses -mno-relax but still gets a lot of relocations which can be eliminated" and "... resolve some fixups to constants and remove relocations" On Tue, Dec 12, 2023 at 4:31=E2=80=AFPM Palmer Dabbelt = wrote: > > On Tue, 12 Dec 2023 02:44:07 PST (-0800), nelson@rivosinc.com wrote: > > There is another stuff, do we need to limit linkers that only can link = the > > objects with -mno-relax if this optimization applied? If so, then we > > probably need, > > 1. An assembler option for this optimization > > 2. Record the object is relaxable or not into the elf attribute? or > > readelf header? or ... > > 2.1 Maybe we can record the relaxation information with > > Tag_RISCV_x3_reg_usage?, > > https://sourceware.org/pipermail/binutils/2023-September/129500.html? > > 2.2 Once the object enables relaxation for some code, the object needs = to > > be marked as "relaxable", even if it sets `.option norelax' later. > > > > On Tue, Dec 12, 2023 at 5:46=E2=80=AFPM Nelson Chu wrote: > > > >> The idea looks good to me. Passed regressions of riscv-gnu-toolchain,= so > >> committed with some minor changes and indent fixes. > >> > >> There are some TODOs, but no rush to do for now, > >> 1. The implementation is based on the code from bfd/elfnn-riscv.c. We > >> probably can move the code to bfd/elfxx-riscv.c, so that can reduce > >> duplicate code, just like what we did for the architecture parser. > >> Before that, I renamed functions and variables from *reloc* to *fixup*= , to > >> distinguish the code from bfd/elfnn-riscv.c, since they are still a li= ttle > >> bit different. > >> 2. Maybe not only pcrel_hi/lo12 relocation with local symbols can be r= esolved > >> at assembler time. Other pc-relative relocation, like branch, may als= o > >> be able to perform related optimizations. Agree that GNU assembler can resolve more PC-relative instructions to a constant without a relocation. For example, LLVM integrated assembler resolves the following `j` to consta= nts: // clang --target=3Driscv64-linux-gnu -march=3Drv64i -mno-relax -c j .Ltmp1 j .Ltmp1 .Ltmp1: (Though LLVM has its own problem that a `.option relax` anywhere in the assembly file will lose this optimization.) > Nelson and I were just talking about this. I'd been leaning towards > adding another option along the lines of "-mno-relax-abi", which would > explicitly mean that users can depend on no objects being relaxed. That > said, I'm not actually sure I can come up with a case where anything > breaks. > > I was specifically worried about things like the compiler doing label > subtraction, but IIUC that can only happen within a single translation > unit so we're safe to take advantage of on R_RISCV_RELAX relocations. > > There's also cases like misaligned globals, but we're already broken > there so I'm not sure it counts. > > So maybe this safe? I lean towards not providing an option. Beside the previous paragraph that LLVM integrated assembler omits relocations in more cases, architectures not using linker relaxation do optimize out relocations in these cases as well. It's a missing size optimization for RISC-V in the -mno-relax configuration= . Users relying on the relocation can switch to `.reloc ., R_RISCV_JAL, xx`.