Blame SOURCES/0143-Suport-for-bi-endianess-in-elf-file.patch

a85e8e
From 2dae852b62bb2227ba2e4faf4864c7779104657a Mon Sep 17 00:00:00 2001
a85e8e
From: Paulo Flabiano Smorigo <pfsmorigo@linux.vnet.ibm.com>
a85e8e
Date: Fri, 15 Aug 2014 14:39:53 -0300
a85e8e
Subject: [PATCH 143/260] Suport for bi-endianess in elf file
a85e8e
a85e8e
* grub-core/kern/elf.c: check and switch endianess with grub_{be,le}_to
a85e8e
  cpu functions.
a85e8e
* grub-core/kern/elfXX.c: Likewise.
a85e8e
a85e8e
Also-by: Tomohiro B Berry <tbberry@us.ibm.com>
a85e8e
---
a85e8e
 grub-core/kern/elf.c   | 60 +++++++++++++++++++++++++++++++++++++++--
a85e8e
 grub-core/kern/elfXX.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
a85e8e
 2 files changed, 131 insertions(+), 2 deletions(-)
a85e8e
a85e8e
diff --git a/grub-core/kern/elf.c b/grub-core/kern/elf.c
a85e8e
index 5f99c43cc..de9081180 100644
a85e8e
--- a/grub-core/kern/elf.c
a85e8e
+++ b/grub-core/kern/elf.c
a85e8e
@@ -28,6 +28,11 @@
a85e8e
 
a85e8e
 GRUB_MOD_LICENSE ("GPLv3+");
a85e8e
 
a85e8e
+void grub_elf32_check_endianess (grub_elf_t elf);
a85e8e
+void grub_elf64_check_endianess (grub_elf_t elf);
a85e8e
+grub_err_t grub_elf32_check_version (grub_elf_t elf);
a85e8e
+grub_err_t grub_elf64_check_version (grub_elf_t elf);
a85e8e
+
a85e8e
 /* Check if EHDR is a valid ELF header.  */
a85e8e
 static grub_err_t
a85e8e
 grub_elf_check_header (grub_elf_t elf)
a85e8e
@@ -38,10 +43,22 @@ grub_elf_check_header (grub_elf_t elf)
a85e8e
       || e->e_ident[EI_MAG1] != ELFMAG1
a85e8e
       || e->e_ident[EI_MAG2] != ELFMAG2
a85e8e
       || e->e_ident[EI_MAG3] != ELFMAG3
a85e8e
-      || e->e_ident[EI_VERSION] != EV_CURRENT
a85e8e
-      || e->e_version != EV_CURRENT)
a85e8e
+      || e->e_ident[EI_VERSION] != EV_CURRENT)
a85e8e
     return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
a85e8e
 
a85e8e
+  if (grub_elf_is_elf32 (elf))
a85e8e
+    {
a85e8e
+      grub_elf32_check_endianess (elf);
a85e8e
+      grub_elf32_check_version (elf);
a85e8e
+    }
a85e8e
+  else if (grub_elf_is_elf64 (elf))
a85e8e
+    {
a85e8e
+      grub_elf64_check_endianess (elf);
a85e8e
+      grub_elf64_check_version (elf);
a85e8e
+    }
a85e8e
+  else
a85e8e
+    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
a85e8e
+
a85e8e
   return GRUB_ERR_NONE;
a85e8e
 }
a85e8e
 
a85e8e
@@ -127,7 +144,20 @@ grub_elf_open (const char *name)
a85e8e
 #define grub_elf_is_elfXX grub_elf_is_elf32
a85e8e
 #define grub_elfXX_load_phdrs grub_elf32_load_phdrs
a85e8e
 #define ElfXX_Phdr Elf32_Phdr
a85e8e
+#define ElfXX_Ehdr Elf32_Ehdr
a85e8e
 #define grub_uintXX_t grub_uint32_t
a85e8e
+#define grub_be_to_halfXX grub_be_to_cpu16
a85e8e
+#define grub_be_to_wordXX grub_be_to_cpu32
a85e8e
+#define grub_be_to_addrXX grub_be_to_cpu32
a85e8e
+#define grub_be_to_offXX grub_be_to_cpu32
a85e8e
+#define grub_be_to_XwordXX grub_be_to_wordXX
a85e8e
+#define grub_le_to_halfXX grub_le_to_cpu16
a85e8e
+#define grub_le_to_wordXX grub_le_to_cpu32
a85e8e
+#define grub_le_to_addrXX grub_le_to_cpu32
a85e8e
+#define grub_le_to_offXX grub_le_to_cpu32
a85e8e
+#define grub_le_to_XwordXX grub_le_to_wordXX
a85e8e
+#define grub_elfXX_check_endianess grub_elf32_check_endianess
a85e8e
+#define grub_elfXX_check_version grub_elf32_check_version
a85e8e
 
