module.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * File: arch/blackfin/kernel/module.c
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description:
  8. *
  9. * Modified:
  10. * Copyright 2004-2006 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  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.
  18. *
  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.
  23. *
  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
  28. */
  29. #include <linux/moduleloader.h>
  30. #include <linux/elf.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/fs.h>
  33. #include <linux/string.h>
  34. #include <linux/kernel.h>
  35. #include <asm/dma.h>
  36. #include <asm/cacheflush.h>
  37. /*
  38. * handle arithmetic relocations.
  39. * See binutils/bfd/elf32-bfin.c for more details
  40. */
  41. #define RELOC_STACK_SIZE 100
  42. static uint32_t reloc_stack[RELOC_STACK_SIZE];
  43. static unsigned int reloc_stack_tos;
  44. #define is_reloc_stack_empty() ((reloc_stack_tos > 0)?0:1)
  45. static void reloc_stack_push(uint32_t value)
  46. {
  47. reloc_stack[reloc_stack_tos++] = value;
  48. }
  49. static uint32_t reloc_stack_pop(void)
  50. {
  51. return reloc_stack[--reloc_stack_tos];
  52. }
  53. static uint32_t reloc_stack_operate(unsigned int oper, struct module *mod)
  54. {
  55. uint32_t value;
  56. switch (oper) {
  57. case R_add:
  58. value = reloc_stack[reloc_stack_tos - 2] +
  59. reloc_stack[reloc_stack_tos - 1];
  60. reloc_stack_tos -= 2;
  61. break;
  62. case R_sub:
  63. value = reloc_stack[reloc_stack_tos - 2] -
  64. reloc_stack[reloc_stack_tos - 1];
  65. reloc_stack_tos -= 2;
  66. break;
  67. case R_mult:
  68. value = reloc_stack[reloc_stack_tos - 2] *
  69. reloc_stack[reloc_stack_tos - 1];
  70. reloc_stack_tos -= 2;
  71. break;
  72. case R_div:
  73. value = reloc_stack[reloc_stack_tos - 2] /
  74. reloc_stack[reloc_stack_tos - 1];
  75. reloc_stack_tos -= 2;
  76. break;
  77. case R_mod:
  78. value = reloc_stack[reloc_stack_tos - 2] %
  79. reloc_stack[reloc_stack_tos - 1];
  80. reloc_stack_tos -= 2;
  81. break;
  82. case R_lshift:
  83. value = reloc_stack[reloc_stack_tos - 2] <<
  84. reloc_stack[reloc_stack_tos - 1];
  85. reloc_stack_tos -= 2;
  86. break;
  87. case R_rshift:
  88. value = reloc_stack[reloc_stack_tos - 2] >>
  89. reloc_stack[reloc_stack_tos - 1];
  90. reloc_stack_tos -= 2;
  91. break;
  92. case R_and:
  93. value = reloc_stack[reloc_stack_tos - 2] &
  94. reloc_stack[reloc_stack_tos - 1];
  95. reloc_stack_tos -= 2;
  96. break;
  97. case R_or:
  98. value = reloc_stack[reloc_stack_tos - 2] |
  99. reloc_stack[reloc_stack_tos - 1];
  100. reloc_stack_tos -= 2;
  101. break;
  102. case R_xor:
  103. value = reloc_stack[reloc_stack_tos - 2] ^
  104. reloc_stack[reloc_stack_tos - 1];
  105. reloc_stack_tos -= 2;
  106. break;
  107. case R_land:
  108. value = reloc_stack[reloc_stack_tos - 2] &&
  109. reloc_stack[reloc_stack_tos - 1];
  110. reloc_stack_tos -= 2;
  111. break;
  112. case R_lor:
  113. value = reloc_stack[reloc_stack_tos - 2] ||
  114. reloc_stack[reloc_stack_tos - 1];
  115. reloc_stack_tos -= 2;
  116. break;
  117. case R_neg:
  118. value = -reloc_stack[reloc_stack_tos - 1];
  119. reloc_stack_tos--;
  120. break;
  121. case R_comp:
  122. value = ~reloc_stack[reloc_stack_tos - 1];
  123. reloc_stack_tos -= 1;
  124. break;
  125. default:
  126. printk(KERN_WARNING "module %s: unhandled reloction\n",
  127. mod->name);
  128. return 0;
  129. }
  130. /* now push the new value back on stack */
  131. reloc_stack_push(value);
  132. return value;
  133. }
  134. void *module_alloc(unsigned long size)
  135. {
  136. if (size == 0)
  137. return NULL;
  138. return vmalloc(size);
  139. }
  140. /* Free memory returned from module_alloc */
  141. void module_free(struct module *mod, void *module_region)
  142. {
  143. vfree(module_region);
  144. }
  145. /* Transfer the section to the L1 memory */
  146. int
  147. module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
  148. char *secstrings, struct module *mod)
  149. {
  150. Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
  151. void *dest = NULL;
  152. for (s = sechdrs; s < sechdrs_end; ++s) {
  153. if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) ||
  154. ((strcmp(".text", secstrings + s->sh_name) == 0) &&
  155. (hdr->e_flags & FLG_CODE_IN_L1) && (s->sh_size > 0))) {
  156. mod->arch.text_l1 = s;
  157. dest = l1_inst_sram_alloc(s->sh_size);
  158. if (dest == NULL) {
  159. printk(KERN_ERR
  160. "module %s: L1 instruction memory allocation failed\n",
  161. mod->name);
  162. return -1;
  163. }
  164. dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
  165. s->sh_flags &= ~SHF_ALLOC;
  166. s->sh_addr = (unsigned long)dest;
  167. }
  168. if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
  169. ((strcmp(".data", secstrings + s->sh_name) == 0) &&
  170. (hdr->e_flags & FLG_DATA_IN_L1) && (s->sh_size > 0))) {
  171. mod->arch.data_a_l1 = s;
  172. dest = l1_data_sram_alloc(s->sh_size);
  173. if (dest == NULL) {
  174. printk(KERN_ERR
  175. "module %s: L1 data memory allocation failed\n",
  176. mod->name);
  177. return -1;
  178. }
  179. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  180. s->sh_flags &= ~SHF_ALLOC;
  181. s->sh_addr = (unsigned long)dest;
  182. }
  183. if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 ||
  184. ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
  185. (hdr->e_flags & FLG_DATA_IN_L1) && (s->sh_size > 0))) {
  186. mod->arch.bss_a_l1 = s;
  187. dest = l1_data_sram_alloc(s->sh_size);
  188. if (dest == NULL) {
  189. printk(KERN_ERR
  190. "module %s: L1 data memory allocation failed\n",
  191. mod->name);
  192. return -1;
  193. }
  194. memset(dest, 0, s->sh_size);
  195. s->sh_flags &= ~SHF_ALLOC;
  196. s->sh_addr = (unsigned long)dest;
  197. }
  198. if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) {
  199. mod->arch.data_b_l1 = s;
  200. dest = l1_data_B_sram_alloc(s->sh_size);
  201. if (dest == NULL) {
  202. printk(KERN_ERR
  203. "module %s: L1 data memory allocation failed\n",
  204. mod->name);
  205. return -1;
  206. }
  207. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  208. s->sh_flags &= ~SHF_ALLOC;
  209. s->sh_addr = (unsigned long)dest;
  210. }
  211. if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) {
  212. mod->arch.bss_b_l1 = s;
  213. dest = l1_data_B_sram_alloc(s->sh_size);
  214. if (dest == NULL) {
  215. printk(KERN_ERR
  216. "module %s: L1 data memory allocation failed\n",
  217. mod->name);
  218. return -1;
  219. }
  220. memset(dest, 0, s->sh_size);
  221. s->sh_flags &= ~SHF_ALLOC;
  222. s->sh_addr = (unsigned long)dest;
  223. }
  224. }
  225. return 0;
  226. }
  227. int
  228. apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
  229. unsigned int symindex, unsigned int relsec, struct module *me)
  230. {
  231. printk(KERN_ERR "module %s: .rel unsupported\n", me->name);
  232. return -ENOEXEC;
  233. }
  234. /*************************************************************************/
  235. /* FUNCTION : apply_relocate_add */
  236. /* ABSTRACT : Blackfin specific relocation handling for the loadable */
  237. /* modules. Modules are expected to be .o files. */
  238. /* Arithmetic relocations are handled. */
  239. /* We do not expect LSETUP to be split and hence is not */
  240. /* handled. */
  241. /* R_byte and R_byte2 are also not handled as the gas */
  242. /* does not generate it. */
  243. /*************************************************************************/
  244. int
  245. apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
  246. unsigned int symindex, unsigned int relsec,
  247. struct module *mod)
  248. {
  249. unsigned int i;
  250. unsigned short tmp;
  251. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  252. Elf32_Sym *sym;
  253. uint32_t *location32;
  254. uint16_t *location16;
  255. uint32_t value;
  256. pr_debug("Applying relocate section %u to %u\n", relsec,
  257. sechdrs[relsec].sh_info);
  258. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  259. /* This is where to make the change */
  260. location16 =
  261. (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr +
  262. rel[i].r_offset);
  263. location32 = (uint32_t *) location16;
  264. /* This is the symbol it is referring to. Note that all
  265. undefined symbols have been resolved. */
  266. sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
  267. + ELF32_R_SYM(rel[i].r_info);
  268. if (is_reloc_stack_empty()) {
  269. value = sym->st_value;
  270. } else {
  271. value = reloc_stack_pop();
  272. }
  273. value += rel[i].r_addend;
  274. pr_debug("location is %x, value is %x type is %d \n",
  275. (unsigned int) location32, value,
  276. ELF32_R_TYPE(rel[i].r_info));
  277. switch (ELF32_R_TYPE(rel[i].r_info)) {
  278. case R_pcrel24:
  279. case R_pcrel24_jump_l:
  280. /* Add the value, subtract its postition */
  281. location16 =
  282. (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].
  283. sh_addr + rel[i].r_offset - 2);
  284. location32 = (uint32_t *) location16;
  285. value -= (uint32_t) location32;
  286. value >>= 1;
  287. pr_debug("value is %x, before %x-%x after %x-%x\n", value,
  288. *location16, *(location16 + 1),
  289. (*location16 & 0xff00) | (value >> 16 & 0x00ff),
  290. value & 0xffff);
  291. *location16 =
  292. (*location16 & 0xff00) | (value >> 16 & 0x00ff);
  293. *(location16 + 1) = value & 0xffff;
  294. break;
  295. case R_pcrel12_jump:
  296. case R_pcrel12_jump_s:
  297. value -= (uint32_t) location32;
  298. value >>= 1;
  299. *location16 = (value & 0xfff);
  300. break;
  301. case R_pcrel10:
  302. value -= (uint32_t) location32;
  303. value >>= 1;
  304. *location16 = (value & 0x3ff);
  305. break;
  306. case R_luimm16:
  307. pr_debug("before %x after %x\n", *location16,
  308. (value & 0xffff));
  309. tmp = (value & 0xffff);
  310. if ((unsigned long)location16 >= L1_CODE_START) {
  311. dma_memcpy(location16, &tmp, 2);
  312. } else
  313. *location16 = tmp;
  314. break;
  315. case R_huimm16:
  316. pr_debug("before %x after %x\n", *location16,
  317. ((value >> 16) & 0xffff));
  318. tmp = ((value >> 16) & 0xffff);
  319. if ((unsigned long)location16 >= L1_CODE_START) {
  320. dma_memcpy(location16, &tmp, 2);
  321. } else
  322. *location16 = tmp;
  323. break;
  324. case R_rimm16:
  325. *location16 = (value & 0xffff);
  326. break;
  327. case R_byte4_data:
  328. pr_debug("before %x after %x\n", *location32, value);
  329. *location32 = value;
  330. break;
  331. case R_push:
  332. reloc_stack_push(value);
  333. break;
  334. case R_const:
  335. reloc_stack_push(rel[i].r_addend);
  336. break;
  337. case R_add:
  338. case R_sub:
  339. case R_mult:
  340. case R_div:
  341. case R_mod:
  342. case R_lshift:
  343. case R_rshift:
  344. case R_and:
  345. case R_or:
  346. case R_xor:
  347. case R_land:
  348. case R_lor:
  349. case R_neg:
  350. case R_comp:
  351. reloc_stack_operate(ELF32_R_TYPE(rel[i].r_info), mod);
  352. break;
  353. default:
  354. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  355. mod->name, ELF32_R_TYPE(rel[i].r_info));
  356. return -ENOEXEC;
  357. }
  358. }
  359. return 0;
  360. }
  361. int
  362. module_finalize(const Elf_Ehdr * hdr,
  363. const Elf_Shdr * sechdrs, struct module *mod)
  364. {
  365. unsigned int i, strindex = 0, symindex = 0;
  366. char *secstrings;
  367. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  368. for (i = 1; i < hdr->e_shnum; i++) {
  369. /* Internal symbols and strings. */
  370. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  371. symindex = i;
  372. strindex = sechdrs[i].sh_link;
  373. }
  374. }
  375. for (i = 1; i < hdr->e_shnum; i++) {
  376. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  377. unsigned int info = sechdrs[i].sh_info;
  378. /* Not a valid relocation section? */
  379. if (info >= hdr->e_shnum)
  380. continue;
  381. if ((sechdrs[i].sh_type == SHT_RELA) &&
  382. ((strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
  383. ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
  384. (hdr->e_flags & FLG_CODE_IN_L1)))) {
  385. apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
  386. symindex, i, mod);
  387. }
  388. }
  389. return 0;
  390. }
  391. void module_arch_cleanup(struct module *mod)
  392. {
  393. if ((mod->arch.text_l1) && (mod->arch.text_l1->sh_addr))
  394. l1_inst_sram_free((void *)mod->arch.text_l1->sh_addr);
  395. if ((mod->arch.data_a_l1) && (mod->arch.data_a_l1->sh_addr))
  396. l1_data_sram_free((void *)mod->arch.data_a_l1->sh_addr);
  397. if ((mod->arch.bss_a_l1) && (mod->arch.bss_a_l1->sh_addr))
  398. l1_data_sram_free((void *)mod->arch.bss_a_l1->sh_addr);
  399. if ((mod->arch.data_b_l1) && (mod->arch.data_b_l1->sh_addr))
  400. l1_data_B_sram_free((void *)mod->arch.data_b_l1->sh_addr);
  401. if ((mod->arch.bss_b_l1) && (mod->arch.bss_b_l1->sh_addr))
  402. l1_data_B_sram_free((void *)mod->arch.bss_b_l1->sh_addr);
  403. }