blob: f9937c4b7d1ad289d17ef8749900d61a4ba23386 [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{
70 return 0;
71}
72
73int
74apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
75 unsigned int relindex, struct module *module)
76{
77 Elf32_Shdr *symsec = sechdrs + symindex;
78 Elf32_Shdr *relsec = sechdrs + relindex;
79 Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
80 Elf32_Rel *rel = (void *)relsec->sh_addr;
81 unsigned int i;
82
83 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
84 unsigned long loc;
85 Elf32_Sym *sym;
86 s32 offset;
Catalin Marinasb7493152010-06-21 15:11:38 +010087#ifdef CONFIG_THUMB2_KERNEL
Catalin Marinasadca6dc2009-07-24 12:32:59 +010088 u32 upper, lower, sign, j1, j2;
Catalin Marinasb7493152010-06-21 15:11:38 +010089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 offset = ELF32_R_SYM(rel->r_info);
92 if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
93 printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
94 module->name, relindex, i);
95 return -ENOEXEC;
96 }
97
98 sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
99
100 if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
101 printk(KERN_ERR "%s: out of bounds relocation, "
102 "section %d reloc %d offset %d size %d\n",
103 module->name, relindex, i, rel->r_offset,
104 dstsec->sh_size);
105 return -ENOEXEC;
106 }
107
108 loc = dstsec->sh_addr + rel->r_offset;
109
110 switch (ELF32_R_TYPE(rel->r_info)) {
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100111 case R_ARM_NONE:
112 /* ignore */
113 break;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 case R_ARM_ABS32:
116 *(u32 *)loc += sym->st_value;
117 break;
118
119 case R_ARM_PC24:
Daniel Jacobowitzc2e26112005-12-14 22:04:22 +0000120 case R_ARM_CALL:
121 case R_ARM_JUMP24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 offset = (*(u32 *)loc & 0x00ffffff) << 2;
123 if (offset & 0x02000000)
124 offset -= 0x04000000;
125
126 offset += sym->st_value - loc;
127 if (offset & 3 ||
Kevin Weltonc5f12502007-05-08 22:05:25 +0100128 offset <= (s32)0xfe000000 ||
129 offset >= (s32)0x02000000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 printk(KERN_ERR
131 "%s: relocation out of range, section "
132 "%d reloc %d sym '%s'\n", module->name,
133 relindex, i, strtab + sym->st_name);
134 return -ENOEXEC;
135 }
136
137 offset >>= 2;
138
139 *(u32 *)loc &= 0xff000000;
140 *(u32 *)loc |= offset & 0x00ffffff;
141 break;
142
Daniel Silverstone4731f8b2009-03-20 11:11:43 +0100143 case R_ARM_V4BX:
144 /* Preserve Rm and the condition code. Alter
145 * other bits to re-code instruction as
146 * MOV PC,Rm.
147 */
148 *(u32 *)loc &= 0xf000000f;
149 *(u32 *)loc |= 0x01a0f000;
150 break;
151
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100152 case R_ARM_PREL31:
153 offset = *(u32 *)loc + sym->st_value - loc;
154 *(u32 *)loc = offset & 0x7fffffff;
155 break;
156
Paul Gortmakerae51e602009-05-07 16:18:40 +0100157 case R_ARM_MOVW_ABS_NC:
158 case R_ARM_MOVT_ABS:
159 offset = *(u32 *)loc;
160 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
161 offset = (offset ^ 0x8000) - 0x8000;
162
163 offset += sym->st_value;
164 if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
165 offset >>= 16;
166
167 *(u32 *)loc &= 0xfff0f000;
168 *(u32 *)loc |= ((offset & 0xf000) << 4) |
169 (offset & 0x0fff);
170 break;
171
Catalin Marinasb7493152010-06-21 15:11:38 +0100172#ifdef CONFIG_THUMB2_KERNEL
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100173 case R_ARM_THM_CALL:
174 case R_ARM_THM_JUMP24:
175 upper = *(u16 *)loc;
176 lower = *(u16 *)(loc + 2);
177
178 /*
179 * 25 bit signed address range (Thumb-2 BL and B.W
180 * instructions):
181 * S:I1:I2:imm10:imm11:0
182 * where:
183 * S = upper[10] = offset[24]
184 * I1 = ~(J1 ^ S) = offset[23]
185 * I2 = ~(J2 ^ S) = offset[22]
186 * imm10 = upper[9:0] = offset[21:12]
187 * imm11 = lower[10:0] = offset[11:1]
188 * J1 = lower[13]
189 * J2 = lower[11]
190 */
191 sign = (upper >> 10) & 1;
192 j1 = (lower >> 13) & 1;
193 j2 = (lower >> 11) & 1;
194 offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
195 ((~(j2 ^ sign) & 1) << 22) |
196 ((upper & 0x03ff) << 12) |
197 ((lower & 0x07ff) << 1);
198 if (offset & 0x01000000)
199 offset -= 0x02000000;
200 offset += sym->st_value - loc;
201
202 /* only Thumb addresses allowed (no interworking) */
203 if (!(offset & 1) ||
204 offset <= (s32)0xff000000 ||
205 offset >= (s32)0x01000000) {
206 printk(KERN_ERR
207 "%s: relocation out of range, section "
208 "%d reloc %d sym '%s'\n", module->name,
209 relindex, i, strtab + sym->st_name);
210 return -ENOEXEC;
211 }
212
213 sign = (offset >> 24) & 1;
214 j1 = sign ^ (~(offset >> 23) & 1);
215 j2 = sign ^ (~(offset >> 22) & 1);
216 *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) |
217 ((offset >> 12) & 0x03ff));
218 *(u16 *)(loc + 2) = (u16)((lower & 0xd000) |
219 (j1 << 13) | (j2 << 11) |
220 ((offset >> 1) & 0x07ff));
Catalin Marinasadca6dc2009-07-24 12:32:59 +0100221 break;
222
Catalin Marinas8dd47742010-06-21 15:10:37 +0100223 case R_ARM_THM_MOVW_ABS_NC:
224 case R_ARM_THM_MOVT_ABS:
225 upper = *(u16 *)loc;
226 lower = *(u16 *)(loc + 2);
227
228 /*
229 * MOVT/MOVW instructions encoding in Thumb-2:
230 *
231 * i = upper[10]
232 * imm4 = upper[3:0]
233 * imm3 = lower[14:12]
234 * imm8 = lower[7:0]
235 *
236 * imm16 = imm4:i:imm3:imm8
237 */
238 offset = ((upper & 0x000f) << 12) |
239 ((upper & 0x0400) << 1) |
240 ((lower & 0x7000) >> 4) | (lower & 0x00ff);
241 offset = (offset ^ 0x8000) - 0x8000;
242 offset += sym->st_value;
243
244 if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
245 offset >>= 16;
246
247 *(u16 *)loc = (u16)((upper & 0xfbf0) |
248 ((offset & 0xf000) >> 12) |
249 ((offset & 0x0800) >> 1));
250 *(u16 *)(loc + 2) = (u16)((lower & 0x8f00) |
251 ((offset & 0x0700) << 4) |
252 (offset & 0x00ff));
253 break;
Catalin Marinasb7493152010-06-21 15:11:38 +0100254#endif
Catalin Marinas8dd47742010-06-21 15:10:37 +0100255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 default:
257 printk(KERN_ERR "%s: unknown relocation: %u\n",
258 module->name, ELF32_R_TYPE(rel->r_info));
259 return -ENOEXEC;
260 }
261 }
262 return 0;
263}
264
265int
266apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
267 unsigned int symindex, unsigned int relsec, struct module *module)
268{
269 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
270 module->name);
271 return -ENOEXEC;
272}
273
Russell King89313602010-11-12 13:02:46 +0000274struct mod_unwind_map {
275 const Elf_Shdr *unw_sec;
276 const Elf_Shdr *txt_sec;
277};
278
279int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
280 struct module *mod)
281{
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100282#ifdef CONFIG_ARM_UNWIND
Russell King89313602010-11-12 13:02:46 +0000283 const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
284 const Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
285 struct mod_unwind_map maps[ARM_SEC_MAX];
Phil Carmodye5f77722010-08-19 15:16:37 +0100286 int i;
Russell King89313602010-11-12 13:02:46 +0000287
288 memset(maps, 0, sizeof(maps));
289
290 for (s = sechdrs; s < sechdrs_end; s++) {
291 const char *secname = secstrs + s->sh_name;
292
293 if (strcmp(".ARM.exidx.init.text", secname) == 0)
294 maps[ARM_SEC_INIT].unw_sec = s;
295 else if (strcmp(".ARM.exidx.devinit.text", secname) == 0)
296 maps[ARM_SEC_DEVINIT].unw_sec = s;
297 else if (strcmp(".ARM.exidx", secname) == 0)
298 maps[ARM_SEC_CORE].unw_sec = s;
299 else if (strcmp(".ARM.exidx.exit.text", secname) == 0)
300 maps[ARM_SEC_EXIT].unw_sec = s;
301 else if (strcmp(".ARM.exidx.devexit.text", secname) == 0)
302 maps[ARM_SEC_DEVEXIT].unw_sec = s;
303 else if (strcmp(".init.text", secname) == 0)
304 maps[ARM_SEC_INIT].txt_sec = s;
305 else if (strcmp(".devinit.text", secname) == 0)
306 maps[ARM_SEC_DEVINIT].txt_sec = s;
307 else if (strcmp(".text", secname) == 0)
308 maps[ARM_SEC_CORE].txt_sec = s;
309 else if (strcmp(".exit.text", secname) == 0)
310 maps[ARM_SEC_EXIT].txt_sec = s;
311 else if (strcmp(".devexit.text", secname) == 0)
312 maps[ARM_SEC_DEVEXIT].txt_sec = s;
Phil Carmodye5f77722010-08-19 15:16:37 +0100313 }
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100314
Russell King89313602010-11-12 13:02:46 +0000315 for (i = 0; i < ARM_SEC_MAX; i++)
316 if (maps[i].unw_sec && maps[i].txt_sec)
317 mod->arch.unwind[i] =
318 unwind_table_add(maps[i].unw_sec->sh_addr,
319 maps[i].unw_sec->sh_size,
320 maps[i].txt_sec->sh_addr,
321 maps[i].txt_sec->sh_size);
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
324}
325
326void
327module_arch_cleanup(struct module *mod)
328{
Russell King89313602010-11-12 13:02:46 +0000329#ifdef CONFIG_ARM_UNWIND
330 int i;
331
332 for (i = 0; i < ARM_SEC_MAX; i++)
333 if (mod->arch.unwind[i])
334 unwind_table_del(mod->arch.unwind[i]);
335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}