module.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* MN10300 Kernel module helper routines
  2. *
  3. * Copyright (C) 2007 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. #if 0
  28. #define DEBUGP printk
  29. #else
  30. #define DEBUGP(fmt, ...)
  31. #endif
  32. /*
  33. * allocate storage for a module
  34. */
  35. void *module_alloc(unsigned long size)
  36. {
  37. if (size == 0)
  38. return NULL;
  39. return vmalloc_exec(size);
  40. }
  41. /*
  42. * free memory returned from module_alloc()
  43. */
  44. void module_free(struct module *mod, void *module_region)
  45. {
  46. vfree(module_region);
  47. /* FIXME: If module_region == mod->init_region, trim exception
  48. * table entries. */
  49. }
  50. /*
  51. * allow the arch to fix up the section table
  52. * - we don't need anything special
  53. */
  54. int module_frob_arch_sections(Elf_Ehdr *hdr,
  55. Elf_Shdr *sechdrs,
  56. char *secstrings,
  57. struct module *mod)
  58. {
  59. return 0;
  60. }
  61. static uint32_t reloc_get16(uint8_t *p)
  62. {
  63. return p[0] | (p[1] << 8);
  64. }
  65. static uint32_t reloc_get24(uint8_t *p)
  66. {
  67. return reloc_get16(p) | (p[2] << 16);
  68. }
  69. static uint32_t reloc_get32(uint8_t *p)
  70. {
  71. return reloc_get16(p) | (reloc_get16(p+2) << 16);
  72. }
  73. static void reloc_put16(uint8_t *p, uint32_t val)
  74. {
  75. p[0] = val & 0xff;
  76. p[1] = (val >> 8) & 0xff;
  77. }
  78. static void reloc_put24(uint8_t *p, uint32_t val)
  79. {
  80. reloc_put16(p, val);
  81. p[2] = (val >> 16) & 0xff;
  82. }
  83. static void reloc_put32(uint8_t *p, uint32_t val)
  84. {
  85. reloc_put16(p, val);
  86. reloc_put16(p+2, val >> 16);
  87. }
  88. /*
  89. * apply a REL relocation
  90. */
  91. int apply_relocate(Elf32_Shdr *sechdrs,
  92. const char *strtab,
  93. unsigned int symindex,
  94. unsigned int relsec,
  95. struct module *me)
  96. {
  97. printk(KERN_ERR "module %s: RELOCATION unsupported\n",
  98. me->name);
  99. return -ENOEXEC;
  100. }
  101. /*
  102. * apply a RELA relocation
  103. */
  104. int apply_relocate_add(Elf32_Shdr *sechdrs,
  105. const char *strtab,
  106. unsigned int symindex,
  107. unsigned int relsec,
  108. struct module *me)
  109. {
  110. unsigned int i;
  111. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  112. Elf32_Sym *sym;
  113. Elf32_Addr relocation;
  114. uint8_t *location;
  115. uint32_t value;
  116. DEBUGP("Applying relocate section %u to %u\n",
  117. relsec, sechdrs[relsec].sh_info);
  118. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  119. /* this is where to make the change */
  120. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  121. + rel[i].r_offset;
  122. /* this is the symbol the relocation is referring to (note that
  123. * all undefined symbols have been resolved by the caller) */
  124. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  125. + ELF32_R_SYM(rel[i].r_info);
  126. /* this is the adjustment to be made */
  127. relocation = sym->st_value + rel[i].r_addend;
  128. switch (ELF32_R_TYPE(rel[i].r_info)) {
  129. /* for the first four relocation types, we add the
  130. * adjustment into the value at the location given */
  131. case R_MN10300_32:
  132. value = reloc_get32(location);
  133. value += relocation;
  134. reloc_put32(location, value);
  135. break;
  136. case R_MN10300_24:
  137. value = reloc_get24(location);
  138. value += relocation;
  139. reloc_put24(location, value);
  140. break;
  141. case R_MN10300_16:
  142. value = reloc_get16(location);
  143. value += relocation;
  144. reloc_put16(location, value);
  145. break;
  146. case R_MN10300_8:
  147. *location += relocation;
  148. break;
  149. /* for the next three relocation types, we write the
  150. * adjustment with the address subtracted over the
  151. * value at the location given */
  152. case R_MN10300_PCREL32:
  153. value = relocation - (uint32_t) location;
  154. reloc_put32(location, value);
  155. break;
  156. case R_MN10300_PCREL16:
  157. value = relocation - (uint32_t) location;
  158. reloc_put16(location, value);
  159. break;
  160. case R_MN10300_PCREL8:
  161. *location = relocation - (uint32_t) location;
  162. break;
  163. default:
  164. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  165. me->name, ELF32_R_TYPE(rel[i].r_info));
  166. return -ENOEXEC;
  167. }
  168. }
  169. return 0;
  170. }
  171. /*
  172. * finish loading the module
  173. */
  174. int module_finalize(const Elf_Ehdr *hdr,
  175. const Elf_Shdr *sechdrs,
  176. struct module *me)
  177. {
  178. return 0;
  179. }
  180. /*
  181. * finish clearing the module
  182. */
  183. void module_arch_cleanup(struct module *mod)
  184. {
  185. }