MIPS: Fix memory leak in error path of HI16/LO16 relocation handling.
[linux-2.6-block.git] / arch / mips / kernel / module.c
CommitLineData
4e6a05fe
TS
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 * Copyright (C) 2005 Thiemo Seufer
19 */
20
21#undef DEBUG
22
23#include <linux/moduleloader.h>
24#include <linux/elf.h>
27ac792c 25#include <linux/mm.h>
4e6a05fe
TS
26#include <linux/vmalloc.h>
27#include <linux/slab.h>
28#include <linux/fs.h>
29#include <linux/string.h>
30#include <linux/kernel.h>
1da177e4 31#include <linux/spinlock.h>
94bb0c1a
DD
32#include <linux/jump_label.h>
33
656be92f 34#include <asm/pgtable.h> /* MODULE_START */
1da177e4 35
4e6a05fe
TS
36struct mips_hi16 {
37 struct mips_hi16 *next;
38 Elf_Addr *addr;
39 Elf_Addr value;
40};
41
42static struct mips_hi16 *mips_hi16_list;
43
1da177e4
LT
44static LIST_HEAD(dbe_list);
45static DEFINE_SPINLOCK(dbe_lock);
46
66574cc0 47#ifdef MODULE_START
4e6a05fe
TS
48void *module_alloc(unsigned long size)
49{
d0a21265
DR
50 return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
51 GFP_KERNEL, PAGE_KERNEL, -1,
52 __builtin_return_address(0));
4e6a05fe 53}
66574cc0 54#endif
4e6a05fe
TS
55
56static int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
57{
58 return 0;
59}
60
61static int apply_r_mips_32_rel(struct module *me, u32 *location, Elf_Addr v)
62{
63 *location += v;
64
65 return 0;
66}
67
68static int apply_r_mips_32_rela(struct module *me, u32 *location, Elf_Addr v)
69{
70 *location = v;
71
72 return 0;
73}
74
75static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
76{
77 if (v % 4) {
6f9fdeb6
RB
78 pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
79 me->name);
4e6a05fe
TS
80 return -ENOEXEC;
81 }
82
83 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
84 printk(KERN_ERR
85 "module %s: relocation overflow\n",
86 me->name);
87 return -ENOEXEC;
88 }
89
90 *location = (*location & ~0x03ffffff) |
91 ((*location + (v >> 2)) & 0x03ffffff);
92
93 return 0;
94}
95
96static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
97{
98 if (v % 4) {
6f9fdeb6
RB
99 pr_err("module %s: dangerous R_MIPS_26 RELArelocation\n",
100 me->name);
4e6a05fe
TS
101 return -ENOEXEC;
102 }
103
104 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
105 printk(KERN_ERR
106 "module %s: relocation overflow\n",
107 me->name);
108 return -ENOEXEC;
109 }
110
111 *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
112
113 return 0;
114}
115
116static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
117{
118 struct mips_hi16 *n;
119
120 /*
121 * We cannot relocate this one now because we don't know the value of
122 * the carry we need to add. Save the information, and let LO16 do the
123 * actual relocation.
124 */
125 n = kmalloc(sizeof *n, GFP_KERNEL);
126 if (!n)
127 return -ENOMEM;
128
129 n->addr = (Elf_Addr *)location;
130 n->value = v;
131 n->next = mips_hi16_list;
132 mips_hi16_list = n;
133
134 return 0;
135}
136
137static int apply_r_mips_hi16_rela(struct module *me, u32 *location, Elf_Addr v)
138{
139 *location = (*location & 0xffff0000) |
140 ((((long long) v + 0x8000LL) >> 16) & 0xffff);
141
142 return 0;
143}
144
145static int apply_r_mips_lo16_rel(struct module *me, u32 *location, Elf_Addr v)
146{
147 unsigned long insnlo = *location;
148 Elf_Addr val, vallo;
d3cac35c 149 struct mips_hi16 *l, *next;
4e6a05fe
TS
150
151 /* Sign extend the addend we extract from the lo insn. */
152 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
153
154 if (mips_hi16_list != NULL) {
4e6a05fe
TS
155
156 l = mips_hi16_list;
157 while (l != NULL) {
4e6a05fe
TS
158 unsigned long insn;
159
160 /*
161 * The value for the HI16 had best be the same.
162 */
163 if (v != l->value)
164 goto out_danger;
165
166 /*
167 * Do the HI16 relocation. Note that we actually don't
168 * need to know anything about the LO16 itself, except
169 * where to find the low 16 bits of the addend needed
170 * by the LO16.
171 */
172 insn = *l->addr;
173 val = ((insn & 0xffff) << 16) + vallo;
174 val += v;
175
176 /*
177 * Account for the sign extension that will happen in
178 * the low bits.
179 */
180 val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
181
182 insn = (insn & ~0xffff) | val;
183 *l->addr = insn;
184
185 next = l->next;
186 kfree(l);
187 l = next;
188 }
189
190 mips_hi16_list = NULL;
191 }
192
193 /*
194 * Ok, we're done with the HI16 relocs. Now deal with the LO16.
195 */
196 val = v + vallo;
197 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
198 *location = insnlo;
199
200 return 0;
201
202out_danger:
d3cac35c
RB
203 while (l) {
204 next = l->next;
205 kfree(l);
206 l = next;
207 }
208
6f9fdeb6 209 pr_err("module %s: dangerous R_MIPS_LO16 REL relocation\n", me->name);
4e6a05fe
TS
210
211 return -ENOEXEC;
212}
213
214static int apply_r_mips_lo16_rela(struct module *me, u32 *location, Elf_Addr v)
215{
216 *location = (*location & 0xffff0000) | (v & 0xffff);
217
218 return 0;
219}
220
221static int apply_r_mips_64_rela(struct module *me, u32 *location, Elf_Addr v)
222{
223 *(Elf_Addr *)location = v;
224
225 return 0;
226}
227
228static int apply_r_mips_higher_rela(struct module *me, u32 *location,
229 Elf_Addr v)
230{
231 *location = (*location & 0xffff0000) |
232 ((((long long) v + 0x80008000LL) >> 32) & 0xffff);
233
234 return 0;
235}
236
237static int apply_r_mips_highest_rela(struct module *me, u32 *location,
238 Elf_Addr v)
239{
240 *location = (*location & 0xffff0000) |
241 ((((long long) v + 0x800080008000LL) >> 48) & 0xffff);
242
243 return 0;
244}
245
246static int (*reloc_handlers_rel[]) (struct module *me, u32 *location,
247 Elf_Addr v) = {
248 [R_MIPS_NONE] = apply_r_mips_none,
249 [R_MIPS_32] = apply_r_mips_32_rel,
250 [R_MIPS_26] = apply_r_mips_26_rel,
251 [R_MIPS_HI16] = apply_r_mips_hi16_rel,
252 [R_MIPS_LO16] = apply_r_mips_lo16_rel
253};
254
255static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
256 Elf_Addr v) = {
257 [R_MIPS_NONE] = apply_r_mips_none,
258 [R_MIPS_32] = apply_r_mips_32_rela,
259 [R_MIPS_26] = apply_r_mips_26_rela,
260 [R_MIPS_HI16] = apply_r_mips_hi16_rela,
261 [R_MIPS_LO16] = apply_r_mips_lo16_rela,
262 [R_MIPS_64] = apply_r_mips_64_rela,
263 [R_MIPS_HIGHER] = apply_r_mips_higher_rela,
264 [R_MIPS_HIGHEST] = apply_r_mips_highest_rela
265};
266
267int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
268 unsigned int symindex, unsigned int relsec,
269 struct module *me)
270{
271 Elf_Mips_Rel *rel = (void *) sechdrs[relsec].sh_addr;
272 Elf_Sym *sym;
273 u32 *location;
274 unsigned int i;
275 Elf_Addr v;
276 int res;
277
278 pr_debug("Applying relocate section %u to %u\n", relsec,
279 sechdrs[relsec].sh_info);
280
281 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
282 /* This is where to make the change */
283 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
284 + rel[i].r_offset;
285 /* This is the symbol it is referring to */
286 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
287 + ELF_MIPS_R_SYM(rel[i]);
0e66fff8 288 if (IS_ERR_VALUE(sym->st_value)) {
f3bf07b9
AN
289 /* Ignore unresolved weak symbol */
290 if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
291 continue;
4e6a05fe
TS
292 printk(KERN_WARNING "%s: Unknown symbol %s\n",
293 me->name, strtab + sym->st_name);
294 return -ENOENT;
295 }
296
297 v = sym->st_value;
298
299 res = reloc_handlers_rel[ELF_MIPS_R_TYPE(rel[i])](me, location, v);
300 if (res)
301 return res;
302 }
303
304 return 0;
305}
306
307int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
308 unsigned int symindex, unsigned int relsec,
309 struct module *me)
310{
311 Elf_Mips_Rela *rel = (void *) sechdrs[relsec].sh_addr;
312 Elf_Sym *sym;
313 u32 *location;
314 unsigned int i;
315 Elf_Addr v;
316 int res;
317
318 pr_debug("Applying relocate section %u to %u\n", relsec,
319 sechdrs[relsec].sh_info);
320
321 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
322 /* This is where to make the change */
323 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
324 + rel[i].r_offset;
325 /* This is the symbol it is referring to */
326 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
327 + ELF_MIPS_R_SYM(rel[i]);
0e66fff8 328 if (IS_ERR_VALUE(sym->st_value)) {
f3bf07b9
AN
329 /* Ignore unresolved weak symbol */
330 if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
331 continue;
4e6a05fe
TS
332 printk(KERN_WARNING "%s: Unknown symbol %s\n",
333 me->name, strtab + sym->st_name);
334 return -ENOENT;
335 }
336
337 v = sym->st_value + rel[i].r_addend;
338
339 res = reloc_handlers_rela[ELF_MIPS_R_TYPE(rel[i])](me, location, v);
340 if (res)
341 return res;
342 }
343
344 return 0;
345}
346
1da177e4
LT
347/* Given an address, look for it in the module exception tables. */
348const struct exception_table_entry *search_module_dbetables(unsigned long addr)
349{
350 unsigned long flags;
351 const struct exception_table_entry *e = NULL;
352 struct mod_arch_specific *dbe;
353
354 spin_lock_irqsave(&dbe_lock, flags);
355 list_for_each_entry(dbe, &dbe_list, dbe_list) {
356 e = search_extable(dbe->dbe_start, dbe->dbe_end - 1, addr);
357 if (e)
358 break;
359 }
360 spin_unlock_irqrestore(&dbe_lock, flags);
361
362 /* Now, if we found one, we are running inside it now, hence
363 we cannot unload the module, hence no refcnt needed. */
364 return e;
365}
366
3a4fa0a2 367/* Put in dbe list if necessary. */
1da177e4
LT
368int module_finalize(const Elf_Ehdr *hdr,
369 const Elf_Shdr *sechdrs,
370 struct module *me)
371{
372 const Elf_Shdr *s;
373 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
374
94bb0c1a
DD
375 /* Make jump label nops. */
376 jump_label_apply_nops(me);
377
1da177e4
LT
378 INIT_LIST_HEAD(&me->arch.dbe_list);
379 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
380 if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
381 continue;
382 me->arch.dbe_start = (void *)s->sh_addr;
383 me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
384 spin_lock_irq(&dbe_lock);
385 list_add(&me->arch.dbe_list, &dbe_list);
386 spin_unlock_irq(&dbe_lock);
387 }
388 return 0;
389}
390
391void module_arch_cleanup(struct module *mod)
392{
393 spin_lock_irq(&dbe_lock);
394 list_del(&mod->arch.dbe_list);
395 spin_unlock_irq(&dbe_lock);
396}