Merge branch 'linus' into x86/xen
[sfrench/cifs-2.6.git] / arch / mn10300 / kernel / module.c
1 /* MN10300 Kernel module helper routines
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by Mark Salter (msalter@redhat.com)
5  * - Derived from arch/i386/kernel/module.c
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public Licence as published by
9  * the Free Software Foundation; either version 2 of the Licence, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public Licence for more details.
16  *
17  * You should have received a copy of the GNU General Public Licence
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include <linux/moduleloader.h>
22 #include <linux/elf.h>
23 #include <linux/vmalloc.h>
24 #include <linux/fs.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/bug.h>
28
29 #if 0
30 #define DEBUGP printk
31 #else
32 #define DEBUGP(fmt, ...)
33 #endif
34
35 /*
36  * allocate storage for a module
37  */
38 void *module_alloc(unsigned long size)
39 {
40         if (size == 0)
41                 return NULL;
42         return vmalloc_exec(size);
43 }
44
45 /*
46  * free memory returned from module_alloc()
47  */
48 void module_free(struct module *mod, void *module_region)
49 {
50         vfree(module_region);
51         /* FIXME: If module_region == mod->init_region, trim exception
52          * table entries. */
53 }
54
55 /*
56  * allow the arch to fix up the section table
57  * - we don't need anything special
58  */
59 int module_frob_arch_sections(Elf_Ehdr *hdr,
60                               Elf_Shdr *sechdrs,
61                               char *secstrings,
62                               struct module *mod)
63 {
64         return 0;
65 }
66
67 static uint32_t reloc_get16(uint8_t *p)
68 {
69         return p[0] | (p[1] << 8);
70 }
71
72 static uint32_t reloc_get24(uint8_t *p)
73 {
74         return reloc_get16(p) | (p[2] << 16);
75 }
76
77 static uint32_t reloc_get32(uint8_t *p)
78 {
79         return reloc_get16(p) | (reloc_get16(p+2) << 16);
80 }
81
82 static void reloc_put16(uint8_t *p, uint32_t val)
83 {
84         p[0] = val & 0xff;
85         p[1] = (val >> 8) & 0xff;
86 }
87
88 static void reloc_put24(uint8_t *p, uint32_t val)
89 {
90         reloc_put16(p, val);
91         p[2] = (val >> 16) & 0xff;
92 }
93
94 static void reloc_put32(uint8_t *p, uint32_t val)
95 {
96         reloc_put16(p, val);
97         reloc_put16(p+2, val >> 16);
98 }
99
100 /*
101  * apply a REL relocation
102  */
103 int apply_relocate(Elf32_Shdr *sechdrs,
104                    const char *strtab,
105                    unsigned int symindex,
106                    unsigned int relsec,
107                    struct module *me)
108 {
109         printk(KERN_ERR "module %s: RELOCATION unsupported\n",
110                me->name);
111         return -ENOEXEC;
112 }
113
114 /*
115  * apply a RELA relocation
116  */
117 int apply_relocate_add(Elf32_Shdr *sechdrs,
118                        const char *strtab,
119                        unsigned int symindex,
120                        unsigned int relsec,
121                        struct module *me)
122 {
123         unsigned int i;
124         Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
125         Elf32_Sym *sym;
126         Elf32_Addr relocation;
127         uint8_t *location;
128         uint32_t value;
129
130         DEBUGP("Applying relocate section %u to %u\n",
131                relsec, sechdrs[relsec].sh_info);
132
133         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
134                 /* this is where to make the change */
135                 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
136                         + rel[i].r_offset;
137
138                 /* this is the symbol the relocation is referring to (note that
139                  * all undefined symbols have been resolved by the caller) */
140                 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
141                         + ELF32_R_SYM(rel[i].r_info);
142
143                 /* this is the adjustment to be made */
144                 relocation = sym->st_value + rel[i].r_addend;
145
146                 switch (ELF32_R_TYPE(rel[i].r_info)) {
147                         /* for the first four relocation types, we add the
148                          * adjustment into the value at the location given */
149                 case R_MN10300_32:
150                         value = reloc_get32(location);
151                         value += relocation;
152                         reloc_put32(location, value);
153                         break;
154                 case R_MN10300_24:
155                         value = reloc_get24(location);
156                         value += relocation;
157                         reloc_put24(location, value);
158                         break;
159                 case R_MN10300_16:
160                         value = reloc_get16(location);
161                         value += relocation;
162                         reloc_put16(location, value);
163                         break;
164                 case R_MN10300_8:
165                         *location += relocation;
166                         break;
167
168                         /* for the next three relocation types, we write the
169                          * adjustment with the address subtracted over the
170                          * value at the location given */
171                 case R_MN10300_PCREL32:
172                         value = relocation - (uint32_t) location;
173                         reloc_put32(location, value);
174                         break;
175                 case R_MN10300_PCREL16:
176                         value = relocation - (uint32_t) location;
177                         reloc_put16(location, value);
178                         break;
179                 case R_MN10300_PCREL8:
180                         *location = relocation - (uint32_t) location;
181                         break;
182
183                 default:
184                         printk(KERN_ERR "module %s: Unknown relocation: %u\n",
185                                me->name, ELF32_R_TYPE(rel[i].r_info));
186                         return -ENOEXEC;
187                 }
188         }
189         return 0;
190 }
191
192 /*
193  * finish loading the module
194  */
195 int module_finalize(const Elf_Ehdr *hdr,
196                     const Elf_Shdr *sechdrs,
197                     struct module *me)
198 {
199         return module_bug_finalize(hdr, sechdrs, me);
200 }
201
202 /*
203  * finish clearing the module
204  */
205 void module_arch_cleanup(struct module *mod)
206 {
207         module_bug_cleanup(mod);
208 }