~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to efiemu/i386/loadcore64.c

Tags: upstream-1.99~20101122
ImportĀ upstreamĀ versionĀ 1.99~20101122

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* i386 CPU-specific part of loadcore.c for 32-bit mode */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 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/mm.h>
22
 
#include <grub/misc.h>
23
 
#include <grub/efiemu/efiemu.h>
24
 
#include <grub/cpu/efiemu.h>
25
 
#include <grub/elf.h>
26
 
 
27
 
/* Check if EHDR is a valid ELF header.  */
28
 
int
29
 
grub_arch_efiemu_check_header64 (void *ehdr)
30
 
{
31
 
  Elf64_Ehdr *e = ehdr;
32
 
 
33
 
  return (e->e_ident[EI_CLASS] == ELFCLASS64
34
 
          && e->e_ident[EI_DATA] == ELFDATA2LSB
35
 
          && e->e_machine == EM_X86_64);
36
 
}
37
 
 
38
 
/* Relocate symbols.  */
39
 
grub_err_t
40
 
grub_arch_efiemu_relocate_symbols64 (grub_efiemu_segment_t segs,
41
 
                                     struct grub_efiemu_elf_sym *elfsyms,
42
 
                                     void *ehdr)
43
 
{
44
 
  unsigned i;
45
 
  Elf64_Ehdr *e = ehdr;
46
 
  Elf64_Shdr *s;
47
 
  grub_err_t err;
48
 
 
49
 
  for (i = 0, s = (Elf64_Shdr *) ((char *) e + e->e_shoff);
50
 
       i < e->e_shnum;
51
 
       i++, s = (Elf64_Shdr *) ((char *) s + e->e_shentsize))
52
 
    if (s->sh_type == SHT_RELA)
53
 
      {
54
 
        grub_efiemu_segment_t seg;
55
 
        grub_dprintf ("efiemu", "shtrel\n");
56
 
 
57
 
        /* Find the target segment.  */
58
 
        for (seg = segs; seg; seg = seg->next)
59
 
          if (seg->section == s->sh_info)
60
 
            break;
61
 
 
62
 
        if (seg)
63
 
          {
64
 
            Elf64_Rela *rel, *max;
65
 
 
66
 
            for (rel = (Elf64_Rela *) ((char *) e + s->sh_offset),
67
 
                   max = rel + (unsigned long) s->sh_size
68
 
                   / (unsigned long)s->sh_entsize;
69
 
                 rel < max;
70
 
                 rel++)
71
 
              {
72
 
                void *addr;
73
 
                grub_uint32_t *addr32;
74
 
                grub_uint64_t *addr64;
75
 
                struct grub_efiemu_elf_sym sym;
76
 
                if (seg->size < rel->r_offset)
77
 
                  return grub_error (GRUB_ERR_BAD_MODULE,
78
 
                                     "reloc offset is out of the segment");
79
 
 
80
 
                addr =
81
 
                  ((char *) grub_efiemu_mm_obtain_request (seg->handle)
82
 
                   + seg->off + rel->r_offset);
83
 
                addr32 = (grub_uint32_t *) addr;
84
 
                addr64 = (grub_uint64_t *) addr;
85
 
                sym = elfsyms[ELF64_R_SYM (rel->r_info)];
86
 
 
87
 
                switch (ELF64_R_TYPE (rel->r_info))
88
 
                  {
89
 
                  case R_X86_64_64:
90
 
                    if ((err = grub_efiemu_write_value
91
 
                         (addr, *addr64 + rel->r_addend + sym.off, sym.handle,
92
 
                          0, seg->ptv_rel_needed, sizeof (grub_uint64_t))))
93
 
                      return err;
94
 
                    break;
95
 
 
96
 
                  case R_X86_64_PC32:
97
 
                    if ((err = grub_efiemu_write_value
98
 
                         (addr, *addr32 + rel->r_addend + sym.off
99
 
                          - rel->r_offset - seg->off, sym.handle, seg->handle,
100
 
                          seg->ptv_rel_needed, sizeof (grub_uint32_t))))
101
 
                      return err;
102
 
                    break;
103
 
 
104
 
                  case R_X86_64_32:
105
 
                  case R_X86_64_32S:
106
 
                    if ((err = grub_efiemu_write_value
107
 
                         (addr, *addr32 + rel->r_addend + sym.off, sym.handle,
108
 
                          0, seg->ptv_rel_needed, sizeof (grub_uint32_t))))
109
 
                      return err;
110
 
                    break;
111
 
                  default:
112
 
                    return grub_error (GRUB_ERR_BAD_OS,
113
 
                                       "unrecognised relocation");
114
 
                  }
115
 
              }
116
 
          }
117
 
      }
118
 
 
119
 
  return GRUB_ERR_NONE;
120
 
}