sibyte: add 3.3 support
[openwrt/svn-archive/archive.git] / target / linux / sibyte / patches-3.3 / 106-no_module_reloc.patch
1 --- a/arch/mips/Makefile
2 +++ b/arch/mips/Makefile
3 @@ -90,8 +90,8 @@ all-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlin
4 cflags-y += -G 0 -mno-abicalls -fno-pic -pipe
5 cflags-y += -msoft-float
6 LDFLAGS_vmlinux += -G 0 -static -n -nostdlib
7 -KBUILD_AFLAGS_MODULE += -mno-long-calls
8 -KBUILD_CFLAGS_MODULE += -mno-long-calls
9 +KBUILD_AFLAGS_MODULE += -mlong-calls
10 +KBUILD_CFLAGS_MODULE += -mlong-calls
11
12 cflags-y += -ffreestanding
13
14 --- a/arch/mips/include/asm/module.h
15 +++ b/arch/mips/include/asm/module.h
16 @@ -9,11 +9,6 @@ struct mod_arch_specific {
17 struct list_head dbe_list;
18 const struct exception_table_entry *dbe_start;
19 const struct exception_table_entry *dbe_end;
20 -
21 - void *phys_plt_tbl;
22 - void *virt_plt_tbl;
23 - unsigned int phys_plt_offset;
24 - unsigned int virt_plt_offset;
25 };
26
27 typedef uint8_t Elf64_Byte; /* Type for a 8-bit quantity. */
28 --- a/arch/mips/kernel/module.c
29 +++ b/arch/mips/kernel/module.c
30 @@ -44,117 +44,6 @@ static struct mips_hi16 *mips_hi16_list;
31 static LIST_HEAD(dbe_list);
32 static DEFINE_SPINLOCK(dbe_lock);
33
34 -/*
35 - * Get the potential max trampolines size required of the init and
36 - * non-init sections. Only used if we cannot find enough contiguous
37 - * physically mapped memory to put the module into.
38 - */
39 -static unsigned int
40 -get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
41 - const char *secstrings, unsigned int symindex, bool is_init)
42 -{
43 - unsigned long ret = 0;
44 - unsigned int i, j;
45 - Elf_Sym *syms;
46 -
47 - /* Everything marked ALLOC (this includes the exported symbols) */
48 - for (i = 1; i < hdr->e_shnum; ++i) {
49 - unsigned int info = sechdrs[i].sh_info;
50 -
51 - if (sechdrs[i].sh_type != SHT_REL
52 - && sechdrs[i].sh_type != SHT_RELA)
53 - continue;
54 -
55 - /* Not a valid relocation section? */
56 - if (info >= hdr->e_shnum)
57 - continue;
58 -
59 - /* Don't bother with non-allocated sections */
60 - if (!(sechdrs[info].sh_flags & SHF_ALLOC))
61 - continue;
62 -
63 - /* If it's called *.init*, and we're not init, we're
64 - not interested */
65 - if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
66 - != is_init)
67 - continue;
68 -
69 - syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
70 - if (sechdrs[i].sh_type == SHT_REL) {
71 - Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
72 - unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
73 -
74 - for (j = 0; j < size; ++j) {
75 - Elf_Sym *sym;
76 -
77 - if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
78 - continue;
79 -
80 - sym = syms + ELF_MIPS_R_SYM(rel[j]);
81 - if (!is_init && sym->st_shndx != SHN_UNDEF)
82 - continue;
83 -
84 - ret += 4 * sizeof(int);
85 - }
86 - } else {
87 - Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
88 - unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
89 -
90 - for (j = 0; j < size; ++j) {
91 - Elf_Sym *sym;
92 -
93 - if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
94 - continue;
95 -
96 - sym = syms + ELF_MIPS_R_SYM(rela[j]);
97 - if (!is_init && sym->st_shndx != SHN_UNDEF)
98 - continue;
99 -
100 - ret += 4 * sizeof(int);
101 - }
102 - }
103 - }
104 -
105 - return ret;
106 -}
107 -
108 -#ifndef MODULE_START
109 -static void *alloc_phys(unsigned long size)
110 -{
111 - unsigned order;
112 - struct page *page;
113 - struct page *p;
114 -
115 - size = PAGE_ALIGN(size);
116 - order = get_order(size);
117 -
118 - page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
119 - __GFP_THISNODE, order);
120 - if (!page)
121 - return NULL;
122 -
123 - split_page(page, order);
124 -
125 - for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
126 - __free_page(p);
127 -
128 - return page_address(page);
129 -}
130 -#endif
131 -
132 -static void free_phys(void *ptr, unsigned long size)
133 -{
134 - struct page *page;
135 - struct page *end;
136 -
137 - page = virt_to_page(ptr);
138 - end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
139 -
140 - for (; page < end; ++page)
141 - __free_page(page);
142 -}
143 -
144 -
145 void *module_alloc(unsigned long size)
146 {
147 #ifdef MODULE_START
148 @@ -162,99 +51,21 @@ void *module_alloc(unsigned long size)
149 GFP_KERNEL, PAGE_KERNEL, -1,
150 __builtin_return_address(0));
151 #else
152 - void *ptr;
153 -
154 if (size == 0)
155 return NULL;
156 -
157 - ptr = alloc_phys(size);
158 -
159 - /* If we failed to allocate physically contiguous memory,
160 - * fall back to regular vmalloc. The module loader code will
161 - * create jump tables to handle long jumps */
162 - if (!ptr)
163 - return vmalloc(size);
164 -
165 - return ptr;
166 -#endif
167 -}
168 -
169 -static inline bool is_phys_addr(void *ptr)
170 -{
171 -#ifdef CONFIG_64BIT
172 - return (KSEGX((unsigned long)ptr) == CKSEG0);
173 -#else
174 - return (KSEGX(ptr) == KSEG0);
175 + return vmalloc(size);
176 #endif
177 }
178
179 /* Free memory returned from module_alloc */
180 void module_free(struct module *mod, void *module_region)
181 {
182 - if (is_phys_addr(module_region)) {
183 - if (mod->module_init == module_region)
184 - free_phys(module_region, mod->init_size);
185 - else if (mod->module_core == module_region)
186 - free_phys(module_region, mod->core_size);
187 - else
188 - BUG();
189 - } else {
190 - vfree(module_region);
191 - }
192 -}
193 -
194 -static void *__module_alloc(int size, bool phys)
195 -{
196 - void *ptr;
197 -
198 - if (phys)
199 - ptr = kmalloc(size, GFP_KERNEL);
200 - else
201 - ptr = vmalloc(size);
202 - return ptr;
203 -}
204 -
205 -static void __module_free(void *ptr)
206 -{
207 - if (is_phys_addr(ptr))
208 - kfree(ptr);
209 - else
210 - vfree(ptr);
211 + vfree(module_region);
212 }
213
214 int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
215 char *secstrings, struct module *mod)
216 {
217 - unsigned int symindex = 0;
218 - unsigned int core_size, init_size;
219 - int i;
220 -
221 - for (i = 1; i < hdr->e_shnum; i++)
222 - if (sechdrs[i].sh_type == SHT_SYMTAB)
223 - symindex = i;
224 -
225 - core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
226 - init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
227 -
228 - mod->arch.phys_plt_offset = 0;
229 - mod->arch.virt_plt_offset = 0;
230 - mod->arch.phys_plt_tbl = NULL;
231 - mod->arch.virt_plt_tbl = NULL;
232 -
233 - if ((core_size + init_size) == 0)
234 - return 0;
235 -
236 - mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
237 - if (!mod->arch.phys_plt_tbl)
238 - return -ENOMEM;
239 -
240 - mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
241 - if (!mod->arch.virt_plt_tbl) {
242 - __module_free(mod->arch.phys_plt_tbl);
243 - mod->arch.phys_plt_tbl = NULL;
244 - return -ENOMEM;
245 - }
246 -
247 return 0;
248 }
249
250 @@ -277,36 +88,28 @@ static int apply_r_mips_32_rela(struct m
251 return 0;
252 }
253
254 -static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
255 - void *start, Elf_Addr v)
256 +static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
257 {
258 - unsigned *tramp = start + *plt_offset;
259 - *plt_offset += 4 * sizeof(int);
260 -
261 - /* adjust carry for addiu */
262 - if (v & 0x00008000)
263 - v += 0x10000;
264 -
265 - tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
266 - tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
267 - tramp[2] = 0x03200008; /* jr t9 */
268 - tramp[3] = 0x00000000; /* nop */
269 + if (v % 4) {
270 + pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
271 + me->name);
272 + return -ENOEXEC;
273 + }
274
275 - return (Elf_Addr) tramp;
276 -}
277 + if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
278 + printk(KERN_ERR
279 + "module %s: relocation overflow\n",
280 + me->name);
281 + return -ENOEXEC;
282 + }
283
284 -static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
285 -{
286 - if (is_phys_addr(location))
287 - return add_plt_entry_to(&me->arch.phys_plt_offset,
288 - me->arch.phys_plt_tbl, v);
289 - else
290 - return add_plt_entry_to(&me->arch.virt_plt_offset,
291 - me->arch.virt_plt_tbl, v);
292 + *location = (*location & ~0x03ffffff) |
293 + ((*location + (v >> 2)) & 0x03ffffff);
294
295 + return 0;
296 }
297
298 -static int set_r_mips_26(struct module *me, u32 *location, u32 ofs, Elf_Addr v)
299 +static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
300 {
301 if (v % 4) {
302 pr_err("module %s: dangerous R_MIPS_26 RELArelocation\n",
303 @@ -315,31 +118,17 @@ static int set_r_mips_26(struct module *
304 }
305
306 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
307 - v = add_plt_entry(me, location, v + (ofs << 2));
308 - if (!v) {
309 - printk(KERN_ERR
310 + printk(KERN_ERR
311 "module %s: relocation overflow\n",
312 me->name);
313 - return -ENOEXEC;
314 - }
315 - ofs = 0;
316 + return -ENOEXEC;
317 }
318
319 - *location = (*location & ~0x03ffffff) | ((ofs + (v >> 2)) & 0x03ffffff);
320 + *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
321
322 return 0;
323 }
324
325 -static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
326 -{
327 - return set_r_mips_26(me, location, *location & 0x03ffffff, v);
328 -}
329 -
330 -static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
331 -{
332 - return set_r_mips_26(me, location, 0, v);
333 -}
334 -
335 static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
336 {
337 struct mips_hi16 *n;
338 @@ -607,32 +396,11 @@ int module_finalize(const Elf_Ehdr *hdr,
339 list_add(&me->arch.dbe_list, &dbe_list);
340 spin_unlock_irq(&dbe_lock);
341 }
342 -
343 - /* Get rid of the fixup trampoline if we're running the module
344 - * from physically mapped address space */
345 - if (me->arch.phys_plt_offset == 0) {
346 - __module_free(me->arch.phys_plt_tbl);
347 - me->arch.phys_plt_tbl = NULL;
348 - }
349 - if (me->arch.virt_plt_offset == 0) {
350 - __module_free(me->arch.virt_plt_tbl);
351 - me->arch.virt_plt_tbl = NULL;
352 - }
353 -
354 return 0;
355 }
356
357 void module_arch_cleanup(struct module *mod)
358 {
359 - if (mod->arch.phys_plt_tbl) {
360 - __module_free(mod->arch.phys_plt_tbl);
361 - mod->arch.phys_plt_tbl = NULL;
362 - }
363 - if (mod->arch.virt_plt_tbl) {
364 - __module_free(mod->arch.virt_plt_tbl);
365 - mod->arch.virt_plt_tbl = NULL;
366 - }
367 -
368 spin_lock_irq(&dbe_lock);
369 list_del(&mod->arch.dbe_list);
370 spin_unlock_irq(&dbe_lock);