module.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. if (is_livepatch_module(mod) &&
  54. mod->state == MODULE_STATE_LIVE)
  55. return;
  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_layout.size = ALIGN(me->core_layout.size, 4);
  158. me->arch.got_offset = me->core_layout.size;
  159. me->core_layout.size += me->arch.got_size;
  160. me->arch.plt_offset = me->core_layout.size;
  161. if (me->arch.plt_size) {
  162. if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable)
  163. me->arch.plt_size += PLT_ENTRY_SIZE;
  164. me->core_layout.size += me->arch.plt_size;
  165. }
  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->core_layout.base + 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->core_layout.base - 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->core_layout.base + me->arch.plt_offset +
  311. info->plt_offset;
  312. ip[0] = 0x0d10e310; /* basr 1,0 */
  313. ip[1] = 0x100a0004; /* lg 1,10(1) */
  314. if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) {
  315. unsigned int *ij;
  316. ij = me->core_layout.base +
  317. me->arch.plt_offset +
  318. me->arch.plt_size - PLT_ENTRY_SIZE;
  319. ip[2] = 0xa7f40000 + /* j __jump_r1 */
  320. (unsigned int)(u16)
  321. (((unsigned long) ij - 8 -
  322. (unsigned long) ip) / 2);
  323. } else {
  324. ip[2] = 0x07f10000; /* br %r1 */
  325. }
  326. ip[3] = (unsigned int) (val >> 32);
  327. ip[4] = (unsigned int) val;
  328. info->plt_initialized = 1;
  329. }
  330. if (r_type == R_390_PLTOFF16 ||
  331. r_type == R_390_PLTOFF32 ||
  332. r_type == R_390_PLTOFF64)
  333. val = me->arch.plt_offset - me->arch.got_offset +
  334. info->plt_offset + rela->r_addend;
  335. else {
  336. if (!((r_type == R_390_PLT16DBL &&
  337. val - loc + 0xffffUL < 0x1ffffeUL) ||
  338. (r_type == R_390_PLT32DBL &&
  339. val - loc + 0xffffffffULL < 0x1fffffffeULL)))
  340. val = (Elf_Addr) me->core_layout.base +
  341. me->arch.plt_offset +
  342. info->plt_offset;
  343. val += rela->r_addend - loc;
  344. }
  345. if (r_type == R_390_PLT16DBL)
  346. rc = apply_rela_bits(loc, val, 1, 16, 1);
  347. else if (r_type == R_390_PLTOFF16)
  348. rc = apply_rela_bits(loc, val, 0, 16, 0);
  349. else if (r_type == R_390_PLT32DBL)
  350. rc = apply_rela_bits(loc, val, 1, 32, 1);
  351. else if (r_type == R_390_PLT32 ||
  352. r_type == R_390_PLTOFF32)
  353. rc = apply_rela_bits(loc, val, 0, 32, 0);
  354. else if (r_type == R_390_PLT64 ||
  355. r_type == R_390_PLTOFF64)
  356. rc = apply_rela_bits(loc, val, 0, 64, 0);
  357. break;
  358. case R_390_GOTOFF16: /* 16 bit offset to GOT. */
  359. case R_390_GOTOFF32: /* 32 bit offset to GOT. */
  360. case R_390_GOTOFF64: /* 64 bit offset to GOT. */
  361. val = val + rela->r_addend -
  362. ((Elf_Addr) me->core_layout.base + me->arch.got_offset);
  363. if (r_type == R_390_GOTOFF16)
  364. rc = apply_rela_bits(loc, val, 0, 16, 0);
  365. else if (r_type == R_390_GOTOFF32)
  366. rc = apply_rela_bits(loc, val, 0, 32, 0);
  367. else if (r_type == R_390_GOTOFF64)
  368. rc = apply_rela_bits(loc, val, 0, 64, 0);
  369. break;
  370. case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
  371. case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
  372. val = (Elf_Addr) me->core_layout.base + me->arch.got_offset +
  373. rela->r_addend - loc;
  374. if (r_type == R_390_GOTPC)
  375. rc = apply_rela_bits(loc, val, 1, 32, 0);
  376. else if (r_type == R_390_GOTPCDBL)
  377. rc = apply_rela_bits(loc, val, 1, 32, 1);
  378. break;
  379. case R_390_COPY:
  380. case R_390_GLOB_DAT: /* Create GOT entry. */
  381. case R_390_JMP_SLOT: /* Create PLT entry. */
  382. case R_390_RELATIVE: /* Adjust by program base. */
  383. /* Only needed if we want to support loading of
  384. modules linked with -shared. */
  385. return -ENOEXEC;
  386. default:
  387. printk(KERN_ERR "module %s: unknown relocation: %u\n",
  388. me->name, r_type);
  389. return -ENOEXEC;
  390. }
  391. if (rc) {
  392. printk(KERN_ERR "module %s: relocation error for symbol %s "
  393. "(r_type %i, value 0x%lx)\n",
  394. me->name, strtab + symtab[r_sym].st_name,
  395. r_type, (unsigned long) val);
  396. return rc;
  397. }
  398. return 0;
  399. }
  400. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  401. unsigned int symindex, unsigned int relsec,
  402. struct module *me)
  403. {
  404. Elf_Addr base;
  405. Elf_Sym *symtab;
  406. Elf_Rela *rela;
  407. unsigned long i, n;
  408. int rc;
  409. DEBUGP("Applying relocate section %u to %u\n",
  410. relsec, sechdrs[relsec].sh_info);
  411. base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
  412. symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
  413. rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
  414. n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
  415. for (i = 0; i < n; i++, rela++) {
  416. rc = apply_rela(rela, base, symtab, strtab, me);
  417. if (rc)
  418. return rc;
  419. }
  420. return 0;
  421. }
  422. int module_finalize(const Elf_Ehdr *hdr,
  423. const Elf_Shdr *sechdrs,
  424. struct module *me)
  425. {
  426. const Elf_Shdr *s;
  427. char *secstrings, *secname;
  428. void *aseg;
  429. if (IS_ENABLED(CONFIG_EXPOLINE) &&
  430. !nospec_disable && me->arch.plt_size) {
  431. unsigned int *ij;
  432. ij = me->core_layout.base + me->arch.plt_offset +
  433. me->arch.plt_size - PLT_ENTRY_SIZE;
  434. if (test_facility(35)) {
  435. ij[0] = 0xc6000000; /* exrl %r0,.+10 */
  436. ij[1] = 0x0005a7f4; /* j . */
  437. ij[2] = 0x000007f1; /* br %r1 */
  438. } else {
  439. ij[0] = 0x44000000 | (unsigned int)
  440. offsetof(struct lowcore, br_r1_trampoline);
  441. ij[1] = 0xa7f40000; /* j . */
  442. }
  443. }
  444. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  445. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  446. aseg = (void *) s->sh_addr;
  447. secname = secstrings + s->sh_name;
  448. if (!strcmp(".altinstructions", secname))
  449. /* patch .altinstructions */
  450. apply_alternatives(aseg, aseg + s->sh_size);
  451. if (IS_ENABLED(CONFIG_EXPOLINE) &&
  452. (!strncmp(".s390_indirect", secname, 14)))
  453. nospec_revert(aseg, aseg + s->sh_size);
  454. if (IS_ENABLED(CONFIG_EXPOLINE) &&
  455. (!strncmp(".s390_return", secname, 12)))
  456. nospec_revert(aseg, aseg + s->sh_size);
  457. }
  458. jump_label_apply_nops(me);
  459. return 0;
  460. }