blob: d9bd786ce23dc1228ac7937ad442292f08c74cca [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;
Phil Carmodye5f77722010-08-19 15:16:37 +010072 struct arm_unwind_mapping *maps = mod->arch.map;
Catalin Marinas2e1926e2009-02-11 13:09:54 +010073
74 for (s = sechdrs; s < sechdrs_end; s++) {
Phil Carmody57934322010-08-19 15:10:24 +010075 char const *secname = secstrings + s->sh_name;
76
77 if (strcmp(".ARM.exidx.init.text", secname) == 0)
Phil Carmodye5f77722010-08-19 15:16:37 +010078 maps[ARM_SEC_INIT].unw_sec = s;
Phil Carmody57934322010-08-19 15:10:24 +010079 else if (strcmp(".ARM.exidx.devinit.text", secname) == 0)
Phil Carmodye5f77722010-08-19 15:16:37 +010080 maps[ARM_SEC_DEVINIT].unw_sec = s;
Phil Carmody57934322010-08-19 15:10:24 +010081 else if (strcmp(".ARM.exidx", secname) == 0)
Phil Carmodye5f77722010-08-19 15:16:37 +010082 maps[ARM_SEC_CORE].unw_sec = s;
Phil Carmody09e56a22010-08-19 15:19:04 +010083 else if (strcmp(".ARM.exidx.exit.text", secname) == 0)
84 maps[ARM_SEC_EXIT].unw_sec = s;
85 else if (strcmp(".ARM.exidx.devexit.text", secname) == 0)
86 maps[ARM_SEC_DEVEXIT].unw_sec = s;
Phil Carmody57934322010-08-19 15:10:24 +010087 else if (strcmp(".init.text", secname) == 0)
Phil Carmodye5f77722010-08-19 15:16:37 +010088 maps[ARM_SEC_INIT].sec_text = s;
Phil Carmody57934322010-08-19 15:10:24 +010089 else if (strcmp(".devinit.text", secname) == 0)
Phil Carmodye5f77722010-08-19 15:16:37 +010090 maps[ARM_SEC_DEVINIT].sec_text = s;
Phil Carmody57934322010-08-19 15:10:24 +010091 else if (strcmp(".text", secname) == 0)
Phil Carmodye5f77722010-08-19 15:16:37 +010092 maps[ARM_SEC_CORE].sec_text = s;
Phil Carmody09e56a22010-08-19 15:19:04 +010093 else if (strcmp(".exit.text", secname) == 0)
94 maps[ARM_SEC_EXIT].sec_text = s;
95 else if (strcmp(".devexit.text", secname) == 0)
96 maps[ARM_SEC_DEVEXIT].sec_text = s;
Catalin Marinas2e1926e2009-02-11 13:09:54 +010097 }
98#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100}
101
102int
103apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
104 unsigned int relindex, struct module *module)
105{
106 Elf32_Shdr *symsec = sechdrs + symindex;
107 Elf32_Shdr *relsec = sechdrs + relindex;
108 Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
109 Elf32_Rel *rel = (void *)relsec->sh_addr;
110 unsigned int i;
111
112 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
113 unsigned long loc;
114 Elf32_Sym *sym;
115 s32 offset;
Catalin Marinasb7493152010-06-21 15:11:38 +0100116#ifdef CONFIG_THUMB2_KERNEL
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100117 u32 upper, lower, sign, j1, j2;
Catalin Marinasb7493152010-06-21 15:11:38 +0100118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 offset = ELF32_R_SYM(rel->r_info);
121 if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
122 printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
123 module->name, relindex, i);
124 return -ENOEXEC;
125 }
126
127 sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
128
129 if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
130 printk(KERN_ERR "%s: out of bounds relocation, "
131 "section %d reloc %d offset %d size %d\n",
132 module->name, relindex, i, rel->r_offset,
133 dstsec->sh_size);
134 return -ENOEXEC;
135 }
136
137 loc = dstsec->sh_addr + rel->r_offset;
138
139 switch (ELF32_R_TYPE(rel->r_info)) {
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100140 case R_ARM_NONE:
141 /* ignore */
142 break;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 case R_ARM_ABS32:
145 *(u32 *)loc += sym->st_value;
146 break;
147
148 case R_ARM_PC24:
Daniel Jacobowitzc2e26112005-12-14 22:04:22 +0000149 case R_ARM_CALL:
150 case R_ARM_JUMP24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 offset = (*(u32 *)loc & 0x00ffffff) << 2;
152 if (offset & 0x02000000)
153 offset -= 0x04000000;
154
155 offset += sym->st_value - loc;
156 if (offset & 3 ||
Kevin Weltonc5f12502007-05-08 22:05:25 +0100157 offset <= (s32)0xfe000000 ||
158 offset >= (s32)0x02000000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 printk(KERN_ERR
160 "%s: relocation out of range, section "
161 "%d reloc %d sym '%s'\n", module->name,
162 relindex, i, strtab + sym->st_name);
163 return -ENOEXEC;
164 }
165
166 offset >>= 2;
167
168 *(u32 *)loc &= 0xff000000;
169 *(u32 *)loc |= offset & 0x00ffffff;
170 break;
171
Daniel Silverstone4731f8b2009-03-20 11:11:43 +0100172 case R_ARM_V4BX:
173 /* Preserve Rm and the condition code. Alter
174 * other bits to re-code instruction as
175 * MOV PC,Rm.
176 */
177 *(u32 *)loc &= 0xf000000f;
178 *(u32 *)loc |= 0x01a0f000;
179 break;
180
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100181 case R_ARM_PREL31:
182 offset = *(u32 *)loc + sym->st_value - loc;
183 *(u32 *)loc = offset & 0x7fffffff;
184 break;
185
Paul Gortmakerae51e6092009-05-07 16:18:40 +0100186 case R_ARM_MOVW_ABS_NC:
187 case R_ARM_MOVT_ABS:
188 offset = *(u32 *)loc;
189 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
190 offset = (offset ^ 0x8000) - 0x8000;
191
192 offset += sym->st_value;
193 if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
194 offset >>= 16;
195
196 *(u32 *)loc &= 0xfff0f000;
197 *(u32 *)loc |= ((offset & 0xf000) << 4) |
198 (offset & 0x0fff);
199 break;
200
Catalin Marinasb7493152010-06-21 15:11:38 +0100201#ifdef CONFIG_THUMB2_KERNEL
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100202 case R_ARM_THM_CALL:
203 case R_ARM_THM_JUMP24:
204 upper = *(u16 *)loc;
205 lower = *(u16 *)(loc + 2);
206
207 /*
208 * 25 bit signed address range (Thumb-2 BL and B.W
209 * instructions):
210 * S:I1:I2:imm10:imm11:0
211 * where:
212 * S = upper[10] = offset[24]
213 * I1 = ~(J1 ^ S) = offset[23]
214 * I2 = ~(J2 ^ S) = offset[22]
215 * imm10 = upper[9:0] = offset[21:12]
216 * imm11 = lower[10:0] = offset[11:1]
217 * J1 = lower[13]
218 * J2 = lower[11]
219 */
220 sign = (upper >> 10) & 1;
221 j1 = (lower >> 13) & 1;
222 j2 = (lower >> 11) & 1;
223 offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
224 ((~(j2 ^ sign) & 1) << 22) |
225 ((upper & 0x03ff) << 12) |
226 ((lower & 0x07ff) << 1);
227 if (offset & 0x01000000)
228 offset -= 0x02000000;
229 offset += sym->st_value - loc;
230
231 /* only Thumb addresses allowed (no interworking) */
232 if (!(offset & 1) ||
233 offset <= (s32)0xff000000 ||
234 offset >= (s32)0x01000000) {
235 printk(KERN_ERR
236 "%s: relocation out of range, section "
237 "%d reloc %d sym '%s'\n", module->name,
238 relindex, i, strtab + sym->st_name);
239 return -ENOEXEC;
240 }
241
242 sign = (offset >> 24) & 1;
243 j1 = sign ^ (~(offset >> 23) & 1);
244 j2 = sign ^ (~(offset >> 22) & 1);
245 *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) |
246 ((offset >> 12) & 0x03ff));
247 *(u16 *)(loc + 2) = (u16)((lower & 0xd000) |
248 (j1 << 13) | (j2 << 11) |
249 ((offset >> 1) & 0x07ff));
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100250 break;
251
Catalin Marinas8dd47742010-06-21 15:10:37 +0100252 case R_ARM_THM_MOVW_ABS_NC:
253 case R_ARM_THM_MOVT_ABS:
254 upper = *(u16 *)loc;
255 lower = *(u16 *)(loc + 2);
256
257 /*
258 * MOVT/MOVW instructions encoding in Thumb-2:
259 *
260 * i = upper[10]
261 * imm4 = upper[3:0]
262 * imm3 = lower[14:12]
263 * imm8 = lower[7:0]
264 *
265 * imm16 = imm4:i:imm3:imm8
266 */
267 offset = ((upper & 0x000f) << 12) |
268 ((upper & 0x0400) << 1) |
269 ((lower & 0x7000) >> 4) | (lower & 0x00ff);
270 offset = (offset ^ 0x8000) - 0x8000;
271 offset += sym->st_value;
272
273 if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
274 offset >>= 16;
275
276 *(u16 *)loc = (u16)((upper & 0xfbf0) |
277 ((offset & 0xf000) >> 12) |
278 ((offset & 0x0800) >> 1));
279 *(u16 *)(loc + 2) = (u16)((lower & 0x8f00) |
280 ((offset & 0x0700) << 4) |
281 (offset & 0x00ff));
282 break;
Catalin Marinasb7493152010-06-21 15:11:38 +0100283#endif
Catalin Marinas8dd47742010-06-21 15:10:37 +0100284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 default:
286 printk(KERN_ERR "%s: unknown relocation: %u\n",
287 module->name, ELF32_R_TYPE(rel->r_info));
288 return -ENOEXEC;
289 }
290 }
291 return 0;
292}
293
294int
295apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
296 unsigned int symindex, unsigned int relsec, struct module *module)
297{
298 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
299 module->name);
300 return -ENOEXEC;
301}
302
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100303#ifdef CONFIG_ARM_UNWIND
304static void register_unwind_tables(struct module *mod)
305{
Phil Carmodye5f77722010-08-19 15:16:37 +0100306 int i;
307 for (i = 0; i < ARM_SEC_MAX; ++i) {
308 struct arm_unwind_mapping *map = &mod->arch.map[i];
309 if (map->unw_sec && map->sec_text)
310 map->unwind = unwind_table_add(map->unw_sec->sh_addr,
311 map->unw_sec->sh_size,
312 map->sec_text->sh_addr,
313 map->sec_text->sh_size);
314 }
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100315}
316
317static void unregister_unwind_tables(struct module *mod)
318{
Phil Carmodye5f77722010-08-19 15:16:37 +0100319 int i = ARM_SEC_MAX;
320 while (--i >= 0)
321 unwind_table_del(mod->arch.map[i].unwind);
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100322}
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}