module.c 26 KB

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