[PATCH v3 next 03/13] scripts/elf-parse: add elf_map_big_endian() helper

From: David Laight

Date: Wed Nov 11 2026 - 07:07:58 EST


Returns 1 for big-endian, 0 for little-endian.
While the tools are compiled for a specific architecture the target
endianness isn't easily obtainable.

Use 'const Elf_Ehdr *ehdr' for the argument, change the argument of the
very similar elf_map_machine() and elf_map_long_size() to be the same.
All the callers either pass the correct type or 'void *'.

Will be used by kallsyms.

Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
---

New patch for v3.

scripts/elf-parse.c | 13 +++++++------
scripts/elf-parse.h | 5 +++--
2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/scripts/elf-parse.c b/scripts/elf-parse.c
index ce1c2a670244..24d99ab9adc0 100644
--- a/scripts/elf-parse.c
+++ b/scripts/elf-parse.c
@@ -170,20 +170,21 @@ static int elf_parse(const char *fname, void *addr, uint32_t types)
return 0;
}

-int elf_map_machine(void *addr)
+int elf_map_machine(const Elf_Ehdr *ehdr)
{
- Elf_Ehdr *ehdr = addr;
-
return elf_parser.r2(&ehdr->e32.e_machine);
}

-int elf_map_long_size(void *addr)
+int elf_map_long_size(const Elf_Ehdr *ehdr)
{
- Elf_Ehdr *ehdr = addr;
-
return ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
}

+int elf_map_big_endian(const Elf_Ehdr *ehdr)
+{
+ return ehdr->e32.e_ident[EI_DATA] == ELFDATA2MSB;
+}
+
static void *__elf_map(char const *fname, size_t *size, uint32_t types,
bool writable)
{
diff --git a/scripts/elf-parse.h b/scripts/elf-parse.h
index c5ad754da254..72c78da5cb3a 100644
--- a/scripts/elf-parse.h
+++ b/scripts/elf-parse.h
@@ -318,8 +318,9 @@ static inline void w8le(uint64_t val, uint64_t *x)
void *elf_map(char const *fname, size_t *size, uint32_t types);
void *elf_map_ro(char const *fname, size_t *size, uint32_t types);
void elf_unmap(void *addr, size_t size);
-int elf_map_machine(void *addr);
-int elf_map_long_size(void *addr);
+int elf_map_machine(const Elf_Ehdr *ehdr);
+int elf_map_long_size(const Elf_Ehdr *ehdr);
+int elf_map_big_endian(const Elf_Ehdr *ehdr);

/* A mapped file with its section headers, section names and symbol table. */
struct elf_file {
--
2.39.5