From patchwork Wed Mar 29 04:53:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13191881 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D531CC6FD18 for ; Wed, 29 Mar 2023 04:54:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=v8zlL2gbRGFL59LHEbZZ3DJoQm0JTT85nUi6fAD50xo=; b=1OX/4WP5NXV71J QEpHnNww7DuRVT2qMSghppDNQlAYttADj83Y0HqA9cvSWnZsXAdC+hIw9xaqtmoRe09aooDLxJOyd zMIluhpRIUt+e7fGP4SNaKAxz4NhDr8C2sLG0JBSKdkoL2yz4/a1Rt4fvhnS7Q4/qs5S/Viqnl5wb 4NRMkU2PbqR1AoirSxkcpIPIj4W/vpt9ZLzdZ6/bXU4j6tdZQZB86zRUzLWy9cE9rTBAxiwCIgK45 /vYPf5Q+0BJv42EmYyh02Kjw3KIgNjQL+vIHLsHVwIQUWWbrVlgPMq2bfz21fFUy2Wlm/fkHYrsxX rLDCjqlJqVvlhr3lE0Yw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phNpX-00Gbeo-1b; Wed, 29 Mar 2023 04:54:39 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phNpU-00Gbdp-1p for linux-riscv@lists.infradead.org; Wed, 29 Mar 2023 04:54:38 +0000 Received: by mail-wm1-x333.google.com with SMTP id o32so8112760wms.1 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=PR9k3YnN0zl1X6sa5YJLjIQ64TICeqWdMSH1CocVf3dZOEH4YL6a/GC9dfoozC6fqN cbU/F+Pgt7J8QXNP9V/rtg/ttVsQIexyFuZ5m9eedgu0P+8ymTfSwEScc9IslbwiP70Q tfaZr7XLvPQVIaepWB0N2mep6TQnAbZb4nNnJI9jcaNCSOasRN2ofditBhDDqiJ3MtxR 7MASn0RPm5FgLW+H7XVK9wjy8Tnh15HxEloS8TcpkYXMZlszcA1t2aE6N9BSF/OsdVKf AUmNlFcxZUdMCGZG+7qBQKZ2RYJJVyUqBjGHUww5Vx5oqd0jxBdBTeA7VI4azsVvTJ5W /Zow== X-Gm-Message-State: AO0yUKVPCPbwXQgDWfKslAEoqqbLcgWyrDQzYUXxezStxuWY6KorrL06 ltc24Qf6CmVei5u3BbCACgFwkQ== 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?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_215436_609412_7F280D73 X-CRM114-Status: GOOD ( 12.59 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/set_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 */ +#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 @@ #include #include +#include .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 .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 .long 0 // PointerToRelocations diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.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) jiffies = jiffies_64; -PECOFF_SECTION_ALIGNMENT = 0x1000; -PECOFF_FILE_ALIGNMENT = 0x200; - SECTIONS { /* Beginning of code and text segment */ @@ -132,6 +129,7 @@ SECTIONS #ifdef CONFIG_EFI .pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); } __pecoff_data_raw_size = ABSOLUTE(. - __pecoff_text_end); + __pecoff_data_raw_end = ABSOLUTE(.); #endif /* End of data section */ @@ -142,6 +140,7 @@ SECTIONS #ifdef CONFIG_EFI . = ALIGN(PECOFF_SECTION_ALIGNMENT); __pecoff_data_virt_size = ABSOLUTE(. - __pecoff_text_end); + __pecoff_data_virt_end = ABSOLUTE(.); #endif _end = .; From patchwork Wed Mar 29 04:53:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13191882 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2EF14C74A5B for ; Wed, 29 Mar 2023 04:55:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=BmrnxjutQiW8FvuB2rChVonUIuISlWQ1Ek6puX3kJqs=; b=NgPSWqX9CH07Ug j8szNH8nIuvWaRnh0hBzVdRWtHO7kHvG3/Ldg3Eqz555W6BzOZRKSkYj697lObAqbsTDoELulaTBZ k8+O75TMT7F0cZSPl8TooZVcu2/SN+SmQFMc1ZlWheaFi4YYXglKhsJPMCABZKXyVmgc/jDoazeKY RKJNwUNWDPwwExDtqvw1GBtIcamTqYuo5Al7f1fcAsnEHP5eeA+VoAH6CjqgY4F3nULfoVLa9jNdn Ssifr63f2nXUIQl2eVPP1YTkc2c391L/flNaxhPadgVByW/9NnO2oY9qsPG8M+be5X2i9iscfgdjb +zkrft1apbNf8nW+m/pQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phNqX-00Gbq9-1v; Wed, 29 Mar 2023 04:55:41 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phNqU-00Gbox-00 for linux-riscv@lists.infradead.org; Wed, 29 Mar 2023 04:55:39 +0000 Received: by mail-wm1-x329.google.com with SMTP id bg13-20020a05600c3c8d00b003ef90adc168so998459wmb.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=3LukP/3X25FDaHTwhvfYrmfo3bGevZFsBt10P4o0vOzIpl7qIvxXgT8kWt8lk/36lD UGUn9oNj08pztngbwB3z/Echdx2x2vxXd8pGdfJpheq6ST0tz2jVrbJO+DZVChru74Hr xiqoCDuW3bs97m9+MeoSqMarQ2203uQJd++h7CotJb4tdGpKZ2K5VZIKUdXSS+9oyp/q 6ToV6BMMvCxwNP8vPgZ495qopFJlcN2PsXPXBmUxRvUbV51CmtyoaL2rI+xH5LfPxftU kx2HIbFzpUW6EOxloeYLOpdIJzCrxeRwhbN8ZMgjuDCNKQ4cKqvtzKfpJiJZ0csDzFbt 5bbA== X-Gm-Message-State: AO0yUKVOqzRguMV5GWhFOvXlrVgHqCEy6BjoYYTZb3LuIKjmvTs8cf/l pOT9od9HZkwi+nwbMQIWsMz1fw== 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?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_215538_037048_BC2B390A X-CRM114-Status: GOOD ( 10.67 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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.lds.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*) } - .rela.dyn : { - *(.rela*) - } - __init_data_end = .; . = ALIGN(8); @@ -126,6 +122,10 @@ SECTIONS *(.sdata*) } + .rela.dyn : { + *(.rela*) + } + #ifdef CONFIG_EFI .pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); } __pecoff_data_raw_size = ABSOLUTE(. - __pecoff_text_end); From patchwork Wed Mar 29 04:53:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13191883 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AB710C6FD18 for ; Wed, 29 Mar 2023 04:56:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=b+OfHud2AwsuyXadefLejRn3QcZxGTsjiSCzEcx0eRk=; b=pvggRirlMf79RZ NI124qcAI1EzlhXszG6wqvJNJnmFtjYt9SSFKIbGSbAoN+Y+douQGhvav31ZAEXoDBkrYAbo0rryV mpDsHpieZHbjVQgttNPKLOT/t2EJDR3RCiVYn7HeR09DsV8YY0+tmxY5FZTemeru05TsU+QJesXng 6SrgzDYAKf9mCwqnyZrWyDbw8qVUOv0rEvpCG2xEWyPc0dMbV5ZfLqQwYJDntUhhuunzFD0PQjQLV Bz/L2AHSjZDP7ASLvZ05S3bbUi16mC2s/puH2x/lOhPPjmB917Pa2se4iMNBHFpvJedhdzdQy7n0F A6i7JhO6ne3b2QJPQCsA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phNrW-00Gc1F-2G; Wed, 29 Mar 2023 04:56:42 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phNrT-00GbzS-08 for linux-riscv@lists.infradead.org; Wed, 29 Mar 2023 04:56:40 +0000 Received: by mail-wr1-x435.google.com with SMTP id m2so14323739wrh.6 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=O8rV58qU4PNAvmzhce7M5+GCw4DqLQb1NwfAvYjaAi28DBUOnQJIcyCKp6aqaeTHKo JQNV9fykG/Qr8TdyfqPuVsJ4Jnk9w1BM4TkseD+xPhVn/CabAsS872Cpr4oCMctfv2K8 I1mvA023l0RPJYizprzQkE7fQTjled7dhaM+nVtbKKWgO0DVVm1VeIcZQFs7w1BPeM1b XIM3Jm+Sm9zBaMRkmqrYNQ/AOmvViGplmhNGdbJPLY0eqo3L3H8SSbaj3UKbCyw2xKDy 4nWoel1ONKtbJ9sIOlIY0X4Enn+pLDHNob1QdsIaeSSo4Y6WjmHD0sbp5OVrR3coJPyz jHLA== X-Gm-Message-State: AAQBX9cVvCgrc/cF4NYNqRCu3nQCkJTVA85HdE0sPj4QYVRExhkLgQIg WU76ZO9xGukFwUD0mZdFBmWdYA== 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?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_215639_080029_D6DC408C X-CRM114-Status: GOOD ( 22.05 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 If you want to execute 32-bit userspace applications, say Y. +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" 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 @@ # OBJCOPYFLAGS := -O binary -LDFLAGS_vmlinux := +ifeq ($(CONFIG_RELOCATABLE),y) + LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro + KBUILD_CFLAGS += -fPIE +endif ifeq ($(CONFIG_DYNAMIC_FTRACE),y) - LDFLAGS_vmlinux := --no-relax + LDFLAGS_vmlinux += --no-relax KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY ifeq ($(CONFIG_RISCV_ISA_C),y) CC_FLAGS_FTRACE := -fpatchable-function-entry=4 diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.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*) } - .rela.dyn : { - *(.rela*) + .rela.dyn : ALIGN(8) { + __rela_dyn_start = .; + *(.rela .rela*) + __rela_dyn_end = .; } +#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); . = ALIGN(PECOFF_FILE_ALIGNMENT); } __pecoff_data_raw_size = 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 CFLAGS_init.o := -mcmodel=medany +ifdef CONFIG_RELOCATABLE +CFLAGS_init.o += -fno-pie +endif + ifdef CONFIG_FTRACE CFLAGS_REMOVE_init.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_cacheflush.o = $(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 #include #include @@ -146,7 +149,7 @@ static void __init print_vm_layout(void) print_ml("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END); #endif - 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 +#ifdef CONFIG_RELOCATABLE +extern unsigned long __rela_dyn_start, __rela_dyn_end; + +static void __init relocate_kernel(void) +{ + Elf64_Rela *rela = (Elf64_Rela *)&__rela_dyn_start; + /* + * This holds the offset between the linked virtual address and the + * relocated virtual address. + */ + uintptr_t reloc_offset = 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 = KERNEL_LINK_ADDR - kernel_map.phys_addr; + + for ( ; rela < (Elf64_Rela *)&__rela_dyn_end; rela++) { + Elf64_Addr addr = (rela->r_offset - va_kernel_link_pa_offset); + Elf64_Addr relocated_addr = rela->r_addend; + + if (rela->r_info != 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 >= KERNEL_LINK_ADDR) + relocated_addr += reloc_offset; + + *(Elf64_Addr *)addr = 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 +#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.size); + relocate_kernel(); +#endif + apply_early_boot_alternatives(); pt_ops_set_early(); From patchwork Wed Mar 29 04:53:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13191884 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 64448C74A5B for ; Wed, 29 Mar 2023 04:57:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jyTjhjGOLseZxH8AQQMNYc6eciw0j2lDnqa5P2S03HI=; b=NoEdJdBrmnU0Ia GuKTAG8+8to/v+/a1KjSq7xKPjHuXUrorfzUmDNzlkT1yn/WrfHSoSxu8bg+4o52qxuW1b3tD7jR5 p0tGI/GRF4sm7cMTtYxX/tOjo1xJ1TiQ7wj4tjBv8yoNo6fBuBlgk/kK2GHS1iGklhIwmhiH+/9TW BPcdEajkzxn4RBlsEwcRjTzRPRMWOz6/0wiueMk2QkAColZYCs4SsIJJbUd8nMEWGB+OfcOLcxTph mL7Jo05WB8ORPDTawXDgYfHazstYGaXs1P48oxsljnhht4/J/gvub4w9we8Ds/64emIeU7PkzT7I7 Gq65Vn5kBS5YzrNJSzww==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phNsX-00GcBf-2a; Wed, 29 Mar 2023 04:57:45 +0000 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phNsU-00GcAB-1t for linux-riscv@lists.infradead.org; Wed, 29 Mar 2023 04:57:43 +0000 Received: by mail-wm1-x32c.google.com with SMTP id n19so8133513wms.0 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=SdOAL/tlGfI1KD/KkzSFmxjhKCnHZUxK0RZbSX3mm0kDVC2PTR2g1AGDhGilQAnLw7 KrbnGj/6mFBBYaSF8VaQakwa1lkqAoxCguhlhaSwv76NHbqbXd21P8VWh9i9nRWPIqD+ l7XlsihF5MA4fPyQa5/jxl1qfcYo7dySs2kRykHtNQJPR+FxIr6PINgZfbTYNE3Hr00b yUptLzpg+1Obw5OsA+8AjjApVUo5V9e1oZOJOruJDZeBTe1akVGjz43Xm7HOYDx2DywP mDkzWR9EBpFT8DTSo+O/eL9yxN+CzkAzBQu8tuJ+wW7KRI3gdL8axPHJKIl9Ckz1gNYx f6Rg== X-Gm-Message-State: AAQBX9elw2qgDZMwhsygPu4NZuM0MbrTFbrdh8WmfAK/FVPKT+XQgCPC iDrk85oQI9KS9FbAuzWrfUFKFw== 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?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_215742_629362_BABE68B2 X-CRM114-Status: GOOD ( 15.07 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.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 -# Have Kbuild supply the path to objdump and nm so we handle cross compilation. -objdump="$1" -nm="$2" -vmlinux="$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=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }') - bad_relocs=$( -$objdump -R "$vmlinux" | - # Only look at relocation lines. - grep -E '\ X-Patchwork-Id: 13191888 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3C085C6FD18 for ; Wed, 29 Mar 2023 04:58:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=RNfHB3lnGeDc+Uc4S8hFq4VzLyDRQka5WXdTgqf7M1c=; b=AXnULGUH3nJtJ3 /AvUKuUiA9k2CK5fQccOsCMNGjRR5Lc++R/VHBpLvIsnTnI6mRu3Z+uhujlq9dmlX0hy435JMSksY 4NqiMAb8B6PyoiaR/cAAAglJPaBeAstdR3IsQBme2k7I5RDf4EtiNOq75my/PMs4hXCkuwIgfxo+V U+qHkmAxAQv2EU9eQ37YWLa9Az9s4WFPaPpWUOI2GRm6Lrq9McE1IiyxpENJCcqdzTLwfC5XnmJv5 wHVAu0+8qaRs15oAI/Xbj4S3ovl+nC3+xz8IzzGBcc8h29jjpY3hwagk3cQe8WRwWG8+H8x8SkuOE Zlrh5bY2+EMElrgxLwBQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phNtT-00GcLu-2y; Wed, 29 Mar 2023 04:58:43 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phNtR-00GcL3-0f for linux-riscv@lists.infradead.org; Wed, 29 Mar 2023 04:58:42 +0000 Received: by mail-wr1-x42e.google.com with SMTP id v1so14339658wrv.1 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=aPBg8PlvvKw4/jZq/7eOkBhNS6XLE42Ueo/GoLsLsR5CNYBQuTi146jZr6jpCOhRmm lshtkZnpgFiL+cEwIy5sD1AMQA8Sed21O3dkijKu+8XH6R076NISrwd7ZhlA7PccqeUD eHVY7sRnoMvzCOcaIAS9s0w5u1RQjQrF0DMMldqRxoLdWF4U4ElX85oNqRdNIvruIWZk jnk5YJHRamqLhdfHJg9Bu8Bg9/kUvlxamnW8X4zNHQcqOc3kgqNpI2v78vTPQ4synQta fzAPZjBlE6qa3KEDPomShgUyfeluaavu27igWPPOThZMNtd3QMF3UQBndYCRfYs/3qKx ieqg== X-Gm-Message-State: AAQBX9fA+kOAwPzMvuEFQJUmD9DNZZZ96fyZW4nCL4wKKRVvwwJFo+VX ukm9o8pcb548bgz45gVw7WQZvw== 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?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_215841_242558_2651E888 X-CRM114-Status: GOOD ( 14.45 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.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. 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 +# =========================================================================== +# Post-link riscv pass +# =========================================================================== +# +# Check that vmlinux relocations look sane + +PHONY := __archpost +__archpost: + +-include include/config/auto.conf +include $(srctree)/scripts/Kbuild.include + +quiet_cmd_relocs_check = CHKREL $@ +cmd_relocs_check = \ + $(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 += FORCE clean + +FORCE: + +.PHONY: $(PHONY) diff --git a/arch/riscv/tools/relocs_check.sh b/arch/riscv/tools/relocs_check.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=$( +${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=$(echo "$bad_relocs" | wc -l) +echo "WARNING: $num_bad bad relocations" +echo "$bad_relocs" From patchwork Wed Mar 29 04:53:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13191889 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4FF1BC761AF for ; Wed, 29 Mar 2023 04:59:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=W+dFR/Sq2DedrHg3WxDfJ/MSTsBvsypZOhz7mH6wtjc=; b=CN7zr08AmkrGyf 5kdqO5vaKgzpXf1CyUq7SE7ZVikK3IOti5VzEcbXLRYLdODPdxIeQbuWi9RRNXIQYg1mCrOH8hU8v ZH+cgxDuO8oAs1odvr8oXVzQii4iO0cOgO996I5SThi0LwNSmDV/fiT0OnMewhuFwEoDNdhNKa4B0 hgE3wAmJxBBnbalViwfCSkskqnVWqvnGwGJmduKSxGfiARzPepkiGWg2X6fkLZVmL+XkkNP9Psr8+ ZGX5/mfSVYS44G4ywvOeW+8GfvR/IILJtLDfDq7363eIm26oc1DcT466kDdQO9kKQIkfu7/DE6rvz hP3KHlXsc5IKoJpFPj+w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phNuT-00GcZ6-01; Wed, 29 Mar 2023 04:59:45 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phNuP-00GcYJ-2e for linux-riscv@lists.infradead.org; Wed, 29 Mar 2023 04:59:43 +0000 Received: by mail-wm1-x32a.google.com with SMTP id i5-20020a05600c354500b003edd24054e0so10648725wmq.4 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=vOkdxPIf/FPDYmOmcMVbGxBYUD8F2VNcmDmKSvRJu3sUTZBn1QjieRJqmh0+6iLTRc U6y9dgV7r6rEyZuCFqNU/h9m5l/GWspKwadpGOIRs9fg9gZepOr6v5b4QFDRPn2c27tL xOqswV1kNP+RxaKRRbhEe+2bIrhiqVYQSAshvOrr8rU4jtTSz+oG1WpBDGcVaBshR7Qq kLb81LC3/knuqhoW3n+dOf/ojGPcWZ2jfUk9jQ8juMLI2tX3mEP7LdGUCYFQnFyytmBW 7YhXzB6lujo44s4AekK5WxTDBa7CLeovhkRHk+3i6W+X8G/8VYvoHbfxXRF8dukAKFcK zpfw== X-Gm-Message-State: AO0yUKUcNpgBp6tLFcAsThc3Jn28R4S7QmQIgDVP7iKjHoWkX0CnNxRs PF15wngIhw2kTUvOxkFvs8lS2+mWIQlr7l4VFC0= 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?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_215941_859537_2683B70A X-CRM114-Status: GOOD ( 11.90 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.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örn Töpel 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 @@ OBJCOPYFLAGS := -O binary ifeq ($(CONFIG_RELOCATABLE),y) - LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro + LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro --emit-relocs KBUILD_CFLAGS += -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 = CHKREL $@ cmd_relocs_check = \ $(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" "$(NM)" "$@" +ifdef CONFIG_RELOCATABLE +quiet_cmd_cp_vmlinux_relocs = CPREL vmlinux.relocs +cmd_cp_vmlinux_relocs = cp vmlinux vmlinux.relocs + +quiet_cmd_relocs_strip = STRIPREL $@ +cmd_relocs_strip = $(OBJCOPY) --remove-section='.rel.*' \ + --remove-section='.rel__*' \ + --remove-section='.rela.*' \ + --remove-section='.rela__*' $@ +endif + # `@true` prevents complaint when there is nothing to be done vmlinux: FORCE @true ifdef CONFIG_RELOCATABLE $(call if_changed,relocs_check) + $(call if_changed,cp_vmlinux_relocs) + $(call if_changed,relocs_strip) endif %.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 endif +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) $(obj)/Image.gz: $(obj)/Image FORCE