module.c 14 KB

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