module.c 7.6 KB

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