]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/sh/kernel/module.c
modules: Fix module_bug_list list corruption race
[net-next-2.6.git] / arch / sh / kernel / module.c
CommitLineData
1da177e4
LT
1/* Kernel module help for SH.
2
1cb80fcf
PM
3 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
1da177e4
LT
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.
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>
3108cf06 30#include <linux/bug.h>
1da177e4
LT
31#include <linux/fs.h>
32#include <linux/string.h>
33#include <linux/kernel.h>
1f9d2949 34#include <asm/unaligned.h>
a6a2f2ad 35#include <asm/dwarf.h>
1da177e4 36
1da177e4
LT
37void *module_alloc(unsigned long size)
38{
39 if (size == 0)
40 return NULL;
4b59c973
PM
41
42 return vmalloc_exec(size);
1da177e4
LT
43}
44
45
46/* Free memory returned from module_alloc */
47void module_free(struct module *mod, void *module_region)
48{
49 vfree(module_region);
1da177e4
LT
50}
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
1da177e4
LT
61int 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;
1da177e4 73
1cb80fcf
PM
74 pr_debug("Applying relocate section %u to %u\n", relsec,
75 sechdrs[relsec].sh_info);
1da177e4
LT
76 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;
1da177e4 85
1cb80fcf
PM
86#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. */
7cd0378e 92 relocation |= !!(sym->st_other & 4);
1cb80fcf
PM
93#endif
94
1da177e4
LT
95 switch (ELF32_R_TYPE(rel[i].r_info)) {
96 case R_SH_DIR32:
1f9d2949 97 value = get_unaligned(location);
1da177e4 98 value += relocation;
1f9d2949 99 put_unaligned(value, location);
1da177e4
LT
100 break;
101 case R_SH_REL32:
1cb80fcf 102 relocation = (relocation - (Elf32_Addr) location);
1f9d2949 103 value = get_unaligned(location);
1da177e4 104 value += relocation;
1f9d2949 105 put_unaligned(value, location);
1cb80fcf
PM
106 break;
107 case R_SH_IMM_LOW16:
108 *location = (*location & ~0x3fffc00) |
109 ((relocation & 0xffff) << 10);
110 break;
111 case R_SH_IMM_MEDLOW16:
112 *location = (*location & ~0x3fffc00) |
113 (((relocation >> 16) & 0xffff) << 10);
114 break;
115 case R_SH_IMM_LOW16_PCREL:
116 relocation -= (Elf32_Addr) location;
117 *location = (*location & ~0x3fffc00) |
118 ((relocation & 0xffff) << 10);
119 break;
120 case R_SH_IMM_MEDLOW16_PCREL:
121 relocation -= (Elf32_Addr) location;
122 *location = (*location & ~0x3fffc00) |
123 (((relocation >> 16) & 0xffff) << 10);
1da177e4
LT
124 break;
125 default:
126 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
127 me->name, ELF32_R_TYPE(rel[i].r_info));
128 return -ENOEXEC;
129 }
130 }
131 return 0;
132}
133
134int apply_relocate(Elf32_Shdr *sechdrs,
135 const char *strtab,
136 unsigned int symindex,
137 unsigned int relsec,
138 struct module *me)
139{
140 printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
141 me->name);
142 return -ENOEXEC;
143}
144
145int module_finalize(const Elf_Ehdr *hdr,
146 const Elf_Shdr *sechdrs,
147 struct module *me)
148{
5a3abba7 149 int ret = 0;
a6a2f2ad 150
5a3abba7 151 ret |= module_dwarf_finalize(hdr, sechdrs, me);
a6a2f2ad 152
5a3abba7 153 return ret;
1da177e4
LT
154}
155
156void module_arch_cleanup(struct module *mod)
157{
5a3abba7 158 module_dwarf_cleanup(mod);
1da177e4 159}