module.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <asm/pgtable.h>
  13. #if defined(__CSKYABIV2__)
  14. #define IS_BSR32(hi16, lo16) (((hi16) & 0xFC00) == 0xE000)
  15. #define IS_JSRI32(hi16, lo16) ((hi16) == 0xEAE0)
  16. #define CHANGE_JSRI_TO_LRW(addr) do { \
  17. *(uint16_t *)(addr) = (*(uint16_t *)(addr) & 0xFF9F) | 0x001a; \
  18. *((uint16_t *)(addr) + 1) = *((uint16_t *)(addr) + 1) & 0xFFFF; \
  19. } while (0)
  20. #define SET_JSR32_R26(addr) do { \
  21. *(uint16_t *)(addr) = 0xE8Fa; \
  22. *((uint16_t *)(addr) + 1) = 0x0000; \
  23. } while (0)
  24. #endif
  25. int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  26. unsigned int symindex, unsigned int relsec, struct module *me)
  27. {
  28. unsigned int i;
  29. Elf32_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  30. Elf32_Sym *sym;
  31. uint32_t *location;
  32. short *temp;
  33. #if defined(__CSKYABIV2__)
  34. uint16_t *location_tmp;
  35. #endif
  36. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  37. /* This is where to make the change */
  38. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  39. + rel[i].r_offset;
  40. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  41. + ELF32_R_SYM(rel[i].r_info);
  42. switch (ELF32_R_TYPE(rel[i].r_info)) {
  43. case R_CSKY_32:
  44. /* We add the value into the location given */
  45. *location = rel[i].r_addend + sym->st_value;
  46. break;
  47. case R_CSKY_PC32:
  48. /* Add the value, subtract its postition */
  49. *location = rel[i].r_addend + sym->st_value
  50. - (uint32_t)location;
  51. break;
  52. case R_CSKY_PCRELJSR_IMM11BY2:
  53. break;
  54. case R_CSKY_PCRELJSR_IMM26BY2:
  55. #if defined(__CSKYABIV2__)
  56. location_tmp = (uint16_t *)location;
  57. if (IS_BSR32(*location_tmp, *(location_tmp + 1)))
  58. break;
  59. if (IS_JSRI32(*location_tmp, *(location_tmp + 1))) {
  60. /* jsri 0x... --> lrw r26, 0x... */
  61. CHANGE_JSRI_TO_LRW(location);
  62. /* lsli r0, r0 --> jsr r26 */
  63. SET_JSR32_R26(location + 1);
  64. }
  65. #endif
  66. break;
  67. case R_CSKY_ADDR_HI16:
  68. temp = ((short *)location) + 1;
  69. *temp = (short)
  70. ((rel[i].r_addend + sym->st_value) >> 16);
  71. break;
  72. case R_CSKY_ADDR_LO16:
  73. temp = ((short *)location) + 1;
  74. *temp = (short)
  75. ((rel[i].r_addend + sym->st_value) & 0xffff);
  76. break;
  77. default:
  78. pr_err("module %s: Unknown relocation: %u\n",
  79. me->name, ELF32_R_TYPE(rel[i].r_info));
  80. return -ENOEXEC;
  81. }
  82. }
  83. return 0;
  84. }