module.c 14 KB

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