module.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #define MODULE_ARCH_VERMAGIC "riscv"
  7. u64 module_emit_got_entry(struct module *mod, u64 val);
  8. u64 module_emit_plt_entry(struct module *mod, u64 val);
  9. #ifdef CONFIG_MODULE_SECTIONS
  10. struct mod_section {
  11. struct elf64_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. u64 symbol_addr; /* the real variable address */
  22. };
  23. static inline struct got_entry emit_got_entry(u64 val)
  24. {
  25. return (struct got_entry) {val};
  26. }
  27. static inline struct got_entry *get_got_entry(u64 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(u64 val, u64 plt, u64 got_plt)
  53. {
  54. /*
  55. * U-Type encoding:
  56. * +------------+----------+----------+
  57. * | imm[31:12] | rd[11:7] | opc[6:0] |
  58. * +------------+----------+----------+
  59. *
  60. * I-Type encoding:
  61. * +------------+------------+--------+----------+----------+
  62. * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] |
  63. * +------------+------------+--------+----------+----------+
  64. *
  65. */
  66. u64 offset = got_plt - plt;
  67. u32 hi20 = (offset + 0x800) & 0xfffff000;
  68. u32 lo12 = (offset - hi20);
  69. return (struct plt_entry) {
  70. OPC_AUIPC | (REG_T0 << 7) | hi20,
  71. OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7),
  72. OPC_JALR | (REG_T1 << 15)
  73. };
  74. }
  75. static inline int get_got_plt_idx(u64 val, const struct mod_section *sec)
  76. {
  77. struct got_entry *got_plt = (struct got_entry *)sec->shdr->sh_addr;
  78. int i;
  79. for (i = 0; i < sec->num_entries; i++) {
  80. if (got_plt[i].symbol_addr == val)
  81. return i;
  82. }
  83. return -1;
  84. }
  85. static inline struct plt_entry *get_plt_entry(u64 val,
  86. const struct mod_section *sec_plt,
  87. const struct mod_section *sec_got_plt)
  88. {
  89. struct plt_entry *plt = (struct plt_entry *)sec_plt->shdr->sh_addr;
  90. int got_plt_idx = get_got_plt_idx(val, sec_got_plt);
  91. if (got_plt_idx >= 0)
  92. return plt + got_plt_idx;
  93. else
  94. return NULL;
  95. }
  96. #endif /* CONFIG_MODULE_SECTIONS */
  97. #endif /* _ASM_RISCV_MODULE_H */