2006-11-03 21:55:16 +00:00
|
|
|
|
/* elf.c - load ELF files */
|
|
|
|
|
/*
|
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-03 22:05:07 +00:00
|
|
|
|
* Copyright (C) 2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
|
2006-11-03 21:55:16 +00:00
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2006-11-03 21:55:16 +00:00
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2006-11-03 21:55:16 +00:00
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2006-11-03 21:55:16 +00:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2006-11-03 21:55:16 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <grub/err.h>
|
|
|
|
|
#include <grub/elf.h>
|
|
|
|
|
#include <grub/elfload.h>
|
|
|
|
|
#include <grub/file.h>
|
|
|
|
|
#include <grub/misc.h>
|
|
|
|
|
#include <grub/mm.h>
|
2011-04-11 21:01:51 +00:00
|
|
|
|
#include <grub/dl.h>
|
2012-02-08 18:26:01 +00:00
|
|
|
|
#include <grub/i18n.h>
|
2011-04-11 21:01:51 +00:00
|
|
|
|
|
|
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
2006-11-03 21:55:16 +00:00
|
|
|
|
|
|
|
|
|
/* Check if EHDR is a valid ELF header. */
|
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_elf_check_header (grub_elf_t elf)
|
|
|
|
|
{
|
|
|
|
|
Elf32_Ehdr *e = &elf->ehdr.ehdr32;
|
|
|
|
|
|
|
|
|
|
if (e->e_ident[EI_MAG0] != ELFMAG0
|
|
|
|
|
|| e->e_ident[EI_MAG1] != ELFMAG1
|
|
|
|
|
|| e->e_ident[EI_MAG2] != ELFMAG2
|
|
|
|
|
|| e->e_ident[EI_MAG3] != ELFMAG3
|
|
|
|
|
|| e->e_ident[EI_VERSION] != EV_CURRENT
|
|
|
|
|
|| e->e_version != EV_CURRENT)
|
2012-02-26 16:28:05 +00:00
|
|
|
|
return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
|
2006-11-03 21:55:16 +00:00
|
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
|
grub_elf_close (grub_elf_t elf)
|
|
|
|
|
{
|
|
|
|
|
grub_file_t file = elf->file;
|
|
|
|
|
|
|
|
|
|
grub_free (elf->phdrs);
|
2013-03-02 15:45:57 +00:00
|
|
|
|
grub_free (elf->filename);
|
2006-11-03 21:55:16 +00:00
|
|
|
|
grub_free (elf);
|
|
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
|
grub_file_close (file);
|
|
|
|
|
|
|
|
|
|
return grub_errno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grub_elf_t
|
2012-02-08 18:26:01 +00:00
|
|
|
|
grub_elf_file (grub_file_t file, const char *filename)
|
2006-11-03 21:55:16 +00:00
|
|
|
|
{
|
|
|
|
|
grub_elf_t elf;
|
|
|
|
|
|
2009-07-16 22:14:09 +00:00
|
|
|
|
elf = grub_zalloc (sizeof (*elf));
|
2006-11-03 21:55:16 +00:00
|
|
|
|
if (! elf)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
elf->file = file;
|
|
|
|
|
|
2006-11-03 23:05:14 +00:00
|
|
|
|
if (grub_file_seek (elf->file, 0) == (grub_off_t) -1)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2009-06-10 23:47:49 +00:00
|
|
|
|
if (grub_file_read (elf->file, &elf->ehdr, sizeof (elf->ehdr))
|
2006-11-03 21:55:16 +00:00
|
|
|
|
!= sizeof (elf->ehdr))
|
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
|
if (!grub_errno)
|
|
|
|
|
grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
|
|
|
|
|
filename);
|
2006-11-03 21:55:16 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (grub_elf_check_header (elf))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2013-03-02 15:45:57 +00:00
|
|
|
|
elf->filename = grub_strdup (filename);
|
|
|
|
|
if (!elf->filename)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2006-11-03 21:55:16 +00:00
|
|
|
|
return elf;
|
|
|
|
|
|
|
|
|
|
fail:
|
2013-03-02 15:45:57 +00:00
|
|
|
|
grub_free (elf->filename);
|
2008-02-19 16:40:45 +00:00
|
|
|
|
grub_free (elf->phdrs);
|
|
|
|
|
grub_free (elf);
|
2006-11-03 21:55:16 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grub_elf_t
|
|
|
|
|
grub_elf_open (const char *name)
|
|
|
|
|
{
|
|
|
|
|
grub_file_t file;
|
2008-02-19 16:40:45 +00:00
|
|
|
|
grub_elf_t elf;
|
2006-11-03 21:55:16 +00:00
|
|
|
|
|
2010-09-05 11:05:36 +00:00
|
|
|
|
file = grub_file_open (name);
|
2006-11-03 21:55:16 +00:00
|
|
|
|
if (! file)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2012-02-08 18:26:01 +00:00
|
|
|
|
elf = grub_elf_file (file, name);
|
2008-02-19 16:40:45 +00:00
|
|
|
|
if (! elf)
|
|
|
|
|
grub_file_close (file);
|
|
|
|
|
|
|
|
|
|
return elf;
|
2006-11-03 21:55:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 32-bit */
|
2013-03-02 15:45:57 +00:00
|
|
|
|
#define ehdrXX ehdr32
|
|
|
|
|
#define ELFCLASSXX ELFCLASS32
|
|
|
|
|
#define ElfXX_Addr Elf32_Addr
|
|
|
|
|
#define grub_elfXX_size grub_elf32_size
|
|
|
|
|
#define grub_elfXX_load grub_elf32_load
|
|
|
|
|
#define FOR_ELFXX_PHDRS FOR_ELF32_PHDRS
|
|
|
|
|
#define grub_elf_is_elfXX grub_elf_is_elf32
|
|
|
|
|
#define grub_elfXX_load_phdrs grub_elf32_load_phdrs
|
|
|
|
|
#define ElfXX_Phdr Elf32_Phdr
|
|
|
|
|
#define grub_uintXX_t grub_uint32_t
|
|
|
|
|
|
|
|
|
|
#include "elfXX.c"
|
|
|
|
|
|
|
|
|
|
#undef ehdrXX
|
|
|
|
|
#undef ELFCLASSXX
|
|
|
|
|
#undef ElfXX_Addr
|
|
|
|
|
#undef grub_elfXX_size
|
|
|
|
|
#undef grub_elfXX_load
|
|
|
|
|
#undef FOR_ELFXX_PHDRS
|
|
|
|
|
#undef grub_elf_is_elfXX
|
|
|
|
|
#undef grub_elfXX_load_phdrs
|
|
|
|
|
#undef ElfXX_Phdr
|
|
|
|
|
#undef grub_uintXX_t
|
2006-11-03 21:55:16 +00:00
|
|
|
|
|
2007-02-13 03:49:43 +00:00
|
|
|
|
|
|
|
|
|
/* 64-bit */
|
2013-03-02 15:45:57 +00:00
|
|
|
|
#define ehdrXX ehdr64
|
|
|
|
|
#define ELFCLASSXX ELFCLASS64
|
|
|
|
|
#define ElfXX_Addr Elf64_Addr
|
|
|
|
|
#define grub_elfXX_size grub_elf64_size
|
|
|
|
|
#define grub_elfXX_load grub_elf64_load
|
|
|
|
|
#define FOR_ELFXX_PHDRS FOR_ELF64_PHDRS
|
|
|
|
|
#define grub_elf_is_elfXX grub_elf_is_elf64
|
|
|
|
|
#define grub_elfXX_load_phdrs grub_elf64_load_phdrs
|
|
|
|
|
#define ElfXX_Phdr Elf64_Phdr
|
|
|
|
|
#define grub_uintXX_t grub_uint64_t
|
|
|
|
|
|
|
|
|
|
#include "elfXX.c"
|