2 * linux/arch/arm/kernel/module.c
4 * Copyright (C) 2002 Russell King.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Module allocation method suggested by Andi Kleen.
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/elf.h>
16 #include <linux/vmalloc.h>
17 #include <linux/slab.h>
19 #include <linux/string.h>
21 #include <asm/pgtable.h>
23 #ifdef CONFIG_XIP_KERNEL
25 * The XIP kernel text is mapped in the module area for modules and
26 * some other stuff to work without any indirect relocations.
27 * MODULE_START is redefined here and not in asm/memory.h to avoid
28 * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
32 #define MODULE_START (((unsigned long)&_etext + ~PGDIR_MASK) & PGDIR_MASK)
35 void *module_alloc(unsigned long size)
37 struct vm_struct *area;
39 unsigned int array_size, i;
41 size = PAGE_ALIGN(size);
45 area = __get_vm_area(size, VM_ALLOC, MODULE_START, MODULE_END);
49 area->nr_pages = size >> PAGE_SHIFT;
50 array_size = area->nr_pages * sizeof(struct page *);
51 area->pages = pages = kmalloc(array_size, GFP_KERNEL);
53 remove_vm_area(area->addr);
58 memset(pages, 0, array_size);
60 for (i = 0; i < area->nr_pages; i++) {
61 pages[i] = alloc_page(GFP_KERNEL);
62 if (unlikely(!pages[i])) {
68 if (map_vm_area(area, PAGE_KERNEL, &pages))
78 void module_free(struct module *module, void *region)
83 int module_frob_arch_sections(Elf_Ehdr *hdr,
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)) {
127 *(u32 *)loc += sym->st_value;
131 offset = (*(u32 *)loc & 0x00ffffff) << 2;
132 if (offset & 0x02000000)
133 offset -= 0x04000000;
135 offset += sym->st_value - loc;
137 offset <= (s32)0xfc000000 ||
138 offset >= (s32)0x04000000) {
140 "%s: relocation out of range, section "
141 "%d reloc %d sym '%s'\n", module->name,
142 relindex, i, strtab + sym->st_name);
148 *(u32 *)loc &= 0xff000000;
149 *(u32 *)loc |= offset & 0x00ffffff;
153 printk(KERN_ERR "%s: unknown relocation: %u\n",
154 module->name, ELF32_R_TYPE(rel->r_info));
162 apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
163 unsigned int symindex, unsigned int relsec, struct module *module)
165 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
171 module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
172 struct module *module)
178 module_arch_cleanup(struct module *mod)