module.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #ifndef _ASM_MODULE_H
  6. #define _ASM_MODULE_H
  7. #include <asm/inst.h>
  8. #include <asm/orc_types.h>
  9. #include <asm-generic/module.h>
  10. #define RELA_STACK_DEPTH 16
  11. struct mod_section {
  12. int shndx;
  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 plt_idx;
  20. #ifdef CONFIG_UNWINDER_ORC
  21. unsigned int num_orcs;
  22. int *orc_unwind_ip;
  23. struct orc_entry *orc_unwind;
  24. #endif
  25. /* For CONFIG_DYNAMIC_FTRACE */
  26. struct plt_entry *ftrace_trampolines;
  27. };
  28. struct got_entry {
  29. Elf_Addr symbol_addr;
  30. };
  31. struct plt_entry {
  32. u32 inst_lu12iw;
  33. #ifdef CONFIG_64BIT
  34. u32 inst_lu32id;
  35. u32 inst_lu52id;
  36. #endif
  37. u32 inst_jirl;
  38. };
  39. struct plt_idx_entry {
  40. Elf_Addr symbol_addr;
  41. };
  42. Elf_Addr module_emit_got_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val);
  43. Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val);
  44. static inline struct got_entry emit_got_entry(Elf_Addr val)
  45. {
  46. return (struct got_entry) { val };
  47. }
  48. static inline struct plt_entry emit_plt_entry(unsigned long val)
  49. {
  50. #ifdef CONFIG_32BIT
  51. u32 lu12iw, jirl;
  52. lu12iw = larch_insn_gen_lu12iw(LOONGARCH_GPR_T1, ADDR_IMM(val, LU12IW));
  53. jirl = larch_insn_gen_jirl(0, LOONGARCH_GPR_T1, ADDR_IMM(val, ORI));
  54. return (struct plt_entry) { lu12iw, jirl };
  55. #else
  56. u32 lu12iw, lu32id, lu52id, jirl;
  57. lu12iw = larch_insn_gen_lu12iw(LOONGARCH_GPR_T1, ADDR_IMM(val, LU12IW));
  58. lu32id = larch_insn_gen_lu32id(LOONGARCH_GPR_T1, ADDR_IMM(val, LU32ID));
  59. lu52id = larch_insn_gen_lu52id(LOONGARCH_GPR_T1, LOONGARCH_GPR_T1, ADDR_IMM(val, LU52ID));
  60. jirl = larch_insn_gen_jirl(0, LOONGARCH_GPR_T1, ADDR_IMM(val, ORI));
  61. return (struct plt_entry) { lu12iw, lu32id, lu52id, jirl };
  62. #endif
  63. }
  64. static inline struct plt_idx_entry emit_plt_idx_entry(unsigned long val)
  65. {
  66. return (struct plt_idx_entry) { val };
  67. }
  68. static inline int get_plt_idx(unsigned long val, Elf_Shdr *sechdrs, const struct mod_section *sec)
  69. {
  70. int i;
  71. struct plt_idx_entry *plt_idx = (struct plt_idx_entry *)sechdrs[sec->shndx].sh_addr;
  72. for (i = 0; i < sec->num_entries; i++) {
  73. if (plt_idx[i].symbol_addr == val)
  74. return i;
  75. }
  76. return -1;
  77. }
  78. static inline struct plt_entry *get_plt_entry(unsigned long val,
  79. Elf_Shdr *sechdrs,
  80. const struct mod_section *sec_plt,
  81. const struct mod_section *sec_plt_idx)
  82. {
  83. int plt_idx = get_plt_idx(val, sechdrs, sec_plt_idx);
  84. struct plt_entry *plt = (struct plt_entry *)sechdrs[sec_plt->shndx].sh_addr;
  85. if (plt_idx < 0)
  86. return NULL;
  87. return plt + plt_idx;
  88. }
  89. static inline struct got_entry *get_got_entry(Elf_Addr val,
  90. Elf_Shdr *sechdrs,
  91. const struct mod_section *sec)
  92. {
  93. int i;
  94. struct got_entry *got = (struct got_entry *)sechdrs[sec->shndx].sh_addr;
  95. for (i = 0; i < sec->num_entries; i++)
  96. if (got[i].symbol_addr == val)
  97. return &got[i];
  98. return NULL;
  99. }
  100. #endif /* _ASM_MODULE_H */