Fix cross compilation (e.g. on Darwin). Following changes to make.tmpl,
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / sparc64 / dl.c
blobd25c15e10dd464849257cf8a0351306133df276a
1 /* dl.c - arch-dependent part of loadable module support */
2 /*
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/>.
20 #include <grub/dl.h>
21 #include <grub/elf.h>
22 #include <grub/misc.h>
23 #include <grub/err.h>
24 #include <grub/i18n.h>
26 /* Check if EHDR is a valid ELF header. */
27 grub_err_t
28 grub_arch_dl_check_header (void *ehdr)
30 Elf_Ehdr *e = 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"));
38 return GRUB_ERR_NONE;
41 #pragma GCC diagnostic ignored "-Wcast-align"
43 struct trampoline
45 grub_uint8_t code[0x28];
46 grub_uint64_t addr;
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 */
63 grub_err_t
64 grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
65 grub_size_t *got)
67 const Elf_Ehdr *e = ehdr;
68 const Elf_Shdr *s;
69 unsigned i;
71 *tramp = 0;
72 *got = 0;
74 for (i = 0, s = (const Elf_Shdr *) ((grub_addr_t) e + e->e_shoff);
75 i < e->e_shnum;
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;
83 rel < max;
84 rel = (const Elf_Rel *) ((grub_addr_t) rel + s->sh_entsize))
85 switch (ELF_R_TYPE (rel->r_info))
87 case R_SPARC_WDISP30:
89 *tramp += sizeof (struct trampoline);
90 break;
95 return GRUB_ERR_NONE;
98 /* Relocate symbols. */
99 grub_err_t
100 grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
101 Elf_Shdr *s, grub_dl_segment_t seg)
103 Elf_Rela *rel, *max;
105 for (rel = (Elf_Rela *) ((char *) ehdr + s->sh_offset),
106 max = (Elf_Rela *) ((char *) rel + s->sh_size);
107 rel < max;
108 rel = (Elf_Rela *) ((char *) rel + s->sh_entsize))
110 Elf_Word *addr;
111 Elf_Sym *sym;
112 Elf_Addr value;
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");
129 *addr = value;
130 break;
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));
139 tp->addr = value;
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)) &
150 0x3FFFFFFF);
151 break;
152 case R_SPARC_HH22: /* 9 V-imm22 */
153 *addr = (*addr & 0xFFC00000) | ((value >> 42) & 0x3FFFFF);
154 break;
155 case R_SPARC_HM10: /* 12 T-simm13 */
156 *addr = (*addr & 0xFFFFFC00) | ((value >> 32) & 0x3FF);
157 break;
158 case R_SPARC_HI22: /* 9 V-imm22 */
159 if (value >> 32)
160 return grub_error (GRUB_ERR_BAD_MODULE,
161 "address out of 32 bits range");
162 case R_SPARC_LM22:
163 *addr = (*addr & 0xFFC00000) | ((value >> 10) & 0x3FFFFF);
164 break;
165 case R_SPARC_LO10: /* 12 T-simm13 */
166 *addr = (*addr & 0xFFFFFC00) | (value & 0x3FF);
167 break;
168 case R_SPARC_64: /* 32 V-xwords64 */
169 *(Elf_Xword *) addr = value;
170 break;
171 case R_SPARC_OLO10:
172 *addr = (*addr & ~0x1fff)
173 | (((value & 0x3ff) +
174 (ELF_R_TYPE (rel->r_info) >> 8))
175 & 0x1fff);
176 break;
177 default:
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;