module.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* MN10300 Kernel module helper routines
  2. *
  3. * Copyright (C) 2007, 2008, 2009 Red Hat, Inc. All Rights Reserved.
  4. * Written by Mark Salter (msalter@redhat.com)
  5. * - Derived from arch/i386/kernel/module.c
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public Licence as published by
  9. * the Free Software Foundation; either version 2 of the Licence, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public Licence for more details.
  16. *
  17. * You should have received a copy of the GNU General Public Licence
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/moduleloader.h>
  22. #include <linux/elf.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/fs.h>
  25. #include <linux/string.h>
  26. #include <linux/kernel.h>
  27. #include <linux/bug.h>
  28. #if 0
  29. #define DEBUGP printk
  30. #else
  31. #define DEBUGP(fmt, ...)
  32. #endif
  33. /*
  34. * allocate storage for a module
  35. */
  36. void *module_alloc(unsigned long size)
  37. {
  38. if (size == 0)
  39. return NULL;
  40. return vmalloc_exec(size);
  41. }
  42. /*
  43. * free memory returned from module_alloc()
  44. */
  45. void module_free(struct module *mod, void *module_region)
  46. {
  47. vfree(module_region);
  48. }
  49. /*
  50. * allow the arch to fix up the section table
  51. * - we don't need anything special
  52. */
  53. int module_frob_arch_sections(Elf_Ehdr *hdr,
  54. Elf_Shdr *sechdrs,
  55. char *secstrings,
  56. struct module *mod)
  57. {
  58. return 0;
  59. }
  60. static void reloc_put16(uint8_t *p, uint32_t val)
  61. {
  62. p[0] = val & 0xff;
  63. p[1] = (val >> 8) & 0xff;
  64. }
  65. static void reloc_put24(uint8_t *p, uint32_t val)
  66. {
  67. reloc_put16(p, val);
  68. p[2] = (val >> 16) & 0xff;
  69. }
  70. static void reloc_put32(uint8_t *p, uint32_t val)
  71. {
  72. reloc_put16(p, val);
  73. reloc_put16(p+2, val >> 16);
  74. }
  75. /*
  76. * apply a REL relocation
  77. */
  78. int apply_relocate(Elf32_Shdr *sechdrs,
  79. const char *strtab,
  80. unsigned int symindex,
  81. unsigned int relsec,
  82. struct module *me)
  83. {
  84. printk(KERN_ERR "module %s: RELOCATION unsupported\n",
  85. me->name);
  86. return -ENOEXEC;
  87. }
  88. /*
  89. * apply a RELA relocation
  90. */
  91. int apply_relocate_add(Elf32_Shdr *sechdrs,
  92. const char *strtab,
  93. unsigned int symindex,
  94. unsigned int relsec,
  95. struct module *me)
  96. {
  97. unsigned int i, sym_diff_seen = 0;
  98. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  99. Elf32_Sym *sym;
  100. Elf32_Addr relocation, sym_diff_val = 0;
  101. uint8_t *location;
  102. uint32_t value;
  103. DEBUGP("Applying relocate section %u to %u\n",
  104. relsec, sechdrs[relsec].sh_info);
  105. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  106. /* this is where to make the change */
  107. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  108. + rel[i].r_offset;
  109. /* this is the symbol the relocation is referring to (note that
  110. * all undefined symbols have been resolved by the caller) */
  111. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  112. + ELF32_R_SYM(rel[i].r_info);
  113. /* this is the adjustment to be made */
  114. relocation = sym->st_value + rel[i].r_addend;
  115. if (sym_diff_seen) {
  116. switch (ELF32_R_TYPE(rel[i].r_info)) {
  117. case R_MN10300_32:
  118. case R_MN10300_24:
  119. case R_MN10300_16:
  120. case R_MN10300_8:
  121. relocation -= sym_diff_val;
  122. sym_diff_seen = 0;
  123. break;
  124. default:
  125. printk(KERN_ERR "module %s: Unexpected SYM_DIFF relocation: %u\n",
  126. me->name, ELF32_R_TYPE(rel[i].r_info));
  127. return -ENOEXEC;
  128. }
  129. }
  130. switch (ELF32_R_TYPE(rel[i].r_info)) {
  131. /* for the first four relocation types, we simply
  132. * store the adjustment at the location given */
  133. case R_MN10300_32:
  134. reloc_put32(location, relocation);
  135. break;
  136. case R_MN10300_24:
  137. reloc_put24(location, relocation);
  138. break;
  139. case R_MN10300_16:
  140. reloc_put16(location, relocation);
  141. break;
  142. case R_MN10300_8:
  143. *location = relocation;
  144. break;
  145. /* for the next three relocation types, we write the
  146. * adjustment with the address subtracted over the
  147. * value at the location given */
  148. case R_MN10300_PCREL32:
  149. value = relocation - (uint32_t) location;
  150. reloc_put32(location, value);
  151. break;
  152. case R_MN10300_PCREL16:
  153. value = relocation - (uint32_t) location;
  154. reloc_put16(location, value);
  155. break;
  156. case R_MN10300_PCREL8:
  157. *location = relocation - (uint32_t) location;
  158. break;
  159. case R_MN10300_SYM_DIFF:
  160. /* This is used to adjust the next reloc as required
  161. * by relaxation. */
  162. sym_diff_seen = 1;
  163. sym_diff_val = sym->st_value;
  164. break;
  165. case R_MN10300_ALIGN:
  166. /* Just ignore the ALIGN relocs.
  167. * Only interesting if kernel performed relaxation. */
  168. continue;
  169. default:
  170. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  171. me->name, ELF32_R_TYPE(rel[i].r_info));
  172. return -ENOEXEC;
  173. }
  174. }
  175. if (sym_diff_seen) {
  176. printk(KERN_ERR "module %s: Nothing follows SYM_DIFF relocation: %u\n",
  177. me->name, ELF32_R_TYPE(rel[i].r_info));
  178. return -ENOEXEC;
  179. }
  180. return 0;
  181. }
  182. /*
  183. * finish loading the module
  184. */
  185. int module_finalize(const Elf_Ehdr *hdr,
  186. const Elf_Shdr *sechdrs,
  187. struct module *me)
  188. {
  189. return module_bug_finalize(hdr, sechdrs, me);
  190. }
  191. /*
  192. * finish clearing the module
  193. */
  194. void module_arch_cleanup(struct module *mod)
  195. {
  196. module_bug_cleanup(mod);
  197. }