module.c 14 KB

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