module-elf32.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. Elf32_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. static int apply_r_mips_none(struct module *me, uint32_t *location,
  54. Elf32_Addr v)
  55. {
  56. return 0;
  57. }
  58. static int apply_r_mips_32(struct module *me, uint32_t *location,
  59. Elf32_Addr v)
  60. {
  61. *location += v;
  62. return 0;
  63. }
  64. static int apply_r_mips_26(struct module *me, uint32_t *location,
  65. Elf32_Addr v)
  66. {
  67. if (v % 4) {
  68. printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
  69. return -ENOEXEC;
  70. }
  71. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  72. printk(KERN_ERR
  73. "module %s: relocation overflow\n",
  74. me->name);
  75. return -ENOEXEC;
  76. }
  77. *location = (*location & ~0x03ffffff) |
  78. ((*location + (v >> 2)) & 0x03ffffff);
  79. return 0;
  80. }
  81. static int apply_r_mips_hi16(struct module *me, uint32_t *location,
  82. Elf32_Addr v)
  83. {
  84. struct mips_hi16 *n;
  85. /*
  86. * We cannot relocate this one now because we don't know the value of
  87. * the carry we need to add. Save the information, and let LO16 do the
  88. * actual relocation.
  89. */
  90. n = kmalloc(sizeof *n, GFP_KERNEL);
  91. if (!n)
  92. return -ENOMEM;
  93. n->addr = location;
  94. n->value = v;
  95. n->next = mips_hi16_list;
  96. mips_hi16_list = n;
  97. return 0;
  98. }
  99. static int apply_r_mips_lo16(struct module *me, uint32_t *location,
  100. Elf32_Addr v)
  101. {
  102. unsigned long insnlo = *location;
  103. Elf32_Addr val, vallo;
  104. /* Sign extend the addend we extract from the lo insn. */
  105. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  106. if (mips_hi16_list != NULL) {
  107. struct mips_hi16 *l;
  108. l = mips_hi16_list;
  109. while (l != NULL) {
  110. struct mips_hi16 *next;
  111. unsigned long insn;
  112. /*
  113. * The value for the HI16 had best be the same.
  114. */
  115. if (v != l->value)
  116. goto out_danger;
  117. /*
  118. * Do the HI16 relocation. Note that we actually don't
  119. * need to know anything about the LO16 itself, except
  120. * where to find the low 16 bits of the addend needed
  121. * by the LO16.
  122. */
  123. insn = *l->addr;
  124. val = ((insn & 0xffff) << 16) + vallo;
  125. val += v;
  126. /*
  127. * Account for the sign extension that will happen in
  128. * the low bits.
  129. */
  130. val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
  131. insn = (insn & ~0xffff) | val;
  132. *l->addr = insn;
  133. next = l->next;
  134. kfree(l);
  135. l = next;
  136. }
  137. mips_hi16_list = NULL;
  138. }
  139. /*
  140. * Ok, we're done with the HI16 relocs. Now deal with the LO16.
  141. */
  142. val = v + vallo;
  143. insnlo = (insnlo & ~0xffff) | (val & 0xffff);
  144. *location = insnlo;
  145. return 0;
  146. out_danger:
  147. printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
  148. return -ENOEXEC;
  149. }
  150. static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
  151. Elf32_Addr v) = {
  152. [R_MIPS_NONE] = apply_r_mips_none,
  153. [R_MIPS_32] = apply_r_mips_32,
  154. [R_MIPS_26] = apply_r_mips_26,
  155. [R_MIPS_HI16] = apply_r_mips_hi16,
  156. [R_MIPS_LO16] = apply_r_mips_lo16
  157. };
  158. int apply_relocate(Elf32_Shdr *sechdrs,
  159. const char *strtab,
  160. unsigned int symindex,
  161. unsigned int relsec,
  162. struct module *me)
  163. {
  164. Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
  165. Elf32_Sym *sym;
  166. uint32_t *location;
  167. unsigned int i;
  168. Elf32_Addr v;
  169. int res;
  170. pr_debug("Applying relocate section %u to %u\n", relsec,
  171. sechdrs[relsec].sh_info);
  172. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  173. Elf32_Word r_info = rel[i].r_info;
  174. /* This is where to make the change */
  175. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  176. + rel[i].r_offset;
  177. /* This is the symbol it is referring to */
  178. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  179. + ELF32_R_SYM(r_info);
  180. if (!sym->st_value) {
  181. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  182. me->name, strtab + sym->st_name);
  183. return -ENOENT;
  184. }
  185. v = sym->st_value;
  186. res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
  187. if (res)
  188. return res;
  189. }
  190. return 0;
  191. }
  192. int apply_relocate_add(Elf32_Shdr *sechdrs,
  193. const char *strtab,
  194. unsigned int symindex,
  195. unsigned int relsec,
  196. struct module *me)
  197. {
  198. /*
  199. * Current binutils always generate .rela relocations. Keep smiling
  200. * if it's empty, abort otherwise.
  201. */
  202. if (!sechdrs[relsec].sh_size)
  203. return 0;
  204. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  205. me->name);
  206. return -ENOEXEC;
  207. }