module.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * arch/s390/kernel/module.c - Kernel module help for s390.
  3. *
  4. * S390 version
  5. * Copyright (C) 2002, 2003 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. *
  10. * based on i386 version
  11. * Copyright (C) 2001 Rusty Russell.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/module.h>
  28. #include <linux/elf.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/fs.h>
  31. #include <linux/string.h>
  32. #include <linux/kernel.h>
  33. #include <linux/moduleloader.h>
  34. #include <linux/bug.h>
  35. #if 0
  36. #define DEBUGP printk
  37. #else
  38. #define DEBUGP(fmt , ...)
  39. #endif
  40. #ifndef CONFIG_64BIT
  41. #define PLT_ENTRY_SIZE 12
  42. #else /* CONFIG_64BIT */
  43. #define PLT_ENTRY_SIZE 20
  44. #endif /* CONFIG_64BIT */
  45. void *module_alloc(unsigned long size)
  46. {
  47. if (size == 0)
  48. return NULL;
  49. return vmalloc(size);
  50. }
  51. /* Free memory returned from module_alloc */
  52. void module_free(struct module *mod, void *module_region)
  53. {
  54. vfree(module_region);
  55. }
  56. static void
  57. 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
  105. 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_size = ALIGN(me->core_size, 4);
  157. me->arch.got_offset = me->core_size;
  158. me->core_size += me->arch.got_size;
  159. me->arch.plt_offset = me->core_size;
  160. me->core_size += me->arch.plt_size;
  161. return 0;
  162. }
  163. int
  164. apply_relocate(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  165. unsigned int relsec, struct module *me)
  166. {
  167. printk(KERN_ERR "module %s: RELOCATION unsupported\n",
  168. me->name);
  169. return -ENOEXEC;
  170. }
  171. static int
  172. apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
  173. struct module *me)
  174. {
  175. struct mod_arch_syminfo *info;
  176. Elf_Addr loc, val;
  177. int r_type, r_sym;
  178. /* This is where to make the change */
  179. loc = base + rela->r_offset;
  180. /* This is the symbol it is referring to. Note that all
  181. undefined symbols have been resolved. */
  182. r_sym = ELF_R_SYM(rela->r_info);
  183. r_type = ELF_R_TYPE(rela->r_info);
  184. info = me->arch.syminfo + r_sym;
  185. val = symtab[r_sym].st_value;
  186. switch (r_type) {
  187. case R_390_8: /* Direct 8 bit. */
  188. case R_390_12: /* Direct 12 bit. */
  189. case R_390_16: /* Direct 16 bit. */
  190. case R_390_20: /* Direct 20 bit. */
  191. case R_390_32: /* Direct 32 bit. */
  192. case R_390_64: /* Direct 64 bit. */
  193. val += rela->r_addend;
  194. if (r_type == R_390_8)
  195. *(unsigned char *) loc = val;
  196. else if (r_type == R_390_12)
  197. *(unsigned short *) loc = (val & 0xfff) |
  198. (*(unsigned short *) loc & 0xf000);
  199. else if (r_type == R_390_16)
  200. *(unsigned short *) loc = val;
  201. else if (r_type == R_390_20)
  202. *(unsigned int *) loc =
  203. (*(unsigned int *) loc & 0xf00000ff) |
  204. (val & 0xfff) << 16 | (val & 0xff000) >> 4;
  205. else if (r_type == R_390_32)
  206. *(unsigned int *) loc = val;
  207. else if (r_type == R_390_64)
  208. *(unsigned long *) loc = val;
  209. break;
  210. case R_390_PC16: /* PC relative 16 bit. */
  211. case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
  212. case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
  213. case R_390_PC32: /* PC relative 32 bit. */
  214. case R_390_PC64: /* PC relative 64 bit. */
  215. val += rela->r_addend - loc;
  216. if (r_type == R_390_PC16)
  217. *(unsigned short *) loc = val;
  218. else if (r_type == R_390_PC16DBL)
  219. *(unsigned short *) loc = val >> 1;
  220. else if (r_type == R_390_PC32DBL)
  221. *(unsigned int *) loc = val >> 1;
  222. else if (r_type == R_390_PC32)
  223. *(unsigned int *) loc = val;
  224. else if (r_type == R_390_PC64)
  225. *(unsigned long *) loc = val;
  226. break;
  227. case R_390_GOT12: /* 12 bit GOT offset. */
  228. case R_390_GOT16: /* 16 bit GOT offset. */
  229. case R_390_GOT20: /* 20 bit GOT offset. */
  230. case R_390_GOT32: /* 32 bit GOT offset. */
  231. case R_390_GOT64: /* 64 bit GOT offset. */
  232. case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
  233. case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
  234. case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
  235. case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
  236. case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
  237. case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
  238. case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
  239. if (info->got_initialized == 0) {
  240. Elf_Addr *gotent;
  241. gotent = me->module_core + me->arch.got_offset +
  242. info->got_offset;
  243. *gotent = val;
  244. info->got_initialized = 1;
  245. }
  246. val = info->got_offset + rela->r_addend;
  247. if (r_type == R_390_GOT12 ||
  248. r_type == R_390_GOTPLT12)
  249. *(unsigned short *) loc = (val & 0xfff) |
  250. (*(unsigned short *) loc & 0xf000);
  251. else if (r_type == R_390_GOT16 ||
  252. r_type == R_390_GOTPLT16)
  253. *(unsigned short *) loc = val;
  254. else if (r_type == R_390_GOT20 ||
  255. r_type == R_390_GOTPLT20)
  256. *(unsigned int *) loc =
  257. (*(unsigned int *) loc & 0xf00000ff) |
  258. (val & 0xfff) << 16 | (val & 0xff000) >> 4;
  259. else if (r_type == R_390_GOT32 ||
  260. r_type == R_390_GOTPLT32)
  261. *(unsigned int *) loc = val;
  262. else if (r_type == R_390_GOTENT ||
  263. r_type == R_390_GOTPLTENT)
  264. *(unsigned int *) loc =
  265. (val + (Elf_Addr) me->module_core - loc) >> 1;
  266. else if (r_type == R_390_GOT64 ||
  267. r_type == R_390_GOTPLT64)
  268. *(unsigned long *) loc = val;
  269. break;
  270. case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
  271. case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
  272. case R_390_PLT32: /* 32 bit PC relative PLT address. */
  273. case R_390_PLT64: /* 64 bit PC relative PLT address. */
  274. case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
  275. case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
  276. case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
  277. if (info->plt_initialized == 0) {
  278. unsigned int *ip;
  279. ip = me->module_core + me->arch.plt_offset +
  280. info->plt_offset;
  281. #ifndef CONFIG_64BIT
  282. ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
  283. ip[1] = 0x100607f1;
  284. ip[2] = val;
  285. #else /* CONFIG_64BIT */
  286. ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
  287. ip[1] = 0x100a0004;
  288. ip[2] = 0x07f10000;
  289. ip[3] = (unsigned int) (val >> 32);
  290. ip[4] = (unsigned int) val;
  291. #endif /* CONFIG_64BIT */
  292. info->plt_initialized = 1;
  293. }
  294. if (r_type == R_390_PLTOFF16 ||
  295. r_type == R_390_PLTOFF32 ||
  296. r_type == R_390_PLTOFF64)
  297. val = me->arch.plt_offset - me->arch.got_offset +
  298. info->plt_offset + rela->r_addend;
  299. else {
  300. if (!((r_type == R_390_PLT16DBL &&
  301. val - loc + 0xffffUL < 0x1ffffeUL) ||
  302. (r_type == R_390_PLT32DBL &&
  303. val - loc + 0xffffffffULL < 0x1fffffffeULL)))
  304. val = (Elf_Addr) me->module_core +
  305. me->arch.plt_offset +
  306. info->plt_offset;
  307. val += rela->r_addend - loc;
  308. }
  309. if (r_type == R_390_PLT16DBL)
  310. *(unsigned short *) loc = val >> 1;
  311. else if (r_type == R_390_PLTOFF16)
  312. *(unsigned short *) loc = val;
  313. else if (r_type == R_390_PLT32DBL)
  314. *(unsigned int *) loc = val >> 1;
  315. else if (r_type == R_390_PLT32 ||
  316. r_type == R_390_PLTOFF32)
  317. *(unsigned int *) loc = val;
  318. else if (r_type == R_390_PLT64 ||
  319. r_type == R_390_PLTOFF64)
  320. *(unsigned long *) loc = val;
  321. break;
  322. case R_390_GOTOFF16: /* 16 bit offset to GOT. */
  323. case R_390_GOTOFF32: /* 32 bit offset to GOT. */
  324. case R_390_GOTOFF64: /* 64 bit offset to GOT. */
  325. val = val + rela->r_addend -
  326. ((Elf_Addr) me->module_core + me->arch.got_offset);
  327. if (r_type == R_390_GOTOFF16)
  328. *(unsigned short *) loc = val;
  329. else if (r_type == R_390_GOTOFF32)
  330. *(unsigned int *) loc = val;
  331. else if (r_type == R_390_GOTOFF64)
  332. *(unsigned long *) loc = val;
  333. break;
  334. case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
  335. case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
  336. val = (Elf_Addr) me->module_core + me->arch.got_offset +
  337. rela->r_addend - loc;
  338. if (r_type == R_390_GOTPC)
  339. *(unsigned int *) loc = val;
  340. else if (r_type == R_390_GOTPCDBL)
  341. *(unsigned int *) loc = val >> 1;
  342. break;
  343. case R_390_COPY:
  344. case R_390_GLOB_DAT: /* Create GOT entry. */
  345. case R_390_JMP_SLOT: /* Create PLT entry. */
  346. case R_390_RELATIVE: /* Adjust by program base. */
  347. /* Only needed if we want to support loading of
  348. modules linked with -shared. */
  349. break;
  350. default:
  351. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  352. me->name, r_type);
  353. return -ENOEXEC;
  354. }
  355. return 0;
  356. }
  357. int
  358. apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  359. unsigned int symindex, unsigned int relsec,
  360. struct module *me)
  361. {
  362. Elf_Addr base;
  363. Elf_Sym *symtab;
  364. Elf_Rela *rela;
  365. unsigned long i, n;
  366. int rc;
  367. DEBUGP("Applying relocate section %u to %u\n",
  368. relsec, sechdrs[relsec].sh_info);
  369. base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
  370. symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
  371. rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
  372. n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
  373. for (i = 0; i < n; i++, rela++) {
  374. rc = apply_rela(rela, base, symtab, me);
  375. if (rc)
  376. return rc;
  377. }
  378. return 0;
  379. }
  380. int module_finalize(const Elf_Ehdr *hdr,
  381. const Elf_Shdr *sechdrs,
  382. struct module *me)
  383. {
  384. vfree(me->arch.syminfo);
  385. return module_bug_finalize(hdr, sechdrs, me);
  386. }
  387. void module_arch_cleanup(struct module *mod)
  388. {
  389. module_bug_cleanup(mod);
  390. }