module.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/moduleloader.h>
  4. #include <linux/elf.h>
  5. #include <linux/mm.h>
  6. #include <linux/vmalloc.h>
  7. #include <linux/slab.h>
  8. #include <linux/fs.h>
  9. #include <linux/string.h>
  10. #include <linux/kernel.h>
  11. #include <linux/spinlock.h>
  12. #ifdef CONFIG_CPU_CK810
  13. #define IS_BSR32(hi16, lo16) (((hi16) & 0xFC00) == 0xE000)
  14. #define IS_JSRI32(hi16, lo16) ((hi16) == 0xEAE0)
  15. #define CHANGE_JSRI_TO_LRW(addr) do { \
  16. *(uint16_t *)(addr) = (*(uint16_t *)(addr) & 0xFF9F) | 0x001a; \
  17. *((uint16_t *)(addr) + 1) = *((uint16_t *)(addr) + 1) & 0xFFFF; \
  18. } while (0)
  19. #define SET_JSR32_R26(addr) do { \
  20. *(uint16_t *)(addr) = 0xE8Fa; \
  21. *((uint16_t *)(addr) + 1) = 0x0000; \
  22. } while (0)
  23. static void jsri_2_lrw_jsr(uint32_t *location)
  24. {
  25. uint16_t *location_tmp = (uint16_t *)location;
  26. if (IS_BSR32(*location_tmp, *(location_tmp + 1)))
  27. return;
  28. if (IS_JSRI32(*location_tmp, *(location_tmp + 1))) {
  29. /* jsri 0x... --> lrw r26, 0x... */
  30. CHANGE_JSRI_TO_LRW(location);
  31. /* lsli r0, r0 --> jsr r26 */
  32. SET_JSR32_R26(location + 1);
  33. }
  34. }
  35. #else
  36. static void inline jsri_2_lrw_jsr(uint32_t *location)
  37. {
  38. return;
  39. }
  40. #endif
  41. int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  42. unsigned int symindex, unsigned int relsec, struct module *me)
  43. {
  44. unsigned int i;
  45. Elf32_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  46. Elf32_Sym *sym;
  47. uint32_t *location;
  48. short *temp;
  49. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  50. /* This is where to make the change */
  51. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  52. + rel[i].r_offset;
  53. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  54. + ELF32_R_SYM(rel[i].r_info);
  55. switch (ELF32_R_TYPE(rel[i].r_info)) {
  56. case R_CSKY_32:
  57. /* We add the value into the location given */
  58. *location = rel[i].r_addend + sym->st_value;
  59. break;
  60. case R_CSKY_PC32:
  61. /* Add the value, subtract its position */
  62. *location = rel[i].r_addend + sym->st_value
  63. - (uint32_t)location;
  64. break;
  65. case R_CSKY_PCRELJSR_IMM11BY2:
  66. break;
  67. case R_CSKY_PCRELJSR_IMM26BY2:
  68. jsri_2_lrw_jsr(location);
  69. break;
  70. case R_CSKY_ADDR_HI16:
  71. temp = ((short *)location) + 1;
  72. *temp = (short)
  73. ((rel[i].r_addend + sym->st_value) >> 16);
  74. break;
  75. case R_CSKY_ADDR_LO16:
  76. temp = ((short *)location) + 1;
  77. *temp = (short)
  78. ((rel[i].r_addend + sym->st_value) & 0xffff);
  79. break;
  80. default:
  81. pr_err("module %s: Unknown relocation: %u\n",
  82. me->name, ELF32_R_TYPE(rel[i].r_info));
  83. return -ENOEXEC;
  84. }
  85. }
  86. return 0;
  87. }