module.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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, 0, NUMA_NO_NODE,
  51. __builtin_return_address(0));
  52. }
  53. #endif
  54. void module_arch_freeing_init(struct module *mod)
  55. {
  56. vfree(mod->arch.syminfo);
  57. mod->arch.syminfo = NULL;
  58. }
  59. static void check_rela(Elf_Rela *rela, struct module *me)
  60. {
  61. struct mod_arch_syminfo *info;
  62. info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
  63. switch (ELF_R_TYPE (rela->r_info)) {
  64. case R_390_GOT12: /* 12 bit GOT offset. */
  65. case R_390_GOT16: /* 16 bit GOT offset. */
  66. case R_390_GOT20: /* 20 bit GOT offset. */
  67. case R_390_GOT32: /* 32 bit GOT offset. */
  68. case R_390_GOT64: /* 64 bit GOT offset. */
  69. case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
  70. case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
  71. case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
  72. case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
  73. case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
  74. case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
  75. case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
  76. if (info->got_offset == -1UL) {
  77. info->got_offset = me->arch.got_size;
  78. me->arch.got_size += sizeof(void*);
  79. }
  80. break;
  81. case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
  82. case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
  83. case R_390_PLT32: /* 32 bit PC relative PLT address. */
  84. case R_390_PLT64: /* 64 bit PC relative PLT address. */
  85. case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
  86. case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
  87. case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
  88. if (info->plt_offset == -1UL) {
  89. info->plt_offset = me->arch.plt_size;
  90. me->arch.plt_size += PLT_ENTRY_SIZE;
  91. }
  92. break;
  93. case R_390_COPY:
  94. case R_390_GLOB_DAT:
  95. case R_390_JMP_SLOT:
  96. case R_390_RELATIVE:
  97. /* Only needed if we want to support loading of
  98. modules linked with -shared. */
  99. break;
  100. }
  101. }
  102. /*
  103. * Account for GOT and PLT relocations. We can't add sections for
  104. * got and plt but we can increase the core module size.
  105. */
  106. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  107. char *secstrings, struct module *me)
  108. {
  109. Elf_Shdr *symtab;
  110. Elf_Sym *symbols;
  111. Elf_Rela *rela;
  112. char *strings;
  113. int nrela, i, j;
  114. /* Find symbol table and string table. */
  115. symtab = NULL;
  116. for (i = 0; i < hdr->e_shnum; i++)
  117. switch (sechdrs[i].sh_type) {
  118. case SHT_SYMTAB:
  119. symtab = sechdrs + i;
  120. break;
  121. }
  122. if (!symtab) {
  123. printk(KERN_ERR "module %s: no symbol table\n", me->name);
  124. return -ENOEXEC;
  125. }
  126. /* Allocate one syminfo structure per symbol. */
  127. me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
  128. me->arch.syminfo = vmalloc(me->arch.nsyms *
  129. sizeof(struct mod_arch_syminfo));
  130. if (!me->arch.syminfo)
  131. return -ENOMEM;
  132. symbols = (void *) hdr + symtab->sh_offset;
  133. strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
  134. for (i = 0; i < me->arch.nsyms; i++) {
  135. if (symbols[i].st_shndx == SHN_UNDEF &&
  136. strcmp(strings + symbols[i].st_name,
  137. "_GLOBAL_OFFSET_TABLE_") == 0)
  138. /* "Define" it as absolute. */
  139. symbols[i].st_shndx = SHN_ABS;
  140. me->arch.syminfo[i].got_offset = -1UL;
  141. me->arch.syminfo[i].plt_offset = -1UL;
  142. me->arch.syminfo[i].got_initialized = 0;
  143. me->arch.syminfo[i].plt_initialized = 0;
  144. }
  145. /* Search for got/plt relocations. */
  146. me->arch.got_size = me->arch.plt_size = 0;
  147. for (i = 0; i < hdr->e_shnum; i++) {
  148. if (sechdrs[i].sh_type != SHT_RELA)
  149. continue;
  150. nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
  151. rela = (void *) hdr + sechdrs[i].sh_offset;
  152. for (j = 0; j < nrela; j++)
  153. check_rela(rela + j, me);
  154. }
  155. /* Increase core size by size of got & plt and set start
  156. offsets for got and plt. */
  157. me->core_size = ALIGN(me->core_size, 4);
  158. me->arch.got_offset = me->core_size;
  159. me->core_size += me->arch.got_size;
  160. me->arch.plt_offset = me->core_size;
  161. me->core_size += me->arch.plt_size;
  162. return 0;
  163. }
  164. static int apply_rela_bits(Elf_Addr loc, Elf_Addr val,
  165. int sign, int bits, int shift)
  166. {
  167. unsigned long umax;
  168. long min, max;
  169. if (val & ((1UL << shift) - 1))
  170. return -ENOEXEC;
  171. if (sign) {
  172. val = (Elf_Addr)(((long) val) >> shift);
  173. min = -(1L << (bits - 1));
  174. max = (1L << (bits - 1)) - 1;
  175. if ((long) val < min || (long) val > max)
  176. return -ENOEXEC;
  177. } else {
  178. val >>= shift;
  179. umax = ((1UL << (bits - 1)) << 1) - 1;
  180. if ((unsigned long) val > umax)
  181. return -ENOEXEC;
  182. }
  183. if (bits == 8)
  184. *(unsigned char *) loc = val;
  185. else if (bits == 12)
  186. *(unsigned short *) loc = (val & 0xfff) |
  187. (*(unsigned short *) loc & 0xf000);
  188. else if (bits == 16)
  189. *(unsigned short *) loc = val;
  190. else if (bits == 20)
  191. *(unsigned int *) loc = (val & 0xfff) << 16 |
  192. (val & 0xff000) >> 4 |
  193. (*(unsigned int *) loc & 0xf00000ff);
  194. else if (bits == 32)
  195. *(unsigned int *) loc = val;
  196. else if (bits == 64)
  197. *(unsigned long *) loc = val;
  198. return 0;
  199. }
  200. static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
  201. const char *strtab, struct module *me)
  202. {
  203. struct mod_arch_syminfo *info;
  204. Elf_Addr loc, val;
  205. int r_type, r_sym;
  206. int rc = -ENOEXEC;
  207. /* This is where to make the change */
  208. loc = base + rela->r_offset;
  209. /* This is the symbol it is referring to. Note that all
  210. undefined symbols have been resolved. */
  211. r_sym = ELF_R_SYM(rela->r_info);
  212. r_type = ELF_R_TYPE(rela->r_info);
  213. info = me->arch.syminfo + r_sym;
  214. val = symtab[r_sym].st_value;
  215. switch (r_type) {
  216. case R_390_NONE: /* No relocation. */
  217. rc = 0;
  218. break;
  219. case R_390_8: /* Direct 8 bit. */
  220. case R_390_12: /* Direct 12 bit. */
  221. case R_390_16: /* Direct 16 bit. */
  222. case R_390_20: /* Direct 20 bit. */
  223. case R_390_32: /* Direct 32 bit. */
  224. case R_390_64: /* Direct 64 bit. */
  225. val += rela->r_addend;
  226. if (r_type == R_390_8)
  227. rc = apply_rela_bits(loc, val, 0, 8, 0);
  228. else if (r_type == R_390_12)
  229. rc = apply_rela_bits(loc, val, 0, 12, 0);
  230. else if (r_type == R_390_16)
  231. rc = apply_rela_bits(loc, val, 0, 16, 0);
  232. else if (r_type == R_390_20)
  233. rc = apply_rela_bits(loc, val, 1, 20, 0);
  234. else if (r_type == R_390_32)
  235. rc = apply_rela_bits(loc, val, 0, 32, 0);
  236. else if (r_type == R_390_64)
  237. rc = apply_rela_bits(loc, val, 0, 64, 0);
  238. break;
  239. case R_390_PC16: /* PC relative 16 bit. */
  240. case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
  241. case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
  242. case R_390_PC32: /* PC relative 32 bit. */
  243. case R_390_PC64: /* PC relative 64 bit. */
  244. val += rela->r_addend - loc;
  245. if (r_type == R_390_PC16)
  246. rc = apply_rela_bits(loc, val, 1, 16, 0);
  247. else if (r_type == R_390_PC16DBL)
  248. rc = apply_rela_bits(loc, val, 1, 16, 1);
  249. else if (r_type == R_390_PC32DBL)
  250. rc = apply_rela_bits(loc, val, 1, 32, 1);
  251. else if (r_type == R_390_PC32)
  252. rc = apply_rela_bits(loc, val, 1, 32, 0);
  253. else if (r_type == R_390_PC64)
  254. rc = apply_rela_bits(loc, val, 1, 64, 0);
  255. break;
  256. case R_390_GOT12: /* 12 bit GOT offset. */
  257. case R_390_GOT16: /* 16 bit GOT offset. */
  258. case R_390_GOT20: /* 20 bit GOT offset. */
  259. case R_390_GOT32: /* 32 bit GOT offset. */
  260. case R_390_GOT64: /* 64 bit GOT offset. */
  261. case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
  262. case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
  263. case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
  264. case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
  265. case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
  266. case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
  267. case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
  268. if (info->got_initialized == 0) {
  269. Elf_Addr *gotent;
  270. gotent = me->module_core + me->arch.got_offset +
  271. info->got_offset;
  272. *gotent = val;
  273. info->got_initialized = 1;
  274. }
  275. val = info->got_offset + rela->r_addend;
  276. if (r_type == R_390_GOT12 ||
  277. r_type == R_390_GOTPLT12)
  278. rc = apply_rela_bits(loc, val, 0, 12, 0);
  279. else if (r_type == R_390_GOT16 ||
  280. r_type == R_390_GOTPLT16)
  281. rc = apply_rela_bits(loc, val, 0, 16, 0);
  282. else if (r_type == R_390_GOT20 ||
  283. r_type == R_390_GOTPLT20)
  284. rc = apply_rela_bits(loc, val, 1, 20, 0);
  285. else if (r_type == R_390_GOT32 ||
  286. r_type == R_390_GOTPLT32)
  287. rc = apply_rela_bits(loc, val, 0, 32, 0);
  288. else if (r_type == R_390_GOT64 ||
  289. r_type == R_390_GOTPLT64)
  290. rc = apply_rela_bits(loc, val, 0, 64, 0);
  291. else if (r_type == R_390_GOTENT ||
  292. r_type == R_390_GOTPLTENT) {
  293. val += (Elf_Addr) me->module_core - loc;
  294. rc = apply_rela_bits(loc, val, 1, 32, 1);
  295. }
  296. break;
  297. case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
  298. case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
  299. case R_390_PLT32: /* 32 bit PC relative PLT address. */
  300. case R_390_PLT64: /* 64 bit PC relative PLT address. */
  301. case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
  302. case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
  303. case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
  304. if (info->plt_initialized == 0) {
  305. unsigned int *ip;
  306. ip = me->module_core + me->arch.plt_offset +
  307. info->plt_offset;
  308. #ifndef CONFIG_64BIT
  309. ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
  310. ip[1] = 0x100607f1;
  311. ip[2] = val;
  312. #else /* CONFIG_64BIT */
  313. ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
  314. ip[1] = 0x100a0004;
  315. ip[2] = 0x07f10000;
  316. ip[3] = (unsigned int) (val >> 32);
  317. ip[4] = (unsigned int) val;
  318. #endif /* CONFIG_64BIT */
  319. info->plt_initialized = 1;
  320. }
  321. if (r_type == R_390_PLTOFF16 ||
  322. r_type == R_390_PLTOFF32 ||
  323. r_type == R_390_PLTOFF64)
  324. val = me->arch.plt_offset - me->arch.got_offset +
  325. info->plt_offset + rela->r_addend;
  326. else {
  327. if (!((r_type == R_390_PLT16DBL &&
  328. val - loc + 0xffffUL < 0x1ffffeUL) ||
  329. (r_type == R_390_PLT32DBL &&
  330. val - loc + 0xffffffffULL < 0x1fffffffeULL)))
  331. val = (Elf_Addr) me->module_core +
  332. me->arch.plt_offset +
  333. info->plt_offset;
  334. val += rela->r_addend - loc;
  335. }
  336. if (r_type == R_390_PLT16DBL)
  337. rc = apply_rela_bits(loc, val, 1, 16, 1);
  338. else if (r_type == R_390_PLTOFF16)
  339. rc = apply_rela_bits(loc, val, 0, 16, 0);
  340. else if (r_type == R_390_PLT32DBL)
  341. rc = apply_rela_bits(loc, val, 1, 32, 1);
  342. else if (r_type == R_390_PLT32 ||
  343. r_type == R_390_PLTOFF32)
  344. rc = apply_rela_bits(loc, val, 0, 32, 0);
  345. else if (r_type == R_390_PLT64 ||
  346. r_type == R_390_PLTOFF64)
  347. rc = apply_rela_bits(loc, val, 0, 64, 0);
  348. break;
  349. case R_390_GOTOFF16: /* 16 bit offset to GOT. */
  350. case R_390_GOTOFF32: /* 32 bit offset to GOT. */
  351. case R_390_GOTOFF64: /* 64 bit offset to GOT. */
  352. val = val + rela->r_addend -
  353. ((Elf_Addr) me->module_core + me->arch.got_offset);
  354. if (r_type == R_390_GOTOFF16)
  355. rc = apply_rela_bits(loc, val, 0, 16, 0);
  356. else if (r_type == R_390_GOTOFF32)
  357. rc = apply_rela_bits(loc, val, 0, 32, 0);
  358. else if (r_type == R_390_GOTOFF64)
  359. rc = apply_rela_bits(loc, val, 0, 64, 0);
  360. break;
  361. case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
  362. case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
  363. val = (Elf_Addr) me->module_core + me->arch.got_offset +
  364. rela->r_addend - loc;
  365. if (r_type == R_390_GOTPC)
  366. rc = apply_rela_bits(loc, val, 1, 32, 0);
  367. else if (r_type == R_390_GOTPCDBL)
  368. rc = apply_rela_bits(loc, val, 1, 32, 1);
  369. break;
  370. case R_390_COPY:
  371. case R_390_GLOB_DAT: /* Create GOT entry. */
  372. case R_390_JMP_SLOT: /* Create PLT entry. */
  373. case R_390_RELATIVE: /* Adjust by program base. */
  374. /* Only needed if we want to support loading of
  375. modules linked with -shared. */
  376. return -ENOEXEC;
  377. default:
  378. printk(KERN_ERR "module %s: unknown relocation: %u\n",
  379. me->name, r_type);
  380. return -ENOEXEC;
  381. }
  382. if (rc) {
  383. printk(KERN_ERR "module %s: relocation error for symbol %s "
  384. "(r_type %i, value 0x%lx)\n",
  385. me->name, strtab + symtab[r_sym].st_name,
  386. r_type, (unsigned long) val);
  387. return rc;
  388. }
  389. return 0;
  390. }
  391. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  392. unsigned int symindex, unsigned int relsec,
  393. struct module *me)
  394. {
  395. Elf_Addr base;
  396. Elf_Sym *symtab;
  397. Elf_Rela *rela;
  398. unsigned long i, n;
  399. int rc;
  400. DEBUGP("Applying relocate section %u to %u\n",
  401. relsec, sechdrs[relsec].sh_info);
  402. base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
  403. symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
  404. rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
  405. n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
  406. for (i = 0; i < n; i++, rela++) {
  407. rc = apply_rela(rela, base, symtab, strtab, me);
  408. if (rc)
  409. return rc;
  410. }
  411. return 0;
  412. }
  413. int module_finalize(const Elf_Ehdr *hdr,
  414. const Elf_Shdr *sechdrs,
  415. struct module *me)
  416. {
  417. vfree(me->arch.syminfo);
  418. me->arch.syminfo = NULL;
  419. return 0;
  420. }