Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / arch / sh / kernel / module.c
blob573f024baf145a6897a19ce5ae493795a9eed478
1 /* Kernel module help for SH.
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.
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.
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
17 #include <linux/moduleloader.h>
18 #include <linux/elf.h>
19 #include <linux/vmalloc.h>
20 #include <linux/fs.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
24 #if 0
25 #define DEBUGP printk
26 #else
27 #define DEBUGP(fmt...)
28 #endif
30 void *module_alloc(unsigned long size)
32 if (size == 0)
33 return NULL;
34 return vmalloc(size);
38 /* Free memory returned from module_alloc */
39 void module_free(struct module *mod, void *module_region)
41 vfree(module_region);
42 /* FIXME: If module_region == mod->init_region, trim exception
43 table entries. */
46 /* We don't need anything special. */
47 int module_frob_arch_sections(Elf_Ehdr *hdr,
48 Elf_Shdr *sechdrs,
49 char *secstrings,
50 struct module *mod)
52 return 0;
55 #define COPY_UNALIGNED_WORD(sw, tw, align) \
56 { \
57 void *__s = &(sw), *__t = &(tw); \
58 switch ((align)) \
59 { \
60 case 0: \
61 *(unsigned long *) __t = *(unsigned long *) __s; \
62 break; \
63 case 2: \
64 *((unsigned short *) __t)++ = *((unsigned short *) __s)++; \
65 *((unsigned short *) __t) = *((unsigned short *) __s); \
66 break; \
67 default: \
68 *((unsigned char *) __t)++ = *((unsigned char *) __s)++; \
69 *((unsigned char *) __t)++ = *((unsigned char *) __s)++; \
70 *((unsigned char *) __t)++ = *((unsigned char *) __s)++; \
71 *((unsigned char *) __t) = *((unsigned char *) __s); \
72 break; \
73 } \
76 int apply_relocate_add(Elf32_Shdr *sechdrs,
77 const char *strtab,
78 unsigned int symindex,
79 unsigned int relsec,
80 struct module *me)
82 unsigned int i;
83 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
84 Elf32_Sym *sym;
85 Elf32_Addr relocation;
86 uint32_t *location;
87 uint32_t value;
88 int align;
90 DEBUGP("Applying relocate section %u to %u\n", relsec,
91 sechdrs[relsec].sh_info);
92 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
93 /* This is where to make the change */
94 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
95 + rel[i].r_offset;
96 /* This is the symbol it is referring to. Note that all
97 undefined symbols have been resolved. */
98 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
99 + ELF32_R_SYM(rel[i].r_info);
100 relocation = sym->st_value + rel[i].r_addend;
101 align = (int)location & 3;
103 switch (ELF32_R_TYPE(rel[i].r_info)) {
104 case R_SH_DIR32:
105 COPY_UNALIGNED_WORD (*location, value, align);
106 value += relocation;
107 COPY_UNALIGNED_WORD (value, *location, align);
108 break;
109 case R_SH_REL32:
110 relocation = (relocation - (Elf32_Addr) location);
111 COPY_UNALIGNED_WORD (*location, value, align);
112 value += relocation;
113 COPY_UNALIGNED_WORD (value, *location, align);
114 break;
115 default:
116 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
117 me->name, ELF32_R_TYPE(rel[i].r_info));
118 return -ENOEXEC;
121 return 0;
124 int apply_relocate(Elf32_Shdr *sechdrs,
125 const char *strtab,
126 unsigned int symindex,
127 unsigned int relsec,
128 struct module *me)
130 printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
131 me->name);
132 return -ENOEXEC;
135 int module_finalize(const Elf_Ehdr *hdr,
136 const Elf_Shdr *sechdrs,
137 struct module *me)
139 return 0;