module.c 13 KB

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