module.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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, 0, NUMA_NO_NODE,
  46. __builtin_return_address(0));
  47. }
  48. void module_arch_freeing_init(struct module *mod)
  49. {
  50. if (is_livepatch_module(mod) &&
  51. mod->state == MODULE_STATE_LIVE)
  52. return;
  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_layout.size = ALIGN(me->core_layout.size, 4);
  155. me->arch.got_offset = me->core_layout.size;
  156. me->core_layout.size += me->arch.got_size;
  157. me->arch.plt_offset = me->core_layout.size;
  158. me->core_layout.size += me->arch.plt_size;
  159. return 0;
  160. }
  161. static int apply_rela_bits(Elf_Addr loc, Elf_Addr val,
  162. int sign, int bits, int shift)
  163. {
  164. unsigned long umax;
  165. long min, max;
  166. if (val & ((1UL << shift) - 1))
  167. return -ENOEXEC;
  168. if (sign) {
  169. val = (Elf_Addr)(((long) val) >> shift);
  170. min = -(1L << (bits - 1));
  171. max = (1L << (bits - 1)) - 1;
  172. if ((long) val < min || (long) val > max)
  173. return -ENOEXEC;
  174. } else {
  175. val >>= shift;
  176. umax = ((1UL << (bits - 1)) << 1) - 1;
  177. if ((unsigned long) val > umax)
  178. return -ENOEXEC;
  179. }
  180. if (bits == 8)
  181. *(unsigned char *) loc = val;
  182. else if (bits == 12)
  183. *(unsigned short *) loc = (val & 0xfff) |
  184. (*(unsigned short *) loc & 0xf000);
  185. else if (bits == 16)
  186. *(unsigned short *) loc = val;
  187. else if (bits == 20)
  188. *(unsigned int *) loc = (val & 0xfff) << 16 |
  189. (val & 0xff000) >> 4 |
  190. (*(unsigned int *) loc & 0xf00000ff);
  191. else if (bits == 32)
  192. *(unsigned int *) loc = val;
  193. else if (bits == 64)
  194. *(unsigned long *) loc = val;
  195. return 0;
  196. }
  197. static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
  198. const char *strtab, struct module *me)
  199. {
  200. struct mod_arch_syminfo *info;
  201. Elf_Addr loc, val;
  202. int r_type, r_sym;
  203. int rc = -ENOEXEC;
  204. /* This is where to make the change */
  205. loc = base + rela->r_offset;
  206. /* This is the symbol it is referring to. Note that all
  207. undefined symbols have been resolved. */
  208. r_sym = ELF_R_SYM(rela->r_info);
  209. r_type = ELF_R_TYPE(rela->r_info);
  210. info = me->arch.syminfo + r_sym;
  211. val = symtab[r_sym].st_value;
  212. switch (r_type) {
  213. case R_390_NONE: /* No relocation. */
  214. rc = 0;
  215. break;
  216. case R_390_8: /* Direct 8 bit. */
  217. case R_390_12: /* Direct 12 bit. */
  218. case R_390_16: /* Direct 16 bit. */
  219. case R_390_20: /* Direct 20 bit. */
  220. case R_390_32: /* Direct 32 bit. */
  221. case R_390_64: /* Direct 64 bit. */
  222. val += rela->r_addend;
  223. if (r_type == R_390_8)
  224. rc = apply_rela_bits(loc, val, 0, 8, 0);
  225. else if (r_type == R_390_12)
  226. rc = apply_rela_bits(loc, val, 0, 12, 0);
  227. else if (r_type == R_390_16)
  228. rc = apply_rela_bits(loc, val, 0, 16, 0);
  229. else if (r_type == R_390_20)
  230. rc = apply_rela_bits(loc, val, 1, 20, 0);
  231. else if (r_type == R_390_32)
  232. rc = apply_rela_bits(loc, val, 0, 32, 0);
  233. else if (r_type == R_390_64)
  234. rc = apply_rela_bits(loc, val, 0, 64, 0);
  235. break;
  236. case R_390_PC16: /* PC relative 16 bit. */
  237. case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
  238. case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
  239. case R_390_PC32: /* PC relative 32 bit. */
  240. case R_390_PC64: /* PC relative 64 bit. */
  241. val += rela->r_addend - loc;
  242. if (r_type == R_390_PC16)
  243. rc = apply_rela_bits(loc, val, 1, 16, 0);
  244. else if (r_type == R_390_PC16DBL)
  245. rc = apply_rela_bits(loc, val, 1, 16, 1);
  246. else if (r_type == R_390_PC32DBL)
  247. rc = apply_rela_bits(loc, val, 1, 32, 1);
  248. else if (r_type == R_390_PC32)
  249. rc = apply_rela_bits(loc, val, 1, 32, 0);
  250. else if (r_type == R_390_PC64)
  251. rc = apply_rela_bits(loc, val, 1, 64, 0);
  252. break;
  253. case R_390_GOT12: /* 12 bit GOT offset. */
  254. case R_390_GOT16: /* 16 bit GOT offset. */
  255. case R_390_GOT20: /* 20 bit GOT offset. */
  256. case R_390_GOT32: /* 32 bit GOT offset. */
  257. case R_390_GOT64: /* 64 bit GOT offset. */
  258. case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
  259. case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
  260. case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
  261. case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
  262. case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
  263. case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
  264. case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
  265. if (info->got_initialized == 0) {
  266. Elf_Addr *gotent;
  267. gotent = me->core_layout.base + me->arch.got_offset +
  268. info->got_offset;
  269. *gotent = val;
  270. info->got_initialized = 1;
  271. }
  272. val = info->got_offset + rela->r_addend;
  273. if (r_type == R_390_GOT12 ||
  274. r_type == R_390_GOTPLT12)
  275. rc = apply_rela_bits(loc, val, 0, 12, 0);
  276. else if (r_type == R_390_GOT16 ||
  277. r_type == R_390_GOTPLT16)
  278. rc = apply_rela_bits(loc, val, 0, 16, 0);
  279. else if (r_type == R_390_GOT20 ||
  280. r_type == R_390_GOTPLT20)
  281. rc = apply_rela_bits(loc, val, 1, 20, 0);
  282. else if (r_type == R_390_GOT32 ||
  283. r_type == R_390_GOTPLT32)
  284. rc = apply_rela_bits(loc, val, 0, 32, 0);
  285. else if (r_type == R_390_GOT64 ||
  286. r_type == R_390_GOTPLT64)
  287. rc = apply_rela_bits(loc, val, 0, 64, 0);
  288. else if (r_type == R_390_GOTENT ||
  289. r_type == R_390_GOTPLTENT) {
  290. val += (Elf_Addr) me->core_layout.base - loc;
  291. rc = apply_rela_bits(loc, val, 1, 32, 1);
  292. }
  293. break;
  294. case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
  295. case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
  296. case R_390_PLT32: /* 32 bit PC relative PLT address. */
  297. case R_390_PLT64: /* 64 bit PC relative PLT address. */
  298. case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
  299. case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
  300. case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
  301. if (info->plt_initialized == 0) {
  302. unsigned int *ip;
  303. ip = me->core_layout.base + me->arch.plt_offset +
  304. info->plt_offset;
  305. ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
  306. ip[1] = 0x100a0004;
  307. ip[2] = 0x07f10000;
  308. ip[3] = (unsigned int) (val >> 32);
  309. ip[4] = (unsigned int) val;
  310. info->plt_initialized = 1;
  311. }
  312. if (r_type == R_390_PLTOFF16 ||
  313. r_type == R_390_PLTOFF32 ||
  314. r_type == R_390_PLTOFF64)
  315. val = me->arch.plt_offset - me->arch.got_offset +
  316. info->plt_offset + rela->r_addend;
  317. else {
  318. if (!((r_type == R_390_PLT16DBL &&
  319. val - loc + 0xffffUL < 0x1ffffeUL) ||
  320. (r_type == R_390_PLT32DBL &&
  321. val - loc + 0xffffffffULL < 0x1fffffffeULL)))
  322. val = (Elf_Addr) me->core_layout.base +
  323. me->arch.plt_offset +
  324. info->plt_offset;
  325. val += rela->r_addend - loc;
  326. }
  327. if (r_type == R_390_PLT16DBL)
  328. rc = apply_rela_bits(loc, val, 1, 16, 1);
  329. else if (r_type == R_390_PLTOFF16)
  330. rc = apply_rela_bits(loc, val, 0, 16, 0);
  331. else if (r_type == R_390_PLT32DBL)
  332. rc = apply_rela_bits(loc, val, 1, 32, 1);
  333. else if (r_type == R_390_PLT32 ||
  334. r_type == R_390_PLTOFF32)
  335. rc = apply_rela_bits(loc, val, 0, 32, 0);
  336. else if (r_type == R_390_PLT64 ||
  337. r_type == R_390_PLTOFF64)
  338. rc = apply_rela_bits(loc, val, 0, 64, 0);
  339. break;
  340. case R_390_GOTOFF16: /* 16 bit offset to GOT. */
  341. case R_390_GOTOFF32: /* 32 bit offset to GOT. */
  342. case R_390_GOTOFF64: /* 64 bit offset to GOT. */
  343. val = val + rela->r_addend -
  344. ((Elf_Addr) me->core_layout.base + me->arch.got_offset);
  345. if (r_type == R_390_GOTOFF16)
  346. rc = apply_rela_bits(loc, val, 0, 16, 0);
  347. else if (r_type == R_390_GOTOFF32)
  348. rc = apply_rela_bits(loc, val, 0, 32, 0);
  349. else if (r_type == R_390_GOTOFF64)
  350. rc = apply_rela_bits(loc, val, 0, 64, 0);
  351. break;
  352. case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
  353. case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
  354. val = (Elf_Addr) me->core_layout.base + me->arch.got_offset +
  355. rela->r_addend - loc;
  356. if (r_type == R_390_GOTPC)
  357. rc = apply_rela_bits(loc, val, 1, 32, 0);
  358. else if (r_type == R_390_GOTPCDBL)
  359. rc = apply_rela_bits(loc, val, 1, 32, 1);
  360. break;
  361. case R_390_COPY:
  362. case R_390_GLOB_DAT: /* Create GOT entry. */
  363. case R_390_JMP_SLOT: /* Create PLT entry. */
  364. case R_390_RELATIVE: /* Adjust by program base. */
  365. /* Only needed if we want to support loading of
  366. modules linked with -shared. */
  367. return -ENOEXEC;
  368. default:
  369. printk(KERN_ERR "module %s: unknown relocation: %u\n",
  370. me->name, r_type);
  371. return -ENOEXEC;
  372. }
  373. if (rc) {
  374. printk(KERN_ERR "module %s: relocation error for symbol %s "
  375. "(r_type %i, value 0x%lx)\n",
  376. me->name, strtab + symtab[r_sym].st_name,
  377. r_type, (unsigned long) val);
  378. return rc;
  379. }
  380. return 0;
  381. }
  382. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  383. unsigned int symindex, unsigned int relsec,
  384. struct module *me)
  385. {
  386. Elf_Addr base;
  387. Elf_Sym *symtab;
  388. Elf_Rela *rela;
  389. unsigned long i, n;
  390. int rc;
  391. DEBUGP("Applying relocate section %u to %u\n",
  392. relsec, sechdrs[relsec].sh_info);
  393. base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
  394. symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
  395. rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
  396. n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
  397. for (i = 0; i < n; i++, rela++) {
  398. rc = apply_rela(rela, base, symtab, strtab, me);
  399. if (rc)
  400. return rc;
  401. }
  402. return 0;
  403. }
  404. int module_finalize(const Elf_Ehdr *hdr,
  405. const Elf_Shdr *sechdrs,
  406. struct module *me)
  407. {
  408. jump_label_apply_nops(me);
  409. return 0;
  410. }