2 * Copyright 2004-2009 Analog Devices Inc.
4 * Licensed under the GPL-2 or later
7 #include <linux/moduleloader.h>
9 #include <linux/vmalloc.h>
11 #include <linux/string.h>
12 #include <linux/kernel.h>
14 #include <asm/cacheflush.h>
15 #include <linux/uaccess.h>
17 #define mod_err(mod, fmt, ...) \
18 pr_err("module %s: " fmt, (mod)->name, ##__VA_ARGS__)
19 #define mod_debug(mod, fmt, ...) \
20 pr_debug("module %s: " fmt, (mod)->name, ##__VA_ARGS__)
22 /* Transfer the section to the L1 memory */
24 module_frob_arch_sections(Elf_Ehdr
*hdr
, Elf_Shdr
*sechdrs
,
25 char *secstrings
, struct module
*mod
)
28 * XXX: sechdrs are vmalloced in kernel/module.c
29 * and would be vfreed just after module is loaded,
30 * so we hack to keep the only information we needed
31 * in mod->arch to correctly free L1 I/D sram later.
32 * NOTE: this breaks the semantic of mod->arch structure.
34 Elf_Shdr
*s
, *sechdrs_end
= sechdrs
+ hdr
->e_shnum
;
37 for (s
= sechdrs
; s
< sechdrs_end
; ++s
) {
38 const char *shname
= secstrings
+ s
->sh_name
;
43 if (!strcmp(".l1.text", shname
) ||
44 (!strcmp(".text", shname
) &&
45 (hdr
->e_flags
& EF_BFIN_CODE_IN_L1
))) {
47 dest
= l1_inst_sram_alloc(s
->sh_size
);
48 mod
->arch
.text_l1
= dest
;
50 mod_err(mod
, "L1 inst memory allocation failed\n");
53 dma_memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
55 } else if (!strcmp(".l1.data", shname
) ||
56 (!strcmp(".data", shname
) &&
57 (hdr
->e_flags
& EF_BFIN_DATA_IN_L1
))) {
59 dest
= l1_data_sram_alloc(s
->sh_size
);
60 mod
->arch
.data_a_l1
= dest
;
62 mod_err(mod
, "L1 data memory allocation failed\n");
65 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
67 } else if (!strcmp(".l1.bss", shname
) ||
68 (!strcmp(".bss", shname
) &&
69 (hdr
->e_flags
& EF_BFIN_DATA_IN_L1
))) {
71 dest
= l1_data_sram_zalloc(s
->sh_size
);
72 mod
->arch
.bss_a_l1
= dest
;
74 mod_err(mod
, "L1 data memory allocation failed\n");
78 } else if (!strcmp(".l1.data.B", shname
)) {
80 dest
= l1_data_B_sram_alloc(s
->sh_size
);
81 mod
->arch
.data_b_l1
= dest
;
83 mod_err(mod
, "L1 data memory allocation failed\n");
86 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
88 } else if (!strcmp(".l1.bss.B", shname
)) {
90 dest
= l1_data_B_sram_alloc(s
->sh_size
);
91 mod
->arch
.bss_b_l1
= dest
;
93 mod_err(mod
, "L1 data memory allocation failed\n");
96 memset(dest
, 0, s
->sh_size
);
98 } else if (!strcmp(".l2.text", shname
) ||
99 (!strcmp(".text", shname
) &&
100 (hdr
->e_flags
& EF_BFIN_CODE_IN_L2
))) {
102 dest
= l2_sram_alloc(s
->sh_size
);
103 mod
->arch
.text_l2
= dest
;
105 mod_err(mod
, "L2 SRAM allocation failed\n");
108 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
110 } else if (!strcmp(".l2.data", shname
) ||
111 (!strcmp(".data", shname
) &&
112 (hdr
->e_flags
& EF_BFIN_DATA_IN_L2
))) {
114 dest
= l2_sram_alloc(s
->sh_size
);
115 mod
->arch
.data_l2
= dest
;
117 mod_err(mod
, "L2 SRAM allocation failed\n");
120 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
122 } else if (!strcmp(".l2.bss", shname
) ||
123 (!strcmp(".bss", shname
) &&
124 (hdr
->e_flags
& EF_BFIN_DATA_IN_L2
))) {
126 dest
= l2_sram_zalloc(s
->sh_size
);
127 mod
->arch
.bss_l2
= dest
;
129 mod_err(mod
, "L2 SRAM allocation failed\n");
136 s
->sh_flags
&= ~SHF_ALLOC
;
137 s
->sh_addr
= (unsigned long)dest
;
143 /*************************************************************************/
144 /* FUNCTION : apply_relocate_add */
145 /* ABSTRACT : Blackfin specific relocation handling for the loadable */
146 /* modules. Modules are expected to be .o files. */
147 /* Arithmetic relocations are handled. */
148 /* We do not expect LSETUP to be split and hence is not */
150 /* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
151 /* gas does not generate it. */
152 /*************************************************************************/
154 apply_relocate_add(Elf_Shdr
*sechdrs
, const char *strtab
,
155 unsigned int symindex
, unsigned int relsec
,
159 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
161 unsigned long location
, value
, size
;
163 mod_debug(mod
, "applying relocate section %u to %u\n",
164 relsec
, sechdrs
[relsec
].sh_info
);
166 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
167 /* This is where to make the change */
168 location
= sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
+
171 /* This is the symbol it is referring to. Note that all
172 undefined symbols have been resolved. */
173 sym
= (Elf32_Sym
*) sechdrs
[symindex
].sh_addr
174 + ELF32_R_SYM(rel
[i
].r_info
);
175 value
= sym
->st_value
;
176 value
+= rel
[i
].r_addend
;
179 if (location
>= COREB_L1_DATA_A_START
) {
180 mod_err(mod
, "cannot relocate in L1: %u (SMP kernel)\n",
181 ELF32_R_TYPE(rel
[i
].r_info
));
186 mod_debug(mod
, "location is %lx, value is %lx type is %d\n",
187 location
, value
, ELF32_R_TYPE(rel
[i
].r_info
));
189 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
197 case R_BFIN_BYTE4_DATA
:
202 case R_BFIN_PCREL24_JUMP_L
:
203 case R_BFIN_PCREL12_JUMP
:
204 case R_BFIN_PCREL12_JUMP_S
:
206 mod_err(mod
, "unsupported relocation: %u (no -mlong-calls?)\n",
207 ELF32_R_TYPE(rel
[i
].r_info
));
211 mod_err(mod
, "unknown relocation: %u\n",
212 ELF32_R_TYPE(rel
[i
].r_info
));
216 switch (bfin_mem_access_type(location
, size
)) {
217 case BFIN_MEM_ACCESS_CORE
:
218 case BFIN_MEM_ACCESS_CORE_ONLY
:
219 memcpy((void *)location
, &value
, size
);
221 case BFIN_MEM_ACCESS_DMA
:
222 dma_memcpy((void *)location
, &value
, size
);
224 case BFIN_MEM_ACCESS_ITEST
:
225 isram_memcpy((void *)location
, &value
, size
);
228 mod_err(mod
, "invalid relocation for %#lx\n", location
);
237 module_finalize(const Elf_Ehdr
* hdr
,
238 const Elf_Shdr
* sechdrs
, struct module
*mod
)
240 unsigned int i
, strindex
= 0, symindex
= 0;
244 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
246 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
247 /* Internal symbols and strings. */
248 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
250 strindex
= sechdrs
[i
].sh_link
;
254 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
255 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
256 unsigned int info
= sechdrs
[i
].sh_info
;
257 const char *shname
= secstrings
+ sechdrs
[i
].sh_name
;
259 /* Not a valid relocation section? */
260 if (info
>= hdr
->e_shnum
)
263 /* Only support RELA relocation types */
264 if (sechdrs
[i
].sh_type
!= SHT_RELA
)
267 if (!strcmp(".rela.l2.text", shname
) ||
268 !strcmp(".rela.l1.text", shname
) ||
269 (!strcmp(".rela.text", shname
) &&
270 (hdr
->e_flags
& (EF_BFIN_CODE_IN_L1
| EF_BFIN_CODE_IN_L2
)))) {
272 err
= apply_relocate_add((Elf_Shdr
*) sechdrs
, strtab
,
282 void module_arch_cleanup(struct module
*mod
)
284 l1_inst_sram_free(mod
->arch
.text_l1
);
285 l1_data_A_sram_free(mod
->arch
.data_a_l1
);
286 l1_data_A_sram_free(mod
->arch
.bss_a_l1
);
287 l1_data_B_sram_free(mod
->arch
.data_b_l1
);
288 l1_data_B_sram_free(mod
->arch
.bss_b_l1
);
289 l2_sram_free(mod
->arch
.text_l2
);
290 l2_sram_free(mod
->arch
.data_l2
);
291 l2_sram_free(mod
->arch
.bss_l2
);