module-elf64.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Copyright (C) 2001 Rusty Russell.
  17. * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
  18. */
  19. #undef DEBUG
  20. #include <linux/moduleloader.h>
  21. #include <linux/elf.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/slab.h>
  24. #include <linux/fs.h>
  25. #include <linux/string.h>
  26. #include <linux/kernel.h>
  27. struct mips_hi16 {
  28. struct mips_hi16 *next;
  29. Elf32_Addr *addr;
  30. Elf64_Addr value;
  31. };
  32. static struct mips_hi16 *mips_hi16_list;
  33. void *module_alloc(unsigned long size)
  34. {
  35. if (size == 0)
  36. return NULL;
  37. return vmalloc(size);
  38. }
  39. /* Free memory returned from module_alloc */
  40. void module_free(struct module *mod, void *module_region)
  41. {
  42. vfree(module_region);
  43. /* FIXME: If module_region == mod->init_region, trim exception
  44. table entries. */
  45. }
  46. int module_frob_arch_sections(Elf_Ehdr *hdr,
  47. Elf_Shdr *sechdrs,
  48. char *secstrings,
  49. struct module *mod)
  50. {
  51. return 0;
  52. }
  53. int apply_relocate(Elf64_Shdr *sechdrs,
  54. const char *strtab,
  55. unsigned int symindex,
  56. unsigned int relsec,
  57. struct module *me)
  58. {
  59. /*
  60. * We don't want to deal with REL relocations - RELA is so much saner.
  61. */
  62. if (!sechdrs[relsec].sh_size)
  63. return 0;
  64. printk(KERN_ERR "module %s: REL relocation unsupported\n",
  65. me->name);
  66. return -ENOEXEC;
  67. }
  68. static int apply_r_mips_none(struct module *me, uint32_t *location,
  69. Elf64_Addr v)
  70. {
  71. return 0;
  72. }
  73. static int apply_r_mips_32(struct module *me, uint32_t *location,
  74. Elf64_Addr v)
  75. {
  76. *location = v;
  77. return 0;
  78. }
  79. static int apply_r_mips_26(struct module *me, uint32_t *location,
  80. Elf64_Addr v)
  81. {
  82. if (v % 4) {
  83. printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
  84. return -ENOEXEC;
  85. }
  86. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  87. printk(KERN_ERR
  88. "module %s: relocation overflow\n",
  89. me->name);
  90. return -ENOEXEC;
  91. }
  92. *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
  93. return 0;
  94. }
  95. static int apply_r_mips_hi16(struct module *me, uint32_t *location,
  96. Elf64_Addr v)
  97. {
  98. struct mips_hi16 *n;
  99. /*
  100. * We cannot relocate this one now because we don't know the value of
  101. * the carry we need to add. Save the information, and let LO16 do the
  102. * actual relocation.
  103. */
  104. n = kmalloc(sizeof *n, GFP_KERNEL);
  105. if (!n)
  106. return -ENOMEM;
  107. n->addr = location;
  108. n->value = v;
  109. n->next = mips_hi16_list;
  110. mips_hi16_list = n;
  111. return 0;
  112. }
  113. static int apply_r_mips_lo16(struct module *me, uint32_t *location,
  114. Elf64_Addr v)
  115. {
  116. unsigned long insnlo = *location;
  117. Elf32_Addr val, vallo;
  118. /* Sign extend the addend we extract from the lo insn. */
  119. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  120. if (mips_hi16_list != NULL) {
  121. struct mips_hi16 *l;
  122. l = mips_hi16_list;
  123. while (l != NULL) {
  124. struct mips_hi16 *next;
  125. unsigned long insn;
  126. /*
  127. * The value for the HI16 had best be the same.
  128. */
  129. if (v != l->value)
  130. goto out_danger;
  131. /*
  132. * Do the HI16 relocation. Note that we actually don't
  133. * need to know anything about the LO16 itself, except
  134. * where to find the low 16 bits of the addend needed
  135. * by the LO16.
  136. */
  137. insn = *l->addr;
  138. val = ((insn & 0xffff) << 16) + vallo;
  139. val += v;
  140. /*
  141. * Account for the sign extension that will happen in
  142. * the low bits.
  143. */
  144. val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
  145. insn = (insn & ~0xffff) | val;
  146. *l->addr = insn;
  147. next = l->next;
  148. kfree(l);
  149. l = next;
  150. }
  151. mips_hi16_list = NULL;
  152. }
  153. /*
  154. * Ok, we're done with the HI16 relocs. Now deal with the LO16.
  155. */
  156. insnlo = (insnlo & ~0xffff) | (v & 0xffff);
  157. *location = insnlo;
  158. return 0;
  159. out_danger:
  160. printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
  161. return -ENOEXEC;
  162. }
  163. static int apply_r_mips_64(struct module *me, uint32_t *location,
  164. Elf64_Addr v)
  165. {
  166. *(uint64_t *) location = v;
  167. return 0;
  168. }
  169. static int apply_r_mips_higher(struct module *me, uint32_t *location,
  170. Elf64_Addr v)
  171. {
  172. *location = (*location & 0xffff0000) |
  173. ((((long long) v + 0x80008000LL) >> 32) & 0xffff);
  174. return 0;
  175. }
  176. static int apply_r_mips_highest(struct module *me, uint32_t *location,
  177. Elf64_Addr v)
  178. {
  179. *location = (*location & 0xffff0000) |
  180. ((((long long) v + 0x800080008000LL) >> 48) & 0xffff);
  181. return 0;
  182. }
  183. static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
  184. Elf64_Addr v) = {
  185. [R_MIPS_NONE] = apply_r_mips_none,
  186. [R_MIPS_32] = apply_r_mips_32,
  187. [R_MIPS_26] = apply_r_mips_26,
  188. [R_MIPS_HI16] = apply_r_mips_hi16,
  189. [R_MIPS_LO16] = apply_r_mips_lo16,
  190. [R_MIPS_64] = apply_r_mips_64,
  191. [R_MIPS_HIGHER] = apply_r_mips_higher,
  192. [R_MIPS_HIGHEST] = apply_r_mips_highest
  193. };
  194. int apply_relocate_add(Elf64_Shdr *sechdrs,
  195. const char *strtab,
  196. unsigned int symindex,
  197. unsigned int relsec,
  198. struct module *me)
  199. {
  200. Elf64_Mips_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  201. Elf64_Sym *sym;
  202. uint32_t *location;
  203. unsigned int i;
  204. Elf64_Addr v;
  205. int res;
  206. pr_debug("Applying relocate section %u to %u\n", relsec,
  207. sechdrs[relsec].sh_info);
  208. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  209. /* This is where to make the change */
  210. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  211. + rel[i].r_offset;
  212. /* This is the symbol it is referring to */
  213. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr + rel[i].r_sym;
  214. if (!sym->st_value) {
  215. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  216. me->name, strtab + sym->st_name);
  217. return -ENOENT;
  218. }
  219. v = sym->st_value;
  220. res = reloc_handlers[rel[i].r_type](me, location, v);
  221. if (res)
  222. return res;
  223. }
  224. return 0;
  225. }