2 * Copyright 2004-2009 Analog Devices Inc.
4 * Licensed under the GPL-2 or later
7 #define pr_fmt(fmt) "module %s: " fmt, mod->name
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");
63 dma_memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
65 } else if (!strcmp(".l1.data", shname
) ||
66 (!strcmp(".data", shname
) &&
67 (hdr
->e_flags
& EF_BFIN_DATA_IN_L1
))) {
69 dest
= l1_data_sram_alloc(s
->sh_size
);
70 mod
->arch
.data_a_l1
= dest
;
72 pr_err("L1 data memory allocation failed\n");
75 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
77 } else if (!strcmp(".l1.bss", shname
) ||
78 (!strcmp(".bss", shname
) &&
79 (hdr
->e_flags
& EF_BFIN_DATA_IN_L1
))) {
81 dest
= l1_data_sram_zalloc(s
->sh_size
);
82 mod
->arch
.bss_a_l1
= dest
;
84 pr_err("L1 data memory allocation failed\n");
88 } else if (!strcmp(".l1.data.B", shname
)) {
90 dest
= l1_data_B_sram_alloc(s
->sh_size
);
91 mod
->arch
.data_b_l1
= dest
;
93 pr_err("L1 data memory allocation failed\n");
96 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
98 } else if (!strcmp(".l1.bss.B", shname
)) {
100 dest
= l1_data_B_sram_alloc(s
->sh_size
);
101 mod
->arch
.bss_b_l1
= dest
;
103 pr_err("L1 data memory allocation failed\n");
106 memset(dest
, 0, s
->sh_size
);
108 } else if (!strcmp(".l2.text", shname
) ||
109 (!strcmp(".text", shname
) &&
110 (hdr
->e_flags
& EF_BFIN_CODE_IN_L2
))) {
112 dest
= l2_sram_alloc(s
->sh_size
);
113 mod
->arch
.text_l2
= dest
;
115 pr_err("L2 SRAM allocation failed\n");
118 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
120 } else if (!strcmp(".l2.data", shname
) ||
121 (!strcmp(".data", shname
) &&
122 (hdr
->e_flags
& EF_BFIN_DATA_IN_L2
))) {
124 dest
= l2_sram_alloc(s
->sh_size
);
125 mod
->arch
.data_l2
= dest
;
127 pr_err("L2 SRAM allocation failed\n");
130 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
132 } else if (!strcmp(".l2.bss", shname
) ||
133 (!strcmp(".bss", shname
) &&
134 (hdr
->e_flags
& EF_BFIN_DATA_IN_L2
))) {
136 dest
= l2_sram_zalloc(s
->sh_size
);
137 mod
->arch
.bss_l2
= dest
;
139 pr_err("L2 SRAM allocation failed\n");
146 s
->sh_flags
&= ~SHF_ALLOC
;
147 s
->sh_addr
= (unsigned long)dest
;
154 apply_relocate(Elf_Shdr
* sechdrs
, const char *strtab
,
155 unsigned int symindex
, unsigned int relsec
, struct module
*mod
)
157 pr_err(".rel unsupported\n");
161 /*************************************************************************/
162 /* FUNCTION : apply_relocate_add */
163 /* ABSTRACT : Blackfin specific relocation handling for the loadable */
164 /* modules. Modules are expected to be .o files. */
165 /* Arithmetic relocations are handled. */
166 /* We do not expect LSETUP to be split and hence is not */
168 /* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
169 /* gas does not generate it. */
170 /*************************************************************************/
172 apply_relocate_add(Elf_Shdr
*sechdrs
, const char *strtab
,
173 unsigned int symindex
, unsigned int relsec
,
177 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
179 unsigned long location
, value
, size
;
181 pr_debug("applying relocate section %u to %u\n",
182 relsec
, sechdrs
[relsec
].sh_info
);
184 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
185 /* This is where to make the change */
186 location
= sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
+
189 /* This is the symbol it is referring to. Note that all
190 undefined symbols have been resolved. */
191 sym
= (Elf32_Sym
*) sechdrs
[symindex
].sh_addr
192 + ELF32_R_SYM(rel
[i
].r_info
);
193 value
= sym
->st_value
;
194 value
+= rel
[i
].r_addend
;
197 if (location
>= COREB_L1_DATA_A_START
) {
198 pr_err("cannot relocate in L1: %u (SMP kernel)\n",
199 ELF32_R_TYPE(rel
[i
].r_info
));
204 pr_debug("location is %lx, value is %lx type is %d\n",
205 location
, value
, ELF32_R_TYPE(rel
[i
].r_info
));
207 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
215 case R_BFIN_BYTE4_DATA
:
220 case R_BFIN_PCREL24_JUMP_L
:
221 case R_BFIN_PCREL12_JUMP
:
222 case R_BFIN_PCREL12_JUMP_S
:
224 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
225 ELF32_R_TYPE(rel
[i
].r_info
));
229 pr_err("unknown relocation: %u\n",
230 ELF32_R_TYPE(rel
[i
].r_info
));
234 switch (bfin_mem_access_type(location
, size
)) {
235 case BFIN_MEM_ACCESS_CORE
:
236 case BFIN_MEM_ACCESS_CORE_ONLY
:
237 memcpy((void *)location
, &value
, size
);
239 case BFIN_MEM_ACCESS_DMA
:
240 dma_memcpy((void *)location
, &value
, size
);
242 case BFIN_MEM_ACCESS_ITEST
:
243 isram_memcpy((void *)location
, &value
, size
);
246 pr_err("invalid relocation for %#lx\n", location
);
255 module_finalize(const Elf_Ehdr
* hdr
,
256 const Elf_Shdr
* sechdrs
, struct module
*mod
)
258 unsigned int i
, strindex
= 0, symindex
= 0;
262 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
264 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
265 /* Internal symbols and strings. */
266 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
268 strindex
= sechdrs
[i
].sh_link
;
272 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
273 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
274 unsigned int info
= sechdrs
[i
].sh_info
;
275 const char *shname
= secstrings
+ sechdrs
[i
].sh_name
;
277 /* Not a valid relocation section? */
278 if (info
>= hdr
->e_shnum
)
281 /* Only support RELA relocation types */
282 if (sechdrs
[i
].sh_type
!= SHT_RELA
)
285 if (!strcmp(".rela.l2.text", shname
) ||
286 !strcmp(".rela.l1.text", shname
) ||
287 (!strcmp(".rela.text", shname
) &&
288 (hdr
->e_flags
& (EF_BFIN_CODE_IN_L1
| EF_BFIN_CODE_IN_L2
)))) {
290 err
= apply_relocate_add((Elf_Shdr
*) sechdrs
, strtab
,
300 void module_arch_cleanup(struct module
*mod
)
302 l1_inst_sram_free(mod
->arch
.text_l1
);
303 l1_data_A_sram_free(mod
->arch
.data_a_l1
);
304 l1_data_A_sram_free(mod
->arch
.bss_a_l1
);
305 l1_data_B_sram_free(mod
->arch
.data_b_l1
);
306 l1_data_B_sram_free(mod
->arch
.bss_b_l1
);
307 l2_sram_free(mod
->arch
.text_l2
);
308 l2_sram_free(mod
->arch
.data_l2
);
309 l2_sram_free(mod
->arch
.bss_l2
);