blob: 4489efc528837f9bf6fd3e7bbb4ae94743906b09 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * Copyright 2004-2009 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Licensed under the GPL-2 or later
Bryan Wu1394f032007-05-06 14:50:22 -07005 */
6
Joe Perchesb75a9e62010-10-20 11:11:51 -07007#define pr_fmt(fmt) "module %s: " fmt, mod->name
Bryan Wu1394f032007-05-06 14:50:22 -07008
9#include <linux/moduleloader.h>
10#include <linux/elf.h>
11#include <linux/vmalloc.h>
12#include <linux/fs.h>
13#include <linux/string.h>
14#include <linux/kernel.h>
15#include <asm/dma.h>
16#include <asm/cacheflush.h>
Mike Frysingera7690942009-06-26 00:49:51 +000017#include <asm/uaccess.h>
Bryan Wu1394f032007-05-06 14:50:22 -070018
Bryan Wu1394f032007-05-06 14:50:22 -070019/* Transfer the section to the L1 memory */
20int
Mike Frysinger459fec92009-06-26 00:48:33 +000021module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
Bryan Wu1394f032007-05-06 14:50:22 -070022 char *secstrings, struct module *mod)
23{
Meihui Fan96a87e22008-05-07 11:41:26 +080024 /*
25 * XXX: sechdrs are vmalloced in kernel/module.c
26 * and would be vfreed just after module is loaded,
27 * so we hack to keep the only information we needed
28 * in mod->arch to correctly free L1 I/D sram later.
29 * NOTE: this breaks the semantic of mod->arch structure.
30 */
Bryan Wu1394f032007-05-06 14:50:22 -070031 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
Mike Frysinger459fec92009-06-26 00:48:33 +000032 void *dest;
Bryan Wu1394f032007-05-06 14:50:22 -070033
34 for (s = sechdrs; s < sechdrs_end; ++s) {
Mike Frysinger459fec92009-06-26 00:48:33 +000035 const char *shname = secstrings + s->sh_name;
36
37 if (s->sh_size == 0)
38 continue;
39
40 if (!strcmp(".l1.text", shname) ||
41 (!strcmp(".text", shname) &&
42 (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
43
Bryan Wu1394f032007-05-06 14:50:22 -070044 dest = l1_inst_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080045 mod->arch.text_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070046 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070047 pr_err("L1 inst memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070048 return -1;
49 }
50 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000051
52 } else if (!strcmp(".l1.data", shname) ||
53 (!strcmp(".data", shname) &&
54 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
55
Bryan Wu1394f032007-05-06 14:50:22 -070056 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080057 mod->arch.data_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070058 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070059 pr_err("L1 data memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070060 return -1;
61 }
62 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000063
64 } else if (!strcmp(".l1.bss", shname) ||
65 (!strcmp(".bss", shname) &&
66 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
67
Mike Frysinger70deca92009-06-26 00:37:40 +000068 dest = l1_data_sram_zalloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080069 mod->arch.bss_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070070 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070071 pr_err("L1 data memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070072 return -1;
73 }
Mike Frysinger459fec92009-06-26 00:48:33 +000074
75 } else if (!strcmp(".l1.data.B", shname)) {
76
Bryan Wu1394f032007-05-06 14:50:22 -070077 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080078 mod->arch.data_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070079 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070080 pr_err("L1 data memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070081 return -1;
82 }
83 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000084
85 } else if (!strcmp(".l1.bss.B", shname)) {
86
Bryan Wu1394f032007-05-06 14:50:22 -070087 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080088 mod->arch.bss_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070089 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070090 pr_err("L1 data memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070091 return -1;
92 }
93 memset(dest, 0, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000094
95 } else if (!strcmp(".l2.text", shname) ||
96 (!strcmp(".text", shname) &&
97 (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
98
Sonic Zhang262c3822008-07-19 15:42:41 +080099 dest = l2_sram_alloc(s->sh_size);
100 mod->arch.text_l2 = dest;
101 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700102 pr_err("L2 SRAM allocation failed\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800103 return -1;
104 }
105 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000106
107 } else if (!strcmp(".l2.data", shname) ||
108 (!strcmp(".data", shname) &&
109 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
110
Sonic Zhang262c3822008-07-19 15:42:41 +0800111 dest = l2_sram_alloc(s->sh_size);
112 mod->arch.data_l2 = dest;
113 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700114 pr_err("L2 SRAM allocation failed\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800115 return -1;
116 }
117 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000118
119 } else if (!strcmp(".l2.bss", shname) ||
120 (!strcmp(".bss", shname) &&
121 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
122
Mike Frysinger70deca92009-06-26 00:37:40 +0000123 dest = l2_sram_zalloc(s->sh_size);
Sonic Zhang262c3822008-07-19 15:42:41 +0800124 mod->arch.bss_l2 = dest;
125 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700126 pr_err("L2 SRAM allocation failed\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800127 return -1;
128 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000129
130 } else
131 continue;
132
133 s->sh_flags &= ~SHF_ALLOC;
134 s->sh_addr = (unsigned long)dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700135 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000136
Bryan Wu1394f032007-05-06 14:50:22 -0700137 return 0;
138}
139
Bryan Wu1394f032007-05-06 14:50:22 -0700140/*************************************************************************/
141/* FUNCTION : apply_relocate_add */
142/* ABSTRACT : Blackfin specific relocation handling for the loadable */
143/* modules. Modules are expected to be .o files. */
144/* Arithmetic relocations are handled. */
145/* We do not expect LSETUP to be split and hence is not */
146/* handled. */
Mike Frysinger595d6812009-06-02 00:35:33 +0000147/* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
148/* gas does not generate it. */
Bryan Wu1394f032007-05-06 14:50:22 -0700149/*************************************************************************/
150int
Mike Frysingera7690942009-06-26 00:49:51 +0000151apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700152 unsigned int symindex, unsigned int relsec,
153 struct module *mod)
154{
155 unsigned int i;
Bryan Wu1394f032007-05-06 14:50:22 -0700156 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
157 Elf32_Sym *sym;
Mike Frysingera7690942009-06-26 00:49:51 +0000158 unsigned long location, value, size;
Bryan Wu1394f032007-05-06 14:50:22 -0700159
Joe Perchesb75a9e62010-10-20 11:11:51 -0700160 pr_debug("applying relocate section %u to %u\n",
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000161 relsec, sechdrs[relsec].sh_info);
Mike Frysingera7690942009-06-26 00:49:51 +0000162
Bryan Wu1394f032007-05-06 14:50:22 -0700163 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
164 /* This is where to make the change */
Mike Frysingera7690942009-06-26 00:49:51 +0000165 location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
166 rel[i].r_offset;
167
Bryan Wu1394f032007-05-06 14:50:22 -0700168 /* This is the symbol it is referring to. Note that all
169 undefined symbols have been resolved. */
170 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
171 + ELF32_R_SYM(rel[i].r_info);
Bernd Schmidtd1a85302009-01-07 23:14:39 +0800172 value = sym->st_value;
Bryan Wu1394f032007-05-06 14:50:22 -0700173 value += rel[i].r_addend;
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000174
Graf Yang8f658732008-11-18 17:48:22 +0800175#ifdef CONFIG_SMP
Mike Frysingera7690942009-06-26 00:49:51 +0000176 if (location >= COREB_L1_DATA_A_START) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700177 pr_err("cannot relocate in L1: %u (SMP kernel)\n",
178 ELF32_R_TYPE(rel[i].r_info));
Graf Yang8f658732008-11-18 17:48:22 +0800179 return -ENOEXEC;
180 }
181#endif
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000182
Mike Frysingera7690942009-06-26 00:49:51 +0000183 pr_debug("location is %lx, value is %lx type is %d\n",
Joe Perchesb75a9e62010-10-20 11:11:51 -0700184 location, value, ELF32_R_TYPE(rel[i].r_info));
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000185
Bryan Wu1394f032007-05-06 14:50:22 -0700186 switch (ELF32_R_TYPE(rel[i].r_info)) {
187
Mike Frysinger595d6812009-06-02 00:35:33 +0000188 case R_BFIN_HUIMM16:
Mike Frysingera7690942009-06-26 00:49:51 +0000189 value >>= 16;
190 case R_BFIN_LUIMM16:
Mike Frysinger595d6812009-06-02 00:35:33 +0000191 case R_BFIN_RIMM16:
Mike Frysingera7690942009-06-26 00:49:51 +0000192 size = 2;
Bryan Wu1394f032007-05-06 14:50:22 -0700193 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000194 case R_BFIN_BYTE4_DATA:
Mike Frysingera7690942009-06-26 00:49:51 +0000195 size = 4;
Bryan Wu1394f032007-05-06 14:50:22 -0700196 break;
Mike Frysingera7690942009-06-26 00:49:51 +0000197
Robin Getz22532572009-06-25 15:49:38 +0000198 case R_BFIN_PCREL24:
199 case R_BFIN_PCREL24_JUMP_L:
200 case R_BFIN_PCREL12_JUMP:
201 case R_BFIN_PCREL12_JUMP_S:
202 case R_BFIN_PCREL10:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000203 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
Joe Perchesb75a9e62010-10-20 11:11:51 -0700204 ELF32_R_TYPE(rel[i].r_info));
Robin Getz22532572009-06-25 15:49:38 +0000205 return -ENOEXEC;
Mike Frysingera7690942009-06-26 00:49:51 +0000206
Bryan Wu1394f032007-05-06 14:50:22 -0700207 default:
Joe Perchesb75a9e62010-10-20 11:11:51 -0700208 pr_err("unknown relocation: %u\n",
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000209 ELF32_R_TYPE(rel[i].r_info));
Bryan Wu1394f032007-05-06 14:50:22 -0700210 return -ENOEXEC;
211 }
Mike Frysingera7690942009-06-26 00:49:51 +0000212
213 switch (bfin_mem_access_type(location, size)) {
214 case BFIN_MEM_ACCESS_CORE:
215 case BFIN_MEM_ACCESS_CORE_ONLY:
216 memcpy((void *)location, &value, size);
217 break;
218 case BFIN_MEM_ACCESS_DMA:
219 dma_memcpy((void *)location, &value, size);
220 break;
221 case BFIN_MEM_ACCESS_ITEST:
222 isram_memcpy((void *)location, &value, size);
223 break;
224 default:
Joe Perchesb75a9e62010-10-20 11:11:51 -0700225 pr_err("invalid relocation for %#lx\n", location);
Mike Frysingera7690942009-06-26 00:49:51 +0000226 return -ENOEXEC;
227 }
Bryan Wu1394f032007-05-06 14:50:22 -0700228 }
Mike Frysingera7690942009-06-26 00:49:51 +0000229
Bryan Wu1394f032007-05-06 14:50:22 -0700230 return 0;
231}
232
233int
234module_finalize(const Elf_Ehdr * hdr,
235 const Elf_Shdr * sechdrs, struct module *mod)
236{
237 unsigned int i, strindex = 0, symindex = 0;
238 char *secstrings;
Graf Yang8f658732008-11-18 17:48:22 +0800239 long err = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700240
241 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
242
243 for (i = 1; i < hdr->e_shnum; i++) {
244 /* Internal symbols and strings. */
245 if (sechdrs[i].sh_type == SHT_SYMTAB) {
246 symindex = i;
247 strindex = sechdrs[i].sh_link;
248 }
249 }
250
251 for (i = 1; i < hdr->e_shnum; i++) {
252 const char *strtab = (char *)sechdrs[strindex].sh_addr;
253 unsigned int info = sechdrs[i].sh_info;
Mike Frysinger459fec92009-06-26 00:48:33 +0000254 const char *shname = secstrings + sechdrs[i].sh_name;
Bryan Wu1394f032007-05-06 14:50:22 -0700255
256 /* Not a valid relocation section? */
257 if (info >= hdr->e_shnum)
258 continue;
259
Mike Frysinger459fec92009-06-26 00:48:33 +0000260 /* Only support RELA relocation types */
261 if (sechdrs[i].sh_type != SHT_RELA)
262 continue;
263
264 if (!strcmp(".rela.l2.text", shname) ||
265 !strcmp(".rela.l1.text", shname) ||
266 (!strcmp(".rela.text", shname) &&
267 (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
268
Graf Yang8f658732008-11-18 17:48:22 +0800269 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700270 symindex, i, mod);
Graf Yang8f658732008-11-18 17:48:22 +0800271 if (err < 0)
272 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700273 }
274 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000275
Bryan Wu1394f032007-05-06 14:50:22 -0700276 return 0;
277}
278
279void module_arch_cleanup(struct module *mod)
280{
Sonic Zhang262c3822008-07-19 15:42:41 +0800281 l1_inst_sram_free(mod->arch.text_l1);
282 l1_data_A_sram_free(mod->arch.data_a_l1);
283 l1_data_A_sram_free(mod->arch.bss_a_l1);
284 l1_data_B_sram_free(mod->arch.data_b_l1);
285 l1_data_B_sram_free(mod->arch.bss_b_l1);
286 l2_sram_free(mod->arch.text_l2);
287 l2_sram_free(mod->arch.data_l2);
288 l2_sram_free(mod->arch.bss_l2);
Bryan Wu1394f032007-05-06 14:50:22 -0700289}