xref: /openbmc/linux/arch/sh/kernel/module.c (revision 643d1f7f)
1 /*  Kernel module help for SH.
2 
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 
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>
30 #include <linux/fs.h>
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33 
34 void *module_alloc(unsigned long size)
35 {
36 	if (size == 0)
37 		return NULL;
38 	return vmalloc(size);
39 }
40 
41 
42 /* Free memory returned from module_alloc */
43 void module_free(struct module *mod, void *module_region)
44 {
45 	vfree(module_region);
46 	/* FIXME: If module_region == mod->init_region, trim exception
47            table entries. */
48 }
49 
50 /* We don't need anything special. */
51 int module_frob_arch_sections(Elf_Ehdr *hdr,
52 			      Elf_Shdr *sechdrs,
53 			      char *secstrings,
54 			      struct module *mod)
55 {
56 	return 0;
57 }
58 
59 #ifdef CONFIG_SUPERH32
60 #define COPY_UNALIGNED_WORD(sw, tw, align) \
61 { \
62 	void *__s = &(sw), *__t = &(tw); \
63 	unsigned short *__s2 = __s, *__t2 = __t; \
64 	unsigned char *__s1 = __s, *__t1 = __t; \
65 	switch ((align)) \
66 	{ \
67 	case 0: \
68 		*(unsigned long *) __t = *(unsigned long *) __s; \
69 		break; \
70 	case 2: \
71 		*__t2++ = *__s2++; \
72 		*__t2 = *__s2; \
73 		break; \
74 	default: \
75 		*__t1++ = *__s1++; \
76 		*__t1++ = *__s1++; \
77 		*__t1++ = *__s1++; \
78 		*__t1 = *__s1; \
79 		break; \
80 	} \
81 }
82 #else
83 /* One thing SHmedia doesn't screw up! */
84 #define COPY_UNALIGNED_WORD(sw, tw, align)	{ (tw) = (sw); }
85 #endif
86 
87 int apply_relocate_add(Elf32_Shdr *sechdrs,
88 		   const char *strtab,
89 		   unsigned int symindex,
90 		   unsigned int relsec,
91 		   struct module *me)
92 {
93 	unsigned int i;
94 	Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
95 	Elf32_Sym *sym;
96 	Elf32_Addr relocation;
97 	uint32_t *location;
98 	uint32_t value;
99 	int align;
100 
101 	pr_debug("Applying relocate section %u to %u\n", relsec,
102 		 sechdrs[relsec].sh_info);
103 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
104 		/* This is where to make the change */
105 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
106 			+ rel[i].r_offset;
107 		/* This is the symbol it is referring to.  Note that all
108 		   undefined symbols have been resolved.  */
109 		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
110 			+ ELF32_R_SYM(rel[i].r_info);
111 		relocation = sym->st_value + rel[i].r_addend;
112 		align = (int)location & 3;
113 
114 #ifdef CONFIG_SUPERH64
115 		/* For text addresses, bit2 of the st_other field indicates
116 		 * whether the symbol is SHmedia (1) or SHcompact (0).  If
117 		 * SHmedia, the LSB of the symbol needs to be asserted
118 		 * for the CPU to be in SHmedia mode when it starts executing
119 		 * the branch target. */
120 		relocation |= (sym->st_other & 4);
121 #endif
122 
123 		switch (ELF32_R_TYPE(rel[i].r_info)) {
124 		case R_SH_DIR32:
125 			COPY_UNALIGNED_WORD (*location, value, align);
126 			value += relocation;
127 			COPY_UNALIGNED_WORD (value, *location, align);
128 			break;
129 		case R_SH_REL32:
130 			relocation = (relocation - (Elf32_Addr) location);
131 			COPY_UNALIGNED_WORD (*location, value, align);
132 			value += relocation;
133 			COPY_UNALIGNED_WORD (value, *location, align);
134 			break;
135 		case R_SH_IMM_LOW16:
136 			*location = (*location & ~0x3fffc00) |
137 				((relocation & 0xffff) << 10);
138 			break;
139 		case R_SH_IMM_MEDLOW16:
140 			*location = (*location & ~0x3fffc00) |
141 				(((relocation >> 16) & 0xffff) << 10);
142 			break;
143 		case R_SH_IMM_LOW16_PCREL:
144 			relocation -= (Elf32_Addr) location;
145 			*location = (*location & ~0x3fffc00) |
146 				((relocation & 0xffff) << 10);
147 			break;
148 		case R_SH_IMM_MEDLOW16_PCREL:
149 			relocation -= (Elf32_Addr) location;
150 			*location = (*location & ~0x3fffc00) |
151 				(((relocation >> 16) & 0xffff) << 10);
152 			break;
153 		default:
154 			printk(KERN_ERR "module %s: Unknown relocation: %u\n",
155 			       me->name, ELF32_R_TYPE(rel[i].r_info));
156 			return -ENOEXEC;
157 		}
158 	}
159 	return 0;
160 }
161 
162 int apply_relocate(Elf32_Shdr *sechdrs,
163 		       const char *strtab,
164 		       unsigned int symindex,
165 		       unsigned int relsec,
166 		       struct module *me)
167 {
168 	printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
169 	       me->name);
170 	return -ENOEXEC;
171 }
172 
173 int module_finalize(const Elf_Ehdr *hdr,
174 		    const Elf_Shdr *sechdrs,
175 		    struct module *me)
176 {
177 	return 0;
178 }
179 
180 void module_arch_cleanup(struct module *mod)
181 {
182 }
183