module.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Kernel module help for Alpha.
  2. Copyright (C) 2002 Richard Henderson.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <linux/moduleloader.h>
  16. #include <linux/elf.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #if 0
  23. #define DEBUGP printk
  24. #else
  25. #define DEBUGP(fmt...)
  26. #endif
  27. void *
  28. module_alloc(unsigned long size)
  29. {
  30. if (size == 0)
  31. return NULL;
  32. return vmalloc(size);
  33. }
  34. void
  35. module_free(struct module *mod, void *module_region)
  36. {
  37. vfree(module_region);
  38. }
  39. /* Allocate the GOT at the end of the core sections. */
  40. struct got_entry {
  41. struct got_entry *next;
  42. Elf64_Sxword r_addend;
  43. int got_offset;
  44. };
  45. static inline void
  46. process_reloc_for_got(Elf64_Rela *rela,
  47. struct got_entry *chains, Elf64_Xword *poffset)
  48. {
  49. unsigned long r_sym = ELF64_R_SYM (rela->r_info);
  50. unsigned long r_type = ELF64_R_TYPE (rela->r_info);
  51. Elf64_Sxword r_addend = rela->r_addend;
  52. struct got_entry *g;
  53. if (r_type != R_ALPHA_LITERAL)
  54. return;
  55. for (g = chains + r_sym; g ; g = g->next)
  56. if (g->r_addend == r_addend) {
  57. if (g->got_offset == 0) {
  58. g->got_offset = *poffset;
  59. *poffset += 8;
  60. }
  61. goto found_entry;
  62. }
  63. g = kmalloc (sizeof (*g), GFP_KERNEL);
  64. g->next = chains[r_sym].next;
  65. g->r_addend = r_addend;
  66. g->got_offset = *poffset;
  67. *poffset += 8;
  68. chains[r_sym].next = g;
  69. found_entry:
  70. /* Trick: most of the ELF64_R_TYPE field is unused. There are
  71. 42 valid relocation types, and a 32-bit field. Co-opt the
  72. bits above 256 to store the got offset for this reloc. */
  73. rela->r_info |= g->got_offset << 8;
  74. }
  75. int
  76. module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs,
  77. char *secstrings, struct module *me)
  78. {
  79. struct got_entry *chains;
  80. Elf64_Rela *rela;
  81. Elf64_Shdr *esechdrs, *symtab, *s, *got;
  82. unsigned long nsyms, nrela, i;
  83. esechdrs = sechdrs + hdr->e_shnum;
  84. symtab = got = NULL;
  85. /* Find out how large the symbol table is. Allocate one got_entry
  86. head per symbol. Normally this will be enough, but not always.
  87. We'll chain different offsets for the symbol down each head. */
  88. for (s = sechdrs; s < esechdrs; ++s)
  89. if (s->sh_type == SHT_SYMTAB)
  90. symtab = s;
  91. else if (!strcmp(".got", secstrings + s->sh_name)) {
  92. got = s;
  93. me->arch.gotsecindex = s - sechdrs;
  94. }
  95. if (!symtab) {
  96. printk(KERN_ERR "module %s: no symbol table\n", me->name);
  97. return -ENOEXEC;
  98. }
  99. if (!got) {
  100. printk(KERN_ERR "module %s: no got section\n", me->name);
  101. return -ENOEXEC;
  102. }
  103. nsyms = symtab->sh_size / sizeof(Elf64_Sym);
  104. chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL);
  105. got->sh_size = 0;
  106. got->sh_addralign = 8;
  107. got->sh_type = SHT_NOBITS;
  108. /* Examine all LITERAL relocations to find out what GOT entries
  109. are required. This sizes the GOT section as well. */
  110. for (s = sechdrs; s < esechdrs; ++s)
  111. if (s->sh_type == SHT_RELA) {
  112. nrela = s->sh_size / sizeof(Elf64_Rela);
  113. rela = (void *)hdr + s->sh_offset;
  114. for (i = 0; i < nrela; ++i)
  115. process_reloc_for_got(rela+i, chains,
  116. &got->sh_size);
  117. }
  118. /* Free the memory we allocated. */
  119. for (i = 0; i < nsyms; ++i) {
  120. struct got_entry *g, *n;
  121. for (g = chains[i].next; g ; g = n) {
  122. n = g->next;
  123. kfree(g);
  124. }
  125. }
  126. kfree(chains);
  127. return 0;
  128. }
  129. int
  130. apply_relocate(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  131. unsigned int relsec, struct module *me)
  132. {
  133. printk(KERN_ERR "module %s: REL relocation unsupported\n", me->name);
  134. return -ENOEXEC;
  135. }
  136. int
  137. apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab,
  138. unsigned int symindex, unsigned int relsec,
  139. struct module *me)
  140. {
  141. Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  142. unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela);
  143. Elf64_Sym *symtab, *sym;
  144. void *base, *location;
  145. unsigned long got, gp;
  146. DEBUGP("Applying relocate section %u to %u\n", relsec,
  147. sechdrs[relsec].sh_info);
  148. base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr;
  149. symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr;
  150. /* The small sections were sorted to the end of the segment.
  151. The following should definitely cover them. */
  152. gp = (u64)me->module_core + me->core_size - 0x8000;
  153. got = sechdrs[me->arch.gotsecindex].sh_addr;
  154. for (i = 0; i < n; i++) {
  155. unsigned long r_sym = ELF64_R_SYM (rela[i].r_info);
  156. unsigned long r_type = ELF64_R_TYPE (rela[i].r_info);
  157. unsigned long r_got_offset = r_type >> 8;
  158. unsigned long value, hi, lo;
  159. r_type &= 0xff;
  160. /* This is where to make the change. */
  161. location = base + rela[i].r_offset;
  162. /* This is the symbol it is referring to. Note that all
  163. unresolved symbols have been resolved. */
  164. sym = symtab + r_sym;
  165. value = sym->st_value + rela[i].r_addend;
  166. switch (r_type) {
  167. case R_ALPHA_NONE:
  168. break;
  169. case R_ALPHA_REFQUAD:
  170. /* BUG() can produce misaligned relocations. */
  171. ((u32 *)location)[0] = value;
  172. ((u32 *)location)[1] = value >> 32;
  173. break;
  174. case R_ALPHA_GPREL32:
  175. value -= gp;
  176. if ((int)value != value)
  177. goto reloc_overflow;
  178. *(u32 *)location = value;
  179. break;
  180. case R_ALPHA_LITERAL:
  181. hi = got + r_got_offset;
  182. lo = hi - gp;
  183. if ((short)lo != lo)
  184. goto reloc_overflow;
  185. *(u16 *)location = lo;
  186. *(u64 *)hi = value;
  187. break;
  188. case R_ALPHA_LITUSE:
  189. break;
  190. case R_ALPHA_GPDISP:
  191. value = gp - (u64)location;
  192. lo = (short)value;
  193. hi = (int)(value - lo);
  194. if (hi + lo != value)
  195. goto reloc_overflow;
  196. *(u16 *)location = hi >> 16;
  197. *(u16 *)(location + rela[i].r_addend) = lo;
  198. break;
  199. case R_ALPHA_BRSGP:
  200. /* BRSGP is only allowed to bind to local symbols.
  201. If the section is undef, this means that the
  202. value was resolved from somewhere else. */
  203. if (sym->st_shndx == SHN_UNDEF)
  204. goto reloc_overflow;
  205. if ((sym->st_other & STO_ALPHA_STD_GPLOAD) ==
  206. STO_ALPHA_STD_GPLOAD)
  207. /* Omit the prologue. */
  208. value += 8;
  209. /* FALLTHRU */
  210. case R_ALPHA_BRADDR:
  211. value -= (u64)location + 4;
  212. if (value & 3)
  213. goto reloc_overflow;
  214. value = (long)value >> 2;
  215. if (value + (1<<21) >= 1<<22)
  216. goto reloc_overflow;
  217. value &= 0x1fffff;
  218. value |= *(u32 *)location & ~0x1fffff;
  219. *(u32 *)location = value;
  220. break;
  221. case R_ALPHA_HINT:
  222. break;
  223. case R_ALPHA_SREL32:
  224. value -= (u64)location;
  225. if ((int)value != value)
  226. goto reloc_overflow;
  227. *(u32 *)location = value;
  228. break;
  229. case R_ALPHA_SREL64:
  230. value -= (u64)location;
  231. *(u64 *)location = value;
  232. break;
  233. case R_ALPHA_GPRELHIGH:
  234. value = (long)(value - gp + 0x8000) >> 16;
  235. if ((short) value != value)
  236. goto reloc_overflow;
  237. *(u16 *)location = value;
  238. break;
  239. case R_ALPHA_GPRELLOW:
  240. value -= gp;
  241. *(u16 *)location = value;
  242. break;
  243. case R_ALPHA_GPREL16:
  244. value -= gp;
  245. if ((short) value != value)
  246. goto reloc_overflow;
  247. *(u16 *)location = value;
  248. break;
  249. default:
  250. printk(KERN_ERR "module %s: Unknown relocation: %lu\n",
  251. me->name, r_type);
  252. return -ENOEXEC;
  253. reloc_overflow:
  254. if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION)
  255. printk(KERN_ERR
  256. "module %s: Relocation (type %lu) overflow vs section %d\n",
  257. me->name, r_type, sym->st_shndx);
  258. else
  259. printk(KERN_ERR
  260. "module %s: Relocation (type %lu) overflow vs %s\n",
  261. me->name, r_type, strtab + sym->st_name);
  262. return -ENOEXEC;
  263. }
  264. }
  265. return 0;
  266. }
  267. int
  268. module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  269. struct module *me)
  270. {
  271. return 0;
  272. }
  273. void
  274. module_arch_cleanup(struct module *mod)
  275. {
  276. }