2 * File: arch/blackfin/kernel/module.c
10 * Copyright 2004-2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <linux/moduleloader.h>
32 #include <linux/elf.h>
33 #include <linux/vmalloc.h>
35 #include <linux/string.h>
36 #include <linux/kernel.h>
38 #include <asm/cacheflush.h>
41 * handle arithmetic relocations.
42 * See binutils/bfd/elf32-bfin.c for more details
44 #define RELOC_STACK_SIZE 100
45 static uint32_t reloc_stack[RELOC_STACK_SIZE];
46 static unsigned int reloc_stack_tos;
48 #define is_reloc_stack_empty() ((reloc_stack_tos > 0)?0:1)
50 static void reloc_stack_push(uint32_t value)
52 reloc_stack[reloc_stack_tos++] = value;
55 static uint32_t reloc_stack_pop(void)
57 return reloc_stack[--reloc_stack_tos];
60 static uint32_t reloc_stack_operate(unsigned int oper, struct module *mod)
66 value = reloc_stack[reloc_stack_tos - 2] +
67 reloc_stack[reloc_stack_tos - 1];
71 value = reloc_stack[reloc_stack_tos - 2] -
72 reloc_stack[reloc_stack_tos - 1];
76 value = reloc_stack[reloc_stack_tos - 2] *
77 reloc_stack[reloc_stack_tos - 1];
81 value = reloc_stack[reloc_stack_tos - 2] /
82 reloc_stack[reloc_stack_tos - 1];
86 value = reloc_stack[reloc_stack_tos - 2] %
87 reloc_stack[reloc_stack_tos - 1];
91 value = reloc_stack[reloc_stack_tos - 2] <<
92 reloc_stack[reloc_stack_tos - 1];
96 value = reloc_stack[reloc_stack_tos - 2] >>
97 reloc_stack[reloc_stack_tos - 1];
101 value = reloc_stack[reloc_stack_tos - 2] &
102 reloc_stack[reloc_stack_tos - 1];
103 reloc_stack_tos -= 2;
106 value = reloc_stack[reloc_stack_tos - 2] |
107 reloc_stack[reloc_stack_tos - 1];
108 reloc_stack_tos -= 2;
111 value = reloc_stack[reloc_stack_tos - 2] ^
112 reloc_stack[reloc_stack_tos - 1];
113 reloc_stack_tos -= 2;
116 value = reloc_stack[reloc_stack_tos - 2] &&
117 reloc_stack[reloc_stack_tos - 1];
118 reloc_stack_tos -= 2;
121 value = reloc_stack[reloc_stack_tos - 2] ||
122 reloc_stack[reloc_stack_tos - 1];
123 reloc_stack_tos -= 2;
126 value = -reloc_stack[reloc_stack_tos - 1];
130 value = ~reloc_stack[reloc_stack_tos - 1];
131 reloc_stack_tos -= 1;
134 printk(KERN_WARNING "module %s: unhandled reloction\n",
139 /* now push the new value back on stack */
140 reloc_stack_push(value);
145 void *module_alloc(unsigned long size)
149 return vmalloc(size);
152 /* Free memory returned from module_alloc */
153 void module_free(struct module *mod, void *module_region)
155 vfree(module_region);
158 /* Transfer the section to the L1 memory */
160 module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
161 char *secstrings, struct module *mod)
164 * XXX: sechdrs are vmalloced in kernel/module.c
165 * and would be vfreed just after module is loaded,
166 * so we hack to keep the only information we needed
167 * in mod->arch to correctly free L1 I/D sram later.
168 * NOTE: this breaks the semantic of mod->arch structure.
170 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
173 for (s = sechdrs; s < sechdrs_end; ++s) {
174 if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) ||
175 ((strcmp(".text", secstrings + s->sh_name) == 0) &&
176 (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) {
177 dest = l1_inst_sram_alloc(s->sh_size);
178 mod->arch.text_l1 = dest;
181 "module %s: L1 instruction memory allocation failed\n",
185 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
186 s->sh_flags &= ~SHF_ALLOC;
187 s->sh_addr = (unsigned long)dest;
189 if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
190 ((strcmp(".data", secstrings + s->sh_name) == 0) &&
191 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
192 dest = l1_data_sram_alloc(s->sh_size);
193 mod->arch.data_a_l1 = dest;
196 "module %s: L1 data memory allocation failed\n",
200 memcpy(dest, (void *)s->sh_addr, s->sh_size);
201 s->sh_flags &= ~SHF_ALLOC;
202 s->sh_addr = (unsigned long)dest;
204 if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 ||
205 ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
206 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
207 dest = l1_data_sram_alloc(s->sh_size);
208 mod->arch.bss_a_l1 = dest;
211 "module %s: L1 data memory allocation failed\n",
215 memset(dest, 0, s->sh_size);
216 s->sh_flags &= ~SHF_ALLOC;
217 s->sh_addr = (unsigned long)dest;
219 if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) {
220 dest = l1_data_B_sram_alloc(s->sh_size);
221 mod->arch.data_b_l1 = dest;
224 "module %s: L1 data memory allocation failed\n",
228 memcpy(dest, (void *)s->sh_addr, s->sh_size);
229 s->sh_flags &= ~SHF_ALLOC;
230 s->sh_addr = (unsigned long)dest;
232 if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) {
233 dest = l1_data_B_sram_alloc(s->sh_size);
234 mod->arch.bss_b_l1 = dest;
237 "module %s: L1 data memory allocation failed\n",
241 memset(dest, 0, s->sh_size);
242 s->sh_flags &= ~SHF_ALLOC;
243 s->sh_addr = (unsigned long)dest;
245 if ((strcmp(".l2.text", secstrings + s->sh_name) == 0) ||
246 ((strcmp(".text", secstrings + s->sh_name) == 0) &&
247 (hdr->e_flags & EF_BFIN_CODE_IN_L2) && (s->sh_size > 0))) {
248 dest = l2_sram_alloc(s->sh_size);
249 mod->arch.text_l2 = dest;
252 "module %s: L2 SRAM allocation failed\n",
256 memcpy(dest, (void *)s->sh_addr, s->sh_size);
257 s->sh_flags &= ~SHF_ALLOC;
258 s->sh_addr = (unsigned long)dest;
260 if ((strcmp(".l2.data", secstrings + s->sh_name) == 0) ||
261 ((strcmp(".data", secstrings + s->sh_name) == 0) &&
262 (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
263 dest = l2_sram_alloc(s->sh_size);
264 mod->arch.data_l2 = dest;
267 "module %s: L2 SRAM allocation failed\n",
271 memcpy(dest, (void *)s->sh_addr, s->sh_size);
272 s->sh_flags &= ~SHF_ALLOC;
273 s->sh_addr = (unsigned long)dest;
275 if (strcmp(".l2.bss", secstrings + s->sh_name) == 0 ||
276 ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
277 (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
278 dest = l2_sram_alloc(s->sh_size);
279 mod->arch.bss_l2 = dest;
282 "module %s: L2 SRAM allocation failed\n",
286 memset(dest, 0, s->sh_size);
287 s->sh_flags &= ~SHF_ALLOC;
288 s->sh_addr = (unsigned long)dest;
295 apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
296 unsigned int symindex, unsigned int relsec, struct module *me)
298 printk(KERN_ERR "module %s: .rel unsupported\n", me->name);
302 /*************************************************************************/
303 /* FUNCTION : apply_relocate_add */
304 /* ABSTRACT : Blackfin specific relocation handling for the loadable */
305 /* modules. Modules are expected to be .o files. */
306 /* Arithmetic relocations are handled. */
307 /* We do not expect LSETUP to be split and hence is not */
309 /* R_byte and R_byte2 are also not handled as the gas */
310 /* does not generate it. */
311 /*************************************************************************/
313 apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
314 unsigned int symindex, unsigned int relsec,
319 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
321 uint32_t *location32;
322 uint16_t *location16;
325 pr_debug("Applying relocate section %u to %u\n", relsec,
326 sechdrs[relsec].sh_info);
327 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
328 /* This is where to make the change */
330 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr +
332 location32 = (uint32_t *) location16;
333 /* This is the symbol it is referring to. Note that all
334 undefined symbols have been resolved. */
335 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
336 + ELF32_R_SYM(rel[i].r_info);
337 if (is_reloc_stack_empty()) {
338 value = sym->st_value;
340 value = reloc_stack_pop();
342 value += rel[i].r_addend;
343 pr_debug("location is %x, value is %x type is %d \n",
344 (unsigned int) location32, value,
345 ELF32_R_TYPE(rel[i].r_info));
347 switch (ELF32_R_TYPE(rel[i].r_info)) {
350 case R_pcrel24_jump_l:
351 /* Add the value, subtract its postition */
353 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].
354 sh_addr + rel[i].r_offset - 2);
355 location32 = (uint32_t *) location16;
356 value -= (uint32_t) location32;
358 pr_debug("value is %x, before %x-%x after %x-%x\n", value,
359 *location16, *(location16 + 1),
360 (*location16 & 0xff00) | (value >> 16 & 0x00ff),
363 (*location16 & 0xff00) | (value >> 16 & 0x00ff);
364 *(location16 + 1) = value & 0xffff;
367 case R_pcrel12_jump_s:
368 value -= (uint32_t) location32;
370 *location16 = (value & 0xfff);
373 value -= (uint32_t) location32;
375 *location16 = (value & 0x3ff);
378 pr_debug("before %x after %x\n", *location16,
380 tmp = (value & 0xffff);
381 if ((unsigned long)location16 >= L1_CODE_START) {
382 dma_memcpy(location16, &tmp, 2);
387 pr_debug("before %x after %x\n", *location16,
388 ((value >> 16) & 0xffff));
389 tmp = ((value >> 16) & 0xffff);
390 if ((unsigned long)location16 >= L1_CODE_START) {
391 dma_memcpy(location16, &tmp, 2);
396 *location16 = (value & 0xffff);
399 pr_debug("before %x after %x\n", *location32, value);
403 reloc_stack_push(value);
406 reloc_stack_push(rel[i].r_addend);
422 reloc_stack_operate(ELF32_R_TYPE(rel[i].r_info), mod);
425 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
426 mod->name, ELF32_R_TYPE(rel[i].r_info));
434 module_finalize(const Elf_Ehdr * hdr,
435 const Elf_Shdr * sechdrs, struct module *mod)
437 unsigned int i, strindex = 0, symindex = 0;
440 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
442 for (i = 1; i < hdr->e_shnum; i++) {
443 /* Internal symbols and strings. */
444 if (sechdrs[i].sh_type == SHT_SYMTAB) {
446 strindex = sechdrs[i].sh_link;
450 for (i = 1; i < hdr->e_shnum; i++) {
451 const char *strtab = (char *)sechdrs[strindex].sh_addr;
452 unsigned int info = sechdrs[i].sh_info;
454 /* Not a valid relocation section? */
455 if (info >= hdr->e_shnum)
458 if ((sechdrs[i].sh_type == SHT_RELA) &&
459 ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) ||
460 (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
461 ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
462 (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) {
463 apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
470 void module_arch_cleanup(struct module *mod)
472 l1_inst_sram_free(mod->arch.text_l1);
473 l1_data_A_sram_free(mod->arch.data_a_l1);
474 l1_data_A_sram_free(mod->arch.bss_a_l1);
475 l1_data_B_sram_free(mod->arch.data_b_l1);
476 l1_data_B_sram_free(mod->arch.bss_b_l1);
477 l2_sram_free(mod->arch.text_l2);
478 l2_sram_free(mod->arch.data_l2);
479 l2_sram_free(mod->arch.bss_l2);