source: grub-pc/trunk/fuentes/debian/patches/elf_bi_endian.patch @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 6.4 KB
  • grub-core/kern/elf.c

    From 793e71681684520de266daeaa188165aa7810955 Mon Sep 17 00:00:00 2001
    From: Tomohiro B Berry <tbberry@us.ibm.com>
    Date: Tue, 14 Jan 2014 17:43:25 +0000
    Subject: Add bi-endian support to ELF parser
    
    This patch adds bi-endian support for both 32-bit and 64-bit elf files.
    
    It compares the native endianness to the endianness of the elf file, and
    swaps the header bytes if necessary.  This will allow, for example,
    32-bit Big Endian grub to load a 64-bit Little Endian kernel.
    
    Origin: other, https://lists.gnu.org/archive/html/grub-devel/2014-01/msg00039.html
    
    Patch-Name: elf_bi_endian.patch
    ---
     grub-core/kern/elf.c   | 55 ++++++++++++++++++++++++++++++++++++++++++++++++--
     grub-core/kern/elfXX.c | 43 +++++++++++++++++++++++++++++++++++++++
     include/grub/elf.h     |  7 +++++++
     3 files changed, 103 insertions(+), 2 deletions(-)
    
    diff --git a/grub-core/kern/elf.c b/grub-core/kern/elf.c
    index 5f99c43..9a3cc71 100644
    a b  
    2828
    2929GRUB_MOD_LICENSE ("GPLv3+");
    3030
     31void grub_elf32_byteswap_header (grub_elf_t elf);
     32void grub_elf64_byteswap_header (grub_elf_t elf);
     33grub_err_t grub_elf32_check_version (grub_elf_t elf);
     34grub_err_t grub_elf64_check_version (grub_elf_t elf);
     35
    3136/* Check if EHDR is a valid ELF header.  */
    3237static grub_err_t
    3338grub_elf_check_header (grub_elf_t elf)
    3439{
     40  /* e_ident is the same for both 64-bit and 32-bit so just load into a 32-bit struct for now */
    3541  Elf32_Ehdr *e = &elf->ehdr.ehdr32;
    3642
     43  /* check if it is an ELF image at all */
    3744  if (e->e_ident[EI_MAG0] != ELFMAG0
    3845      || e->e_ident[EI_MAG1] != ELFMAG1
    3946      || e->e_ident[EI_MAG2] != ELFMAG2
    4047      || e->e_ident[EI_MAG3] != ELFMAG3
    41       || e->e_ident[EI_VERSION] != EV_CURRENT
    42       || e->e_version != EV_CURRENT)
     48      || e->e_ident[EI_VERSION] != EV_CURRENT)
    4349    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
    4450
     51  switch (e->e_ident[EI_CLASS])
     52    {
     53      case ELFCLASS32:
     54        if (e->e_ident[EI_DATA] != ELFDATA_NATIVE)
     55          grub_elf32_byteswap_header (elf);
     56        if (grub_elf32_check_version (elf) != GRUB_ERR_NONE)
     57          return grub_errno;
     58        break;
     59      case ELFCLASS64:
     60        if (e->e_ident[EI_DATA] != ELFDATA_NATIVE)
     61          grub_elf64_byteswap_header (elf);
     62        if (grub_elf64_check_version (elf) != GRUB_ERR_NONE)
     63          return grub_errno;
     64        break;
     65      default:
     66        return grub_error (GRUB_ERR_BAD_OS, N_("unrecognized ELF class"));
     67        break;
     68    }
     69
    4570  return GRUB_ERR_NONE;
    4671}
    4772
    grub_elf_open (const char *name) 
    127152#define grub_elf_is_elfXX grub_elf_is_elf32
    128153#define grub_elfXX_load_phdrs grub_elf32_load_phdrs
    129154#define ElfXX_Phdr Elf32_Phdr
     155#define ElfXX_Ehdr Elf32_Ehdr
    130156#define grub_uintXX_t grub_uint32_t
     157/* for phdr/ehdr byte swaps */
     158#define byte_swap_halfXX grub_swap_bytes16
     159#define byte_swap_wordXX grub_swap_bytes32
     160#define byte_swap_addrXX grub_swap_bytes32
     161#define byte_swap_offXX grub_swap_bytes32
     162#define byte_swap_XwordXX byte_swap_wordXX /* the 64-bit phdr uses Xwords and the 32-bit uses words */
     163#define grub_elfXX_byteswap_header grub_elf32_byteswap_header
     164#define grub_elfXX_check_version grub_elf32_check_version
    131165
    132166#include "elfXX.c"
    133167
    grub_elf_open (const char *name) 
    140174#undef grub_elf_is_elfXX
    141175#undef grub_elfXX_load_phdrs
    142176#undef ElfXX_Phdr
     177#undef ElfXX_Ehdr
    143178#undef grub_uintXX_t
     179#undef byte_swap_halfXX
     180#undef byte_swap_wordXX
     181#undef byte_swap_addrXX
     182#undef byte_swap_offXX
     183#undef byte_swap_XwordXX
     184#undef grub_elfXX_byteswap_header
     185#undef grub_elfXX_check_version
    144186
    145187
    146188
  • grub-core/kern/elfXX.c

     /* 64-bit */
    @@ -153,6 +195,15 @@ grub_elf_open (const char *name)
     #define grub_elf_is_elfXX grub_elf_is_elf64
     #define grub_elfXX_load_phdrs grub_elf64_load_phdrs
     #define ElfXX_Phdr Elf64_Phdr
    +#define ElfXX_Ehdr Elf64_Ehdr
     #define grub_uintXX_t grub_uint64_t
    +/* for phdr/ehdr byte swaps */
    +#define byte_swap_halfXX grub_swap_bytes16
    +#define byte_swap_wordXX grub_swap_bytes32
    +#define byte_swap_addrXX grub_swap_bytes64
    +#define byte_swap_offXX grub_swap_bytes64
    +#define byte_swap_XwordXX grub_swap_bytes64
    +#define grub_elfXX_byteswap_header grub_elf64_byteswap_header
    +#define grub_elfXX_check_version grub_elf64_check_version
     
     #include "elfXX.c"
    diff --git a/grub-core/kern/elfXX.c b/grub-core/kern/elfXX.c
    index 1d09971..639cda6 100644
    a b grub_elfXX_load (grub_elf_t elf, const char *filename, 
    154154
    155155  return grub_errno;
    156156}
     157
     158void
     159grub_elfXX_byteswap_header (grub_elf_t elf)
     160{
     161  ElfXX_Ehdr *e = &(elf->ehdr.ehdrXX);
     162  ElfXX_Phdr *phdr;
     163
     164  e->e_type = byte_swap_halfXX (e->e_type);
     165  e->e_machine = byte_swap_halfXX (e->e_machine);
     166  e->e_version = byte_swap_wordXX (e->e_version);
     167  e->e_entry = byte_swap_addrXX (e->e_entry);
     168  e->e_phoff = byte_swap_offXX (e->e_phoff);
     169  e->e_shoff = byte_swap_offXX (e->e_shoff);
     170  e->e_flags = byte_swap_wordXX (e->e_flags);
     171  e->e_ehsize = byte_swap_halfXX (e->e_ehsize);
     172  e->e_phentsize = byte_swap_halfXX (e->e_phentsize);
     173  e->e_phnum = byte_swap_halfXX (e->e_phnum);
     174  e->e_shentsize = byte_swap_halfXX (e->e_shentsize);
     175  e->e_shnum = byte_swap_halfXX (e->e_shnum);
     176  e->e_shstrndx = byte_swap_halfXX (e->e_shstrndx);
     177
     178  FOR_ELFXX_PHDRS (elf,phdr)
     179    {
     180      phdr->p_type = byte_swap_wordXX (phdr->p_type);
     181      phdr->p_flags = byte_swap_wordXX (phdr->p_flags);
     182      phdr->p_offset = byte_swap_offXX (phdr->p_offset);
     183      phdr->p_vaddr = byte_swap_addrXX (phdr->p_vaddr);
     184      phdr->p_paddr = byte_swap_addrXX (phdr->p_paddr);
     185      phdr->p_filesz = byte_swap_XwordXX (phdr->p_filesz);
     186      phdr->p_memsz = byte_swap_XwordXX (phdr->p_memsz);
     187      phdr->p_align = byte_swap_XwordXX (phdr->p_align);
     188    }
     189
     190}
     191
     192grub_err_t
     193grub_elfXX_check_version (grub_elf_t elf)
     194{
     195  if (elf->ehdr.ehdrXX.e_version != EV_CURRENT)
     196    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
     197
     198  return GRUB_ERR_NONE;
     199}
  • include/grub/elf.h

    diff --git a/include/grub/elf.h b/include/grub/elf.h
    index caa7963..ca6e436 100644
    a b typedef grub_uint16_t Elf64_Section; 
    5656typedef Elf32_Half Elf32_Versym;
    5757typedef Elf64_Half Elf64_Versym;
    5858
     59/* Define the native endianness */
     60
     61#ifdef GRUB_CPU_WORDS_BIGENDIAN
     62#define ELFDATA_NATIVE         ELFDATA2MSB
     63#else
     64#define ELFDATA_NATIVE         ELFDATA2LSB
     65#endif
    5966
    6067/* The ELF file header.  This appears at the start of every ELF file.  */
    6168
Note: See TracBrowser for help on using the repository browser.