Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / arch / sh64 / kernel / module.c
1 /*  Kernel module help for sh64.
2
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17     Copyright 2004 SuperH (UK) Ltd
18     Author: Richard Curnow
19
20     Based on the sh version, and on code from the sh64-specific parts of
21     modutils, originally written by Richard Curnow and Ben Gaster.
22
23 */
24 #include <linux/moduleloader.h>
25 #include <linux/elf.h>
26 #include <linux/vmalloc.h>
27 #include <linux/fs.h>
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30
31 #if 0
32 #define DEBUGP printk
33 #else
34 #define DEBUGP(fmt...)
35 #endif
36
37 void *module_alloc(unsigned long size)
38 {
39         if (size == 0)
40                 return NULL;
41         return vmalloc(size);
42 }
43
44
45 /* Free memory returned from module_alloc */
46 void module_free(struct module *mod, void *module_region)
47 {
48         vfree(module_region);
49         /* FIXME: If module_region == mod->init_region, trim exception
50            table entries. */
51 }
52
53 /* We don't need anything special. */
54 int module_frob_arch_sections(Elf_Ehdr *hdr,
55                               Elf_Shdr *sechdrs,
56                               char *secstrings,
57                               struct module *mod)
58 {
59         return 0;
60 }
61
62 int apply_relocate_add(Elf32_Shdr *sechdrs,
63                    const char *strtab,
64                    unsigned int symindex,
65                    unsigned int relsec,
66                    struct module *me)
67 {
68         unsigned int i;
69         Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
70         Elf32_Sym *sym;
71         Elf32_Addr relocation;
72         uint32_t *location;
73         int align;
74         int is_shmedia;
75
76         DEBUGP("Applying relocate section %u to %u\n", relsec,
77                sechdrs[relsec].sh_info);
78         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
79                 /* This is where to make the change */
80                 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
81                         + rel[i].r_offset;
82                 /* This is the symbol it is referring to.  Note that all
83                    undefined symbols have been resolved.  */
84                 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
85                         + ELF32_R_SYM(rel[i].r_info);
86                 relocation = sym->st_value + rel[i].r_addend;
87                 align = (int)location & 3;
88
89                 /* For text addresses, bit2 of the st_other field indicates
90                  * whether the symbol is SHmedia (1) or SHcompact (0).  If
91                  * SHmedia, the LSB of the symbol needs to be asserted
92                  * for the CPU to be in SHmedia mode when it starts executing
93                  * the branch target. */
94                 is_shmedia = (sym->st_other & 4) ? 1 : 0;
95                 if (is_shmedia) {
96                         relocation |= 1;
97                 }
98
99                 switch (ELF32_R_TYPE(rel[i].r_info)) {
100                 case R_SH_DIR32:
101                         DEBUGP("R_SH_DIR32 @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
102                         *location += relocation;
103                         break;
104                 case R_SH_REL32:
105                         DEBUGP("R_SH_REL32 @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
106                         relocation -= (Elf32_Addr) location;
107                         *location += relocation;
108                         break;
109                 case R_SH_IMM_LOW16:
110                         DEBUGP("R_SH_IMM_LOW16 @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
111                         *location = (*location & ~0x3fffc00) |
112                                 ((relocation & 0xffff) << 10);
113                         break;
114                 case R_SH_IMM_MEDLOW16:
115                         DEBUGP("R_SH_IMM_MEDLOW16 @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
116                         *location = (*location & ~0x3fffc00) |
117                                 (((relocation >> 16) & 0xffff) << 10);
118                         break;
119                 case R_SH_IMM_LOW16_PCREL:
120                         DEBUGP("R_SH_IMM_LOW16_PCREL @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
121                         relocation -= (Elf32_Addr) location;
122                         *location = (*location & ~0x3fffc00) |
123                                 ((relocation & 0xffff) << 10);
124                         break;
125                 case R_SH_IMM_MEDLOW16_PCREL:
126                         DEBUGP("R_SH_IMM_MEDLOW16_PCREL @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
127                         relocation -= (Elf32_Addr) location;
128                         *location = (*location & ~0x3fffc00) |
129                                 (((relocation >> 16) & 0xffff) << 10);
130                         break;
131                 default:
132                         printk(KERN_ERR "module %s: Unknown relocation: %u\n",
133                                me->name, ELF32_R_TYPE(rel[i].r_info));
134                         return -ENOEXEC;
135                 }
136         }
137         return 0;
138 }
139
140 int apply_relocate(Elf32_Shdr *sechdrs,
141                        const char *strtab,
142                        unsigned int symindex,
143                        unsigned int relsec,
144                        struct module *me)
145 {
146         printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
147                me->name);
148         return -ENOEXEC;
149 }
150
151 int module_finalize(const Elf_Ehdr *hdr,
152                     const Elf_Shdr *sechdrs,
153                     struct module *me)
154 {
155         return 0;
156 }
157
158 void module_arch_cleanup(struct module *mod)
159 {
160 }
161