module.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Kernel module help for s390.
  3. *
  4. * S390 version
  5. * Copyright IBM Corp. 2002, 2003
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  8. *
  9. * based on i386 version
  10. * Copyright (C) 2001 Rusty Russell.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/module.h>
  27. #include <linux/elf.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/fs.h>
  30. #include <linux/string.h>
  31. #include <linux/kernel.h>
  32. #include <linux/moduleloader.h>
  33. #include <linux/bug.h>
  34. #if 0
  35. #define DEBUGP printk
  36. #else
  37. #define DEBUGP(fmt , ...)
  38. #endif
  39. #ifndef CONFIG_64BIT
  40. #define PLT_ENTRY_SIZE 12
  41. #else /* CONFIG_64BIT */
  42. #define PLT_ENTRY_SIZE 20
  43. #endif /* CONFIG_64BIT */
  44. #ifdef CONFIG_64BIT
  45. void *module_alloc(unsigned long size)
  46. {
  47. if (PAGE_ALIGN(size) > MODULES_LEN)
  48. return NULL;
  49. return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  50. GFP_KERNEL, PAGE_KERNEL, -1,
  51. __builtin_return_address(0));
  52. }
  53. #endif
  54. /* Free memory returned from module_alloc */
  55. void module_free(struct module *mod, void *module_region)
  56. {
  57. if (mod) {
  58. vfree(mod->arch.syminfo);
  59. mod->arch.syminfo = NULL;
  60. }
  61. vfree(module_region);
  62. }
  63. static void
  64. check_rela(Elf_Rela *rela, struct module *me)
  65. {
  66. struct mod_arch_syminfo *info;
  67. info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
  68. switch (ELF_R_TYPE (rela->r_info)) {
  69. case R_390_GOT12: /* 12 bit GOT offset. */
  70. case R_390_GOT16: /* 16 bit GOT offset. */
  71. case R_390_GOT20: /* 20 bit GOT offset. */
  72. case R_390_GOT32: /* 32 bit GOT offset. */
  73. case R_390_GOT64: /* 64 bit GOT offset. */
  74. case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
  75. case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
  76. case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
  77. case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
  78. case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
  79. case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
  80. case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
  81. if (info->got_offset == -1UL) {
  82. info->got_offset = me->arch.got_size;
  83. me->arch.got_size += sizeof(void*);
  84. }
  85. break;
  86. case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
  87. case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
  88. case R_390_PLT32: /* 32 bit PC relative PLT address. */
  89. case R_390_PLT64: /* 64 bit PC relative PLT address. */
  90. case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
  91. case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
  92. case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
  93. if (info->plt_offset == -1UL) {
  94. info->plt_offset = me->arch.plt_size;
  95. me->arch.plt_size += PLT_ENTRY_SIZE;
  96. }
  97. break;
  98. case R_390_COPY:
  99. case R_390_GLOB_DAT:
  100. case R_390_JMP_SLOT:
  101. case R_390_RELATIVE:
  102. /* Only needed if we want to support loading of
  103. modules linked with -shared. */
  104. break;
  105. }
  106. }
  107. /*
  108. * Account for GOT and PLT relocations. We can't add sections for
  109. * got and plt but we can increase the core module size.
  110. */
  111. int
  112. module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  113. char *secstrings, struct module *me)
  114. {
  115. Elf_Shdr *symtab;
  116. Elf_Sym *symbols;
  117. Elf_Rela *rela;
  118. char *strings;
  119. int nrela, i, j;
  120. /* Find symbol table and string table. */
  121. symtab = NULL;
  122. for (i = 0; i < hdr->e_shnum; i++)
  123. switch (sechdrs[i].sh_type) {
  124. case SHT_SYMTAB:
  125. symtab = sechdrs + i;
  126. break;
  127. }
  128. if (!symtab) {
  129. printk(KERN_ERR "module %s: no symbol table\n", me->name);
  130. return -ENOEXEC;
  131. }
  132. /* Allocate one syminfo structure per symbol. */
  133. me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
  134. me->arch.syminfo = vmalloc(me->arch.nsyms *
  135. sizeof(struct mod_arch_syminfo));
  136. if (!me->arch.syminfo)
  137. return -ENOMEM;
  138. symbols = (void *) hdr + symtab->sh_offset;
  139. strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
  140. for (i = 0; i < me->arch.nsyms; i++) {
  141. if (symbols[i].st_shndx == SHN_UNDEF &&
  142. strcmp(strings + symbols[i].st_name,
  143. "_GLOBAL_OFFSET_TABLE_") == 0)
  144. /* "Define" it as absolute. */
  145. symbols[i].st_shndx = SHN_ABS;
  146. me->arch.syminfo[i].got_offset = -1UL;
  147. me->arch.syminfo[i].plt_offset = -1UL;
  148. me->arch.syminfo[i].got_initialized = 0;
  149. me->arch.syminfo[i].plt_initialized = 0;
  150. }
  151. /* Search for got/plt relocations. */
  152. me->arch.got_size = me->arch.plt_size = 0;
  153. for (i = 0; i < hdr->e_shnum; i++) {
  154. if (sechdrs[i].sh_type != SHT_RELA)
  155. continue;
  156. nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
  157. rela = (void *) hdr + sechdrs[i].sh_offset;
  158. for (j = 0; j < nrela; j++)
  159. check_rela(rela + j, me);
  160. }
  161. /* Increase core size by size of got & plt and set start
  162. offsets for got and plt. */
  163. me->core_size = ALIGN(me->core_size, 4);
  164. me->arch.got_offset = me->core_size;
  165. me->core_size += me->arch.got_size;
  166. me->arch.plt_offset = me->core_size;
  167. me->core_size += me->arch.plt_size;
  168. return 0;
  169. }
  170. static int
  171. apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
  172. struct module *me)
  173. {
  174. struct mod_arch_syminfo *info;
  175. Elf_Addr loc, val;
  176. int r_type, r_sym;
  177. /* This is where to make the change */
  178. loc = base + rela->r_offset;
  179. /* This is the symbol it is referring to. Note that all
  180. undefined symbols have been resolved. */
  181. r_sym = ELF_R_SYM(rela->r_info);
  182. r_type = ELF_R_TYPE(rela->r_info);
  183. info = me->arch.syminfo + r_sym;
  184. val = symtab[r_sym].st_value;
  185. switch (r_type) {
  186. case R_390_8: /* Direct 8 bit. */
  187. case R_390_12: /* Direct 12 bit. */
  188. case R_390_16: /* Direct 16 bit. */
  189. case R_390_20: /* Direct 20 bit. */
  190. case R_390_32: /* Direct 32 bit. */
  191. case R_390_64: /* Direct 64 bit. */
  192. val += rela->r_addend;
  193. if (r_type == R_390_8)
  194. *(unsigned char *) loc = val;
  195. else if (r_type == R_390_12)
  196. *(unsigned short *) loc = (val & 0xfff) |
  197. (*(unsigned short *) loc & 0xf000);
  198. else if (r_type == R_390_16)
  199. *(unsigned short *) loc = val;
  200. else if (r_type == R_390_20)
  201. *(unsigned int *) loc =
  202. (*(unsigned int *) loc & 0xf00000ff) |
  203. (val & 0xfff) << 16 | (val & 0xff000) >> 4;
  204. else if (r_type == R_390_32)
  205. *(unsigned int *) loc = val;
  206. else if (r_type == R_390_64)
  207. *(unsigned long *) loc = val;
  208. break;
  209. case R_390_PC16: /* PC relative 16 bit. */
  210. case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
  211. case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
  212. case R_390_PC32: /* PC relative 32 bit. */
  213. case R_390_PC64: /* PC relative 64 bit. */
  214. val += rela->r_addend - loc;
  215. if (r_type == R_390_PC16)
  216. *(unsigned short *) loc = val;
  217. else if (r_type == R_390_PC16DBL)
  218. *(unsigned short *) loc = val >> 1;
  219. else if (r_type == R_390_PC32DBL)
  220. *(unsigned int *) loc = val >> 1;
  221. else if (r_type == R_390_PC32)
  222. *(unsigned int *) loc = val;
  223. else if (r_type == R_390_PC64)
  224. *(unsigned long *) loc = val;
  225. break;
  226. case R_390_GOT12: /* 12 bit GOT offset. */
  227. case R_390_GOT16: /* 16 bit GOT offset. */
  228. case R_390_GOT20: /* 20 bit GOT offset. */
  229. case R_390_GOT32: /* 32 bit GOT offset. */
  230. case R_390_GOT64: /* 64 bit GOT offset. */
  231. case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
  232. case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
  233. case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
  234. case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
  235. case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
  236. case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
  237. case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
  238. if (info->got_initialized == 0) {
  239. Elf_Addr *gotent;
  240. gotent = me->module_core + me->arch.got_offset +
  241. info->got_offset;
  242. *gotent = val;
  243. info->got_initialized = 1;
  244. }
  245. val = info->got_offset + rela->r_addend;
  246. if (r_type == R_390_GOT12 ||
  247. r_type == R_390_GOTPLT12)
  248. *(unsigned short *) loc = (val & 0xfff) |
  249. (*(unsigned short *) loc & 0xf000);
  250. else if (r_type == R_390_GOT16 ||
  251. r_type == R_390_GOTPLT16)
  252. *(unsigned short *) loc = val;
  253. else if (r_type == R_390_GOT20 ||
  254. r_type == R_390_GOTPLT20)
  255. *(unsigned int *) loc =
  256. (*(unsigned int *) loc & 0xf00000ff) |
  257. (val & 0xfff) << 16 | (val & 0xff000) >> 4;
  258. else if (r_type == R_390_GOT32 ||
  259. r_type == R_390_GOTPLT32)
  260. *(unsigned int *) loc = val;
  261. else if (r_type == R_390_GOTENT ||
  262. r_type == R_390_GOTPLTENT)
  263. *(unsigned int *) loc =
  264. (val + (Elf_Addr) me->module_core - loc) >> 1;
  265. else if (r_type == R_390_GOT64 ||
  266. r_type == R_390_GOTPLT64)
  267. *(unsigned long *) loc = val;
  268. break;
  269. case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
  270. case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
  271. case R_390_PLT32: /* 32 bit PC relative PLT address. */
  272. case R_390_PLT64: /* 64 bit PC relative PLT address. */
  273. case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
  274. case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
  275. case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
  276. if (info->plt_initialized == 0) {
  277. unsigned int *ip;
  278. ip = me->module_core + me->arch.plt_offset +
  279. info->plt_offset;
  280. #ifndef CONFIG_64BIT
  281. ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
  282. ip[1] = 0x100607f1;
  283. ip[2] = val;
  284. #else /* CONFIG_64BIT */
  285. ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
  286. ip[1] = 0x100a0004;
  287. ip[2] = 0x07f10000;
  288. ip[3] = (unsigned int) (val >> 32);
  289. ip[4] = (unsigned int) val;
  290. #endif /* CONFIG_64BIT */
  291. info->plt_initialized = 1;
  292. }
  293. if (r_type == R_390_PLTOFF16 ||
  294. r_type == R_390_PLTOFF32 ||
  295. r_type == R_390_PLTOFF64)
  296. val = me->arch.plt_offset - me->arch.got_offset +
  297. info->plt_offset + rela->r_addend;
  298. else {
  299. if (!((r_type == R_390_PLT16DBL &&
  300. val - loc + 0xffffUL < 0x1ffffeUL) ||
  301. (r_type == R_390_PLT32DBL &&
  302. val - loc + 0xffffffffULL < 0x1fffffffeULL)))
  303. val = (Elf_Addr) me->module_core +
  304. me->arch.plt_offset +
  305. info->plt_offset;
  306. val += rela->r_addend - loc;
  307. }
  308. if (r_type == R_390_PLT16DBL)
  309. *(unsigned short *) loc = val >> 1;
  310. else if (r_type == R_390_PLTOFF16)
  311. *(unsigned short *) loc = val;
  312. else if (r_type == R_390_PLT32DBL)
  313. *(unsigned int *) loc = val >> 1;
  314. else if (r_type == R_390_PLT32 ||
  315. r_type == R_390_PLTOFF32)
  316. *(unsigned int *) loc = val;
  317. else if (r_type == R_390_PLT64 ||
  318. r_type == R_390_PLTOFF64)
  319. *(unsigned long *) loc = val;
  320. break;
  321. case R_390_GOTOFF16: /* 16 bit offset to GOT. */
  322. case R_390_GOTOFF32: /* 32 bit offset to GOT. */
  323. case R_390_GOTOFF64: /* 64 bit offset to GOT. */
  324. val = val + rela->r_addend -
  325. ((Elf_Addr) me->module_core + me->arch.got_offset);
  326. if (r_type == R_390_GOTOFF16)
  327. *(unsigned short *) loc = val;
  328. else if (r_type == R_390_GOTOFF32)
  329. *(unsigned int *) loc = val;
  330. else if (r_type == R_390_GOTOFF64)
  331. *(unsigned long *) loc = val;
  332. break;
  333. case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
  334. case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
  335. val = (Elf_Addr) me->module_core + me->arch.got_offset +
  336. rela->r_addend - loc;
  337. if (r_type == R_390_GOTPC)
  338. *(unsigned int *) loc = val;
  339. else if (r_type == R_390_GOTPCDBL)
  340. *(unsigned int *) loc = val >> 1;
  341. break;
  342. case R_390_COPY:
  343. case R_390_GLOB_DAT: /* Create GOT entry. */
  344. case R_390_JMP_SLOT: /* Create PLT entry. */
  345. case R_390_RELATIVE: /* Adjust by program base. */
  346. /* Only needed if we want to support loading of
  347. modules linked with -shared. */
  348. break;
  349. default:
  350. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  351. me->name, r_type);
  352. return -ENOEXEC;
  353. }
  354. return 0;
  355. }
  356. int
  357. apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  358. unsigned int symindex, unsigned int relsec,
  359. struct module *me)
  360. {
  361. Elf_Addr base;
  362. Elf_Sym *symtab;
  363. Elf_Rela *rela;
  364. unsigned long i, n;
  365. int rc;
  366. DEBUGP("Applying relocate section %u to %u\n",
  367. relsec, sechdrs[relsec].sh_info);
  368. base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
  369. symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
  370. rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
  371. n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
  372. for (i = 0; i < n; i++, rela++) {
  373. rc = apply_rela(rela, base, symtab, me);
  374. if (rc)
  375. return rc;
  376. }
  377. return 0;
  378. }
  379. int module_finalize(const Elf_Ehdr *hdr,
  380. const Elf_Shdr *sechdrs,
  381. struct module *me)
  382. {
  383. vfree(me->arch.syminfo);
  384. me->arch.syminfo = NULL;
  385. return 0;
  386. }