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