Linux-2.6.12-rc2
[deliverable/linux.git] / arch / mips / kernel / module-elf64.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * Copyright (C) 2001 Rusty Russell.
17 * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
18 */
19
20 #undef DEBUG
21
22 #include <linux/moduleloader.h>
23 #include <linux/elf.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/fs.h>
27 #include <linux/string.h>
28 #include <linux/kernel.h>
29
30 struct mips_hi16 {
31 struct mips_hi16 *next;
32 Elf32_Addr *addr;
33 Elf64_Addr value;
34 };
35
36 static struct mips_hi16 *mips_hi16_list;
37
38 void *module_alloc(unsigned long size)
39 {
40 if (size == 0)
41 return NULL;
42 return vmalloc(size);
43 }
44
45
46 /* Free memory returned from module_alloc */
47 void module_free(struct module *mod, void *module_region)
48 {
49 vfree(module_region);
50 /* FIXME: If module_region == mod->init_region, trim exception
51 table entries. */
52 }
53
54 int module_frob_arch_sections(Elf_Ehdr *hdr,
55 Elf_Shdr *sechdrs,
56 char *secstrings,
57 struct module *mod)
58 {
59 return 0;
60 }
61
62 int apply_relocate(Elf64_Shdr *sechdrs,
63 const char *strtab,
64 unsigned int symindex,
65 unsigned int relsec,
66 struct module *me)
67 {
68 /*
69 * We don't want to deal with REL relocations - RELA is so much saner.
70 */
71 if (!sechdrs[relsec].sh_size)
72 return 0;
73
74 printk(KERN_ERR "module %s: REL relocation unsupported\n",
75 me->name);
76 return -ENOEXEC;
77 }
78
79 static int apply_r_mips_none(struct module *me, uint32_t *location,
80 Elf64_Addr v)
81 {
82 return 0;
83 }
84
85 static int apply_r_mips_32(struct module *me, uint32_t *location,
86 Elf64_Addr v)
87 {
88 *location = v;
89
90 return 0;
91 }
92
93 static int apply_r_mips_26(struct module *me, uint32_t *location,
94 Elf64_Addr v)
95 {
96 if (v % 4) {
97 printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
98 return -ENOEXEC;
99 }
100
101 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
102 printk(KERN_ERR
103 "module %s: relocation overflow\n",
104 me->name);
105 return -ENOEXEC;
106 }
107
108 *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
109
110 return 0;
111 }
112
113 static int apply_r_mips_hi16(struct module *me, uint32_t *location,
114 Elf64_Addr v)
115 {
116 struct mips_hi16 *n;
117
118 /*
119 * We cannot relocate this one now because we don't know the value of
120 * the carry we need to add. Save the information, and let LO16 do the
121 * actual relocation.
122 */
123 n = kmalloc(sizeof *n, GFP_KERNEL);
124 if (!n)
125 return -ENOMEM;
126
127 n->addr = location;
128 n->value = v;
129 n->next = mips_hi16_list;
130 mips_hi16_list = n;
131
132 return 0;
133 }
134
135 static int apply_r_mips_lo16(struct module *me, uint32_t *location,
136 Elf64_Addr v)
137 {
138 unsigned long insnlo = *location;
139 Elf32_Addr val, vallo;
140
141 /* Sign extend the addend we extract from the lo insn. */
142 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
143
144 if (mips_hi16_list != NULL) {
145 struct mips_hi16 *l;
146
147 l = mips_hi16_list;
148 while (l != NULL) {
149 struct mips_hi16 *next;
150 unsigned long insn;
151
152 /*
153 * The value for the HI16 had best be the same.
154 */
155 if (v != l->value)
156 goto out_danger;
157
158 /*
159 * Do the HI16 relocation. Note that we actually don't
160 * need to know anything about the LO16 itself, except
161 * where to find the low 16 bits of the addend needed
162 * by the LO16.
163 */
164 insn = *l->addr;
165 val = ((insn & 0xffff) << 16) + vallo;
166 val += v;
167
168 /*
169 * Account for the sign extension that will happen in
170 * the low bits.
171 */
172 val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
173
174 insn = (insn & ~0xffff) | val;
175 *l->addr = insn;
176
177 next = l->next;
178 kfree(l);
179 l = next;
180 }
181
182 mips_hi16_list = NULL;
183 }
184
185 /*
186 * Ok, we're done with the HI16 relocs. Now deal with the LO16.
187 */
188 insnlo = (insnlo & ~0xffff) | (v & 0xffff);
189 *location = insnlo;
190
191 return 0;
192
193 out_danger:
194 printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
195
196 return -ENOEXEC;
197 }
198
199 static int apply_r_mips_64(struct module *me, uint32_t *location,
200 Elf64_Addr v)
201 {
202 *(uint64_t *) location = v;
203
204 return 0;
205 }
206
207
208 static int apply_r_mips_higher(struct module *me, uint32_t *location,
209 Elf64_Addr v)
210 {
211 *location = (*location & 0xffff0000) |
212 ((((long long) v + 0x80008000LL) >> 32) & 0xffff);
213
214 return 0;
215 }
216
217 static int apply_r_mips_highest(struct module *me, uint32_t *location,
218 Elf64_Addr v)
219 {
220 *location = (*location & 0xffff0000) |
221 ((((long long) v + 0x800080008000LL) >> 48) & 0xffff);
222
223 return 0;
224 }
225
226 static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
227 Elf64_Addr v) = {
228 [R_MIPS_NONE] = apply_r_mips_none,
229 [R_MIPS_32] = apply_r_mips_32,
230 [R_MIPS_26] = apply_r_mips_26,
231 [R_MIPS_HI16] = apply_r_mips_hi16,
232 [R_MIPS_LO16] = apply_r_mips_lo16,
233 [R_MIPS_64] = apply_r_mips_64,
234 [R_MIPS_HIGHER] = apply_r_mips_higher,
235 [R_MIPS_HIGHEST] = apply_r_mips_highest
236 };
237
238 int apply_relocate_add(Elf64_Shdr *sechdrs,
239 const char *strtab,
240 unsigned int symindex,
241 unsigned int relsec,
242 struct module *me)
243 {
244 Elf64_Mips_Rela *rel = (void *) sechdrs[relsec].sh_addr;
245 Elf64_Sym *sym;
246 uint32_t *location;
247 unsigned int i;
248 Elf64_Addr v;
249 int res;
250
251 pr_debug("Applying relocate section %u to %u\n", relsec,
252 sechdrs[relsec].sh_info);
253
254 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
255 /* This is where to make the change */
256 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
257 + rel[i].r_offset;
258 /* This is the symbol it is referring to */
259 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr + rel[i].r_sym;
260 if (!sym->st_value) {
261 printk(KERN_WARNING "%s: Unknown symbol %s\n",
262 me->name, strtab + sym->st_name);
263 return -ENOENT;
264 }
265
266 v = sym->st_value;
267
268 res = reloc_handlers[rel[i].r_type](me, location, v);
269 if (res)
270 return res;
271 }
272
273 return 0;
274 }
This page took 0.035862 seconds and 5 git commands to generate.