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>
40 void *module_alloc(unsigned long size
)
47 /* Free memory returned from module_alloc */
48 void module_free(struct module
*mod
, void *module_region
)
53 /* Transfer the section to the L1 memory */
55 module_frob_arch_sections(Elf_Ehdr
* hdr
, Elf_Shdr
* sechdrs
,
56 char *secstrings
, struct module
*mod
)
59 * XXX: sechdrs are vmalloced in kernel/module.c
60 * and would be vfreed just after module is loaded,
61 * so we hack to keep the only information we needed
62 * in mod->arch to correctly free L1 I/D sram later.
63 * NOTE: this breaks the semantic of mod->arch structure.
65 Elf_Shdr
*s
, *sechdrs_end
= sechdrs
+ hdr
->e_shnum
;
68 for (s
= sechdrs
; s
< sechdrs_end
; ++s
) {
69 if ((strcmp(".l1.text", secstrings
+ s
->sh_name
) == 0) ||
70 ((strcmp(".text", secstrings
+ s
->sh_name
) == 0) &&
71 (hdr
->e_flags
& EF_BFIN_CODE_IN_L1
) && (s
->sh_size
> 0))) {
72 dest
= l1_inst_sram_alloc(s
->sh_size
);
73 mod
->arch
.text_l1
= dest
;
76 "module %s: L1 instruction memory allocation failed\n",
80 dma_memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
81 s
->sh_flags
&= ~SHF_ALLOC
;
82 s
->sh_addr
= (unsigned long)dest
;
84 if ((strcmp(".l1.data", secstrings
+ s
->sh_name
) == 0) ||
85 ((strcmp(".data", secstrings
+ s
->sh_name
) == 0) &&
86 (hdr
->e_flags
& EF_BFIN_DATA_IN_L1
) && (s
->sh_size
> 0))) {
87 dest
= l1_data_sram_alloc(s
->sh_size
);
88 mod
->arch
.data_a_l1
= dest
;
91 "module %s: L1 data memory allocation failed\n",
95 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
96 s
->sh_flags
&= ~SHF_ALLOC
;
97 s
->sh_addr
= (unsigned long)dest
;
99 if (strcmp(".l1.bss", secstrings
+ s
->sh_name
) == 0 ||
100 ((strcmp(".bss", secstrings
+ s
->sh_name
) == 0) &&
101 (hdr
->e_flags
& EF_BFIN_DATA_IN_L1
) && (s
->sh_size
> 0))) {
102 dest
= l1_data_sram_alloc(s
->sh_size
);
103 mod
->arch
.bss_a_l1
= dest
;
106 "module %s: L1 data memory allocation failed\n",
110 memset(dest
, 0, s
->sh_size
);
111 s
->sh_flags
&= ~SHF_ALLOC
;
112 s
->sh_addr
= (unsigned long)dest
;
114 if (strcmp(".l1.data.B", secstrings
+ s
->sh_name
) == 0) {
115 dest
= l1_data_B_sram_alloc(s
->sh_size
);
116 mod
->arch
.data_b_l1
= dest
;
119 "module %s: L1 data memory allocation failed\n",
123 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
124 s
->sh_flags
&= ~SHF_ALLOC
;
125 s
->sh_addr
= (unsigned long)dest
;
127 if (strcmp(".l1.bss.B", secstrings
+ s
->sh_name
) == 0) {
128 dest
= l1_data_B_sram_alloc(s
->sh_size
);
129 mod
->arch
.bss_b_l1
= dest
;
132 "module %s: L1 data memory allocation failed\n",
136 memset(dest
, 0, s
->sh_size
);
137 s
->sh_flags
&= ~SHF_ALLOC
;
138 s
->sh_addr
= (unsigned long)dest
;
140 if ((strcmp(".l2.text", secstrings
+ s
->sh_name
) == 0) ||
141 ((strcmp(".text", secstrings
+ s
->sh_name
) == 0) &&
142 (hdr
->e_flags
& EF_BFIN_CODE_IN_L2
) && (s
->sh_size
> 0))) {
143 dest
= l2_sram_alloc(s
->sh_size
);
144 mod
->arch
.text_l2
= dest
;
147 "module %s: L2 SRAM allocation failed\n",
151 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
152 s
->sh_flags
&= ~SHF_ALLOC
;
153 s
->sh_addr
= (unsigned long)dest
;
155 if ((strcmp(".l2.data", secstrings
+ s
->sh_name
) == 0) ||
156 ((strcmp(".data", secstrings
+ s
->sh_name
) == 0) &&
157 (hdr
->e_flags
& EF_BFIN_DATA_IN_L2
) && (s
->sh_size
> 0))) {
158 dest
= l2_sram_alloc(s
->sh_size
);
159 mod
->arch
.data_l2
= dest
;
162 "module %s: L2 SRAM allocation failed\n",
166 memcpy(dest
, (void *)s
->sh_addr
, s
->sh_size
);
167 s
->sh_flags
&= ~SHF_ALLOC
;
168 s
->sh_addr
= (unsigned long)dest
;
170 if (strcmp(".l2.bss", secstrings
+ s
->sh_name
) == 0 ||
171 ((strcmp(".bss", secstrings
+ s
->sh_name
) == 0) &&
172 (hdr
->e_flags
& EF_BFIN_DATA_IN_L2
) && (s
->sh_size
> 0))) {
173 dest
= l2_sram_alloc(s
->sh_size
);
174 mod
->arch
.bss_l2
= dest
;
177 "module %s: L2 SRAM allocation failed\n",
181 memset(dest
, 0, s
->sh_size
);
182 s
->sh_flags
&= ~SHF_ALLOC
;
183 s
->sh_addr
= (unsigned long)dest
;
190 apply_relocate(Elf_Shdr
* sechdrs
, const char *strtab
,
191 unsigned int symindex
, unsigned int relsec
, struct module
*me
)
193 printk(KERN_ERR
"module %s: .rel unsupported\n", me
->name
);
197 /*************************************************************************/
198 /* FUNCTION : apply_relocate_add */
199 /* ABSTRACT : Blackfin specific relocation handling for the loadable */
200 /* modules. Modules are expected to be .o files. */
201 /* Arithmetic relocations are handled. */
202 /* We do not expect LSETUP to be split and hence is not */
204 /* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
205 /* gas does not generate it. */
206 /*************************************************************************/
208 apply_relocate_add(Elf_Shdr
* sechdrs
, const char *strtab
,
209 unsigned int symindex
, unsigned int relsec
,
214 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
216 uint32_t *location32
;
217 uint16_t *location16
;
220 pr_debug("Applying relocate section %u to %u\n", relsec
,
221 sechdrs
[relsec
].sh_info
);
222 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
223 /* This is where to make the change */
225 (uint16_t *) (sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
+
227 location32
= (uint32_t *) location16
;
228 /* This is the symbol it is referring to. Note that all
229 undefined symbols have been resolved. */
230 sym
= (Elf32_Sym
*) sechdrs
[symindex
].sh_addr
231 + ELF32_R_SYM(rel
[i
].r_info
);
232 value
= sym
->st_value
;
233 value
+= rel
[i
].r_addend
;
234 pr_debug("location is %x, value is %x type is %d \n",
235 (unsigned int) location32
, value
,
236 ELF32_R_TYPE(rel
[i
].r_info
));
238 if ((unsigned long)location16
>= COREB_L1_DATA_A_START
) {
239 printk(KERN_ERR
"module %s: cannot relocate in L1: %u (SMP kernel)",
240 mod
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
244 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
247 case R_BFIN_PCREL24_JUMP_L
:
248 /* Add the value, subtract its postition */
250 (uint16_t *) (sechdrs
[sechdrs
[relsec
].sh_info
].
251 sh_addr
+ rel
[i
].r_offset
- 2);
252 location32
= (uint32_t *) location16
;
253 value
-= (uint32_t) location32
;
255 if ((value
& 0xFF000000) != 0 &&
256 (value
& 0xFF000000) != 0xFF000000) {
257 printk(KERN_ERR
"module %s: relocation overflow\n",
261 pr_debug("value is %x, before %x-%x after %x-%x\n", value
,
262 *location16
, *(location16
+ 1),
263 (*location16
& 0xff00) | (value
>> 16 & 0x00ff),
266 (*location16
& 0xff00) | (value
>> 16 & 0x00ff);
267 *(location16
+ 1) = value
& 0xffff;
269 case R_BFIN_PCREL12_JUMP
:
270 case R_BFIN_PCREL12_JUMP_S
:
271 value
-= (uint32_t) location32
;
273 *location16
= (value
& 0xfff);
276 value
-= (uint32_t) location32
;
278 *location16
= (value
& 0x3ff);
281 pr_debug("before %x after %x\n", *location16
,
283 tmp
= (value
& 0xffff);
284 if ((unsigned long)location16
>= L1_CODE_START
) {
285 dma_memcpy(location16
, &tmp
, 2);
290 pr_debug("before %x after %x\n", *location16
,
291 ((value
>> 16) & 0xffff));
292 tmp
= ((value
>> 16) & 0xffff);
293 if ((unsigned long)location16
>= L1_CODE_START
) {
294 dma_memcpy(location16
, &tmp
, 2);
299 *location16
= (value
& 0xffff);
301 case R_BFIN_BYTE4_DATA
:
302 pr_debug("before %x after %x\n", *location32
, value
);
306 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
307 mod
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
315 module_finalize(const Elf_Ehdr
* hdr
,
316 const Elf_Shdr
* sechdrs
, struct module
*mod
)
318 unsigned int i
, strindex
= 0, symindex
= 0;
322 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
324 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
325 /* Internal symbols and strings. */
326 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
328 strindex
= sechdrs
[i
].sh_link
;
332 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
333 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
334 unsigned int info
= sechdrs
[i
].sh_info
;
336 /* Not a valid relocation section? */
337 if (info
>= hdr
->e_shnum
)
340 if ((sechdrs
[i
].sh_type
== SHT_RELA
) &&
341 ((strcmp(".rela.l2.text", secstrings
+ sechdrs
[i
].sh_name
) == 0) ||
342 (strcmp(".rela.l1.text", secstrings
+ sechdrs
[i
].sh_name
) == 0) ||
343 ((strcmp(".rela.text", secstrings
+ sechdrs
[i
].sh_name
) == 0) &&
344 (hdr
->e_flags
& (EF_BFIN_CODE_IN_L1
|EF_BFIN_CODE_IN_L2
))))) {
345 err
= apply_relocate_add((Elf_Shdr
*) sechdrs
, strtab
,
354 void module_arch_cleanup(struct module
*mod
)
356 l1_inst_sram_free(mod
->arch
.text_l1
);
357 l1_data_A_sram_free(mod
->arch
.data_a_l1
);
358 l1_data_A_sram_free(mod
->arch
.bss_a_l1
);
359 l1_data_B_sram_free(mod
->arch
.data_b_l1
);
360 l1_data_B_sram_free(mod
->arch
.bss_b_l1
);
361 l2_sram_free(mod
->arch
.text_l2
);
362 l2_sram_free(mod
->arch
.data_l2
);
363 l2_sram_free(mod
->arch
.bss_l2
);