]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
tests: Make elf-from-memory testcase work on big endian setups users/mark/try-elf-from-memory-endian
authorMark Wielaard <mark@klomp.org>
Sun, 21 Jun 2026 15:19:18 +0000 (17:19 +0200)
committerMark Wielaard <mark@klomp.org>
Sun, 21 Jun 2026 15:41:18 +0000 (17:41 +0200)
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 <mark@klomp.org>
tests/elf-from-memory.c

index cc9a83eeaac0298531790e55f39ccbaceb7b8bd1..8a82ce290a4d65db6a8a2f53706834f4be9bd899 100644 (file)
@@ -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)