1 /* Kernel module help for SH.
3 SHcompact version by Kaz Kojima and Paul Mundt.
7 Copyright 2004 SuperH (UK) Ltd
10 Based on the sh version, and on code from the sh64-specific parts of
11 modutils, originally written by Richard Curnow and Ben Gaster.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/moduleloader.h>
28 #include <linux/elf.h>
29 #include <linux/vmalloc.h>
30 #include <linux/bug.h>
32 #include <linux/string.h>
33 #include <linux/kernel.h>
34 #include <asm/unaligned.h>
36 void *module_alloc(unsigned long size)
41 return vmalloc_exec(size);
45 /* Free memory returned from module_alloc */
46 void module_free(struct module *mod, void *module_region)
49 /* FIXME: If module_region == mod->init_region, trim exception
53 /* We don't need anything special. */
54 int module_frob_arch_sections(Elf_Ehdr *hdr,
62 int apply_relocate_add(Elf32_Shdr *sechdrs,
64 unsigned int symindex,
69 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
71 Elf32_Addr relocation;
75 pr_debug("Applying relocate section %u to %u\n", relsec,
76 sechdrs[relsec].sh_info);
77 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
78 /* This is where to make the change */
79 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
81 /* This is the symbol it is referring to. Note that all
82 undefined symbols have been resolved. */
83 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
84 + ELF32_R_SYM(rel[i].r_info);
85 relocation = sym->st_value + rel[i].r_addend;
87 #ifdef CONFIG_SUPERH64
88 /* For text addresses, bit2 of the st_other field indicates
89 * whether the symbol is SHmedia (1) or SHcompact (0). If
90 * SHmedia, the LSB of the symbol needs to be asserted
91 * for the CPU to be in SHmedia mode when it starts executing
92 * the branch target. */
93 relocation |= (sym->st_other & 4);
96 switch (ELF32_R_TYPE(rel[i].r_info)) {
98 value = get_unaligned(location);
100 put_unaligned(value, location);
103 relocation = (relocation - (Elf32_Addr) location);
104 value = get_unaligned(location);
106 put_unaligned(value, location);
109 *location = (*location & ~0x3fffc00) |
110 ((relocation & 0xffff) << 10);
112 case R_SH_IMM_MEDLOW16:
113 *location = (*location & ~0x3fffc00) |
114 (((relocation >> 16) & 0xffff) << 10);
116 case R_SH_IMM_LOW16_PCREL:
117 relocation -= (Elf32_Addr) location;
118 *location = (*location & ~0x3fffc00) |
119 ((relocation & 0xffff) << 10);
121 case R_SH_IMM_MEDLOW16_PCREL:
122 relocation -= (Elf32_Addr) location;
123 *location = (*location & ~0x3fffc00) |
124 (((relocation >> 16) & 0xffff) << 10);
127 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
128 me->name, ELF32_R_TYPE(rel[i].r_info));
135 int apply_relocate(Elf32_Shdr *sechdrs,
137 unsigned int symindex,
141 printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
146 int module_finalize(const Elf_Ehdr *hdr,
147 const Elf_Shdr *sechdrs,
150 return module_bug_finalize(hdr, sechdrs, me);
153 void module_arch_cleanup(struct module *mod)
155 module_bug_cleanup(mod);