module-plts.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2014-2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/elf.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/sort.h>
  12. static bool in_init(const struct module *mod, void *loc)
  13. {
  14. return (u64)loc - (u64)mod->init_layout.base < mod->init_layout.size;
  15. }
  16. u64 module_emit_plt_entry(struct module *mod, void *loc, const Elf64_Rela *rela,
  17. Elf64_Sym *sym)
  18. {
  19. struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
  20. &mod->arch.init;
  21. struct plt_entry *plt = (struct plt_entry *)pltsec->plt->sh_addr;
  22. int i = pltsec->plt_num_entries;
  23. u64 val = sym->st_value + rela->r_addend;
  24. plt[i] = get_plt_entry(val);
  25. /*
  26. * Check if the entry we just created is a duplicate. Given that the
  27. * relocations are sorted, this will be the last entry we allocated.
  28. * (if one exists).
  29. */
  30. if (i > 0 && plt_entries_equal(plt + i, plt + i - 1))
  31. return (u64)&plt[i - 1];
  32. pltsec->plt_num_entries++;
  33. BUG_ON(pltsec->plt_num_entries > pltsec->plt_max_entries);
  34. return (u64)&plt[i];
  35. }
  36. #define cmp_3way(a,b) ((a) < (b) ? -1 : (a) > (b))
  37. static int cmp_rela(const void *a, const void *b)
  38. {
  39. const Elf64_Rela *x = a, *y = b;
  40. int i;
  41. /* sort by type, symbol index and addend */
  42. i = cmp_3way(ELF64_R_TYPE(x->r_info), ELF64_R_TYPE(y->r_info));
  43. if (i == 0)
  44. i = cmp_3way(ELF64_R_SYM(x->r_info), ELF64_R_SYM(y->r_info));
  45. if (i == 0)
  46. i = cmp_3way(x->r_addend, y->r_addend);
  47. return i;
  48. }
  49. static bool duplicate_rel(const Elf64_Rela *rela, int num)
  50. {
  51. /*
  52. * Entries are sorted by type, symbol index and addend. That means
  53. * that, if a duplicate entry exists, it must be in the preceding
  54. * slot.
  55. */
  56. return num > 0 && cmp_rela(rela + num, rela + num - 1) == 0;
  57. }
  58. static unsigned int count_plts(Elf64_Sym *syms, Elf64_Rela *rela, int num,
  59. Elf64_Word dstidx)
  60. {
  61. unsigned int ret = 0;
  62. Elf64_Sym *s;
  63. int i;
  64. for (i = 0; i < num; i++) {
  65. switch (ELF64_R_TYPE(rela[i].r_info)) {
  66. case R_AARCH64_JUMP26:
  67. case R_AARCH64_CALL26:
  68. /*
  69. * We only have to consider branch targets that resolve
  70. * to symbols that are defined in a different section.
  71. * This is not simply a heuristic, it is a fundamental
  72. * limitation, since there is no guaranteed way to emit
  73. * PLT entries sufficiently close to the branch if the
  74. * section size exceeds the range of a branch
  75. * instruction. So ignore relocations against defined
  76. * symbols if they live in the same section as the
  77. * relocation target.
  78. */
  79. s = syms + ELF64_R_SYM(rela[i].r_info);
  80. if (s->st_shndx == dstidx)
  81. break;
  82. /*
  83. * Jump relocations with non-zero addends against
  84. * undefined symbols are supported by the ELF spec, but
  85. * do not occur in practice (e.g., 'jump n bytes past
  86. * the entry point of undefined function symbol f').
  87. * So we need to support them, but there is no need to
  88. * take them into consideration when trying to optimize
  89. * this code. So let's only check for duplicates when
  90. * the addend is zero: this allows us to record the PLT
  91. * entry address in the symbol table itself, rather than
  92. * having to search the list for duplicates each time we
  93. * emit one.
  94. */
  95. if (rela[i].r_addend != 0 || !duplicate_rel(rela, i))
  96. ret++;
  97. break;
  98. }
  99. }
  100. return ret;
  101. }
  102. int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  103. char *secstrings, struct module *mod)
  104. {
  105. unsigned long core_plts = 0;
  106. unsigned long init_plts = 0;
  107. Elf64_Sym *syms = NULL;
  108. int i;
  109. /*
  110. * Find the empty .plt section so we can expand it to store the PLT
  111. * entries. Record the symtab address as well.
  112. */
  113. for (i = 0; i < ehdr->e_shnum; i++) {
  114. if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
  115. mod->arch.core.plt = sechdrs + i;
  116. else if (!strcmp(secstrings + sechdrs[i].sh_name, ".init.plt"))
  117. mod->arch.init.plt = sechdrs + i;
  118. else if (sechdrs[i].sh_type == SHT_SYMTAB)
  119. syms = (Elf64_Sym *)sechdrs[i].sh_addr;
  120. }
  121. if (!mod->arch.core.plt || !mod->arch.init.plt) {
  122. pr_err("%s: module PLT section(s) missing\n", mod->name);
  123. return -ENOEXEC;
  124. }
  125. if (!syms) {
  126. pr_err("%s: module symtab section missing\n", mod->name);
  127. return -ENOEXEC;
  128. }
  129. for (i = 0; i < ehdr->e_shnum; i++) {
  130. Elf64_Rela *rels = (void *)ehdr + sechdrs[i].sh_offset;
  131. int numrels = sechdrs[i].sh_size / sizeof(Elf64_Rela);
  132. Elf64_Shdr *dstsec = sechdrs + sechdrs[i].sh_info;
  133. if (sechdrs[i].sh_type != SHT_RELA)
  134. continue;
  135. /* ignore relocations that operate on non-exec sections */
  136. if (!(dstsec->sh_flags & SHF_EXECINSTR))
  137. continue;
  138. /* sort by type, symbol index and addend */
  139. sort(rels, numrels, sizeof(Elf64_Rela), cmp_rela, NULL);
  140. if (strncmp(secstrings + dstsec->sh_name, ".init", 5) != 0)
  141. core_plts += count_plts(syms, rels, numrels,
  142. sechdrs[i].sh_info);
  143. else
  144. init_plts += count_plts(syms, rels, numrels,
  145. sechdrs[i].sh_info);
  146. }
  147. mod->arch.core.plt->sh_type = SHT_NOBITS;
  148. mod->arch.core.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  149. mod->arch.core.plt->sh_addralign = L1_CACHE_BYTES;
  150. mod->arch.core.plt->sh_size = (core_plts + 1) * sizeof(struct plt_entry);
  151. mod->arch.core.plt_num_entries = 0;
  152. mod->arch.core.plt_max_entries = core_plts;
  153. mod->arch.init.plt->sh_type = SHT_NOBITS;
  154. mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  155. mod->arch.init.plt->sh_addralign = L1_CACHE_BYTES;
  156. mod->arch.init.plt->sh_size = (init_plts + 1) * sizeof(struct plt_entry);
  157. mod->arch.init.plt_num_entries = 0;
  158. mod->arch.init.plt_max_entries = init_plts;
  159. return 0;
  160. }