module-sections.c 4.6 KB

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