a85e8e
 #include "elfXX.c"
a85e8e
 
a85e8e
@@ -140,7 +170,20 @@ grub_elf_open (const char *name)
a85e8e
 #undef grub_elf_is_elfXX
a85e8e
 #undef grub_elfXX_load_phdrs
a85e8e
 #undef ElfXX_Phdr
a85e8e
+#undef ElfXX_Ehdr
a85e8e
 #undef grub_uintXX_t
a85e8e
+#undef grub_be_to_halfXX
a85e8e
+#undef grub_be_to_wordXX
a85e8e
+#undef grub_be_to_addrXX
a85e8e
+#undef grub_be_to_offXX
a85e8e
+#undef grub_be_to_XwordXX
a85e8e
+#undef grub_le_to_halfXX
a85e8e
+#undef grub_le_to_wordXX
a85e8e
+#undef grub_le_to_addrXX
a85e8e
+#undef grub_le_to_offXX
a85e8e
+#undef grub_le_to_XwordXX
a85e8e
+#undef grub_elfXX_check_endianess
a85e8e
+#undef grub_elfXX_check_version
a85e8e
 
a85e8e
 
a85e8e
 /* 64-bit */
a85e8e
@@ -153,6 +196,19 @@ grub_elf_open (const char *name)
a85e8e
 #define grub_elf_is_elfXX grub_elf_is_elf64
a85e8e
 #define grub_elfXX_load_phdrs grub_elf64_load_phdrs
a85e8e
 #define ElfXX_Phdr Elf64_Phdr
a85e8e
+#define ElfXX_Ehdr Elf64_Ehdr
a85e8e
 #define grub_uintXX_t grub_uint64_t
a85e8e
+#define grub_be_to_halfXX grub_be_to_cpu16
a85e8e
+#define grub_be_to_wordXX grub_be_to_cpu32
a85e8e
+#define grub_be_to_addrXX grub_be_to_cpu64
a85e8e
+#define grub_be_to_offXX grub_be_to_cpu64
a85e8e
+#define grub_be_to_XwordXX grub_be_to_cpu64
a85e8e
+#define grub_le_to_halfXX grub_le_to_cpu16
a85e8e
+#define grub_le_to_wordXX grub_le_to_cpu32
a85e8e
+#define grub_le_to_addrXX grub_le_to_cpu64
a85e8e
+#define grub_le_to_offXX grub_le_to_cpu64
a85e8e
+#define grub_le_to_XwordXX grub_le_to_cpu64
a85e8e
+#define grub_elfXX_check_endianess grub_elf64_check_endianess
a85e8e
+#define grub_elfXX_check_version grub_elf64_check_version
a85e8e
 
a85e8e
 #include "elfXX.c"
a85e8e
diff --git a/grub-core/kern/elfXX.c b/grub-core/kern/elfXX.c
a85e8e
index 1d0997186..ecf9df63a 100644
a85e8e
--- a/grub-core/kern/elfXX.c
a85e8e
+++ b/grub-core/kern/elfXX.c
a85e8e
@@ -154,3 +154,76 @@ grub_elfXX_load (grub_elf_t elf, const char *filename,
a85e8e
 
a85e8e
   return grub_errno;
a85e8e
 }
