module.c 13 KB

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