From: Mark Wielaard Date: Sun, 21 Jun 2026 15:19:18 +0000 (+0200) Subject: tests: Make elf-from-memory testcase work on big endian setups X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ad3083f104d22c964988378b127c436cb3ef5532;p=thirdparty%2Felfutils.git tests: Make elf-from-memory testcase work on big endian setups We are providing structs for x86_64 (little endian). So make sure the Ehdr and Phdr are always xlated to ELFDATA2LSB. * tests/elf-from-memory.c (main): Call elf64_xlatetom. Signed-off-by: Mark Wielaard --- diff --git a/tests/elf-from-memory.c b/tests/elf-from-memory.c index cc9a83ee..8a82ce29 100644 --- a/tests/elf-from-memory.c +++ b/tests/elf-from-memory.c @@ -77,6 +77,15 @@ main (void) init_ehdr (&ehdr); ehdr.e_phoff = (Elf64_Off) (0ULL - (uint64_t) sizeof (Elf64_Phdr)); + /* Make sure data is in LSB order. */ + Elf_Data xlate_data_ehdr = + { + .d_type = ELF_T_EHDR, + .d_buf = &ehdr, + .d_size = sizeof ehdr, + .d_version = EV_CURRENT, + }; + elf64_xlatetom (&xlate_data_ehdr, &xlate_data_ehdr, ELFDATA2LSB); memset (backing, 0, sizeof backing); memcpy (backing, &ehdr, sizeof ehdr); Elf *elf = elf_from_remote_memory (0, 4096, NULL, read_mem, NULL); @@ -91,15 +100,26 @@ main (void) init_ehdr (&ehdr); ehdr.e_phoff = sizeof (Elf64_Ehdr); + /* Make sure data is in LSB order. */ + elf64_xlatetom (&xlate_data_ehdr, &xlate_data_ehdr, ELFDATA2LSB); Elf64_Phdr phdr; memset (&phdr, 0, sizeof phdr); phdr.p_type = PT_LOAD; phdr.p_filesz = BACKSZ; phdr.p_memsz = BACKSZ; phdr.p_align = 4096; + /* Make sure data is in LSB order. */ + Elf_Data xlate_data_phdr = + { + .d_type = ELF_T_PHDR, + .d_buf = &phdr, + .d_size = sizeof phdr, + .d_version = EV_CURRENT, + }; + elf64_xlatetom (&xlate_data_phdr, &xlate_data_phdr, ELFDATA2LSB); memset (backing, 0, sizeof backing); memcpy (backing, &ehdr, sizeof ehdr); - memcpy (backing + ehdr.e_phoff, &phdr, sizeof phdr); + memcpy (backing + sizeof (Elf64_Ehdr), &phdr, sizeof phdr); elf = elf_from_remote_memory (0, 4096, NULL, read_mem, NULL); GElf_Ehdr got; if (elf == NULL || gelf_getehdr (elf, &got) == NULL || got.e_phnum != 1)