From: Masahiro Yamada <masahiroy@kernel.org>
To: linux-kbuild@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Kees Cook <kees@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nicolas Schier <nicolas@fjasle.eu>,
Ben Hutchings <ben@decadent.org.uk>,
Masahiro Yamada <masahiroy@kernel.org>
Subject: [PATCH 2/4] modpost: detect endianness on run-time
Date: Sat, 27 Jul 2024 16:42:02 +0900 [thread overview]
Message-ID: <20240727074526.1771247-3-masahiroy@kernel.org> (raw)
In-Reply-To: <20240727074526.1771247-1-masahiroy@kernel.org>
Endianness is currently detected on compile-time, but we can defer this
until run-time. This change avoids re-executing scripts/mod/mk_elfconfig
even if modpost in the linux-headers package needs to be rebuilt for a
foreign architecture.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---
scripts/mod/mk_elfconfig.c | 19 -------------------
scripts/mod/modpost.c | 36 ++++++++++++++++++++++++++++++++++++
scripts/mod/modpost.h | 13 ++++---------
3 files changed, 40 insertions(+), 28 deletions(-)
diff --git a/scripts/mod/mk_elfconfig.c b/scripts/mod/mk_elfconfig.c
index aca96b3aada0..e8cee4e4bc73 100644
--- a/scripts/mod/mk_elfconfig.c
+++ b/scripts/mod/mk_elfconfig.c
@@ -8,7 +8,6 @@ int
main(int argc, char **argv)
{
unsigned char ei[EI_NIDENT];
- union { short s; char c[2]; } endian_test;
if (fread(ei, 1, EI_NIDENT, stdin) != EI_NIDENT) {
fprintf(stderr, "Error: input truncated\n");
@@ -28,24 +27,6 @@ main(int argc, char **argv)
default:
exit(1);
}
- switch (ei[EI_DATA]) {
- case ELFDATA2LSB:
- printf("#define KERNEL_ELFDATA ELFDATA2LSB\n");
- break;
- case ELFDATA2MSB:
- printf("#define KERNEL_ELFDATA ELFDATA2MSB\n");
- break;
- default:
- exit(1);
- }
-
- endian_test.s = 0x0102;
- if (memcmp(endian_test.c, "\x01\x02", 2) == 0)
- printf("#define HOST_ELFDATA ELFDATA2MSB\n");
- else if (memcmp(endian_test.c, "\x02\x01", 2) == 0)
- printf("#define HOST_ELFDATA ELFDATA2LSB\n");
- else
- exit(1);
return 0;
}
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index d16d0ace2775..f492fbeed300 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -50,6 +50,9 @@ static bool error_occurred;
static bool extra_warn;
+bool target_is_big_endian;
+bool host_is_big_endian;
+
/*
* Cut off the warnings when there are too many. This typically occurs when
* vmlinux is missing. ('make modules' without building vmlinux.)
@@ -438,6 +441,18 @@ static int parse_elf(struct elf_info *info, const char *filename)
/* Not an ELF file - silently ignore it */
return 0;
}
+
+ switch (hdr->e_ident[EI_DATA]) {
+ case ELFDATA2LSB:
+ target_is_big_endian = false;
+ break;
+ case ELFDATA2MSB:
+ target_is_big_endian = true;
+ break;
+ default:
+ fatal("kernel endian is unknown\n");
+ }
+
/* Fix endianness in ELF header */
hdr->e_type = TO_NATIVE(hdr->e_type);
hdr->e_machine = TO_NATIVE(hdr->e_machine);
@@ -2117,6 +2132,25 @@ struct dump_list {
const char *file;
};
+static void check_host_endian(void)
+{
+ static const union {
+ short s;
+ char c[2];
+ } endian_test = { .c = {0x01, 0x02} };
+
+ switch (endian_test.s) {
+ case 0x0102:
+ host_is_big_endian = true;
+ break;
+ case 0x0201:
+ host_is_big_endian = false;
+ break;
+ default:
+ fatal("Unknown host endian\n");
+ }
+}
+
int main(int argc, char **argv)
{
struct module *mod;
@@ -2181,6 +2215,8 @@ int main(int argc, char **argv)
}
}
+ check_host_endian();
+
list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
read_dump(dl->file);
list_del(&dl->list);
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 58197b34a3c8..54ba9431713f 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -62,15 +62,8 @@
x); \
})
-#if KERNEL_ELFDATA != HOST_ELFDATA
-
-#define TO_NATIVE(x) (bswap(x))
-
-#else /* endianness matches */
-
-#define TO_NATIVE(x) (x)
-
-#endif
+#define TO_NATIVE(x) \
+ (target_is_big_endian == host_is_big_endian ? x : bswap(x))
#define NOFAIL(ptr) do_nofail((ptr), #ptr)
@@ -187,6 +180,8 @@ void add_moddevtable(struct buffer *buf, struct module *mod);
void get_src_version(const char *modname, char sum[], unsigned sumlen);
/* from modpost.c */
+extern bool target_is_big_endian;
+extern bool host_is_big_endian;
char *read_text_file(const char *filename);
char *get_line(char **stringp);
void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym);
--
2.43.0
next prev parent reply other threads:[~2024-07-27 7:45 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-27 7:42 [PATCH 0/4] kbuild: cross-compile linux-headers package Masahiro Yamada
2024-07-27 7:42 ` [PATCH 1/4] modpost: remove unused HOST_ELFCLASS Masahiro Yamada
2024-07-31 20:43 ` Nicolas Schier
2024-07-27 7:42 ` Masahiro Yamada [this message]
2024-07-31 20:47 ` [PATCH 2/4] modpost: detect endianness on run-time Nicolas Schier
2024-07-27 7:42 ` [PATCH 3/4] kbuild: slim down package for building external modules Masahiro Yamada
2024-07-31 21:01 ` Nicolas Schier
2024-08-24 12:27 ` Thomas Weißschuh
2024-08-24 16:58 ` Masahiro Yamada
2025-02-18 20:25 ` Jeffrey Hugo
2025-02-20 10:03 ` Nicolas Schier
2025-02-20 15:03 ` Jeffrey Hugo
2025-02-20 15:54 ` Nicolas Schier
2025-02-20 16:31 ` Jeffrey Hugo
2025-02-20 16:49 ` Greg KH
2025-02-20 17:24 ` Jeffrey Hugo
2025-02-20 17:43 ` Greg KH
2025-02-20 18:12 ` Masahiro Yamada
2025-02-28 22:04 ` Jeffrey Hugo
2025-02-20 17:57 ` Masahiro Yamada
2024-07-27 7:42 ` [PATCH 4/4] kbuild: cross-compile linux-headers package when possible Masahiro Yamada
2024-07-30 1:03 ` Kees Cook
2024-08-01 2:26 ` Masahiro Yamada
2024-07-31 21:10 ` Nicolas Schier
2024-08-01 2:37 ` Masahiro Yamada
2024-08-01 7:04 ` Nicolas Schier
2024-10-17 14:45 ` Ron Economos
2024-10-17 19:24 ` Nicolas Schier
2024-10-17 19:34 ` Ron Economos
2024-11-14 20:57 ` Charlie Jenkins
2024-11-16 8:03 ` Masahiro Yamada
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240727074526.1771247-3-masahiroy@kernel.org \
--to=masahiroy@kernel.org \
--cc=ben@decadent.org.uk \
--cc=kees@kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nathan@kernel.org \
--cc=nicolas@fjasle.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox