blob: 1dae0468677a2f7e6ec85331fb38e232c5eac7ee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/kernel/module.c
3 *
4 * Copyright (C) 2002 Russell King.
Hyok S. Choi6a570b22006-09-26 17:37:07 +09005 * Modified for nommu by Hyok S. Choi
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Module allocation method suggested by Andi Kleen.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
Russell Kingf339ab32005-10-28 14:29:43 +010014#include <linux/moduleloader.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070016#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/elf.h>
18#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/fs.h>
20#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/pgtable.h>
Russell King37efe642008-12-01 11:53:07 +000024#include <asm/sections.h>
Catalin Marinas2e1926e2009-02-11 13:09:54 +010025#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#ifdef CONFIG_XIP_KERNEL
28/*
29 * The XIP kernel text is mapped in the module area for modules and
30 * some other stuff to work without any indirect relocations.
Russell Kingab4f2ee2008-11-06 17:11:07 +000031 * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
33 */
Russell Kingab4f2ee2008-11-06 17:11:07 +000034#undef MODULES_VADDR
Russell King37efe642008-12-01 11:53:07 +000035#define MODULES_VADDR (((unsigned long)_etext + ~PGDIR_MASK) & PGDIR_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#endif
37
Hyok S. Choi6a570b22006-09-26 17:37:07 +090038#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070039void *module_alloc(unsigned long size)
40{
41 struct vm_struct *area;
42
43 size = PAGE_ALIGN(size);
44 if (!size)
45 return NULL;
46
Russell Kingab4f2ee2008-11-06 17:11:07 +000047 area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (!area)
49 return NULL;
50
Russell King8ec53662008-09-07 17:16:54 +010051 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Hyok S. Choi6a570b22006-09-26 17:37:07 +090053#else /* CONFIG_MMU */
54void *module_alloc(unsigned long size)
55{
56 return size == 0 ? NULL : vmalloc(size);
57}
58#endif /* !CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60void module_free(struct module *module, void *region)
61{
62 vfree(region);
63}
64
65int module_frob_arch_sections(Elf_Ehdr *hdr,
66 Elf_Shdr *sechdrs,
67 char *secstrings,
68 struct module *mod)
69{
Catalin Marinas2e1926e2009-02-11 13:09:54 +010070#ifdef CONFIG_ARM_UNWIND
71 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
72
73 for (s = sechdrs; s < sechdrs_end; s++) {
Phil Carmody57934322010-08-19 15:10:24 +010074 char const *secname = secstrings + s->sh_name;
75
76 if (strcmp(".ARM.exidx.init.text", secname) == 0)
Catalin Marinas2e1926e2009-02-11 13:09:54 +010077 mod->arch.unw_sec_init = s;
Phil Carmody57934322010-08-19 15:10:24 +010078 else if (strcmp(".ARM.exidx.devinit.text", secname) == 0)
Catalin Marinas2e1926e2009-02-11 13:09:54 +010079 mod->arch.unw_sec_devinit = s;
Phil Carmody57934322010-08-19 15:10:24 +010080 else if (strcmp(".ARM.exidx", secname) == 0)
Catalin Marinas2e1926e2009-02-11 13:09:54 +010081 mod->arch.unw_sec_core = s;
Phil Carmody57934322010-08-19 15:10:24 +010082 else if (strcmp(".init.text", secname) == 0)
Catalin Marinas2e1926e2009-02-11 13:09:54 +010083 mod->arch.sec_init_text = s;
Phil Carmody57934322010-08-19 15:10:24 +010084 else if (strcmp(".devinit.text", secname) == 0)
Catalin Marinas2e1926e2009-02-11 13:09:54 +010085 mod->arch.sec_devinit_text = s;
Phil Carmody57934322010-08-19 15:10:24 +010086 else if (strcmp(".text", secname) == 0)
Catalin Marinas2e1926e2009-02-11 13:09:54 +010087 mod->arch.sec_core_text = s;
88 }
89#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91}
92
93int
94apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
95 unsigned int relindex, struct module *module)
96{
97 Elf32_Shdr *symsec = sechdrs + symindex;
98 Elf32_Shdr *relsec = sechdrs + relindex;
99 Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
100 Elf32_Rel *rel = (void *)relsec->sh_addr;
101 unsigned int i;
102
103 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
104 unsigned long loc;
105 Elf32_Sym *sym;
106 s32 offset;
Catalin Marinasb7493152010-06-21 15:11:38 +0100107#ifdef CONFIG_THUMB2_KERNEL
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100108 u32 upper, lower, sign, j1, j2;
Catalin Marinasb7493152010-06-21 15:11:38 +0100109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 offset = ELF32_R_SYM(rel->r_info);
112 if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
113 printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
114 module->name, relindex, i);
115 return -ENOEXEC;
116 }
117
118 sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
119
120 if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
121 printk(KERN_ERR "%s: out of bounds relocation, "
122 "section %d reloc %d offset %d size %d\n",
123 module->name, relindex, i, rel->r_offset,
124 dstsec->sh_size);
125 return -ENOEXEC;
126 }
127
128 loc = dstsec->sh_addr + rel->r_offset;
129
130 switch (ELF32_R_TYPE(rel->r_info)) {
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100131 case R_ARM_NONE:
132 /* ignore */
133 break;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 case R_ARM_ABS32:
136 *(u32 *)loc += sym->st_value;
137 break;
138
139 case R_ARM_PC24:
Daniel Jacobowitzc2e26112005-12-14 22:04:22 +0000140 case R_ARM_CALL:
141 case R_ARM_JUMP24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 offset = (*(u32 *)loc & 0x00ffffff) << 2;
143 if (offset & 0x02000000)
144 offset -= 0x04000000;
145
146 offset += sym->st_value - loc;
147 if (offset & 3 ||
Kevin Weltonc5f12502007-05-08 22:05:25 +0100148 offset <= (s32)0xfe000000 ||
149 offset >= (s32)0x02000000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 printk(KERN_ERR
151 "%s: relocation out of range, section "
152 "%d reloc %d sym '%s'\n", module->name,
153 relindex, i, strtab + sym->st_name);
154 return -ENOEXEC;
155 }
156
157 offset >>= 2;
158
159 *(u32 *)loc &= 0xff000000;
160 *(u32 *)loc |= offset & 0x00ffffff;
161 break;
162
Daniel Silverstone4731f8b2009-03-20 11:11:43 +0100163 case R_ARM_V4BX:
164 /* Preserve Rm and the condition code. Alter
165 * other bits to re-code instruction as
166 * MOV PC,Rm.
167 */
168 *(u32 *)loc &= 0xf000000f;
169 *(u32 *)loc |= 0x01a0f000;
170 break;
171
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100172 case R_ARM_PREL31:
173 offset = *(u32 *)loc + sym->st_value - loc;
174 *(u32 *)loc = offset & 0x7fffffff;
175 break;
176
Paul Gortmakerae51e602009-05-07 16:18:40 +0100177 case R_ARM_MOVW_ABS_NC:
178 case R_ARM_MOVT_ABS:
179 offset = *(u32 *)loc;
180 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
181 offset = (offset ^ 0x8000) - 0x8000;
182
183 offset += sym->st_value;
184 if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
185 offset >>= 16;
186
187 *(u32 *)loc &= 0xfff0f000;
188 *(u32 *)loc |= ((offset & 0xf000) << 4) |
189 (offset & 0x0fff);
190 break;
191
Catalin Marinasb7493152010-06-21 15:11:38 +0100192#ifdef CONFIG_THUMB2_KERNEL
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100193 case R_ARM_THM_CALL:
194 case R_ARM_THM_JUMP24:
195 upper = *(u16 *)loc;
196 lower = *(u16 *)(loc + 2);
197
198 /*
199 * 25 bit signed address range (Thumb-2 BL and B.W
200 * instructions):
201 * S:I1:I2:imm10:imm11:0
202 * where:
203 * S = upper[10] = offset[24]
204 * I1 = ~(J1 ^ S) = offset[23]
205 * I2 = ~(J2 ^ S) = offset[22]
206 * imm10 = upper[9:0] = offset[21:12]
207 * imm11 = lower[10:0] = offset[11:1]
208 * J1 = lower[13]
209 * J2 = lower[11]
210 */
211 sign = (upper >> 10) & 1;
212 j1 = (lower >> 13) & 1;
213 j2 = (lower >> 11) & 1;
214 offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
215 ((~(j2 ^ sign) & 1) << 22) |
216 ((upper & 0x03ff) << 12) |
217 ((lower & 0x07ff) << 1);
218 if (offset & 0x01000000)
219 offset -= 0x02000000;
220 offset += sym->st_value - loc;
221
222 /* only Thumb addresses allowed (no interworking) */
223 if (!(offset & 1) ||
224 offset <= (s32)0xff000000 ||
225 offset >= (s32)0x01000000) {
226 printk(KERN_ERR
227 "%s: relocation out of range, section "
228 "%d reloc %d sym '%s'\n", module->name,
229 relindex, i, strtab + sym->st_name);
230 return -ENOEXEC;
231 }
232
233 sign = (offset >> 24) & 1;
234 j1 = sign ^ (~(offset >> 23) & 1);
235 j2 = sign ^ (~(offset >> 22) & 1);
236 *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) |
237 ((offset >> 12) & 0x03ff));
238 *(u16 *)(loc + 2) = (u16)((lower & 0xd000) |
239 (j1 << 13) | (j2 << 11) |
240 ((offset >> 1) & 0x07ff));
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100241 break;
242
Catalin Marinas8dd47742010-06-21 15:10:37 +0100243 case R_ARM_THM_MOVW_ABS_NC:
244 case R_ARM_THM_MOVT_ABS:
245 upper = *(u16 *)loc;
246 lower = *(u16 *)(loc + 2);
247
248 /*
249 * MOVT/MOVW instructions encoding in Thumb-2:
250 *
251 * i = upper[10]
252 * imm4 = upper[3:0]
253 * imm3 = lower[14:12]
254 * imm8 = lower[7:0]
255 *
256 * imm16 = imm4:i:imm3:imm8
257 */
258 offset = ((upper & 0x000f) << 12) |
259 ((upper & 0x0400) << 1) |
260 ((lower & 0x7000) >> 4) | (lower & 0x00ff);
261 offset = (offset ^ 0x8000) - 0x8000;
262 offset += sym->st_value;
263
264 if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
265 offset >>= 16;
266
267 *(u16 *)loc = (u16)((upper & 0xfbf0) |
268 ((offset & 0xf000) >> 12) |
269 ((offset & 0x0800) >> 1));
270 *(u16 *)(loc + 2) = (u16)((lower & 0x8f00) |
271 ((offset & 0x0700) << 4) |
272 (offset & 0x00ff));
273 break;
Catalin Marinasb7493152010-06-21 15:11:38 +0100274#endif
Catalin Marinas8dd47742010-06-21 15:10:37 +0100275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 default:
277 printk(KERN_ERR "%s: unknown relocation: %u\n",
278 module->name, ELF32_R_TYPE(rel->r_info));
279 return -ENOEXEC;
280 }
281 }
282 return 0;
283}
284
285int
286apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
287 unsigned int symindex, unsigned int relsec, struct module *module)
288{
289 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
290 module->name);
291 return -ENOEXEC;
292}
293
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100294#ifdef CONFIG_ARM_UNWIND
295static void register_unwind_tables(struct module *mod)
296{
297 if (mod->arch.unw_sec_init && mod->arch.sec_init_text)
298 mod->arch.unwind_init =
299 unwind_table_add(mod->arch.unw_sec_init->sh_addr,
300 mod->arch.unw_sec_init->sh_size,
301 mod->arch.sec_init_text->sh_addr,
302 mod->arch.sec_init_text->sh_size);
303 if (mod->arch.unw_sec_devinit && mod->arch.sec_devinit_text)
304 mod->arch.unwind_devinit =
305 unwind_table_add(mod->arch.unw_sec_devinit->sh_addr,
306 mod->arch.unw_sec_devinit->sh_size,
307 mod->arch.sec_devinit_text->sh_addr,
308 mod->arch.sec_devinit_text->sh_size);
309 if (mod->arch.unw_sec_core && mod->arch.sec_core_text)
310 mod->arch.unwind_core =
311 unwind_table_add(mod->arch.unw_sec_core->sh_addr,
312 mod->arch.unw_sec_core->sh_size,
313 mod->arch.sec_core_text->sh_addr,
314 mod->arch.sec_core_text->sh_size);
315}
316
317static void unregister_unwind_tables(struct module *mod)
318{
319 unwind_table_del(mod->arch.unwind_init);
320 unwind_table_del(mod->arch.unwind_devinit);
321 unwind_table_del(mod->arch.unwind_core);
322}
323#else
324static inline void register_unwind_tables(struct module *mod) { }
325static inline void unregister_unwind_tables(struct module *mod) { }
326#endif
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328int
329module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
330 struct module *module)
331{
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100332 register_unwind_tables(module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334}
335
336void
337module_arch_cleanup(struct module *mod)
338{
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100339 unregister_unwind_tables(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}