module-sections.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #include <linux/elf.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/moduleloader.h>
  9. #include <linux/ftrace.h>
  10. #include <linux/sort.h>
  11. Elf_Addr module_emit_got_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val)
  12. {
  13. struct mod_section *got_sec = &mod->arch.got;
  14. int i = got_sec->num_entries;
  15. struct got_entry *got = get_got_entry(val, sechdrs, got_sec);
  16. if (got)
  17. return (Elf_Addr)got;
  18. /* There is no GOT entry for val yet, create a new one. */
  19. got = (struct got_entry *)sechdrs[got_sec->shndx].sh_addr;
  20. got[i] = emit_got_entry(val);
  21. got_sec->num_entries++;
  22. if (got_sec->num_entries > got_sec->max_entries) {
  23. /*
  24. * This may happen when the module contains a GOT_HI20 without
  25. * a paired GOT_LO12. Such a module is broken, reject it.
  26. */
  27. pr_err("%s: module contains bad GOT relocation\n", mod->name);
  28. return 0;
  29. }
  30. return (Elf_Addr)&got[i];
  31. }
  32. Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val)
  33. {
  34. int nr;
  35. struct mod_section *plt_sec = &mod->arch.plt;
  36. struct mod_section *plt_idx_sec = &mod->arch.plt_idx;
  37. struct plt_entry *plt = get_plt_entry(val, sechdrs, plt_sec, plt_idx_sec);
  38. struct plt_idx_entry *plt_idx;
  39. if (plt)
  40. return (Elf_Addr)plt;
  41. nr = plt_sec->num_entries;
  42. /* There is no duplicate entry, create a new one */
  43. plt = (struct plt_entry *)sechdrs[plt_sec->shndx].sh_addr;
  44. plt[nr] = emit_plt_entry(val);
  45. plt_idx = (struct plt_idx_entry *)sechdrs[plt_idx_sec->shndx].sh_addr;
  46. plt_idx[nr] = emit_plt_idx_entry(val);
  47. plt_sec->num_entries++;
  48. plt_idx_sec->num_entries++;
  49. BUG_ON(plt_sec->num_entries > plt_sec->max_entries);
  50. return (Elf_Addr)&plt[nr];
  51. }
  52. #define cmp_3way(a, b) ((a) < (b) ? -1 : (a) > (b))
  53. static int compare_rela(const void *x, const void *y)
  54. {
  55. int ret;
  56. const Elf_Rela *rela_x = x, *rela_y = y;
  57. ret = cmp_3way(rela_x->r_info, rela_y->r_info);
  58. if (ret == 0)
  59. ret = cmp_3way(rela_x->r_addend, rela_y->r_addend);
  60. return ret;
  61. }
  62. static void count_max_entries(Elf_Rela *relas, int num,
  63. unsigned int *plts, unsigned int *gots)
  64. {
  65. unsigned int i;
  66. sort(relas, num, sizeof(Elf_Rela), compare_rela, NULL);
  67. for (i = 0; i < num; i++) {
  68. if (i && !compare_rela(&relas[i-1], &relas[i]))
  69. continue;
  70. switch (ELF_R_TYPE(relas[i].r_info)) {
  71. case R_LARCH_SOP_PUSH_PLT_PCREL:
  72. case R_LARCH_B26:
  73. (*plts)++;
  74. break;
  75. case R_LARCH_GOT_PC_HI20:
  76. (*gots)++;
  77. break;
  78. default:
  79. break; /* Do nothing. */
  80. }
  81. }
  82. }
  83. int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  84. char *secstrings, struct module *mod)
  85. {
  86. unsigned int i, num_plts = 0, num_gots = 0;
  87. Elf_Shdr *got_sec, *plt_sec, *plt_idx_sec, *tramp = NULL;
  88. /*
  89. * Find the empty .plt sections.
  90. */
  91. for (i = 0; i < ehdr->e_shnum; i++) {
  92. if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
  93. mod->arch.got.shndx = i;
  94. else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
  95. mod->arch.plt.shndx = i;
  96. else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt.idx"))
  97. mod->arch.plt_idx.shndx = i;
  98. else if (!strcmp(secstrings + sechdrs[i].sh_name, ".ftrace_trampoline"))
  99. tramp = sechdrs + i;
  100. }
  101. if (!mod->arch.got.shndx) {
  102. pr_err("%s: module GOT section(s) missing\n", mod->name);
  103. return -ENOEXEC;
  104. }
  105. if (!mod->arch.plt.shndx) {
  106. pr_err("%s: module PLT section(s) missing\n", mod->name);
  107. return -ENOEXEC;
  108. }
  109. if (!mod->arch.plt_idx.shndx) {
  110. pr_err("%s: module PLT.IDX section(s) missing\n", mod->name);
  111. return -ENOEXEC;
  112. }
  113. /* Calculate the maxinum number of entries */
  114. for (i = 0; i < ehdr->e_shnum; i++) {
  115. int num_rela = sechdrs[i].sh_size / sizeof(Elf_Rela);
  116. Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
  117. Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
  118. if (sechdrs[i].sh_type != SHT_RELA)
  119. continue;
  120. /* ignore relocations that operate on non-exec sections */
  121. if (!(dst_sec->sh_flags & SHF_EXECINSTR))
  122. continue;
  123. count_max_entries(relas, num_rela, &num_plts, &num_gots);
  124. }
  125. got_sec = sechdrs + mod->arch.got.shndx;
  126. got_sec->sh_type = SHT_NOBITS;
  127. got_sec->sh_flags = SHF_ALLOC;
  128. got_sec->sh_addralign = L1_CACHE_BYTES;
  129. got_sec->sh_size = (num_gots + 1) * sizeof(struct got_entry);
  130. mod->arch.got.num_entries = 0;
  131. mod->arch.got.max_entries = num_gots;
  132. plt_sec = sechdrs + mod->arch.plt.shndx;
  133. plt_sec->sh_type = SHT_NOBITS;
  134. plt_sec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  135. plt_sec->sh_addralign = L1_CACHE_BYTES;
  136. plt_sec->sh_size = (num_plts + 1) * sizeof(struct plt_entry);
  137. mod->arch.plt.num_entries = 0;
  138. mod->arch.plt.max_entries = num_plts;
  139. plt_idx_sec = sechdrs + mod->arch.plt_idx.shndx;
  140. plt_idx_sec->sh_type = SHT_NOBITS;
  141. plt_idx_sec->sh_flags = SHF_ALLOC;
  142. plt_idx_sec->sh_addralign = L1_CACHE_BYTES;
  143. plt_idx_sec->sh_size = (num_plts + 1) * sizeof(struct plt_idx_entry);
  144. mod->arch.plt_idx.num_entries = 0;
  145. mod->arch.plt_idx.max_entries = num_plts;
  146. if (tramp) {
  147. tramp->sh_type = SHT_NOBITS;
  148. tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  149. tramp->sh_addralign = __alignof__(struct plt_entry);
  150. tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry);
  151. }
  152. return 0;
  153. }