module.c 7.7 KB

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