Commit | Line | Data |
---|---|---|
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> |
761845f0 | 26 | #include <linux/numa.h> |
4e6a05fe TS |
27 | #include <linux/vmalloc.h> |
28 | #include <linux/slab.h> | |
29 | #include <linux/fs.h> | |
30 | #include <linux/string.h> | |
31 | #include <linux/kernel.h> | |
1da177e4 | 32 | #include <linux/spinlock.h> |
94bb0c1a DD |
33 | #include <linux/jump_label.h> |
34 | ||
656be92f | 35 | #include <asm/pgtable.h> /* MODULE_START */ |
1da177e4 | 36 | |
4e6a05fe TS |
37 | struct mips_hi16 { |
38 | struct mips_hi16 *next; | |
39 | Elf_Addr *addr; | |
40 | Elf_Addr value; | |
41 | }; | |
42 | ||
1da177e4 LT |
43 | static LIST_HEAD(dbe_list); |
44 | static DEFINE_SPINLOCK(dbe_lock); | |
45 | ||
66574cc0 | 46 | #ifdef MODULE_START |
4e6a05fe TS |
47 | void *module_alloc(unsigned long size) |
48 | { | |
d0a21265 | 49 | return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END, |
cb9e3c29 | 50 | GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, |
d0a21265 | 51 | __builtin_return_address(0)); |
4e6a05fe | 52 | } |
66574cc0 | 53 | #endif |
4e6a05fe | 54 | |
6ede8123 | 55 | int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v) |
4e6a05fe TS |
56 | { |
57 | return 0; | |
58 | } | |
59 | ||
60 | static int apply_r_mips_32_rel(struct module *me, u32 *location, Elf_Addr v) | |
61 | { | |
62 | *location += v; | |
63 | ||
64 | return 0; | |
65 | } | |
66 | ||
4e6a05fe TS |
67 | static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v) |
68 | { | |
69 | if (v % 4) { | |
6f9fdeb6 RB |
70 | pr_err("module %s: dangerous R_MIPS_26 REL relocation\n", |
71 | me->name); | |
4e6a05fe TS |
72 | return -ENOEXEC; |
73 | } | |
74 | ||
75 | if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) { | |
55d791f3 | 76 | pr_err("module %s: relocation overflow\n", |
4e6a05fe TS |
77 | me->name); |
78 | return -ENOEXEC; | |
79 | } | |
80 | ||
81 | *location = (*location & ~0x03ffffff) | | |
70342287 | 82 | ((*location + (v >> 2)) & 0x03ffffff); |
4e6a05fe TS |
83 | |
84 | return 0; | |
85 | } | |
86 | ||
4e6a05fe TS |
87 | static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v) |
88 | { | |
89 | struct mips_hi16 *n; | |
90 | ||
91 | /* | |
92 | * We cannot relocate this one now because we don't know the value of | |
93 | * the carry we need to add. Save the information, and let LO16 do the | |
94 | * actual relocation. | |
95 | */ | |
96 | n = kmalloc(sizeof *n, GFP_KERNEL); | |
97 | if (!n) | |
98 | return -ENOMEM; | |
99 | ||
100 | n->addr = (Elf_Addr *)location; | |
101 | n->value = v; | |
861667dc RB |
102 | n->next = me->arch.r_mips_hi16_list; |
103 | me->arch.r_mips_hi16_list = n; | |
4e6a05fe TS |
104 | |
105 | return 0; | |
106 | } | |
107 | ||
c54de490 RB |
108 | static void free_relocation_chain(struct mips_hi16 *l) |
109 | { | |
110 | struct mips_hi16 *next; | |
111 | ||
112 | while (l) { | |
113 | next = l->next; | |
114 | kfree(l); | |
115 | l = next; | |
116 | } | |
117 | } | |
118 | ||
4e6a05fe TS |
119 | static int apply_r_mips_lo16_rel(struct module *me, u32 *location, Elf_Addr v) |
120 | { | |
121 | unsigned long insnlo = *location; | |
c54de490 | 122 | struct mips_hi16 *l; |
4e6a05fe TS |
123 | Elf_Addr val, vallo; |
124 | ||
70342287 | 125 | /* Sign extend the addend we extract from the lo insn. */ |
4e6a05fe TS |
126 | vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000; |
127 | ||
861667dc | 128 | if (me->arch.r_mips_hi16_list != NULL) { |
861667dc | 129 | l = me->arch.r_mips_hi16_list; |
4e6a05fe | 130 | while (l != NULL) { |
c54de490 | 131 | struct mips_hi16 *next; |
4e6a05fe TS |
132 | unsigned long insn; |
133 | ||
134 | /* | |
135 | * The value for the HI16 had best be the same. | |
136 | */ | |
137 | if (v != l->value) | |
138 | goto out_danger; | |
139 | ||
140 | /* | |
141 | * Do the HI16 relocation. Note that we actually don't | |
142 | * need to know anything about the LO16 itself, except | |
143 | * where to find the low 16 bits of the addend needed | |
144 | * by the LO16. | |
145 | */ | |
146 | insn = *l->addr; | |
147 | val = ((insn & 0xffff) << 16) + vallo; | |
148 | val += v; | |
149 | ||
150 | /* | |
151 | * Account for the sign extension that will happen in | |
152 | * the low bits. | |
153 | */ | |
154 | val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff; | |
155 | ||
156 | insn = (insn & ~0xffff) | val; | |
157 | *l->addr = insn; | |
158 | ||
159 | next = l->next; | |
160 | kfree(l); | |
161 | l = next; | |
162 | } | |
163 | ||
861667dc | 164 | me->arch.r_mips_hi16_list = NULL; |
4e6a05fe TS |
165 | } |
166 | ||
167 | /* | |
70342287 | 168 | * Ok, we're done with the HI16 relocs. Now deal with the LO16. |
4e6a05fe TS |
169 | */ |
170 | val = v + vallo; | |
171 | insnlo = (insnlo & ~0xffff) | (val & 0xffff); | |
172 | *location = insnlo; | |
173 | ||
174 | return 0; | |
175 | ||
176 | out_danger: | |
c54de490 RB |
177 | free_relocation_chain(l); |
178 | me->arch.r_mips_hi16_list = NULL; | |
d3cac35c | 179 | |
6f9fdeb6 | 180 | pr_err("module %s: dangerous R_MIPS_LO16 REL relocation\n", me->name); |
4e6a05fe TS |
181 | |
182 | return -ENOEXEC; | |
183 | } | |
184 | ||
5d3c7925 PB |
185 | static int apply_r_mips_pc_rel(struct module *me, u32 *location, Elf_Addr v, |
186 | unsigned bits) | |
187 | { | |
188 | unsigned long mask = GENMASK(bits - 1, 0); | |
189 | unsigned long se_bits; | |
190 | long offset; | |
191 | ||
192 | if (v % 4) { | |
193 | pr_err("module %s: dangerous R_MIPS_PC%u REL relocation\n", | |
194 | me->name, bits); | |
195 | return -ENOEXEC; | |
196 | } | |
197 | ||
198 | /* retrieve & sign extend implicit addend */ | |
199 | offset = *location & mask; | |
200 | offset |= (offset & BIT(bits - 1)) ? ~mask : 0; | |
201 | ||
202 | offset += ((long)v - (long)location) >> 2; | |
203 | ||
204 | /* check the sign bit onwards are identical - ie. we didn't overflow */ | |
205 | se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0; | |
206 | if ((offset & ~mask) != (se_bits & ~mask)) { | |
207 | pr_err("module %s: relocation overflow\n", me->name); | |
208 | return -ENOEXEC; | |
209 | } | |
210 | ||
211 | *location = (*location & ~mask) | (offset & mask); | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
216 | static int apply_r_mips_pc16_rel(struct module *me, u32 *location, Elf_Addr v) | |
217 | { | |
218 | return apply_r_mips_pc_rel(me, location, v, 16); | |
219 | } | |
220 | ||
221 | static int apply_r_mips_pc21_rel(struct module *me, u32 *location, Elf_Addr v) | |
222 | { | |
223 | return apply_r_mips_pc_rel(me, location, v, 21); | |
224 | } | |
225 | ||
226 | static int apply_r_mips_pc26_rel(struct module *me, u32 *location, Elf_Addr v) | |
227 | { | |
228 | return apply_r_mips_pc_rel(me, location, v, 26); | |
229 | } | |
230 | ||
4e6a05fe TS |
231 | static int (*reloc_handlers_rel[]) (struct module *me, u32 *location, |
232 | Elf_Addr v) = { | |
233 | [R_MIPS_NONE] = apply_r_mips_none, | |
234 | [R_MIPS_32] = apply_r_mips_32_rel, | |
235 | [R_MIPS_26] = apply_r_mips_26_rel, | |
236 | [R_MIPS_HI16] = apply_r_mips_hi16_rel, | |
5d3c7925 PB |
237 | [R_MIPS_LO16] = apply_r_mips_lo16_rel, |
238 | [R_MIPS_PC16] = apply_r_mips_pc16_rel, | |
239 | [R_MIPS_PC21_S2] = apply_r_mips_pc21_rel, | |
240 | [R_MIPS_PC26_S2] = apply_r_mips_pc26_rel, | |
4e6a05fe TS |
241 | }; |
242 | ||
4e6a05fe TS |
243 | int apply_relocate(Elf_Shdr *sechdrs, const char *strtab, |
244 | unsigned int symindex, unsigned int relsec, | |
245 | struct module *me) | |
246 | { | |
247 | Elf_Mips_Rel *rel = (void *) sechdrs[relsec].sh_addr; | |
04211a57 | 248 | int (*handler)(struct module *me, u32 *location, Elf_Addr v); |
4e6a05fe TS |
249 | Elf_Sym *sym; |
250 | u32 *location; | |
04211a57 | 251 | unsigned int i, type; |
4e6a05fe TS |
252 | Elf_Addr v; |
253 | int res; | |
254 | ||
255 | pr_debug("Applying relocate section %u to %u\n", relsec, | |
256 | sechdrs[relsec].sh_info); | |
257 | ||
861667dc | 258 | me->arch.r_mips_hi16_list = NULL; |
4e6a05fe TS |
259 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { |
260 | /* This is where to make the change */ | |
261 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr | |
262 | + rel[i].r_offset; | |
263 | /* This is the symbol it is referring to */ | |
264 | sym = (Elf_Sym *)sechdrs[symindex].sh_addr | |
265 | + ELF_MIPS_R_SYM(rel[i]); | |
ba837d38 | 266 | if (sym->st_value >= -MAX_ERRNO) { |
f3bf07b9 AN |
267 | /* Ignore unresolved weak symbol */ |
268 | if (ELF_ST_BIND(sym->st_info) == STB_WEAK) | |
269 | continue; | |
55d791f3 SH |
270 | pr_warn("%s: Unknown symbol %s\n", |
271 | me->name, strtab + sym->st_name); | |
4e6a05fe TS |
272 | return -ENOENT; |
273 | } | |
274 | ||
04211a57 PB |
275 | type = ELF_MIPS_R_TYPE(rel[i]); |
276 | ||
277 | if (type < ARRAY_SIZE(reloc_handlers_rel)) | |
278 | handler = reloc_handlers_rel[type]; | |
279 | else | |
280 | handler = NULL; | |
4e6a05fe | 281 | |
04211a57 PB |
282 | if (!handler) { |
283 | pr_err("%s: Unknown relocation type %u\n", | |
284 | me->name, type); | |
285 | return -EINVAL; | |
286 | } | |
287 | ||
288 | v = sym->st_value; | |
289 | res = handler(me, location, v); | |
4e6a05fe TS |
290 | if (res) |
291 | return res; | |
292 | } | |
293 | ||
c54de490 | 294 | /* |
70342287 | 295 | * Normally the hi16 list should be deallocated at this point. A |
c54de490 RB |
296 | * malformed binary however could contain a series of R_MIPS_HI16 |
297 | * relocations not followed by a R_MIPS_LO16 relocation. In that | |
298 | * case, free up the list and return an error. | |
299 | */ | |
300 | if (me->arch.r_mips_hi16_list) { | |
301 | free_relocation_chain(me->arch.r_mips_hi16_list); | |
302 | me->arch.r_mips_hi16_list = NULL; | |
303 | ||
304 | return -ENOEXEC; | |
305 | } | |
306 | ||
4e6a05fe TS |
307 | return 0; |
308 | } | |
309 | ||
1da177e4 LT |
310 | /* Given an address, look for it in the module exception tables. */ |
311 | const struct exception_table_entry *search_module_dbetables(unsigned long addr) | |
312 | { | |
313 | unsigned long flags; | |
314 | const struct exception_table_entry *e = NULL; | |
315 | struct mod_arch_specific *dbe; | |
316 | ||
317 | spin_lock_irqsave(&dbe_lock, flags); | |
318 | list_for_each_entry(dbe, &dbe_list, dbe_list) { | |
319 | e = search_extable(dbe->dbe_start, dbe->dbe_end - 1, addr); | |
320 | if (e) | |
321 | break; | |
322 | } | |
323 | spin_unlock_irqrestore(&dbe_lock, flags); | |
324 | ||
325 | /* Now, if we found one, we are running inside it now, hence | |
70342287 | 326 | we cannot unload the module, hence no refcnt needed. */ |
1da177e4 LT |
327 | return e; |
328 | } | |
329 | ||
3a4fa0a2 | 330 | /* Put in dbe list if necessary. */ |
1da177e4 LT |
331 | int module_finalize(const Elf_Ehdr *hdr, |
332 | const Elf_Shdr *sechdrs, | |
333 | struct module *me) | |
334 | { | |
335 | const Elf_Shdr *s; | |
336 | char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; | |
337 | ||
94bb0c1a DD |
338 | /* Make jump label nops. */ |
339 | jump_label_apply_nops(me); | |
340 | ||
1da177e4 LT |
341 | INIT_LIST_HEAD(&me->arch.dbe_list); |
342 | for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { | |
343 | if (strcmp("__dbe_table", secstrings + s->sh_name) != 0) | |
344 | continue; | |
345 | me->arch.dbe_start = (void *)s->sh_addr; | |
346 | me->arch.dbe_end = (void *)s->sh_addr + s->sh_size; | |
347 | spin_lock_irq(&dbe_lock); | |
348 | list_add(&me->arch.dbe_list, &dbe_list); | |
349 | spin_unlock_irq(&dbe_lock); | |
350 | } | |
351 | return 0; | |
352 | } | |
353 | ||
354 | void module_arch_cleanup(struct module *mod) | |
355 | { | |
356 | spin_lock_irq(&dbe_lock); | |
357 | list_del(&mod->arch.dbe_list); | |
358 | spin_unlock_irq(&dbe_lock); | |
359 | } |