module.c 12 KB

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