vserver 1.9.5.x5
[linux-2.6.git] / arch / arm / kernel / module.c
1 /*
2  *  linux/arch/arm/kernel/module.c
3  *
4  *  Copyright (C) 2002 Russell King.
5  *
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.
9  *
10  * Module allocation method suggested by Andi Kleen.
11  */
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>
18 #include <linux/fs.h>
19 #include <linux/string.h>
20
21 #include <asm/pgtable.h>
22
23 #ifdef CONFIG_XIP_KERNEL
24 /*
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.
29  */
30 extern void _etext;
31 #undef MODULE_START
32 #define MODULE_START    (((unsigned long)&_etext + ~PGDIR_MASK) & PGDIR_MASK)
33 #endif
34
35 void *module_alloc(unsigned long size)
36 {
37         struct vm_struct *area;
38         struct page **pages;
39         unsigned int array_size, i;
40
41         size = PAGE_ALIGN(size);
42         if (!size)
43                 goto out_null;
44
45         area = __get_vm_area(size, VM_ALLOC, MODULE_START, MODULE_END);
46         if (!area)
47                 goto out_null;
48
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);
52         if (!area->pages) {
53                 remove_vm_area(area->addr);
54                 kfree(area);
55                 goto out_null;
56         }
57
58         memset(pages, 0, array_size);
59
60         for (i = 0; i < area->nr_pages; i++) {
61                 pages[i] = alloc_page(GFP_KERNEL);
62                 if (unlikely(!pages[i])) {
63                         area->nr_pages = i;
64                         goto out_no_pages;
65                 }
66         }
67
68         if (map_vm_area(area, PAGE_KERNEL, &pages))
69                 goto out_no_pages;
70         return area->addr;
71
72  out_no_pages:
73         vfree(area->addr);
74  out_null:
75         return NULL;
76 }
77
78 void module_free(struct module *module, void *region)
79 {
80         vfree(region);
81 }
82
83 int module_frob_arch_sections(Elf_Ehdr *hdr,
84                               Elf_Shdr *sechdrs,
85                               char *secstrings,
86                               struct module *mod)
87 {
88         return 0;
89 }
90
91 int
92 apply_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)) {
126                 case R_ARM_ABS32:
127                         *(u32 *)loc += sym->st_value;
128                         break;
129
130                 case R_ARM_PC24:
131                         offset = (*(u32 *)loc & 0x00ffffff) << 2;
132                         if (offset & 0x02000000)
133                                 offset -= 0x04000000;
134
135                         offset += sym->st_value - loc;
136                         if (offset & 3 ||
137                             offset <= (s32)0xfc000000 ||
138                             offset >= (s32)0x04000000) {
139                                 printk(KERN_ERR
140                                        "%s: relocation out of range, section "
141                                        "%d reloc %d sym '%s'\n", module->name,
142                                        relindex, i, strtab + sym->st_name);
143                                 return -ENOEXEC;
144                         }
145
146                         offset >>= 2;
147
148                         *(u32 *)loc &= 0xff000000;
149                         *(u32 *)loc |= offset & 0x00ffffff;
150                         break;
151
152                 default:
153                         printk(KERN_ERR "%s: unknown relocation: %u\n",
154                                module->name, ELF32_R_TYPE(rel->r_info));
155                         return -ENOEXEC;
156                 }
157         }
158         return 0;
159 }
160
161 int
162 apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
163                    unsigned int symindex, unsigned int relsec, struct module *module)
164 {
165         printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
166                module->name);
167         return -ENOEXEC;
168 }
169
170 int
171 module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
172                 struct module *module)
173 {
174         return 0;
175 }
176
177 void
178 module_arch_cleanup(struct module *mod)
179 {
180 }