Linux 2.6.13-rc4
[linux-2.6/next.git] / arch / mips / kernel / module-elf32.c
blobffd216d6d6dc39edeb1c881e02a1629cd5c31ec7
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.
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.
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
16 * Copyright (C) 2001 Rusty Russell.
17 * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
20 #undef DEBUG
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>
30 struct mips_hi16 {
31 struct mips_hi16 *next;
32 Elf32_Addr *addr;
33 Elf32_Addr value;
36 static struct mips_hi16 *mips_hi16_list;
38 void *module_alloc(unsigned long size)
40 if (size == 0)
41 return NULL;
42 return vmalloc(size);
46 /* Free memory returned from module_alloc */
47 void module_free(struct module *mod, void *module_region)
49 vfree(module_region);
50 /* FIXME: If module_region == mod->init_region, trim exception
51 table entries. */
54 int module_frob_arch_sections(Elf_Ehdr *hdr,
55 Elf_Shdr *sechdrs,
56 char *secstrings,
57 struct module *mod)
59 return 0;
62 static int apply_r_mips_none(struct module *me, uint32_t *location,
63 Elf32_Addr v)
65 return 0;
68 static int apply_r_mips_32(struct module *me, uint32_t *location,
69 Elf32_Addr v)
71 *location += v;
73 return 0;
76 static int apply_r_mips_26(struct module *me, uint32_t *location,
77 Elf32_Addr v)
79 if (v % 4) {
80 printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
81 return -ENOEXEC;
84 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
85 printk(KERN_ERR
86 "module %s: relocation overflow\n",
87 me->name);
88 return -ENOEXEC;
91 *location = (*location & ~0x03ffffff) |
92 ((*location + (v >> 2)) & 0x03ffffff);
94 return 0;
97 static int apply_r_mips_hi16(struct module *me, uint32_t *location,
98 Elf32_Addr v)
100 struct mips_hi16 *n;
103 * We cannot relocate this one now because we don't know the value of
104 * the carry we need to add. Save the information, and let LO16 do the
105 * actual relocation.
107 n = kmalloc(sizeof *n, GFP_KERNEL);
108 if (!n)
109 return -ENOMEM;
111 n->addr = location;
112 n->value = v;
113 n->next = mips_hi16_list;
114 mips_hi16_list = n;
116 return 0;
119 static int apply_r_mips_lo16(struct module *me, uint32_t *location,
120 Elf32_Addr v)
122 unsigned long insnlo = *location;
123 Elf32_Addr val, vallo;
125 /* Sign extend the addend we extract from the lo insn. */
126 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
128 if (mips_hi16_list != NULL) {
129 struct mips_hi16 *l;
131 l = mips_hi16_list;
132 while (l != NULL) {
133 struct mips_hi16 *next;
134 unsigned long insn;
137 * The value for the HI16 had best be the same.
139 if (v != l->value)
140 goto out_danger;
143 * Do the HI16 relocation. Note that we actually don't
144 * need to know anything about the LO16 itself, except
145 * where to find the low 16 bits of the addend needed
146 * by the LO16.
148 insn = *l->addr;
149 val = ((insn & 0xffff) << 16) + vallo;
150 val += v;
153 * Account for the sign extension that will happen in
154 * the low bits.
156 val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
158 insn = (insn & ~0xffff) | val;
159 *l->addr = insn;
161 next = l->next;
162 kfree(l);
163 l = next;
166 mips_hi16_list = NULL;
170 * Ok, we're done with the HI16 relocs. Now deal with the LO16.
172 val = v + vallo;
173 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
174 *location = insnlo;
176 return 0;
178 out_danger:
179 printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
181 return -ENOEXEC;
184 static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
185 Elf32_Addr v) = {
186 [R_MIPS_NONE] = apply_r_mips_none,
187 [R_MIPS_32] = apply_r_mips_32,
188 [R_MIPS_26] = apply_r_mips_26,
189 [R_MIPS_HI16] = apply_r_mips_hi16,
190 [R_MIPS_LO16] = apply_r_mips_lo16
193 int apply_relocate(Elf32_Shdr *sechdrs,
194 const char *strtab,
195 unsigned int symindex,
196 unsigned int relsec,
197 struct module *me)
199 Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
200 Elf32_Sym *sym;
201 uint32_t *location;
202 unsigned int i;
203 Elf32_Addr v;
204 int res;
206 pr_debug("Applying relocate section %u to %u\n", relsec,
207 sechdrs[relsec].sh_info);
209 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
210 Elf32_Word r_info = rel[i].r_info;
212 /* This is where to make the change */
213 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
214 + rel[i].r_offset;
215 /* This is the symbol it is referring to */
216 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
217 + ELF32_R_SYM(r_info);
218 if (!sym->st_value) {
219 printk(KERN_WARNING "%s: Unknown symbol %s\n",
220 me->name, strtab + sym->st_name);
221 return -ENOENT;
224 v = sym->st_value;
226 res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
227 if (res)
228 return res;
231 return 0;
234 int apply_relocate_add(Elf32_Shdr *sechdrs,
235 const char *strtab,
236 unsigned int symindex,
237 unsigned int relsec,
238 struct module *me)
241 * Current binutils always generate .rela relocations. Keep smiling
242 * if it's empty, abort otherwise.
244 if (!sechdrs[relsec].sh_size)
245 return 0;
247 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
248 me->name);
249 return -ENOEXEC;