From patchwork Sat Oct 7 17:04:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13412427 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 8785DE95A61 for ; Sat, 7 Oct 2023 17:04:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344054AbjJGRE6 (ORCPT ); Sat, 7 Oct 2023 13:04:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344032AbjJGRE6 (ORCPT ); Sat, 7 Oct 2023 13:04:58 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6921B9; Sat, 7 Oct 2023 10:04:55 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22F07C433C8; Sat, 7 Oct 2023 17:04:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696698295; bh=5rWN3r1QrWbZPdwY53XXoqrpI9vkxw4GAZMoimstNTU=; h=From:To:Cc:Subject:Date:From; b=ne3VlCVVWtdJlPXT1Sksj5UMIr8o7iegZVtoYOu+WNb8oD72PHoQ0DS3n7xxd451P dKdElA/aK5liU8cGmISK7mIQ9tWuwrGKuVcwzRtyy7sOXQ8HMMOE/ptiKzqX7HTmTW Ki0wW+XN3u0oZ1BDbkug60VpfBjvKSyWoH2aE0nXbf45BHbWtmw7WcA3mreTtwoLea 85+bV00WxR29ecLxzHVzx/s36oLAQ2oZbHqIs6fwmQY1K4q+ynlVtxQf+p+FRy1lpG blsa/RQzXTZe2pisEPrHwh3T9zohUTugXF2q9o1uW+YHKhrolJy1lBesq+GXCoJzLO WiWvd4yF+jkgw== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Bhupesh Sharma , Daniel Thompson , Jens Wiklander , Nathan Chancellor , Nick Desaulniers , Nicolas Schier , Sumit Garg Subject: [PATCH 1/5] modpost: fix tee MODULE_DEVICE_TABLE built on big endian host Date: Sun, 8 Oct 2023 02:04:44 +0900 Message-Id: <20231007170448.505487-1-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org When MODULE_DEVICE_TABLE(tee, ) is built on a host with a different endianness from the target architecture, it results in an incorrect MODULE_ALIAS(). For example, see a case where drivers/char/hw_random/optee-rng.c is built as a module. If you build it on a little endian host, you will get the correct MODULE_ALIAS: $ grep MODULE_ALIAS drivers/char/hw_random/optee-rng.mod.c MODULE_ALIAS("tee:ab7a617c-b8e7-4d8f-8301-d09b61036b64*"); However, if you build it on a big endian host, you will get a wrong MODULE_ALIAS: $ grep MODULE_ALIAS drivers/char/hw_random/optee-rng.mod.c MODULE_ALIAS("tee:646b0361-9bd0-0183-8f4d-e7b87c617aab*"); This issue has been unnoticed because the ARM kernel is most likely built on a little endian host (cross-build on x86 or native-build on ARM). The uuid field must not be reversed because uuid_t is an array of __u8. Fixes: 0fc1db9d1059 ("tee: add bus driver framework for TEE based devices") Signed-off-by: Masahiro Yamada Reviewed-by: Sumit Garg --- scripts/mod/file2alias.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c index 7056751c29b1..70bf6a2f585c 100644 --- a/scripts/mod/file2alias.c +++ b/scripts/mod/file2alias.c @@ -1348,13 +1348,13 @@ static int do_typec_entry(const char *filename, void *symval, char *alias) /* Looks like: tee:uuid */ static int do_tee_entry(const char *filename, void *symval, char *alias) { - DEF_FIELD(symval, tee_client_device_id, uuid); + DEF_FIELD_ADDR(symval, tee_client_device_id, uuid); sprintf(alias, "tee:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - uuid.b[0], uuid.b[1], uuid.b[2], uuid.b[3], uuid.b[4], - uuid.b[5], uuid.b[6], uuid.b[7], uuid.b[8], uuid.b[9], - uuid.b[10], uuid.b[11], uuid.b[12], uuid.b[13], uuid.b[14], - uuid.b[15]); + uuid->b[0], uuid->b[1], uuid->b[2], uuid->b[3], uuid->b[4], + uuid->b[5], uuid->b[6], uuid->b[7], uuid->b[8], uuid->b[9], + uuid->b[10], uuid->b[11], uuid->b[12], uuid->b[13], uuid->b[14], + uuid->b[15]); add_wildcard(alias); return 1; From patchwork Sat Oct 7 17:04:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13412428 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 5025DE95A64 for ; Sat, 7 Oct 2023 17:05:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344086AbjJGRE7 (ORCPT ); Sat, 7 Oct 2023 13:04:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344046AbjJGRE6 (ORCPT ); Sat, 7 Oct 2023 13:04:58 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12F0BBA; Sat, 7 Oct 2023 10:04:58 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDADAC433C7; Sat, 7 Oct 2023 17:04:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696698297; bh=13ShY6HnWg7Sx2I9hscgBsQOpo9Vq3faGjdLVYoBM5o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eUeSBoRC8ODJTmKIkYd6gNHF/IrKY8Gdzo0GJtWGKXvn/Gwxg66E66zwK+R+L641h RH91OtdShkzWHuMANps+J8Tk/390bUD43IXxpuXrUmzlZ05XTXonzNYEnWWPx7X41M 2yz+S0Ez0O60sxYV+gxrOXChiiB5dI2NdlKFf2mSvqGCjH5w5iO1wDyObNm6wVEK1y 8CafTp2S7rUo6l50QfyjmNHtmXI+FjjRaK7qOO11kQeSR4vpSHgoIaidgiPSnAlanS dThJcqOhH5Y0mQPS4fb8cu0vCfja+eoOcDBKuq1Iab/VnMOtrRwqUseRmQm0ueA0/L 8jdcl8NIkKFpA== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Hans de Goede , Jiri Kosina , Nathan Chancellor , Nick Desaulniers , Nicolas Schier , Srinivas Pandruvada , =?utf-8?q?Thomas?= =?utf-8?q?_Wei=C3=9Fschuh?= Subject: [PATCH 2/5] modpost: fix ishtp MODULE_DEVICE_TABLE built on big endian host Date: Sun, 8 Oct 2023 02:04:45 +0900 Message-Id: <20231007170448.505487-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231007170448.505487-1-masahiroy@kernel.org> References: <20231007170448.505487-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org When MODULE_DEVICE_TABLE(ishtp, ) is built on a host with a different endianness from the target architecture, it results in an incorrect MODULE_ALIAS(). For example, see a case where drivers/platform/x86/intel/ishtp_eclite.c is built as a module. If you build it on a little endian host, you will get the correct MODULE_ALIAS: $ grep MODULE_ALIAS drivers/platform/x86/intel/ishtp_eclite.mod.c MODULE_ALIAS("ishtp:{6A19CC4B-D760-4DE3-B14D-F25EBD0FBCD9}"); However, if you build it on a big endian host, you will get a wrong MODULE_ALIAS: $ grep MODULE_ALIAS drivers/platform/x86/intel/ishtp_eclite.mod.c MODULE_ALIAS("ishtp:{BD0FBCD9-F25E-B14D-4DE3-D7606A19CC4B}"); This issue has been unnoticed because the x86 kernel is most likely built natively on an x86 host. The guid field must not be reversed because guid_t is an array of __u8. Fixes: fa443bc3c1e4 ("HID: intel-ish-hid: add support for MODULE_DEVICE_TABLE()") Signed-off-by: Masahiro Yamada Reviewed-by: Thomas Weißschuh Tested-by: Srinivas Pandruvada Acked-by: Srinivas Pandruvada --- scripts/mod/file2alias.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c index 70bf6a2f585c..6583b36dbe69 100644 --- a/scripts/mod/file2alias.c +++ b/scripts/mod/file2alias.c @@ -1401,10 +1401,10 @@ static int do_mhi_ep_entry(const char *filename, void *symval, char *alias) /* Looks like: ishtp:{guid} */ static int do_ishtp_entry(const char *filename, void *symval, char *alias) { - DEF_FIELD(symval, ishtp_device_id, guid); + DEF_FIELD_ADDR(symval, ishtp_device_id, guid); strcpy(alias, ISHTP_MODULE_PREFIX "{"); - add_guid(alias, guid); + add_guid(alias, *guid); strcat(alias, "}"); return 1; From patchwork Sat Oct 7 17:04:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13412430 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 9BBB0E95A61 for ; Sat, 7 Oct 2023 17:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344118AbjJGRFE (ORCPT ); Sat, 7 Oct 2023 13:05:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344098AbjJGRFB (ORCPT ); Sat, 7 Oct 2023 13:05:01 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE623B9; Sat, 7 Oct 2023 10:04:59 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 23CA5C433C8; Sat, 7 Oct 2023 17:04:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696698299; bh=7PA8scmHfu5nyFOhItoPibMephbX/u9X82eA3NzQP1c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LWnn0sOt6WygtD1hvtwiVIqmKHBDxYUleCmm8Su8hd/lnUJvGVbe6sHfuJl7ZjjA8 pZ96QAL2xYi7Egbrrdjvt0EvwffMJKtblV8mUCndaQJ2RZJAoTQibExZrVfXJykOnj aWSpG0WDrEdYgYLSX33jj/riVgkH8hz3+x/rwrSdyQsPVmBDpQ12ADSFv3EiQADHaY J8cCrKBe64utnoCGPbxGVmreLogcNc4NqaPkbm5XwX4Abes0XcwIAeQBco/YFqve0Q 0kNqxSJHhRmkr4BW2uaIYynAZfKuDg+aL1DvDsjChq5pL746vpys8wjaVLH/1nrUI5 f9e2VYHEtY3FA== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nathan Chancellor , Nick Desaulniers , Nicolas Schier Subject: [PATCH 3/5] modpost: define TO_NATIVE() using bswap_* functions Date: Sun, 8 Oct 2023 02:04:46 +0900 Message-Id: <20231007170448.505487-3-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231007170448.505487-1-masahiroy@kernel.org> References: <20231007170448.505487-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org The current TO_NATIVE() has some limitations: 1) You cannot cast the argument. 2) You cannot pass a variable marked as 'const'. 3) Passing an array is a bug, but it is not detected. Impelement TO_NATIVE() using bswap_*() functions. These are GNU extensions. If we face portability issues, we can port the code from include/uapi/linux/swab.h. With this change, get_rel_type_and_sym() can be simplified by casting the arguments directly. Signed-off-by: Masahiro Yamada Reviewed-by: Nick Desaulniers --- scripts/mod/modpost.c | 13 ++++--------- scripts/mod/modpost.h | 25 ++++++++++++------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 2f3b0fe6f68d..99476a9695c5 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1410,15 +1410,10 @@ static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info, return; } - if (is_64bit) { - Elf64_Xword r_info64 = r_info; - - r_info = TO_NATIVE(r_info64); - } else { - Elf32_Word r_info32 = r_info; - - r_info = TO_NATIVE(r_info32); - } + if (is_64bit) + r_info = TO_NATIVE((Elf64_Xword)r_info); + else + r_info = TO_NATIVE((Elf32_Word)r_info); *r_type = ELF_R_TYPE(r_info); *r_sym = ELF_R_SYM(r_info); diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h index 6413f26fcb6b..1392afec118c 100644 --- a/scripts/mod/modpost.h +++ b/scripts/mod/modpost.h @@ -1,4 +1,5 @@ /* SPDX-License-Identifier: GPL-2.0 */ +#include #include #include #include @@ -51,21 +52,19 @@ #define ELF_R_TYPE ELF64_R_TYPE #endif +#define bswap(x) \ +({ \ + _Static_assert(sizeof(x) == 1 || sizeof(x) == 2 || \ + sizeof(x) == 4 || sizeof(x) == 8, "bug"); \ + (typeof(x))(sizeof(x) == 2 ? bswap_16(x) : \ + sizeof(x) == 4 ? bswap_32(x) : \ + sizeof(x) == 8 ? bswap_64(x) : \ + x); \ +}) + #if KERNEL_ELFDATA != HOST_ELFDATA -static inline void __endian(const void *src, void *dest, unsigned int size) -{ - unsigned int i; - for (i = 0; i < size; i++) - ((unsigned char*)dest)[i] = ((unsigned char*)src)[size - i-1]; -} - -#define TO_NATIVE(x) \ -({ \ - typeof(x) __x; \ - __endian(&(x), &(__x), sizeof(__x)); \ - __x; \ -}) +#define TO_NATIVE(x) (bswap(x)) #else /* endianness matches */ From patchwork Sat Oct 7 17:04:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13412429 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 83149E95A64 for ; Sat, 7 Oct 2023 17:05:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344046AbjJGRFD (ORCPT ); Sat, 7 Oct 2023 13:05:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344105AbjJGRFC (ORCPT ); Sat, 7 Oct 2023 13:05:02 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DB12BA; Sat, 7 Oct 2023 10:05:01 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D892FC433CB; Sat, 7 Oct 2023 17:04:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696698301; bh=TTLYNPGjyBivwozdoa07Smf/suztpzKvRG2LWS6FNas=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b8HZlTL4Afo59CcwyOtgb4Y93r5LNHEiDZEKyeCjOhuDNWTEokl9GgaNHBDGyQl1m k98Volk7bHlMt8cG0kuf/X3YROh35Oz+RhlBeaNSQvs0X0nifaBb4BxkSA5iNpwmTK 9iW+aoM6iguuZVAIQnuN2KeXyNwMyiaGCWXPNG+Sn/NlT+R4sOQLgIvk6685gIjDdx 63f1dugnXyAhbGubHI/ZKunvbkiuu32lOajBgwSdiDrzzZilDkZDZybvabEJOA5Rnc w/pf5cUTLzYO+89crg7fWJMZBsp5CI260rVEUWjRUaoVlTxz8GdyxUWnty3p5PH4Z6 uocIjpipmOmtQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nick Desaulniers , Nathan Chancellor , Nicolas Schier Subject: [PATCH 4/5] modpost: refactor check_sec_ref() Date: Sun, 8 Oct 2023 02:04:47 +0900 Message-Id: <20231007170448.505487-4-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231007170448.505487-1-masahiroy@kernel.org> References: <20231007170448.505487-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org We can replace &elf->sechdrs[i] with &sechdrs[i] to slightly shorten the code because we already have the local variable 'sechdrs'. However, defining 'sechdr' instead shortens the code further. Signed-off-by: Masahiro Yamada Reviewed-by: Nick Desaulniers --- scripts/mod/modpost.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 99476a9695c5..441d57ee3275 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1518,16 +1518,17 @@ static void section_rel(struct module *mod, struct elf_info *elf, static void check_sec_ref(struct module *mod, struct elf_info *elf) { int i; - Elf_Shdr *sechdrs = elf->sechdrs; /* Walk through all sections */ for (i = 0; i < elf->num_sections; i++) { - check_section(mod->name, elf, &elf->sechdrs[i]); + Elf_Shdr *sechdr = &elf->sechdrs[i]; + + check_section(mod->name, elf, sechdr); /* We want to process only relocation sections and not .init */ - if (sechdrs[i].sh_type == SHT_RELA) - section_rela(mod, elf, &elf->sechdrs[i]); - else if (sechdrs[i].sh_type == SHT_REL) - section_rel(mod, elf, &elf->sechdrs[i]); + if (sechdr->sh_type == SHT_RELA) + section_rela(mod, elf, sechdr); + else if (sechdr->sh_type == SHT_REL) + section_rel(mod, elf, sechdr); } } From patchwork Sat Oct 7 17:04:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13412431 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 AB32EE95A65 for ; Sat, 7 Oct 2023 17:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344113AbjJGRFG (ORCPT ); Sat, 7 Oct 2023 13:05:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344120AbjJGRFE (ORCPT ); Sat, 7 Oct 2023 13:05:04 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EF2FB9; Sat, 7 Oct 2023 10:05:03 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9964FC433C8; Sat, 7 Oct 2023 17:05:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696698303; bh=VFhwLN6cPSTo8azgI6QvVm2lvfds7OQ34SuiYHF6MzU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sBynyLUd1/uJaABdRrkJDXMqCBr6rKnMibc1ZccirbcOVP0DwA/e6rFwgRutM+vYO C4kcvfL+Ojlxuec+WLBCy7aRXhcVG9AopB9Q6UBjRY593Srgrp7kt6kAUNcJsUzyzx tJX4/8N4Vlg2L0zkVrmGv2n83/MVoStZQbTb8ETiJeaKFuR7gNiG45L5z3WhNzAnT6 0RPHK8V1m/jU2/2iIyjugJDYW1Ct2Yw/Uhii6rDcnnA2A1pNTMvF6f8hiFCouxgYbl sBayNGlrDVh/ixc8rMSgyy69ofVu+BLzBwH2Bvae06izplRquNEs7i2VGiBvvqje+L UzfP+q5pGXKkg== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nick Desaulniers , Nathan Chancellor , Nicolas Schier Subject: [PATCH 5/5] modpost: factor out the common boilerplate of section_rel(a) Date: Sun, 8 Oct 2023 02:04:48 +0900 Message-Id: <20231007170448.505487-5-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231007170448.505487-1-masahiroy@kernel.org> References: <20231007170448.505487-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org The first few lines of section_rel() and section_rela() are the same. They both retrieve the index of the section to which the relocaton applies, and skip known-good sections. This common code should be moved to check_sec_ref(). Avoid ugly casts when computing 'start' and 'stop', and also make the Elf_Rel and Elf_Rela pointers const. Signed-off-by: Masahiro Yamada Reviewed-by: Nick Desaulniers --- scripts/mod/modpost.c | 50 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 441d57ee3275..f1f658122ad8 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1420,17 +1420,10 @@ static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info, } static void section_rela(struct module *mod, struct elf_info *elf, - Elf_Shdr *sechdr) + unsigned int fsecndx, const char *fromsec, + const Elf_Rela *start, const Elf_Rela *stop) { - Elf_Rela *rela; - unsigned int fsecndx = sechdr->sh_info; - const char *fromsec = sec_name(elf, fsecndx); - Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset; - Elf_Rela *stop = (void *)start + sechdr->sh_size; - - /* if from section (name) is know good then skip it */ - if (match(fromsec, section_white_list)) - return; + const Elf_Rela *rela; for (rela = start; rela < stop; rela++) { Elf_Addr taddr, r_offset; @@ -1460,17 +1453,10 @@ static void section_rela(struct module *mod, struct elf_info *elf, } static void section_rel(struct module *mod, struct elf_info *elf, - Elf_Shdr *sechdr) + unsigned int fsecndx, const char *fromsec, + const Elf_Rel *start, const Elf_Rel *stop) { - Elf_Rel *rel; - unsigned int fsecndx = sechdr->sh_info; - const char *fromsec = sec_name(elf, fsecndx); - Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset; - Elf_Rel *stop = (void *)start + sechdr->sh_size; - - /* if from section (name) is know good then skip it */ - if (match(fromsec, section_white_list)) - return; + const Elf_Rel *rel; for (rel = start; rel < stop; rel++) { Elf_Sym *tsym; @@ -1525,10 +1511,26 @@ static void check_sec_ref(struct module *mod, struct elf_info *elf) check_section(mod->name, elf, sechdr); /* We want to process only relocation sections and not .init */ - if (sechdr->sh_type == SHT_RELA) - section_rela(mod, elf, sechdr); - else if (sechdr->sh_type == SHT_REL) - section_rel(mod, elf, sechdr); + if (sechdr->sh_type == SHT_REL || sechdr->sh_type == SHT_RELA) { + /* section to which the relocation applies */ + unsigned int secndx = sechdr->sh_info; + const char *secname = sec_name(elf, secndx); + const void *start, *stop; + + /* If the section is known good, skip it */ + if (match(secname, section_white_list)) + continue; + + start = sym_get_data_by_offset(elf, i, 0); + stop = start + sechdr->sh_size; + + if (sechdr->sh_type == SHT_RELA) + section_rela(mod, elf, secndx, secname, + start, stop); + else + section_rel(mod, elf, secndx, secname, + start, stop); + } } }