module.c 13 KB

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