module.c 14 KB

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