module.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright 2004-2009 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2 or later
  5. */
  6. #define pr_fmt(fmt) "module %s: " fmt
  7. #include <linux/moduleloader.h>
  8. #include <linux/elf.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/fs.h>
  11. #include <linux/string.h>
  12. #include <linux/kernel.h>
  13. #include <asm/dma.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/uaccess.h>
  16. void *module_alloc(unsigned long size)
  17. {
  18. if (size == 0)
  19. return NULL;
  20. return vmalloc(size);
  21. }
  22. /* Free memory returned from module_alloc */
  23. void module_free(struct module *mod, void *module_region)
  24. {
  25. vfree(module_region);
  26. }
  27. /* Transfer the section to the L1 memory */
  28. int
  29. module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  30. char *secstrings, struct module *mod)
  31. {
  32. /*
  33. * XXX: sechdrs are vmalloced in kernel/module.c
  34. * and would be vfreed just after module is loaded,
  35. * so we hack to keep the only information we needed
  36. * in mod->arch to correctly free L1 I/D sram later.
  37. * NOTE: this breaks the semantic of mod->arch structure.
  38. */
  39. Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
  40. void *dest;
  41. for (s = sechdrs; s < sechdrs_end; ++s) {
  42. const char *shname = secstrings + s->sh_name;
  43. if (s->sh_size == 0)
  44. continue;
  45. if (!strcmp(".l1.text", shname) ||
  46. (!strcmp(".text", shname) &&
  47. (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
  48. dest = l1_inst_sram_alloc(s->sh_size);
  49. mod->arch.text_l1 = dest;
  50. if (dest == NULL) {
  51. pr_err("L1 inst memory allocation failed\n",
  52. mod->name);
  53. return -1;
  54. }
  55. dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
  56. } else if (!strcmp(".l1.data", shname) ||
  57. (!strcmp(".data", shname) &&
  58. (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
  59. dest = l1_data_sram_alloc(s->sh_size);
  60. mod->arch.data_a_l1 = dest;
  61. if (dest == NULL) {
  62. pr_err("L1 data memory allocation failed\n",
  63. mod->name);
  64. return -1;
  65. }
  66. 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))) {
  70. dest = l1_data_sram_zalloc(s->sh_size);
  71. mod->arch.bss_a_l1 = dest;
  72. if (dest == NULL) {
  73. pr_err("L1 data memory allocation failed\n",
  74. mod->name);
  75. return -1;
  76. }
  77. } else if (!strcmp(".l1.data.B", shname)) {
  78. dest = l1_data_B_sram_alloc(s->sh_size);
  79. mod->arch.data_b_l1 = dest;
  80. if (dest == NULL) {
  81. pr_err("L1 data memory allocation failed\n",
  82. mod->name);
  83. return -1;
  84. }
  85. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  86. } else if (!strcmp(".l1.bss.B", shname)) {
  87. dest = l1_data_B_sram_alloc(s->sh_size);
  88. mod->arch.bss_b_l1 = dest;
  89. if (dest == NULL) {
  90. pr_err("L1 data memory allocation failed\n",
  91. mod->name);
  92. return -1;
  93. }
  94. memset(dest, 0, s->sh_size);
  95. } else if (!strcmp(".l2.text", shname) ||
  96. (!strcmp(".text", shname) &&
  97. (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
  98. dest = l2_sram_alloc(s->sh_size);
  99. mod->arch.text_l2 = dest;
  100. if (dest == NULL) {
  101. pr_err("L2 SRAM allocation failed\n",
  102. mod->name);
  103. return -1;
  104. }
  105. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  106. } else if (!strcmp(".l2.data", shname) ||
  107. (!strcmp(".data", shname) &&
  108. (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
  109. dest = l2_sram_alloc(s->sh_size);
  110. mod->arch.data_l2 = dest;
  111. if (dest == NULL) {
  112. pr_err("L2 SRAM allocation failed\n",
  113. mod->name);
  114. return -1;
  115. }
  116. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  117. } else if (!strcmp(".l2.bss", shname) ||
  118. (!strcmp(".bss", shname) &&
  119. (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
  120. dest = l2_sram_zalloc(s->sh_size);
  121. mod->arch.bss_l2 = dest;
  122. if (dest == NULL) {
  123. pr_err("L2 SRAM allocation failed\n",
  124. mod->name);
  125. return -1;
  126. }
  127. } else
  128. continue;
  129. s->sh_flags &= ~SHF_ALLOC;
  130. s->sh_addr = (unsigned long)dest;
  131. }
  132. return 0;
  133. }
  134. int
  135. apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
  136. unsigned int symindex, unsigned int relsec, struct module *me)
  137. {
  138. pr_err(".rel unsupported\n", me->name);
  139. return -ENOEXEC;
  140. }
  141. /*************************************************************************/
  142. /* FUNCTION : apply_relocate_add */
  143. /* ABSTRACT : Blackfin specific relocation handling for the loadable */
  144. /* modules. Modules are expected to be .o files. */
  145. /* Arithmetic relocations are handled. */
  146. /* We do not expect LSETUP to be split and hence is not */
  147. /* handled. */
  148. /* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
  149. /* gas does not generate it. */
  150. /*************************************************************************/
  151. int
  152. apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  153. unsigned int symindex, unsigned int relsec,
  154. struct module *mod)
  155. {
  156. unsigned int i;
  157. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  158. Elf32_Sym *sym;
  159. unsigned long location, value, size;
  160. pr_debug("applying relocate section %u to %u\n", mod->name,
  161. relsec, sechdrs[relsec].sh_info);
  162. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  163. /* This is where to make the change */
  164. location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
  165. rel[i].r_offset;
  166. /* This is the symbol it is referring to. Note that all
  167. undefined symbols have been resolved. */
  168. sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
  169. + ELF32_R_SYM(rel[i].r_info);
  170. value = sym->st_value;
  171. value += rel[i].r_addend;
  172. #ifdef CONFIG_SMP
  173. if (location >= COREB_L1_DATA_A_START) {
  174. pr_err("cannot relocate in L1: %u (SMP kernel)",
  175. mod->name, ELF32_R_TYPE(rel[i].r_info));
  176. return -ENOEXEC;
  177. }
  178. #endif
  179. pr_debug("location is %lx, value is %lx type is %d\n",
  180. mod->name, location, value, ELF32_R_TYPE(rel[i].r_info));
  181. switch (ELF32_R_TYPE(rel[i].r_info)) {
  182. case R_BFIN_HUIMM16:
  183. value >>= 16;
  184. case R_BFIN_LUIMM16:
  185. case R_BFIN_RIMM16:
  186. size = 2;
  187. break;
  188. case R_BFIN_BYTE4_DATA:
  189. size = 4;
  190. break;
  191. case R_BFIN_PCREL24:
  192. case R_BFIN_PCREL24_JUMP_L:
  193. case R_BFIN_PCREL12_JUMP:
  194. case R_BFIN_PCREL12_JUMP_S:
  195. case R_BFIN_PCREL10:
  196. pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
  197. mod->name, ELF32_R_TYPE(rel[i].r_info));
  198. return -ENOEXEC;
  199. default:
  200. pr_err("unknown relocation: %u\n", mod->name,
  201. ELF32_R_TYPE(rel[i].r_info));
  202. return -ENOEXEC;
  203. }
  204. switch (bfin_mem_access_type(location, size)) {
  205. case BFIN_MEM_ACCESS_CORE:
  206. case BFIN_MEM_ACCESS_CORE_ONLY:
  207. memcpy((void *)location, &value, size);
  208. break;
  209. case BFIN_MEM_ACCESS_DMA:
  210. dma_memcpy((void *)location, &value, size);
  211. break;
  212. case BFIN_MEM_ACCESS_ITEST:
  213. isram_memcpy((void *)location, &value, size);
  214. break;
  215. default:
  216. pr_err("invalid relocation for %#lx\n",
  217. mod->name, location);
  218. return -ENOEXEC;
  219. }
  220. }
  221. return 0;
  222. }
  223. int
  224. module_finalize(const Elf_Ehdr * hdr,
  225. const Elf_Shdr * sechdrs, struct module *mod)
  226. {
  227. unsigned int i, strindex = 0, symindex = 0;
  228. char *secstrings;
  229. long err = 0;
  230. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  231. for (i = 1; i < hdr->e_shnum; i++) {
  232. /* Internal symbols and strings. */
  233. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  234. symindex = i;
  235. strindex = sechdrs[i].sh_link;
  236. }
  237. }
  238. for (i = 1; i < hdr->e_shnum; i++) {
  239. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  240. unsigned int info = sechdrs[i].sh_info;
  241. const char *shname = secstrings + sechdrs[i].sh_name;
  242. /* Not a valid relocation section? */
  243. if (info >= hdr->e_shnum)
  244. continue;
  245. /* Only support RELA relocation types */
  246. if (sechdrs[i].sh_type != SHT_RELA)
  247. continue;
  248. if (!strcmp(".rela.l2.text", shname) ||
  249. !strcmp(".rela.l1.text", shname) ||
  250. (!strcmp(".rela.text", shname) &&
  251. (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
  252. err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
  253. symindex, i, mod);
  254. if (err < 0)
  255. return -ENOEXEC;
  256. }
  257. }
  258. return 0;
  259. }
  260. void module_arch_cleanup(struct module *mod)
  261. {
  262. l1_inst_sram_free(mod->arch.text_l1);
  263. l1_data_A_sram_free(mod->arch.data_a_l1);
  264. l1_data_A_sram_free(mod->arch.bss_a_l1);
  265. l1_data_B_sram_free(mod->arch.data_b_l1);
  266. l1_data_B_sram_free(mod->arch.bss_b_l1);
  267. l2_sram_free(mod->arch.text_l2);
  268. l2_sram_free(mod->arch.data_l2);
  269. l2_sram_free(mod->arch.bss_l2);
  270. }