module-plts.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2014 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 <asm/cache.h>
  12. #include <asm/opcodes.h>
  13. #define PLT_ENT_STRIDE L1_CACHE_BYTES
  14. #define PLT_ENT_COUNT (PLT_ENT_STRIDE / sizeof(u32))
  15. #define PLT_ENT_SIZE (sizeof(struct plt_entries) / PLT_ENT_COUNT)
  16. #ifdef CONFIG_THUMB2_KERNEL
  17. #define PLT_ENT_LDR __opcode_to_mem_thumb32(0xf8dff000 | \
  18. (PLT_ENT_STRIDE - 4))
  19. #else
  20. #define PLT_ENT_LDR __opcode_to_mem_arm(0xe59ff000 | \
  21. (PLT_ENT_STRIDE - 8))
  22. #endif
  23. struct plt_entries {
  24. u32 ldr[PLT_ENT_COUNT];
  25. u32 lit[PLT_ENT_COUNT];
  26. };
  27. static bool in_init(const struct module *mod, u32 addr)
  28. {
  29. return addr - (u32)mod->module_init < mod->init_size;
  30. }
  31. u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val)
  32. {
  33. struct plt_entries *plt, *plt_end;
  34. int c, *count;
  35. if (in_init(mod, loc)) {
  36. plt = (void *)mod->arch.init_plt->sh_addr;
  37. plt_end = (void *)plt + mod->arch.init_plt->sh_size;
  38. count = &mod->arch.init_plt_count;
  39. } else {
  40. plt = (void *)mod->arch.core_plt->sh_addr;
  41. plt_end = (void *)plt + mod->arch.core_plt->sh_size;
  42. count = &mod->arch.core_plt_count;
  43. }
  44. /* Look for an existing entry pointing to 'val' */
  45. for (c = *count; plt < plt_end; c -= PLT_ENT_COUNT, plt++) {
  46. int i;
  47. if (!c) {
  48. /* Populate a new set of entries */
  49. *plt = (struct plt_entries){
  50. { [0 ... PLT_ENT_COUNT - 1] = PLT_ENT_LDR, },
  51. { val, }
  52. };
  53. ++*count;
  54. return (u32)plt->ldr;
  55. }
  56. for (i = 0; i < PLT_ENT_COUNT; i++) {
  57. if (!plt->lit[i]) {
  58. plt->lit[i] = val;
  59. ++*count;
  60. }
  61. if (plt->lit[i] == val)
  62. return (u32)&plt->ldr[i];
  63. }
  64. }
  65. BUG();
  66. }
  67. static int duplicate_rel(Elf32_Addr base, const Elf32_Rel *rel, int num,
  68. u32 mask)
  69. {
  70. u32 *loc1, *loc2;
  71. int i;
  72. for (i = 0; i < num; i++) {
  73. if (rel[i].r_info != rel[num].r_info)
  74. continue;
  75. /*
  76. * Identical relocation types against identical symbols can
  77. * still result in different PLT entries if the addend in the
  78. * place is different. So resolve the target of the relocation
  79. * to compare the values.
  80. */
  81. loc1 = (u32 *)(base + rel[i].r_offset);
  82. loc2 = (u32 *)(base + rel[num].r_offset);
  83. if (((*loc1 ^ *loc2) & mask) == 0)
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. /* Count how many PLT entries we may need */
  89. static unsigned int count_plts(Elf32_Addr base, const Elf32_Rel *rel, int num)
  90. {
  91. unsigned int ret = 0;
  92. int i;
  93. /*
  94. * Sure, this is order(n^2), but it's usually short, and not
  95. * time critical
  96. */
  97. for (i = 0; i < num; i++)
  98. switch (ELF32_R_TYPE(rel[i].r_info)) {
  99. case R_ARM_CALL:
  100. case R_ARM_PC24:
  101. case R_ARM_JUMP24:
  102. if (!duplicate_rel(base, rel, i,
  103. __opcode_to_mem_arm(0x00ffffff)))
  104. ret++;
  105. break;
  106. #ifdef CONFIG_THUMB2_KERNEL
  107. case R_ARM_THM_CALL:
  108. case R_ARM_THM_JUMP24:
  109. if (!duplicate_rel(base, rel, i,
  110. __opcode_to_mem_thumb32(0x07ff2fff)))
  111. ret++;
  112. #endif
  113. }
  114. return ret;
  115. }
  116. int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  117. char *secstrings, struct module *mod)
  118. {
  119. unsigned long core_plts = 0, init_plts = 0;
  120. Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
  121. /*
  122. * To store the PLTs, we expand the .text section for core module code
  123. * and the .init.text section for initialization code.
  124. */
  125. for (s = sechdrs; s < sechdrs_end; ++s)
  126. if (strcmp(".core.plt", secstrings + s->sh_name) == 0)
  127. mod->arch.core_plt = s;
  128. else if (strcmp(".init.plt", secstrings + s->sh_name) == 0)
  129. mod->arch.init_plt = s;
  130. if (!mod->arch.core_plt || !mod->arch.init_plt) {
  131. pr_err("%s: sections missing\n", mod->name);
  132. return -ENOEXEC;
  133. }
  134. for (s = sechdrs + 1; s < sechdrs_end; ++s) {
  135. const Elf32_Rel *rels = (void *)ehdr + s->sh_offset;
  136. int numrels = s->sh_size / sizeof(Elf32_Rel);
  137. Elf32_Shdr *dstsec = sechdrs + s->sh_info;
  138. if (s->sh_type != SHT_REL)
  139. continue;
  140. if (strstr(secstrings + s->sh_name, ".init"))
  141. init_plts += count_plts(dstsec->sh_addr, rels, numrels);
  142. else
  143. core_plts += count_plts(dstsec->sh_addr, rels, numrels);
  144. }
  145. mod->arch.core_plt->sh_type = SHT_NOBITS;
  146. mod->arch.core_plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  147. mod->arch.core_plt->sh_addralign = L1_CACHE_BYTES;
  148. mod->arch.core_plt->sh_size = round_up(core_plts * PLT_ENT_SIZE,
  149. sizeof(struct plt_entries));
  150. mod->arch.core_plt_count = 0;
  151. mod->arch.init_plt->sh_type = SHT_NOBITS;
  152. mod->arch.init_plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  153. mod->arch.init_plt->sh_addralign = L1_CACHE_BYTES;
  154. mod->arch.init_plt->sh_size = round_up(init_plts * PLT_ENT_SIZE,
  155. sizeof(struct plt_entries));
  156. mod->arch.init_plt_count = 0;
  157. pr_debug("%s: core.plt=%x, init.plt=%x\n", __func__,
  158. mod->arch.core_plt->sh_size, mod->arch.init_plt->sh_size);
  159. return 0;
  160. }