source: grub-pc/trunk/fuentes/.pc/elf_bi_endian.patch/grub-core/kern/elf.c @ 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: 3.6 KB
Line 
1/* elf.c - load ELF files */
2/*
3 *  GRUB  --  GRand Unified Bootloader
4 *  Copyright (C) 2003,2004,2005,2006,2007,2008,2009  Free Software Foundation, Inc.
5 *
6 *  GRUB is free software: you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation, either version 3 of the License, or
9 *  (at your option) any later version.
10 *
11 *  GRUB is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <grub/err.h>
21#include <grub/elf.h>
22#include <grub/elfload.h>
23#include <grub/file.h>
24#include <grub/misc.h>
25#include <grub/mm.h>
26#include <grub/dl.h>
27#include <grub/i18n.h>
28
29GRUB_MOD_LICENSE ("GPLv3+");
30
31/* Check if EHDR is a valid ELF header.  */
32static grub_err_t
33grub_elf_check_header (grub_elf_t elf)
34{
35  Elf32_Ehdr *e = &elf->ehdr.ehdr32;
36
37  if (e->e_ident[EI_MAG0] != ELFMAG0
38      || e->e_ident[EI_MAG1] != ELFMAG1
39      || e->e_ident[EI_MAG2] != ELFMAG2
40      || e->e_ident[EI_MAG3] != ELFMAG3
41      || e->e_ident[EI_VERSION] != EV_CURRENT
42      || e->e_version != EV_CURRENT)
43    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
44
45  return GRUB_ERR_NONE;
46}
47
48grub_err_t
49grub_elf_close (grub_elf_t elf)
50{
51  grub_file_t file = elf->file;
52
53  grub_free (elf->phdrs);
54  grub_free (elf->filename);
55  grub_free (elf);
56
57  if (file)
58    grub_file_close (file);
59
60  return grub_errno;
61}
62
63grub_elf_t
64grub_elf_file (grub_file_t file, const char *filename)
65{
66  grub_elf_t elf;
67
68  elf = grub_zalloc (sizeof (*elf));
69  if (! elf)
70    return 0;
71
72  elf->file = file;
73
74  if (grub_file_seek (elf->file, 0) == (grub_off_t) -1)
75    goto fail;
76
77  if (grub_file_read (elf->file, &elf->ehdr, sizeof (elf->ehdr))
78      != sizeof (elf->ehdr))
79    {
80      if (!grub_errno)
81        grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
82                    filename);
83      goto fail;
84    }
85
86  if (grub_elf_check_header (elf))
87    goto fail;
88
89  elf->filename = grub_strdup (filename);
90  if (!elf->filename)
91    goto fail;
92
93  return elf;
94
95fail:
96  grub_free (elf->filename);
97  grub_free (elf->phdrs);
98  grub_free (elf);
99  return 0;
100}
101
102grub_elf_t
103grub_elf_open (const char *name)
104{
105  grub_file_t file;
106  grub_elf_t elf;
107
108  file = grub_file_open (name);
109  if (! file)
110    return 0;
111
112  elf = grub_elf_file (file, name);
113  if (! elf)
114    grub_file_close (file);
115
116  return elf;
117}
118
119
120/* 32-bit */
121#define ehdrXX ehdr32
122#define ELFCLASSXX ELFCLASS32
123#define ElfXX_Addr Elf32_Addr
124#define grub_elfXX_size grub_elf32_size
125#define grub_elfXX_load grub_elf32_load
126#define FOR_ELFXX_PHDRS FOR_ELF32_PHDRS
127#define grub_elf_is_elfXX grub_elf_is_elf32
128#define grub_elfXX_load_phdrs grub_elf32_load_phdrs
129#define ElfXX_Phdr Elf32_Phdr
130#define grub_uintXX_t grub_uint32_t
131
132#include "elfXX.c"
133
134#undef ehdrXX
135#undef ELFCLASSXX
136#undef ElfXX_Addr
137#undef grub_elfXX_size
138#undef grub_elfXX_load
139#undef FOR_ELFXX_PHDRS
140#undef grub_elf_is_elfXX
141#undef grub_elfXX_load_phdrs
142#undef ElfXX_Phdr
143#undef grub_uintXX_t
144
145
146/* 64-bit */
147#define ehdrXX ehdr64
148#define ELFCLASSXX ELFCLASS64
149#define ElfXX_Addr Elf64_Addr
150#define grub_elfXX_size grub_elf64_size
151#define grub_elfXX_load grub_elf64_load
152#define FOR_ELFXX_PHDRS FOR_ELF64_PHDRS
153#define grub_elf_is_elfXX grub_elf_is_elf64
154#define grub_elfXX_load_phdrs grub_elf64_load_phdrs
155#define ElfXX_Phdr Elf64_Phdr
156#define grub_uintXX_t grub_uint64_t
157
158#include "elfXX.c"
Note: See TracBrowser for help on using the repository browser.