module.c 8.1 KB

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