module.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <linux/elf.h>
  7. struct module;
  8. unsigned long module_emit_got_entry(struct module *mod, unsigned long val);
  9. unsigned long module_emit_plt_entry(struct module *mod, unsigned long val);
  10. #ifdef CONFIG_MODULE_SECTIONS
  11. struct mod_section {
  12. Elf_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. unsigned long symbol_addr; /* the real variable address */
  23. };
  24. static inline struct got_entry emit_got_entry(unsigned long val)
  25. {
  26. return (struct got_entry) {val};
  27. }
  28. static inline struct got_entry *get_got_entry(unsigned long 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(unsigned long val,
  54. unsigned long plt,
  55. unsigned long got_plt)
  56. {
  57. /*
  58. * U-Type encoding:
  59. * +------------+----------+----------+
  60. * | imm[31:12] | rd[11:7] | opc[6:0] |
  61. * +------------+----------+----------+
  62. *
  63. * I-Type encoding:
  64. * +------------+------------+--------+----------+----------+
  65. * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] |
  66. * +------------+------------+--------+----------+----------+
  67. *
  68. */
  69. unsigned long offset = got_plt - plt;
  70. u32 hi20 = (offset + 0x800) & 0xfffff000;
  71. u32 lo12 = (offset - hi20);
  72. return (struct plt_entry) {
  73. OPC_AUIPC | (REG_T0 << 7) | hi20,
  74. OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7),
  75. OPC_JALR | (REG_T1 << 15)
  76. };
  77. }
  78. static inline int get_got_plt_idx(unsigned long val, const struct mod_section *sec)
  79. {
  80. struct got_entry *got_plt = (struct got_entry *)sec->shdr->sh_addr;
  81. int i;
  82. for (i = 0; i < sec->num_entries; i++) {
  83. if (got_plt[i].symbol_addr == val)
  84. return i;
  85. }
  86. return -1;
  87. }
  88. static inline struct plt_entry *get_plt_entry(unsigned long val,
  89. const struct mod_section *sec_plt,
  90. const struct mod_section *sec_got_plt)
  91. {
  92. struct plt_entry *plt = (struct plt_entry *)sec_plt->shdr->sh_addr;
  93. int got_plt_idx = get_got_plt_idx(val, sec_got_plt);
  94. if (got_plt_idx >= 0)
  95. return plt + got_plt_idx;
  96. else
  97. return NULL;
  98. }
  99. #endif /* CONFIG_MODULE_SECTIONS */
  100. static inline const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
  101. const Elf_Shdr *sechdrs,
  102. const char *name)
  103. {
  104. const Elf_Shdr *s, *se;
  105. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  106. for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
  107. if (strcmp(name, secstrs + s->sh_name) == 0)
  108. return s;
  109. }
  110. return NULL;
  111. }
  112. #endif /* _ASM_RISCV_MODULE_H */