module.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2017 Andes Technology Corporation */
  3. #ifndef _ASM_RISCV_MODULE_H
  4. #define _ASM_RISCV_MODULE_H
  5. #include <asm-generic/module.h>
  6. struct module;
  7. unsigned long module_emit_got_entry(struct module *mod, unsigned long val);
  8. unsigned long module_emit_plt_entry(struct module *mod, unsigned long val);
  9. #ifdef CONFIG_MODULE_SECTIONS
  10. struct mod_section {
  11. Elf_Shdr *shdr;
  12. int num_entries;
  13. int max_entries;
  14. };
  15. struct mod_arch_specific {
  16. struct mod_section got;
  17. struct mod_section plt;
  18. struct mod_section got_plt;
  19. };
  20. struct got_entry {
  21. unsigned long symbol_addr; /* the real variable address */
  22. };
  23. static inline struct got_entry emit_got_entry(unsigned long val)
  24. {
  25. return (struct got_entry) {val};
  26. }
  27. static inline struct got_entry *get_got_entry(unsigned long val,
  28. const struct mod_section *sec)
  29. {
  30. struct got_entry *got = (struct got_entry *)(sec->shdr->sh_addr);
  31. int i;
  32. for (i = 0; i < sec->num_entries; i++) {
  33. if (got[i].symbol_addr == val)
  34. return &got[i];
  35. }
  36. return NULL;
  37. }
  38. struct plt_entry {
  39. /*
  40. * Trampoline code to real target address. The return address
  41. * should be the original (pc+4) before entring plt entry.
  42. */
  43. u32 insn_auipc; /* auipc t0, 0x0 */
  44. u32 insn_ld; /* ld t1, 0x10(t0) */
  45. u32 insn_jr; /* jr t1 */
  46. };
  47. #define OPC_AUIPC 0x0017
  48. #define OPC_LD 0x3003
  49. #define OPC_JALR 0x0067
  50. #define REG_T0 0x5
  51. #define REG_T1 0x6
  52. static inline struct plt_entry emit_plt_entry(unsigned long val,
  53. unsigned long plt,
  54. unsigned long got_plt)
  55. {
  56. /*
  57. * U-Type encoding:
  58. * +------------+----------+----------+
  59. * | imm[31:12] | rd[11:7] | opc[6:0] |
  60. * +------------+----------+----------+
  61. *
  62. * I-Type encoding:
  63. * +------------+------------+--------+----------+----------+
  64. * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] |
  65. * +------------+------------+--------+----------+----------+
  66. *
  67. */
  68. unsigned long offset = got_plt - plt;
  69. u32 hi20 = (offset + 0x800) & 0xfffff000;
  70. u32 lo12 = (offset - hi20);
  71. return (struct plt_entry) {
  72. OPC_AUIPC | (REG_T0 << 7) | hi20,
  73. OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7),
  74. OPC_JALR | (REG_T1 << 15)
  75. };
  76. }
  77. static inline int get_got_plt_idx(unsigned long val, const struct mod_section *sec)
  78. {
  79. struct got_entry *got_plt = (struct got_entry *)sec->shdr->sh_addr;
  80. int i;
  81. for (i = 0; i < sec->num_entries; i++) {
  82. if (got_plt[i].symbol_addr == val)
  83. return i;
  84. }
  85. return -1;
  86. }
  87. static inline struct plt_entry *get_plt_entry(unsigned long val,
  88. const struct mod_section *sec_plt,
  89. const struct mod_section *sec_got_plt)
  90. {
  91. struct plt_entry *plt = (struct plt_entry *)sec_plt->shdr->sh_addr;
  92. int got_plt_idx = get_got_plt_idx(val, sec_got_plt);
  93. if (got_plt_idx >= 0)
  94. return plt + got_plt_idx;
  95. else
  96. return NULL;
  97. }
  98. #endif /* CONFIG_MODULE_SECTIONS */
  99. #endif /* _ASM_RISCV_MODULE_H */