module.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. u32 inst_lu32id;
  34. u32 inst_lu52id;
  35. u32 inst_jirl;
  36. };
  37. struct plt_idx_entry {
  38. Elf_Addr symbol_addr;
  39. };
  40. Elf_Addr module_emit_got_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val);
  41. Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val);
  42. static inline struct got_entry emit_got_entry(Elf_Addr val)
  43. {
  44. return (struct got_entry) { val };
  45. }
  46. static inline struct plt_entry emit_plt_entry(unsigned long val)
  47. {
  48. u32 lu12iw, lu32id, lu52id, jirl;
  49. lu12iw = larch_insn_gen_lu12iw(LOONGARCH_GPR_T1, ADDR_IMM(val, LU12IW));
  50. lu32id = larch_insn_gen_lu32id(LOONGARCH_GPR_T1, ADDR_IMM(val, LU32ID));
  51. lu52id = larch_insn_gen_lu52id(LOONGARCH_GPR_T1, LOONGARCH_GPR_T1, ADDR_IMM(val, LU52ID));
  52. jirl = larch_insn_gen_jirl(0, LOONGARCH_GPR_T1, ADDR_IMM(val, ORI));
  53. return (struct plt_entry) { lu12iw, lu32id, lu52id, jirl };
  54. }
  55. static inline struct plt_idx_entry emit_plt_idx_entry(unsigned long val)
  56. {
  57. return (struct plt_idx_entry) { val };
  58. }
  59. static inline int get_plt_idx(unsigned long val, Elf_Shdr *sechdrs, const struct mod_section *sec)
  60. {
  61. int i;
  62. struct plt_idx_entry *plt_idx = (struct plt_idx_entry *)sechdrs[sec->shndx].sh_addr;
  63. for (i = 0; i < sec->num_entries; i++) {
  64. if (plt_idx[i].symbol_addr == val)
  65. return i;
  66. }
  67. return -1;
  68. }
  69. static inline struct plt_entry *get_plt_entry(unsigned long val,
  70. Elf_Shdr *sechdrs,
  71. const struct mod_section *sec_plt,
  72. const struct mod_section *sec_plt_idx)
  73. {
  74. int plt_idx = get_plt_idx(val, sechdrs, sec_plt_idx);
  75. struct plt_entry *plt = (struct plt_entry *)sechdrs[sec_plt->shndx].sh_addr;
  76. if (plt_idx < 0)
  77. return NULL;
  78. return plt + plt_idx;
  79. }
  80. static inline struct got_entry *get_got_entry(Elf_Addr val,
  81. Elf_Shdr *sechdrs,
  82. const struct mod_section *sec)
  83. {
  84. int i;
  85. struct got_entry *got = (struct got_entry *)sechdrs[sec->shndx].sh_addr;
  86. for (i = 0; i < sec->num_entries; i++)
  87. if (got[i].symbol_addr == val)
  88. return &got[i];
  89. return NULL;
  90. }
  91. #endif /* _ASM_MODULE_H */