2 * linux/arch/arm/kernel/module.c
4 * Copyright (C) 2002 Russell King.
5 * Modified for nommu by Hyok S. Choi
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.
11 * Module allocation method suggested by Andi Kleen.
13 #include <linux/module.h>
14 #include <linux/moduleloader.h>
15 #include <linux/kernel.h>
17 #include <linux/elf.h>
18 #include <linux/vmalloc.h>
19 #include <linux/slab.h>
21 #include <linux/string.h>
23 #include <asm/pgtable.h>
24 #include <asm/sections.h>
25 #include <asm/unwind.h>
27 #ifdef CONFIG_XIP_KERNEL
29 * The XIP kernel text is mapped in the module area for modules and
30 * some other stuff to work without any indirect relocations.
31 * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
32 * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
35 #define MODULES_VADDR (((unsigned long)_etext + ~PGDIR_MASK) & PGDIR_MASK)
39 void *module_alloc(unsigned long size)
41 struct vm_struct *area;
43 size = PAGE_ALIGN(size);
47 area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
51 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
53 #else /* CONFIG_MMU */
54 void *module_alloc(unsigned long size)
56 return size == 0 ? NULL : vmalloc(size);
58 #endif /* !CONFIG_MMU */
60 void module_free(struct module *module, void *region)
65 int module_frob_arch_sections(Elf_Ehdr *hdr,
70 #ifdef CONFIG_ARM_UNWIND
71 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
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;
92 apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
93 unsigned int relindex, struct module *module)
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;
101 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
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);
113 sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
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,
123 loc = dstsec->sh_addr + rel->r_offset;
125 switch (ELF32_R_TYPE(rel->r_info)) {
131 *(u32 *)loc += sym->st_value;
137 offset = (*(u32 *)loc & 0x00ffffff) << 2;
138 if (offset & 0x02000000)
139 offset -= 0x04000000;
141 offset += sym->st_value - loc;
143 offset <= (s32)0xfe000000 ||
144 offset >= (s32)0x02000000) {
146 "%s: relocation out of range, section "
147 "%d reloc %d sym '%s'\n", module->name,
148 relindex, i, strtab + sym->st_name);
154 *(u32 *)loc &= 0xff000000;
155 *(u32 *)loc |= offset & 0x00ffffff;
159 /* Preserve Rm and the condition code. Alter
160 * other bits to re-code instruction as
163 *(u32 *)loc &= 0xf000000f;
164 *(u32 *)loc |= 0x01a0f000;
168 offset = *(u32 *)loc + sym->st_value - loc;
169 *(u32 *)loc = offset & 0x7fffffff;
173 printk(KERN_ERR "%s: unknown relocation: %u\n",
174 module->name, ELF32_R_TYPE(rel->r_info));
182 apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
183 unsigned int symindex, unsigned int relsec, struct module *module)
185 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
190 #ifdef CONFIG_ARM_UNWIND
191 static void register_unwind_tables(struct module *mod)
193 if (mod->arch.unw_sec_init && mod->arch.sec_init_text)
194 mod->arch.unwind_init =
195 unwind_table_add(mod->arch.unw_sec_init->sh_addr,
196 mod->arch.unw_sec_init->sh_size,
197 mod->arch.sec_init_text->sh_addr,
198 mod->arch.sec_init_text->sh_size);
199 if (mod->arch.unw_sec_devinit && mod->arch.sec_devinit_text)
200 mod->arch.unwind_devinit =
201 unwind_table_add(mod->arch.unw_sec_devinit->sh_addr,
202 mod->arch.unw_sec_devinit->sh_size,
203 mod->arch.sec_devinit_text->sh_addr,
204 mod->arch.sec_devinit_text->sh_size);
205 if (mod->arch.unw_sec_core && mod->arch.sec_core_text)
206 mod->arch.unwind_core =
207 unwind_table_add(mod->arch.unw_sec_core->sh_addr,
208 mod->arch.unw_sec_core->sh_size,
209 mod->arch.sec_core_text->sh_addr,
210 mod->arch.sec_core_text->sh_size);
213 static void unregister_unwind_tables(struct module *mod)
215 unwind_table_del(mod->arch.unwind_init);
216 unwind_table_del(mod->arch.unwind_devinit);
217 unwind_table_del(mod->arch.unwind_core);
220 static inline void register_unwind_tables(struct module *mod) { }
221 static inline void unregister_unwind_tables(struct module *mod) { }
225 module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
226 struct module *module)
228 register_unwind_tables(module);
233 module_arch_cleanup(struct module *mod)
235 unregister_unwind_tables(mod);