blob: c43081039dd571ab3992628830d5cd171a3d23fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel module help for SH.
2
Paul Mundt1cb80fc2007-11-20 15:16:25 +09003 SHcompact version by Kaz Kojima and Paul Mundt.
4
5 SHmedia bits:
6
7 Copyright 2004 SuperH (UK) Ltd
8 Author: Richard Curnow
9
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.
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 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.
17
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.
22
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
26*/
27#include <linux/moduleloader.h>
28#include <linux/elf.h>
29#include <linux/vmalloc.h>
Paul Mundt3108cf02008-08-04 13:32:04 +090030#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/fs.h>
32#include <linux/string.h>
33#include <linux/kernel.h>
Harvey Harrison1f9d2942008-05-28 16:38:17 -070034#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036void *module_alloc(unsigned long size)
37{
38 if (size == 0)
39 return NULL;
Paul Mundt4b59c972008-08-04 13:34:29 +090040
41 return vmalloc_exec(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44
45/* Free memory returned from module_alloc */
46void 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. */
54int module_frob_arch_sections(Elf_Ehdr *hdr,
55 Elf_Shdr *sechdrs,
56 char *secstrings,
57 struct module *mod)
58{
59 return 0;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062int 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 uint32_t value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Paul Mundt1cb80fc2007-11-20 15:16:25 +090075 pr_debug("Applying relocate section %u to %u\n", relsec,
76 sechdrs[relsec].sh_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 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
80 + rel[i].r_offset;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Paul Mundt1cb80fc2007-11-20 15:16:25 +090087#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);
94#endif
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 switch (ELF32_R_TYPE(rel[i].r_info)) {
97 case R_SH_DIR32:
Harvey Harrison1f9d2942008-05-28 16:38:17 -070098 value = get_unaligned(location);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 value += relocation;
Harvey Harrison1f9d2942008-05-28 16:38:17 -0700100 put_unaligned(value, location);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 break;
102 case R_SH_REL32:
Paul Mundt1cb80fc2007-11-20 15:16:25 +0900103 relocation = (relocation - (Elf32_Addr) location);
Harvey Harrison1f9d2942008-05-28 16:38:17 -0700104 value = get_unaligned(location);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 value += relocation;
Harvey Harrison1f9d2942008-05-28 16:38:17 -0700106 put_unaligned(value, location);
Paul Mundt1cb80fc2007-11-20 15:16:25 +0900107 break;
108 case R_SH_IMM_LOW16:
109 *location = (*location & ~0x3fffc00) |
110 ((relocation & 0xffff) << 10);
111 break;
112 case R_SH_IMM_MEDLOW16:
113 *location = (*location & ~0x3fffc00) |
114 (((relocation >> 16) & 0xffff) << 10);
115 break;
116 case R_SH_IMM_LOW16_PCREL:
117 relocation -= (Elf32_Addr) location;
118 *location = (*location & ~0x3fffc00) |
119 ((relocation & 0xffff) << 10);
120 break;
121 case R_SH_IMM_MEDLOW16_PCREL:
122 relocation -= (Elf32_Addr) location;
123 *location = (*location & ~0x3fffc00) |
124 (((relocation >> 16) & 0xffff) << 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
126 default:
127 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
128 me->name, ELF32_R_TYPE(rel[i].r_info));
129 return -ENOEXEC;
130 }
131 }
132 return 0;
133}
134
135int apply_relocate(Elf32_Shdr *sechdrs,
136 const char *strtab,
137 unsigned int symindex,
138 unsigned int relsec,
139 struct module *me)
140{
141 printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
142 me->name);
143 return -ENOEXEC;
144}
145
146int module_finalize(const Elf_Ehdr *hdr,
147 const Elf_Shdr *sechdrs,
148 struct module *me)
149{
Paul Mundt3108cf02008-08-04 13:32:04 +0900150 return module_bug_finalize(hdr, sechdrs, me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153void module_arch_cleanup(struct module *mod)
154{
Paul Mundt3108cf02008-08-04 13:32:04 +0900155 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}