module.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * IA-64-specific support for kernel module loader.
  3. *
  4. * Copyright (C) 2003 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. *
  7. * Loosely based on patch by Rusty Russell.
  8. */
  9. /* relocs tested so far:
  10. DIR64LSB
  11. FPTR64LSB
  12. GPREL22
  13. LDXMOV
  14. LDXMOV
  15. LTOFF22
  16. LTOFF22X
  17. LTOFF22X
  18. LTOFF_FPTR22
  19. PCREL21B (for br.call only; br.cond is not supported out of modules!)
  20. PCREL60B (for brl.cond only; brl.call is not supported for modules!)
  21. PCREL64LSB
  22. SECREL32LSB
  23. SEGREL64LSB
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/elf.h>
  28. #include <linux/moduleloader.h>
  29. #include <linux/string.h>
  30. #include <linux/vmalloc.h>
  31. #include <asm/patch.h>
  32. #include <asm/unaligned.h>
  33. #define ARCH_MODULE_DEBUG 0
  34. #if ARCH_MODULE_DEBUG
  35. # define DEBUGP printk
  36. # define inline
  37. #else
  38. # define DEBUGP(fmt , a...)
  39. #endif
  40. #ifdef CONFIG_ITANIUM
  41. # define USE_BRL 0
  42. #else
  43. # define USE_BRL 1
  44. #endif
  45. #define MAX_LTOFF ((uint64_t) (1 << 22)) /* max. allowable linkage-table offset */
  46. /* Define some relocation helper macros/types: */
  47. #define FORMAT_SHIFT 0
  48. #define FORMAT_BITS 3
  49. #define FORMAT_MASK ((1 << FORMAT_BITS) - 1)
  50. #define VALUE_SHIFT 3
  51. #define VALUE_BITS 5
  52. #define VALUE_MASK ((1 << VALUE_BITS) - 1)
  53. enum reloc_target_format {
  54. /* direct encoded formats: */
  55. RF_NONE = 0,
  56. RF_INSN14 = 1,
  57. RF_INSN22 = 2,
  58. RF_INSN64 = 3,
  59. RF_32MSB = 4,
  60. RF_32LSB = 5,
  61. RF_64MSB = 6,
  62. RF_64LSB = 7,
  63. /* formats that cannot be directly decoded: */
  64. RF_INSN60,
  65. RF_INSN21B, /* imm21 form 1 */
  66. RF_INSN21M, /* imm21 form 2 */
  67. RF_INSN21F /* imm21 form 3 */
  68. };
  69. enum reloc_value_formula {
  70. RV_DIRECT = 4, /* S + A */
  71. RV_GPREL = 5, /* @gprel(S + A) */
  72. RV_LTREL = 6, /* @ltoff(S + A) */
  73. RV_PLTREL = 7, /* @pltoff(S + A) */
  74. RV_FPTR = 8, /* @fptr(S + A) */
  75. RV_PCREL = 9, /* S + A - P */
  76. RV_LTREL_FPTR = 10, /* @ltoff(@fptr(S + A)) */
  77. RV_SEGREL = 11, /* @segrel(S + A) */
  78. RV_SECREL = 12, /* @secrel(S + A) */
  79. RV_BDREL = 13, /* BD + A */
  80. RV_LTV = 14, /* S + A (like RV_DIRECT, except frozen at static link-time) */
  81. RV_PCREL2 = 15, /* S + A - P */
  82. RV_SPECIAL = 16, /* various (see below) */
  83. RV_RSVD17 = 17,
  84. RV_TPREL = 18, /* @tprel(S + A) */
  85. RV_LTREL_TPREL = 19, /* @ltoff(@tprel(S + A)) */
  86. RV_DTPMOD = 20, /* @dtpmod(S + A) */
  87. RV_LTREL_DTPMOD = 21, /* @ltoff(@dtpmod(S + A)) */
  88. RV_DTPREL = 22, /* @dtprel(S + A) */
  89. RV_LTREL_DTPREL = 23, /* @ltoff(@dtprel(S + A)) */
  90. RV_RSVD24 = 24,
  91. RV_RSVD25 = 25,
  92. RV_RSVD26 = 26,
  93. RV_RSVD27 = 27
  94. /* 28-31 reserved for implementation-specific purposes. */
  95. };
  96. #define N(reloc) [R_IA64_##reloc] = #reloc
  97. static const char *reloc_name[256] = {
  98. N(NONE), N(IMM14), N(IMM22), N(IMM64),
  99. N(DIR32MSB), N(DIR32LSB), N(DIR64MSB), N(DIR64LSB),
  100. N(GPREL22), N(GPREL64I), N(GPREL32MSB), N(GPREL32LSB),
  101. N(GPREL64MSB), N(GPREL64LSB), N(LTOFF22), N(LTOFF64I),
  102. N(PLTOFF22), N(PLTOFF64I), N(PLTOFF64MSB), N(PLTOFF64LSB),
  103. N(FPTR64I), N(FPTR32MSB), N(FPTR32LSB), N(FPTR64MSB),
  104. N(FPTR64LSB), N(PCREL60B), N(PCREL21B), N(PCREL21M),
  105. N(PCREL21F), N(PCREL32MSB), N(PCREL32LSB), N(PCREL64MSB),
  106. N(PCREL64LSB), N(LTOFF_FPTR22), N(LTOFF_FPTR64I), N(LTOFF_FPTR32MSB),
  107. N(LTOFF_FPTR32LSB), N(LTOFF_FPTR64MSB), N(LTOFF_FPTR64LSB), N(SEGREL32MSB),
  108. N(SEGREL32LSB), N(SEGREL64MSB), N(SEGREL64LSB), N(SECREL32MSB),
  109. N(SECREL32LSB), N(SECREL64MSB), N(SECREL64LSB), N(REL32MSB),
  110. N(REL32LSB), N(REL64MSB), N(REL64LSB), N(LTV32MSB),
  111. N(LTV32LSB), N(LTV64MSB), N(LTV64LSB), N(PCREL21BI),
  112. N(PCREL22), N(PCREL64I), N(IPLTMSB), N(IPLTLSB),
  113. N(COPY), N(LTOFF22X), N(LDXMOV), N(TPREL14),
  114. N(TPREL22), N(TPREL64I), N(TPREL64MSB), N(TPREL64LSB),
  115. N(LTOFF_TPREL22), N(DTPMOD64MSB), N(DTPMOD64LSB), N(LTOFF_DTPMOD22),
  116. N(DTPREL14), N(DTPREL22), N(DTPREL64I), N(DTPREL32MSB),
  117. N(DTPREL32LSB), N(DTPREL64MSB), N(DTPREL64LSB), N(LTOFF_DTPREL22)
  118. };
  119. #undef N
  120. /* Opaque struct for insns, to protect against derefs. */
  121. struct insn;
  122. static inline uint64_t
  123. bundle (const struct insn *insn)
  124. {
  125. return (uint64_t) insn & ~0xfUL;
  126. }
  127. static inline int
  128. slot (const struct insn *insn)
  129. {
  130. return (uint64_t) insn & 0x3;
  131. }
  132. static int
  133. apply_imm64 (struct module *mod, struct insn *insn, uint64_t val)
  134. {
  135. if (slot(insn) != 2) {
  136. printk(KERN_ERR "%s: invalid slot number %d for IMM64\n",
  137. mod->name, slot(insn));
  138. return 0;
  139. }
  140. ia64_patch_imm64((u64) insn, val);
  141. return 1;
  142. }
  143. static int
  144. apply_imm60 (struct module *mod, struct insn *insn, uint64_t val)
  145. {
  146. if (slot(insn) != 2) {
  147. printk(KERN_ERR "%s: invalid slot number %d for IMM60\n",
  148. mod->name, slot(insn));
  149. return 0;
  150. }
  151. if (val + ((uint64_t) 1 << 59) >= (1UL << 60)) {
  152. printk(KERN_ERR "%s: value %ld out of IMM60 range\n",
  153. mod->name, (long) val);
  154. return 0;
  155. }
  156. ia64_patch_imm60((u64) insn, val);
  157. return 1;
  158. }
  159. static int
  160. apply_imm22 (struct module *mod, struct insn *insn, uint64_t val)
  161. {
  162. if (val + (1 << 21) >= (1 << 22)) {
  163. printk(KERN_ERR "%s: value %li out of IMM22 range\n",
  164. mod->name, (long)val);
  165. return 0;
  166. }
  167. ia64_patch((u64) insn, 0x01fffcfe000UL, ( ((val & 0x200000UL) << 15) /* bit 21 -> 36 */
  168. | ((val & 0x1f0000UL) << 6) /* bit 16 -> 22 */
  169. | ((val & 0x00ff80UL) << 20) /* bit 7 -> 27 */
  170. | ((val & 0x00007fUL) << 13) /* bit 0 -> 13 */));
  171. return 1;
  172. }
  173. static int
  174. apply_imm21b (struct module *mod, struct insn *insn, uint64_t val)
  175. {
  176. if (val + (1 << 20) >= (1 << 21)) {
  177. printk(KERN_ERR "%s: value %li out of IMM21b range\n",
  178. mod->name, (long)val);
  179. return 0;
  180. }
  181. ia64_patch((u64) insn, 0x11ffffe000UL, ( ((val & 0x100000UL) << 16) /* bit 20 -> 36 */
  182. | ((val & 0x0fffffUL) << 13) /* bit 0 -> 13 */));
  183. return 1;
  184. }
  185. #if USE_BRL
  186. struct plt_entry {
  187. /* Three instruction bundles in PLT. */
  188. unsigned char bundle[2][16];
  189. };
  190. static const struct plt_entry ia64_plt_template = {
  191. {
  192. {
  193. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  194. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, /* movl gp=TARGET_GP */
  195. 0x00, 0x00, 0x00, 0x60
  196. },
  197. {
  198. 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.many gp=TARGET_GP */
  200. 0x08, 0x00, 0x00, 0xc0
  201. }
  202. }
  203. };
  204. static int
  205. patch_plt (struct module *mod, struct plt_entry *plt, long target_ip, unsigned long target_gp)
  206. {
  207. if (apply_imm64(mod, (struct insn *) (plt->bundle[0] + 2), target_gp)
  208. && apply_imm60(mod, (struct insn *) (plt->bundle[1] + 2),
  209. (target_ip - (int64_t) plt->bundle[1]) / 16))
  210. return 1;
  211. return 0;
  212. }
  213. unsigned long
  214. plt_target (struct plt_entry *plt)
  215. {
  216. uint64_t b0, b1, *b = (uint64_t *) plt->bundle[1];
  217. long off;
  218. b0 = b[0]; b1 = b[1];
  219. off = ( ((b1 & 0x00fffff000000000UL) >> 36) /* imm20b -> bit 0 */
  220. | ((b0 >> 48) << 20) | ((b1 & 0x7fffffUL) << 36) /* imm39 -> bit 20 */
  221. | ((b1 & 0x0800000000000000UL) << 0)); /* i -> bit 59 */
  222. return (long) plt->bundle[1] + 16*off;
  223. }
  224. #else /* !USE_BRL */
  225. struct plt_entry {
  226. /* Three instruction bundles in PLT. */
  227. unsigned char bundle[3][16];
  228. };
  229. static const struct plt_entry ia64_plt_template = {
  230. {
  231. {
  232. 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  233. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* movl r16=TARGET_IP */
  234. 0x02, 0x00, 0x00, 0x60
  235. },
  236. {
  237. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  238. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, /* movl gp=TARGET_GP */
  239. 0x00, 0x00, 0x00, 0x60
  240. },
  241. {
  242. 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
  243. 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
  244. 0x60, 0x00, 0x80, 0x00 /* br.few b6 */
  245. }
  246. }
  247. };
  248. static int
  249. patch_plt (struct module *mod, struct plt_entry *plt, long target_ip, unsigned long target_gp)
  250. {
  251. if (apply_imm64(mod, (struct insn *) (plt->bundle[0] + 2), target_ip)
  252. && apply_imm64(mod, (struct insn *) (plt->bundle[1] + 2), target_gp))
  253. return 1;
  254. return 0;
  255. }
  256. unsigned long
  257. plt_target (struct plt_entry *plt)
  258. {
  259. uint64_t b0, b1, *b = (uint64_t *) plt->bundle[0];
  260. b0 = b[0]; b1 = b[1];
  261. return ( ((b1 & 0x000007f000000000) >> 36) /* imm7b -> bit 0 */
  262. | ((b1 & 0x07fc000000000000) >> 43) /* imm9d -> bit 7 */
  263. | ((b1 & 0x0003e00000000000) >> 29) /* imm5c -> bit 16 */
  264. | ((b1 & 0x0000100000000000) >> 23) /* ic -> bit 21 */
  265. | ((b0 >> 46) << 22) | ((b1 & 0x7fffff) << 40) /* imm41 -> bit 22 */
  266. | ((b1 & 0x0800000000000000) << 4)); /* i -> bit 63 */
  267. }
  268. #endif /* !USE_BRL */
  269. void *
  270. module_alloc (unsigned long size)
  271. {
  272. if (!size)
  273. return NULL;
  274. return vmalloc(size);
  275. }
  276. void
  277. module_free (struct module *mod, void *module_region)
  278. {
  279. if (mod && mod->arch.init_unw_table &&
  280. module_region == mod->module_init) {
  281. unw_remove_unwind_table(mod->arch.init_unw_table);
  282. mod->arch.init_unw_table = NULL;
  283. }
  284. vfree(module_region);
  285. }
  286. /* Have we already seen one of these relocations? */
  287. /* FIXME: we could look in other sections, too --RR */
  288. static int
  289. duplicate_reloc (const Elf64_Rela *rela, unsigned int num)
  290. {
  291. unsigned int i;
  292. for (i = 0; i < num; i++) {
  293. if (rela[i].r_info == rela[num].r_info && rela[i].r_addend == rela[num].r_addend)
  294. return 1;
  295. }
  296. return 0;
  297. }
  298. /* Count how many GOT entries we may need */
  299. static unsigned int
  300. count_gots (const Elf64_Rela *rela, unsigned int num)
  301. {
  302. unsigned int i, ret = 0;
  303. /* Sure, this is order(n^2), but it's usually short, and not
  304. time critical */
  305. for (i = 0; i < num; i++) {
  306. switch (ELF64_R_TYPE(rela[i].r_info)) {
  307. case R_IA64_LTOFF22:
  308. case R_IA64_LTOFF22X:
  309. case R_IA64_LTOFF64I:
  310. case R_IA64_LTOFF_FPTR22:
  311. case R_IA64_LTOFF_FPTR64I:
  312. case R_IA64_LTOFF_FPTR32MSB:
  313. case R_IA64_LTOFF_FPTR32LSB:
  314. case R_IA64_LTOFF_FPTR64MSB:
  315. case R_IA64_LTOFF_FPTR64LSB:
  316. if (!duplicate_reloc(rela, i))
  317. ret++;
  318. break;
  319. }
  320. }
  321. return ret;
  322. }
  323. /* Count how many PLT entries we may need */
  324. static unsigned int
  325. count_plts (const Elf64_Rela *rela, unsigned int num)
  326. {
  327. unsigned int i, ret = 0;
  328. /* Sure, this is order(n^2), but it's usually short, and not
  329. time critical */
  330. for (i = 0; i < num; i++) {
  331. switch (ELF64_R_TYPE(rela[i].r_info)) {
  332. case R_IA64_PCREL21B:
  333. case R_IA64_PLTOFF22:
  334. case R_IA64_PLTOFF64I:
  335. case R_IA64_PLTOFF64MSB:
  336. case R_IA64_PLTOFF64LSB:
  337. case R_IA64_IPLTMSB:
  338. case R_IA64_IPLTLSB:
  339. if (!duplicate_reloc(rela, i))
  340. ret++;
  341. break;
  342. }
  343. }
  344. return ret;
  345. }
  346. /* We need to create an function-descriptors for any internal function
  347. which is referenced. */
  348. static unsigned int
  349. count_fdescs (const Elf64_Rela *rela, unsigned int num)
  350. {
  351. unsigned int i, ret = 0;
  352. /* Sure, this is order(n^2), but it's usually short, and not time critical. */
  353. for (i = 0; i < num; i++) {
  354. switch (ELF64_R_TYPE(rela[i].r_info)) {
  355. case R_IA64_FPTR64I:
  356. case R_IA64_FPTR32LSB:
  357. case R_IA64_FPTR32MSB:
  358. case R_IA64_FPTR64LSB:
  359. case R_IA64_FPTR64MSB:
  360. case R_IA64_LTOFF_FPTR22:
  361. case R_IA64_LTOFF_FPTR32LSB:
  362. case R_IA64_LTOFF_FPTR32MSB:
  363. case R_IA64_LTOFF_FPTR64I:
  364. case R_IA64_LTOFF_FPTR64LSB:
  365. case R_IA64_LTOFF_FPTR64MSB:
  366. case R_IA64_IPLTMSB:
  367. case R_IA64_IPLTLSB:
  368. /*
  369. * Jumps to static functions sometimes go straight to their
  370. * offset. Of course, that may not be possible if the jump is
  371. * from init -> core or vice. versa, so we need to generate an
  372. * FDESC (and PLT etc) for that.
  373. */
  374. case R_IA64_PCREL21B:
  375. if (!duplicate_reloc(rela, i))
  376. ret++;
  377. break;
  378. }
  379. }
  380. return ret;
  381. }
  382. int
  383. module_frob_arch_sections (Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, char *secstrings,
  384. struct module *mod)
  385. {
  386. unsigned long core_plts = 0, init_plts = 0, gots = 0, fdescs = 0;
  387. Elf64_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
  388. /*
  389. * To store the PLTs and function-descriptors, we expand the .text section for
  390. * core module-code and the .init.text section for initialization code.
  391. */
  392. for (s = sechdrs; s < sechdrs_end; ++s)
  393. if (strcmp(".core.plt", secstrings + s->sh_name) == 0)
  394. mod->arch.core_plt = s;
  395. else if (strcmp(".init.plt", secstrings + s->sh_name) == 0)
  396. mod->arch.init_plt = s;
  397. else if (strcmp(".got", secstrings + s->sh_name) == 0)
  398. mod->arch.got = s;
  399. else if (strcmp(".opd", secstrings + s->sh_name) == 0)
  400. mod->arch.opd = s;
  401. else if (strcmp(".IA_64.unwind", secstrings + s->sh_name) == 0)
  402. mod->arch.unwind = s;
  403. #ifdef CONFIG_PARAVIRT
  404. else if (strcmp(".paravirt_bundles",
  405. secstrings + s->sh_name) == 0)
  406. mod->arch.paravirt_bundles = s;
  407. else if (strcmp(".paravirt_insts",
  408. secstrings + s->sh_name) == 0)
  409. mod->arch.paravirt_insts = s;
  410. #endif
  411. if (!mod->arch.core_plt || !mod->arch.init_plt || !mod->arch.got || !mod->arch.opd) {
  412. printk(KERN_ERR "%s: sections missing\n", mod->name);
  413. return -ENOEXEC;
  414. }
  415. /* GOT and PLTs can occur in any relocated section... */
  416. for (s = sechdrs + 1; s < sechdrs_end; ++s) {
  417. const Elf64_Rela *rels = (void *)ehdr + s->sh_offset;
  418. unsigned long numrels = s->sh_size/sizeof(Elf64_Rela);
  419. if (s->sh_type != SHT_RELA)
  420. continue;
  421. gots += count_gots(rels, numrels);
  422. fdescs += count_fdescs(rels, numrels);
  423. if (strstr(secstrings + s->sh_name, ".init"))
  424. init_plts += count_plts(rels, numrels);
  425. else
  426. core_plts += count_plts(rels, numrels);
  427. }
  428. mod->arch.core_plt->sh_type = SHT_NOBITS;
  429. mod->arch.core_plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  430. mod->arch.core_plt->sh_addralign = 16;
  431. mod->arch.core_plt->sh_size = core_plts * sizeof(struct plt_entry);
  432. mod->arch.init_plt->sh_type = SHT_NOBITS;
  433. mod->arch.init_plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  434. mod->arch.init_plt->sh_addralign = 16;
  435. mod->arch.init_plt->sh_size = init_plts * sizeof(struct plt_entry);
  436. mod->arch.got->sh_type = SHT_NOBITS;
  437. mod->arch.got->sh_flags = ARCH_SHF_SMALL | SHF_ALLOC;
  438. mod->arch.got->sh_addralign = 8;
  439. mod->arch.got->sh_size = gots * sizeof(struct got_entry);
  440. mod->arch.opd->sh_type = SHT_NOBITS;
  441. mod->arch.opd->sh_flags = SHF_ALLOC;
  442. mod->arch.opd->sh_addralign = 8;
  443. mod->arch.opd->sh_size = fdescs * sizeof(struct fdesc);
  444. DEBUGP("%s: core.plt=%lx, init.plt=%lx, got=%lx, fdesc=%lx\n",
  445. __func__, mod->arch.core_plt->sh_size, mod->arch.init_plt->sh_size,
  446. mod->arch.got->sh_size, mod->arch.opd->sh_size);
  447. return 0;
  448. }
  449. static inline int
  450. in_init (const struct module *mod, uint64_t addr)
  451. {
  452. return addr - (uint64_t) mod->module_init < mod->init_size;
  453. }
  454. static inline int
  455. in_core (const struct module *mod, uint64_t addr)
  456. {
  457. return addr - (uint64_t) mod->module_core < mod->core_size;
  458. }
  459. static inline int
  460. is_internal (const struct module *mod, uint64_t value)
  461. {
  462. return in_init(mod, value) || in_core(mod, value);
  463. }
  464. /*
  465. * Get gp-relative offset for the linkage-table entry of VALUE.
  466. */
  467. static uint64_t
  468. get_ltoff (struct module *mod, uint64_t value, int *okp)
  469. {
  470. struct got_entry *got, *e;
  471. if (!*okp)
  472. return 0;
  473. got = (void *) mod->arch.got->sh_addr;
  474. for (e = got; e < got + mod->arch.next_got_entry; ++e)
  475. if (e->val == value)
  476. goto found;
  477. /* Not enough GOT entries? */
  478. BUG_ON(e >= (struct got_entry *) (mod->arch.got->sh_addr + mod->arch.got->sh_size));
  479. e->val = value;
  480. ++mod->arch.next_got_entry;
  481. found:
  482. return (uint64_t) e - mod->arch.gp;
  483. }
  484. static inline int
  485. gp_addressable (struct module *mod, uint64_t value)
  486. {
  487. return value - mod->arch.gp + MAX_LTOFF/2 < MAX_LTOFF;
  488. }
  489. /* Get PC-relative PLT entry for this value. Returns 0 on failure. */
  490. static uint64_t
  491. get_plt (struct module *mod, const struct insn *insn, uint64_t value, int *okp)
  492. {
  493. struct plt_entry *plt, *plt_end;
  494. uint64_t target_ip, target_gp;
  495. if (!*okp)
  496. return 0;
  497. if (in_init(mod, (uint64_t) insn)) {
  498. plt = (void *) mod->arch.init_plt->sh_addr;
  499. plt_end = (void *) plt + mod->arch.init_plt->sh_size;
  500. } else {
  501. plt = (void *) mod->arch.core_plt->sh_addr;
  502. plt_end = (void *) plt + mod->arch.core_plt->sh_size;
  503. }
  504. /* "value" is a pointer to a function-descriptor; fetch the target ip/gp from it: */
  505. target_ip = ((uint64_t *) value)[0];
  506. target_gp = ((uint64_t *) value)[1];
  507. /* Look for existing PLT entry. */
  508. while (plt->bundle[0][0]) {
  509. if (plt_target(plt) == target_ip)
  510. goto found;
  511. if (++plt >= plt_end)
  512. BUG();
  513. }
  514. *plt = ia64_plt_template;
  515. if (!patch_plt(mod, plt, target_ip, target_gp)) {
  516. *okp = 0;
  517. return 0;
  518. }
  519. #if ARCH_MODULE_DEBUG
  520. if (plt_target(plt) != target_ip) {
  521. printk("%s: mistargeted PLT: wanted %lx, got %lx\n",
  522. __func__, target_ip, plt_target(plt));
  523. *okp = 0;
  524. return 0;
  525. }
  526. #endif
  527. found:
  528. return (uint64_t) plt;
  529. }
  530. /* Get function descriptor for VALUE. */
  531. static uint64_t
  532. get_fdesc (struct module *mod, uint64_t value, int *okp)
  533. {
  534. struct fdesc *fdesc = (void *) mod->arch.opd->sh_addr;
  535. if (!*okp)
  536. return 0;
  537. if (!value) {
  538. printk(KERN_ERR "%s: fdesc for zero requested!\n", mod->name);
  539. return 0;
  540. }
  541. if (!is_internal(mod, value))
  542. /*
  543. * If it's not a module-local entry-point, "value" already points to a
  544. * function-descriptor.
  545. */
  546. return value;
  547. /* Look for existing function descriptor. */
  548. while (fdesc->ip) {
  549. if (fdesc->ip == value)
  550. return (uint64_t)fdesc;
  551. if ((uint64_t) ++fdesc >= mod->arch.opd->sh_addr + mod->arch.opd->sh_size)
  552. BUG();
  553. }
  554. /* Create new one */
  555. fdesc->ip = value;
  556. fdesc->gp = mod->arch.gp;
  557. return (uint64_t) fdesc;
  558. }
  559. static inline int
  560. do_reloc (struct module *mod, uint8_t r_type, Elf64_Sym *sym, uint64_t addend,
  561. Elf64_Shdr *sec, void *location)
  562. {
  563. enum reloc_target_format format = (r_type >> FORMAT_SHIFT) & FORMAT_MASK;
  564. enum reloc_value_formula formula = (r_type >> VALUE_SHIFT) & VALUE_MASK;
  565. uint64_t val;
  566. int ok = 1;
  567. val = sym->st_value + addend;
  568. switch (formula) {
  569. case RV_SEGREL: /* segment base is arbitrarily chosen to be 0 for kernel modules */
  570. case RV_DIRECT:
  571. break;
  572. case RV_GPREL: val -= mod->arch.gp; break;
  573. case RV_LTREL: val = get_ltoff(mod, val, &ok); break;
  574. case RV_PLTREL: val = get_plt(mod, location, val, &ok); break;
  575. case RV_FPTR: val = get_fdesc(mod, val, &ok); break;
  576. case RV_SECREL: val -= sec->sh_addr; break;
  577. case RV_LTREL_FPTR: val = get_ltoff(mod, get_fdesc(mod, val, &ok), &ok); break;
  578. case RV_PCREL:
  579. switch (r_type) {
  580. case R_IA64_PCREL21B:
  581. if ((in_init(mod, val) && in_core(mod, (uint64_t)location)) ||
  582. (in_core(mod, val) && in_init(mod, (uint64_t)location))) {
  583. /*
  584. * Init section may have been allocated far away from core,
  585. * if the branch won't reach, then allocate a plt for it.
  586. */
  587. uint64_t delta = ((int64_t)val - (int64_t)location) / 16;
  588. if (delta + (1 << 20) >= (1 << 21)) {
  589. val = get_fdesc(mod, val, &ok);
  590. val = get_plt(mod, location, val, &ok);
  591. }
  592. } else if (!is_internal(mod, val))
  593. val = get_plt(mod, location, val, &ok);
  594. /* FALL THROUGH */
  595. default:
  596. val -= bundle(location);
  597. break;
  598. case R_IA64_PCREL32MSB:
  599. case R_IA64_PCREL32LSB:
  600. case R_IA64_PCREL64MSB:
  601. case R_IA64_PCREL64LSB:
  602. val -= (uint64_t) location;
  603. break;
  604. }
  605. switch (r_type) {
  606. case R_IA64_PCREL60B: format = RF_INSN60; break;
  607. case R_IA64_PCREL21B: format = RF_INSN21B; break;
  608. case R_IA64_PCREL21M: format = RF_INSN21M; break;
  609. case R_IA64_PCREL21F: format = RF_INSN21F; break;
  610. default: break;
  611. }
  612. break;
  613. case RV_BDREL:
  614. val -= (uint64_t) (in_init(mod, val) ? mod->module_init : mod->module_core);
  615. break;
  616. case RV_LTV:
  617. /* can link-time value relocs happen here? */
  618. BUG();
  619. break;
  620. case RV_PCREL2:
  621. if (r_type == R_IA64_PCREL21BI) {
  622. if (!is_internal(mod, val)) {
  623. printk(KERN_ERR "%s: %s reloc against "
  624. "non-local symbol (%lx)\n", __func__,
  625. reloc_name[r_type], (unsigned long)val);
  626. return -ENOEXEC;
  627. }
  628. format = RF_INSN21B;
  629. }
  630. val -= bundle(location);
  631. break;
  632. case RV_SPECIAL:
  633. switch (r_type) {
  634. case R_IA64_IPLTMSB:
  635. case R_IA64_IPLTLSB:
  636. val = get_fdesc(mod, get_plt(mod, location, val, &ok), &ok);
  637. format = RF_64LSB;
  638. if (r_type == R_IA64_IPLTMSB)
  639. format = RF_64MSB;
  640. break;
  641. case R_IA64_SUB:
  642. val = addend - sym->st_value;
  643. format = RF_INSN64;
  644. break;
  645. case R_IA64_LTOFF22X:
  646. if (gp_addressable(mod, val))
  647. val -= mod->arch.gp;
  648. else
  649. val = get_ltoff(mod, val, &ok);
  650. format = RF_INSN22;
  651. break;
  652. case R_IA64_LDXMOV:
  653. if (gp_addressable(mod, val)) {
  654. /* turn "ld8" into "mov": */
  655. DEBUGP("%s: patching ld8 at %p to mov\n", __func__, location);
  656. ia64_patch((u64) location, 0x1fff80fe000UL, 0x10000000000UL);
  657. }
  658. return 0;
  659. default:
  660. if (reloc_name[r_type])
  661. printk(KERN_ERR "%s: special reloc %s not supported",
  662. mod->name, reloc_name[r_type]);
  663. else
  664. printk(KERN_ERR "%s: unknown special reloc %x\n",
  665. mod->name, r_type);
  666. return -ENOEXEC;
  667. }
  668. break;
  669. case RV_TPREL:
  670. case RV_LTREL_TPREL:
  671. case RV_DTPMOD:
  672. case RV_LTREL_DTPMOD:
  673. case RV_DTPREL:
  674. case RV_LTREL_DTPREL:
  675. printk(KERN_ERR "%s: %s reloc not supported\n",
  676. mod->name, reloc_name[r_type] ? reloc_name[r_type] : "?");
  677. return -ENOEXEC;
  678. default:
  679. printk(KERN_ERR "%s: unknown reloc %x\n", mod->name, r_type);
  680. return -ENOEXEC;
  681. }
  682. if (!ok)
  683. return -ENOEXEC;
  684. DEBUGP("%s: [%p]<-%016lx = %s(%lx)\n", __func__, location, val,
  685. reloc_name[r_type] ? reloc_name[r_type] : "?", sym->st_value + addend);
  686. switch (format) {
  687. case RF_INSN21B: ok = apply_imm21b(mod, location, (int64_t) val / 16); break;
  688. case RF_INSN22: ok = apply_imm22(mod, location, val); break;
  689. case RF_INSN64: ok = apply_imm64(mod, location, val); break;
  690. case RF_INSN60: ok = apply_imm60(mod, location, (int64_t) val / 16); break;
  691. case RF_32LSB: put_unaligned(val, (uint32_t *) location); break;
  692. case RF_64LSB: put_unaligned(val, (uint64_t *) location); break;
  693. case RF_32MSB: /* ia64 Linux is little-endian... */
  694. case RF_64MSB: /* ia64 Linux is little-endian... */
  695. case RF_INSN14: /* must be within-module, i.e., resolved by "ld -r" */
  696. case RF_INSN21M: /* must be within-module, i.e., resolved by "ld -r" */
  697. case RF_INSN21F: /* must be within-module, i.e., resolved by "ld -r" */
  698. printk(KERN_ERR "%s: format %u needed by %s reloc is not supported\n",
  699. mod->name, format, reloc_name[r_type] ? reloc_name[r_type] : "?");
  700. return -ENOEXEC;
  701. default:
  702. printk(KERN_ERR "%s: relocation %s resulted in unknown format %u\n",
  703. mod->name, reloc_name[r_type] ? reloc_name[r_type] : "?", format);
  704. return -ENOEXEC;
  705. }
  706. return ok ? 0 : -ENOEXEC;
  707. }
  708. int
  709. apply_relocate_add (Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  710. unsigned int relsec, struct module *mod)
  711. {
  712. unsigned int i, n = sechdrs[relsec].sh_size / sizeof(Elf64_Rela);
  713. Elf64_Rela *rela = (void *) sechdrs[relsec].sh_addr;
  714. Elf64_Shdr *target_sec;
  715. int ret;
  716. DEBUGP("%s: applying section %u (%u relocs) to %u\n", __func__,
  717. relsec, n, sechdrs[relsec].sh_info);
  718. target_sec = sechdrs + sechdrs[relsec].sh_info;
  719. if (target_sec->sh_entsize == ~0UL)
  720. /*
  721. * If target section wasn't allocated, we don't need to relocate it.
  722. * Happens, e.g., for debug sections.
  723. */
  724. return 0;
  725. if (!mod->arch.gp) {
  726. /*
  727. * XXX Should have an arch-hook for running this after final section
  728. * addresses have been selected...
  729. */
  730. uint64_t gp;
  731. if (mod->core_size > MAX_LTOFF)
  732. /*
  733. * This takes advantage of fact that SHF_ARCH_SMALL gets allocated
  734. * at the end of the module.
  735. */
  736. gp = mod->core_size - MAX_LTOFF / 2;
  737. else
  738. gp = mod->core_size / 2;
  739. gp = (uint64_t) mod->module_core + ((gp + 7) & -8);
  740. mod->arch.gp = gp;
  741. DEBUGP("%s: placing gp at 0x%lx\n", __func__, gp);
  742. }
  743. for (i = 0; i < n; i++) {
  744. ret = do_reloc(mod, ELF64_R_TYPE(rela[i].r_info),
  745. ((Elf64_Sym *) sechdrs[symindex].sh_addr
  746. + ELF64_R_SYM(rela[i].r_info)),
  747. rela[i].r_addend, target_sec,
  748. (void *) target_sec->sh_addr + rela[i].r_offset);
  749. if (ret < 0)
  750. return ret;
  751. }
  752. return 0;
  753. }
  754. int
  755. apply_relocate (Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  756. unsigned int relsec, struct module *mod)
  757. {
  758. printk(KERN_ERR "module %s: REL relocs in section %u unsupported\n", mod->name, relsec);
  759. return -ENOEXEC;
  760. }
  761. /*
  762. * Modules contain a single unwind table which covers both the core and the init text
  763. * sections but since the two are not contiguous, we need to split this table up such that
  764. * we can register (and unregister) each "segment" separately. Fortunately, this sounds
  765. * more complicated than it really is.
  766. */
  767. static void
  768. register_unwind_table (struct module *mod)
  769. {
  770. struct unw_table_entry *start = (void *) mod->arch.unwind->sh_addr;
  771. struct unw_table_entry *end = start + mod->arch.unwind->sh_size / sizeof (*start);
  772. struct unw_table_entry tmp, *e1, *e2, *core, *init;
  773. unsigned long num_init = 0, num_core = 0;
  774. /* First, count how many init and core unwind-table entries there are. */
  775. for (e1 = start; e1 < end; ++e1)
  776. if (in_init(mod, e1->start_offset))
  777. ++num_init;
  778. else
  779. ++num_core;
  780. /*
  781. * Second, sort the table such that all unwind-table entries for the init and core
  782. * text sections are nicely separated. We do this with a stupid bubble sort
  783. * (unwind tables don't get ridiculously huge).
  784. */
  785. for (e1 = start; e1 < end; ++e1) {
  786. for (e2 = e1 + 1; e2 < end; ++e2) {
  787. if (e2->start_offset < e1->start_offset) {
  788. tmp = *e1;
  789. *e1 = *e2;
  790. *e2 = tmp;
  791. }
  792. }
  793. }
  794. /*
  795. * Third, locate the init and core segments in the unwind table:
  796. */
  797. if (in_init(mod, start->start_offset)) {
  798. init = start;
  799. core = start + num_init;
  800. } else {
  801. core = start;
  802. init = start + num_core;
  803. }
  804. DEBUGP("%s: name=%s, gp=%lx, num_init=%lu, num_core=%lu\n", __func__,
  805. mod->name, mod->arch.gp, num_init, num_core);
  806. /*
  807. * Fourth, register both tables (if not empty).
  808. */
  809. if (num_core > 0) {
  810. mod->arch.core_unw_table = unw_add_unwind_table(mod->name, 0, mod->arch.gp,
  811. core, core + num_core);
  812. DEBUGP("%s: core: handle=%p [%p-%p)\n", __func__,
  813. mod->arch.core_unw_table, core, core + num_core);
  814. }
  815. if (num_init > 0) {
  816. mod->arch.init_unw_table = unw_add_unwind_table(mod->name, 0, mod->arch.gp,
  817. init, init + num_init);
  818. DEBUGP("%s: init: handle=%p [%p-%p)\n", __func__,
  819. mod->arch.init_unw_table, init, init + num_init);
  820. }
  821. }
  822. int
  823. module_finalize (const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, struct module *mod)
  824. {
  825. DEBUGP("%s: init: entry=%p\n", __func__, mod->init);
  826. if (mod->arch.unwind)
  827. register_unwind_table(mod);
  828. #ifdef CONFIG_PARAVIRT
  829. if (mod->arch.paravirt_bundles) {
  830. struct paravirt_patch_site_bundle *start =
  831. (struct paravirt_patch_site_bundle *)
  832. mod->arch.paravirt_bundles->sh_addr;
  833. struct paravirt_patch_site_bundle *end =
  834. (struct paravirt_patch_site_bundle *)
  835. (mod->arch.paravirt_bundles->sh_addr +
  836. mod->arch.paravirt_bundles->sh_size);
  837. paravirt_patch_apply_bundle(start, end);
  838. }
  839. if (mod->arch.paravirt_insts) {
  840. struct paravirt_patch_site_inst *start =
  841. (struct paravirt_patch_site_inst *)
  842. mod->arch.paravirt_insts->sh_addr;
  843. struct paravirt_patch_site_inst *end =
  844. (struct paravirt_patch_site_inst *)
  845. (mod->arch.paravirt_insts->sh_addr +
  846. mod->arch.paravirt_insts->sh_size);
  847. paravirt_patch_apply_inst(start, end);
  848. }
  849. #endif
  850. return 0;
  851. }
  852. void
  853. module_arch_cleanup (struct module *mod)
  854. {
  855. if (mod->arch.init_unw_table)
  856. unw_remove_unwind_table(mod->arch.init_unw_table);
  857. if (mod->arch.core_unw_table)
  858. unw_remove_unwind_table(mod->arch.core_unw_table);
  859. }