module.c 15 KB

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