blob: 19b1f8826aef9ba9b2508d71db05f6d8b178b772 [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>
Matt Fleminga6a2f2a2009-10-09 23:20:54 +010035#include <asm/dwarf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037void *module_alloc(unsigned long size)
38{
39 if (size == 0)
40 return NULL;
Paul Mundt4b59c972008-08-04 13:34:29 +090041
42 return vmalloc_exec(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45
46/* Free memory returned from module_alloc */
47void module_free(struct module *mod, void *module_region)
48{
49 vfree(module_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52/* We don't need anything special. */
53int module_frob_arch_sections(Elf_Ehdr *hdr,
54 Elf_Shdr *sechdrs,
55 char *secstrings,
56 struct module *mod)
57{
58 return 0;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061int apply_relocate_add(Elf32_Shdr *sechdrs,
62 const char *strtab,
63 unsigned int symindex,
64 unsigned int relsec,
65 struct module *me)
66{
67 unsigned int i;
68 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
69 Elf32_Sym *sym;
70 Elf32_Addr relocation;
71 uint32_t *location;
72 uint32_t value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Paul Mundt1cb80fc2007-11-20 15:16:25 +090074 pr_debug("Applying relocate section %u to %u\n", relsec,
75 sechdrs[relsec].sh_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
77 /* This is where to make the change */
78 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
79 + rel[i].r_offset;
80 /* This is the symbol it is referring to. Note that all
81 undefined symbols have been resolved. */
82 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
83 + ELF32_R_SYM(rel[i].r_info);
84 relocation = sym->st_value + rel[i].r_addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Paul Mundt1cb80fc2007-11-20 15:16:25 +090086#ifdef CONFIG_SUPERH64
87 /* For text addresses, bit2 of the st_other field indicates
88 * whether the symbol is SHmedia (1) or SHcompact (0). If
89 * SHmedia, the LSB of the symbol needs to be asserted
90 * for the CPU to be in SHmedia mode when it starts executing
91 * the branch target. */
Paul Mundt7cd03782009-05-09 18:03:37 +090092 relocation |= !!(sym->st_other & 4);
Paul Mundt1cb80fc2007-11-20 15:16:25 +090093#endif
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 switch (ELF32_R_TYPE(rel[i].r_info)) {
Paul Mundt78207ff2011-05-23 17:09:30 +090096 case R_SH_NONE:
97 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 case R_SH_DIR32:
Harvey Harrison1f9d2942008-05-28 16:38:17 -070099 value = get_unaligned(location);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 value += relocation;
Harvey Harrison1f9d2942008-05-28 16:38:17 -0700101 put_unaligned(value, location);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 break;
103 case R_SH_REL32:
Paul Mundt1cb80fc2007-11-20 15:16:25 +0900104 relocation = (relocation - (Elf32_Addr) location);
Harvey Harrison1f9d2942008-05-28 16:38:17 -0700105 value = get_unaligned(location);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 value += relocation;
Harvey Harrison1f9d2942008-05-28 16:38:17 -0700107 put_unaligned(value, location);
Paul Mundt1cb80fc2007-11-20 15:16:25 +0900108 break;
109 case R_SH_IMM_LOW16:
110 *location = (*location & ~0x3fffc00) |
111 ((relocation & 0xffff) << 10);
112 break;
113 case R_SH_IMM_MEDLOW16:
114 *location = (*location & ~0x3fffc00) |
115 (((relocation >> 16) & 0xffff) << 10);
116 break;
117 case R_SH_IMM_LOW16_PCREL:
118 relocation -= (Elf32_Addr) location;
119 *location = (*location & ~0x3fffc00) |
120 ((relocation & 0xffff) << 10);
121 break;
122 case R_SH_IMM_MEDLOW16_PCREL:
123 relocation -= (Elf32_Addr) location;
124 *location = (*location & ~0x3fffc00) |
125 (((relocation >> 16) & 0xffff) << 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 break;
127 default:
128 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
129 me->name, ELF32_R_TYPE(rel[i].r_info));
130 return -ENOEXEC;
131 }
132 }
133 return 0;
134}
135
136int apply_relocate(Elf32_Shdr *sechdrs,
137 const char *strtab,
138 unsigned int symindex,
139 unsigned int relsec,
140 struct module *me)
141{
142 printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
143 me->name);
144 return -ENOEXEC;
145}
146
147int module_finalize(const Elf_Ehdr *hdr,
148 const Elf_Shdr *sechdrs,
149 struct module *me)
150{
Paul Mundt5a3abba72009-10-13 13:32:19 +0900151 int ret = 0;
Matt Fleminga6a2f2a2009-10-09 23:20:54 +0100152
Paul Mundt5a3abba72009-10-13 13:32:19 +0900153 ret |= module_dwarf_finalize(hdr, sechdrs, me);
Matt Fleminga6a2f2a2009-10-09 23:20:54 +0100154
Paul Mundt5a3abba72009-10-13 13:32:19 +0900155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158void module_arch_cleanup(struct module *mod)
159{
Paul Mundt5a3abba72009-10-13 13:32:19 +0900160 module_dwarf_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}