module.c 26 KB

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