a85e8e
+
a85e8e
+void
a85e8e
+grub_elfXX_check_endianess (grub_elf_t elf)
a85e8e
+{
a85e8e
+  ElfXX_Ehdr *e = &(elf->ehdr.ehdrXX);
a85e8e
+  ElfXX_Phdr *phdr;
a85e8e
+
a85e8e
+  if (e->e_ident[EI_DATA] == ELFDATA2MSB)
a85e8e
+    {
a85e8e
+      e->e_type = grub_be_to_halfXX (e->e_type);
a85e8e
+      e->e_machine = grub_be_to_halfXX (e->e_machine);
a85e8e
+      e->e_version = grub_be_to_wordXX (e->e_version);
a85e8e
+      e->e_entry = grub_be_to_addrXX (e->e_entry);
a85e8e
+      e->e_phoff = grub_be_to_offXX (e->e_phoff);
a85e8e
+      e->e_shoff = grub_be_to_offXX (e->e_shoff);
a85e8e
+      e->e_flags = grub_be_to_wordXX (e->e_flags);
a85e8e
+      e->e_ehsize = grub_be_to_halfXX (e->e_ehsize);
a85e8e
+      e->e_phentsize = grub_be_to_halfXX (e->e_phentsize);
a85e8e
+      e->e_phnum = grub_be_to_halfXX (e->e_phnum);
a85e8e
+      e->e_shentsize = grub_be_to_halfXX (e->e_shentsize);
a85e8e
+      e->e_shnum = grub_be_to_halfXX (e->e_shnum);
a85e8e
+      e->e_shstrndx = grub_be_to_halfXX (e->e_shstrndx);
a85e8e
+
a85e8e
+      FOR_ELFXX_PHDRS (elf,phdr)
a85e8e
+        {
a85e8e
+          phdr->p_type = grub_be_to_wordXX (phdr->p_type);
a85e8e
+          phdr->p_flags = grub_be_to_wordXX (phdr->p_flags);
a85e8e
+          phdr->p_offset = grub_be_to_offXX (phdr->p_offset);
a85e8e
+          phdr->p_vaddr = grub_be_to_addrXX (phdr->p_vaddr);
a85e8e
+          phdr->p_paddr = grub_be_to_addrXX (phdr->p_paddr);
a85e8e
+          phdr->p_filesz = grub_be_to_XwordXX (phdr->p_filesz);
a85e8e
+          phdr->p_memsz = grub_be_to_XwordXX (phdr->p_memsz);
a85e8e
+          phdr->p_align = grub_be_to_XwordXX (phdr->p_align);
a85e8e
+        }
a85e8e
+    }
a85e8e
+  else if (e->e_ident[EI_DATA] == ELFDATA2LSB)
a85e8e
+    {
a85e8e
+      e->e_type = grub_le_to_halfXX (e->e_type);
a85e8e
+      e->e_machine = grub_le_to_halfXX (e->e_machine);
a85e8e
+      e->e_version = grub_le_to_wordXX (e->e_version);
a85e8e
+      e->e_entry = grub_le_to_addrXX (e->e_entry);
a85e8e
+      e->e_phoff = grub_le_to_offXX (e->e_phoff);
a85e8e
+      e->e_shoff = grub_le_to_offXX (e->e_shoff);
a85e8e
+      e->e_flags = grub_le_to_wordXX (e->e_flags);
a85e8e
+      e->e_ehsize = grub_le_to_halfXX (e->e_ehsize);
a85e8e
+      e->e_phentsize = grub_le_to_halfXX (e->e_phentsize);
a85e8e
+      e->e_phnum = grub_le_to_halfXX (e->e_phnum);
a85e8e
+      e->e_shentsize = grub_le_to_halfXX (e->e_shentsize);
a85e8e
+      e->e_shnum = grub_le_to_halfXX (e->e_shnum);
a85e8e
+      e->e_shstrndx = grub_le_to_halfXX (e->e_shstrndx);
a85e8e
+
a85e8e
+      FOR_ELFXX_PHDRS (elf,phdr)
a85e8e
+        {
a85e8e
+          phdr->p_type = grub_le_to_wordXX (phdr->p_type);
a85e8e
+          phdr->p_flags = grub_le_to_wordXX (phdr->p_flags);
a85e8e
+          phdr->p_offset = grub_le_to_offXX (phdr->p_offset);
a85e8e
+          phdr->p_vaddr = grub_le_to_addrXX (phdr->p_vaddr);
a85e8e
+          phdr->p_paddr = grub_le_to_addrXX (phdr->p_paddr);
a85e8e
+          phdr->p_filesz = grub_le_to_XwordXX (phdr->p_filesz);
a85e8e
+          phdr->p_memsz = grub_le_to_XwordXX (phdr->p_memsz);
a85e8e
+          phdr->p_align = grub_le_to_XwordXX (phdr->p_align);
a85e8e
+        }
a85e8e
+    }
a85e8e
+}
a85e8e
+
a85e8e
+grub_err_t
a85e8e
+grub_elfXX_check_version (grub_elf_t elf)
a85e8e
+{
a85e8e
+  if (elf->ehdr.ehdrXX.e_version != EV_CURRENT)
a85e8e
+    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
a85e8e
+
a85e8e
+  return GRUB_ERR_NONE;
a85e8e
+}
a85e8e
-- 
a85e8e
2.13.0
a85e8e