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