module.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. /*
  151. * XXX: sechdrs are vmalloced in kernel/module.c
  152. * and would be vfreed just after module is loaded,
  153. * so we hack to keep the only information we needed
  154. * in mod->arch to correctly free L1 I/D sram later.
  155. * NOTE: this breaks the semantic of mod->arch structure.
  156. */
  157. Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
  158. void *dest = NULL;
  159. for (s = sechdrs; s < sechdrs_end; ++s) {
  160. if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) ||
  161. ((strcmp(".text", secstrings + s->sh_name) == 0) &&
  162. (hdr->e_flags & FLG_CODE_IN_L1) && (s->sh_size > 0))) {
  163. dest = l1_inst_sram_alloc(s->sh_size);
  164. mod->arch.text_l1 = dest;
  165. if (dest == NULL) {
  166. printk(KERN_ERR
  167. "module %s: L1 instruction memory allocation failed\n",
  168. mod->name);
  169. return -1;
  170. }
  171. dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
  172. s->sh_flags &= ~SHF_ALLOC;
  173. s->sh_addr = (unsigned long)dest;
  174. }
  175. if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
  176. ((strcmp(".data", secstrings + s->sh_name) == 0) &&
  177. (hdr->e_flags & FLG_DATA_IN_L1) && (s->sh_size > 0))) {
  178. dest = l1_data_sram_alloc(s->sh_size);
  179. mod->arch.data_a_l1 = dest;
  180. if (dest == NULL) {
  181. printk(KERN_ERR
  182. "module %s: L1 data memory allocation failed\n",
  183. mod->name);
  184. return -1;
  185. }
  186. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  187. s->sh_flags &= ~SHF_ALLOC;
  188. s->sh_addr = (unsigned long)dest;
  189. }
  190. if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 ||
  191. ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
  192. (hdr->e_flags & FLG_DATA_IN_L1) && (s->sh_size > 0))) {
  193. dest = l1_data_sram_alloc(s->sh_size);
  194. mod->arch.bss_a_l1 = dest;
  195. if (dest == NULL) {
  196. printk(KERN_ERR
  197. "module %s: L1 data memory allocation failed\n",
  198. mod->name);
  199. return -1;
  200. }
  201. memset(dest, 0, s->sh_size);
  202. s->sh_flags &= ~SHF_ALLOC;
  203. s->sh_addr = (unsigned long)dest;
  204. }
  205. if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) {
  206. dest = l1_data_B_sram_alloc(s->sh_size);
  207. mod->arch.data_b_l1 = dest;
  208. if (dest == NULL) {
  209. printk(KERN_ERR
  210. "module %s: L1 data memory allocation failed\n",
  211. mod->name);
  212. return -1;
  213. }
  214. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  215. s->sh_flags &= ~SHF_ALLOC;
  216. s->sh_addr = (unsigned long)dest;
  217. }
  218. if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) {
  219. dest = l1_data_B_sram_alloc(s->sh_size);
  220. mod->arch.bss_b_l1 = dest;
  221. if (dest == NULL) {
  222. printk(KERN_ERR
  223. "module %s: L1 data memory allocation failed\n",
  224. mod->name);
  225. return -1;
  226. }
  227. memset(dest, 0, s->sh_size);
  228. s->sh_flags &= ~SHF_ALLOC;
  229. s->sh_addr = (unsigned long)dest;
  230. }
  231. }
  232. return 0;
  233. }
  234. int
  235. apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
  236. unsigned int symindex, unsigned int relsec, struct module *me)
  237. {
  238. printk(KERN_ERR "module %s: .rel unsupported\n", me->name);
  239. return -ENOEXEC;
  240. }
  241. /*************************************************************************/
  242. /* FUNCTION : apply_relocate_add */
  243. /* ABSTRACT : Blackfin specific relocation handling for the loadable */
  244. /* modules. Modules are expected to be .o files. */
  245. /* Arithmetic relocations are handled. */
  246. /* We do not expect LSETUP to be split and hence is not */
  247. /* handled. */
  248. /* R_byte and R_byte2 are also not handled as the gas */
  249. /* does not generate it. */
  250. /*************************************************************************/
  251. int
  252. apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
  253. unsigned int symindex, unsigned int relsec,
  254. struct module *mod)
  255. {
  256. unsigned int i;
  257. unsigned short tmp;
  258. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  259. Elf32_Sym *sym;
  260. uint32_t *location32;
  261. uint16_t *location16;
  262. uint32_t value;
  263. pr_debug("Applying relocate section %u to %u\n", relsec,
  264. sechdrs[relsec].sh_info);
  265. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  266. /* This is where to make the change */
  267. location16 =
  268. (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr +
  269. rel[i].r_offset);
  270. location32 = (uint32_t *) location16;
  271. /* This is the symbol it is referring to. Note that all
  272. undefined symbols have been resolved. */
  273. sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
  274. + ELF32_R_SYM(rel[i].r_info);
  275. if (is_reloc_stack_empty()) {
  276. value = sym->st_value;
  277. } else {
  278. value = reloc_stack_pop();
  279. }
  280. value += rel[i].r_addend;
  281. pr_debug("location is %x, value is %x type is %d \n",
  282. (unsigned int) location32, value,
  283. ELF32_R_TYPE(rel[i].r_info));
  284. switch (ELF32_R_TYPE(rel[i].r_info)) {
  285. case R_pcrel24:
  286. case R_pcrel24_jump_l:
  287. /* Add the value, subtract its postition */
  288. location16 =
  289. (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].
  290. sh_addr + rel[i].r_offset - 2);
  291. location32 = (uint32_t *) location16;
  292. value -= (uint32_t) location32;
  293. value >>= 1;
  294. pr_debug("value is %x, before %x-%x after %x-%x\n", value,
  295. *location16, *(location16 + 1),
  296. (*location16 & 0xff00) | (value >> 16 & 0x00ff),
  297. value & 0xffff);
  298. *location16 =
  299. (*location16 & 0xff00) | (value >> 16 & 0x00ff);
  300. *(location16 + 1) = value & 0xffff;
  301. break;
  302. case R_pcrel12_jump:
  303. case R_pcrel12_jump_s:
  304. value -= (uint32_t) location32;
  305. value >>= 1;
  306. *location16 = (value & 0xfff);
  307. break;
  308. case R_pcrel10:
  309. value -= (uint32_t) location32;
  310. value >>= 1;
  311. *location16 = (value & 0x3ff);
  312. break;
  313. case R_luimm16:
  314. pr_debug("before %x after %x\n", *location16,
  315. (value & 0xffff));
  316. tmp = (value & 0xffff);
  317. if ((unsigned long)location16 >= L1_CODE_START) {
  318. dma_memcpy(location16, &tmp, 2);
  319. } else
  320. *location16 = tmp;
  321. break;
  322. case R_huimm16:
  323. pr_debug("before %x after %x\n", *location16,
  324. ((value >> 16) & 0xffff));
  325. tmp = ((value >> 16) & 0xffff);
  326. if ((unsigned long)location16 >= L1_CODE_START) {
  327. dma_memcpy(location16, &tmp, 2);
  328. } else
  329. *location16 = tmp;
  330. break;
  331. case R_rimm16:
  332. *location16 = (value & 0xffff);
  333. break;
  334. case R_byte4_data:
  335. pr_debug("before %x after %x\n", *location32, value);
  336. *location32 = value;
  337. break;
  338. case R_push:
  339. reloc_stack_push(value);
  340. break;
  341. case R_const:
  342. reloc_stack_push(rel[i].r_addend);
  343. break;
  344. case R_add:
  345. case R_sub:
  346. case R_mult:
  347. case R_div:
  348. case R_mod:
  349. case R_lshift:
  350. case R_rshift:
  351. case R_and:
  352. case R_or:
  353. case R_xor:
  354. case R_land:
  355. case R_lor:
  356. case R_neg:
  357. case R_comp:
  358. reloc_stack_operate(ELF32_R_TYPE(rel[i].r_info), mod);
  359. break;
  360. default:
  361. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  362. mod->name, ELF32_R_TYPE(rel[i].r_info));
  363. return -ENOEXEC;
  364. }
  365. }
  366. return 0;
  367. }
  368. int
  369. module_finalize(const Elf_Ehdr * hdr,
  370. const Elf_Shdr * sechdrs, struct module *mod)
  371. {
  372. unsigned int i, strindex = 0, symindex = 0;
  373. char *secstrings;
  374. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  375. for (i = 1; i < hdr->e_shnum; i++) {
  376. /* Internal symbols and strings. */
  377. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  378. symindex = i;
  379. strindex = sechdrs[i].sh_link;
  380. }
  381. }
  382. for (i = 1; i < hdr->e_shnum; i++) {
  383. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  384. unsigned int info = sechdrs[i].sh_info;
  385. /* Not a valid relocation section? */
  386. if (info >= hdr->e_shnum)
  387. continue;
  388. if ((sechdrs[i].sh_type == SHT_RELA) &&
  389. ((strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
  390. ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
  391. (hdr->e_flags & FLG_CODE_IN_L1)))) {
  392. apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
  393. symindex, i, mod);
  394. }
  395. }
  396. return 0;
  397. }
  398. void module_arch_cleanup(struct module *mod)
  399. {
  400. if (mod->arch.text_l1)
  401. l1_inst_sram_free((void *)mod->arch.text_l1);
  402. if (mod->arch.data_a_l1)
  403. l1_data_sram_free((void *)mod->arch.data_a_l1);
  404. if (mod->arch.bss_a_l1)
  405. l1_data_sram_free((void *)mod->arch.bss_a_l1);
  406. if (mod->arch.data_b_l1)
  407. l1_data_B_sram_free((void *)mod->arch.data_b_l1);
  408. if (mod->arch.bss_b_l1)
  409. l1_data_B_sram_free((void *)mod->arch.bss_b_l1);
  410. }