module.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 = kmalloc(nsyms * sizeof(struct got_entry), GFP_KERNEL);
  105. memset(chains, 0, nsyms * sizeof(struct got_entry));
  106. got->sh_size = 0;
  107. got->sh_addralign = 8;
  108. got->sh_type = SHT_NOBITS;
  109. /* Examine all LITERAL relocations to find out what GOT entries
  110. are required. This sizes the GOT section as well. */
  111. for (s = sechdrs; s < esechdrs; ++s)
  112. if (s->sh_type == SHT_RELA) {
  113. nrela = s->sh_size / sizeof(Elf64_Rela);
  114. rela = (void *)hdr + s->sh_offset;
  115. for (i = 0; i < nrela; ++i)
  116. process_reloc_for_got(rela+i, chains,
  117. &got->sh_size);
  118. }
  119. /* Free the memory we allocated. */
  120. for (i = 0; i < nsyms; ++i) {
  121. struct got_entry *g, *n;
  122. for (g = chains[i].next; g ; g = n) {
  123. n = g->next;
  124. kfree(g);
  125. }
  126. }
  127. kfree(chains);
  128. return 0;
  129. }
  130. int
  131. apply_relocate(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  132. unsigned int relsec, struct module *me)
  133. {
  134. printk(KERN_ERR "module %s: REL relocation unsupported\n", me->name);
  135. return -ENOEXEC;
  136. }
  137. int
  138. apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab,
  139. unsigned int symindex, unsigned int relsec,
  140. struct module *me)
  141. {
  142. Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  143. unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela);
  144. Elf64_Sym *symtab, *sym;
  145. void *base, *location;
  146. unsigned long got, gp;
  147. DEBUGP("Applying relocate section %u to %u\n", relsec,
  148. sechdrs[relsec].sh_info);
  149. base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr;
  150. symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr;
  151. /* The small sections were sorted to the end of the segment.
  152. The following should definitely cover them. */
  153. gp = (u64)me->module_core + me->core_size - 0x8000;
  154. got = sechdrs[me->arch.gotsecindex].sh_addr;
  155. for (i = 0; i < n; i++) {
  156. unsigned long r_sym = ELF64_R_SYM (rela[i].r_info);
  157. unsigned long r_type = ELF64_R_TYPE (rela[i].r_info);
  158. unsigned long r_got_offset = r_type >> 8;
  159. unsigned long value, hi, lo;
  160. r_type &= 0xff;
  161. /* This is where to make the change. */
  162. location = base + rela[i].r_offset;
  163. /* This is the symbol it is referring to. Note that all
  164. unresolved symbols have been resolved. */
  165. sym = symtab + r_sym;
  166. value = sym->st_value + rela[i].r_addend;
  167. switch (r_type) {
  168. case R_ALPHA_NONE:
  169. break;
  170. case R_ALPHA_REFQUAD:
  171. /* BUG() can produce misaligned relocations. */
  172. ((u32 *)location)[0] = value;
  173. ((u32 *)location)[1] = value >> 32;
  174. break;
  175. case R_ALPHA_GPREL32:
  176. value -= gp;
  177. if ((int)value != value)
  178. goto reloc_overflow;
  179. *(u32 *)location = value;
  180. break;
  181. case R_ALPHA_LITERAL:
  182. hi = got + r_got_offset;
  183. lo = hi - gp;
  184. if ((short)lo != lo)
  185. goto reloc_overflow;
  186. *(u16 *)location = lo;
  187. *(u64 *)hi = value;
  188. break;
  189. case R_ALPHA_LITUSE:
  190. break;
  191. case R_ALPHA_GPDISP:
  192. value = gp - (u64)location;
  193. lo = (short)value;
  194. hi = (int)(value - lo);
  195. if (hi + lo != value)
  196. goto reloc_overflow;
  197. *(u16 *)location = hi >> 16;
  198. *(u16 *)(location + rela[i].r_addend) = lo;
  199. break;
  200. case R_ALPHA_BRSGP:
  201. /* BRSGP is only allowed to bind to local symbols.
  202. If the section is undef, this means that the
  203. value was resolved from somewhere else. */
  204. if (sym->st_shndx == SHN_UNDEF)
  205. goto reloc_overflow;
  206. if ((sym->st_other & STO_ALPHA_STD_GPLOAD) ==
  207. STO_ALPHA_STD_GPLOAD)
  208. /* Omit the prologue. */
  209. value += 8;
  210. /* FALLTHRU */
  211. case R_ALPHA_BRADDR:
  212. value -= (u64)location + 4;
  213. if (value & 3)
  214. goto reloc_overflow;
  215. value = (long)value >> 2;
  216. if (value + (1<<21) >= 1<<22)
  217. goto reloc_overflow;
  218. value &= 0x1fffff;
  219. value |= *(u32 *)location & ~0x1fffff;
  220. *(u32 *)location = value;
  221. break;
  222. case R_ALPHA_HINT:
  223. break;
  224. case R_ALPHA_SREL32:
  225. value -= (u64)location;
  226. if ((int)value != value)
  227. goto reloc_overflow;
  228. *(u32 *)location = value;
  229. break;
  230. case R_ALPHA_SREL64:
  231. value -= (u64)location;
  232. *(u64 *)location = value;
  233. break;
  234. case R_ALPHA_GPRELHIGH:
  235. value = (long)(value - gp + 0x8000) >> 16;
  236. if ((short) value != value)
  237. goto reloc_overflow;
  238. *(u16 *)location = value;
  239. break;
  240. case R_ALPHA_GPRELLOW:
  241. value -= gp;
  242. *(u16 *)location = value;
  243. break;
  244. case R_ALPHA_GPREL16:
  245. value -= gp;
  246. if ((short) value != value)
  247. goto reloc_overflow;
  248. *(u16 *)location = value;
  249. break;
  250. default:
  251. printk(KERN_ERR "module %s: Unknown relocation: %lu\n",
  252. me->name, r_type);
  253. return -ENOEXEC;
  254. reloc_overflow:
  255. if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION)
  256. printk(KERN_ERR
  257. "module %s: Relocation overflow vs section %d\n",
  258. me->name, sym->st_shndx);
  259. else
  260. printk(KERN_ERR
  261. "module %s: Relocation overflow vs %s\n",
  262. me->name, strtab + sym->st_name);
  263. return -ENOEXEC;
  264. }
  265. }
  266. return 0;
  267. }
  268. int
  269. module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  270. struct module *me)
  271. {
  272. return 0;
  273. }
  274. void
  275. module_arch_cleanup(struct module *mod)
  276. {
  277. }