2 * Copyright 2004-2009 Analog Devices Inc.
4 * Licensed under the GPL-2 or later
7 #define pr_fmt(fmt) "module %s: " fmt
9 #include <linux/moduleloader.h>
10 #include <linux/elf.h>
11 #include <linux/vmalloc.h>
13 #include <linux/string.h>
14 #include <linux/kernel.h>
16 #include <asm/cacheflush.h>
17 #include <asm/uaccess.h>
19 void *module_alloc(unsigned long size)
26 /* Free memory returned from module_alloc */
27 void module_free(struct module *mod, void *module_region)
32 /* Transfer the section to the L1 memory */
34 module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
35 char *secstrings, struct module *mod)
38 * XXX: sechdrs are vmalloced in kernel/module.c
39 * and would be vfreed just after module is loaded,
40 * so we hack to keep the only information we needed
41 * in mod->arch to correctly free L1 I/D sram later.
42 * NOTE: this breaks the semantic of mod->arch structure.
44 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
47 for (s = sechdrs; s < sechdrs_end; ++s) {
48 const char *shname = secstrings + s->sh_name;
53 if (!strcmp(".l1.text", shname) ||
54 (!strcmp(".text", shname) &&
55 (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
57 dest = l1_inst_sram_alloc(s->sh_size);
58 mod->arch.text_l1 = dest;
60 pr_err("L1 inst memory allocation failed\n",
64 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
66 } else if (!strcmp(".l1.data", shname) ||
67 (!strcmp(".data", shname) &&
68 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
70 dest = l1_data_sram_alloc(s->sh_size);
71 mod->arch.data_a_l1 = dest;
73 pr_err("L1 data memory allocation failed\n",
77 memcpy(dest, (void *)s->sh_addr, s->sh_size);
79 } else if (!strcmp(".l1.bss", shname) ||
80 (!strcmp(".bss", shname) &&
81 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
83 dest = l1_data_sram_zalloc(s->sh_size);
84 mod->arch.bss_a_l1 = dest;
86 pr_err("L1 data memory allocation failed\n",
91 } else if (!strcmp(".l1.data.B", shname)) {
93 dest = l1_data_B_sram_alloc(s->sh_size);
94 mod->arch.data_b_l1 = dest;
96 pr_err("L1 data memory allocation failed\n",
100 memcpy(dest, (void *)s->sh_addr, s->sh_size);
102 } else if (!strcmp(".l1.bss.B", shname)) {
104 dest = l1_data_B_sram_alloc(s->sh_size);
105 mod->arch.bss_b_l1 = dest;
107 pr_err("L1 data memory allocation failed\n",
111 memset(dest, 0, s->sh_size);
113 } else if (!strcmp(".l2.text", shname) ||
114 (!strcmp(".text", shname) &&
115 (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
117 dest = l2_sram_alloc(s->sh_size);
118 mod->arch.text_l2 = dest;
120 pr_err("L2 SRAM allocation failed\n",
124 memcpy(dest, (void *)s->sh_addr, s->sh_size);
126 } else if (!strcmp(".l2.data", shname) ||
127 (!strcmp(".data", shname) &&
128 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
130 dest = l2_sram_alloc(s->sh_size);
131 mod->arch.data_l2 = dest;
133 pr_err("L2 SRAM allocation failed\n",
137 memcpy(dest, (void *)s->sh_addr, s->sh_size);
139 } else if (!strcmp(".l2.bss", shname) ||
140 (!strcmp(".bss", shname) &&
141 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
143 dest = l2_sram_zalloc(s->sh_size);
144 mod->arch.bss_l2 = dest;
146 pr_err("L2 SRAM allocation failed\n",
154 s->sh_flags &= ~SHF_ALLOC;
155 s->sh_addr = (unsigned long)dest;
162 apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
163 unsigned int symindex, unsigned int relsec, struct module *me)
165 pr_err(".rel unsupported\n", me->name);
169 /*************************************************************************/
170 /* FUNCTION : apply_relocate_add */
171 /* ABSTRACT : Blackfin specific relocation handling for the loadable */
172 /* modules. Modules are expected to be .o files. */
173 /* Arithmetic relocations are handled. */
174 /* We do not expect LSETUP to be split and hence is not */
176 /* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
177 /* gas does not generate it. */
178 /*************************************************************************/
180 apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
181 unsigned int symindex, unsigned int relsec,
185 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
187 unsigned long location, value, size;
189 pr_debug("applying relocate section %u to %u\n", mod->name,
190 relsec, sechdrs[relsec].sh_info);
192 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
193 /* This is where to make the change */
194 location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
197 /* This is the symbol it is referring to. Note that all
198 undefined symbols have been resolved. */
199 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
200 + ELF32_R_SYM(rel[i].r_info);
201 value = sym->st_value;
202 value += rel[i].r_addend;
205 if (location >= COREB_L1_DATA_A_START) {
206 pr_err("cannot relocate in L1: %u (SMP kernel)",
207 mod->name, ELF32_R_TYPE(rel[i].r_info));
212 pr_debug("location is %lx, value is %lx type is %d\n",
213 mod->name, location, value, ELF32_R_TYPE(rel[i].r_info));
215 switch (ELF32_R_TYPE(rel[i].r_info)) {
223 case R_BFIN_BYTE4_DATA:
228 case R_BFIN_PCREL24_JUMP_L:
229 case R_BFIN_PCREL12_JUMP:
230 case R_BFIN_PCREL12_JUMP_S:
232 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
233 mod->name, ELF32_R_TYPE(rel[i].r_info));
237 pr_err("unknown relocation: %u\n", mod->name,
238 ELF32_R_TYPE(rel[i].r_info));
242 switch (bfin_mem_access_type(location, size)) {
243 case BFIN_MEM_ACCESS_CORE:
244 case BFIN_MEM_ACCESS_CORE_ONLY:
245 memcpy((void *)location, &value, size);
247 case BFIN_MEM_ACCESS_DMA:
248 dma_memcpy((void *)location, &value, size);
250 case BFIN_MEM_ACCESS_ITEST:
251 isram_memcpy((void *)location, &value, size);
254 pr_err("invalid relocation for %#lx\n",
255 mod->name, location);
264 module_finalize(const Elf_Ehdr * hdr,
265 const Elf_Shdr * sechdrs, struct module *mod)
267 unsigned int i, strindex = 0, symindex = 0;
271 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
273 for (i = 1; i < hdr->e_shnum; i++) {
274 /* Internal symbols and strings. */
275 if (sechdrs[i].sh_type == SHT_SYMTAB) {
277 strindex = sechdrs[i].sh_link;
281 for (i = 1; i < hdr->e_shnum; i++) {
282 const char *strtab = (char *)sechdrs[strindex].sh_addr;
283 unsigned int info = sechdrs[i].sh_info;
284 const char *shname = secstrings + sechdrs[i].sh_name;
286 /* Not a valid relocation section? */
287 if (info >= hdr->e_shnum)
290 /* Only support RELA relocation types */
291 if (sechdrs[i].sh_type != SHT_RELA)
294 if (!strcmp(".rela.l2.text", shname) ||
295 !strcmp(".rela.l1.text", shname) ||
296 (!strcmp(".rela.text", shname) &&
297 (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
299 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
309 void module_arch_cleanup(struct module *mod)
311 l1_inst_sram_free(mod->arch.text_l1);
312 l1_data_A_sram_free(mod->arch.data_a_l1);
313 l1_data_A_sram_free(mod->arch.bss_a_l1);
314 l1_data_B_sram_free(mod->arch.data_b_l1);
315 l1_data_B_sram_free(mod->arch.bss_b_l1);
316 l2_sram_free(mod->arch.text_l2);
317 l2_sram_free(mod->arch.data_l2);
318 l2_sram_free(mod->arch.bss_l2);