From nobody Thu Sep 11 22:33:07 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DA14C761AF for ; Wed, 29 Mar 2023 04:54:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229869AbjC2Eym (ORCPT ); Wed, 29 Mar 2023 00:54:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229846AbjC2Eyj (ORCPT ); Wed, 29 Mar 2023 00:54:39 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90A3D3AA6 for ; Tue, 28 Mar 2023 21:54:35 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id u1so8093492wmn.5 for ; Tue, 28 Mar 2023 21:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680065674; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ktbMUCpwKr4p6sxSafOqlZuluRkmcCN6C+8Lyu2qvAc=; b=4X7ifeA/SGmd5f+PrymDHio4j1xNhUrnthuq1Wov3mtXUT8875BfSubfmgak5IIJwz QL6Do9Vy3s0gGYmeUieXjJfEhsHN2s1Rc4dIqt+B+I8gIHCQ6ZOuaaUGWRhWhS+Twosv TosP9XCyPv4IF3ILX78Zz5NGkhVf5jvOLbYOhA53tWPxCd2PToqZvtb3YeyfsJZPJb+u FF/k19pYOl2V/lK2KrrIGEijTrAHtnRRNO7ZYzxDjZBfRDitSg0rmEm85z43dKcpfzRp /hf6rokfCu6eYDu4gOA18iGUaXVxzH2Qy3cMGjoPzk9mpqOme9dgqqJ69ENBbmrnf1wf MVdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680065674; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ktbMUCpwKr4p6sxSafOqlZuluRkmcCN6C+8Lyu2qvAc=; b=IRL01p75w/jKAKlAj92VZDCMnJSCv3DT7h3GplRXS3Xkx4aJ7TItG/Z86oEyyhzyBS vOo/8151RR0HuiJxRHjlVP9nxfP5ji9ZWnZZ4oG4RO1+hoOU3x0vMB9oRxG9v3lU/U1K KBtnal5SzJnfWYIvep8APbI91YJzFwJV4s1mvxE2Dz6FdiwH9sqfNqqBlNxCNMjlpvDg lxRjEMUjqUxEFFLsp9MVeuyQXtsu+xLKCCTZUwKuXeNdJG9+C5o8X6KBWUJlmXVMQ+jo Zhx+YqaaLTazdvHKqe9HwcEG7jVHWiiOf+QpK/66YJ//VmcAolVP9zDHKV9AXpY5i/Ej k6yw== X-Gm-Message-State: AO0yUKUzHlC0R3c8cF1j3vW9fzqYYONCVvBd/NmLI71o4p88V012HbLX dMNzChUWhusnuc7unvEn8w/PaQ== X-Google-Smtp-Source: AK7set8KyrHOxCHh16uvCtOCDVkCarPqxygWiKW2Mqp0rpgc5a8LupWu2x+qke+olNk5QXh69XyzWQ== X-Received: by 2002:a7b:c3c7:0:b0:3ed:ff92:dd2a with SMTP id t7-20020a7bc3c7000000b003edff92dd2amr14084380wmj.12.1680065673979; Tue, 28 Mar 2023 21:54:33 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id q18-20020a7bce92000000b003ef71d541cbsm831127wmj.1.2023.03.28.21.54.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 21:54:33 -0700 (PDT) From: Alexandre Ghiti To: Nick Desaulniers , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kbuild@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH v9 1/6] riscv: Prepare EFI header for relocatable kernels Date: Wed, 29 Mar 2023 06:53:24 +0200 Message-Id: <20230329045329.64565-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329045329.64565-1-alexghiti@rivosinc.com> References: <20230329045329.64565-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" ld does not handle relocations correctly as explained here [1], a fix for that was proposed by Nelson there but we have to support older toolchains and then provide this fix. Note that llvm does not need this fix and is then excluded. [1] https://sourceware.org/pipermail/binutils/2023-March/126690.html Signed-off-by: Alexandre Ghiti --- arch/riscv/include/asm/set_memory.h | 3 +++ arch/riscv/kernel/efi-header.S | 19 ++++++++++++++++--- arch/riscv/kernel/vmlinux.lds.S | 5 ++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/arch/riscv/include/asm/set_memory.h b/arch/riscv/include/asm/s= et_memory.h index a2c14d4b3993..ec11001c3fe0 100644 --- a/arch/riscv/include/asm/set_memory.h +++ b/arch/riscv/include/asm/set_memory.h @@ -56,4 +56,7 @@ bool kernel_page_present(struct page *page); #define SECTION_ALIGN L1_CACHE_BYTES #endif /* CONFIG_STRICT_KERNEL_RWX */ =20 +#define PECOFF_SECTION_ALIGNMENT 0x1000 +#define PECOFF_FILE_ALIGNMENT 0x200 + #endif /* _ASM_RISCV_SET_MEMORY_H */ diff --git a/arch/riscv/kernel/efi-header.S b/arch/riscv/kernel/efi-header.S index 8e733aa48ba6..515b2dfbca75 100644 --- a/arch/riscv/kernel/efi-header.S +++ b/arch/riscv/kernel/efi-header.S @@ -6,6 +6,7 @@ =20 #include #include +#include =20 .macro __EFI_PE_HEADER .long PE_MAGIC @@ -33,7 +34,11 @@ optional_header: .byte 0x02 // MajorLinkerVersion .byte 0x14 // MinorLinkerVersion .long __pecoff_text_end - efi_header_end // SizeOfCode - .long __pecoff_data_virt_size // SizeOfInitializedData +#ifdef __clang__ + .long __pecoff_data_virt_size // SizeOfInitializedData +#else + .long __pecoff_data_virt_end - __pecoff_text_end // SizeOfInitializedData +#endif .long 0 // SizeOfUninitializedData .long __efistub_efi_pe_entry - _start // AddressOfEntryPoint .long efi_header_end - _start // BaseOfCode @@ -91,9 +96,17 @@ section_table: IMAGE_SCN_MEM_EXECUTE // Characteristics =20 .ascii ".data\0\0\0" - .long __pecoff_data_virt_size // VirtualSize +#ifdef __clang__ + .long __pecoff_data_virt_size // VirtualSize +#else + .long __pecoff_data_virt_end - __pecoff_text_end // VirtualSize +#endif .long __pecoff_text_end - _start // VirtualAddress - .long __pecoff_data_raw_size // SizeOfRawData +#ifdef __clang__ + .long __pecoff_data_raw_size // SizeOfRawData +#else + .long __pecoff_data_raw_end - __pecoff_text_end // SizeOfRawData +#endif .long __pecoff_text_end - _start // PointerToRawData =20 .long 0 // PointerToRelocations diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.ld= s.S index 53a8ad65b255..1c38294580c0 100644 --- a/arch/riscv/kernel/vmlinux.lds.S +++ b/arch/riscv/kernel/vmlinux.lds.S @@ -27,9 +27,6 @@ ENTRY(_start) =20 jiffies =3D jiffies_64; =20 -PECOFF_SECTION_ALIGNMENT =3D 0x1000; -PECOFF_FILE_ALIGNMENT =3D 0x200; - SECTIONS { /* Beginning of code and text segment */ @@ -132,6 +129,7 @@ SECTIONS #ifdef CONFIG_EFI .pecoff_edata_padding : { BYTE(0); . =3D ALIGN(PECOFF_FILE_ALIGNMENT); } __pecoff_data_raw_size =3D ABSOLUTE(. - __pecoff_text_end); + __pecoff_data_raw_end =3D ABSOLUTE(.); #endif =20 /* End of data section */ @@ -142,6 +140,7 @@ SECTIONS #ifdef CONFIG_EFI . =3D ALIGN(PECOFF_SECTION_ALIGNMENT); __pecoff_data_virt_size =3D ABSOLUTE(. - __pecoff_text_end); + __pecoff_data_virt_end =3D ABSOLUTE(.); #endif _end =3D .; =20 --=20 2.37.2 From nobody Thu Sep 11 22:33:07 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1333FC74A5B for ; Wed, 29 Mar 2023 04:55:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229854AbjC2Ezj (ORCPT ); Wed, 29 Mar 2023 00:55:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229632AbjC2Ezi (ORCPT ); Wed, 29 Mar 2023 00:55:38 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A0122D46 for ; Tue, 28 Mar 2023 21:55:37 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id d11-20020a05600c3acb00b003ef6e6754c5so5202714wms.5 for ; Tue, 28 Mar 2023 21:55:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680065736; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EwZQgZuukK5ZcMfwhQ8cOp9XaPAwPgUAMS4WLeQguqk=; b=E3Z4Yqhoin1W8r2t+Jyhb4Jqxbb/dLpgY31WgMC/8f4M9Agb0OzJlakAr0drlghh2B ZtOOx+SrUI2+Y2A2rSR9cMwIRUMF5pNcTOP1QTN3TGiqltjw5qd92s7SeTRGf1Falocp QTJ5ejL81s/SAgZfBKC69BFU2tmVRBauch20G2Y5j6xUaUdlA0SGZopmV6daT6VRNgRI IJS+IFtjoBb0K1xT2ifMxLBr8JTiqs+T8R/Rth3HfyOLzpfjohMpDGfFJaDQgExXIBL/ 5H7bEEdsD0Sy58CqDhf7boCxDSJekNe+SKevoFuOkns50ZDd6e9wroN5P47Q03l4x0Sl VTDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680065736; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EwZQgZuukK5ZcMfwhQ8cOp9XaPAwPgUAMS4WLeQguqk=; b=RxgzMxT8ysSFi7aVcLkb8ytNTZcfp74hh+72sJe7WvtGYFEcySyGFY8OBXyy+m2kBZ YSFnwYcRZCFhvk1A8Wxy/M6UdIzAmxB8U0fiDioQH/qjklyh4jrceilXUamEsTqIRKF9 lpqmjtyYUjGe7HpVNgXohgqBnWEmfAQf4xJ4yZwhWFqjobVj+BkjhJc8zWUPstFs7a2j Ujy3eMhpHnDULKEdtmRQQ5HBBUZbA9iWBGhFeTcu7Le7CinT/mXUueA4SS1V2/QW4g1B f82fkhF3+lKBRcT963d9LAf5ZDUdWXPcv0qWgqDpIakKTRA341He8ORrNHGm7Wu8O9LO 4+5g== X-Gm-Message-State: AO0yUKWZLTrYE63nQgYfa6pXqCtEoCe6cbsuzarU415uv0FjuX5WT0Bj GZjjij8dd1dDGG5OFhYpI1QHVQ== X-Google-Smtp-Source: AK7set+5IRHyYxxuib+WjeFew51ry16Zrb5/PPkIgLCKxGSq+H5MoxgqvQV7tTLU4AjobT2NNsYQgQ== X-Received: by 2002:a7b:cbd2:0:b0:3ee:3e07:5d26 with SMTP id n18-20020a7bcbd2000000b003ee3e075d26mr13763028wmi.24.1680065735865; Tue, 28 Mar 2023 21:55:35 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id c1-20020a05600c0a4100b003ee5fa61f45sm840822wmq.3.2023.03.28.21.55.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 21:55:35 -0700 (PDT) From: Alexandre Ghiti To: Nick Desaulniers , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kbuild@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH v9 2/6] riscv: Move .rela.dyn outside of init to avoid empty relocations Date: Wed, 29 Mar 2023 06:53:25 +0200 Message-Id: <20230329045329.64565-3-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329045329.64565-1-alexghiti@rivosinc.com> References: <20230329045329.64565-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a preparatory patch for relocatable kernels: .rela.dyn should be in .init but doing so actually produces empty relocations, so this should be a temporary commit until we find a solution. This issue was reported here [1]. [1] https://lore.kernel.org/all/4a6fc7a3-9697-a49b-0941-97f32194b0d7@ghiti.= fr/. Signed-off-by: Alexandre Ghiti --- arch/riscv/kernel/vmlinux.lds.S | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.ld= s.S index 1c38294580c0..e05e6df44225 100644 --- a/arch/riscv/kernel/vmlinux.lds.S +++ b/arch/riscv/kernel/vmlinux.lds.S @@ -96,10 +96,6 @@ SECTIONS *(.rel.dyn*) } =20 - .rela.dyn : { - *(.rela*) - } - __init_data_end =3D .; =20 . =3D ALIGN(8); @@ -126,6 +122,10 @@ SECTIONS *(.sdata*) } =20 + .rela.dyn : { + *(.rela*) + } + #ifdef CONFIG_EFI .pecoff_edata_padding : { BYTE(0); . =3D ALIGN(PECOFF_FILE_ALIGNMENT); } __pecoff_data_raw_size =3D ABSOLUTE(. - __pecoff_text_end); --=20 2.37.2 From nobody Thu Sep 11 22:33:07 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7385EC6FD18 for ; Wed, 29 Mar 2023 04:56:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229632AbjC2E4l (ORCPT ); Wed, 29 Mar 2023 00:56:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229505AbjC2E4j (ORCPT ); Wed, 29 Mar 2023 00:56:39 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B2D82D46 for ; Tue, 28 Mar 2023 21:56:38 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id r29so14294680wra.13 for ; Tue, 28 Mar 2023 21:56:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680065797; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OQMSo3dI1TiCgJ5bOYmXGeEP35jM8ytuP5TLzldwWRk=; b=oHzBbKmfvidPMXVZTsEnD3Nq8DrVl2ki2+fns5DbUkbhqkVcyOXHnVU1ShAOw6KMfN jRKYI06+OpsREA2TvO2HcVgRzHy36hzSfmGcbmpF6fQkFOCWxB67H4T8BvFIsWgBAvdq 9oLf3Bj6yrlCorWf6W4vJ0M9L+T57cGuF39BtWSk279Vww3Yv1Jj095jPMBEAipUhvUs U9myrZhKJnMgsGxbidyNhkaEk0I0UGdO5RrjgQdqIFPlt5dgQBL2+aqZO+un5TtgpOWO DGKqZD7CUpiPHsBrH7foUX9kVx7D0a373qtyl0OeOaAkDgQI5POdME9T8+scDzUUlOvw 9iiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680065797; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OQMSo3dI1TiCgJ5bOYmXGeEP35jM8ytuP5TLzldwWRk=; b=d2QwLz6kxJ5Qfl8Lbev/1vN24xhI9rzMd7cpnyU7jQ7kUxt09Z7BAvx69zNmdnsjls 5ESkNdsGMfD8mdDH+0Z5mV2YOGvlLieRUIUu3QHeUot8Ihnq3YMjpk48d9UD5V55ylnC O5kPch5x/6qw5GEOvxXwoYA5i371Ds8veqyYJKK8zqSXN/4DSb69ANhVXfw9YsJQekMX XguybEQIJGW4Ol7vf4jSABbC2xc6xE8s17JFmKSZoWK8Qe+Cu3NDqlrYq37n5Rfqh6/n EgP7UI9XI1nTbBv0nLACGGoVKdiMYJ9V6HN96you5dRcRG5NcwpyS3voUdh2u/FqXf3Z ZIjw== X-Gm-Message-State: AAQBX9didIrjWWC2Cb7WfHVBPQJtPbFsiv1aC2IQ9DhugcruIP7/d2/H YrEYOMY4obOgDnkZZnNMMIWHJKzFax+uNulRy9A= X-Google-Smtp-Source: AKy350apw2rJxTh795JIx8DPttqwEHcr5qnVOJOYeKOdyO3sghAOnPCeswjEAny+PP0Gt+12hyUksA== X-Received: by 2002:adf:fe51:0:b0:2d9:457a:1069 with SMTP id m17-20020adffe51000000b002d9457a1069mr14280855wrs.37.1680065796823; Tue, 28 Mar 2023 21:56:36 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id e8-20020adffd08000000b002c592535839sm29042488wrr.17.2023.03.28.21.56.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 21:56:36 -0700 (PDT) From: Alexandre Ghiti To: Nick Desaulniers , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kbuild@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH v9 3/6] riscv: Introduce CONFIG_RELOCATABLE Date: Wed, 29 Mar 2023 06:53:26 +0200 Message-Id: <20230329045329.64565-4-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329045329.64565-1-alexghiti@rivosinc.com> References: <20230329045329.64565-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This config allows to compile 64b kernel as PIE and to relocate it at any virtual address at runtime: this paves the way to KASLR. Runtime relocation is possible since relocation metadata are embedded into the kernel. Note that relocating at runtime introduces an overhead even if the kernel is loaded at the same address it was linked at and that the compiler options are those used in arm64 which uses the same RELA relocation format. Signed-off-by: Alexandre Ghiti --- arch/riscv/Kconfig | 14 +++++++++ arch/riscv/Makefile | 7 +++-- arch/riscv/kernel/vmlinux.lds.S | 17 +++++++++-- arch/riscv/mm/Makefile | 4 +++ arch/riscv/mm/init.c | 54 ++++++++++++++++++++++++++++++++- 5 files changed, 91 insertions(+), 5 deletions(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 3c5907431081..6ff9f574195d 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -562,6 +562,20 @@ config COMPAT =20 If you want to execute 32-bit userspace applications, say Y. =20 +config RELOCATABLE + bool "Build a relocatable kernel" + depends on MMU && 64BIT && !XIP_KERNEL + help + This builds a kernel as a Position Independent Executable (PIE), + which retains all relocation metadata required to relocate the + kernel binary at runtime to a different virtual address than the + address it was linked at. + Since RISCV uses the RELA relocation format, this requires a + relocation pass at runtime even if the kernel is loaded at the + same address it was linked at. + + If unsure, say N. + endmenu # "Kernel features" =20 menu "Boot options" diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 6203c3378922..860b09e409c7 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -7,9 +7,12 @@ # =20 OBJCOPYFLAGS :=3D -O binary -LDFLAGS_vmlinux :=3D +ifeq ($(CONFIG_RELOCATABLE),y) + LDFLAGS_vmlinux +=3D -shared -Bsymbolic -z notext -z norelro + KBUILD_CFLAGS +=3D -fPIE +endif ifeq ($(CONFIG_DYNAMIC_FTRACE),y) - LDFLAGS_vmlinux :=3D --no-relax + LDFLAGS_vmlinux +=3D --no-relax KBUILD_CPPFLAGS +=3D -DCC_USING_PATCHABLE_FUNCTION_ENTRY ifeq ($(CONFIG_RISCV_ISA_C),y) CC_FLAGS_FTRACE :=3D -fpatchable-function-entry=3D4 diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.ld= s.S index e05e6df44225..615ff5842690 100644 --- a/arch/riscv/kernel/vmlinux.lds.S +++ b/arch/riscv/kernel/vmlinux.lds.S @@ -122,10 +122,23 @@ SECTIONS *(.sdata*) } =20 - .rela.dyn : { - *(.rela*) + .rela.dyn : ALIGN(8) { + __rela_dyn_start =3D .; + *(.rela .rela*) + __rela_dyn_end =3D .; } =20 +#ifdef CONFIG_RELOCATABLE + .data.rel : { *(.data.rel*) } + .got : { *(.got*) } + .plt : { *(.plt) } + .dynamic : { *(.dynamic) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .hash : { *(.hash) } + .gnu.hash : { *(.gnu.hash) } +#endif + #ifdef CONFIG_EFI .pecoff_edata_padding : { BYTE(0); . =3D ALIGN(PECOFF_FILE_ALIGNMENT); } __pecoff_data_raw_size =3D ABSOLUTE(. - __pecoff_text_end); diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile index 2ac177c05352..b85e9e82f082 100644 --- a/arch/riscv/mm/Makefile +++ b/arch/riscv/mm/Makefile @@ -1,6 +1,10 @@ # SPDX-License-Identifier: GPL-2.0-only =20 CFLAGS_init.o :=3D -mcmodel=3Dmedany +ifdef CONFIG_RELOCATABLE +CFLAGS_init.o +=3D -fno-pie +endif + ifdef CONFIG_FTRACE CFLAGS_REMOVE_init.o =3D $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_cacheflush.o =3D $(CC_FLAGS_FTRACE) diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index f803671d18b2..bce899b180cd 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -20,6 +20,9 @@ #include #include #include +#ifdef CONFIG_RELOCATABLE +#include +#endif =20 #include #include @@ -146,7 +149,7 @@ static void __init print_vm_layout(void) print_ml("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END); #endif =20 - print_ml("kernel", (unsigned long)KERNEL_LINK_ADDR, + print_ml("kernel", (unsigned long)kernel_map.virt_addr, (unsigned long)ADDRESS_SPACE_END); } } @@ -831,6 +834,44 @@ static __init void set_satp_mode(void) #error "setup_vm() is called from head.S before relocate so it should not = use absolute addressing." #endif =20 +#ifdef CONFIG_RELOCATABLE +extern unsigned long __rela_dyn_start, __rela_dyn_end; + +static void __init relocate_kernel(void) +{ + Elf64_Rela *rela =3D (Elf64_Rela *)&__rela_dyn_start; + /* + * This holds the offset between the linked virtual address and the + * relocated virtual address. + */ + uintptr_t reloc_offset =3D kernel_map.virt_addr - KERNEL_LINK_ADDR; + /* + * This holds the offset between kernel linked virtual address and + * physical address. + */ + uintptr_t va_kernel_link_pa_offset =3D KERNEL_LINK_ADDR - kernel_map.phys= _addr; + + for ( ; rela < (Elf64_Rela *)&__rela_dyn_end; rela++) { + Elf64_Addr addr =3D (rela->r_offset - va_kernel_link_pa_offset); + Elf64_Addr relocated_addr =3D rela->r_addend; + + if (rela->r_info !=3D R_RISCV_RELATIVE) + continue; + + /* + * Make sure to not relocate vdso symbols like rt_sigreturn + * which are linked from the address 0 in vmlinux since + * vdso symbol addresses are actually used as an offset from + * mm->context.vdso in VDSO_OFFSET macro. + */ + if (relocated_addr >=3D KERNEL_LINK_ADDR) + relocated_addr +=3D reloc_offset; + + *(Elf64_Addr *)addr =3D relocated_addr; + } +} +#endif /* CONFIG_RELOCATABLE */ + #ifdef CONFIG_XIP_KERNEL static void __init create_kernel_page_table(pgd_t *pgdir, __always_unused bool early) @@ -1029,6 +1070,17 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_= 4K); #endif =20 +#ifdef CONFIG_RELOCATABLE + /* + * Early page table uses only one PUD, which makes it possible + * to map PUD_SIZE aligned on PUD_SIZE: if the relocation offset + * makes the kernel cross over a PUD_SIZE boundary, raise a bug + * since a part of the kernel would not get mapped. + */ + BUG_ON(PUD_SIZE - (kernel_map.virt_addr & (PUD_SIZE - 1)) < kernel_map.si= ze); + relocate_kernel(); +#endif + apply_early_boot_alternatives(); pt_ops_set_early(); =20 --=20 2.37.2 From nobody Thu Sep 11 22:33:07 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD75DC6FD18 for ; Wed, 29 Mar 2023 04:57:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229573AbjC2E5m (ORCPT ); Wed, 29 Mar 2023 00:57:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjC2E5k (ORCPT ); Wed, 29 Mar 2023 00:57:40 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 877C92707 for ; Tue, 28 Mar 2023 21:57:39 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id p34so8110089wms.3 for ; Tue, 28 Mar 2023 21:57:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680065858; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dDDQpcu4wlArU6dZYMfeUKSEQsg6b1kdryOeCUrFQlc=; b=VKRX5Tw8ADg3H3As96bXMjtA9WlMjtkGfL47ArT3E95iTfus7FSaAJlBDnfp920k7x PdemGUW/YeLJrVkn6J1yQZSUU28mxt+AhtJugWUcs2fm2mUKZ0SkjAwTFfxmYAHWaBh9 34oXo9l0Qw8UjbcFYLjr6lPWCBQFUyc7ZMbeeKPlzhXYm2TxI9Zb/khVejjqkrAi2csd V36Yv1AqWNEsC+gGTi11hi+mhiFJz3StbOahvPtxZEULbxL//+Dp1E6LZNcu2MmWc2DD 5/N1MLJY4bAjXif0FAnP0Jx1XJ+xrmLt5ZpihM+Ked2Iheb0vjcAMP+KOvbGX43nq5Gq mrXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680065858; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dDDQpcu4wlArU6dZYMfeUKSEQsg6b1kdryOeCUrFQlc=; b=RfZNUQkjwl9QEvcdCf2GlZ1MyyazL3zdBhvAkEc2ZmzgtOFP6xX8F5nvsr9xmJzGYe 3oH1iz5QYy6m5mejkcgqyis7/gweFskI/f4CRHGFISP3zDHfRliK3BH8yQtTGNaNDkls GcLN925p3SxRMXVLY22iaLpGoKX9XQfvl01jo54wqGVDn1NkvSsVtr47ZU5VMNcfWMih 3l1cSt1u/y180KWLF7w2iuz/iS2982nDOjMRHg3RxiuO7TXvWkXWkLuCUwFVMbheEb55 diCxTifWl10at+vyiIbq6eOArh+o+dPQhzAu8MTUis7LcbDTMVIU8KzeG4V+6EiRO8Or Y/Ow== X-Gm-Message-State: AAQBX9eXBay6VtCb9JVUHryRg8eWHb6AhMEHq5k1BXmY6iELihjiEbJQ qxAcgnGSi6wdjJU3kW90nOuPoA== X-Google-Smtp-Source: AKy350Zwf3+/m0WcA/QJKFN+KuVJqUrDEjpA3YZZy+HJLAgtX0Af6zJPQfGj5nLRuIS4Y+NAhHLENw== X-Received: by 2002:a05:600c:3789:b0:3ef:62cd:1ee with SMTP id o9-20020a05600c378900b003ef62cd01eemr11455074wmr.16.1680065858027; Tue, 28 Mar 2023 21:57:38 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id x19-20020a1c7c13000000b003ed1f111fdesm779942wmc.20.2023.03.28.21.57.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 21:57:37 -0700 (PDT) From: Alexandre Ghiti To: Nick Desaulniers , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kbuild@vger.kernel.org Cc: Alexandre Ghiti , Anup Patel Subject: [PATCH v9 4/6] powerpc: Move script to check relocations at compile time in scripts/ Date: Wed, 29 Mar 2023 06:53:27 +0200 Message-Id: <20230329045329.64565-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329045329.64565-1-alexghiti@rivosinc.com> References: <20230329045329.64565-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexandre Ghiti Relocating kernel at runtime is done very early in the boot process, so it is not convenient to check for relocations there and react in case a relocation was not expected. Powerpc architecture has a script that allows to check at compile time for such unexpected relocations: extract the common logic to scripts/ so that other architectures can take advantage of it. Signed-off-by: Alexandre Ghiti Reviewed-by: Anup Patel Acked-by: Michael Ellerman (powerpc) --- arch/powerpc/tools/relocs_check.sh | 18 ++---------------- scripts/relocs_check.sh | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) create mode 100755 scripts/relocs_check.sh diff --git a/arch/powerpc/tools/relocs_check.sh b/arch/powerpc/tools/relocs= _check.sh index 63792af00417..6b350e75014c 100755 --- a/arch/powerpc/tools/relocs_check.sh +++ b/arch/powerpc/tools/relocs_check.sh @@ -15,21 +15,8 @@ if [ $# -lt 3 ]; then exit 1 fi =20 -# Have Kbuild supply the path to objdump and nm so we handle cross compila= tion. -objdump=3D"$1" -nm=3D"$2" -vmlinux=3D"$3" - -# Remove from the bad relocations those that match an undefined weak symbol -# which will result in an absolute relocation to 0. -# Weak unresolved symbols are of that form in nm output: -# " w _binary__btf_vmlinux_bin_end" -undef_weak_symbols=3D$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }') - bad_relocs=3D$( -$objdump -R "$vmlinux" | - # Only look at relocation lines. - grep -E '\ X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49A02C6FD18 for ; Wed, 29 Mar 2023 04:58:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229659AbjC2E6p (ORCPT ); Wed, 29 Mar 2023 00:58:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjC2E6n (ORCPT ); Wed, 29 Mar 2023 00:58:43 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DEC62723 for ; Tue, 28 Mar 2023 21:58:40 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id q19so11256231wrc.5 for ; Tue, 28 Mar 2023 21:58:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680065919; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZSs9kTLEr3gcw2nAKoZU9JNtCpmjcd0KenqM1ZfQgo4=; b=lO1y4yq1PwBxe+8AJb6u6BDoMPFaCMy/BL0GvwPGmyd2EIze0IcibTOK/QPflRVmnA hFY/Ukba0Dp3YB+iOiOOfTTbrDUDqxk57w8qLlZaBwAVPw6orKn+jDfJTUabT0vjO0Gs MfPreJqaqCjbjpaep9XRh3Fz1bImQeDyizQ6Au5kaxkj2sVzhTrc2F4yTwlM9Y6JNti6 3AGBBA0b/J0E8pFGlRtm4995Qb3cbWvFO5oIp26KsHCaNh2+toBq9Ct/1yIrbic4HaBy WkA3LLbqfJ8UkkYCF0H5KXjY5f/NBPGG0LChQRG7/6oYxj54WKXo7p7eTNs+mi7QSLNx xG8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680065919; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZSs9kTLEr3gcw2nAKoZU9JNtCpmjcd0KenqM1ZfQgo4=; b=XXG6HSVjcLBiMpu5RLzsYUH9Nyo4plspnYjvc9Jj3B6FMxEk2K3WTAjn1o7BJ7essb YkqUXIjMFlClsQTqTmd43vSIx67faA4uE4g88AqSGmvEO2pdbhqV1S8Sen1CXOT2uUow azixHeSArgWQyEceA0eOJqeUl3G3m/C8kKLalwNhtpo8tEAjICEjfM/HowV4qjKKf+ki 5NQVL5y5iTq1/K6bRkHxvEAI/8lso6nVVnmhZ1/kL2o/i8k8fotIAziuC7dzOj2y2iQ2 qjDuE6fro681d7IAwCkYnh/8RigdTpBU32Lkc6Q8gWOQ5pTkFprTlMNxNSBB0RLKKnTb f/rA== X-Gm-Message-State: AAQBX9ciJdZ+JQVWN+EI7QRVc9HQCiTkY2SZBZJrjGhRgbh4yAeZPHb0 NBSdGzeLOtvafq4hpsz/LTSdZQ== X-Google-Smtp-Source: AKy350Z51j1bBKrXk97y5JX1wSgTTowVMS1BaZOjNaHlHomGdJnrKLmbOfwk1rsbLBsYnb7mc20NEQ== X-Received: by 2002:adf:eace:0:b0:2cf:e3d0:2a43 with SMTP id o14-20020adfeace000000b002cfe3d02a43mr16161943wrn.4.1680065919124; Tue, 28 Mar 2023 21:58:39 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id p12-20020a5d48cc000000b002d431f61b18sm27284287wrs.103.2023.03.28.21.58.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 21:58:38 -0700 (PDT) From: Alexandre Ghiti To: Nick Desaulniers , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kbuild@vger.kernel.org Cc: Alexandre Ghiti , Anup Patel Subject: [PATCH v9 5/6] riscv: Check relocations at compile time Date: Wed, 29 Mar 2023 06:53:28 +0200 Message-Id: <20230329045329.64565-6-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329045329.64565-1-alexghiti@rivosinc.com> References: <20230329045329.64565-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Alexandre Ghiti Relocating kernel at runtime is done very early in the boot process, so it is not convenient to check for relocations there and react in case a relocation was not expected. There exists a script in scripts/ that extracts the relocations from vmlinux that is then used at postlink to check the relocations. Signed-off-by: Alexandre Ghiti Reviewed-by: Anup Patel --- arch/riscv/Makefile.postlink | 36 ++++++++++++++++++++++++++++++++ arch/riscv/tools/relocs_check.sh | 26 +++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 arch/riscv/Makefile.postlink create mode 100755 arch/riscv/tools/relocs_check.sh diff --git a/arch/riscv/Makefile.postlink b/arch/riscv/Makefile.postlink new file mode 100644 index 000000000000..d5de8d520d3e --- /dev/null +++ b/arch/riscv/Makefile.postlink @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: GPL-2.0 +# =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D +# Post-link riscv pass +# =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D +# +# Check that vmlinux relocations look sane + +PHONY :=3D __archpost +__archpost: + +-include include/config/auto.conf +include $(srctree)/scripts/Kbuild.include + +quiet_cmd_relocs_check =3D CHKREL $@ +cmd_relocs_check =3D \ + $(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" = "$(NM)" "$@" + +# `@true` prevents complaint when there is nothing to be done + +vmlinux: FORCE + @true +ifdef CONFIG_RELOCATABLE + $(call if_changed,relocs_check) +endif + +%.ko: FORCE + @true + +clean: + @true + +PHONY +=3D FORCE clean + +FORCE: + +.PHONY: $(PHONY) diff --git a/arch/riscv/tools/relocs_check.sh b/arch/riscv/tools/relocs_che= ck.sh new file mode 100755 index 000000000000..baeb2e7b2290 --- /dev/null +++ b/arch/riscv/tools/relocs_check.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0-or-later +# Based on powerpc relocs_check.sh + +# This script checks the relocations of a vmlinux for "suspicious" +# relocations. + +if [ $# -lt 3 ]; then + echo "$0 [path to objdump] [path to nm] [path to vmlinux]" 1>&2 + exit 1 +fi + +bad_relocs=3D$( +${srctree}/scripts/relocs_check.sh "$@" | + # These relocations are okay + # R_RISCV_RELATIVE + grep -F -w -v 'R_RISCV_RELATIVE' +) + +if [ -z "$bad_relocs" ]; then + exit 0 +fi + +num_bad=3D$(echo "$bad_relocs" | wc -l) +echo "WARNING: $num_bad bad relocations" +echo "$bad_relocs" --=20 2.37.2 From nobody Thu Sep 11 22:33:07 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D85FC6FD18 for ; Wed, 29 Mar 2023 04:59:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229768AbjC2E7p (ORCPT ); Wed, 29 Mar 2023 00:59:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjC2E7n (ORCPT ); Wed, 29 Mar 2023 00:59:43 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCE672723 for ; Tue, 28 Mar 2023 21:59:41 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id d11-20020a05600c3acb00b003ef6e6754c5so5205695wms.5 for ; Tue, 28 Mar 2023 21:59:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680065980; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=l2nyBWkEl893cb8p3Xcv9ddibZeqm3dx5h/n4mJzM0I=; b=a+A3vmtN33rV7G2K+kBT5+09RNfw/x9RSy+q2X3PBbr29xgkn5AQt12Fbediy1rrcC iHU5gwK6JXKuap5Q1K7ZWEHCpYah/bxgqzwBr1yWkZ7mh7r58y1ARcbbZcV8tzEpyFZf VDgXJr/tDJG2F0g9ahkj0NkZFJvy+ATuc/EtdJZvDtU/skKpBzTkNMEHgFcM7dZ0S8Er 0eHpqVt7Wim1e1rCAGgxA04h6ZrRWhIYTlP6NK+v9auLZyoJkLfTpVcTWrSdLfMq4V8r hjcdeqmxZeOMiEHhoNumsB/hafrv2w9DmhB2P7mDfgctEKEXOeNUvMmKfuAJRA7CSlDX 2FeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680065980; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l2nyBWkEl893cb8p3Xcv9ddibZeqm3dx5h/n4mJzM0I=; b=m3F7axumW1QXQ/2zAlisehYwotoK9/eOoXgal6MMG9NInYbqk2htMbMeQPd99gk0yf y4oNsuzyA5YuTqs0EoriS3G0G4WniT7RArhc3H6vsf1dbHtihzYQcloau9ZHUo1l9wE3 LupAn0sT1mADh5oU7rWo4bQUg5DKVQmLue4I3D8EE/5576YkiFnT2SjpQq9/rntUypzV YeI5xg2qAj1hbm8H42XLIMkntQqqslRkCOw0Lly2oCCDZPtuOCdGN/PdMohy7hv6L3oq WDXaR3PS2sCYhcAvc94IJYQPLjuUDtGdaNsv7HboIsoWzUxK/Q7Z7owPRs6q8X6xbU80 N+2g== X-Gm-Message-State: AO0yUKWxBmkxpBPOgnmMmppkHbXHn/V19RqsZXtEHJUv8t6O4vTEWgLQ c2mIHl19CkXrK0k/EfZO0T6Trg== X-Google-Smtp-Source: AK7set/hrrFzd6yhsYIengKEXesiDvT2OHC1fUfzzUtI8h99/a73zFv/Utk9ctYdbI75FCHmhSsncw== X-Received: by 2002:a05:600c:209:b0:3ee:282d:1016 with SMTP id 9-20020a05600c020900b003ee282d1016mr13218010wmi.25.1680065980323; Tue, 28 Mar 2023 21:59:40 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id q18-20020a7bce92000000b003ef71d541cbsm842566wmj.1.2023.03.28.21.59.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 21:59:39 -0700 (PDT) From: Alexandre Ghiti To: Nick Desaulniers , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kbuild@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH v9 6/6] riscv: Use --emit-relocs in order to move .rela.dyn in init Date: Wed, 29 Mar 2023 06:53:29 +0200 Message-Id: <20230329045329.64565-7-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329045329.64565-1-alexghiti@rivosinc.com> References: <20230329045329.64565-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To circumvent an issue where placing the relocations inside the init sections produces empty relocations, use --emit-relocs. But to avoid carrying those relocations in vmlinux, use an intermediate vmlinux.relocs file which is a copy of vmlinux *before* stripping its relocations. Suggested-by: Bj=C3=B6rn T=C3=B6pel Suggested-by: Nick Desaulniers Signed-off-by: Alexandre Ghiti --- arch/riscv/Makefile | 2 +- arch/riscv/Makefile.postlink | 13 +++++++++++++ arch/riscv/boot/Makefile | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 860b09e409c7..7dc6904a6836 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -8,7 +8,7 @@ =20 OBJCOPYFLAGS :=3D -O binary ifeq ($(CONFIG_RELOCATABLE),y) - LDFLAGS_vmlinux +=3D -shared -Bsymbolic -z notext -z norelro + LDFLAGS_vmlinux +=3D -shared -Bsymbolic -z notext -z norelro --emit-relocs KBUILD_CFLAGS +=3D -fPIE endif ifeq ($(CONFIG_DYNAMIC_FTRACE),y) diff --git a/arch/riscv/Makefile.postlink b/arch/riscv/Makefile.postlink index d5de8d520d3e..a46fc578b30b 100644 --- a/arch/riscv/Makefile.postlink +++ b/arch/riscv/Makefile.postlink @@ -15,12 +15,25 @@ quiet_cmd_relocs_check =3D CHKREL $@ cmd_relocs_check =3D \ $(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" = "$(NM)" "$@" =20 +ifdef CONFIG_RELOCATABLE +quiet_cmd_cp_vmlinux_relocs =3D CPREL vmlinux.relocs +cmd_cp_vmlinux_relocs =3D cp vmlinux vmlinux.relocs + +quiet_cmd_relocs_strip =3D STRIPREL $@ +cmd_relocs_strip =3D $(OBJCOPY) --remove-section=3D'.rel.*' \ + --remove-section=3D'.rel__*' \ + --remove-section=3D'.rela.*' \ + --remove-section=3D'.rela__*' $@ +endif + # `@true` prevents complaint when there is nothing to be done =20 vmlinux: FORCE @true ifdef CONFIG_RELOCATABLE $(call if_changed,relocs_check) + $(call if_changed,cp_vmlinux_relocs) + $(call if_changed,relocs_strip) endif =20 %.ko: FORCE diff --git a/arch/riscv/boot/Makefile b/arch/riscv/boot/Makefile index c72de7232abb..22b13947bd13 100644 --- a/arch/riscv/boot/Makefile +++ b/arch/riscv/boot/Makefile @@ -33,7 +33,14 @@ $(obj)/xipImage: vmlinux FORCE =20 endif =20 +ifdef CONFIG_RELOCATABLE +vmlinux.relocs: vmlinux + @ (! [ -f vmlinux.relocs ] && echo "vmlinux.relocs can't be found, please= remove vmlinux and try again") || true + +$(obj)/Image: vmlinux.relocs FORCE +else $(obj)/Image: vmlinux FORCE +endif $(call if_changed,objcopy) =20 $(obj)/Image.gz: $(obj)/Image FORCE --=20 2.37.2