Message ID | 20231007170448.505487-3-masahiroy@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp1025676vqo; Sat, 7 Oct 2023 10:05:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHqrbUySneorD2c+8Ovu9jT5IRAvC5BTmn1aubL8HaYnlz6CwLfrQhFHhFGX69SQ2vX2cAk X-Received: by 2002:a17:902:e749:b0:1c5:e00f:2 with SMTP id p9-20020a170902e74900b001c5e00f0002mr14020644plf.28.1696698319697; Sat, 07 Oct 2023 10:05:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696698319; cv=none; d=google.com; s=arc-20160816; b=UrVboj5qwNQjogj5F13XzlxySKrDpcMjiodtAv6NjzeFvX3jeYkAQ6aobCK0RyNPjs sUGrlMQ5a0JsAOBuy15a4Qsxznt3/3tVo1raXXrmvkgiqobPrHl0rFWb4kwQEPCLq0DX lJ+eVUv2h9FnwihwXjcvOAOta3X8Fgns8TbEoninzpNywhTOspjKzVPjcvPJi9YjMxDM jZza164X8T12j12ypHFE+lB6CcZM7wYemzlus5dGcuQh4vYlpZqB18rHo687/uruoMja vQx/OLYV09BrJQGUZ1/x6Euy/GEr3F7a9So/6lvKNkq5sTz2hRj3Q3clY84Fndr17sRi LEVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CLLBSsMf0TcxS3JOe8JbCCtUtr+EhGCipjY+ISr+TGE=; fh=wKrPioF+0wPaktGqmf0aQvlk8PY4Pc32rTAsBSAiP8I=; b=xUHVpqUdXA7+6On/yLMDw4XSDfMvHfKV60SL3lvdpC1sVLp+NRE6k9XAZwAT2VRIc2 r9lxlzjtkJV/ZYKFlBK2PIlfq5rcRBNVmF4J7HQulQGNtFCOWd2RKWSlhyw56u207dMA zzRjGhwazHBhZigP6jqpbZbCjin7TZ78JKwxZE9z2BvL/OjaVWOycTjpIQ7+z8EmJS6+ SVcEA3DEGnrKdtuOVK1khN7eNprzfWqQQwFpsCSR3gcyYCOTha4y6H1DrPzfBWhzCCfX kt/DMcSx1NHnV9LJ8cOffrZcitZbGJM4q6tuIzzEQe9OQRdCt0wiCg1Xq4+IPeeEQGmF 1ggg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LWnn0sOt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id m17-20020a170902e41100b001b82da85c5asi3327484ple.336.2023.10.07.10.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 10:05:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LWnn0sOt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 8ABFC805C3FF; Sat, 7 Oct 2023 10:05:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344125AbjJGRFG (ORCPT <rfc822;pusanteemu@gmail.com> + 17 others); Sat, 7 Oct 2023 13:05:06 -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 <rfc822;linux-kernel@vger.kernel.org>); 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 <masahiroy@kernel.org> To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada <masahiroy@kernel.org>, Nathan Chancellor <nathan@kernel.org>, Nick Desaulniers <ndesaulniers@google.com>, Nicolas Schier <nicolas@fjasle.eu> 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sat, 07 Oct 2023 10:05:17 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779117137291247208 X-GMAIL-MSGID: 1779117137291247208 |
Series |
[1/5] modpost: fix tee MODULE_DEVICE_TABLE built on big endian host
|
|
Commit Message
Masahiro Yamada
Oct. 7, 2023, 5:04 p.m. UTC
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 <masahiroy@kernel.org>
---
scripts/mod/modpost.c | 13 ++++---------
scripts/mod/modpost.h | 25 ++++++++++++-------------
2 files changed, 16 insertions(+), 22 deletions(-)
Comments
On Sat, Oct 7, 2023 at 10:05 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > > 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 <masahiroy@kernel.org> > --- > > 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 <byteswap.h> > #include <stdbool.h> > #include <stdio.h> > #include <stdlib.h> > @@ -51,21 +52,19 @@ > #define ELF_R_TYPE ELF64_R_TYPE > #endif > > +#define bswap(x) \ > +({ \ > + _Static_assert(sizeof(x) == 1 || sizeof(x) == 2 || \ Seems fine, but do we need to support folks trying to swap 1B values? i.e. is someone calling TO_NATIVE with 1B values? Seems silly unless one of these types is variable length dependent on the target machine type? Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> > + 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 */ > > -- > 2.39.2 >
On Tue, Oct 10, 2023 at 2:44 AM Nick Desaulniers <ndesaulniers@google.com> wrote: > > On Sat, Oct 7, 2023 at 10:05 AM Masahiro Yamada <masahiroy@kernel.org> wrote: > > > > 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 <masahiroy@kernel.org> > > --- > > > > 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 <byteswap.h> > > #include <stdbool.h> > > #include <stdio.h> > > #include <stdlib.h> > > @@ -51,21 +52,19 @@ > > #define ELF_R_TYPE ELF64_R_TYPE > > #endif > > > > +#define bswap(x) \ > > +({ \ > > + _Static_assert(sizeof(x) == 1 || sizeof(x) == 2 || \ > > Seems fine, but do we need to support folks trying to swap 1B values? > i.e. is someone calling TO_NATIVE with 1B values? Yes. In scripts/mod/file2alias.c, DEF_FIELD() calls TO_NATIVE(). DEF_FIELD() is also used to get access to 1-byte fields. So, TO_NATIVE() needs to accept sizeof(x)==1. > Seems silly unless > one of these types is variable length dependent on the target machine > type? You can use DEF_FIELD() without knowing the field width. This is good.
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 <byteswap.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -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 */