blob: bac03c81489dc48fd8e5e8c330172b83c0451151 [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>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/string.h>
22
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;
105
106 offset = ELF32_R_SYM(rel->r_info);
107 if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
108 printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
109 module->name, relindex, i);
110 return -ENOEXEC;
111 }
112
113 sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
114
115 if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
116 printk(KERN_ERR "%s: out of bounds relocation, "
117 "section %d reloc %d offset %d size %d\n",
118 module->name, relindex, i, rel->r_offset,
119 dstsec->sh_size);
120 return -ENOEXEC;
121 }
122
123 loc = dstsec->sh_addr + rel->r_offset;
124
125 switch (ELF32_R_TYPE(rel->r_info)) {
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100126 case R_ARM_NONE:
127 /* ignore */
128 break;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 case R_ARM_ABS32:
131 *(u32 *)loc += sym->st_value;
132 break;
133
134 case R_ARM_PC24:
Daniel Jacobowitzc2e26112005-12-14 22:04:22 +0000135 case R_ARM_CALL:
136 case R_ARM_JUMP24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 offset = (*(u32 *)loc & 0x00ffffff) << 2;
138 if (offset & 0x02000000)
139 offset -= 0x04000000;
140
141 offset += sym->st_value - loc;
142 if (offset & 3 ||
Kevin Weltonc5f12502007-05-08 22:05:25 +0100143 offset <= (s32)0xfe000000 ||
144 offset >= (s32)0x02000000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 printk(KERN_ERR
146 "%s: relocation out of range, section "
147 "%d reloc %d sym '%s'\n", module->name,
148 relindex, i, strtab + sym->st_name);
149 return -ENOEXEC;
150 }
151
152 offset >>= 2;
153
154 *(u32 *)loc &= 0xff000000;
155 *(u32 *)loc |= offset & 0x00ffffff;
156 break;
157
Daniel Silverstone4731f8b2009-03-20 11:11:43 +0100158 case R_ARM_V4BX:
159 /* Preserve Rm and the condition code. Alter
160 * other bits to re-code instruction as
161 * MOV PC,Rm.
162 */
163 *(u32 *)loc &= 0xf000000f;
164 *(u32 *)loc |= 0x01a0f000;
165 break;
166
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100167 case R_ARM_PREL31:
168 offset = *(u32 *)loc + sym->st_value - loc;
169 *(u32 *)loc = offset & 0x7fffffff;
170 break;
171
Paul Gortmakerae51e602009-05-07 16:18:40 +0100172 case R_ARM_MOVW_ABS_NC:
173 case R_ARM_MOVT_ABS:
174 offset = *(u32 *)loc;
175 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
176 offset = (offset ^ 0x8000) - 0x8000;
177
178 offset += sym->st_value;
179 if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
180 offset >>= 16;
181
182 *(u32 *)loc &= 0xfff0f000;
183 *(u32 *)loc |= ((offset & 0xf000) << 4) |
184 (offset & 0x0fff);
185 break;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 default:
188 printk(KERN_ERR "%s: unknown relocation: %u\n",
189 module->name, ELF32_R_TYPE(rel->r_info));
190 return -ENOEXEC;
191 }
192 }
193 return 0;
194}
195
196int
197apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
198 unsigned int symindex, unsigned int relsec, struct module *module)
199{
200 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
201 module->name);
202 return -ENOEXEC;
203}
204
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100205#ifdef CONFIG_ARM_UNWIND
206static void register_unwind_tables(struct module *mod)
207{
208 if (mod->arch.unw_sec_init && mod->arch.sec_init_text)
209 mod->arch.unwind_init =
210 unwind_table_add(mod->arch.unw_sec_init->sh_addr,
211 mod->arch.unw_sec_init->sh_size,
212 mod->arch.sec_init_text->sh_addr,
213 mod->arch.sec_init_text->sh_size);
214 if (mod->arch.unw_sec_devinit && mod->arch.sec_devinit_text)
215 mod->arch.unwind_devinit =
216 unwind_table_add(mod->arch.unw_sec_devinit->sh_addr,
217 mod->arch.unw_sec_devinit->sh_size,
218 mod->arch.sec_devinit_text->sh_addr,
219 mod->arch.sec_devinit_text->sh_size);
220 if (mod->arch.unw_sec_core && mod->arch.sec_core_text)
221 mod->arch.unwind_core =
222 unwind_table_add(mod->arch.unw_sec_core->sh_addr,
223 mod->arch.unw_sec_core->sh_size,
224 mod->arch.sec_core_text->sh_addr,
225 mod->arch.sec_core_text->sh_size);
226}
227
228static void unregister_unwind_tables(struct module *mod)
229{
230 unwind_table_del(mod->arch.unwind_init);
231 unwind_table_del(mod->arch.unwind_devinit);
232 unwind_table_del(mod->arch.unwind_core);
233}
234#else
235static inline void register_unwind_tables(struct module *mod) { }
236static inline void unregister_unwind_tables(struct module *mod) { }
237#endif
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239int
240module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
241 struct module *module)
242{
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100243 register_unwind_tables(module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
246
247void
248module_arch_cleanup(struct module *mod)
249{
Catalin Marinas2e1926e2009-02-11 13:09:54 +0100250 unregister_unwind_tables(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}