http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / kernel / linux / 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/module.h>
13 #include <linux/kernel.h>
14 #include <linux/elf.h>
15 #include <linux/vmalloc.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/string.h>
19
20 #include <asm/pgtable.h>
21
22 void *module_alloc(unsigned long size)
23 {
24         struct vm_struct *area;
25         struct page **pages;
26         unsigned int array_size, i;
27
28         size = PAGE_ALIGN(size);
29         if (!size)
30                 goto out_null;
31
32         area = __get_vm_area(size, VM_ALLOC, MODULE_START, MODULE_END);
33         if (!area)
34                 goto out_null;
35
36         area->nr_pages = size >> PAGE_SHIFT;
37         array_size = area->nr_pages * sizeof(struct page *);
38         area->pages = pages = kmalloc(array_size, GFP_KERNEL);
39         if (!area->pages) {
40                 remove_vm_area(area->addr);
41                 kfree(area);
42                 goto out_null;
43         }
44
45         memset(pages, 0, array_size);
46
47         for (i = 0; i < area->nr_pages; i++) {
48                 pages[i] = alloc_page(GFP_KERNEL);
49                 if (unlikely(!pages[i])) {
50                         area->nr_pages = i;
51                         goto out_no_pages;
52                 }
53         }
54
55         if (map_vm_area(area, PAGE_KERNEL, &pages))
56                 goto out_no_pages;
57         return area->addr;
58
59  out_no_pages:
60         vfree(area->addr);
61  out_null:
62         return NULL;
63 }
64
65 void module_free(struct module *module, void *region)
66 {
67         vfree(region);
68 }
69
70 int module_frob_arch_sections(Elf_Ehdr *hdr,
71                               Elf_Shdr *sechdrs,
72                               char *secstrings,
73                               struct module *mod)
74 {
75         return 0;
76 }
77
78 int
79 apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
80                unsigned int relindex, struct module *module)
81 {
82         Elf32_Shdr *symsec = sechdrs + symindex;
83         Elf32_Shdr *relsec = sechdrs + relindex;
84         Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
85         Elf32_Rel *rel = (void *)relsec->sh_addr;
86         unsigned int i;
87
88         for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
89                 unsigned long loc;
90                 Elf32_Sym *sym;
91                 s32 offset;
92
93                 offset = ELF32_R_SYM(rel->r_info);
94                 if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
95                         printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
96                                 module->name, relindex, i);
97                         return -ENOEXEC;
98                 }
99
100                 sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
101
102                 if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
103                         printk(KERN_ERR "%s: out of bounds relocation, "
104                                 "section %d reloc %d offset %d size %d\n",
105                                 module->name, relindex, i, rel->r_offset,
106                                 dstsec->sh_size);
107                         return -ENOEXEC;
108                 }
109
110                 loc = dstsec->sh_addr + rel->r_offset;
111
112                 switch (ELF32_R_TYPE(rel->r_info)) {
113                 case R_ARM_ABS32:
114                         *(u32 *)loc += sym->st_value;
115                         break;
116
117                 case R_ARM_PC24:
118                         offset = (*(u32 *)loc & 0x00ffffff) << 2;
119                         if (offset & 0x02000000)
120                                 offset -= 0x04000000;
121
122                         offset += sym->st_value - loc;
123                         if (offset & 3 ||
124                             offset <= (s32)0xfc000000 ||
125                             offset >= (s32)0x04000000) {
126                                 printk(KERN_ERR
127                                        "%s: relocation out of range, section "
128                                        "%d reloc %d sym '%s'\n", module->name,
129                                        relindex, i, strtab + sym->st_name);
130                                 return -ENOEXEC;
131                         }
132
133                         offset >>= 2;
134
135                         *(u32 *)loc &= 0xff000000;
136                         *(u32 *)loc |= offset & 0x00ffffff;
137                         break;
138
139                 default:
140                         printk(KERN_ERR "%s: unknown relocation: %u\n",
141                                module->name, ELF32_R_TYPE(rel->r_info));
142                         return -ENOEXEC;
143                 }
144         }
145         return 0;
146 }
147
148 int
149 apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
150                    unsigned int symindex, unsigned int relsec, struct module *module)
151 {
152         printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
153                module->name);
154         return -ENOEXEC;
155 }
156
157 int
158 module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
159                 struct module *module)
160 {
161         return 0;
162 }
163
164 void
165 module_arch_cleanup(struct module *mod)
166 {
167 }