module.h 2.8 KB

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