module.c 12 KB

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