1 /* dl.c - arch-dependent part of loadable module support */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2005,2007,2009 Free Software Foundation, Inc.
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.
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.
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/>.
22 #include <grub/misc.h>
24 #include <grub/i18n.h>
26 /* Check if EHDR is a valid ELF header. */
28 grub_arch_dl_check_header (void *ehdr
)
32 /* Check the magic numbers. */
33 if (e
->e_ident
[EI_CLASS
] != ELFCLASS64
34 || e
->e_ident
[EI_DATA
] != ELFDATA2MSB
35 || e
->e_machine
!= EM_SPARCV9
)
36 return grub_error (GRUB_ERR_BAD_OS
, N_("invalid arch-dependent ELF magic"));
41 #pragma GCC diagnostic ignored "-Wcast-align"
45 grub_uint8_t code
[0x28];
49 static const grub_uint8_t trampoline_code
[0x28] =
51 /* 0: */ 0x82, 0x10, 0x00, 0x0f, /* mov %o7, %g1 */
52 /* 4: */ 0x40, 0x00, 0x00, 0x02, /* call 0xc */
53 /* 8: */ 0x01, 0x00, 0x00, 0x00, /* nop */
54 /* c: */ 0x9e, 0x1b, 0xc0, 0x01, /* xor %o7, %g1, %o7 */
55 /* 10: */ 0x82, 0x18, 0x40, 0x0f, /* xor %g1, %o7, %g1 */
56 /* 14: */ 0x9e, 0x1b, 0xc0, 0x01, /* xor %o7, %g1, %o7 */
57 /* 18: */ 0xc2, 0x58, 0x60, 0x24, /* ldx [ %g1 + 0x24 ], %g1 */
58 /* 1c: */ 0x81, 0xc0, 0x40, 0x00, /* jmp %g1 */
59 /* 20: */ 0x01, 0x00, 0x00, 0x00, /* nop */
60 /* 24: */ 0x01, 0x00, 0x00, 0x00, /* nop */
64 grub_arch_dl_get_tramp_got_size (const void *ehdr
, grub_size_t
*tramp
,
67 const Elf_Ehdr
*e
= ehdr
;
74 for (i
= 0, s
= (const Elf_Shdr
*) ((grub_addr_t
) e
+ e
->e_shoff
);
76 i
++, s
= (const Elf_Shdr
*) ((grub_addr_t
) s
+ e
->e_shentsize
))
77 if (s
->sh_type
== SHT_REL
|| s
->sh_type
== SHT_RELA
)
79 const Elf_Rel
*rel
, *max
;
81 for (rel
= (const Elf_Rel
*) ((grub_addr_t
) e
+ s
->sh_offset
),
82 max
= rel
+ s
->sh_size
/ s
->sh_entsize
;
84 rel
= (const Elf_Rel
*) ((grub_addr_t
) rel
+ s
->sh_entsize
))
85 switch (ELF_R_TYPE (rel
->r_info
))
89 *tramp
+= sizeof (struct trampoline
);
98 /* Relocate symbols. */
100 grub_arch_dl_relocate_symbols (grub_dl_t mod
, void *ehdr
,
101 Elf_Shdr
*s
, grub_dl_segment_t seg
)
105 for (rel
= (Elf_Rela
*) ((char *) ehdr
+ s
->sh_offset
),
106 max
= (Elf_Rela
*) ((char *) rel
+ s
->sh_size
);
108 rel
= (Elf_Rela
*) ((char *) rel
+ s
->sh_entsize
))
114 if (seg
->size
< rel
->r_offset
)
115 return grub_error (GRUB_ERR_BAD_MODULE
,
116 "reloc offset is out of the segment");
118 addr
= (Elf_Word
*) ((char *) seg
->addr
+ rel
->r_offset
);
119 sym
= (Elf_Sym
*) ((char *) mod
->symtab
120 + mod
->symsize
* ELF_R_SYM (rel
->r_info
));
122 value
= sym
->st_value
+ rel
->r_addend
;
123 switch (ELF_R_TYPE (rel
->r_info
) & 0xff)
125 case R_SPARC_32
: /* 3 V-word32 */
126 if (value
& 0xFFFFFFFF00000000)
127 return grub_error (GRUB_ERR_BAD_MODULE
,
128 "address out of 32 bits range");
131 case R_SPARC_WDISP30
: /* 7 V-disp30 */
132 if (((value
- (Elf_Addr
) addr
) & 0xFFFFFFFF00000000) &&
133 (((value
- (Elf_Addr
) addr
) & 0xFFFFFFFF00000000)
134 != 0xFFFFFFFF00000000))
136 struct trampoline
*tp
= mod
->trampptr
;
137 mod
->trampptr
= tp
+ 1;
138 grub_memcpy (tp
->code
, trampoline_code
, sizeof (tp
->code
));
140 value
= (Elf_Addr
) tp
;
143 if (((value
- (Elf_Addr
) addr
) & 0xFFFFFFFF00000000) &&
144 (((value
- (Elf_Addr
) addr
) & 0xFFFFFFFF00000000)
145 != 0xFFFFFFFF00000000))
146 return grub_error (GRUB_ERR_BAD_MODULE
,
147 "displacement out of 30 bits range");
148 *addr
= (*addr
& 0xC0000000) |
149 (((grub_int32_t
) ((value
- (Elf_Addr
) addr
) >> 2)) &
152 case R_SPARC_HH22
: /* 9 V-imm22 */
153 *addr
= (*addr
& 0xFFC00000) | ((value
>> 42) & 0x3FFFFF);
155 case R_SPARC_HM10
: /* 12 T-simm13 */
156 *addr
= (*addr
& 0xFFFFFC00) | ((value
>> 32) & 0x3FF);
158 case R_SPARC_HI22
: /* 9 V-imm22 */
160 return grub_error (GRUB_ERR_BAD_MODULE
,
161 "address out of 32 bits range");
163 *addr
= (*addr
& 0xFFC00000) | ((value
>> 10) & 0x3FFFFF);
165 case R_SPARC_LO10
: /* 12 T-simm13 */
166 *addr
= (*addr
& 0xFFFFFC00) | (value
& 0x3FF);
168 case R_SPARC_64
: /* 32 V-xwords64 */
169 *(Elf_Xword
*) addr
= value
;
172 *addr
= (*addr
& ~0x1fff)
173 | (((value
& 0x3ff) +
174 (ELF_R_TYPE (rel
->r_info
) >> 8))
178 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
179 N_("relocation 0x%x is not implemented yet"),
180 ELF_R_TYPE (rel
->r_info
));
184 return GRUB_ERR_NONE
;