blob: 6b4605893f1e45329ab4058e33c2590056e02468 [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++) {
74 if (strcmp(".ARM.exidx.init.text", secstrings + s->sh_name) == 0)
75 mod->arch.unw_sec_init = s;
76 else if (strcmp(".ARM.exidx.devinit.text", secstrings + s->sh_name) == 0)
77 mod->arch.unw_sec_devinit = s;
78 else if (strcmp(".ARM.exidx", secstrings + s->sh_name) == 0)
79 mod->arch.unw_sec_core = s;
80 else if (strcmp(".init.text", secstrings + s->sh_name) == 0)
81 mod->arch.sec_init_text = s;
82 else if (strcmp(".devinit.text", secstrings + s->sh_name) == 0)
83 mod->arch.sec_devinit_text = s;
84 else if (strcmp(".text", secstrings + s->sh_name) == 0)
85 mod->arch.sec_core_text = s;
86 }
87#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
89}
90
91int
92apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
93 unsigned int relindex, struct module *module)
94{
95 Elf32_Shdr *symsec = sechdrs + symindex;
96 Elf32_Shdr *relsec = sechdrs + relindex;
97 Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
98 Elf32_Rel *rel = (void *)relsec->sh_addr;
99 unsigned int i;
100
101 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
102 unsigned long loc;
103 Elf32_Sym *sym;
104 s32 offset;
Catalin Marinasb7493152010-06-21 15:11:38 +0100105#ifdef CONFIG_THUMB2_KERNEL
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100106 u32 upper, lower, sign, j1, j2;
Catalin Marinasb7493152010-06-21 15:11:38 +0100107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 offset = ELF32_R_SYM(rel->r_info);
110 if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
111 printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
112 module->name, relindex, i);
113 return -ENOEXEC;
114 }
115
116 sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
117
118 if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
119 printk(KERN_ERR "%s: out of bounds relocation, "
120 "section %d reloc %d offset %d size %d\n",
121 module->name, relindex, i, rel->r_offset,
122 dstsec->sh_size);
123 return -ENOEXEC;
124 }
125
126 loc = dstsec->sh_addr + rel->r_offset;
127
128 switch (ELF32_R_TYPE(rel->r_info)) {
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100129 case R_ARM_NONE:
130 /* ignore */
131 break;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 case R_ARM_ABS32:
134 *(u32 *)loc += sym->st_value;
135 break;
136
137 case R_ARM_PC24:
Daniel Jacobowitzc2e26112005-12-14 22:04:22 +0000138 case R_ARM_CALL:
139 case R_ARM_JUMP24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 offset = (*(u32 *)loc & 0x00ffffff) << 2;
141 if (offset & 0x02000000)
142 offset -= 0x04000000;
143
144 offset += sym->st_value - loc;
145 if (offset & 3 ||
Kevin Weltonc5f12502007-05-08 22:05:25 +0100146 offset <= (s32)0xfe000000 ||
147 offset >= (s32)0x02000000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 printk(KERN_ERR
149 "%s: relocation out of range, section "
150 "%d reloc %d sym '%s'\n", module->name,
151 relindex, i, strtab + sym->st_name);
152 return -ENOEXEC;
153 }
154
155 offset >>= 2;
156
157 *(u32 *)loc &= 0xff000000;
158 *(u32 *)loc |= offset & 0x00ffffff;
159 break;
160
Daniel Silverstone4731f8b2009-03-20 11:11:43 +0100161 case R_ARM_V4BX:
162 /* Preserve Rm and the condition code. Alter
163 * other bits to re-code instruction as
164 * MOV PC,Rm.
165 */
166 *(u32 *)loc &= 0xf000000f;
167 *(u32 *)loc |= 0x01a0f000;
168 break;
169
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100170 case R_ARM_PREL31:
171 offset = *(u32 *)loc + sym->st_value - loc;
172 *(u32 *)loc = offset & 0x7fffffff;
173 break;
174
Paul Gortmakerae51e602009-05-07 16:18:40 +0100175 case R_ARM_MOVW_ABS_NC:
176 case R_ARM_MOVT_ABS:
177 offset = *(u32 *)loc;
178 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
179 offset = (offset ^ 0x8000) - 0x8000;
180
181 offset += sym->st_value;
182 if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
183 offset >>= 16;
184
185 *(u32 *)loc &= 0xfff0f000;
186 *(u32 *)loc |= ((offset & 0xf000) << 4) |
187 (offset & 0x0fff);
188 break;
189
Catalin Marinasb7493152010-06-21 15:11:38 +0100190#ifdef CONFIG_THUMB2_KERNEL
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100191 case R_ARM_THM_CALL:
192 case R_ARM_THM_JUMP24:
193 upper = *(u16 *)loc;
194 lower = *(u16 *)(loc + 2);
195
196 /*
197 * 25 bit signed address range (Thumb-2 BL and B.W
198 * instructions):
199 * S:I1:I2:imm10:imm11:0
200 * where:
201 * S = upper[10] = offset[24]
202 * I1 = ~(J1 ^ S) = offset[23]
203 * I2 = ~(J2 ^ S) = offset[22]
204 * imm10 = upper[9:0] = offset[21:12]
205 * imm11 = lower[10:0] = offset[11:1]
206 * J1 = lower[13]
207 * J2 = lower[11]
208 */
209 sign = (upper >> 10) & 1;
210 j1 = (lower >> 13) & 1;
211 j2 = (lower >> 11) & 1;
212 offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
213 ((~(j2 ^ sign) & 1) << 22) |
214 ((upper & 0x03ff) << 12) |
215 ((lower & 0x07ff) << 1);
216 if (offset & 0x01000000)
217 offset -= 0x02000000;
218 offset += sym->st_value - loc;
219
220 /* only Thumb addresses allowed (no interworking) */
221 if (!(offset & 1) ||
222 offset <= (s32)0xff000000 ||
223 offset >= (s32)0x01000000) {
224 printk(KERN_ERR
225 "%s: relocation out of range, section "
226 "%d reloc %d sym '%s'\n", module->name,
227 relindex, i, strtab + sym->st_name);
228 return -ENOEXEC;
229 }
230
231 sign = (offset >> 24) & 1;
232 j1 = sign ^ (~(offset >> 23) & 1);
233 j2 = sign ^ (~(offset >> 22) & 1);
234 *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) |
235 ((offset >> 12) & 0x03ff));
236 *(u16 *)(loc + 2) = (u16)((lower & 0xd000) |
237 (j1 << 13) | (j2 << 11) |
238 ((offset >> 1) & 0x07ff));
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100239 break;
240
Catalin Marinas8dd47742010-06-21 15:10:37 +0100241 case R_ARM_THM_MOVW_ABS_NC:
242 case R_ARM_THM_MOVT_ABS:
243 upper = *(u16 *)loc;
244 lower = *(u16 *)(loc + 2);
245
246 /*
247 * MOVT/MOVW instructions encoding in Thumb-2:
248 *
249 * i = upper[10]
250 * imm4 = upper[3:0]
251 * imm3 = lower[14:12]
252 * imm8 = lower[7:0]
253 *
254 * imm16 = imm4:i:imm3:imm8
255 */
256 offset = ((upper & 0x000f) << 12) |
257 ((upper & 0x0400) << 1) |
258 ((lower & 0x7000) >> 4) | (lower & 0x00ff);
259 offset = (offset ^ 0x8000) - 0x8000;
260 offset += sym->st_value;
261
262 if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
263 offset >>= 16;
264
265 *(u16 *)loc = (u16)((upper & 0xfbf0) |
266 ((offset & 0xf000) >> 12) |
267 ((offset & 0x0800) >> 1));
268 *(u16 *)(loc + 2) = (u16)((lower & 0x8f00) |
269 ((offset & 0x0700) << 4) |
270 (offset & 0x00ff));
271 break;
Catalin Marinasb7493152010-06-21 15:11:38 +0100272#endif
Catalin Marinas8dd47742010-06-21 15:10:37 +0100273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 default:
275 printk(KERN_ERR "%s: unknown relocation: %u\n",
276 module->name, ELF32_R_TYPE(rel->r_info));
277 return -ENOEXEC;
278 }
279 }
280 return 0;
281}
282
283int
284apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
285 unsigned int symindex, unsigned int relsec, struct module *module)
286{
287 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
288 module->name);
289 return -ENOEXEC;
290}
291
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100292#ifdef CONFIG_ARM_UNWIND
293static void register_unwind_tables(struct module *mod)
294{
295 if (mod->arch.unw_sec_init && mod->arch.sec_init_text)
296 mod->arch.unwind_init =
297 unwind_table_add(mod->arch.unw_sec_init->sh_addr,
298 mod->arch.unw_sec_init->sh_size,
299 mod->arch.sec_init_text->sh_addr,
300 mod->arch.sec_init_text->sh_size);
301 if (mod->arch.unw_sec_devinit && mod->arch.sec_devinit_text)
302 mod->arch.unwind_devinit =
303 unwind_table_add(mod->arch.unw_sec_devinit->sh_addr,
304 mod->arch.unw_sec_devinit->sh_size,
305 mod->arch.sec_devinit_text->sh_addr,
306 mod->arch.sec_devinit_text->sh_size);
307 if (mod->arch.unw_sec_core && mod->arch.sec_core_text)
308 mod->arch.unwind_core =
309 unwind_table_add(mod->arch.unw_sec_core->sh_addr,
310 mod->arch.unw_sec_core->sh_size,
311 mod->arch.sec_core_text->sh_addr,
312 mod->arch.sec_core_text->sh_size);
313}
314
315static void unregister_unwind_tables(struct module *mod)
316{
317 unwind_table_del(mod->arch.unwind_init);
318 unwind_table_del(mod->arch.unwind_devinit);
319 unwind_table_del(mod->arch.unwind_core);
320}
321#else
322static inline void register_unwind_tables(struct module *mod) { }
323static inline void unregister_unwind_tables(struct module *mod) { }
324#endif
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326int
327module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
328 struct module *module)
329{
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100330 register_unwind_tables(module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
332}
333
334void
335module_arch_cleanup(struct module *mod)
336{
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100337 unregister_unwind_tables(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}